diff --git a/glue/rexglue-sdk-main/CMakeLists.txt b/glue/rexglue-sdk-main/CMakeLists.txt index fef3fea0..ad3302f1 100644 --- a/glue/rexglue-sdk-main/CMakeLists.txt +++ b/glue/rexglue-sdk-main/CMakeLists.txt @@ -120,10 +120,10 @@ endif() if(NOT WIN32) # Use appropriate code model based on architecture - if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64" AND NOT APPLE) + # [ps5 port] PS5: the default (small) code model, as the PS5 linker script + # and signer expect. + if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64" AND NOT APPLE AND NOT PS5) # Large code model on Linux x86_64 for linking with very large recompiled executables (35MB+). - # macOS uses small: the ld64 linker on macOS 15+ rejects text relocations produced - # by -mcmodel=large, and macOS maps binaries in the low address space so ±2GB is sufficient. add_compile_options(-mcmodel=large) else() add_compile_options(-mcmodel=small) @@ -139,8 +139,13 @@ endif() # and FP-exception awareness, which blocks reordering and vectorization of the # renderer's float code; guest FP semantics live in the generated code, which # never had it. Contract off still forbids fusing into FMA. +# [ps5 port] 1.0.3 Test 2: the PS5 the same way (it had strict everywhere, +# the recompiled game's code included; its render and game threads are +# CPU-bound). if(ANDROID) add_compile_options(-ffp-contract=off $<$:-O3>) +elseif(PS5) + add_compile_options(-ffp-contract=off) else() add_compile_options(-ffp-model=strict) endif() @@ -181,6 +186,12 @@ elseif(UNIX AND NOT APPLE) set(REX_PLATFORM "linux-${REX_TARGET_PROCESSOR}") endif() add_compile_definitions(REX_PLATFORM_LINUX=1) + if(PS5) + # [ps5 port] PS5 homebrew: FreeBSD-based, shares the POSIX paths. + set(REX_PLATFORM "ps5-amd64") + add_compile_definitions(REX_PLATFORM_PS5=1) + add_compile_options("SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/include/rex/ps5_compat.h") + endif() elseif(APPLE) if(REX_TARGET_PROCESSOR MATCHES "AMD64|x86_64") set(REX_PLATFORM "mac-amd64") diff --git a/glue/rexglue-sdk-main/cmake/rexglue_fidelityfx.cmake b/glue/rexglue-sdk-main/cmake/rexglue_fidelityfx.cmake index c71aa2db..f975f798 100644 --- a/glue/rexglue-sdk-main/cmake/rexglue_fidelityfx.cmake +++ b/glue/rexglue-sdk-main/cmake/rexglue_fidelityfx.cmake @@ -21,6 +21,19 @@ function(_rexglue_ffx_write_if_changed path content) endfunction() # ── Dependency validation ──────────────────────────────────────────────── +if(PS5) + # [ps5 port] No Vulkan SDK on the PS5: the headers are the vendored ones and + # there is no loader to link (RADV is in the eboot; the few vk* functions the + # SDK calls directly are forwarded in ui/vulkan/vulkan_instance.cpp). An empty + # archive stands in for the loader library. + set(_rexglue_ffx_vulkan_stub "${CMAKE_BINARY_DIR}/ffx-ps5/libvulkan_none.a") + if(NOT EXISTS "${_rexglue_ffx_vulkan_stub}") + file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ffx-ps5") + file(WRITE "${_rexglue_ffx_vulkan_stub}" "!\n") + endif() + set(Vulkan_INCLUDE_DIR "${REXGLUE_ROOT}/thirdparty/vulkan-headers/include" CACHE PATH "" FORCE) + set(Vulkan_LIBRARY "${_rexglue_ffx_vulkan_stub}" CACHE FILEPATH "" FORCE) +endif() if(NOT WIN32) find_package(Vulkan QUIET) if(NOT Vulkan_FOUND) @@ -104,6 +117,21 @@ if(NOT WIN32) _rexglue_ffx_shader_commands "${_rexglue_ffx_shader_commands}") _rexglue_ffx_write_if_changed("${_rexglue_ffx_portable_source}/${_rexglue_ffx_shader_build}" "${_rexglue_ffx_shader_commands}") + if(PS5) + # [ps5 port] One static program: the API library is shared upstream. + # Overlay ffx-api/ (its CMakeLists.txt rewritten) in place of the link + # to the pinned checkout's. + set(_rexglue_ffx_api_dir "${_rexglue_ffx_portable_source}/ffx-api") + if(IS_SYMLINK "${_rexglue_ffx_api_dir}") + file(REMOVE "${_rexglue_ffx_api_dir}") + endif() + _rexglue_ffx_overlay("${fidelityfx_SOURCE_DIR}/ffx-api" "${_rexglue_ffx_api_dir}" "CMakeLists.txt") + file(READ "${fidelityfx_SOURCE_DIR}/ffx-api/CMakeLists.txt" _rexglue_ffx_api_cmake) + string(REPLACE "add_library(amd_fidelityfx_\${FFX_PLATFORM_NAME} SHARED" + "add_library(amd_fidelityfx_\${FFX_PLATFORM_NAME} STATIC" + _rexglue_ffx_api_cmake "${_rexglue_ffx_api_cmake}") + _rexglue_ffx_write_if_changed("${_rexglue_ffx_api_dir}/CMakeLists.txt" "${_rexglue_ffx_api_cmake}") + endif() set(fidelityfx_SOURCE_DIR "${_rexglue_ffx_portable_source}") endif() @@ -179,6 +207,97 @@ if(TARGET amd_fidelityfx_vk AND REXGLUE_FIDELITYFX_FRAME_GENERATION AND NOT WIN3 target_include_directories(amd_fidelityfx_vk PRIVATE "${fidelityfx_SOURCE_DIR}/ffx-api/src") endif() +if(TARGET ffx_backend_vk_x64 AND NOT WIN32) + # The Vulkan backend carves its scratch buffer into arrays at 4-byte + # offsets, but EffectContext is alignas(32): clang may store to it with + # aligned vector moves, which fault (the PS5 crashed this way creating the + # FSR 3 context). Lay every array out on 64-byte boundaries instead, with + # room for the first one's padding. + set(_rexglue_ffx_vk "${fidelityfx_SOURCE_DIR}/sdk/src/backends/vk/ffx_vk.cpp") + file(READ "${_rexglue_ffx_vk}" _rexglue_ffx_vk_source) + set(_rexglue_ffx_vk_patched "${_rexglue_ffx_vk_source}") + string(REPLACE "sizeof(uint32_t));" "64);" _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + string(REPLACE "uint8_t* pMem = (uint8_t*)((BackendContext_VK*)(backendContext + 1));" + "uint8_t* pMem = (uint8_t*)((reinterpret_cast(backendContext + 1) + 63) & ~uintptr_t(63));" + _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + string(REPLACE "return FFX_ALIGN_UP(sizeof(BackendContext_VK) + extensionPropArraySize" + "return FFX_ALIGN_UP(sizeof(BackendContext_VK) + 64 + extensionPropArraySize" + _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + string(FIND "${_rexglue_ffx_vk_patched}" "& ~uintptr_t(63)" _rexglue_ffx_vk_mapped) + string(FIND "${_rexglue_ffx_vk_patched}" "sizeof(BackendContext_VK) + 64 +" _rexglue_ffx_vk_sized) + string(FIND "${_rexglue_ffx_vk_patched}" "sizeof(uint32_t));" _rexglue_ffx_vk_left) + if(_rexglue_ffx_vk_mapped EQUAL -1 OR _rexglue_ffx_vk_sized EQUAL -1 OR NOT _rexglue_ffx_vk_left EQUAL -1) + message(FATAL_ERROR "FidelityFX: the Vulkan backend's scratch layout changed; update the alignment overlay") + endif() + if(PS5) + # [ps5 port] The SDK reports a failed Vulkan call only as "runtime + # error" by the time it reaches the API. Say which call it was + # (rexglue_ffx_report, in fsr_vulkan.cpp, logs the source line). + string(REPLACE "return FFX_ERROR_BACKEND_API_ERROR;" + "return FfxErrorCode(rexglue_ffx_report(__LINE__, FFX_ERROR_BACKEND_API_ERROR));" + _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + set(_rexglue_ffx_vk_patched + "extern \"C\" int rexglue_ffx_report(int line, int code);\n${_rexglue_ffx_vk_patched}") + # wchar_t is 2 bytes on the PS5, but its libc++ converts UTF-8 to + # 4-byte units (it only knows Windows has a short wchar_t): every + # shader binding name came out as its first letter, no binding + # matched, and creating FSR 3 failed. The names are ASCII. + set(_rexglue_ffx_vk_from " std::wstring_convert, wchar_t> converter;\n wcscpy_s(outputBuffer, outputLen, converter.from_bytes(inputName).c_str());") + set(_rexglue_ffx_vk_to " std::wstring_convert, wchar_t> converter;\n strcpy_s(outputBuffer, outputLen, converter.to_bytes(inputName).c_str());") + string(FIND "${_rexglue_ffx_vk_patched}" "${_rexglue_ffx_vk_from}" _rexglue_ffx_vk_from_at) + string(FIND "${_rexglue_ffx_vk_patched}" "${_rexglue_ffx_vk_to}" _rexglue_ffx_vk_to_at) + if(_rexglue_ffx_vk_from_at EQUAL -1 OR _rexglue_ffx_vk_to_at EQUAL -1) + message(FATAL_ERROR "FidelityFX: the Vulkan backend's name conversion changed; update the PS5 overlay") + endif() + string(REPLACE "${_rexglue_ffx_vk_from}" + " for (size_t i = 0; inputName && inputName[i] && i + 1 < outputLen; ++i)\n outputBuffer[i] = wchar_t(static_cast(inputName[i]));" + _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + string(REPLACE "${_rexglue_ffx_vk_to}" + " for (size_t i = 0; inputName && inputName[i] && i + 1 < outputLen; ++i)\n outputBuffer[i] = inputName[i] < 128 ? char(inputName[i]) : '?';" + _rexglue_ffx_vk_patched "${_rexglue_ffx_vk_patched}") + endif() + set(_rexglue_ffx_vk_overlay "${fidelityfx_BINARY_DIR}/ffx_vk_aligned.cpp") + _rexglue_ffx_write_if_changed("${_rexglue_ffx_vk_overlay}" "${_rexglue_ffx_vk_patched}") + get_target_property(_rexglue_ffx_vk_sources ffx_backend_vk_x64 SOURCES) + list(FILTER _rexglue_ffx_vk_sources EXCLUDE REGEX "(^|/)ffx_vk\\.cpp$") + list(APPEND _rexglue_ffx_vk_sources "${_rexglue_ffx_vk_overlay}") + set_property(TARGET ffx_backend_vk_x64 PROPERTY SOURCES "${_rexglue_ffx_vk_sources}") +endif() + +if(PS5) + # [ps5 port] Same for the effects FSR 3 is made of: every error return + # in their context code goes through rexglue_ffx_report with its line + # (plus base, which tells the files apart in the log). + function(_rexglue_ffx_report_errors target component base) + if(NOT TARGET ${target}) + return() + endif() + set(source "${fidelityfx_SOURCE_DIR}/sdk/src/components/${component}/ffx_${component}.cpp") + file(READ "${source}" text) + set(anchor "#include \"ffx_${component}_private.h\"") + string(FIND "${text}" "${anchor}" found) + if(found EQUAL -1) + message(FATAL_ERROR "FidelityFX: ${component} no longer includes its private header; update the report overlay") + endif() + string(REPLACE "${anchor}" "${anchor} +extern \"C\" int rexglue_ffx_report(int line, int code); +#undef FFX_RETURN_ON_ERROR +#define FFX_RETURN_ON_ERROR(x, y) if (!(x)) { return FfxErrorCode(rexglue_ffx_report(${base} + __LINE__, (y))); } +#undef FFX_VALIDATE +#define FFX_VALIDATE(x) { FfxErrorCode ret = x; FFX_RETURN_ON_ERROR(ret == FFX_OK, ret); }" text "${text}") + set(overlay "${fidelityfx_BINARY_DIR}/ffx_${component}_reported.cpp") + _rexglue_ffx_write_if_changed("${overlay}" "${text}") + get_target_property(sources ${target} SOURCES) + list(FILTER sources EXCLUDE REGEX "(^|/)ffx_${component}\\.cpp$") + list(APPEND sources "${overlay}") + set_property(TARGET ${target} PROPERTY SOURCES "${sources}") + target_include_directories(${target} PRIVATE "${fidelityfx_SOURCE_DIR}/sdk/src/components/${component}") + endfunction() + _rexglue_ffx_report_errors(ffx_fsr3upscaler_x64 fsr3upscaler 100000) + _rexglue_ffx_report_errors(ffx_frameinterpolation_x64 frameinterpolation 200000) + _rexglue_ffx_report_errors(ffx_opticalflow_x64 opticalflow 300000) +endif() + # The upstream FidelityFX targets expose source-tree include paths in # INTERFACE_INCLUDE_DIRECTORIES, which breaks our install export checks. # We only link against these targets internally, so no public includes are diff --git a/glue/rexglue-sdk-main/cmake/rexglue_helpers.cmake b/glue/rexglue-sdk-main/cmake/rexglue_helpers.cmake index 9ffeffdf..1f19e16d 100644 --- a/glue/rexglue-sdk-main/cmake/rexglue_helpers.cmake +++ b/glue/rexglue-sdk-main/cmake/rexglue_helpers.cmake @@ -151,13 +151,21 @@ function(rexglue_apply_target_settings target_name) if(UNIX AND NOT APPLE) # Large executable support - if(_rexglue_target_processor MATCHES "x86_64|AMD64") + if(PS5) + # [ps5 port] default code model (see the top-level CMakeLists.txt). + elseif(_rexglue_target_processor MATCHES "x86_64|AMD64") target_link_options(${target_name} PRIVATE -Wl,--no-relax) target_compile_options(${target_name} PRIVATE -mcmodel=large) elseif(_rexglue_target_processor MATCHES "aarch64|ARM64|arm64" AND NOT ANDROID) # Android builds pass their own -march (armv8.2-a: inline LSE atomics); # appending armv8-a here would override it for the recompiled code. target_compile_options(${target_name} PRIVATE -march=armv8-a) + # [svr2011] REXGLUE_ARM64_TUNE=: instruction scheduling for that + # core (the same instructions run everywhere) - e.g. cortex-a53 for + # in-order little cores. + if(REXGLUE_ARM64_TUNE) + target_compile_options(${target_name} PRIVATE -mtune=${REXGLUE_ARM64_TUNE}) + endif() endif() endif() diff --git a/glue/rexglue-sdk-main/gta4-recomp/CMakeLists.txt b/glue/rexglue-sdk-main/gta4-recomp/CMakeLists.txt index 3b033069..97da2aa8 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/CMakeLists.txt +++ b/glue/rexglue-sdk-main/gta4-recomp/CMakeLists.txt @@ -22,6 +22,7 @@ set(GTA4_APP_SOURCES src/gta4_presentation_hooks.cpp src/gta4_aspect_hooks.cpp src/gta4_quicksave_hooks.cpp + src/gta4_camera_hooks.cpp src/gta4_input_hooks.cpp src/gta4_sony_feedback.cpp src/gta4_sony_event_hooks.cpp @@ -443,6 +444,122 @@ elseif(ANDROID) -Wno-backend-plugin) target_link_options(LibertyRecomp PRIVATE "-fprofile-use=${LIBERTY_ANDROID_PGO}") endif() +elseif(PS5) + # [ps5 port] One static PS5 program: the game, the runtime and the + # gta4-native renderer (no GPU plugin files). Linked and packaged by + # ps5/tools/ps5_package.sh with the PS5 toolchain's RADV. + set(GTA4_PS5_SOURCES ${GTA4_APP_SOURCES}) + list(REMOVE_ITEM GTA4_PS5_SOURCES src/network/community_multiplayer.cpp) + + # The hub (the screen the app opens on; fonts built in, SIL Open Font + # License), trophies, the frame rate counter and watermark, 360 save import. + set(GTA4_PS5_DIR "${LIBERTY_RECOMP_WORKSPACE_ROOT}/ps5") + file(STRINGS "${GTA4_PS5_DIR}/VERSION.txt" GTA4_PS5_VERSION LIMIT_COUNT 1) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${GTA4_PS5_DIR}/VERSION.txt") + list(APPEND GTA4_PS5_SOURCES + src/ps5/gta4_ps5_app.cpp + src/ps5/hub/hub_ui.cpp + src/ps5/hub/hub_install.cpp + src/ps5/hub/hub_installer_page.cpp + src/ps5/hub/hub_lang.cpp + src/ps5/hub/hub_sfx.cpp + src/ps5/hub/hub_trophies.cpp + src/ps5/hub/hub_trophies_page.cpp + src/ps5/hub/ps5_hub.cpp + src/ps5/game/sound.cpp + src/ps5/game/trophies_game.cpp + src/ps5/game/ps5_display.cpp + src/ps5/game/ps5_overlay.cpp + src/ps5/game/save_import.cpp + src/ps5/game/memory_watch.cpp + src/ps5/trophies_native/native_trophies.cpp + # RecompTrophy's (GPL-3.0, by lamarking, used with permission): the + # game's PS5 trophy set; its payloads are ps5/payload/trophies + src/ps5/trophies_native/install.cpp + src/ps5/trophies_native/trophyset.cpp) + # (the fonts are built into it: a changed one rebuilds it) + file(GLOB GTA4_PS5_FONTS CONFIGURE_DEPENDS "${GTA4_PS5_DIR}/assets/fonts/*.ttf" + "${GTA4_PS5_DIR}/assets/fonts/*.otf") + # (Helvetica Neue Heavy, the episodes' tabs, only when it's in fonts/: its + # licence doesn't let it be shared, so the public repository goes without + # and those tabs use the menu type) + set(GTA4_PS5_TAB_FONT 0) + if(EXISTS "${GTA4_PS5_DIR}/assets/fonts/HelveticaNeue-Heavy.ttf") + set(GTA4_PS5_TAB_FONT 1) + endif() + set_source_files_properties(src/ps5/hub/ps5_hub.cpp PROPERTIES + COMPILE_DEFINITIONS "GTA4PS5_FONTS_DIR=\"${GTA4_PS5_DIR}/assets/fonts\";GTA4PS5_TAB_FONT=${GTA4_PS5_TAB_FONT}" + OBJECT_DEPENDS "${GTA4_PS5_FONTS}") + message(STATUS "GTA IV for PS5 ${GTA4_PS5_VERSION}") + + # The title's wait for a free frame slot sleeps instead of spinning a core + # (written for Android, nothing Android-specific in it). + add_executable(gta4 + ${GTA4_PS5_SOURCES} + src/android/android_platform_stubs.cpp + src/android/android_frame_wait_hook.cpp + ${GENERATED_SOURCES} + ) + set_property(SOURCE generated/gta4_register.cpp APPEND PROPERTY COMPILE_OPTIONS + "$<$:-O0>") + # The recompiled game's own files: mfmsr/mtmsrd without a hardware fence + # (generated/gta4_init.h). + set(GTA4_PS5_GUEST_SOURCES ${GENERATED_SOURCES}) + list(FILTER GTA4_PS5_GUEST_SOURCES INCLUDE REGEX "gta4_recomp\\.[0-9]+\\.cpp$") + set_property(SOURCE ${GTA4_PS5_GUEST_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS REX_PS5_GUEST_CODE=1) + # vmaddfp/vnmsubfp fused (one rounding, as the console's VMX does; one FMA + # instruction on the PS5's CPU instead of two or three), as on Android. + target_compile_definitions(gta4 PRIVATE REX_FUSED_VMADDFP=1) + target_include_directories(gta4 PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src + ${CMAKE_CURRENT_SOURCE_DIR}/generated + ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/crypto + ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/imgui + ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/inja/third_party/include + ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/stb + ) + set_property(SOURCE src/gta4_native_hooks.cpp APPEND PROPERTY INCLUDE_DIRECTORIES + "${CMAKE_CURRENT_SOURCE_DIR}/../src") + set_source_files_properties(src/gta4_sony_feedback.cpp src/gta4_sony_event_hooks.cpp + PROPERTIES COMPILE_DEFINITIONS GTA4_SONY_PRIMARY_PLAYER_ALIAS) + # (the version only where it is shown: on the whole target, a version + # change rebuilt every recompiled file) + set_property(SOURCE src/ps5/hub/ps5_hub.cpp src/ps5/game/ps5_overlay.cpp src/ps5/game/ps5_display.cpp APPEND PROPERTY + COMPILE_DEFINITIONS GTA4PS5_VERSION="${GTA4_PS5_VERSION}") + target_compile_definitions(gta4 PRIVATE + GTA4_RECOMP_ASSET_XEX="/app0/assets/default_v8.xex" + GTA4_BUTTON_PROMPT_ASSET_ROOT="/app0/Resources/button_prompts" + GTA4_RPF_AES_KEY_SOURCE="/app0/Resources/aes_key.bin" + ) + target_compile_options(gta4 PRIVATE "SHELL:-include ${REXSDK_DIR}/include/rex/ps5_compat.h") + target_link_libraries(gta4 PRIVATE + rex::runtime + rexgpu-gta4-native + xxHash::xxhash + SDL3::SDL3 + utf8cpp + SceUserService ScePad SceAudioOut SceSystemService SceVideoOut + ) + # The TV's refresh rate and frame pacing (src/ps5/game/ps5_display.cpp): + # RADV's VideoOut calls go through it. ps5_package.sh's link wraps them too. + target_link_options(gta4 PRIVATE + "LINKER:--wrap=sceVideoOutIsOutputSupported,--wrap=sceVideoOutConfigureOutput,--wrap=sceVideoOutSetFlipRate") + rexglue_configure_target(gta4) + # With ThinLTO the program is linked (and its code generated) once, by + # ps5_package.sh; CMake's own link only names the output. + if(GTA4_PS5_THINLTO) + set_property(TARGET gta4 PROPERTY RULE_LAUNCH_LINK "${GTA4_PS5_DIR}/tools/ps5_skip_link.sh") + endif() + # zlib for the installer's RPF extraction: the PS5 toolchain's static build. + set(PS5_ZLIB_ROOT "${PS5_PAYLOAD_SDK}/../zlib/root/usr" CACHE PATH "PS5 zlib (include/, lib/libz.a)") + target_include_directories(gta4 PRIVATE "${PS5_ZLIB_ROOT}/include") + target_link_libraries(gta4 PRIVATE "${PS5_ZLIB_ROOT}/lib/libz.a") + # FSR 3 (with REXGLUE_ENABLE_FIDELITYFX): the motion-vector shader archive + # next to the binary for ps5_package.sh, and the menus' FSR 3 entries. + if(COMMAND liberty_stage_vulkan_temporal_archive AND TARGET rexglue_fidelityfx_vulkan) + liberty_stage_vulkan_temporal_archive(gta4) + set_property(SOURCE src/ps5/hub/hub_ui.cpp APPEND PROPERTY COMPILE_DEFINITIONS LIBERTY_HAS_FSR3=1) + endif() else() add_library(gta4_recomp INTERFACE) target_include_directories(gta4_recomp INTERFACE diff --git a/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_init.h b/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_init.h index 42ddaa34..e58fd9d5 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_init.h +++ b/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_init.h @@ -114,8 +114,16 @@ extern PPCFuncMapping PPCFuncMappings[]; #if defined(REX_FUSED_VMADDFP) #include #define REX_VMADDFP(a, b, c) simde_mm_fmadd_ps((a), (b), (c)) +#if defined(__FMA__) +// [ps5 port] -(a*b - c) in one rounding: x86's fused multiply-subtract with +// the sign flipped (c - a*b gives +0 where the console gives -0, a*b == c). +#define REX_VNMSUBFP(a, b, c) \ + simde_mm_xor_ps(simde_mm_fmsub_ps((a), (b), (c)), \ + simde_mm_castsi128_ps(simde_mm_set1_epi32(int(0x80000000)))) +#else // c - a*b in one rounding (fmls); simde's fmsub is not fused. #define REX_VNMSUBFP(a, b, c) simde_mm_fnmadd_ps((a), (b), (c)) +#endif #else #define REX_VMADDFP(a, b, c) simde_mm_add_ps(simde_mm_mul_ps((a), (b)), (c)) #define REX_VNMSUBFP(a, b, c) \ @@ -146,7 +154,7 @@ extern PPCFuncMapping PPCFuncMappings[]; // Memory Access //============================================================================= -#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC +#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC || REX_PLATFORM_PS5 #define REX_PHYS_HOST_OFFSET(addr) (((u32)(addr) >= 0xE0000000u) ? 0x1000u : 0u) #else #define REX_PHYS_HOST_OFFSET(addr) 0u @@ -370,28 +378,38 @@ inline void ppc_trap(PPCContext& ctx, u8* base, u16 trap_type) { // Interrupt Locks //============================================================================= -// Interrupt-disabled depth of the calling thread. The count under the global -// lock is non-zero exactly when the caller holds the lock, so mfmsr reads this -// instead of taking the lock: guest sections are a few instructions -// (mfmsr/mtmsrd around lwarx/stwcx.), and the extra lock round trip doubled -// their contention. -inline thread_local i32 ppc_global_lock_depth_ = 0; - -#define REX_CHECK_GLOBAL_LOCK() (ppc_global_lock_depth_ ? u64{0} : u64{0x8000}) +// Interrupt-disabled depth of the calling thread (ctx.global_lock_depth, in +// the guest thread's context: a thread_local cost a system lookup on the PS5). +// The count under the global lock is non-zero exactly when the caller holds the +// lock, so mfmsr reads this instead of taking the lock: guest sections are a +// few instructions (mfmsr/mtmsrd around lwarx/stwcx.), and the extra lock round +// trip doubled their contention. +#define REX_CHECK_GLOBAL_LOCK() (ctx.global_lock_depth ? u64{0} : u64{0x8000}) #define REX_ENTER_GLOBAL_LOCK() \ do { \ rex::thread::global_critical_region::LockSpinning(); \ - ++ppc_global_lock_depth_; \ + ++ctx.global_lock_depth; \ } while (0) -#define REX_LEAVE_GLOBAL_LOCK() \ - do { \ - assert(ppc_global_lock_depth_ >= 1 && "LeaveGlobalLock called without EnterGlobalLock"); \ - --ppc_global_lock_depth_; \ - rex::thread::global_critical_region::mutex().unlock(); \ +#define REX_LEAVE_GLOBAL_LOCK() \ + do { \ + assert(ctx.global_lock_depth >= 1 && "LeaveGlobalLock called without EnterGlobalLock"); \ + --ctx.global_lock_depth; \ + rex::thread::global_critical_region::mutex().unlock(); \ } while (0) +#if defined(__PROSPERO__) && defined(REX_PS5_GUEST_CODE) +// [ps5 port] mfmsr and mtmsrd are the only instructions recompiled with a +// std::atomic_thread_fence, and each sits next to this thread's own lock depth +// or the global lock's locked compare-exchange (or the guest's stwcx. one), +// full barriers on x86 already: the mfence (three per guest interlocked +// operation) ordered nothing more. A compiler barrier keeps the code's order. +// Only in the recompiled game's own files (CMakeLists.txt), after every +// header they include. +#define atomic_thread_fence atomic_signal_fence +#endif + //============================================================================= // Function Declarations //============================================================================= diff --git a/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_recomp.38.cpp b/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_recomp.38.cpp index 82a5e9dd..0e64ea24 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_recomp.38.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/generated/gta4_recomp.38.cpp @@ -1,5 +1,8 @@ #include "gta4_init.h" +// [ps5 port] gta4_camera_hooks.cpp +double GTA4_VehicleCameraReturn(double step); + DEFINE_REX_FUNC(sub_82538948) { REX_FUNC_PROLOGUE(); PPCRegister ctr{}; @@ -7460,6 +7463,8 @@ loc_8253B6E8: f27.f64 = ctx.f13.f64 >= 0.0 ? ctx.f0.f64 : f23.f64; // fdivs f30,f12,f27 f30.f64 = double(float(ctx.f12.f64 / f27.f64)); + // [ps5 port] the car camera's return behind the car, up and down (gta4_camera_hooks.cpp) + f30.f64 = GTA4_VehicleCameraReturn(f30.f64); // bl 0x82a02648 ctx.lr = 0x8253B70C; { ctx.v0 = v0; ctx.v7 = v7; ctx.v8 = v8; ctx.v9 = v9; ctx.v10 = v10; ctx.v11 = v11; ctx.v12 = v12; ctx.v13 = v13; sub_82A02648(ctx, base); v0 = ctx.v0; v7 = ctx.v7; v8 = ctx.v8; v9 = ctx.v9; v10 = ctx.v10; v11 = ctx.v11; v12 = ctx.v12; v13 = ctx.v13; } @@ -7759,6 +7764,8 @@ loc_8253B8CC: cr6.compare(r22.u32, 0, xer); // fdivs f31,f0,f27 f31.f64 = double(float(ctx.f0.f64 / f27.f64)); + // [ps5 port] and sideways + f31.f64 = GTA4_VehicleCameraReturn(f31.f64); // beq cr6,0x8253b9e0 if (cr6.eq) goto loc_8253B9E0; // lbz r11,-27534(r26) diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/android/android_frame_wait_hook.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/android/android_frame_wait_hook.cpp index 380838a7..1697730e 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/android/android_frame_wait_hook.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/android/android_frame_wait_hook.cpp @@ -15,6 +15,9 @@ #include #include "gta4_init.h" +#if REX_PLATFORM_PS5 +#include +#endif // No frame rate change, but the spin held the main game thread at ~97% of a // big core while it waited (17% with the sleep): the whole process used 2.16 @@ -41,6 +44,12 @@ extern "C" void sub_82A46D70(PPCContext& ctx, uint8_t* base) { } if (pending >= 2 && REXCVAR_GET(gta4_frame_wait_sleep)) { const timespec delay{0, 250 * 1000}; +#if REX_PLATFORM_PS5 + const int64_t begin_ns = rex::frame_stats::NowNs(); nanosleep(&delay, nullptr); + rex::frame_stats::NoteTitleSlotWait(uint64_t(rex::frame_stats::NowNs() - begin_ns) / 1000); +#else + nanosleep(&delay, nullptr); +#endif } } diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.cpp index 68d694b9..432a38a9 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.cpp @@ -1,4 +1,7 @@ #include "gta4_app.h" +#if REX_PLATFORM_PS5 +#include "ps5/gta4_ps5_app.h" +#endif #include "gta4_hack_registry.h" #include "gta4_present_mode_policy.h" #include "gta4_streaming_hooks.h" @@ -28,6 +31,10 @@ #include #include #include +#include +#if REX_PLATFORM_PS5 +REXCVAR_DECLARE(std::string, ps5_frame_generation); +#endif #include "achievement_bridge_gc.h" #include "gta4_frontend_hooks.h" @@ -84,6 +91,13 @@ REXCVAR_DEFINE_STRING(gta4_episode_startup_prompt, "retail", "GTA IV/Episodes", // without failing. Both aliases now preserve the retail one-time prompt. .allowed({"retail", "new_only", "always", "off"}) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); +// The PS5 hub's Home choice. -1 leaves it to the game (GTA IV, and the retail +// question once for a new episode); 0 starts GTA IV without the question; 1 +// The Lost and Damned, 2 The Ballad of Gay Tony, as that question's Yes does. +REXCVAR_DEFINE_INT32(gta4_start_episode, -1, "GTA IV/Episodes", + "Start in: -1 the game's choice, 0 GTA IV, 1 The Lost and Damned, 2 The Ballad of Gay Tony") + .range(-1, 2) + .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); REXCVAR_DEFINE_BOOL(install, false, "GTA IV/Installation", "Run the full game installation wizard") .lifecycle(rex::cvar::Lifecycle::kInitOnly); @@ -115,7 +129,7 @@ REXCVAR_DEFINE_STRING(gta4_aspect_ratio, "auto", "GTA IV/Graphics/Display", REXCVAR_DEFINE_STRING(gta4_present_mode, "vsync", "GTA IV/Graphics/Display", "Vertical sync: vsync is On; immediate is Off; auto/mailbox are legacy modes") .allowed({"auto", "vsync", "fifo", "mailbox", "immediate"}); -// Android: 30 FPS with VSync divides the 60 Hz panel evenly. +// Android: 30 FPS with VSync divides the 60 Hz panel evenly. [ps5 port] PS5: 60. REXCVAR_DEFINE_UINT32(gta4_frame_limit, REX_PLATFORM_ANDROID ? 30 : 60, "GTA IV/Graphics/Display", "Maximum game and host presentation rate: 0 is unlocked; caps are 30, 40, 60, and " "120 FPS") @@ -150,7 +164,7 @@ REXCVAR_DEFINE_UINT32(gta4_shadow_map_base_size, REX_PLATFORM_ANDROID ? 256 : 51 REXCVAR_DEFINE_DOUBLE(gta4_shadow_distance_guard_factor, 1.0, "GTA IV/Graphics/Shadows", "Multiplier on the shadow distance owned by the 30 FPS guard") .range(0.1, 1.0); -REXCVAR_DEFINE_DOUBLE(gta4_shadow_distance_scale, 2.0, "GTA IV/Graphics/Shadows", +REXCVAR_DEFINE_DOUBLE(gta4_shadow_distance_scale, REX_PLATFORM_PS5 ? 1.0 : 2.0, "GTA IV/Graphics/Shadows", "Multiplier applied to GTA IV's directional shadow range") .range(0.1, 4.0) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); @@ -188,7 +202,7 @@ REXCVAR_DEFINE_STRING(gta4_reflection_capture_distance, "original", "Exterior environment capture distance: near (20), original (40), extended (60), or " "far (80)") .allowed({"near", "original", "extended", "far"}); -REXCVAR_DEFINE_STRING(gta4_native_anti_aliasing, REX_PLATFORM_ANDROID ? "fxaa" : "smaa", +REXCVAR_DEFINE_STRING(gta4_native_anti_aliasing, REX_PLATFORM_ANDROID || REX_PLATFORM_PS5 ? "fxaa" : "smaa", "GTA IV/Graphics/Anti-Aliasing", "Anti-aliasing: off, fxaa, smaa, deferred MSAA, or even-factor SSAA") // `spatial` remains loadable as a deprecated compatibility alias. It is @@ -238,7 +252,7 @@ REXCVAR_DEFINE_DOUBLE(gta4_dynamic_draw_distance_min, 0.7, "GTA IV/Graphics/LOD" REXCVAR_DEFINE_DOUBLE(gta4_draw_distance_dynamic_factor, 1.0, "GTA IV/Graphics/LOD", "Current dynamic draw-distance factor (managed automatically)") .range(0.25, 1.0); -REXCVAR_DEFINE_DOUBLE(gta4_draw_distance_scale, REX_PLATFORM_ANDROID ? 1.0 : 3.0, +REXCVAR_DEFINE_DOUBLE(gta4_draw_distance_scale, REX_PLATFORM_ANDROID || REX_PLATFORM_PS5 ? 1.0 : 3.0, "GTA IV/Graphics/LOD", "Multiplier applied through GTA IV's built-in world-distance input") .range(0.25, 4.0); @@ -629,6 +643,20 @@ struct GTA4App::TitleProfileSyncState { std::optional GTA4App::OnFinalizePaths( const rex::PathConfig& defaults, std::function resume) { +#if REX_PLATFORM_PS5 + // [ps5 port] The hub first; START GAME carries on with the start. + gta4ps5::ShowHub(imgui_drawer(), immediate_drawer(), defaults, liberty_root_, native_config_path_, + [this, defaults, resume](void) { + if (auto paths = ContinueStartup(defaults, resume)) resume(std::move(*paths)); + }); + return std::nullopt; +#else + return ContinueStartup(defaults, std::move(resume)); +#endif +} + +std::optional GTA4App::ContinueStartup( + const rex::PathConfig& defaults, std::function resume) { auto paths = defaults; paths.game_data_root = liberty_root_ / "game"; paths.marketplace_content_root = liberty_root_ / "dlc"; @@ -655,6 +683,25 @@ std::optional GTA4App::OnFinalizePaths( } } +#if REX_PLATFORM_PS5 + // [ps5 port] The hub's installer sets the game up (START GAME only gets + // here once it's installed); the PC installer dialog isn't used. + (void)force_install, (void)force_dlc; + if (!ready) { + REXLOG_WARN("GTA IV installation is not launch-ready: {} (back to the hub)", readiness_error); + OnFinalizePaths(defaults, std::move(resume)); + return std::nullopt; + } + { + std::error_code error; + const auto update_root = paths.game_data_root / "update"; + if (std::filesystem::is_directory(update_root, error)) { + paths.update_data_root = update_root; + } + return paths; + } +#endif + if (ready && !force_install && !force_dlc) { std::error_code error; const auto update_root = paths.game_data_root / "update"; @@ -681,7 +728,9 @@ std::optional GTA4App::OnFinalizePaths( resume(std::move(paths)); }, [this]() { app_context().QuitFromUIThread(); }); - install_dialog->Preselect(REXCVAR_GET(install_game_source), REXCVAR_GET(install_update_source)); + std::string game_source = REXCVAR_GET(install_game_source); + std::string update_source = REXCVAR_GET(install_update_source); + install_dialog->Preselect(game_source, update_source); return std::nullopt; } @@ -704,6 +753,18 @@ void GTA4App::OnPreSetup(rex::RuntimeConfig& config) { REXCVAR_GET(gta4_native_upscaler)); REXCVAR_SET(gta4_native_upscaler, "native"); } +#if REX_PLATFORM_PS5 + // [ps5 port] Frame generation is off on the PS5 (it made no frames: every + // frame short of motion vectors), and the in-game menu's graphics rows are + // gone (the hub has the PS5's own): anything they set before goes back to + // default. + REXCVAR_SET(ps5_frame_generation, "off"); + REXCVAR_SET(gta4_native_frame_generation, "off"); + for (const char* name : {"gta4_native_upscaler", "gta4_fsr1_quality", "gta4_present_mode", "gta4_aspect_ratio", + "gta4_native_hdr_mode", "gta4_native_hdr_paper_white_nits", "gta4_native_hdr_peak_nits", + "gta4_texture_filtering", "gta4_reflection_aa", "gta4_native_output_dither"}) + rex::cvar::ResetToDefault(name); +#endif if (REXCVAR_GET(gta4_native_frame_generation) != "off") { if (config.gpu_plugin != "gta4-native") { REXLOG_WARN("{} frame generation requires the native Vulkan renderer; disabled for this launch", @@ -1020,6 +1081,9 @@ void StartLiveCvarWatcher(std::filesystem::path path) { #endif void GTA4App::OnPostSetup() { +#if REX_PLATFORM_PS5 + gta4ps5::OnGameSetUp(imgui_drawer(), runtime(), user_data_root()); +#endif #if REX_PLATFORM_ANDROID StartLiveCvarWatcher(rex::filesystem::GetUserFolder() / "live_cvars.txt"); StartHotThreadPinning(); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.h b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.h index 22adcbd5..7c5f2215 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.h +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_app.h @@ -45,6 +45,15 @@ class GTA4App final : public rex::ReXApp { const rex::PathConfig& defaults, std::function resume) override; void OnPostSetup() override; +#if REX_PLATFORM_PS5 + // [ps5 port] the hub's fonts, the trophy popup (ps5/gta4_ps5_app.cpp) + void OnConfigureFonts(ImFontAtlas* atlas) override; + std::unique_ptr CreateAchievementNotificationDialog() override; +#endif + // The start after the paths are known: the installer when the game isn't + // installed (OnFinalizePaths, and the PS5 hub's START GAME). + std::optional ContinueStartup(const rex::PathConfig& defaults, + std::function resume); void OnCreateDialogs(rex::ui::ImGuiDrawer* drawer) override; bool RequiresSynchronizedInitialThreadResume() const override; void OnShutdown() override; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_aspect_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_aspect_hooks.cpp index 2e8c1f8e..5da66777 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_aspect_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_aspect_hooks.cpp @@ -31,6 +31,11 @@ struct OutputState { }; std::mutex output_mutex; OutputState output_state; +// [ps5 port] Bumped (under output_mutex) whenever output_state changes, so +// Output() can hand back a per-thread copy without taking the lock. It is +// read on every draw command; the lock showed up in the PS5 profiles. +std::atomic output_version{1}; +std::atomic output_ready{false}; thread_local UiContext ui_context; thread_local unsigned baked_font_depth = 0; thread_local unsigned append_depth = 0; @@ -56,7 +61,7 @@ bool Span(uint8_t* base, uint32_t address, size_t size, bool write = false) { const uint32_t last = uint32_t(uint64_t(address) + size - 1); if (!heap || heap != memory->LookupHeap(last)) return false; - const auto access = heap->QueryRangeAccess(address, last); + const auto access = heap->QueryRangeAccessUnlocked(address, last); using rex::memory::PageAccess; return access == PageAccess::kReadWrite || access == PageAccess::kExecuteReadWrite || (!write && (access == PageAccess::kReadOnly || access == PageAccess::kExecuteReadOnly)); @@ -77,8 +82,19 @@ void Float(uint8_t* base, uint32_t address, double value) { Write(base, address, std::bit_cast(float(value))); } OutputState Output() { + // (one thread_local: on the PS5 each is a lookup through the system) + struct Cached { + OutputState state; + uint64_t version = 0; + }; + thread_local Cached cached; + Cached& c = cached; + if (output_version.load(std::memory_order_acquire) == c.version) + return c.state; std::lock_guard lock(output_mutex); - return output_state; + c.state = output_state; + c.version = output_version.load(std::memory_order_relaxed); + return c.state; } bool Trace() { return REXCVAR_GET(gta4_trace_aspect) && @@ -175,6 +191,23 @@ std::unordered_map dc_layouts; std::deque> dc_order; uint64_t next_dc_serial = 0; constexpr size_t kMaximumDcLayouts = 32768; +// [ps5 port] How many dc_layouts entries fall in each bucket of command +// addresses, changed only under dc_mutex. FinalizeDc runs for every draw +// command the game makes (world ones too) and used to lock and search the map +// each time; a command that is not a UI one and whose bucket is empty cannot +// be in the map, so it skips both. (32768 entries at most: no overflow.) +constexpr uint32_t kDcBuckets = 1u << 16; +std::atomic dc_bucket_count[kDcBuckets]; +uint32_t DcBucket(uint32_t dc) { + return ((dc >> 2) * 2654435761u) >> 16; +} +void EraseDcLayout(std::unordered_map::iterator it) { + dc_bucket_count[DcBucket(it->first)].fetch_sub(1, std::memory_order_relaxed); + dc_layouts.erase(it); +} +bool MaybeDcLayout(uint32_t dc) { + return dc_bucket_count[DcBucket(dc)].load(std::memory_order_acquire) != 0; +} bool LayoutDc(uint32_t vtable) { return HasUiDrawExecutor(vtable); @@ -184,6 +217,8 @@ UiContext DcContext(PPCContext& ctx, uint8_t* base) { const uint32_t dc = ctx.r3.u32; if (!Span(base, dc, 8)) return ui_context; + if (!MaybeDcLayout(dc)) + return ui_context; const uint32_t vtable = Read(base, dc), token = StableDcToken(Read(base, dc + 4)); std::lock_guard lock(dc_mutex); const auto it = dc_layouts.find(dc); @@ -276,6 +311,10 @@ void Publish(Extent render, Extent output) { render.width != output_state.render.width || render.height != output_state.render.height; output_state = {render, output, true, output_state.generation + uint64_t(changed)}; + if (changed) { + output_version.fetch_add(1, std::memory_order_release); + output_ready.store(true, std::memory_order_release); + } } if (changed) { const auto layout = Layout(output); @@ -373,23 +412,28 @@ Scope::~Scope() { DcScope::DcScope(PPCContext& ctx, uint8_t* base) : scope_(DcContext(ctx, base)) {} void FinalizeDc(uint8_t* base, uint32_t dc) { - if (!Output().ready || !Span(base, dc, 8)) + if (!output_ready.load(std::memory_order_acquire) || !Span(base, dc, 8)) return; const uint32_t vtable = Read(base, dc), token = StableDcToken(Read(base, dc + 4)); + const bool keep = LayoutDc(vtable) && ui_context.active; + if (!keep && !MaybeDcLayout(dc)) + return; std::lock_guard lock(dc_mutex); // Invalidate on every publication, including reuse by a non-UI command. - dc_layouts.erase(dc); - if (!LayoutDc(vtable) || !ui_context.active) + if (const auto it = dc_layouts.find(dc); it != dc_layouts.end()) + EraseDcLayout(it); + if (!keep) return; const uint64_t serial = ++next_dc_serial; dc_layouts.emplace(dc, DcLayout{token, vtable, serial, ui_context}); + dc_bucket_count[DcBucket(dc)].fetch_add(1, std::memory_order_release); dc_order.emplace_back(dc, serial); // Both containers are bounded. Token + vtable prevents address-reuse contamination. for (; dc_order.size() > kMaximumDcLayouts; dc_order.pop_front()) { const auto [old_dc, old_serial] = dc_order.front(); const auto it = dc_layouts.find(old_dc); if (it != dc_layouts.end() && it->second.serial == old_serial) - dc_layouts.erase(it); + EraseDcLayout(it); } } void PrepareViewport(PPCContext& ctx, uint8_t* base) { diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_bulb_source_trace.inc b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_bulb_source_trace.inc index 3f66709a..6b1417af 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_bulb_source_trace.inc +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_bulb_source_trace.inc @@ -34,7 +34,7 @@ uint32_t BulbSourceGuestFrame(uint8_t* base) { } bool BulbSourceObservationActive(uint8_t* base) { const auto& config = BulbSourceObserverOptions(); - if (!config.enabled || !IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kLogging)) return false; + if (!config.enabled || !IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kLogging)) return false; std::error_code error; if (!config.arm.empty() && (!std::filesystem::is_regular_file(config.arm, error) || error)) return false; const auto frame = BulbSourceGuestFrame(base); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_camera_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_camera_hooks.cpp new file mode 100644 index 00000000..84b66eeb --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_camera_hooks.cpp @@ -0,0 +1,93 @@ +// GTA IV - the car camera's sideways offset, as an option (gta4_centered_vehicle_camera). +// +// The chase camera behind a car doesn't sit straight behind it: sub_82535D98, +// called by the car camera's update (sub_8253AB20, CCamFollowVehicle), gives +// an offset half a metre to the side in the vehicle's own space (0x82AAAFB0: +// -0.5, 0, 0; 0x82AAAFC0 the same for The Ballad of Gay Tony's case), turned +// with the vehicle, while the byte at 0x82AAADF3 is set (1 in the game's +// image; the camera setup, sub_822148A0, may clear it). That byte is the PC +// version's CCamFollowVehicle::ms_bAllowSideOffset, which FusionFix's +// Centered Vehicle Camera clears. Centred, it's off for the call: the offset +// is zero and the camera sits right behind. Off, the game's own way. +// +// The same update swings the camera back behind the car each frame by a step +// (the turn still to go, divided by the frame's time step): sideways at +// 0x8253B8DC, up and down at 0x8253B704. FusionFix hooks the same two +// divisions on PC for its "centering delay" and "turn speed" options, and +// GTA4_VehicleCameraReturn (called right after each, by a line added to the +// recompiled code) does what it does: no step for a while after the right +// stick was last moved, and a bigger step for a faster swing. +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "gta4_init.h" + +REXCVAR_DEFINE_BOOL(gta4_centered_vehicle_camera, false, "GTA IV/Camera", + "Car camera straight behind the vehicle, without the game's sideways offset"); +REXCVAR_DEFINE_UINT32(gta4_vehicle_camera_return_delay, 0, "GTA IV/Camera", + "Milliseconds the car camera waits after the right stick before it swings " + "back behind the car (0 is the game's)") + .range(0, 5000); +REXCVAR_DEFINE_UINT32(gta4_vehicle_camera_turn_speed, 0, "GTA IV/Camera", + "How much faster the car camera swings back behind the car: the step is " + "divided by 1 - 0.1 x this, as in FusionFix (0 is the game's)") + .range(0, 7); + +namespace { +constexpr uint32_t kAllowSideOffset = 0x82AAADF3; +// The right stick's dead zone, XInput's. +constexpr int kRightStickDeadZone = 8689; + +std::atomic g_return_after_ms{0}; + +int64_t NowMs() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); +} + +bool RightStickMoved() { + auto* runtime = rex::Runtime::instance(); + auto* input = runtime ? static_cast(runtime->input_system()) : nullptr; + rex::input::X_INPUT_STATE state{}; + if (!input || !input->TryGetLastState(0, &state)) return false; + const int x = int16_t(state.gamepad.thumb_rx), y = int16_t(state.gamepad.thumb_ry); + return std::abs(x) > kRightStickDeadZone || std::abs(y) > kRightStickDeadZone; +} +} // namespace + +double GTA4_VehicleCameraReturn(double step) { + const uint32_t delay = REXCVAR_GET(gta4_vehicle_camera_return_delay); + if (delay) { + const int64_t now = NowMs(); + if (RightStickMoved()) g_return_after_ms.store(now + delay, std::memory_order_relaxed); + if (now < g_return_after_ms.load(std::memory_order_relaxed)) return 0.0; + } + if (const uint32_t speed = REXCVAR_GET(gta4_vehicle_camera_turn_speed); speed) { + step = double(float(step / (1.0 - 0.1 * double(speed)))); + } + return step; +} + +extern "C" void sub_82535D98(PPCContext& ctx, uint8_t* base) { + if (!REXCVAR_GET(gta4_centered_vehicle_camera)) { + __imp__sub_82535D98(ctx, base); + return; + } + const uint8_t allow = REX_LOAD_U8(kAllowSideOffset); + static bool noted = false; // (once: a report shows it was on) + if (!noted) { + noted = true; + REXLOG_INFO("gta4-camera: centered vehicle camera on (the game's side offset was {})", allow ? "on" : "off"); + } + REX_STORE_U8(kAllowSideOffset, 0); + __imp__sub_82535D98(ctx, base); + REX_STORE_U8(kAllowSideOffset, allow); +} diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_fire_escape_trace.inc b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_fire_escape_trace.inc index 68fcf860..e8c5ee1c 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_fire_escape_trace.inc +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_fire_escape_trace.inc @@ -75,12 +75,18 @@ void FireModelEntry(PPCContext& ctx, uint8_t* base) { } } struct ScopedFireMaterial { - FireTraceContext previous = g_fire_context; - uint8_t* previous_base = g_fire_base; - uint64_t before = g_fire_submissions; + // [ps5 port] Saved only with the trace on: copying the thread-local context + // (~500 bytes, emulated TLS reads on the PS5) for every material list cost + // the game's render thread in normal play. + std::optional previous; + uint8_t* previous_base = nullptr; + uint64_t before = 0; bool selected = false, verbose = false; ScopedFireMaterial(PPCContext& ctx, uint8_t* base) { if (!FireTraceConfig().enabled) return; + previous = g_fire_context; + previous_base = g_fire_base; + before = g_fire_submissions; FireTraceContext c; selected = FireMaterial(base, ctx.r3.u32, c); if (!selected) return; @@ -103,11 +109,11 @@ struct ScopedFireMaterial { if (!selected) return; if (verbose) FireTraceLog("material-exit", fmt::format("occurrence={} guest-frame={} model={:08X} material={:08X} submissions={}", g_fire_context.occurrence,g_fire_context.guest_frame,g_fire_context.model,g_fire_context.material,g_fire_submissions-before)); - g_fire_context = previous; g_fire_base = previous_base; + g_fire_context = *previous; g_fire_base = previous_base; } }; void FireMaterialPass(PPCContext& ctx, uint8_t* base, bool after) { - if (!g_fire_context.occurrence) return; + if (!FireTraceConfig().enabled || !g_fire_context.occurrence) return; if (FireReadable(base, kActiveDeferredTechniqueGlobal, 4)) g_fire_context.technique = LoadU32(base, kActiveDeferredTechniqueGlobal); if (!after) g_fire_context.pass = ctx.r3.u32; if (g_fire_context.guest_frame % FireTraceConfig().interval == 0) FireTraceLog(after ? "pass-applied" : "pass-enter", @@ -129,8 +135,11 @@ FireReplayState CaptureFireReplayState(uint8_t* base, uint32_t device) { bool SubmitFireTracedCommand(const void* bytes, size_t size, uint32_t device, const FireTraceContext* captured = nullptr, uint32_t list = 0, uint32_t ordinal = 0, const FireReplayState* replay_live = nullptr, const FireReplayState* replay_applied = nullptr) { + // [ps5 port] Off (always, outside a trace): straight on, without reading the + // thread-local or copying its ~500-byte context for every command. + if (!FireTraceConfig().enabled) return SubmitTvTracedCommand(bytes, size, device); auto c = g_fire_context.occurrence ? g_fire_context : captured ? *captured : FireTraceContext{}; - if (!FireTraceConfig().enabled || !c.occurrence) return SubmitTvTracedCommand(bytes, size, device); + if (!c.occurrence) return SubmitTvTracedCommand(bytes, size, device); c.event = g_fire_next_event.fetch_add(1, std::memory_order_relaxed); c.device = device; c.replay = list != 0; c.command_list = list; c.replay_ordinal = ordinal; if (replay_live) c.replay_live = *replay_live; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_hooks.cpp index b63d73e6..c9b3ebc3 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_hooks.cpp @@ -3,6 +3,7 @@ #include "gta4_frontend_hooks.h" #include +#include #include #include #include @@ -33,6 +34,7 @@ #include "input/context_touch_controls.h" REXCVAR_DECLARE(std::string, gta4_episode_startup_prompt); +REXCVAR_DECLARE(int32_t, gta4_start_episode); REXCVAR_DEFINE_BOOL(gta4_frontend_advanced_graphics_menu, true, "GTA IV/Frontend", "Add an Advanced Graphics page without exceeding the retail 20-row list widget") .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); @@ -53,6 +55,17 @@ constexpr uint32_t kDisplayOptionsCount = 0x831DAEFC; constexpr uint32_t kDisplayOptionsCapacity = 0x831DAEFE; constexpr uint32_t kCurrentScreenAddress = 0x82BFA124; constexpr uint32_t kPauseMenuActiveAddress = 0x82BF9EF4; +// The pause menu's header (sub_8214F230 steps it with Left/Right, sub_8214F7C0 +// draws it; both get it as r3): the tab it highlights, a screen id, at +24. +constexpr uint32_t kPauseHeaderAddress = 0x831D5278; +constexpr uint32_t kPauseHeaderTabOffset = 24; +// What sub_8214F230 does besides the switch, for the same steps: the Brief +// page's flag, the Display page's flag (not in a network game), and +// sub_821B5A68 with this when the map or a network game's last tab is left. +constexpr uint32_t kBriefTabFlagAddress = 0x82BF9D8D; +constexpr uint32_t kDisplayTabFlagAddress = 0x82BF9EF6; +constexpr uint32_t kMapLeaveArgument = 0x82B2F7E0; +constexpr uint32_t kMapScreen = 3; constexpr uint32_t kOptionRecordSize = 22; constexpr uint32_t kOptionActionOffset = 0; constexpr uint32_t kOptionLabelOffset = 1; @@ -1376,6 +1389,12 @@ void ReleaseDisplayExtension(PPCContext& ctx, uint8_t* base) { } void InstallDisplayExtension(PPCContext& ctx, uint8_t* base) { +#if REX_PLATFORM_PS5 + // [ps5 port] No Advanced Graphics in the pause menu: its rows are the PC + // port's (VSync, FSR 1, renderer, window), and the hub sets the PS5's. + (void)ctx, (void)base; + return; +#endif if (!REXCVAR_GET(gta4_frontend_advanced_graphics_menu)) { return; } @@ -1744,12 +1763,21 @@ void SetConfigPath(std::filesystem::path path) { bool SwitchPauseTab(PPCContext& parent, uint8_t* base, policy::PauseTabDirection direction, uint32_t* previous_screen, uint32_t* target_screen) { - const uint32_t current = REX_LOAD_U32(kCurrentScreenAddress); + // The header's tab, not only the screen: the header highlights its own + // copy, so a switch that only changed the screen left the highlight on the + // old tab (the page changed under it). + const uint32_t screen = REX_LOAD_U32(kCurrentScreenAddress); + const uint32_t current = REX_LOAD_U32(kPauseHeaderAddress + kPauseHeaderTabOffset); uint32_t target = 0; if (previous_screen) { - *previous_screen = current; + *previous_screen = screen; } - if (!policy::ResolvePauseTab(current, direction, target)) { + // (a page without the header, or one the header isn't on) + if (current != screen) { + return false; + } + const bool network = (InvokeGuest(parent, base, sub_826C4CA8).r3.u32 & 0xFF) != 0; + if (!policy::ResolvePauseTab(current, direction, network, target)) { return false; } if (target_screen) { @@ -1758,22 +1786,102 @@ bool SwitchPauseTab(PPCContext& parent, uint8_t* base, policy::PauseTabDirection if (policy::ShouldRestorePrimaryBeforeSwitch(g_display_menu.page, target, kDisplayScreen)) { PublishPrimaryPage(base); } + // As the retail header's Left/Right does (sub_8214F230). + const bool next = direction == policy::PauseTabDirection::kNext; + if (target == 2 && (next ? current == 3 : current == 4)) { + REX_STORE_U8(kBriefTabFlagAddress, 0); + } + if (!network && target == kDisplayScreen) { + REX_STORE_U8(kDisplayTabFlagAddress, 1); + } + if (network && (next ? current == kDisplayScreen && target == 1 : current == 1)) { + InvokeGuest(parent, base, sub_821B5A68, kMapLeaveArgument); + } + REX_STORE_U32(kPauseHeaderAddress + kPauseHeaderTabOffset, target); + if (screen == kMapScreen) { + InvokeGuest(parent, base, sub_821B5A68, kMapLeaveArgument); + } InvokeGuest(parent, base, __imp__sub_82256D08, g_last_frontend_channel, target, 0); if (rex::input::IsInputTraceEnabled()) { REXLOG_INFO( "input-e2e: seq={} stage=pause-tab result=switched direction={} channel={} " - "screen={}->{}", + "screen={}->{} network={}", rex::input::NextInputTraceSequence(), static_cast(direction), - g_last_frontend_channel, current, target); + g_last_frontend_channel, screen, target, network); } return true; } } // namespace gta4::frontend_menu +namespace { +// The start's episode check (sub_822440F8, the start's steps): sub_82241428 +// gives a newly installed episode (else -1); the next step asks question 52 +// of sub_8223F9F0 ("play it now?", the episode at kNewEpisodeAddress), and +// its Yes makes it the episode the game starts in. A chosen episode +// (gta4_start_episode) goes the same way, the question answered Yes unseen. +constexpr uint32_t kNewEpisodeQuestion = 52; +constexpr uint32_t kNewEpisodeAddress = 0x82BF9864; +// The episode the session starts in (sub_82142230 hands it to sub_82141F00, +// which starts it and loads its latest save). The start's first step sets it +// to the episode the profile played last (sub_8221B198, from the profile's +// flags) before asking sub_82241428; the question's Yes sets it to the new one. +constexpr uint32_t kStartEpisodeAddress = 0x82BF9858; +int32_t g_answer_yes_for = -1; // (the episode whose question is answered) +bool g_start_episode_used = false; // (the start only: later checks are the game's) +// [ps5 port] Past the question, the start scans the saves (sub_822440F8's +// states 1-2): sub_822409F0 resets the start episode to the profile's last +// one, then sub_82240798 runs for each save and keeps the newest of ANY +// episode, writing its slot to kBootSlotAddress and its episode (read from the +// save's name) to kStartEpisodeAddress. So with a TLAD save newest, GTA IV +// chosen on Home still loaded TLAD. With a Home choice the scan here only +// takes saves of that episode, and with none the episode starts new. +constexpr uint32_t kBootSlotAddress = 0x82A95478; // (-1: none, a new game) +constexpr uint32_t kNewestSaveTimeAddress = 0x82BF98A0; // (6 words) +constexpr uint32_t kSaveTableAddress = 0x82BF3D98; // (XCONTENT_DATA, 308 bytes each) +constexpr uint32_t kSaveEntrySize = 308; +constexpr uint32_t kSaveDisplayNameOffset = 8; // (UTF-16BE, 128) +constexpr uint32_t kSaveFileNameOffset = 264; // ("SGTA4NN") +int32_t g_boot_episode = -1; // (the Home choice, while the start's steps run) +bool g_in_start_steps = false; // (inside sub_822440F8) +} // namespace + +namespace { +std::string SaveFileName(uint8_t* base, uint32_t slot) { + return ReadGuestString(base, kSaveTableAddress + slot * kSaveEntrySize + kSaveFileNameOffset, 42); +} +std::string SaveDisplayName(uint8_t* base, uint32_t slot) { + std::string name; + const uint32_t at = kSaveTableAddress + slot * kSaveEntrySize + kSaveDisplayNameOffset; + for (uint32_t i = 0; i < 128; ++i) { + const uint16_t c = REX_LOAD_U16(at + i * 2); + if (c == 0) break; + name.push_back(c < 0x80 ? char(c) : '?'); + } + return name; +} +} // namespace + extern "C" void sub_82241428(PPCContext& ctx, uint8_t* base) { - __imp__sub_82241428(ctx, base); + __imp__sub_82241428(ctx, base); // (keeps the profile's list of episodes seen) const int32_t retail_episode = ctx.r3.s32; + const int32_t start = REXCVAR_GET(gta4_start_episode); + if (start >= 0 && !g_start_episode_used) { + g_start_episode_used = true; + // (No installed check here: question 52's own, sub_8221A0D0, said "not + // there" for installed episodes in 1.0.1 testers' logs, and the hub only + // offers installed episodes.) + // No question: the start goes on to its save scan, which only takes saves + // of the choice (sub_82240798 below). Set here too for the starts that + // skip the scan (no saves): those start the choice new. + ctx.r3.s64 = -1; + REX_STORE_U32(kStartEpisodeAddress, static_cast(start)); + g_boot_episode = start; + REXLOG_INFO("GTA IV episode startup: episode {} chosen on Home (retail result {}); its newest save, " + "else a new game", + start, retail_episode); + return; + } const std::string policy = REXCVAR_GET(gta4_episode_startup_prompt); if (policy == "off") { @@ -1790,6 +1898,56 @@ extern "C" void sub_82241428(PPCContext& ctx, uint8_t* base) { retail_episode); } +// The start's steps: 1 is a new game in kStartEpisodeAddress, 2 loads kBootSlotAddress. +extern "C" void sub_822440F8(PPCContext& ctx, uint8_t* base) { + g_in_start_steps = true; + __imp__sub_822440F8(ctx, base); + g_in_start_steps = false; + if (g_boot_episode >= 0 && ctx.r3.s32 != 0) { + const int32_t episode = static_cast(REX_LOAD_U32(kStartEpisodeAddress)); + const int32_t slot = static_cast(REX_LOAD_U32(kBootSlotAddress)); + if (ctx.r3.s32 == 2 && slot >= 0) + REXLOG_INFO("GTA IV episode startup: loading {} (episode {})", SaveFileName(base, uint32_t(slot)), episode); + else + REXLOG_INFO("GTA IV episode startup: no save of episode {}; starting it new", episode); + g_boot_episode = -1; // (later scans, the pause menu's Load, are the game's own) + } +} + +// The scan's reset sets the start episode to the profile's last: the choice back. +extern "C" void sub_822409F0(PPCContext& ctx, uint8_t* base) { + __imp__sub_822409F0(ctx, base); + if (g_in_start_steps && g_boot_episode >= 0) + REX_STORE_U32(kStartEpisodeAddress, static_cast(g_boot_episode)); +} + +// Each save's "newest so far?": during the start with a Home choice, a save of +// another episode that won is undone, so the newest save of the choice wins. +extern "C" void sub_82240798(PPCContext& ctx, uint8_t* base) { + if (!g_in_start_steps || g_boot_episode < 0 || (ctx.r4.u32 & 0xFF) == 0) { + __imp__sub_82240798(ctx, base); + return; + } + const uint32_t slot = ctx.r3.u32; + uint32_t time[6], episode_words[3]; + for (uint32_t i = 0; i < 6; ++i) time[i] = REX_LOAD_U32(kNewestSaveTimeAddress + i * 4); + for (uint32_t i = 0; i < 3; ++i) episode_words[i] = REX_LOAD_U32(kStartEpisodeAddress + i * 4); + const uint32_t previous_slot = REX_LOAD_U32(kBootSlotAddress); + __imp__sub_82240798(ctx, base); + if (REX_LOAD_U32(kBootSlotAddress) != slot || previous_slot == slot) return; // (not newer) + const int32_t save_episode = static_cast(REX_LOAD_U32(kStartEpisodeAddress)); + if (save_episode == g_boot_episode) { + REXLOG_INFO("GTA IV episode startup: {} '{}' (episode {}) is the newest so far", SaveFileName(base, slot), + SaveDisplayName(base, slot), save_episode); + return; + } + for (uint32_t i = 0; i < 6; ++i) REX_STORE_U32(kNewestSaveTimeAddress + i * 4, time[i]); + for (uint32_t i = 0; i < 3; ++i) REX_STORE_U32(kStartEpisodeAddress + i * 4, episode_words[i]); + REX_STORE_U32(kBootSlotAddress, previous_slot); + REXLOG_INFO("GTA IV episode startup: skipping {} '{}' (episode {}, not {})", SaveFileName(base, slot), + SaveDisplayName(base, slot), save_episode, g_boot_episode); +} + extern "C" void sub_82157F90(PPCContext& ctx, uint8_t* base) { ReleaseDisplayExtension(ctx, base); __imp__sub_82157F90(ctx, base); @@ -1842,6 +2000,14 @@ extern "C" void sub_8223F9F0(PPCContext& ctx, uint8_t* base) { // and the save-block serializer. Observe them without changing save flow. thread_local uint32_t previous_reason = std::numeric_limits::max(); const uint32_t reason = ctx.r3.u32; + if (reason == kNewEpisodeQuestion && g_answer_yes_for > 0 && + static_cast(REX_LOAD_U32(kNewEpisodeAddress)) == g_answer_yes_for) { + REX_STORE_U8(ctx.r5.u32, 1); // (Yes) + ctx.r3.u64 = 1; // (answered) + REXLOG_INFO("GTA IV episode startup: starting in episode {}", g_answer_yes_for); + g_answer_yes_for = -1; + return; + } const char* stage = nullptr; switch (reason) { case 17: stage = "begin-save"; break; @@ -2283,5 +2449,34 @@ extern "C" void sub_82258EC0(PPCContext& ctx, uint8_t* base) { __imp__sub_82258EC0(ctx, base); } +// [ps5 port] A script native's lookup (the table r3, the ID r4; native +// sub_8247C148 passes 0x82B31960): when the ID is listed among its 128, it +// takes the first of its 256 entries (28 bytes from +8) with that ID whose +// object (at +12) has a nonzero word at +112. An entry with that ID and no +// object at all made it read address 112: a Ballad of Gay Tony player's game +// crashed there in the helicopter mission against the yacht. Such an entry, +// met before a live one, now counts as not found (the lookup's own answer +// when none matches: r3 = 0). +extern "C" void sub_821B8A80(PPCContext& ctx, uint8_t* base) { + const uint32_t table = ctx.r3.u32, id = ctx.r4.u32; + bool listed = false; + for (uint32_t i = 0; i < 128 && !listed; ++i) listed = REX_LOAD_U32(table + 7336 + i * 8) == id; + if (listed) { + for (uint32_t i = 0; i < 256; ++i) { + const uint32_t entry = table + 8 + i * 28; + if (REX_LOAD_U32(entry) != id) continue; + const uint32_t object = REX_LOAD_U32(entry + 4); + if (object && REX_LOAD_U32(object + 112)) break; // (the original finds this one) + if (object) continue; + static std::atomic logged{0}; + if (logged.fetch_add(1, std::memory_order_relaxed) < 8) + REXLOG_WARN("gta4-native-lookup: table {:08X} has ID {:08X} with no object (entry {}); not found", table, id, i); + ctx.r3.u64 = 0; + return; + } + } + __imp__sub_821B8A80(ctx, base); +} + // The retail bar renderer reads our slider's position through the cvar bridge. #include "gta4_frontend_slider_guest.inc" diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_menu_policy.h b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_menu_policy.h index 7c8962a1..dad4815a 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_menu_policy.h +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_frontend_menu_policy.h @@ -31,7 +31,12 @@ enum class PauseTabShoulderEdge : uint8_t { kSimultaneous, }; -constexpr std::array kPauseTabScreens = {3, 4, 5, 6, 7, 8, 9}; +// The pause menu's header tabs, as screen ids, in the order the retail +// header's Left/Right steps through them (sub_8214F230's jump tables; the +// header draws them in this order, sub_8214F7C0). A network game has tab 1 +// at the end and no tab 4. Screens 6 and 9 are pages without the header. +constexpr std::array kPauseTabScreens = {3, 2, 4, 5, 7, 8, 0}; +constexpr std::array kNetworkPauseTabScreens = {3, 2, 5, 7, 8, 1}; constexpr bool IsPauseTabScreen(uint32_t screen) noexcept { for (uint32_t candidate : kPauseTabScreens) { @@ -39,6 +44,11 @@ constexpr bool IsPauseTabScreen(uint32_t screen) noexcept { return true; } } + for (uint32_t candidate : kNetworkPauseTabScreens) { + if (candidate == screen) { + return true; + } + } return false; } @@ -59,23 +69,29 @@ constexpr PauseTabShoulderEdge ClassifyPauseTabShoulderEdge( return PauseTabShoulderEdge::kNone; } -constexpr bool ResolvePauseTab(uint32_t current_screen, PauseTabDirection direction, - uint32_t& target_screen) noexcept { - for (std::size_t index = 0; index < kPauseTabScreens.size(); ++index) { - if (kPauseTabScreens[index] != current_screen) { +template +constexpr bool ResolvePauseTabIn(const std::array& tabs, uint32_t current_screen, + PauseTabDirection direction, uint32_t& target_screen) noexcept { + for (std::size_t index = 0; index < tabs.size(); ++index) { + if (tabs[index] != current_screen) { continue; } if (direction == PauseTabDirection::kPrevious) { - target_screen = index == 0 ? kPauseTabScreens.back() : kPauseTabScreens[index - 1]; + target_screen = index == 0 ? tabs.back() : tabs[index - 1]; } else { - target_screen = index + 1 == kPauseTabScreens.size() ? kPauseTabScreens.front() - : kPauseTabScreens[index + 1]; + target_screen = index + 1 == tabs.size() ? tabs.front() : tabs[index + 1]; } return true; } return false; } +constexpr bool ResolvePauseTab(uint32_t current_screen, PauseTabDirection direction, bool network, + uint32_t& target_screen) noexcept { + return network ? ResolvePauseTabIn(kNetworkPauseTabScreens, current_screen, direction, target_screen) + : ResolvePauseTabIn(kPauseTabScreens, current_screen, direction, target_screen); +} + struct StockLayout { uint32_t rows = 0; uint16_t count = 0; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_gpu_pass_context.h b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_gpu_pass_context.h index 3fa04e1f..57c8ad5b 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_gpu_pass_context.h +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_gpu_pass_context.h @@ -25,9 +25,12 @@ class ScopedFinalComposite { }; inline std::atomic temporal_inputs{false}; inline std::atomic next_scope{1},unresolved_scopes{0},conflicting_scopes{0},oversized_envelopes{0}; -inline bool Enabled(){ +inline bool MarkersRequested(){ static const bool markers=[] {const char* p=std::getenv("REX_GTA4_GPU_PASS_MARKERS");return p&&std::strcmp(p,"1")==0;}(); - return temporal_inputs.load(std::memory_order_relaxed)||markers||rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeProfiler); + return markers||rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeProfiler); +} +inline bool Enabled(){ + return temporal_inputs.load(std::memory_order_relaxed)||MarkersRequested(); } class ScopedExecutedList { public: @@ -52,7 +55,9 @@ template bool Submit(Graphics* graphics,uint32_t abi,const void* command,size_t size){ using namespace rex::graphics::gta4_native; if(!graphics)return false; - if(!Enabled())return graphics->SubmitTitleCommand(kTitleId,abi,command,size); + // [ps5 port] Temporal inputs need the current pass (Enabled), not a copy of + // it in every command: only the profiler and markers read that. + if(!MarkersRequested())return graphics->SubmitTitleCommand(kTitleId,abi,command,size); std::array storage; const size_t packed=PackGpuPassEnvelope(storage,command,size,abi,current); if(!packed){++oversized_envelopes;return graphics->SubmitTitleCommand(kTitleId,abi,command,size);} diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_help_trace_hooks.inc b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_help_trace_hooks.inc index 64de44f5..393299f5 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_help_trace_hooks.inc +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_help_trace_hooks.inc @@ -29,7 +29,7 @@ const HelpTraceConfig& HelpConfig() { return config; } bool HelpEnabled() { - return rex::diagnostics::IsEnabled(rex::diagnostics::Category::kLogging) && + return HookDiagnosticsEnabled(rex::diagnostics::Category::kLogging) && HelpConfig().enabled; } struct HelpText { @@ -225,7 +225,9 @@ void HelpImmediate(PPCContext& ctx, uint8_t* base, const char* kind, uint32_t co } // namespace void GTA4_HelpTraceFinalize(uint8_t* base, uint32_t dc, uint32_t caller) { - if (!g_help_call || !dc) return; + // [ps5 port] Every draw command the main thread finishes: the trace's + // switch first, not the thread-local (a call on the PS5). + if (!dc || !HelpEnabled() || !g_help_call) return; HelpBackground bg; { std::lock_guard lock(g_help_dc_mutex); @@ -241,8 +243,11 @@ void GTA4_HelpTraceFinalize(uint8_t* base, uint32_t dc, uint32_t caller) { bg.call, bg.id, GetNativeLightSubmittedFrame(base), dc, caller); } -void GTA4_HelpTraceNativeSubmission(uint32_t type, uint32_t device, - bool accepted, bool captured) { +namespace { +// [ps5 port] Out of line: inlined, the compiler read g_help_execution (a call +// on the PS5) for every command, before testing whether the trace is on. +[[gnu::noinline]] void HelpTraceNativeSubmissionTraced(uint32_t type, uint32_t device, + bool accepted, bool captured) { if (!g_help_execution) return; using T = rex::graphics::gta4_native::CommandType; if (type != uint32_t(T::kDrawPrimitive) && type != uint32_t(T::kDrawPrimitiveUp) && @@ -258,6 +263,14 @@ void GTA4_HelpTraceNativeSubmission(uint32_t type, uint32_t device, bg.call, bg.id, GetNativeLightSubmittedFrame(execution.base), device, type, captured ? "captured-not-submitted" : accepted ? "accepted" : "rejected"); } +} // namespace + +void GTA4_HelpTraceNativeSubmission(uint32_t type, uint32_t device, + bool accepted, bool captured) { + // [ps5 port] The thread-local is a call on the PS5, and this runs for every + // command the game submits: check the trace is on at all first. + if (HelpEnabled()) HelpTraceNativeSubmissionTraced(type, device, accepted, captured); +} void GTA4_HelpTraceTextSubmit(PPCContext& ctx, uint8_t* base, void (*original)(PPCContext&, uint8_t*)) { diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_input_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_input_hooks.cpp index f8dc1eec..6076b114 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_input_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_input_hooks.cpp @@ -26,8 +26,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -458,6 +460,45 @@ struct ParachuteScriptContext { bool active = false; }; +// [ps5 port] The finale's helicopter climb ("Repeatedly tap A to climb into +// the chopper", both endings) counts taps per game frame as tuned for 30 FPS: +// at 60 it can't be passed (as on PC and the Xbox Series X). Each button or +// control query the finale's scripts make is noted here; the frame limit holds +// 30 while they keep asking (gta4_native_hooks.cpp). +constexpr uint32_t kFinaleProgramKeys[] = { + 0xDF7C5C9C, 0x8F959493, 0xFB236BB5, 0xECE5CF3A, 0x96C722FE, // finale1, finale1a, finale1b, finale1c, finale1d + 0xEA2671F0, 0xEE6FD0E6, 0x9DC72F96, 0x8B818B0B, 0x94BE471D, // finale2, finale2a, finale2b, finale2c, finale3 +}; +std::atomic g_finale_button_query_ns{0}; + +void NoteFinaleButtonQuery(uint8_t* base) { + const uint32_t thread = LoadU32(base, kExecutingScriptThreadAddress); + if (!thread) return; + const uint32_t key = LoadU32(base, thread + kScriptProgramKeyOffset); + // Which scripts ask for buttons, once each (names are hashed: a log shows + // whether the climb's script is one of the above). + { + static std::mutex seen_mutex; + static std::array seen{}; + static size_t seen_count = 0; + std::lock_guard lock(seen_mutex); + if (seen_count < seen.size() && + std::find(seen.begin(), seen.begin() + seen_count, key) == seen.begin() + seen_count) { + seen[seen_count++] = key; + REXLOG_INFO("gta4-input: script {:08X} asks for buttons", key); + } + } + for (const uint32_t finale : kFinaleProgramKeys) { + if (key == finale) { + g_finale_button_query_ns.store(std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(), + std::memory_order_relaxed); + return; + } + } +} + ParachuteScriptContext ReadParachuteScriptContext(uint8_t* base) { const uint32_t thread = LoadU32(base, kExecutingScriptThreadAddress); if (!thread || @@ -940,7 +981,8 @@ void ProcessPauseTabShoulders(PPCContext& parent, uint8_t* base, const bool left_down = keyboard_left_down || gamepad_left_down; const bool right_down = gamepad_right_down; - if (!epoch.frontend_active || + // (the pause menu only: screen 0 is one of its tabs, and the start screen's) + if (!epoch.frontend_active || LoadU8(base, kPauseMenuVisibleAddress) == 0 || !gta4::frontend_menu::policy::IsPauseTabScreen(screen)) { g_pause_tab_input = { .epoch = epoch.sequence, @@ -2092,6 +2134,8 @@ void MaybeForceDirectWeaponAction(PPCContext& ctx, uint8_t* base, } } +// Steady-clock time of the finale scripts' last button query (0: none yet). +int64_t FinaleButtonQueryNs() { return g_finale_button_query_ns.load(std::memory_order_relaxed); } } // namespace gta4::input extern "C" void sub_828CCD60(PPCContext& ctx, uint8_t* base) { @@ -2562,6 +2606,7 @@ extern "C" void sub_822D4158(PPCContext& ctx, uint8_t* base) { extern "C" void sub_825D1AF8(PPCContext& ctx, uint8_t* base) { const uint32_t call_context = ctx.r3.u32; + gta4::input::NoteFinaleButtonQuery(base); const uint32_t touch_thread = gta4::input::ReadTouchScriptThread(base); const auto touch_context = gta4::input::GetTouchContextSnapshot(); __imp__sub_825D1AF8(ctx, base); @@ -2583,6 +2628,7 @@ extern "C" void sub_825D1AF8(PPCContext& ctx, uint8_t* base) { extern "C" void sub_825D1B40(PPCContext& ctx, uint8_t* base) { const uint32_t call_context = ctx.r3.u32; + gta4::input::NoteFinaleButtonQuery(base); const uint32_t touch_thread = gta4::input::ReadTouchScriptThread(base); const auto touch_context = gta4::input::GetTouchContextSnapshot(); const auto parachute = gta4::input::ReadTouchParachuteState(base); @@ -2596,6 +2642,7 @@ extern "C" void sub_825D1B40(PPCContext& ctx, uint8_t* base) { extern "C" void sub_825D1B88(PPCContext& ctx, uint8_t* base) { const uint32_t call_context = ctx.r3.u32; + gta4::input::NoteFinaleButtonQuery(base); const uint32_t touch_thread = gta4::input::ReadTouchScriptThread(base); const auto touch_context = gta4::input::GetTouchContextSnapshot(); __imp__sub_825D1B88(ctx, base); @@ -2617,6 +2664,7 @@ extern "C" void sub_825D1B88(PPCContext& ctx, uint8_t* base) { extern "C" void sub_825D1BD0(PPCContext& ctx, uint8_t* base) { const uint32_t call_context = ctx.r3.u32; + gta4::input::NoteFinaleButtonQuery(base); const uint32_t touch_thread = gta4::input::ReadTouchScriptThread(base); const auto touch_context = gta4::input::GetTouchContextSnapshot(); __imp__sub_825D1BD0(ctx, base); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_loading_parallax_fix.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_loading_parallax_fix.cpp index c1faf846..14121e44 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_loading_parallax_fix.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_loading_parallax_fix.cpp @@ -1,12 +1,17 @@ #include "gta4_loading_parallax_fix.h" #include +#include +#include #include #include #include "gta4_init.h" #include "gta4_aspect_hooks.h" +#include +#include + namespace { // Layout and limits are taken from the generated sub_82143DC8 animator and @@ -43,7 +48,19 @@ void StoreGuestFloat(uint8_t* base, uint32_t address, float value) { } // namespace +#if REX_PLATFORM_PS5 +// [ps5 port] When a loading screen was last drawn (the frame limiter holds +// the game at 30 FPS while one is up: gta4_native_hooks.cpp). +std::atomic g_ps5_loading_screen_ns{0}; +#endif + extern "C" void sub_82143DC8(PPCContext& ctx, uint8_t* base) { +#if REX_PLATFORM_PS5 + g_ps5_loading_screen_ns.store( + std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()) + .count(), + std::memory_order_relaxed); +#endif const uint32_t screen = REX_LOAD_U32(kCurrentScreenGlobal); if (screen >= kScreenCount) { __imp__sub_82143DC8(ctx, base); @@ -59,6 +76,24 @@ extern "C" void sub_82143DC8(PPCContext& ctx, uint8_t* base) { const float cumulative_elapsed = LoadGuestFloat(base, kCumulativeElapsedGlobal); const float frame_delta = LoadGuestFloat(base, kFrameDeltaGlobal); +#if REX_PLATFORM_PS5 + // [ps5 port] Each loading screen once: how long it was meant to show, how + // long the previous one really showed, and the game's clock, to compare. + { + static uint32_t logged_screen = ~0u; + static auto shown_at = std::chrono::steady_clock::now(); + if (screen != logged_screen) { + const auto now = std::chrono::steady_clock::now(); + REXLOG_INFO("gta4-loading: screen {} duration {} (previous screen showed {} ms) elapsed {:.3f} " + "delta {:.4f}", + screen, LoadGuestFloat(base, screen_base), + std::chrono::duration_cast(now - shown_at).count(), + cumulative_elapsed, frame_delta); + logged_screen = screen; + shown_at = now; + } + } +#endif const bool advance_animation = animation_clock.Observe(cumulative_elapsed, frame_delta); std::array snapshots = {}; for (uint32_t layer = 0; layer < layer_count; ++layer) { diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_multiplayer_64_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_multiplayer_64_hooks.cpp index bb44175b..1b222e35 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_multiplayer_64_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_multiplayer_64_hooks.cpp @@ -173,7 +173,13 @@ struct NearestNetworkDecisionContext { PPCContext call_ctx{}; }; -thread_local NearestNetworkDecisionContext g_nearest_network_decision; +// [ps5 port] Over-aligned for the PS5's thread-local storage (32 at most): +// a per-thread heap object instead. +static NearestNetworkDecisionContext& Ps5Tls_g_nearest_network_decision() { + thread_local NearestNetworkDecisionContext* p = new NearestNetworkDecisionContext(); + return *p; +} +#define g_nearest_network_decision (Ps5Tls_g_nearest_network_decision()) struct PlayerInfoBatchProjectionContext { bool active = false; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_native_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_native_hooks.cpp index 2a4966bd..53bf9b9f 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_native_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_native_hooks.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,13 @@ #include #include +#include +#include +#if REX_PLATFORM_PS5 +#include "ps5/game/ps5_display.h" +#endif #include + #include #include #include @@ -55,6 +62,12 @@ #include "gta4_draw_distance_policy.h" REXCVAR_DECLARE(uint32_t, gta4_shadow_map_base_size); +#if REX_PLATFORM_PS5 +REXCVAR_DECLARE(uint32_t, gta4_native_shadow_min_indices); +REXCVAR_DECLARE(uint32_t, ps5_environment_reflection_every); +REXCVAR_DECLARE(bool, ps5_smooth_40); +REXCVAR_DECLARE(uint32_t, ps5_water_reflection_every); +#endif REXCVAR_DEFINE_UINT32(gta4_shadow_cascade_count, 0, "GTA IV/Graphics/Shadows", "Directional shadow cascades (0 keeps the stock 5)") .range(0, 5) @@ -88,11 +101,33 @@ REXCVAR_DEFINE_BOOL( "Diagnostic-only removal of the authored filler-light flag from apartment bulb x890") .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); +#if REX_PLATFORM_PS5 +bool DropSmallShadowDraw(uint8_t* base, uint32_t device, uint32_t index_count); // (below) +#endif + namespace { using namespace rex::graphics::gta4_native; using GuestFunction = void (*)(PPCContext&, uint8_t*); +// [ps5 port] The diagnostics policy cannot change once configured, but asking +// it is a call into the runtime, several per draw from these hooks. Cached as +// a mask (bit 31: cached) once configured; a load and a bit test after that. +std::atomic g_hook_diagnostics_mask{0}; +[[gnu::noinline]] bool HookDiagnosticsEnabledSlow(rex::diagnostics::Category category) { + if (!rex::diagnostics::IsConfigured()) return rex::diagnostics::IsEnabled(category); + uint32_t mask = 0x80000000u; + for (uint32_t i = 0; i < uint32_t(rex::diagnostics::Category::kCount) && i < 31; ++i) + if (rex::diagnostics::IsEnabled(rex::diagnostics::Category(i))) mask |= 1u << i; + g_hook_diagnostics_mask.store(mask, std::memory_order_relaxed); + return (mask >> uint32_t(category)) & 1u; +} +[[gnu::always_inline]] inline bool HookDiagnosticsEnabled(rex::diagnostics::Category category) { + const uint32_t mask = g_hook_diagnostics_mask.load(std::memory_order_relaxed); + if (!(mask & 0x80000000u)) [[unlikely]] return HookDiagnosticsEnabledSlow(category); + return (mask >> uint32_t(category)) & 1u; +} + constexpr uint32_t kOutOfMemory = 0x8007000E; constexpr uint32_t kAlignedAllocationFreeFlags = 0x24800000; constexpr uint32_t kFallbackAllocationFreeFlags = 0xB1800000; @@ -410,27 +445,39 @@ uint32_t IdentifyVectorFontOwner(uint8_t* base, uint32_t owner) { return 0; } +// [ps5 port] Each thread-local access is a call on the PS5 (also again after +// the wrapped call), and this runs for every texture the game binds: the +// addresses are kept, and the owner and outer stage, read only while a font +// id is set, are left alone by a binding that is not a font's. class ScopedVectorFontBinding final { public: ScopedVectorFontBinding(uint32_t logical_font_id, uint32_t owner, uint32_t outer_stage) - : previous_id_(g_vector_font_id), - previous_owner_(g_vector_font_owner), - previous_outer_stage_(g_vector_font_outer_stage) { - g_vector_font_id = logical_font_id; - g_vector_font_owner = owner; - g_vector_font_outer_stage = outer_stage; + : id_(&g_vector_font_id), previous_id_(*id_) { + *id_ = logical_font_id; + if (logical_font_id) { + owner_ = &g_vector_font_owner; + outer_stage_ = &g_vector_font_outer_stage; + previous_owner_ = std::exchange(*owner_, owner); + previous_outer_stage_ = std::exchange(*outer_stage_, outer_stage); + } } ~ScopedVectorFontBinding() { - g_vector_font_id = previous_id_; - g_vector_font_owner = previous_owner_; - g_vector_font_outer_stage = previous_outer_stage_; + *id_ = previous_id_; + if (owner_) { + *owner_ = previous_owner_; + *outer_stage_ = previous_outer_stage_; + } } private: + // Volatile, or the compiler works the address out again after the call. + uint32_t* volatile id_; uint32_t previous_id_; - uint32_t previous_owner_; - uint32_t previous_outer_stage_; + uint32_t* owner_ = nullptr; + uint32_t* outer_stage_ = nullptr; + uint32_t previous_owner_ = 0; + uint32_t previous_outer_stage_ = 0; }; struct CapturedDrawSnapshot { @@ -477,11 +524,17 @@ struct ActiveNativeCommandCapture { }; thread_local ActiveNativeCommandCapture g_active_native_capture; +// [ps5 port] Threads with a capture open. Every command the game submits +// checks for one; the thread-local is a call on the PS5, this is a load. +std::atomic g_native_captures_open{0}; std::mutex g_cached_native_command_lists_mutex; std::unordered_map> g_cached_native_command_lists; std::mutex g_vertex_declaration_diagnostic_mutex; std::unordered_map g_logged_vertex_declarations; +// [ps5 port] Declarations already registered with the renderer, by address: +// the hash of what was registered (under the mutex above). +std::unordered_map g_registered_vertex_declarations; struct NativeShaderBindingDiagnosticState { uint32_t vertex_shader = 0; @@ -496,7 +549,7 @@ uint8_t* GuestPointer(uint8_t* base, uint32_t address) { } void TrackNativeShaderBinding(uint32_t device, ShaderStage stage, uint32_t shader) { - if (!rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace)) { + if (!HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { return; } std::lock_guard lock(g_shader_binding_diagnostic_mutex); @@ -2598,6 +2651,12 @@ double GetExteriorReflectionCaptureDistance() { } rex::system::IGraphicsSystem* GetNativeGraphicsSystem() { +#if REX_PLATFORM_PS5 + // [ps5 port] Asked several times per draw; the graphics system lives as long + // as the game does here (the app ends with the process), so look it up once. + static std::atomic cached{nullptr}; + if (auto* graphics = cached.load(std::memory_order_acquire)) return graphics; +#endif auto* runtime = rex::Runtime::instance(); if (!runtime) { return nullptr; @@ -2606,6 +2665,9 @@ rex::system::IGraphicsSystem* GetNativeGraphicsSystem() { if (!graphics || graphics->GetTitleCommandAbi(kTitleId) != kTitleCommandAbi) { return nullptr; } +#if REX_PLATFORM_PS5 + cached.store(graphics, std::memory_order_release); +#endif return graphics; } @@ -3305,7 +3367,8 @@ bool IsCachedListCommandType(CommandType type) { template bool CaptureNativeCommand(const Command& command) { - if (!g_active_native_capture.active || !IsCachedListCommandType(command.header.type)) { + if (!g_native_captures_open.load(std::memory_order_relaxed) || !g_active_native_capture.active || + !IsCachedListCommandType(command.header.type)) { return false; } @@ -3323,7 +3386,7 @@ bool CaptureNativeCommand(const Command& command) { if constexpr (std::is_same_v) { const bool trace_gbuffer_snapshot = - rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) && + HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && (command.index_count == 5961 || command.index_count == 1056); if (trace_gbuffer_snapshot) { captured.draw_diagnostic_valid = true; @@ -3412,9 +3475,16 @@ bool SubmitNativeCommand(const Command& command) { if (auto* graphics = GetNativeGraphicsSystem()) { uint32_t phone_device = 0; if constexpr (requires { command.device; }) phone_device = command.device; - const bool accepted = SubmitFireTracedCommand(&command, sizeof(command), phone_device); + // [ps5 port] Outside the fire, TV and phone traces and the pass markers + // (always, in play) the wrapper chain below ends in this same call; go + // straight there instead of through four out-of-line checks per command. + const bool plain = !FireTraceConfig().enabled && !TvTraceConfig().enabled && + !PhoneTraceConfig().enabled && !gta4::gpu_pass::MarkersRequested(); + const bool accepted = + plain ? graphics->SubmitTitleCommand(kTitleId, kTitleCommandAbi, &command, sizeof(command)) + : SubmitFireTracedCommand(&command, sizeof(command), phone_device); GTA4_HelpTraceNativeSubmission(uint32_t(command.header.type), help_device, accepted, false); - if (!accepted && rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks)) { + if (!accepted && HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks)) { static std::atomic rejection_count{0}; const uint64_t count = NextNativeHookDiagnosticCall(rejection_count); if (ShouldLogNativeHookCall(count)) { @@ -3574,12 +3644,12 @@ void SubmitEnvironmentalData(uint8_t* base, uint32_t device, uint32_t postfx) { #include "gta4_temporal_hooks.inc" bool ShouldLogNativeHookCall(uint64_t call_count) { - return call_count != 0 && rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && + return call_count != 0 && HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && (call_count <= 32 || !(call_count % 4096)); } uint64_t NextNativeHookDiagnosticCall(std::atomic& counter) { - if (!rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks)) { + if (!HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks)) { return 0; } return counter.fetch_add(1, std::memory_order_relaxed) + 1; @@ -3854,9 +3924,20 @@ bool SubmitNativeVertexDeclaration(uint8_t* base, uint32_t device, uint32_t decl output.padding = 0; } + // [ps5 port] The game sets the same few declarations over and over. Each + // registration sent an ~800-byte command through the producer and had the + // worker build a new declaration object, so register one only when what is + // at that address changed. + const uint64_t content = + XXH3_64bits_withSeed(command.elements, element_count * sizeof(VertexElement), + (uint64_t(maximum_stream) << 32) | element_count); bool log_declaration = false; { std::lock_guard lock(g_vertex_declaration_diagnostic_mutex); + if (const auto known = g_registered_vertex_declarations.find(declaration); + known != g_registered_vertex_declarations.end() && known->second == content) { + return true; + } if (g_logged_vertex_declarations.size() < kVertexDeclarationDiagnosticLimit) { log_declaration = g_logged_vertex_declarations.emplace(declaration, true).second; } @@ -3872,7 +3953,10 @@ bool SubmitNativeVertexDeclaration(uint8_t* base, uint32_t device, uint32_t decl REXLOG_INFO("gta4-native-vertex-declaration: handle={:08X} elements={} maximum-stream={} [{}]", declaration, element_count, maximum_stream, elements_summary); } - SubmitNativeCommand(command); + if (SubmitNativeCommand(command)) { + std::lock_guard lock(g_vertex_declaration_diagnostic_mutex); + g_registered_vertex_declarations[declaration] = content; + } return true; } @@ -4000,7 +4084,7 @@ bool SubmitCapturedNativeCommand(PPCContext& ctx, uint8_t* base, uint32_t device const bool trace_gbuffer_replay = captured.draw_diagnostic_valid && - rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace); + HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace); const uint32_t replay_frame = trace_gbuffer_replay ? LoadU32(base, device + kSubmittedFrameOffset) : 0; auto log_replay_state = [&](std::string_view point, const std::array& transform, @@ -4164,7 +4248,7 @@ bool ReplayCachedNativeCommandList(PPCContext& ctx, uint8_t* base, uint32_t devi ResolveCommand resolve{}; std::memcpy(&resolve, command.bytes.data(), sizeof(resolve)); const uint32_t known_frontbuffer = g_last_present_frontbuffer.load(std::memory_order_relaxed); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && ((known_frontbuffer && resolve.destination_texture == known_frontbuffer) || (resolve.flags & 0x04000000u))) { std::fprintf(stderr, @@ -4333,7 +4417,7 @@ void ApplyNativeShaderState(PPCContext& ctx, uint8_t* base, GuestFunction implem } extern "C" void sub_82A42168(PPCContext& ctx, uint8_t* base) { - if (!IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace)) { + if (!IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { __imp__sub_82A42168(ctx, base); return; } @@ -4405,7 +4489,7 @@ extern "C" void sub_82A42168(PPCContext& ctx, uint8_t* base) { void LogKnownOffscreenTransformBoundary(std::string_view point, const PPCContext& ctx, uint8_t* base, uint32_t object, uint32_t explicit_device = 0) { - if (!IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace)) { + if (!IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { return; } const uint32_t device = explicit_device ? explicit_device : LoadU32(base, kDeferredDeviceGlobal); @@ -4445,7 +4529,7 @@ void LogKnownOffscreenTransformTransition(std::string_view point, uint8_t* base, const std::array& args, const std::array& before, const std::array& after) { - if (!IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) || + if (!IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) || !device) { return; } @@ -4480,7 +4564,7 @@ void LogKnownOffscreenTransformTransition(std::string_view point, uint8_t* base, template void TraceKnownOffscreenTransformTransition(std::string_view point, PPCContext& ctx, uint8_t* base, Function function) { - if (!IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace)) { + if (!IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { function(ctx, base); return; } @@ -4553,8 +4637,8 @@ extern "C" void sub_828C6620(PPCContext& ctx, uint8_t* base) { extern "C" void sub_828C4338(PPCContext& ctx, uint8_t* base) { // The executed model, not its outer model-list, distinguishes rigid submodels // that share a vertex stream. r5 is a material bucket in this entry point. - temporal_host::Scope temporal_scope(temporal_host::instance.matrix, ctx.r3.u32, - temporal_host::instance.pose); + temporal_host::Scope temporal_scope(temporal_host::Scope::kInherit, ctx.r3.u32, + temporal_host::Scope::kInherit); FireModelEntry(ctx, base); TraceKnownOffscreenTransformTransition("offscreen-transition-model-materials", ctx, base, __imp__sub_828C4338); @@ -4563,8 +4647,8 @@ extern "C" void sub_828C4338(PPCContext& ctx, uint8_t* base) { extern "C" void sub_828C4578(PPCContext& ctx, uint8_t* base) { // Skinned submodels retain the owning transform/skeleton from D4268 while // capturing the model actually being executed in this nested call. - temporal_host::Scope temporal_scope(temporal_host::instance.matrix, ctx.r3.u32, - temporal_host::instance.pose); + temporal_host::Scope temporal_scope(temporal_host::Scope::kInherit, ctx.r3.u32, + temporal_host::Scope::kInherit); __imp__sub_828C4578(ctx, base); } @@ -4587,7 +4671,7 @@ extern "C" void sub_828D4268(PPCContext& ctx, uint8_t* base) { } extern "C" void sub_828BD250(PPCContext& ctx, uint8_t* base) { - if (!IsNativeMode() || !rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace)) { + if (!IsNativeMode() || !HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { __imp__sub_828BD250(ctx, base); return; } @@ -4636,6 +4720,22 @@ extern "C" void sub_828E7000(PPCContext& ctx, uint8_t* base) { extern "C" void sub_828BFC00(PPCContext& ctx, uint8_t* base) { LogKnownOffscreenTransformBoundary("offscreen-boundary-indexed-entry", ctx, base, ctx.r3.u32); +#if REX_PLATFORM_PS5 + // [ps5 port] The game's geometry draw: vertex declaration (when it changed), + // indices, stream and DrawIndexedPrimitive, each a command we capture. A + // small geometry going into the sun shadow map, whose draw DropSmallShadowDraw + // drops (Small Object Shadows off), is left out whole here instead: about a + // thousand of them a frame in the city. The game's own caches of the bound + // declaration and stream (0x831C2414 and 364 bytes before) are left as they + // were, matching the device. (r6: the index buffer; r7: the count, 0 or more + // than the buffer has meaning all of it, as the function itself does.) + if (IsNativeMode() && ctx.r6.u32) { + const int32_t available = int32_t(LoadU32(base, ctx.r6.u32 + 4)); + const int32_t requested = int32_t(ctx.r7.u32); + const uint32_t count = uint32_t(requested == 0 || requested > available ? available : requested); + if (DropSmallShadowDraw(base, LoadU32(base, kDeferredDeviceGlobal), count)) return; + } +#endif __imp__sub_828BFC00(ctx, base); } @@ -4885,7 +4985,7 @@ extern "C" void sub_821F1670(PPCContext& ctx, uint8_t* base) { __imp__sub_821F1670(ctx, base); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && g_vector_font_rectangle_trace_count.load(std::memory_order_relaxed) < 32 && REXCVAR_QUERY(bool, gta4_trace_vector_fonts)) { const uint64_t trace = ++g_vector_font_rectangle_trace_count; @@ -5043,6 +5143,7 @@ extern "C" void sub_828C01E0(PPCContext& ctx, uint8_t* base) { REXLOG_WARN("gta4-native-cache: replacing unfinished build object={:08X} commands={}", g_active_native_capture.build_object, g_active_native_capture.commands.size()); } + if (!g_active_native_capture.active) g_native_captures_open.fetch_add(1, std::memory_order_relaxed); g_active_native_capture = {}; g_active_native_capture.active = true; g_active_native_capture.base = base; @@ -5070,6 +5171,7 @@ extern "C" void sub_828C0338(PPCContext& ctx, uint8_t* base) { const uint32_t command_list = output ? LoadU32(base, output) : 0; std::vector commands = std::move(g_active_native_capture.commands); g_active_native_capture = {}; + g_native_captures_open.fetch_sub(1, std::memory_order_relaxed); if (command_list) { const size_t command_count = commands.size(); @@ -5196,6 +5298,11 @@ extern "C" void sub_82A416B8(PPCContext& ctx, uint8_t* base) { StoreU32(base, output, device); } DeviceCommand command{{sizeof(DeviceCommand), CommandType::kDeviceCreated}, device, mode}; + { + // A new device registers its declarations afresh. + std::lock_guard lock(g_vertex_declaration_diagnostic_mutex); + g_registered_vertex_declarations.clear(); + } SubmitNativeCommand(command); ctx.r3.u32 = 0; } @@ -5344,11 +5451,13 @@ std::string ShadowTargetFetch(uint8_t* base, uint32_t texture) { bool ShadowTraceActive(uint8_t* base) { const uint32_t frames = REXCVAR_GET(gta4_trace_shadow_targets); +#if !REX_PLATFORM_PS5 // [ps5 port] (six warnings a second in every PS5 log) static std::atomic heartbeat{0}; if ((heartbeat.fetch_add(1, std::memory_order_relaxed) & 0x3FFF) == 0) { SHADOW_TRACE("shadow-trace: heartbeat value={} frame={}", frames, GetNativeLightSubmittedFrame(base)); } +#endif if (!frames) { g_shadow_trace_value = 0; return false; @@ -5434,7 +5543,7 @@ extern "C" void sub_828DA250(PPCContext& ctx, uint8_t* base) { __imp__sub_828DA250(ctx, base); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && slot == 0 && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && slot == 0 && (depth_wrapper != g_last_title_depth_wrapper || depth_surface != g_last_title_depth_surface)) { static std::atomic transition_count{0}; @@ -5671,7 +5780,7 @@ extern "C" void sub_828E0048(PPCContext& ctx, uint8_t* base) { IsNativeMode() ? IdentifyVectorFontOwner(base, resource_owner) : 0; ScopedVectorFontBinding binding(logical_font_id, resource_owner, ctx.r3.u32); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) && logical_font_id && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && logical_font_id && g_vector_font_owner_trace_count.load(std::memory_order_relaxed) < 64 && REXCVAR_QUERY(bool, gta4_trace_vector_fonts)) { const uint64_t trace = ++g_vector_font_owner_trace_count; @@ -5704,6 +5813,8 @@ extern "C" void sub_82A44B78(PPCContext& ctx, uint8_t* base) { if (stage >= kTextureStageCount) { return; } + // [ps5 port] Read once: each read of the thread-local is a call on the PS5. + const uint32_t vector_font_id = g_vector_font_id; const uint32_t slot = device + kTextureHandleBase + stage * 4; const uint32_t previous = LoadU32(base, slot); @@ -5743,7 +5854,7 @@ extern "C" void sub_82A44B78(PPCContext& ctx, uint8_t* base) { StoreU64(base, device + 24, LoadU64(base, device + 24) | dirty_mask); } - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) && g_vector_font_id && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && vector_font_id && g_vector_font_texture_trace_count.load(std::memory_order_relaxed) < 64 && REXCVAR_QUERY(bool, gta4_trace_vector_fonts)) { const uint64_t trace = ++g_vector_font_texture_trace_count; @@ -5753,7 +5864,7 @@ extern "C" void sub_82A44B78(PPCContext& ctx, uint8_t* base) { "outer-stage={} device={:08X} stage={} texture={:08X} previous={:08X} " "descriptor={:08X},{:08X},{:08X},{:08X},{:08X},{:08X} " "fetch={:08X},{:08X},{:08X},{:08X},{:08X},{:08X} dirty={:016X}", - trace, g_vector_font_id, g_vector_font_owner, g_vector_font_outer_stage, device, stage, + trace, vector_font_id, g_vector_font_owner, g_vector_font_outer_stage, device, stage, texture, previous, source_descriptor[0], source_descriptor[1], source_descriptor[2], source_descriptor[3], source_descriptor[4], source_descriptor[5], final_fetch[0], final_fetch[1], final_fetch[2], final_fetch[3], final_fetch[4], final_fetch[5], @@ -5767,7 +5878,7 @@ extern "C" void sub_82A44B78(PPCContext& ctx, uint8_t* base) { command.device = device; command.stage = stage; command.texture = texture; - command.vector_font_id = g_vector_font_id; + command.vector_font_id = vector_font_id; if (ShadowTraceActive(base)) { const int target = ShadowTargetIndex(base, texture, true); if (target >= 0) { @@ -5777,7 +5888,9 @@ extern "C" void sub_82A44B78(PPCContext& ctx, uint8_t* base) { ShadowTargetFetch(base, texture), LoadU32(base, device + kRenderTargetBase * 4)); } } - GTA4_FontSelectionTraceBinding(g_vector_font_id, g_vector_font_owner, texture, stage); + // (The owner only for a font: the trace ignores bindings without one.) + GTA4_FontSelectionTraceBinding(vector_font_id, vector_font_id ? g_vector_font_owner : 0, + texture, stage); SubmitNativeCommand(command); } @@ -5927,6 +6040,10 @@ extern "C" void sub_82A3DAB0(PPCContext& ctx, uint8_t* base) { ctx.r3.u32 = vertex_data; } +#if REX_PLATFORM_PS5 +bool SkipReflectionRestCommand(uint8_t* base, uint32_t device); // (below) +#endif + extern "C" void sub_82A3DF50(PPCContext& ctx, uint8_t* base) { if (!IsNativeMode()) { __imp__sub_82A3DF50(ctx, base); @@ -5949,6 +6066,18 @@ extern "C" void sub_82A3DF50(PPCContext& ctx, uint8_t* base) { return; } +#if REX_PLATFORM_PS5 + if (SkipReflectionRestCommand(base, device)) { + // (the state changes it took at its begin stay pending for the next draw) + for (size_t index = 0; index < g_pending_draw_primitive_up.dirty_state.words.size(); ++index) { + const uint32_t address = device + uint32_t(index * sizeof(uint64_t)); + StoreU64(base, address, LoadU64(base, address) | g_pending_draw_primitive_up.dirty_state.words[index]); + } + FreeAlignedGuestAllocation(ctx, base, g_pending_draw_primitive_up.vertex_data); + g_pending_draw_primitive_up = {}; + return; + } +#endif DrawPrimitiveUpCommand command{}; command.device = device; ApplyDrawLightingContext(command, GetNativeLightingContext()); @@ -5968,6 +6097,131 @@ extern "C" void sub_82A3DF50(PPCContext& ctx, uint8_t* base) { g_pending_draw_primitive_up = {}; } +#if REX_PLATFORM_PS5 +// [ps5 port] The reflection targets as the game creates them (sub_828DC7F0), +// so the hooks can tell a draw into a reflection (the environment map cars +// and glass reflect, the water's mirror image) without asking the renderer. +struct HookReflectionTarget { + std::atomic surface{0}, texture{0}; + std::atomic family{uint32_t(ReflectionFamily::kMirror)}; +}; +std::array g_hook_reflection_targets; +std::atomic g_hook_reflection_target_count{0}; +// A reflection rests only once it has been resolved since its target was +// made (a world load makes them again): before that its texture holds nothing +// the game drew, and the first frames of a city would sample it. +std::atomic g_hook_environment_reflection_ready{false}; +std::atomic g_hook_water_reflection_ready{false}; + +void NoteHookReflectionTarget(uint32_t surface, uint32_t texture, ReflectionFamily family) { + if (!surface) return; + const uint32_t count = g_hook_reflection_target_count.load(std::memory_order_acquire); + uint32_t index = 0; + while (index < count && g_hook_reflection_targets[index].surface.load(std::memory_order_relaxed) != surface) + ++index; + if (index == g_hook_reflection_targets.size()) return; + HookReflectionTarget& target = g_hook_reflection_targets[index]; + target.texture.store(texture, std::memory_order_relaxed); + target.family.store(uint32_t(family), std::memory_order_relaxed); + target.surface.store(surface, std::memory_order_relaxed); + if (index == count) g_hook_reflection_target_count.store(count + 1, std::memory_order_release); + if (family == ReflectionFamily::kEnvironment) + g_hook_environment_reflection_ready.store(false, std::memory_order_release); + if (family == ReflectionFamily::kWater) + g_hook_water_reflection_ready.store(false, std::memory_order_release); +} + +// A resolve of a reflection went through: from now on it may rest. +void NoteHookReflectionResolved(ReflectionFamily family) { + if (family == ReflectionFamily::kEnvironment) + g_hook_environment_reflection_ready.store(true, std::memory_order_release); + if (family == ReflectionFamily::kWater) + g_hook_water_reflection_ready.store(true, std::memory_order_release); +} + +// Whether `surface` is a reflection target, and of which family. +bool FindHookReflectionSurface(uint32_t surface, ReflectionFamily& family) { + const uint32_t count = g_hook_reflection_target_count.load(std::memory_order_acquire); + for (uint32_t index = 0; index < count; ++index) { + const HookReflectionTarget& target = g_hook_reflection_targets[index]; + if (target.surface.load(std::memory_order_relaxed) == surface) { + family = ReflectionFamily(target.family.load(std::memory_order_relaxed)); + return true; + } + } + return false; +} + +// The family of the reflection whose texture this is (false: none). +bool FindHookReflectionTexture(uint32_t texture, ReflectionFamily& family) { + if (!texture) return false; + const uint32_t count = g_hook_reflection_target_count.load(std::memory_order_acquire); + for (uint32_t index = 0; index < count; ++index) { + const HookReflectionTarget& target = g_hook_reflection_targets[index]; + if (target.texture.load(std::memory_order_relaxed) == texture) { + family = ReflectionFamily(target.family.load(std::memory_order_relaxed)); + return true; + } + } + return false; +} + +// [ps5 port] Car Reflections and Water Reflections every other frame: on the +// frames in between, a reflection's draws, clears and resolves are left out +// here, before the renderer copies them, and its texture keeps the last one +// (skipped before they cost the game's render thread anything). The water's +// frames are the car reflections' in-between ones, so with both on each frame +// draws one of them. Never inside a cached command list, which is replayed on +// other frames. +struct ReflectionRest { + bool environment = false, water = false; + bool any() const { return environment || water; } + bool covers(ReflectionFamily family) const { + return (environment && family == ReflectionFamily::kEnvironment) || + (water && family == ReflectionFamily::kWater); + } +}; + +constexpr bool kWaterReflectionRest = false; + +ReflectionRest ReflectionRestFrame(uint8_t* base, uint32_t device) { + ReflectionRest rest; + if (!device) return rest; + const uint32_t environment_every = REXCVAR_GET(ps5_environment_reflection_every); + const uint32_t water_every = REXCVAR_GET(ps5_water_reflection_every); + if (environment_every <= 1 && water_every <= 1) return rest; + if (g_native_captures_open.load(std::memory_order_relaxed) && g_active_native_capture.active) + return rest; + const uint32_t frame = LoadU32(base, device + kSubmittedFrameOffset); + rest.environment = environment_every > 1 && + g_hook_environment_reflection_ready.load(std::memory_order_relaxed) && + frame % environment_every != 0; + // (The water's rest is held back for now: the city froze loading with it on + // at 0.5b7, cause not yet found. Every frame, whatever the setting.) + rest.water = kWaterReflectionRest && water_every > 1 && + g_hook_water_reflection_ready.load(std::memory_order_relaxed) && (frame + 1) % water_every != 0; + return rest; +} + +// The renderer's test: the first colour target's reflection family, else the +// depth target's. +bool ReflectionTargetBound(uint8_t* base, uint32_t device, ReflectionFamily& family) { + const uint32_t color = LoadU32(base, device + 12432); + if (color && FindHookReflectionSurface(color, family)) return true; + const uint32_t depth = LoadU32(base, device + 12448); + return depth && FindHookReflectionSurface(depth, family); +} + +bool SkipReflectionRestCommand(uint8_t* base, uint32_t device) { + const ReflectionRest rest = ReflectionRestFrame(base, device); + ReflectionFamily family{}; + if (!rest.any() || !ReflectionTargetBound(base, device, family) || !rest.covers(family)) return false; + rex::frame_stats::g_title_reflection_commands_skipped.fetch_add(1, std::memory_order_relaxed); + return true; +} + +#endif + extern "C" void sub_82A3DF60(PPCContext& ctx, uint8_t* base) { if (!IsNativeMode()) { __imp__sub_82A3DF60(ctx, base); @@ -5984,6 +6238,9 @@ extern "C" void sub_82A3DF60(PPCContext& ctx, uint8_t* base) { } const uint32_t device = ctx.r3.u32; +#if REX_PLATFORM_PS5 + if (SkipReflectionRestCommand(base, device)) return; +#endif DrawPrimitiveCommand command{}; command.device = device; ApplyDrawLightingContext(command, GetNativeLightingContext()); @@ -6016,6 +6273,22 @@ void SubmitNativeResolve(uint8_t* base, uint32_t device, uint32_t flags, } else if (source_index < kRenderTargetCount) { source_surface = LoadU32(base, device + (kRenderTargetBase + source_index) * sizeof(uint32_t)); } +#if REX_PLATFORM_PS5 + { + // The renderer's test: the source surface's reflection family, else the + // destination texture's. + ReflectionFamily family{}; + const bool reflection = (source_surface && FindHookReflectionSurface(source_surface, family)) || + FindHookReflectionTexture(destination_texture, family); + if (reflection) { + if (ReflectionRestFrame(base, device).covers(family)) { + rex::frame_stats::g_title_reflection_commands_skipped.fetch_add(1, std::memory_order_relaxed); + return; + } + NoteHookReflectionResolved(family); + } + } +#endif command.source = CaptureSurfaceDescriptor(base, source_surface); command.flags = NormalizeResolveSampleFlags(command.flags, command.source.sample_type); @@ -6033,7 +6306,7 @@ void SubmitNativeResolve(uint8_t* base, uint32_t device, uint32_t flags, command.destination_fetch[index] = LoadU32(base, destination_texture + 28 + index * sizeof(uint32_t)); } - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && (flags & 0x04000000u)) { std::fprintf( stderr, @@ -6145,11 +6418,26 @@ void FinalizeNativeResolveBatchState(uint8_t* base, uint32_t device) { StoreU32(base, device + 12700, UINT32_MAX); } +// [ps5 port] A texture or surface past the Xbox 360's 1280x720 that isn't +// made virtual (its full size comes out of the game's own heap): once per +// caller and size, for the "Lure" rooftop hang (ps5/game/memory_watch.cpp). +void NoteConcreteLargeResource(const char* kind, uint32_t caller, uint32_t width, uint32_t height) { + if (width <= kOriginalRenderTargetWidth && height <= kOriginalRenderTargetHeight) return; + static std::mutex mutex; + static std::set seen; + { + std::lock_guard lock(mutex); + if (seen.size() >= 1024 || !seen.insert((uint64_t(caller) << 32) | (width << 16) | height).second) return; + } + REXLOG_INFO("ps5 memory: a {}x{} {} made at full size from game code {:08X}", width, height, kind, caller); +} + extern "C" void sub_82A44850(PPCContext& ctx, uint8_t* base) { const uint32_t requested_width = ctx.r3.u32; const uint32_t requested_height = ctx.r4.u32; const uint32_t return_address = ctx.lr; if (!ShouldUseNativeDisplayBacking(ctx, requested_width, requested_height)) { + if (IsNativeMode()) NoteConcreteLargeResource("texture", return_address, requested_width, requested_height); __imp__sub_82A44850(ctx, base); return; } @@ -6166,6 +6454,7 @@ extern "C" void sub_82A44970(PPCContext& ctx, uint8_t* base) { const uint32_t requested_height = ctx.r4.u32; const uint32_t return_address = ctx.lr; if (!ShouldUseNativeDisplayBacking(ctx, requested_width, requested_height)) { + if (IsNativeMode()) NoteConcreteLargeResource("surface", return_address, requested_width, requested_height); __imp__sub_82A44970(ctx, base); return; } @@ -6342,6 +6631,9 @@ extern "C" void sub_828DC7F0(PPCContext& ctx, uint8_t* base) { command.physical_height = physical_height; command.sample_count_override = GetReflectionSampleCountOverride(); const bool texture_required = reflection.role == ReflectionRole::kColor; +#if REX_PLATFORM_PS5 + NoteHookReflectionTarget(command.surface, command.texture, reflection.family); +#endif if (!command.surface || (texture_required && !command.texture) || !SubmitNativeCommand(command)) { REXLOG_ERROR( @@ -7038,7 +7330,7 @@ extern "C" void sub_82A3CC68(PPCContext& ctx, uint8_t* base) { } const uint32_t known_frontbuffer = g_last_present_frontbuffer.load(std::memory_order_relaxed); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && known_frontbuffer && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && known_frontbuffer && ctx.r6.u32 == known_frontbuffer) { std::array render_targets{}; for (uint32_t index = 0; index < kRenderTargetCount; ++index) { @@ -7096,7 +7388,7 @@ extern "C" void sub_82A3EDA8(PPCContext& ctx, uint8_t* base) { const uint32_t source_surface = device ? LoadU32(base, device + 12716) : 0; const uint32_t known_frontbuffer = g_last_present_frontbuffer.load(std::memory_order_relaxed); - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kGuestHooks) && known_frontbuffer && + if (HookDiagnosticsEnabled(rex::diagnostics::Category::kGuestHooks) && known_frontbuffer && destination_texture == known_frontbuffer) { std::fprintf(stderr, "[GuestResolveTrace] origin=batch caller=%08X device=%08X flags=%08X " @@ -7167,11 +7459,40 @@ extern "C" void sub_82A3EDA8(PPCContext& ctx, uint8_t* base) { ctx.r3.u32 = 0; } +#if REX_PLATFORM_PS5 +// [ps5 port] Small Object Shadows off: a small draw into the sun shadow map is +// dropped here, before it is handed to the renderer. Handing a draw over costs +// the game's render thread about what recording it costs the renderer, and +// the renderer's own cull of these only saved the second. Same test as there: +// a depth-only draw into a square depth surface of 512 or more. Its dirty +// state is left for the next draw to take. +bool DropSmallShadowDraw(uint8_t* base, uint32_t device, uint32_t index_count) { + const uint32_t minimum = REXCVAR_GET(gta4_native_shadow_min_indices); + if (!minimum || index_count >= minimum || !device) return false; + if (LoadU32(base, device + 12432)) return false; // a colour target: not the shadow map + const uint32_t depth = LoadU32(base, device + 12448); + if (!depth || (g_native_captures_open.load(std::memory_order_relaxed) && g_active_native_capture.active)) + return false; + const uint32_t packed = LoadU32(base, depth + 36); + const uint32_t width = (std::rotl(packed, 14) & 0x3FFF) + 1; + const uint32_t height = (std::rotl(packed, 29) & 0x7FFF) + 1; + if (width < 512 || width != height) return false; + rex::frame_stats::g_title_small_shadows_dropped.fetch_add(1, std::memory_order_relaxed); + return true; +} + +#endif + extern "C" void sub_82A3E348(PPCContext& ctx, uint8_t* base) { if (!IsNativeMode()) { __imp__sub_82A3E348(ctx, base); return; } +#if REX_PLATFORM_PS5 + if (DropSmallShadowDraw(base, ctx.r3.u32, ctx.r7.u32) || + SkipReflectionRestCommand(base, ctx.r3.u32)) + return; +#endif static std::atomic call_count{0}; const uint64_t call = NextNativeHookDiagnosticCall(call_count); @@ -7198,7 +7519,7 @@ extern "C" void sub_82A3E348(PPCContext& ctx, uint8_t* base) { command.draw_id = call; const bool trace_gbuffer_submission = - rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace) && + HookDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && (command.index_count == 5961 || command.index_count == 1056); uint64_t constants_hash_before = 0; uint64_t transform_hash_before = 0; @@ -7336,8 +7657,15 @@ extern "C" void sub_82A457B0(PPCContext& ctx, uint8_t* base) { command.depth_bits = std::bit_cast(ctx.f1.f64); command.stencil = ctx.r8.u32; - command.dirty_state = ConsumeNativeDrawDirtyState(base, device); - SubmitNativeCommand(command); +#if REX_PLATFORM_PS5 + const bool skip_clear = SkipReflectionRestCommand(base, device); +#else + constexpr bool skip_clear = false; +#endif + if (!skip_clear) { + command.dirty_state = ConsumeNativeDrawDirtyState(base, device); + SubmitNativeCommand(command); + } StoreU8(base, device + 10941, LoadU8(base, device + 10941) | uint8_t(4)); StoreU64(base, device + 16, LoadU64(base, device + 16) | uint64_t(0x00020000)); } @@ -7555,6 +7883,227 @@ extern "C" void sub_826273A0(PPCContext& ctx, uint8_t* base) { } } +#if REX_PLATFORM_PS5 +REXCVAR_DECLARE(bool, ps5_decoupled_presentation); +REXCVAR_DECLARE(uint32_t, gta4_frame_limit); +REXCVAR_DECLARE(std::string, gta4_native_frame_generation); +REXCVAR_DECLARE(std::string, ps5_frame_generation); +namespace gta4::input { +int64_t FinaleButtonQueryNs(); +} +REXCVAR_DEFINE_BOOL(ps5_cutscene_30, false, "PS5", + "Hold the game at 30 FPS during cutscenes and loading screens (the Xbox 360's rate)"); +extern std::atomic g_ps5_loading_screen_ns; + +// [ps5 port] Frame generation's cadence at 120 Hz: 60 -> 120 (each image one +// vblank) while the game and the renderer fit a frame in two vblanks, else +// 30 -> 60 (each image two). A miss is a frame whose work (the game's render +// thread less its waits for the renderer, or the recorder's) left less than +// 0.4 ms of the 16.68 ms, or that came 1.5 ms late however it was spent (the +// GPU, say); 8 misses in 30 frames drop to 30 -> 60, and 3 s with every frame +// 1.5 ms under brings 60 -> 120 back (twice as long each time 60 -> 120 fails +// again within 15 s, up to a minute: the GPU's share isn't in the load). At +// 60 Hz it is always 30 -> 60. A change re-applies the TV's flip rate (one +// visible hitch), at most once every 3 s. +static void Ps5FrameGenerationCadence(int64_t now, int64_t work_ns, int64_t late_ns) { + using namespace rex::frame_stats; + constexpr int64_t kTwoVblanks = 16683333, kSecond = 1000000000; + static bool started = false; + static uint32_t frames = 0, misses = 0; + static int64_t changed_ns = 0, roomy_since_ns = 0, returned_ns = 0, roomy_needed_ns = 3 * kSecond; + const bool high = gta4ps5::display::HighRefresh(); + const auto set = [&](uint32_t vblanks, const char* why) { + g_fg_vblanks_per_image.store(vblanks, std::memory_order_release); + changed_ns = now; + frames = misses = 0; + roomy_since_ns = 0; + if (auto* changed = g_display_pacing_changed.load(std::memory_order_acquire)) changed(why); + REXLOG_INFO("ps5 frame generation: {} ({})", + !high ? "30 -> 60 at 60 Hz" : vblanks == 1 ? "60 -> 120" : "30 -> 60 at 120 Hz", why); + }; + if (!started) { + started = true; + set(high && REXCVAR_GET(ps5_frame_generation) == "30-60" ? 2 : 1, "frame generation on"); + return; + } + const uint32_t vblanks = g_fg_vblanks_per_image.load(std::memory_order_relaxed); + if (!high) { + if (vblanks != 1) set(1, "60 Hz"); + return; + } + // 30 to 60 chosen: held there at 120 Hz too (each image two vblanks). + if (REXCVAR_GET(ps5_frame_generation) == "30-60") { + if (vblanks != 2) set(2, "30 to 60 chosen"); + return; + } + const int64_t recorder_ns = int64_t(g_last_recorder_busy_us.load(std::memory_order_relaxed)) * 1000; + const int64_t load = std::max(work_ns, recorder_ns); + if (now - changed_ns < 3 * kSecond) return; + if (vblanks == 1) { + ++frames; + misses += load > kTwoVblanks - 400000 || late_ns > 1500000; + if (misses >= 8) { + roomy_needed_ns = returned_ns && now - returned_ns < 15 * kSecond + ? std::min(2 * roomy_needed_ns, 60 * kSecond) + : 3 * kSecond; + set(2, "the game can't hold 60 here"); + } else if (frames >= 30) { + frames = misses = 0; + } + } else if (load < kTwoVblanks - 1500000) { + if (!roomy_since_ns) roomy_since_ns = now; + if (now - roomy_since_ns >= roomy_needed_ns) { + set(1, "the game holds 60 again"); + returned_ns = now; + } + } else { + roomy_since_ns = 0; + } +} + +// [ps5 port] With the renderer decoupled from the TV, the frame limit holds +// the game's render thread to its rate here, after each frame is handed over: +// an early frame waits for its time, a late one goes on at once (and moves +// the schedule when it is a whole frame late), so a 40 ms frame is shown at +// 40 ms, not at the next 33 ms slot after it. With frame generation the game +// runs at half the TV's rate instead (a quarter while the cadence says so). +static void Ps5LimitFrameRate(uint8_t* base) { + if (!rex::frame_stats::g_title_thread.load(std::memory_order_relaxed)) { + rex::frame_stats::g_title_thread.store(reinterpret_cast(pthread_self()), + std::memory_order_relaxed); + } + static uint64_t title_cpu_mask_applied = 0; + rex::frame_stats::FollowRendererCpuMask(title_cpu_mask_applied, rex::frame_stats::RendererRole::kTitle); + rex::frame_stats::NoteCpu(rex::frame_stats::g_cpus_title); + if (!REXCVAR_GET(ps5_decoupled_presentation)) return; + static int64_t next_ns = 0; + // When the previous frame left here, and the game's waits for the renderer + // so far (the 10 s timing line resets the sum). + static int64_t last_exit_ns = 0; + static uint64_t last_slot_wait_us = 0; + int64_t now = rex::frame_stats::NowNs(); + uint32_t fps = REXCVAR_GET(gta4_frame_limit); + const auto leave = [&] { last_exit_ns = now; }; + if (rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) { + const uint64_t slot_wait_us = rex::frame_stats::g_title_slot_wait_us.load(std::memory_order_relaxed); + const uint64_t waited_us = slot_wait_us >= last_slot_wait_us ? slot_wait_us - last_slot_wait_us : slot_wait_us; + last_slot_wait_us = slot_wait_us; + const int64_t work_ns = last_exit_ns ? std::max(0, now - last_exit_ns - int64_t(waited_us) * 1000) : 0; + // (how late this frame is for its slot) + Ps5FrameGenerationCadence(now, work_ns, next_ns ? now - next_ns : 0); + const uint32_t refresh = gta4ps5::display::HighRefresh() ? 120 : 60; + fps = refresh / (2 * std::max(1, rex::frame_stats::g_fg_vblanks_per_image.load(std::memory_order_relaxed))); + } + // [ps5 port] Cutscenes and loading screens at 30, as on the Xbox 360 (off + // since 1.2). It was a guess at Lure's endless rooftop loading screen, which + // hung at 30 as well: that was the game's allocator waiting for room the + // streamer never made (ps5/game/memory_watch.cpp). + if (REXCVAR_GET(ps5_cutscene_30) && !rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) { + constexpr uint32_t kCutsceneGlobal = 0x82B977F0; // (gta4_sony_feedback.cpp's) + const int64_t loading_ns = g_ps5_loading_screen_ns.load(std::memory_order_relaxed); + const int64_t steady_ns = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); + const bool story = REX_LOAD_U32(kCutsceneGlobal) != 0 || (loading_ns && steady_ns - loading_ns < 500000000); + static bool was_story = false; + if (story != was_story) { + was_story = story; + REXLOG_INFO("ps5 frame limit: {}", story ? "30 FPS for a cutscene or loading screen" : "back to the limit"); + } + if (story && (fps == 0 || fps > 30)) fps = 30; + } + // [ps5 port] The finale's helicopter climb counts taps per frame as tuned for + // 30 FPS (gta4_input_hooks.cpp): 30 while its scripts ask for buttons, and a + // moment after. + if (!rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) { + const int64_t asked_ns = gta4::input::FinaleButtonQueryNs(); + const int64_t steady_ns = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); + const bool finale = asked_ns && steady_ns - asked_ns < 1500000000; + static bool was_finale = false; + if (finale != was_finale) { + was_finale = finale; + REXLOG_INFO("ps5 frame limit: {}", finale ? "30 FPS while the finale asks for button taps" : "back to the limit"); + } + if (finale && (fps == 0 || fps > 30)) fps = 30; + } + // [ps5 port] Smooth Heavy Areas (ps5_smooth_40): at 120 Hz with a 60 limit, + // where a quarter of a second's frames run past 16.9 ms, an even 40 (three + // vblanks each) instead of 40-60 landing unevenly on the TV's 8.3 ms steps; + // back to 60 when 90% of 3 s of frames come in under 14 ms. Each frame's + // own time: from the end of the last one's hold to this call. + { + static bool at_40 = false; + static int64_t window_start = 0; + static uint32_t window_frames = 0, window_slow = 0, window_fast = 0; + const bool eligible = REXCVAR_GET(ps5_smooth_40) && fps == 60 && gta4ps5::display::HighRefresh() && + !rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed); + if (eligible && last_exit_ns) { + const int64_t frame_ns = now - last_exit_ns; + ++window_frames; + if (frame_ns > 16900000) ++window_slow; + if (frame_ns < 14000000) ++window_fast; + if (!window_start) window_start = now; + if (now - window_start >= (at_40 ? 3000000000ll : 1000000000ll)) { + const bool next = at_40 ? window_fast * 100 < window_frames * 90 : window_slow * 4 >= window_frames; + if (next != at_40) { + at_40 = next; + REXLOG_INFO("ps5 frame limit: {}", at_40 ? "an even 40 for a busy area (Smooth Heavy Areas)" + : "back to 60"); + } + window_start = now; + window_frames = window_slow = window_fast = 0; + } + } else { + at_40 = false; + window_start = 0; + window_frames = window_slow = window_fast = 0; + } + const uint32_t effective = eligible && at_40 ? 40 : 0; + if (rex::frame_stats::g_effective_frame_limit.exchange(effective, std::memory_order_acq_rel) != effective) { + if (auto* changed = rex::frame_stats::g_display_pacing_changed.load(std::memory_order_acquire)) + changed(effective ? "smooth heavy areas: 40" : "smooth heavy areas: 60"); + } + if (effective) fps = effective; + } + if (!fps) { + next_ns = 0; + leave(); + return; + } + // At the TV's own rate (59.94 Hz: 30 is 29.97 here): a plain 30 drifts + // against the vblanks and a frame slips a vblank every ~17 s. + const int64_t period = 1001000000ll / fps; // 33.367 ms at 30 + + if (!next_ns || now - next_ns >= period || next_ns - now > 2 * period) { + next_ns = now + period; + leave(); + return; + } + if (now < next_ns) { + const int64_t begin = now; + while (now < next_ns) { + const int64_t left = next_ns - now; + const timespec delay{0, long(left > 1000000 ? left - 500000 : left)}; + nanosleep(&delay, nullptr); + now = rex::frame_stats::NowNs(); + } + const uint64_t held_us = uint64_t(now - begin) / 1000; + rex::frame_stats::g_pacer_wait_us.fetch_add(held_us, std::memory_order_relaxed); + } else if (gta4ps5::display::Vrr() && (fps == 60 || fps == 120) && now - next_ns > 1500000 && + !rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) { + // [ps5 port] With VRR, a late frame moves the schedule: the next one gets + // its whole frame time. Caught up instead, it went out right behind the + // late one, and the TV showed them for 25 ms and then 8 (1.0.3 Test 2's + // drive: 1 frame in 16 shown for one refresh, nearly all after a late one). + next_ns = now; + } + next_ns += period; + leave(); +} +#endif + extern "C" void sub_82A467D8(PPCContext& ctx, uint8_t* base) { if (!IsNativeMode()) { __imp__sub_82A467D8(ctx, base); @@ -7648,9 +8197,13 @@ extern "C" void sub_82A467D8(PPCContext& ctx, uint8_t* base) { } g_last_present_frontbuffer.store(command.frontbuffer_texture, std::memory_order_relaxed); SubmitNativeCommand(command); +#if REX_PLATFORM_PS5 + Ps5LimitFrameRate(base); +#endif ctx.r3.u32 = device; } + #include "gta4_help_trace_hooks.inc" #include "gta4_fade_trace_hooks.inc" #include "gta4_phone_trace_hooks.inc" diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_player_info_alias_gate.h b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_player_info_alias_gate.h index 9bd16af5..ff8778d2 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_player_info_alias_gate.h +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_player_info_alias_gate.h @@ -1,7 +1,9 @@ #ifndef GTA4_PLAYER_INFO_ALIAS_GATE_H_ #define GTA4_PLAYER_INFO_ALIAS_GATE_H_ +#include #include +#include #include #include #include @@ -307,7 +309,25 @@ class JoinableProjectionGate { size_t reader_depth = 0; size_t projection_depth = 0; }; - ThreadState& State(std::thread::id thread) { return thread_states_[thread]; } + ThreadState& State(std::thread::id thread) { + // [ps5 port] The calling thread's own entry, cached: the game's main thread + // looks itself up on every player info read (entries are never erased, and + // a node-based map keeps them in place). + // (By the gate's serial, not its address, which a later gate can reuse.) + thread_local uint64_t cached_gate = 0; + thread_local std::thread::id cached_thread; + thread_local ThreadState* cached_state = nullptr; + if (cached_gate == serial_ && cached_thread == thread) return *cached_state; + ThreadState& state = thread_states_[thread]; + if (thread == std::this_thread::get_id()) { + cached_gate = serial_; + cached_thread = thread; + cached_state = &state; + } + return state; + } + static inline std::atomic next_serial_{1}; + const uint64_t serial_ = next_serial_.fetch_add(1, std::memory_order_relaxed); void Register(bool& waiting) noexcept { if (!waiting) { ++exclusive_waiters_; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_population_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_population_hooks.cpp index 2dcc9cda..f9980e78 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_population_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_population_hooks.cpp @@ -10,19 +10,19 @@ #include "gta4_init.h" -REXCVAR_DEFINE_DOUBLE(gta4_traffic_density_scale, 1.25, "GTA IV/Population", +REXCVAR_DEFINE_DOUBLE(gta4_traffic_density_scale, REX_PLATFORM_PS5 ? 1.0 : 1.25, "GTA IV/Population", "Scale GTA IV's requested ambient traffic density") .range(0.0, 4.0) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); -REXCVAR_DEFINE_DOUBLE(gta4_parked_car_density_scale, 1.25, "GTA IV/Population", +REXCVAR_DEFINE_DOUBLE(gta4_parked_car_density_scale, REX_PLATFORM_PS5 ? 1.0 : 1.25, "GTA IV/Population", "Scale GTA IV's requested parked-car density") .range(0.0, 4.0) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); -REXCVAR_DEFINE_DOUBLE(gta4_ped_density_scale, 1.25, "GTA IV/Population", +REXCVAR_DEFINE_DOUBLE(gta4_ped_density_scale, REX_PLATFORM_PS5 ? 1.0 : 1.25, "GTA IV/Population", "Scale GTA IV's requested ambient pedestrian density") .range(0.0, 4.0) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); -REXCVAR_DEFINE_DOUBLE(gta4_scenario_ped_density_scale, 1.25, "GTA IV/Population", +REXCVAR_DEFINE_DOUBLE(gta4_scenario_ped_density_scale, REX_PLATFORM_PS5 ? 1.0 : 1.25, "GTA IV/Population", "Scale GTA IV's requested scenario-pedestrian density") .range(0.0, 4.0) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_presentation_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_presentation_hooks.cpp index b8f4b209..8637a2f4 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_presentation_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_presentation_hooks.cpp @@ -57,7 +57,7 @@ bool GuestSpan(uint8_t* base, uint32_t address, std::size_t size, bool writable auto* heap = memory ? memory->LookupHeap(address) : nullptr; if (!heap || heap != memory->LookupHeap(static_cast(last))) return false; - const auto access = heap->QueryRangeAccess(address, static_cast(last)); + const auto access = heap->QueryRangeAccessUnlocked(address, static_cast(last)); using rex::memory::PageAccess; return access == PageAccess::kReadWrite || access == PageAccess::kExecuteReadWrite || (!writable && (access == PageAccess::kReadOnly || access == PageAccess::kExecuteReadOnly)); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_event_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_event_hooks.cpp index ae158bb5..450a03d9 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_event_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_event_hooks.cpp @@ -29,7 +29,7 @@ bool Readable(uint8_t* base, uint32_t address, uint32_t offset, uint32_t bytes, auto* memory = runtime ? runtime->memory() : nullptr; auto* heap = memory ? memory->LookupHeap(static_cast(begin)) : nullptr; if (!heap || heap != memory->LookupHeap(static_cast(end)) || - heap->QueryRangeAccess(static_cast(begin), static_cast(end)) == + heap->QueryRangeAccessUnlocked(static_cast(begin), static_cast(end)) == rex::memory::PageAccess::kNoAccess) { return false; } diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_feedback.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_feedback.cpp index 2459be2a..e719b3fc 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_feedback.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_sony_feedback.cpp @@ -61,7 +61,7 @@ bool GuestSpan(uint8_t* base, uint32_t address, size_t size, bool writable = fal auto* memory = kernel ? kernel->memory() : nullptr; auto* heap = memory ? memory->LookupHeap(address) : nullptr; if (!heap || heap != memory->LookupHeap(static_cast(last))) return false; - const auto access = heap->QueryRangeAccess(address, static_cast(last)); + const auto access = heap->QueryRangeAccessUnlocked(address, static_cast(last)); using rex::memory::PageAccess; return access == PageAccess::kReadWrite || access == PageAccess::kExecuteReadWrite || (!writable && (access == PageAccess::kReadOnly || access == PageAccess::kExecuteReadOnly)); @@ -149,14 +149,18 @@ void ReadAliased(PPCContext&, uint8_t* base) { #if !defined(GTA4_SONY_LEGACY_HOST) title_owned = GTA4_TouchTitleInputOwned(); #endif + // [ps5 port] The phone only takes the touchpad and the gun: driving with it + // up kept the pedals' adaptive triggers and the added rumble off (the + // phone counted as a menu for all of it). + const bool phone = PhoneVisible(base); state.active = gta4::sony::GameplayOwned( playing, REX_LOAD_U32(info + 1200), REX_LOAD_U32(kGameMode) == 1, - REX_LOAD_U32(ped + 2496), FrontendActive(base), PhoneVisible(base), + REX_LOAD_U32(ped + 2496), FrontendActive(base), false, REX_LOAD_U32(kMinigame) != 0, title_owned || REX_LOAD_U32(kCutscene) != 0); - state.gestures_owned = state.active; + state.gestures_owned = state.active && !phone; const uint32_t aim = kGameplayControl + kActionArray + 6 * kActionStride; - state.aiming = state.active && rex::input::mnk::DecodeActionMagnitude( - REX_LOAD_U8(aim), REX_LOAD_U8(aim + 2)) > 127; + state.aiming = state.gestures_owned && rex::input::mnk::DecodeActionMagnitude( + REX_LOAD_U8(aim), REX_LOAD_U8(aim + 2)) > 127; // Checked equivalent of sub_823D4D60: both object+600 and manager+32 // contain pointers, not inline CWeapon structures. Unarmed/special fallback @@ -172,8 +176,9 @@ void ReadAliased(PPCContext&, uint8_t* base) { // continuing shot. Other states are rejected even with rounds left in // the clip, so partial-clip reloads must release trigger resistance too. const uint32_t weapon_state = REX_LOAD_U32(weapon + 24); - state.can_fire = state.active && REX_LOAD_U16(weapon + 28) != 0 && + state.can_fire = state.gestures_owned && REX_LOAD_U16(weapon + 28) != 0 && (weapon_state == 0 || weapon_state == 1); + state.weapon_slot = static_cast(slot); } } if (state.active && state.in_vehicle && state.driver) { @@ -182,11 +187,13 @@ void ReadAliased(PPCContext&, uint8_t* base) { state.trigger_context = sony::TriggerContext::kWeapon; } - // Entity position selection in sub_822343C0: matrix+48 or inline+16. - const uint32_t matrix = REX_LOAD_U32(ped + 32); + // Entity position selection in sub_822343C0: matrix+48 or inline+16 (the + // vehicle's when in one: its speed drives the pedals' feel). + const uint32_t entity = state.in_vehicle ? player.vehicle : ped; + const uint32_t matrix = REX_LOAD_U32(entity + 32); uint32_t position = 0; if (matrix && GuestSpan(base, matrix, 60)) position = matrix + 48; - if (!matrix) position = ped + 16; + if (!matrix) position = entity + 16; if (position) { player.position_valid = true; for (size_t axis = 0; axis < player.position.size(); ++axis) { @@ -259,10 +266,44 @@ uint64_t poll_sequence = 0; } // namespace +// How fast the local player (or their vehicle) moves, from its position at each +// poll, smoothed; a jump of more than 50 m (a teleport, a respawn) starts over. +static float Speed(const GTA4SonyLocalPlayer& player, bool valid, uint64_t now_ms) { + static std::mutex mutex; + static std::array last{}; + static uint64_t last_ms = 0; + static uint32_t last_context = 0; + static float speed = 0; + std::lock_guard lock(mutex); + if (!valid || !player.position_valid) { + last_ms = 0; + return speed = 0; + } + const float dt = last_ms && now_ms > last_ms ? float(now_ms - last_ms) / 1000.0f : 0.0f; + if (dt > 0.0f && dt < 0.010f) return speed; // (the same frame polled twice) + float distance = 0; + for (size_t axis = 0; axis < 3; ++axis) { + const float d = player.position[axis] - last[axis]; + distance += d * d; + } + distance = std::sqrt(distance); + if (dt <= 0.0f || dt > 0.5f || distance > 50.0f || + last_context != player.state.context_identity) { + speed = 0; + } else { + speed = speed * 0.7f + (distance / dt) * 0.3f; + } + last = player.position; + last_ms = now_ms; + last_context = player.state.context_identity; + return speed; +} + void GTA4_SonyEndPoll(PPCContext& context, uint8_t* base) { - const auto snapshot = ReadSnapshot(context, base); + auto snapshot = ReadSnapshot(context, base); const auto options = sony::GetOptions(); const uint64_t now = SDL_GetTicks(); + snapshot.player.state.speed = Speed(snapshot.player, snapshot.valid, now); std::array input; for (uint32_t user = 0; user < input.size(); ++user) { input[user] = sony::GetService().Consume(user, now, options); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_temporal_hooks.inc b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_temporal_hooks.inc index 962ad6a6..85f9f497 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_temporal_hooks.inc +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_temporal_hooks.inc @@ -14,13 +14,24 @@ uint64_t epoch = 1; std::mutex scene_mutex; class Scope { public: - Scope(uint64_t matrix, uint64_t drawable, uint64_t pose) : old_(instance) { - instance = {matrix, drawable, pose}; + // The enclosing scope's matrix or pose. + static constexpr uint64_t kInherit = ~uint64_t(0); + // [ps5 port] Only with temporal output on: otherwise the thread_local reads + // and writes (calls on the PS5) bought nothing on every model drawn. + Scope(uint64_t matrix, uint64_t drawable, uint64_t pose) { + if (!enabled.load(std::memory_order_relaxed)) return; + active_ = true; + old_ = instance; + instance = {matrix == kInherit ? old_.matrix : matrix, drawable, + pose == kInherit ? old_.pose : pose}; + } + ~Scope() { + if (active_) instance = old_; } - ~Scope() { instance = old_; } private: Instance old_; + bool active_ = false; }; bool Requested() { const auto upscaler = rex::cvar::GetFlagByName("gta4_native_upscaler"); @@ -28,13 +39,16 @@ bool Requested() { upscaler == "metalfx" || upscaler == "fsr3" || upscaler == "dlss"; } void Begin(uint8_t* base, uint32_t device, uint32_t viewport) { - const bool requested=Requested(); + bool requested=Requested(); + DeviceCapabilitiesResult caps{}; + // [ps5 port] Chosen but unavailable (FSR 3 failed to start): no temporal + // inputs, so every command is not wrapped in a pass envelope for nothing. + if (requested && + (!QueryNativeDeviceCapabilities(caps) || !(caps.capabilities & kCapabilityTemporalInputs))) + requested = false; gta4::gpu_pass::temporal_inputs.store(requested,std::memory_order_relaxed); if (!device || !viewport || !requested) return; - DeviceCapabilitiesResult caps{}; - if (!QueryNativeDeviceCapabilities(caps) || !(caps.capabilities & kCapabilityTemporalInputs)) - return; TemporalCommand next; next.event = TemporalEvent::kBeginScene; next.device = device; diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_touch_guest_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_touch_guest_hooks.cpp index 0650b771..3722cd94 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_touch_guest_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_touch_guest_hooks.cpp @@ -43,7 +43,7 @@ bool RadarSpan(uint8_t* base, uint32_t address, size_t size, bool write = false) auto* heap = memory ? memory->LookupHeap(address) : nullptr; const uint32_t last = uint32_t(uint64_t(address) + size - 1); if (!heap || heap != memory->LookupHeap(last)) return false; - const auto access = heap->QueryRangeAccess(address, last); + const auto access = heap->QueryRangeAccessUnlocked(address, last); using rex::memory::PageAccess; return access == PageAccess::kReadWrite || access == PageAccess::kExecuteReadWrite || (!write && (access == PageAccess::kReadOnly || access == PageAccess::kExecuteReadOnly)); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_transition_hooks.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_transition_hooks.cpp index 9052d3be..3effe121 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/gta4_transition_hooks.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/gta4_transition_hooks.cpp @@ -149,7 +149,7 @@ extern "C" void sub_821BB2D0(PPCContext& ctx, uint8_t* base) { auto* kernel=REX_KERNEL_STATE();auto* memory=kernel?kernel->memory():nullptr; auto* heap=memory?memory->LookupHeap(address):nullptr; if(!base||!heap||address>UINT32_MAX-3||heap!=memory->LookupHeap(address+3))return {}; - const auto access=heap->QueryRangeAccess(address,address+3); + const auto access=heap->QueryRangeAccessUnlocked(address,address+3); using rex::memory::PageAccess; if(access!=PageAccess::kReadOnly&&access!=PageAccess::kReadWrite&& access!=PageAccess::kExecuteReadOnly&&access!=PageAccess::kExecuteReadWrite)return {}; @@ -191,7 +191,7 @@ std::array HandoffGuestName(uint8_t* base,uint32_t address) { auto* kernel=REX_KERNEL_STATE();auto* memory=kernel?kernel->memory():nullptr; auto* heap=memory?memory->LookupHeap(address):nullptr; if(!base||!heap||address>UINT32_MAX-79||heap!=memory->LookupHeap(address+79))return text; - const auto access=heap->QueryRangeAccess(address,address+79); + const auto access=heap->QueryRangeAccessUnlocked(address,address+79); using rex::memory::PageAccess; if(access!=PageAccess::kReadOnly&&access!=PageAccess::kReadWrite&&access!=PageAccess::kExecuteReadOnly&&access!=PageAccess::kExecuteReadWrite)return text; for(size_t i=0;i+1(uint64_t{address} + size - 1); if (!heap || heap != memory->LookupHeap(last)) return false; using rex::memory::PageAccess; - const auto access = heap->QueryRangeAccess(address, last); + const auto access = heap->QueryRangeAccessUnlocked(address, last); if (access != PageAccess::kReadOnly && access != PageAccess::kExecuteReadOnly && access != PageAccess::kReadWrite && access != PageAccess::kExecuteReadWrite) return false; std::memcpy(output, base + address + REX_PHYS_HOST_OFFSET(address), size); @@ -311,7 +311,7 @@ uint32_t QueryVehicleEntry(void* opaque, uint32_t ped, uint32_t forward) { const uint32_t stack = original_sp - kCallerFrame; auto* heap = memory ? memory->LookupHeap(stack) : nullptr; if (!heap || heap != memory->LookupHeap(original_sp - 1)) return 0; - const auto access = heap->QueryRangeAccess(stack, original_sp - 1); + const auto access = heap->QueryRangeAccessUnlocked(stack, original_sp - 1); using rex::memory::PageAccess; if (access != PageAccess::kReadWrite && access != PageAccess::kExecuteReadWrite) return 0; ScopedVehicleQueryContext scope(query.parent); diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_install_io.h b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_install_io.h new file mode 100644 index 00000000..c60abf22 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_install_io.h @@ -0,0 +1,486 @@ +// The installer's file reading and writing: positional reads, big sequential +// writes, and a few threads to copy files side by side. +// +// [ps5 port] What made installing on the PS5 crawl: +// - C++ file streams hand the file 4 KB at a time, and each small write is +// slow there. Here a write goes out 8 MB at a time. +// - Everything read or written through the file cache stays in memory, and +// while an app runs the console has little to spare: after about 2 GB the +// cache is full and every read and write waits on the console freeing +// memory (85 MB/s fell to a few). So reads and writes go past the cache +// (O_DIRECT, rex/filesystem/direct_io.h), as games do: data gathers in an +// aligned buffer and goes out in aligned 8 MB pieces. +// - The written data is flushed as it goes (each file every 16 MB, small +// files every 64 MB), from the writing thread. +// - Files are copied three at a time, each thread with buffers made once +// (a big allocation per file wears the console's heap down). +// The time spent reading, writing and flushing is counted for the log and +// the install screen. +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#if defined(_WIN32) +#include +#else +#include +#include +#include +#include + +#include +#endif + +namespace gta4::install::io { + +// Bytes and time spent reading, writing and flushing (since the app started). +struct Stats { + std::atomic read_bytes{0}, read_ns{0}, write_bytes{0}, write_ns{0}, sync_ns{0}, syncs{0}; + std::atomic direct_files{0}, cached_files{0}, reserve_ns{0}; +}; + +// How the installer writes, for the drive it writes to (set before an install). +// - preallocate: each file is made its full size before it's written. On an +// exFAT drive a file that grows write by write gets slower the bigger it +// gets (its cluster chain is extended again and again: 6 MB/s falling to +// 1); a file made full size first is written straight through. +// - direct: writes go past the file cache (an internal UFS drive). +// - threads: files copied at once (one on an external drive). +struct WritePolicy { + bool preallocate = false; + uint64_t reserve_step = 0; // made bigger this much ahead of the writing (0: all at once) + bool direct = true; + int threads = 3; +}; +inline WritePolicy& Policy() { + static WritePolicy policy; + return policy; +} + +// The file being written now (the last one opened), for the install screen. +namespace detail { +struct CurrentFile { + std::mutex mutex; + std::string path; + uint64_t size = 0; +}; +inline CurrentFile& Now() { + static CurrentFile current; + return current; +} +} // namespace detail +inline void SetCurrent(const std::filesystem::path& path, uint64_t size) { + std::lock_guard lock(detail::Now().mutex); + detail::Now().path = path.string(); + detail::Now().size = size; +} +inline void Current(std::string& path, uint64_t& size) { + std::lock_guard lock(detail::Now().mutex); + path = detail::Now().path; + size = detail::Now().size; +} +inline Stats& GetStats() { + static Stats stats; + return stats; +} + +namespace detail { +inline uint64_t NowNs() { + return uint64_t(std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count()); +} + +#if REX_PLATFORM_PS5 +constexpr bool kFlushAsWeGo = true; +#else +constexpr bool kFlushAsWeGo = false; +#endif +// A file's own data is flushed every this many bytes, and at its end when +// it's at least kFlushFileMin; smaller files add up toward kFlushSmall. +constexpr uint64_t kFlushEvery = 16ull << 20; +constexpr uint64_t kFlushFileMin = 1ull << 20; +constexpr uint64_t kFlushSmall = 64ull << 20; + +inline std::atomic& SmallUnflushed() { + static std::atomic bytes{0}; + return bytes; +} +} // namespace detail + +// Memory aligned for direct transfers (64 KiB), grown as needed and kept. +class AlignedBuffer { + public: + static constexpr size_t kAlign = size_t(64) << 10; + + AlignedBuffer() = default; + AlignedBuffer(const AlignedBuffer&) = delete; + AlignedBuffer& operator=(const AlignedBuffer&) = delete; + ~AlignedBuffer() { Free(); } + + // At least `bytes` (false if that much couldn't be had). + bool Reserve(size_t bytes) { + if (bytes <= size_) return true; + Free(); +#if defined(_WIN32) + data_ = static_cast(_aligned_malloc(bytes, kAlign)); +#else + void* p = nullptr; + data_ = posix_memalign(&p, kAlign, bytes) == 0 ? static_cast(p) : nullptr; +#endif + size_ = data_ ? bytes : 0; + return data_ != nullptr; + } + uint8_t* data() const { return data_; } + size_t size() const { return size_; } + + private: + void Free() { +#if defined(_WIN32) + _aligned_free(data_); +#else + std::free(data_); +#endif + data_ = nullptr; + size_ = 0; + } + uint8_t* data_ = nullptr; + size_t size_ = 0; +}; + +// A file read at any offset (safe from several threads at once). +class InFile { + public: + InFile() = default; + InFile(const InFile&) = delete; + InFile& operator=(const InFile&) = delete; + ~InFile() { Close(); } + + bool Open(const std::filesystem::path& path) { + Close(); +#if defined(_WIN32) + file_ = _wfopen(path.c_str(), L"rb"); + if (!file_) return false; + _fseeki64(file_, 0, SEEK_END); + size_ = uint64_t(_ftelli64(file_)); + return true; +#else + if (!reader_.Open(path.c_str())) return false; + size_ = reader_.size(); + return true; +#endif + } + + uint64_t size() const { return size_; } + + // Exactly `length` bytes at `offset` (false short of that). + bool ReadAt(uint64_t offset, void* out, size_t length) { + const uint64_t start = detail::NowNs(); +#if defined(_WIN32) + bool ok = false; + { + std::lock_guard lock(win_mutex_); + ok = file_ && _fseeki64(file_, int64_t(offset), SEEK_SET) == 0 && + std::fread(out, 1, length, file_) == length; + } +#else + const bool ok = reader_.ReadAt(offset, out, length); +#endif + if (ok) GetStats().read_bytes += length; + GetStats().read_ns += detail::NowNs() - start; + return ok; + } + + void Close() { +#if defined(_WIN32) + if (file_) std::fclose(file_), file_ = nullptr; +#else + reader_.Close(); +#endif + } + + private: +#if defined(_WIN32) + FILE* file_ = nullptr; + std::mutex win_mutex_; +#else + rex::filesystem::direct::Reader reader_; +#endif + uint64_t size_ = 0; +}; + +// A new file written from start to end. What's written gathers in an aligned +// buffer (the caller's, kept between files) and goes out 8 MB at a time. +class OutFile { + public: + static constexpr size_t kChunk = size_t(8) << 20; + + OutFile() = default; + OutFile(const OutFile&) = delete; + OutFile& operator=(const OutFile&) = delete; + ~OutFile() { + if (open_) Close(); + } + + // `gather`: the buffer (null: one of its own). `size`: what the file will + // be (0: not known), so it can be made that size first (Policy()). + bool Open(const std::filesystem::path& path, AlignedBuffer* gather = nullptr, uint64_t size = 0) { + gather_ = gather ? gather : &own_; + if (!gather_->Reserve(kChunk)) return false; +#if defined(_WIN32) + file_ = _wfopen(path.c_str(), L"wb"); + open_ = file_ != nullptr; +#else + direct_ = false; + if (rex::filesystem::direct::kEnabled && Policy().direct) { + fd_ = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, 0666); + direct_ = fd_ >= 0; + } + if (fd_ < 0) fd_ = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + open_ = fd_ >= 0; + reserved_ = 0; + size_ = size; + if (open_ && size && Policy().preallocate) ReserveFor(0); +#endif + if (open_) SetCurrent(path, size); + ok_ = open_; + pending_ = 0; + written_ = unflushed_ = 0; + return open_; + } + + bool Write(const void* data, size_t length) { + const auto* p = static_cast(data); + while (ok_ && length) { + const size_t take = std::min(length, kChunk - pending_); + std::memcpy(gather_->data() + pending_, p, take); + pending_ += take, p += take, length -= take; + if (pending_ == kChunk) Flush(); + } + return ok_; + } + + // Writes what's waiting and closes the file; false if any write failed. + bool Close() { + if (!open_) return ok_; + Flush(); +#if !defined(_WIN32) + (direct_ ? GetStats().direct_files : GetStats().cached_files)++; + if (detail::kFlushAsWeGo && ok_ && unflushed_) { + if (written_ >= detail::kFlushFileMin) { + Sync(); + } else if ((detail::SmallUnflushed() += unflushed_) >= detail::kFlushSmall) { + // the small files' data, all at once + detail::SmallUnflushed() = 0; + const uint64_t start = detail::NowNs(); + ::sync(); + GetStats().sync_ns += detail::NowNs() - start; + ++GetStats().syncs; + } + } + // (made bigger than what was written: back to what was written) + if (reserved_ && reserved_ != written_ && ::ftruncate(fd_, off_t(written_)) != 0) ok_ = false; + if (::close(fd_) != 0) ok_ = false; + fd_ = -1; +#else + if (std::fclose(file_) != 0) ok_ = false; + file_ = nullptr; +#endif + open_ = false; + return ok_; + } + + private: + void Flush() { + if (pending_ && ok_) Out(gather_->data(), pending_); + pending_ = 0; + } + + void Out(const uint8_t* p, size_t length) { +#if !defined(_WIN32) + // (made big enough first, when the policy says so) + if (size_ && Policy().preallocate && written_ + length > reserved_ && reserved_ < size_) { + ReserveFor(written_ + length); + } +#endif + const uint64_t start = detail::NowNs(); + size_t left = length; +#if defined(_WIN32) + left -= std::fwrite(p, 1, left, file_); +#else + while (left) { + const ssize_t n = ::write(fd_, p, left); + if (n < 0 && errno == EINTR) continue; + if (n < 0 && errno == EINVAL && direct_) { + // the kernel won't write this directly (the last piece's size): + // through the cache from here + rex::filesystem::direct::TurnOff(fd_); + direct_ = false; + continue; + } + if (n <= 0) break; + p += n, left -= size_t(n); + } +#endif + GetStats().write_bytes += length - left; + GetStats().write_ns += detail::NowNs() - start; + if (left) { + ok_ = false; + return; + } + written_ += length; + unflushed_ += length; +#if !defined(_WIN32) + if (detail::kFlushAsWeGo && unflushed_ >= detail::kFlushEvery) Sync(); +#endif + } + +#if !defined(_WIN32) + // The file made bigger ahead of the writing (Policy()): up to its full size, + // or one step past what's written. + void ReserveFor(uint64_t need) { + const uint64_t step = Policy().reserve_step; + const uint64_t target = step ? std::min(size_, std::max(need, written_) + step) : size_; + if (target <= reserved_) return; + const uint64_t start = detail::NowNs(); + if (::ftruncate(fd_, off_t(target)) == 0) reserved_ = target; + GetStats().reserve_ns += detail::NowNs() - start; + } + + void Sync() { + const uint64_t start = detail::NowNs(); + ::fsync(fd_); + GetStats().sync_ns += detail::NowNs() - start; + ++GetStats().syncs; + unflushed_ = 0; + } +#endif + +#if defined(_WIN32) + FILE* file_ = nullptr; +#else + int fd_ = -1; + bool direct_ = false; + uint64_t reserved_ = 0, size_ = 0; +#endif + bool open_ = false, ok_ = false; + AlignedBuffer* gather_ = nullptr; + AlignedBuffer own_; + size_t pending_ = 0; + uint64_t written_ = 0, unflushed_ = 0; +}; + +// A few threads running jobs from a queue. Each thread has buffers of its +// own (Scratch), made the first time a job needs them and kept. The first +// job that fails stops the rest; Wait() reports its error. +class WorkerPool { + public: + struct Scratch { + AlignedBuffer read, write, gather; + }; + using Job = std::function; + + explicit WorkerPool(int threads) { + for (int i = 0; i < std::max(1, threads); ++i) threads_.emplace_back([this] { Run(); }); + } + ~WorkerPool() { + { + std::lock_guard lock(mutex_); + quit_ = true; + } + work_.notify_all(); + for (auto& thread : threads_) thread.join(); + } + WorkerPool(const WorkerPool&) = delete; + WorkerPool& operator=(const WorkerPool&) = delete; + + // Queues a job (waiting while the queue is long). False once one has failed. + bool Add(Job job) { + std::unique_lock lock(mutex_); + room_.wait(lock, [this] { return queue_.size() < kMaxQueued || failed_; }); + if (failed_) return false; + queue_.push_back(std::move(job)); + work_.notify_one(); + return true; + } + + // Waits for every queued job. False, with the first error, if one failed. + bool Wait(std::string& error) { + std::unique_lock lock(mutex_); + idle_.wait(lock, [this] { return queue_.empty() && busy_ == 0; }); + if (failed_) { + error = error_; + return false; + } + return true; + } + + private: + static constexpr size_t kMaxQueued = 256; + + void Run() { + Scratch scratch; + for (;;) { + Job job; + { + std::unique_lock lock(mutex_); + work_.wait(lock, [this] { return quit_ || !queue_.empty(); }); + if (queue_.empty()) return; + job = std::move(queue_.front()); + queue_.pop_front(); + ++busy_; + room_.notify_one(); + } + std::string error; + bool ok = false; + try { + ok = job(scratch, error); + } catch (const std::exception& exception) { + error = exception.what(); + } catch (...) { + error = "A file could not be installed."; + } + { + std::lock_guard lock(mutex_); + --busy_; + if (!ok && !failed_) { + failed_ = true; + error_ = error.empty() ? "A file could not be installed." : std::move(error); + queue_.clear(); + room_.notify_all(); + } + if (queue_.empty() && busy_ == 0) idle_.notify_all(); + } + } + } + + std::mutex mutex_; + std::condition_variable work_, room_, idle_; + std::deque queue_; + std::vector threads_; + int busy_ = 0; + bool quit_ = false, failed_ = false; + std::string error_; +}; + +// How many files are copied at once (for the drive written to). +inline int CopyThreads() { return std::max(1, Policy().threads); } + +} // namespace gta4::install::io diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.cpp index 84a17237..14675536 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.cpp @@ -31,7 +31,10 @@ #include #include +#include + #include "TinySHA1.hpp" +#include "gta4_install_io.h" #include "gta4_rpf_extractor.h" #ifndef GTA4_RPF_AES_KEY_SOURCE @@ -51,7 +54,7 @@ constexpr std::string_view kRequiredPatchSha256 = constexpr std::string_view kEmbeddedTargetXexSha256 = "8268fdc91f83c4288e1db8319a109e622865497d1405d9f4ca98f46830d1ff21"; constexpr size_t kMaximumMetadataFileSize = 64_MiB; -constexpr size_t kCopyBufferSize = 1_MiB; +constexpr size_t kCopyBufferSize = 8_MiB; constexpr size_t kMaximumTreeEntries = 500000; struct FileDestroyer { @@ -335,7 +338,10 @@ bool ValidatePatch(const XexInfo& base, const XexInfo& patch, std::spansize())) { error = "Could not create " + destination.string() + "."; return false; } - std::vector buffer(kCopyBufferSize); + // (the worker's buffer, kept between files) + io::AlignedBuffer& buffer = scratch.read; + if (!buffer.Reserve(kCopyBufferSize)) { + error = "Not enough memory to copy " + entry->path() + "."; + return false; + } size_t offset = 0; while (offset < entry->size()) { if (progress.cancel_requested.load(std::memory_order_relaxed)) { @@ -483,32 +495,59 @@ bool CopyFileEntry(rex::filesystem::Entry* entry, const std::filesystem::path& d return false; } const size_t remaining = entry->size() - offset; - const size_t request = std::min(remaining, buffer.size()); + const size_t request = std::min(remaining, kCopyBufferSize); size_t bytes_read = 0; + const uint64_t read_start = io::detail::NowNs(); const rex::X_STATUS read_status = - file->ReadSync(std::span(buffer).first(request), offset, &bytes_read); + file->ReadSync(std::span(buffer.data(), request), offset, &bytes_read); + io::GetStats().read_ns += io::detail::NowNs() - read_start; + io::GetStats().read_bytes += bytes_read; if (read_status != 0 || bytes_read == 0 || bytes_read > request) { error = "Failed while reading " + entry->path() + "."; return false; } - output.write(reinterpret_cast(buffer.data()), bytes_read); - if (!output) { + if (!output.Write(buffer.data(), bytes_read)) { error = "Failed while writing " + destination.string() + "."; return false; } offset += bytes_read; progress.copied_bytes.fetch_add(bytes_read, std::memory_order_relaxed); } - output.flush(); - if (!output) { + if (!output.Close()) { error = "Failed while flushing " + destination.string() + "."; return false; } + const double seconds = double(io::detail::NowNs() - started) / 1e9; + if (seconds > 5) { // (a slow file, for the log) + REXLOG_INFO("Installer: {} ({} MB) took {:.1f} s", entry->path(), entry->size() >> 20, seconds); + } return true; } +// How many files of a source can be copied at once: one for an Xbox content +// package (its reads share one file position), more for a disc image or a +// folder. +int CopyThreadsFor(rex::filesystem::Device* device) { + if (dynamic_cast(device) || + dynamic_cast(device)) { + return io::CopyThreads(); + } + return 1; +} + bool CopyTree(rex::filesystem::Entry* root, const std::filesystem::path& destination, Progress& progress, std::string& error) { + // The folders are made here, in order; the files are copied by the pool, + // several at once. + io::WorkerPool pool(CopyThreadsFor(root->device())); + auto finish = [&](bool ok) { + std::string job_error; + const bool jobs_ok = pool.Wait(job_error); + if (ok && !jobs_ok) { + error = std::move(job_error); + } + return ok && jobs_ok; + }; struct Pending { rex::filesystem::Entry* entry; std::filesystem::path relative_path; @@ -530,13 +569,13 @@ bool CopyTree(rex::filesystem::Entry* root, const std::filesystem::path& destina pending.pop_back(); if (++visited > kMaximumTreeEntries) { error = "The source contains an unexpectedly large directory tree."; - return false; + return finish(false); } const std::string collision_key = Lowercase(current.relative_path.generic_string()); if (!destinations.insert(collision_key).second) { error = "The source contains colliding case-insensitive paths."; - return false; + return finish(false); } const auto output_path = destination / current.relative_path; @@ -545,21 +584,28 @@ bool CopyTree(rex::filesystem::Entry* root, const std::filesystem::path& destina std::filesystem::create_directories(output_path, fs_error); if (fs_error) { error = "Could not create an installation directory: " + fs_error.message(); - return false; + return finish(false); } for (const auto& child : current.entry->children()) { auto component = SafeComponent(child->name()); if (!component) { error = "The source contains an unsafe path component."; - return false; + return finish(false); } pending.push_back({child.get(), current.relative_path / *component}); } - } else if (!CopyFileEntry(current.entry, output_path, progress, error)) { - return false; + } else { + rex::filesystem::Entry* entry = current.entry; + const bool queued = pool.Add([entry, output_path, &progress](io::WorkerPool::Scratch& scratch, + std::string& job_error) { + return CopyFileEntry(entry, output_path, progress, scratch, job_error); + }); + if (!queued) { + break; // (a copy failed: finish() reports it) + } } } - return true; + return finish(true); } bool CopyHostFile(const std::filesystem::path& source, const std::filesystem::path& destination, @@ -571,35 +617,35 @@ bool CopyHostFile(const std::filesystem::path& source, const std::filesystem::pa return false; } - std::ifstream input(source, std::ios::binary); - std::ofstream output(destination, std::ios::binary | std::ios::trunc); - if (!input || !output) { + io::InFile input; + io::OutFile output; + if (!input.Open(source) || !output.Open(destination, nullptr, input.size())) { error = "Could not copy " + source.string() + "."; return false; } - std::vector buffer(kCopyBufferSize); - while (input) { + std::vector buffer(kCopyBufferSize); + for (uint64_t offset = 0; offset < input.size();) { if (progress.cancel_requested.load(std::memory_order_relaxed)) { error = "Installation was cancelled."; return false; } - input.read(buffer.data(), buffer.size()); - const std::streamsize count = input.gcount(); - if (count > 0) { - output.write(buffer.data(), count); - if (!output) { - error = "Failed while writing " + destination.string() + "."; - return false; - } - progress.copied_bytes.fetch_add(static_cast(count), std::memory_order_relaxed); + const size_t count = size_t(std::min(buffer.size(), input.size() - offset)); + if (!input.ReadAt(offset, buffer.data(), count)) { + error = "Failed while reading " + source.string() + "."; + return false; + } + if (!output.Write(buffer.data(), count)) { + error = "Failed while writing " + destination.string() + "."; + return false; } + offset += count; + progress.copied_bytes.fetch_add(count, std::memory_order_relaxed); } - if (!input.eof()) { - error = "Failed while reading " + source.string() + "."; + if (!output.Close()) { + error = "Failed while writing " + destination.string() + "."; return false; } - output.flush(); - return static_cast(output); + return true; } std::optional PrepareGameSource(const std::filesystem::path& path, @@ -710,6 +756,42 @@ rex::filesystem::Entry* FindDlcPayloadRoot(rex::filesystem::Entry* root, Episode return nullptr; } +// [ps5 port] The folder FindDlcPayloadRoot finds, and its path from the top. +struct DlcPayload { + rex::filesystem::Entry* root = nullptr; + std::filesystem::path relative_path; +}; +std::optional FindDlcPayload(rex::filesystem::Entry* root, Episode episode) { + struct Pending { + rex::filesystem::Entry* entry; + std::filesystem::path relative_path; + }; + std::vector pending; + pending.push_back({root, {}}); + size_t visited = 0; + while (!pending.empty()) { + Pending current = std::move(pending.back()); + pending.pop_back(); + if (++visited > kMaximumTreeEntries) { + return std::nullopt; + } + // (as FindDlcPayloadRoot: setup2.xml, content.dat and the episode's own + // audio list, not the other episode's) + if (FindDirectChild(current.entry, "setup2.xml") && FindDirectChild(current.entry, "content.dat") && + FindDirectChild(current.entry, episode == Episode::kTlad ? "e1_audio.xml" : "e2_audio.xml") && + !FindDirectChild(current.entry, episode == Episode::kTlad ? "e2_audio.xml" : "e1_audio.xml")) { + return DlcPayload{current.entry, current.relative_path}; + } + for (const auto& child : current.entry->children()) { + if (!IsDirectory(child.get())) continue; + auto component = SafeComponent(child->name()); + if (!component) return std::nullopt; + pending.push_back({child.get(), current.relative_path / *component}); + } + } + return std::nullopt; +} + std::optional PrepareDlcSource(const DlcSelection& selection, std::string& error) { auto mounted = MountSource(selection.source, error); if (!mounted) { @@ -881,6 +963,49 @@ bool ValidateInstalledPair(const std::filesystem::path& game_root, std::string& return ParseXex(patch_bytes, patch, reason) && ValidatePatch(base, patch, patch_bytes, reason); } +// [ps5 port] Every file under `root` in `destination`, at its size (an +// install written by someone else, checked before it's used). +bool MatchesTree(rex::filesystem::Entry* root, const std::filesystem::path& destination, + std::string& error) { + struct Pending { + rex::filesystem::Entry* entry; + std::filesystem::path relative_path; + }; + std::vector pending; + pending.push_back({root, {}}); + size_t visited = 0; + while (!pending.empty()) { + Pending current = std::move(pending.back()); + pending.pop_back(); + if (++visited > kMaximumTreeEntries) { + error = "The source contains an unexpectedly large directory tree."; + return false; + } + if (IsDirectory(current.entry)) { + for (const auto& child : current.entry->children()) { + auto component = SafeComponent(child->name()); + if (!component) { + error = "The source contains an unsafe path component."; + return false; + } + pending.push_back({child.get(), current.relative_path / *component}); + } + continue; + } + std::error_code fs_error; + const auto size = std::filesystem::file_size(destination / current.relative_path, fs_error); + if (fs_error) { + error = current.relative_path.generic_string() + " is missing from the installed game."; + return false; + } + if (size != current.entry->size()) { + error = current.relative_path.generic_string() + " wasn't copied whole."; + return false; + } + } + return true; +} + } // namespace const char* EpisodeDirectory(Episode episode) { @@ -1026,8 +1151,15 @@ Result Install(const Selection& selection, const std::filesystem::path& install_ result.error = "Could not create the staging directory: " + fs_error.message(); return result; } +#if REX_PLATFORM_PS5 + // [ps5 port] A title can't learn the free space (the SDK's statvfs always + // answers 16 GiB): not checked. + const bool enough_space = true; +#else const auto available_space = std::filesystem::space(staging_root, fs_error); - if (fs_error || available_space.available < total) { + const bool enough_space = !fs_error && available_space.available >= total; +#endif + if (!enough_space) { result.error = fs_error ? "Could not determine available installation space: " + fs_error.message() : "There is not enough free space for the GTA IV installation."; @@ -1069,7 +1201,8 @@ Result Install(const Selection& selection, const std::filesystem::path& install_ if (!CopyHostFile(selection.update_source, sibling_patch, progress, result.error)) { return fail(std::move(result.error)); } - } else if (!CopyFileEntry(update->patch.entry, sibling_patch, progress, result.error)) { + } else if (io::WorkerPool::Scratch scratch; + !CopyFileEntry(update->patch.entry, sibling_patch, progress, scratch, result.error)) { return fail(std::move(result.error)); } } @@ -1134,4 +1267,182 @@ Result Install(const Selection& selection, const std::filesystem::path& install_ } } +bool PlanInstall(const Selection& selection, InstallPlan& plan, std::string& error) { + try { + plan = {}; + if (selection.game_source.empty() || selection.update_source.empty() || + !selection.dlc_sources.empty()) { + error = "Only the game and its title update can be installed this way."; + return false; + } + const GameSourceInspection inspection = InspectGameSource(selection.game_source); + if (!inspection.supported()) { + error = inspection.rejection_reason; + return false; + } + auto game = PrepareGameSource(selection.game_source, error); + if (!game) { + return false; + } + auto update = PrepareUpdateSource(selection.update_source, game->xex_info, error); + if (!update) { + return false; + } + uint64_t total = 0; + if (!TreeSize(game->payload_root, total, error)) { + return false; + } + plan.disc_root = game->xex.relative_path.parent_path().generic_string(); + plan.update_package = !update->raw_patch; + if (plan.update_package) { + plan.update_root = update->patch.relative_path.parent_path().generic_string(); + plan.update_xexp = update->patch.relative_path.generic_string(); + if (!TreeSize(update->payload_root, total, error)) { + return false; + } + } + const auto aes_key_path = FindRpfAesKey(); + if (!aes_key_path || !ReadHostFile(*aes_key_path, plan.aes_key, error) || + plan.aes_key.size() != 32) { + error = "The bundled GTA IV RPF AES key is missing."; + return false; + } + if (!CheckedAdd(update->patch_bytes.size(), total) || !CheckedAdd(plan.aes_key.size(), total)) { + error = "The installation size exceeds the supported range."; + return false; + } + plan.copy_bytes = total; + return true; + } catch (const std::exception& exception) { + error = "The selected source couldn't be read: " + std::string(exception.what()); + return false; + } +} + +Result FinishInstall(const Selection& selection, const std::filesystem::path& install_root) { + const auto staging_root = install_root / ".install-staging"; + const auto staged_game = staging_root / "game"; + auto fail = [&](std::string error) { + std::error_code cleanup_error; + std::filesystem::remove_all(staging_root, cleanup_error); + return Result{false, std::move(error)}; + }; + try { + Result result; + auto game = PrepareGameSource(selection.game_source, result.error); + if (!game) { + return fail(std::move(result.error)); + } + auto update = PrepareUpdateSource(selection.update_source, game->xex_info, result.error); + if (!update) { + return fail(std::move(result.error)); + } + // the source's files and the update's, each whole; the key; the archives + // unpacked beside themselves + if (!MatchesTree(game->payload_root, staged_game, result.error) || + (!update->raw_patch && + !MatchesTree(update->payload_root, staged_game / "update", result.error))) { + return fail("The installed game isn't complete: " + result.error); + } + std::error_code fs_error; + if (std::filesystem::file_size(staged_game / "aes_key.bin", fs_error) != 32 || fs_error) { + return fail("The installed game has no archive key."); + } + for (const char* unpacked : {"common", "xbox360", "audio"}) { + if (!std::filesystem::is_directory(staged_game / unpacked, fs_error)) { + return fail("The game's archives weren't unpacked."); + } + } + // (default.xex and default.xexp checked byte for byte against the supported + // release; a look through the whole folder, archives unpacked, would add nothing) + std::string staged_reason; + if (!ValidateInstalledPair(staged_game, staged_reason)) { + return fail("The staged game failed final validation: " + staged_reason); + } + if (!WriteManifest(staged_game, game->xex_info, game->xex_bytes, update->patch_bytes, + result.error)) { + return fail(std::move(result.error)); + } + std::vector publish; + publish.push_back({staged_game, install_root / "game", install_root / ".game-backup"}); + if (!PublishDirectories(publish, result.error)) { + return fail(std::move(result.error)); + } + std::filesystem::remove_all(staging_root, fs_error); + result.success = true; + return result; + } catch (const std::exception& exception) { + return fail("The installed game couldn't be checked: " + std::string(exception.what())); + } +} + +bool PlanEpisode(const std::filesystem::path& source, EpisodePlan& plan, std::string& error) { + try { + plan = {}; + auto mounted = MountSource(source, error); + if (!mounted) { + return false; + } + if (mounted->package_title_id && *mounted->package_title_id != kGta4TitleId) { + error = "This package belongs to a different game."; + return false; + } + for (const Episode episode : {Episode::kTlad, Episode::kTbogt}) { + // (the folder with setup2.xml, content.dat and the episode's audio list) + auto payload = FindDlcPayload(mounted->root, episode); + if (!payload) continue; + uint64_t total = 0; + if (!TreeSize(payload->root, total, error)) { + return false; + } + plan.episode = episode; + plan.package = mounted->package_title_id.has_value(); + plan.root = payload->relative_path.generic_string(); + plan.copy_bytes = total; + return true; + } + error = "This isn't The Lost and Damned or The Ballad of Gay Tony."; + return false; + } catch (const std::exception& exception) { + error = "The package couldn't be read: " + std::string(exception.what()); + return false; + } +} + +Result FinishEpisode(const std::filesystem::path& source, Episode episode, + const std::filesystem::path& install_root) { + const auto staging_root = install_root / ".install-staging"; + const auto staged = staging_root / "dlc" / EpisodeDirectory(episode); + auto fail = [&](std::string error) { + std::error_code cleanup_error; + std::filesystem::remove_all(staging_root, cleanup_error); + return Result{false, std::move(error)}; + }; + try { + Result result; + auto prepared = PrepareDlcSource(DlcSelection{episode, source}, result.error); + if (!prepared) { + return fail(std::move(result.error)); + } + if (!MatchesTree(prepared->payload_root, staged, result.error)) { + return fail("The installed episode isn't complete: " + result.error); + } + if (!ValidateStagedEpisode(staged, episode)) { + return fail(std::string(EpisodeDirectory(episode)) + " did not produce a complete DLC layout."); + } + std::vector publish; + publish.push_back({staged, install_root / "dlc" / EpisodeDirectory(episode), + install_root / (std::string(".") + EpisodeDirectory(episode) + "-backup")}); + if (!PublishDirectories(publish, result.error)) { + return fail(std::move(result.error)); + } + std::error_code fs_error; + std::filesystem::remove_all(staging_root, fs_error); + result.success = true; + return result; + } catch (const std::exception& exception) { + return fail("The installed episode couldn't be checked: " + std::string(exception.what())); + } +} + } // namespace gta4::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.h b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.h index 7e9c3911..7d9ddc0a 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.h +++ b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_installer.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -43,6 +44,41 @@ Result VerifyInstall(const std::filesystem::path& game_root, Result Install(const Selection& selection, const std::filesystem::path& install_root, Progress& progress); +// [ps5 port] An install of the game and its title update whose writing is +// done somewhere else: on the PS5 a title may only write slowly, so the +// installer payload writes it (ps5/payload/installer). PlanInstall checks the +// sources as Install() does and says what to copy; the writer puts the game in +// /.install-staging/game the way Install() does (the source's +// files, the update's files in update/, default.xexp beside default.xex, +// aes_key.bin, the archives unpacked); FinishInstall checks that against the +// sources, writes the manifest and moves it into place. +struct InstallPlan { + std::string disc_root; // the source's folder with default.xex ("": its top), '/' between + bool update_package = false; // Title Update 8 is a content package (else a bare .xexp) + std::string update_root; // the package's folder with default.xexp + std::string update_xexp; // default.xexp's path in the package + uint64_t copy_bytes = 0; // what's copied before the archives are unpacked + std::vector aes_key; // the archives' key (32 bytes) +}; +bool PlanInstall(const Selection& selection, InstallPlan& plan, std::string& error); +Result FinishInstall(const Selection& selection, const std::filesystem::path& install_root); + +// [ps5 port] An episode, written by the installer payload as the game is: +// PlanEpisode says which episode a source holds (a content package from the +// Xbox 360, or a folder of its files) and which of its folders to copy; the +// writer copies that folder's files into +// /.install-staging/dlc/; FinishEpisode checks +// them against the source and moves them into /dlc. +struct EpisodePlan { + Episode episode = Episode::kTlad; + bool package = false; // a content package (else a folder) + std::string root; // the folder with setup2.xml in it ("": the top), '/' between + uint64_t copy_bytes = 0; // what's copied +}; +bool PlanEpisode(const std::filesystem::path& source, EpisodePlan& plan, std::string& error); +Result FinishEpisode(const std::filesystem::path& source, Episode episode, + const std::filesystem::path& install_root); + const char* EpisodeDirectory(Episode episode); } // namespace gta4::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_rpf_extractor.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_rpf_extractor.cpp index 98543925..55fc045d 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_rpf_extractor.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/install/gta4_rpf_extractor.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,10 @@ #include +#include + +#include "gta4_install_io.h" + extern "C" { #include } @@ -39,7 +44,7 @@ constexpr size_t kMaximumArchiveDepth = 32; constexpr size_t kMaximumDirectoryDepth = 256; constexpr size_t kMaximumNestedArchiveCount = 100000; constexpr uint64_t kMaximumExtractedFileSize = 2147483648; -constexpr size_t kIoBufferSize = 1048576; +constexpr size_t kIoBufferSize = size_t(4) << 20; struct TocEntry { std::string name; @@ -157,9 +162,9 @@ std::optional ParseArchive(const std::filesystem::path& path, std::span return std::nullopt; } - std::ifstream stream(path, std::ios::binary); + io::InFile stream; std::array header{}; - if (!stream.read(reinterpret_cast(header.data()), header.size()) || + if (!stream.Open(path) || !stream.ReadAt(0, header.data(), header.size()) || ReadU32(header, 0) != kRpf2Magic) { error = "The selected game contains an invalid RPF2 archive: " + path.string(); return std::nullopt; @@ -181,8 +186,7 @@ std::optional ParseArchive(const std::filesystem::path& path, std::span } std::vector toc(toc_size); - stream.seekg(static_cast(kTocOffset), std::ios::beg); - if (!stream.read(reinterpret_cast(toc.data()), toc.size())) { + if (!stream.ReadAt(kTocOffset, toc.data(), toc.size())) { error = "Could not read the RPF2 table of contents: " + path.string(); return std::nullopt; } @@ -307,6 +311,12 @@ bool ValidateArchiveGraph(const Archive& archive, const std::filesystem::path& a bool EnsureFreeSpace(const std::filesystem::path& destination, uint64_t required, std::string& error) { +#if REX_PLATFORM_PS5 + // [ps5 port] A title can't learn the free space (the SDK's statvfs always + // answers 16 GiB): not checked. + (void)destination, (void)required, (void)error; + return true; +#endif std::error_code fs_error; const auto space = std::filesystem::space(destination, fs_error); if (fs_error) { @@ -320,16 +330,16 @@ bool EnsureFreeSpace(const std::filesystem::path& destination, uint64_t required return true; } -bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry, - const std::filesystem::path& output_path, const RpfExtractionProgress& progress, +bool WriteEntry(const std::filesystem::path& archive_path, io::InFile& input, + const TocEntry& entry, const std::filesystem::path& output_path, + const RpfExtractionProgress& progress, io::WorkerPool::Scratch& scratch, std::string& error) { if (IsCancelled(progress)) { error = "Installation was cancelled."; return false; } - std::ifstream input(archive_path, std::ios::binary); - input.seekg(entry.offset, std::ios::beg); - if (!input) { + uint64_t read_at = entry.offset; + if (read_at > input.size() || entry.archived_size > input.size() - read_at) { error = "Could not read an RPF2 file entry from " + archive_path.string() + "."; return false; } @@ -345,14 +355,19 @@ bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry error = "Could not create an RPF extraction directory: " + fs_error.message(); return false; } - std::ofstream destination(output_path, std::ios::binary | std::ios::trunc); - if (!destination) { + io::OutFile destination; + if (!destination.Open(output_path, &scratch.gather, entry.size)) { error = "Could not write an extracted RPF2 file: " + output_path.string(); return false; } - std::vector input_buffer(kIoBufferSize); - std::vector output_buffer(kIoBufferSize); + // (the worker's buffers, kept between files) + io::AlignedBuffer& input_buffer = scratch.read; + io::AlignedBuffer& output_buffer = scratch.write; + if (!input_buffer.Reserve(kIoBufferSize) || !output_buffer.Reserve(kIoBufferSize)) { + error = "Not enough memory to extract " + output_path.string() + "."; + return false; + } uint64_t remaining = entry.archived_size; if (!entry.is_compressed) { while (remaining != 0) { @@ -361,12 +376,13 @@ bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry return false; } const size_t chunk = static_cast(std::min(remaining, input_buffer.size())); - if (!input.read(reinterpret_cast(input_buffer.data()), chunk) || - !destination.write(reinterpret_cast(input_buffer.data()), chunk)) { + if (!input.ReadAt(read_at, input_buffer.data(), chunk) || + !destination.Write(input_buffer.data(), chunk)) { error = "Could not copy an RPF2 file entry from " + archive_path.string() + "."; return false; } remaining -= chunk; + read_at += chunk; if (progress.completed_bytes) { progress.completed_bytes->fetch_add(chunk, std::memory_order_relaxed); } @@ -388,12 +404,13 @@ bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry if (stream.avail_in == 0 && remaining != 0) { const size_t chunk = static_cast(std::min(remaining, input_buffer.size())); - if (!input.read(reinterpret_cast(input_buffer.data()), chunk)) { + if (!input.ReadAt(read_at, input_buffer.data(), chunk)) { inflateEnd(&stream); error = "Could not read a compressed RPF2 file entry from " + archive_path.string() + "."; return false; } remaining -= chunk; + read_at += chunk; stream.next_in = input_buffer.data(); stream.avail_in = static_cast(chunk); } @@ -404,7 +421,7 @@ bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry const int status = inflate(&stream, flush); const size_t produced = output_buffer.size() - stream.avail_out; if (produced != 0) { - if (!destination.write(reinterpret_cast(output_buffer.data()), produced)) { + if (!destination.Write(output_buffer.data(), produced)) { inflateEnd(&stream); error = "Could not write a decompressed RPF2 file entry: " + output_path.string(); return false; @@ -434,16 +451,16 @@ bool WriteEntry(const std::filesystem::path& archive_path, const TocEntry& entry return false; } } - destination.flush(); - if (!destination) { + if (!destination.Close()) { error = "Could not finish an extracted RPF2 file: " + output_path.string(); return false; } return true; } -bool ExtractDirectory(const std::filesystem::path& archive_path, const Archive& archive, - size_t index, const std::filesystem::path& output_root, +bool ExtractDirectory(const std::filesystem::path& archive_path, io::InFile& input, + io::WorkerPool& pool, std::mutex& nested_mutex, + const Archive& archive, size_t index, const std::filesystem::path& output_root, std::vector& active, std::vector& visited, std::vector& nested_archives, const RpfExtractionProgress& progress, size_t depth, std::string& error) { @@ -477,7 +494,8 @@ bool ExtractDirectory(const std::filesystem::path& archive_path, const Archive& if (entry.is_directory) { std::error_code fs_error; std::filesystem::create_directories(output_path, fs_error); - if (fs_error || !ExtractDirectory(archive_path, archive, child, output_path, active, visited, + if (fs_error || !ExtractDirectory(archive_path, input, pool, nested_mutex, archive, child, + output_path, active, visited, nested_archives, progress, depth + 1, error)) { if (error.empty()) { error = "Could not create an RPF extraction directory: " + fs_error.message(); @@ -486,15 +504,26 @@ bool ExtractDirectory(const std::filesystem::path& archive_path, const Archive& } } else { visited[child] = true; - if (!WriteEntry(archive_path, entry, output_path, progress, error)) { + // (written by the pool, several files at once) + const bool queued = pool.Add([&archive_path, &input, entry, output_path, &progress, + &nested_archives, &nested_mutex]( + io::WorkerPool::Scratch& scratch, std::string& job_error) { + if (!WriteEntry(archive_path, input, entry, output_path, progress, scratch, job_error)) { + return false; + } + std::string extension = output_path.extension().string(); + std::transform(extension.begin(), extension.end(), extension.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + if (extension == ".rpf") { + std::lock_guard lock(nested_mutex); + nested_archives.push_back(output_path); + } + return true; + }); + if (!queued) { + error = "An RPF2 file entry could not be written."; return false; } - std::string extension = output_path.extension().string(); - std::transform(extension.begin(), extension.end(), extension.begin(), - [](unsigned char c) { return static_cast(std::tolower(c)); }); - if (extension == ".rpf") { - nested_archives.push_back(output_path); - } } } active[index] = false; @@ -503,7 +532,8 @@ bool ExtractDirectory(const std::filesystem::path& archive_path, const Archive& bool ExtractOne(const std::filesystem::path& archive_path, const std::filesystem::path& output_root, std::span key, const RpfExtractionProgress& progress, - std::vector& nested_archives, std::string& error) { + io::WorkerPool& pool, std::vector& nested_archives, + std::string& error) { auto archive = ParseArchive(archive_path, key, error); if (!archive) { return false; @@ -530,10 +560,25 @@ bool ExtractOne(const std::filesystem::path& archive_path, const std::filesystem error = "Could not create an RPF extraction directory: " + fs_error.message(); return false; } + io::InFile input; + if (!input.Open(archive_path)) { + error = "Could not open " + archive_path.string() + "."; + return false; + } std::vector active(archive->entries.size()); std::vector visited(archive->entries.size()); - if (!ExtractDirectory(archive_path, *archive, 0, output_root, active, visited, nested_archives, - progress, 0, error)) { + std::mutex nested_mutex; + const bool walked = ExtractDirectory(archive_path, input, pool, nested_mutex, *archive, 0, + output_root, active, visited, nested_archives, progress, 0, + error); + // (the queued files read `input`: every one is done before it closes) + std::string job_error; + const bool written = pool.Wait(job_error); + if (!walked) { + return false; + } + if (!written) { + error = std::move(job_error); return false; } return true; @@ -579,6 +624,7 @@ bool ExtractGameArchives(const std::filesystem::path& staged_game_root, std::unordered_set extracted; size_t archive_count = 0; + io::WorkerPool pool(io::CopyThreads()); while (!pending.empty()) { if (++archive_count > kMaximumNestedArchiveCount) { error = "The GTA IV source contains too many nested RPF archives."; @@ -596,7 +642,7 @@ bool ExtractGameArchives(const std::filesystem::path& staged_game_root, return false; } std::vector nested; - if (!ExtractOne(archive_path, output_root, *key, progress, nested, error)) { + if (!ExtractOne(archive_path, output_root, *key, progress, pool, nested, error)) { return false; } for (auto& child : nested) { diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/memory_watch.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/memory_watch.cpp new file mode 100644 index 00000000..b7e90136 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/memory_watch.cpp @@ -0,0 +1,151 @@ +// [ps5 port] Where the game runs out of room (the "Lure" rooftop hang). +// +// Players reach the rooftop in "Lure", the screen goes black and never comes +// back: the game's main thread loops for good in its allocator, asking its +// streaming code to free room for one allocation (sub_824E2D60 calls +// sub_821CFF10 until the heap's largest free block fits). +// +// Why it never ends: the port raises the streamed-resource budget above +// retail's (gta4_streaming_hooks.cpp: 221.5 MB to about 256 MB of a 272 MB +// heap). The same heap holds everything else the game allocates, so it can be +// out of room (or too broken up for one block) while the streamer is still +// under its budget, and the streamer only frees anything when over budget: +// each try answers "there is room" and frees nothing. So while an allocation +// waits, each try is run as if the budget ended at what is in use now, and the +// streamer evicts as retail would at its own budget, until the block fits; +// the budget is back as it was before the try returns. Nothing changes for an +// allocation that fits. +// +// Also logged: what waited, from where, and how the budgets stood, once a wait +// has gone on for a while; and every large allocation, with where it came from. +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "gta4_init.h" + +namespace { + +constexpr uint32_t kCodeBegin = 0x82140000, kCodeEnd = 0x82140000 + 0x94635C; +// The streaming budgets sub_824E2980 compares (used/limit pairs at +32/+36 +// and +44/+48 of this block). +constexpr uint32_t kStreamingBudgets = 0x82A9AA7C; +// Streamed physical memory: the budget and what is in use (bytes). +constexpr uint32_t kPhysicalLimit = kStreamingBudgets + 44, kPhysicalUsed = kStreamingBudgets + 48; +// Tries left as the game made them before the streamer is asked to evict +// (room can come back on its own: another thread freeing, a late release). +constexpr uint64_t kTriesBeforeEvicting = 32; + +struct Wait { + uint32_t size = 0, caller = 0, stack = 0; + uint64_t tries = 0, evicting_tries = 0; + std::chrono::steady_clock::time_point start{}; + bool logged = false; +}; +thread_local Wait t_wait; + +// Guest return addresses on the stack, nearest first (a scan: some may be stale). +std::string GuestTrail(uint8_t* base, uint32_t stack) { + std::string out; + int found = 0; + for (uint32_t offset = 0; offset < 1536 && found < 16; offset += 4) { + const uint32_t word = REX_LOAD_U32(stack + offset); + if (word >= kCodeBegin && word < kCodeEnd && !(word & 3)) { + out += fmt::format("{}{:08X}", found ? " < " : "", word); + ++found; + } + } + return out; +} + +void LogWait(uint8_t* base, const char* what) { + const double seconds = + std::chrono::duration(std::chrono::steady_clock::now() - t_wait.start).count(); + std::string budgets; + for (uint32_t i = 0; i < 16; ++i) + budgets += fmt::format("{}{:08X}", i ? " " : "", REX_LOAD_U32(kStreamingBudgets + i * 4)); + REXLOG_WARN("ps5 memory: {} the game is waiting for room for {} KB ({} bytes) from game code {:08X}, " + "{} tries ({} evicting) over {:.1f} s; called from {}; streaming budgets [{}]", + what, (t_wait.size + 1023) / 1024, t_wait.size, t_wait.caller, t_wait.tries, + t_wait.evicting_tries, seconds, GuestTrail(base, t_wait.stack), budgets); +} + +std::mutex g_large_mutex; +std::set> g_large_seen; + +// Tries running with the budget lowered, and the budget to put back after them. +std::mutex g_evict_mutex; +int g_evicting = 0; +uint32_t g_evict_saved_limit = 0; + +} // namespace + +// The allocation that waits for room (r3: bytes). +extern "C" void sub_824E2D60(PPCContext& ctx, uint8_t* base) { + const Wait outer = t_wait; + t_wait = Wait{ctx.r3.u32, uint32_t(ctx.lr), ctx.r1.u32, 0, 0, {}, false}; + __imp__sub_824E2D60(ctx, base); + if (t_wait.logged || t_wait.evicting_tries) LogWait(base, "done:"); + t_wait = outer; +} + +// One try at making room, while that allocation waits (r3 + r4: bytes). +extern "C" void sub_821CFF10(PPCContext& ctx, uint8_t* base) { + if (!t_wait.size) { + __imp__sub_821CFF10(ctx, base); + return; + } + if (!t_wait.tries++) t_wait.start = std::chrono::steady_clock::now(); + if (t_wait.tries == 20000 || (t_wait.tries > 20000 && t_wait.tries % 1000000 == 0)) { + t_wait.logged = true; + LogWait(base, "stuck:"); + } + // Under budget, the streamer frees nothing: this try runs with the budget + // ending at what is in use, so it evicts (sub_824E2980 checks used > limit - bytes). + // (Threads stuck at once share it: the last one out puts back the budget the + // first one found. The lock is never held over game code.) + bool evict = false; + if (t_wait.tries > kTriesBeforeEvicting) { + std::lock_guard lock(g_evict_mutex); + const uint32_t limit = g_evicting ? g_evict_saved_limit : REX_LOAD_U32(kPhysicalLimit); + const uint32_t used = REX_LOAD_U32(kPhysicalUsed); + if (limit && used <= limit) { + evict = true; + if (!g_evicting++) g_evict_saved_limit = limit; + if (!t_wait.evicting_tries++) + REXLOG_INFO("ps5 memory: no room for {} KB under the streaming budget ({} of {} MB in use); " + "freeing streamed resources for it", (t_wait.size + 1023) / 1024, used >> 20, limit >> 20); + REX_STORE_U32(kPhysicalLimit, used); + } + } + __imp__sub_821CFF10(ctx, base); + if (evict) { + std::lock_guard lock(g_evict_mutex); + if (!--g_evicting) REX_STORE_U32(kPhysicalLimit, g_evict_saved_limit); + } +} + +// The game's aligned allocations (r3: bytes): the large ones, once per caller +// and size, with where they came from. +extern "C" void sub_82812260(PPCContext& ctx, uint8_t* base) { + const uint32_t size = ctx.r3.u32; + if (size >= (2u << 20) && size != 0xFFFFFFFFu) { + const uint32_t caller = uint32_t(ctx.lr); + bool first = false; + { + std::lock_guard lock(g_large_mutex); + if (g_large_seen.size() < 4096) first = g_large_seen.insert({caller, size}).second; + } + if (first) + REXLOG_INFO("ps5 memory: the game allocates {} KB from game code {:08X}; called from {}", + (size + 1023) / 1024, caller, GuestTrail(base, ctx.r1.u32)); + } + __imp__sub_82812260(ctx, base); +} diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.cpp new file mode 100644 index 00000000..835bf858 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.cpp @@ -0,0 +1,503 @@ +// GTA IV PS5 - the TV's refresh rate and frame pacing (see ps5_display.h). +#include +#if REX_PLATFORM_PS5 + +#include "ps5_display.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +REXCVAR_DEFINE_UINT32(ps5_refresh_rate, 60, "PS5", + "The TV's refresh rate, 60 or 120 Hz (120 needs a TV that takes it); read when the app starts"); +REXCVAR_DEFINE_STRING(ps5_frame_generation, "off", "PS5", + "Frame generation's rate: off, 30-60 (the game at 30, at either refresh rate) or 60-120 (the " + "game at 60 at 120 Hz, 30 where it can't hold 60); the hub sets gta4_native_frame_generation with it") + .allowed({"off", "30-60", "60-120"}); +REXCVAR_DEFINE_BOOL(ps5_pacing_log, true, "PS5", + "Every 10 s, log how long the TV showed each of the game's frames and how evenly they came"); +REXCVAR_DEFINE_BOOL(ps5_even_frames, true, "PS5", + "At 120 Hz with a 60 limit: hold each image until the one before has been on the TV a whole " + "vblank, so none is shown for a single refresh (8 ms) between two of 16 and 25"); +REXCVAR_DEFINE_BOOL(ps5_smooth_40, false, "PS5", + "At 120 Hz with a 60 limit: where the game can't hold 60, an even 40 (each frame three vblanks) " + "instead of frames between 40 and 60, back to 60 once it can"); +REXCVAR_DEFINE_BOOL(ps5_vrr_keepalive, true, "PS5", + "With VRR at 120 Hz and a 60 limit, while the game's frames take longer than the TV waits " + "between refreshes (~21 ms): refresh the frame on the TV once, timed so the TV is free again " + "when the late frame comes (shown then, not ~29 ms after the one before)"); +REXCVAR_DECLARE(uint32_t, gta4_frame_limit); +REXCVAR_DECLARE(bool, ps5_overlay_every_vblank); +REXCVAR_DECLARE(bool, gta4_native_texture_watch); +REXCVAR_DECLARE(std::string, log_level); + +extern "C" { +int sceVideoOutGetFlipStatus(int32_t handle, uint64_t status[16]); +int __real_sceVideoOutIsOutputSupported(int32_t handle, uint32_t mode, const void* reserved1, + const void* reserved2, const void* reserved3); +int __real_sceVideoOutConfigureOutput(int32_t handle, uint32_t mode, const void* reserved1, + const void* reserved2, const void* reserved3); +int __real_sceVideoOutSetFlipRate(int32_t handle, int32_t rate); +int sceVideoOutWaitVblank(int32_t handle); +uint64_t sceKernelGetProcessTime(void); +} + +namespace gta4ps5::display { +namespace { + +constexpr uint32_t kHighFrameRateMode = 15; // (videoout.h: 119.88 Hz) +constexpr uint32_t kRestoreMode = 1; + +// After the 120 Hz mode is taken (ps5_display.h): a TV at a fixed 60 Hz has +// every vblank kFixed60MinNs..kFixed60MaxNs apart (59.94 Hz: 16.683 ms). The +// median under kHighRefreshLimitNs is a fixed 120 Hz; past kFixed60MaxNs, the +// TV idling at its VRR floor; any one interval under kFixed60MinNs, a TV +// refreshing faster than 60 Hz (120, or VRR following flips). +constexpr int64_t kHighRefreshLimitNs = 12500000; +constexpr int64_t kFixed60MinNs = 14500000; +constexpr int64_t kFixed60MaxNs = 18500000; + +std::atomic g_handle{-1}; +std::atomic g_high{false}; +std::atomic g_vrr{false}; +std::atomic g_game{false}; +std::atomic g_rate{-1}; // (the rate VideoOut has: -1 unknown) +std::mutex g_switch_mutex; // (one switch at a time) + +// The hub's setting, straight from its file: VideoOut opens before the config +// is loaded (as ps5_save_logs in windowed_app_main_sdl.cpp). +bool WantsHighRefresh() { + bool high = false; + if (std::FILE* f = std::fopen("/app0/UserData/LibertyRecomp/native.toml", "rb")) { + char line[512]; + while (std::fgets(line, sizeof(line), f)) { + const char* start = line + std::strspn(line, " \t"); + if (std::strncmp(start, "ps5_refresh_rate", 16) != 0) continue; + const char* value = std::strchr(start, '='); + if (!value) continue; + value += 1 + std::strspn(value + 1, " \t\""); + high = std::strtoul(value, nullptr, 10) >= 100; + } + std::fclose(f); + } + return high; +} + +// Vblanks a frame stays for, less one (sceVideoOutSetFlipRate's rate). +int32_t WantedRate() { + if (!g_game.load(std::memory_order_acquire)) return 0; + // Frame generation: each image (generated or real) its own vblank, or two + // while the game runs at a quarter of 120 Hz. The frame limit doesn't apply. + if (rex::frame_stats::g_frame_generation_active.load(std::memory_order_acquire)) + return int32_t(std::max(rex::frame_stats::g_fg_vblanks_per_image.load(std::memory_order_acquire), 1)) - 1; + const uint32_t effective = rex::frame_stats::g_effective_frame_limit.load(std::memory_order_acquire); + const uint32_t fps = effective ? effective : REXCVAR_GET(gta4_frame_limit); + // VRR: the TV refreshes when a frame comes; the frame limit's own pacing + // (Ps5LimitFrameRate) spaces them. (1.0.3 Test 1 held them to two vblanks + // as at a fixed 120 Hz: every frame past 16.7 ms then waited for the next + // pair and stayed up 33 ms, where VRR shows it at 20 or 25. 30 and 40 are + // under most TVs' VRR floor: held to vblanks as at a fixed 120 Hz.) + if (g_vrr.load(std::memory_order_acquire) && (fps == 0 || fps == 60 || fps == 120)) return 0; + if (g_high.load(std::memory_order_acquire)) { + if (fps == 40) return 2; + if (fps == 30 || fps == 60) return 1; + return 0; + } + return fps == 30 ? 1 : 0; +} + +void Apply(const char* why) { + static std::mutex apply_mutex; // (from the hub, the game's render thread, the cvar callback) + std::lock_guard lock(apply_mutex); + const int32_t handle = g_handle.load(std::memory_order_acquire); + const int32_t rate = WantedRate(); + if (handle < 0 || g_rate.load(std::memory_order_acquire) == rate) return; + const int result = __real_sceVideoOutSetFlipRate(handle, rate); + if (result == 0) g_rate.store(rate, std::memory_order_release); + REXLOG_INFO("PS5 display: each frame shows for at least {} vblank{} at {} Hz{} ({}; {})", rate + 1, + rate ? "s" : "", g_high.load() ? "119.88" : "59.94", g_vrr.load() ? " VRR" : "", why, + result == 0 ? std::string("ok") : fmt::format("failed {:08X}", uint32_t(result))); +} + +// The time between vblanks over eight of them, the first wait finding the +// phase, sorted (a wait that misses a vblank gives a longer interval, never a +// shorter one). False when a wait fails. +bool VblankIntervals(int32_t handle, std::array& intervals) { + if (sceVideoOutWaitVblank(handle) != 0) return false; + auto previous = std::chrono::steady_clock::now(); + for (int64_t& interval : intervals) { + if (sceVideoOutWaitVblank(handle) != 0) return false; + const auto now = std::chrono::steady_clock::now(); + interval = std::chrono::duration_cast(now - previous).count(); + previous = now; + } + std::sort(intervals.begin(), intervals.end()); + return true; +} + +// The pacing log (StartGamePacing). VideoOut's flip status (word 3: the flip +// shown last, numbered as RADV submits them, which is the presenter's image +// count: frame_stats::g_image_frame) is looked at every millisecond off the +// renderer's cores: when the frame on screen changes, the one before was shown +// from when it came until now (seen at most ~1 ms late), rounded to vblanks. +// A repeat of the same frame (Every Vblank) counts as the same frame. The +// game's own frame times come from frame_stats' history. +struct PacingWindow { + std::array shown{}; // frames on screen for 1, 2, 3, 4 or more vblanks + uint32_t changes = 0; // a frame held for a different number than the one before + uint32_t unseen = 0; // frames shown and gone between two looks + int64_t shown_min = 0, shown_max = 0; + std::array game{}; // the game's frames: to 17.5 ms, 25, 34.5, longer + uint32_t jumps = 0; // a game frame 4 ms or more off the one before + uint32_t by_flip = 0; // holds timed by the flips themselves +}; + +// [ps5 port] Before the presenter queues an image: at 120 Hz with a 60 limit +// (flip rate 0), the image before it must have been on the TV a whole vblank, +// so this one, shown at the next, gives it two. 1.0.3 Test 3's logs: with the +// game on time (8 ms of work a frame, waiting for its slot), a third of the +// frames still came 8 ms after the one before and a third 25 ms (finished +// frames landing on either side of a vblank). A frame that's late still goes +// out at the next vblank. Waits at most ~25 ms; the VideoOut flip clock +// (sceKernelGetProcessTime's microseconds) is checked first, once. +// The new frames held, and for how long, for the pacing log. +std::atomic g_holds{0}, g_hold_us{0}; + +void HoldBeforePresent() { + if (!REXCVAR_GET(ps5_even_frames) || !g_high.load(std::memory_order_acquire) || + g_rate.load(std::memory_order_acquire) != 0 || + rex::frame_stats::g_frame_generation_active.load(std::memory_order_acquire)) + return; + const uint32_t effective = rex::frame_stats::g_effective_frame_limit.load(std::memory_order_acquire); + if ((effective ? effective : REXCVAR_GET(gta4_frame_limit)) != 60) return; + const int32_t handle = g_handle.load(std::memory_order_acquire); + if (handle < 0) return; + // The flip's time must be on sceKernelGetProcessTime's clock: not ahead of + // it, and not years behind. (Test 4 also asked for it to be under a second + // old, and the first check came during a loading screen, 1.7 s after its + // last flip: off for the whole session.) 32 failed checks in a row: off. + static std::atomic clock_failures{0}; + static std::atomic clock_logged{false}; + if (clock_failures.load(std::memory_order_relaxed) >= 32) return; + constexpr uint64_t kVblankUs = 8342, kMarginUs = 400, kGiveUpUs = 25000; + const uint64_t begin = sceKernelGetProcessTime(); + // Only a new game frame is held: the presenter also sends the frame on the + // TV again at the TV's rate (about 110 images a second at 60 FPS), and + // those just keep it there. 1.1's first two builds held every image, the + // repeats too: a new frame then waited behind a repeat and was up 25 ms + // (15% of frames) where it should have been 16.7. + // (The repaints themselves are gone too: Presenter::PaintFromUIThreadImpl.) + // The previous new frame is on the TV once a flip came after it was sent + // (the flips' own times, VideoOut's status[1], on the process clock: no + // frame numbers or counts, which the driver doesn't promise), and has had + // its whole vblank a vblank (and a margin) after that flip. 16 waits in a + // row running out (the graphics card that far behind) pause this for the + // next 600 frames. + if (!rex::frame_stats::g_present_new_frame.load(std::memory_order_acquire)) return; + static uint64_t previous_sent_us = 0; + static bool have_previous = false; + static int give_ups = 0, paused_frames = 0; + if (paused_frames > 0) { + --paused_frames; + return; + } + for (;;) { + uint64_t status[16] = {}; + if (sceVideoOutGetFlipStatus(handle, status) != 0) return; + const uint64_t now = sceKernelGetProcessTime(); + const uint64_t flipped = status[1]; // (when the last flip happened) + const int32_t pending = int32_t(status[6] >> 32); + const bool clock_ok = flipped && flipped <= now + 2000 && now - flipped < 600000000ull; + if (!clock_ok) { + if (clock_failures.fetch_add(1, std::memory_order_relaxed) == 31) + REXLOG_INFO("ps5 even frames: off (the flip time {} isn't on the process clock, now {})", flipped, now); + return; + } + clock_failures.store(0, std::memory_order_relaxed); + if (!clock_logged.exchange(true, std::memory_order_relaxed)) + REXLOG_INFO("ps5 even frames: on (last flip {} us ago, {} pending)", int64_t(now) - int64_t(flipped), pending); + const bool previous_shown = !have_previous || flipped >= previous_sent_us; + const bool go = previous_shown && pending <= 0 && (now < flipped || now - flipped >= kVblankUs + kMarginUs); + const bool ran_out = now - begin > kGiveUpUs; + if (go || ran_out) { + if (ran_out && ++give_ups >= 16) { + REXLOG_INFO("ps5 even frames: paused for 600 frames (16 waits in a row ran out; last flip at {}, previous " + "frame sent at {})", flipped, previous_sent_us); + give_ups = 0; + paused_frames = 600; + } + if (!ran_out) give_ups = 0; + previous_sent_us = now; + have_previous = true; + if (now > begin + 200) { + g_holds.fetch_add(1, std::memory_order_relaxed); + g_hold_us.fetch_add(now - begin, std::memory_order_relaxed); + } + return; + } + const timespec delay{0, 300000}; + nanosleep(&delay, nullptr); + } +} + +void PacingMain() { + using namespace rex::frame_stats; + uint64_t cpus_applied = UINT64_MAX; + int64_t flip = -1; // the flip shown at the last look + uint64_t frame = 0; // the game frame (publication) it showed + int64_t frame_since_ns = 0; // when that frame came on screen (0: not known) + uint64_t frame_since_us = 0; // the same by the flip's own time (0: not known) + // VRR keep-alive: the time between new frames on the TV (ms, smoothed), + // and whether this frame has had its refresh. + double interval_ms = 16.7; + bool kept_alive = false; + uint32_t last_held = 0, last_game_us = 0; + uint64_t game_seen = g_presents.load(std::memory_order_acquire); + PacingWindow w; + int64_t window_ns = NowNs(); + for (;;) { + FollowRendererCpuMask(cpus_applied, RendererRole::kOther); + const timespec delay{0, 1000000}; + nanosleep(&delay, nullptr); + const int64_t now = NowNs(); + const int32_t handle = g_handle.load(std::memory_order_acquire); + uint64_t status[16] = {}; + if (handle >= 0 && sceVideoOutGetFlipStatus(handle, status) == 0 && int64_t(status[3]) != flip) { + const int64_t shown = int64_t(status[3]); + const uint64_t sent = g_images_sent.load(std::memory_order_acquire); + const bool known = shown > 0 && uint64_t(shown) <= sent && sent - uint64_t(shown) < kImageHistory; + if (known && flip > 0 && shown > flip && shown - flip <= int64_t(kImageHistory)) { + // the frames of the flips since the last look: a change of frame ends the one before + uint32_t changed = 0; + uint64_t f = frame; + for (int64_t i = flip + 1; i <= shown; ++i) { + const uint64_t next = g_image_frame[uint64_t(i) % kImageHistory].load(std::memory_order_relaxed); + if (next != f) ++changed, f = next; + } + // When the flip itself happened (VideoOut's clock), if this look saw + // only it: this thread's looks can come late on a busy CPU, which put + // a late look's lateness on one frame and took it off the next (Test 3). + const uint64_t flip_us = shown - flip == 1 ? status[1] : 0; + if (changed == 1 && frame_since_ns) { + int64_t held = now - frame_since_ns; + if (flip_us && frame_since_us && flip_us > frame_since_us && flip_us - frame_since_us < 1000000) { + const int64_t by_flip = int64_t(flip_us - frame_since_us) * 1000; + if (std::abs(by_flip - held) < 12000000) held = by_flip, ++w.by_flip; + } + const double vblank = g_high.load(std::memory_order_relaxed) ? 8341700.0 : 16683400.0; + const uint32_t n = uint32_t(std::clamp(std::lround(double(held) / vblank), 1, 4)); + ++w.shown[n - 1]; + { + const uint64_t k = g_tv_shown.load(std::memory_order_relaxed); + g_tv_hold_us[k % kTvHistory].store(uint32_t(std::min(held / 1000, 4000000)), + std::memory_order_relaxed); + g_tv_shown.store(k + 1, std::memory_order_release); + } + interval_ms = interval_ms * 0.8 + double(held) / 1e6 * 0.2; + if (last_held && n != last_held) ++w.changes; + last_held = n; + w.shown_min = w.shown_min ? std::min(w.shown_min, held) : held; + w.shown_max = std::max(w.shown_max, held); + } else if (changed > 1) { + w.unseen += changed - 1; + g_tv_missed.fetch_add(changed - 1, std::memory_order_relaxed); + last_held = 0; + } + if (changed) frame = f, frame_since_ns = now, frame_since_us = flip_us, kept_alive = false; + } else { + frame = known ? g_image_frame[uint64_t(shown) % kImageHistory].load(std::memory_order_relaxed) : 0; + frame_since_ns = 0; + frame_since_us = 0; + last_held = 0; + } + flip = shown; + } + // [ps5 port] VRR keep-alive. With VRR the TV waits at most ~20.9 ms (its + // 48 Hz floor) for the next frame, then refreshes the old one by itself, + // and a refresh takes 8.3 ms at 120 Hz: a frame that came 21-29 ms after + // the one before waited for it and was up ~29 ms (1.1's busy streets, a + // third of frames). While frames are coming that late, this refresh comes + // sooner, when the next frame is expected less a refresh and a + // millisecond, so the TV is free again when it arrives. + { + const uint32_t effective = g_effective_frame_limit.load(std::memory_order_relaxed); + const bool applies = REXCVAR_GET(ps5_vrr_keepalive) && g_vrr.load(std::memory_order_relaxed) && + g_high.load(std::memory_order_relaxed) && g_rate.load(std::memory_order_relaxed) == 0 && + (effective ? effective : REXCVAR_GET(gta4_frame_limit)) == 60 && + !g_frame_generation_active.load(std::memory_order_relaxed); + if (applies && !kept_alive && frame_since_ns && interval_ms > 21.0) { + const double at_ms = std::clamp(interval_ms - 8.34 - 1.0, 11.0, 19.5); + if (double(now - frame_since_ns) / 1e6 >= at_ms) { + kept_alive = true; + if (auto* request = g_request_keepalive_paint.load(std::memory_order_acquire)) request(); + } + } + } + // the game's frames since the last look + const uint64_t presents = g_presents.load(std::memory_order_acquire); + if (presents - game_seen > kHistory) game_seen = presents - kHistory; + for (; game_seen < presents; ++game_seen) { + const uint32_t us = g_history_us[game_seen % kHistory].load(std::memory_order_relaxed); + if (!us) continue; + ++w.game[us <= 17500 ? 0 : us <= 25000 ? 1 : us <= 34500 ? 2 : 3]; + if (last_game_us && (us > last_game_us + 4000 || last_game_us > us + 4000)) ++w.jumps; + last_game_us = us; + } + if (now - window_ns < 10000000000ll) continue; + window_ns = now; + if (!REXCVAR_GET(ps5_pacing_log)) { + w = {}; + continue; + } + const uint32_t shown_total = w.shown[0] + w.shown[1] + w.shown[2] + w.shown[3]; + const uint32_t game_total = w.game[0] + w.game[1] + w.game[2] + w.game[3]; + const uint64_t holds = g_holds.exchange(0, std::memory_order_relaxed); + const uint64_t hold_us = g_hold_us.exchange(0, std::memory_order_relaxed); + REXLOG_INFO( + "ps5 pacing: the TV showed {} frames: {} for 1 vblank, {} for 2, {} for 3, {} for 4 or more ({} times " + "held for a different number than the frame before; {:.1f} to {:.1f} ms; {} gone between looks); the " + "game's {} frames: {} to 17.5 ms, {} to 25, {} to 34.5, {} longer, {} jumps of 4 ms or more; {} " + "images queued, not shown yet; {} Hz{}, frame limit {}{}, frames {}{}, texture watch {}, log {}; {} holds " + "timed by the flips; {} new frames held back, {:.1f} ms each; {} repaints of the frame on the TV skipped, {} " + "made to keep VRR ready; version {}", + shown_total, w.shown[0], w.shown[1], w.shown[2], w.shown[3], w.changes, w.shown_min / 1e6, + w.shown_max / 1e6, w.unseen, game_total, w.game[0], w.game[1], w.game[2], w.game[3], w.jumps, + flip > 0 ? int64_t(g_images_sent.load(std::memory_order_acquire)) - flip : -1, + g_high.load() ? "119.88" : "59.94", g_vrr.load() ? " VRR" : "", REXCVAR_GET(gta4_frame_limit), + rex::frame_stats::g_effective_frame_limit.load() == 40 && REXCVAR_GET(gta4_frame_limit) != 40 + ? " (at 40 now)" : "", + REXCVAR_GET(ps5_overlay_every_vblank) ? "every vblank" : "as finished", + REXCVAR_GET(ps5_even_frames) ? ", held whole vblanks" : "", + REXCVAR_GET(gta4_native_texture_watch) ? "on" : "off", REXCVAR_GET(log_level), w.by_flip, holds, + holds ? double(hold_us) / 1000.0 / double(holds) : 0.0, + rex::frame_stats::g_repeat_paints_skipped.exchange(0, std::memory_order_relaxed), + rex::frame_stats::g_keepalive_paints.exchange(0, std::memory_order_relaxed), GTA4PS5_VERSION); + w = {}; + } +} + +} // namespace + +bool HighRefresh() { return g_high.load(std::memory_order_acquire); } +bool Vrr() { return g_vrr.load(std::memory_order_acquire); } +bool Ready() { return g_handle.load(std::memory_order_acquire) >= 0; } + +bool SetHighRefresh(bool high) { + std::lock_guard lock(g_switch_mutex); + const int32_t handle = g_handle.load(std::memory_order_acquire); + if (handle < 0) { + REXLOG_WARN("PS5 display: no TV open yet; {} Hz not switched", high ? 120 : 60); + return false; + } + if (g_high.load(std::memory_order_acquire) == high) return true; + if (!high) { + const int result = __real_sceVideoOutConfigureOutput(handle, kRestoreMode, nullptr, nullptr, nullptr); + g_high.store(false, std::memory_order_release); + g_vrr.store(false, std::memory_order_release); + REXLOG_INFO("PS5 display: back to 60 Hz ({})", result); + Apply("60 Hz"); + return true; + } + const int supported = __real_sceVideoOutIsOutputSupported(handle, kHighFrameRateMode, nullptr, nullptr, nullptr); + if (supported <= 0) { + REXLOG_INFO("PS5 display: 120 Hz not offered on the PS5's video output ({}); staying at 60 Hz", supported); + return false; + } + const int configured = __real_sceVideoOutConfigureOutput(handle, kHighFrameRateMode, nullptr, nullptr, nullptr); + if (configured != 0) { + REXLOG_INFO("PS5 display: 120 Hz refused ({:08X}); staying at 60 Hz", uint32_t(configured)); + return false; + } + std::array intervals{}; + const bool measured = VblankIntervals(handle, intervals); + const int64_t shortest = intervals.front(), median = intervals[intervals.size() / 2]; + const bool fixed_120 = measured && median <= kHighRefreshLimitNs; + const bool faster_than_60 = measured && shortest < kFixed60MinNs; + const bool vrr_floor = measured && median > kFixed60MaxNs; + if (!fixed_120 && !faster_than_60 && !vrr_floor) { + __real_sceVideoOutConfigureOutput(handle, kRestoreMode, nullptr, nullptr, nullptr); + REXLOG_INFO("PS5 display: 120 Hz taken, but a vblank comes every {:.3f} ms (shortest {:.3f}); back to 60 Hz", + median / 1e6, shortest / 1e6); + return false; + } + g_vrr.store(!fixed_120, std::memory_order_release); + g_high.store(true, std::memory_order_release); + REXLOG_INFO("PS5 display: 120 Hz on{} (vblanks {:.3f} to {:.3f} ms apart, median {:.3f})", + fixed_120 ? "" : " as VRR", shortest / 1e6, intervals.back() / 1e6, median / 1e6); + Apply("120 Hz"); + return true; +} + +void StartGamePacing() { + g_game.store(true, std::memory_order_release); + rex::frame_stats::g_display_pacing_changed.store(+[](const char* why) { Apply(why); }, + std::memory_order_release); + rex::frame_stats::g_before_present.store(&HoldBeforePresent, std::memory_order_release); + static std::atomic registered{false}; + if (!registered.exchange(true)) { + rex::cvar::RegisterChangeCallback("gta4_frame_limit", + [](std::string_view, std::string_view) { Apply("frame limit changed"); }); + std::thread(PacingMain).detach(); + } + Apply("game started"); +} + +} // namespace gta4ps5::display + +using namespace gta4ps5::display; + +extern "C" int __wrap_sceVideoOutIsOutputSupported(int32_t handle, uint32_t mode, const void* reserved1, + const void* reserved2, const void* reserved3) { + if (handle >= 0) g_handle.store(handle, std::memory_order_release); + if (mode == kHighFrameRateMode && !WantsHighRefresh()) { + REXLOG_INFO("PS5 display: 60 Hz (the hub's Refresh Rate)"); + return 0; + } + const int result = __real_sceVideoOutIsOutputSupported(handle, mode, reserved1, reserved2, reserved3); + if (mode == kHighFrameRateMode) + REXLOG_INFO("PS5 display: 120 Hz asked for; the TV {} it ({})", result > 0 ? "takes" : "doesn't take", result); + return result; +} + +extern "C" int __wrap_sceVideoOutConfigureOutput(int32_t handle, uint32_t mode, const void* reserved1, + const void* reserved2, const void* reserved3) { + if (handle >= 0) g_handle.store(handle, std::memory_order_release); + const int result = __real_sceVideoOutConfigureOutput(handle, mode, reserved1, reserved2, reserved3); + if (mode == kHighFrameRateMode) { + g_high.store(result == 0, std::memory_order_release); + REXLOG_INFO("PS5 display: 120 Hz mode {} ({})", result == 0 ? "on" : "refused", result); + } else if (mode == kRestoreMode && g_high.exchange(false)) { + g_vrr.store(false, std::memory_order_release); + // (RADV measured the vblanks and found the TV still at 60 Hz) + REXLOG_INFO("PS5 display: the TV stayed at 60 Hz; 120 Hz mode off"); + } + return result; +} + +extern "C" int __wrap_sceVideoOutSetFlipRate(int32_t handle, int32_t rate) { + g_handle.store(handle, std::memory_order_release); + const int32_t wanted = WantedRate(); + const int result = __real_sceVideoOutSetFlipRate(handle, wanted); + g_rate.store(result == 0 ? wanted : -1, std::memory_order_release); + (void)rate; + // (RADV doesn't ask for 120 Hz with our param.json: the hub switches at its + // first frame, ps5_display.h) + return result; +} + +#endif // REX_PLATFORM_PS5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.h new file mode 100644 index 00000000..0539f240 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_display.h @@ -0,0 +1,54 @@ +// GTA IV PS5 - the TV's refresh rate (the hub's Refresh Rate) and how many +// vblanks each frame stays on screen. +// +// The 120 Hz mode is VideoOut's mode 15, on the handle RADV opened +// (wsi_common_videoout.c). RADV asks for it itself when it opens the TV, once, +// before the hub, if param.json declares 120 Hz output (attribute3 0x40, with +// 0x40000 as retail titles with VRR do: ps5_package.sh). The link wraps three +// of its VideoOut calls (ps5_package.sh and CMakeLists.txt, --wrap): +// - IsOutputSupported: notes the handle; RADV's ask for the 120 Hz mode only +// goes through when the hub's Refresh Rate is 120. +// - ConfigureOutput: notes whether the 120 Hz mode was taken. +// - SetFlipRate: notes the handle; RADV sets 0 (a frame shows from the next +// vblank) for each swapchain, and the rate the frame limit wants is kept +// instead. +// RADV asks only for attribute3 0x80040 (with 0x80000, "VRR at 120 Hz"), +// which ours (0x40040: 120 Hz, VRR supported) isn't, so the switch is the +// hub's: at its first frame when the Refresh Rate is 120, and when it's chosen +// (SetHighRefresh), both from its own thread, while no new frame is flipped. +// +// With VRR on in the PS5's settings and a TV that has it, the 120 Hz output +// becomes a VRR one: until frames come, the TV refreshes at its VRR floor +// (48 Hz on most, a vblank every ~20.8 ms), which the first check here (and +// RADV's) took for a TV stuck at 60 Hz, putting it back (PS5_Mesa issue 4; a +// TCL C6K tester's "can't switch to 120 Hz"). A TV at a fixed 60 Hz keeps +// every vblank ~16.7 ms apart; one faster than that, or a period past the 60 +// Hz one, is a 120 Hz or a VRR output, kept. +#pragma once + +namespace gta4ps5::display { + +// Whether the TV refreshes at 119.88 Hz (else 59.94). +bool HighRefresh(); +// Whether that 120 Hz output is a VRR one (the PS5's VRR setting on, a TV +// with it): frames are then shown as soon as they're ready (the frame limit +// spaces them), the TV refreshing with them. +bool Vrr(); +// Whether the TV is open (a switch can be made). +bool Ready(); + +// Switches the TV to 119.88 Hz (true) or back to 59.94 Hz now. False when +// it stays where it was: no TV open yet, or (for 120) the TV or the PS5's +// video output settings don't take it. The screen goes dark for a moment. +bool SetHighRefresh(bool high); + +// From the game's start (the hub keeps every vblank): each frame stays on +// screen for as many vblanks as the frame limit takes. At 120 Hz: 40 FPS +// three, 60 FPS two; at 60 Hz: 30 FPS two; with VRR, 60 and 120 FPS none (as +// soon as it's ready). Follows later frame limit changes (the pause menu's +// Graphics page). Also starts the pacing log (ps5_pacing_log): every 10 s, +// how many vblanks the TV showed each of the game's frames for, from +// VideoOut's flips, and how evenly the game's frames came. +void StartGamePacing(); + +} // namespace gta4ps5::display diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.cpp new file mode 100644 index 00000000..f23883c1 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.cpp @@ -0,0 +1,243 @@ +// GTA IV PS5 - the frame rate counter and the frame time capture (see +// ps5_overlay.h). +#include "ps5_overlay.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "../hub/ps5_hub.h" +#include "ps5_display.h" + +REXCVAR_DEFINE_STRING(ps5_fps_counter, "false", "PS5", + "The frame rate counter at the left edge of the screen while playing: false (off), simple " + "(frames a second and the frame time) or true (detailed: also the slowest frames, a graph, " + "the resolution, the TV's refresh rate, the frame limit and frame generation)"); +REXCVAR_DECLARE(uint32_t, gta4_frame_limit); +REXCVAR_DEFINE_BOOL(ps5_frametime_capture, false, "PS5", + "Record every frame's time to a CSV file in /app0/logs"); +REXCVAR_DEFINE_BOOL(ps5_overlay_every_vblank, false, "PS5", + "Repaint the game's overlay every vblank (the old way: each new frame waits " + "behind repeats of the last one)"); + +namespace gta4ps5 { + +const char* BuildVersion() { return GTA4PS5_VERSION; } + +GameOverlay::GameOverlay(rex::ui::ImGuiDrawer* drawer) : ImGuiDialog(drawer) { + window_start_ns_ = rex::frame_stats::NowNs(); + window_frames_ = rex::frame_stats::g_presents.load(std::memory_order_acquire); + if (REXCVAR_GET(ps5_frametime_capture)) { + std::error_code ec; + std::filesystem::create_directories("/app0/logs", ec); + const std::time_t now = std::time(nullptr); + std::tm tm{}; + localtime_r(&now, &tm); + char name[96]; + std::strftime(name, sizeof(name), "/app0/logs/frametimes-%Y-%m-%d-%H-%M-%S.csv", &tm); + csv_ = std::fopen(name, "w"); + if (csv_) { + std::fputs("frame,seconds,frame_ms\n", csv_); + REXLOG_INFO("PS5: frame time capture to {}", name); + } else { + REXLOG_WARN("PS5: couldn't start the frame time capture ({})", name); + } + } +} + +GameOverlay::~GameOverlay() { + if (csv_) std::fclose(csv_); +} + +void GameOverlay::Sample() { + using namespace rex::frame_stats; + const int64_t now = NowNs(); + const uint64_t frames = g_presents.load(std::memory_order_acquire); + const uint64_t tv = g_tv_shown.load(std::memory_order_acquire); + const int64_t span = now - window_start_ns_; + if (span < 500'000'000) return; + const uint64_t n = frames - window_frames_; + const uint64_t shown = tv - window_tv_; + game_fps_ = float(double(n) * 1e9 / double(span)); + // [ps5 port] What the TV showed when the pacing thread sees it: the game can + // finish 60 frames a second while some stay up for two refreshes and others + // never reach the screen, which the game's own count reads as a clean 60. + from_tv_ = shown > 0; + const uint64_t counted = from_tv_ ? shown : n; + fps_ = float(double(counted) * 1e9 / double(span)); + ms_ = counted ? float(double(span) / 1e6 / double(counted)) : 0.0f; + window_start_ns_ = now; + window_frames_ = frames; + window_tv_ = tv; + // the slowest frames of the last few seconds: 1% low and the worst one + std::vector times; + if (from_tv_) { + const uint32_t count = uint32_t(std::min(tv, 300)); + times.reserve(count); + for (uint32_t i = 0; i < count; ++i) + if (const uint32_t us = g_tv_hold_us[(tv - 1 - i) % kTvHistory].load(std::memory_order_relaxed)) + times.push_back(us); + } else { + const uint32_t count = uint32_t(std::min(frames > 1 ? frames - 1 : 0, kHistory)); + times.reserve(count); + for (uint32_t i = 0; i < count; ++i) + if (const uint32_t us = g_history_us[(frames - 1 - i) % kHistory].load(std::memory_order_relaxed)) + times.push_back(us); + } + // a stutter: a frame on screen half again as long as the usual one + stutters_ = 0; + if (from_tv_ && times.size() >= 30) { + std::vector sorted = times; + std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); + const uint32_t usual = sorted[sorted.size() / 2]; + for (const uint32_t us : times) stutters_ += us > usual + usual / 2 && us > usual + 5000; + } + if (times.empty()) { + low_fps_ = worst_ms_ = 0; + return; + } + std::sort(times.begin(), times.end()); + const uint32_t p99 = times[size_t(double(times.size() - 1) * 0.99)]; + low_fps_ = p99 ? float(1e6 / double(p99)) : 0.0f; + worst_ms_ = float(times.back()) / 1000.0f; +} + +void GameOverlay::Capture() { + if (!csv_) return; + const uint64_t frames = rex::frame_stats::g_presents.load(std::memory_order_acquire); + if (frames == captured_) return; + // (one line for the newest frame; frames between two draws of the overlay + // share its time, which is rare at the rates the game runs) + const int64_t now = rex::frame_stats::g_last_present_ns.load(std::memory_order_acquire); + if (!capture_start_ns_) capture_start_ns_ = now; + const double ms = rex::frame_stats::g_last_frame_us.load(std::memory_order_relaxed) / 1000.0; + for (uint64_t f = captured_ + 1; f <= frames; ++f) + std::fprintf(csv_, "%llu,%.4f,%.3f\n", (unsigned long long)f, double(now - capture_start_ns_) / 1e9, ms); + captured_ = frames; + if ((frames & 255) == 0) std::fflush(csv_); +} + +namespace { +// green at 60 or better, white at 30, then yellow, then red +ImU32 RateColor(float ms) { + return ms <= 17.5f ? IM_COL32(120, 230, 140, 255) + : ms <= 34.5f ? IM_COL32(244, 245, 247, 255) + : ms <= 50.5f ? IM_COL32(245, 200, 80, 255) + : IM_COL32(240, 90, 80, 255); +} +} // namespace + +bool GameOverlay::WantsContinuousRepaint() const { return REXCVAR_GET(ps5_overlay_every_vblank); } + +void GameOverlay::OnDraw(ImGuiIO& io) { + Capture(); + // (the overlay stays up with the counter off, drawing nothing: the game's + // frames reach the TV the same way either way) + const std::string mode = REXCVAR_GET(ps5_fps_counter); + if (mode.empty() || mode == "false" || mode == "off") return; + const bool simple = mode == "simple"; + Sample(); + const ImVec2 size = io.DisplaySize; + const float s = std::min(size.x / 1920.0f, size.y / 1080.0f); + ImDrawList* dl = ImGui::GetForegroundDrawList(); + const auto& fonts = hub::Fonts(); + ImFont* bold = fonts.body_bold ? fonts.body_bold : ImGui::GetFont(); + ImFont* body = fonts.body ? fonts.body : ImGui::GetFont(); + auto P = [&](float x, float y) { return ImVec2(x * s, y * s); }; + + // ---- the counter, on the left edge halfway down (the game's own HUD uses + // the corners). Simple: frames a second and the frame time. Detailed: also + // the slowest frames, what the picture is set to and a graph. + std::vector info; + if (!simple) { + // what the picture is: the game's resolution, the TV, the frame limit + if (!resolution_at_ns_ || rex::frame_stats::NowNs() - resolution_at_ns_ > 1'000'000'000) { + resolution_ = rex::cvar::GetFlagByName("resolution"); + resolution_at_ns_ = rex::frame_stats::NowNs(); + } + std::string res = resolution_; + if (res == "4k") res = "4K"; + std::string tv = display::HighRefresh() ? (display::Vrr() ? "120 Hz VRR" : "120 Hz") : "60 Hz"; + const bool fg = rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed); + info.push_back(res + " \xC2\xB7 " + tv + + (fg ? std::string() : " \xC2\xB7 limit " + std::to_string(REXCVAR_GET(gta4_frame_limit)))); + if (fg) { + const uint32_t v = std::max(1, rex::frame_stats::g_fg_vblanks_per_image.load(std::memory_order_relaxed)); + const int out = (display::HighRefresh() ? 120 : 60) / int(v); + info.push_back("Frame generation " + std::to_string(out / 2) + " \xE2\x86\x92 " + std::to_string(out)); + } + // the renderer's share of the last frame (the graphics thread's recording) + if (from_tv_) info.push_back(fmt::format("Game {:.0f} FPS \xC2\xB7 {} stutter{}", game_fps_, stutters_, + stutters_ == 1 ? "" : "s")); + if (const uint32_t rec = rex::frame_stats::g_last_recorder_busy_us.load(std::memory_order_relaxed)) + info.push_back("Renderer " + fmt::format("{:.1f}", rec / 1000.0) + " ms a frame"); + } + const float w = simple ? 196 : 264, pad = 12, line_h = 19; + const float graph_h = simple ? 0 : 48; + const float h = simple ? pad + 36 + pad - 4 + : pad + 36 + 20 + float(info.size()) * line_h + 10 + graph_h + pad; + const float x = 12, y = (size.y / s - h) * 0.5f; + dl->AddRectFilled(P(x, y), P(x + w, y + h), IM_COL32(0, 0, 0, 170), 8 * s); + char big[16], unit[] = "FPS", ms[32], small[96]; + if (fps_ > 0) { + std::snprintf(big, sizeof(big), "%.0f", fps_); + std::snprintf(ms, sizeof(ms), "%.1f ms", ms_); + std::snprintf(small, sizeof(small), "1%% low %.0f \xC2\xB7 worst frame %.0f ms", low_fps_, worst_ms_); + } else { + std::snprintf(big, sizeof(big), "--"); + ms[0] = small[0] = 0; + } + const ImU32 col = fps_ > 0 ? RateColor(ms_) : IM_COL32(244, 245, 247, 255); + const float big_size = 36 * s; + const ImVec2 bsz = bold->CalcTextSizeA(big_size, 1e9f, 0, big); + dl->AddText(bold, big_size, P(x + pad, y + pad - 5), col, big); + dl->AddText(bold, 15 * s, ImVec2((x + pad) * s + bsz.x + 6 * s, (y + pad + 13) * s), IM_COL32(255, 255, 255, 170), unit); + const ImVec2 msz = bold->CalcTextSizeA(20 * s, 1e9f, 0, ms); + dl->AddText(bold, 20 * s, ImVec2((x + w - pad) * s - msz.x, (y + pad + 8) * s), IM_COL32(244, 245, 247, 255), ms); + if (simple) return; + dl->AddText(body, 14 * s, P(x + pad, y + pad + 37), IM_COL32(255, 255, 255, 175), small); + for (size_t i = 0; i < info.size(); ++i) + dl->AddText(body, 14 * s, P(x + pad, y + pad + 37 + 20 + float(i) * line_h), IM_COL32(255, 255, 255, 175), + info[i].c_str()); + + // the graph: one bar a frame (as the TV showed them, when known), newest + // on the right, 0 to 66 ms (15 FPS) tall + const float gx = x + pad, gy = y + pad + 36 + 20 + float(info.size()) * line_h + 10, gw = w - pad * 2; + dl->AddRectFilled(P(gx, gy), P(gx + gw, gy + graph_h), IM_COL32(255, 255, 255, 18), 4 * s); + constexpr float kTopMs = 66.7f; + const uint64_t frames = from_tv_ ? rex::frame_stats::g_tv_shown.load(std::memory_order_acquire) + : rex::frame_stats::g_presents.load(std::memory_order_acquire); + constexpr int kBars = 120; + const float bar = gw / float(kBars); + for (int i = 0; i < kBars && uint64_t(i) + 1 < frames; ++i) { + const uint32_t us = + from_tv_ ? rex::frame_stats::g_tv_hold_us[(frames - 1 - uint64_t(i)) % rex::frame_stats::kTvHistory].load( + std::memory_order_relaxed) + : rex::frame_stats::g_history_us[(frames - 1 - uint64_t(i)) % rex::frame_stats::kHistory].load( + std::memory_order_relaxed); + if (!us) continue; + const float f_ms = float(us) / 1000.0f; + const float bh = graph_h * std::min(f_ms, kTopMs) / kTopMs; + const float bx = gx + gw - bar * float(i + 1); + dl->AddRectFilled(P(bx, gy + graph_h - bh), P(bx + bar * 0.8f, gy + graph_h), RateColor(f_ms)); + } + for (float line_ms : {16.7f, 33.3f}) { // the 60 and 30 FPS marks, over the bars + const float ly = gy + graph_h - graph_h * line_ms / kTopMs; + dl->AddLine(P(gx, ly), P(gx + gw, ly), IM_COL32(0, 0, 0, 140), 2 * s); + dl->AddLine(P(gx, ly), P(gx + gw, ly), IM_COL32(255, 255, 255, 90), 1 * s); + const char* label = line_ms < 20 ? "60" : "30"; + dl->AddText(bold, 11 * s, P(gx + 3 + 1, ly - 13 + 1), IM_COL32(0, 0, 0, 200), label); + dl->AddText(bold, 11 * s, P(gx + 3, ly - 13), IM_COL32(255, 255, 255, 200), label); + } +} + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.h new file mode 100644 index 00000000..34ef92a6 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/ps5_overlay.h @@ -0,0 +1,52 @@ +// GTA IV PS5 - what the PS5 build draws over the game: the frame rate counter +// when it's on in the hub's Display settings (on the left edge: frames a +// second, the frame time, the slowest frames and a graph of the last frames), +// and, when it's on in the hub's Debug settings, a frame time capture to +// /app0/logs. +#pragma once + +#include +#include +#include + +#include + +namespace gta4ps5 { + +class GameOverlay final : public rex::ui::ImGuiDialog { + public: + explicit GameOverlay(rex::ui::ImGuiDrawer* drawer); + ~GameOverlay() override; + + protected: + void OnDraw(ImGuiIO& io) override; + // [ps5 port] Drawn with each game frame, not every vblank: repainting every + // vblank kept the TV's queue full of repeats, and each new frame waited + // behind two of them (two vblanks of input lag). + bool WantsContinuousRepaint() const override; + + private: + void Sample(); + void Capture(); + + // the counter: averaged over half a second, from what the TV showed + // (VideoOut's flips) when the pacing thread has it, else the game's frames + uint64_t window_frames_ = 0, window_tv_ = 0; + int64_t window_start_ns_ = 0; + float fps_ = 0, ms_ = 0; + float low_fps_ = 0, worst_ms_ = 0; // (the 1% slowest frames, and the slowest) + float game_fps_ = 0; // the game's own frames a second + uint32_t stutters_ = 0; // frames the TV held too long, of the last 300 + bool from_tv_ = false; + std::string resolution_; // (read once a second: a by-name lookup locks) + int64_t resolution_at_ns_ = 0; + // the capture + std::FILE* csv_ = nullptr; + uint64_t captured_ = 0; + int64_t capture_start_ns_ = 0; +}; + +// The build's version. +const char* BuildVersion(); + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.cpp new file mode 100644 index 00000000..4e2ec4be --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.cpp @@ -0,0 +1,162 @@ +// GTA IV PS5 - Xbox 360 saves into the PS5 app (see save_import.h). +#include "save_import.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace gta4ps5 { + +namespace fs = std::filesystem; + +namespace { + +constexpr uint32_t kGta4TitleId = 0x545407F2; +constexpr uint64_t kDefaultProfile = 0xB13EBABEBABEBABEull; // the runtime's profile + +struct FileDestroyer { + void operator()(rex::filesystem::File* f) const { + if (f) f->Destroy(); + } +}; + +bool IsDir(const rex::filesystem::Entry* e) { + return e && (e->attributes() & rex::filesystem::kFileAttributeDirectory) != 0; +} + +bool SafeName(const std::string& n) { + return !n.empty() && n != "." && n != ".." && n.find_first_of("/\\:") == std::string::npos; +} + +bool CopyEntry(rex::filesystem::Entry* entry, const fs::path& to, std::string& error) { + if (IsDir(entry)) { + std::error_code ec; + fs::create_directories(to, ec); + for (const auto& child : entry->children()) { + if (!SafeName(child->name())) continue; + if (!CopyEntry(child.get(), to / child->name(), error)) return false; + } + return true; + } + rex::filesystem::File* raw = nullptr; + if (entry->Open(rex::filesystem::FileAccess::kGenericRead, &raw) != 0 || !raw) { + error = "couldn't open " + entry->path(); + return false; + } + std::unique_ptr file(raw); + std::ofstream out(to, std::ios::binary | std::ios::trunc); + if (!out) { + error = "couldn't write " + to.string(); + return false; + } + std::vector buf(1 << 20); + size_t offset = 0; + while (offset < entry->size()) { + const size_t want = std::min(buf.size(), size_t(entry->size() - offset)); + size_t got = 0; + if (file->ReadSync(std::span(buf).first(want), offset, &got) != 0 || got == 0) { + error = "couldn't read " + entry->path(); + return false; + } + out.write(reinterpret_cast(buf.data()), std::streamsize(got)); + offset += got; + } + return bool(out); +} + +// The profile folders saves go in: the ones the game already uses, else the +// runtime's default profile. +std::vector ProfileFolders(const fs::path& saves_root) { + std::vector out; + std::error_code ec; + for (fs::directory_iterator it(saves_root, ec), end; !ec && it != end; it.increment(ec)) { + const std::string n = it->path().filename().string(); + if (!it->is_directory(ec) || n.size() != 16) continue; + bool hex = true; + for (char c : n) hex &= std::isxdigit(static_cast(c)) != 0; + if (hex) out.push_back(it->path()); + } + if (out.empty()) out.push_back(saves_root / fmt::format("{:016X}", kDefaultProfile)); + return out; +} + +} // namespace + +fs::path SaveImportFolder(const fs::path& app) { return app / "import"; } + +int PendingSaveImports(const fs::path& app) { + int n = 0; + std::error_code ec; + for (fs::directory_iterator it(SaveImportFolder(app), ec), end; !ec && it != end; it.increment(ec)) + if (it->is_regular_file(ec) && it->path().filename().string()[0] != '.') ++n; + return n; +} + +SaveImportResult ImportXbox360Saves(const fs::path& app, const fs::path& saves_root) { + SaveImportResult result; + const fs::path folder = SaveImportFolder(app); + std::error_code ec; + fs::create_directories(folder, ec); + std::vector files; + for (fs::directory_iterator it(folder, ec), end; !ec && it != end; it.increment(ec)) + if (it->is_regular_file(ec) && it->path().filename().string()[0] != '.') files.push_back(it->path()); + + for (const fs::path& file : files) { + const std::string name = file.filename().string(); + auto header = rex::filesystem::StfsContainerDevice::ReadPackageHeader(file); + if (!header) { + result.errors.push_back(name + ": not an Xbox 360 save package"); + continue; + } + const uint32_t title = header->metadata.execution_info.title_id; + const auto type = header->metadata.content_type; + if (title != kGta4TitleId) { + result.errors.push_back(fmt::format("{}: another game's ({:08X})", name, title)); + continue; + } + if (type != rex::filesystem::XContentType::kSavedGame) { + result.errors.push_back(name + ": not a saved game"); + continue; + } + rex::filesystem::StfsContainerDevice device("import:", file, false); + if (!device.Initialize()) { + result.errors.push_back(name + ": the package is damaged"); + continue; + } + auto* root = device.ResolvePath(""); + if (!root) { + result.errors.push_back(name + ": the package is empty"); + continue; + } + bool ok = true; + for (const fs::path& profile : ProfileFolders(saves_root)) { + const fs::path dest = profile / "545407F2" / "00000001" / name; + fs::remove_all(dest, ec); + std::string error; + if (!CopyEntry(root, dest, error)) { + result.errors.push_back(name + ": " + error); + ok = false; + break; + } + REXLOG_INFO("PS5 save import: {} -> {}", name, dest.string()); + } + if (!ok) continue; + ++result.imported; + result.names.push_back(name); + fs::create_directories(folder / "done", ec); + fs::rename(file, folder / "done" / name, ec); + } + for (const auto& e : result.errors) REXLOG_WARN("PS5 save import: {}", e); + return result; +} + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.h new file mode 100644 index 00000000..83297a9d --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/save_import.h @@ -0,0 +1,28 @@ +// GTA IV PS5 - Xbox 360 saves into the PS5 app. The player copies the save +// packages off a 360 (the files in Content//545407F2/00000001 on its +// drive, or a USB stick's Xbox360 folder) into the app's import folder; this +// unpacks each one into the game's save folder. +#pragma once + +#include +#include +#include + +namespace gta4ps5 { + +struct SaveImportResult { + int imported = 0; // packages unpacked + std::vector names; // ...their names + std::vector errors; // files that couldn't be used, and why +}; + +// /import: where the Xbox 360 save packages go. +std::filesystem::path SaveImportFolder(const std::filesystem::path& app); +// How many files wait in the import folder. +int PendingSaveImports(const std::filesystem::path& app); +// Unpacks every GTA IV save package in the import folder into `saves_root` +// (//545407F2/00000001//). Imported +// packages move to import/done. +SaveImportResult ImportXbox360Saves(const std::filesystem::path& app, const std::filesystem::path& saves_root); + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.cpp new file mode 100644 index 00000000..786c5fc6 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.cpp @@ -0,0 +1,443 @@ +// GTA IV PS5 - the port's own sound on the PS5 (sound.h). +#include "sound.h" + +#include +#include +#include +#include +#include +#include + +#define DR_MP3_IMPLEMENTATION +#define DRMP3_API static +#include "../hub/dr_mp3.h" +#define DR_FLAC_IMPLEMENTATION +#define DRFLAC_API static +#include "../hub/dr_flac.h" +#define STB_VORBIS_NO_PUSHDATA_API +#include "../hub/stb_vorbis.c" +#undef L +#undef C +#undef R + +#if defined(__PROSPERO__) +#include +extern "C" { +int32_t sceAudioOutInit(void); +int32_t sceAudioOutOpen(int32_t user_id, int32_t type, int32_t index, uint32_t len, uint32_t freq, uint32_t param); +int32_t sceAudioOutOutput(int32_t handle, const void* ptr); +} +#endif + +namespace gta4ps5::sound { +namespace { + +std::string Lower(std::string s) { + for (char& c : s) c = char(std::tolower(static_cast(c))); + return s; +} + +// A decoder: frames at the file's own rate and channel count. +class Decoder { + public: + virtual ~Decoder() = default; + virtual size_t Read(float* out, size_t frames) = 0; // interleaved, `channels` per frame + virtual bool Rewind() = 0; + uint32_t rate = 0, channels = 0; + uint64_t total = 0; // frames (0: not known) +}; + +class Mp3 final : public Decoder { + public: + bool Open(const std::filesystem::path& f) { + if (!drmp3_init_file(&mp3_, f.string().c_str(), nullptr)) return false; + open_ = true; + rate = mp3_.sampleRate, channels = mp3_.channels; + return rate && channels; + } + ~Mp3() override { + if (open_) drmp3_uninit(&mp3_); + } + size_t Read(float* out, size_t frames) override { return size_t(drmp3_read_pcm_frames_f32(&mp3_, frames, out)); } + bool Rewind() override { return drmp3_seek_to_pcm_frame(&mp3_, 0); } + + private: + drmp3 mp3_{}; + bool open_ = false; +}; + +class Flac final : public Decoder { + public: + bool Open(const std::filesystem::path& f) { + flac_ = drflac_open_file(f.string().c_str(), nullptr); + if (!flac_) return false; + rate = flac_->sampleRate, channels = flac_->channels, total = flac_->totalPCMFrameCount; + return rate && channels; + } + ~Flac() override { + if (flac_) drflac_close(flac_); + } + size_t Read(float* out, size_t frames) override { return size_t(drflac_read_pcm_frames_f32(flac_, frames, out)); } + bool Rewind() override { return drflac_seek_to_pcm_frame(flac_, 0); } + + private: + drflac* flac_ = nullptr; +}; + +class Ogg final : public Decoder { + public: + bool Open(const std::filesystem::path& f) { + int error = 0; + ogg_ = stb_vorbis_open_filename(f.string().c_str(), &error, nullptr); + if (!ogg_) return false; + const stb_vorbis_info info = stb_vorbis_get_info(ogg_); + rate = info.sample_rate, channels = uint32_t(info.channels), total = stb_vorbis_stream_length_in_samples(ogg_); + return rate && channels; + } + ~Ogg() override { + if (ogg_) stb_vorbis_close(ogg_); + } + size_t Read(float* out, size_t frames) override { + return size_t(stb_vorbis_get_samples_float_interleaved(ogg_, int(channels), out, int(frames * channels))); + } + bool Rewind() override { return stb_vorbis_seek_start(ogg_) != 0; } + + private: + stb_vorbis* ogg_ = nullptr; +}; + +// WAV: PCM 8/16/24/32-bit or 32-bit float (plain or WAVE_FORMAT_EXTENSIBLE). +class Wav final : public Decoder { + public: + bool Open(const std::filesystem::path& f) { + file_ = std::fopen(f.string().c_str(), "rb"); + if (!file_) return false; + uint8_t riff[12]; + if (std::fread(riff, 1, 12, file_) != 12 || std::memcmp(riff, "RIFF", 4) || std::memcmp(riff + 8, "WAVE", 4)) + return false; + bool have_fmt = false; + for (;;) { + uint8_t head[8]; + if (std::fread(head, 1, 8, file_) != 8) return false; + const uint32_t size = U32(head + 4); + if (!std::memcmp(head, "fmt ", 4)) { + uint8_t fmt[40] = {}; + const size_t n = std::min(size, sizeof(fmt)); + if (std::fread(fmt, 1, n, file_) != n) return false; + if (size > n) std::fseek(file_, long(size - n), SEEK_CUR); + uint32_t tag = U16(fmt); + channels = U16(fmt + 2), rate = U32(fmt + 4), bits_ = U16(fmt + 14); + if (tag == 0xFFFE && size >= 26) tag = U16(fmt + 24); // (extensible: the sub-format's first word) + float_ = tag == 3; + if ((tag != 1 && tag != 3) || (float_ && bits_ != 32) || (!float_ && bits_ != 8 && bits_ != 16 && bits_ != 24 && bits_ != 32)) + return false; + have_fmt = true; + } else if (!std::memcmp(head, "data", 4)) { + if (!have_fmt || !channels) return false; + data_ = std::ftell(file_); + frame_bytes_ = channels * (bits_ / 8); + total = size / frame_bytes_; + left_ = total; + return rate != 0; + } else { + std::fseek(file_, long(size + (size & 1)), SEEK_CUR); + } + } + } + ~Wav() override { + if (file_) std::fclose(file_); + } + size_t Read(float* out, size_t frames) override { + frames = size_t(std::min(frames, left_)); + buf_.resize(frames * frame_bytes_); + const size_t got = std::fread(buf_.data(), frame_bytes_, frames, file_); + left_ -= got; + const size_t n = got * channels; + const uint8_t* p = buf_.data(); + for (size_t i = 0; i < n; ++i) { + switch (bits_) { + case 8: out[i] = (float(p[i]) - 128.0f) / 128.0f; break; + case 16: out[i] = float(int16_t(U16(p + i * 2))) / 32768.0f; break; + case 24: out[i] = float(int32_t(U32Of3(p + i * 3) << 8) >> 8) / 8388608.0f; break; + default: { + const uint32_t v = U32(p + i * 4); + if (float_) std::memcpy(&out[i], &v, 4); + else out[i] = float(int32_t(v)) / 2147483648.0f; + } + } + } + return got; + } + bool Rewind() override { + left_ = total; + return std::fseek(file_, data_, SEEK_SET) == 0; + } + + private: + static uint32_t U16(const uint8_t* p) { return uint32_t(p[0]) | uint32_t(p[1]) << 8; } + static uint32_t U32(const uint8_t* p) { return U16(p) | U16(p + 2) << 16; } + static uint32_t U32Of3(const uint8_t* p) { return uint32_t(p[0]) | uint32_t(p[1]) << 8 | uint32_t(p[2]) << 16; } + std::FILE* file_ = nullptr; + long data_ = 0; + uint32_t bits_ = 0, frame_bytes_ = 1; + bool float_ = false; + uint64_t left_ = 0; + std::vector buf_; +}; + +// Any decoder as 48 kHz stereo (linear resampling; mono doubled, more than +// two channels: the first two). +class Resampled final : public Source { + public: + explicit Resampled(std::unique_ptr d) : d_(std::move(d)), step_(double(d_->rate) / kRate) {} + size_t Read(float* lr, size_t frames) override { + size_t done = 0; + while (done < frames) { + if (pos_ + 1 >= double(have_)) { + if (!Fill()) break; + continue; + } + const size_t i = size_t(pos_); + const float t = float(pos_ - double(i)); + const float* a = &buf_[i * 2]; + const float* b = &buf_[(i + 1) * 2]; + lr[done * 2] = a[0] + (b[0] - a[0]) * t; + lr[done * 2 + 1] = a[1] + (b[1] - a[1]) * t; + pos_ += step_; + ++done; + } + return done; + } + bool Rewind() override { + if (!d_->Rewind()) return false; + buf_.clear(), have_ = 0, pos_ = 0, ended_ = false; + return true; + } + double Seconds() const override { return d_->total && d_->rate ? double(d_->total) / d_->rate : 0; } + + private: + // More decoded frames after the current ones (keeping the one under pos_). + bool Fill() { + if (ended_) return false; + const size_t keep = std::min(size_t(pos_), have_); + buf_.erase(buf_.begin(), buf_.begin() + std::ptrdiff_t(keep * 2)); + have_ -= keep; + pos_ -= double(keep); + constexpr size_t kChunk = 2048; + raw_.resize(kChunk * d_->channels); + const size_t got = d_->Read(raw_.data(), kChunk); + if (!got) { + ended_ = true; + return false; + } + for (size_t f = 0; f < got; ++f) { + const float* s = &raw_[f * d_->channels]; + buf_.push_back(s[0]); + buf_.push_back(d_->channels > 1 ? s[1] : s[0]); + } + have_ += got; + return true; + } + std::unique_ptr d_; + double step_, pos_ = 0; + std::vector buf_, raw_; + size_t have_ = 0; + bool ended_ = false; +}; + +class InMemory final : public Source { + public: + explicit InMemory(std::shared_ptr> lr) : lr_(std::move(lr)) {} + size_t Read(float* out, size_t frames) override { + const size_t n = std::min(frames, lr_->size() / 2 - at_); + std::memcpy(out, lr_->data() + at_ * 2, n * 2 * sizeof(float)); + at_ += n; + return n; + } + bool Rewind() override { + at_ = 0; + return true; + } + double Seconds() const override { return double(lr_->size() / 2) / kRate; } + + private: + std::shared_ptr> lr_; + size_t at_ = 0; +}; + +template +std::unique_ptr Try(const std::filesystem::path& file) { + auto d = std::make_unique(); + if (!d->Open(file)) return nullptr; + return d; +} + +} // namespace + +bool CanOpen(const std::filesystem::path& file) { + const std::string ext = Lower(file.extension().string()); + return ext == ".mp3" || ext == ".wav" || ext == ".flac" || ext == ".ogg"; +} + +std::unique_ptr Open(const std::filesystem::path& file) { + const std::string ext = Lower(file.extension().string()); + std::unique_ptr d; + if (ext == ".mp3") d = Try(file); + else if (ext == ".wav") d = Try(file); + else if (ext == ".flac") d = Try(file); + else if (ext == ".ogg") d = Try(file); + if (!d) return nullptr; + return std::make_unique(std::move(d)); +} + +std::unique_ptr Memory(std::shared_ptr> lr) { + return std::make_unique(std::move(lr)); +} + +// ------------------------------------------------------------------- mixer + +class Mixer { + public: + static Mixer& Get() { + static Mixer* m = new Mixer; + return *m; + } + std::mutex mutex; + std::vector voices; + std::vector scratch, sum; + std::atomic game_muted{false}; + + void MixInto(int16_t* out, size_t frames) { + sum.assign(frames * 2, 0.0f); + scratch.resize(frames * 2); + { + std::lock_guard lock(mutex); + for (Voice* v : voices) Render(*v, frames); + } + for (size_t i = 0; i < frames * 2; ++i) { + // (a soft knee above 0.9 instead of a hard clip when sounds pile up) + float s = sum[i]; + const float a = std::fabs(s); + if (a > 0.9f) s = std::copysign(0.9f + 0.1f * std::tanh((a - 0.9f) * 10.0f), s); + out[i] = int16_t(std::clamp(s, -1.0f, 1.0f) * 32767.0f); + } + } + + private: + void Render(Voice& v, size_t frames) { + if (!v.source_ || v.paused_) return; + size_t got = 0; + while (got < frames) { + const size_t n = v.source_->Read(scratch.data() + got * 2, frames - got); + got += n; + if (n == 0) { + if (v.loop_ && v.source_->Rewind() && got < frames) { + const size_t again = v.source_->Read(scratch.data() + got * 2, frames - got); + if (!again) break; // (an empty loop) + got += again; + continue; + } + break; + } + } + const float target = (v.stopping_ || (v.game_sound && game_muted)) ? 0.0f : v.volume_; + if (v.fresh_) v.gain_ = target, v.fresh_ = false; + // (volume changes glide over ~20 ms; a fade-out at its own pace) + const float glide = 1.0f / (0.02f * kRate); + for (size_t i = 0; i < got; ++i) { + if (v.stopping_ && v.fade_step_ > 0) v.gain_ = std::max(0.0f, v.gain_ - v.fade_step_); + else if (v.gain_ < target) v.gain_ = std::min(target, v.gain_ + glide); + else if (v.gain_ > target) v.gain_ = std::max(target, v.gain_ - glide); + const float g = v.gain_ * v.gain_; // (a gentler curve than linear) + sum[i * 2] += scratch[i * 2] * g; + sum[i * 2 + 1] += scratch[i * 2 + 1] * g; + } + if (got < frames || (v.stopping_ && v.gain_ <= 0.0f)) { + v.source_.reset(); + v.active_.store(false, std::memory_order_release); + } + } +}; + +void Voice::Play(std::unique_ptr source, bool loop) { + Mixer& m = Mixer::Get(); + std::lock_guard lock(m.mutex); + source_ = std::move(source); + loop_ = loop, paused_ = false, stopping_ = false, fade_step_ = 0, fresh_ = true; + active_.store(source_ != nullptr, std::memory_order_release); +} + +void Voice::Stop() { FadeOut(0.02f); } + +void Voice::FadeOut(float seconds) { + Mixer& m = Mixer::Get(); + std::lock_guard lock(m.mutex); + if (!source_) return; + stopping_ = true; + fade_step_ = std::max(gain_, 0.001f) / (std::max(seconds, 0.001f) * kRate); +} + +void Voice::Pause(bool paused) { + Mixer& m = Mixer::Get(); + std::lock_guard lock(m.mutex); + paused_ = paused; +} + +void Voice::SetVolume(float volume) { + Mixer& m = Mixer::Get(); + std::lock_guard lock(m.mutex); + volume_ = std::clamp(volume, 0.0f, 1.0f); +} + +Voice* NewVoice() { + Voice* v = new Voice; + Mixer& m = Mixer::Get(); + std::lock_guard lock(m.mutex); + m.voices.push_back(v); + return v; +} + +void Mix(int16_t* out, size_t frames) { Mixer::Get().MixInto(out, frames); } + +void SetGameMuted(bool muted) { Mixer::Get().game_muted = muted; } + +bool StartOutput() { +#if defined(__PROSPERO__) + static std::atomic state{0}; // 0 not yet, 1 running, -1 failed + int expected = 0; + if (!state.compare_exchange_strong(expected, 2)) { + while (state.load() == 2) { + } + return state.load() == 1; + } + sceAudioOutInit(); + constexpr uint32_t kFrames = 256; + static int32_t port = sceAudioOutOpen(0xFF, 0, 0, kFrames, kRate, 1 /* s16 stereo */); + if (port < 0) { + state = -1; + return false; + } + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 1 << 20); // (the decoders' frames are big) + pthread_t thread; + const int rc = pthread_create( + &thread, &attr, + [](void*) -> void* { + int16_t out[kFrames * 2]; + for (;;) { + Mix(out, kFrames); + sceAudioOutOutput(port, out); // (blocks: paces the loop) + } + return nullptr; + }, + nullptr); + pthread_attr_destroy(&attr); + state = rc == 0 ? 1 : -1; + return rc == 0; +#else + return false; // (tests call Mix themselves) +#endif +} + +} // namespace gta4ps5::sound diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.h new file mode 100644 index 00000000..f0379480 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/sound.h @@ -0,0 +1,78 @@ +// GTA IV PS5 - sound of the port's own on the PS5 (the hub's +// music and menu sounds, and in the game the songs of USER PLAYLIST, the +// jukebox's MY MUSIC and the mods' sounds: music.cpp). +// +// Files (.mp3, .wav, .flac, .ogg) are decoded on the fly to 48 kHz stereo +// and mixed into one audio port of their own, beside the game's. +// The decoding and mixing are plain C++ (tests run them on a PC); only the +// output is the PS5's (sceAudioOut). +#pragma once + +#include +#include +#include +#include +#include + +namespace gta4ps5::sound { + +constexpr uint32_t kRate = 48000; + +// A decoded sound: 48 kHz stereo float frames. +class Source { + public: + virtual ~Source() = default; + // Up to `frames` frames into lr (interleaved); fewer at the end. + virtual size_t Read(float* lr, size_t frames) = 0; + // Back to the start (for a loop); false if it can't. + virtual bool Rewind() = 0; + // Its length in seconds (0: not known). + virtual double Seconds() const { return 0; } +}; + +// The file's sound (by its extension and content); null if it can't be read. +std::unique_ptr Open(const std::filesystem::path& file); +// A sound in memory (48 kHz stereo frames), shared by every play of it. +std::unique_ptr Memory(std::shared_ptr> lr); +// Whether Open knows the file's type (by its extension). +bool CanOpen(const std::filesystem::path& file); + +// A voice of the mixer: plays one source at a time. +class Voice { + public: + // Starts `source` (replacing what was playing); `loop`: from the top again at its end. + void Play(std::unique_ptr source, bool loop); + void Stop(); // at once (a short fade, no click) + void FadeOut(float seconds); // then stops + void Pause(bool paused); + void SetVolume(float volume); // 0..1, eased + // Playing or paused (not finished, not stopped). + bool Active() const { return active_.load(std::memory_order_acquire); } + // Honour the game's audio_mute (the game's sounds; not the hub's). + bool game_sound = false; + + private: + friend class Mixer; + std::atomic active_{false}; + // (the rest belongs to the mixer, under its lock) + std::unique_ptr source_; + bool loop_ = false, paused_ = false, stopping_ = false; + float volume_ = 1, gain_ = 0, fade_step_ = 0; + bool fresh_ = false; // (its gain starts at the volume: no fade in) +}; + +// The game's audio_mute (silences the voices marked game_sound). +void SetGameMuted(bool muted); + +// A new voice (they live as long as the program). +Voice* NewVoice(); + +// Mixes every voice's next `frames` frames into out (interleaved stereo +// 16-bit): what the output plays. (Called by the output; tests call it.) +void Mix(int16_t* out, size_t frames); + +// Starts the output (the PS5's own audio port, on a thread of its own). +// Idempotent; false if the port couldn't be opened. +bool StartOutput(); + +} // namespace gta4ps5::sound diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.cpp new file mode 100644 index 00000000..27250925 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.cpp @@ -0,0 +1,182 @@ +// [ps5 port] see trophies_game.h +#include "trophies_game.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../hub/hub_lang.h" +#include "../hub/hub_sfx.h" +#include "../hub/hub_trophies.h" +#include "sound.h" +#if defined(__PROSPERO__) +#include "../hub/ps5_hub.h" +#endif + +namespace gta4ps5 { + +namespace fs = std::filesystem; + +// ------------------------------------------------------------- the catalog + +void WriteTrophyCatalog(rex::Runtime* runtime, const fs::path& user_data) { + auto* ks = runtime ? runtime->kernel_state() : nullptr; + if (!ks) return; + std::vector list; + int earned = 0; + for (const auto& a : ks->achievements().ListAchievements()) { + trophies::Trophy t; + t.id = a.id, t.gamerscore = a.gamerscore, t.flags = a.flags, t.image_id = a.image_id; + t.name = a.label, t.description = a.description, t.locked_description = a.unachieved_description; + earned += ks->achievements().IsUnlocked(a.id) ? 1 : 0; + list.push_back(std::move(t)); + } + std::sort(list.begin(), list.end(), [](const auto& a, const auto& b) { return a.id < b.id; }); + if (list.empty()) { + REXLOG_WARN("trophies: the game has no achievements loaded"); + return; + } + const bool ok = trophies::WriteCatalog(user_data, list); + // their pictures (PNGs in default.xex), once + int pictures = 0; + const auto db = ks->title_xdbf(); + auto save = [&](const fs::path& file, const rex::system::util::XdbfBlock& block) { + std::error_code ec; + if (!block || fs::exists(file, ec)) return; + fs::create_directories(file.parent_path(), ec); + std::ofstream(file, std::ios::binary).write(reinterpret_cast(block.buffer), std::streamsize(block.size)); + ++pictures; + }; + if (db.is_valid()) { + for (const auto& t : list) + if (t.image_id) + save(trophies::IconFile(user_data, t.image_id), + db.GetEntry(rex::system::util::XdbfSection::kImage, uint64_t(t.image_id))); + save(trophies::TitleIconFile(user_data), db.icon()); + } + REXLOG_INFO("trophies: {} achievements, {} earned; the list {} ({} new pictures) in {}", list.size(), earned, + ok ? "written" : "NOT written", pictures, trophies::Folder(user_data).string()); +} + +// ---------------------------------------------------------------- the popup + +namespace { + +struct Fonts { + ImFont* body = nullptr; + ImFont* bold = nullptr; +}; + +Fonts GetFonts() { + Fonts f; +#if defined(__PROSPERO__) + f.body = hub::Fonts().body, f.bold = hub::Fonts().body_bold; +#endif + if (!f.body) f.body = ImGui::GetFont(); + if (!f.bold) f.bold = f.body; + return f; +} + +} // namespace + +TrophyPopup::TrophyPopup(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, rex::Runtime* runtime) + : AchievementNotificationDialog(drawer), runtime_(runtime), immediate_(immediate), icons_(immediate, runtime) {} + +TrophyPopup::~TrophyPopup() = default; + +void TrophyPopup::Push(const rex::system::AchievementEvent& event) { + // the last one that can be earned here: the platinum after it + bool platinum = false; + if (trophies::Here(event.achievement.id) && runtime_ && runtime_->kernel_state()) { + auto& m = runtime_->kernel_state()->achievements(); + int earnable = 0, earned = 0; + for (const auto& a : m.ListAchievements()) { + if (!trophies::Here(a.id)) continue; + ++earnable; + earned += m.IsUnlocked(a.id) ? 1 : 0; + } + platinum = earnable > 0 && earned == earnable; + } + REXLOG_INFO("trophies: {} ({}, {}G){}", event.achievement.label, + trophies::GradeName(trophies::GradeFor(event.achievement.gamerscore)), event.achievement.gamerscore, + platinum ? ", and with it the platinum" : ""); + { + std::lock_guard lock(mutex_); + queue_.push_back({event.achievement, false, std::nullopt}); + if (platinum) queue_.push_back({event.achievement, true, std::nullopt}); + } + RequestRepaint(); +} + +bool TrophyPopup::WantsContinuousRepaint() const { + std::lock_guard lock(mutex_); + return !queue_.empty(); +} + +void TrophyPopup::Chime(bool platinum) { + static auto* voice = [] { + sound::StartOutput(); + return sound::NewVoice(); + }(); + static const auto chime = std::make_shared>(hub::TrophyChime(false)); + static const auto chime_platinum = std::make_shared>(hub::TrophyChime(true)); + voice->SetVolume(0.9f); + voice->Play(sound::Memory(platinum ? chime_platinum : chime), false); +} + +rex::ui::ImmediateTexture* TrophyPopup::TitlePicture() { + if (title_picture_tried_) return title_picture_.get(); + title_picture_tried_ = true; + if (!immediate_ || !runtime_ || !runtime_->kernel_state()) return nullptr; + const auto db = runtime_->kernel_state()->title_xdbf(); + if (!db.is_valid()) return nullptr; + const auto block = db.icon(); + if (!block) return nullptr; + int w = 0, h = 0; + const std::vector px = rex::ui::DecodeImageRGBA(block.buffer, block.size, w, h); + if (px.empty()) return nullptr; + title_picture_ = immediate_->CreateTexture(uint32_t(w), uint32_t(h), rex::ui::ImmediateTextureFilter::kLinear, false, + px.data()); + return title_picture_.get(); +} + +void TrophyPopup::OnDraw(ImGuiIO& io) { + const auto now = std::chrono::steady_clock::now(); + Card card; + float age = 0; + bool start = false; + { + std::lock_guard lock(mutex_); + while (!queue_.empty()) { + Card& front = queue_.front(); + if (!front.shown) front.shown = now, start = true; + age = std::chrono::duration(now - *front.shown).count(); + if (age < (front.platinum ? 7.0f : 5.5f)) { + card = front; + break; + } + queue_.pop_front(); + } + if (queue_.empty()) return; + } + if (start) Chime(card.platinum); + trophies::Card view; + view.platinum = card.platinum; + view.grade = card.platinum ? trophies::Grade::kPlatinum : trophies::GradeFor(card.achievement.gamerscore); + view.name = card.platinum ? hub::lang::T("Grand Theft Auto IV Platinum") : card.achievement.label; + view.picture = reinterpret_cast(card.platinum ? TitlePicture() : icons_.GetIcon(card.achievement)); + const Fonts f = GetFonts(); + trophies::DrawCard(ImGui::GetForegroundDrawList(), io.DisplaySize, view, age, f.body, f.bold); +} + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.h new file mode 100644 index 00000000..22d17320 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/game/trophies_game.h @@ -0,0 +1,68 @@ +// [ps5 port] GTA IV's achievements as trophies, in the game (see +// hub/hub_trophies.h for the whole of it): +// +// - WriteCatalog: as the game starts, its achievements' list and pictures +// (from default.xex) go to user_data/achievements, for the hub's Trophies page; +// - TrophyPopup: an unlock shows in the top-right corner, where the PS5 shows +// its trophies - the achievement's picture, its grade's cup and its name, +// with a chime - instead of the runtime's toast. The last of GTA IV's +// brings the platinum's after it. +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace rex { +class Runtime; +} +namespace rex::ui { +class ImmediateDrawer; +class ImmediateTexture; +} // namespace rex::ui + +namespace gta4ps5 { + +void WriteTrophyCatalog(rex::Runtime* runtime, const std::filesystem::path& user_data); + +class TrophyPopup final : public rex::ui::AchievementNotificationDialog { + public: + TrophyPopup(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, rex::Runtime* runtime); + ~TrophyPopup() override; + + // (from the thread that unlocked it) + void Push(const rex::system::AchievementEvent& event) override; + // [ps5 port] Only while a card is up. The default (always) had the screen + // redrawn at every vblank for the whole session: the frame on the TV sent + // again ~50 times a second, new frames waiting behind those repeats and + // landing one vblank early or late (1.1's logs: "TV 110 fps" at 60 FPS). + bool WantsContinuousRepaint() const override; + + protected: + void OnDraw(ImGuiIO& io) override; + + private: + struct Card { + rex::system::AchievementInfo achievement; + bool platinum = false; + std::optional shown; + }; + void Chime(bool platinum); + rex::ui::ImmediateTexture* TitlePicture(); + + rex::Runtime* runtime_ = nullptr; + rex::ui::ImmediateDrawer* immediate_ = nullptr; + rex::ui::AchievementIconCache icons_; + std::unique_ptr title_picture_; + bool title_picture_tried_ = false; + mutable std::mutex mutex_; + std::deque queue_; +}; + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.cpp new file mode 100644 index 00000000..6e7ba8f3 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.cpp @@ -0,0 +1,108 @@ +// GTA IV PS5 - the app's PS5 parts (see gta4_ps5_app.h). +#include +#if REX_PLATFORM_PS5 + +#include "gta4_ps5_app.h" + +#include +#include + +#include +#include +#include +#include + +#include "../gta4_app.h" +#include "trophies_native/native_trophies.h" +#include "game/ps5_display.h" +#include "game/ps5_overlay.h" +#include "game/save_import.h" +#include "game/trophies_game.h" +#include "hub/hub_lang.h" +#include "hub/ps5_hub.h" + +// The hub's Language (Game page): auto (the PS5's), english, french, german, +// spanish or italian. The game is told it as the Xbox 360's language and +// reads its text from the matching .gxt (the USA disc has all five). +REXCVAR_DEFINE_STRING(ps5_language, "auto", "PS5", "Game and hub language: auto, english, french, german, spanish, italian"); +// The hub's City Population (Display page): the game's four population +// scales together (gta4_population_hooks.cpp), set at the start. +REXCVAR_DEFINE_DOUBLE(ps5_population, 1.0, "PS5", "Cars and people in the city: 1.0 is the Xbox 360's").range(0.25, 2.0); + +namespace gta4ps5 { + +namespace fs = std::filesystem; + +namespace { +const fs::path kApp("/app0"); +fs::path g_saves_root; +std::unique_ptr g_overlay; +} // namespace + +void ShowHub(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, const rex::PathConfig& paths, + const fs::path& liberty_root, const fs::path& config, std::function launch) { + g_saves_root = paths.saved_game_root.empty() ? liberty_root / "saves" : paths.saved_game_root; + std::error_code ec; + fs::create_directories(config.parent_path(), ec); + fs::create_directories(SaveImportFolder(kApp), ec); + hub::Paths hub_paths; + hub_paths.app = kApp; + hub_paths.config = config; + hub_paths.user_data = g_saves_root; + hub_paths.install = liberty_root; + hub::SetSaveImport({[] { return PendingSaveImports(kApp); }, + [] { + const SaveImportResult r = ImportXbox360Saves(kApp, g_saves_root); + namespace lang = hub::lang; + std::string message; + if (r.imported) + message = lang::F(r.imported == 1 ? "{} save imported." : "{} saves imported.", + {std::to_string(r.imported)}) + + " " + lang::T("Load them from the game's menu."); + else + message = lang::T("No saves were imported."); + for (size_t i = 0; i < r.errors.size() && i < 4; ++i) message += "\n" + r.errors[i]; + if (r.errors.size() > 4) message += std::string("\n") + lang::T("...the log lists the rest."); + return message; + }}); + hub::SetDisplay({[] { return display::HighRefresh(); }, [](bool high) { return display::SetHighRefresh(high); }, + [] { return display::Vrr(); }, [] { return display::Ready(); }}); + hub::Show(drawer, immediate, hub_paths, [config, launch = std::move(launch)] { + // the hub's settings, for this start + rex::cvar::LoadConfig(config); + // the language: the game reads it as the console's (XCONFIG user language) + const hub::lang::Lang language = hub::lang::FromSetting(REXCVAR_GET(ps5_language)); + REXCVAR_SET(user_language, hub::lang::XboxLanguage(language)); + REXLOG_INFO("PS5 hub: language {} (setting {}, Xbox language {})", hub::lang::Name(language), + REXCVAR_GET(ps5_language), REXCVAR_GET(user_language)); + // the city's population + const std::string population = fmt::format("{}", REXCVAR_GET(ps5_population)); + for (const char* scale : {"gta4_traffic_density_scale", "gta4_parked_car_density_scale", "gta4_ped_density_scale", + "gta4_scenario_ped_density_scale"}) + rex::cvar::SetFlagByName(scale, population); + REXLOG_INFO("PS5 hub: city population {}", population); + REXLOG_INFO("PS5 hub: START GAME (GTA IV PS5 {})", BuildVersion()); + // (the frame limit's pacing on the TV, from now on) + display::StartGamePacing(); + launch(); + }); +} + +void OnGameSetUp(rex::ui::ImGuiDrawer* drawer, rex::Runtime* runtime, const fs::path& user_data) { + WriteTrophyCatalog(runtime, user_data); + // (the game's PS5 trophy set and pop-ups, on a thread of its own) + StartNativeTrophies(runtime, user_data); + g_overlay = std::make_unique(drawer); +} + +} // namespace gta4ps5 + +void GTA4App::OnConfigureFonts(ImFontAtlas* atlas) { gta4ps5::hub::ConfigureFonts(atlas); } + +// [ps5 port] 1.1: no trophy popup in the game: the PS5 shows its own +// (trophies_native/native_trophies.h). Trophies are still earned and recorded as before. +std::unique_ptr GTA4App::CreateAchievementNotificationDialog() { + return nullptr; +} + +#endif // REX_PLATFORM_PS5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.h new file mode 100644 index 00000000..f357bee2 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/gta4_ps5_app.h @@ -0,0 +1,23 @@ +// GTA IV PS5 - the app's PS5 parts: the hub before the game, the trophy +// popup, the frame rate counter and watermark, Xbox 360 save import. +#pragma once + +#include + +#if REX_PLATFORM_PS5 +#include +#include + +#include + +namespace gta4ps5 { + +// Shows the hub (on the UI thread); `launch` runs on START GAME. +void ShowHub(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, const rex::PathConfig& paths, + const std::filesystem::path& liberty_root, const std::filesystem::path& config, + std::function launch); +// The runtime is set up: the trophy list for the hub, the in-game overlay. +void OnGameSetUp(rex::ui::ImGuiDrawer* drawer, rex::Runtime* runtime, const std::filesystem::path& user_data); + +} // namespace gta4ps5 +#endif diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_flac.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_flac.h new file mode 100644 index 00000000..051f37c6 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_flac.h @@ -0,0 +1,12760 @@ +/* +FLAC audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file. +dr_flac - v0.13.4 - TBD + +David Reid - mackron@gmail.com + +GitHub: https://github.com/mackron/dr_libs +*/ + +/* +Introduction +============ +dr_flac is a single file library. To use it, do something like the following in one .c file. + + ```c + #define DR_FLAC_IMPLEMENTATION + #include "dr_flac.h" + ``` + +You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following: + + ```c + drflac* pFlac = drflac_open_file("MySong.flac", NULL); + if (pFlac == NULL) { + // Failed to open FLAC file + } + + drflac_int32* pSamples = malloc(pFlac->totalPCMFrameCount * pFlac->channels * sizeof(drflac_int32)); + drflac_uint64 numberOfInterleavedSamplesActuallyRead = drflac_read_pcm_frames_s32(pFlac, pFlac->totalPCMFrameCount, pSamples); + ``` + +The drflac object represents the decoder. It is a transparent type so all the information you need, such as the number of channels and the bits per sample, +should be directly accessible - just make sure you don't change their values. Samples are always output as interleaved signed 32-bit PCM. In the example above +a native FLAC stream was opened, however dr_flac has seamless support for Ogg encapsulated FLAC streams as well. + +You do not need to decode the entire stream in one go - you just specify how many samples you'd like at any given time and the decoder will give you as many +samples as it can, up to the amount requested. Later on when you need the next batch of samples, just call it again. Example: + + ```c + while (drflac_read_pcm_frames_s32(pFlac, chunkSizeInPCMFrames, pChunkSamples) > 0) { + do_something(); + } + ``` + +You can seek to a specific PCM frame with `drflac_seek_to_pcm_frame()`. + +If you just want to quickly decode an entire FLAC file in one go you can do something like this: + + ```c + unsigned int channels; + unsigned int sampleRate; + drflac_uint64 totalPCMFrameCount; + drflac_int32* pSampleData = drflac_open_file_and_read_pcm_frames_s32("MySong.flac", &channels, &sampleRate, &totalPCMFrameCount, NULL); + if (pSampleData == NULL) { + // Failed to open and decode FLAC file. + } + + ... + + drflac_free(pSampleData, NULL); + ``` + +You can read samples as signed 16-bit integer and 32-bit floating-point PCM with the *_s16() and *_f32() family of APIs respectively, but note that these +should be considered lossy. + + +If you need access to metadata (album art, etc.), use `drflac_open_with_metadata()`, `drflac_open_file_with_metdata()` or `drflac_open_memory_with_metadata()`. +The rationale for keeping these APIs separate is that they're slightly slower than the normal versions and also just a little bit harder to use. dr_flac +reports metadata to the application through the use of a callback, and every metadata block is reported before `drflac_open_with_metdata()` returns. + +The main opening APIs (`drflac_open()`, etc.) will fail if the header is not present. The presents a problem in certain scenarios such as broadcast style +streams or internet radio where the header may not be present because the user has started playback mid-stream. To handle this, use the relaxed APIs: + + `drflac_open_relaxed()` + `drflac_open_with_metadata_relaxed()` + +It is not recommended to use these APIs for file based streams because a missing header would usually indicate a corrupt or perverse file. In addition, these +APIs can take a long time to initialize because they may need to spend a lot of time finding the first frame. + + + +Build Options +============= +#define these options before including this file. + +#define DR_FLAC_NO_STDIO + Disable `drflac_open_file()` and family. + +#define DR_FLAC_NO_OGG + Disables support for Ogg/FLAC streams. + +#define DR_FLAC_BUFFER_SIZE + Defines the size of the internal buffer to store data from onRead(). This buffer is used to reduce the number of calls back to the client for more data. + Larger values means more memory, but better performance. My tests show diminishing returns after about 4KB (which is the default). Consider reducing this if + you have a very efficient implementation of onRead(), or increase it if it's very inefficient. Must be a multiple of 8. + +#define DR_FLAC_NO_CRC + Disables CRC checks. This will offer a performance boost when CRC is unnecessary. This will disable binary search seeking. When seeking, the seek table will + be used if available. Otherwise the seek will be performed using brute force. + +#define DR_FLAC_NO_SIMD + Disables SIMD optimizations (SSE on x86/x64 architectures, NEON on ARM architectures). Use this if you are having compatibility issues with your compiler. + +#define DR_FLAC_NO_WCHAR + Disables all functions ending with `_w`. Use this if your compiler does not provide wchar.h. Not required if DR_FLAC_NO_STDIO is also defined. + + + +Notes +===== +- dr_flac does not support changing the sample rate nor channel count mid stream. +- dr_flac is not thread-safe, but its APIs can be called from any thread so long as you do your own synchronization. +- When using Ogg encapsulation, a corrupted metadata block will result in `drflac_open_with_metadata()` and `drflac_open()` returning inconsistent samples due + to differences in corrupted stream recorvery logic between the two APIs. +*/ + +#ifndef dr_flac_h +#define dr_flac_h + +#ifdef __cplusplus +extern "C" { +#endif + +#define DRFLAC_STRINGIFY(x) #x +#define DRFLAC_XSTRINGIFY(x) DRFLAC_STRINGIFY(x) + +#define DRFLAC_VERSION_MAJOR 0 +#define DRFLAC_VERSION_MINOR 13 +#define DRFLAC_VERSION_REVISION 4 +#define DRFLAC_VERSION_STRING DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MAJOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MINOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_REVISION) + +#include /* For size_t. */ + +/* Sized Types */ +typedef signed char drflac_int8; +typedef unsigned char drflac_uint8; +typedef signed short drflac_int16; +typedef unsigned short drflac_uint16; +typedef signed int drflac_int32; +typedef unsigned int drflac_uint32; +#if defined(_MSC_VER) && !defined(__clang__) + typedef signed __int64 drflac_int64; + typedef unsigned __int64 drflac_uint64; +#else + #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wlong-long" + #if defined(__clang__) + #pragma GCC diagnostic ignored "-Wc++11-long-long" + #endif + #endif + typedef signed long long drflac_int64; + typedef unsigned long long drflac_uint64; + #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic pop + #endif +#endif +#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__powerpc64__) + typedef drflac_uint64 drflac_uintptr; +#else + typedef drflac_uint32 drflac_uintptr; +#endif +typedef drflac_uint8 drflac_bool8; +typedef drflac_uint32 drflac_bool32; +#define DRFLAC_TRUE 1 +#define DRFLAC_FALSE 0 +/* End Sized Types */ + +/* Decorations */ +#if !defined(DRFLAC_API) + #if defined(DRFLAC_DLL) + #if defined(_WIN32) + #define DRFLAC_DLL_IMPORT __declspec(dllimport) + #define DRFLAC_DLL_EXPORT __declspec(dllexport) + #define DRFLAC_DLL_PRIVATE static + #else + #if defined(__GNUC__) && __GNUC__ >= 4 + #define DRFLAC_DLL_IMPORT __attribute__((visibility("default"))) + #define DRFLAC_DLL_EXPORT __attribute__((visibility("default"))) + #define DRFLAC_DLL_PRIVATE __attribute__((visibility("hidden"))) + #else + #define DRFLAC_DLL_IMPORT + #define DRFLAC_DLL_EXPORT + #define DRFLAC_DLL_PRIVATE static + #endif + #endif + + #if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION) + #define DRFLAC_API DRFLAC_DLL_EXPORT + #else + #define DRFLAC_API DRFLAC_DLL_IMPORT + #endif + #define DRFLAC_PRIVATE DRFLAC_DLL_PRIVATE + #else + #define DRFLAC_API extern + #define DRFLAC_PRIVATE static + #endif +#endif +/* End Decorations */ + +#if defined(_MSC_VER) && _MSC_VER >= 1700 /* Visual Studio 2012 */ + #define DRFLAC_DEPRECATED __declspec(deprecated) +#elif (defined(__GNUC__) && __GNUC__ >= 4) /* GCC 4 */ + #define DRFLAC_DEPRECATED __attribute__((deprecated)) +#elif defined(__has_feature) /* Clang */ + #if __has_feature(attribute_deprecated) + #define DRFLAC_DEPRECATED __attribute__((deprecated)) + #else + #define DRFLAC_DEPRECATED + #endif +#else + #define DRFLAC_DEPRECATED +#endif + +DRFLAC_API void drflac_version(drflac_uint32* pMajor, drflac_uint32* pMinor, drflac_uint32* pRevision); +DRFLAC_API const char* drflac_version_string(void); + +/* Allocation Callbacks */ +typedef struct +{ + void* pUserData; + void* (* onMalloc)(size_t sz, void* pUserData); + void* (* onRealloc)(void* p, size_t sz, void* pUserData); + void (* onFree)(void* p, void* pUserData); +} drflac_allocation_callbacks; +/* End Allocation Callbacks */ + +/* +As data is read from the client it is placed into an internal buffer for fast access. This controls the size of that buffer. Larger values means more speed, +but also more memory. In my testing there is diminishing returns after about 4KB, but you can fiddle with this to suit your own needs. Must be a multiple of 8. +*/ +#ifndef DR_FLAC_BUFFER_SIZE +#define DR_FLAC_BUFFER_SIZE 4096 +#endif + + +/* Architecture Detection */ +#if defined(_WIN64) || defined(_LP64) || defined(__LP64__) +#define DRFLAC_64BIT +#endif + +#if defined(__x86_64__) || (defined(_M_X64) && !defined(_M_ARM64EC)) + #define DRFLAC_X64 +#elif defined(__i386) || defined(_M_IX86) + #define DRFLAC_X86 +#elif defined(__arm__) || defined(_M_ARM) || defined(__arm64) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) + #define DRFLAC_ARM +#endif +/* End Architecture Detection */ + + +#ifdef DRFLAC_64BIT +typedef drflac_uint64 drflac_cache_t; +#else +typedef drflac_uint32 drflac_cache_t; +#endif + +/* The various metadata block types. */ +#define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO 0 +#define DRFLAC_METADATA_BLOCK_TYPE_PADDING 1 +#define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION 2 +#define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE 3 +#define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT 4 +#define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET 5 +#define DRFLAC_METADATA_BLOCK_TYPE_PICTURE 6 +#define DRFLAC_METADATA_BLOCK_TYPE_INVALID 127 + +/* The various picture types specified in the PICTURE block. */ +#define DRFLAC_PICTURE_TYPE_OTHER 0 +#define DRFLAC_PICTURE_TYPE_FILE_ICON 1 +#define DRFLAC_PICTURE_TYPE_OTHER_FILE_ICON 2 +#define DRFLAC_PICTURE_TYPE_COVER_FRONT 3 +#define DRFLAC_PICTURE_TYPE_COVER_BACK 4 +#define DRFLAC_PICTURE_TYPE_LEAFLET_PAGE 5 +#define DRFLAC_PICTURE_TYPE_MEDIA 6 +#define DRFLAC_PICTURE_TYPE_LEAD_ARTIST 7 +#define DRFLAC_PICTURE_TYPE_ARTIST 8 +#define DRFLAC_PICTURE_TYPE_CONDUCTOR 9 +#define DRFLAC_PICTURE_TYPE_BAND 10 +#define DRFLAC_PICTURE_TYPE_COMPOSER 11 +#define DRFLAC_PICTURE_TYPE_LYRICIST 12 +#define DRFLAC_PICTURE_TYPE_RECORDING_LOCATION 13 +#define DRFLAC_PICTURE_TYPE_DURING_RECORDING 14 +#define DRFLAC_PICTURE_TYPE_DURING_PERFORMANCE 15 +#define DRFLAC_PICTURE_TYPE_SCREEN_CAPTURE 16 +#define DRFLAC_PICTURE_TYPE_BRIGHT_COLORED_FISH 17 +#define DRFLAC_PICTURE_TYPE_ILLUSTRATION 18 +#define DRFLAC_PICTURE_TYPE_BAND_LOGOTYPE 19 +#define DRFLAC_PICTURE_TYPE_PUBLISHER_LOGOTYPE 20 + +typedef enum +{ + drflac_container_native, + drflac_container_ogg, + drflac_container_unknown +} drflac_container; + +typedef enum +{ + DRFLAC_SEEK_SET, + DRFLAC_SEEK_CUR, + DRFLAC_SEEK_END +} drflac_seek_origin; + +/* The order of members in this structure is important because we map this directly to the raw data within the SEEKTABLE metadata block. */ +typedef struct +{ + drflac_uint64 firstPCMFrame; + drflac_uint64 flacFrameOffset; /* The offset from the first byte of the header of the first frame. */ + drflac_uint16 pcmFrameCount; +} drflac_seekpoint; + +typedef struct +{ + drflac_uint16 minBlockSizeInPCMFrames; + drflac_uint16 maxBlockSizeInPCMFrames; + drflac_uint32 minFrameSizeInPCMFrames; + drflac_uint32 maxFrameSizeInPCMFrames; + drflac_uint32 sampleRate; + drflac_uint8 channels; + drflac_uint8 bitsPerSample; + drflac_uint64 totalPCMFrameCount; + drflac_uint8 md5[16]; +} drflac_streaminfo; + +typedef struct +{ + /* + The metadata type. Use this to know how to interpret the data below. Will be set to one of the + DRFLAC_METADATA_BLOCK_TYPE_* tokens. + */ + drflac_uint32 type; + + /* The size in bytes of the block and the buffer pointed to by pRawData if it's non-NULL. */ + drflac_uint32 rawDataSize; + + /* The offset in the stream of the raw data. */ + drflac_uint64 rawDataOffset; + + /* + A pointer to the raw data. This points to a temporary buffer so don't hold on to it. It's best to + not modify the contents of this buffer. Use the structures below for more meaningful and structured + information about the metadata. It's possible for this to be null. + */ + const void* pRawData; + + union + { + drflac_streaminfo streaminfo; + + struct + { + int unused; + } padding; + + struct + { + drflac_uint32 id; + const void* pData; + drflac_uint32 dataSize; + } application; + + struct + { + drflac_uint32 seekpointCount; + const drflac_seekpoint* pSeekpoints; + } seektable; + + struct + { + drflac_uint32 vendorLength; + const char* vendor; + drflac_uint32 commentCount; + const void* pComments; + } vorbis_comment; + + struct + { + char catalog[128]; + drflac_uint64 leadInSampleCount; + drflac_bool32 isCD; + drflac_uint8 trackCount; + const void* pTrackData; + } cuesheet; + + struct + { + drflac_uint32 type; + drflac_uint32 mimeLength; + const char* mime; + drflac_uint32 descriptionLength; + const char* description; + drflac_uint32 width; + drflac_uint32 height; + drflac_uint32 colorDepth; + drflac_uint32 indexColorCount; + drflac_uint32 pictureDataSize; + drflac_uint64 pictureDataOffset; /* Offset from the start of the stream. */ + const drflac_uint8* pPictureData; + } picture; + } data; +} drflac_metadata; + + +/* +Callback for when data needs to be read from the client. + + +Parameters +---------- +pUserData (in) + The user data that was passed to drflac_open() and family. + +pBufferOut (out) + The output buffer. + +bytesToRead (in) + The number of bytes to read. + + +Return Value +------------ +The number of bytes actually read. + + +Remarks +------- +A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until either the entire bytesToRead is filled or +you have reached the end of the stream. +*/ +typedef size_t (* drflac_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead); + +/* +Callback for when data needs to be seeked. + + +Parameters +---------- +pUserData (in) + The user data that was passed to drflac_open() and family. + +offset (in) + The number of bytes to move, relative to the origin. Will never be negative. + +origin (in) + The origin of the seek - the current position, the start of the stream, or the end of the stream. + + +Return Value +------------ +Whether or not the seek was successful. + + +Remarks +------- +Seeking relative to the start and the current position must always be supported. If seeking from the end of the stream is not supported, return DRFLAC_FALSE. + +When seeking to a PCM frame using drflac_seek_to_pcm_frame(), dr_flac may call this with an offset beyond the end of the FLAC stream. This needs to be detected +and handled by returning DRFLAC_FALSE. +*/ +typedef drflac_bool32 (* drflac_seek_proc)(void* pUserData, int offset, drflac_seek_origin origin); + +/* +Callback for when the current position in the stream needs to be retrieved. + + +Parameters +---------- +pUserData (in) + The user data that was passed to drflac_open() and family. + +pCursor (out) + A pointer to a variable to receive the current position in the stream. + + +Return Value +------------ +Whether or not the operation was successful. +*/ +typedef drflac_bool32 (* drflac_tell_proc)(void* pUserData, drflac_int64* pCursor); + +/* +Callback for when a metadata block is read. + + +Parameters +---------- +pUserData (in) + The user data that was passed to drflac_open() and family. + +pMetadata (in) + A pointer to a structure containing the data of the metadata block. + + +Remarks +------- +Use pMetadata->type to determine which metadata block is being handled and how to read the data. This +will be set to one of the DRFLAC_METADATA_BLOCK_TYPE_* tokens. +*/ +typedef void (* drflac_meta_proc)(void* pUserData, drflac_metadata* pMetadata); + + +/* Structure for internal use. Only used for decoders opened with drflac_open_memory. */ +typedef struct +{ + const drflac_uint8* data; + size_t dataSize; + size_t currentReadPos; +} drflac__memory_stream; + +/* Structure for internal use. Used for bit streaming. */ +typedef struct +{ + /* The function to call when more data needs to be read. */ + drflac_read_proc onRead; + + /* The function to call when the current read position needs to be moved. */ + drflac_seek_proc onSeek; + + /* The function to call when the current read position needs to be retrieved. */ + drflac_tell_proc onTell; + + /* The user data to pass around to onRead and onSeek. */ + void* pUserData; + + + /* + The number of unaligned bytes in the L2 cache. This will always be 0 until the end of the stream is hit. At the end of the + stream there will be a number of bytes that don't cleanly fit in an L1 cache line, so we use this variable to know whether + or not the bistreamer needs to run on a slower path to read those last bytes. This will never be more than sizeof(drflac_cache_t). + */ + size_t unalignedByteCount; + + /* The content of the unaligned bytes. */ + drflac_cache_t unalignedCache; + + /* The index of the next valid cache line in the "L2" cache. */ + drflac_uint32 nextL2Line; + + /* The number of bits that have been consumed by the cache. This is used to determine how many valid bits are remaining. */ + drflac_uint32 consumedBits; + + /* + The cached data which was most recently read from the client. There are two levels of cache. Data flows as such: + Client -> L2 -> L1. The L2 -> L1 movement is aligned and runs on a fast path in just a few instructions. + */ + drflac_cache_t cacheL2[DR_FLAC_BUFFER_SIZE/sizeof(drflac_cache_t)]; + drflac_cache_t cache; + + /* + CRC-16. This is updated whenever bits are read from the bit stream. Manually set this to 0 to reset the CRC. For FLAC, this + is reset to 0 at the beginning of each frame. + */ + drflac_uint16 crc16; + drflac_cache_t crc16Cache; /* A cache for optimizing CRC calculations. This is filled when when the L1 cache is reloaded. */ + drflac_uint32 crc16CacheIgnoredBytes; /* The number of bytes to ignore when updating the CRC-16 from the CRC-16 cache. */ +} drflac_bs; + +typedef struct +{ + /* The type of the subframe: SUBFRAME_CONSTANT, SUBFRAME_VERBATIM, SUBFRAME_FIXED or SUBFRAME_LPC. */ + drflac_uint8 subframeType; + + /* The number of wasted bits per sample as specified by the sub-frame header. */ + drflac_uint8 wastedBitsPerSample; + + /* The order to use for the prediction stage for SUBFRAME_FIXED and SUBFRAME_LPC. */ + drflac_uint8 lpcOrder; + + /* A pointer to the buffer containing the decoded samples in the subframe. This pointer is an offset from drflac::pExtraData. */ + drflac_int32* pSamplesS32; +} drflac_subframe; + +typedef struct +{ + /* + If the stream uses variable block sizes, this will be set to the index of the first PCM frame. If fixed block sizes are used, this will + always be set to 0. This is 64-bit because the decoded PCM frame number will be 36 bits. + */ + drflac_uint64 pcmFrameNumber; + + /* + If the stream uses fixed block sizes, this will be set to the frame number. If variable block sizes are used, this will always be 0. This + is 32-bit because in fixed block sizes, the maximum frame number will be 31 bits. + */ + drflac_uint32 flacFrameNumber; + + /* The sample rate of this frame. */ + drflac_uint32 sampleRate; + + /* The number of PCM frames in each sub-frame within this frame. */ + drflac_uint16 blockSizeInPCMFrames; + + /* + The channel assignment of this frame. This is not always set to the channel count. If interchannel decorrelation is being used this + will be set to DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE. + */ + drflac_uint8 channelAssignment; + + /* The number of bits per sample within this frame. */ + drflac_uint8 bitsPerSample; + + /* The frame's CRC. */ + drflac_uint8 crc8; +} drflac_frame_header; + +typedef struct +{ + /* The header. */ + drflac_frame_header header; + + /* + The number of PCM frames left to be read in this FLAC frame. This is initially set to the block size. As PCM frames are read, + this will be decremented. When it reaches 0, the decoder will see this frame as fully consumed and load the next frame. + */ + drflac_uint32 pcmFramesRemaining; + + /* The list of sub-frames within the frame. There is one sub-frame for each channel, and there's a maximum of 8 channels. */ + drflac_subframe subframes[8]; +} drflac_frame; + +typedef struct +{ + /* The function to call when a metadata block is read. */ + drflac_meta_proc onMeta; + + /* The user data posted to the metadata callback function. */ + void* pUserDataMD; + + /* Memory allocation callbacks. */ + drflac_allocation_callbacks allocationCallbacks; + + + /* The sample rate. Will be set to something like 44100. */ + drflac_uint32 sampleRate; + + /* + The number of channels. This will be set to 1 for monaural streams, 2 for stereo, etc. Maximum 8. This is set based on the + value specified in the STREAMINFO block. + */ + drflac_uint8 channels; + + /* The bits per sample. Will be set to something like 16, 24, etc. */ + drflac_uint8 bitsPerSample; + + /* The maximum block size, in samples. This number represents the number of samples in each channel (not combined). */ + drflac_uint16 maxBlockSizeInPCMFrames; + + /* + The total number of PCM Frames making up the stream. Can be 0 in which case it's still a valid stream, but just means + the total PCM frame count is unknown. Likely the case with streams like internet radio. + */ + drflac_uint64 totalPCMFrameCount; + + + /* The container type. This is set based on whether or not the decoder was opened from a native or Ogg stream. */ + drflac_container container; + + /* The number of seekpoints in the seektable. */ + drflac_uint32 seekpointCount; + + + /* Information about the frame the decoder is currently sitting on. */ + drflac_frame currentFLACFrame; + + + /* The index of the PCM frame the decoder is currently sitting on. This is only used for seeking. */ + drflac_uint64 currentPCMFrame; + + /* The position of the first FLAC frame in the stream. This is only ever used for seeking. */ + drflac_uint64 firstFLACFramePosInBytes; + + + /* A hack to avoid a malloc() when opening a decoder with drflac_open_memory(). */ + drflac__memory_stream memoryStream; + + + /* A pointer to the decoded sample data. This is an offset of pExtraData. */ + drflac_int32* pDecodedSamples; + + /* A pointer to the seek table. This is an offset of pExtraData, or NULL if there is no seek table. */ + drflac_seekpoint* pSeekpoints; + + /* Internal use only. Only used with Ogg containers. Points to a drflac_oggbs object. This is an offset of pExtraData. */ + void* _oggbs; + + /* Internal use only. Used for profiling and testing different seeking modes. */ + drflac_bool32 _noSeekTableSeek : 1; + drflac_bool32 _noBinarySearchSeek : 1; + drflac_bool32 _noBruteForceSeek : 1; + + /* The bit streamer. The raw FLAC data is fed through this object. */ + drflac_bs bs; + + /* Variable length extra data. We attach this to the end of the object so we can avoid unnecessary mallocs. */ + drflac_uint8 pExtraData[1]; +} drflac; + + +/* +Opens a FLAC decoder. + + +Parameters +---------- +onRead (in) + The function to call when data needs to be read from the client. + +onSeek (in) + The function to call when the read position of the client data needs to move. + +onTell (in) + The function to call when the read position of the client needs to be queried. + +pUserData (in, optional) + A pointer to application defined data that will be passed to onRead and onSeek. + +pAllocationCallbacks (in, optional) + A pointer to application defined callbacks for managing memory allocations. + + +Return Value +------------ +Returns a pointer to an object representing the decoder. + + +Remarks +------- +Close the decoder with `drflac_close()`. + +`pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`. + +This function will automatically detect whether or not you are attempting to open a native or Ogg encapsulated FLAC, both of which should work seamlessly +without any manual intervention. Ogg encapsulation also works with multiplexed streams which basically means it can play FLAC encoded audio tracks in videos. + +This is the lowest level function for opening a FLAC stream. You can also use `drflac_open_file()` and `drflac_open_memory()` to open the stream from a file or +from a block of memory respectively. + +The STREAMINFO block must be present for this to succeed. Use `drflac_open_relaxed()` to open a FLAC stream where the header may not be present. + +Use `drflac_open_with_metadata()` if you need access to metadata. + + +Seek Also +--------- +drflac_open_file() +drflac_open_memory() +drflac_open_with_metadata() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Opens a FLAC stream with relaxed validation of the header block. + + +Parameters +---------- +onRead (in) + The function to call when data needs to be read from the client. + +onSeek (in) + The function to call when the read position of the client data needs to move. + +onTell (in) + The function to call when the read position of the client needs to be queried. + +container (in) + Whether or not the FLAC stream is encapsulated using standard FLAC encapsulation or Ogg encapsulation. + +pUserData (in, optional) + A pointer to application defined data that will be passed to onRead and onSeek. + +pAllocationCallbacks (in, optional) + A pointer to application defined callbacks for managing memory allocations. + + +Return Value +------------ +A pointer to an object representing the decoder. + + +Remarks +------- +The same as drflac_open(), except attempts to open the stream even when a header block is not present. + +Because the header is not necessarily available, the caller must explicitly define the container (Native or Ogg). Do not set this to `drflac_container_unknown` +as that is for internal use only. + +Opening in relaxed mode will continue reading data from onRead until it finds a valid frame. If a frame is never found it will continue forever. To abort, +force your `onRead` callback to return 0, which dr_flac will use as an indicator that the end of the stream was found. + +Use `drflac_open_with_metadata_relaxed()` if you need access to metadata. +*/ +DRFLAC_API drflac* drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Opens a FLAC decoder and notifies the caller of the metadata chunks (album art, etc.). + + +Parameters +---------- +onRead (in) + The function to call when data needs to be read from the client. + +onSeek (in) + The function to call when the read position of the client data needs to move. + +onTell (in) + The function to call when the read position of the client needs to be queried. + +onMeta (in) + The function to call for every metadata block. + +pUserData (in, optional) + A pointer to application defined data that will be passed to onRead, onSeek and onMeta. + +pAllocationCallbacks (in, optional) + A pointer to application defined callbacks for managing memory allocations. + + +Return Value +------------ +A pointer to an object representing the decoder. + + +Remarks +------- +Close the decoder with `drflac_close()`. + +`pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`. + +This is slower than `drflac_open()`, so avoid this one if you don't need metadata. Internally, this will allocate and free memory on the heap for every +metadata block except for STREAMINFO and PADDING blocks. + +The caller is notified of the metadata via the `onMeta` callback. All metadata blocks will be handled before the function returns. This callback takes a +pointer to a `drflac_metadata` object which is a union containing the data of all relevant metadata blocks. Use the `type` member to discriminate against +the different metadata types. + +The STREAMINFO block must be present for this to succeed. Use `drflac_open_with_metadata_relaxed()` to open a FLAC stream where the header may not be present. + +Note that this will behave inconsistently with `drflac_open()` if the stream is an Ogg encapsulated stream and a metadata block is corrupted. This is due to +the way the Ogg stream recovers from corrupted pages. When `drflac_open_with_metadata()` is being used, the open routine will try to read the contents of the +metadata block, whereas `drflac_open()` will simply seek past it (for the sake of efficiency). This inconsistency can result in different samples being +returned depending on whether or not the stream is being opened with metadata. + + +Seek Also +--------- +drflac_open_file_with_metadata() +drflac_open_memory_with_metadata() +drflac_open() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +The same as drflac_open_with_metadata(), except attempts to open the stream even when a header block is not present. + +See Also +-------- +drflac_open_with_metadata() +drflac_open_relaxed() +*/ +DRFLAC_API drflac* drflac_open_with_metadata_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Closes the given FLAC decoder. + + +Parameters +---------- +pFlac (in) + The decoder to close. + + +Remarks +------- +This will destroy the decoder object. + + +See Also +-------- +drflac_open() +drflac_open_with_metadata() +drflac_open_file() +drflac_open_file_w() +drflac_open_file_with_metadata() +drflac_open_file_with_metadata_w() +drflac_open_memory() +drflac_open_memory_with_metadata() +*/ +DRFLAC_API void drflac_close(drflac* pFlac); + + +/* +Reads sample data from the given FLAC decoder, output as interleaved signed 32-bit PCM. + + +Parameters +---------- +pFlac (in) + The decoder. + +framesToRead (in) + The number of PCM frames to read. + +pBufferOut (out, optional) + A pointer to the buffer that will receive the decoded samples. + + +Return Value +------------ +Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end. + + +Remarks +------- +pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked. +*/ +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s32(drflac* pFlac, drflac_uint64 framesToRead, drflac_int32* pBufferOut); + + +/* +Reads sample data from the given FLAC decoder, output as interleaved signed 16-bit PCM. + + +Parameters +---------- +pFlac (in) + The decoder. + +framesToRead (in) + The number of PCM frames to read. + +pBufferOut (out, optional) + A pointer to the buffer that will receive the decoded samples. + + +Return Value +------------ +Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end. + + +Remarks +------- +pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked. + +Note that this is lossy for streams where the bits per sample is larger than 16. +*/ +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s16(drflac* pFlac, drflac_uint64 framesToRead, drflac_int16* pBufferOut); + +/* +Reads sample data from the given FLAC decoder, output as interleaved 32-bit floating point PCM. + + +Parameters +---------- +pFlac (in) + The decoder. + +framesToRead (in) + The number of PCM frames to read. + +pBufferOut (out, optional) + A pointer to the buffer that will receive the decoded samples. + + +Return Value +------------ +Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end. + + +Remarks +------- +pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked. + +Note that this should be considered lossy due to the nature of floating point numbers not being able to exactly represent every possible number. +*/ +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_f32(drflac* pFlac, drflac_uint64 framesToRead, float* pBufferOut); + +/* +Seeks to the PCM frame at the given index. + + +Parameters +---------- +pFlac (in) + The decoder. + +pcmFrameIndex (in) + The index of the PCM frame to seek to. See notes below. + + +Return Value +------------- +`DRFLAC_TRUE` if successful; `DRFLAC_FALSE` otherwise. +*/ +DRFLAC_API drflac_bool32 drflac_seek_to_pcm_frame(drflac* pFlac, drflac_uint64 pcmFrameIndex); + + + +#ifndef DR_FLAC_NO_STDIO +/* +Opens a FLAC decoder from the file at the given path. + + +Parameters +---------- +pFileName (in) + The path of the file to open, either absolute or relative to the current directory. + +pAllocationCallbacks (in, optional) + A pointer to application defined callbacks for managing memory allocations. + + +Return Value +------------ +A pointer to an object representing the decoder. + + +Remarks +------- +Close the decoder with drflac_close(). + + +Remarks +------- +This will hold a handle to the file until the decoder is closed with drflac_close(). Some platforms will restrict the number of files a process can have open +at any given time, so keep this mind if you have many decoders open at the same time. + + +See Also +-------- +drflac_open_file_with_metadata() +drflac_open() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open_file(const char* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks); +DRFLAC_API drflac* drflac_open_file_w(const wchar_t* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Opens a FLAC decoder from the file at the given path and notifies the caller of the metadata chunks (album art, etc.) + + +Parameters +---------- +pFileName (in) + The path of the file to open, either absolute or relative to the current directory. + +pAllocationCallbacks (in, optional) + A pointer to application defined callbacks for managing memory allocations. + +onMeta (in) + The callback to fire for each metadata block. + +pUserData (in) + A pointer to the user data to pass to the metadata callback. + +pAllocationCallbacks (in) + A pointer to application defined callbacks for managing memory allocations. + + +Remarks +------- +Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled. + + +See Also +-------- +drflac_open_with_metadata() +drflac_open() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open_file_with_metadata(const char* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); +DRFLAC_API drflac* drflac_open_file_with_metadata_w(const wchar_t* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); +#endif + +/* +Opens a FLAC decoder from a pre-allocated block of memory + + +Parameters +---------- +pData (in) + A pointer to the raw encoded FLAC data. + +dataSize (in) + The size in bytes of `data`. + +pAllocationCallbacks (in) + A pointer to application defined callbacks for managing memory allocations. + + +Return Value +------------ +A pointer to an object representing the decoder. + + +Remarks +------- +This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for the lifetime of the decoder. + + +See Also +-------- +drflac_open() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open_memory(const void* pData, size_t dataSize, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Opens a FLAC decoder from a pre-allocated block of memory and notifies the caller of the metadata chunks (album art, etc.) + + +Parameters +---------- +pData (in) + A pointer to the raw encoded FLAC data. + +dataSize (in) + The size in bytes of `data`. + +onMeta (in) + The callback to fire for each metadata block. + +pUserData (in) + A pointer to the user data to pass to the metadata callback. + +pAllocationCallbacks (in) + A pointer to application defined callbacks for managing memory allocations. + + +Remarks +------- +Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled. + + +See Also +------- +drflac_open_with_metadata() +drflac_open() +drflac_close() +*/ +DRFLAC_API drflac* drflac_open_memory_with_metadata(const void* pData, size_t dataSize, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks); + + + +/* High Level APIs */ + +/* +Opens a FLAC stream from the given callbacks and fully decodes it in a single operation. The return value is a +pointer to the sample data as interleaved signed 32-bit PCM. The returned data must be freed with drflac_free(). + +You can pass in custom memory allocation callbacks via the pAllocationCallbacks parameter. This can be NULL in which +case it will use DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE. + +Sometimes a FLAC file won't keep track of the total sample count. In this situation the function will continuously +read samples into a dynamically sized buffer on the heap until no samples are left. + +Do not call this function on a broadcast type of stream (like internet radio streams and whatnot). +*/ +DRFLAC_API drflac_int32* drflac_open_and_read_pcm_frames_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */ +DRFLAC_API drflac_int16* drflac_open_and_read_pcm_frames_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */ +DRFLAC_API float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +#ifndef DR_FLAC_NO_STDIO +/* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a file. */ +DRFLAC_API drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */ +DRFLAC_API drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */ +DRFLAC_API float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); +#endif + +/* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a block of memory. */ +DRFLAC_API drflac_int32* drflac_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */ +DRFLAC_API drflac_int16* drflac_open_memory_and_read_pcm_frames_s16(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */ +DRFLAC_API float* drflac_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks); + +/* +Frees memory that was allocated internally by dr_flac. + +Set pAllocationCallbacks to the same object that was passed to drflac_open_*_and_read_pcm_frames_*(). If you originally passed in NULL, pass in NULL for this. +*/ +DRFLAC_API void drflac_free(void* p, const drflac_allocation_callbacks* pAllocationCallbacks); + + +/* Structure representing an iterator for vorbis comments in a VORBIS_COMMENT metadata block. */ +typedef struct +{ + drflac_uint32 countRemaining; + const char* pRunningData; +} drflac_vorbis_comment_iterator; + +/* +Initializes a vorbis comment iterator. This can be used for iterating over the vorbis comments in a VORBIS_COMMENT +metadata block. +*/ +DRFLAC_API void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator* pIter, drflac_uint32 commentCount, const void* pComments); + +/* +Goes to the next vorbis comment in the given iterator. If null is returned it means there are no more comments. The +returned string is NOT null terminated. +*/ +DRFLAC_API const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator* pIter, drflac_uint32* pCommentLengthOut); + + +/* Structure representing an iterator for cuesheet tracks in a CUESHEET metadata block. */ +typedef struct +{ + drflac_uint32 countRemaining; + const char* pRunningData; +} drflac_cuesheet_track_iterator; + +/* The order of members here is important because we map this directly to the raw data within the CUESHEET metadata block. */ +typedef struct +{ + drflac_uint64 offset; + drflac_uint8 index; + drflac_uint8 reserved[3]; +} drflac_cuesheet_track_index; + +typedef struct +{ + drflac_uint64 offset; + drflac_uint8 trackNumber; + char ISRC[12]; + drflac_bool8 isAudio; + drflac_bool8 preEmphasis; + drflac_uint8 indexCount; + const drflac_cuesheet_track_index* pIndexPoints; +} drflac_cuesheet_track; + +/* +Initializes a cuesheet track iterator. This can be used for iterating over the cuesheet tracks in a CUESHEET metadata +block. +*/ +DRFLAC_API void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator* pIter, drflac_uint32 trackCount, const void* pTrackData); + +/* Goes to the next cuesheet track in the given iterator. If DRFLAC_FALSE is returned it means there are no more comments. */ +DRFLAC_API drflac_bool32 drflac_next_cuesheet_track(drflac_cuesheet_track_iterator* pIter, drflac_cuesheet_track* pCuesheetTrack); + + +#ifdef __cplusplus +} +#endif +#endif /* dr_flac_h */ + + +/************************************************************************************************************************************************************ + ************************************************************************************************************************************************************ + + IMPLEMENTATION + + ************************************************************************************************************************************************************ + ************************************************************************************************************************************************************/ +#if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION) +#ifndef dr_flac_c +#define dr_flac_c + +/* Disable some annoying warnings. */ +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic push + #if __GNUC__ >= 7 + #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" + #endif +#endif + +#ifdef __linux__ + #ifndef _BSD_SOURCE + #define _BSD_SOURCE + #endif + #ifndef _DEFAULT_SOURCE + #define _DEFAULT_SOURCE + #endif + #ifndef __USE_BSD + #define __USE_BSD + #endif + #include +#endif + +#include +#include + +/* Inline */ +#ifdef _MSC_VER + #define DRFLAC_INLINE __forceinline +#elif defined(__GNUC__) + /* + I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when + the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some + case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the + command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue + I am using "__inline__" only when we're compiling in strict ANSI mode. + */ + #if defined(__STRICT_ANSI__) + #define DRFLAC_GNUC_INLINE_HINT __inline__ + #else + #define DRFLAC_GNUC_INLINE_HINT inline + #endif + + #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)) || defined(__clang__) + #define DRFLAC_INLINE DRFLAC_GNUC_INLINE_HINT __attribute__((always_inline)) + #else + #define DRFLAC_INLINE DRFLAC_GNUC_INLINE_HINT + #endif +#elif defined(__WATCOMC__) + #define DRFLAC_INLINE __inline +#else + #define DRFLAC_INLINE +#endif +/* End Inline */ + +/* +Intrinsics Support + +There's a bug in GCC 4.2.x which results in an incorrect compilation error when using _mm_slli_epi32() where it complains with + + "error: shift must be an immediate" + +Unfortuantely dr_flac depends on this for a few things so we're just going to disable SSE on GCC 4.2 and below. +*/ +#if !defined(DR_FLAC_NO_SIMD) + #if defined(DRFLAC_X64) || defined(DRFLAC_X86) + #if defined(_MSC_VER) && !defined(__clang__) + /* MSVC. */ + #if _MSC_VER >= 1400 && !defined(DRFLAC_NO_SSE2) /* 2005 */ + #define DRFLAC_SUPPORT_SSE2 + #endif + #if _MSC_VER >= 1600 && !defined(DRFLAC_NO_SSE41) /* 2010 */ + #define DRFLAC_SUPPORT_SSE41 + #endif + #elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) + /* Assume GNUC-style. */ + #if defined(__SSE2__) && !defined(DRFLAC_NO_SSE2) + #define DRFLAC_SUPPORT_SSE2 + #endif + #if defined(__SSE4_1__) && !defined(DRFLAC_NO_SSE41) + #define DRFLAC_SUPPORT_SSE41 + #endif + #endif + + /* If at this point we still haven't determined compiler support for the intrinsics just fall back to __has_include. */ + #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include) + #if !defined(DRFLAC_SUPPORT_SSE2) && !defined(DRFLAC_NO_SSE2) && __has_include() + #define DRFLAC_SUPPORT_SSE2 + #endif + #if !defined(DRFLAC_SUPPORT_SSE41) && !defined(DRFLAC_NO_SSE41) && __has_include() + #define DRFLAC_SUPPORT_SSE41 + #endif + #endif + + #if defined(DRFLAC_SUPPORT_SSE41) + #include + #elif defined(DRFLAC_SUPPORT_SSE2) + #include + #endif + #endif + + #if defined(DRFLAC_ARM) + #if !defined(DRFLAC_NO_NEON) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64)) + #define DRFLAC_SUPPORT_NEON + #include + #endif + #endif +#endif + +/* Compile-time CPU feature support. */ +#if !defined(DR_FLAC_NO_SIMD) && (defined(DRFLAC_X86) || defined(DRFLAC_X64)) + #if defined(_MSC_VER) && !defined(__clang__) + #if _MSC_VER >= 1400 + #include + static void drflac__cpuid(int info[4], int fid) + { + __cpuid(info, fid); + } + #else + #define DRFLAC_NO_CPUID + #endif + #else + #if defined(__GNUC__) || defined(__clang__) + static void drflac__cpuid(int info[4], int fid) + { + /* + It looks like the -fPIC option uses the ebx register which GCC complains about. We can work around this by just using a different register, the + specific register of which I'm letting the compiler decide on. The "k" prefix is used to specify a 32-bit register. The {...} syntax is for + supporting different assembly dialects. + + What's basically happening is that we're saving and restoring the ebx register manually. + */ + #if defined(DRFLAC_X86) && defined(__PIC__) + __asm__ __volatile__ ( + "xchg{l} {%%}ebx, %k1;" + "cpuid;" + "xchg{l} {%%}ebx, %k1;" + : "=a"(info[0]), "=&r"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0) + ); + #else + __asm__ __volatile__ ( + "cpuid" : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0) + ); + #endif + } + #else + #define DRFLAC_NO_CPUID + #endif + #endif +#else + #define DRFLAC_NO_CPUID +#endif + +static DRFLAC_INLINE drflac_bool32 drflac_has_sse2(void) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE2) + #if defined(DRFLAC_X64) + return DRFLAC_TRUE; /* 64-bit targets always support SSE2. */ + #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__) + return DRFLAC_TRUE; /* If the compiler is allowed to freely generate SSE2 code we can assume support. */ + #else + #if defined(DRFLAC_NO_CPUID) + return DRFLAC_FALSE; + #else + int info[4]; + drflac__cpuid(info, 1); + return (info[3] & (1 << 26)) != 0; + #endif + #endif + #else + return DRFLAC_FALSE; /* SSE2 is only supported on x86 and x64 architectures. */ + #endif +#else + return DRFLAC_FALSE; /* No compiler support. */ +#endif +} + +static DRFLAC_INLINE drflac_bool32 drflac_has_sse41(void) +{ +#if defined(DRFLAC_SUPPORT_SSE41) + #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE41) + #if defined(__SSE4_1__) || defined(__AVX__) + return DRFLAC_TRUE; /* If the compiler is allowed to freely generate SSE41 code we can assume support. */ + #else + #if defined(DRFLAC_NO_CPUID) + return DRFLAC_FALSE; + #else + int info[4]; + drflac__cpuid(info, 1); + return (info[2] & (1 << 19)) != 0; + #endif + #endif + #else + return DRFLAC_FALSE; /* SSE41 is only supported on x86 and x64 architectures. */ + #endif +#else + return DRFLAC_FALSE; /* No compiler support. */ +#endif +} + + +#if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DRFLAC_X86) || defined(DRFLAC_X64)) && !defined(__clang__) + #define DRFLAC_HAS_LZCNT_INTRINSIC +#elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))) + #define DRFLAC_HAS_LZCNT_INTRINSIC +#elif defined(__clang__) + #if defined(__has_builtin) + #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl) + #define DRFLAC_HAS_LZCNT_INTRINSIC + #endif + #endif +#endif + +#if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(__clang__) + #define DRFLAC_HAS_BYTESWAP16_INTRINSIC + #define DRFLAC_HAS_BYTESWAP32_INTRINSIC + #define DRFLAC_HAS_BYTESWAP64_INTRINSIC +#elif defined(__clang__) + #if defined(__has_builtin) + #if __has_builtin(__builtin_bswap16) + #define DRFLAC_HAS_BYTESWAP16_INTRINSIC + #endif + #if __has_builtin(__builtin_bswap32) + #define DRFLAC_HAS_BYTESWAP32_INTRINSIC + #endif + #if __has_builtin(__builtin_bswap64) + #define DRFLAC_HAS_BYTESWAP64_INTRINSIC + #endif + #endif +#elif defined(__GNUC__) + #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) + #define DRFLAC_HAS_BYTESWAP32_INTRINSIC + #define DRFLAC_HAS_BYTESWAP64_INTRINSIC + #endif + #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) + #define DRFLAC_HAS_BYTESWAP16_INTRINSIC + #endif +#elif defined(__WATCOMC__) && defined(__386__) + #define DRFLAC_HAS_BYTESWAP16_INTRINSIC + #define DRFLAC_HAS_BYTESWAP32_INTRINSIC + #define DRFLAC_HAS_BYTESWAP64_INTRINSIC + extern __inline drflac_uint16 _watcom_bswap16(drflac_uint16); + extern __inline drflac_uint32 _watcom_bswap32(drflac_uint32); + extern __inline drflac_uint64 _watcom_bswap64(drflac_uint64); +#pragma aux _watcom_bswap16 = \ + "xchg al, ah" \ + parm [ax] \ + value [ax] \ + modify nomemory; +#pragma aux _watcom_bswap32 = \ + "bswap eax" \ + parm [eax] \ + value [eax] \ + modify nomemory; +#pragma aux _watcom_bswap64 = \ + "bswap eax" \ + "bswap edx" \ + "xchg eax,edx" \ + parm [eax edx] \ + value [eax edx] \ + modify nomemory; +#endif + + +/* Standard library stuff. */ +#ifndef DRFLAC_ASSERT +#include +#define DRFLAC_ASSERT(expression) assert(expression) +#endif +#ifndef DRFLAC_MALLOC +#define DRFLAC_MALLOC(sz) malloc((sz)) +#endif +#ifndef DRFLAC_REALLOC +#define DRFLAC_REALLOC(p, sz) realloc((p), (sz)) +#endif +#ifndef DRFLAC_FREE +#define DRFLAC_FREE(p) free((p)) +#endif +#ifndef DRFLAC_COPY_MEMORY +#define DRFLAC_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz)) +#endif +#ifndef DRFLAC_ZERO_MEMORY +#define DRFLAC_ZERO_MEMORY(p, sz) memset((p), 0, (sz)) +#endif +#ifndef DRFLAC_ZERO_OBJECT +#define DRFLAC_ZERO_OBJECT(p) DRFLAC_ZERO_MEMORY((p), sizeof(*(p))) +#endif + +#define DRFLAC_MIN(a, b) (((a) < (b)) ? (a) : (b)) + +#define DRFLAC_MAX_SIMD_VECTOR_SIZE 64 /* 64 for AVX-512 in the future. */ + +/* Result Codes */ +typedef drflac_int32 drflac_result; +#define DRFLAC_SUCCESS 0 +#define DRFLAC_ERROR -1 /* A generic error. */ +#define DRFLAC_INVALID_ARGS -2 +#define DRFLAC_INVALID_OPERATION -3 +#define DRFLAC_OUT_OF_MEMORY -4 +#define DRFLAC_OUT_OF_RANGE -5 +#define DRFLAC_ACCESS_DENIED -6 +#define DRFLAC_DOES_NOT_EXIST -7 +#define DRFLAC_ALREADY_EXISTS -8 +#define DRFLAC_TOO_MANY_OPEN_FILES -9 +#define DRFLAC_INVALID_FILE -10 +#define DRFLAC_TOO_BIG -11 +#define DRFLAC_PATH_TOO_LONG -12 +#define DRFLAC_NAME_TOO_LONG -13 +#define DRFLAC_NOT_DIRECTORY -14 +#define DRFLAC_IS_DIRECTORY -15 +#define DRFLAC_DIRECTORY_NOT_EMPTY -16 +#define DRFLAC_END_OF_FILE -17 +#define DRFLAC_NO_SPACE -18 +#define DRFLAC_BUSY -19 +#define DRFLAC_IO_ERROR -20 +#define DRFLAC_INTERRUPT -21 +#define DRFLAC_UNAVAILABLE -22 +#define DRFLAC_ALREADY_IN_USE -23 +#define DRFLAC_BAD_ADDRESS -24 +#define DRFLAC_BAD_SEEK -25 +#define DRFLAC_BAD_PIPE -26 +#define DRFLAC_DEADLOCK -27 +#define DRFLAC_TOO_MANY_LINKS -28 +#define DRFLAC_NOT_IMPLEMENTED -29 +#define DRFLAC_NO_MESSAGE -30 +#define DRFLAC_BAD_MESSAGE -31 +#define DRFLAC_NO_DATA_AVAILABLE -32 +#define DRFLAC_INVALID_DATA -33 +#define DRFLAC_TIMEOUT -34 +#define DRFLAC_NO_NETWORK -35 +#define DRFLAC_NOT_UNIQUE -36 +#define DRFLAC_NOT_SOCKET -37 +#define DRFLAC_NO_ADDRESS -38 +#define DRFLAC_BAD_PROTOCOL -39 +#define DRFLAC_PROTOCOL_UNAVAILABLE -40 +#define DRFLAC_PROTOCOL_NOT_SUPPORTED -41 +#define DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED -42 +#define DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED -43 +#define DRFLAC_SOCKET_NOT_SUPPORTED -44 +#define DRFLAC_CONNECTION_RESET -45 +#define DRFLAC_ALREADY_CONNECTED -46 +#define DRFLAC_NOT_CONNECTED -47 +#define DRFLAC_CONNECTION_REFUSED -48 +#define DRFLAC_NO_HOST -49 +#define DRFLAC_IN_PROGRESS -50 +#define DRFLAC_CANCELLED -51 +#define DRFLAC_MEMORY_ALREADY_MAPPED -52 +#define DRFLAC_AT_END -53 + +#define DRFLAC_CRC_MISMATCH -100 +/* End Result Codes */ + + +#define DRFLAC_SUBFRAME_CONSTANT 0 +#define DRFLAC_SUBFRAME_VERBATIM 1 +#define DRFLAC_SUBFRAME_FIXED 8 +#define DRFLAC_SUBFRAME_LPC 32 +#define DRFLAC_SUBFRAME_RESERVED 255 + +#define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE 0 +#define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1 + +#define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT 0 +#define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE 8 +#define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE 9 +#define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE 10 + +#define DRFLAC_SEEKPOINT_SIZE_IN_BYTES 18 +#define DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES 36 +#define DRFLAC_CUESHEET_TRACK_INDEX_SIZE_IN_BYTES 12 + +#define drflac_align(x, a) ((((x) + (a) - 1) / (a)) * (a)) + + +DRFLAC_API void drflac_version(drflac_uint32* pMajor, drflac_uint32* pMinor, drflac_uint32* pRevision) +{ + if (pMajor) { + *pMajor = DRFLAC_VERSION_MAJOR; + } + + if (pMinor) { + *pMinor = DRFLAC_VERSION_MINOR; + } + + if (pRevision) { + *pRevision = DRFLAC_VERSION_REVISION; + } +} + +DRFLAC_API const char* drflac_version_string(void) +{ + return DRFLAC_VERSION_STRING; +} + + +/* CPU caps. */ +#if defined(__has_feature) + #if __has_feature(thread_sanitizer) + #define DRFLAC_NO_THREAD_SANITIZE __attribute__((no_sanitize("thread"))) + #else + #define DRFLAC_NO_THREAD_SANITIZE + #endif +#else + #define DRFLAC_NO_THREAD_SANITIZE +#endif + +#if defined(DRFLAC_HAS_LZCNT_INTRINSIC) +static drflac_bool32 drflac__gIsLZCNTSupported = DRFLAC_FALSE; +#endif + +#ifndef DRFLAC_NO_CPUID +static drflac_bool32 drflac__gIsSSE2Supported = DRFLAC_FALSE; +static drflac_bool32 drflac__gIsSSE41Supported = DRFLAC_FALSE; + +/* +I've had a bug report that Clang's ThreadSanitizer presents a warning in this function. Having reviewed this, this does +actually make sense. However, since CPU caps should never differ for a running process, I don't think the trade off of +complicating internal API's by passing around CPU caps versus just disabling the warnings is worthwhile. I'm therefore +just going to disable these warnings. This is disabled via the DRFLAC_NO_THREAD_SANITIZE attribute. +*/ +DRFLAC_NO_THREAD_SANITIZE static void drflac__init_cpu_caps(void) +{ + static drflac_bool32 isCPUCapsInitialized = DRFLAC_FALSE; + + if (!isCPUCapsInitialized) { + /* LZCNT */ +#if defined(DRFLAC_HAS_LZCNT_INTRINSIC) + int info[4] = {0}; + drflac__cpuid(info, 0x80000001); + drflac__gIsLZCNTSupported = (info[2] & (1 << 5)) != 0; +#endif + + /* SSE2 */ + drflac__gIsSSE2Supported = drflac_has_sse2(); + + /* SSE4.1 */ + drflac__gIsSSE41Supported = drflac_has_sse41(); + + /* Initialized. */ + isCPUCapsInitialized = DRFLAC_TRUE; + } +} +#else +static drflac_bool32 drflac__gIsNEONSupported = DRFLAC_FALSE; + +static DRFLAC_INLINE drflac_bool32 drflac__has_neon(void) +{ +#if defined(DRFLAC_SUPPORT_NEON) + #if defined(DRFLAC_ARM) && !defined(DRFLAC_NO_NEON) + #if (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64)) + return DRFLAC_TRUE; /* If the compiler is allowed to freely generate NEON code we can assume support. */ + #else + /* TODO: Runtime check. */ + return DRFLAC_FALSE; + #endif + #else + return DRFLAC_FALSE; /* NEON is only supported on ARM architectures. */ + #endif +#else + return DRFLAC_FALSE; /* No compiler support. */ +#endif +} + +DRFLAC_NO_THREAD_SANITIZE static void drflac__init_cpu_caps(void) +{ + drflac__gIsNEONSupported = drflac__has_neon(); + +#if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) + drflac__gIsLZCNTSupported = DRFLAC_TRUE; +#endif +} +#endif + + +/* Endian Management */ +static DRFLAC_INLINE drflac_bool32 drflac__is_little_endian(void) +{ +#if defined(DRFLAC_X86) || defined(DRFLAC_X64) + return DRFLAC_TRUE; +#elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN + return DRFLAC_TRUE; +#else + int n = 1; + return (*(char*)&n) == 1; +#endif +} + +static DRFLAC_INLINE drflac_uint16 drflac__swap_endian_uint16(drflac_uint16 n) +{ +#ifdef DRFLAC_HAS_BYTESWAP16_INTRINSIC + #if defined(_MSC_VER) && !defined(__clang__) + return _byteswap_ushort(n); + #elif defined(__GNUC__) || defined(__clang__) + return __builtin_bswap16(n); + #elif defined(__WATCOMC__) && defined(__386__) + return _watcom_bswap16(n); + #else + #error "This compiler does not support the byte swap intrinsic." + #endif +#else + return ((n & 0xFF00) >> 8) | + ((n & 0x00FF) << 8); +#endif +} + +static DRFLAC_INLINE drflac_uint32 drflac__swap_endian_uint32(drflac_uint32 n) +{ +#ifdef DRFLAC_HAS_BYTESWAP32_INTRINSIC + #if defined(_MSC_VER) && !defined(__clang__) + return _byteswap_ulong(n); + #elif defined(__GNUC__) || defined(__clang__) + #if defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(__ARM_ARCH_6M__) && !defined(DRFLAC_64BIT) /* <-- 64-bit inline assembly has not been tested, so disabling for now. */ + /* Inline assembly optimized implementation for ARM. In my testing, GCC does not generate optimized code with __builtin_bswap32(). */ + drflac_uint32 r; + __asm__ __volatile__ ( + #if defined(DRFLAC_64BIT) + "rev %w[out], %w[in]" : [out]"=r"(r) : [in]"r"(n) /* <-- This is untested. If someone in the community could test this, that would be appreciated! */ + #else + "rev %[out], %[in]" : [out]"=r"(r) : [in]"r"(n) + #endif + ); + return r; + #else + return __builtin_bswap32(n); + #endif + #elif defined(__WATCOMC__) && defined(__386__) + return _watcom_bswap32(n); + #else + #error "This compiler does not support the byte swap intrinsic." + #endif +#else + return ((n & 0xFF000000) >> 24) | + ((n & 0x00FF0000) >> 8) | + ((n & 0x0000FF00) << 8) | + ((n & 0x000000FF) << 24); +#endif +} + +static DRFLAC_INLINE drflac_uint64 drflac__swap_endian_uint64(drflac_uint64 n) +{ +#ifdef DRFLAC_HAS_BYTESWAP64_INTRINSIC + #if defined(_MSC_VER) && !defined(__clang__) + return _byteswap_uint64(n); + #elif defined(__GNUC__) || defined(__clang__) + return __builtin_bswap64(n); + #elif defined(__WATCOMC__) && defined(__386__) + return _watcom_bswap64(n); + #else + #error "This compiler does not support the byte swap intrinsic." + #endif +#else + /* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */ + return ((n & ((drflac_uint64)0xFF000000 << 32)) >> 56) | + ((n & ((drflac_uint64)0x00FF0000 << 32)) >> 40) | + ((n & ((drflac_uint64)0x0000FF00 << 32)) >> 24) | + ((n & ((drflac_uint64)0x000000FF << 32)) >> 8) | + ((n & ((drflac_uint64)0xFF000000 )) << 8) | + ((n & ((drflac_uint64)0x00FF0000 )) << 24) | + ((n & ((drflac_uint64)0x0000FF00 )) << 40) | + ((n & ((drflac_uint64)0x000000FF )) << 56); +#endif +} + + +static DRFLAC_INLINE drflac_uint16 drflac__be2host_16(drflac_uint16 n) +{ + if (drflac__is_little_endian()) { + return drflac__swap_endian_uint16(n); + } + + return n; +} + +static DRFLAC_INLINE drflac_uint32 drflac__be2host_32(drflac_uint32 n) +{ + if (drflac__is_little_endian()) { + return drflac__swap_endian_uint32(n); + } + + return n; +} + +static DRFLAC_INLINE drflac_uint32 drflac__be2host_32_ptr_unaligned(const void* pData) +{ + const drflac_uint8* pNum = (drflac_uint8*)pData; + return *(pNum) << 24 | *(pNum+1) << 16 | *(pNum+2) << 8 | *(pNum+3); +} + +static DRFLAC_INLINE drflac_uint64 drflac__be2host_64(drflac_uint64 n) +{ + if (drflac__is_little_endian()) { + return drflac__swap_endian_uint64(n); + } + + return n; +} + + +static DRFLAC_INLINE drflac_uint32 drflac__le2host_32(drflac_uint32 n) +{ + if (!drflac__is_little_endian()) { + return drflac__swap_endian_uint32(n); + } + + return n; +} + +static DRFLAC_INLINE drflac_uint32 drflac__le2host_32_ptr_unaligned(const void* pData) +{ + const drflac_uint8* pNum = (drflac_uint8*)pData; + return *pNum | *(pNum+1) << 8 | *(pNum+2) << 16 | *(pNum+3) << 24; +} + + +static DRFLAC_INLINE drflac_uint32 drflac__unsynchsafe_32(drflac_uint32 n) +{ + drflac_uint32 result = 0; + result |= (n & 0x7F000000) >> 3; + result |= (n & 0x007F0000) >> 2; + result |= (n & 0x00007F00) >> 1; + result |= (n & 0x0000007F) >> 0; + + return result; +} + + + +/* The CRC code below is based on this document: http://zlib.net/crc_v3.txt */ +static drflac_uint8 drflac__crc8_table[] = { + 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, + 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, + 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, + 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, + 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, + 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, + 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, + 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, + 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, + 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, + 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, + 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, + 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, + 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, + 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, + 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 +}; + +static drflac_uint16 drflac__crc16_table[] = { + 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, + 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022, + 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072, + 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041, + 0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2, + 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1, + 0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1, + 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082, + 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192, + 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1, + 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1, + 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2, + 0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151, + 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162, + 0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132, + 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101, + 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312, + 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321, + 0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371, + 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342, + 0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1, + 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2, + 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2, + 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381, + 0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291, + 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2, + 0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2, + 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1, + 0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252, + 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261, + 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231, + 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202 +}; + +static DRFLAC_INLINE drflac_uint8 drflac_crc8_byte(drflac_uint8 crc, drflac_uint8 data) +{ + return drflac__crc8_table[crc ^ data]; +} + +static DRFLAC_INLINE drflac_uint8 drflac_crc8(drflac_uint8 crc, drflac_uint32 data, drflac_uint32 count) +{ +#ifdef DR_FLAC_NO_CRC + (void)crc; + (void)data; + (void)count; + return 0; +#else +#if 0 + /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc8(crc, 0, 8);") */ + drflac_uint8 p = 0x07; + for (int i = count-1; i >= 0; --i) { + drflac_uint8 bit = (data & (1 << i)) >> i; + if (crc & 0x80) { + crc = ((crc << 1) | bit) ^ p; + } else { + crc = ((crc << 1) | bit); + } + } + return crc; +#else + drflac_uint32 wholeBytes; + drflac_uint32 leftoverBits; + drflac_uint64 leftoverDataMask; + + static drflac_uint64 leftoverDataMaskTable[8] = { + 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F + }; + + DRFLAC_ASSERT(count <= 32); + + wholeBytes = count >> 3; + leftoverBits = count - (wholeBytes*8); + leftoverDataMask = leftoverDataMaskTable[leftoverBits]; + + switch (wholeBytes) { + case 4: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits))); + case 3: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits))); + case 2: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits))); + case 1: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits))); + case 0: if (leftoverBits > 0) crc = (drflac_uint8)((crc << leftoverBits) ^ drflac__crc8_table[(crc >> (8 - leftoverBits)) ^ (data & leftoverDataMask)]); + } + return crc; +#endif +#endif +} + +static DRFLAC_INLINE drflac_uint16 drflac_crc16_byte(drflac_uint16 crc, drflac_uint8 data) +{ + return (crc << 8) ^ drflac__crc16_table[(drflac_uint8)(crc >> 8) ^ data]; +} + +static DRFLAC_INLINE drflac_uint16 drflac_crc16_cache(drflac_uint16 crc, drflac_cache_t data) +{ +#ifdef DRFLAC_64BIT + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 56) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 48) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 40) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 32) & 0xFF)); +#endif + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 24) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 16) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 8) & 0xFF)); + crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 0) & 0xFF)); + + return crc; +} + +static DRFLAC_INLINE drflac_uint16 drflac_crc16_bytes(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 byteCount) +{ + switch (byteCount) + { +#ifdef DRFLAC_64BIT + case 8: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 56) & 0xFF)); + case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 48) & 0xFF)); + case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 40) & 0xFF)); + case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 32) & 0xFF)); +#endif + case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 24) & 0xFF)); + case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 16) & 0xFF)); + case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 8) & 0xFF)); + case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 0) & 0xFF)); + } + + return crc; +} + +#if 0 +static DRFLAC_INLINE drflac_uint16 drflac_crc16__32bit(drflac_uint16 crc, drflac_uint32 data, drflac_uint32 count) +{ +#ifdef DR_FLAC_NO_CRC + (void)crc; + (void)data; + (void)count; + return 0; +#else +#if 0 + /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc16(crc, 0, 16);") */ + drflac_uint16 p = 0x8005; + for (int i = count-1; i >= 0; --i) { + drflac_uint16 bit = (data & (1ULL << i)) >> i; + if (r & 0x8000) { + r = ((r << 1) | bit) ^ p; + } else { + r = ((r << 1) | bit); + } + } + + return crc; +#else + drflac_uint32 wholeBytes; + drflac_uint32 leftoverBits; + drflac_uint64 leftoverDataMask; + + static drflac_uint64 leftoverDataMaskTable[8] = { + 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F + }; + + DRFLAC_ASSERT(count <= 64); + + wholeBytes = count >> 3; + leftoverBits = count & 7; + leftoverDataMask = leftoverDataMaskTable[leftoverBits]; + + switch (wholeBytes) { + default: + case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits))); + case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits))); + case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits))); + case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits))); + case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)]; + } + return crc; +#endif +#endif +} + +static DRFLAC_INLINE drflac_uint16 drflac_crc16__64bit(drflac_uint16 crc, drflac_uint64 data, drflac_uint32 count) +{ +#ifdef DR_FLAC_NO_CRC + (void)crc; + (void)data; + (void)count; + return 0; +#else + drflac_uint32 wholeBytes; + drflac_uint32 leftoverBits; + drflac_uint64 leftoverDataMask; + + static drflac_uint64 leftoverDataMaskTable[8] = { + 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F + }; + + DRFLAC_ASSERT(count <= 64); + + wholeBytes = count >> 3; + leftoverBits = count & 7; + leftoverDataMask = leftoverDataMaskTable[leftoverBits]; + + switch (wholeBytes) { + default: + case 8: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0xFF000000 << 32) << leftoverBits)) >> (56 + leftoverBits))); /* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */ + case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000 << 32) << leftoverBits)) >> (48 + leftoverBits))); + case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00 << 32) << leftoverBits)) >> (40 + leftoverBits))); + case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF << 32) << leftoverBits)) >> (32 + leftoverBits))); + case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0xFF000000 ) << leftoverBits)) >> (24 + leftoverBits))); + case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000 ) << leftoverBits)) >> (16 + leftoverBits))); + case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00 ) << leftoverBits)) >> ( 8 + leftoverBits))); + case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF ) << leftoverBits)) >> ( 0 + leftoverBits))); + case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)]; + } + return crc; +#endif +} + + +static DRFLAC_INLINE drflac_uint16 drflac_crc16(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 count) +{ +#ifdef DRFLAC_64BIT + return drflac_crc16__64bit(crc, data, count); +#else + return drflac_crc16__32bit(crc, data, count); +#endif +} +#endif + + +#ifdef DRFLAC_64BIT +#define drflac__be2host__cache_line drflac__be2host_64 +#else +#define drflac__be2host__cache_line drflac__be2host_32 +#endif + +/* +BIT READING ATTEMPT #2 + +This uses a 32- or 64-bit bit-shifted cache - as bits are read, the cache is shifted such that the first valid bit is sitting +on the most significant bit. It uses the notion of an L1 and L2 cache (borrowed from CPU architecture), where the L1 cache +is a 32- or 64-bit unsigned integer (depending on whether or not a 32- or 64-bit build is being compiled) and the L2 is an +array of "cache lines", with each cache line being the same size as the L1. The L2 is a buffer of about 4KB and is where data +from onRead() is read into. +*/ +#define DRFLAC_CACHE_L1_SIZE_BYTES(bs) (sizeof((bs)->cache)) +#define DRFLAC_CACHE_L1_SIZE_BITS(bs) (sizeof((bs)->cache)*8) +#define DRFLAC_CACHE_L1_BITS_REMAINING(bs) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits) +#define DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount) (~((~(drflac_cache_t)0) >> (_bitCount))) +#define DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount)) +#define DRFLAC_CACHE_L1_SELECT(bs, _bitCount) (((bs)->cache) & DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount)) +#define DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount))) +#define DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, _bitCount)(DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> (DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)) & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1))) +#define DRFLAC_CACHE_L2_SIZE_BYTES(bs) (sizeof((bs)->cacheL2)) +#define DRFLAC_CACHE_L2_LINE_COUNT(bs) (DRFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0])) +#define DRFLAC_CACHE_L2_LINES_REMAINING(bs) (DRFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line) + + +#ifndef DR_FLAC_NO_CRC +static DRFLAC_INLINE void drflac__reset_crc16(drflac_bs* bs) +{ + bs->crc16 = 0; + bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3; +} + +static DRFLAC_INLINE void drflac__update_crc16(drflac_bs* bs) +{ + if (bs->crc16CacheIgnoredBytes == 0) { + bs->crc16 = drflac_crc16_cache(bs->crc16, bs->crc16Cache); + } else { + bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache, DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bs->crc16CacheIgnoredBytes); + bs->crc16CacheIgnoredBytes = 0; + } +} + +static DRFLAC_INLINE drflac_uint16 drflac__flush_crc16(drflac_bs* bs) +{ + /* We should never be flushing in a situation where we are not aligned on a byte boundary. */ + DRFLAC_ASSERT((DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7) == 0); + + /* + The bits that were read from the L1 cache need to be accumulated. The number of bytes needing to be accumulated is determined + by the number of bits that have been consumed. + */ + if (DRFLAC_CACHE_L1_BITS_REMAINING(bs) == 0) { + drflac__update_crc16(bs); + } else { + /* We only accumulate the consumed bits. */ + bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache >> DRFLAC_CACHE_L1_BITS_REMAINING(bs), (bs->consumedBits >> 3) - bs->crc16CacheIgnoredBytes); + + /* + The bits that we just accumulated should never be accumulated again. We need to keep track of how many bytes were accumulated + so we can handle that later. + */ + bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3; + } + + return bs->crc16; +} +#endif + +static DRFLAC_INLINE drflac_bool32 drflac__reload_l1_cache_from_l2(drflac_bs* bs) +{ + size_t bytesRead; + size_t alignedL1LineCount; + + /* Fast path. Try loading straight from L2. */ + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { + bs->cache = bs->cacheL2[bs->nextL2Line++]; + return DRFLAC_TRUE; + } + + /* + If we get here it means we've run out of data in the L2 cache. We'll need to fetch more from the client, if there's + any left. + */ + if (bs->unalignedByteCount > 0) { + return DRFLAC_FALSE; /* If we have any unaligned bytes it means there's no more aligned bytes left in the client. */ + } + + bytesRead = bs->onRead(bs->pUserData, bs->cacheL2, DRFLAC_CACHE_L2_SIZE_BYTES(bs)); + + bs->nextL2Line = 0; + if (bytesRead == DRFLAC_CACHE_L2_SIZE_BYTES(bs)) { + bs->cache = bs->cacheL2[bs->nextL2Line++]; + return DRFLAC_TRUE; + } + + + /* + If we get here it means we were unable to retrieve enough data to fill the entire L2 cache. It probably + means we've just reached the end of the file. We need to move the valid data down to the end of the buffer + and adjust the index of the next line accordingly. Also keep in mind that the L2 cache must be aligned to + the size of the L1 so we'll need to seek backwards by any misaligned bytes. + */ + alignedL1LineCount = bytesRead / DRFLAC_CACHE_L1_SIZE_BYTES(bs); + + /* We need to keep track of any unaligned bytes for later use. */ + bs->unalignedByteCount = bytesRead - (alignedL1LineCount * DRFLAC_CACHE_L1_SIZE_BYTES(bs)); + if (bs->unalignedByteCount > 0) { + bs->unalignedCache = bs->cacheL2[alignedL1LineCount]; + } + + if (alignedL1LineCount > 0) { + size_t offset = DRFLAC_CACHE_L2_LINE_COUNT(bs) - alignedL1LineCount; + size_t i; + for (i = alignedL1LineCount; i > 0; --i) { + bs->cacheL2[i-1 + offset] = bs->cacheL2[i-1]; + } + + bs->nextL2Line = (drflac_uint32)offset; + bs->cache = bs->cacheL2[bs->nextL2Line++]; + return DRFLAC_TRUE; + } else { + /* If we get into this branch it means we weren't able to load any L1-aligned data. */ + bs->nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs); + return DRFLAC_FALSE; + } +} + +static drflac_bool32 drflac__reload_cache(drflac_bs* bs) +{ + size_t bytesRead; + +#ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); +#endif + + /* Fast path. Try just moving the next value in the L2 cache to the L1 cache. */ + if (drflac__reload_l1_cache_from_l2(bs)) { + bs->cache = drflac__be2host__cache_line(bs->cache); + bs->consumedBits = 0; +#ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs->cache; +#endif + return DRFLAC_TRUE; + } + + /* Slow path. */ + + /* + If we get here it means we have failed to load the L1 cache from the L2. Likely we've just reached the end of the stream and the last + few bytes did not meet the alignment requirements for the L2 cache. In this case we need to fall back to a slower path and read the + data from the unaligned cache. + */ + bytesRead = bs->unalignedByteCount; + if (bytesRead == 0) { + bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- The stream has been exhausted, so marked the bits as consumed. */ + return DRFLAC_FALSE; + } + + DRFLAC_ASSERT(bytesRead < DRFLAC_CACHE_L1_SIZE_BYTES(bs)); + bs->consumedBits = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bytesRead) * 8; + + bs->cache = drflac__be2host__cache_line(bs->unalignedCache); + bs->cache &= DRFLAC_CACHE_L1_SELECTION_MASK(DRFLAC_CACHE_L1_BITS_REMAINING(bs)); /* <-- Make sure the consumed bits are always set to zero. Other parts of the library depend on this property. */ + bs->unalignedByteCount = 0; /* <-- At this point the unaligned bytes have been moved into the cache and we thus have no more unaligned bytes. */ + +#ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs->cache >> bs->consumedBits; + bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3; +#endif + return DRFLAC_TRUE; +} + +static void drflac__reset_cache(drflac_bs* bs) +{ + bs->nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs); /* <-- This clears the L2 cache. */ + bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- This clears the L1 cache. */ + bs->cache = 0; + bs->unalignedByteCount = 0; /* <-- This clears the trailing unaligned bytes. */ + bs->unalignedCache = 0; + +#ifndef DR_FLAC_NO_CRC + bs->crc16Cache = 0; + bs->crc16CacheIgnoredBytes = 0; +#endif +} + + +static DRFLAC_INLINE drflac_bool32 drflac__read_uint32(drflac_bs* bs, unsigned int bitCount, drflac_uint32* pResultOut) +{ + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResultOut != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 32); + + if (bs->consumedBits == DRFLAC_CACHE_L1_SIZE_BITS(bs)) { + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + } + + if (bitCount <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* + If we want to load all 32-bits from a 32-bit cache we need to do it slightly differently because we can't do + a 32-bit shift on a 32-bit integer. This will never be the case on 64-bit caches, so we can have a slightly + more optimal solution for this. + */ +#ifdef DRFLAC_64BIT + *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount); + bs->consumedBits += bitCount; + bs->cache <<= bitCount; +#else + if (bitCount < DRFLAC_CACHE_L1_SIZE_BITS(bs)) { + *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount); + bs->consumedBits += bitCount; + bs->cache <<= bitCount; + } else { + /* Cannot shift by 32-bits, so need to do it differently. */ + *pResultOut = (drflac_uint32)bs->cache; + bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs); + bs->cache = 0; + } +#endif + + return DRFLAC_TRUE; + } else { + /* It straddles the cached data. It will never cover more than the next chunk. We just read the number in two parts and combine them. */ + drflac_uint32 bitCountHi = DRFLAC_CACHE_L1_BITS_REMAINING(bs); + drflac_uint32 bitCountLo = bitCount - bitCountHi; + drflac_uint32 resultHi; + + DRFLAC_ASSERT(bitCountHi > 0); + DRFLAC_ASSERT(bitCountHi < 32); + resultHi = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountHi); + + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + if (bitCountLo > DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* This happens when we get to end of stream */ + return DRFLAC_FALSE; + } + + *pResultOut = (resultHi << bitCountLo) | (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountLo); + bs->consumedBits += bitCountLo; + bs->cache <<= bitCountLo; + return DRFLAC_TRUE; + } +} + +static drflac_bool32 drflac__read_int32(drflac_bs* bs, unsigned int bitCount, drflac_int32* pResult) +{ + drflac_uint32 result; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResult != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 32); + + if (!drflac__read_uint32(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + /* Do not attempt to shift by 32 as it's undefined. */ + if (bitCount < 32) { + drflac_uint32 signbit; + signbit = ((result >> (bitCount-1)) & 0x01); + result |= (~signbit + 1) << bitCount; + } + + *pResult = (drflac_int32)result; + return DRFLAC_TRUE; +} + +#ifdef DRFLAC_64BIT +static drflac_bool32 drflac__read_uint64(drflac_bs* bs, unsigned int bitCount, drflac_uint64* pResultOut) +{ + drflac_uint32 resultHi; + drflac_uint32 resultLo; + + DRFLAC_ASSERT(bitCount <= 64); + DRFLAC_ASSERT(bitCount > 32); + + if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) { + return DRFLAC_FALSE; + } + + if (!drflac__read_uint32(bs, 32, &resultLo)) { + return DRFLAC_FALSE; + } + + *pResultOut = (((drflac_uint64)resultHi) << 32) | ((drflac_uint64)resultLo); + return DRFLAC_TRUE; +} +#endif + +/* Function below is unused, but leaving it here in case I need to quickly add it again. */ +#if 0 +static drflac_bool32 drflac__read_int64(drflac_bs* bs, unsigned int bitCount, drflac_int64* pResultOut) +{ + drflac_uint64 result; + drflac_uint64 signbit; + + DRFLAC_ASSERT(bitCount <= 64); + + if (!drflac__read_uint64(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + signbit = ((result >> (bitCount-1)) & 0x01); + result |= (~signbit + 1) << bitCount; + + *pResultOut = (drflac_int64)result; + return DRFLAC_TRUE; +} +#endif + +static drflac_bool32 drflac__read_uint16(drflac_bs* bs, unsigned int bitCount, drflac_uint16* pResult) +{ + drflac_uint32 result; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResult != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 16); + + if (!drflac__read_uint32(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + *pResult = (drflac_uint16)result; + return DRFLAC_TRUE; +} + +#if 0 +static drflac_bool32 drflac__read_int16(drflac_bs* bs, unsigned int bitCount, drflac_int16* pResult) +{ + drflac_int32 result; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResult != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 16); + + if (!drflac__read_int32(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + *pResult = (drflac_int16)result; + return DRFLAC_TRUE; +} +#endif + +static drflac_bool32 drflac__read_uint8(drflac_bs* bs, unsigned int bitCount, drflac_uint8* pResult) +{ + drflac_uint32 result; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResult != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 8); + + if (!drflac__read_uint32(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + *pResult = (drflac_uint8)result; + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__read_int8(drflac_bs* bs, unsigned int bitCount, drflac_int8* pResult) +{ + drflac_int32 result; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pResult != NULL); + DRFLAC_ASSERT(bitCount > 0); + DRFLAC_ASSERT(bitCount <= 8); + + if (!drflac__read_int32(bs, bitCount, &result)) { + return DRFLAC_FALSE; + } + + *pResult = (drflac_int8)result; + return DRFLAC_TRUE; +} + + +static drflac_bool32 drflac__seek_bits(drflac_bs* bs, size_t bitsToSeek) +{ + if (bitsToSeek <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + bs->consumedBits += (drflac_uint32)bitsToSeek; + bs->cache <<= bitsToSeek; + return DRFLAC_TRUE; + } else { + /* It straddles the cached data. This function isn't called too frequently so I'm favouring simplicity here. */ + bitsToSeek -= DRFLAC_CACHE_L1_BITS_REMAINING(bs); + bs->consumedBits += DRFLAC_CACHE_L1_BITS_REMAINING(bs); + bs->cache = 0; + + /* Simple case. Seek in groups of the same number as bits that fit within a cache line. */ +#ifdef DRFLAC_64BIT + while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) { + drflac_uint64 bin; + if (!drflac__read_uint64(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) { + return DRFLAC_FALSE; + } + bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs); + } +#else + while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) { + drflac_uint32 bin; + if (!drflac__read_uint32(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) { + return DRFLAC_FALSE; + } + bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs); + } +#endif + + /* Whole leftover bytes. */ + while (bitsToSeek >= 8) { + drflac_uint8 bin; + if (!drflac__read_uint8(bs, 8, &bin)) { + return DRFLAC_FALSE; + } + bitsToSeek -= 8; + } + + /* Leftover bits. */ + if (bitsToSeek > 0) { + drflac_uint8 bin; + if (!drflac__read_uint8(bs, (drflac_uint32)bitsToSeek, &bin)) { + return DRFLAC_FALSE; + } + bitsToSeek = 0; /* <-- Necessary for the assert below. */ + } + + DRFLAC_ASSERT(bitsToSeek == 0); + return DRFLAC_TRUE; + } +} + + +/* This function moves the bit streamer to the first bit after the sync code (bit 15 of the of the frame header). It will also update the CRC-16. */ +static drflac_bool32 drflac__find_and_seek_to_next_sync_code(drflac_bs* bs) +{ + DRFLAC_ASSERT(bs != NULL); + + /* + The sync code is always aligned to 8 bits. This is convenient for us because it means we can do byte-aligned movements. The first + thing to do is align to the next byte. + */ + if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) { + return DRFLAC_FALSE; + } + + for (;;) { + drflac_uint8 hi; + +#ifndef DR_FLAC_NO_CRC + drflac__reset_crc16(bs); +#endif + + if (!drflac__read_uint8(bs, 8, &hi)) { + return DRFLAC_FALSE; + } + + if (hi == 0xFF) { + drflac_uint8 lo; + if (!drflac__read_uint8(bs, 6, &lo)) { + return DRFLAC_FALSE; + } + + if (lo == 0x3E) { + return DRFLAC_TRUE; + } else { + if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) { + return DRFLAC_FALSE; + } + } + } + } + + /* Should never get here. */ + /*return DRFLAC_FALSE;*/ +} + + +#if defined(DRFLAC_HAS_LZCNT_INTRINSIC) +#define DRFLAC_IMPLEMENT_CLZ_LZCNT +#endif +#if defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(__clang__) +#define DRFLAC_IMPLEMENT_CLZ_MSVC +#endif +#if defined(__WATCOMC__) && defined(__386__) +#define DRFLAC_IMPLEMENT_CLZ_WATCOM +#endif +#ifdef __MRC__ +#include +#define DRFLAC_IMPLEMENT_CLZ_MRC +#endif + +static DRFLAC_INLINE drflac_uint32 drflac__clz_software(drflac_cache_t x) +{ + drflac_uint32 n; + static drflac_uint32 clz_table_4[] = { + 0, + 4, + 3, 3, + 2, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 1 + }; + + if (x == 0) { + return sizeof(x)*8; + } + + n = clz_table_4[x >> (sizeof(x)*8 - 4)]; + if (n == 0) { +#ifdef DRFLAC_64BIT + if ((x & ((drflac_uint64)0xFFFFFFFF << 32)) == 0) { n = 32; x <<= 32; } + if ((x & ((drflac_uint64)0xFFFF0000 << 32)) == 0) { n += 16; x <<= 16; } + if ((x & ((drflac_uint64)0xFF000000 << 32)) == 0) { n += 8; x <<= 8; } + if ((x & ((drflac_uint64)0xF0000000 << 32)) == 0) { n += 4; x <<= 4; } +#else + if ((x & 0xFFFF0000) == 0) { n = 16; x <<= 16; } + if ((x & 0xFF000000) == 0) { n += 8; x <<= 8; } + if ((x & 0xF0000000) == 0) { n += 4; x <<= 4; } +#endif + n += clz_table_4[x >> (sizeof(x)*8 - 4)]; + } + + return n - 1; +} + +#ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT +static DRFLAC_INLINE drflac_bool32 drflac__is_lzcnt_supported(void) +{ + /* Fast compile time check for ARM. */ +#if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) + return DRFLAC_TRUE; +#elif defined(__MRC__) + return DRFLAC_TRUE; +#else + /* If the compiler itself does not support the intrinsic then we'll need to return false. */ + #ifdef DRFLAC_HAS_LZCNT_INTRINSIC + return drflac__gIsLZCNTSupported; + #else + return DRFLAC_FALSE; + #endif +#endif +} + +static DRFLAC_INLINE drflac_uint32 drflac__clz_lzcnt(drflac_cache_t x) +{ + /* + It's critical for competitive decoding performance that this function be highly optimal. With MSVC we can use the __lzcnt64() and __lzcnt() intrinsics + to achieve good performance, however on GCC and Clang it's a little bit more annoying. The __builtin_clzl() and __builtin_clzll() intrinsics leave + it undefined as to the return value when `x` is 0. We need this to be well defined as returning 32 or 64, depending on whether or not it's a 32- or + 64-bit build. To work around this we would need to add a conditional to check for the x = 0 case, but this creates unnecessary inefficiency. To work + around this problem I have written some inline assembly to emit the LZCNT (x86) or CLZ (ARM) instruction directly which removes the need to include + the conditional. This has worked well in the past, but for some reason Clang's MSVC compatible driver, clang-cl, does not seem to be handling this + in the same way as the normal Clang driver. It seems that `clang-cl` is just outputting the wrong results sometimes, maybe due to some register + getting clobbered? + + I'm not sure if this is a bug with dr_flac's inlined assembly (most likely), a bug in `clang-cl` or just a misunderstanding on my part with inline + assembly rules for `clang-cl`. If somebody can identify an error in dr_flac's inlined assembly I'm happy to get that fixed. + + Fortunately there is an easy workaround for this. Clang implements MSVC-specific intrinsics for compatibility. It also defines _MSC_VER for extra + compatibility. We can therefore just check for _MSC_VER and use the MSVC intrinsic which, fortunately for us, Clang supports. It would still be nice + to know how to fix the inlined assembly for correctness sake, however. + */ + +#if defined(_MSC_VER) /*&& !defined(__clang__)*/ /* <-- Intentionally wanting Clang to use the MSVC __lzcnt64/__lzcnt intrinsics due to above ^. */ + #ifdef DRFLAC_64BIT + return (drflac_uint32)__lzcnt64(x); + #else + return (drflac_uint32)__lzcnt(x); + #endif +#else + #if defined(__GNUC__) || defined(__clang__) + #if defined(DRFLAC_X64) + { + /* + A note on lzcnt. + + We check for the presence of the lzcnt instruction at runtime before calling this function, but we still generate this code. I have had + a report where the assembler does not recognize the lzcnt instruction. To work around this we are going to use `rep; bsr` instead which + has an identical byte encoding as lzcnt, and should hopefully improve compatibility with older assemblers. + */ + drflac_uint64 r; + __asm__ __volatile__ ( + "rep; bsr{q %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc" + /*"lzcnt{ %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"*/ + ); + + return (drflac_uint32)r; + } + #elif defined(DRFLAC_X86) + { + drflac_uint32 r; + __asm__ __volatile__ ( + "rep; bsr{l %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc" + /*"lzcnt{l %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"*/ + ); + + return r; + } + #elif defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(__ARM_ARCH_6M__) && !(defined(__thumb__) && !defined(__thumb2__)) && !defined(DRFLAC_64BIT) /* <-- I haven't tested 64-bit inline assembly, so only enabling this for the 32-bit build for now. */ + { + unsigned int r; + __asm__ __volatile__ ( + #if defined(DRFLAC_64BIT) + "clz %w[out], %w[in]" : [out]"=r"(r) : [in]"r"(x) /* <-- This is untested. If someone in the community could test this, that would be appreciated! */ + #else + "clz %[out], %[in]" : [out]"=r"(r) : [in]"r"(x) + #endif + ); + + return r; + } + #else + if (x == 0) { + return sizeof(x)*8; + } + #ifdef DRFLAC_64BIT + return (drflac_uint32)__builtin_clzll((drflac_uint64)x); + #else + return (drflac_uint32)__builtin_clzl((drflac_uint32)x); + #endif + #endif + #else + /* Unsupported compiler. */ + #error "This compiler does not support the lzcnt intrinsic." + #endif +#endif +} +#endif + +#ifdef DRFLAC_IMPLEMENT_CLZ_MSVC +#include /* For BitScanReverse(). */ + +static DRFLAC_INLINE drflac_uint32 drflac__clz_msvc(drflac_cache_t x) +{ + drflac_uint32 n; + + if (x == 0) { + return sizeof(x)*8; + } + +#ifdef DRFLAC_64BIT + _BitScanReverse64((unsigned long*)&n, x); +#else + _BitScanReverse((unsigned long*)&n, x); +#endif + return sizeof(x)*8 - n - 1; +} +#endif + +#ifdef DRFLAC_IMPLEMENT_CLZ_WATCOM +static __inline drflac_uint32 drflac__clz_watcom (drflac_uint32); +#ifdef DRFLAC_IMPLEMENT_CLZ_WATCOM_LZCNT +/* Use the LZCNT instruction (only available on some processors since the 2010s). */ +#pragma aux drflac__clz_watcom_lzcnt = \ + "db 0F3h, 0Fh, 0BDh, 0C0h" /* lzcnt eax, eax */ \ + parm [eax] \ + value [eax] \ + modify nomemory; +#else +/* Use the 386+-compatible implementation. */ +#pragma aux drflac__clz_watcom = \ + "bsr eax, eax" \ + "xor eax, 31" \ + parm [eax] nomemory \ + value [eax] \ + modify exact [eax] nomemory; +#endif +#endif + +static DRFLAC_INLINE drflac_uint32 drflac__clz(drflac_cache_t x) +{ +#ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT + if (drflac__is_lzcnt_supported()) { + return drflac__clz_lzcnt(x); + } else +#endif + { +#ifdef DRFLAC_IMPLEMENT_CLZ_MSVC + return drflac__clz_msvc(x); +#elif defined(DRFLAC_IMPLEMENT_CLZ_WATCOM_LZCNT) + return drflac__clz_watcom_lzcnt(x); +#elif defined(DRFLAC_IMPLEMENT_CLZ_WATCOM) + return (x == 0) ? sizeof(x)*8 : drflac__clz_watcom(x); +#elif defined(__MRC__) + return __cntlzw(x); +#else + return drflac__clz_software(x); +#endif + } +} + + +static DRFLAC_INLINE drflac_bool32 drflac__seek_past_next_set_bit(drflac_bs* bs, unsigned int* pOffsetOut) +{ + drflac_uint32 zeroCounter = 0; + drflac_uint32 setBitOffsetPlus1; + + while (bs->cache == 0) { + zeroCounter += (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs); + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + } + + if (bs->cache == 1) { + /* Not catching this would lead to undefined behaviour: a shift of a 32-bit number by 32 or more is undefined */ + *pOffsetOut = zeroCounter + (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs) - 1; + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + + return DRFLAC_TRUE; + } + + setBitOffsetPlus1 = drflac__clz(bs->cache); + setBitOffsetPlus1 += 1; + + if (setBitOffsetPlus1 > DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* This happens when we get to end of stream */ + return DRFLAC_FALSE; + } + + bs->consumedBits += setBitOffsetPlus1; + bs->cache <<= setBitOffsetPlus1; + + *pOffsetOut = zeroCounter + setBitOffsetPlus1 - 1; + return DRFLAC_TRUE; +} + + + +static drflac_bool32 drflac__seek_to_byte(drflac_bs* bs, drflac_uint64 offsetFromStart) +{ + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(offsetFromStart > 0); + + /* + Seeking from the start is not quite as trivial as it sounds because the onSeek callback takes a signed 32-bit integer (which + is intentional because it simplifies the implementation of the onSeek callbacks), however offsetFromStart is unsigned 64-bit. + To resolve we just need to do an initial seek from the start, and then a series of offset seeks to make up the remainder. + */ + if (offsetFromStart > 0x7FFFFFFF) { + drflac_uint64 bytesRemaining = offsetFromStart; + if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + bytesRemaining -= 0x7FFFFFFF; + + while (bytesRemaining > 0x7FFFFFFF) { + if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + bytesRemaining -= 0x7FFFFFFF; + } + + if (bytesRemaining > 0) { + if (!bs->onSeek(bs->pUserData, (int)bytesRemaining, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + } + } else { + if (!bs->onSeek(bs->pUserData, (int)offsetFromStart, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + } + + /* The cache should be reset to force a reload of fresh data from the client. */ + drflac__reset_cache(bs); + return DRFLAC_TRUE; +} + + +static drflac_result drflac__read_utf8_coded_number(drflac_bs* bs, drflac_uint64* pNumberOut, drflac_uint8* pCRCOut) +{ + drflac_uint8 crc; + drflac_uint64 result; + drflac_uint8 utf8[7] = {0}; + int byteCount; + int i; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pNumberOut != NULL); + DRFLAC_ASSERT(pCRCOut != NULL); + + crc = *pCRCOut; + + if (!drflac__read_uint8(bs, 8, utf8)) { + *pNumberOut = 0; + return DRFLAC_AT_END; + } + crc = drflac_crc8(crc, utf8[0], 8); + + if ((utf8[0] & 0x80) == 0) { + *pNumberOut = utf8[0]; + *pCRCOut = crc; + return DRFLAC_SUCCESS; + } + + /*byteCount = 1;*/ + if ((utf8[0] & 0xE0) == 0xC0) { + byteCount = 2; + } else if ((utf8[0] & 0xF0) == 0xE0) { + byteCount = 3; + } else if ((utf8[0] & 0xF8) == 0xF0) { + byteCount = 4; + } else if ((utf8[0] & 0xFC) == 0xF8) { + byteCount = 5; + } else if ((utf8[0] & 0xFE) == 0xFC) { + byteCount = 6; + } else if ((utf8[0] & 0xFF) == 0xFE) { + byteCount = 7; + } else { + *pNumberOut = 0; + return DRFLAC_CRC_MISMATCH; /* Bad UTF-8 encoding. */ + } + + /* Read extra bytes. */ + DRFLAC_ASSERT(byteCount > 1); + + result = (drflac_uint64)(utf8[0] & (0xFF >> (byteCount + 1))); + for (i = 1; i < byteCount; ++i) { + if (!drflac__read_uint8(bs, 8, utf8 + i)) { + *pNumberOut = 0; + return DRFLAC_AT_END; + } + crc = drflac_crc8(crc, utf8[i], 8); + + result = (result << 6) | (utf8[i] & 0x3F); + } + + *pNumberOut = result; + *pCRCOut = crc; + return DRFLAC_SUCCESS; +} + + +static DRFLAC_INLINE drflac_uint32 drflac__ilog2_u32(drflac_uint32 x) +{ +#if 1 /* Needs optimizing. */ + drflac_uint32 result = 0; + while (x > 0) { + result += 1; + x >>= 1; + } + + return result; +#endif +} + +static DRFLAC_INLINE drflac_bool32 drflac__use_64_bit_prediction(drflac_uint32 bitsPerSample, drflac_uint32 order, drflac_uint32 precision) +{ + /* https://web.archive.org/web/20220205005724/https://github.com/ietf-wg-cellar/flac-specification/blob/37a49aa48ba4ba12e8757badfc59c0df35435fec/rfc_backmatter.md */ + return bitsPerSample + precision + drflac__ilog2_u32(order) > 32; +} + + +/* +The next two functions are responsible for calculating the prediction. + +When the bits per sample is >16 we need to use 64-bit integer arithmetic because otherwise we'll run out of precision. It's +safe to assume this will be slower on 32-bit platforms so we use a more optimal solution when the bits per sample is <=16. +*/ +#if defined(__clang__) +__attribute__((no_sanitize("signed-integer-overflow"))) +#endif +static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples) +{ + drflac_int32 prediction = 0; + + DRFLAC_ASSERT(order <= 32); + + /* 32-bit version. */ + + /* VC++ optimizes this to a single jmp. I've not yet verified this for other compilers. */ + switch (order) + { + case 32: prediction += coefficients[31] * pDecodedSamples[-32]; + case 31: prediction += coefficients[30] * pDecodedSamples[-31]; + case 30: prediction += coefficients[29] * pDecodedSamples[-30]; + case 29: prediction += coefficients[28] * pDecodedSamples[-29]; + case 28: prediction += coefficients[27] * pDecodedSamples[-28]; + case 27: prediction += coefficients[26] * pDecodedSamples[-27]; + case 26: prediction += coefficients[25] * pDecodedSamples[-26]; + case 25: prediction += coefficients[24] * pDecodedSamples[-25]; + case 24: prediction += coefficients[23] * pDecodedSamples[-24]; + case 23: prediction += coefficients[22] * pDecodedSamples[-23]; + case 22: prediction += coefficients[21] * pDecodedSamples[-22]; + case 21: prediction += coefficients[20] * pDecodedSamples[-21]; + case 20: prediction += coefficients[19] * pDecodedSamples[-20]; + case 19: prediction += coefficients[18] * pDecodedSamples[-19]; + case 18: prediction += coefficients[17] * pDecodedSamples[-18]; + case 17: prediction += coefficients[16] * pDecodedSamples[-17]; + case 16: prediction += coefficients[15] * pDecodedSamples[-16]; + case 15: prediction += coefficients[14] * pDecodedSamples[-15]; + case 14: prediction += coefficients[13] * pDecodedSamples[-14]; + case 13: prediction += coefficients[12] * pDecodedSamples[-13]; + case 12: prediction += coefficients[11] * pDecodedSamples[-12]; + case 11: prediction += coefficients[10] * pDecodedSamples[-11]; + case 10: prediction += coefficients[ 9] * pDecodedSamples[-10]; + case 9: prediction += coefficients[ 8] * pDecodedSamples[- 9]; + case 8: prediction += coefficients[ 7] * pDecodedSamples[- 8]; + case 7: prediction += coefficients[ 6] * pDecodedSamples[- 7]; + case 6: prediction += coefficients[ 5] * pDecodedSamples[- 6]; + case 5: prediction += coefficients[ 4] * pDecodedSamples[- 5]; + case 4: prediction += coefficients[ 3] * pDecodedSamples[- 4]; + case 3: prediction += coefficients[ 2] * pDecodedSamples[- 3]; + case 2: prediction += coefficients[ 1] * pDecodedSamples[- 2]; + case 1: prediction += coefficients[ 0] * pDecodedSamples[- 1]; + } + + return (drflac_int32)(prediction >> shift); +} + +static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples) +{ + drflac_int64 prediction; + + DRFLAC_ASSERT(order <= 32); + + /* 64-bit version. */ + + /* This method is faster on the 32-bit build when compiling with VC++. See note below. */ +#ifndef DRFLAC_64BIT + if (order == 8) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8]; + } + else if (order == 7) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + } + else if (order == 3) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + } + else if (order == 6) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + } + else if (order == 5) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + } + else if (order == 4) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + } + else if (order == 12) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8]; + prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9]; + prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10]; + prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11]; + prediction += coefficients[11] * (drflac_int64)pDecodedSamples[-12]; + } + else if (order == 2) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + } + else if (order == 1) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + } + else if (order == 10) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8]; + prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9]; + prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10]; + } + else if (order == 9) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8]; + prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9]; + } + else if (order == 11) + { + prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1]; + prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2]; + prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3]; + prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4]; + prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5]; + prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6]; + prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7]; + prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8]; + prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9]; + prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10]; + prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11]; + } + else + { + int j; + + prediction = 0; + for (j = 0; j < (int)order; ++j) { + prediction += coefficients[j] * (drflac_int64)pDecodedSamples[-j-1]; + } + } +#endif + + /* + VC++ optimizes this to a single jmp instruction, but only the 64-bit build. The 32-bit build generates less efficient code for some + reason. The ugly version above is faster so we'll just switch between the two depending on the target platform. + */ +#ifdef DRFLAC_64BIT + prediction = 0; + switch (order) + { + case 32: prediction += coefficients[31] * (drflac_int64)pDecodedSamples[-32]; + case 31: prediction += coefficients[30] * (drflac_int64)pDecodedSamples[-31]; + case 30: prediction += coefficients[29] * (drflac_int64)pDecodedSamples[-30]; + case 29: prediction += coefficients[28] * (drflac_int64)pDecodedSamples[-29]; + case 28: prediction += coefficients[27] * (drflac_int64)pDecodedSamples[-28]; + case 27: prediction += coefficients[26] * (drflac_int64)pDecodedSamples[-27]; + case 26: prediction += coefficients[25] * (drflac_int64)pDecodedSamples[-26]; + case 25: prediction += coefficients[24] * (drflac_int64)pDecodedSamples[-25]; + case 24: prediction += coefficients[23] * (drflac_int64)pDecodedSamples[-24]; + case 23: prediction += coefficients[22] * (drflac_int64)pDecodedSamples[-23]; + case 22: prediction += coefficients[21] * (drflac_int64)pDecodedSamples[-22]; + case 21: prediction += coefficients[20] * (drflac_int64)pDecodedSamples[-21]; + case 20: prediction += coefficients[19] * (drflac_int64)pDecodedSamples[-20]; + case 19: prediction += coefficients[18] * (drflac_int64)pDecodedSamples[-19]; + case 18: prediction += coefficients[17] * (drflac_int64)pDecodedSamples[-18]; + case 17: prediction += coefficients[16] * (drflac_int64)pDecodedSamples[-17]; + case 16: prediction += coefficients[15] * (drflac_int64)pDecodedSamples[-16]; + case 15: prediction += coefficients[14] * (drflac_int64)pDecodedSamples[-15]; + case 14: prediction += coefficients[13] * (drflac_int64)pDecodedSamples[-14]; + case 13: prediction += coefficients[12] * (drflac_int64)pDecodedSamples[-13]; + case 12: prediction += coefficients[11] * (drflac_int64)pDecodedSamples[-12]; + case 11: prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11]; + case 10: prediction += coefficients[ 9] * (drflac_int64)pDecodedSamples[-10]; + case 9: prediction += coefficients[ 8] * (drflac_int64)pDecodedSamples[- 9]; + case 8: prediction += coefficients[ 7] * (drflac_int64)pDecodedSamples[- 8]; + case 7: prediction += coefficients[ 6] * (drflac_int64)pDecodedSamples[- 7]; + case 6: prediction += coefficients[ 5] * (drflac_int64)pDecodedSamples[- 6]; + case 5: prediction += coefficients[ 4] * (drflac_int64)pDecodedSamples[- 5]; + case 4: prediction += coefficients[ 3] * (drflac_int64)pDecodedSamples[- 4]; + case 3: prediction += coefficients[ 2] * (drflac_int64)pDecodedSamples[- 3]; + case 2: prediction += coefficients[ 1] * (drflac_int64)pDecodedSamples[- 2]; + case 1: prediction += coefficients[ 0] * (drflac_int64)pDecodedSamples[- 1]; + } +#endif + + return (drflac_int32)(prediction >> shift); +} + + +#if 0 +/* +Reference implementation for reading and decoding samples with residual. This is intentionally left unoptimized for the +sake of readability and should only be used as a reference. +*/ +static drflac_bool32 drflac__decode_samples_with_residual__rice__reference(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + drflac_uint32 i; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pSamplesOut != NULL); + + for (i = 0; i < count; ++i) { + drflac_uint32 zeroCounter = 0; + for (;;) { + drflac_uint8 bit; + if (!drflac__read_uint8(bs, 1, &bit)) { + return DRFLAC_FALSE; + } + + if (bit == 0) { + zeroCounter += 1; + } else { + break; + } + } + + drflac_uint32 decodedRice; + if (riceParam > 0) { + if (!drflac__read_uint32(bs, riceParam, &decodedRice)) { + return DRFLAC_FALSE; + } + } else { + decodedRice = 0; + } + + decodedRice |= (zeroCounter << riceParam); + if ((decodedRice & 0x01)) { + decodedRice = ~(decodedRice >> 1); + } else { + decodedRice = (decodedRice >> 1); + } + + + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + pSamplesOut[i] = decodedRice + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + i); + } else { + pSamplesOut[i] = decodedRice + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + i); + } + } + + return DRFLAC_TRUE; +} +#endif + +#if 0 +static drflac_bool32 drflac__read_rice_parts__reference(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut) +{ + drflac_uint32 zeroCounter = 0; + drflac_uint32 decodedRice; + + for (;;) { + drflac_uint8 bit; + if (!drflac__read_uint8(bs, 1, &bit)) { + return DRFLAC_FALSE; + } + + if (bit == 0) { + zeroCounter += 1; + } else { + break; + } + } + + if (riceParam > 0) { + if (!drflac__read_uint32(bs, riceParam, &decodedRice)) { + return DRFLAC_FALSE; + } + } else { + decodedRice = 0; + } + + *pZeroCounterOut = zeroCounter; + *pRiceParamPartOut = decodedRice; + return DRFLAC_TRUE; +} +#endif + +#if 0 +static DRFLAC_INLINE drflac_bool32 drflac__read_rice_parts(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut) +{ + drflac_cache_t riceParamMask; + drflac_uint32 zeroCounter; + drflac_uint32 setBitOffsetPlus1; + drflac_uint32 riceParamPart; + drflac_uint32 riceLength; + + DRFLAC_ASSERT(riceParam > 0); /* <-- riceParam should never be 0. drflac__read_rice_parts__param_equals_zero() should be used instead for this case. */ + + riceParamMask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParam); + + zeroCounter = 0; + while (bs->cache == 0) { + zeroCounter += (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs); + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + } + + setBitOffsetPlus1 = drflac__clz(bs->cache); + zeroCounter += setBitOffsetPlus1; + setBitOffsetPlus1 += 1; + + riceLength = setBitOffsetPlus1 + riceParam; + if (riceLength < DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + riceParamPart = (drflac_uint32)((bs->cache & (riceParamMask >> setBitOffsetPlus1)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceLength)); + + bs->consumedBits += riceLength; + bs->cache <<= riceLength; + } else { + drflac_uint32 bitCountLo; + drflac_cache_t resultHi; + + bs->consumedBits += riceLength; + bs->cache <<= setBitOffsetPlus1 & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1); /* <-- Equivalent to "if (setBitOffsetPlus1 < DRFLAC_CACHE_L1_SIZE_BITS(bs)) { bs->cache <<= setBitOffsetPlus1; }" */ + + /* It straddles the cached data. It will never cover more than the next chunk. We just read the number in two parts and combine them. */ + bitCountLo = bs->consumedBits - DRFLAC_CACHE_L1_SIZE_BITS(bs); + resultHi = DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, riceParam); /* <-- Use DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE() if ever this function allows riceParam=0. */ + + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { +#ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); +#endif + bs->cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]); + bs->consumedBits = 0; +#ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs->cache; +#endif + } else { + /* Slow path. We need to fetch more data from the client. */ + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + if (bitCountLo > DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* This happens when we get to end of stream */ + return DRFLAC_FALSE; + } + } + + riceParamPart = (drflac_uint32)(resultHi | DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, bitCountLo)); + + bs->consumedBits += bitCountLo; + bs->cache <<= bitCountLo; + } + + pZeroCounterOut[0] = zeroCounter; + pRiceParamPartOut[0] = riceParamPart; + + return DRFLAC_TRUE; +} +#endif + +static DRFLAC_INLINE drflac_bool32 drflac__read_rice_parts_x1(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut) +{ + drflac_uint32 riceParamPlus1 = riceParam + 1; + /*drflac_cache_t riceParamPlus1Mask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParamPlus1);*/ + drflac_uint32 riceParamPlus1Shift = DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPlus1); + drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1; + + /* + The idea here is to use local variables for the cache in an attempt to encourage the compiler to store them in registers. I have + no idea how this will work in practice... + */ + drflac_cache_t bs_cache = bs->cache; + drflac_uint32 bs_consumedBits = bs->consumedBits; + + /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */ + drflac_uint32 lzcount = drflac__clz(bs_cache); + if (lzcount < sizeof(bs_cache)*8) { + pZeroCounterOut[0] = lzcount; + + /* + It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting + this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled + outside of this function at a higher level. + */ + extract_rice_param_part: + bs_cache <<= lzcount; + bs_consumedBits += lzcount; + + if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) { + /* Getting here means the rice parameter part is wholly contained within the current cache line. */ + pRiceParamPartOut[0] = (drflac_uint32)(bs_cache >> riceParamPlus1Shift); + bs_cache <<= riceParamPlus1; + bs_consumedBits += riceParamPlus1; + } else { + drflac_uint32 riceParamPartHi; + drflac_uint32 riceParamPartLo; + drflac_uint32 riceParamPartLoBitCount; + + /* + Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache + line, reload the cache, and then combine it with the head of the next cache line. + */ + + /* Grab the high part of the rice parameter part. */ + riceParamPartHi = (drflac_uint32)(bs_cache >> riceParamPlus1Shift); + + /* Before reloading the cache we need to grab the size in bits of the low part. */ + riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits; + DRFLAC_ASSERT(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32); + + /* Now reload the cache. */ + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { + #ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); + #endif + bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]); + bs_consumedBits = riceParamPartLoBitCount; + #ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs_cache; + #endif + } else { + /* Slow path. We need to fetch more data from the client. */ + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + if (riceParamPartLoBitCount > DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* This happens when we get to end of stream */ + return DRFLAC_FALSE; + } + + bs_cache = bs->cache; + bs_consumedBits = bs->consumedBits + riceParamPartLoBitCount; + } + + /* We should now have enough information to construct the rice parameter part. */ + riceParamPartLo = (drflac_uint32)(bs_cache >> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPartLoBitCount))); + pRiceParamPartOut[0] = riceParamPartHi | riceParamPartLo; + + bs_cache <<= riceParamPartLoBitCount; + } + } else { + /* + Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call + to drflac__clz() and we need to reload the cache. + */ + drflac_uint32 zeroCounter = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BITS(bs) - bs_consumedBits); + for (;;) { + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { + #ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); + #endif + bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]); + bs_consumedBits = 0; + #ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs_cache; + #endif + } else { + /* Slow path. We need to fetch more data from the client. */ + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + + bs_cache = bs->cache; + bs_consumedBits = bs->consumedBits; + } + + lzcount = drflac__clz(bs_cache); + zeroCounter += lzcount; + + if (lzcount < sizeof(bs_cache)*8) { + break; + } + } + + pZeroCounterOut[0] = zeroCounter; + goto extract_rice_param_part; + } + + /* Make sure the cache is restored at the end of it all. */ + bs->cache = bs_cache; + bs->consumedBits = bs_consumedBits; + + return DRFLAC_TRUE; +} + +static DRFLAC_INLINE drflac_bool32 drflac__seek_rice_parts(drflac_bs* bs, drflac_uint8 riceParam) +{ + drflac_uint32 riceParamPlus1 = riceParam + 1; + drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1; + + /* + The idea here is to use local variables for the cache in an attempt to encourage the compiler to store them in registers. I have + no idea how this will work in practice... + */ + drflac_cache_t bs_cache = bs->cache; + drflac_uint32 bs_consumedBits = bs->consumedBits; + + /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */ + drflac_uint32 lzcount = drflac__clz(bs_cache); + if (lzcount < sizeof(bs_cache)*8) { + /* + It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting + this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled + outside of this function at a higher level. + */ + extract_rice_param_part: + bs_cache <<= lzcount; + bs_consumedBits += lzcount; + + if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) { + /* Getting here means the rice parameter part is wholly contained within the current cache line. */ + bs_cache <<= riceParamPlus1; + bs_consumedBits += riceParamPlus1; + } else { + /* + Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache + line, reload the cache, and then combine it with the head of the next cache line. + */ + + /* Before reloading the cache we need to grab the size in bits of the low part. */ + drflac_uint32 riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits; + DRFLAC_ASSERT(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32); + + /* Now reload the cache. */ + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { + #ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); + #endif + bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]); + bs_consumedBits = riceParamPartLoBitCount; + #ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs_cache; + #endif + } else { + /* Slow path. We need to fetch more data from the client. */ + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + + if (riceParamPartLoBitCount > DRFLAC_CACHE_L1_BITS_REMAINING(bs)) { + /* This happens when we get to end of stream */ + return DRFLAC_FALSE; + } + + bs_cache = bs->cache; + bs_consumedBits = bs->consumedBits + riceParamPartLoBitCount; + } + + bs_cache <<= riceParamPartLoBitCount; + } + } else { + /* + Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call + to drflac__clz() and we need to reload the cache. + */ + for (;;) { + if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) { + #ifndef DR_FLAC_NO_CRC + drflac__update_crc16(bs); + #endif + bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]); + bs_consumedBits = 0; + #ifndef DR_FLAC_NO_CRC + bs->crc16Cache = bs_cache; + #endif + } else { + /* Slow path. We need to fetch more data from the client. */ + if (!drflac__reload_cache(bs)) { + return DRFLAC_FALSE; + } + + bs_cache = bs->cache; + bs_consumedBits = bs->consumedBits; + } + + lzcount = drflac__clz(bs_cache); + if (lzcount < sizeof(bs_cache)*8) { + break; + } + } + + goto extract_rice_param_part; + } + + /* Make sure the cache is restored at the end of it all. */ + bs->cache = bs_cache; + bs->consumedBits = bs_consumedBits; + + return DRFLAC_TRUE; +} + + +static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar_zeroorder(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + drflac_uint32 zeroCountPart0; + drflac_uint32 riceParamPart0; + drflac_uint32 riceParamMask; + drflac_uint32 i; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pSamplesOut != NULL); + + (void)bitsPerSample; + (void)order; + (void)shift; + (void)coefficients; + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + + i = 0; + while (i < count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0)) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamPart0 &= riceParamMask; + riceParamPart0 |= (zeroCountPart0 << riceParam); + riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01]; + + pSamplesOut[i] = riceParamPart0; + + i += 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + drflac_uint32 zeroCountPart0 = 0; + drflac_uint32 zeroCountPart1 = 0; + drflac_uint32 zeroCountPart2 = 0; + drflac_uint32 zeroCountPart3 = 0; + drflac_uint32 riceParamPart0 = 0; + drflac_uint32 riceParamPart1 = 0; + drflac_uint32 riceParamPart2 = 0; + drflac_uint32 riceParamPart3 = 0; + drflac_uint32 riceParamMask; + const drflac_int32* pSamplesOutEnd; + drflac_uint32 i; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pSamplesOut != NULL); + + if (lpcOrder == 0) { + return drflac__decode_samples_with_residual__rice__scalar_zeroorder(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, coefficients, pSamplesOut); + } + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + pSamplesOutEnd = pSamplesOut + (count & ~3); + + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + while (pSamplesOut < pSamplesOutEnd) { + /* + Rice extraction. It's faster to do this one at a time against local variables than it is to use the x4 version + against an array. Not sure why, but perhaps it's making more efficient use of registers? + */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) { + return DRFLAC_FALSE; + } + + riceParamPart0 &= riceParamMask; + riceParamPart1 &= riceParamMask; + riceParamPart2 &= riceParamMask; + riceParamPart3 &= riceParamMask; + + riceParamPart0 |= (zeroCountPart0 << riceParam); + riceParamPart1 |= (zeroCountPart1 << riceParam); + riceParamPart2 |= (zeroCountPart2 << riceParam); + riceParamPart3 |= (zeroCountPart3 << riceParam); + + riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01]; + riceParamPart1 = (riceParamPart1 >> 1) ^ t[riceParamPart1 & 0x01]; + riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01]; + riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01]; + + pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 0); + pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 1); + pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 2); + pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 3); + + pSamplesOut += 4; + } + } else { + while (pSamplesOut < pSamplesOutEnd) { + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) { + return DRFLAC_FALSE; + } + + riceParamPart0 &= riceParamMask; + riceParamPart1 &= riceParamMask; + riceParamPart2 &= riceParamMask; + riceParamPart3 &= riceParamMask; + + riceParamPart0 |= (zeroCountPart0 << riceParam); + riceParamPart1 |= (zeroCountPart1 << riceParam); + riceParamPart2 |= (zeroCountPart2 << riceParam); + riceParamPart3 |= (zeroCountPart3 << riceParam); + + riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01]; + riceParamPart1 = (riceParamPart1 >> 1) ^ t[riceParamPart1 & 0x01]; + riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01]; + riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01]; + + pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0); + pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 1); + pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 2); + pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 3); + + pSamplesOut += 4; + } + } + + i = (count & ~3); + while (i < count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0)) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamPart0 &= riceParamMask; + riceParamPart0 |= (zeroCountPart0 << riceParam); + riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01]; + /*riceParamPart0 = (riceParamPart0 >> 1) ^ (~(riceParamPart0 & 0x01) + 1);*/ + + /* Sample reconstruction. */ + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 0); + } else { + pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0); + } + + i += 1; + pSamplesOut += 1; + } + + return DRFLAC_TRUE; +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE __m128i drflac__mm_packs_interleaved_epi32(__m128i a, __m128i b) +{ + __m128i r; + + /* Pack. */ + r = _mm_packs_epi32(a, b); + + /* a3a2 a1a0 b3b2 b1b0 -> a3a2 b3b2 a1a0 b1b0 */ + r = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 1, 2, 0)); + + /* a3a2 b3b2 a1a0 b1b0 -> a3b3 a2b2 a1b1 a0b0 */ + r = _mm_shufflehi_epi16(r, _MM_SHUFFLE(3, 1, 2, 0)); + r = _mm_shufflelo_epi16(r, _MM_SHUFFLE(3, 1, 2, 0)); + + return r; +} +#endif + +#if defined(DRFLAC_SUPPORT_SSE41) +static DRFLAC_INLINE __m128i drflac__mm_not_si128(__m128i a) +{ + return _mm_xor_si128(a, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())); +} + +static DRFLAC_INLINE __m128i drflac__mm_hadd_epi32(__m128i x) +{ + __m128i x64 = _mm_add_epi32(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(1, 0, 3, 2))); + __m128i x32 = _mm_shufflelo_epi16(x64, _MM_SHUFFLE(1, 0, 3, 2)); + return _mm_add_epi32(x64, x32); +} + +static DRFLAC_INLINE __m128i drflac__mm_hadd_epi64(__m128i x) +{ + return _mm_add_epi64(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(1, 0, 3, 2))); +} + +static DRFLAC_INLINE __m128i drflac__mm_srai_epi64(__m128i x, int count) +{ + /* + To simplify this we are assuming count < 32. This restriction allows us to work on a low side and a high side. The low side + is shifted with zero bits, whereas the right side is shifted with sign bits. + */ + __m128i lo = _mm_srli_epi64(x, count); + __m128i hi = _mm_srai_epi32(x, count); + + hi = _mm_and_si128(hi, _mm_set_epi32(0xFFFFFFFF, 0, 0xFFFFFFFF, 0)); /* The high part needs to have the low part cleared. */ + + return _mm_or_si128(lo, hi); +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41_32(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + int i; + drflac_uint32 riceParamMask; + drflac_int32* pDecodedSamples = pSamplesOut; + drflac_int32* pDecodedSamplesEnd = pSamplesOut + (count & ~3); + drflac_uint32 zeroCountParts0 = 0; + drflac_uint32 zeroCountParts1 = 0; + drflac_uint32 zeroCountParts2 = 0; + drflac_uint32 zeroCountParts3 = 0; + drflac_uint32 riceParamParts0 = 0; + drflac_uint32 riceParamParts1 = 0; + drflac_uint32 riceParamParts2 = 0; + drflac_uint32 riceParamParts3 = 0; + __m128i coefficients128_0; + __m128i coefficients128_4; + __m128i coefficients128_8; + __m128i samples128_0; + __m128i samples128_4; + __m128i samples128_8; + __m128i riceParamMask128; + + const drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + riceParamMask128 = _mm_set1_epi32(riceParamMask); + + /* Pre-load. */ + coefficients128_0 = _mm_setzero_si128(); + coefficients128_4 = _mm_setzero_si128(); + coefficients128_8 = _mm_setzero_si128(); + + samples128_0 = _mm_setzero_si128(); + samples128_4 = _mm_setzero_si128(); + samples128_8 = _mm_setzero_si128(); + + /* + Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than + what's available in the input buffers. It would be convenient to use a fall-through switch to do this, but this results + in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted + so I think there's opportunity for this to be simplified. + */ +#if 1 + { + int runningOrder = order; + + /* 0 - 3. */ + if (runningOrder >= 4) { + coefficients128_0 = _mm_loadu_si128((const __m128i*)(coefficients + 0)); + samples128_0 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 4)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_0 = _mm_set_epi32(0, coefficients[2], coefficients[1], coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], pSamplesOut[-2], pSamplesOut[-3], 0); break; + case 2: coefficients128_0 = _mm_set_epi32(0, 0, coefficients[1], coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], pSamplesOut[-2], 0, 0); break; + case 1: coefficients128_0 = _mm_set_epi32(0, 0, 0, coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* 4 - 7 */ + if (runningOrder >= 4) { + coefficients128_4 = _mm_loadu_si128((const __m128i*)(coefficients + 4)); + samples128_4 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 8)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_4 = _mm_set_epi32(0, coefficients[6], coefficients[5], coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], pSamplesOut[-6], pSamplesOut[-7], 0); break; + case 2: coefficients128_4 = _mm_set_epi32(0, 0, coefficients[5], coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], pSamplesOut[-6], 0, 0); break; + case 1: coefficients128_4 = _mm_set_epi32(0, 0, 0, coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* 8 - 11 */ + if (runningOrder == 4) { + coefficients128_8 = _mm_loadu_si128((const __m128i*)(coefficients + 8)); + samples128_8 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 12)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_8 = _mm_set_epi32(0, coefficients[10], coefficients[9], coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], pSamplesOut[-10], pSamplesOut[-11], 0); break; + case 2: coefficients128_8 = _mm_set_epi32(0, 0, coefficients[9], coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], pSamplesOut[-10], 0, 0); break; + case 1: coefficients128_8 = _mm_set_epi32(0, 0, 0, coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */ + coefficients128_0 = _mm_shuffle_epi32(coefficients128_0, _MM_SHUFFLE(0, 1, 2, 3)); + coefficients128_4 = _mm_shuffle_epi32(coefficients128_4, _MM_SHUFFLE(0, 1, 2, 3)); + coefficients128_8 = _mm_shuffle_epi32(coefficients128_8, _MM_SHUFFLE(0, 1, 2, 3)); + } +#else + /* This causes strict-aliasing warnings with GCC. */ + switch (order) + { + case 12: ((drflac_int32*)&coefficients128_8)[0] = coefficients[11]; ((drflac_int32*)&samples128_8)[0] = pDecodedSamples[-12]; + case 11: ((drflac_int32*)&coefficients128_8)[1] = coefficients[10]; ((drflac_int32*)&samples128_8)[1] = pDecodedSamples[-11]; + case 10: ((drflac_int32*)&coefficients128_8)[2] = coefficients[ 9]; ((drflac_int32*)&samples128_8)[2] = pDecodedSamples[-10]; + case 9: ((drflac_int32*)&coefficients128_8)[3] = coefficients[ 8]; ((drflac_int32*)&samples128_8)[3] = pDecodedSamples[- 9]; + case 8: ((drflac_int32*)&coefficients128_4)[0] = coefficients[ 7]; ((drflac_int32*)&samples128_4)[0] = pDecodedSamples[- 8]; + case 7: ((drflac_int32*)&coefficients128_4)[1] = coefficients[ 6]; ((drflac_int32*)&samples128_4)[1] = pDecodedSamples[- 7]; + case 6: ((drflac_int32*)&coefficients128_4)[2] = coefficients[ 5]; ((drflac_int32*)&samples128_4)[2] = pDecodedSamples[- 6]; + case 5: ((drflac_int32*)&coefficients128_4)[3] = coefficients[ 4]; ((drflac_int32*)&samples128_4)[3] = pDecodedSamples[- 5]; + case 4: ((drflac_int32*)&coefficients128_0)[0] = coefficients[ 3]; ((drflac_int32*)&samples128_0)[0] = pDecodedSamples[- 4]; + case 3: ((drflac_int32*)&coefficients128_0)[1] = coefficients[ 2]; ((drflac_int32*)&samples128_0)[1] = pDecodedSamples[- 3]; + case 2: ((drflac_int32*)&coefficients128_0)[2] = coefficients[ 1]; ((drflac_int32*)&samples128_0)[2] = pDecodedSamples[- 2]; + case 1: ((drflac_int32*)&coefficients128_0)[3] = coefficients[ 0]; ((drflac_int32*)&samples128_0)[3] = pDecodedSamples[- 1]; + } +#endif + + /* For this version we are doing one sample at a time. */ + while (pDecodedSamples < pDecodedSamplesEnd) { + __m128i prediction128; + __m128i zeroCountPart128; + __m128i riceParamPart128; + + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) { + return DRFLAC_FALSE; + } + + zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0); + riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0); + + riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128); + riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam)); + riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_add_epi32(drflac__mm_not_si128(_mm_and_si128(riceParamPart128, _mm_set1_epi32(0x01))), _mm_set1_epi32(0x01))); /* <-- SSE2 compatible */ + /*riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, _mm_set1_epi32(0x01)), _mm_set1_epi32(0xFFFFFFFF)));*/ /* <-- Only supported from SSE4.1 and is slower in my testing... */ + + if (order <= 4) { + for (i = 0; i < 4; i += 1) { + prediction128 = _mm_mullo_epi32(coefficients128_0, samples128_0); + + /* Horizontal add and shift. */ + prediction128 = drflac__mm_hadd_epi32(prediction128); + prediction128 = _mm_srai_epi32(prediction128, shift); + prediction128 = _mm_add_epi32(riceParamPart128, prediction128); + + samples128_0 = _mm_alignr_epi8(prediction128, samples128_0, 4); + riceParamPart128 = _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128, 4); + } + } else if (order <= 8) { + for (i = 0; i < 4; i += 1) { + prediction128 = _mm_mullo_epi32(coefficients128_4, samples128_4); + prediction128 = _mm_add_epi32(prediction128, _mm_mullo_epi32(coefficients128_0, samples128_0)); + + /* Horizontal add and shift. */ + prediction128 = drflac__mm_hadd_epi32(prediction128); + prediction128 = _mm_srai_epi32(prediction128, shift); + prediction128 = _mm_add_epi32(riceParamPart128, prediction128); + + samples128_4 = _mm_alignr_epi8(samples128_0, samples128_4, 4); + samples128_0 = _mm_alignr_epi8(prediction128, samples128_0, 4); + riceParamPart128 = _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128, 4); + } + } else { + for (i = 0; i < 4; i += 1) { + prediction128 = _mm_mullo_epi32(coefficients128_8, samples128_8); + prediction128 = _mm_add_epi32(prediction128, _mm_mullo_epi32(coefficients128_4, samples128_4)); + prediction128 = _mm_add_epi32(prediction128, _mm_mullo_epi32(coefficients128_0, samples128_0)); + + /* Horizontal add and shift. */ + prediction128 = drflac__mm_hadd_epi32(prediction128); + prediction128 = _mm_srai_epi32(prediction128, shift); + prediction128 = _mm_add_epi32(riceParamPart128, prediction128); + + samples128_8 = _mm_alignr_epi8(samples128_4, samples128_8, 4); + samples128_4 = _mm_alignr_epi8(samples128_0, samples128_4, 4); + samples128_0 = _mm_alignr_epi8(prediction128, samples128_0, 4); + riceParamPart128 = _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128, 4); + } + } + + /* We store samples in groups of 4. */ + _mm_storeu_si128((__m128i*)pDecodedSamples, samples128_0); + pDecodedSamples += 4; + } + + /* Make sure we process the last few samples. */ + i = (count & ~3); + while (i < (int)count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0)) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamParts0 &= riceParamMask; + riceParamParts0 |= (zeroCountParts0 << riceParam); + riceParamParts0 = (riceParamParts0 >> 1) ^ t[riceParamParts0 & 0x01]; + + /* Sample reconstruction. */ + pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_32(order, shift, coefficients, pDecodedSamples); + + i += 1; + pDecodedSamples += 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41_64(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + int i; + drflac_uint32 riceParamMask; + drflac_int32* pDecodedSamples = pSamplesOut; + drflac_int32* pDecodedSamplesEnd = pSamplesOut + (count & ~3); + drflac_uint32 zeroCountParts0 = 0; + drflac_uint32 zeroCountParts1 = 0; + drflac_uint32 zeroCountParts2 = 0; + drflac_uint32 zeroCountParts3 = 0; + drflac_uint32 riceParamParts0 = 0; + drflac_uint32 riceParamParts1 = 0; + drflac_uint32 riceParamParts2 = 0; + drflac_uint32 riceParamParts3 = 0; + __m128i coefficients128_0; + __m128i coefficients128_4; + __m128i coefficients128_8; + __m128i samples128_0; + __m128i samples128_4; + __m128i samples128_8; + __m128i prediction128; + __m128i riceParamMask128; + + const drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + + DRFLAC_ASSERT(order <= 12); + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + riceParamMask128 = _mm_set1_epi32(riceParamMask); + + prediction128 = _mm_setzero_si128(); + + /* Pre-load. */ + coefficients128_0 = _mm_setzero_si128(); + coefficients128_4 = _mm_setzero_si128(); + coefficients128_8 = _mm_setzero_si128(); + + samples128_0 = _mm_setzero_si128(); + samples128_4 = _mm_setzero_si128(); + samples128_8 = _mm_setzero_si128(); + +#if 1 + { + int runningOrder = order; + + /* 0 - 3. */ + if (runningOrder >= 4) { + coefficients128_0 = _mm_loadu_si128((const __m128i*)(coefficients + 0)); + samples128_0 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 4)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_0 = _mm_set_epi32(0, coefficients[2], coefficients[1], coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], pSamplesOut[-2], pSamplesOut[-3], 0); break; + case 2: coefficients128_0 = _mm_set_epi32(0, 0, coefficients[1], coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], pSamplesOut[-2], 0, 0); break; + case 1: coefficients128_0 = _mm_set_epi32(0, 0, 0, coefficients[0]); samples128_0 = _mm_set_epi32(pSamplesOut[-1], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* 4 - 7 */ + if (runningOrder >= 4) { + coefficients128_4 = _mm_loadu_si128((const __m128i*)(coefficients + 4)); + samples128_4 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 8)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_4 = _mm_set_epi32(0, coefficients[6], coefficients[5], coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], pSamplesOut[-6], pSamplesOut[-7], 0); break; + case 2: coefficients128_4 = _mm_set_epi32(0, 0, coefficients[5], coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], pSamplesOut[-6], 0, 0); break; + case 1: coefficients128_4 = _mm_set_epi32(0, 0, 0, coefficients[4]); samples128_4 = _mm_set_epi32(pSamplesOut[-5], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* 8 - 11 */ + if (runningOrder == 4) { + coefficients128_8 = _mm_loadu_si128((const __m128i*)(coefficients + 8)); + samples128_8 = _mm_loadu_si128((const __m128i*)(pSamplesOut - 12)); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: coefficients128_8 = _mm_set_epi32(0, coefficients[10], coefficients[9], coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], pSamplesOut[-10], pSamplesOut[-11], 0); break; + case 2: coefficients128_8 = _mm_set_epi32(0, 0, coefficients[9], coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], pSamplesOut[-10], 0, 0); break; + case 1: coefficients128_8 = _mm_set_epi32(0, 0, 0, coefficients[8]); samples128_8 = _mm_set_epi32(pSamplesOut[-9], 0, 0, 0); break; + } + runningOrder = 0; + } + + /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */ + coefficients128_0 = _mm_shuffle_epi32(coefficients128_0, _MM_SHUFFLE(0, 1, 2, 3)); + coefficients128_4 = _mm_shuffle_epi32(coefficients128_4, _MM_SHUFFLE(0, 1, 2, 3)); + coefficients128_8 = _mm_shuffle_epi32(coefficients128_8, _MM_SHUFFLE(0, 1, 2, 3)); + } +#else + switch (order) + { + case 12: ((drflac_int32*)&coefficients128_8)[0] = coefficients[11]; ((drflac_int32*)&samples128_8)[0] = pDecodedSamples[-12]; + case 11: ((drflac_int32*)&coefficients128_8)[1] = coefficients[10]; ((drflac_int32*)&samples128_8)[1] = pDecodedSamples[-11]; + case 10: ((drflac_int32*)&coefficients128_8)[2] = coefficients[ 9]; ((drflac_int32*)&samples128_8)[2] = pDecodedSamples[-10]; + case 9: ((drflac_int32*)&coefficients128_8)[3] = coefficients[ 8]; ((drflac_int32*)&samples128_8)[3] = pDecodedSamples[- 9]; + case 8: ((drflac_int32*)&coefficients128_4)[0] = coefficients[ 7]; ((drflac_int32*)&samples128_4)[0] = pDecodedSamples[- 8]; + case 7: ((drflac_int32*)&coefficients128_4)[1] = coefficients[ 6]; ((drflac_int32*)&samples128_4)[1] = pDecodedSamples[- 7]; + case 6: ((drflac_int32*)&coefficients128_4)[2] = coefficients[ 5]; ((drflac_int32*)&samples128_4)[2] = pDecodedSamples[- 6]; + case 5: ((drflac_int32*)&coefficients128_4)[3] = coefficients[ 4]; ((drflac_int32*)&samples128_4)[3] = pDecodedSamples[- 5]; + case 4: ((drflac_int32*)&coefficients128_0)[0] = coefficients[ 3]; ((drflac_int32*)&samples128_0)[0] = pDecodedSamples[- 4]; + case 3: ((drflac_int32*)&coefficients128_0)[1] = coefficients[ 2]; ((drflac_int32*)&samples128_0)[1] = pDecodedSamples[- 3]; + case 2: ((drflac_int32*)&coefficients128_0)[2] = coefficients[ 1]; ((drflac_int32*)&samples128_0)[2] = pDecodedSamples[- 2]; + case 1: ((drflac_int32*)&coefficients128_0)[3] = coefficients[ 0]; ((drflac_int32*)&samples128_0)[3] = pDecodedSamples[- 1]; + } +#endif + + /* For this version we are doing one sample at a time. */ + while (pDecodedSamples < pDecodedSamplesEnd) { + __m128i zeroCountPart128; + __m128i riceParamPart128; + + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) { + return DRFLAC_FALSE; + } + + zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0); + riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0); + + riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128); + riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam)); + riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_add_epi32(drflac__mm_not_si128(_mm_and_si128(riceParamPart128, _mm_set1_epi32(1))), _mm_set1_epi32(1))); + + for (i = 0; i < 4; i += 1) { + prediction128 = _mm_xor_si128(prediction128, prediction128); /* Reset to 0. */ + + switch (order) + { + case 12: + case 11: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_8, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_8, _MM_SHUFFLE(1, 1, 0, 0)))); + case 10: + case 9: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_8, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_8, _MM_SHUFFLE(3, 3, 2, 2)))); + case 8: + case 7: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_4, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_4, _MM_SHUFFLE(1, 1, 0, 0)))); + case 6: + case 5: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_4, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_4, _MM_SHUFFLE(3, 3, 2, 2)))); + case 4: + case 3: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_0, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_0, _MM_SHUFFLE(1, 1, 0, 0)))); + case 2: + case 1: prediction128 = _mm_add_epi64(prediction128, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_0, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_0, _MM_SHUFFLE(3, 3, 2, 2)))); + } + + /* Horizontal add and shift. */ + prediction128 = drflac__mm_hadd_epi64(prediction128); + prediction128 = drflac__mm_srai_epi64(prediction128, shift); + prediction128 = _mm_add_epi32(riceParamPart128, prediction128); + + /* Our value should be sitting in prediction128[0]. We need to combine this with our SSE samples. */ + samples128_8 = _mm_alignr_epi8(samples128_4, samples128_8, 4); + samples128_4 = _mm_alignr_epi8(samples128_0, samples128_4, 4); + samples128_0 = _mm_alignr_epi8(prediction128, samples128_0, 4); + + /* Slide our rice parameter down so that the value in position 0 contains the next one to process. */ + riceParamPart128 = _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128, 4); + } + + /* We store samples in groups of 4. */ + _mm_storeu_si128((__m128i*)pDecodedSamples, samples128_0); + pDecodedSamples += 4; + } + + /* Make sure we process the last few samples. */ + i = (count & ~3); + while (i < (int)count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0)) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamParts0 &= riceParamMask; + riceParamParts0 |= (zeroCountParts0 << riceParam); + riceParamParts0 = (riceParamParts0 >> 1) ^ t[riceParamParts0 & 0x01]; + + /* Sample reconstruction. */ + pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_64(order, shift, coefficients, pDecodedSamples); + + i += 1; + pDecodedSamples += 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pSamplesOut != NULL); + + /* In my testing the order is rarely > 12, so in this case I'm going to simplify the SSE implementation by only handling order <= 12. */ + if (lpcOrder > 0 && lpcOrder <= 12) { + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + return drflac__decode_samples_with_residual__rice__sse41_64(bs, count, riceParam, lpcOrder, lpcShift, coefficients, pSamplesOut); + } else { + return drflac__decode_samples_with_residual__rice__sse41_32(bs, count, riceParam, lpcOrder, lpcShift, coefficients, pSamplesOut); + } + } else { + return drflac__decode_samples_with_residual__rice__scalar(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac__vst2q_s32(drflac_int32* p, int32x4x2_t x) +{ + vst1q_s32(p+0, x.val[0]); + vst1q_s32(p+4, x.val[1]); +} + +static DRFLAC_INLINE void drflac__vst2q_u32(drflac_uint32* p, uint32x4x2_t x) +{ + vst1q_u32(p+0, x.val[0]); + vst1q_u32(p+4, x.val[1]); +} + +static DRFLAC_INLINE void drflac__vst2q_f32(float* p, float32x4x2_t x) +{ + vst1q_f32(p+0, x.val[0]); + vst1q_f32(p+4, x.val[1]); +} + +static DRFLAC_INLINE void drflac__vst2q_s16(drflac_int16* p, int16x4x2_t x) +{ + vst1q_s16(p, vcombine_s16(x.val[0], x.val[1])); +} + +static DRFLAC_INLINE void drflac__vst2q_u16(drflac_uint16* p, uint16x4x2_t x) +{ + vst1q_u16(p, vcombine_u16(x.val[0], x.val[1])); +} + +static DRFLAC_INLINE int32x4_t drflac__vdupq_n_s32x4(drflac_int32 x3, drflac_int32 x2, drflac_int32 x1, drflac_int32 x0) +{ + drflac_int32 x[4]; + x[3] = x3; + x[2] = x2; + x[1] = x1; + x[0] = x0; + return vld1q_s32(x); +} + +static DRFLAC_INLINE int32x4_t drflac__valignrq_s32_1(int32x4_t a, int32x4_t b) +{ + /* Equivalent to SSE's _mm_alignr_epi8(a, b, 4) */ + + /* Reference */ + /*return drflac__vdupq_n_s32x4( + vgetq_lane_s32(a, 0), + vgetq_lane_s32(b, 3), + vgetq_lane_s32(b, 2), + vgetq_lane_s32(b, 1) + );*/ + + return vextq_s32(b, a, 1); +} + +static DRFLAC_INLINE uint32x4_t drflac__valignrq_u32_1(uint32x4_t a, uint32x4_t b) +{ + /* Equivalent to SSE's _mm_alignr_epi8(a, b, 4) */ + + /* Reference */ + /*return drflac__vdupq_n_s32x4( + vgetq_lane_s32(a, 0), + vgetq_lane_s32(b, 3), + vgetq_lane_s32(b, 2), + vgetq_lane_s32(b, 1) + );*/ + + return vextq_u32(b, a, 1); +} + +static DRFLAC_INLINE int32x2_t drflac__vhaddq_s32(int32x4_t x) +{ + /* The sum must end up in position 0. */ + + /* Reference */ + /*return vdupq_n_s32( + vgetq_lane_s32(x, 3) + + vgetq_lane_s32(x, 2) + + vgetq_lane_s32(x, 1) + + vgetq_lane_s32(x, 0) + );*/ + + int32x2_t r = vadd_s32(vget_high_s32(x), vget_low_s32(x)); + return vpadd_s32(r, r); +} + +static DRFLAC_INLINE int64x1_t drflac__vhaddq_s64(int64x2_t x) +{ + return vadd_s64(vget_high_s64(x), vget_low_s64(x)); +} + +static DRFLAC_INLINE int32x4_t drflac__vrevq_s32(int32x4_t x) +{ + /* Reference */ + /*return drflac__vdupq_n_s32x4( + vgetq_lane_s32(x, 0), + vgetq_lane_s32(x, 1), + vgetq_lane_s32(x, 2), + vgetq_lane_s32(x, 3) + );*/ + + return vrev64q_s32(vcombine_s32(vget_high_s32(x), vget_low_s32(x))); +} + +static DRFLAC_INLINE int32x4_t drflac__vnotq_s32(int32x4_t x) +{ + return veorq_s32(x, vdupq_n_s32(0xFFFFFFFF)); +} + +static DRFLAC_INLINE uint32x4_t drflac__vnotq_u32(uint32x4_t x) +{ + return veorq_u32(x, vdupq_n_u32(0xFFFFFFFF)); +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__neon_32(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + int i; + drflac_uint32 riceParamMask; + drflac_int32* pDecodedSamples = pSamplesOut; + drflac_int32* pDecodedSamplesEnd = pSamplesOut + (count & ~3); + drflac_uint32 zeroCountParts[4]; + drflac_uint32 riceParamParts[4]; + int32x4_t coefficients128_0; + int32x4_t coefficients128_4; + int32x4_t coefficients128_8; + int32x4_t samples128_0; + int32x4_t samples128_4; + int32x4_t samples128_8; + uint32x4_t riceParamMask128; + int32x4_t riceParam128; + int32x2_t shift64; + uint32x4_t one128; + + const drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + riceParamMask128 = vdupq_n_u32(riceParamMask); + + riceParam128 = vdupq_n_s32(riceParam); + shift64 = vdup_n_s32(-shift); /* Negate the shift because we'll be doing a variable shift using vshlq_s32(). */ + one128 = vdupq_n_u32(1); + + /* + Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than + what's available in the input buffers. It would be conenient to use a fall-through switch to do this, but this results + in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted + so I think there's opportunity for this to be simplified. + */ + { + int runningOrder = order; + drflac_int32 tempC[4] = {0, 0, 0, 0}; + drflac_int32 tempS[4] = {0, 0, 0, 0}; + + /* 0 - 3. */ + if (runningOrder >= 4) { + coefficients128_0 = vld1q_s32(coefficients + 0); + samples128_0 = vld1q_s32(pSamplesOut - 4); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[2]; tempS[1] = pSamplesOut[-3]; /* fallthrough */ + case 2: tempC[1] = coefficients[1]; tempS[2] = pSamplesOut[-2]; /* fallthrough */ + case 1: tempC[0] = coefficients[0]; tempS[3] = pSamplesOut[-1]; /* fallthrough */ + } + + coefficients128_0 = vld1q_s32(tempC); + samples128_0 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* 4 - 7 */ + if (runningOrder >= 4) { + coefficients128_4 = vld1q_s32(coefficients + 4); + samples128_4 = vld1q_s32(pSamplesOut - 8); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[6]; tempS[1] = pSamplesOut[-7]; /* fallthrough */ + case 2: tempC[1] = coefficients[5]; tempS[2] = pSamplesOut[-6]; /* fallthrough */ + case 1: tempC[0] = coefficients[4]; tempS[3] = pSamplesOut[-5]; /* fallthrough */ + } + + coefficients128_4 = vld1q_s32(tempC); + samples128_4 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* 8 - 11 */ + if (runningOrder == 4) { + coefficients128_8 = vld1q_s32(coefficients + 8); + samples128_8 = vld1q_s32(pSamplesOut - 12); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[10]; tempS[1] = pSamplesOut[-11]; /* fallthrough */ + case 2: tempC[1] = coefficients[ 9]; tempS[2] = pSamplesOut[-10]; /* fallthrough */ + case 1: tempC[0] = coefficients[ 8]; tempS[3] = pSamplesOut[- 9]; /* fallthrough */ + } + + coefficients128_8 = vld1q_s32(tempC); + samples128_8 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */ + coefficients128_0 = drflac__vrevq_s32(coefficients128_0); + coefficients128_4 = drflac__vrevq_s32(coefficients128_4); + coefficients128_8 = drflac__vrevq_s32(coefficients128_8); + } + + /* For this version we are doing one sample at a time. */ + while (pDecodedSamples < pDecodedSamplesEnd) { + int32x4_t prediction128; + int32x2_t prediction64; + uint32x4_t zeroCountPart128; + uint32x4_t riceParamPart128; + + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[0], &riceParamParts[0]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[1], &riceParamParts[1]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[2], &riceParamParts[2]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[3], &riceParamParts[3])) { + return DRFLAC_FALSE; + } + + zeroCountPart128 = vld1q_u32(zeroCountParts); + riceParamPart128 = vld1q_u32(riceParamParts); + + riceParamPart128 = vandq_u32(riceParamPart128, riceParamMask128); + riceParamPart128 = vorrq_u32(riceParamPart128, vshlq_u32(zeroCountPart128, riceParam128)); + riceParamPart128 = veorq_u32(vshrq_n_u32(riceParamPart128, 1), vaddq_u32(drflac__vnotq_u32(vandq_u32(riceParamPart128, one128)), one128)); + + if (order <= 4) { + for (i = 0; i < 4; i += 1) { + prediction128 = vmulq_s32(coefficients128_0, samples128_0); + + /* Horizontal add and shift. */ + prediction64 = drflac__vhaddq_s32(prediction128); + prediction64 = vshl_s32(prediction64, shift64); + prediction64 = vadd_s32(prediction64, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128))); + + samples128_0 = drflac__valignrq_s32_1(vcombine_s32(prediction64, vdup_n_s32(0)), samples128_0); + riceParamPart128 = drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128); + } + } else if (order <= 8) { + for (i = 0; i < 4; i += 1) { + prediction128 = vmulq_s32(coefficients128_4, samples128_4); + prediction128 = vmlaq_s32(prediction128, coefficients128_0, samples128_0); + + /* Horizontal add and shift. */ + prediction64 = drflac__vhaddq_s32(prediction128); + prediction64 = vshl_s32(prediction64, shift64); + prediction64 = vadd_s32(prediction64, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128))); + + samples128_4 = drflac__valignrq_s32_1(samples128_0, samples128_4); + samples128_0 = drflac__valignrq_s32_1(vcombine_s32(prediction64, vdup_n_s32(0)), samples128_0); + riceParamPart128 = drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128); + } + } else { + for (i = 0; i < 4; i += 1) { + prediction128 = vmulq_s32(coefficients128_8, samples128_8); + prediction128 = vmlaq_s32(prediction128, coefficients128_4, samples128_4); + prediction128 = vmlaq_s32(prediction128, coefficients128_0, samples128_0); + + /* Horizontal add and shift. */ + prediction64 = drflac__vhaddq_s32(prediction128); + prediction64 = vshl_s32(prediction64, shift64); + prediction64 = vadd_s32(prediction64, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128))); + + samples128_8 = drflac__valignrq_s32_1(samples128_4, samples128_8); + samples128_4 = drflac__valignrq_s32_1(samples128_0, samples128_4); + samples128_0 = drflac__valignrq_s32_1(vcombine_s32(prediction64, vdup_n_s32(0)), samples128_0); + riceParamPart128 = drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128); + } + } + + /* We store samples in groups of 4. */ + vst1q_s32(pDecodedSamples, samples128_0); + pDecodedSamples += 4; + } + + /* Make sure we process the last few samples. */ + i = (count & ~3); + while (i < (int)count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[0], &riceParamParts[0])) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamParts[0] &= riceParamMask; + riceParamParts[0] |= (zeroCountParts[0] << riceParam); + riceParamParts[0] = (riceParamParts[0] >> 1) ^ t[riceParamParts[0] & 0x01]; + + /* Sample reconstruction. */ + pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_32(order, shift, coefficients, pDecodedSamples); + + i += 1; + pDecodedSamples += 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__neon_64(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + int i; + drflac_uint32 riceParamMask; + drflac_int32* pDecodedSamples = pSamplesOut; + drflac_int32* pDecodedSamplesEnd = pSamplesOut + (count & ~3); + drflac_uint32 zeroCountParts[4]; + drflac_uint32 riceParamParts[4]; + int32x4_t coefficients128_0; + int32x4_t coefficients128_4; + int32x4_t coefficients128_8; + int32x4_t samples128_0; + int32x4_t samples128_4; + int32x4_t samples128_8; + uint32x4_t riceParamMask128; + int32x4_t riceParam128; + int64x1_t shift64; + uint32x4_t one128; + int64x2_t prediction128 = { 0 }; + uint32x4_t zeroCountPart128; + uint32x4_t riceParamPart128; + + const drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF}; + + riceParamMask = (drflac_uint32)~((~0UL) << riceParam); + riceParamMask128 = vdupq_n_u32(riceParamMask); + + riceParam128 = vdupq_n_s32(riceParam); + shift64 = vdup_n_s64(-shift); /* Negate the shift because we'll be doing a variable shift using vshlq_s32(). */ + one128 = vdupq_n_u32(1); + + /* + Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than + what's available in the input buffers. It would be convenient to use a fall-through switch to do this, but this results + in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted + so I think there's opportunity for this to be simplified. + */ + { + int runningOrder = order; + drflac_int32 tempC[4] = {0, 0, 0, 0}; + drflac_int32 tempS[4] = {0, 0, 0, 0}; + + /* 0 - 3. */ + if (runningOrder >= 4) { + coefficients128_0 = vld1q_s32(coefficients + 0); + samples128_0 = vld1q_s32(pSamplesOut - 4); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[2]; tempS[1] = pSamplesOut[-3]; /* fallthrough */ + case 2: tempC[1] = coefficients[1]; tempS[2] = pSamplesOut[-2]; /* fallthrough */ + case 1: tempC[0] = coefficients[0]; tempS[3] = pSamplesOut[-1]; /* fallthrough */ + } + + coefficients128_0 = vld1q_s32(tempC); + samples128_0 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* 4 - 7 */ + if (runningOrder >= 4) { + coefficients128_4 = vld1q_s32(coefficients + 4); + samples128_4 = vld1q_s32(pSamplesOut - 8); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[6]; tempS[1] = pSamplesOut[-7]; /* fallthrough */ + case 2: tempC[1] = coefficients[5]; tempS[2] = pSamplesOut[-6]; /* fallthrough */ + case 1: tempC[0] = coefficients[4]; tempS[3] = pSamplesOut[-5]; /* fallthrough */ + } + + coefficients128_4 = vld1q_s32(tempC); + samples128_4 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* 8 - 11 */ + if (runningOrder == 4) { + coefficients128_8 = vld1q_s32(coefficients + 8); + samples128_8 = vld1q_s32(pSamplesOut - 12); + runningOrder -= 4; + } else { + switch (runningOrder) { + case 3: tempC[2] = coefficients[10]; tempS[1] = pSamplesOut[-11]; /* fallthrough */ + case 2: tempC[1] = coefficients[ 9]; tempS[2] = pSamplesOut[-10]; /* fallthrough */ + case 1: tempC[0] = coefficients[ 8]; tempS[3] = pSamplesOut[- 9]; /* fallthrough */ + } + + coefficients128_8 = vld1q_s32(tempC); + samples128_8 = vld1q_s32(tempS); + runningOrder = 0; + } + + /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */ + coefficients128_0 = drflac__vrevq_s32(coefficients128_0); + coefficients128_4 = drflac__vrevq_s32(coefficients128_4); + coefficients128_8 = drflac__vrevq_s32(coefficients128_8); + } + + /* For this version we are doing one sample at a time. */ + while (pDecodedSamples < pDecodedSamplesEnd) { + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[0], &riceParamParts[0]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[1], &riceParamParts[1]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[2], &riceParamParts[2]) || + !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[3], &riceParamParts[3])) { + return DRFLAC_FALSE; + } + + zeroCountPart128 = vld1q_u32(zeroCountParts); + riceParamPart128 = vld1q_u32(riceParamParts); + + riceParamPart128 = vandq_u32(riceParamPart128, riceParamMask128); + riceParamPart128 = vorrq_u32(riceParamPart128, vshlq_u32(zeroCountPart128, riceParam128)); + riceParamPart128 = veorq_u32(vshrq_n_u32(riceParamPart128, 1), vaddq_u32(drflac__vnotq_u32(vandq_u32(riceParamPart128, one128)), one128)); + + for (i = 0; i < 4; i += 1) { + int64x1_t prediction64; + + prediction128 = veorq_s64(prediction128, prediction128); /* Reset to 0. */ + switch (order) + { + case 12: + case 11: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_low_s32(coefficients128_8), vget_low_s32(samples128_8))); + case 10: + case 9: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_high_s32(coefficients128_8), vget_high_s32(samples128_8))); + case 8: + case 7: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_low_s32(coefficients128_4), vget_low_s32(samples128_4))); + case 6: + case 5: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_high_s32(coefficients128_4), vget_high_s32(samples128_4))); + case 4: + case 3: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_low_s32(coefficients128_0), vget_low_s32(samples128_0))); + case 2: + case 1: prediction128 = vaddq_s64(prediction128, vmull_s32(vget_high_s32(coefficients128_0), vget_high_s32(samples128_0))); + } + + /* Horizontal add and shift. */ + prediction64 = drflac__vhaddq_s64(prediction128); + prediction64 = vshl_s64(prediction64, shift64); + prediction64 = vadd_s64(prediction64, vdup_n_s64(vgetq_lane_u32(riceParamPart128, 0))); + + /* Our value should be sitting in prediction64[0]. We need to combine this with our SSE samples. */ + samples128_8 = drflac__valignrq_s32_1(samples128_4, samples128_8); + samples128_4 = drflac__valignrq_s32_1(samples128_0, samples128_4); + samples128_0 = drflac__valignrq_s32_1(vcombine_s32(vreinterpret_s32_s64(prediction64), vdup_n_s32(0)), samples128_0); + + /* Slide our rice parameter down so that the value in position 0 contains the next one to process. */ + riceParamPart128 = drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128); + } + + /* We store samples in groups of 4. */ + vst1q_s32(pDecodedSamples, samples128_0); + pDecodedSamples += 4; + } + + /* Make sure we process the last few samples. */ + i = (count & ~3); + while (i < (int)count) { + /* Rice extraction. */ + if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts[0], &riceParamParts[0])) { + return DRFLAC_FALSE; + } + + /* Rice reconstruction. */ + riceParamParts[0] &= riceParamMask; + riceParamParts[0] |= (zeroCountParts[0] << riceParam); + riceParamParts[0] = (riceParamParts[0] >> 1) ^ t[riceParamParts[0] & 0x01]; + + /* Sample reconstruction. */ + pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_64(order, shift, coefficients, pDecodedSamples); + + i += 1; + pDecodedSamples += 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples_with_residual__rice__neon(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(pSamplesOut != NULL); + + /* In my testing the order is rarely > 12, so in this case I'm going to simplify the NEON implementation by only handling order <= 12. */ + if (lpcOrder > 0 && lpcOrder <= 12) { + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + return drflac__decode_samples_with_residual__rice__neon_64(bs, count, riceParam, lpcOrder, lpcShift, coefficients, pSamplesOut); + } else { + return drflac__decode_samples_with_residual__rice__neon_32(bs, count, riceParam, lpcOrder, lpcShift, coefficients, pSamplesOut); + } + } else { + return drflac__decode_samples_with_residual__rice__scalar(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + } +} +#endif + +static drflac_bool32 drflac__decode_samples_with_residual__rice(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ +#if defined(DRFLAC_SUPPORT_SSE41) + if (drflac__gIsSSE41Supported) { + return drflac__decode_samples_with_residual__rice__sse41(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported) { + return drflac__decode_samples_with_residual__rice__neon(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + } else +#endif + { + /* Scalar fallback. */ + #if 0 + return drflac__decode_samples_with_residual__rice__reference(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + #else + return drflac__decode_samples_with_residual__rice__scalar(bs, bitsPerSample, count, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pSamplesOut); + #endif + } +} + +/* Reads and seeks past a string of residual values as Rice codes. The decoder should be sitting on the first bit of the Rice codes. */ +static drflac_bool32 drflac__read_and_seek_residual__rice(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam) +{ + drflac_uint32 i; + + DRFLAC_ASSERT(bs != NULL); + + for (i = 0; i < count; ++i) { + if (!drflac__seek_rice_parts(bs, riceParam)) { + return DRFLAC_FALSE; + } + } + + return DRFLAC_TRUE; +} + +#if defined(__clang__) +__attribute__((no_sanitize("signed-integer-overflow"))) +#endif +static drflac_bool32 drflac__decode_samples_with_residual__unencoded(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 unencodedBitsPerSample, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pSamplesOut) +{ + drflac_uint32 i; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(unencodedBitsPerSample <= 31); /* <-- unencodedBitsPerSample is a 5 bit number, so cannot exceed 31. */ + DRFLAC_ASSERT(pSamplesOut != NULL); + + for (i = 0; i < count; ++i) { + if (unencodedBitsPerSample > 0) { + if (!drflac__read_int32(bs, unencodedBitsPerSample, pSamplesOut + i)) { + return DRFLAC_FALSE; + } + } else { + pSamplesOut[i] = 0; + } + + if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) { + pSamplesOut[i] += drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + i); + } else { + pSamplesOut[i] += drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + i); + } + } + + return DRFLAC_TRUE; +} + + +/* +Reads and decodes the residual for the sub-frame the decoder is currently sitting on. This function should be called +when the decoder is sitting at the very start of the RESIDUAL block. The first residuals will be ignored. The + and parameters are used to determine how many residual values need to be decoded. +*/ +static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 blockSize, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pDecodedSamples) +{ + drflac_uint8 residualMethod; + drflac_uint8 partitionOrder; + drflac_uint32 samplesInPartition; + drflac_uint32 partitionsRemaining; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(blockSize != 0); + DRFLAC_ASSERT(pDecodedSamples != NULL); /* <-- Should we allow NULL, in which case we just seek past the residual rather than do a full decode? */ + + if (!drflac__read_uint8(bs, 2, &residualMethod)) { + return DRFLAC_FALSE; + } + + if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) { + return DRFLAC_FALSE; /* Unknown or unsupported residual coding method. */ + } + + /* Ignore the first values. */ + pDecodedSamples += lpcOrder; + + if (!drflac__read_uint8(bs, 4, &partitionOrder)) { + return DRFLAC_FALSE; + } + + /* + From the FLAC spec: + The Rice partition order in a Rice-coded residual section must be less than or equal to 8. + */ + if (partitionOrder > 8) { + return DRFLAC_FALSE; + } + + /* Validation check. */ + if ((blockSize / (1 << partitionOrder)) < lpcOrder) { + return DRFLAC_FALSE; + } + + samplesInPartition = (blockSize / (1 << partitionOrder)) - lpcOrder; + partitionsRemaining = (1 << partitionOrder); + for (;;) { + drflac_uint8 riceParam = 0; + if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) { + if (!drflac__read_uint8(bs, 4, &riceParam)) { + return DRFLAC_FALSE; + } + if (riceParam == 15) { + riceParam = 0xFF; + } + } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) { + if (!drflac__read_uint8(bs, 5, &riceParam)) { + return DRFLAC_FALSE; + } + if (riceParam == 31) { + riceParam = 0xFF; + } + } + + if (riceParam != 0xFF) { + if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamples)) { + return DRFLAC_FALSE; + } + } else { + drflac_uint8 unencodedBitsPerSample = 0; + if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) { + return DRFLAC_FALSE; + } + + if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamples)) { + return DRFLAC_FALSE; + } + } + + pDecodedSamples += samplesInPartition; + + if (partitionsRemaining == 1) { + break; + } + + partitionsRemaining -= 1; + + if (partitionOrder != 0) { + samplesInPartition = blockSize / (1 << partitionOrder); + } + } + + return DRFLAC_TRUE; +} + +/* +Reads and seeks past the residual for the sub-frame the decoder is currently sitting on. This function should be called +when the decoder is sitting at the very start of the RESIDUAL block. The first residuals will be set to 0. The + and parameters are used to determine how many residual values need to be decoded. +*/ +static drflac_bool32 drflac__read_and_seek_residual(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 order) +{ + drflac_uint8 residualMethod; + drflac_uint8 partitionOrder; + drflac_uint32 samplesInPartition; + drflac_uint32 partitionsRemaining; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(blockSize != 0); + + if (!drflac__read_uint8(bs, 2, &residualMethod)) { + return DRFLAC_FALSE; + } + + if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) { + return DRFLAC_FALSE; /* Unknown or unsupported residual coding method. */ + } + + if (!drflac__read_uint8(bs, 4, &partitionOrder)) { + return DRFLAC_FALSE; + } + + /* + From the FLAC spec: + The Rice partition order in a Rice-coded residual section must be less than or equal to 8. + */ + if (partitionOrder > 8) { + return DRFLAC_FALSE; + } + + /* Validation check. */ + if ((blockSize / (1 << partitionOrder)) <= order) { + return DRFLAC_FALSE; + } + + samplesInPartition = (blockSize / (1 << partitionOrder)) - order; + partitionsRemaining = (1 << partitionOrder); + for (;;) + { + drflac_uint8 riceParam = 0; + if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) { + if (!drflac__read_uint8(bs, 4, &riceParam)) { + return DRFLAC_FALSE; + } + if (riceParam == 15) { + riceParam = 0xFF; + } + } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) { + if (!drflac__read_uint8(bs, 5, &riceParam)) { + return DRFLAC_FALSE; + } + if (riceParam == 31) { + riceParam = 0xFF; + } + } + + if (riceParam != 0xFF) { + if (!drflac__read_and_seek_residual__rice(bs, samplesInPartition, riceParam)) { + return DRFLAC_FALSE; + } + } else { + drflac_uint8 unencodedBitsPerSample = 0; + if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) { + return DRFLAC_FALSE; + } + + if (!drflac__seek_bits(bs, unencodedBitsPerSample * samplesInPartition)) { + return DRFLAC_FALSE; + } + } + + + if (partitionsRemaining == 1) { + break; + } + + partitionsRemaining -= 1; + samplesInPartition = blockSize / (1 << partitionOrder); + } + + return DRFLAC_TRUE; +} + + +static drflac_bool32 drflac__decode_samples__constant(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamples) +{ + drflac_uint32 i; + + /* Only a single sample needs to be decoded here. */ + drflac_int32 sample; + if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) { + return DRFLAC_FALSE; + } + + /* + We don't really need to expand this, but it does simplify the process of reading samples. If this becomes a performance issue (unlikely) + we'll want to look at a more efficient way. + */ + for (i = 0; i < blockSize; ++i) { + pDecodedSamples[i] = sample; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples__verbatim(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamples) +{ + drflac_uint32 i; + + for (i = 0; i < blockSize; ++i) { + drflac_int32 sample; + if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) { + return DRFLAC_FALSE; + } + + pDecodedSamples[i] = sample; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples__fixed(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples) +{ + drflac_uint32 i; + + static drflac_int32 lpcCoefficientsTable[5][4] = { + {0, 0, 0, 0}, + {1, 0, 0, 0}, + {2, -1, 0, 0}, + {3, -3, 1, 0}, + {4, -6, 4, -1} + }; + + /* Warm up samples and coefficients. */ + for (i = 0; i < lpcOrder; ++i) { + drflac_int32 sample; + if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) { + return DRFLAC_FALSE; + } + + pDecodedSamples[i] = sample; + } + + if (!drflac__decode_samples_with_residual(bs, subframeBitsPerSample, blockSize, lpcOrder, 0, 4, lpcCoefficientsTable[lpcOrder], pDecodedSamples)) { + return DRFLAC_FALSE; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_samples__lpc(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples) +{ + drflac_uint8 i; + drflac_uint8 lpcPrecision; + drflac_int8 lpcShift; + drflac_int32 coefficients[32]; + + /* Warm up samples. */ + for (i = 0; i < lpcOrder; ++i) { + drflac_int32 sample; + if (!drflac__read_int32(bs, bitsPerSample, &sample)) { + return DRFLAC_FALSE; + } + + pDecodedSamples[i] = sample; + } + + if (!drflac__read_uint8(bs, 4, &lpcPrecision)) { + return DRFLAC_FALSE; + } + if (lpcPrecision == 15) { + return DRFLAC_FALSE; /* Invalid. */ + } + lpcPrecision += 1; + + if (!drflac__read_int8(bs, 5, &lpcShift)) { + return DRFLAC_FALSE; + } + + /* + From the FLAC specification: + + Quantized linear predictor coefficient shift needed in bits (NOTE: this number is signed two's-complement) + + Emphasis on the "signed two's-complement". In practice there does not seem to be any encoders nor decoders supporting negative shifts. For now dr_flac is + not going to support negative shifts as I don't have any reference files. However, when a reference file comes through I will consider adding support. + */ + if (lpcShift < 0) { + return DRFLAC_FALSE; + } + + DRFLAC_ZERO_MEMORY(coefficients, sizeof(coefficients)); + for (i = 0; i < lpcOrder; ++i) { + if (!drflac__read_int32(bs, lpcPrecision, coefficients + i)) { + return DRFLAC_FALSE; + } + } + + if (!drflac__decode_samples_with_residual(bs, bitsPerSample, blockSize, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamples)) { + return DRFLAC_FALSE; + } + + return DRFLAC_TRUE; +} + + +static drflac_bool32 drflac__read_next_flac_frame_header(drflac_bs* bs, drflac_uint8 streaminfoBitsPerSample, drflac_frame_header* header) +{ + const drflac_uint32 sampleRateTable[12] = {0, 88200, 176400, 192000, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000}; + const drflac_uint8 bitsPerSampleTable[8] = {0, 8, 12, (drflac_uint8)-1, 16, 20, 24, (drflac_uint8)-1}; /* -1 = reserved. */ + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(header != NULL); + + /* Keep looping until we find a valid sync code. */ + for (;;) { + drflac_uint8 crc8 = 0xCE; /* 0xCE = drflac_crc8(0, 0x3FFE, 14); */ + drflac_uint8 reserved = 0; + drflac_uint8 blockingStrategy = 0; + drflac_uint8 blockSize = 0; + drflac_uint8 sampleRate = 0; + drflac_uint8 channelAssignment = 0; + drflac_uint8 bitsPerSample = 0; + drflac_bool32 isVariableBlockSize; + + if (!drflac__find_and_seek_to_next_sync_code(bs)) { + return DRFLAC_FALSE; + } + + if (!drflac__read_uint8(bs, 1, &reserved)) { + return DRFLAC_FALSE; + } + if (reserved == 1) { + continue; + } + crc8 = drflac_crc8(crc8, reserved, 1); + + if (!drflac__read_uint8(bs, 1, &blockingStrategy)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, blockingStrategy, 1); + + if (!drflac__read_uint8(bs, 4, &blockSize)) { + return DRFLAC_FALSE; + } + if (blockSize == 0) { + continue; + } + crc8 = drflac_crc8(crc8, blockSize, 4); + + if (!drflac__read_uint8(bs, 4, &sampleRate)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, sampleRate, 4); + + if (!drflac__read_uint8(bs, 4, &channelAssignment)) { + return DRFLAC_FALSE; + } + if (channelAssignment > 10) { + continue; + } + crc8 = drflac_crc8(crc8, channelAssignment, 4); + + if (!drflac__read_uint8(bs, 3, &bitsPerSample)) { + return DRFLAC_FALSE; + } + if (bitsPerSample == 3 || bitsPerSample == 7) { + continue; + } + crc8 = drflac_crc8(crc8, bitsPerSample, 3); + + + if (!drflac__read_uint8(bs, 1, &reserved)) { + return DRFLAC_FALSE; + } + if (reserved == 1) { + continue; + } + crc8 = drflac_crc8(crc8, reserved, 1); + + + isVariableBlockSize = blockingStrategy == 1; + if (isVariableBlockSize) { + drflac_uint64 pcmFrameNumber; + drflac_result result = drflac__read_utf8_coded_number(bs, &pcmFrameNumber, &crc8); + if (result != DRFLAC_SUCCESS) { + if (result == DRFLAC_AT_END) { + return DRFLAC_FALSE; + } else { + continue; + } + } + header->flacFrameNumber = 0; + header->pcmFrameNumber = pcmFrameNumber; + } else { + drflac_uint64 flacFrameNumber = 0; + drflac_result result = drflac__read_utf8_coded_number(bs, &flacFrameNumber, &crc8); + if (result != DRFLAC_SUCCESS) { + if (result == DRFLAC_AT_END) { + return DRFLAC_FALSE; + } else { + continue; + } + } + header->flacFrameNumber = (drflac_uint32)flacFrameNumber; /* <-- Safe cast. */ + header->pcmFrameNumber = 0; + } + + + DRFLAC_ASSERT(blockSize > 0); + if (blockSize == 1) { + header->blockSizeInPCMFrames = 192; + } else if (blockSize <= 5) { + DRFLAC_ASSERT(blockSize >= 2); + header->blockSizeInPCMFrames = 576 * (1 << (blockSize - 2)); + } else if (blockSize == 6) { + if (!drflac__read_uint16(bs, 8, &header->blockSizeInPCMFrames)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, header->blockSizeInPCMFrames, 8); + header->blockSizeInPCMFrames += 1; + } else if (blockSize == 7) { + if (!drflac__read_uint16(bs, 16, &header->blockSizeInPCMFrames)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, header->blockSizeInPCMFrames, 16); + if (header->blockSizeInPCMFrames == 0xFFFF) { + return DRFLAC_FALSE; /* Frame is too big. This is the size of the frame minus 1. The STREAMINFO block defines the max block size which is 16-bits. Adding one will make it 17 bits and therefore too big. */ + } + header->blockSizeInPCMFrames += 1; + } else { + DRFLAC_ASSERT(blockSize >= 8); + header->blockSizeInPCMFrames = 256 * (1 << (blockSize - 8)); + } + + + if (sampleRate <= 11) { + header->sampleRate = sampleRateTable[sampleRate]; + } else if (sampleRate == 12) { + if (!drflac__read_uint32(bs, 8, &header->sampleRate)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, header->sampleRate, 8); + header->sampleRate *= 1000; + } else if (sampleRate == 13) { + if (!drflac__read_uint32(bs, 16, &header->sampleRate)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, header->sampleRate, 16); + } else if (sampleRate == 14) { + if (!drflac__read_uint32(bs, 16, &header->sampleRate)) { + return DRFLAC_FALSE; + } + crc8 = drflac_crc8(crc8, header->sampleRate, 16); + header->sampleRate *= 10; + } else { + continue; /* Invalid. Assume an invalid block. */ + } + + + header->channelAssignment = channelAssignment; + + header->bitsPerSample = bitsPerSampleTable[bitsPerSample]; + if (header->bitsPerSample == 0) { + header->bitsPerSample = streaminfoBitsPerSample; + } + + if (header->bitsPerSample != streaminfoBitsPerSample) { + /* If this subframe has a different bitsPerSample then streaminfo or the first frame, reject it */ + return DRFLAC_FALSE; + } + + if (!drflac__read_uint8(bs, 8, &header->crc8)) { + return DRFLAC_FALSE; + } + +#ifndef DR_FLAC_NO_CRC + if (header->crc8 != crc8) { + continue; /* CRC mismatch. Loop back to the top and find the next sync code. */ + } +#endif + return DRFLAC_TRUE; + } +} + +static drflac_bool32 drflac__read_subframe_header(drflac_bs* bs, drflac_subframe* pSubframe) +{ + drflac_uint8 header; + int type; + + if (!drflac__read_uint8(bs, 8, &header)) { + return DRFLAC_FALSE; + } + + /* First bit should always be 0. */ + if ((header & 0x80) != 0) { + return DRFLAC_FALSE; + } + + /* + Default to 0 for the LPC order. It's important that we always set this to 0 for non LPC + and FIXED subframes because we'll be using it in a generic validation check later. + */ + pSubframe->lpcOrder = 0; + + type = (header & 0x7E) >> 1; + if (type == 0) { + pSubframe->subframeType = DRFLAC_SUBFRAME_CONSTANT; + } else if (type == 1) { + pSubframe->subframeType = DRFLAC_SUBFRAME_VERBATIM; + } else { + if ((type & 0x20) != 0) { + pSubframe->subframeType = DRFLAC_SUBFRAME_LPC; + pSubframe->lpcOrder = (drflac_uint8)(type & 0x1F) + 1; + } else if ((type & 0x08) != 0) { + pSubframe->subframeType = DRFLAC_SUBFRAME_FIXED; + pSubframe->lpcOrder = (drflac_uint8)(type & 0x07); + if (pSubframe->lpcOrder > 4) { + pSubframe->subframeType = DRFLAC_SUBFRAME_RESERVED; + pSubframe->lpcOrder = 0; + } + } else { + pSubframe->subframeType = DRFLAC_SUBFRAME_RESERVED; + } + } + + if (pSubframe->subframeType == DRFLAC_SUBFRAME_RESERVED) { + return DRFLAC_FALSE; + } + + /* Wasted bits per sample. */ + pSubframe->wastedBitsPerSample = 0; + if ((header & 0x01) == 1) { + unsigned int wastedBitsPerSample; + if (!drflac__seek_past_next_set_bit(bs, &wastedBitsPerSample)) { + return DRFLAC_FALSE; + } + pSubframe->wastedBitsPerSample = (drflac_uint8)wastedBitsPerSample + 1; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_subframe(drflac_bs* bs, drflac_frame* frame, int subframeIndex, drflac_int32* pDecodedSamplesOut) +{ + drflac_subframe* pSubframe; + drflac_uint32 subframeBitsPerSample; + drflac_bool32 decodeResult; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(frame != NULL); + + pSubframe = frame->subframes + subframeIndex; + if (!drflac__read_subframe_header(bs, pSubframe)) { + return DRFLAC_FALSE; + } + + /* Side channels require an extra bit per sample. Took a while to figure that one out... */ + subframeBitsPerSample = frame->header.bitsPerSample; + if ((frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE || frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE) && subframeIndex == 1) { + subframeBitsPerSample += 1; + } else if (frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE && subframeIndex == 0) { + subframeBitsPerSample += 1; + } + + if (subframeBitsPerSample > 32) { + /* libFLAC and ffmpeg reject 33-bit subframes as well */ + return DRFLAC_FALSE; + } + + /* Need to handle wasted bits per sample. */ + if (pSubframe->wastedBitsPerSample >= subframeBitsPerSample) { + return DRFLAC_FALSE; + } + subframeBitsPerSample -= pSubframe->wastedBitsPerSample; + + pSubframe->pSamplesS32 = pDecodedSamplesOut; + + /* + pDecodedSamplesOut will be pointing to a buffer that was allocated with enough memory to store + maxBlockSizeInPCMFrames samples (as specified in the FLAC header). We need to guard against an + overflow here. At a higher level we are checking maxBlockSizeInPCMFrames from the header, but + here we need to do an additional check to ensure this frame's block size fully encompasses any + warmup samples which is determined by the LPC order. For non LPC and FIXED subframes, the LPC + order will be have been set to 0 in drflac__read_subframe_header(). + */ + if (frame->header.blockSizeInPCMFrames < pSubframe->lpcOrder) { + return DRFLAC_FALSE; + } + + switch (pSubframe->subframeType) + { + case DRFLAC_SUBFRAME_CONSTANT: + { + decodeResult = drflac__decode_samples__constant(bs, frame->header.blockSizeInPCMFrames, subframeBitsPerSample, pSubframe->pSamplesS32); + } break; + + case DRFLAC_SUBFRAME_VERBATIM: + { + decodeResult = drflac__decode_samples__verbatim(bs, frame->header.blockSizeInPCMFrames, subframeBitsPerSample, pSubframe->pSamplesS32); + } break; + + case DRFLAC_SUBFRAME_FIXED: + { + decodeResult = drflac__decode_samples__fixed(bs, frame->header.blockSizeInPCMFrames, subframeBitsPerSample, pSubframe->lpcOrder, pSubframe->pSamplesS32); + } break; + + case DRFLAC_SUBFRAME_LPC: + { + decodeResult = drflac__decode_samples__lpc(bs, frame->header.blockSizeInPCMFrames, subframeBitsPerSample, pSubframe->lpcOrder, pSubframe->pSamplesS32); + } break; + + default: decodeResult = DRFLAC_FALSE; + } + + return decodeResult; +} + +static drflac_bool32 drflac__seek_subframe(drflac_bs* bs, drflac_frame* frame, int subframeIndex) +{ + drflac_subframe* pSubframe; + drflac_uint32 subframeBitsPerSample; + + DRFLAC_ASSERT(bs != NULL); + DRFLAC_ASSERT(frame != NULL); + + pSubframe = frame->subframes + subframeIndex; + if (!drflac__read_subframe_header(bs, pSubframe)) { + return DRFLAC_FALSE; + } + + /* Side channels require an extra bit per sample. Took a while to figure that one out... */ + subframeBitsPerSample = frame->header.bitsPerSample; + if ((frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE || frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE) && subframeIndex == 1) { + subframeBitsPerSample += 1; + } else if (frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE && subframeIndex == 0) { + subframeBitsPerSample += 1; + } + + /* Need to handle wasted bits per sample. */ + if (pSubframe->wastedBitsPerSample >= subframeBitsPerSample) { + return DRFLAC_FALSE; + } + subframeBitsPerSample -= pSubframe->wastedBitsPerSample; + + pSubframe->pSamplesS32 = NULL; + + switch (pSubframe->subframeType) + { + case DRFLAC_SUBFRAME_CONSTANT: + { + if (!drflac__seek_bits(bs, subframeBitsPerSample)) { + return DRFLAC_FALSE; + } + } break; + + case DRFLAC_SUBFRAME_VERBATIM: + { + unsigned int bitsToSeek = frame->header.blockSizeInPCMFrames * subframeBitsPerSample; + if (!drflac__seek_bits(bs, bitsToSeek)) { + return DRFLAC_FALSE; + } + } break; + + case DRFLAC_SUBFRAME_FIXED: + { + unsigned int bitsToSeek = pSubframe->lpcOrder * subframeBitsPerSample; + if (!drflac__seek_bits(bs, bitsToSeek)) { + return DRFLAC_FALSE; + } + + if (!drflac__read_and_seek_residual(bs, frame->header.blockSizeInPCMFrames, pSubframe->lpcOrder)) { + return DRFLAC_FALSE; + } + } break; + + case DRFLAC_SUBFRAME_LPC: + { + drflac_uint8 lpcPrecision; + + unsigned int bitsToSeek = pSubframe->lpcOrder * subframeBitsPerSample; + if (!drflac__seek_bits(bs, bitsToSeek)) { + return DRFLAC_FALSE; + } + + if (!drflac__read_uint8(bs, 4, &lpcPrecision)) { + return DRFLAC_FALSE; + } + if (lpcPrecision == 15) { + return DRFLAC_FALSE; /* Invalid. */ + } + lpcPrecision += 1; + + + bitsToSeek = (pSubframe->lpcOrder * lpcPrecision) + 5; /* +5 for shift. */ + if (!drflac__seek_bits(bs, bitsToSeek)) { + return DRFLAC_FALSE; + } + + if (!drflac__read_and_seek_residual(bs, frame->header.blockSizeInPCMFrames, pSubframe->lpcOrder)) { + return DRFLAC_FALSE; + } + } break; + + default: return DRFLAC_FALSE; + } + + return DRFLAC_TRUE; +} + + +static DRFLAC_INLINE drflac_uint8 drflac__get_channel_count_from_channel_assignment(drflac_int8 channelAssignment) +{ + drflac_uint8 lookup[] = {1, 2, 3, 4, 5, 6, 7, 8, 2, 2, 2}; + + DRFLAC_ASSERT(channelAssignment <= 10); + return lookup[channelAssignment]; +} + +static drflac_result drflac__decode_flac_frame(drflac* pFlac) +{ + int channelCount; + int i; + drflac_uint8 paddingSizeInBits; + drflac_uint16 desiredCRC16; +#ifndef DR_FLAC_NO_CRC + drflac_uint16 actualCRC16; +#endif + + /* This function should be called while the stream is sitting on the first byte after the frame header. */ + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + DRFLAC_ZERO_MEMORY(pFlac->currentFLACFrame.subframes, sizeof(pFlac->currentFLACFrame.subframes)); + + /* The frame block size must never be larger than the maximum block size defined by the FLAC stream. */ + if (pFlac->currentFLACFrame.header.blockSizeInPCMFrames > pFlac->maxBlockSizeInPCMFrames) { + return DRFLAC_ERROR; + } + + /* The number of channels in the frame must match the channel count from the STREAMINFO block. */ + channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFLACFrame.header.channelAssignment); + if (channelCount != (int)pFlac->channels) { + return DRFLAC_ERROR; + } + + for (i = 0; i < channelCount; ++i) { + if (!drflac__decode_subframe(&pFlac->bs, &pFlac->currentFLACFrame, i, pFlac->pDecodedSamples + (pFlac->currentFLACFrame.header.blockSizeInPCMFrames * i))) { + return DRFLAC_ERROR; + } + } + + paddingSizeInBits = (drflac_uint8)(DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->bs) & 7); + if (paddingSizeInBits > 0) { + drflac_uint8 padding = 0; + if (!drflac__read_uint8(&pFlac->bs, paddingSizeInBits, &padding)) { + return DRFLAC_AT_END; + } + } + +#ifndef DR_FLAC_NO_CRC + actualCRC16 = drflac__flush_crc16(&pFlac->bs); +#endif + if (!drflac__read_uint16(&pFlac->bs, 16, &desiredCRC16)) { + return DRFLAC_AT_END; + } + +#ifndef DR_FLAC_NO_CRC + if (actualCRC16 != desiredCRC16) { + return DRFLAC_CRC_MISMATCH; /* CRC mismatch. */ + } +#endif + + pFlac->currentFLACFrame.pcmFramesRemaining = pFlac->currentFLACFrame.header.blockSizeInPCMFrames; + + return DRFLAC_SUCCESS; +} + +static drflac_result drflac__seek_flac_frame(drflac* pFlac) +{ + drflac_result result; + int channelCount; + int i; + drflac_uint16 desiredCRC16; +#ifndef DR_FLAC_NO_CRC + drflac_uint16 actualCRC16; +#endif + + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + + channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFLACFrame.header.channelAssignment); + for (i = 0; i < channelCount; ++i) { + if (!drflac__seek_subframe(&pFlac->bs, &pFlac->currentFLACFrame, i)) { + result = DRFLAC_ERROR; + goto error; + } + } + + /* Padding. */ + if (!drflac__seek_bits(&pFlac->bs, DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->bs) & 7)) { + result = DRFLAC_ERROR; + goto error; + } + + /* CRC. */ +#ifndef DR_FLAC_NO_CRC + actualCRC16 = drflac__flush_crc16(&pFlac->bs); +#endif + if (!drflac__read_uint16(&pFlac->bs, 16, &desiredCRC16)) { + result = DRFLAC_AT_END; + goto error; + } + +#ifndef DR_FLAC_NO_CRC + if (actualCRC16 != desiredCRC16) { + result = DRFLAC_CRC_MISMATCH; /* CRC mismatch. */ + goto error; + } +#endif + + return DRFLAC_SUCCESS; + +error: + DRFLAC_ZERO_MEMORY(pFlac->currentFLACFrame.subframes, sizeof(pFlac->currentFLACFrame.subframes)); + return result; +} + +static drflac_bool32 drflac__read_and_decode_next_flac_frame(drflac* pFlac) +{ + DRFLAC_ASSERT(pFlac != NULL); + + for (;;) { + drflac_result result; + + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + + result = drflac__decode_flac_frame(pFlac); + if (result != DRFLAC_SUCCESS) { + if (result == DRFLAC_CRC_MISMATCH) { + continue; /* CRC mismatch. Skip to the next frame. */ + } else { + return DRFLAC_FALSE; + } + } + + return DRFLAC_TRUE; + } +} + +static void drflac__get_pcm_frame_range_of_current_flac_frame(drflac* pFlac, drflac_uint64* pFirstPCMFrame, drflac_uint64* pLastPCMFrame) +{ + drflac_uint64 firstPCMFrame; + drflac_uint64 lastPCMFrame; + + DRFLAC_ASSERT(pFlac != NULL); + + firstPCMFrame = pFlac->currentFLACFrame.header.pcmFrameNumber; + if (firstPCMFrame == 0) { + firstPCMFrame = ((drflac_uint64)pFlac->currentFLACFrame.header.flacFrameNumber) * pFlac->maxBlockSizeInPCMFrames; + } + + lastPCMFrame = firstPCMFrame + pFlac->currentFLACFrame.header.blockSizeInPCMFrames; + if (lastPCMFrame > 0) { + lastPCMFrame -= 1; /* Needs to be zero based. */ + } + + if (pFirstPCMFrame) { + *pFirstPCMFrame = firstPCMFrame; + } + if (pLastPCMFrame) { + *pLastPCMFrame = lastPCMFrame; + } +} + +static drflac_bool32 drflac__seek_to_first_frame(drflac* pFlac) +{ + drflac_bool32 result; + + DRFLAC_ASSERT(pFlac != NULL); + + result = drflac__seek_to_byte(&pFlac->bs, pFlac->firstFLACFramePosInBytes); + + DRFLAC_ZERO_MEMORY(&pFlac->currentFLACFrame, sizeof(pFlac->currentFLACFrame)); + pFlac->currentPCMFrame = 0; + + return result; +} + +static DRFLAC_INLINE drflac_result drflac__seek_to_next_flac_frame(drflac* pFlac) +{ + /* This function should only ever be called while the decoder is sitting on the first byte past the FRAME_HEADER section. */ + DRFLAC_ASSERT(pFlac != NULL); + return drflac__seek_flac_frame(pFlac); +} + + +static drflac_uint64 drflac__seek_forward_by_pcm_frames(drflac* pFlac, drflac_uint64 pcmFramesToSeek) +{ + drflac_uint64 pcmFramesRead = 0; + while (pcmFramesToSeek > 0) { + if (pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_and_decode_next_flac_frame(pFlac)) { + break; /* Couldn't read the next frame, so just break from the loop and return. */ + } + } else { + if (pFlac->currentFLACFrame.pcmFramesRemaining > pcmFramesToSeek) { + pcmFramesRead += pcmFramesToSeek; + pFlac->currentFLACFrame.pcmFramesRemaining -= (drflac_uint32)pcmFramesToSeek; /* <-- Safe cast. Will always be < currentFrame.pcmFramesRemaining < 65536. */ + pcmFramesToSeek = 0; + } else { + pcmFramesRead += pFlac->currentFLACFrame.pcmFramesRemaining; + pcmFramesToSeek -= pFlac->currentFLACFrame.pcmFramesRemaining; + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + } + } + } + + pFlac->currentPCMFrame += pcmFramesRead; + return pcmFramesRead; +} + + +static drflac_bool32 drflac__seek_to_pcm_frame__brute_force(drflac* pFlac, drflac_uint64 pcmFrameIndex) +{ + drflac_bool32 isMidFrame = DRFLAC_FALSE; + drflac_uint64 runningPCMFrameCount; + + DRFLAC_ASSERT(pFlac != NULL); + + /* If we are seeking forward we start from the current position. Otherwise we need to start all the way from the start of the file. */ + if (pcmFrameIndex >= pFlac->currentPCMFrame) { + /* Seeking forward. Need to seek from the current position. */ + runningPCMFrameCount = pFlac->currentPCMFrame; + + /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */ + if (pFlac->currentPCMFrame == 0 && pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } else { + isMidFrame = DRFLAC_TRUE; + } + } else { + /* Seeking backwards. Need to seek from the start of the file. */ + runningPCMFrameCount = 0; + + /* Move back to the start. */ + if (!drflac__seek_to_first_frame(pFlac)) { + return DRFLAC_FALSE; + } + + /* Decode the first frame in preparation for sample-exact seeking below. */ + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } + + /* + We need to as quickly as possible find the frame that contains the target sample. To do this, we iterate over each frame and inspect its + header. If based on the header we can determine that the frame contains the sample, we do a full decode of that frame. + */ + for (;;) { + drflac_uint64 pcmFrameCountInThisFLACFrame; + drflac_uint64 firstPCMFrameInFLACFrame = 0; + drflac_uint64 lastPCMFrameInFLACFrame = 0; + + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &firstPCMFrameInFLACFrame, &lastPCMFrameInFLACFrame); + + pcmFrameCountInThisFLACFrame = (lastPCMFrameInFLACFrame - firstPCMFrameInFLACFrame) + 1; + if (pcmFrameIndex < (runningPCMFrameCount + pcmFrameCountInThisFLACFrame)) { + /* + The sample should be in this frame. We need to fully decode it, however if it's an invalid frame (a CRC mismatch), we need to pretend + it never existed and keep iterating. + */ + drflac_uint64 pcmFramesToDecode = pcmFrameIndex - runningPCMFrameCount; + + if (!isMidFrame) { + drflac_result result = drflac__decode_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */ + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFramesToDecode) == pcmFramesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */ + } else { + if (result == DRFLAC_CRC_MISMATCH) { + goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } else { + /* We started seeking mid-frame which means we need to skip the frame decoding part. */ + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFramesToDecode) == pcmFramesToDecode; + } + } else { + /* + It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this + frame never existed and leave the running sample count untouched. + */ + if (!isMidFrame) { + drflac_result result = drflac__seek_to_next_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + runningPCMFrameCount += pcmFrameCountInThisFLACFrame; + } else { + if (result == DRFLAC_CRC_MISMATCH) { + goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } else { + /* + We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with + drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header. + */ + runningPCMFrameCount += pFlac->currentFLACFrame.pcmFramesRemaining; + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + isMidFrame = DRFLAC_FALSE; + } + + /* If we are seeking to the end of the file and we've just hit it, we're done. */ + if (pcmFrameIndex == pFlac->totalPCMFrameCount && runningPCMFrameCount == pFlac->totalPCMFrameCount) { + return DRFLAC_TRUE; + } + } + + next_iteration: + /* Grab the next frame in preparation for the next iteration. */ + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } +} + + +#if !defined(DR_FLAC_NO_CRC) +/* +We use an average compression ratio to determine our approximate start location. FLAC files are generally about 50%-70% the size of their +uncompressed counterparts so we'll use this as a basis. I'm going to split the middle and use a factor of 0.6 to determine the starting +location. +*/ +#define DRFLAC_BINARY_SEARCH_APPROX_COMPRESSION_RATIO 0.6f + +static drflac_bool32 drflac__seek_to_approximate_flac_frame_to_byte(drflac* pFlac, drflac_uint64 targetByte, drflac_uint64 rangeLo, drflac_uint64 rangeHi, drflac_uint64* pLastSuccessfulSeekOffset) +{ + DRFLAC_ASSERT(pFlac != NULL); + DRFLAC_ASSERT(pLastSuccessfulSeekOffset != NULL); + DRFLAC_ASSERT(targetByte >= rangeLo); + DRFLAC_ASSERT(targetByte <= rangeHi); + + *pLastSuccessfulSeekOffset = pFlac->firstFLACFramePosInBytes; + + for (;;) { + /* After rangeLo == rangeHi == targetByte fails, we need to break out. */ + drflac_uint64 lastTargetByte = targetByte; + + /* When seeking to a byte, failure probably means we've attempted to seek beyond the end of the stream. To counter this we just halve it each attempt. */ + if (!drflac__seek_to_byte(&pFlac->bs, targetByte)) { + /* If we couldn't even seek to the first byte in the stream we have a problem. Just abandon the whole thing. */ + if (targetByte == 0) { + drflac__seek_to_first_frame(pFlac); /* Try to recover. */ + return DRFLAC_FALSE; + } + + /* Halve the byte location and continue. */ + targetByte = rangeLo + ((rangeHi - rangeLo)/2); + rangeHi = targetByte; + } else { + /* Getting here should mean that we have seeked to an appropriate byte. */ + + /* Clear the details of the FLAC frame so we don't misreport data. */ + DRFLAC_ZERO_MEMORY(&pFlac->currentFLACFrame, sizeof(pFlac->currentFLACFrame)); + + /* + Now seek to the next FLAC frame. We need to decode the entire frame (not just the header) because it's possible for the header to incorrectly pass the + CRC check and return bad data. We need to decode the entire frame to be more certain. Although this seems unlikely, this has happened to me in testing + so it needs to stay this way for now. + */ +#if 1 + if (!drflac__read_and_decode_next_flac_frame(pFlac)) { + /* Halve the byte location and continue. */ + targetByte = rangeLo + ((rangeHi - rangeLo)/2); + rangeHi = targetByte; + } else { + break; + } +#else + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + /* Halve the byte location and continue. */ + targetByte = rangeLo + ((rangeHi - rangeLo)/2); + rangeHi = targetByte; + } else { + break; + } +#endif + } + + /* We already tried this byte and there are no more to try, break out. */ + if(targetByte == lastTargetByte) { + return DRFLAC_FALSE; + } + } + + /* The current PCM frame needs to be updated based on the frame we just seeked to. */ + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &pFlac->currentPCMFrame, NULL); + + DRFLAC_ASSERT(targetByte <= rangeHi); + + *pLastSuccessfulSeekOffset = targetByte; + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(drflac* pFlac, drflac_uint64 offset) +{ + /* This section of code would be used if we were only decoding the FLAC frame header when calling drflac__seek_to_approximate_flac_frame_to_byte(). */ +#if 0 + if (drflac__decode_flac_frame(pFlac) != DRFLAC_SUCCESS) { + /* We failed to decode this frame which may be due to it being corrupt. We'll just use the next valid FLAC frame. */ + if (drflac__read_and_decode_next_flac_frame(pFlac) == DRFLAC_FALSE) { + return DRFLAC_FALSE; + } + } +#endif + + return drflac__seek_forward_by_pcm_frames(pFlac, offset) == offset; +} + + +static drflac_bool32 drflac__seek_to_pcm_frame__binary_search_internal(drflac* pFlac, drflac_uint64 pcmFrameIndex, drflac_uint64 byteRangeLo, drflac_uint64 byteRangeHi) +{ + /* This assumes pFlac->currentPCMFrame is sitting on byteRangeLo upon entry. */ + + drflac_uint64 targetByte; + drflac_uint64 pcmRangeLo = pFlac->totalPCMFrameCount; + drflac_uint64 pcmRangeHi = 0; + drflac_uint64 lastSuccessfulSeekOffset = (drflac_uint64)-1; + drflac_uint64 closestSeekOffsetBeforeTargetPCMFrame = byteRangeLo; + drflac_uint32 seekForwardThreshold = (pFlac->maxBlockSizeInPCMFrames != 0) ? pFlac->maxBlockSizeInPCMFrames*2 : 4096; + + targetByte = byteRangeLo + (drflac_uint64)(((drflac_int64)((pcmFrameIndex - pFlac->currentPCMFrame) * pFlac->channels * pFlac->bitsPerSample)/8.0f) * DRFLAC_BINARY_SEARCH_APPROX_COMPRESSION_RATIO); + if (targetByte > byteRangeHi) { + targetByte = byteRangeHi; + } + + for (;;) { + /* + If only two adjacent byte offsets remain, binary search cannot narrow the range any further. Seek to the closest frame before the target and decode + forward from there. + */ + if ((byteRangeHi - byteRangeLo) == 1) { + if (!drflac__seek_to_approximate_flac_frame_to_byte(pFlac, closestSeekOffsetBeforeTargetPCMFrame, closestSeekOffsetBeforeTargetPCMFrame, byteRangeHi, &lastSuccessfulSeekOffset)) { + break; + } + + if (pFlac->currentPCMFrame <= pcmFrameIndex && drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac, pcmFrameIndex - pFlac->currentPCMFrame)) { + return DRFLAC_TRUE; + } + + break; + } + + if (drflac__seek_to_approximate_flac_frame_to_byte(pFlac, targetByte, byteRangeLo, byteRangeHi, &lastSuccessfulSeekOffset)) { + /* We found a FLAC frame. We need to check if it contains the sample we're looking for. */ + drflac_uint64 newPCMRangeLo; + drflac_uint64 newPCMRangeHi; + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &newPCMRangeLo, &newPCMRangeHi); + + /* If we selected the same frame, it means we should be pretty close. Just decode the rest. */ + if (pcmRangeLo == newPCMRangeLo) { + if (!drflac__seek_to_approximate_flac_frame_to_byte(pFlac, closestSeekOffsetBeforeTargetPCMFrame, closestSeekOffsetBeforeTargetPCMFrame, byteRangeHi, &lastSuccessfulSeekOffset)) { + break; /* Failed to seek to closest frame. */ + } + + if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac, pcmFrameIndex - pFlac->currentPCMFrame)) { + return DRFLAC_TRUE; + } else { + break; /* Failed to seek forward. */ + } + } + + pcmRangeLo = newPCMRangeLo; + pcmRangeHi = newPCMRangeHi; + + if (pcmRangeLo <= pcmFrameIndex && pcmRangeHi >= pcmFrameIndex) { + /* The target PCM frame is in this FLAC frame. */ + if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac, pcmFrameIndex - pFlac->currentPCMFrame) ) { + return DRFLAC_TRUE; + } else { + break; /* Failed to seek to FLAC frame. */ + } + } else { + if (pcmRangeLo > pcmFrameIndex) { + /* We seeked too far forward. We need to move our target byte backward and try again. */ + byteRangeHi = lastSuccessfulSeekOffset; + if (byteRangeLo > byteRangeHi) { + byteRangeLo = byteRangeHi; + } + + targetByte = byteRangeLo + ((byteRangeHi - byteRangeLo) / 2); + if (targetByte < byteRangeLo) { + targetByte = byteRangeLo; + } + } else /*if (pcmRangeHi < pcmFrameIndex)*/ { + /* We didn't seek far enough. We need to move our target byte forward and try again. */ + + /* If we're close enough we can just seek forward. */ + if ((pcmFrameIndex - pcmRangeLo) < seekForwardThreshold) { + if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac, pcmFrameIndex - pFlac->currentPCMFrame)) { + return DRFLAC_TRUE; + } else { + break; /* Failed to seek to FLAC frame. */ + } + } else { + const double approxCompressionRatio = (drflac_int64)(lastSuccessfulSeekOffset - pFlac->firstFLACFramePosInBytes) / ((drflac_int64)(pcmRangeLo * pFlac->channels * pFlac->bitsPerSample)/8.0); + + byteRangeLo = lastSuccessfulSeekOffset; + if (byteRangeHi < byteRangeLo) { + byteRangeHi = byteRangeLo; + } + + targetByte = lastSuccessfulSeekOffset + (drflac_uint64)(((drflac_int64)((pcmFrameIndex-pcmRangeLo) * pFlac->channels * pFlac->bitsPerSample)/8.0) * approxCompressionRatio); + if (targetByte > byteRangeHi) { + targetByte = byteRangeHi; + } + + if (closestSeekOffsetBeforeTargetPCMFrame < lastSuccessfulSeekOffset) { + closestSeekOffsetBeforeTargetPCMFrame = lastSuccessfulSeekOffset; + } + } + } + } + } else { + /* Getting here is really bad. We just recover as best we can, but moving to the first frame in the stream, and then abort. */ + break; + } + } + + drflac__seek_to_first_frame(pFlac); /* <-- Try to recover. */ + return DRFLAC_FALSE; +} + +static drflac_bool32 drflac__seek_to_pcm_frame__binary_search(drflac* pFlac, drflac_uint64 pcmFrameIndex) +{ + drflac_uint64 byteRangeLo; + drflac_uint64 byteRangeHi; + drflac_uint32 seekForwardThreshold = (pFlac->maxBlockSizeInPCMFrames != 0) ? pFlac->maxBlockSizeInPCMFrames*2 : 4096; + + /* Our algorithm currently assumes the FLAC stream is currently sitting at the start. */ + if (drflac__seek_to_first_frame(pFlac) == DRFLAC_FALSE) { + return DRFLAC_FALSE; + } + + /* If we're close enough to the start, just move to the start and seek forward. */ + if (pcmFrameIndex < seekForwardThreshold) { + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFrameIndex) == pcmFrameIndex; + } + + /* + Our starting byte range is the byte position of the first FLAC frame and the approximate end of the file as if it were completely uncompressed. This ensures + the entire file is included, even though most of the time it'll exceed the end of the actual stream. This is OK as the frame searching logic will handle it. + */ + byteRangeLo = pFlac->firstFLACFramePosInBytes; + byteRangeHi = pFlac->firstFLACFramePosInBytes + (drflac_uint64)((drflac_int64)(pFlac->totalPCMFrameCount * pFlac->channels * pFlac->bitsPerSample)/8.0f); + + return drflac__seek_to_pcm_frame__binary_search_internal(pFlac, pcmFrameIndex, byteRangeLo, byteRangeHi); +} +#endif /* !DR_FLAC_NO_CRC */ + +static drflac_bool32 drflac__seek_to_pcm_frame__seek_table(drflac* pFlac, drflac_uint64 pcmFrameIndex) +{ + drflac_uint32 iClosestSeekpoint = 0; + drflac_bool32 isMidFrame = DRFLAC_FALSE; + drflac_uint64 runningPCMFrameCount; + drflac_uint32 iSeekpoint; + + + DRFLAC_ASSERT(pFlac != NULL); + + if (pFlac->pSeekpoints == NULL || pFlac->seekpointCount == 0) { + return DRFLAC_FALSE; + } + + /* Do not use the seektable if pcmFramIndex is not coverd by it. */ + if (pFlac->pSeekpoints[0].firstPCMFrame > pcmFrameIndex) { + return DRFLAC_FALSE; + } + + for (iSeekpoint = 0; iSeekpoint < pFlac->seekpointCount; ++iSeekpoint) { + if (pFlac->pSeekpoints[iSeekpoint].firstPCMFrame >= pcmFrameIndex) { + break; + } + + iClosestSeekpoint = iSeekpoint; + } + + /* There's been cases where the seek table contains only zeros. We need to do some basic validation on the closest seekpoint. */ + if (pFlac->pSeekpoints[iClosestSeekpoint].pcmFrameCount == 0 || pFlac->pSeekpoints[iClosestSeekpoint].pcmFrameCount > pFlac->maxBlockSizeInPCMFrames) { + return DRFLAC_FALSE; + } + if (pFlac->pSeekpoints[iClosestSeekpoint].firstPCMFrame > pFlac->totalPCMFrameCount && pFlac->totalPCMFrameCount > 0) { + return DRFLAC_FALSE; + } + +#if !defined(DR_FLAC_NO_CRC) + /* At this point we should know the closest seek point. We can use a binary search for this. We need to know the total sample count for this. */ + if (pFlac->totalPCMFrameCount > 0) { + drflac_uint64 byteRangeLo; + drflac_uint64 byteRangeHi; + + byteRangeHi = pFlac->firstFLACFramePosInBytes + (drflac_uint64)((drflac_int64)(pFlac->totalPCMFrameCount * pFlac->channels * pFlac->bitsPerSample)/8.0f); + byteRangeLo = pFlac->firstFLACFramePosInBytes + pFlac->pSeekpoints[iClosestSeekpoint].flacFrameOffset; + + /* + If our closest seek point is not the last one, we only need to search between it and the next one. The section below calculates an appropriate starting + value for byteRangeHi which will clamp it appropriately. + + Note that the next seekpoint must have an offset greater than the closest seekpoint because otherwise our binary search algorithm will break down. There + have been cases where a seektable consists of seek points where every byte offset is set to 0 which causes problems. If this happens we need to abort. + */ + if (iClosestSeekpoint < pFlac->seekpointCount-1) { + drflac_uint32 iNextSeekpoint = iClosestSeekpoint + 1; + + /* Basic validation on the seekpoints to ensure they're usable. */ + if (pFlac->pSeekpoints[iClosestSeekpoint].flacFrameOffset >= pFlac->pSeekpoints[iNextSeekpoint].flacFrameOffset || pFlac->pSeekpoints[iNextSeekpoint].pcmFrameCount == 0) { + return DRFLAC_FALSE; /* The next seekpoint doesn't look right. The seek table cannot be trusted from here. Abort. */ + } + + if (pFlac->pSeekpoints[iNextSeekpoint].firstPCMFrame != (((drflac_uint64)0xFFFFFFFF << 32) | 0xFFFFFFFF)) { /* Make sure it's not a placeholder seekpoint. */ + byteRangeHi = pFlac->firstFLACFramePosInBytes + pFlac->pSeekpoints[iNextSeekpoint].flacFrameOffset - 1; /* byteRangeHi must be zero based. */ + } + } + + if (drflac__seek_to_byte(&pFlac->bs, pFlac->firstFLACFramePosInBytes + pFlac->pSeekpoints[iClosestSeekpoint].flacFrameOffset)) { + if (drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &pFlac->currentPCMFrame, NULL); + + if (drflac__seek_to_pcm_frame__binary_search_internal(pFlac, pcmFrameIndex, byteRangeLo, byteRangeHi)) { + return DRFLAC_TRUE; + } + } + } + } +#endif /* !DR_FLAC_NO_CRC */ + + /* Getting here means we need to use a slower algorithm because the binary search method failed or cannot be used. */ + + /* + If we are seeking forward and the closest seekpoint is _before_ the current sample, we just seek forward from where we are. Otherwise we start seeking + from the seekpoint's first sample. + */ + if (pcmFrameIndex >= pFlac->currentPCMFrame && pFlac->pSeekpoints[iClosestSeekpoint].firstPCMFrame <= pFlac->currentPCMFrame) { + /* Optimized case. Just seek forward from where we are. */ + runningPCMFrameCount = pFlac->currentPCMFrame; + + /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */ + if (pFlac->currentPCMFrame == 0 && pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } else { + isMidFrame = DRFLAC_TRUE; + } + } else { + /* Slower case. Seek to the start of the seekpoint and then seek forward from there. */ + runningPCMFrameCount = pFlac->pSeekpoints[iClosestSeekpoint].firstPCMFrame; + + if (!drflac__seek_to_byte(&pFlac->bs, pFlac->firstFLACFramePosInBytes + pFlac->pSeekpoints[iClosestSeekpoint].flacFrameOffset)) { + return DRFLAC_FALSE; + } + + /* Grab the frame the seekpoint is sitting on in preparation for the sample-exact seeking below. */ + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } + + for (;;) { + drflac_uint64 pcmFrameCountInThisFLACFrame; + drflac_uint64 firstPCMFrameInFLACFrame = 0; + drflac_uint64 lastPCMFrameInFLACFrame = 0; + + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &firstPCMFrameInFLACFrame, &lastPCMFrameInFLACFrame); + + pcmFrameCountInThisFLACFrame = (lastPCMFrameInFLACFrame - firstPCMFrameInFLACFrame) + 1; + if (pcmFrameIndex < (runningPCMFrameCount + pcmFrameCountInThisFLACFrame)) { + /* + The sample should be in this frame. We need to fully decode it, but if it's an invalid frame (a CRC mismatch) we need to pretend + it never existed and keep iterating. + */ + drflac_uint64 pcmFramesToDecode = pcmFrameIndex - runningPCMFrameCount; + + if (!isMidFrame) { + drflac_result result = drflac__decode_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */ + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFramesToDecode) == pcmFramesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */ + } else { + if (result == DRFLAC_CRC_MISMATCH) { + goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } else { + /* We started seeking mid-frame which means we need to skip the frame decoding part. */ + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFramesToDecode) == pcmFramesToDecode; + } + } else { + /* + It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this + frame never existed and leave the running sample count untouched. + */ + if (!isMidFrame) { + drflac_result result = drflac__seek_to_next_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + runningPCMFrameCount += pcmFrameCountInThisFLACFrame; + } else { + if (result == DRFLAC_CRC_MISMATCH) { + goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } else { + /* + We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with + drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header. + */ + runningPCMFrameCount += pFlac->currentFLACFrame.pcmFramesRemaining; + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + isMidFrame = DRFLAC_FALSE; + } + + /* If we are seeking to the end of the file and we've just hit it, we're done. */ + if (pcmFrameIndex == pFlac->totalPCMFrameCount && runningPCMFrameCount == pFlac->totalPCMFrameCount) { + return DRFLAC_TRUE; + } + } + + next_iteration: + /* Grab the next frame in preparation for the next iteration. */ + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + } +} + + +#ifndef DR_FLAC_NO_OGG +typedef struct +{ + drflac_uint8 capturePattern[4]; /* Should be "OggS" */ + drflac_uint8 structureVersion; /* Always 0. */ + drflac_uint8 headerType; + drflac_uint64 granulePosition; + drflac_uint32 serialNumber; + drflac_uint32 sequenceNumber; + drflac_uint32 checksum; + drflac_uint8 segmentCount; + drflac_uint8 segmentTable[255]; +} drflac_ogg_page_header; +#endif + +typedef struct +{ + drflac_read_proc onRead; + drflac_seek_proc onSeek; + drflac_tell_proc onTell; + drflac_meta_proc onMeta; + drflac_container container; + void* pUserData; + void* pUserDataMD; + drflac_uint32 sampleRate; + drflac_uint8 channels; + drflac_uint8 bitsPerSample; + drflac_uint64 totalPCMFrameCount; + drflac_uint16 maxBlockSizeInPCMFrames; + drflac_uint64 runningFilePos; + drflac_bool32 hasStreamInfoBlock; + drflac_bool32 hasMetadataBlocks; + drflac_bs bs; /* <-- A bit streamer is required for loading data during initialization. */ + drflac_frame_header firstFrameHeader; /* <-- The header of the first frame that was read during relaxed initalization. Only set if there is no STREAMINFO block. */ + +#ifndef DR_FLAC_NO_OGG + drflac_uint32 oggSerial; + drflac_uint64 oggFirstBytePos; + drflac_ogg_page_header oggBosHeader; +#endif +} drflac_init_info; + +static DRFLAC_INLINE void drflac__decode_block_header(drflac_uint32 blockHeader, drflac_uint8* isLastBlock, drflac_uint8* blockType, drflac_uint32* blockSize) +{ + blockHeader = drflac__be2host_32(blockHeader); + *isLastBlock = (drflac_uint8)((blockHeader & 0x80000000UL) >> 31); + *blockType = (drflac_uint8)((blockHeader & 0x7F000000UL) >> 24); + *blockSize = (blockHeader & 0x00FFFFFFUL); +} + +static DRFLAC_INLINE drflac_bool32 drflac__read_and_decode_block_header(drflac_read_proc onRead, void* pUserData, drflac_uint8* isLastBlock, drflac_uint8* blockType, drflac_uint32* blockSize) +{ + drflac_uint32 blockHeader; + + *blockSize = 0; + if (onRead(pUserData, &blockHeader, 4) != 4) { + return DRFLAC_FALSE; + } + + drflac__decode_block_header(blockHeader, isLastBlock, blockType, blockSize); + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__read_streaminfo(drflac_read_proc onRead, void* pUserData, drflac_streaminfo* pStreamInfo) +{ + drflac_uint32 blockSizes; + drflac_uint64 frameSizes = 0; + drflac_uint64 importantProps; + drflac_uint8 md5[16]; + + /* min/max block size. */ + if (onRead(pUserData, &blockSizes, 4) != 4) { + return DRFLAC_FALSE; + } + + /* min/max frame size. */ + if (onRead(pUserData, &frameSizes, 6) != 6) { + return DRFLAC_FALSE; + } + + /* Sample rate, channels, bits per sample and total sample count. */ + if (onRead(pUserData, &importantProps, 8) != 8) { + return DRFLAC_FALSE; + } + + /* MD5 */ + if (onRead(pUserData, md5, sizeof(md5)) != sizeof(md5)) { + return DRFLAC_FALSE; + } + + blockSizes = drflac__be2host_32(blockSizes); + frameSizes = drflac__be2host_64(frameSizes); + importantProps = drflac__be2host_64(importantProps); + + pStreamInfo->minBlockSizeInPCMFrames = (drflac_uint16)((blockSizes & 0xFFFF0000) >> 16); + pStreamInfo->maxBlockSizeInPCMFrames = (drflac_uint16) (blockSizes & 0x0000FFFF); + pStreamInfo->minFrameSizeInPCMFrames = (drflac_uint32)((frameSizes & (((drflac_uint64)0x00FFFFFF << 16) << 24)) >> 40); + pStreamInfo->maxFrameSizeInPCMFrames = (drflac_uint32)((frameSizes & (((drflac_uint64)0x00FFFFFF << 16) << 0)) >> 16); + pStreamInfo->sampleRate = (drflac_uint32)((importantProps & (((drflac_uint64)0x000FFFFF << 16) << 28)) >> 44); + pStreamInfo->channels = (drflac_uint8 )((importantProps & (((drflac_uint64)0x0000000E << 16) << 24)) >> 41) + 1; + pStreamInfo->bitsPerSample = (drflac_uint8 )((importantProps & (((drflac_uint64)0x0000001F << 16) << 20)) >> 36) + 1; + pStreamInfo->totalPCMFrameCount = ((importantProps & ((((drflac_uint64)0x0000000F << 16) << 16) | 0xFFFFFFFF))); + DRFLAC_COPY_MEMORY(pStreamInfo->md5, md5, sizeof(md5)); + + return DRFLAC_TRUE; +} + + +static void* drflac__malloc_default(size_t sz, void* pUserData) +{ + (void)pUserData; + return DRFLAC_MALLOC(sz); +} + +static void* drflac__realloc_default(void* p, size_t sz, void* pUserData) +{ + (void)pUserData; + return DRFLAC_REALLOC(p, sz); +} + +static void drflac__free_default(void* p, void* pUserData) +{ + (void)pUserData; + DRFLAC_FREE(p); +} + + +static void* drflac__malloc_from_callbacks(size_t sz, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks == NULL) { + return NULL; + } + + if (pAllocationCallbacks->onMalloc != NULL) { + return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData); + } + + /* Try using realloc(). */ + if (pAllocationCallbacks->onRealloc != NULL) { + return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData); + } + + return NULL; +} + +static void* drflac__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks == NULL) { + return NULL; + } + + if (pAllocationCallbacks->onRealloc != NULL) { + return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData); + } + + /* Try emulating realloc() in terms of malloc()/free(). */ + if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) { + void* p2; + + p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData); + if (p2 == NULL) { + return NULL; + } + + if (p != NULL) { + DRFLAC_COPY_MEMORY(p2, p, DRFLAC_MIN(szNew, szOld)); + pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); + } + + return p2; + } + + return NULL; +} + +static void drflac__free_from_callbacks(void* p, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + if (p == NULL || pAllocationCallbacks == NULL) { + return; + } + + if (pAllocationCallbacks->onFree != NULL) { + pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); + } +} + + +static drflac_bool32 drflac__read_and_decode_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_uint64* pFirstFramePos, drflac_uint64* pSeektablePos, drflac_uint32* pSeekpointCount, drflac_allocation_callbacks* pAllocationCallbacks) +{ + /* + We want to keep track of the byte position in the stream of the seektable. At the time of calling this function we know that + we'll be sitting on byte 42. + */ + drflac_uint64 runningFilePos = 42; + drflac_uint64 seektablePos = 0; + drflac_uint32 seektableSize = 0; + drflac_int64 fileSize = 0; + drflac_bool32 hasKnownFileSize = DRFLAC_FALSE; + + /* We'll be doing some memory allocations here against untrusted data. We'll do a basic validation check that they don't exceed the size of the file. */ + if (onTell != NULL && onSeek != NULL) { + if (onSeek(pUserData, 0, DRFLAC_SEEK_END)) { + if (onTell(pUserData, &fileSize)) { + hasKnownFileSize = DRFLAC_TRUE; + } + + onSeek(pUserData, (int)runningFilePos, DRFLAC_SEEK_SET); /* Safe cast because runningFilePos should always be 42 at this point. */ + } + } + + for (;;) { + drflac_metadata metadata; + drflac_uint8 isLastBlock = 0; + drflac_uint8 blockType = 0; + drflac_uint32 blockSize; + if (drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize) == DRFLAC_FALSE) { + return DRFLAC_FALSE; + } + + if (hasKnownFileSize && (blockSize > ((drflac_uint64)fileSize - runningFilePos))) { + return DRFLAC_FALSE; /* Block size exceeds the size of the file. */ + } + + runningFilePos += 4; + + metadata.type = blockType; + metadata.rawDataSize = 0; + metadata.rawDataOffset = runningFilePos; + metadata.pRawData = NULL; + + switch (blockType) + { + case DRFLAC_METADATA_BLOCK_TYPE_APPLICATION: + { + if (blockSize < 4) { + return DRFLAC_FALSE; + } + + if (onMeta) { + void* pRawData = drflac__malloc_from_callbacks(blockSize, pAllocationCallbacks); + if (pRawData == NULL) { + return DRFLAC_FALSE; + } + + if (onRead(pUserData, pRawData, blockSize) != blockSize) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + metadata.pRawData = pRawData; + metadata.rawDataSize = blockSize; + metadata.data.application.id = drflac__be2host_32(*(drflac_uint32*)pRawData); + metadata.data.application.pData = (const void*)((drflac_uint8*)pRawData + sizeof(drflac_uint32)); + metadata.data.application.dataSize = blockSize - sizeof(drflac_uint32); + onMeta(pUserDataMD, &metadata); + + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE: + { + seektablePos = runningFilePos; + seektableSize = blockSize; + + if (onMeta) { + drflac_uint32 seekpointCount; + drflac_uint32 iSeekpoint; + void* pRawData; + size_t rawDataSize; + + seekpointCount = blockSize/DRFLAC_SEEKPOINT_SIZE_IN_BYTES; + rawDataSize = seekpointCount * sizeof(drflac_seekpoint); + + pRawData = drflac__malloc_from_callbacks(rawDataSize, pAllocationCallbacks); + if (pRawData == NULL) { + return DRFLAC_FALSE; + } + + /* We need to read seekpoint by seekpoint and do some processing. */ + for (iSeekpoint = 0; iSeekpoint < seekpointCount; ++iSeekpoint) { + drflac_seekpoint* pSeekpoint = (drflac_seekpoint*)pRawData + iSeekpoint; + + if (onRead(pUserData, pSeekpoint, DRFLAC_SEEKPOINT_SIZE_IN_BYTES) != DRFLAC_SEEKPOINT_SIZE_IN_BYTES) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + /* Endian swap. */ + pSeekpoint->firstPCMFrame = drflac__be2host_64(pSeekpoint->firstPCMFrame); + pSeekpoint->flacFrameOffset = drflac__be2host_64(pSeekpoint->flacFrameOffset); + pSeekpoint->pcmFrameCount = drflac__be2host_16(pSeekpoint->pcmFrameCount); + } + + metadata.pRawData = pRawData; + metadata.rawDataSize = rawDataSize; + metadata.data.seektable.seekpointCount = seekpointCount; + metadata.data.seektable.pSeekpoints = (const drflac_seekpoint*)pRawData; + + onMeta(pUserDataMD, &metadata); + + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT: + { + if (blockSize < 8) { + return DRFLAC_FALSE; + } + + if (onMeta) { + void* pRawData; + const char* pRunningData; + const char* pRunningDataEnd; + drflac_uint32 i; + + pRawData = drflac__malloc_from_callbacks(blockSize, pAllocationCallbacks); + if (pRawData == NULL) { + return DRFLAC_FALSE; + } + + if (onRead(pUserData, pRawData, blockSize) != blockSize) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + metadata.pRawData = pRawData; + metadata.rawDataSize = blockSize; + + pRunningData = (const char*)pRawData; + pRunningDataEnd = (const char*)pRawData + blockSize; + + metadata.data.vorbis_comment.vendorLength = drflac__le2host_32_ptr_unaligned(pRunningData); pRunningData += 4; + + /* Need space for the rest of the block */ + if ((pRunningDataEnd - pRunningData) - 4 < (drflac_int64)metadata.data.vorbis_comment.vendorLength) { /* <-- Note the order of operations to avoid overflow to a valid value */ + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + metadata.data.vorbis_comment.vendor = pRunningData; pRunningData += metadata.data.vorbis_comment.vendorLength; + metadata.data.vorbis_comment.commentCount = drflac__le2host_32_ptr_unaligned(pRunningData); pRunningData += 4; + + /* Need space for 'commentCount' comments after the block, which at minimum is a drflac_uint32 per comment */ + if ((pRunningDataEnd - pRunningData) / sizeof(drflac_uint32) < metadata.data.vorbis_comment.commentCount) { /* <-- Note the order of operations to avoid overflow to a valid value */ + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + metadata.data.vorbis_comment.pComments = pRunningData; + + /* Check that the comments section is valid before passing it to the callback */ + for (i = 0; i < metadata.data.vorbis_comment.commentCount; ++i) { + drflac_uint32 commentLength; + + if (pRunningDataEnd - pRunningData < 4) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + commentLength = drflac__le2host_32_ptr_unaligned(pRunningData); pRunningData += 4; + if (pRunningDataEnd - pRunningData < (drflac_int64)commentLength) { /* <-- Note the order of operations to avoid overflow to a valid value */ + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + pRunningData += commentLength; + } + + onMeta(pUserDataMD, &metadata); + + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_CUESHEET: + { + if (blockSize < 396) { + return DRFLAC_FALSE; + } + + if (onMeta) { + void* pRawData; + const char* pRunningData; + const char* pRunningDataEnd; + size_t bufferSize; + drflac_uint8 iTrack; + drflac_uint8 iIndex; + void* pTrackData; + + /* + This needs to be loaded in two passes. The first pass is used to calculate the size of the memory allocation + we need for storing the necessary data. The second pass will fill that buffer with usable data. + */ + pRawData = drflac__malloc_from_callbacks(blockSize, pAllocationCallbacks); + if (pRawData == NULL) { + return DRFLAC_FALSE; + } + + if (onRead(pUserData, pRawData, blockSize) != blockSize) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + metadata.pRawData = pRawData; + metadata.rawDataSize = blockSize; + + pRunningData = (const char*)pRawData; + pRunningDataEnd = (const char*)pRawData + blockSize; + + DRFLAC_COPY_MEMORY(metadata.data.cuesheet.catalog, pRunningData, 128); pRunningData += 128; + metadata.data.cuesheet.leadInSampleCount = drflac__be2host_64(*(const drflac_uint64*)pRunningData); pRunningData += 8; + metadata.data.cuesheet.isCD = (pRunningData[0] & 0x80) != 0; pRunningData += 259; + metadata.data.cuesheet.trackCount = pRunningData[0]; pRunningData += 1; + metadata.data.cuesheet.pTrackData = NULL; /* Will be filled later. */ + + /* Pass 1: Calculate the size of the buffer for the track data. */ + { + const char* pRunningDataSaved = pRunningData; /* Will be restored at the end in preparation for the second pass. */ + + bufferSize = metadata.data.cuesheet.trackCount * DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES; + + for (iTrack = 0; iTrack < metadata.data.cuesheet.trackCount; ++iTrack) { + drflac_uint8 indexCount; + drflac_uint32 indexPointSize; + + if (pRunningDataEnd - pRunningData < DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + /* Skip to the index point count */ + pRunningData += 35; + + indexCount = pRunningData[0]; + pRunningData += 1; + + bufferSize += indexCount * sizeof(drflac_cuesheet_track_index); + + /* Quick validation check. */ + indexPointSize = indexCount * DRFLAC_CUESHEET_TRACK_INDEX_SIZE_IN_BYTES; + if (pRunningDataEnd - pRunningData < (drflac_int64)indexPointSize) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + pRunningData += indexPointSize; + } + + pRunningData = pRunningDataSaved; + } + + /* Pass 2: Allocate a buffer and fill the data. Validation was done in the step above so can be skipped. */ + { + char* pRunningTrackData; + + pTrackData = drflac__malloc_from_callbacks(bufferSize, pAllocationCallbacks); + if (pTrackData == NULL) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + + pRunningTrackData = (char*)pTrackData; + + for (iTrack = 0; iTrack < metadata.data.cuesheet.trackCount; ++iTrack) { + drflac_uint8 indexCount; + + DRFLAC_COPY_MEMORY(pRunningTrackData, pRunningData, DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES); + pRunningData += DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES-1; /* Skip forward, but not beyond the last byte in the CUESHEET_TRACK block which is the index count. */ + pRunningTrackData += DRFLAC_CUESHEET_TRACK_SIZE_IN_BYTES-1; + + /* Grab the index count for the next part. */ + indexCount = pRunningData[0]; + pRunningData += 1; + pRunningTrackData += 1; + + /* Extract each track index. */ + for (iIndex = 0; iIndex < indexCount; ++iIndex) { + drflac_cuesheet_track_index* pTrackIndex = (drflac_cuesheet_track_index*)pRunningTrackData; + + DRFLAC_COPY_MEMORY(pRunningTrackData, pRunningData, DRFLAC_CUESHEET_TRACK_INDEX_SIZE_IN_BYTES); + pRunningData += DRFLAC_CUESHEET_TRACK_INDEX_SIZE_IN_BYTES; + pRunningTrackData += sizeof(drflac_cuesheet_track_index); + + pTrackIndex->offset = drflac__be2host_64(pTrackIndex->offset); + } + } + + metadata.data.cuesheet.pTrackData = pTrackData; + } + + /* The original data is no longer needed. */ + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + pRawData = NULL; + + onMeta(pUserDataMD, &metadata); + + drflac__free_from_callbacks(pTrackData, pAllocationCallbacks); + pTrackData = NULL; + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_PICTURE: + { + if (blockSize < 32) { + return DRFLAC_FALSE; + } + + if (onMeta) { + drflac_bool32 result = DRFLAC_TRUE; + drflac_uint32 blockSizeRemaining = blockSize; + char* pMime = NULL; + char* pDescription = NULL; + void* pPictureData = NULL; + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.type, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.type = drflac__be2host_32(metadata.data.picture.type); + + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.mimeLength, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.mimeLength = drflac__be2host_32(metadata.data.picture.mimeLength); + + if (blockSizeRemaining < metadata.data.picture.mimeLength) { + result = DRFLAC_FALSE; + goto done_flac; + } + + pMime = (char*)drflac__malloc_from_callbacks(metadata.data.picture.mimeLength + 1, pAllocationCallbacks); /* +1 for null terminator. */ + if (pMime == NULL) { + result = DRFLAC_FALSE; + goto done_flac; + } + + if (onRead(pUserData, pMime, metadata.data.picture.mimeLength) != metadata.data.picture.mimeLength) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= metadata.data.picture.mimeLength; + pMime[metadata.data.picture.mimeLength] = '\0'; /* Null terminate for safety. */ + metadata.data.picture.mime = (const char*)pMime; + + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.descriptionLength, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.descriptionLength = drflac__be2host_32(metadata.data.picture.descriptionLength); + + if (blockSizeRemaining < metadata.data.picture.descriptionLength) { + result = DRFLAC_FALSE; + goto done_flac; + } + + pDescription = (char*)drflac__malloc_from_callbacks(metadata.data.picture.descriptionLength + 1, pAllocationCallbacks); /* +1 for null terminator. */ + if (pDescription == NULL) { + result = DRFLAC_FALSE; + goto done_flac; + } + + if (onRead(pUserData, pDescription, metadata.data.picture.descriptionLength) != metadata.data.picture.descriptionLength) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= metadata.data.picture.descriptionLength; + pDescription[metadata.data.picture.descriptionLength] = '\0'; /* Null terminate for safety. */ + metadata.data.picture.description = (const char*)pDescription; + + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.width, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.width = drflac__be2host_32(metadata.data.picture.width); + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.height, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.height = drflac__be2host_32(metadata.data.picture.height); + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.colorDepth, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.colorDepth = drflac__be2host_32(metadata.data.picture.colorDepth); + + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.indexColorCount, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.indexColorCount = drflac__be2host_32(metadata.data.picture.indexColorCount); + + + /* Picture data. */ + if (blockSizeRemaining < 4 || onRead(pUserData, &metadata.data.picture.pictureDataSize, 4) != 4) { + result = DRFLAC_FALSE; + goto done_flac; + } + blockSizeRemaining -= 4; + metadata.data.picture.pictureDataSize = drflac__be2host_32(metadata.data.picture.pictureDataSize); + + if (blockSizeRemaining < metadata.data.picture.pictureDataSize) { + result = DRFLAC_FALSE; + goto done_flac; + } + + /* For the actual image data we want to store the offset to the start of the stream. */ + metadata.data.picture.pictureDataOffset = runningFilePos + (blockSize - blockSizeRemaining); + + /* + For the allocation of image data, we can allow memory allocation to fail, in which case we just leave + the pointer as null. If it fails, we need to fall back to seeking past the image data. + */ + #ifndef DR_FLAC_NO_PICTURE_METADATA_MALLOC + pPictureData = drflac__malloc_from_callbacks(metadata.data.picture.pictureDataSize, pAllocationCallbacks); + if (pPictureData != NULL) { + if (onRead(pUserData, pPictureData, metadata.data.picture.pictureDataSize) != metadata.data.picture.pictureDataSize) { + result = DRFLAC_FALSE; + goto done_flac; + } + } else + #endif + { + /* Allocation failed. We need to seek past the picture data. */ + if (!onSeek(pUserData, metadata.data.picture.pictureDataSize, DRFLAC_SEEK_CUR)) { + result = DRFLAC_FALSE; + goto done_flac; + } + } + + blockSizeRemaining -= metadata.data.picture.pictureDataSize; + (void)blockSizeRemaining; + + metadata.data.picture.pPictureData = (const drflac_uint8*)pPictureData; + + + /* Only fire the callback if we actually have a way to read the image data. We must have either a valid offset, or a valid data pointer. */ + if (metadata.data.picture.pictureDataOffset != 0 || metadata.data.picture.pPictureData != NULL) { + onMeta(pUserDataMD, &metadata); + } else { + /* Don't have a valid offset or data pointer, so just pretend we don't have a picture metadata. */ + } + + done_flac: + drflac__free_from_callbacks(pMime, pAllocationCallbacks); + drflac__free_from_callbacks(pDescription, pAllocationCallbacks); + drflac__free_from_callbacks(pPictureData, pAllocationCallbacks); + + if (result != DRFLAC_TRUE) { + return DRFLAC_FALSE; + } + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_PADDING: + { + if (onMeta) { + metadata.data.padding.unused = 0; + + /* Padding doesn't have anything meaningful in it, so just skip over it, but make sure the caller is aware of it by firing the callback. */ + if (!onSeek(pUserData, blockSize, DRFLAC_SEEK_CUR)) { + isLastBlock = DRFLAC_TRUE; /* An error occurred while seeking. Attempt to recover by treating this as the last block which will in turn terminate the loop. */ + } else { + onMeta(pUserDataMD, &metadata); + } + } + } break; + + case DRFLAC_METADATA_BLOCK_TYPE_INVALID: + { + /* Invalid chunk. Just skip over this one. */ + if (onMeta) { + if (!onSeek(pUserData, blockSize, DRFLAC_SEEK_CUR)) { + isLastBlock = DRFLAC_TRUE; /* An error occurred while seeking. Attempt to recover by treating this as the last block which will in turn terminate the loop. */ + } + } + } break; + + default: + { + /* + It's an unknown chunk, but not necessarily invalid. There's a chance more metadata blocks might be defined later on, so we + can at the very least report the chunk to the application and let it look at the raw data. + */ + if (onMeta) { + void* pRawData = drflac__malloc_from_callbacks(blockSize, pAllocationCallbacks); + if (pRawData != NULL) { + if (onRead(pUserData, pRawData, blockSize) != blockSize) { + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + return DRFLAC_FALSE; + } + } else { + /* Allocation failed. We need to seek past the block. */ + if (!onSeek(pUserData, blockSize, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + } + + metadata.pRawData = pRawData; + metadata.rawDataSize = blockSize; + onMeta(pUserDataMD, &metadata); + + drflac__free_from_callbacks(pRawData, pAllocationCallbacks); + } + } break; + } + + /* If we're not handling metadata, just skip over the block. If we are, it will have been handled earlier in the switch statement above. */ + if (onMeta == NULL && blockSize > 0) { + if (!onSeek(pUserData, blockSize, DRFLAC_SEEK_CUR)) { + isLastBlock = DRFLAC_TRUE; + } + } + + runningFilePos += blockSize; + if (isLastBlock) { + break; + } + } + + *pSeektablePos = seektablePos; + *pSeekpointCount = seektableSize / DRFLAC_SEEKPOINT_SIZE_IN_BYTES; + *pFirstFramePos = runningFilePos; + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__init_private__native(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_bool32 relaxed) +{ + /* Pre Condition: The bit stream should be sitting just past the 4-byte id header. */ + + drflac_uint8 isLastBlock; + drflac_uint8 blockType; + drflac_uint32 blockSize; + + (void)onSeek; + + pInit->container = drflac_container_native; + + /* The first metadata block should be the STREAMINFO block. */ + if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) { + return DRFLAC_FALSE; + } + + if (blockType != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO || blockSize != 34) { + if (!relaxed) { + /* We're opening in strict mode and the first block is not the STREAMINFO block. Error. */ + return DRFLAC_FALSE; + } else { + /* + Relaxed mode. To open from here we need to just find the first frame and set the sample rate, etc. to whatever is defined + for that frame. + */ + pInit->hasStreamInfoBlock = DRFLAC_FALSE; + pInit->hasMetadataBlocks = DRFLAC_FALSE; + + if (!drflac__read_next_flac_frame_header(&pInit->bs, 0, &pInit->firstFrameHeader)) { + return DRFLAC_FALSE; /* Couldn't find a frame. */ + } + + if (pInit->firstFrameHeader.bitsPerSample == 0) { + return DRFLAC_FALSE; /* Failed to initialize because the first frame depends on the STREAMINFO block, which does not exist. */ + } + + pInit->sampleRate = pInit->firstFrameHeader.sampleRate; + pInit->channels = drflac__get_channel_count_from_channel_assignment(pInit->firstFrameHeader.channelAssignment); + pInit->bitsPerSample = pInit->firstFrameHeader.bitsPerSample; + pInit->maxBlockSizeInPCMFrames = 65535; /* <-- See notes here: https://xiph.org/flac/format.html#metadata_block_streaminfo */ + return DRFLAC_TRUE; + } + } else { + drflac_streaminfo streaminfo; + if (!drflac__read_streaminfo(onRead, pUserData, &streaminfo)) { + return DRFLAC_FALSE; + } + + pInit->hasStreamInfoBlock = DRFLAC_TRUE; + pInit->sampleRate = streaminfo.sampleRate; + pInit->channels = streaminfo.channels; + pInit->bitsPerSample = streaminfo.bitsPerSample; + pInit->totalPCMFrameCount = streaminfo.totalPCMFrameCount; + pInit->maxBlockSizeInPCMFrames = streaminfo.maxBlockSizeInPCMFrames; /* Don't care about the min block size - only the max (used for determining the size of the memory allocation). */ + pInit->hasMetadataBlocks = !isLastBlock; + + if (onMeta) { + drflac_metadata metadata; + metadata.type = DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO; + metadata.pRawData = NULL; + metadata.rawDataSize = 0; + metadata.data.streaminfo = streaminfo; + onMeta(pUserDataMD, &metadata); + } + + return DRFLAC_TRUE; + } +} + +#ifndef DR_FLAC_NO_OGG +#define DRFLAC_OGG_MAX_PAGE_SIZE 65307 +#define DRFLAC_OGG_CAPTURE_PATTERN_CRC32 1605413199 /* CRC-32 of "OggS". */ + +typedef enum +{ + drflac_ogg_recover_on_crc_mismatch, + drflac_ogg_fail_on_crc_mismatch +} drflac_ogg_crc_mismatch_recovery; + +#ifndef DR_FLAC_NO_CRC +static drflac_uint32 drflac__crc32_table[] = { + 0x00000000L, 0x04C11DB7L, 0x09823B6EL, 0x0D4326D9L, + 0x130476DCL, 0x17C56B6BL, 0x1A864DB2L, 0x1E475005L, + 0x2608EDB8L, 0x22C9F00FL, 0x2F8AD6D6L, 0x2B4BCB61L, + 0x350C9B64L, 0x31CD86D3L, 0x3C8EA00AL, 0x384FBDBDL, + 0x4C11DB70L, 0x48D0C6C7L, 0x4593E01EL, 0x4152FDA9L, + 0x5F15ADACL, 0x5BD4B01BL, 0x569796C2L, 0x52568B75L, + 0x6A1936C8L, 0x6ED82B7FL, 0x639B0DA6L, 0x675A1011L, + 0x791D4014L, 0x7DDC5DA3L, 0x709F7B7AL, 0x745E66CDL, + 0x9823B6E0L, 0x9CE2AB57L, 0x91A18D8EL, 0x95609039L, + 0x8B27C03CL, 0x8FE6DD8BL, 0x82A5FB52L, 0x8664E6E5L, + 0xBE2B5B58L, 0xBAEA46EFL, 0xB7A96036L, 0xB3687D81L, + 0xAD2F2D84L, 0xA9EE3033L, 0xA4AD16EAL, 0xA06C0B5DL, + 0xD4326D90L, 0xD0F37027L, 0xDDB056FEL, 0xD9714B49L, + 0xC7361B4CL, 0xC3F706FBL, 0xCEB42022L, 0xCA753D95L, + 0xF23A8028L, 0xF6FB9D9FL, 0xFBB8BB46L, 0xFF79A6F1L, + 0xE13EF6F4L, 0xE5FFEB43L, 0xE8BCCD9AL, 0xEC7DD02DL, + 0x34867077L, 0x30476DC0L, 0x3D044B19L, 0x39C556AEL, + 0x278206ABL, 0x23431B1CL, 0x2E003DC5L, 0x2AC12072L, + 0x128E9DCFL, 0x164F8078L, 0x1B0CA6A1L, 0x1FCDBB16L, + 0x018AEB13L, 0x054BF6A4L, 0x0808D07DL, 0x0CC9CDCAL, + 0x7897AB07L, 0x7C56B6B0L, 0x71159069L, 0x75D48DDEL, + 0x6B93DDDBL, 0x6F52C06CL, 0x6211E6B5L, 0x66D0FB02L, + 0x5E9F46BFL, 0x5A5E5B08L, 0x571D7DD1L, 0x53DC6066L, + 0x4D9B3063L, 0x495A2DD4L, 0x44190B0DL, 0x40D816BAL, + 0xACA5C697L, 0xA864DB20L, 0xA527FDF9L, 0xA1E6E04EL, + 0xBFA1B04BL, 0xBB60ADFCL, 0xB6238B25L, 0xB2E29692L, + 0x8AAD2B2FL, 0x8E6C3698L, 0x832F1041L, 0x87EE0DF6L, + 0x99A95DF3L, 0x9D684044L, 0x902B669DL, 0x94EA7B2AL, + 0xE0B41DE7L, 0xE4750050L, 0xE9362689L, 0xEDF73B3EL, + 0xF3B06B3BL, 0xF771768CL, 0xFA325055L, 0xFEF34DE2L, + 0xC6BCF05FL, 0xC27DEDE8L, 0xCF3ECB31L, 0xCBFFD686L, + 0xD5B88683L, 0xD1799B34L, 0xDC3ABDEDL, 0xD8FBA05AL, + 0x690CE0EEL, 0x6DCDFD59L, 0x608EDB80L, 0x644FC637L, + 0x7A089632L, 0x7EC98B85L, 0x738AAD5CL, 0x774BB0EBL, + 0x4F040D56L, 0x4BC510E1L, 0x46863638L, 0x42472B8FL, + 0x5C007B8AL, 0x58C1663DL, 0x558240E4L, 0x51435D53L, + 0x251D3B9EL, 0x21DC2629L, 0x2C9F00F0L, 0x285E1D47L, + 0x36194D42L, 0x32D850F5L, 0x3F9B762CL, 0x3B5A6B9BL, + 0x0315D626L, 0x07D4CB91L, 0x0A97ED48L, 0x0E56F0FFL, + 0x1011A0FAL, 0x14D0BD4DL, 0x19939B94L, 0x1D528623L, + 0xF12F560EL, 0xF5EE4BB9L, 0xF8AD6D60L, 0xFC6C70D7L, + 0xE22B20D2L, 0xE6EA3D65L, 0xEBA91BBCL, 0xEF68060BL, + 0xD727BBB6L, 0xD3E6A601L, 0xDEA580D8L, 0xDA649D6FL, + 0xC423CD6AL, 0xC0E2D0DDL, 0xCDA1F604L, 0xC960EBB3L, + 0xBD3E8D7EL, 0xB9FF90C9L, 0xB4BCB610L, 0xB07DABA7L, + 0xAE3AFBA2L, 0xAAFBE615L, 0xA7B8C0CCL, 0xA379DD7BL, + 0x9B3660C6L, 0x9FF77D71L, 0x92B45BA8L, 0x9675461FL, + 0x8832161AL, 0x8CF30BADL, 0x81B02D74L, 0x857130C3L, + 0x5D8A9099L, 0x594B8D2EL, 0x5408ABF7L, 0x50C9B640L, + 0x4E8EE645L, 0x4A4FFBF2L, 0x470CDD2BL, 0x43CDC09CL, + 0x7B827D21L, 0x7F436096L, 0x7200464FL, 0x76C15BF8L, + 0x68860BFDL, 0x6C47164AL, 0x61043093L, 0x65C52D24L, + 0x119B4BE9L, 0x155A565EL, 0x18197087L, 0x1CD86D30L, + 0x029F3D35L, 0x065E2082L, 0x0B1D065BL, 0x0FDC1BECL, + 0x3793A651L, 0x3352BBE6L, 0x3E119D3FL, 0x3AD08088L, + 0x2497D08DL, 0x2056CD3AL, 0x2D15EBE3L, 0x29D4F654L, + 0xC5A92679L, 0xC1683BCEL, 0xCC2B1D17L, 0xC8EA00A0L, + 0xD6AD50A5L, 0xD26C4D12L, 0xDF2F6BCBL, 0xDBEE767CL, + 0xE3A1CBC1L, 0xE760D676L, 0xEA23F0AFL, 0xEEE2ED18L, + 0xF0A5BD1DL, 0xF464A0AAL, 0xF9278673L, 0xFDE69BC4L, + 0x89B8FD09L, 0x8D79E0BEL, 0x803AC667L, 0x84FBDBD0L, + 0x9ABC8BD5L, 0x9E7D9662L, 0x933EB0BBL, 0x97FFAD0CL, + 0xAFB010B1L, 0xAB710D06L, 0xA6322BDFL, 0xA2F33668L, + 0xBCB4666DL, 0xB8757BDAL, 0xB5365D03L, 0xB1F740B4L +}; +#endif + +static DRFLAC_INLINE drflac_uint32 drflac_crc32_byte(drflac_uint32 crc32, drflac_uint8 data) +{ +#ifndef DR_FLAC_NO_CRC + return (crc32 << 8) ^ drflac__crc32_table[(drflac_uint8)((crc32 >> 24) & 0xFF) ^ data]; +#else + (void)data; + return crc32; +#endif +} + +#if 0 +static DRFLAC_INLINE drflac_uint32 drflac_crc32_uint32(drflac_uint32 crc32, drflac_uint32 data) +{ + crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 24) & 0xFF)); + crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 16) & 0xFF)); + crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 8) & 0xFF)); + crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 0) & 0xFF)); + return crc32; +} + +static DRFLAC_INLINE drflac_uint32 drflac_crc32_uint64(drflac_uint32 crc32, drflac_uint64 data) +{ + crc32 = drflac_crc32_uint32(crc32, (drflac_uint32)((data >> 32) & 0xFFFFFFFF)); + crc32 = drflac_crc32_uint32(crc32, (drflac_uint32)((data >> 0) & 0xFFFFFFFF)); + return crc32; +} +#endif + +static DRFLAC_INLINE drflac_uint32 drflac_crc32_buffer(drflac_uint32 crc32, drflac_uint8* pData, drflac_uint32 dataSize) +{ + /* This can be optimized. */ + drflac_uint32 i; + for (i = 0; i < dataSize; ++i) { + crc32 = drflac_crc32_byte(crc32, pData[i]); + } + return crc32; +} + + +static DRFLAC_INLINE drflac_bool32 drflac_ogg__is_capture_pattern(drflac_uint8 pattern[4]) +{ + return pattern[0] == 'O' && pattern[1] == 'g' && pattern[2] == 'g' && pattern[3] == 'S'; +} + +static DRFLAC_INLINE drflac_uint32 drflac_ogg__get_page_header_size(drflac_ogg_page_header* pHeader) +{ + return 27 + pHeader->segmentCount; +} + +static DRFLAC_INLINE drflac_uint32 drflac_ogg__get_page_body_size(drflac_ogg_page_header* pHeader) +{ + drflac_uint32 pageBodySize = 0; + int i; + + for (i = 0; i < pHeader->segmentCount; ++i) { + pageBodySize += pHeader->segmentTable[i]; + } + + return pageBodySize; +} + +static drflac_result drflac_ogg__read_page_header_after_capture_pattern(drflac_read_proc onRead, void* pUserData, drflac_ogg_page_header* pHeader, drflac_uint32* pBytesRead, drflac_uint32* pCRC32) +{ + drflac_uint8 data[23]; + drflac_uint32 i; + + DRFLAC_ASSERT(*pCRC32 == DRFLAC_OGG_CAPTURE_PATTERN_CRC32); + + if (onRead(pUserData, data, 23) != 23) { + return DRFLAC_AT_END; + } + *pBytesRead += 23; + + /* + It's not actually used, but set the capture pattern to 'OggS' for completeness. Not doing this will cause static analysers to complain about + us trying to access uninitialized data. We could alternatively just comment out this member of the drflac_ogg_page_header structure, but I + like to have it map to the structure of the underlying data. + */ + pHeader->capturePattern[0] = 'O'; + pHeader->capturePattern[1] = 'g'; + pHeader->capturePattern[2] = 'g'; + pHeader->capturePattern[3] = 'S'; + + pHeader->structureVersion = data[0]; + pHeader->headerType = data[1]; + DRFLAC_COPY_MEMORY(&pHeader->granulePosition, &data[ 2], 8); + DRFLAC_COPY_MEMORY(&pHeader->serialNumber, &data[10], 4); + DRFLAC_COPY_MEMORY(&pHeader->sequenceNumber, &data[14], 4); + DRFLAC_COPY_MEMORY(&pHeader->checksum, &data[18], 4); + pHeader->segmentCount = data[22]; + + /* Calculate the CRC. Note that for the calculation the checksum part of the page needs to be set to 0. */ + data[18] = 0; + data[19] = 0; + data[20] = 0; + data[21] = 0; + + for (i = 0; i < 23; ++i) { + *pCRC32 = drflac_crc32_byte(*pCRC32, data[i]); + } + + + if (onRead(pUserData, pHeader->segmentTable, pHeader->segmentCount) != pHeader->segmentCount) { + return DRFLAC_AT_END; + } + *pBytesRead += pHeader->segmentCount; + + for (i = 0; i < pHeader->segmentCount; ++i) { + *pCRC32 = drflac_crc32_byte(*pCRC32, pHeader->segmentTable[i]); + } + + return DRFLAC_SUCCESS; +} + +static drflac_result drflac_ogg__read_page_header(drflac_read_proc onRead, void* pUserData, drflac_ogg_page_header* pHeader, drflac_uint32* pBytesRead, drflac_uint32* pCRC32) +{ + drflac_uint8 id[4]; + + *pBytesRead = 0; + + if (onRead(pUserData, id, 4) != 4) { + return DRFLAC_AT_END; + } + *pBytesRead += 4; + + /* We need to read byte-by-byte until we find the OggS capture pattern. */ + for (;;) { + if (drflac_ogg__is_capture_pattern(id)) { + drflac_result result; + + *pCRC32 = DRFLAC_OGG_CAPTURE_PATTERN_CRC32; + + result = drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, pHeader, pBytesRead, pCRC32); + if (result == DRFLAC_SUCCESS) { + return DRFLAC_SUCCESS; + } else { + if (result == DRFLAC_CRC_MISMATCH) { + continue; + } else { + return result; + } + } + } else { + /* The first 4 bytes did not equal the capture pattern. Read the next byte and try again. */ + id[0] = id[1]; + id[1] = id[2]; + id[2] = id[3]; + if (onRead(pUserData, &id[3], 1) != 1) { + return DRFLAC_AT_END; + } + *pBytesRead += 1; + } + } +} + + +/* +The main part of the Ogg encapsulation is the conversion from the physical Ogg bitstream to the native FLAC bitstream. It works +in three general stages: Ogg Physical Bitstream -> Ogg/FLAC Logical Bitstream -> FLAC Native Bitstream. dr_flac is designed +in such a way that the core sections assume everything is delivered in native format. Therefore, for each encapsulation type +dr_flac is supporting there needs to be a layer sitting on top of the onRead and onSeek callbacks that ensures the bits read from +the physical Ogg bitstream are converted and delivered in native FLAC format. +*/ +typedef struct +{ + drflac_read_proc onRead; /* The original onRead callback from drflac_open() and family. */ + drflac_seek_proc onSeek; /* The original onSeek callback from drflac_open() and family. */ + drflac_tell_proc onTell; /* The original onTell callback from drflac_open() and family. */ + void* pUserData; /* The user data passed on onRead and onSeek. This is the user data that was passed on drflac_open() and family. */ + drflac_uint64 currentBytePos; /* The position of the byte we are sitting on in the physical byte stream. Used for efficient seeking. */ + drflac_uint64 firstBytePos; /* The position of the first byte in the physical bitstream. Points to the start of the "OggS" identifier of the FLAC bos page. */ + drflac_uint32 serialNumber; /* The serial number of the FLAC audio pages. This is determined by the initial header page that was read during initialization. */ + drflac_ogg_page_header bosPageHeader; /* Used for seeking. */ + drflac_ogg_page_header currentPageHeader; + drflac_uint32 bytesRemainingInPage; + drflac_uint32 pageDataSize; + drflac_uint8 pageData[DRFLAC_OGG_MAX_PAGE_SIZE]; +} drflac_oggbs; /* oggbs = Ogg Bitstream */ + +static size_t drflac_oggbs__read_physical(drflac_oggbs* oggbs, void* bufferOut, size_t bytesToRead) +{ + size_t bytesActuallyRead = oggbs->onRead(oggbs->pUserData, bufferOut, bytesToRead); + oggbs->currentBytePos += bytesActuallyRead; + + return bytesActuallyRead; +} + +static drflac_bool32 drflac_oggbs__seek_physical(drflac_oggbs* oggbs, drflac_uint64 offset, drflac_seek_origin origin) +{ + if (origin == DRFLAC_SEEK_SET) { + if (offset <= 0x7FFFFFFF) { + if (!oggbs->onSeek(oggbs->pUserData, (int)offset, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + oggbs->currentBytePos = offset; + + return DRFLAC_TRUE; + } else { + if (!oggbs->onSeek(oggbs->pUserData, 0x7FFFFFFF, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + oggbs->currentBytePos = offset; + + return drflac_oggbs__seek_physical(oggbs, offset - 0x7FFFFFFF, DRFLAC_SEEK_CUR); + } + } else { + while (offset > 0x7FFFFFFF) { + if (!oggbs->onSeek(oggbs->pUserData, 0x7FFFFFFF, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + oggbs->currentBytePos += 0x7FFFFFFF; + offset -= 0x7FFFFFFF; + } + + if (!oggbs->onSeek(oggbs->pUserData, (int)offset, DRFLAC_SEEK_CUR)) { /* <-- Safe cast thanks to the loop above. */ + return DRFLAC_FALSE; + } + oggbs->currentBytePos += offset; + + return DRFLAC_TRUE; + } +} + +static drflac_bool32 drflac_oggbs__goto_next_page(drflac_oggbs* oggbs, drflac_ogg_crc_mismatch_recovery recoveryMethod) +{ + drflac_ogg_page_header header; + for (;;) { + drflac_uint32 crc32 = 0; + drflac_uint32 bytesRead; + drflac_uint32 pageBodySize; +#ifndef DR_FLAC_NO_CRC + drflac_uint32 actualCRC32; +#endif + + if (drflac_ogg__read_page_header(oggbs->onRead, oggbs->pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) { + return DRFLAC_FALSE; + } + oggbs->currentBytePos += bytesRead; + + pageBodySize = drflac_ogg__get_page_body_size(&header); + if (pageBodySize > DRFLAC_OGG_MAX_PAGE_SIZE) { + continue; /* Invalid page size. Assume it's corrupted and just move to the next page. */ + } + + if (header.serialNumber != oggbs->serialNumber) { + /* It's not a FLAC page. Skip it. */ + if (pageBodySize > 0 && !drflac_oggbs__seek_physical(oggbs, pageBodySize, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + continue; + } + + + /* We need to read the entire page and then do a CRC check on it. If there's a CRC mismatch we need to skip this page. */ + if (drflac_oggbs__read_physical(oggbs, oggbs->pageData, pageBodySize) != pageBodySize) { + return DRFLAC_FALSE; + } + oggbs->pageDataSize = pageBodySize; + +#ifndef DR_FLAC_NO_CRC + actualCRC32 = drflac_crc32_buffer(crc32, oggbs->pageData, oggbs->pageDataSize); + if (actualCRC32 != header.checksum) { + if (recoveryMethod == drflac_ogg_recover_on_crc_mismatch) { + continue; /* CRC mismatch. Skip this page. */ + } else { + /* + Even though we are failing on a CRC mismatch, we still want our stream to be in a good state. Therefore we + go to the next valid page to ensure we're in a good state, but return false to let the caller know that the + seek did not fully complete. + */ + drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch); + return DRFLAC_FALSE; + } + } +#else + (void)recoveryMethod; /* <-- Silence a warning. */ +#endif + + oggbs->currentPageHeader = header; + oggbs->bytesRemainingInPage = pageBodySize; + return DRFLAC_TRUE; + } +} + +/* Function below is unused at the moment, but I might be re-adding it later. */ +#if 0 +static drflac_uint8 drflac_oggbs__get_current_segment_index(drflac_oggbs* oggbs, drflac_uint8* pBytesRemainingInSeg) +{ + drflac_uint32 bytesConsumedInPage = drflac_ogg__get_page_body_size(&oggbs->currentPageHeader) - oggbs->bytesRemainingInPage; + drflac_uint8 iSeg = 0; + drflac_uint32 iByte = 0; + while (iByte < bytesConsumedInPage) { + drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg]; + if (iByte + segmentSize > bytesConsumedInPage) { + break; + } else { + iSeg += 1; + iByte += segmentSize; + } + } + + *pBytesRemainingInSeg = oggbs->currentPageHeader.segmentTable[iSeg] - (drflac_uint8)(bytesConsumedInPage - iByte); + return iSeg; +} + +static drflac_bool32 drflac_oggbs__seek_to_next_packet(drflac_oggbs* oggbs) +{ + /* The current packet ends when we get to the segment with a lacing value of < 255 which is not at the end of a page. */ + for (;;) { + drflac_bool32 atEndOfPage = DRFLAC_FALSE; + + drflac_uint8 bytesRemainingInSeg; + drflac_uint8 iFirstSeg = drflac_oggbs__get_current_segment_index(oggbs, &bytesRemainingInSeg); + + drflac_uint32 bytesToEndOfPacketOrPage = bytesRemainingInSeg; + for (drflac_uint8 iSeg = iFirstSeg; iSeg < oggbs->currentPageHeader.segmentCount; ++iSeg) { + drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg]; + if (segmentSize < 255) { + if (iSeg == oggbs->currentPageHeader.segmentCount-1) { + atEndOfPage = DRFLAC_TRUE; + } + + break; + } + + bytesToEndOfPacketOrPage += segmentSize; + } + + /* + At this point we will have found either the packet or the end of the page. If were at the end of the page we'll + want to load the next page and keep searching for the end of the packet. + */ + drflac_oggbs__seek_physical(oggbs, bytesToEndOfPacketOrPage, DRFLAC_SEEK_CUR); + oggbs->bytesRemainingInPage -= bytesToEndOfPacketOrPage; + + if (atEndOfPage) { + /* + We're potentially at the next packet, but we need to check the next page first to be sure because the packet may + straddle pages. + */ + if (!drflac_oggbs__goto_next_page(oggbs)) { + return DRFLAC_FALSE; + } + + /* If it's a fresh packet it most likely means we're at the next packet. */ + if ((oggbs->currentPageHeader.headerType & 0x01) == 0) { + return DRFLAC_TRUE; + } + } else { + /* We're at the next packet. */ + return DRFLAC_TRUE; + } + } +} + +static drflac_bool32 drflac_oggbs__seek_to_next_frame(drflac_oggbs* oggbs) +{ + /* The bitstream should be sitting on the first byte just after the header of the frame. */ + + /* What we're actually doing here is seeking to the start of the next packet. */ + return drflac_oggbs__seek_to_next_packet(oggbs); +} +#endif + +static size_t drflac__on_read_ogg(void* pUserData, void* bufferOut, size_t bytesToRead) +{ + drflac_oggbs* oggbs = (drflac_oggbs*)pUserData; + drflac_uint8* pRunningBufferOut = (drflac_uint8*)bufferOut; + size_t bytesRead = 0; + + DRFLAC_ASSERT(oggbs != NULL); + DRFLAC_ASSERT(pRunningBufferOut != NULL); + + /* Reading is done page-by-page. If we've run out of bytes in the page we need to move to the next one. */ + while (bytesRead < bytesToRead) { + size_t bytesRemainingToRead = bytesToRead - bytesRead; + + if (oggbs->bytesRemainingInPage >= bytesRemainingToRead) { + DRFLAC_COPY_MEMORY(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), bytesRemainingToRead); + bytesRead += bytesRemainingToRead; + oggbs->bytesRemainingInPage -= (drflac_uint32)bytesRemainingToRead; + break; + } + + /* If we get here it means some of the requested data is contained in the next pages. */ + if (oggbs->bytesRemainingInPage > 0) { + DRFLAC_COPY_MEMORY(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), oggbs->bytesRemainingInPage); + bytesRead += oggbs->bytesRemainingInPage; + pRunningBufferOut += oggbs->bytesRemainingInPage; + oggbs->bytesRemainingInPage = 0; + } + + DRFLAC_ASSERT(bytesRemainingToRead > 0); + if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) { + break; /* Failed to go to the next page. Might have simply hit the end of the stream. */ + } + } + + return bytesRead; +} + +static drflac_bool32 drflac__on_seek_ogg(void* pUserData, int offset, drflac_seek_origin origin) +{ + drflac_oggbs* oggbs = (drflac_oggbs*)pUserData; + int bytesSeeked = 0; + + DRFLAC_ASSERT(oggbs != NULL); + DRFLAC_ASSERT(offset >= 0); /* <-- Never seek backwards. */ + + /* Seeking is always forward which makes things a lot simpler. */ + if (origin == DRFLAC_SEEK_SET) { + if (!drflac_oggbs__seek_physical(oggbs, (int)oggbs->firstBytePos, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + + if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) { + return DRFLAC_FALSE; + } + + return drflac__on_seek_ogg(pUserData, offset, DRFLAC_SEEK_CUR); + } else if (origin == DRFLAC_SEEK_CUR) { + while (bytesSeeked < offset) { + int bytesRemainingToSeek = offset - bytesSeeked; + DRFLAC_ASSERT(bytesRemainingToSeek >= 0); + + if (oggbs->bytesRemainingInPage >= (size_t)bytesRemainingToSeek) { + bytesSeeked += bytesRemainingToSeek; + (void)bytesSeeked; /* <-- Silence a dead store warning emitted by Clang Static Analyzer. */ + oggbs->bytesRemainingInPage -= bytesRemainingToSeek; + break; + } + + /* If we get here it means some of the requested data is contained in the next pages. */ + if (oggbs->bytesRemainingInPage > 0) { + bytesSeeked += (int)oggbs->bytesRemainingInPage; + oggbs->bytesRemainingInPage = 0; + } + + DRFLAC_ASSERT(bytesRemainingToSeek > 0); + if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) { + /* Failed to go to the next page. We either hit the end of the stream or had a CRC mismatch. */ + return DRFLAC_FALSE; + } + } + } else if (origin == DRFLAC_SEEK_END) { + /* Seeking to the end is not supported. */ + return DRFLAC_FALSE; + } + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__on_tell_ogg(void* pUserData, drflac_int64* pCursor) +{ + /* + Not implemented for Ogg containers because we don't currently track the byte position of the logical bitstream. To support this, we'll need + to track the position in drflac__on_read_ogg and drflac__on_seek_ogg. + */ + (void)pUserData; + (void)pCursor; + return DRFLAC_FALSE; +} + + +static drflac_bool32 drflac_ogg__seek_to_pcm_frame(drflac* pFlac, drflac_uint64 pcmFrameIndex) +{ + drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs; + drflac_uint64 originalBytePos; + drflac_uint64 runningGranulePosition; + drflac_uint64 runningFrameBytePos; + drflac_uint64 runningPCMFrameCount; + + DRFLAC_ASSERT(oggbs != NULL); + + originalBytePos = oggbs->currentBytePos; /* For recovery. Points to the OggS identifier. */ + + /* First seek to the first frame. */ + if (!drflac__seek_to_byte(&pFlac->bs, pFlac->firstFLACFramePosInBytes)) { + return DRFLAC_FALSE; + } + oggbs->bytesRemainingInPage = 0; + + runningGranulePosition = 0; + for (;;) { + if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) { + drflac_oggbs__seek_physical(oggbs, originalBytePos, DRFLAC_SEEK_SET); + return DRFLAC_FALSE; /* Never did find that sample... */ + } + + runningFrameBytePos = oggbs->currentBytePos - drflac_ogg__get_page_header_size(&oggbs->currentPageHeader) - oggbs->pageDataSize; + if (oggbs->currentPageHeader.granulePosition >= pcmFrameIndex) { + break; /* The sample is somewhere in the previous page. */ + } + + /* + At this point we know the sample is not in the previous page. It could possibly be in this page. For simplicity we + disregard any pages that do not begin a fresh packet. + */ + if ((oggbs->currentPageHeader.headerType & 0x01) == 0) { /* <-- Is it a fresh page? */ + if (oggbs->currentPageHeader.segmentTable[0] >= 2) { + drflac_uint8 firstBytesInPage[2]; + firstBytesInPage[0] = oggbs->pageData[0]; + firstBytesInPage[1] = oggbs->pageData[1]; + + if ((firstBytesInPage[0] == 0xFF) && (firstBytesInPage[1] & 0xFC) == 0xF8) { /* <-- Does the page begin with a frame's sync code? */ + runningGranulePosition = oggbs->currentPageHeader.granulePosition; + } + + continue; + } + } + } + + /* + We found the page that that is closest to the sample, so now we need to find it. The first thing to do is seek to the + start of that page. In the loop above we checked that it was a fresh page which means this page is also the start of + a new frame. This property means that after we've seeked to the page we can immediately start looping over frames until + we find the one containing the target sample. + */ + if (!drflac_oggbs__seek_physical(oggbs, runningFrameBytePos, DRFLAC_SEEK_SET)) { + return DRFLAC_FALSE; + } + if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) { + return DRFLAC_FALSE; + } + + /* + At this point we'll be sitting on the first byte of the frame header of the first frame in the page. We just keep + looping over these frames until we find the one containing the sample we're after. + */ + runningPCMFrameCount = runningGranulePosition; + for (;;) { + /* + There are two ways to find the sample and seek past irrelevant frames: + 1) Use the native FLAC decoder. + 2) Use Ogg's framing system. + + Both of these options have their own pros and cons. Using the native FLAC decoder is slower because it needs to + do a full decode of the frame. Using Ogg's framing system is faster, but more complicated and involves some code + duplication for the decoding of frame headers. + + Another thing to consider is that using the Ogg framing system will perform direct seeking of the physical Ogg + bitstream. This is important to consider because it means we cannot read data from the drflac_bs object using the + standard drflac__*() APIs because that will read in extra data for its own internal caching which in turn breaks + the positioning of the read pointer of the physical Ogg bitstream. Therefore, anything that would normally be read + using the native FLAC decoding APIs, such as drflac__read_next_flac_frame_header(), need to be re-implemented so as to + avoid the use of the drflac_bs object. + + Considering these issues, I have decided to use the slower native FLAC decoding method for the following reasons: + 1) Seeking is already partially accelerated using Ogg's paging system in the code block above. + 2) Seeking in an Ogg encapsulated FLAC stream is probably quite uncommon. + 3) Simplicity. + */ + drflac_uint64 firstPCMFrameInFLACFrame = 0; + drflac_uint64 lastPCMFrameInFLACFrame = 0; + drflac_uint64 pcmFrameCountInThisFrame; + + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + return DRFLAC_FALSE; + } + + drflac__get_pcm_frame_range_of_current_flac_frame(pFlac, &firstPCMFrameInFLACFrame, &lastPCMFrameInFLACFrame); + + pcmFrameCountInThisFrame = (lastPCMFrameInFLACFrame - firstPCMFrameInFLACFrame) + 1; + + /* If we are seeking to the end of the file and we've just hit it, we're done. */ + if (pcmFrameIndex == pFlac->totalPCMFrameCount && (runningPCMFrameCount + pcmFrameCountInThisFrame) == pFlac->totalPCMFrameCount) { + drflac_result result = drflac__decode_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + pFlac->currentPCMFrame = pcmFrameIndex; + pFlac->currentFLACFrame.pcmFramesRemaining = 0; + return DRFLAC_TRUE; + } else { + return DRFLAC_FALSE; + } + } + + if (pcmFrameIndex < (runningPCMFrameCount + pcmFrameCountInThisFrame)) { + /* + The sample should be in this FLAC frame. We need to fully decode it, however if it's an invalid frame (a CRC mismatch), we need to pretend + it never existed and keep iterating. + */ + drflac_result result = drflac__decode_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */ + drflac_uint64 pcmFramesToDecode = (size_t)(pcmFrameIndex - runningPCMFrameCount); /* <-- Safe cast because the maximum number of samples in a frame is 65535. */ + if (pcmFramesToDecode == 0) { + return DRFLAC_TRUE; + } + + pFlac->currentPCMFrame = runningPCMFrameCount; + + return drflac__seek_forward_by_pcm_frames(pFlac, pcmFramesToDecode) == pcmFramesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */ + } else { + if (result == DRFLAC_CRC_MISMATCH) { + continue; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } else { + /* + It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this + frame never existed and leave the running sample count untouched. + */ + drflac_result result = drflac__seek_to_next_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + runningPCMFrameCount += pcmFrameCountInThisFrame; + } else { + if (result == DRFLAC_CRC_MISMATCH) { + continue; /* CRC mismatch. Pretend this frame never existed. */ + } else { + return DRFLAC_FALSE; + } + } + } + } +} + + + +static drflac_bool32 drflac__init_private__ogg(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_bool32 relaxed) +{ + drflac_ogg_page_header header; + drflac_uint32 crc32 = DRFLAC_OGG_CAPTURE_PATTERN_CRC32; + drflac_uint32 bytesRead = 0; + + /* Pre Condition: The bit stream should be sitting just past the 4-byte OggS capture pattern. */ + (void)relaxed; + + pInit->container = drflac_container_ogg; + pInit->oggFirstBytePos = 0; + + /* + We'll get here if the first 4 bytes of the stream were the OggS capture pattern, however it doesn't necessarily mean the + stream includes FLAC encoded audio. To check for this we need to scan the beginning-of-stream page markers and check if + any match the FLAC specification. Important to keep in mind that the stream may be multiplexed. + */ + if (drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) { + return DRFLAC_FALSE; + } + pInit->runningFilePos += bytesRead; + + for (;;) { + int pageBodySize; + + /* Break if we're past the beginning of stream page. */ + if ((header.headerType & 0x02) == 0) { + return DRFLAC_FALSE; + } + + /* Check if it's a FLAC header. */ + pageBodySize = drflac_ogg__get_page_body_size(&header); + if (pageBodySize == 51) { /* 51 = the lacing value of the FLAC header packet. */ + /* It could be a FLAC page... */ + drflac_uint32 bytesRemainingInPage = pageBodySize; + drflac_uint8 packetType; + + if (onRead(pUserData, &packetType, 1) != 1) { + return DRFLAC_FALSE; + } + + bytesRemainingInPage -= 1; + if (packetType == 0x7F) { + /* Increasingly more likely to be a FLAC page... */ + drflac_uint8 sig[4]; + if (onRead(pUserData, sig, 4) != 4) { + return DRFLAC_FALSE; + } + + bytesRemainingInPage -= 4; + if (sig[0] == 'F' && sig[1] == 'L' && sig[2] == 'A' && sig[3] == 'C') { + /* Almost certainly a FLAC page... */ + drflac_uint8 mappingVersion[2]; + if (onRead(pUserData, mappingVersion, 2) != 2) { + return DRFLAC_FALSE; + } + + if (mappingVersion[0] != 1) { + return DRFLAC_FALSE; /* Only supporting version 1.x of the Ogg mapping. */ + } + + /* + The next 2 bytes are the non-audio packets, not including this one. We don't care about this because we're going to + be handling it in a generic way based on the serial number and packet types. + */ + if (!onSeek(pUserData, 2, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + + /* Expecting the native FLAC signature "fLaC". */ + if (onRead(pUserData, sig, 4) != 4) { + return DRFLAC_FALSE; + } + + if (sig[0] == 'f' && sig[1] == 'L' && sig[2] == 'a' && sig[3] == 'C') { + /* The remaining data in the page should be the STREAMINFO block. */ + drflac_streaminfo streaminfo; + drflac_uint8 isLastBlock; + drflac_uint8 blockType; + drflac_uint32 blockSize; + if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) { + return DRFLAC_FALSE; + } + + if (blockType != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO || blockSize != 34) { + return DRFLAC_FALSE; /* Invalid block type. First block must be the STREAMINFO block. */ + } + + if (drflac__read_streaminfo(onRead, pUserData, &streaminfo)) { + /* Success! */ + pInit->hasStreamInfoBlock = DRFLAC_TRUE; + pInit->sampleRate = streaminfo.sampleRate; + pInit->channels = streaminfo.channels; + pInit->bitsPerSample = streaminfo.bitsPerSample; + pInit->totalPCMFrameCount = streaminfo.totalPCMFrameCount; + pInit->maxBlockSizeInPCMFrames = streaminfo.maxBlockSizeInPCMFrames; + pInit->hasMetadataBlocks = !isLastBlock; + + if (onMeta) { + drflac_metadata metadata; + metadata.type = DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO; + metadata.pRawData = NULL; + metadata.rawDataSize = 0; + metadata.data.streaminfo = streaminfo; + onMeta(pUserDataMD, &metadata); + } + + pInit->runningFilePos += pageBodySize; + pInit->oggFirstBytePos = pInit->runningFilePos - 79; /* Subtracting 79 will place us right on top of the "OggS" identifier of the FLAC bos page. */ + pInit->oggSerial = header.serialNumber; + pInit->oggBosHeader = header; + break; + } else { + /* Failed to read STREAMINFO block. Aww, so close... */ + return DRFLAC_FALSE; + } + } else { + /* Invalid file. */ + return DRFLAC_FALSE; + } + } else { + /* Not a FLAC header. Skip it. */ + if (!onSeek(pUserData, bytesRemainingInPage, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + } + } else { + /* Not a FLAC header. Seek past the entire page and move on to the next. */ + if (!onSeek(pUserData, bytesRemainingInPage, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + } + } else { + if (!onSeek(pUserData, pageBodySize, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; + } + } + + pInit->runningFilePos += pageBodySize; + + + /* Read the header of the next page. */ + if (drflac_ogg__read_page_header(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) { + return DRFLAC_FALSE; + } + pInit->runningFilePos += bytesRead; + } + + /* + If we get here it means we found a FLAC audio stream. We should be sitting on the first byte of the header of the next page. The next + packets in the FLAC logical stream contain the metadata. The only thing left to do in the initialization phase for Ogg is to create the + Ogg bistream object. + */ + pInit->hasMetadataBlocks = DRFLAC_TRUE; /* <-- Always have at least VORBIS_COMMENT metadata block. */ + return DRFLAC_TRUE; +} +#endif + +static drflac_bool32 drflac__init_private(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, drflac_container container, void* pUserData, void* pUserDataMD) +{ + drflac_bool32 relaxed; + drflac_uint8 id[4]; + + if (pInit == NULL || onRead == NULL || onSeek == NULL) { /* <-- onTell is optional. */ + return DRFLAC_FALSE; + } + + DRFLAC_ZERO_MEMORY(pInit, sizeof(*pInit)); + pInit->onRead = onRead; + pInit->onSeek = onSeek; + pInit->onTell = onTell; + pInit->onMeta = onMeta; + pInit->container = container; + pInit->pUserData = pUserData; + pInit->pUserDataMD = pUserDataMD; + + pInit->bs.onRead = onRead; + pInit->bs.onSeek = onSeek; + pInit->bs.onTell = onTell; + pInit->bs.pUserData = pUserData; + drflac__reset_cache(&pInit->bs); + + + /* If the container is explicitly defined then we can try opening in relaxed mode. */ + relaxed = container != drflac_container_unknown; + + /* Skip over any ID3 tags. */ + for (;;) { + if (onRead(pUserData, id, 4) != 4) { + return DRFLAC_FALSE; /* Ran out of data. */ + } + pInit->runningFilePos += 4; + + if (id[0] == 'I' && id[1] == 'D' && id[2] == '3') { + drflac_uint8 header[6]; + drflac_uint8 flags; + drflac_uint32 headerSize; + + if (onRead(pUserData, header, 6) != 6) { + return DRFLAC_FALSE; /* Ran out of data. */ + } + pInit->runningFilePos += 6; + + flags = header[1]; + + DRFLAC_COPY_MEMORY(&headerSize, header+2, 4); + headerSize = drflac__unsynchsafe_32(drflac__be2host_32(headerSize)); + if (flags & 0x10) { + headerSize += 10; + } + + if (!onSeek(pUserData, headerSize, DRFLAC_SEEK_CUR)) { + return DRFLAC_FALSE; /* Failed to seek past the tag. */ + } + pInit->runningFilePos += headerSize; + } else { + break; + } + } + + if (id[0] == 'f' && id[1] == 'L' && id[2] == 'a' && id[3] == 'C') { + return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed); + } +#ifndef DR_FLAC_NO_OGG + if (id[0] == 'O' && id[1] == 'g' && id[2] == 'g' && id[3] == 'S') { + return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed); + } +#endif + + /* If we get here it means we likely don't have a header. Try opening in relaxed mode, if applicable. */ + if (relaxed) { + if (container == drflac_container_native) { + return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed); + } +#ifndef DR_FLAC_NO_OGG + if (container == drflac_container_ogg) { + return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed); + } +#endif + } + + /* Unsupported container. */ + return DRFLAC_FALSE; +} + +static void drflac__init_from_info(drflac* pFlac, const drflac_init_info* pInit) +{ + DRFLAC_ASSERT(pFlac != NULL); + DRFLAC_ASSERT(pInit != NULL); + + DRFLAC_ZERO_MEMORY(pFlac, sizeof(*pFlac)); + pFlac->bs = pInit->bs; + pFlac->onMeta = pInit->onMeta; + pFlac->pUserDataMD = pInit->pUserDataMD; + pFlac->maxBlockSizeInPCMFrames = pInit->maxBlockSizeInPCMFrames; + pFlac->sampleRate = pInit->sampleRate; + pFlac->channels = (drflac_uint8)pInit->channels; + pFlac->bitsPerSample = (drflac_uint8)pInit->bitsPerSample; + pFlac->totalPCMFrameCount = pInit->totalPCMFrameCount; + pFlac->container = pInit->container; +} + + +static drflac* drflac_open_with_metadata_private(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, drflac_container container, void* pUserData, void* pUserDataMD, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac_init_info init; + drflac_uint32 allocationSize; + drflac_uint32 wholeSIMDVectorCountPerChannel; + drflac_uint32 decodedSamplesAllocationSize; +#ifndef DR_FLAC_NO_OGG + drflac_oggbs* pOggbs = NULL; +#endif + drflac_uint64 firstFramePos; + drflac_uint64 seektablePos; + drflac_uint32 seekpointCount; + drflac_allocation_callbacks allocationCallbacks; + drflac* pFlac; + + /* CPU support first. */ + drflac__init_cpu_caps(); + + if (!drflac__init_private(&init, onRead, onSeek, onTell, onMeta, container, pUserData, pUserDataMD)) { + return NULL; + } + + if (pAllocationCallbacks != NULL) { + allocationCallbacks = *pAllocationCallbacks; + if (allocationCallbacks.onFree == NULL || (allocationCallbacks.onMalloc == NULL && allocationCallbacks.onRealloc == NULL)) { + return NULL; /* Invalid allocation callbacks. */ + } + } else { + allocationCallbacks.pUserData = NULL; + allocationCallbacks.onMalloc = drflac__malloc_default; + allocationCallbacks.onRealloc = drflac__realloc_default; + allocationCallbacks.onFree = drflac__free_default; + } + + + /* + The size of the allocation for the drflac object needs to be large enough to fit the following: + 1) The main members of the drflac structure + 2) A block of memory large enough to store the decoded samples of the largest frame in the stream + 3) If the container is Ogg, a drflac_oggbs object + + The complicated part of the allocation is making sure there's enough room the decoded samples, taking into consideration + the different SIMD instruction sets. + */ + allocationSize = sizeof(drflac); + + /* + The allocation size for decoded frames depends on the number of 32-bit integers that fit inside the largest SIMD vector + we are supporting. + */ + if ((init.maxBlockSizeInPCMFrames % (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32))) == 0) { + wholeSIMDVectorCountPerChannel = (init.maxBlockSizeInPCMFrames / (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32))); + } else { + wholeSIMDVectorCountPerChannel = (init.maxBlockSizeInPCMFrames / (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32))) + 1; + } + + decodedSamplesAllocationSize = wholeSIMDVectorCountPerChannel * DRFLAC_MAX_SIMD_VECTOR_SIZE * init.channels; + + allocationSize += decodedSamplesAllocationSize; + allocationSize += DRFLAC_MAX_SIMD_VECTOR_SIZE; /* Allocate extra bytes to ensure we have enough for alignment. */ + +#ifndef DR_FLAC_NO_OGG + /* There's additional data required for Ogg streams. */ + if (init.container == drflac_container_ogg) { + allocationSize += sizeof(drflac_oggbs); + + pOggbs = (drflac_oggbs*)drflac__malloc_from_callbacks(sizeof(*pOggbs), &allocationCallbacks); + if (pOggbs == NULL) { + return NULL; /*DRFLAC_OUT_OF_MEMORY;*/ + } + + DRFLAC_ZERO_MEMORY(pOggbs, sizeof(*pOggbs)); + pOggbs->onRead = onRead; + pOggbs->onSeek = onSeek; + pOggbs->onTell = onTell; + pOggbs->pUserData = pUserData; + pOggbs->currentBytePos = init.oggFirstBytePos; + pOggbs->firstBytePos = init.oggFirstBytePos; + pOggbs->serialNumber = init.oggSerial; + pOggbs->bosPageHeader = init.oggBosHeader; + pOggbs->bytesRemainingInPage = 0; + } +#endif + + /* + This part is a bit awkward. We need to load the seektable so that it can be referenced in-memory, but I want the drflac object to + consist of only a single heap allocation. To this, the size of the seek table needs to be known, which we determine when reading + and decoding the metadata. + */ + firstFramePos = 42; /* <-- We know we are at byte 42 at this point. */ + seektablePos = 0; + seekpointCount = 0; + if (init.hasMetadataBlocks) { + drflac_read_proc onReadOverride = onRead; + drflac_seek_proc onSeekOverride = onSeek; + drflac_tell_proc onTellOverride = onTell; + void* pUserDataOverride = pUserData; + +#ifndef DR_FLAC_NO_OGG + if (init.container == drflac_container_ogg) { + onReadOverride = drflac__on_read_ogg; + onSeekOverride = drflac__on_seek_ogg; + onTellOverride = drflac__on_tell_ogg; + pUserDataOverride = (void*)pOggbs; + } +#endif + + if (!drflac__read_and_decode_metadata(onReadOverride, onSeekOverride, onTellOverride, onMeta, pUserDataOverride, pUserDataMD, &firstFramePos, &seektablePos, &seekpointCount, &allocationCallbacks)) { + #ifndef DR_FLAC_NO_OGG + drflac__free_from_callbacks(pOggbs, &allocationCallbacks); + #endif + return NULL; + } + + if ((0xFFFFFFFF - (seekpointCount * sizeof(drflac_seekpoint))) < allocationSize) { + #ifndef DR_FLAC_NO_OGG + drflac__free_from_callbacks(pOggbs, &allocationCallbacks); + #endif + return NULL; + } + + allocationSize += seekpointCount * sizeof(drflac_seekpoint); + } + + pFlac = (drflac*)drflac__malloc_from_callbacks((size_t)allocationSize, &allocationCallbacks); + if (pFlac == NULL) { + #ifndef DR_FLAC_NO_OGG + drflac__free_from_callbacks(pOggbs, &allocationCallbacks); + #endif + return NULL; + } + + drflac__init_from_info(pFlac, &init); + pFlac->allocationCallbacks = allocationCallbacks; + pFlac->pDecodedSamples = (drflac_int32*)drflac_align((size_t)pFlac->pExtraData, DRFLAC_MAX_SIMD_VECTOR_SIZE); + +#ifndef DR_FLAC_NO_OGG + if (init.container == drflac_container_ogg) { + drflac_oggbs* pInternalOggbs = (drflac_oggbs*)((drflac_uint8*)pFlac->pDecodedSamples + decodedSamplesAllocationSize + (seekpointCount * sizeof(drflac_seekpoint))); + DRFLAC_COPY_MEMORY(pInternalOggbs, pOggbs, sizeof(*pOggbs)); + + /* At this point the pOggbs object has been handed over to pInternalOggbs and can be freed. */ + drflac__free_from_callbacks(pOggbs, &allocationCallbacks); + pOggbs = NULL; + + /* The Ogg bistream needs to be layered on top of the original bitstream. */ + pFlac->bs.onRead = drflac__on_read_ogg; + pFlac->bs.onSeek = drflac__on_seek_ogg; + pFlac->bs.onTell = drflac__on_tell_ogg; + pFlac->bs.pUserData = (void*)pInternalOggbs; + pFlac->_oggbs = (void*)pInternalOggbs; + } +#endif + + pFlac->firstFLACFramePosInBytes = firstFramePos; + + /* NOTE: Seektables are not currently compatible with Ogg encapsulation (Ogg has its own accelerated seeking system). I may change this later, so I'm leaving this here for now. */ +#ifndef DR_FLAC_NO_OGG + if (init.container == drflac_container_ogg) + { + pFlac->pSeekpoints = NULL; + pFlac->seekpointCount = 0; + } + else +#endif + { + /* If we have a seektable we need to load it now, making sure we move back to where we were previously. */ + if (seektablePos != 0) { + pFlac->seekpointCount = seekpointCount; + pFlac->pSeekpoints = (drflac_seekpoint*)((drflac_uint8*)pFlac->pDecodedSamples + decodedSamplesAllocationSize); + + DRFLAC_ASSERT(pFlac->bs.onSeek != NULL); + DRFLAC_ASSERT(pFlac->bs.onRead != NULL); + + /* Seek to the seektable, then just read directly into our seektable buffer. */ + if (pFlac->bs.onSeek(pFlac->bs.pUserData, (int)seektablePos, DRFLAC_SEEK_SET)) { + drflac_uint32 iSeekpoint; + + for (iSeekpoint = 0; iSeekpoint < seekpointCount; iSeekpoint += 1) { + if (pFlac->bs.onRead(pFlac->bs.pUserData, pFlac->pSeekpoints + iSeekpoint, DRFLAC_SEEKPOINT_SIZE_IN_BYTES) == DRFLAC_SEEKPOINT_SIZE_IN_BYTES) { + /* Endian swap. */ + pFlac->pSeekpoints[iSeekpoint].firstPCMFrame = drflac__be2host_64(pFlac->pSeekpoints[iSeekpoint].firstPCMFrame); + pFlac->pSeekpoints[iSeekpoint].flacFrameOffset = drflac__be2host_64(pFlac->pSeekpoints[iSeekpoint].flacFrameOffset); + pFlac->pSeekpoints[iSeekpoint].pcmFrameCount = drflac__be2host_16(pFlac->pSeekpoints[iSeekpoint].pcmFrameCount); + } else { + /* Failed to read the seektable. Pretend we don't have one. */ + pFlac->pSeekpoints = NULL; + pFlac->seekpointCount = 0; + break; + } + } + + /* We need to seek back to where we were. If this fails it's a critical error. */ + if (!pFlac->bs.onSeek(pFlac->bs.pUserData, (int)pFlac->firstFLACFramePosInBytes, DRFLAC_SEEK_SET)) { + drflac__free_from_callbacks(pFlac, &allocationCallbacks); + return NULL; + } + } else { + /* Failed to seek to the seektable. Ominous sign, but for now we can just pretend we don't have one. */ + pFlac->pSeekpoints = NULL; + pFlac->seekpointCount = 0; + } + } + } + + + /* + If we get here, but don't have a STREAMINFO block, it means we've opened the stream in relaxed mode and need to decode + the first frame. + */ + if (!init.hasStreamInfoBlock) { + pFlac->currentFLACFrame.header = init.firstFrameHeader; + for (;;) { + drflac_result result = drflac__decode_flac_frame(pFlac); + if (result == DRFLAC_SUCCESS) { + break; + } else { + if (result == DRFLAC_CRC_MISMATCH) { + if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFLACFrame.header)) { + drflac__free_from_callbacks(pFlac, &allocationCallbacks); + return NULL; + } + continue; + } else { + drflac__free_from_callbacks(pFlac, &allocationCallbacks); + return NULL; + } + } + } + } + + return pFlac; +} + + + +#ifndef DR_FLAC_NO_STDIO +#include +#ifndef DR_FLAC_NO_WCHAR +#include /* For wcslen(), wcsrtombs() */ +#endif + +/* Errno */ +/* drflac_result_from_errno() is only used for fopen() and wfopen() so putting it inside DR_WAV_NO_STDIO for now. If something else needs this later we can move it out. */ +#include +static drflac_result drflac_result_from_errno(int e) +{ + switch (e) + { + case 0: return DRFLAC_SUCCESS; + #ifdef EPERM + case EPERM: return DRFLAC_INVALID_OPERATION; + #endif + #ifdef ENOENT + case ENOENT: return DRFLAC_DOES_NOT_EXIST; + #endif + #ifdef ESRCH + case ESRCH: return DRFLAC_DOES_NOT_EXIST; + #endif + #ifdef EINTR + case EINTR: return DRFLAC_INTERRUPT; + #endif + #ifdef EIO + case EIO: return DRFLAC_IO_ERROR; + #endif + #ifdef ENXIO + case ENXIO: return DRFLAC_DOES_NOT_EXIST; + #endif + #ifdef E2BIG + case E2BIG: return DRFLAC_INVALID_ARGS; + #endif + #ifdef ENOEXEC + case ENOEXEC: return DRFLAC_INVALID_FILE; + #endif + #ifdef EBADF + case EBADF: return DRFLAC_INVALID_FILE; + #endif + #ifdef ECHILD + case ECHILD: return DRFLAC_ERROR; + #endif + #ifdef EAGAIN + case EAGAIN: return DRFLAC_UNAVAILABLE; + #endif + #ifdef ENOMEM + case ENOMEM: return DRFLAC_OUT_OF_MEMORY; + #endif + #ifdef EACCES + case EACCES: return DRFLAC_ACCESS_DENIED; + #endif + #ifdef EFAULT + case EFAULT: return DRFLAC_BAD_ADDRESS; + #endif + #ifdef ENOTBLK + case ENOTBLK: return DRFLAC_ERROR; + #endif + #ifdef EBUSY + case EBUSY: return DRFLAC_BUSY; + #endif + #ifdef EEXIST + case EEXIST: return DRFLAC_ALREADY_EXISTS; + #endif + #ifdef EXDEV + case EXDEV: return DRFLAC_ERROR; + #endif + #ifdef ENODEV + case ENODEV: return DRFLAC_DOES_NOT_EXIST; + #endif + #ifdef ENOTDIR + case ENOTDIR: return DRFLAC_NOT_DIRECTORY; + #endif + #ifdef EISDIR + case EISDIR: return DRFLAC_IS_DIRECTORY; + #endif + #ifdef EINVAL + case EINVAL: return DRFLAC_INVALID_ARGS; + #endif + #ifdef ENFILE + case ENFILE: return DRFLAC_TOO_MANY_OPEN_FILES; + #endif + #ifdef EMFILE + case EMFILE: return DRFLAC_TOO_MANY_OPEN_FILES; + #endif + #ifdef ENOTTY + case ENOTTY: return DRFLAC_INVALID_OPERATION; + #endif + #ifdef ETXTBSY + case ETXTBSY: return DRFLAC_BUSY; + #endif + #ifdef EFBIG + case EFBIG: return DRFLAC_TOO_BIG; + #endif + #ifdef ENOSPC + case ENOSPC: return DRFLAC_NO_SPACE; + #endif + #ifdef ESPIPE + case ESPIPE: return DRFLAC_BAD_SEEK; + #endif + #ifdef EROFS + case EROFS: return DRFLAC_ACCESS_DENIED; + #endif + #ifdef EMLINK + case EMLINK: return DRFLAC_TOO_MANY_LINKS; + #endif + #ifdef EPIPE + case EPIPE: return DRFLAC_BAD_PIPE; + #endif + #ifdef EDOM + case EDOM: return DRFLAC_OUT_OF_RANGE; + #endif + #ifdef ERANGE + case ERANGE: return DRFLAC_OUT_OF_RANGE; + #endif + #ifdef EDEADLK + case EDEADLK: return DRFLAC_DEADLOCK; + #endif + #ifdef ENAMETOOLONG + case ENAMETOOLONG: return DRFLAC_PATH_TOO_LONG; + #endif + #ifdef ENOLCK + case ENOLCK: return DRFLAC_ERROR; + #endif + #ifdef ENOSYS + case ENOSYS: return DRFLAC_NOT_IMPLEMENTED; + #endif + #if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST /* In AIX, ENOTEMPTY and EEXIST use the same value. */ + case ENOTEMPTY: return DRFLAC_DIRECTORY_NOT_EMPTY; + #endif + #ifdef ELOOP + case ELOOP: return DRFLAC_TOO_MANY_LINKS; + #endif + #ifdef ENOMSG + case ENOMSG: return DRFLAC_NO_MESSAGE; + #endif + #ifdef EIDRM + case EIDRM: return DRFLAC_ERROR; + #endif + #ifdef ECHRNG + case ECHRNG: return DRFLAC_ERROR; + #endif + #ifdef EL2NSYNC + case EL2NSYNC: return DRFLAC_ERROR; + #endif + #ifdef EL3HLT + case EL3HLT: return DRFLAC_ERROR; + #endif + #ifdef EL3RST + case EL3RST: return DRFLAC_ERROR; + #endif + #ifdef ELNRNG + case ELNRNG: return DRFLAC_OUT_OF_RANGE; + #endif + #ifdef EUNATCH + case EUNATCH: return DRFLAC_ERROR; + #endif + #ifdef ENOCSI + case ENOCSI: return DRFLAC_ERROR; + #endif + #ifdef EL2HLT + case EL2HLT: return DRFLAC_ERROR; + #endif + #ifdef EBADE + case EBADE: return DRFLAC_ERROR; + #endif + #ifdef EBADR + case EBADR: return DRFLAC_ERROR; + #endif + #ifdef EXFULL + case EXFULL: return DRFLAC_ERROR; + #endif + #ifdef ENOANO + case ENOANO: return DRFLAC_ERROR; + #endif + #ifdef EBADRQC + case EBADRQC: return DRFLAC_ERROR; + #endif + #ifdef EBADSLT + case EBADSLT: return DRFLAC_ERROR; + #endif + #ifdef EBFONT + case EBFONT: return DRFLAC_INVALID_FILE; + #endif + #ifdef ENOSTR + case ENOSTR: return DRFLAC_ERROR; + #endif + #ifdef ENODATA + case ENODATA: return DRFLAC_NO_DATA_AVAILABLE; + #endif + #ifdef ETIME + case ETIME: return DRFLAC_TIMEOUT; + #endif + #ifdef ENOSR + case ENOSR: return DRFLAC_NO_DATA_AVAILABLE; + #endif + #ifdef ENONET + case ENONET: return DRFLAC_NO_NETWORK; + #endif + #ifdef ENOPKG + case ENOPKG: return DRFLAC_ERROR; + #endif + #ifdef EREMOTE + case EREMOTE: return DRFLAC_ERROR; + #endif + #ifdef ENOLINK + case ENOLINK: return DRFLAC_ERROR; + #endif + #ifdef EADV + case EADV: return DRFLAC_ERROR; + #endif + #ifdef ESRMNT + case ESRMNT: return DRFLAC_ERROR; + #endif + #ifdef ECOMM + case ECOMM: return DRFLAC_ERROR; + #endif + #ifdef EPROTO + case EPROTO: return DRFLAC_ERROR; + #endif + #ifdef EMULTIHOP + case EMULTIHOP: return DRFLAC_ERROR; + #endif + #ifdef EDOTDOT + case EDOTDOT: return DRFLAC_ERROR; + #endif + #ifdef EBADMSG + case EBADMSG: return DRFLAC_BAD_MESSAGE; + #endif + #ifdef EOVERFLOW + case EOVERFLOW: return DRFLAC_TOO_BIG; + #endif + #ifdef ENOTUNIQ + case ENOTUNIQ: return DRFLAC_NOT_UNIQUE; + #endif + #ifdef EBADFD + case EBADFD: return DRFLAC_ERROR; + #endif + #ifdef EREMCHG + case EREMCHG: return DRFLAC_ERROR; + #endif + #ifdef ELIBACC + case ELIBACC: return DRFLAC_ACCESS_DENIED; + #endif + #ifdef ELIBBAD + case ELIBBAD: return DRFLAC_INVALID_FILE; + #endif + #ifdef ELIBSCN + case ELIBSCN: return DRFLAC_INVALID_FILE; + #endif + #ifdef ELIBMAX + case ELIBMAX: return DRFLAC_ERROR; + #endif + #ifdef ELIBEXEC + case ELIBEXEC: return DRFLAC_ERROR; + #endif + #ifdef EILSEQ + case EILSEQ: return DRFLAC_INVALID_DATA; + #endif + #ifdef ERESTART + case ERESTART: return DRFLAC_ERROR; + #endif + #ifdef ESTRPIPE + case ESTRPIPE: return DRFLAC_ERROR; + #endif + #ifdef EUSERS + case EUSERS: return DRFLAC_ERROR; + #endif + #ifdef ENOTSOCK + case ENOTSOCK: return DRFLAC_NOT_SOCKET; + #endif + #ifdef EDESTADDRREQ + case EDESTADDRREQ: return DRFLAC_NO_ADDRESS; + #endif + #ifdef EMSGSIZE + case EMSGSIZE: return DRFLAC_TOO_BIG; + #endif + #ifdef EPROTOTYPE + case EPROTOTYPE: return DRFLAC_BAD_PROTOCOL; + #endif + #ifdef ENOPROTOOPT + case ENOPROTOOPT: return DRFLAC_PROTOCOL_UNAVAILABLE; + #endif + #ifdef EPROTONOSUPPORT + case EPROTONOSUPPORT: return DRFLAC_PROTOCOL_NOT_SUPPORTED; + #endif + #ifdef ESOCKTNOSUPPORT + case ESOCKTNOSUPPORT: return DRFLAC_SOCKET_NOT_SUPPORTED; + #endif + #ifdef EOPNOTSUPP + case EOPNOTSUPP: return DRFLAC_INVALID_OPERATION; + #endif + #ifdef EPFNOSUPPORT + case EPFNOSUPPORT: return DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED; + #endif + #ifdef EAFNOSUPPORT + case EAFNOSUPPORT: return DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED; + #endif + #ifdef EADDRINUSE + case EADDRINUSE: return DRFLAC_ALREADY_IN_USE; + #endif + #ifdef EADDRNOTAVAIL + case EADDRNOTAVAIL: return DRFLAC_ERROR; + #endif + #ifdef ENETDOWN + case ENETDOWN: return DRFLAC_NO_NETWORK; + #endif + #ifdef ENETUNREACH + case ENETUNREACH: return DRFLAC_NO_NETWORK; + #endif + #ifdef ENETRESET + case ENETRESET: return DRFLAC_NO_NETWORK; + #endif + #ifdef ECONNABORTED + case ECONNABORTED: return DRFLAC_NO_NETWORK; + #endif + #ifdef ECONNRESET + case ECONNRESET: return DRFLAC_CONNECTION_RESET; + #endif + #ifdef ENOBUFS + case ENOBUFS: return DRFLAC_NO_SPACE; + #endif + #ifdef EISCONN + case EISCONN: return DRFLAC_ALREADY_CONNECTED; + #endif + #ifdef ENOTCONN + case ENOTCONN: return DRFLAC_NOT_CONNECTED; + #endif + #ifdef ESHUTDOWN + case ESHUTDOWN: return DRFLAC_ERROR; + #endif + #ifdef ETOOMANYREFS + case ETOOMANYREFS: return DRFLAC_ERROR; + #endif + #ifdef ETIMEDOUT + case ETIMEDOUT: return DRFLAC_TIMEOUT; + #endif + #ifdef ECONNREFUSED + case ECONNREFUSED: return DRFLAC_CONNECTION_REFUSED; + #endif + #ifdef EHOSTDOWN + case EHOSTDOWN: return DRFLAC_NO_HOST; + #endif + #ifdef EHOSTUNREACH + case EHOSTUNREACH: return DRFLAC_NO_HOST; + #endif + #ifdef EALREADY + case EALREADY: return DRFLAC_IN_PROGRESS; + #endif + #ifdef EINPROGRESS + case EINPROGRESS: return DRFLAC_IN_PROGRESS; + #endif + #ifdef ESTALE + case ESTALE: return DRFLAC_INVALID_FILE; + #endif + #ifdef EUCLEAN + case EUCLEAN: return DRFLAC_ERROR; + #endif + #ifdef ENOTNAM + case ENOTNAM: return DRFLAC_ERROR; + #endif + #ifdef ENAVAIL + case ENAVAIL: return DRFLAC_ERROR; + #endif + #ifdef EISNAM + case EISNAM: return DRFLAC_ERROR; + #endif + #ifdef EREMOTEIO + case EREMOTEIO: return DRFLAC_IO_ERROR; + #endif + #ifdef EDQUOT + case EDQUOT: return DRFLAC_NO_SPACE; + #endif + #ifdef ENOMEDIUM + case ENOMEDIUM: return DRFLAC_DOES_NOT_EXIST; + #endif + #ifdef EMEDIUMTYPE + case EMEDIUMTYPE: return DRFLAC_ERROR; + #endif + #ifdef ECANCELED + case ECANCELED: return DRFLAC_CANCELLED; + #endif + #ifdef ENOKEY + case ENOKEY: return DRFLAC_ERROR; + #endif + #ifdef EKEYEXPIRED + case EKEYEXPIRED: return DRFLAC_ERROR; + #endif + #ifdef EKEYREVOKED + case EKEYREVOKED: return DRFLAC_ERROR; + #endif + #ifdef EKEYREJECTED + case EKEYREJECTED: return DRFLAC_ERROR; + #endif + #ifdef EOWNERDEAD + case EOWNERDEAD: return DRFLAC_ERROR; + #endif + #ifdef ENOTRECOVERABLE + case ENOTRECOVERABLE: return DRFLAC_ERROR; + #endif + #ifdef ERFKILL + case ERFKILL: return DRFLAC_ERROR; + #endif + #ifdef EHWPOISON + case EHWPOISON: return DRFLAC_ERROR; + #endif + default: return DRFLAC_ERROR; + } +} +/* End Errno */ + +/* fopen */ +static drflac_result drflac_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode) +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + errno_t err; +#endif + + if (ppFile != NULL) { + *ppFile = NULL; /* Safety. */ + } + + if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { + return DRFLAC_INVALID_ARGS; + } + +#if defined(_MSC_VER) && _MSC_VER >= 1400 + err = fopen_s(ppFile, pFilePath, pOpenMode); + if (err != 0) { + return drflac_result_from_errno(err); + } +#else +#if defined(_WIN32) || defined(__APPLE__) + *ppFile = fopen(pFilePath, pOpenMode); +#else + #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE) + *ppFile = fopen64(pFilePath, pOpenMode); + #else + *ppFile = fopen(pFilePath, pOpenMode); + #endif +#endif + if (*ppFile == NULL) { + drflac_result result = drflac_result_from_errno(errno); + if (result == DRFLAC_SUCCESS) { + result = DRFLAC_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */ + } + + return result; + } +#endif + + return DRFLAC_SUCCESS; +} + +/* +_wfopen() isn't always available in all compilation environments. + + * Windows only. + * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back). + * MinGW-64 (both 32- and 64-bit) seems to support it. + * MinGW wraps it in !defined(__STRICT_ANSI__). + * OpenWatcom wraps it in !defined(_NO_EXT_KEYS). + +This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs() +fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support. +*/ +#if defined(_WIN32) + #if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS)) + #define DRFLAC_HAS_WFOPEN + #endif +#endif + +#ifndef DR_FLAC_NO_WCHAR +static drflac_result drflac_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + if (ppFile != NULL) { + *ppFile = NULL; /* Safety. */ + } + + if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { + return DRFLAC_INVALID_ARGS; + } + +#if defined(DRFLAC_HAS_WFOPEN) + { + /* Use _wfopen() on Windows. */ + #if defined(_MSC_VER) && _MSC_VER >= 1400 + errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode); + if (err != 0) { + return drflac_result_from_errno(err); + } + #else + *ppFile = _wfopen(pFilePath, pOpenMode); + if (*ppFile == NULL) { + return drflac_result_from_errno(errno); + } + #endif + (void)pAllocationCallbacks; + } +#else + /* + Use fopen() on anything other than Windows. Requires a conversion. This is annoying because + fopen() is locale specific. The only real way I can think of to do this is with wcsrtombs(). Note + that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for + maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler + error I'll look into improving compatibility. + */ + + /* + Some compilers don't support wchar_t or wcsrtombs() which we're using below. In this case we just + need to abort with an error. If you encounter a compiler lacking such support, add it to this list + and submit a bug report and it'll be added to the library upstream. + */ + #if defined(__DJGPP__) + { + /* Nothing to do here. This will fall through to the error check below. */ + } + #else + { + mbstate_t mbs; + size_t lenMB; + const wchar_t* pFilePathTemp = pFilePath; + char* pFilePathMB = NULL; + char pOpenModeMB[32] = {0}; + + /* Get the length first. */ + DRFLAC_ZERO_OBJECT(&mbs); + lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs); + if (lenMB == (size_t)-1) { + return drflac_result_from_errno(errno); + } + + pFilePathMB = (char*)drflac__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks); + if (pFilePathMB == NULL) { + return DRFLAC_OUT_OF_MEMORY; + } + + pFilePathTemp = pFilePath; + DRFLAC_ZERO_OBJECT(&mbs); + wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs); + + /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */ + { + size_t i = 0; + for (;;) { + if (pOpenMode[i] == 0) { + pOpenModeMB[i] = '\0'; + break; + } + + pOpenModeMB[i] = (char)pOpenMode[i]; + i += 1; + } + } + + *ppFile = fopen(pFilePathMB, pOpenModeMB); + + drflac__free_from_callbacks(pFilePathMB, pAllocationCallbacks); + } + #endif + + if (*ppFile == NULL) { + return DRFLAC_ERROR; + } +#endif + + return DRFLAC_SUCCESS; +} +#endif +/* End fopen */ + +static size_t drflac__on_read_stdio(void* pUserData, void* bufferOut, size_t bytesToRead) +{ + return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData); +} + +static drflac_bool32 drflac__on_seek_stdio(void* pUserData, int offset, drflac_seek_origin origin) +{ + int whence = SEEK_SET; + if (origin == DRFLAC_SEEK_CUR) { + whence = SEEK_CUR; + } else if (origin == DRFLAC_SEEK_END) { + whence = SEEK_END; + } + + return fseek((FILE*)pUserData, offset, whence) == 0; +} + +static drflac_bool32 drflac__on_tell_stdio(void* pUserData, drflac_int64* pCursor) +{ + FILE* pFileStdio = (FILE*)pUserData; + drflac_int64 result; + + /* These were all validated at a higher level. */ + DRFLAC_ASSERT(pFileStdio != NULL); + DRFLAC_ASSERT(pCursor != NULL); + +#if defined(_WIN32) && !defined(NXDK) + #if defined(_MSC_VER) && _MSC_VER > 1200 + result = _ftelli64(pFileStdio); + #else + result = ftell(pFileStdio); + #endif +#else + result = ftell(pFileStdio); +#endif + + *pCursor = result; + + return DRFLAC_TRUE; +} + + + +DRFLAC_API drflac* drflac_open_file(const char* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + FILE* pFile; + + if (drflac_fopen(&pFile, pFileName, "rb") != DRFLAC_SUCCESS) { + return NULL; + } + + pFlac = drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, (void*)pFile, pAllocationCallbacks); + if (pFlac == NULL) { + fclose(pFile); + return NULL; + } + + return pFlac; +} + +#ifndef DR_FLAC_NO_WCHAR +DRFLAC_API drflac* drflac_open_file_w(const wchar_t* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + FILE* pFile; + + if (drflac_wfopen(&pFile, pFileName, L"rb", pAllocationCallbacks) != DRFLAC_SUCCESS) { + return NULL; + } + + pFlac = drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, (void*)pFile, pAllocationCallbacks); + if (pFlac == NULL) { + fclose(pFile); + return NULL; + } + + return pFlac; +} +#endif + +DRFLAC_API drflac* drflac_open_file_with_metadata(const char* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + FILE* pFile; + + if (drflac_fopen(&pFile, pFileName, "rb") != DRFLAC_SUCCESS) { + return NULL; + } + + pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, onMeta, drflac_container_unknown, (void*)pFile, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + fclose(pFile); + return pFlac; + } + + return pFlac; +} + +#ifndef DR_FLAC_NO_WCHAR +DRFLAC_API drflac* drflac_open_file_with_metadata_w(const wchar_t* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + FILE* pFile; + + if (drflac_wfopen(&pFile, pFileName, L"rb", pAllocationCallbacks) != DRFLAC_SUCCESS) { + return NULL; + } + + pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, onMeta, drflac_container_unknown, (void*)pFile, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + fclose(pFile); + return pFlac; + } + + return pFlac; +} +#endif +#endif /* DR_FLAC_NO_STDIO */ + +static size_t drflac__on_read_memory(void* pUserData, void* bufferOut, size_t bytesToRead) +{ + drflac__memory_stream* memoryStream = (drflac__memory_stream*)pUserData; + size_t bytesRemaining; + + DRFLAC_ASSERT(memoryStream != NULL); + DRFLAC_ASSERT(memoryStream->dataSize >= memoryStream->currentReadPos); + + bytesRemaining = memoryStream->dataSize - memoryStream->currentReadPos; + if (bytesToRead > bytesRemaining) { + bytesToRead = bytesRemaining; + } + + if (bytesToRead > 0) { + DRFLAC_COPY_MEMORY(bufferOut, memoryStream->data + memoryStream->currentReadPos, bytesToRead); + memoryStream->currentReadPos += bytesToRead; + } + + return bytesToRead; +} + +static drflac_bool32 drflac__on_seek_memory(void* pUserData, int offset, drflac_seek_origin origin) +{ + drflac__memory_stream* memoryStream = (drflac__memory_stream*)pUserData; + drflac_int64 newCursor; + + DRFLAC_ASSERT(memoryStream != NULL); + + if (origin == DRFLAC_SEEK_SET) { + newCursor = 0; + } else if (origin == DRFLAC_SEEK_CUR) { + newCursor = (drflac_int64)memoryStream->currentReadPos; + } else if (origin == DRFLAC_SEEK_END) { + newCursor = (drflac_int64)memoryStream->dataSize; + } else { + DRFLAC_ASSERT(!"Invalid seek origin"); + return DRFLAC_FALSE; + } + + newCursor += offset; + + if (newCursor < 0) { + return DRFLAC_FALSE; /* Trying to seek prior to the start of the buffer. */ + } + if ((size_t)newCursor > memoryStream->dataSize) { + return DRFLAC_FALSE; /* Trying to seek beyond the end of the buffer. */ + } + + memoryStream->currentReadPos = (size_t)newCursor; + + return DRFLAC_TRUE; +} + +static drflac_bool32 drflac__on_tell_memory(void* pUserData, drflac_int64* pCursor) +{ + drflac__memory_stream* memoryStream = (drflac__memory_stream*)pUserData; + + DRFLAC_ASSERT(memoryStream != NULL); + DRFLAC_ASSERT(pCursor != NULL); + + *pCursor = (drflac_int64)memoryStream->currentReadPos; + return DRFLAC_TRUE; +} + +DRFLAC_API drflac* drflac_open_memory(const void* pData, size_t dataSize, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac__memory_stream memoryStream; + drflac* pFlac; + + memoryStream.data = (const drflac_uint8*)pData; + memoryStream.dataSize = dataSize; + memoryStream.currentReadPos = 0; + pFlac = drflac_open(drflac__on_read_memory, drflac__on_seek_memory, drflac__on_tell_memory, &memoryStream, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + pFlac->memoryStream = memoryStream; + + /* This is an awful hack... */ +#ifndef DR_FLAC_NO_OGG + if (pFlac->container == drflac_container_ogg) + { + drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs; + oggbs->pUserData = &pFlac->memoryStream; + } + else +#endif + { + pFlac->bs.pUserData = &pFlac->memoryStream; + } + + return pFlac; +} + +DRFLAC_API drflac* drflac_open_memory_with_metadata(const void* pData, size_t dataSize, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac__memory_stream memoryStream; + drflac* pFlac; + + memoryStream.data = (const drflac_uint8*)pData; + memoryStream.dataSize = dataSize; + memoryStream.currentReadPos = 0; + pFlac = drflac_open_with_metadata_private(drflac__on_read_memory, drflac__on_seek_memory, drflac__on_tell_memory, onMeta, drflac_container_unknown, &memoryStream, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + pFlac->memoryStream = memoryStream; + + /* This is an awful hack... */ +#ifndef DR_FLAC_NO_OGG + if (pFlac->container == drflac_container_ogg) + { + drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs; + oggbs->pUserData = &pFlac->memoryStream; + } + else +#endif + { + pFlac->bs.pUserData = &pFlac->memoryStream; + } + + return pFlac; +} + + + +DRFLAC_API drflac* drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + return drflac_open_with_metadata_private(onRead, onSeek, onTell, NULL, drflac_container_unknown, pUserData, pUserData, pAllocationCallbacks); +} +DRFLAC_API drflac* drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + return drflac_open_with_metadata_private(onRead, onSeek, onTell, NULL, container, pUserData, pUserData, pAllocationCallbacks); +} + +DRFLAC_API drflac* drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + return drflac_open_with_metadata_private(onRead, onSeek, onTell, onMeta, drflac_container_unknown, pUserData, pUserData, pAllocationCallbacks); +} +DRFLAC_API drflac* drflac_open_with_metadata_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, drflac_meta_proc onMeta, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + return drflac_open_with_metadata_private(onRead, onSeek, onTell, onMeta, container, pUserData, pUserData, pAllocationCallbacks); +} + +DRFLAC_API void drflac_close(drflac* pFlac) +{ + if (pFlac == NULL) { + return; + } + +#ifndef DR_FLAC_NO_STDIO + /* + If we opened the file with drflac_open_file() we will want to close the file handle. We can know whether or not drflac_open_file() + was used by looking at the callbacks. + */ + if (pFlac->bs.onRead == drflac__on_read_stdio) { + fclose((FILE*)pFlac->bs.pUserData); + } + +#ifndef DR_FLAC_NO_OGG + /* Need to clean up Ogg streams a bit differently due to the way the bit streaming is chained. */ + if (pFlac->container == drflac_container_ogg) { + drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs; + DRFLAC_ASSERT(pFlac->bs.onRead == drflac__on_read_ogg); + + if (oggbs->onRead == drflac__on_read_stdio) { + fclose((FILE*)oggbs->pUserData); + } + } +#endif +#endif + + drflac__free_from_callbacks(pFlac, &pFlac->allocationCallbacks); +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_left_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 left = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 side = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_left_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 left0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 left1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 left2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 left3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 right0 = left0 - side0; + drflac_uint32 right1 = left1 - side1; + drflac_uint32 right2 = left2 - side2; + drflac_uint32 right3 = left3 - side3; + + pOutputSamples[i*8+0] = (drflac_int32)left0; + pOutputSamples[i*8+1] = (drflac_int32)right0; + pOutputSamples[i*8+2] = (drflac_int32)left1; + pOutputSamples[i*8+3] = (drflac_int32)right1; + pOutputSamples[i*8+4] = (drflac_int32)left2; + pOutputSamples[i*8+5] = (drflac_int32)right2; + pOutputSamples[i*8+6] = (drflac_int32)left3; + pOutputSamples[i*8+7] = (drflac_int32)right3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_left_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + for (i = 0; i < frameCount4; ++i) { + __m128i left = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i right = _mm_sub_epi32(left, side); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 0), _mm_unpacklo_epi32(left, right)); + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 4), _mm_unpackhi_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_left_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t left; + uint32x4_t side; + uint32x4_t right; + + left = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + right = vsubq_u32(left, side); + + drflac__vst2q_u32((drflac_uint32*)pOutputSamples + i*8, vzipq_u32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_left_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_left_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_left_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s32__decode_left_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s32__decode_left_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_right_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 side = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 right = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_right_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 side0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 side1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 side2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 side3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 right0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 right1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 right2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 right3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 left0 = right0 + side0; + drflac_uint32 left1 = right1 + side1; + drflac_uint32 left2 = right2 + side2; + drflac_uint32 left3 = right3 + side3; + + pOutputSamples[i*8+0] = (drflac_int32)left0; + pOutputSamples[i*8+1] = (drflac_int32)right0; + pOutputSamples[i*8+2] = (drflac_int32)left1; + pOutputSamples[i*8+3] = (drflac_int32)right1; + pOutputSamples[i*8+4] = (drflac_int32)left2; + pOutputSamples[i*8+5] = (drflac_int32)right2; + pOutputSamples[i*8+6] = (drflac_int32)left3; + pOutputSamples[i*8+7] = (drflac_int32)right3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_right_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + for (i = 0; i < frameCount4; ++i) { + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i right = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i left = _mm_add_epi32(right, side); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 0), _mm_unpacklo_epi32(left, right)); + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 4), _mm_unpackhi_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_right_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t side; + uint32x4_t right; + uint32x4_t left; + + side = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + right = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + left = vaddq_u32(right, side); + + drflac__vst2q_u32((drflac_uint32*)pOutputSamples + i*8, vzipq_u32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left; + pOutputSamples[i*2+1] = (drflac_int32)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_right_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_right_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_right_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s32__decode_right_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s32__decode_right_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_mid_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid + side) >> 1) << unusedBitsPerSample); + pOutputSamples[i*2+1] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid - side) >> 1) << unusedBitsPerSample); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_mid_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_int32 shift = unusedBitsPerSample; + + if (shift > 0) { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = (mid0 + side0) << shift; + temp1L = (mid1 + side1) << shift; + temp2L = (mid2 + side2) << shift; + temp3L = (mid3 + side3) << shift; + + temp0R = (mid0 - side0) << shift; + temp1R = (mid1 - side1) << shift; + temp2R = (mid2 - side2) << shift; + temp3R = (mid3 - side3) << shift; + + pOutputSamples[i*8+0] = (drflac_int32)temp0L; + pOutputSamples[i*8+1] = (drflac_int32)temp0R; + pOutputSamples[i*8+2] = (drflac_int32)temp1L; + pOutputSamples[i*8+3] = (drflac_int32)temp1R; + pOutputSamples[i*8+4] = (drflac_int32)temp2L; + pOutputSamples[i*8+5] = (drflac_int32)temp2R; + pOutputSamples[i*8+6] = (drflac_int32)temp3L; + pOutputSamples[i*8+7] = (drflac_int32)temp3R; + } + } else { + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = (drflac_uint32)((drflac_int32)(mid0 + side0) >> 1); + temp1L = (drflac_uint32)((drflac_int32)(mid1 + side1) >> 1); + temp2L = (drflac_uint32)((drflac_int32)(mid2 + side2) >> 1); + temp3L = (drflac_uint32)((drflac_int32)(mid3 + side3) >> 1); + + temp0R = (drflac_uint32)((drflac_int32)(mid0 - side0) >> 1); + temp1R = (drflac_uint32)((drflac_int32)(mid1 - side1) >> 1); + temp2R = (drflac_uint32)((drflac_int32)(mid2 - side2) >> 1); + temp3R = (drflac_uint32)((drflac_int32)(mid3 - side3) >> 1); + + pOutputSamples[i*8+0] = (drflac_int32)temp0L; + pOutputSamples[i*8+1] = (drflac_int32)temp0R; + pOutputSamples[i*8+2] = (drflac_int32)temp1L; + pOutputSamples[i*8+3] = (drflac_int32)temp1R; + pOutputSamples[i*8+4] = (drflac_int32)temp2L; + pOutputSamples[i*8+5] = (drflac_int32)temp2R; + pOutputSamples[i*8+6] = (drflac_int32)temp3L; + pOutputSamples[i*8+7] = (drflac_int32)temp3R; + } + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid + side) >> 1) << unusedBitsPerSample); + pOutputSamples[i*2+1] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid - side) >> 1) << unusedBitsPerSample); + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_mid_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_int32 shift = unusedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i left; + __m128i right; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + left = _mm_srai_epi32(_mm_add_epi32(mid, side), 1); + right = _mm_srai_epi32(_mm_sub_epi32(mid, side), 1); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 0), _mm_unpacklo_epi32(left, right)); + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 4), _mm_unpackhi_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)(mid + side) >> 1; + pOutputSamples[i*2+1] = (drflac_int32)(mid - side) >> 1; + } + } else { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i left; + __m128i right; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + left = _mm_slli_epi32(_mm_add_epi32(mid, side), shift); + right = _mm_slli_epi32(_mm_sub_epi32(mid, side), shift); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 0), _mm_unpacklo_epi32(left, right)); + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 4), _mm_unpackhi_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((mid + side) << shift); + pOutputSamples[i*2+1] = (drflac_int32)((mid - side) << shift); + } + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_mid_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_int32 shift = unusedBitsPerSample; + int32x4_t wbpsShift0_4; /* wbps = Wasted Bits Per Sample */ + int32x4_t wbpsShift1_4; /* wbps = Wasted Bits Per Sample */ + uint32x4_t one4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + wbpsShift0_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + wbpsShift1_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + one4 = vdupq_n_u32(1); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + uint32x4_t mid; + uint32x4_t side; + int32x4_t left; + int32x4_t right; + + mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbpsShift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbpsShift1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, one4)); + + left = vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid, side)), 1); + right = vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid, side)), 1); + + drflac__vst2q_s32(pOutputSamples + i*8, vzipq_s32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)(mid + side) >> 1; + pOutputSamples[i*2+1] = (drflac_int32)(mid - side) >> 1; + } + } else { + int32x4_t shift4; + + shift -= 1; + shift4 = vdupq_n_s32(shift); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t mid; + uint32x4_t side; + int32x4_t left; + int32x4_t right; + + mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbpsShift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbpsShift1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, one4)); + + left = vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid, side), shift4)); + right = vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid, side), shift4)); + + drflac__vst2q_s32(pOutputSamples + i*8, vzipq_s32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((mid + side) << shift); + pOutputSamples[i*2+1] = (drflac_int32)((mid - side) << shift); + } + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_mid_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_mid_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_mid_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s32__decode_mid_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s32__decode_mid_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_independent_stereo__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)((drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample)); + pOutputSamples[i*2+1] = (drflac_int32)((drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample)); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_independent_stereo__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 tempL0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 tempL1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 tempL2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 tempL3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 tempR0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 tempR1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 tempR2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 tempR3 = pInputSamples1U32[i*4+3] << shift1; + + pOutputSamples[i*8+0] = (drflac_int32)tempL0; + pOutputSamples[i*8+1] = (drflac_int32)tempR0; + pOutputSamples[i*8+2] = (drflac_int32)tempL1; + pOutputSamples[i*8+3] = (drflac_int32)tempR1; + pOutputSamples[i*8+4] = (drflac_int32)tempL2; + pOutputSamples[i*8+5] = (drflac_int32)tempR2; + pOutputSamples[i*8+6] = (drflac_int32)tempL3; + pOutputSamples[i*8+7] = (drflac_int32)tempR3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0); + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1); + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_independent_stereo__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + __m128i left = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i right = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 0), _mm_unpacklo_epi32(left, right)); + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8 + 4), _mm_unpackhi_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0); + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1); + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_independent_stereo__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + int32x4_t shift4_0 = vdupq_n_s32(shift0); + int32x4_t shift4_1 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + int32x4_t left; + int32x4_t right; + + left = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift4_0)); + right = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift4_1)); + + drflac__vst2q_s32(pOutputSamples + i*8, vzipq_s32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0); + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s32__decode_independent_stereo(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int32* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_independent_stereo__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s32__decode_independent_stereo__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s32__decode_independent_stereo__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s32__decode_independent_stereo__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +static drflac_bool32 drflac__is_current_flac_frame_valid(drflac* pFlac) +{ + drflac_uint32 iChannel; + + if (pFlac->currentFLACFrame.header.blockSizeInPCMFrames > pFlac->maxBlockSizeInPCMFrames || pFlac->currentFLACFrame.pcmFramesRemaining > pFlac->currentFLACFrame.header.blockSizeInPCMFrames) { + return DRFLAC_FALSE; + } + + for (iChannel = 0; iChannel < pFlac->channels; iChannel += 1) { + if (pFlac->currentFLACFrame.subframes[iChannel].pSamplesS32 == NULL) { + return DRFLAC_FALSE; + } + } + + return DRFLAC_TRUE; +} + +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s32(drflac* pFlac, drflac_uint64 framesToRead, drflac_int32* pBufferOut) +{ + drflac_uint64 framesRead; + drflac_uint32 unusedBitsPerSample; + + if (pFlac == NULL || framesToRead == 0) { + return 0; + } + + if (pBufferOut == NULL) { + return drflac__seek_forward_by_pcm_frames(pFlac, framesToRead); + } + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 32); + unusedBitsPerSample = 32 - pFlac->bitsPerSample; + + framesRead = 0; + while (framesToRead > 0) { + /* If we've run out of samples in this frame, go to the next. */ + if (pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_and_decode_next_flac_frame(pFlac)) { + break; /* Couldn't read the next frame, so just break from the loop and return. */ + } + } else { + unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFLACFrame.header.channelAssignment); + drflac_uint64 iFirstPCMFrame = pFlac->currentFLACFrame.header.blockSizeInPCMFrames - pFlac->currentFLACFrame.pcmFramesRemaining; + drflac_uint64 frameCountThisIteration = framesToRead; + + if (frameCountThisIteration > pFlac->currentFLACFrame.pcmFramesRemaining) { + frameCountThisIteration = pFlac->currentFLACFrame.pcmFramesRemaining; + } + + if (channelCount == 2) { + const drflac_int32* pDecodedSamples0 = pFlac->currentFLACFrame.subframes[0].pSamplesS32 + iFirstPCMFrame; + const drflac_int32* pDecodedSamples1 = pFlac->currentFLACFrame.subframes[1].pSamplesS32 + iFirstPCMFrame; + + switch (pFlac->currentFLACFrame.header.channelAssignment) + { + case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE: + { + drflac_read_pcm_frames_s32__decode_left_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE: + { + drflac_read_pcm_frames_s32__decode_right_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE: + { + drflac_read_pcm_frames_s32__decode_mid_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT: + default: + { + drflac_read_pcm_frames_s32__decode_independent_stereo(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + } + } else { + /* Generic interleaving. */ + drflac_uint64 i; + for (i = 0; i < frameCountThisIteration; ++i) { + unsigned int j; + for (j = 0; j < channelCount; ++j) { + pBufferOut[(i*channelCount)+j] = (drflac_int32)((drflac_uint32)(pFlac->currentFLACFrame.subframes[j].pSamplesS32[iFirstPCMFrame + i]) << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[j].wastedBitsPerSample)); + } + } + } + + framesRead += frameCountThisIteration; + pBufferOut += frameCountThisIteration * channelCount; + framesToRead -= frameCountThisIteration; + pFlac->currentPCMFrame += frameCountThisIteration; + pFlac->currentFLACFrame.pcmFramesRemaining -= (drflac_uint32)frameCountThisIteration; + } + } + + return framesRead; +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_left_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 left = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 side = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 right = left - side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_left_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 left0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 left1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 left2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 left3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 right0 = left0 - side0; + drflac_uint32 right1 = left1 - side1; + drflac_uint32 right2 = left2 - side2; + drflac_uint32 right3 = left3 - side3; + + left0 >>= 16; + left1 >>= 16; + left2 >>= 16; + left3 >>= 16; + + right0 >>= 16; + right1 >>= 16; + right2 >>= 16; + right3 >>= 16; + + pOutputSamples[i*8+0] = (drflac_int16)left0; + pOutputSamples[i*8+1] = (drflac_int16)right0; + pOutputSamples[i*8+2] = (drflac_int16)left1; + pOutputSamples[i*8+3] = (drflac_int16)right1; + pOutputSamples[i*8+4] = (drflac_int16)left2; + pOutputSamples[i*8+5] = (drflac_int16)right2; + pOutputSamples[i*8+6] = (drflac_int16)left3; + pOutputSamples[i*8+7] = (drflac_int16)right3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_left_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + for (i = 0; i < frameCount4; ++i) { + __m128i left = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i right = _mm_sub_epi32(left, side); + + left = _mm_srai_epi32(left, 16); + right = _mm_srai_epi32(right, 16); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8), drflac__mm_packs_interleaved_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_left_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t left; + uint32x4_t side; + uint32x4_t right; + + left = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + right = vsubq_u32(left, side); + + left = vshrq_n_u32(left, 16); + right = vshrq_n_u32(right, 16); + + drflac__vst2q_u16((drflac_uint16*)pOutputSamples + i*8, vzip_u16(vmovn_u32(left), vmovn_u32(right))); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_left_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_left_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_left_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s16__decode_left_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s16__decode_left_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_right_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 side = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 right = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 left = right + side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_right_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 side0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 side1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 side2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 side3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 right0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 right1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 right2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 right3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 left0 = right0 + side0; + drflac_uint32 left1 = right1 + side1; + drflac_uint32 left2 = right2 + side2; + drflac_uint32 left3 = right3 + side3; + + left0 >>= 16; + left1 >>= 16; + left2 >>= 16; + left3 >>= 16; + + right0 >>= 16; + right1 >>= 16; + right2 >>= 16; + right3 >>= 16; + + pOutputSamples[i*8+0] = (drflac_int16)left0; + pOutputSamples[i*8+1] = (drflac_int16)right0; + pOutputSamples[i*8+2] = (drflac_int16)left1; + pOutputSamples[i*8+3] = (drflac_int16)right1; + pOutputSamples[i*8+4] = (drflac_int16)left2; + pOutputSamples[i*8+5] = (drflac_int16)right2; + pOutputSamples[i*8+6] = (drflac_int16)left3; + pOutputSamples[i*8+7] = (drflac_int16)right3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_right_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + for (i = 0; i < frameCount4; ++i) { + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i right = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i left = _mm_add_epi32(right, side); + + left = _mm_srai_epi32(left, 16); + right = _mm_srai_epi32(right, 16); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8), drflac__mm_packs_interleaved_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_right_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t side; + uint32x4_t right; + uint32x4_t left; + + side = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + right = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + left = vaddq_u32(right, side); + + left = vshrq_n_u32(left, 16); + right = vshrq_n_u32(right, 16); + + drflac__vst2q_u16((drflac_uint16*)pOutputSamples + i*8, vzip_u16(vmovn_u32(left), vmovn_u32(right))); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + left >>= 16; + right >>= 16; + + pOutputSamples[i*2+0] = (drflac_int16)left; + pOutputSamples[i*2+1] = (drflac_int16)right; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_right_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_right_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_right_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s16__decode_right_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s16__decode_right_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_mid_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + drflac_uint32 mid = (drflac_uint32)pInputSamples0[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = (drflac_uint32)pInputSamples1[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((drflac_uint32)((drflac_int32)(mid + side) >> 1) << unusedBitsPerSample) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((drflac_uint32)((drflac_int32)(mid - side) >> 1) << unusedBitsPerSample) >> 16); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_mid_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample; + + if (shift > 0) { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = (mid0 + side0) << shift; + temp1L = (mid1 + side1) << shift; + temp2L = (mid2 + side2) << shift; + temp3L = (mid3 + side3) << shift; + + temp0R = (mid0 - side0) << shift; + temp1R = (mid1 - side1) << shift; + temp2R = (mid2 - side2) << shift; + temp3R = (mid3 - side3) << shift; + + temp0L >>= 16; + temp1L >>= 16; + temp2L >>= 16; + temp3L >>= 16; + + temp0R >>= 16; + temp1R >>= 16; + temp2R >>= 16; + temp3R >>= 16; + + pOutputSamples[i*8+0] = (drflac_int16)temp0L; + pOutputSamples[i*8+1] = (drflac_int16)temp0R; + pOutputSamples[i*8+2] = (drflac_int16)temp1L; + pOutputSamples[i*8+3] = (drflac_int16)temp1R; + pOutputSamples[i*8+4] = (drflac_int16)temp2L; + pOutputSamples[i*8+5] = (drflac_int16)temp2R; + pOutputSamples[i*8+6] = (drflac_int16)temp3L; + pOutputSamples[i*8+7] = (drflac_int16)temp3R; + } + } else { + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = ((drflac_int32)(mid0 + side0) >> 1); + temp1L = ((drflac_int32)(mid1 + side1) >> 1); + temp2L = ((drflac_int32)(mid2 + side2) >> 1); + temp3L = ((drflac_int32)(mid3 + side3) >> 1); + + temp0R = ((drflac_int32)(mid0 - side0) >> 1); + temp1R = ((drflac_int32)(mid1 - side1) >> 1); + temp2R = ((drflac_int32)(mid2 - side2) >> 1); + temp3R = ((drflac_int32)(mid3 - side3) >> 1); + + temp0L >>= 16; + temp1L >>= 16; + temp2L >>= 16; + temp3L >>= 16; + + temp0R >>= 16; + temp1R >>= 16; + temp2R >>= 16; + temp3R >>= 16; + + pOutputSamples[i*8+0] = (drflac_int16)temp0L; + pOutputSamples[i*8+1] = (drflac_int16)temp0R; + pOutputSamples[i*8+2] = (drflac_int16)temp1L; + pOutputSamples[i*8+3] = (drflac_int16)temp1R; + pOutputSamples[i*8+4] = (drflac_int16)temp2L; + pOutputSamples[i*8+5] = (drflac_int16)temp2R; + pOutputSamples[i*8+6] = (drflac_int16)temp3L; + pOutputSamples[i*8+7] = (drflac_int16)temp3R; + } + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((drflac_uint32)((drflac_int32)(mid + side) >> 1) << unusedBitsPerSample) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((drflac_uint32)((drflac_int32)(mid - side) >> 1) << unusedBitsPerSample) >> 16); + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_mid_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i left; + __m128i right; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + left = _mm_srai_epi32(_mm_add_epi32(mid, side), 1); + right = _mm_srai_epi32(_mm_sub_epi32(mid, side), 1); + + left = _mm_srai_epi32(left, 16); + right = _mm_srai_epi32(right, 16); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8), drflac__mm_packs_interleaved_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((drflac_int32)(mid + side) >> 1) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((drflac_int32)(mid - side) >> 1) >> 16); + } + } else { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i left; + __m128i right; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + left = _mm_slli_epi32(_mm_add_epi32(mid, side), shift); + right = _mm_slli_epi32(_mm_sub_epi32(mid, side), shift); + + left = _mm_srai_epi32(left, 16); + right = _mm_srai_epi32(right, 16); + + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8), drflac__mm_packs_interleaved_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((mid + side) << shift) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((mid - side) << shift) >> 16); + } + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_mid_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample; + int32x4_t wbpsShift0_4; /* wbps = Wasted Bits Per Sample */ + int32x4_t wbpsShift1_4; /* wbps = Wasted Bits Per Sample */ + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + wbpsShift0_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + wbpsShift1_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + uint32x4_t mid; + uint32x4_t side; + int32x4_t left; + int32x4_t right; + + mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbpsShift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbpsShift1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, vdupq_n_u32(1))); + + left = vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid, side)), 1); + right = vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid, side)), 1); + + left = vshrq_n_s32(left, 16); + right = vshrq_n_s32(right, 16); + + drflac__vst2q_s16(pOutputSamples + i*8, vzip_s16(vmovn_s32(left), vmovn_s32(right))); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((drflac_int32)(mid + side) >> 1) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((drflac_int32)(mid - side) >> 1) >> 16); + } + } else { + int32x4_t shift4; + + shift -= 1; + shift4 = vdupq_n_s32(shift); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t mid; + uint32x4_t side; + int32x4_t left; + int32x4_t right; + + mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbpsShift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbpsShift1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, vdupq_n_u32(1))); + + left = vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid, side), shift4)); + right = vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid, side), shift4)); + + left = vshrq_n_s32(left, 16); + right = vshrq_n_s32(right, 16); + + drflac__vst2q_s16(pOutputSamples + i*8, vzip_s16(vmovn_s32(left), vmovn_s32(right))); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int16)(((mid + side) << shift) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)(((mid - side) << shift) >> 16); + } + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_mid_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_mid_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_mid_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s16__decode_mid_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s16__decode_mid_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_independent_stereo__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int16)((drflac_int32)((drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample)) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)((drflac_int32)((drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample)) >> 16); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_independent_stereo__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 tempL0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 tempL1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 tempL2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 tempL3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 tempR0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 tempR1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 tempR2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 tempR3 = pInputSamples1U32[i*4+3] << shift1; + + tempL0 >>= 16; + tempL1 >>= 16; + tempL2 >>= 16; + tempL3 >>= 16; + + tempR0 >>= 16; + tempR1 >>= 16; + tempR2 >>= 16; + tempR3 >>= 16; + + pOutputSamples[i*8+0] = (drflac_int16)tempL0; + pOutputSamples[i*8+1] = (drflac_int16)tempR0; + pOutputSamples[i*8+2] = (drflac_int16)tempL1; + pOutputSamples[i*8+3] = (drflac_int16)tempR1; + pOutputSamples[i*8+4] = (drflac_int16)tempL2; + pOutputSamples[i*8+5] = (drflac_int16)tempR2; + pOutputSamples[i*8+6] = (drflac_int16)tempL3; + pOutputSamples[i*8+7] = (drflac_int16)tempR3; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int16)((pInputSamples0U32[i] << shift0) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)((pInputSamples1U32[i] << shift1) >> 16); + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_independent_stereo__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + for (i = 0; i < frameCount4; ++i) { + __m128i left = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i right = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + + left = _mm_srai_epi32(left, 16); + right = _mm_srai_epi32(right, 16); + + /* At this point we have results. We can now pack and interleave these into a single __m128i object and then store the in the output buffer. */ + _mm_storeu_si128((__m128i*)(pOutputSamples + i*8), drflac__mm_packs_interleaved_epi32(left, right)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int16)((pInputSamples0U32[i] << shift0) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)((pInputSamples1U32[i] << shift1) >> 16); + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_independent_stereo__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + int32x4_t shift0_4 = vdupq_n_s32(shift0); + int32x4_t shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + int32x4_t left; + int32x4_t right; + + left = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4)); + right = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4)); + + left = vshrq_n_s32(left, 16); + right = vshrq_n_s32(right, 16); + + drflac__vst2q_s16(pOutputSamples + i*8, vzip_s16(vmovn_s32(left), vmovn_s32(right))); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int16)((pInputSamples0U32[i] << shift0) >> 16); + pOutputSamples[i*2+1] = (drflac_int16)((pInputSamples1U32[i] << shift1) >> 16); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_s16__decode_independent_stereo(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, drflac_int16* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_independent_stereo__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_s16__decode_independent_stereo__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_s16__decode_independent_stereo__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_s16__decode_independent_stereo__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s16(drflac* pFlac, drflac_uint64 framesToRead, drflac_int16* pBufferOut) +{ + drflac_uint64 framesRead; + drflac_uint32 unusedBitsPerSample; + + if (pFlac == NULL || framesToRead == 0) { + return 0; + } + + if (pBufferOut == NULL) { + return drflac__seek_forward_by_pcm_frames(pFlac, framesToRead); + } + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 32); + unusedBitsPerSample = 32 - pFlac->bitsPerSample; + + framesRead = 0; + while (framesToRead > 0) { + /* If we've run out of samples in this frame, go to the next. */ + if (pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_and_decode_next_flac_frame(pFlac)) { + break; /* Couldn't read the next frame, so just break from the loop and return. */ + } + } else { + unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFLACFrame.header.channelAssignment); + drflac_uint64 iFirstPCMFrame = pFlac->currentFLACFrame.header.blockSizeInPCMFrames - pFlac->currentFLACFrame.pcmFramesRemaining; + drflac_uint64 frameCountThisIteration = framesToRead; + + if (frameCountThisIteration > pFlac->currentFLACFrame.pcmFramesRemaining) { + frameCountThisIteration = pFlac->currentFLACFrame.pcmFramesRemaining; + } + + if (channelCount == 2) { + const drflac_int32* pDecodedSamples0 = pFlac->currentFLACFrame.subframes[0].pSamplesS32 + iFirstPCMFrame; + const drflac_int32* pDecodedSamples1 = pFlac->currentFLACFrame.subframes[1].pSamplesS32 + iFirstPCMFrame; + + switch (pFlac->currentFLACFrame.header.channelAssignment) + { + case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE: + { + drflac_read_pcm_frames_s16__decode_left_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE: + { + drflac_read_pcm_frames_s16__decode_right_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE: + { + drflac_read_pcm_frames_s16__decode_mid_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT: + default: + { + drflac_read_pcm_frames_s16__decode_independent_stereo(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + } + } else { + /* Generic interleaving. */ + drflac_uint64 i; + for (i = 0; i < frameCountThisIteration; ++i) { + unsigned int j; + for (j = 0; j < channelCount; ++j) { + drflac_int32 sampleS32 = (drflac_int32)((drflac_uint32)(pFlac->currentFLACFrame.subframes[j].pSamplesS32[iFirstPCMFrame + i]) << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[j].wastedBitsPerSample)); + pBufferOut[(i*channelCount)+j] = (drflac_int16)(sampleS32 >> 16); + } + } + } + + framesRead += frameCountThisIteration; + pBufferOut += frameCountThisIteration * channelCount; + framesToRead -= frameCountThisIteration; + pFlac->currentPCMFrame += frameCountThisIteration; + pFlac->currentFLACFrame.pcmFramesRemaining -= (drflac_uint32)frameCountThisIteration; + } + } + + return framesRead; +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 left = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 side = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (float)((drflac_int32)left / 2147483648.0); + pOutputSamples[i*2+1] = (float)((drflac_int32)right / 2147483648.0); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + float factor = 1 / 2147483648.0; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 left0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 left1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 left2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 left3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 right0 = left0 - side0; + drflac_uint32 right1 = left1 - side1; + drflac_uint32 right2 = left2 - side2; + drflac_uint32 right3 = left3 - side3; + + pOutputSamples[i*8+0] = (drflac_int32)left0 * factor; + pOutputSamples[i*8+1] = (drflac_int32)right0 * factor; + pOutputSamples[i*8+2] = (drflac_int32)left1 * factor; + pOutputSamples[i*8+3] = (drflac_int32)right1 * factor; + pOutputSamples[i*8+4] = (drflac_int32)left2 * factor; + pOutputSamples[i*8+5] = (drflac_int32)right2 * factor; + pOutputSamples[i*8+6] = (drflac_int32)left3 * factor; + pOutputSamples[i*8+7] = (drflac_int32)right3 * factor; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left * factor; + pOutputSamples[i*2+1] = (drflac_int32)right * factor; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + __m128 factor; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor = _mm_set1_ps(1.0f / 8388608.0f); + + for (i = 0; i < frameCount4; ++i) { + __m128i left = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i right = _mm_sub_epi32(left, side); + __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor); + __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(right), factor); + + _mm_storeu_ps(pOutputSamples + i*8 + 0, _mm_unpacklo_ps(leftf, rightf)); + _mm_storeu_ps(pOutputSamples + i*8 + 4, _mm_unpackhi_ps(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left / 8388608.0f; + pOutputSamples[i*2+1] = (drflac_int32)right / 8388608.0f; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + float32x4_t factor4; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor4 = vdupq_n_f32(1.0f / 8388608.0f); + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t left; + uint32x4_t side; + uint32x4_t right; + float32x4_t leftf; + float32x4_t rightf; + + left = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + right = vsubq_u32(left, side); + leftf = vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(left)), factor4); + rightf = vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(right)), factor4); + + drflac__vst2q_f32(pOutputSamples + i*8, vzipq_f32(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 left = pInputSamples0U32[i] << shift0; + drflac_uint32 side = pInputSamples1U32[i] << shift1; + drflac_uint32 right = left - side; + + pOutputSamples[i*2+0] = (drflac_int32)left / 8388608.0f; + pOutputSamples[i*2+1] = (drflac_int32)right / 8388608.0f; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_left_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_left_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_f32__decode_left_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_f32__decode_left_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + for (i = 0; i < frameCount; ++i) { + drflac_uint32 side = (drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + drflac_uint32 right = (drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (float)((drflac_int32)left / 2147483648.0); + pOutputSamples[i*2+1] = (float)((drflac_int32)right / 2147483648.0); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + float factor = 1 / 2147483648.0; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 side0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 side1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 side2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 side3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 right0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 right1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 right2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 right3 = pInputSamples1U32[i*4+3] << shift1; + + drflac_uint32 left0 = right0 + side0; + drflac_uint32 left1 = right1 + side1; + drflac_uint32 left2 = right2 + side2; + drflac_uint32 left3 = right3 + side3; + + pOutputSamples[i*8+0] = (drflac_int32)left0 * factor; + pOutputSamples[i*8+1] = (drflac_int32)right0 * factor; + pOutputSamples[i*8+2] = (drflac_int32)left1 * factor; + pOutputSamples[i*8+3] = (drflac_int32)right1 * factor; + pOutputSamples[i*8+4] = (drflac_int32)left2 * factor; + pOutputSamples[i*8+5] = (drflac_int32)right2 * factor; + pOutputSamples[i*8+6] = (drflac_int32)left3 * factor; + pOutputSamples[i*8+7] = (drflac_int32)right3 * factor; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left * factor; + pOutputSamples[i*2+1] = (drflac_int32)right * factor; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + __m128 factor; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor = _mm_set1_ps(1.0f / 8388608.0f); + + for (i = 0; i < frameCount4; ++i) { + __m128i side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + __m128i right = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + __m128i left = _mm_add_epi32(right, side); + __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor); + __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(right), factor); + + _mm_storeu_ps(pOutputSamples + i*8 + 0, _mm_unpacklo_ps(leftf, rightf)); + _mm_storeu_ps(pOutputSamples + i*8 + 4, _mm_unpackhi_ps(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left / 8388608.0f; + pOutputSamples[i*2+1] = (drflac_int32)right / 8388608.0f; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + float32x4_t factor4; + int32x4_t shift0_4; + int32x4_t shift1_4; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor4 = vdupq_n_f32(1.0f / 8388608.0f); + shift0_4 = vdupq_n_s32(shift0); + shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + uint32x4_t side; + uint32x4_t right; + uint32x4_t left; + float32x4_t leftf; + float32x4_t rightf; + + side = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4); + right = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4); + left = vaddq_u32(right, side); + leftf = vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(left)), factor4); + rightf = vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(right)), factor4); + + drflac__vst2q_f32(pOutputSamples + i*8, vzipq_f32(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 side = pInputSamples0U32[i] << shift0; + drflac_uint32 right = pInputSamples1U32[i] << shift1; + drflac_uint32 left = right + side; + + pOutputSamples[i*2+0] = (drflac_int32)left / 8388608.0f; + pOutputSamples[i*2+1] = (drflac_int32)right / 8388608.0f; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_right_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_right_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_f32__decode_right_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_f32__decode_right_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + drflac_uint32 mid = (drflac_uint32)pInputSamples0[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = (drflac_uint32)pInputSamples1[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (float)((((drflac_int32)(mid + side) >> 1) << (unusedBitsPerSample)) / 2147483648.0); + pOutputSamples[i*2+1] = (float)((((drflac_int32)(mid - side) >> 1) << (unusedBitsPerSample)) / 2147483648.0); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample; + float factor = 1 / 2147483648.0; + + if (shift > 0) { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = (mid0 + side0) << shift; + temp1L = (mid1 + side1) << shift; + temp2L = (mid2 + side2) << shift; + temp3L = (mid3 + side3) << shift; + + temp0R = (mid0 - side0) << shift; + temp1R = (mid1 - side1) << shift; + temp2R = (mid2 - side2) << shift; + temp3R = (mid3 - side3) << shift; + + pOutputSamples[i*8+0] = (drflac_int32)temp0L * factor; + pOutputSamples[i*8+1] = (drflac_int32)temp0R * factor; + pOutputSamples[i*8+2] = (drflac_int32)temp1L * factor; + pOutputSamples[i*8+3] = (drflac_int32)temp1R * factor; + pOutputSamples[i*8+4] = (drflac_int32)temp2L * factor; + pOutputSamples[i*8+5] = (drflac_int32)temp2R * factor; + pOutputSamples[i*8+6] = (drflac_int32)temp3L * factor; + pOutputSamples[i*8+7] = (drflac_int32)temp3R * factor; + } + } else { + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 temp0L; + drflac_uint32 temp1L; + drflac_uint32 temp2L; + drflac_uint32 temp3L; + drflac_uint32 temp0R; + drflac_uint32 temp1R; + drflac_uint32 temp2R; + drflac_uint32 temp3R; + + drflac_uint32 mid0 = pInputSamples0U32[i*4+0] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid1 = pInputSamples0U32[i*4+1] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid2 = pInputSamples0U32[i*4+2] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 mid3 = pInputSamples0U32[i*4+3] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + + drflac_uint32 side0 = pInputSamples1U32[i*4+0] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side1 = pInputSamples1U32[i*4+1] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side2 = pInputSamples1U32[i*4+2] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + drflac_uint32 side3 = pInputSamples1U32[i*4+3] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid0 = (mid0 << 1) | (side0 & 0x01); + mid1 = (mid1 << 1) | (side1 & 0x01); + mid2 = (mid2 << 1) | (side2 & 0x01); + mid3 = (mid3 << 1) | (side3 & 0x01); + + temp0L = (drflac_uint32)((drflac_int32)(mid0 + side0) >> 1); + temp1L = (drflac_uint32)((drflac_int32)(mid1 + side1) >> 1); + temp2L = (drflac_uint32)((drflac_int32)(mid2 + side2) >> 1); + temp3L = (drflac_uint32)((drflac_int32)(mid3 + side3) >> 1); + + temp0R = (drflac_uint32)((drflac_int32)(mid0 - side0) >> 1); + temp1R = (drflac_uint32)((drflac_int32)(mid1 - side1) >> 1); + temp2R = (drflac_uint32)((drflac_int32)(mid2 - side2) >> 1); + temp3R = (drflac_uint32)((drflac_int32)(mid3 - side3) >> 1); + + pOutputSamples[i*8+0] = (drflac_int32)temp0L * factor; + pOutputSamples[i*8+1] = (drflac_int32)temp0R * factor; + pOutputSamples[i*8+2] = (drflac_int32)temp1L * factor; + pOutputSamples[i*8+3] = (drflac_int32)temp1R * factor; + pOutputSamples[i*8+4] = (drflac_int32)temp2L * factor; + pOutputSamples[i*8+5] = (drflac_int32)temp2R * factor; + pOutputSamples[i*8+6] = (drflac_int32)temp3L * factor; + pOutputSamples[i*8+7] = (drflac_int32)temp3R * factor; + } + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid + side) >> 1) << unusedBitsPerSample) * factor; + pOutputSamples[i*2+1] = (drflac_int32)((drflac_uint32)((drflac_int32)(mid - side) >> 1) << unusedBitsPerSample) * factor; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample - 8; + float factor; + __m128 factor128; + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor = 1.0f / 8388608.0f; + factor128 = _mm_set1_ps(factor); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i tempL; + __m128i tempR; + __m128 leftf; + __m128 rightf; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + tempL = _mm_srai_epi32(_mm_add_epi32(mid, side), 1); + tempR = _mm_srai_epi32(_mm_sub_epi32(mid, side), 1); + + leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128); + rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128); + + _mm_storeu_ps(pOutputSamples + i*8 + 0, _mm_unpacklo_ps(leftf, rightf)); + _mm_storeu_ps(pOutputSamples + i*8 + 4, _mm_unpackhi_ps(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = ((drflac_int32)(mid + side) >> 1) * factor; + pOutputSamples[i*2+1] = ((drflac_int32)(mid - side) >> 1) * factor; + } + } else { + shift -= 1; + for (i = 0; i < frameCount4; ++i) { + __m128i mid; + __m128i side; + __m128i tempL; + __m128i tempR; + __m128 leftf; + __m128 rightf; + + mid = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + side = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01))); + + tempL = _mm_slli_epi32(_mm_add_epi32(mid, side), shift); + tempR = _mm_slli_epi32(_mm_sub_epi32(mid, side), shift); + + leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128); + rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128); + + _mm_storeu_ps(pOutputSamples + i*8 + 0, _mm_unpacklo_ps(leftf, rightf)); + _mm_storeu_ps(pOutputSamples + i*8 + 4, _mm_unpackhi_ps(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((mid + side) << shift) * factor; + pOutputSamples[i*2+1] = (drflac_int32)((mid - side) << shift) * factor; + } + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift = unusedBitsPerSample - 8; + float factor; + float32x4_t factor4; + int32x4_t shift4; + int32x4_t wbps0_4; /* Wasted Bits Per Sample */ + int32x4_t wbps1_4; /* Wasted Bits Per Sample */ + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 24); + + factor = 1.0f / 8388608.0f; + factor4 = vdupq_n_f32(factor); + wbps0_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample); + wbps1_4 = vdupq_n_s32(pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample); + + if (shift == 0) { + for (i = 0; i < frameCount4; ++i) { + int32x4_t lefti; + int32x4_t righti; + float32x4_t leftf; + float32x4_t rightf; + + uint32x4_t mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbps0_4); + uint32x4_t side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbps1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, vdupq_n_u32(1))); + + lefti = vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid, side)), 1); + righti = vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid, side)), 1); + + leftf = vmulq_f32(vcvtq_f32_s32(lefti), factor4); + rightf = vmulq_f32(vcvtq_f32_s32(righti), factor4); + + drflac__vst2q_f32(pOutputSamples + i*8, vzipq_f32(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = ((drflac_int32)(mid + side) >> 1) * factor; + pOutputSamples[i*2+1] = ((drflac_int32)(mid - side) >> 1) * factor; + } + } else { + shift -= 1; + shift4 = vdupq_n_s32(shift); + for (i = 0; i < frameCount4; ++i) { + uint32x4_t mid; + uint32x4_t side; + int32x4_t lefti; + int32x4_t righti; + float32x4_t leftf; + float32x4_t rightf; + + mid = vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), wbps0_4); + side = vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), wbps1_4); + + mid = vorrq_u32(vshlq_n_u32(mid, 1), vandq_u32(side, vdupq_n_u32(1))); + + lefti = vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid, side), shift4)); + righti = vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid, side), shift4)); + + leftf = vmulq_f32(vcvtq_f32_s32(lefti), factor4); + rightf = vmulq_f32(vcvtq_f32_s32(righti), factor4); + + drflac__vst2q_f32(pOutputSamples + i*8, vzipq_f32(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + drflac_uint32 mid = pInputSamples0U32[i] << pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 side = pInputSamples1U32[i] << pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + + mid = (mid << 1) | (side & 0x01); + + pOutputSamples[i*2+0] = (drflac_int32)((mid + side) << shift) * factor; + pOutputSamples[i*2+1] = (drflac_int32)((mid - side) << shift) * factor; + } + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_mid_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_mid_side__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_f32__decode_mid_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_f32__decode_mid_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + +#if 0 +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + for (drflac_uint64 i = 0; i < frameCount; ++i) { + pOutputSamples[i*2+0] = (float)((drflac_int32)((drflac_uint32)pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample)) / 2147483648.0); + pOutputSamples[i*2+1] = (float)((drflac_int32)((drflac_uint32)pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample)) / 2147483648.0); + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample; + drflac_uint32 shift1 = unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample; + float factor = 1 / 2147483648.0; + + for (i = 0; i < frameCount4; ++i) { + drflac_uint32 tempL0 = pInputSamples0U32[i*4+0] << shift0; + drflac_uint32 tempL1 = pInputSamples0U32[i*4+1] << shift0; + drflac_uint32 tempL2 = pInputSamples0U32[i*4+2] << shift0; + drflac_uint32 tempL3 = pInputSamples0U32[i*4+3] << shift0; + + drflac_uint32 tempR0 = pInputSamples1U32[i*4+0] << shift1; + drflac_uint32 tempR1 = pInputSamples1U32[i*4+1] << shift1; + drflac_uint32 tempR2 = pInputSamples1U32[i*4+2] << shift1; + drflac_uint32 tempR3 = pInputSamples1U32[i*4+3] << shift1; + + pOutputSamples[i*8+0] = (drflac_int32)tempL0 * factor; + pOutputSamples[i*8+1] = (drflac_int32)tempR0 * factor; + pOutputSamples[i*8+2] = (drflac_int32)tempL1 * factor; + pOutputSamples[i*8+3] = (drflac_int32)tempR1 * factor; + pOutputSamples[i*8+4] = (drflac_int32)tempL2 * factor; + pOutputSamples[i*8+5] = (drflac_int32)tempR2 * factor; + pOutputSamples[i*8+6] = (drflac_int32)tempL3 * factor; + pOutputSamples[i*8+7] = (drflac_int32)tempR3 * factor; + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0) * factor; + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1) * factor; + } +} + +#if defined(DRFLAC_SUPPORT_SSE2) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + + float factor = 1.0f / 8388608.0f; + __m128 factor128 = _mm_set1_ps(factor); + + for (i = 0; i < frameCount4; ++i) { + __m128i lefti; + __m128i righti; + __m128 leftf; + __m128 rightf; + + lefti = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples0 + i), shift0); + righti = _mm_slli_epi32(_mm_loadu_si128((const __m128i*)pInputSamples1 + i), shift1); + + leftf = _mm_mul_ps(_mm_cvtepi32_ps(lefti), factor128); + rightf = _mm_mul_ps(_mm_cvtepi32_ps(righti), factor128); + + _mm_storeu_ps(pOutputSamples + i*8 + 0, _mm_unpacklo_ps(leftf, rightf)); + _mm_storeu_ps(pOutputSamples + i*8 + 4, _mm_unpackhi_ps(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0) * factor; + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1) * factor; + } +} +#endif + +#if defined(DRFLAC_SUPPORT_NEON) +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__neon(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ + drflac_uint64 i; + drflac_uint64 frameCount4 = frameCount >> 2; + const drflac_uint32* pInputSamples0U32 = (const drflac_uint32*)pInputSamples0; + const drflac_uint32* pInputSamples1U32 = (const drflac_uint32*)pInputSamples1; + drflac_uint32 shift0 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[0].wastedBitsPerSample) - 8; + drflac_uint32 shift1 = (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[1].wastedBitsPerSample) - 8; + + float factor = 1.0f / 8388608.0f; + float32x4_t factor4 = vdupq_n_f32(factor); + int32x4_t shift0_4 = vdupq_n_s32(shift0); + int32x4_t shift1_4 = vdupq_n_s32(shift1); + + for (i = 0; i < frameCount4; ++i) { + int32x4_t lefti; + int32x4_t righti; + float32x4_t leftf; + float32x4_t rightf; + + lefti = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32 + i*4), shift0_4)); + righti = vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32 + i*4), shift1_4)); + + leftf = vmulq_f32(vcvtq_f32_s32(lefti), factor4); + rightf = vmulq_f32(vcvtq_f32_s32(righti), factor4); + + drflac__vst2q_f32(pOutputSamples + i*8, vzipq_f32(leftf, rightf)); + } + + for (i = (frameCount4 << 2); i < frameCount; ++i) { + pOutputSamples[i*2+0] = (drflac_int32)(pInputSamples0U32[i] << shift0) * factor; + pOutputSamples[i*2+1] = (drflac_int32)(pInputSamples1U32[i] << shift1) * factor; + } +} +#endif + +static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo(drflac* pFlac, drflac_uint64 frameCount, drflac_uint32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples) +{ +#if defined(DRFLAC_SUPPORT_SSE2) + if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#elif defined(DRFLAC_SUPPORT_NEON) + if (drflac__gIsNEONSupported && pFlac->bitsPerSample <= 24) { + drflac_read_pcm_frames_f32__decode_independent_stereo__neon(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); + } else +#endif + { + /* Scalar fallback. */ +#if 0 + drflac_read_pcm_frames_f32__decode_independent_stereo__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#else + drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples); +#endif + } +} + +DRFLAC_API drflac_uint64 drflac_read_pcm_frames_f32(drflac* pFlac, drflac_uint64 framesToRead, float* pBufferOut) +{ + drflac_uint64 framesRead; + drflac_uint32 unusedBitsPerSample; + + if (pFlac == NULL || framesToRead == 0) { + return 0; + } + + if (pBufferOut == NULL) { + return drflac__seek_forward_by_pcm_frames(pFlac, framesToRead); + } + + DRFLAC_ASSERT(pFlac->bitsPerSample <= 32); + unusedBitsPerSample = 32 - pFlac->bitsPerSample; + + framesRead = 0; + while (framesToRead > 0) { + /* If we've run out of samples in this frame, go to the next. */ + if (pFlac->currentFLACFrame.pcmFramesRemaining == 0) { + if (!drflac__read_and_decode_next_flac_frame(pFlac)) { + break; /* Couldn't read the next frame, so just break from the loop and return. */ + } + } else { + unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFLACFrame.header.channelAssignment); + drflac_uint64 iFirstPCMFrame = pFlac->currentFLACFrame.header.blockSizeInPCMFrames - pFlac->currentFLACFrame.pcmFramesRemaining; + drflac_uint64 frameCountThisIteration = framesToRead; + + if (frameCountThisIteration > pFlac->currentFLACFrame.pcmFramesRemaining) { + frameCountThisIteration = pFlac->currentFLACFrame.pcmFramesRemaining; + } + + if (channelCount == 2) { + const drflac_int32* pDecodedSamples0 = pFlac->currentFLACFrame.subframes[0].pSamplesS32 + iFirstPCMFrame; + const drflac_int32* pDecodedSamples1 = pFlac->currentFLACFrame.subframes[1].pSamplesS32 + iFirstPCMFrame; + + switch (pFlac->currentFLACFrame.header.channelAssignment) + { + case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE: + { + drflac_read_pcm_frames_f32__decode_left_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE: + { + drflac_read_pcm_frames_f32__decode_right_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE: + { + drflac_read_pcm_frames_f32__decode_mid_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + + case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT: + default: + { + drflac_read_pcm_frames_f32__decode_independent_stereo(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut); + } break; + } + } else { + /* Generic interleaving. */ + drflac_uint64 i; + for (i = 0; i < frameCountThisIteration; ++i) { + unsigned int j; + for (j = 0; j < channelCount; ++j) { + drflac_int32 sampleS32 = (drflac_int32)((drflac_uint32)(pFlac->currentFLACFrame.subframes[j].pSamplesS32[iFirstPCMFrame + i]) << (unusedBitsPerSample + pFlac->currentFLACFrame.subframes[j].wastedBitsPerSample)); + pBufferOut[(i*channelCount)+j] = (float)(sampleS32 / 2147483648.0); + } + } + } + + framesRead += frameCountThisIteration; + pBufferOut += frameCountThisIteration * channelCount; + framesToRead -= frameCountThisIteration; + pFlac->currentPCMFrame += frameCountThisIteration; + pFlac->currentFLACFrame.pcmFramesRemaining -= (unsigned int)frameCountThisIteration; + } + } + + return framesRead; +} + + +DRFLAC_API drflac_bool32 drflac_seek_to_pcm_frame(drflac* pFlac, drflac_uint64 pcmFrameIndex) +{ + if (pFlac == NULL) { + return DRFLAC_FALSE; + } + + /* Don't do anything if we're already on the seek point. */ + if (pFlac->currentPCMFrame == pcmFrameIndex) { + return DRFLAC_TRUE; + } + + /* + If we don't know where the first frame begins then we can't seek. This will happen when the STREAMINFO block was not present + when the decoder was opened. + */ + if (pFlac->firstFLACFramePosInBytes == 0) { + return DRFLAC_FALSE; + } + + if (pcmFrameIndex == 0) { + pFlac->currentPCMFrame = 0; + return drflac__seek_to_first_frame(pFlac); + } else { + drflac_bool32 wasSuccessful = DRFLAC_FALSE; + drflac_uint64 originalPCMFrame = pFlac->currentPCMFrame; + + /* Clamp the sample to the end. */ + if (pcmFrameIndex > pFlac->totalPCMFrameCount) { + pcmFrameIndex = pFlac->totalPCMFrameCount; + } + + /* If the target sample and the current sample are in the same frame we just move the position forward. */ + if (drflac__is_current_flac_frame_valid(pFlac)) { + if (pcmFrameIndex > pFlac->currentPCMFrame) { + /* Forward. */ + drflac_uint32 offset = (drflac_uint32)(pcmFrameIndex - pFlac->currentPCMFrame); + if (pFlac->currentFLACFrame.pcmFramesRemaining > offset) { + pFlac->currentFLACFrame.pcmFramesRemaining -= offset; + pFlac->currentPCMFrame = pcmFrameIndex; + return DRFLAC_TRUE; + } + } else { + /* Backward. */ + drflac_uint32 offsetAbs = (drflac_uint32)(pFlac->currentPCMFrame - pcmFrameIndex); + drflac_uint32 currentFLACFramePCMFrameCount = pFlac->currentFLACFrame.header.blockSizeInPCMFrames; + drflac_uint32 currentFLACFramePCMFramesConsumed = currentFLACFramePCMFrameCount - pFlac->currentFLACFrame.pcmFramesRemaining; + if (currentFLACFramePCMFramesConsumed > offsetAbs) { + pFlac->currentFLACFrame.pcmFramesRemaining += offsetAbs; + pFlac->currentPCMFrame = pcmFrameIndex; + return DRFLAC_TRUE; + } + } + } + + /* + Different techniques depending on encapsulation. Using the native FLAC seektable with Ogg encapsulation is a bit awkward so + we'll instead use Ogg's natural seeking facility. + */ +#ifndef DR_FLAC_NO_OGG + if (pFlac->container == drflac_container_ogg) + { + wasSuccessful = drflac_ogg__seek_to_pcm_frame(pFlac, pcmFrameIndex); + } + else +#endif + { + /* First try seeking via the seek table. If this fails, fall back to a brute force seek which is much slower. */ + if (/*!wasSuccessful && */!pFlac->_noSeekTableSeek) { + wasSuccessful = drflac__seek_to_pcm_frame__seek_table(pFlac, pcmFrameIndex); + } + +#if !defined(DR_FLAC_NO_CRC) + /* Fall back to binary search if seek table seeking fails. This requires the length of the stream to be known. */ + if (!wasSuccessful && !pFlac->_noBinarySearchSeek && pFlac->totalPCMFrameCount > 0) { + wasSuccessful = drflac__seek_to_pcm_frame__binary_search(pFlac, pcmFrameIndex); + } +#endif + + /* Fall back to brute force if all else fails. */ + if (!wasSuccessful && !pFlac->_noBruteForceSeek) { + wasSuccessful = drflac__seek_to_pcm_frame__brute_force(pFlac, pcmFrameIndex); + } + } + + if (wasSuccessful) { + pFlac->currentPCMFrame = pcmFrameIndex; + } else { + /* Seek failed. Try putting the decoder back to it's original state. */ + if (drflac_seek_to_pcm_frame(pFlac, originalPCMFrame) == DRFLAC_FALSE) { + /* Failed to seek back to the original PCM frame. Fall back to 0. */ + drflac_seek_to_pcm_frame(pFlac, 0); + } + } + + return wasSuccessful; + } +} + + + +/* High Level APIs */ + +/* SIZE_MAX */ +#if defined(SIZE_MAX) + #define DRFLAC_SIZE_MAX SIZE_MAX +#else + #if defined(DRFLAC_64BIT) + #define DRFLAC_SIZE_MAX ((drflac_uint64)0xFFFFFFFFFFFFFFFF) + #else + #define DRFLAC_SIZE_MAX 0xFFFFFFFF + #endif +#endif +/* End SIZE_MAX */ + + +/* Using a macro as the definition of the drflac__full_decode_and_close_*() API family. Sue me. */ +#define DRFLAC_DEFINE_FULL_READ_AND_CLOSE(extension, type) \ +static type* drflac__full_read_and_close_ ## extension (drflac* pFlac, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)\ +{ \ + type* pSampleData = NULL; \ + drflac_uint64 totalPCMFrameCount; \ + type buffer[4096]; \ + drflac_uint64 pcmFramesRead; \ + size_t sampleDataBufferSize = sizeof(buffer); \ + \ + DRFLAC_ASSERT(pFlac != NULL); \ + \ + totalPCMFrameCount = 0; \ + \ + pSampleData = (type*)drflac__malloc_from_callbacks(sampleDataBufferSize, &pFlac->allocationCallbacks); \ + if (pSampleData == NULL) { \ + goto on_error; \ + } \ + \ + while ((pcmFramesRead = (drflac_uint64)drflac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \ + if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \ + type* pNewSampleData; \ + size_t newSampleDataBufferSize; \ + \ + newSampleDataBufferSize = sampleDataBufferSize * 2; \ + pNewSampleData = (type*)drflac__realloc_from_callbacks(pSampleData, newSampleDataBufferSize, sampleDataBufferSize, &pFlac->allocationCallbacks); \ + if (pNewSampleData == NULL) { \ + drflac__free_from_callbacks(pSampleData, &pFlac->allocationCallbacks); \ + goto on_error; \ + } \ + \ + sampleDataBufferSize = newSampleDataBufferSize; \ + pSampleData = pNewSampleData; \ + } \ + \ + DRFLAC_COPY_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \ + totalPCMFrameCount += pcmFramesRead; \ + } \ + \ + /* At this point everything should be decoded, but we just want to fill the unused part buffer with silence - need to \ + protect those ears from random noise! */ \ + DRFLAC_ZERO_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \ + \ + if (sampleRateOut) *sampleRateOut = pFlac->sampleRate; \ + if (channelsOut) *channelsOut = pFlac->channels; \ + if (totalPCMFrameCountOut) *totalPCMFrameCountOut = totalPCMFrameCount; \ + \ + drflac_close(pFlac); \ + return pSampleData; \ + \ +on_error: \ + drflac_close(pFlac); \ + return NULL; \ +} + +DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s32, drflac_int32) +DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s16, drflac_int16) +DRFLAC_DEFINE_FULL_READ_AND_CLOSE(f32, float) + +DRFLAC_API drflac_int32* drflac_open_and_read_pcm_frames_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (channelsOut) { + *channelsOut = 0; + } + if (sampleRateOut) { + *sampleRateOut = 0; + } + if (totalPCMFrameCountOut) { + *totalPCMFrameCountOut = 0; + } + + pFlac = drflac_open(onRead, onSeek, onTell, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut); +} + +DRFLAC_API drflac_int16* drflac_open_and_read_pcm_frames_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (channelsOut) { + *channelsOut = 0; + } + if (sampleRateOut) { + *sampleRateOut = 0; + } + if (totalPCMFrameCountOut) { + *totalPCMFrameCountOut = 0; + } + + pFlac = drflac_open(onRead, onSeek, onTell, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s16(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut); +} + +DRFLAC_API float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (channelsOut) { + *channelsOut = 0; + } + if (sampleRateOut) { + *sampleRateOut = 0; + } + if (totalPCMFrameCountOut) { + *totalPCMFrameCountOut = 0; + } + + pFlac = drflac_open(onRead, onSeek, onTell, pUserData, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_f32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut); +} + +#ifndef DR_FLAC_NO_STDIO +DRFLAC_API drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_file(filename, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s32(pFlac, channels, sampleRate, totalPCMFrameCount); +} + +DRFLAC_API drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_file(filename, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s16(pFlac, channels, sampleRate, totalPCMFrameCount); +} + +DRFLAC_API float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_file(filename, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_f32(pFlac, channels, sampleRate, totalPCMFrameCount); +} +#endif + +DRFLAC_API drflac_int32* drflac_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_memory(data, dataSize, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s32(pFlac, channels, sampleRate, totalPCMFrameCount); +} + +DRFLAC_API drflac_int16* drflac_open_memory_and_read_pcm_frames_s16(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_memory(data, dataSize, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_s16(pFlac, channels, sampleRate, totalPCMFrameCount); +} + +DRFLAC_API float* drflac_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + drflac* pFlac; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalPCMFrameCount) { + *totalPCMFrameCount = 0; + } + + pFlac = drflac_open_memory(data, dataSize, pAllocationCallbacks); + if (pFlac == NULL) { + return NULL; + } + + return drflac__full_read_and_close_f32(pFlac, channels, sampleRate, totalPCMFrameCount); +} + + +DRFLAC_API void drflac_free(void* p, const drflac_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks != NULL) { + drflac__free_from_callbacks(p, pAllocationCallbacks); + } else { + drflac__free_default(p, NULL); + } +} + + + + +DRFLAC_API void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator* pIter, drflac_uint32 commentCount, const void* pComments) +{ + if (pIter == NULL) { + return; + } + + pIter->countRemaining = commentCount; + pIter->pRunningData = (const char*)pComments; +} + +DRFLAC_API const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator* pIter, drflac_uint32* pCommentLengthOut) +{ + drflac_int32 length; + const char* pComment; + + /* Safety. */ + if (pCommentLengthOut) { + *pCommentLengthOut = 0; + } + + if (pIter == NULL || pIter->countRemaining == 0 || pIter->pRunningData == NULL) { + return NULL; + } + + length = drflac__le2host_32_ptr_unaligned(pIter->pRunningData); + pIter->pRunningData += 4; + + pComment = pIter->pRunningData; + pIter->pRunningData += length; + pIter->countRemaining -= 1; + + if (pCommentLengthOut) { + *pCommentLengthOut = length; + } + + return pComment; +} + + + + +DRFLAC_API void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator* pIter, drflac_uint32 trackCount, const void* pTrackData) +{ + if (pIter == NULL) { + return; + } + + pIter->countRemaining = trackCount; + pIter->pRunningData = (const char*)pTrackData; +} + +DRFLAC_API drflac_bool32 drflac_next_cuesheet_track(drflac_cuesheet_track_iterator* pIter, drflac_cuesheet_track* pCuesheetTrack) +{ + drflac_cuesheet_track cuesheetTrack; + const char* pRunningData; + drflac_uint64 offsetHi; + drflac_uint64 offsetLo; + + if (pIter == NULL || pIter->countRemaining == 0 || pIter->pRunningData == NULL) { + return DRFLAC_FALSE; + } + + pRunningData = pIter->pRunningData; + + offsetHi = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4; + offsetLo = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4; + cuesheetTrack.offset = offsetLo | (offsetHi << 32); + cuesheetTrack.trackNumber = pRunningData[0]; pRunningData += 1; + DRFLAC_COPY_MEMORY(cuesheetTrack.ISRC, pRunningData, sizeof(cuesheetTrack.ISRC)); pRunningData += 12; + cuesheetTrack.isAudio = (pRunningData[0] & 0x80) != 0; + cuesheetTrack.preEmphasis = (pRunningData[0] & 0x40) != 0; pRunningData += 14; + cuesheetTrack.indexCount = pRunningData[0]; pRunningData += 1; + cuesheetTrack.pIndexPoints = (const drflac_cuesheet_track_index*)pRunningData; pRunningData += cuesheetTrack.indexCount * sizeof(drflac_cuesheet_track_index); + + pIter->pRunningData = pRunningData; + pIter->countRemaining -= 1; + + if (pCuesheetTrack) { + *pCuesheetTrack = cuesheetTrack; + } + + return DRFLAC_TRUE; +} + +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic pop +#endif +#endif /* dr_flac_c */ +#endif /* DR_FLAC_IMPLEMENTATION */ + + +/* +REVISION HISTORY +================ +v0.13.4 - TBD + - Add a bounds check when allocating memory during metadata processing. + - Fix a possible overflow error when parsing picture metadata. + - Fix an error with seek point parsing. + - Fix a possible deadlock when seeking. + - Fix an error where the decoder can be put into a bad state when seeking fails which then results in a crash when reading and seeking. + +v0.13.3 - 2026-01-17 + - Fix a compiler compatibility issue with some inlined assembly. + - Fix a compilation warning. + +v0.13.2 - 2025-12-02 + - Improve robustness of the parsing of picture metadata to improve support for memory constrained embedded devices. + - Fix a warning about an assigned by unused variable. + - Improvements to drflac_open_and_read_pcm_frames_*() and family to avoid excessively large memory allocations from malformed files. + +v0.13.1 - 2025-09-10 + - Fix an error with the NXDK build. + +v0.13.0 - 2025-07-23 + - API CHANGE: Seek origin enums have been renamed to match the naming convention used by other dr_libs libraries: + - drflac_seek_origin_start -> DRFLAC_SEEK_SET + - drflac_seek_origin_current -> DRFLAC_SEEK_CUR + - DRFLAC_SEEK_END (new) + - API CHANGE: A new seek origin has been added to allow seeking from the end of the file. If you implement your own `onSeek` callback, you should now detect and handle `DRFLAC_SEEK_END`. If seeking to the end is not supported, return `DRFLAC_FALSE`. If you only use `*_open_file()` or `*_open_memory()`, you need not change anything. + - API CHANGE: An `onTell` callback has been added to the following functions: + - drflac_open() + - drflac_open_relaxed() + - drflac_open_with_metadata() + - drflac_open_with_metadata_relaxed() + - drflac_open_and_read_pcm_frames_s32() + - drflac_open_and_read_pcm_frames_s16() + - drflac_open_and_read_pcm_frames_f32() + - Fix compilation for AIX OS. + +v0.12.43 - 2024-12-17 + - Fix a possible buffer overflow during decoding. + - Improve detection of ARM64EC + +v0.12.42 - 2023-11-02 + - Fix build for ARMv6-M. + - Fix a compilation warning with GCC. + +v0.12.41 - 2023-06-17 + - Fix an incorrect date in revision history. No functional change. + +v0.12.40 - 2023-05-22 + - Minor code restructure. No functional change. + +v0.12.39 - 2022-09-17 + - Fix compilation with DJGPP. + - Fix compilation error with Visual Studio 2019 and the ARM build. + - Fix an error with SSE 4.1 detection. + - Add support for disabling wchar_t with DR_WAV_NO_WCHAR. + - Improve compatibility with compilers which lack support for explicit struct packing. + - Improve compatibility with low-end and embedded hardware by reducing the amount of stack + allocation when loading an Ogg encapsulated file. + +v0.12.38 - 2022-04-10 + - Fix compilation error on older versions of GCC. + +v0.12.37 - 2022-02-12 + - Improve ARM detection. + +v0.12.36 - 2022-02-07 + - Fix a compilation error with the ARM build. + +v0.12.35 - 2022-02-06 + - Fix a bug due to underestimating the amount of precision required for the prediction stage. + - Fix some bugs found from fuzz testing. + +v0.12.34 - 2022-01-07 + - Fix some misalignment bugs when reading metadata. + +v0.12.33 - 2021-12-22 + - Fix a bug with seeking when the seek table does not start at PCM frame 0. + +v0.12.32 - 2021-12-11 + - Fix a warning with Clang. + +v0.12.31 - 2021-08-16 + - Silence some warnings. + +v0.12.30 - 2021-07-31 + - Fix platform detection for ARM64. + +v0.12.29 - 2021-04-02 + - Fix a bug where the running PCM frame index is set to an invalid value when over-seeking. + - Fix a decoding error due to an incorrect validation check. + +v0.12.28 - 2021-02-21 + - Fix a warning due to referencing _MSC_VER when it is undefined. + +v0.12.27 - 2021-01-31 + - Fix a static analysis warning. + +v0.12.26 - 2021-01-17 + - Fix a compilation warning due to _BSD_SOURCE being deprecated. + +v0.12.25 - 2020-12-26 + - Update documentation. + +v0.12.24 - 2020-11-29 + - Fix ARM64/NEON detection when compiling with MSVC. + +v0.12.23 - 2020-11-21 + - Fix compilation with OpenWatcom. + +v0.12.22 - 2020-11-01 + - Fix an error with the previous release. + +v0.12.21 - 2020-11-01 + - Fix a possible deadlock when seeking. + - Improve compiler support for older versions of GCC. + +v0.12.20 - 2020-09-08 + - Fix a compilation error on older compilers. + +v0.12.19 - 2020-08-30 + - Fix a bug due to an undefined 32-bit shift. + +v0.12.18 - 2020-08-14 + - Fix a crash when compiling with clang-cl. + +v0.12.17 - 2020-08-02 + - Simplify sized types. + +v0.12.16 - 2020-07-25 + - Fix a compilation warning. + +v0.12.15 - 2020-07-06 + - Check for negative LPC shifts and return an error. + +v0.12.14 - 2020-06-23 + - Add include guard for the implementation section. + +v0.12.13 - 2020-05-16 + - Add compile-time and run-time version querying. + - DRFLAC_VERSION_MINOR + - DRFLAC_VERSION_MAJOR + - DRFLAC_VERSION_REVISION + - DRFLAC_VERSION_STRING + - drflac_version() + - drflac_version_string() + +v0.12.12 - 2020-04-30 + - Fix compilation errors with VC6. + +v0.12.11 - 2020-04-19 + - Fix some pedantic warnings. + - Fix some undefined behaviour warnings. + +v0.12.10 - 2020-04-10 + - Fix some bugs when trying to seek with an invalid seek table. + +v0.12.9 - 2020-04-05 + - Fix warnings. + +v0.12.8 - 2020-04-04 + - Add drflac_open_file_w() and drflac_open_file_with_metadata_w(). + - Fix some static analysis warnings. + - Minor documentation updates. + +v0.12.7 - 2020-03-14 + - Fix compilation errors with VC6. + +v0.12.6 - 2020-03-07 + - Fix compilation error with Visual Studio .NET 2003. + +v0.12.5 - 2020-01-30 + - Silence some static analysis warnings. + +v0.12.4 - 2020-01-29 + - Silence some static analysis warnings. + +v0.12.3 - 2019-12-02 + - Fix some warnings when compiling with GCC and the -Og flag. + - Fix a crash in out-of-memory situations. + - Fix potential integer overflow bug. + - Fix some static analysis warnings. + - Fix a possible crash when using custom memory allocators without a custom realloc() implementation. + - Fix a bug with binary search seeking where the bits per sample is not a multiple of 8. + +v0.12.2 - 2019-10-07 + - Internal code clean up. + +v0.12.1 - 2019-09-29 + - Fix some Clang Static Analyzer warnings. + - Fix an unused variable warning. + +v0.12.0 - 2019-09-23 + - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation + routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs: + - drflac_open() + - drflac_open_relaxed() + - drflac_open_with_metadata() + - drflac_open_with_metadata_relaxed() + - drflac_open_file() + - drflac_open_file_with_metadata() + - drflac_open_memory() + - drflac_open_memory_with_metadata() + - drflac_open_and_read_pcm_frames_s32() + - drflac_open_and_read_pcm_frames_s16() + - drflac_open_and_read_pcm_frames_f32() + - drflac_open_file_and_read_pcm_frames_s32() + - drflac_open_file_and_read_pcm_frames_s16() + - drflac_open_file_and_read_pcm_frames_f32() + - drflac_open_memory_and_read_pcm_frames_s32() + - drflac_open_memory_and_read_pcm_frames_s16() + - drflac_open_memory_and_read_pcm_frames_f32() + Set this extra parameter to NULL to use defaults which is the same as the previous behaviour. Setting this NULL will use + DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE. + - Remove deprecated APIs: + - drflac_read_s32() + - drflac_read_s16() + - drflac_read_f32() + - drflac_seek_to_sample() + - drflac_open_and_decode_s32() + - drflac_open_and_decode_s16() + - drflac_open_and_decode_f32() + - drflac_open_and_decode_file_s32() + - drflac_open_and_decode_file_s16() + - drflac_open_and_decode_file_f32() + - drflac_open_and_decode_memory_s32() + - drflac_open_and_decode_memory_s16() + - drflac_open_and_decode_memory_f32() + - Remove drflac.totalSampleCount which is now replaced with drflac.totalPCMFrameCount. You can emulate drflac.totalSampleCount + by doing pFlac->totalPCMFrameCount*pFlac->channels. + - Rename drflac.currentFrame to drflac.currentFLACFrame to remove ambiguity with PCM frames. + - Fix errors when seeking to the end of a stream. + - Optimizations to seeking. + - SSE improvements and optimizations. + - ARM NEON optimizations. + - Optimizations to drflac_read_pcm_frames_s16(). + - Optimizations to drflac_read_pcm_frames_s32(). + +v0.11.10 - 2019-06-26 + - Fix a compiler error. + +v0.11.9 - 2019-06-16 + - Silence some ThreadSanitizer warnings. + +v0.11.8 - 2019-05-21 + - Fix warnings. + +v0.11.7 - 2019-05-06 + - C89 fixes. + +v0.11.6 - 2019-05-05 + - Add support for C89. + - Fix a compiler warning when CRC is disabled. + - Change license to choice of public domain or MIT-0. + +v0.11.5 - 2019-04-19 + - Fix a compiler error with GCC. + +v0.11.4 - 2019-04-17 + - Fix some warnings with GCC when compiling with -std=c99. + +v0.11.3 - 2019-04-07 + - Silence warnings with GCC. + +v0.11.2 - 2019-03-10 + - Fix a warning. + +v0.11.1 - 2019-02-17 + - Fix a potential bug with seeking. + +v0.11.0 - 2018-12-16 + - API CHANGE: Deprecated drflac_read_s32(), drflac_read_s16() and drflac_read_f32() and replaced them with + drflac_read_pcm_frames_s32(), drflac_read_pcm_frames_s16() and drflac_read_pcm_frames_f32(). The new APIs take + and return PCM frame counts instead of sample counts. To upgrade you will need to change the input count by + dividing it by the channel count, and then do the same with the return value. + - API_CHANGE: Deprecated drflac_seek_to_sample() and replaced with drflac_seek_to_pcm_frame(). Same rules as + the changes to drflac_read_*() apply. + - API CHANGE: Deprecated drflac_open_and_decode_*() and replaced with drflac_open_*_and_read_*(). Same rules as + the changes to drflac_read_*() apply. + - Optimizations. + +v0.10.0 - 2018-09-11 + - Remove the DR_FLAC_NO_WIN32_IO option and the Win32 file IO functionality. If you need to use Win32 file IO you + need to do it yourself via the callback API. + - Fix the clang build. + - Fix undefined behavior. + - Fix errors with CUESHEET metdata blocks. + - Add an API for iterating over each cuesheet track in the CUESHEET metadata block. This works the same way as the + Vorbis comment API. + - Other miscellaneous bug fixes, mostly relating to invalid FLAC streams. + - Minor optimizations. + +v0.9.11 - 2018-08-29 + - Fix a bug with sample reconstruction. + +v0.9.10 - 2018-08-07 + - Improve 64-bit detection. + +v0.9.9 - 2018-08-05 + - Fix C++ build on older versions of GCC. + +v0.9.8 - 2018-07-24 + - Fix compilation errors. + +v0.9.7 - 2018-07-05 + - Fix a warning. + +v0.9.6 - 2018-06-29 + - Fix some typos. + +v0.9.5 - 2018-06-23 + - Fix some warnings. + +v0.9.4 - 2018-06-14 + - Optimizations to seeking. + - Clean up. + +v0.9.3 - 2018-05-22 + - Bug fix. + +v0.9.2 - 2018-05-12 + - Fix a compilation error due to a missing break statement. + +v0.9.1 - 2018-04-29 + - Fix compilation error with Clang. + +v0.9 - 2018-04-24 + - Fix Clang build. + - Start using major.minor.revision versioning. + +v0.8g - 2018-04-19 + - Fix build on non-x86/x64 architectures. + +v0.8f - 2018-02-02 + - Stop pretending to support changing rate/channels mid stream. + +v0.8e - 2018-02-01 + - Fix a crash when the block size of a frame is larger than the maximum block size defined by the FLAC stream. + - Fix a crash the the Rice partition order is invalid. + +v0.8d - 2017-09-22 + - Add support for decoding streams with ID3 tags. ID3 tags are just skipped. + +v0.8c - 2017-09-07 + - Fix warning on non-x86/x64 architectures. + +v0.8b - 2017-08-19 + - Fix build on non-x86/x64 architectures. + +v0.8a - 2017-08-13 + - A small optimization for the Clang build. + +v0.8 - 2017-08-12 + - API CHANGE: Rename dr_* types to drflac_*. + - Optimizations. This brings dr_flac back to about the same class of efficiency as the reference implementation. + - Add support for custom implementations of malloc(), realloc(), etc. + - Add CRC checking to Ogg encapsulated streams. + - Fix VC++ 6 build. This is only for the C++ compiler. The C compiler is not currently supported. + - Bug fixes. + +v0.7 - 2017-07-23 + - Add support for opening a stream without a header block. To do this, use drflac_open_relaxed() / drflac_open_with_metadata_relaxed(). + +v0.6 - 2017-07-22 + - Add support for recovering from invalid frames. With this change, dr_flac will simply skip over invalid frames as if they + never existed. Frames are checked against their sync code, the CRC-8 of the frame header and the CRC-16 of the whole frame. + +v0.5 - 2017-07-16 + - Fix typos. + - Change drflac_bool* types to unsigned. + - Add CRC checking. This makes dr_flac slower, but can be disabled with #define DR_FLAC_NO_CRC. + +v0.4f - 2017-03-10 + - Fix a couple of bugs with the bitstreaming code. + +v0.4e - 2017-02-17 + - Fix some warnings. + +v0.4d - 2016-12-26 + - Add support for 32-bit floating-point PCM decoding. + - Use drflac_int* and drflac_uint* sized types to improve compiler support. + - Minor improvements to documentation. + +v0.4c - 2016-12-26 + - Add support for signed 16-bit integer PCM decoding. + +v0.4b - 2016-10-23 + - A minor change to drflac_bool8 and drflac_bool32 types. + +v0.4a - 2016-10-11 + - Rename drBool32 to drflac_bool32 for styling consistency. + +v0.4 - 2016-09-29 + - API/ABI CHANGE: Use fixed size 32-bit booleans instead of the built-in bool type. + - API CHANGE: Rename drflac_open_and_decode*() to drflac_open_and_decode*_s32(). + - API CHANGE: Swap the order of "channels" and "sampleRate" parameters in drflac_open_and_decode*(). Rationale for this is to + keep it consistent with drflac_audio. + +v0.3f - 2016-09-21 + - Fix a warning with GCC. + +v0.3e - 2016-09-18 + - Fixed a bug where GCC 4.3+ was not getting properly identified. + - Fixed a few typos. + - Changed date formats to ISO 8601 (YYYY-MM-DD). + +v0.3d - 2016-06-11 + - Minor clean up. + +v0.3c - 2016-05-28 + - Fixed compilation error. + +v0.3b - 2016-05-16 + - Fixed Linux/GCC build. + - Updated documentation. + +v0.3a - 2016-05-15 + - Minor fixes to documentation. + +v0.3 - 2016-05-11 + - Optimizations. Now at about parity with the reference implementation on 32-bit builds. + - Lots of clean up. + +v0.2b - 2016-05-10 + - Bug fixes. + +v0.2a - 2016-05-10 + - Made drflac_open_and_decode() more robust. + - Removed an unused debugging variable + +v0.2 - 2016-05-09 + - Added support for Ogg encapsulation. + - API CHANGE. Have the onSeek callback take a third argument which specifies whether or not the seek + should be relative to the start or the current position. Also changes the seeking rules such that + seeking offsets will never be negative. + - Have drflac_open_and_decode() fail gracefully if the stream has an unknown total sample count. + +v0.1b - 2016-05-07 + - Properly close the file handle in drflac_open_file() and family when the decoder fails to initialize. + - Removed a stale comment. + +v0.1a - 2016-05-05 + - Minor formatting changes. + - Fixed a warning on the GCC build. + +v0.1 - 2016-05-03 + - Initial versioned release. +*/ + +/* +This software is available as a choice of the following licenses. Choose +whichever you prefer. + +=============================================================================== +ALTERNATIVE 1 - Public Domain (www.unlicense.org) +=============================================================================== +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + +=============================================================================== +ALTERNATIVE 2 - MIT No Attribution +=============================================================================== +Copyright 2023 David Reid + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_mp3.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_mp3.h new file mode 100644 index 00000000..a631ae40 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/dr_mp3.h @@ -0,0 +1,5430 @@ +/* +MP3 audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file. +dr_mp3 - v0.7.4 - TBD + +David Reid - mackron@gmail.com + +GitHub: https://github.com/mackron/dr_libs + +Based on minimp3 (https://github.com/lieff/minimp3) which is where the real work was done. See the bottom of this file for differences between minimp3 and dr_mp3. +*/ + +/* +Introduction +============= +dr_mp3 is a single file library. To use it, do something like the following in one .c file. + + ```c + #define DR_MP3_IMPLEMENTATION + #include "dr_mp3.h" + ``` + +You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following: + + ```c + drmp3 mp3; + if (!drmp3_init_file(&mp3, "MySong.mp3", NULL)) { + // Failed to open file + } + + ... + + drmp3_uint64 framesRead = drmp3_read_pcm_frames_f32(pMP3, framesToRead, pFrames); + ``` + +The drmp3 object is transparent so you can get access to the channel count and sample rate like so: + + ``` + drmp3_uint32 channels = mp3.channels; + drmp3_uint32 sampleRate = mp3.sampleRate; + ``` + +The example above initializes a decoder from a file, but you can also initialize it from a block of memory and read and seek callbacks with +`drmp3_init_memory()` and `drmp3_init()` respectively. + +You do not need to do any annoying memory management when reading PCM frames - this is all managed internally. You can request any number of PCM frames in each +call to `drmp3_read_pcm_frames_f32()` and it will return as many PCM frames as it can, up to the requested amount. + +You can also decode an entire file in one go with `drmp3_open_and_read_pcm_frames_f32()`, `drmp3_open_memory_and_read_pcm_frames_f32()` and +`drmp3_open_file_and_read_pcm_frames_f32()`. + + +Build Options +============= +#define these options before including this file. + +#define DR_MP3_NO_STDIO + Disable drmp3_init_file(), etc. + +#define DR_MP3_NO_SIMD + Disable SIMD optimizations. +*/ + +#ifndef dr_mp3_h +#define dr_mp3_h + +#ifdef __cplusplus +extern "C" { +#endif + +#define DRMP3_STRINGIFY(x) #x +#define DRMP3_XSTRINGIFY(x) DRMP3_STRINGIFY(x) + +#define DRMP3_VERSION_MAJOR 0 +#define DRMP3_VERSION_MINOR 7 +#define DRMP3_VERSION_REVISION 4 +#define DRMP3_VERSION_STRING DRMP3_XSTRINGIFY(DRMP3_VERSION_MAJOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_MINOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_REVISION) + +#include /* For size_t. */ + +/* Sized Types */ +typedef signed char drmp3_int8; +typedef unsigned char drmp3_uint8; +typedef signed short drmp3_int16; +typedef unsigned short drmp3_uint16; +typedef signed int drmp3_int32; +typedef unsigned int drmp3_uint32; +#if defined(_MSC_VER) && !defined(__clang__) + typedef signed __int64 drmp3_int64; + typedef unsigned __int64 drmp3_uint64; +#else + #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wlong-long" + #if defined(__clang__) + #pragma GCC diagnostic ignored "-Wc++11-long-long" + #endif + #endif + typedef signed long long drmp3_int64; + typedef unsigned long long drmp3_uint64; + #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) + #pragma GCC diagnostic pop + #endif +#endif +#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(__powerpc64__) + typedef drmp3_uint64 drmp3_uintptr; +#else + typedef drmp3_uint32 drmp3_uintptr; +#endif +typedef drmp3_uint8 drmp3_bool8; +typedef drmp3_uint32 drmp3_bool32; +#define DRMP3_TRUE 1 +#define DRMP3_FALSE 0 + +/* Weird shifting syntax is for VC6 compatibility. */ +#define DRMP3_UINT64_MAX (((drmp3_uint64)0xFFFFFFFF << 32) | (drmp3_uint64)0xFFFFFFFF) +/* End Sized Types */ + +/* Decorations */ +#if !defined(DRMP3_API) + #if defined(DRMP3_DLL) + #if defined(_WIN32) + #define DRMP3_DLL_IMPORT __declspec(dllimport) + #define DRMP3_DLL_EXPORT __declspec(dllexport) + #define DRMP3_DLL_PRIVATE static + #else + #if defined(__GNUC__) && __GNUC__ >= 4 + #define DRMP3_DLL_IMPORT __attribute__((visibility("default"))) + #define DRMP3_DLL_EXPORT __attribute__((visibility("default"))) + #define DRMP3_DLL_PRIVATE __attribute__((visibility("hidden"))) + #else + #define DRMP3_DLL_IMPORT + #define DRMP3_DLL_EXPORT + #define DRMP3_DLL_PRIVATE static + #endif + #endif + + #if defined(DR_MP3_IMPLEMENTATION) + #define DRMP3_API DRMP3_DLL_EXPORT + #else + #define DRMP3_API DRMP3_DLL_IMPORT + #endif + #define DRMP3_PRIVATE DRMP3_DLL_PRIVATE + #else + #define DRMP3_API extern + #define DRMP3_PRIVATE static + #endif +#endif +/* End Decorations */ + +/* Result Codes */ +typedef drmp3_int32 drmp3_result; +#define DRMP3_SUCCESS 0 +#define DRMP3_ERROR -1 /* A generic error. */ +#define DRMP3_INVALID_ARGS -2 +#define DRMP3_INVALID_OPERATION -3 +#define DRMP3_OUT_OF_MEMORY -4 +#define DRMP3_OUT_OF_RANGE -5 +#define DRMP3_ACCESS_DENIED -6 +#define DRMP3_DOES_NOT_EXIST -7 +#define DRMP3_ALREADY_EXISTS -8 +#define DRMP3_TOO_MANY_OPEN_FILES -9 +#define DRMP3_INVALID_FILE -10 +#define DRMP3_TOO_BIG -11 +#define DRMP3_PATH_TOO_LONG -12 +#define DRMP3_NAME_TOO_LONG -13 +#define DRMP3_NOT_DIRECTORY -14 +#define DRMP3_IS_DIRECTORY -15 +#define DRMP3_DIRECTORY_NOT_EMPTY -16 +#define DRMP3_END_OF_FILE -17 +#define DRMP3_NO_SPACE -18 +#define DRMP3_BUSY -19 +#define DRMP3_IO_ERROR -20 +#define DRMP3_INTERRUPT -21 +#define DRMP3_UNAVAILABLE -22 +#define DRMP3_ALREADY_IN_USE -23 +#define DRMP3_BAD_ADDRESS -24 +#define DRMP3_BAD_SEEK -25 +#define DRMP3_BAD_PIPE -26 +#define DRMP3_DEADLOCK -27 +#define DRMP3_TOO_MANY_LINKS -28 +#define DRMP3_NOT_IMPLEMENTED -29 +#define DRMP3_NO_MESSAGE -30 +#define DRMP3_BAD_MESSAGE -31 +#define DRMP3_NO_DATA_AVAILABLE -32 +#define DRMP3_INVALID_DATA -33 +#define DRMP3_TIMEOUT -34 +#define DRMP3_NO_NETWORK -35 +#define DRMP3_NOT_UNIQUE -36 +#define DRMP3_NOT_SOCKET -37 +#define DRMP3_NO_ADDRESS -38 +#define DRMP3_BAD_PROTOCOL -39 +#define DRMP3_PROTOCOL_UNAVAILABLE -40 +#define DRMP3_PROTOCOL_NOT_SUPPORTED -41 +#define DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED -42 +#define DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED -43 +#define DRMP3_SOCKET_NOT_SUPPORTED -44 +#define DRMP3_CONNECTION_RESET -45 +#define DRMP3_ALREADY_CONNECTED -46 +#define DRMP3_NOT_CONNECTED -47 +#define DRMP3_CONNECTION_REFUSED -48 +#define DRMP3_NO_HOST -49 +#define DRMP3_IN_PROGRESS -50 +#define DRMP3_CANCELLED -51 +#define DRMP3_MEMORY_ALREADY_MAPPED -52 +#define DRMP3_AT_END -53 +/* End Result Codes */ + +#define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152 +#define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2) + +/* Inline */ +#ifdef _MSC_VER + #define DRMP3_INLINE __forceinline +#elif defined(__GNUC__) + /* + I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when + the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some + case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the + command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue + I am using "__inline__" only when we're compiling in strict ANSI mode. + */ + #if defined(__STRICT_ANSI__) + #define DRMP3_GNUC_INLINE_HINT __inline__ + #else + #define DRMP3_GNUC_INLINE_HINT inline + #endif + + #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)) || defined(__clang__) + #define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT __attribute__((always_inline)) + #else + #define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT + #endif +#elif defined(__WATCOMC__) + #define DRMP3_INLINE __inline +#else + #define DRMP3_INLINE +#endif +/* End Inline */ + + +DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision); +DRMP3_API const char* drmp3_version_string(void); + + +/* Allocation Callbacks */ +typedef struct +{ + void* pUserData; + void* (* onMalloc)(size_t sz, void* pUserData); + void* (* onRealloc)(void* p, size_t sz, void* pUserData); + void (* onFree)(void* p, void* pUserData); +} drmp3_allocation_callbacks; +/* End Allocation Callbacks */ + + +/* +Low Level Push API +================== +*/ +#define DRMP3_MAX_BITRESERVOIR_BYTES 511 +#define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */ +#define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */ + +typedef struct +{ + int frame_bytes, channels, sample_rate, layer, bitrate_kbps; +} drmp3dec_frame_info; + +typedef struct +{ + const drmp3_uint8 *buf; + int pos, limit; +} drmp3_bs; + +typedef struct +{ + const drmp3_uint8 *sfbtab; + drmp3_uint16 part_23_length, big_values, scalefac_compress; + drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb; + drmp3_uint8 table_select[3], region_count[3], subblock_gain[3]; + drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi; +} drmp3_L3_gr_info; + +typedef struct +{ + drmp3_bs bs; + drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES]; + drmp3_L3_gr_info gr_info[4]; + float grbuf[2][576], scf[40], syn[18 + 15][2*32]; + drmp3_uint8 ist_pos[2][39]; +} drmp3dec_scratch; + +typedef struct +{ + float mdct_overlap[2][9*32], qmf_state[15*2*32]; + int reserv, free_format_bytes; + drmp3_uint8 header[4], reserv_buf[511]; + drmp3dec_scratch scratch; +} drmp3dec; + +/* Initializes a low level decoder. */ +DRMP3_API void drmp3dec_init(drmp3dec *dec); + +/* Reads a frame from a low level decoder. */ +DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info); + +/* Helper for converting between f32 and s16. */ +DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples); + + + +/* +Main API (Pull API) +=================== +*/ +typedef enum +{ + DRMP3_SEEK_SET, + DRMP3_SEEK_CUR, + DRMP3_SEEK_END +} drmp3_seek_origin; + +typedef struct +{ + drmp3_uint64 seekPosInBytes; /* Points to the first byte of an MP3 frame. */ + drmp3_uint64 pcmFrameIndex; /* The index of the PCM frame this seek point targets. */ + drmp3_uint16 mp3FramesToDiscard; /* The number of whole MP3 frames to be discarded before pcmFramesToDiscard. */ + drmp3_uint16 pcmFramesToDiscard; /* The number of leading samples to read and discard. These are discarded after mp3FramesToDiscard. */ +} drmp3_seek_point; + +typedef enum +{ + DRMP3_METADATA_TYPE_ID3V1, + DRMP3_METADATA_TYPE_ID3V2, + DRMP3_METADATA_TYPE_APE, + DRMP3_METADATA_TYPE_XING, + DRMP3_METADATA_TYPE_VBRI +} drmp3_metadata_type; + +typedef struct +{ + drmp3_metadata_type type; + const void* pRawData; /* A pointer to the raw data. */ + size_t rawDataSize; +} drmp3_metadata; + + +/* +Callback for when data is read. Return value is the number of bytes actually read. + +pUserData [in] The user data that was passed to drmp3_init(), and family. +pBufferOut [out] The output buffer. +bytesToRead [in] The number of bytes to read. + +Returns the number of bytes actually read. + +A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until +either the entire bytesToRead is filled or you have reached the end of the stream. +*/ +typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead); + +/* +Callback for when data needs to be seeked. + +pUserData [in] The user data that was passed to drmp3_init(), and family. +offset [in] The number of bytes to move, relative to the origin. Can be negative. +origin [in] The origin of the seek. + +Returns whether or not the seek was successful. +*/ +typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin); + +/* +Callback for retrieving the current cursor position. + +pUserData [in] The user data that was passed to drmp3_init(), and family. +pCursor [out] The cursor position in bytes from the start of the stream. + +Returns whether or not the cursor position was successfully retrieved. +*/ +typedef drmp3_bool32 (* drmp3_tell_proc)(void* pUserData, drmp3_int64* pCursor); + + +/* +Callback for when metadata is read. + +Only the raw data is provided. The client is responsible for parsing the contents of the data themsevles. +*/ +typedef void (* drmp3_meta_proc)(void* pUserData, const drmp3_metadata* pMetadata); + + +typedef struct +{ + drmp3_uint32 channels; + drmp3_uint32 sampleRate; +} drmp3_config; + +typedef struct +{ + drmp3dec decoder; + drmp3_uint32 channels; + drmp3_uint32 sampleRate; + drmp3_read_proc onRead; + drmp3_seek_proc onSeek; + drmp3_meta_proc onMeta; + void* pUserData; + void* pUserDataMeta; + drmp3_allocation_callbacks allocationCallbacks; + drmp3_uint32 mp3FrameChannels; /* The number of channels in the currently loaded MP3 frame. Internal use only. */ + drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */ + drmp3_uint32 pcmFramesConsumedInMP3Frame; + drmp3_uint32 pcmFramesRemainingInMP3Frame; + drmp3_uint8 pcmFrames[sizeof(float)*DRMP3_MAX_SAMPLES_PER_FRAME]; /* <-- Multipled by sizeof(float) to ensure there's enough room for DR_MP3_FLOAT_OUTPUT. */ + drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally. */ + drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */ + drmp3_uint64 streamLength; /* The length of the stream in bytes. dr_mp3 will not read beyond this. If a ID3v1 or APE tag is present, this will be set to the first byte of the tag. */ + drmp3_uint64 streamStartOffset; /* The offset of the start of the MP3 data. This is used for skipping ID3v2 and VBR tags. */ + drmp3_seek_point* pSeekPoints; /* NULL by default. Set with drmp3_bind_seek_table(). Memory is owned by the client. dr_mp3 will never attempt to free this pointer. */ + drmp3_uint32 seekPointCount; /* The number of items in pSeekPoints. When set to 0 assumes to no seek table. Defaults to zero. */ + drmp3_uint32 delayInPCMFrames; + drmp3_uint32 paddingInPCMFrames; + drmp3_uint64 totalPCMFrameCount; /* Set to DRMP3_UINT64_MAX if the length is unknown. Includes delay and padding. */ + drmp3_bool32 isVBR; + drmp3_bool32 isCBR; + size_t dataSize; + size_t dataCapacity; + size_t dataConsumed; + drmp3_uint8* pData; + drmp3_bool32 atEnd; + struct + { + const drmp3_uint8* pData; + size_t dataSize; + size_t currentReadPos; + } memory; /* Only used for decoders that were opened against a block of memory. */ +} drmp3; + +/* +Initializes an MP3 decoder. + +onRead [in] The function to call when data needs to be read from the client. +onSeek [in] The function to call when the read position of the client data needs to move. +onTell [in] The function to call when the read position of the client data needs to be retrieved. +pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek. + +Returns true if successful; false otherwise. + +Close the loader with drmp3_uninit(). + +See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit() +*/ +DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks); + +/* +Initializes an MP3 decoder from a block of memory. + +This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for +the lifetime of the drmp3 object. + +The buffer should contain the contents of the entire MP3 file. +*/ +DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks); + +#ifndef DR_MP3_NO_STDIO +/* +Initializes an MP3 decoder from a file. + +This holds the internal FILE object until drmp3_uninit() is called. Keep this in mind if you're caching drmp3 +objects because the operating system may restrict the number of file handles an application can have open at +any given time. +*/ +DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); + +DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks); +#endif + +/* +Uninitializes an MP3 decoder. +*/ +DRMP3_API void drmp3_uninit(drmp3* pMP3); + +/* +Reads PCM frames as interleaved 32-bit IEEE floating point PCM. + +Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames. +*/ +DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut); + +/* +Reads PCM frames as interleaved signed 16-bit integer PCM. + +Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames. +*/ +DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut); + +/* +Seeks to a specific frame. + +Note that this is _not_ an MP3 frame, but rather a PCM frame. +*/ +DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex); + +/* +Calculates the total number of PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet +radio. Runs in linear time. Returns 0 on error. +*/ +DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3); + +/* +Calculates the total number of MP3 frames in the MP3 stream. Cannot be used for infinite streams such as internet +radio. Runs in linear time. Returns 0 on error. +*/ +DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3); + +/* +Calculates the total number of MP3 and PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet +radio. Runs in linear time. Returns 0 on error. + +This is equivalent to calling drmp3_get_mp3_frame_count() and drmp3_get_pcm_frame_count() except that it's more efficient. +*/ +DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount); + +/* +Calculates the seekpoints based on PCM frames. This is slow. + +pSeekpoint count is a pointer to a uint32 containing the seekpoint count. On input it contains the desired count. +On output it contains the actual count. The reason for this design is that the client may request too many +seekpoints, in which case dr_mp3 will return a corrected count. + +Note that seektable seeking is not quite sample exact when the MP3 stream contains inconsistent sample rates. +*/ +DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints); + +/* +Binds a seek table to the decoder. + +This does _not_ make a copy of pSeekPoints - it only references it. It is up to the application to ensure this +remains valid while it is bound to the decoder. + +Use drmp3_calculate_seek_points() to calculate the seek points. +*/ +DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints); + + +/* +Opens an decodes an entire MP3 stream as a single operation. + +On output pConfig will receive the channel count and sample rate of the stream. + +Free the returned pointer with drmp3_free(). +*/ +DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); + +DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); + +#ifndef DR_MP3_NO_STDIO +DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); +DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); +#endif + +/* +Allocates a block of memory on the heap. +*/ +DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks); + +/* +Frees any memory that was allocated by a public drmp3 API. +*/ +DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks); + +#ifdef __cplusplus +} +#endif +#endif /* dr_mp3_h */ + + +/************************************************************************************************************************************************************ + ************************************************************************************************************************************************************ + + IMPLEMENTATION + + ************************************************************************************************************************************************************ + ************************************************************************************************************************************************************/ +#if defined(DR_MP3_IMPLEMENTATION) +#ifndef dr_mp3_c +#define dr_mp3_c + +#include +#include +#include /* For INT_MAX */ + +DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision) +{ + if (pMajor) { + *pMajor = DRMP3_VERSION_MAJOR; + } + + if (pMinor) { + *pMinor = DRMP3_VERSION_MINOR; + } + + if (pRevision) { + *pRevision = DRMP3_VERSION_REVISION; + } +} + +DRMP3_API const char* drmp3_version_string(void) +{ + return DRMP3_VERSION_STRING; +} + +/* Disable SIMD when compiling with TCC for now. */ +#if defined(__TINYC__) +#define DR_MP3_NO_SIMD +#endif + +#define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset))) + +#ifndef DRMP3_MAX_FRAME_SYNC_MATCHES +#define DRMP3_MAX_FRAME_SYNC_MATCHES 10 +#endif + +#define DRMP3_SHORT_BLOCK_TYPE 2 +#define DRMP3_STOP_BLOCK_TYPE 3 +#define DRMP3_MODE_MONO 3 +#define DRMP3_MODE_JOINT_STEREO 1 +#define DRMP3_HDR_SIZE 4 +#define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0) +#define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60) +#define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0) +#define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1)) +#define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2) +#define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8) +#define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10) +#define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10) +#define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20) +#define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3) +#define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3) +#define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3) +#define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4) +#define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3) +#define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3) +#define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2) +#define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6) + +#define DRMP3_BITS_DEQUANTIZER_OUT -1 +#define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210) +#define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3) + +#define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a)) +#define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a)) + +#if !defined(DR_MP3_NO_SIMD) + +#if !defined(DR_MP3_ONLY_SIMD) && ((defined(_MSC_VER) && _MSC_VER >= 1400) && defined(_M_X64)) || ((defined(__i386) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)) && ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__))) +#define DR_MP3_ONLY_SIMD +#endif +#if !defined(DR_MP3_ONLY_SIMD) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) +#define DR_MP3_ONLY_SIMD +#endif + +#if ((defined(_MSC_VER) && _MSC_VER >= 1400) && defined(_M_X64)) || ((defined(__i386) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)) && ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__))) +#if defined(_MSC_VER) +#include +#endif +#include +#define DRMP3_HAVE_SSE 1 +#define DRMP3_HAVE_SIMD 1 +#define DRMP3_VSTORE _mm_storeu_ps +#define DRMP3_VLD _mm_loadu_ps +#define DRMP3_VSET _mm_set1_ps +#define DRMP3_VADD _mm_add_ps +#define DRMP3_VSUB _mm_sub_ps +#define DRMP3_VMUL _mm_mul_ps +#define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y)) +#define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y)) +#define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s)) +#define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3)) +typedef __m128 drmp3_f4; +#if (defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)) && !defined(__clang__) +#define drmp3_cpuid __cpuid +#else +static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType) +{ +#if defined(__PIC__) + __asm__ __volatile__( +#if defined(__x86_64__) + "push %%rbx\n" + "cpuid\n" + "xchgl %%ebx, %1\n" + "pop %%rbx\n" +#else + "xchgl %%ebx, %1\n" + "cpuid\n" + "xchgl %%ebx, %1\n" +#endif + : "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) + : "a" (InfoType)); +#else + __asm__ __volatile__( + "cpuid" + : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) + : "a" (InfoType)); +#endif +} +#endif +static int drmp3_have_simd(void) +{ +#ifdef DR_MP3_ONLY_SIMD + return 1; +#else + static int g_have_simd; + int CPUInfo[4]; +#ifdef MINIMP3_TEST + static int g_counter; + if (g_counter++ > 100) + return 0; +#endif + if (g_have_simd) + goto end; + drmp3_cpuid(CPUInfo, 0); + if (CPUInfo[0] > 0) + { + drmp3_cpuid(CPUInfo, 1); + g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; /* SSE2 */ + return g_have_simd - 1; + } + +end: + return g_have_simd - 1; +#endif +} +#elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) +#include +#define DRMP3_HAVE_SSE 0 +#define DRMP3_HAVE_SIMD 1 +#define DRMP3_VSTORE vst1q_f32 +#define DRMP3_VLD vld1q_f32 +#define DRMP3_VSET vmovq_n_f32 +#define DRMP3_VADD vaddq_f32 +#define DRMP3_VSUB vsubq_f32 +#define DRMP3_VMUL vmulq_f32 +#define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y) +#define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y) +#define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s)) +#define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x))) +typedef float32x4_t drmp3_f4; +static int drmp3_have_simd(void) +{ /* TODO: detect neon for !DR_MP3_ONLY_SIMD */ + return 1; +} +#else +#define DRMP3_HAVE_SSE 0 +#define DRMP3_HAVE_SIMD 0 +#ifdef DR_MP3_ONLY_SIMD +#error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled +#endif +#endif + +#else + +#define DRMP3_HAVE_SIMD 0 + +#endif + +#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) && !defined(__ARM_ARCH_6M__) +#define DRMP3_HAVE_ARMV6 1 +static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(drmp3_int32 a) +{ + drmp3_int32 x = 0; + __asm__ ("ssat %0, #16, %1" : "=r"(x) : "r"(a)); + return x; +} +#else +#define DRMP3_HAVE_ARMV6 0 +#endif + + +/* Standard library stuff. */ +#ifndef DRMP3_ASSERT +#include +#define DRMP3_ASSERT(expression) assert(expression) +#endif +#ifndef DRMP3_COPY_MEMORY +#define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz)) +#endif +#ifndef DRMP3_MOVE_MEMORY +#define DRMP3_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz)) +#endif +#ifndef DRMP3_ZERO_MEMORY +#define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz)) +#endif +#define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p))) +#ifndef DRMP3_MALLOC +#define DRMP3_MALLOC(sz) malloc((sz)) +#endif +#ifndef DRMP3_REALLOC +#define DRMP3_REALLOC(p, sz) realloc((p), (sz)) +#endif +#ifndef DRMP3_FREE +#define DRMP3_FREE(p) free((p)) +#endif + + + +typedef struct +{ + float scf[3*64]; + drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64]; +} drmp3_L12_scale_info; + +typedef struct +{ + drmp3_uint8 tab_offset, code_tab_width, band_count; +} drmp3_L12_subband_alloc; + +static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes) +{ + bs->buf = data; + bs->pos = 0; + bs->limit = bytes*8; +} + +static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n) +{ + drmp3_uint32 next, cache = 0, s = bs->pos & 7; + int shl = n + s; + const drmp3_uint8 *p = bs->buf + (bs->pos >> 3); + if ((bs->pos += n) > bs->limit) + return 0; + next = *p++ & (255 >> s); + while ((shl -= 8) > 0) + { + cache |= next << shl; + next = *p++; + } + return cache | (next >> -shl); +} + +static int drmp3_hdr_valid(const drmp3_uint8 *h) +{ + return h[0] == 0xff && + ((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) && + (DRMP3_HDR_GET_LAYER(h) != 0) && + (DRMP3_HDR_GET_BITRATE(h) != 15) && + (DRMP3_HDR_GET_SAMPLE_RATE(h) != 3); +} + +static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2) +{ + return drmp3_hdr_valid(h2) && + ((h1[1] ^ h2[1]) & 0xFE) == 0 && + ((h1[2] ^ h2[2]) & 0x0C) == 0 && + !(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2)); +} + +static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h) +{ + static const drmp3_uint8 halfrate[2][3][15] = { + { { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,16,24,28,32,40,48,56,64,72,80,88,96,112,128 } }, + { { 0,16,20,24,28,32,40,48,56,64,80,96,112,128,160 }, { 0,16,24,28,32,40,48,56,64,80,96,112,128,160,192 }, { 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224 } }, + }; + return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)]; +} + +static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h) +{ + static const unsigned g_hz[3] = { 44100, 48000, 32000 }; + return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h); +} + +static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h) +{ + return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h)); +} + +static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size) +{ + int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h); + if (DRMP3_HDR_IS_LAYER_1(h)) + { + frame_bytes &= ~3; /* slot align */ + } + return frame_bytes ? frame_bytes : free_format_size; +} + +static int drmp3_hdr_padding(const drmp3_uint8 *h) +{ + return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0; +} + +#ifndef DR_MP3_ONLY_MP3 +static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci) +{ + const drmp3_L12_subband_alloc *alloc; + int mode = DRMP3_HDR_GET_STEREO_MODE(hdr); + int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32; + + if (DRMP3_HDR_IS_LAYER_1(hdr)) + { + static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } }; + alloc = g_alloc_L1; + nbands = 32; + } else if (!DRMP3_HDR_TEST_MPEG1(hdr)) + { + static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } }; + alloc = g_alloc_L2M2; + nbands = 30; + } else + { + static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } }; + int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr); + unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO); + if (!kbps) /* free-format */ + { + kbps = 192; + } + + alloc = g_alloc_L2M1; + nbands = 27; + if (kbps < 56) + { + static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } }; + alloc = g_alloc_L2M1_lowrate; + nbands = sample_rate_idx == 2 ? 12 : 8; + } else if (kbps >= 96 && sample_rate_idx != 1) + { + nbands = 30; + } + } + + sci->total_bands = (drmp3_uint8)nbands; + sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands); + + return alloc; +} + +static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf) +{ + static const float g_deq_L12[18*3] = { +#define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x + DRMP3_DQ(3),DRMP3_DQ(7),DRMP3_DQ(15),DRMP3_DQ(31),DRMP3_DQ(63),DRMP3_DQ(127),DRMP3_DQ(255),DRMP3_DQ(511),DRMP3_DQ(1023),DRMP3_DQ(2047),DRMP3_DQ(4095),DRMP3_DQ(8191),DRMP3_DQ(16383),DRMP3_DQ(32767),DRMP3_DQ(65535),DRMP3_DQ(3),DRMP3_DQ(5),DRMP3_DQ(9) + }; + int i, m; + for (i = 0; i < bands; i++) + { + float s = 0; + int ba = *pba++; + int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0; + for (m = 4; m; m >>= 1) + { + if (mask & m) + { + int b = drmp3_bs_get_bits(bs, 6); + s = g_deq_L12[ba*3 - 6 + b % 3]*(int)(1 << 21 >> b/3); + } + *scf++ = s; + } + } +} + +static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci) +{ + static const drmp3_uint8 g_bitalloc_code_tab[] = { + 0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16, + 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16, + 0,17,18, 3,19,4,5,16, + 0,17,18,16, + 0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15, + 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14, + 0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16 + }; + const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci); + + int i, k = 0, ba_bits = 0; + const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab; + + for (i = 0; i < sci->total_bands; i++) + { + drmp3_uint8 ba; + if (i == k) + { + k += subband_alloc->band_count; + ba_bits = subband_alloc->code_tab_width; + ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset; + subband_alloc++; + } + ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)]; + sci->bitalloc[2*i] = ba; + if (i < sci->stereo_bands) + { + ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)]; + } + sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0; + } + + for (i = 0; i < 2*sci->total_bands; i++) + { + sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6); + } + + drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf); + + for (i = sci->stereo_bands; i < sci->total_bands; i++) + { + sci->bitalloc[2*i + 1] = 0; + } +} + +static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size) +{ + int i, j, k, choff = 576; + for (j = 0; j < 4; j++) + { + float *dst = grbuf + group_size*j; + for (i = 0; i < 2*sci->total_bands; i++) + { + int ba = sci->bitalloc[i]; + if (ba != 0) + { + if (ba < 17) + { + int half = (1 << (ba - 1)) - 1; + for (k = 0; k < group_size; k++) + { + dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half); + } + } else + { + unsigned mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */ + unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */ + for (k = 0; k < group_size; k++, code /= mod) + { + dst[k] = (float)((int)(code % mod - mod/2)); + } + } + } + dst += choff; + choff = 18 - choff; + } + } + return group_size*4; +} + +static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst) +{ + int i, k; + DRMP3_COPY_MEMORY(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float)); + for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6) + { + for (k = 0; k < 12; k++) + { + dst[k + 0] *= scf[0]; + dst[k + 576] *= scf[3]; + } + } +} +#endif + +static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr) +{ + static const drmp3_uint8 g_scf_long[8][23] = { + { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, + { 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 }, + { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, + { 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 }, + { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, + { 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 }, + { 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 }, + { 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 } + }; + static const drmp3_uint8 g_scf_short[8][40] = { + { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 }, + { 8,8,8,8,8,8,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 }, + { 4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 }, + { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 }, + { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 }, + { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 }, + { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 }, + { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 } + }; + static const drmp3_uint8 g_scf_mixed[8][40] = { + { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 }, + { 12,12,12,4,4,4,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 }, + { 6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 }, + { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 }, + { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 }, + { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 }, + { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 }, + { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 } + }; + + unsigned tables, scfsi = 0; + int main_data_begin, part_23_sum = 0; + int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2; + int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0); + + if (DRMP3_HDR_TEST_MPEG1(hdr)) + { + gr_count *= 2; + main_data_begin = drmp3_bs_get_bits(bs, 9); + scfsi = drmp3_bs_get_bits(bs, 7 + gr_count); + } else + { + main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count; + } + + do + { + if (DRMP3_HDR_IS_MONO(hdr)) + { + scfsi <<= 4; + } + gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12); + part_23_sum += gr->part_23_length; + gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9); + if (gr->big_values > 288) + { + return -1; + } + gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8); + gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9); + gr->sfbtab = g_scf_long[sr_idx]; + gr->n_long_sfb = 22; + gr->n_short_sfb = 0; + if (drmp3_bs_get_bits(bs, 1)) + { + gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2); + if (!gr->block_type) + { + return -1; + } + gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); + gr->region_count[0] = 7; + gr->region_count[1] = 255; + if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE) + { + scfsi &= 0x0F0F; + if (!gr->mixed_block_flag) + { + gr->region_count[0] = 8; + gr->sfbtab = g_scf_short[sr_idx]; + gr->n_long_sfb = 0; + gr->n_short_sfb = 39; + } else + { + gr->sfbtab = g_scf_mixed[sr_idx]; + gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6; + gr->n_short_sfb = 30; + } + } + tables = drmp3_bs_get_bits(bs, 10); + tables <<= 5; + gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); + gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); + gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); + } else + { + gr->block_type = 0; + gr->mixed_block_flag = 0; + tables = drmp3_bs_get_bits(bs, 15); + gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4); + gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); + gr->region_count[2] = 255; + } + gr->table_select[0] = (drmp3_uint8)(tables >> 10); + gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31); + gr->table_select[2] = (drmp3_uint8)((tables) & 31); + gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500)); + gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); + gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); + gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15); + scfsi <<= 4; + gr++; + } while(--gr_count); + + if (part_23_sum + bs->pos > bs->limit + main_data_begin*8) + { + return -1; + } + + return main_data_begin; +} + +static void drmp3_L3_read_scalefactors(drmp3_uint8 *scf, drmp3_uint8 *ist_pos, const drmp3_uint8 *scf_size, const drmp3_uint8 *scf_count, drmp3_bs *bitbuf, int scfsi) +{ + int i, k; + for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2) + { + int cnt = scf_count[i]; + if (scfsi & 8) + { + DRMP3_COPY_MEMORY(scf, ist_pos, cnt); + } else + { + int bits = scf_size[i]; + if (!bits) + { + DRMP3_ZERO_MEMORY(scf, cnt); + DRMP3_ZERO_MEMORY(ist_pos, cnt); + } else + { + int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1; + for (k = 0; k < cnt; k++) + { + int s = drmp3_bs_get_bits(bitbuf, bits); + ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s); + scf[k] = (drmp3_uint8)s; + } + } + } + ist_pos += cnt; + scf += cnt; + } + scf[0] = scf[1] = scf[2] = 0; +} + +static float drmp3_L3_ldexp_q2(float y, int exp_q2) +{ + static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f }; + int e; + do + { + e = DRMP3_MIN(30*4, exp_q2); + y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2)); + } while ((exp_q2 -= e) > 0); + return y; +} + +/* +I've had reports of GCC 14 throwing an incorrect -Wstringop-overflow warning here. This is an attempt +to silence this warning. +*/ +#if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wstringop-overflow" +#endif +static void drmp3_L3_decode_scalefactors(const drmp3_uint8 *hdr, drmp3_uint8 *ist_pos, drmp3_bs *bs, const drmp3_L3_gr_info *gr, float *scf, int ch) +{ + static const drmp3_uint8 g_scf_partitions[3][28] = { + { 6,5,5, 5,6,5,5,5,6,5, 7,3,11,10,0,0, 7, 7, 7,0, 6, 6,6,3, 8, 8,5,0 }, + { 8,9,6,12,6,9,9,9,6,9,12,6,15,18,0,0, 6,15,12,0, 6,12,9,6, 6,18,9,0 }, + { 9,9,6,12,9,9,9,9,9,9,12,6,18,18,0,0,12,12,12,0,12, 9,9,6,15,12,9,0 } + }; + const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb]; + drmp3_uint8 scf_size[4], iscf[40]; + int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi; + float gain; + + if (DRMP3_HDR_TEST_MPEG1(hdr)) + { + static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 }; + int part = g_scfc_decode[gr->scalefac_compress]; + scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2); + scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3); + } else + { + static const drmp3_uint8 g_mod[6*4] = { 5,5,4,4,5,5,4,1,4,3,1,1,5,6,6,1,4,4,4,1,4,3,1,1 }; + int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch; + sfc = gr->scalefac_compress >> ist; + for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4) + { + for (modprod = 1, i = 3; i >= 0; i--) + { + scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]); + modprod *= g_mod[k + i]; + } + } + scf_partition += k; + scfsi = -16; + } + drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi); + + if (gr->n_short_sfb) + { + int sh = 3 - scf_shift; + for (i = 0; i < gr->n_short_sfb; i += 3) + { + iscf[gr->n_long_sfb + i + 0] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 0] + (gr->subblock_gain[0] << sh)); + iscf[gr->n_long_sfb + i + 1] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 1] + (gr->subblock_gain[1] << sh)); + iscf[gr->n_long_sfb + i + 2] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 2] + (gr->subblock_gain[2] << sh)); + } + } else if (gr->preflag) + { + static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 }; + for (i = 0; i < 10; i++) + { + iscf[11 + i] = (drmp3_uint8)(iscf[11 + i] + g_preamp[i]); + } + } + + gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0); + gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp); + for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++) + { + scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift); + } +} +#if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__) + #pragma GCC diagnostic pop +#endif + +static const float g_drmp3_pow43[129 + 16] = { + 0,-1,-2.519842f,-4.326749f,-6.349604f,-8.549880f,-10.902724f,-13.390518f,-16.000000f,-18.720754f,-21.544347f,-24.463781f,-27.473142f,-30.567351f,-33.741992f,-36.993181f, + 0,1,2.519842f,4.326749f,6.349604f,8.549880f,10.902724f,13.390518f,16.000000f,18.720754f,21.544347f,24.463781f,27.473142f,30.567351f,33.741992f,36.993181f,40.317474f,43.711787f,47.173345f,50.699631f,54.288352f,57.937408f,61.644865f,65.408941f,69.227979f,73.100443f,77.024898f,81.000000f,85.024491f,89.097188f,93.216975f,97.382800f,101.593667f,105.848633f,110.146801f,114.487321f,118.869381f,123.292209f,127.755065f,132.257246f,136.798076f,141.376907f,145.993119f,150.646117f,155.335327f,160.060199f,164.820202f,169.614826f,174.443577f,179.305980f,184.201575f,189.129918f,194.090580f,199.083145f,204.107210f,209.162385f,214.248292f,219.364564f,224.510845f,229.686789f,234.892058f,240.126328f,245.389280f,250.680604f,256.000000f,261.347174f,266.721841f,272.123723f,277.552547f,283.008049f,288.489971f,293.998060f,299.532071f,305.091761f,310.676898f,316.287249f,321.922592f,327.582707f,333.267377f,338.976394f,344.709550f,350.466646f,356.247482f,362.051866f,367.879608f,373.730522f,379.604427f,385.501143f,391.420496f,397.362314f,403.326427f,409.312672f,415.320884f,421.350905f,427.402579f,433.475750f,439.570269f,445.685987f,451.822757f,457.980436f,464.158883f,470.357960f,476.577530f,482.817459f,489.077615f,495.357868f,501.658090f,507.978156f,514.317941f,520.677324f,527.056184f,533.454404f,539.871867f,546.308458f,552.764065f,559.238575f,565.731879f,572.243870f,578.774440f,585.323483f,591.890898f,598.476581f,605.080431f,611.702349f,618.342238f,625.000000f,631.675540f,638.368763f,645.079578f +}; + +static float drmp3_L3_pow_43(int x) +{ + float frac; + int sign, mult = 256; + + if (x < 129) + { + return g_drmp3_pow43[16 + x]; + } + + if (x < 1024) + { + mult = 16; + x <<= 3; + } + + sign = 2*x & 64; + frac = (float)((x & 63) - sign) / ((x & ~63) + sign); + return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult; +} + +static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit) +{ + static const drmp3_int16 tabs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 785,785,785,785,784,784,784,784,513,513,513,513,513,513,513,513,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256, + -255,1313,1298,1282,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,290,288, + -255,1313,1298,1282,769,769,769,769,529,529,529,529,529,529,529,529,528,528,528,528,528,528,528,528,512,512,512,512,512,512,512,512,290,288, + -253,-318,-351,-367,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,819,818,547,547,275,275,275,275,561,560,515,546,289,274,288,258, + -254,-287,1329,1299,1314,1312,1057,1057,1042,1042,1026,1026,784,784,784,784,529,529,529,529,529,529,529,529,769,769,769,769,768,768,768,768,563,560,306,306,291,259, + -252,-413,-477,-542,1298,-575,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-383,-399,1107,1092,1106,1061,849,849,789,789,1104,1091,773,773,1076,1075,341,340,325,309,834,804,577,577,532,532,516,516,832,818,803,816,561,561,531,531,515,546,289,289,288,258, + -252,-429,-493,-559,1057,1057,1042,1042,529,529,529,529,529,529,529,529,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,-382,1077,-415,1106,1061,1104,849,849,789,789,1091,1076,1029,1075,834,834,597,581,340,340,339,324,804,833,532,532,832,772,818,803,817,787,816,771,290,290,290,290,288,258, + -253,-349,-414,-447,-463,1329,1299,-479,1314,1312,1057,1057,1042,1042,1026,1026,785,785,785,785,784,784,784,784,769,769,769,769,768,768,768,768,-319,851,821,-335,836,850,805,849,341,340,325,336,533,533,579,579,564,564,773,832,578,548,563,516,321,276,306,291,304,259, + -251,-572,-733,-830,-863,-879,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,1396,1351,1381,1366,1395,1335,1380,-559,1334,1138,1138,1063,1063,1350,1392,1031,1031,1062,1062,1364,1363,1120,1120,1333,1348,881,881,881,881,375,374,359,373,343,358,341,325,791,791,1123,1122,-703,1105,1045,-719,865,865,790,790,774,774,1104,1029,338,293,323,308,-799,-815,833,788,772,818,803,816,322,292,307,320,561,531,515,546,289,274,288,258, + -251,-525,-605,-685,-765,-831,-846,1298,1057,1057,1312,1282,785,785,785,785,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,1399,1398,1383,1367,1382,1396,1351,-511,1381,1366,1139,1139,1079,1079,1124,1124,1364,1349,1363,1333,882,882,882,882,807,807,807,807,1094,1094,1136,1136,373,341,535,535,881,775,867,822,774,-591,324,338,-671,849,550,550,866,864,609,609,293,336,534,534,789,835,773,-751,834,804,308,307,833,788,832,772,562,562,547,547,305,275,560,515,290,290, + -252,-397,-477,-557,-622,-653,-719,-735,-750,1329,1299,1314,1057,1057,1042,1042,1312,1282,1024,1024,785,785,785,785,784,784,784,784,769,769,769,769,-383,1127,1141,1111,1126,1140,1095,1110,869,869,883,883,1079,1109,882,882,375,374,807,868,838,881,791,-463,867,822,368,263,852,837,836,-543,610,610,550,550,352,336,534,534,865,774,851,821,850,805,593,533,579,564,773,832,578,578,548,548,577,577,307,276,306,291,516,560,259,259, + -250,-2107,-2507,-2764,-2909,-2974,-3007,-3023,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-767,-1052,-1213,-1277,-1358,-1405,-1469,-1535,-1550,-1582,-1614,-1647,-1662,-1694,-1726,-1759,-1774,-1807,-1822,-1854,-1886,1565,-1919,-1935,-1951,-1967,1731,1730,1580,1717,-1983,1729,1564,-1999,1548,-2015,-2031,1715,1595,-2047,1714,-2063,1610,-2079,1609,-2095,1323,1323,1457,1457,1307,1307,1712,1547,1641,1700,1699,1594,1685,1625,1442,1442,1322,1322,-780,-973,-910,1279,1278,1277,1262,1276,1261,1275,1215,1260,1229,-959,974,974,989,989,-943,735,478,478,495,463,506,414,-1039,1003,958,1017,927,942,987,957,431,476,1272,1167,1228,-1183,1256,-1199,895,895,941,941,1242,1227,1212,1135,1014,1014,490,489,503,487,910,1013,985,925,863,894,970,955,1012,847,-1343,831,755,755,984,909,428,366,754,559,-1391,752,486,457,924,997,698,698,983,893,740,740,908,877,739,739,667,667,953,938,497,287,271,271,683,606,590,712,726,574,302,302,738,736,481,286,526,725,605,711,636,724,696,651,589,681,666,710,364,467,573,695,466,466,301,465,379,379,709,604,665,679,316,316,634,633,436,436,464,269,424,394,452,332,438,363,347,408,393,448,331,422,362,407,392,421,346,406,391,376,375,359,1441,1306,-2367,1290,-2383,1337,-2399,-2415,1426,1321,-2431,1411,1336,-2447,-2463,-2479,1169,1169,1049,1049,1424,1289,1412,1352,1319,-2495,1154,1154,1064,1064,1153,1153,416,390,360,404,403,389,344,374,373,343,358,372,327,357,342,311,356,326,1395,1394,1137,1137,1047,1047,1365,1392,1287,1379,1334,1364,1349,1378,1318,1363,792,792,792,792,1152,1152,1032,1032,1121,1121,1046,1046,1120,1120,1030,1030,-2895,1106,1061,1104,849,849,789,789,1091,1076,1029,1090,1060,1075,833,833,309,324,532,532,832,772,818,803,561,561,531,560,515,546,289,274,288,258, + -250,-1179,-1579,-1836,-1996,-2124,-2253,-2333,-2413,-2477,-2542,-2574,-2607,-2622,-2655,1314,1313,1298,1312,1282,785,785,785,785,1040,1040,1025,1025,768,768,768,768,-766,-798,-830,-862,-895,-911,-927,-943,-959,-975,-991,-1007,-1023,-1039,-1055,-1070,1724,1647,-1103,-1119,1631,1767,1662,1738,1708,1723,-1135,1780,1615,1779,1599,1677,1646,1778,1583,-1151,1777,1567,1737,1692,1765,1722,1707,1630,1751,1661,1764,1614,1736,1676,1763,1750,1645,1598,1721,1691,1762,1706,1582,1761,1566,-1167,1749,1629,767,766,751,765,494,494,735,764,719,749,734,763,447,447,748,718,477,506,431,491,446,476,461,505,415,430,475,445,504,399,460,489,414,503,383,474,429,459,502,502,746,752,488,398,501,473,413,472,486,271,480,270,-1439,-1455,1357,-1471,-1487,-1503,1341,1325,-1519,1489,1463,1403,1309,-1535,1372,1448,1418,1476,1356,1462,1387,-1551,1475,1340,1447,1402,1386,-1567,1068,1068,1474,1461,455,380,468,440,395,425,410,454,364,467,466,464,453,269,409,448,268,432,1371,1473,1432,1417,1308,1460,1355,1446,1459,1431,1083,1083,1401,1416,1458,1445,1067,1067,1370,1457,1051,1051,1291,1430,1385,1444,1354,1415,1400,1443,1082,1082,1173,1113,1186,1066,1185,1050,-1967,1158,1128,1172,1097,1171,1081,-1983,1157,1112,416,266,375,400,1170,1142,1127,1065,793,793,1169,1033,1156,1096,1141,1111,1155,1080,1126,1140,898,898,808,808,897,897,792,792,1095,1152,1032,1125,1110,1139,1079,1124,882,807,838,881,853,791,-2319,867,368,263,822,852,837,866,806,865,-2399,851,352,262,534,534,821,836,594,594,549,549,593,593,533,533,848,773,579,579,564,578,548,563,276,276,577,576,306,291,516,560,305,305,275,259, + -251,-892,-2058,-2620,-2828,-2957,-3023,-3039,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,-559,1530,-575,-591,1528,1527,1407,1526,1391,1023,1023,1023,1023,1525,1375,1268,1268,1103,1103,1087,1087,1039,1039,1523,-604,815,815,815,815,510,495,509,479,508,463,507,447,431,505,415,399,-734,-782,1262,-815,1259,1244,-831,1258,1228,-847,-863,1196,-879,1253,987,987,748,-767,493,493,462,477,414,414,686,669,478,446,461,445,474,429,487,458,412,471,1266,1264,1009,1009,799,799,-1019,-1276,-1452,-1581,-1677,-1757,-1821,-1886,-1933,-1997,1257,1257,1483,1468,1512,1422,1497,1406,1467,1496,1421,1510,1134,1134,1225,1225,1466,1451,1374,1405,1252,1252,1358,1480,1164,1164,1251,1251,1238,1238,1389,1465,-1407,1054,1101,-1423,1207,-1439,830,830,1248,1038,1237,1117,1223,1148,1236,1208,411,426,395,410,379,269,1193,1222,1132,1235,1221,1116,976,976,1192,1162,1177,1220,1131,1191,963,963,-1647,961,780,-1663,558,558,994,993,437,408,393,407,829,978,813,797,947,-1743,721,721,377,392,844,950,828,890,706,706,812,859,796,960,948,843,934,874,571,571,-1919,690,555,689,421,346,539,539,944,779,918,873,932,842,903,888,570,570,931,917,674,674,-2575,1562,-2591,1609,-2607,1654,1322,1322,1441,1441,1696,1546,1683,1593,1669,1624,1426,1426,1321,1321,1639,1680,1425,1425,1305,1305,1545,1668,1608,1623,1667,1592,1638,1666,1320,1320,1652,1607,1409,1409,1304,1304,1288,1288,1664,1637,1395,1395,1335,1335,1622,1636,1394,1394,1319,1319,1606,1621,1392,1392,1137,1137,1137,1137,345,390,360,375,404,373,1047,-2751,-2767,-2783,1062,1121,1046,-2799,1077,-2815,1106,1061,789,789,1105,1104,263,355,310,340,325,354,352,262,339,324,1091,1076,1029,1090,1060,1075,833,833,788,788,1088,1028,818,818,803,803,561,561,531,531,816,771,546,546,289,274,288,258, + -253,-317,-381,-446,-478,-509,1279,1279,-811,-1179,-1451,-1756,-1900,-2028,-2189,-2253,-2333,-2414,-2445,-2511,-2526,1313,1298,-2559,1041,1041,1040,1040,1025,1025,1024,1024,1022,1007,1021,991,1020,975,1019,959,687,687,1018,1017,671,671,655,655,1016,1015,639,639,758,758,623,623,757,607,756,591,755,575,754,559,543,543,1009,783,-575,-621,-685,-749,496,-590,750,749,734,748,974,989,1003,958,988,973,1002,942,987,957,972,1001,926,986,941,971,956,1000,910,985,925,999,894,970,-1071,-1087,-1102,1390,-1135,1436,1509,1451,1374,-1151,1405,1358,1480,1420,-1167,1507,1494,1389,1342,1465,1435,1450,1326,1505,1310,1493,1373,1479,1404,1492,1464,1419,428,443,472,397,736,526,464,464,486,457,442,471,484,482,1357,1449,1434,1478,1388,1491,1341,1490,1325,1489,1463,1403,1309,1477,1372,1448,1418,1433,1476,1356,1462,1387,-1439,1475,1340,1447,1402,1474,1324,1461,1371,1473,269,448,1432,1417,1308,1460,-1711,1459,-1727,1441,1099,1099,1446,1386,1431,1401,-1743,1289,1083,1083,1160,1160,1458,1445,1067,1067,1370,1457,1307,1430,1129,1129,1098,1098,268,432,267,416,266,400,-1887,1144,1187,1082,1173,1113,1186,1066,1050,1158,1128,1143,1172,1097,1171,1081,420,391,1157,1112,1170,1142,1127,1065,1169,1049,1156,1096,1141,1111,1155,1080,1126,1154,1064,1153,1140,1095,1048,-2159,1125,1110,1137,-2175,823,823,1139,1138,807,807,384,264,368,263,868,838,853,791,867,822,852,837,866,806,865,790,-2319,851,821,836,352,262,850,805,849,-2399,533,533,835,820,336,261,578,548,563,577,532,532,832,772,562,562,547,547,305,275,560,515,290,290,288,258 }; + static const drmp3_uint8 tab32[] = { 130,162,193,209,44,28,76,140,9,9,9,9,9,9,9,9,190,254,222,238,126,94,157,157,109,61,173,205}; + static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 }; + static const drmp3_int16 tabindex[2*16] = { 0,32,64,98,0,132,180,218,292,364,426,538,648,746,0,1126,1460,1460,1460,1460,1460,1460,1460,1460,1842,1842,1842,1842,1842,1842,1842,1842 }; + static const drmp3_uint8 g_linbits[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,10,13,4,5,6,7,8,9,11,13 }; + +#define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - (n))) +#define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); } +#define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; } +#define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh) + + float one = 0.0f; + int ireg = 0, big_val_cnt = gr_info->big_values; + const drmp3_uint8 *sfb = gr_info->sfbtab; + const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8; + drmp3_uint32 bs_cache = (((bs_next_ptr[0]*256u + bs_next_ptr[1])*256u + bs_next_ptr[2])*256u + bs_next_ptr[3]) << (bs->pos & 7); + int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8; + bs_next_ptr += 4; + + while (big_val_cnt > 0) + { + int tab_num = gr_info->table_select[ireg]; + int sfb_cnt = gr_info->region_count[ireg++]; + const drmp3_int16 *codebook = tabs + tabindex[tab_num]; + int linbits = g_linbits[tab_num]; + if (linbits) + { + do + { + np = *sfb++ / 2; + pairs_to_decode = DRMP3_MIN(big_val_cnt, np); + one = *scf++; + do + { + int j, w = 5; + int leaf = codebook[DRMP3_PEEK_BITS(w)]; + while (leaf < 0) + { + DRMP3_FLUSH_BITS(w); + w = leaf & 7; + leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)]; + } + DRMP3_FLUSH_BITS(leaf >> 8); + + for (j = 0; j < 2; j++, dst++, leaf >>= 4) + { + int lsb = leaf & 0x0F; + if (lsb == 15) + { + lsb += DRMP3_PEEK_BITS(linbits); + DRMP3_FLUSH_BITS(linbits); + DRMP3_CHECK_BITS; + *dst = one*drmp3_L3_pow_43(lsb)*((drmp3_int32)bs_cache < 0 ? -1: 1); + } else + { + *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; + } + DRMP3_FLUSH_BITS(lsb ? 1 : 0); + } + DRMP3_CHECK_BITS; + } while (--pairs_to_decode); + } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); + } else + { + do + { + np = *sfb++ / 2; + pairs_to_decode = DRMP3_MIN(big_val_cnt, np); + one = *scf++; + do + { + int j, w = 5; + int leaf = codebook[DRMP3_PEEK_BITS(w)]; + while (leaf < 0) + { + DRMP3_FLUSH_BITS(w); + w = leaf & 7; + leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)]; + } + DRMP3_FLUSH_BITS(leaf >> 8); + + for (j = 0; j < 2; j++, dst++, leaf >>= 4) + { + int lsb = leaf & 0x0F; + *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; + DRMP3_FLUSH_BITS(lsb ? 1 : 0); + } + DRMP3_CHECK_BITS; + } while (--pairs_to_decode); + } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); + } + } + + for (np = 1 - big_val_cnt;; dst += 4) + { + const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32; + int leaf = codebook_count1[DRMP3_PEEK_BITS(4)]; + if (!(leaf & 8)) + { + leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))]; + } + DRMP3_FLUSH_BITS(leaf & 7); + if (DRMP3_BSPOS > layer3gr_limit) + { + break; + } +#define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; } +#define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) } + DRMP3_RELOAD_SCALEFACTOR; + DRMP3_DEQ_COUNT1(0); + DRMP3_DEQ_COUNT1(1); + DRMP3_RELOAD_SCALEFACTOR; + DRMP3_DEQ_COUNT1(2); + DRMP3_DEQ_COUNT1(3); + DRMP3_CHECK_BITS; + } + + bs->pos = layer3gr_limit; +} + +static void drmp3_L3_midside_stereo(float *left, int n) +{ + int i = 0; + float *right = left + 576; +#if DRMP3_HAVE_SIMD + if (drmp3_have_simd()) + { + for (; i < n - 3; i += 4) + { + drmp3_f4 vl = DRMP3_VLD(left + i); + drmp3_f4 vr = DRMP3_VLD(right + i); + DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr)); + DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr)); + } +#ifdef __GNUC__ + /* Workaround for spurious -Waggressive-loop-optimizations warning from gcc. + * For more info see: https://github.com/lieff/minimp3/issues/88 + */ + if (__builtin_constant_p(n % 4 == 0) && n % 4 == 0) + return; +#endif + } +#endif + for (; i < n; i++) + { + float a = left[i]; + float b = right[i]; + left[i] = a + b; + right[i] = a - b; + } +} + +static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr) +{ + int i; + for (i = 0; i < n; i++) + { + left[i + 576] = left[i]*kr; + left[i] = left[i]*kl; + } +} + +static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3]) +{ + int i, k; + + max_band[0] = max_band[1] = max_band[2] = -1; + + for (i = 0; i < nbands; i++) + { + for (k = 0; k < sfb[i]; k += 2) + { + if (right[k] != 0 || right[k + 1] != 0) + { + max_band[i % 3] = i; + break; + } + } + right += sfb[i]; + } +} + +static void drmp3_L3_stereo_process(float *left, const drmp3_uint8 *ist_pos, const drmp3_uint8 *sfb, const drmp3_uint8 *hdr, int max_band[3], int mpeg2_sh) +{ + static const float g_pan[7*2] = { 0,1,0.21132487f,0.78867513f,0.36602540f,0.63397460f,0.5f,0.5f,0.63397460f,0.36602540f,0.78867513f,0.21132487f,1,0 }; + unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64; + + for (i = 0; sfb[i]; i++) + { + unsigned ipos = ist_pos[i]; + if ((int)i > max_band[i % 3] && ipos < max_pos) + { + float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1; + if (DRMP3_HDR_TEST_MPEG1(hdr)) + { + kl = g_pan[2*ipos]; + kr = g_pan[2*ipos + 1]; + } else + { + kl = 1; + kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh); + if (ipos & 1) + { + kl = kr; + kr = 1; + } + } + drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s); + } else if (DRMP3_HDR_TEST_MS_STEREO(hdr)) + { + drmp3_L3_midside_stereo(left, sfb[i]); + } + left += sfb[i]; + } +} + +static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr) +{ + int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb; + int i, max_blocks = gr->n_short_sfb ? 3 : 1; + + drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band); + if (gr->n_long_sfb) + { + max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]); + } + for (i = 0; i < max_blocks; i++) + { + int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0; + int itop = n_sfb - max_blocks + i; + int prev = itop - max_blocks; + ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]); + } + drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1); +} + +static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb) +{ + int i, len; + float *src = grbuf, *dst = scratch; + + for (;0 != (len = *sfb); sfb += 3, src += 2*len) + { + for (i = 0; i < len; i++, src++) + { + *dst++ = src[0*len]; + *dst++ = src[1*len]; + *dst++ = src[2*len]; + } + } + DRMP3_COPY_MEMORY(grbuf, scratch, (dst - scratch)*sizeof(float)); +} + +static void drmp3_L3_antialias(float *grbuf, int nbands) +{ + static const float g_aa[2][8] = { + {0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f}, + {0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f} + }; + + for (; nbands > 0; nbands--, grbuf += 18) + { + int i = 0; +#if DRMP3_HAVE_SIMD + if (drmp3_have_simd()) for (; i < 8; i += 4) + { + drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i); + drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i); + drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i); + drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i); + vd = DRMP3_VREV(vd); + DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1))); + vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0)); + DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd)); + } +#endif +#ifndef DR_MP3_ONLY_SIMD + for(; i < 8; i++) + { + float u = grbuf[18 + i]; + float d = grbuf[17 - i]; + grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i]; + grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i]; + } +#endif + } +} + +static void drmp3_L3_dct3_9(float *y) +{ + float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4; + + s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8]; + t0 = s0 + s6*0.5f; + s0 -= s6; + t4 = (s4 + s2)*0.93969262f; + t2 = (s8 + s2)*0.76604444f; + s6 = (s4 - s8)*0.17364818f; + s4 += s8 - s2; + + s2 = s0 - s4*0.5f; + y[4] = s4 + s0; + s8 = t0 - t2 + s6; + s0 = t0 - t4 + t2; + s4 = t0 + t4 - s6; + + s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7]; + + s3 *= 0.86602540f; + t0 = (s5 + s1)*0.98480775f; + t4 = (s5 - s7)*0.34202014f; + t2 = (s1 + s7)*0.64278761f; + s1 = (s1 - s5 - s7)*0.86602540f; + + s5 = t0 - s3 - t2; + s7 = t4 - s3 - t0; + s3 = t4 + s3 - t2; + + y[0] = s4 - s7; + y[1] = s2 + s1; + y[2] = s0 - s3; + y[3] = s8 + s5; + y[5] = s8 - s5; + y[6] = s0 + s3; + y[7] = s2 - s1; + y[8] = s4 + s7; +} + +static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands) +{ + int i, j; + static const float g_twid9[18] = { + 0.73727734f,0.79335334f,0.84339145f,0.88701083f,0.92387953f,0.95371695f,0.97629601f,0.99144486f,0.99904822f,0.67559021f,0.60876143f,0.53729961f,0.46174861f,0.38268343f,0.30070580f,0.21643961f,0.13052619f,0.04361938f + }; + + for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9) + { + float co[9], si[9]; + co[0] = -grbuf[0]; + si[0] = grbuf[17]; + for (i = 0; i < 4; i++) + { + si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2]; + co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2]; + si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3]; + co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]); + } + drmp3_L3_dct3_9(co); + drmp3_L3_dct3_9(si); + + si[1] = -si[1]; + si[3] = -si[3]; + si[5] = -si[5]; + si[7] = -si[7]; + + i = 0; + +#if DRMP3_HAVE_SIMD + if (drmp3_have_simd()) for (; i < 8; i += 4) + { + drmp3_f4 vovl = DRMP3_VLD(overlap + i); + drmp3_f4 vc = DRMP3_VLD(co + i); + drmp3_f4 vs = DRMP3_VLD(si + i); + drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i); + drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i); + drmp3_f4 vw0 = DRMP3_VLD(window + i); + drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i); + drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0)); + DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1))); + DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1))); + vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0)); + DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum)); + } +#endif + for (; i < 9; i++) + { + float ovl = overlap[i]; + float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i]; + overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i]; + grbuf[i] = ovl*window[0 + i] - sum*window[9 + i]; + grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i]; + } + } +} + +static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst) +{ + float m1 = x1*0.86602540f; + float a1 = x0 - x2*0.5f; + dst[1] = x0 + x2; + dst[0] = a1 + m1; + dst[2] = a1 - m1; +} + +static void drmp3_L3_imdct12(float *x, float *dst, float *overlap) +{ + static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f }; + float co[3], si[3]; + int i; + + drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co); + drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si); + si[1] = -si[1]; + + for (i = 0; i < 3; i++) + { + float ovl = overlap[i]; + float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i]; + overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i]; + dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i]; + dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i]; + } +} + +static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands) +{ + for (;nbands > 0; nbands--, overlap += 9, grbuf += 18) + { + float tmp[18]; + DRMP3_COPY_MEMORY(tmp, grbuf, sizeof(tmp)); + DRMP3_COPY_MEMORY(grbuf, overlap, 6*sizeof(float)); + drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6); + drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6); + drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6); + } +} + +static void drmp3_L3_change_sign(float *grbuf) +{ + int b, i; + for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36) + for (i = 1; i < 18; i += 2) + grbuf[i] = -grbuf[i]; +} + +static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands) +{ + static const float g_mdct_window[2][18] = { + { 0.99904822f,0.99144486f,0.97629601f,0.95371695f,0.92387953f,0.88701083f,0.84339145f,0.79335334f,0.73727734f,0.04361938f,0.13052619f,0.21643961f,0.30070580f,0.38268343f,0.46174861f,0.53729961f,0.60876143f,0.67559021f }, + { 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f } + }; + if (n_long_bands) + { + drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands); + grbuf += 18*n_long_bands; + overlap += 9*n_long_bands; + } + if (block_type == DRMP3_SHORT_BLOCK_TYPE) + drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands); + else + drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands); +} + +static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s) +{ + int pos = (s->bs.pos + 7)/8u; + int remains = s->bs.limit/8u - pos; + if (remains > DRMP3_MAX_BITRESERVOIR_BYTES) + { + pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES; + remains = DRMP3_MAX_BITRESERVOIR_BYTES; + } + if (remains > 0) + { + DRMP3_MOVE_MEMORY(h->reserv_buf, s->maindata + pos, remains); + } + h->reserv = remains; +} + +static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin) +{ + int frame_bytes = (bs->limit - bs->pos)/8; + int bytes_have = DRMP3_MIN(h->reserv, main_data_begin); + DRMP3_COPY_MEMORY(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin)); + DRMP3_COPY_MEMORY(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes); + drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes); + return h->reserv >= main_data_begin; +} + +static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch) +{ + int ch; + + for (ch = 0; ch < nch; ch++) + { + int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length; + drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch); + drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit); + } + + if (DRMP3_HDR_TEST_I_STEREO(h->header)) + { + drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header); + } else if (DRMP3_HDR_IS_MS_STEREO(h->header)) + { + drmp3_L3_midside_stereo(s->grbuf[0], 576); + } + + for (ch = 0; ch < nch; ch++, gr_info++) + { + int aa_bands = 31; + int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2); + + if (gr_info->n_short_sfb) + { + aa_bands = n_long_bands - 1; + drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb); + } + + drmp3_L3_antialias(s->grbuf[ch], aa_bands); + drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands); + drmp3_L3_change_sign(s->grbuf[ch]); + } +} + +static void drmp3d_DCT_II(float *grbuf, int n) +{ + static const float g_sec[24] = { + 10.19000816f,0.50060302f,0.50241929f,3.40760851f,0.50547093f,0.52249861f,2.05778098f,0.51544732f,0.56694406f,1.48416460f,0.53104258f,0.64682180f,1.16943991f,0.55310392f,0.78815460f,0.97256821f,0.58293498f,1.06067765f,0.83934963f,0.62250412f,1.72244716f,0.74453628f,0.67480832f,5.10114861f + }; + int i, k = 0; +#if DRMP3_HAVE_SIMD + if (drmp3_have_simd()) for (; k < n; k += 4) + { + drmp3_f4 t[4][8], *x; + float *y = grbuf + k; + + for (x = t[0], i = 0; i < 8; i++, x++) + { + drmp3_f4 x0 = DRMP3_VLD(&y[i*18]); + drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]); + drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]); + drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]); + drmp3_f4 t0 = DRMP3_VADD(x0, x3); + drmp3_f4 t1 = DRMP3_VADD(x1, x2); + drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]); + drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]); + x[0] = DRMP3_VADD(t0, t1); + x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]); + x[16] = DRMP3_VADD(t3, t2); + x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]); + } + for (x = t[0], i = 0; i < 4; i++, x += 8) + { + drmp3_f4 x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt; + xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7); + x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6); + x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5); + x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4); + x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3); + x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2); + x[0] = DRMP3_VADD(x0, x1); + x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f); + x5 = DRMP3_VADD(x5, x6); + x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f); + x7 = DRMP3_VADD(x7, xt); + x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f); + x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); /* rotate by PI/8 */ + x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f)); + x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); + x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6); + x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f); + x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f); + x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f); + x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f); + x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f); + x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f); + } + + if (k > n - 3) + { +#if DRMP3_HAVE_SSE +#define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v) +#else +#define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[(i)*18], vget_low_f32(v)) +#endif + for (i = 0; i < 7; i++, y += 4*18) + { + drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]); + DRMP3_VSAVE2(0, t[0][i]); + DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s)); + DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1])); + DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s)); + } + DRMP3_VSAVE2(0, t[0][7]); + DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7])); + DRMP3_VSAVE2(2, t[1][7]); + DRMP3_VSAVE2(3, t[3][7]); + } else + { +#define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[(i)*18], v) + for (i = 0; i < 7; i++, y += 4*18) + { + drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]); + DRMP3_VSAVE4(0, t[0][i]); + DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s)); + DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1])); + DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s)); + } + DRMP3_VSAVE4(0, t[0][7]); + DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7])); + DRMP3_VSAVE4(2, t[1][7]); + DRMP3_VSAVE4(3, t[3][7]); + } + } else +#endif +#ifdef DR_MP3_ONLY_SIMD + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ +#else + for (; k < n; k++) + { + float t[4][8], *x, *y = grbuf + k; + + for (x = t[0], i = 0; i < 8; i++, x++) + { + float x0 = y[i*18]; + float x1 = y[(15 - i)*18]; + float x2 = y[(16 + i)*18]; + float x3 = y[(31 - i)*18]; + float t0 = x0 + x3; + float t1 = x1 + x2; + float t2 = (x1 - x2)*g_sec[3*i + 0]; + float t3 = (x0 - x3)*g_sec[3*i + 1]; + x[0] = t0 + t1; + x[8] = (t0 - t1)*g_sec[3*i + 2]; + x[16] = t3 + t2; + x[24] = (t3 - t2)*g_sec[3*i + 2]; + } + for (x = t[0], i = 0; i < 4; i++, x += 8) + { + float x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt; + xt = x0 - x7; x0 += x7; + x7 = x1 - x6; x1 += x6; + x6 = x2 - x5; x2 += x5; + x5 = x3 - x4; x3 += x4; + x4 = x0 - x3; x0 += x3; + x3 = x1 - x2; x1 += x2; + x[0] = x0 + x1; + x[4] = (x0 - x1)*0.70710677f; + x5 = x5 + x6; + x6 = (x6 + x7)*0.70710677f; + x7 = x7 + xt; + x3 = (x3 + x4)*0.70710677f; + x5 -= x7*0.198912367f; /* rotate by PI/8 */ + x7 += x5*0.382683432f; + x5 -= x7*0.198912367f; + x0 = xt - x6; xt += x6; + x[1] = (xt + x7)*0.50979561f; + x[2] = (x4 + x3)*0.54119611f; + x[3] = (x0 - x5)*0.60134488f; + x[5] = (x0 + x5)*0.89997619f; + x[6] = (x4 - x3)*1.30656302f; + x[7] = (xt - x7)*2.56291556f; + + } + for (i = 0; i < 7; i++, y += 4*18) + { + y[0*18] = t[0][i]; + y[1*18] = t[2][i] + t[3][i] + t[3][i + 1]; + y[2*18] = t[1][i] + t[1][i + 1]; + y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1]; + } + y[0*18] = t[0][7]; + y[1*18] = t[2][7] + t[3][7]; + y[2*18] = t[1][7]; + y[3*18] = t[3][7]; + } +#endif +} + +#ifndef DR_MP3_FLOAT_OUTPUT +typedef drmp3_int16 drmp3d_sample_t; + +static drmp3_int16 drmp3d_scale_pcm(float sample) +{ + drmp3_int16 s; +#if DRMP3_HAVE_ARMV6 + drmp3_int32 s32 = (drmp3_int32)(sample + .5f); + s32 -= (s32 < 0); + s = (drmp3_int16)drmp3_clip_int16_arm(s32); +#else + if (sample >= 32766.5f) return (drmp3_int16) 32767; + if (sample <= -32767.5f) return (drmp3_int16)-32768; + s = (drmp3_int16)(sample + .5f); + s -= (s < 0); /* away from zero, to be compliant */ +#endif + return s; +} +#else +typedef float drmp3d_sample_t; + +static float drmp3d_scale_pcm(float sample) +{ + return sample*(1.f/32768.f); +} +#endif + +static void drmp3d_synth_pair(drmp3d_sample_t *pcm, int nch, const float *z) +{ + float a; + a = (z[14*64] - z[ 0]) * 29; + a += (z[ 1*64] + z[13*64]) * 213; + a += (z[12*64] - z[ 2*64]) * 459; + a += (z[ 3*64] + z[11*64]) * 2037; + a += (z[10*64] - z[ 4*64]) * 5153; + a += (z[ 5*64] + z[ 9*64]) * 6574; + a += (z[ 8*64] - z[ 6*64]) * 37489; + a += z[ 7*64] * 75038; + pcm[0] = drmp3d_scale_pcm(a); + + z += 2; + a = z[14*64] * 104; + a += z[12*64] * 1567; + a += z[10*64] * 9727; + a += z[ 8*64] * 64019; + a += z[ 6*64] * -9975; + a += z[ 4*64] * -45; + a += z[ 2*64] * 146; + a += z[ 0*64] * -5; + pcm[16*nch] = drmp3d_scale_pcm(a); +} + +static void drmp3d_synth(float *xl, drmp3d_sample_t *dstl, int nch, float *lins) +{ + int i; + float *xr = xl + 576*(nch - 1); + drmp3d_sample_t *dstr = dstl + (nch - 1); + + static const float g_win[] = { + -1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992, + -1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856, + -1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630, + -1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313, + -1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908, + -1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415, + -2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835, + -2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169, + -2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420, + -2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590, + -3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679, + -3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692, + -4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629, + -4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494, + -5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290 + }; + float *zlin = lins + 15*64; + const float *w = g_win; + + zlin[4*15] = xl[18*16]; + zlin[4*15 + 1] = xr[18*16]; + zlin[4*15 + 2] = xl[0]; + zlin[4*15 + 3] = xr[0]; + + zlin[4*31] = xl[1 + 18*16]; + zlin[4*31 + 1] = xr[1 + 18*16]; + zlin[4*31 + 2] = xl[1]; + zlin[4*31 + 3] = xr[1]; + + drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1); + drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1); + drmp3d_synth_pair(dstl, nch, lins + 4*15); + drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64); + +#if DRMP3_HAVE_SIMD + if (drmp3_have_simd()) for (i = 14; i >= 0; i--) + { +#define DRMP3_VLOAD(k) drmp3_f4 w0 = DRMP3_VSET(*w++); drmp3_f4 w1 = DRMP3_VSET(*w++); drmp3_f4 vz = DRMP3_VLD(&zlin[4*i - 64*k]); drmp3_f4 vy = DRMP3_VLD(&zlin[4*i - 64*(15 - k)]); +#define DRMP3_V0(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0)) ; a = DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1)); } +#define DRMP3_V1(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1))); } +#define DRMP3_V2(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vy, w1), DRMP3_VMUL(vz, w0))); } + drmp3_f4 a, b; + zlin[4*i] = xl[18*(31 - i)]; + zlin[4*i + 1] = xr[18*(31 - i)]; + zlin[4*i + 2] = xl[1 + 18*(31 - i)]; + zlin[4*i + 3] = xr[1 + 18*(31 - i)]; + zlin[4*i + 64] = xl[1 + 18*(1 + i)]; + zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)]; + zlin[4*i - 64 + 2] = xl[18*(1 + i)]; + zlin[4*i - 64 + 3] = xr[18*(1 + i)]; + + DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7) + + { +#ifndef DR_MP3_FLOAT_OUTPUT +#if DRMP3_HAVE_SSE + static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f }; + static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f }; + __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)), + _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min))); + dstr[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 1); + dstr[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 5); + dstl[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 0); + dstl[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 4); + dstr[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 3); + dstr[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 7); + dstl[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 2); + dstl[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 6); +#else + int16x4_t pcma, pcmb; + a = DRMP3_VADD(a, DRMP3_VSET(0.5f)); + b = DRMP3_VADD(b, DRMP3_VSET(0.5f)); + pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0))))); + pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0))))); + vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1); + vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1); + vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0); + vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0); + vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3); + vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3); + vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2); + vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2); +#endif +#else + #if DRMP3_HAVE_SSE + static const drmp3_f4 g_scale = { 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f }; + #else + const drmp3_f4 g_scale = vdupq_n_f32(1.0f/32768.0f); + #endif + a = DRMP3_VMUL(a, g_scale); + b = DRMP3_VMUL(b, g_scale); +#if DRMP3_HAVE_SSE + _mm_store_ss(dstr + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 1, 1, 1))); + _mm_store_ss(dstr + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(1, 1, 1, 1))); + _mm_store_ss(dstl + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 0, 0, 0))); + _mm_store_ss(dstl + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(0, 0, 0, 0))); + _mm_store_ss(dstr + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 3, 3, 3))); + _mm_store_ss(dstr + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 3, 3, 3))); + _mm_store_ss(dstl + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 2, 2, 2))); + _mm_store_ss(dstl + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(2, 2, 2, 2))); +#else + vst1q_lane_f32(dstr + (15 - i)*nch, a, 1); + vst1q_lane_f32(dstr + (17 + i)*nch, b, 1); + vst1q_lane_f32(dstl + (15 - i)*nch, a, 0); + vst1q_lane_f32(dstl + (17 + i)*nch, b, 0); + vst1q_lane_f32(dstr + (47 - i)*nch, a, 3); + vst1q_lane_f32(dstr + (49 + i)*nch, b, 3); + vst1q_lane_f32(dstl + (47 - i)*nch, a, 2); + vst1q_lane_f32(dstl + (49 + i)*nch, b, 2); +#endif +#endif /* DR_MP3_FLOAT_OUTPUT */ + } + } else +#endif +#ifdef DR_MP3_ONLY_SIMD + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ +#else + for (i = 14; i >= 0; i--) + { +#define DRMP3_LOAD(k) float w0 = *w++; float w1 = *w++; float *vz = &zlin[4*i - k*64]; float *vy = &zlin[4*i - (15 - k)*64]; +#define DRMP3_S0(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] = vz[j]*w1 + vy[j]*w0, a[j] = vz[j]*w0 - vy[j]*w1; } +#define DRMP3_S1(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vz[j]*w0 - vy[j]*w1; } +#define DRMP3_S2(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vy[j]*w1 - vz[j]*w0; } + float a[4], b[4]; + + zlin[4*i] = xl[18*(31 - i)]; + zlin[4*i + 1] = xr[18*(31 - i)]; + zlin[4*i + 2] = xl[1 + 18*(31 - i)]; + zlin[4*i + 3] = xr[1 + 18*(31 - i)]; + zlin[4*(i + 16)] = xl[1 + 18*(1 + i)]; + zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)]; + zlin[4*(i - 16) + 2] = xl[18*(1 + i)]; + zlin[4*(i - 16) + 3] = xr[18*(1 + i)]; + + DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7) + + dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]); + dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]); + dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]); + dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]); + dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]); + dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]); + dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]); + dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]); + } +#endif +} + +static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, drmp3d_sample_t *pcm, float *lins) +{ + int i; + for (i = 0; i < nch; i++) + { + drmp3d_DCT_II(grbuf + 576*i, nbands); + } + + DRMP3_COPY_MEMORY(lins, qmf_state, sizeof(float)*15*64); + + for (i = 0; i < nbands; i += 2) + { + drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64); + } +#ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL + if (nch == 1) + { + for (i = 0; i < 15*64; i += 2) + { + qmf_state[i] = lins[nbands*64 + i]; + } + } else +#endif + { + DRMP3_COPY_MEMORY(qmf_state, lins + nbands*64, sizeof(float)*15*64); + } +} + +static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes) +{ + int i, nmatch; + for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++) + { + i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i); + if (i + DRMP3_HDR_SIZE > mp3_bytes) + return nmatch > 0; + if (!drmp3_hdr_compare(hdr, hdr + i)) + return 0; + } + return 1; +} + +static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes) +{ + int i, k; + for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++) + { + if (drmp3_hdr_valid(mp3)) + { + int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes); + int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3); + + for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++) + { + if (drmp3_hdr_compare(mp3, mp3 + k)) + { + int fb = k - drmp3_hdr_padding(mp3); + int nextfb = fb + drmp3_hdr_padding(mp3 + k); + if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb)) + continue; + frame_and_padding = k; + frame_bytes = fb; + *free_format_bytes = fb; + } + } + + if ((frame_bytes && i + frame_and_padding <= mp3_bytes && + drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) || + (!i && frame_and_padding == mp3_bytes)) + { + *ptr_frame_bytes = frame_and_padding; + return i; + } + *free_format_bytes = 0; + } + } + *ptr_frame_bytes = 0; + return mp3_bytes; +} + +DRMP3_API void drmp3dec_init(drmp3dec *dec) +{ + dec->header[0] = 0; +} + +DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info) +{ + int i = 0, igr, frame_size = 0, success = 1; + const drmp3_uint8 *hdr; + drmp3_bs bs_frame[1]; + + if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3)) + { + frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3); + if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size))) + { + frame_size = 0; + } + } + if (!frame_size) + { + DRMP3_ZERO_MEMORY(dec, sizeof(drmp3dec)); + i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size); + if (!frame_size || i + frame_size > mp3_bytes) + { + info->frame_bytes = i; + return 0; + } + } + + hdr = mp3 + i; + DRMP3_COPY_MEMORY(dec->header, hdr, DRMP3_HDR_SIZE); + info->frame_bytes = i + frame_size; + info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2; + info->sample_rate = drmp3_hdr_sample_rate_hz(hdr); + info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr); + info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr); + + drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE); + if (DRMP3_HDR_IS_CRC(hdr)) + { + drmp3_bs_get_bits(bs_frame, 16); + } + + if (info->layer == 3) + { + int main_data_begin = drmp3_L3_read_side_info(bs_frame, dec->scratch.gr_info, hdr); + if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit) + { + drmp3dec_init(dec); + return 0; + } + success = drmp3_L3_restore_reservoir(dec, bs_frame, &dec->scratch, main_data_begin); + if (success && pcm != NULL) + { + for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*576*info->channels)) + { + DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); + drmp3_L3_decode(dec, &dec->scratch, dec->scratch.gr_info + igr*info->channels, info->channels); + drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]); + } + } + drmp3_L3_save_reservoir(dec, &dec->scratch); + } else + { +#ifdef DR_MP3_ONLY_MP3 + return 0; +#else + drmp3_L12_scale_info sci[1]; + + if (pcm == NULL) { + return drmp3_hdr_frame_samples(hdr); + } + + drmp3_L12_read_scale_info(hdr, bs_frame, sci); + + DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); + for (i = 0, igr = 0; igr < 3; igr++) + { + if (12 == (i += drmp3_L12_dequantize_granule(dec->scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1))) + { + i = 0; + drmp3_L12_apply_scf_384(sci, sci->scf + igr, dec->scratch.grbuf[0]); + drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]); + DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); + pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels); + } + if (bs_frame->pos > bs_frame->limit) + { + drmp3dec_init(dec); + return 0; + } + } +#endif + } + + return success*drmp3_hdr_frame_samples(dec->header); +} + +DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples) +{ + size_t i = 0; +#if DRMP3_HAVE_SIMD + size_t aligned_count = num_samples & ~7; + for(; i < aligned_count; i+=8) + { + drmp3_f4 scale = DRMP3_VSET(32768.0f); + drmp3_f4 a = DRMP3_VMUL(DRMP3_VLD(&in[i ]), scale); + drmp3_f4 b = DRMP3_VMUL(DRMP3_VLD(&in[i+4]), scale); +#if DRMP3_HAVE_SSE + drmp3_f4 s16max = DRMP3_VSET( 32767.0f); + drmp3_f4 s16min = DRMP3_VSET(-32768.0f); + __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, s16max), s16min)), + _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, s16max), s16min))); + out[i ] = (drmp3_int16)_mm_extract_epi16(pcm8, 0); + out[i+1] = (drmp3_int16)_mm_extract_epi16(pcm8, 1); + out[i+2] = (drmp3_int16)_mm_extract_epi16(pcm8, 2); + out[i+3] = (drmp3_int16)_mm_extract_epi16(pcm8, 3); + out[i+4] = (drmp3_int16)_mm_extract_epi16(pcm8, 4); + out[i+5] = (drmp3_int16)_mm_extract_epi16(pcm8, 5); + out[i+6] = (drmp3_int16)_mm_extract_epi16(pcm8, 6); + out[i+7] = (drmp3_int16)_mm_extract_epi16(pcm8, 7); +#else + int16x4_t pcma, pcmb; + a = DRMP3_VADD(a, DRMP3_VSET(0.5f)); + b = DRMP3_VADD(b, DRMP3_VSET(0.5f)); + pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0))))); + pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0))))); + vst1_lane_s16(out+i , pcma, 0); + vst1_lane_s16(out+i+1, pcma, 1); + vst1_lane_s16(out+i+2, pcma, 2); + vst1_lane_s16(out+i+3, pcma, 3); + vst1_lane_s16(out+i+4, pcmb, 0); + vst1_lane_s16(out+i+5, pcmb, 1); + vst1_lane_s16(out+i+6, pcmb, 2); + vst1_lane_s16(out+i+7, pcmb, 3); +#endif + } +#endif + for(; i < num_samples; i++) + { + float sample = in[i] * 32768.0f; + if (sample >= 32766.5f) + out[i] = (drmp3_int16) 32767; + else if (sample <= -32767.5f) + out[i] = (drmp3_int16)-32768; + else + { + short s = (drmp3_int16)(sample + .5f); + s -= (s < 0); /* away from zero, to be compliant */ + out[i] = s; + } + } +} + + + +/************************************************************************************************************************************************************ + + Main Public API + + ************************************************************************************************************************************************************/ +/* SIZE_MAX */ +#if defined(SIZE_MAX) + #define DRMP3_SIZE_MAX SIZE_MAX +#else + #if defined(_WIN64) || defined(_LP64) || defined(__LP64__) + #define DRMP3_SIZE_MAX ((drmp3_uint64)0xFFFFFFFFFFFFFFFF) + #else + #define DRMP3_SIZE_MAX 0xFFFFFFFF + #endif +#endif +/* End SIZE_MAX */ + +/* Options. */ +#ifndef DRMP3_SEEK_LEADING_MP3_FRAMES +#define DRMP3_SEEK_LEADING_MP3_FRAMES 2 +#endif + +#define DRMP3_MIN_DATA_CHUNK_SIZE 16384 + +/* The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends at least 16K, but in an attempt to reduce data movement I'm making this slightly larger. */ +#ifndef DRMP3_DATA_CHUNK_SIZE +#define DRMP3_DATA_CHUNK_SIZE (DRMP3_MIN_DATA_CHUNK_SIZE*4) +#endif + + +#define DRMP3_COUNTOF(x) (sizeof(x) / sizeof(x[0])) +#define DRMP3_CLAMP(x, lo, hi) (DRMP3_MAX(lo, DRMP3_MIN(x, hi))) + +#ifndef DRMP3_PI_D +#define DRMP3_PI_D 3.14159265358979323846264 +#endif + +#define DRMP3_DEFAULT_RESAMPLER_LPF_ORDER 2 + +static DRMP3_INLINE float drmp3_mix_f32(float x, float y, float a) +{ + return x*(1-a) + y*a; +} +static DRMP3_INLINE float drmp3_mix_f32_fast(float x, float y, float a) +{ + float r0 = (y - x); + float r1 = r0*a; + return x + r1; + /*return x + (y - x)*a;*/ +} + + +/* +Greatest common factor using Euclid's algorithm iteratively. +*/ +static DRMP3_INLINE drmp3_uint32 drmp3_gcf_u32(drmp3_uint32 a, drmp3_uint32 b) +{ + for (;;) { + if (b == 0) { + break; + } else { + drmp3_uint32 t = a; + a = b; + b = t % a; + } + } + + return a; +} + + +static void* drmp3__malloc_default(size_t sz, void* pUserData) +{ + (void)pUserData; + return DRMP3_MALLOC(sz); +} + +static void* drmp3__realloc_default(void* p, size_t sz, void* pUserData) +{ + (void)pUserData; + return DRMP3_REALLOC(p, sz); +} + +static void drmp3__free_default(void* p, void* pUserData) +{ + (void)pUserData; + DRMP3_FREE(p); +} + + +static void* drmp3__malloc_from_callbacks(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks == NULL) { + return NULL; + } + + if (pAllocationCallbacks->onMalloc != NULL) { + return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData); + } + + /* Try using realloc(). */ + if (pAllocationCallbacks->onRealloc != NULL) { + return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData); + } + + return NULL; +} + +static void* drmp3__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks == NULL) { + return NULL; + } + + if (pAllocationCallbacks->onRealloc != NULL) { + return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData); + } + + /* Try emulating realloc() in terms of malloc()/free(). */ + if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) { + void* p2; + + p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData); + if (p2 == NULL) { + return NULL; + } + + if (p != NULL) { + DRMP3_COPY_MEMORY(p2, p, szOld); + pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); + } + + return p2; + } + + return NULL; +} + +static void drmp3__free_from_callbacks(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (p == NULL || pAllocationCallbacks == NULL) { + return; + } + + if (pAllocationCallbacks->onFree != NULL) { + pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); + } +} + + +static drmp3_allocation_callbacks drmp3_copy_allocation_callbacks_or_defaults(const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks != NULL) { + /* Copy. */ + return *pAllocationCallbacks; + } else { + /* Defaults. */ + drmp3_allocation_callbacks allocationCallbacks; + allocationCallbacks.pUserData = NULL; + allocationCallbacks.onMalloc = drmp3__malloc_default; + allocationCallbacks.onRealloc = drmp3__realloc_default; + allocationCallbacks.onFree = drmp3__free_default; + return allocationCallbacks; + } +} + + + +static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead) +{ + size_t bytesRead; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->onRead != NULL); + + /* + Don't try reading 0 bytes from the callback. This can happen when the stream is clamped against + ID3v1 or APE tags at the end of the stream. + */ + if (bytesToRead == 0) { + return 0; + } + + bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead); + pMP3->streamCursor += bytesRead; + + return bytesRead; +} + +static size_t drmp3__on_read_clamped(drmp3* pMP3, void* pBufferOut, size_t bytesToRead) +{ + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->onRead != NULL); + + if (pMP3->streamLength == DRMP3_UINT64_MAX) { + return drmp3__on_read(pMP3, pBufferOut, bytesToRead); + } else { + drmp3_uint64 bytesRemaining; + + bytesRemaining = (pMP3->streamLength - pMP3->streamCursor); + if (bytesToRead > bytesRemaining) { + bytesToRead = (size_t)bytesRemaining; + } + + return drmp3__on_read(pMP3, pBufferOut, bytesToRead); + } +} + +static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin) +{ + DRMP3_ASSERT(offset >= 0); + DRMP3_ASSERT(origin == DRMP3_SEEK_SET || origin == DRMP3_SEEK_CUR); + + if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) { + return DRMP3_FALSE; + } + + if (origin == DRMP3_SEEK_SET) { + pMP3->streamCursor = (drmp3_uint64)offset; + } else{ + pMP3->streamCursor += offset; + } + + return DRMP3_TRUE; +} + +static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_seek_origin origin) +{ + if (offset <= 0x7FFFFFFF) { + return drmp3__on_seek(pMP3, (int)offset, origin); + } + + /* Getting here "offset" is too large for a 32-bit integer. We just keep seeking forward until we hit the offset. */ + if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; + } + + offset -= 0x7FFFFFFF; + while (offset > 0) { + if (offset <= 0x7FFFFFFF) { + if (!drmp3__on_seek(pMP3, (int)offset, DRMP3_SEEK_CUR)) { + return DRMP3_FALSE; + } + offset = 0; + } else { + if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_CUR)) { + return DRMP3_FALSE; + } + offset -= 0x7FFFFFFF; + } + } + + return DRMP3_TRUE; +} + +static void drmp3__on_meta(drmp3* pMP3, drmp3_metadata_type type, const void* pRawData, size_t rawDataSize) +{ + if (pMP3->onMeta) { + drmp3_metadata metadata; + + DRMP3_ZERO_OBJECT(&metadata); + metadata.type = type; + metadata.pRawData = pRawData; + metadata.rawDataSize = rawDataSize; + + pMP3->onMeta(pMP3->pUserDataMeta, &metadata); + } +} + + +static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) +{ + drmp3_uint32 pcmFramesRead = 0; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->onRead != NULL); + + if (pMP3->atEnd) { + return 0; + } + + for (;;) { + drmp3dec_frame_info info; + + /* minimp3 recommends doing data submission in chunks of at least 16K. If we don't have at least 16K bytes available, get more. */ + if (pMP3->dataSize < DRMP3_MIN_DATA_CHUNK_SIZE) { + size_t bytesRead; + + /* First we need to move the data down. */ + if (pMP3->pData != NULL) { + DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize); + } + + pMP3->dataConsumed = 0; + + if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) { + drmp3_uint8* pNewData; + size_t newDataCap; + + newDataCap = DRMP3_DATA_CHUNK_SIZE; + + pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks); + if (pNewData == NULL) { + return 0; /* Out of memory. */ + } + + pMP3->pData = pNewData; + pMP3->dataCapacity = newDataCap; + } + + bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize)); + if (bytesRead == 0) { + if (pMP3->dataSize == 0) { + pMP3->atEnd = DRMP3_TRUE; + return 0; /* No data. */ + } + } + + pMP3->dataSize += bytesRead; + } + + if (pMP3->dataSize > INT_MAX) { + pMP3->atEnd = DRMP3_TRUE; + return 0; /* File too big. */ + } + + DRMP3_ASSERT(pMP3->pData != NULL); + DRMP3_ASSERT(pMP3->dataCapacity > 0); + + /* Do a runtime check here to try silencing a false-positive from clang-analyzer. */ + if (pMP3->pData == NULL) { + return 0; + } + + pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData + pMP3->dataConsumed, (int)pMP3->dataSize, pPCMFrames, &info); /* <-- Safe size_t -> int conversion thanks to the check above. */ + + /* Consume the data. */ + pMP3->dataConsumed += (size_t)info.frame_bytes; + pMP3->dataSize -= (size_t)info.frame_bytes; + + /* pcmFramesRead will be equal to 0 if decoding failed. If it is zero and info.frame_bytes > 0 then we have successfully decoded the frame. */ + if (pcmFramesRead > 0) { + pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header); + pMP3->pcmFramesConsumedInMP3Frame = 0; + pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead; + pMP3->mp3FrameChannels = info.channels; + pMP3->mp3FrameSampleRate = info.sample_rate; + + if (pMP3FrameInfo != NULL) { + *pMP3FrameInfo = info; + } + + if (ppMP3FrameData != NULL) { + *ppMP3FrameData = pMP3->pData + pMP3->dataConsumed - (size_t)info.frame_bytes; + } + + break; + } else if (info.frame_bytes == 0) { + /* Need more data. minimp3 recommends doing data submission in 16K chunks. */ + size_t bytesRead; + + /* First we need to move the data down. */ + DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize); + pMP3->dataConsumed = 0; + + if (pMP3->dataCapacity == pMP3->dataSize) { + /* No room. Expand. */ + drmp3_uint8* pNewData; + size_t newDataCap; + + newDataCap = pMP3->dataCapacity + DRMP3_DATA_CHUNK_SIZE; + + pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks); + if (pNewData == NULL) { + return 0; /* Out of memory. */ + } + + pMP3->pData = pNewData; + pMP3->dataCapacity = newDataCap; + } + + /* Fill in a chunk. */ + bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize)); + if (bytesRead == 0) { + pMP3->atEnd = DRMP3_TRUE; + return 0; /* Error reading more data. */ + } + + pMP3->dataSize += bytesRead; + } + }; + + return pcmFramesRead; +} + +static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) +{ + drmp3_uint32 pcmFramesRead = 0; + drmp3dec_frame_info info; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->memory.pData != NULL); + + if (pMP3->atEnd) { + return 0; + } + + for (;;) { + pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->memory.pData + pMP3->memory.currentReadPos, (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos), pPCMFrames, &info); + if (pcmFramesRead > 0) { + pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header); + pMP3->pcmFramesConsumedInMP3Frame = 0; + pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead; + pMP3->mp3FrameChannels = info.channels; + pMP3->mp3FrameSampleRate = info.sample_rate; + + if (pMP3FrameInfo != NULL) { + *pMP3FrameInfo = info; + } + + if (ppMP3FrameData != NULL) { + *ppMP3FrameData = pMP3->memory.pData + pMP3->memory.currentReadPos; + } + + break; + } else if (info.frame_bytes > 0) { + /* No frames were read, but it looks like we skipped past one. Read the next MP3 frame. */ + pMP3->memory.currentReadPos += (size_t)info.frame_bytes; + pMP3->streamCursor += (size_t)info.frame_bytes; + } else { + /* Nothing at all was read. Abort. */ + break; + } + } + + /* Consume the data. */ + pMP3->memory.currentReadPos += (size_t)info.frame_bytes; + pMP3->streamCursor += (size_t)info.frame_bytes; + + return pcmFramesRead; +} + +static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) +{ + if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) { + return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData); + } else { + return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData); + } +} + +static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3) +{ + DRMP3_ASSERT(pMP3 != NULL); + return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, NULL, NULL); +} + +#if 0 +static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3) +{ + drmp3_uint32 pcmFrameCount; + + DRMP3_ASSERT(pMP3 != NULL); + + pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); + if (pcmFrameCount == 0) { + return 0; + } + + /* We have essentially just skipped past the frame, so just set the remaining samples to 0. */ + pMP3->currentPCMFrame += pcmFrameCount; + pMP3->pcmFramesConsumedInMP3Frame = pcmFrameCount; + pMP3->pcmFramesRemainingInMP3Frame = 0; + + return pcmFrameCount; +} +#endif + +static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3dec_frame_info firstFrameInfo; + const drmp3_uint8* pFirstFrameData; + drmp3_uint32 firstFramePCMFrameCount; + drmp3_uint32 detectedMP3FrameCount = 0xFFFFFFFF; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(onRead != NULL); + + /* This function assumes the output object has already been reset to 0. Do not do that here, otherwise things will break. */ + drmp3dec_init(&pMP3->decoder); + + pMP3->onRead = onRead; + pMP3->onSeek = onSeek; + pMP3->onMeta = onMeta; + pMP3->pUserData = pUserData; + pMP3->pUserDataMeta = pUserDataMeta; + pMP3->allocationCallbacks = drmp3_copy_allocation_callbacks_or_defaults(pAllocationCallbacks); + + if (pMP3->allocationCallbacks.onFree == NULL || (pMP3->allocationCallbacks.onMalloc == NULL && pMP3->allocationCallbacks.onRealloc == NULL)) { + return DRMP3_FALSE; /* Invalid allocation callbacks. */ + } + + pMP3->streamCursor = 0; + pMP3->streamLength = DRMP3_UINT64_MAX; + pMP3->streamStartOffset = 0; + pMP3->delayInPCMFrames = 0; + pMP3->paddingInPCMFrames = 0; + pMP3->totalPCMFrameCount = DRMP3_UINT64_MAX; + + /* We'll first check for any ID3v1 or APE tags. */ + #if 1 + if (onSeek != NULL && onTell != NULL) { + if (onSeek(pUserData, 0, DRMP3_SEEK_END)) { + drmp3_int64 streamLen; + int streamEndOffset = 0; + + /* First get the length of the stream. We need this so we can ensure the stream is big enough to store the tags. */ + if (onTell(pUserData, &streamLen)) { + /* ID3v1 */ + if (streamLen > 128) { + char id3[3]; + if (onSeek(pUserData, streamEndOffset - 128, DRMP3_SEEK_END)) { + if (onRead(pUserData, id3, 3) == 3 && id3[0] == 'T' && id3[1] == 'A' && id3[2] == 'G') { + /* We have an ID3v1 tag. */ + streamEndOffset -= 128; + streamLen -= 128; + + /* Fire a metadata callback for the TAG data. */ + if (onMeta != NULL) { + drmp3_uint8 tag[128]; + tag[0] = 'T'; tag[1] = 'A'; tag[2] = 'G'; + + if (onRead(pUserData, tag + 3, 125) == 125) { + drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V1, tag, 128); + } + } + } else { + /* No ID3v1 tag. */ + } + } else { + /* Failed to seek to the ID3v1 tag. */ + } + } else { + /* Stream too short. No ID3v1 tag. */ + } + + /* APE */ + if (streamLen > 32) { + char ape[32]; /* The footer. */ + if (onSeek(pUserData, streamEndOffset - 32, DRMP3_SEEK_END)) { + if (onRead(pUserData, ape, 32) == 32 && ape[0] == 'A' && ape[1] == 'P' && ape[2] == 'E' && ape[3] == 'T' && ape[4] == 'A' && ape[5] == 'G' && ape[6] == 'E' && ape[7] == 'X') { + /* We have an APE tag. */ + drmp3_uint32 tagSize = + ((drmp3_uint32)ape[24] << 0) | + ((drmp3_uint32)ape[25] << 8) | + ((drmp3_uint32)ape[26] << 16) | + ((drmp3_uint32)ape[27] << 24); + + if (32 + tagSize < streamLen) { + streamEndOffset -= 32 + tagSize; + streamLen -= 32 + tagSize; + + /* Fire a metadata callback for the APE data. Must include both the main content and footer. */ + if (onMeta != NULL) { + /* We first need to seek to the start of the APE tag. */ + if (onSeek(pUserData, streamEndOffset, DRMP3_SEEK_END)) { + size_t apeTagSize = (size_t)tagSize + 32; + drmp3_uint8* pTagData = (drmp3_uint8*)drmp3_malloc(apeTagSize, pAllocationCallbacks); + if (pTagData != NULL) { + if (onRead(pUserData, pTagData, apeTagSize) == apeTagSize) { + drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_APE, pTagData, apeTagSize); + } + + drmp3_free(pTagData, pAllocationCallbacks); + } + } + } + } else { + /* The tag size is larger than the stream. Invalid APE tag. */ + } + } + } + } else { + /* Stream too short. No APE tag. */ + } + + /* Seek back to the start. */ + if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; /* Failed to seek back to the start. */ + } + + pMP3->streamLength = (drmp3_uint64)streamLen; + + if (pMP3->memory.pData != NULL) { + pMP3->memory.dataSize = (size_t)pMP3->streamLength; + } + } else { + /* Failed to get the length of the stream. ID3v1 and APE tags cannot be skipped. */ + if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; /* Failed to seek back to the start. */ + } + } + } else { + /* Failed to seek to the end. Cannot skip ID3v1 or APE tags. */ + } + } else { + /* No onSeek or onTell callback. Cannot skip ID3v1 or APE tags. */ + } + #endif + + + /* ID3v2 tags */ + #if 1 + { + char header[10]; + if (onRead(pUserData, header, 10) == 10) { + if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') { + drmp3_uint32 tagSize = + (((drmp3_uint32)header[6] & 0x7F) << 21) | + (((drmp3_uint32)header[7] & 0x7F) << 14) | + (((drmp3_uint32)header[8] & 0x7F) << 7) | + (((drmp3_uint32)header[9] & 0x7F) << 0); + + /* Account for the footer. */ + if (header[5] & 0x10) { + tagSize += 10; + } + + /* Read the tag content and fire a metadata callback. */ + if (onMeta != NULL) { + size_t tagSizeWithHeader = 10 + tagSize; + drmp3_uint8* pTagData = (drmp3_uint8*)drmp3_malloc(tagSizeWithHeader, pAllocationCallbacks); + if (pTagData != NULL) { + DRMP3_COPY_MEMORY(pTagData, header, 10); + + if (onRead(pUserData, pTagData + 10, tagSize) == tagSize) { + drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V2, pTagData, tagSizeWithHeader); + } + + drmp3_free(pTagData, pAllocationCallbacks); + } + } else { + /* Don't have a metadata callback, so just skip the tag. */ + if (onSeek != NULL) { + if (!onSeek(pUserData, tagSize, DRMP3_SEEK_CUR)) { + return DRMP3_FALSE; /* Failed to seek past the ID3v2 tag. */ + } + } else { + /* Don't have a seek callback. Read and discard. */ + char discard[1024]; + + while (tagSize > 0) { + size_t bytesToRead = tagSize; + if (bytesToRead > sizeof(discard)) { + bytesToRead = sizeof(discard); + } + + if (onRead(pUserData, discard, bytesToRead) != bytesToRead) { + return DRMP3_FALSE; /* Failed to read data. */ + } + + tagSize -= (drmp3_uint32)bytesToRead; + } + } + } + + pMP3->streamStartOffset += 10 + tagSize; /* +10 for the header. */ + pMP3->streamCursor = pMP3->streamStartOffset; + } else { + /* Not an ID3v2 tag. Seek back to the start. */ + if (onSeek != NULL) { + if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; /* Failed to seek back to the start. */ + } + } else { + /* Don't have a seek callback to move backwards. We'll just fall through and let the decoding process re-sync. The ideal solution here would be to read into the cache. */ + + /* + TODO: Copy the header into the cache. Will need to allocate space. See drmp3_decode_next_frame_ex__callbacks. There is not need + to handle the memory case because that will always have a seek implementation and will never hit this code path. + */ + } + } + } else { + /* Failed to read the header. We can return false here. If we couldn't read 10 bytes there's no way we'll have a valid MP3 stream. */ + return DRMP3_FALSE; + } + } + #endif + + /* + Decode the first frame to confirm that it is indeed a valid MP3 stream. Note that it's possible the first frame + is actually a Xing/LAME/VBRI header. If this is the case we need to skip over it. + */ + firstFramePCMFrameCount = drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, &firstFrameInfo, &pFirstFrameData); + if (firstFramePCMFrameCount > 0) { + DRMP3_ASSERT(pFirstFrameData != NULL); + + /* + It might be a header. If so, we need to clear out the cached PCM frames in order to trigger a reload of fresh + data when decoding starts. We can assume all validation has already been performed to check if this is a valid + MP3 frame and that there is more than 0 bytes making up the frame. + + We're going to be basing this parsing code off the minimp3_ex implementation. + */ + #if 1 + DRMP3_ASSERT(firstFrameInfo.frame_bytes > 0); + { + drmp3_bs bs; + drmp3_L3_gr_info grInfo[4]; + + drmp3_bs_init(&bs, pFirstFrameData + DRMP3_HDR_SIZE, firstFrameInfo.frame_bytes - DRMP3_HDR_SIZE); + + if (DRMP3_HDR_IS_CRC(pFirstFrameData)) { + drmp3_bs_get_bits(&bs, 16); /* CRC. */ + } + + if (drmp3_L3_read_side_info(&bs, grInfo, pFirstFrameData) >= 0) { + drmp3_bool32 isXing = DRMP3_FALSE; + drmp3_bool32 isInfo = DRMP3_FALSE; + const drmp3_uint8* pTagData; + const drmp3_uint8* pTagDataBeg; + const void* pDataBufferEnd = NULL; + size_t frameBytes; + + pTagDataBeg = pFirstFrameData + DRMP3_HDR_SIZE + (bs.pos/8); + pTagData = pTagDataBeg; + + /* + We need to determine how many bytes are actually available in pTagData. Unfortunately this is different depending on + whether or not it's being decoded from memory or callbacks. + */ + if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) { + pDataBufferEnd = pMP3->memory.pData + pMP3->memory.dataSize; + } else { + pDataBufferEnd = pMP3->pData + pMP3->dataCapacity; + } + + frameBytes = DRMP3_MIN((size_t)firstFrameInfo.frame_bytes, (size_t)((drmp3_uint8*)pDataBufferEnd - pFirstFrameData)); + + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 8) { + goto done_xing_info; /* Frame too small for a Xing/Info tag. */ + } + + /* Check for both "Xing" and "Info" identifiers. */ + isXing = (pTagData[0] == 'X' && pTagData[1] == 'i' && pTagData[2] == 'n' && pTagData[3] == 'g'); + isInfo = (pTagData[0] == 'I' && pTagData[1] == 'n' && pTagData[2] == 'f' && pTagData[3] == 'o'); + + if (isXing || isInfo) { + drmp3_uint32 bytes = 0; + drmp3_uint32 flags = pTagData[7]; + + pTagData += 8; /* Skip past the ID and flags. */ + + if (flags & 0x01) { /* FRAMES flag. */ + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) { + goto done_xing_info; /* Invalid Xing/Info tag. */ + } + + detectedMP3FrameCount = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3]; + pTagData += 4; + } + + if (flags & 0x02) { /* BYTES flag. */ + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) { + goto done_xing_info; /* Invalid Xing/Info tag. */ + } + + bytes = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3]; + (void)bytes; /* <-- Just to silence a warning about `bytes` being assigned but unused. Want to leave this here in case I want to make use of it later. */ + pTagData += 4; + } + + if (flags & 0x04) { /* TOC flag. */ + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 100) { + goto done_xing_info; /* Invalid Xing/Info tag. */ + } + + /* TODO: Extract and bind seek points. */ + pTagData += 100; + } + + if (flags & 0x08) { /* SCALE flag. */ + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) { + goto done_xing_info; /* Invalid Xing/Info tag. */ + } + + pTagData += 4; + } + + /* At this point we're done with the Xing/Info header. Now we can look at the LAME data. */ + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 1) { + goto done_xing_info; /* Not enough data left to check for a LAME header. */ + } + + if (pTagData[0]) { + int delayInPCMFrames; + int paddingInPCMFrames; + + if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 36) { + goto done_xing_info; /* Invalid Xing/Info tag. */ + } + + pTagData += 21; + + delayInPCMFrames = (( (drmp3_uint32)pTagData[0] << 4) | ((drmp3_uint32)pTagData[1] >> 4)) + (528 + 1); + paddingInPCMFrames = ((((drmp3_uint32)pTagData[1] & 0xF) << 8) | ((drmp3_uint32)pTagData[2] )) - (528 + 1); + if (paddingInPCMFrames < 0) { + paddingInPCMFrames = 0; /* Padding cannot be negative. Probably a malformed file. Ignore. */ + } + + pMP3->delayInPCMFrames = (drmp3_uint32)delayInPCMFrames; + pMP3->paddingInPCMFrames = (drmp3_uint32)paddingInPCMFrames; + } + + /* + My understanding is that if the "Xing" header is present we can consider this to be a VBR stream and if the "Info" header is + present it's a CBR stream. If this is not the case let me know! I'm just tracking this for the time being in case I want to + look at doing some CBR optimizations later on, such as faster seeking. + */ + if (isXing) { + pMP3->isVBR = DRMP3_TRUE; + } else if (isInfo) { + pMP3->isCBR = DRMP3_TRUE; + } + + /* Post the raw data of the tag to the metadata callback. */ + if (onMeta != NULL) { + drmp3_metadata_type metadataType = isXing ? DRMP3_METADATA_TYPE_XING : DRMP3_METADATA_TYPE_VBRI; + size_t tagDataSize; + + tagDataSize = (size_t)firstFrameInfo.frame_bytes; + tagDataSize -= (size_t)(pTagDataBeg - pFirstFrameData); + + drmp3__on_meta(pMP3, metadataType, pTagDataBeg, tagDataSize); + } + + /* Since this was identified as a tag, we don't want to treat it as audio. We need to clear out the PCM cache. */ + pMP3->pcmFramesRemainingInMP3Frame = 0; + + /* The start offset needs to be moved to the end of this frame so it's not included in any audio processing after seeking. */ + pMP3->streamStartOffset += (drmp3_uint32)(firstFrameInfo.frame_bytes); + pMP3->streamCursor = pMP3->streamStartOffset; + + /* + The internal decoder needs to be reset to clear out any state. If we don't reset this state, it's possible for + there to be inconsistencies in the number of samples read when reading to the end of the stream depending on + whether or not the caller seeks to the start of the stream. + */ + drmp3dec_init(&pMP3->decoder); + } + + done_xing_info:; + } else { + /* Failed to read the side info. */ + } + } + #endif + } else { + /* Not a valid MP3 stream. */ + drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); /* The call above may have allocated memory. Need to make sure it's freed before aborting. */ + return DRMP3_FALSE; + } + + if (detectedMP3FrameCount != 0xFFFFFFFF) { + pMP3->totalPCMFrameCount = (drmp3_uint64)detectedMP3FrameCount * firstFramePCMFrameCount; + } + + pMP3->channels = pMP3->mp3FrameChannels; + pMP3->sampleRate = pMP3->mp3FrameSampleRate; + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pMP3 == NULL || onRead == NULL) { + return DRMP3_FALSE; + } + + DRMP3_ZERO_OBJECT(pMP3); + return drmp3_init_internal(pMP3, onRead, onSeek, onTell, onMeta, pUserData, pUserData, pAllocationCallbacks); +} + + +static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead) +{ + drmp3* pMP3 = (drmp3*)pUserData; + size_t bytesRemaining; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos); + + bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos; + if (bytesToRead > bytesRemaining) { + bytesToRead = bytesRemaining; + } + + if (bytesToRead > 0) { + DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead); + pMP3->memory.currentReadPos += bytesToRead; + } + + return bytesToRead; +} + +static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin) +{ + drmp3* pMP3 = (drmp3*)pUserData; + drmp3_int64 newCursor; + + DRMP3_ASSERT(pMP3 != NULL); + + if (origin == DRMP3_SEEK_SET) { + newCursor = 0; + } else if (origin == DRMP3_SEEK_CUR) { + newCursor = (drmp3_int64)pMP3->memory.currentReadPos; + } else if (origin == DRMP3_SEEK_END) { + newCursor = (drmp3_int64)pMP3->memory.dataSize; + } else { + DRMP3_ASSERT(!"Invalid seek origin"); + return DRMP3_FALSE; + } + + newCursor += byteOffset; + + if (newCursor < 0) { + return DRMP3_FALSE; /* Trying to seek prior to the start of the buffer. */ + } + if ((size_t)newCursor > pMP3->memory.dataSize) { + return DRMP3_FALSE; /* Trying to seek beyond the end of the buffer. */ + } + + pMP3->memory.currentReadPos = (size_t)newCursor; + + return DRMP3_TRUE; +} + +static drmp3_bool32 drmp3__on_tell_memory(void* pUserData, drmp3_int64* pCursor) +{ + drmp3* pMP3 = (drmp3*)pUserData; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pCursor != NULL); + + *pCursor = (drmp3_int64)pMP3->memory.currentReadPos; + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3_bool32 result; + + if (pMP3 == NULL) { + return DRMP3_FALSE; + } + + DRMP3_ZERO_OBJECT(pMP3); + + if (pData == NULL || dataSize == 0) { + return DRMP3_FALSE; + } + + pMP3->memory.pData = (const drmp3_uint8*)pData; + pMP3->memory.dataSize = dataSize; + pMP3->memory.currentReadPos = 0; + + result = drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, drmp3__on_tell_memory, onMeta, pMP3, pUserDataMeta, pAllocationCallbacks); + if (result == DRMP3_FALSE) { + return DRMP3_FALSE; + } + + /* Adjust the length of the memory stream to account for ID3v1 and APE tags. */ + if (pMP3->streamLength <= (drmp3_uint64)DRMP3_SIZE_MAX) { + pMP3->memory.dataSize = (size_t)pMP3->streamLength; /* Safe cast. */ + } + + if (pMP3->streamStartOffset > (drmp3_uint64)DRMP3_SIZE_MAX) { + return DRMP3_FALSE; /* Tags too big. */ + } + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + return drmp3_init_memory_with_metadata(pMP3, pData, dataSize, NULL, NULL, pAllocationCallbacks); +} + + +#ifndef DR_MP3_NO_STDIO +#include +#include /* For wcslen(), wcsrtombs() */ + +/* Errno */ +/* drmp3_result_from_errno() is only used inside DR_MP3_NO_STDIO for now. Move this out if it's ever used elsewhere. */ +#include +static drmp3_result drmp3_result_from_errno(int e) +{ + switch (e) + { + case 0: return DRMP3_SUCCESS; + #ifdef EPERM + case EPERM: return DRMP3_INVALID_OPERATION; + #endif + #ifdef ENOENT + case ENOENT: return DRMP3_DOES_NOT_EXIST; + #endif + #ifdef ESRCH + case ESRCH: return DRMP3_DOES_NOT_EXIST; + #endif + #ifdef EINTR + case EINTR: return DRMP3_INTERRUPT; + #endif + #ifdef EIO + case EIO: return DRMP3_IO_ERROR; + #endif + #ifdef ENXIO + case ENXIO: return DRMP3_DOES_NOT_EXIST; + #endif + #ifdef E2BIG + case E2BIG: return DRMP3_INVALID_ARGS; + #endif + #ifdef ENOEXEC + case ENOEXEC: return DRMP3_INVALID_FILE; + #endif + #ifdef EBADF + case EBADF: return DRMP3_INVALID_FILE; + #endif + #ifdef ECHILD + case ECHILD: return DRMP3_ERROR; + #endif + #ifdef EAGAIN + case EAGAIN: return DRMP3_UNAVAILABLE; + #endif + #ifdef ENOMEM + case ENOMEM: return DRMP3_OUT_OF_MEMORY; + #endif + #ifdef EACCES + case EACCES: return DRMP3_ACCESS_DENIED; + #endif + #ifdef EFAULT + case EFAULT: return DRMP3_BAD_ADDRESS; + #endif + #ifdef ENOTBLK + case ENOTBLK: return DRMP3_ERROR; + #endif + #ifdef EBUSY + case EBUSY: return DRMP3_BUSY; + #endif + #ifdef EEXIST + case EEXIST: return DRMP3_ALREADY_EXISTS; + #endif + #ifdef EXDEV + case EXDEV: return DRMP3_ERROR; + #endif + #ifdef ENODEV + case ENODEV: return DRMP3_DOES_NOT_EXIST; + #endif + #ifdef ENOTDIR + case ENOTDIR: return DRMP3_NOT_DIRECTORY; + #endif + #ifdef EISDIR + case EISDIR: return DRMP3_IS_DIRECTORY; + #endif + #ifdef EINVAL + case EINVAL: return DRMP3_INVALID_ARGS; + #endif + #ifdef ENFILE + case ENFILE: return DRMP3_TOO_MANY_OPEN_FILES; + #endif + #ifdef EMFILE + case EMFILE: return DRMP3_TOO_MANY_OPEN_FILES; + #endif + #ifdef ENOTTY + case ENOTTY: return DRMP3_INVALID_OPERATION; + #endif + #ifdef ETXTBSY + case ETXTBSY: return DRMP3_BUSY; + #endif + #ifdef EFBIG + case EFBIG: return DRMP3_TOO_BIG; + #endif + #ifdef ENOSPC + case ENOSPC: return DRMP3_NO_SPACE; + #endif + #ifdef ESPIPE + case ESPIPE: return DRMP3_BAD_SEEK; + #endif + #ifdef EROFS + case EROFS: return DRMP3_ACCESS_DENIED; + #endif + #ifdef EMLINK + case EMLINK: return DRMP3_TOO_MANY_LINKS; + #endif + #ifdef EPIPE + case EPIPE: return DRMP3_BAD_PIPE; + #endif + #ifdef EDOM + case EDOM: return DRMP3_OUT_OF_RANGE; + #endif + #ifdef ERANGE + case ERANGE: return DRMP3_OUT_OF_RANGE; + #endif + #ifdef EDEADLK + case EDEADLK: return DRMP3_DEADLOCK; + #endif + #ifdef ENAMETOOLONG + case ENAMETOOLONG: return DRMP3_PATH_TOO_LONG; + #endif + #ifdef ENOLCK + case ENOLCK: return DRMP3_ERROR; + #endif + #ifdef ENOSYS + case ENOSYS: return DRMP3_NOT_IMPLEMENTED; + #endif + #if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST /* In AIX, ENOTEMPTY and EEXIST use the same value. */ + case ENOTEMPTY: return DRMP3_DIRECTORY_NOT_EMPTY; + #endif + #ifdef ELOOP + case ELOOP: return DRMP3_TOO_MANY_LINKS; + #endif + #ifdef ENOMSG + case ENOMSG: return DRMP3_NO_MESSAGE; + #endif + #ifdef EIDRM + case EIDRM: return DRMP3_ERROR; + #endif + #ifdef ECHRNG + case ECHRNG: return DRMP3_ERROR; + #endif + #ifdef EL2NSYNC + case EL2NSYNC: return DRMP3_ERROR; + #endif + #ifdef EL3HLT + case EL3HLT: return DRMP3_ERROR; + #endif + #ifdef EL3RST + case EL3RST: return DRMP3_ERROR; + #endif + #ifdef ELNRNG + case ELNRNG: return DRMP3_OUT_OF_RANGE; + #endif + #ifdef EUNATCH + case EUNATCH: return DRMP3_ERROR; + #endif + #ifdef ENOCSI + case ENOCSI: return DRMP3_ERROR; + #endif + #ifdef EL2HLT + case EL2HLT: return DRMP3_ERROR; + #endif + #ifdef EBADE + case EBADE: return DRMP3_ERROR; + #endif + #ifdef EBADR + case EBADR: return DRMP3_ERROR; + #endif + #ifdef EXFULL + case EXFULL: return DRMP3_ERROR; + #endif + #ifdef ENOANO + case ENOANO: return DRMP3_ERROR; + #endif + #ifdef EBADRQC + case EBADRQC: return DRMP3_ERROR; + #endif + #ifdef EBADSLT + case EBADSLT: return DRMP3_ERROR; + #endif + #ifdef EBFONT + case EBFONT: return DRMP3_INVALID_FILE; + #endif + #ifdef ENOSTR + case ENOSTR: return DRMP3_ERROR; + #endif + #ifdef ENODATA + case ENODATA: return DRMP3_NO_DATA_AVAILABLE; + #endif + #ifdef ETIME + case ETIME: return DRMP3_TIMEOUT; + #endif + #ifdef ENOSR + case ENOSR: return DRMP3_NO_DATA_AVAILABLE; + #endif + #ifdef ENONET + case ENONET: return DRMP3_NO_NETWORK; + #endif + #ifdef ENOPKG + case ENOPKG: return DRMP3_ERROR; + #endif + #ifdef EREMOTE + case EREMOTE: return DRMP3_ERROR; + #endif + #ifdef ENOLINK + case ENOLINK: return DRMP3_ERROR; + #endif + #ifdef EADV + case EADV: return DRMP3_ERROR; + #endif + #ifdef ESRMNT + case ESRMNT: return DRMP3_ERROR; + #endif + #ifdef ECOMM + case ECOMM: return DRMP3_ERROR; + #endif + #ifdef EPROTO + case EPROTO: return DRMP3_ERROR; + #endif + #ifdef EMULTIHOP + case EMULTIHOP: return DRMP3_ERROR; + #endif + #ifdef EDOTDOT + case EDOTDOT: return DRMP3_ERROR; + #endif + #ifdef EBADMSG + case EBADMSG: return DRMP3_BAD_MESSAGE; + #endif + #ifdef EOVERFLOW + case EOVERFLOW: return DRMP3_TOO_BIG; + #endif + #ifdef ENOTUNIQ + case ENOTUNIQ: return DRMP3_NOT_UNIQUE; + #endif + #ifdef EBADFD + case EBADFD: return DRMP3_ERROR; + #endif + #ifdef EREMCHG + case EREMCHG: return DRMP3_ERROR; + #endif + #ifdef ELIBACC + case ELIBACC: return DRMP3_ACCESS_DENIED; + #endif + #ifdef ELIBBAD + case ELIBBAD: return DRMP3_INVALID_FILE; + #endif + #ifdef ELIBSCN + case ELIBSCN: return DRMP3_INVALID_FILE; + #endif + #ifdef ELIBMAX + case ELIBMAX: return DRMP3_ERROR; + #endif + #ifdef ELIBEXEC + case ELIBEXEC: return DRMP3_ERROR; + #endif + #ifdef EILSEQ + case EILSEQ: return DRMP3_INVALID_DATA; + #endif + #ifdef ERESTART + case ERESTART: return DRMP3_ERROR; + #endif + #ifdef ESTRPIPE + case ESTRPIPE: return DRMP3_ERROR; + #endif + #ifdef EUSERS + case EUSERS: return DRMP3_ERROR; + #endif + #ifdef ENOTSOCK + case ENOTSOCK: return DRMP3_NOT_SOCKET; + #endif + #ifdef EDESTADDRREQ + case EDESTADDRREQ: return DRMP3_NO_ADDRESS; + #endif + #ifdef EMSGSIZE + case EMSGSIZE: return DRMP3_TOO_BIG; + #endif + #ifdef EPROTOTYPE + case EPROTOTYPE: return DRMP3_BAD_PROTOCOL; + #endif + #ifdef ENOPROTOOPT + case ENOPROTOOPT: return DRMP3_PROTOCOL_UNAVAILABLE; + #endif + #ifdef EPROTONOSUPPORT + case EPROTONOSUPPORT: return DRMP3_PROTOCOL_NOT_SUPPORTED; + #endif + #ifdef ESOCKTNOSUPPORT + case ESOCKTNOSUPPORT: return DRMP3_SOCKET_NOT_SUPPORTED; + #endif + #ifdef EOPNOTSUPP + case EOPNOTSUPP: return DRMP3_INVALID_OPERATION; + #endif + #ifdef EPFNOSUPPORT + case EPFNOSUPPORT: return DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED; + #endif + #ifdef EAFNOSUPPORT + case EAFNOSUPPORT: return DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED; + #endif + #ifdef EADDRINUSE + case EADDRINUSE: return DRMP3_ALREADY_IN_USE; + #endif + #ifdef EADDRNOTAVAIL + case EADDRNOTAVAIL: return DRMP3_ERROR; + #endif + #ifdef ENETDOWN + case ENETDOWN: return DRMP3_NO_NETWORK; + #endif + #ifdef ENETUNREACH + case ENETUNREACH: return DRMP3_NO_NETWORK; + #endif + #ifdef ENETRESET + case ENETRESET: return DRMP3_NO_NETWORK; + #endif + #ifdef ECONNABORTED + case ECONNABORTED: return DRMP3_NO_NETWORK; + #endif + #ifdef ECONNRESET + case ECONNRESET: return DRMP3_CONNECTION_RESET; + #endif + #ifdef ENOBUFS + case ENOBUFS: return DRMP3_NO_SPACE; + #endif + #ifdef EISCONN + case EISCONN: return DRMP3_ALREADY_CONNECTED; + #endif + #ifdef ENOTCONN + case ENOTCONN: return DRMP3_NOT_CONNECTED; + #endif + #ifdef ESHUTDOWN + case ESHUTDOWN: return DRMP3_ERROR; + #endif + #ifdef ETOOMANYREFS + case ETOOMANYREFS: return DRMP3_ERROR; + #endif + #ifdef ETIMEDOUT + case ETIMEDOUT: return DRMP3_TIMEOUT; + #endif + #ifdef ECONNREFUSED + case ECONNREFUSED: return DRMP3_CONNECTION_REFUSED; + #endif + #ifdef EHOSTDOWN + case EHOSTDOWN: return DRMP3_NO_HOST; + #endif + #ifdef EHOSTUNREACH + case EHOSTUNREACH: return DRMP3_NO_HOST; + #endif + #ifdef EALREADY + case EALREADY: return DRMP3_IN_PROGRESS; + #endif + #ifdef EINPROGRESS + case EINPROGRESS: return DRMP3_IN_PROGRESS; + #endif + #ifdef ESTALE + case ESTALE: return DRMP3_INVALID_FILE; + #endif + #ifdef EUCLEAN + case EUCLEAN: return DRMP3_ERROR; + #endif + #ifdef ENOTNAM + case ENOTNAM: return DRMP3_ERROR; + #endif + #ifdef ENAVAIL + case ENAVAIL: return DRMP3_ERROR; + #endif + #ifdef EISNAM + case EISNAM: return DRMP3_ERROR; + #endif + #ifdef EREMOTEIO + case EREMOTEIO: return DRMP3_IO_ERROR; + #endif + #ifdef EDQUOT + case EDQUOT: return DRMP3_NO_SPACE; + #endif + #ifdef ENOMEDIUM + case ENOMEDIUM: return DRMP3_DOES_NOT_EXIST; + #endif + #ifdef EMEDIUMTYPE + case EMEDIUMTYPE: return DRMP3_ERROR; + #endif + #ifdef ECANCELED + case ECANCELED: return DRMP3_CANCELLED; + #endif + #ifdef ENOKEY + case ENOKEY: return DRMP3_ERROR; + #endif + #ifdef EKEYEXPIRED + case EKEYEXPIRED: return DRMP3_ERROR; + #endif + #ifdef EKEYREVOKED + case EKEYREVOKED: return DRMP3_ERROR; + #endif + #ifdef EKEYREJECTED + case EKEYREJECTED: return DRMP3_ERROR; + #endif + #ifdef EOWNERDEAD + case EOWNERDEAD: return DRMP3_ERROR; + #endif + #ifdef ENOTRECOVERABLE + case ENOTRECOVERABLE: return DRMP3_ERROR; + #endif + #ifdef ERFKILL + case ERFKILL: return DRMP3_ERROR; + #endif + #ifdef EHWPOISON + case EHWPOISON: return DRMP3_ERROR; + #endif + default: return DRMP3_ERROR; + } +} +/* End Errno */ + +/* fopen */ +static drmp3_result drmp3_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode) +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + errno_t err; +#endif + + if (ppFile != NULL) { + *ppFile = NULL; /* Safety. */ + } + + if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { + return DRMP3_INVALID_ARGS; + } + +#if defined(_MSC_VER) && _MSC_VER >= 1400 + err = fopen_s(ppFile, pFilePath, pOpenMode); + if (err != 0) { + return drmp3_result_from_errno(err); + } +#else +#if defined(_WIN32) || defined(__APPLE__) + *ppFile = fopen(pFilePath, pOpenMode); +#else + #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE) + *ppFile = fopen64(pFilePath, pOpenMode); + #else + *ppFile = fopen(pFilePath, pOpenMode); + #endif +#endif + if (*ppFile == NULL) { + drmp3_result result = drmp3_result_from_errno(errno); + if (result == DRMP3_SUCCESS) { + result = DRMP3_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */ + } + + return result; + } +#endif + + return DRMP3_SUCCESS; +} + +/* +_wfopen() isn't always available in all compilation environments. + + * Windows only. + * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back). + * MinGW-64 (both 32- and 64-bit) seems to support it. + * MinGW wraps it in !defined(__STRICT_ANSI__). + * OpenWatcom wraps it in !defined(_NO_EXT_KEYS). + +This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs() +fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support. +*/ +#if defined(_WIN32) + #if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS)) + #define DRMP3_HAS_WFOPEN + #endif +#endif + +static drmp3_result drmp3_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (ppFile != NULL) { + *ppFile = NULL; /* Safety. */ + } + + if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { + return DRMP3_INVALID_ARGS; + } + +#if defined(DRMP3_HAS_WFOPEN) + { + /* Use _wfopen() on Windows. */ + #if defined(_MSC_VER) && _MSC_VER >= 1400 + errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode); + if (err != 0) { + return drmp3_result_from_errno(err); + } + #else + *ppFile = _wfopen(pFilePath, pOpenMode); + if (*ppFile == NULL) { + return drmp3_result_from_errno(errno); + } + #endif + (void)pAllocationCallbacks; + } +#else + /* + Use fopen() on anything other than Windows. Requires a conversion. This is annoying because + fopen() is locale specific. The only real way I can think of to do this is with wcsrtombs(). Note + that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for + maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler + error I'll look into improving compatibility. + */ + + /* + Some compilers don't support wchar_t or wcsrtombs() which we're using below. In this case we just + need to abort with an error. If you encounter a compiler lacking such support, add it to this list + and submit a bug report and it'll be added to the library upstream. + */ + #if defined(__DJGPP__) + { + /* Nothing to do here. This will fall through to the error check below. */ + } + #else + { + mbstate_t mbs; + size_t lenMB; + const wchar_t* pFilePathTemp = pFilePath; + char* pFilePathMB = NULL; + char pOpenModeMB[32] = {0}; + + /* Get the length first. */ + DRMP3_ZERO_OBJECT(&mbs); + lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs); + if (lenMB == (size_t)-1) { + return drmp3_result_from_errno(errno); + } + + pFilePathMB = (char*)drmp3__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks); + if (pFilePathMB == NULL) { + return DRMP3_OUT_OF_MEMORY; + } + + pFilePathTemp = pFilePath; + DRMP3_ZERO_OBJECT(&mbs); + wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs); + + /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */ + { + size_t i = 0; + for (;;) { + if (pOpenMode[i] == 0) { + pOpenModeMB[i] = '\0'; + break; + } + + pOpenModeMB[i] = (char)pOpenMode[i]; + i += 1; + } + } + + *ppFile = fopen(pFilePathMB, pOpenModeMB); + + drmp3__free_from_callbacks(pFilePathMB, pAllocationCallbacks); + } + #endif + + if (*ppFile == NULL) { + return DRMP3_ERROR; + } +#endif + + return DRMP3_SUCCESS; +} +/* End fopen */ + + +static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead) +{ + return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData); +} + +static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin) +{ + int whence = SEEK_SET; + if (origin == DRMP3_SEEK_CUR) { + whence = SEEK_CUR; + } else if (origin == DRMP3_SEEK_END) { + whence = SEEK_END; + } + + return fseek((FILE*)pUserData, offset, whence) == 0; +} + +static drmp3_bool32 drmp3__on_tell_stdio(void* pUserData, drmp3_int64* pCursor) +{ + FILE* pFileStdio = (FILE*)pUserData; + drmp3_int64 result; + + /* These were all validated at a higher level. */ + DRMP3_ASSERT(pFileStdio != NULL); + DRMP3_ASSERT(pCursor != NULL); + +#if defined(_WIN32) && !defined(NXDK) + #if defined(_MSC_VER) && _MSC_VER > 1200 + result = _ftelli64(pFileStdio); + #else + result = ftell(pFileStdio); + #endif +#else + result = ftell(pFileStdio); +#endif + + *pCursor = result; + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3_bool32 result; + FILE* pFile; + + if (pMP3 == NULL) { + return DRMP3_FALSE; + } + + DRMP3_ZERO_OBJECT(pMP3); + + if (drmp3_fopen(&pFile, pFilePath, "rb") != DRMP3_SUCCESS) { + return DRMP3_FALSE; + } + + result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks); + if (result != DRMP3_TRUE) { + fclose(pFile); + return result; + } + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3_bool32 result; + FILE* pFile; + + if (pMP3 == NULL) { + return DRMP3_FALSE; + } + + DRMP3_ZERO_OBJECT(pMP3); + + if (drmp3_wfopen(&pFile, pFilePath, L"rb", pAllocationCallbacks) != DRMP3_SUCCESS) { + return DRMP3_FALSE; + } + + result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks); + if (result != DRMP3_TRUE) { + fclose(pFile); + return result; + } + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + return drmp3_init_file_with_metadata(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks); +} + +DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + return drmp3_init_file_with_metadata_w(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks); +} +#endif + +DRMP3_API void drmp3_uninit(drmp3* pMP3) +{ + if (pMP3 == NULL) { + return; + } + +#ifndef DR_MP3_NO_STDIO + if (pMP3->onRead == drmp3__on_read_stdio) { + FILE* pFile = (FILE*)pMP3->pUserData; + if (pFile != NULL) { + fclose(pFile); + pMP3->pUserData = NULL; /* Make sure the file handle is cleared to NULL to we don't attempt to close it a second time. */ + } + } +#endif + + drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); +} + +#if defined(DR_MP3_FLOAT_OUTPUT) +static void drmp3_f32_to_s16(drmp3_int16* dst, const float* src, drmp3_uint64 sampleCount) +{ + drmp3_uint64 i; + drmp3_uint64 i4; + drmp3_uint64 sampleCount4; + + /* Unrolled. */ + i = 0; + sampleCount4 = sampleCount >> 2; + for (i4 = 0; i4 < sampleCount4; i4 += 1) { + float x0 = src[i+0]; + float x1 = src[i+1]; + float x2 = src[i+2]; + float x3 = src[i+3]; + + x0 = ((x0 < -1) ? -1 : ((x0 > 1) ? 1 : x0)); + x1 = ((x1 < -1) ? -1 : ((x1 > 1) ? 1 : x1)); + x2 = ((x2 < -1) ? -1 : ((x2 > 1) ? 1 : x2)); + x3 = ((x3 < -1) ? -1 : ((x3 > 1) ? 1 : x3)); + + x0 = x0 * 32767.0f; + x1 = x1 * 32767.0f; + x2 = x2 * 32767.0f; + x3 = x3 * 32767.0f; + + dst[i+0] = (drmp3_int16)x0; + dst[i+1] = (drmp3_int16)x1; + dst[i+2] = (drmp3_int16)x2; + dst[i+3] = (drmp3_int16)x3; + + i += 4; + } + + /* Leftover. */ + for (; i < sampleCount; i += 1) { + float x = src[i]; + x = ((x < -1) ? -1 : ((x > 1) ? 1 : x)); /* clip */ + x = x * 32767.0f; /* -1..1 to -32767..32767 */ + + dst[i] = (drmp3_int16)x; + } +} +#endif + +#if !defined(DR_MP3_FLOAT_OUTPUT) +static void drmp3_s16_to_f32(float* dst, const drmp3_int16* src, drmp3_uint64 sampleCount) +{ + drmp3_uint64 i; + for (i = 0; i < sampleCount; i += 1) { + float x = (float)src[i]; + x = x * 0.000030517578125f; /* -32768..32767 to -1..0.999969482421875 */ + dst[i] = x; + } +} +#endif + + +static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesToRead, void* pBufferOut) +{ + drmp3_uint64 totalFramesRead = 0; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->onRead != NULL); + + while (framesToRead > 0) { + drmp3_uint32 framesToConsume; + + /* Skip frames if necessary. */ + if (pMP3->currentPCMFrame < pMP3->delayInPCMFrames) { + drmp3_uint32 framesToSkip = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, pMP3->delayInPCMFrames - pMP3->currentPCMFrame); + + pMP3->currentPCMFrame += framesToSkip; + pMP3->pcmFramesConsumedInMP3Frame += framesToSkip; + pMP3->pcmFramesRemainingInMP3Frame -= framesToSkip; + } + + framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead); + + /* Clamp the number of frames to read to the padding. */ + if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames) { + if (pMP3->currentPCMFrame < (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) { + drmp3_uint64 framesRemainigToPadding = (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames) - pMP3->currentPCMFrame; + if (framesToConsume > framesRemainigToPadding) { + framesToConsume = (drmp3_uint32)framesRemainigToPadding; + } + } else { + /* We're into the padding. Abort. */ + break; + } + } + + if (pBufferOut != NULL) { + #if defined(DR_MP3_FLOAT_OUTPUT) + { + /* f32 */ + float* pFramesOutF32 = (float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalFramesRead * pMP3->channels); + float* pFramesInF32 = (float*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(float) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels); + DRMP3_COPY_MEMORY(pFramesOutF32, pFramesInF32, sizeof(float) * framesToConsume * pMP3->channels); + } + #else + { + /* s16 */ + drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels); + drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels); + DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels); + } + #endif + } + + pMP3->currentPCMFrame += framesToConsume; + pMP3->pcmFramesConsumedInMP3Frame += framesToConsume; + pMP3->pcmFramesRemainingInMP3Frame -= framesToConsume; + totalFramesRead += framesToConsume; + framesToRead -= framesToConsume; + + if (framesToRead == 0) { + break; + } + + /* If the cursor is already at the padding we need to abort. */ + if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames && pMP3->currentPCMFrame >= (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) { + break; + } + + DRMP3_ASSERT(pMP3->pcmFramesRemainingInMP3Frame == 0); + + /* At this point we have exhausted our in-memory buffer so we need to re-fill. */ + if (drmp3_decode_next_frame(pMP3) == 0) { + break; + } + } + + return totalFramesRead; +} + + +DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut) +{ + if (pMP3 == NULL || pMP3->onRead == NULL) { + return 0; + } + +#if defined(DR_MP3_FLOAT_OUTPUT) + /* Fast path. No conversion required. */ + return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut); +#else + /* Slow path. Convert from s16 to f32. */ + { + drmp3_int16 pTempS16[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */ + drmp3_uint64 totalPCMFramesRead = 0; + + while (totalPCMFramesRead < framesToRead) { + drmp3_uint64 framesJustRead; + drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead; + drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempS16) / pMP3->channels; + if (framesToReadNow > framesRemaining) { + framesToReadNow = framesRemaining; + } + + framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempS16); + if (framesJustRead == 0) { + break; + } + + drmp3_s16_to_f32((float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalPCMFramesRead * pMP3->channels), pTempS16, framesJustRead * pMP3->channels); + totalPCMFramesRead += framesJustRead; + } + + return totalPCMFramesRead; + } +#endif +} + +DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut) +{ + if (pMP3 == NULL || pMP3->onRead == NULL) { + return 0; + } + +#if !defined(DR_MP3_FLOAT_OUTPUT) + /* Fast path. No conversion required. */ + return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut); +#else + /* Slow path. Convert from f32 to s16. */ + { + float pTempF32[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */ + drmp3_uint64 totalPCMFramesRead = 0; + + while (totalPCMFramesRead < framesToRead) { + drmp3_uint64 framesJustRead; + drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead; + drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempF32) / pMP3->channels; + if (framesToReadNow > framesRemaining) { + framesToReadNow = framesRemaining; + } + + framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempF32); + if (framesJustRead == 0) { + break; + } + + drmp3_f32_to_s16((drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalPCMFramesRead * pMP3->channels), pTempF32, framesJustRead * pMP3->channels); + totalPCMFramesRead += framesJustRead; + } + + return totalPCMFramesRead; + } +#endif +} + +static void drmp3_reset(drmp3* pMP3) +{ + DRMP3_ASSERT(pMP3 != NULL); + + pMP3->pcmFramesConsumedInMP3Frame = 0; + pMP3->pcmFramesRemainingInMP3Frame = 0; + pMP3->currentPCMFrame = 0; + pMP3->dataSize = 0; + pMP3->atEnd = DRMP3_FALSE; + drmp3dec_init(&pMP3->decoder); +} + +static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3) +{ + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->onSeek != NULL); + + /* Seek to the start of the stream to begin with. */ + if (!drmp3__on_seek_64(pMP3, pMP3->streamStartOffset, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; + } + + /* Clear any cached data. */ + drmp3_reset(pMP3); + return DRMP3_TRUE; +} + + +static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset) +{ + drmp3_uint64 framesRead; + + /* + Just using a dumb read-and-discard for now. What would be nice is to parse only the header of the MP3 frame, and then skip over leading + frames without spending the time doing a full decode. I cannot see an easy way to do this in minimp3, however, so it may involve some + kind of manual processing. + */ +#if defined(DR_MP3_FLOAT_OUTPUT) + framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL); +#else + framesRead = drmp3_read_pcm_frames_s16(pMP3, frameOffset, NULL); +#endif + if (framesRead != frameOffset) { + return DRMP3_FALSE; + } + + return DRMP3_TRUE; +} + +static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex) +{ + DRMP3_ASSERT(pMP3 != NULL); + + if (frameIndex == pMP3->currentPCMFrame) { + return DRMP3_TRUE; + } + + /* + If we're moving foward we just read from where we're at. Otherwise we need to move back to the start of + the stream and read from the beginning. + */ + if (frameIndex < pMP3->currentPCMFrame) { + /* Moving backward. Move to the start of the stream and then move forward. */ + if (!drmp3_seek_to_start_of_stream(pMP3)) { + return DRMP3_FALSE; + } + } + + DRMP3_ASSERT(frameIndex >= pMP3->currentPCMFrame); + return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame)); +} + +static drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex) +{ + drmp3_uint32 iSeekPoint; + + DRMP3_ASSERT(pSeekPointIndex != NULL); + + *pSeekPointIndex = 0; + + if (frameIndex < pMP3->pSeekPoints[0].pcmFrameIndex) { + return DRMP3_FALSE; + } + + /* Linear search for simplicity to begin with while I'm getting this thing working. Once it's all working change this to a binary search. */ + for (iSeekPoint = 0; iSeekPoint < pMP3->seekPointCount; ++iSeekPoint) { + if (pMP3->pSeekPoints[iSeekPoint].pcmFrameIndex > frameIndex) { + break; /* Found it. */ + } + + *pSeekPointIndex = iSeekPoint; + } + + return DRMP3_TRUE; +} + +static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint64 frameIndex) +{ + drmp3_seek_point seekPoint; + drmp3_uint32 priorSeekPointIndex; + drmp3_uint16 iMP3Frame; + drmp3_uint64 leftoverFrames; + + DRMP3_ASSERT(pMP3 != NULL); + DRMP3_ASSERT(pMP3->pSeekPoints != NULL); + DRMP3_ASSERT(pMP3->seekPointCount > 0); + + /* If there is no prior seekpoint it means the target PCM frame comes before the first seek point. Just assume a seekpoint at the start of the file in this case. */ + if (drmp3_find_closest_seek_point(pMP3, frameIndex, &priorSeekPointIndex)) { + seekPoint = pMP3->pSeekPoints[priorSeekPointIndex]; + } else { + seekPoint.seekPosInBytes = 0; + seekPoint.pcmFrameIndex = 0; + seekPoint.mp3FramesToDiscard = 0; + seekPoint.pcmFramesToDiscard = 0; + } + + /* First thing to do is seek to the first byte of the relevant MP3 frame. */ + if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, DRMP3_SEEK_SET)) { + return DRMP3_FALSE; /* Failed to seek. */ + } + + /* Clear any cached data. */ + drmp3_reset(pMP3); + + /* Whole MP3 frames need to be discarded first. */ + for (iMP3Frame = 0; iMP3Frame < seekPoint.mp3FramesToDiscard; ++iMP3Frame) { + drmp3_uint32 pcmFramesRead; + drmp3d_sample_t* pPCMFrames; + + /* Pass in non-null for the last frame because we want to ensure the sample rate converter is preloaded correctly. */ + pPCMFrames = NULL; + if (iMP3Frame == seekPoint.mp3FramesToDiscard-1) { + pPCMFrames = (drmp3d_sample_t*)pMP3->pcmFrames; + } + + /* We first need to decode the next frame. */ + pcmFramesRead = drmp3_decode_next_frame_ex(pMP3, pPCMFrames, NULL, NULL); + if (pcmFramesRead == 0) { + return DRMP3_FALSE; + } + } + + /* We seeked to an MP3 frame in the raw stream so we need to make sure the current PCM frame is set correctly. */ + pMP3->currentPCMFrame = seekPoint.pcmFrameIndex - seekPoint.pcmFramesToDiscard; + + /* + Now at this point we can follow the same process as the brute force technique where we just skip over unnecessary MP3 frames and then + read-and-discard at least 2 whole MP3 frames. + */ + leftoverFrames = frameIndex - pMP3->currentPCMFrame; + return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames); +} + +DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex) +{ + if (pMP3 == NULL || pMP3->onSeek == NULL) { + return DRMP3_FALSE; + } + + if (frameIndex == 0) { + return drmp3_seek_to_start_of_stream(pMP3); + } + + /* Use the seek table if we have one. */ + if (pMP3->pSeekPoints != NULL && pMP3->seekPointCount > 0) { + return drmp3_seek_to_pcm_frame__seek_table(pMP3, frameIndex); + } else { + return drmp3_seek_to_pcm_frame__brute_force(pMP3, frameIndex); + } +} + +DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount) +{ + drmp3_uint64 currentPCMFrame; + drmp3_uint64 totalPCMFrameCount; + drmp3_uint64 totalMP3FrameCount; + + if (pMP3 == NULL) { + return DRMP3_FALSE; + } + + /* + The way this works is we move back to the start of the stream, iterate over each MP3 frame and calculate the frame count based + on our output sample rate, the seek back to the PCM frame we were sitting on before calling this function. + */ + + /* The stream must support seeking for this to work. */ + if (pMP3->onSeek == NULL) { + return DRMP3_FALSE; + } + + /* We'll need to seek back to where we were, so grab the PCM frame we're currently sitting on so we can restore later. */ + currentPCMFrame = pMP3->currentPCMFrame; + + if (!drmp3_seek_to_start_of_stream(pMP3)) { + return DRMP3_FALSE; + } + + totalPCMFrameCount = 0; + totalMP3FrameCount = 0; + + for (;;) { + drmp3_uint32 pcmFramesInCurrentMP3Frame; + + pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); + if (pcmFramesInCurrentMP3Frame == 0) { + break; + } + + totalPCMFrameCount += pcmFramesInCurrentMP3Frame; + totalMP3FrameCount += 1; + } + + /* Finally, we need to seek back to where we were. */ + if (!drmp3_seek_to_start_of_stream(pMP3)) { + return DRMP3_FALSE; + } + + if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) { + return DRMP3_FALSE; + } + + if (pMP3FrameCount != NULL) { + *pMP3FrameCount = totalMP3FrameCount; + } + if (pPCMFrameCount != NULL) { + *pPCMFrameCount = totalPCMFrameCount; + } + + return DRMP3_TRUE; +} + +DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3) +{ + drmp3_uint64 totalPCMFrameCount; + + if (pMP3 == NULL) { + return 0; + } + + if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX) { + totalPCMFrameCount = pMP3->totalPCMFrameCount; + + if (totalPCMFrameCount >= pMP3->delayInPCMFrames) { + totalPCMFrameCount -= pMP3->delayInPCMFrames; + } else { + /* The delay is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */ + } + + if (totalPCMFrameCount >= pMP3->paddingInPCMFrames) { + totalPCMFrameCount -= pMP3->paddingInPCMFrames; + } else { + /* The padding is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */ + } + + return totalPCMFrameCount; + } else { + /* Unknown frame count. Need to calculate it. */ + if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) { + return 0; + } + + return totalPCMFrameCount; + } +} + +DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3) +{ + drmp3_uint64 totalMP3FrameCount; + if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, NULL)) { + return 0; + } + + return totalMP3FrameCount; +} + +static void drmp3__accumulate_running_pcm_frame_count(drmp3* pMP3, drmp3_uint32 pcmFrameCountIn, drmp3_uint64* pRunningPCMFrameCount, float* pRunningPCMFrameCountFractionalPart) +{ + float srcRatio; + float pcmFrameCountOutF; + drmp3_uint32 pcmFrameCountOut; + + srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate; + DRMP3_ASSERT(srcRatio > 0); + + pcmFrameCountOutF = *pRunningPCMFrameCountFractionalPart + (pcmFrameCountIn / srcRatio); + pcmFrameCountOut = (drmp3_uint32)pcmFrameCountOutF; + *pRunningPCMFrameCountFractionalPart = pcmFrameCountOutF - pcmFrameCountOut; + *pRunningPCMFrameCount += pcmFrameCountOut; +} + +typedef struct +{ + drmp3_uint64 bytePos; + drmp3_uint64 pcmFrameIndex; /* <-- After sample rate conversion. */ +} drmp3__seeking_mp3_frame_info; + +DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints) +{ + drmp3_uint32 seekPointCount; + drmp3_uint64 currentPCMFrame; + drmp3_uint64 totalMP3FrameCount; + drmp3_uint64 totalPCMFrameCount; + + if (pMP3 == NULL || pSeekPointCount == NULL || pSeekPoints == NULL) { + return DRMP3_FALSE; /* Invalid args. */ + } + + seekPointCount = *pSeekPointCount; + if (seekPointCount == 0) { + return DRMP3_FALSE; /* The client has requested no seek points. Consider this to be invalid arguments since the client has probably not intended this. */ + } + + /* We'll need to seek back to the current sample after calculating the seekpoints so we need to go ahead and grab the current location at the top. */ + currentPCMFrame = pMP3->currentPCMFrame; + + /* We never do more than the total number of MP3 frames and we limit it to 32-bits. */ + if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) { + return DRMP3_FALSE; + } + + /* If there's less than DRMP3_SEEK_LEADING_MP3_FRAMES+1 frames we just report 1 seek point which will be the very start of the stream. */ + if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) { + seekPointCount = 1; + pSeekPoints[0].seekPosInBytes = 0; + pSeekPoints[0].pcmFrameIndex = 0; + pSeekPoints[0].mp3FramesToDiscard = 0; + pSeekPoints[0].pcmFramesToDiscard = 0; + } else { + drmp3_uint64 pcmFramesBetweenSeekPoints; + drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1]; + drmp3_uint64 runningPCMFrameCount = 0; + float runningPCMFrameCountFractionalPart = 0; + drmp3_uint64 nextTargetPCMFrame; + drmp3_uint32 iMP3Frame; + drmp3_uint32 iSeekPoint; + + if (seekPointCount > totalMP3FrameCount-1) { + seekPointCount = (drmp3_uint32)totalMP3FrameCount-1; + } + + pcmFramesBetweenSeekPoints = totalPCMFrameCount / (seekPointCount+1); + + /* + Here is where we actually calculate the seek points. We need to start by moving the start of the stream. We then enumerate over each + MP3 frame. + */ + if (!drmp3_seek_to_start_of_stream(pMP3)) { + return DRMP3_FALSE; + } + + /* + We need to cache the byte positions of the previous MP3 frames. As a new MP3 frame is iterated, we cycle the byte positions in this + array. The value in the first item in this array is the byte position that will be reported in the next seek point. + */ + + /* We need to initialize the array of MP3 byte positions for the leading MP3 frames. */ + for (iMP3Frame = 0; iMP3Frame < DRMP3_SEEK_LEADING_MP3_FRAMES+1; ++iMP3Frame) { + drmp3_uint32 pcmFramesInCurrentMP3FrameIn; + + /* The byte position of the next frame will be the stream's cursor position, minus whatever is sitting in the buffer. */ + DRMP3_ASSERT(pMP3->streamCursor >= pMP3->dataSize); + mp3FrameInfo[iMP3Frame].bytePos = pMP3->streamCursor - pMP3->dataSize; + mp3FrameInfo[iMP3Frame].pcmFrameIndex = runningPCMFrameCount; + + /* We need to get information about this frame so we can know how many samples it contained. */ + pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); + if (pcmFramesInCurrentMP3FrameIn == 0) { + return DRMP3_FALSE; /* This should never happen. */ + } + + drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart); + } + + /* + At this point we will have extracted the byte positions of the leading MP3 frames. We can now start iterating over each seek point and + calculate them. + */ + nextTargetPCMFrame = 0; + for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) { + nextTargetPCMFrame += pcmFramesBetweenSeekPoints; + + for (;;) { + if (nextTargetPCMFrame < runningPCMFrameCount) { + /* The next seek point is in the current MP3 frame. */ + pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos; + pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame; + pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES; + pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex); + break; + } else { + size_t i; + drmp3_uint32 pcmFramesInCurrentMP3FrameIn; + + /* + The next seek point is not in the current MP3 frame, so continue on to the next one. The first thing to do is cycle the cached + MP3 frame info. + */ + for (i = 0; i < DRMP3_COUNTOF(mp3FrameInfo)-1; ++i) { + mp3FrameInfo[i] = mp3FrameInfo[i+1]; + } + + /* Cache previous MP3 frame info. */ + mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].bytePos = pMP3->streamCursor - pMP3->dataSize; + mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].pcmFrameIndex = runningPCMFrameCount; + + /* + Go to the next MP3 frame. This shouldn't ever fail, but just in case it does we just set the seek point and break. If it happens, it + should only ever do it for the last seek point. + */ + pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); + if (pcmFramesInCurrentMP3FrameIn == 0) { + pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos; + pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame; + pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES; + pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex); + break; + } + + drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart); + } + } + } + + /* Finally, we need to seek back to where we were. */ + if (!drmp3_seek_to_start_of_stream(pMP3)) { + return DRMP3_FALSE; + } + if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) { + return DRMP3_FALSE; + } + } + + *pSeekPointCount = seekPointCount; + return DRMP3_TRUE; +} + +DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints) +{ + if (pMP3 == NULL) { + return DRMP3_FALSE; + } + + if (seekPointCount == 0 || pSeekPoints == NULL) { + /* Unbinding. */ + pMP3->seekPointCount = 0; + pMP3->pSeekPoints = NULL; + } else { + /* Binding. */ + pMP3->seekPointCount = seekPointCount; + pMP3->pSeekPoints = pSeekPoints; + } + + return DRMP3_TRUE; +} + + +static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount) +{ + drmp3_uint64 totalFramesRead = 0; + drmp3_uint64 framesCapacity = 0; + float* pFrames = NULL; + float temp[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */ + + DRMP3_ASSERT(pMP3 != NULL); + + for (;;) { + drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels; + drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp); + if (framesJustRead == 0) { + break; + } + + /* Reallocate the output buffer if there's not enough room. */ + if (framesCapacity < totalFramesRead + framesJustRead) { + drmp3_uint64 oldFramesBufferSize; + drmp3_uint64 newFramesBufferSize; + drmp3_uint64 newFramesCap; + float* pNewFrames; + + newFramesCap = framesCapacity * 2; + if (newFramesCap < totalFramesRead + framesJustRead) { + newFramesCap = totalFramesRead + framesJustRead; + } + + oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(float); + newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(float); + if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) { + break; + } + + pNewFrames = (float*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks); + if (pNewFrames == NULL) { + drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks); + pFrames = NULL; + totalFramesRead = 0; + break; + } + + pFrames = pNewFrames; + framesCapacity = newFramesCap; + } + + DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float))); + totalFramesRead += framesJustRead; + + /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */ + if (framesJustRead != framesToReadRightNow) { + break; + } + } + + if (pConfig != NULL) { + pConfig->channels = pMP3->channels; + pConfig->sampleRate = pMP3->sampleRate; + } + + drmp3_uninit(pMP3); + + if (pTotalFrameCount) { + *pTotalFrameCount = totalFramesRead; + } + + return pFrames; +} + +static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount) +{ + drmp3_uint64 totalFramesRead = 0; + drmp3_uint64 framesCapacity = 0; + drmp3_int16* pFrames = NULL; + drmp3_int16 temp[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */ + + DRMP3_ASSERT(pMP3 != NULL); + + for (;;) { + drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels; + drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_s16(pMP3, framesToReadRightNow, temp); + if (framesJustRead == 0) { + break; + } + + /* Reallocate the output buffer if there's not enough room. */ + if (framesCapacity < totalFramesRead + framesJustRead) { + drmp3_uint64 newFramesBufferSize; + drmp3_uint64 oldFramesBufferSize; + drmp3_uint64 newFramesCap; + drmp3_int16* pNewFrames; + + newFramesCap = framesCapacity * 2; + if (newFramesCap < totalFramesRead + framesJustRead) { + newFramesCap = totalFramesRead + framesJustRead; + } + + oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(drmp3_int16); + newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(drmp3_int16); + if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) { + break; + } + + pNewFrames = (drmp3_int16*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks); + if (pNewFrames == NULL) { + drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks); + pFrames = NULL; + totalFramesRead = 0; + break; + } + + pFrames = pNewFrames; + framesCapacity = newFramesCap; + } + + DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(drmp3_int16))); + totalFramesRead += framesJustRead; + + /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */ + if (framesJustRead != framesToReadRightNow) { + break; + } + } + + if (pConfig != NULL) { + pConfig->channels = pMP3->channels; + pConfig->sampleRate = pMP3->sampleRate; + } + + drmp3_uninit(pMP3); + + if (pTotalFrameCount) { + *pTotalFrameCount = totalFramesRead; + } + + return pFrames; +} + + +DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); +} + +DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); +} + + +DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); +} + +DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); +} + + +#ifndef DR_MP3_NO_STDIO +DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); +} + +DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + drmp3 mp3; + if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) { + return NULL; + } + + return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); +} +#endif + +DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks != NULL) { + return drmp3__malloc_from_callbacks(sz, pAllocationCallbacks); + } else { + return drmp3__malloc_default(sz, NULL); + } +} + +DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks) +{ + if (pAllocationCallbacks != NULL) { + drmp3__free_from_callbacks(p, pAllocationCallbacks); + } else { + drmp3__free_default(p, NULL); + } +} + +#endif /* dr_mp3_c */ +#endif /*DR_MP3_IMPLEMENTATION*/ + +/* +DIFFERENCES BETWEEN minimp3 AND dr_mp3 +====================================== +- First, keep in mind that minimp3 (https://github.com/lieff/minimp3) is where all the real work was done. All of the + code relating to the actual decoding remains mostly unmodified, apart from some namespacing changes. +- dr_mp3 adds a pulling style API which allows you to deliver raw data via callbacks. So, rather than pushing data + to the decoder, the decoder _pulls_ data from your callbacks. +- In addition to callbacks, a decoder can be initialized from a block of memory and a file. +- The dr_mp3 pull API reads PCM frames rather than whole MP3 frames. +- dr_mp3 adds convenience APIs for opening and decoding entire files in one go. +- dr_mp3 is fully namespaced, including the implementation section, which is more suitable when compiling projects + as a single translation unit (aka unity builds). At the time of writing this, a unity build is not possible when + using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this. +*/ + +/* +REVISION HISTORY +================ +v0.7.4 - TBD + - Fix an overflow error with "Xing" and "Info" tag parsing. + - Add some validation checks for "Xing" and "Info" tag parsing. + - Reduce size of some stack allocations. + - Improvements to SIMD detection. + +v0.7.3 - 2026-01-17 + - Fix an error in drmp3_open_and_read_pcm_frames_s16() and family when memory allocation fails. + - Fix some compilation warnings. + +v0.7.2 - 2025-12-02 + - Reduce stack space to improve robustness on embedded systems. + - Fix a compilation error with MSVC Clang toolset relating to cpuid. + - Fix an error with APE tag parsing. + +v0.7.1 - 2025-09-10 + - Silence a warning with GCC. + - Fix an error with the NXDK build. + - Fix a decoding inconsistency when seeking. Prior to this change, reading to the end of the stream immediately after initializing will result in a different number of samples read than if the stream is seeked to the start and read to the end. + +v0.7.0 - 2025-07-23 + - The old `DRMP3_IMPLEMENTATION` has been removed. Use `DR_MP3_IMPLEMENTATION` instead. The reason for this change is that in the future everything will eventually be using the underscored naming convention in the future, so `drmp3` will become `dr_mp3`. + - API CHANGE: Seek origins have been renamed to match the naming convention used by dr_wav and my other libraries. + - drmp3_seek_origin_start -> DRMP3_SEEK_SET + - drmp3_seek_origin_current -> DRMP3_SEEK_CUR + - DRMP3_SEEK_END (new) + - API CHANGE: Add DRMP3_SEEK_END as a seek origin for the seek callback. This is required for detection of ID3v1 and APE tags. + - API CHANGE: Add onTell callback to `drmp3_init()`. This is needed in order to track the location of ID3v1 and APE tags. + - API CHANGE: Add onMeta callback to `drmp3_init()`. This is used for reporting tag data back to the caller. Currently this only reports the raw tag data which means applications need to parse the data themselves. + - API CHANGE: Rename `drmp3dec_frame_info.hz` to `drmp3dec_frame_info.sample_rate`. + - Add detection of ID3v2, ID3v1, APE and Xing/VBRI tags. This should fix errors with some files where the decoder was reading tags as audio data. + - Delay and padding samples from LAME tags are now handled. + - Fix compilation for AIX OS. + +v0.6.40 - 2024-12-17 + - Improve detection of ARM64EC + +v0.6.39 - 2024-02-27 + - Fix a Wdouble-promotion warning. + +v0.6.38 - 2023-11-02 + - Fix build for ARMv6-M. + +v0.6.37 - 2023-07-07 + - Silence a static analysis warning. + +v0.6.36 - 2023-06-17 + - Fix an incorrect date in revision history. No functional change. + +v0.6.35 - 2023-05-22 + - Minor code restructure. No functional change. + +v0.6.34 - 2022-09-17 + - Fix compilation with DJGPP. + - Fix compilation when compiling with x86 with no SSE2. + - Remove an unnecessary variable from the drmp3 structure. + +v0.6.33 - 2022-04-10 + - Fix compilation error with the MSVC ARM64 build. + - Fix compilation error on older versions of GCC. + - Remove some unused functions. + +v0.6.32 - 2021-12-11 + - Fix a warning with Clang. + +v0.6.31 - 2021-08-22 + - Fix a bug when loading from memory. + +v0.6.30 - 2021-08-16 + - Silence some warnings. + - Replace memory operations with DRMP3_* macros. + +v0.6.29 - 2021-08-08 + - Bring up to date with minimp3. + +v0.6.28 - 2021-07-31 + - Fix platform detection for ARM64. + - Fix a compilation error with C89. + +v0.6.27 - 2021-02-21 + - Fix a warning due to referencing _MSC_VER when it is undefined. + +v0.6.26 - 2021-01-31 + - Bring up to date with minimp3. + +v0.6.25 - 2020-12-26 + - Remove DRMP3_DEFAULT_CHANNELS and DRMP3_DEFAULT_SAMPLE_RATE which are leftovers from some removed APIs. + +v0.6.24 - 2020-12-07 + - Fix a typo in version date for 0.6.23. + +v0.6.23 - 2020-12-03 + - Fix an error where a file can be closed twice when initialization of the decoder fails. + +v0.6.22 - 2020-12-02 + - Fix an error where it's possible for a file handle to be left open when initialization of the decoder fails. + +v0.6.21 - 2020-11-28 + - Bring up to date with minimp3. + +v0.6.20 - 2020-11-21 + - Fix compilation with OpenWatcom. + +v0.6.19 - 2020-11-13 + - Minor code clean up. + +v0.6.18 - 2020-11-01 + - Improve compiler support for older versions of GCC. + +v0.6.17 - 2020-09-28 + - Bring up to date with minimp3. + +v0.6.16 - 2020-08-02 + - Simplify sized types. + +v0.6.15 - 2020-07-25 + - Fix a compilation warning. + +v0.6.14 - 2020-07-23 + - Fix undefined behaviour with memmove(). + +v0.6.13 - 2020-07-06 + - Fix a bug when converting from s16 to f32 in drmp3_read_pcm_frames_f32(). + +v0.6.12 - 2020-06-23 + - Add include guard for the implementation section. + +v0.6.11 - 2020-05-26 + - Fix use of uninitialized variable error. + +v0.6.10 - 2020-05-16 + - Add compile-time and run-time version querying. + - DRMP3_VERSION_MINOR + - DRMP3_VERSION_MAJOR + - DRMP3_VERSION_REVISION + - DRMP3_VERSION_STRING + - drmp3_version() + - drmp3_version_string() + +v0.6.9 - 2020-04-30 + - Change the `pcm` parameter of drmp3dec_decode_frame() to a `const drmp3_uint8*` for consistency with internal APIs. + +v0.6.8 - 2020-04-26 + - Optimizations to decoding when initializing from memory. + +v0.6.7 - 2020-04-25 + - Fix a compilation error with DR_MP3_NO_STDIO + - Optimization to decoding by reducing some data movement. + +v0.6.6 - 2020-04-23 + - Fix a minor bug with the running PCM frame counter. + +v0.6.5 - 2020-04-19 + - Fix compilation error on ARM builds. + +v0.6.4 - 2020-04-19 + - Bring up to date with changes to minimp3. + +v0.6.3 - 2020-04-13 + - Fix some pedantic warnings. + +v0.6.2 - 2020-04-10 + - Fix a crash in drmp3_open_*_and_read_pcm_frames_*() if the output config object is NULL. + +v0.6.1 - 2020-04-05 + - Fix warnings. + +v0.6.0 - 2020-04-04 + - API CHANGE: Remove the pConfig parameter from the following APIs: + - drmp3_init() + - drmp3_init_memory() + - drmp3_init_file() + - Add drmp3_init_file_w() for opening a file from a wchar_t encoded path. + +v0.5.6 - 2020-02-12 + - Bring up to date with minimp3. + +v0.5.5 - 2020-01-29 + - Fix a memory allocation bug in high level s16 decoding APIs. + +v0.5.4 - 2019-12-02 + - Fix a possible null pointer dereference when using custom memory allocators for realloc(). + +v0.5.3 - 2019-11-14 + - Fix typos in documentation. + +v0.5.2 - 2019-11-02 + - Bring up to date with minimp3. + +v0.5.1 - 2019-10-08 + - Fix a warning with GCC. + +v0.5.0 - 2019-10-07 + - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation + routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs: + - drmp3_init() + - drmp3_init_file() + - drmp3_init_memory() + - drmp3_open_and_read_pcm_frames_f32() + - drmp3_open_and_read_pcm_frames_s16() + - drmp3_open_memory_and_read_pcm_frames_f32() + - drmp3_open_memory_and_read_pcm_frames_s16() + - drmp3_open_file_and_read_pcm_frames_f32() + - drmp3_open_file_and_read_pcm_frames_s16() + - API CHANGE: Renamed the following APIs: + - drmp3_open_and_read_f32() -> drmp3_open_and_read_pcm_frames_f32() + - drmp3_open_and_read_s16() -> drmp3_open_and_read_pcm_frames_s16() + - drmp3_open_memory_and_read_f32() -> drmp3_open_memory_and_read_pcm_frames_f32() + - drmp3_open_memory_and_read_s16() -> drmp3_open_memory_and_read_pcm_frames_s16() + - drmp3_open_file_and_read_f32() -> drmp3_open_file_and_read_pcm_frames_f32() + - drmp3_open_file_and_read_s16() -> drmp3_open_file_and_read_pcm_frames_s16() + +v0.4.7 - 2019-07-28 + - Fix a compiler error. + +v0.4.6 - 2019-06-14 + - Fix a compiler error. + +v0.4.5 - 2019-06-06 + - Bring up to date with minimp3. + +v0.4.4 - 2019-05-06 + - Fixes to the VC6 build. + +v0.4.3 - 2019-05-05 + - Use the channel count and/or sample rate of the first MP3 frame instead of DRMP3_DEFAULT_CHANNELS and + DRMP3_DEFAULT_SAMPLE_RATE when they are set to 0. To use the old behaviour, just set the relevant property to + DRMP3_DEFAULT_CHANNELS or DRMP3_DEFAULT_SAMPLE_RATE. + - Add s16 reading APIs + - drmp3_read_pcm_frames_s16 + - drmp3_open_memory_and_read_pcm_frames_s16 + - drmp3_open_and_read_pcm_frames_s16 + - drmp3_open_file_and_read_pcm_frames_s16 + - Add drmp3_get_mp3_and_pcm_frame_count() to the public header section. + - Add support for C89. + - Change license to choice of public domain or MIT-0. + +v0.4.2 - 2019-02-21 + - Fix a warning. + +v0.4.1 - 2018-12-30 + - Fix a warning. + +v0.4.0 - 2018-12-16 + - API CHANGE: Rename some APIs: + - drmp3_read_f32 -> to drmp3_read_pcm_frames_f32 + - drmp3_seek_to_frame -> drmp3_seek_to_pcm_frame + - drmp3_open_and_decode_f32 -> drmp3_open_and_read_pcm_frames_f32 + - drmp3_open_and_decode_memory_f32 -> drmp3_open_memory_and_read_pcm_frames_f32 + - drmp3_open_and_decode_file_f32 -> drmp3_open_file_and_read_pcm_frames_f32 + - Add drmp3_get_pcm_frame_count(). + - Add drmp3_get_mp3_frame_count(). + - Improve seeking performance. + +v0.3.2 - 2018-09-11 + - Fix a couple of memory leaks. + - Bring up to date with minimp3. + +v0.3.1 - 2018-08-25 + - Fix C++ build. + +v0.3.0 - 2018-08-25 + - Bring up to date with minimp3. This has a minor API change: the "pcm" parameter of drmp3dec_decode_frame() has + been changed from short* to void* because it can now output both s16 and f32 samples, depending on whether or + not the DR_MP3_FLOAT_OUTPUT option is set. + +v0.2.11 - 2018-08-08 + - Fix a bug where the last part of a file is not read. + +v0.2.10 - 2018-08-07 + - Improve 64-bit detection. + +v0.2.9 - 2018-08-05 + - Fix C++ build on older versions of GCC. + - Bring up to date with minimp3. + +v0.2.8 - 2018-08-02 + - Fix compilation errors with older versions of GCC. + +v0.2.7 - 2018-07-13 + - Bring up to date with minimp3. + +v0.2.6 - 2018-07-12 + - Bring up to date with minimp3. + +v0.2.5 - 2018-06-22 + - Bring up to date with minimp3. + +v0.2.4 - 2018-05-12 + - Bring up to date with minimp3. + +v0.2.3 - 2018-04-29 + - Fix TCC build. + +v0.2.2 - 2018-04-28 + - Fix bug when opening a decoder from memory. + +v0.2.1 - 2018-04-27 + - Efficiency improvements when the decoder reaches the end of the stream. + +v0.2 - 2018-04-21 + - Bring up to date with minimp3. + - Start using major.minor.revision versioning. + +v0.1d - 2018-03-30 + - Bring up to date with minimp3. + +v0.1c - 2018-03-11 + - Fix C++ build error. + +v0.1b - 2018-03-07 + - Bring up to date with minimp3. + +v0.1a - 2018-02-28 + - Fix compilation error on GCC/Clang. + - Fix some warnings. + +v0.1 - 2018-02-xx + - Initial versioned release. +*/ + +/* +This software is available as a choice of the following licenses. Choose +whichever you prefer. + +=============================================================================== +ALTERNATIVE 1 - Public Domain (www.unlicense.org) +=============================================================================== +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + +=============================================================================== +ALTERNATIVE 2 - MIT No Attribution +=============================================================================== +Copyright 2023 David Reid + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +/* + https://github.com/lieff/minimp3 + To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. + This software is distributed without any warranty. + See . +*/ diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.cpp new file mode 100644 index 00000000..e43fbcfc --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.cpp @@ -0,0 +1,1163 @@ +// GTA IV PS5 - the hub's check of the game's files and its installer (see +// hub_install.h). The install itself is LibertyRecomp's (install/), written by +// the installer payload (ps5/payload/installer) when a payload loader runs it. +#include "hub_install.h" + +#include "hub_lang.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if defined(__PROSPERO__) +#include +#include +#include +extern "C" int _fstatfs(int fd, struct statfs* buf); +extern "C" int32_t sceKernelAvailableFlexibleMemorySize(size_t* size); +#endif + +#include "../../install/gta4_install_io.h" +#include "../../install/gta4_installer.h" +#include "../../install/gta4_source_inspector.h" + +namespace gta4ps5::hub::install { + +namespace { + +std::string Lower(std::string s) { + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return char(std::tolower(c)); }); + return s; +} + +// An Xbox 360 content package (a title update, an episode): CON / LIVE / PIRS. +bool IsPackage(const fs::path& file) { + char magic[4] = {}; + std::ifstream in(file, std::ios::binary); + if (!in.read(magic, 4)) return false; + return !std::memcmp(magic, "CON ", 4) || !std::memcmp(magic, "LIVE", 4) || !std::memcmp(magic, "PIRS", 4); +} + +// A content package's kind, from its header (one small read): GTA IV's title +// update or an episode (marketplace content), or something else; for an +// episode, which, from the name in the header (-1: not known from it). +// (Opening an episode's package reads its whole file table: seconds, never +// done here, as the hub waits on this.) +enum class PackageKind { kOther, kUpdate, kEpisode }; +struct PackageInfo { + PackageKind kind = PackageKind::kOther; + int episode = -1; +}; +PackageInfo Inspect(const fs::path& file) { + PackageInfo info; + const auto header = rex::filesystem::StfsContainerDevice::ReadPackageHeader(file); + if (!header || uint32_t(header->metadata.execution_info.title_id) != 0x545407F2) return info; + const auto type = static_cast(static_cast(header->metadata.content_type)); + if (type == 0x000B0000) { + info.kind = PackageKind::kUpdate; + } else if (type == 0x00000002) { + info.kind = PackageKind::kEpisode; + std::string name; + for (const char16_t c : header->metadata.display_name(rex::system::XLanguage::kEnglish)) + name += c < 128 ? char(std::tolower(int(c))) : ' '; + if (name.find("lost") != std::string::npos && name.find("damned") != std::string::npos) info.episode = 0; + else if (name.find("gay tony") != std::string::npos || name.find("ballad") != std::string::npos) info.episode = 1; + } + return info; +} + +// A folder of an episode's files (the package's contents copied out), at its +// top or one folder down: which episode (-1: none). Only a few file checks. +int FolderEpisode(const fs::path& dir) { + auto at = [](const fs::path& d) { + std::error_code ec; + if (!fs::is_regular_file(d / "setup2.xml", ec) || !fs::is_regular_file(d / "content.dat", ec)) return -1; + const bool e1 = fs::is_regular_file(d / "e1_audio.xml", ec), e2 = fs::is_regular_file(d / "e2_audio.xml", ec); + return e1 && !e2 ? 0 : e2 && !e1 ? 1 : -1; + }; + if (const int e = at(dir); e >= 0) return e; + for (const auto& sub : ListDir(dir)) { + std::error_code ec; + if (sub.is_directory(ec)) + if (const int e = at(sub.path()); e >= 0) return e; + } + return -1; +} + +uint64_t FolderSize(const fs::path& dir) { + uint64_t total = 0; + std::error_code ec; + for (fs::recursive_directory_iterator it(dir, ec), end; !ec && it != end; it.increment(ec)) + if (it->is_regular_file(ec)) total += it->file_size(ec); + return total; +} + +} // namespace + +std::vector ListDir(const fs::path& dir) { + std::vector out; + std::error_code ec; + for (fs::directory_iterator it(dir, ec), end; !ec && it != end; it.increment(ec)) out.push_back(*it); + return out; +} + +std::string Bytes(uint64_t n) { + char buf[32]; + if (n >= (1ull << 30)) std::snprintf(buf, sizeof(buf), "%.1f GB", double(n) / double(1ull << 30)); + else if (n >= (1ull << 20)) std::snprintf(buf, sizeof(buf), "%.0f MB", double(n) / double(1ull << 20)); + else std::snprintf(buf, sizeof(buf), "%.0f KB", double(n) / 1024.0); + return buf; +} + +fs::path InstallFolder(const fs::path& app) { return app / "install"; } +fs::path GameRoot(const fs::path& app) { return app / "UserData" / "LibertyRecomp" / "game"; } +namespace { +fs::path EpisodesRoot(const fs::path& app) { return app / "UserData" / "LibertyRecomp" / "dlc"; } +} // namespace + +const char* EpisodeName(int episode) { return episode == 1 ? "The Ballad of Gay Tony" : "The Lost and Damned"; } + +State Look(const fs::path& app) { + State s; + s.installed = gta4::install::IsInstallReady(GameRoot(app), &s.reason); + const Sources found = Find(app); + s.iso = !found.disc.empty(); + s.update = !found.update.empty(); + for (int e = 0; e < kEpisodeCount; ++e) { + s.episode[e] = gta4::install::IsEpisodeReady(EpisodesRoot(app), gta4::install::Episode(e)); + s.episode_found[e] = !found.episode[e].empty(); + } + REXLOG_INFO("PS5 hub: installed {}, disc image {}, title update {}{}; episodes installed {}/{}, found {}/{}", + s.installed, s.iso, s.update, s.installed ? "" : " (" + s.reason + ")", s.episode[0], s.episode[1], + s.episode_found[0], s.episode_found[1]); + return s; +} + +Sources Find(const fs::path& app) { + Sources s; + std::error_code ec; + auto entries = ListDir(InstallFolder(app)); + std::sort(entries.begin(), entries.end(), + [](const fs::directory_entry& a, const fs::directory_entry& b) { return a.path() < b.path(); }); + fs::path package; + uint64_t package_bytes = 0; + for (const auto& e : entries) { + const fs::path& p = e.path(); + const std::string name = Lower(p.filename().string()); + if (name.empty() || name[0] == '.') continue; + if (e.is_directory(ec)) { + // an extracted game: a folder with default.xex in it + if (fs::is_regular_file(p / "default.xex", ec)) { + if (s.disc.empty()) s.disc = p, s.disc_bytes = FolderSize(p); + continue; + } + // an episode's files copied out of its package + if (const int k = FolderEpisode(p); k >= 0 && s.episode[k].empty()) s.episode[k] = p, s.episode_bytes[k] = FolderSize(p); + continue; + } + if (!e.is_regular_file(ec)) continue; + const uint64_t size = e.file_size(ec); + const std::string ext = Lower(p.extension().string()); + if (ext == ".iso") { + if (s.disc.empty()) s.disc = p, s.disc_bytes = size; + } else if (ext == ".xexp" || name.rfind("tu_", 0) == 0) { + if (s.update.empty()) s.update = p, s.update_bytes = size; + } else if (IsPackage(p)) { + // (what it is, from its header) + const PackageInfo info = Inspect(p); + switch (info.kind) { + case PackageKind::kEpisode: + if (info.episode < 0) s.other_episodes.push_back(p); // (opened when an episode is installed) + else if (s.episode[info.episode].empty()) s.episode[info.episode] = p, s.episode_bytes[info.episode] = size; + break; + case PackageKind::kUpdate: + if (s.update.empty()) s.update = p, s.update_bytes = size; + break; + case PackageKind::kOther: + // (a package the header doesn't say: the smallest one, as the episodes + // are far bigger) + if (package.empty() || size < package_bytes) package = p, package_bytes = size; + break; + } + } + } + if (s.update.empty() && !package.empty()) s.update = package, s.update_bytes = package_bytes; + return s; +} + +#if defined(__PROSPERO__) +namespace { +// The kernel's statfs for a path (the SDK's statvfs is a stand-in). A title +// can't open a folder, so for a folder a file is made in it for the moment. +bool StatFs(const fs::path& path, struct statfs& st) { + std::error_code ec; + int fd = -1; + fs::path probe; + if (fs::is_directory(path, ec)) { + probe = path / ".drive-probe"; + fd = ::open(probe.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + } else { + fd = ::open(path.c_str(), O_RDONLY); + } + if (fd < 0) return false; + const bool ok = _fstatfs(fd, &st) == 0; + ::close(fd); + if (!probe.empty()) ::unlink(probe.c_str()); + return ok; +} +} // namespace +#endif + +std::string Drive::Name() const { // (shown in the hub: in its language) + if (type.empty()) return hub::lang::T("the app's drive"); + if (type == "ufs") return hub::lang::T("internal storage"); + if (type.find("exfat") != std::string::npos) return hub::lang::T("an exFAT drive"); + if (type.find("msdos") != std::string::npos || type.find("fat") != std::string::npos) return hub::lang::T("a FAT drive"); + return hub::lang::F("a {} drive", {type}); +} + +Drive DriveOf(const fs::path& path) { + Drive d; +#if defined(__PROSPERO__) + std::error_code ec; + if (!fs::exists(path, ec)) fs::create_directories(path, ec); + struct statfs st {}; + if (!StatFs(path, st)) return d; + d.type = st.f_fstypename; + d.free = uint64_t(st.f_bavail) * uint64_t(st.f_bsize); + d.folder = path.string(); + d.device = st.f_mntfromname; + // (/app0 is a nullfs mount of the app's real folder: follow it to the drive) + if (d.type == "nullfs") { + const std::string on = st.f_mntonname, from = st.f_mntfromname, p = path.string(); + if (p.rfind(on, 0) == 0) d.folder = from + p.substr(on.size()); + d.type = "nullfs"; + struct statfs lower {}; + if (StatFs(d.folder, lower)) { + d.type = lower.f_fstypename; + d.free = uint64_t(lower.f_bavail) * uint64_t(lower.f_bsize); + d.device = lower.f_mntfromname; + } + } +#else + (void)path; +#endif + return d; +} + +uint64_t FreeSpace(const fs::path& install_root) { + std::error_code ec; + fs::create_directories(install_root, ec); +#if defined(__PROSPERO__) + if (const Drive d = DriveOf(install_root); !d.type.empty()) return d.free; +#endif + const auto space = fs::space(install_root, ec); + // (on the PS5 a title can't learn the drive's free space: the SDK's + // statvfs always answers 16 GiB, so that means "not known") + if (ec || uint64_t(space.available) == (16ull << 30)) return 0; + return uint64_t(space.available); +} + +// ------------------------------------------------------------------ the installer payload + +namespace { + +using Clock = std::chrono::steady_clock; + +// The installer payload, packaged in the app (ps5/payload/installer). +constexpr const char* kPayloadElf = "payloads/gta4-ps5-installer.elf"; +// The install's job files, in the install root. The payload looks for them at +// /UserData/LibertyRecomp/.install-job. +constexpr const char* kJobDir = ".install-job"; + +// `path` as a path inside the app's folder ("install/x.iso"), or "" when it +// isn't in it. +std::string InsideApp(const fs::path& app, const fs::path& path) { + const fs::path rel = path.lexically_normal().lexically_relative(app.lexically_normal()); + if (rel.empty() || rel.is_absolute()) return {}; + const std::string first = rel.begin()->string(); + if (first == ".." || first == ".") return {}; + return rel.generic_string(); +} + +bool ReadText(const fs::path& path, std::string& out) { + std::ifstream in(path, std::ios::binary); + if (!in) return false; + out.assign(std::istreambuf_iterator(in), std::istreambuf_iterator()); + return true; +} + +// Whole, at once: written beside, then renamed over (never read half done). +bool WriteText(const fs::path& path, const std::string& text) { + const fs::path tmp = path.string() + ".tmp"; + { + std::ofstream out(tmp, std::ios::binary | std::ios::trunc); + if (!out.write(text.data(), std::streamsize(text.size()))) return false; + } + std::error_code ec; + fs::rename(tmp, path, ec); + return !ec; +} + +std::string Field(const std::string& text, const char* key) { + const std::string k = std::string(key) + "="; + size_t at = 0; + while (at < text.size()) { + size_t end = text.find('\n', at); + if (end == std::string::npos) end = text.size(); + if (text.compare(at, k.size(), k) == 0) return text.substr(at + k.size(), end - at - k.size()); + at = end + 1; + } + return {}; +} + +uint64_t Number(const std::string& text, const char* key) { + return std::strtoull(Field(text, key).c_str(), nullptr, 10); +} + +// Seconds since `path` was written (a lot when it isn't there). +double Age(const fs::path& path) { + struct stat st {}; + if (::stat(path.c_str(), &st) != 0) return 1e9; + return std::difftime(std::time(nullptr), st.st_mtime); +} + +// What the payload last wrote to status.txt. +struct PayloadStatus { + std::string token, phase, error, file, folder, drive; + int step = 0; + uint64_t copy_done = 0, copy_total = 0, unpack_done = 0, unpack_total = 0, unpack_guess = 0; + uint64_t file_size = 0, read_bytes = 0, read_ns = 0, write_bytes = 0, write_ns = 0, free = 0, beat = 0; + float seconds = 0; + // still at it (the phases before done, failed or stopped) + bool Working() const { return phase != "done" && phase != "failed" && phase != "stopped"; } +}; + +bool ReadStatus(const fs::path& dir, PayloadStatus& s) { + std::string text; + if (!ReadText(dir / "status.txt", text)) return false; + s.token = Field(text, "token"); + s.phase = Field(text, "phase"); + s.error = Field(text, "error"); + s.file = Field(text, "file"); + s.folder = Field(text, "folder"); + s.drive = Field(text, "drive"); + s.step = int(Number(text, "step")); + s.copy_done = Number(text, "copy_done"); + s.copy_total = Number(text, "copy_total"); + s.unpack_done = Number(text, "unpack_done"); + s.unpack_total = Number(text, "unpack_total"); + s.unpack_guess = Number(text, "unpack_guess"); + s.file_size = Number(text, "file_size"); + s.read_bytes = Number(text, "read_bytes"); + s.read_ns = Number(text, "read_ns"); + s.write_bytes = Number(text, "write_bytes"); + s.write_ns = Number(text, "write_ns"); + s.free = Number(text, "free"); + s.beat = Number(text, "beat"); + s.seconds = std::strtof(Field(text, "seconds").c_str(), nullptr); + return !s.token.empty() && !s.phase.empty(); +} + +// The payload to the console's payload loader (etaHEN, elfldr or any other) +// on 127.0.0.1:9021, which runs it as a process of its own. +bool SendToLoader(const std::string& elf, int& err) { + err = 0; + const int fd = ::socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + err = errno; + return false; + } + timeval limit{}; + limit.tv_sec = 3; + ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &limit, sizeof(limit)); + ::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &limit, sizeof(limit)); +#ifdef SO_NOSIGPIPE + const int one = 1; // (a loader that hangs up mustn't end the app) + ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); +#endif +#ifdef MSG_NOSIGNAL + const int flags = MSG_NOSIGNAL; +#else + const int flags = 0; +#endif + sockaddr_in sa{}; + sa.sin_family = AF_INET; + sa.sin_port = htons(9021); + sa.sin_addr.s_addr = htonl(0x7F000001); + if (::connect(fd, reinterpret_cast(&sa), sizeof(sa)) != 0) { + err = errno; + ::close(fd); + return false; + } + size_t at = 0; + while (at < elf.size()) { + const ssize_t n = ::send(fd, elf.data() + at, elf.size() - at, flags); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) { + err = errno; + ::close(fd); + return false; + } + at += size_t(n); + } + ::shutdown(fd, SHUT_WR); + ::close(fd); + return true; +} + +std::string Hex(const std::vector& bytes) { + static constexpr char kDigits[] = "0123456789abcdef"; + std::string out; + for (const uint8_t b : bytes) out += kDigits[b >> 4], out += kDigits[b & 15]; + return out; +} + +std::string Seconds(uint64_t ns) { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.0f s", double(ns) / 1e9); + return buf; +} + +} // namespace + +// ------------------------------------------------------------------ the job + +struct Job::Impl { + enum class Outcome { kUnavailable, kDone, kFailed, kStopped, kLeft }; + + fs::path app, root; + Sources found; + mutable std::mutex m; + std::condition_variable cv; + Status st; + bool go = false; + std::atomic quit{false}, stop{false}; + gta4::install::Progress progress; + Clock::time_point started; + std::thread thread, monitor; + std::atomic monitoring{false}; + Drive drive; + std::string where; // the install drive, for the screen (installing in the app) + std::string failure; // why the payload's install failed + std::string token; // the payload's job + // an episode instead of the game: which (0, 1; -1 the game), its package or + // folder and what's copied from it + int episode = -1; + fs::path episode_source; + gta4::install::EpisodePlan episode_plan; + + fs::path JobDir() const { return root / kJobDir; } + + void Set(Phase phase, std::string problem = {}) { + std::lock_guard lock(m); + st.phase = phase; + st.problem = std::move(problem); + st.stopping = false; + } + + // The job's files, once it's over ("running" stays: a payload's lock). + void ClearJob() { + std::error_code ec; + for (const char* name : {"job.txt", "job.txt.tmp", "job.taken", "cancel", "cancel.tmp", "status.txt", "status.tmp"}) + fs::remove(JobDir() / name, ec); + } + + // A job no payload has taken yet, taken back (job.txt deleted). False when one + // took it first (it renames job.txt to job.taken), so exactly one of them has it. + bool TakeBack() { + std::error_code ec; + return fs::remove(JobDir() / "job.txt", ec) && !ec; + } + + // A payload at work right now (its status written in the last 15 s). + bool PayloadWorking(PayloadStatus& s) const { + return ReadStatus(JobDir(), s) && s.Working() && Age(JobDir() / "status.txt") <= 15; + } + + // ---------------------------------------------- with the installer payload + + // How fast it's going, from the payload's numbers over the last second or so. + struct Rates { + Clock::time_point at{}, logged{}; + uint64_t written = 0, rb = 0, rns = 0, wb = 0, wns = 0; + float rate = 0, read = 0, write = 0, shown = 0; + }; + + void Show(const PayloadStatus& s, Rates& r) { + const auto now = Clock::now(); + const uint64_t written = s.copy_done + s.unpack_done; + const uint64_t unpack = std::max(s.unpack_total, s.unpack_guess); + // two steps: the disc image's files copied (45%), the archives unpacked + // (their total grows as each one is opened, so it starts as a guess) + float f = 0; + if (episode >= 0) { + // (an episode: copying, and that's all) + f = s.copy_total ? float(double(s.copy_done) / double(s.copy_total)) : 0; + } else if (s.step >= 2) { + f = 0.45f + 0.55f * float(double(s.unpack_done) / double(std::max(unpack, 1))); + } else if (s.copy_total) { + f = 0.45f * float(double(s.copy_done) / double(s.copy_total)); + } + r.shown = std::max(r.shown, std::min(f, 0.99f)); + if (r.at == Clock::time_point{}) { + r.at = now, r.written = written, r.rb = s.read_bytes, r.rns = s.read_ns, r.wb = s.write_bytes, r.wns = s.write_ns; + } + const float dt = std::chrono::duration(now - r.at).count(); + if (dt >= 1.0f) { + const float w = float(double(written - std::min(written, r.written)) / dt); + r.rate = r.rate > 0 ? r.rate * 0.6f + w * 0.4f : w; + // (bytes over the time spent at it: one thread's speed) + if (s.read_ns > r.rns) r.read = float(double(s.read_bytes - std::min(s.read_bytes, r.rb)) / (double(s.read_ns - r.rns) / 1e9)); + if (s.write_ns > r.wns) r.write = float(double(s.write_bytes - std::min(s.write_bytes, r.wb)) / (double(s.write_ns - r.wns) / 1e9)); + r.at = now, r.written = written, r.rb = s.read_bytes, r.rns = s.read_ns, r.wb = s.write_bytes, r.wns = s.write_ns; + } + Drive d; + d.type = s.drive.substr(0, s.drive.find(' ')); + std::lock_guard lock(m); + st.step = s.step >= 2 ? 2 : 1; + st.progress = r.shown; + st.written = written; + st.seconds = std::chrono::duration(now - started).count(); + st.rate = r.rate; + st.read_rate = r.read; + st.write_rate = r.write; + if (!d.type.empty()) st.drive = d.Name(); + // time left: what's still to write, at the speed of late + st.eta = -1; + if (s.copy_total && r.rate > 0 && s.seconds > 8) { + const uint64_t expected = episode >= 0 ? s.copy_total : s.copy_total + (unpack ? unpack : s.copy_total * 23 / 20); + st.eta = float(double(expected - std::min(expected, written)) / double(r.rate)); + } + st.diag_file = s.file.empty() ? std::string() : "Writing " + s.file + " (" + Bytes(s.file_size) + ")"; + st.diag_time = "Installer payload \xC2\xB7 " + s.phase + " \xC2\xB7 reading " + Seconds(s.read_ns) + + ", writing " + Seconds(s.write_ns); + st.diag_drive = s.folder + " \xC2\xB7 " + (s.drive.empty() ? std::string("drive not known") : s.drive) + + " \xC2\xB7 " + Bytes(s.free) + " free"; + if (now - r.logged >= std::chrono::seconds(10)) { + r.logged = now; + REXLOG_INFO("PS5 installer: payload {} step {} {:.1f}%: copied {} of {}, unpacked {} of {}, {:.0f} MB/s now " + "(read {:.0f} / write {:.0f} MB/s per thread); {}", + s.phase, st.step, st.progress * 100, Bytes(s.copy_done), Bytes(s.copy_total), Bytes(s.unpack_done), + Bytes(unpack), double(r.rate) / double(1 << 20), double(r.read) / double(1 << 20), + double(r.write) / double(1 << 20), st.diag_file); + } + } + + // Follows the payload's status until it's done, failed or stopped. Stopping + // (Cancel) is passed on as the cancel file. + Outcome Follow(const std::string& token, bool attached) { + const auto sent = Clock::now(); + auto changed = sent; + uint64_t beat = 0; + bool running = attached, asked = false; + Rates rates; + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + if (quit.load()) return Outcome::kLeft; // (the app is closing: the payload carries on) + const auto now = Clock::now(); + if (stop.load() && !asked) { + asked = true; + // (not taken yet: taken back, and that's the end of it) + if (!running && TakeBack()) return Outcome::kStopped; + WriteText(JobDir() / "cancel", "stop\n"); + REXLOG_INFO("PS5 installer: asked the payload to stop"); + std::lock_guard lock(m); + st.stopping = true; + } + PayloadStatus s; + if (ReadStatus(JobDir(), s) && s.token == token) { + if (!running) { + REXLOG_INFO("PS5 installer: the payload started after {:.1f} s", + std::chrono::duration(now - sent).count()); + } + running = true; + if (s.beat != beat) beat = s.beat, changed = now; + if (s.phase == "done") { + Show(s, rates); + return Outcome::kDone; + } + if (s.phase == "stopped") return Outcome::kStopped; + if (s.phase == "failed") { + failure = s.error.empty() ? std::string("The installer payload stopped with an error.") : s.error; + return Outcome::kFailed; + } + if (s.phase == "cleaning") { + std::lock_guard lock(m); + st.stopping = true; + } else if (!asked) { + Show(s, rates); + } + } + if (!running && now - sent > std::chrono::seconds(15)) { + // Not started: the job taken back (one that starts late finds none), and the + // app installs by itself. Taken but quiet: given longer. + if (TakeBack()) return Outcome::kUnavailable; + if (now - sent > std::chrono::seconds(45)) { + WriteText(JobDir() / "cancel", "stop\n"); + failure = "The installer payload didn't start."; + return Outcome::kFailed; + } + } + if (running && now - changed > std::chrono::seconds(30)) { + failure = "The installer payload stopped answering."; + return Outcome::kFailed; + } + } + } + + // The install, written by the payload. kUnavailable (and why) when it can't + // be used: then the app installs by itself. + // The job written and the payload sent to the loader (kUnavailable: no + // loader, or no payload in the app). + Outcome SendJob(const std::string& body, std::string& why_not) { + std::string elf; + if (!ReadText(app / kPayloadElf, elf) || elf.size() < 64 || elf.compare(0, 4, "\x7F" "ELF") != 0) { + why_not = "The installer payload is missing from the app"; + return Outcome::kUnavailable; + } + // the job (an earlier one's files gone first; never while a payload is at it) + const fs::path dir = JobDir(); + std::error_code ec; + fs::create_directories(dir, ec); + if (PayloadStatus busy; PayloadWorking(busy)) { + failure = "An earlier install is still running. Wait for it to finish, then try again."; + return Outcome::kFailed; + } + ClearJob(); + char made[48]; + std::snprintf(made, sizeof(made), "%lld-%lld", (long long)std::time(nullptr), + (long long)(Clock::now().time_since_epoch().count() % 1000000)); + token = made; + if (!WriteText(dir / "job.txt", "token=" + token + "\n" + body)) { + why_not = "The install job couldn't be written"; + return Outcome::kUnavailable; + } + int err = 0; + if (!SendToLoader(elf, err)) { + fs::remove(dir / "job.txt", ec); + REXLOG_WARN("PS5 installer: no payload loader answered on port 9021 (errno {})", err); + why_not = "No payload loader answered on port 9021"; + return Outcome::kUnavailable; + } + REXLOG_INFO("PS5 installer: sent the installer payload ({} KB) to the payload loader on port 9021, job {}", + elf.size() / 1024, token); + { + std::lock_guard lock(m); + st.diag_time = "Starting the installer payload"; + } + const Outcome outcome = Follow(token, false); + if (outcome == Outcome::kUnavailable) why_not = "The payload loader didn't start the installer"; + return outcome; + } + + // An episode, written by the payload. + Outcome EpisodeWithPayload(std::string& why_not) { + if (root.lexically_normal() != (app / "UserData" / "LibertyRecomp").lexically_normal()) { + why_not = "The install folder isn't where the installer payload looks for it"; + return Outcome::kUnavailable; + } + const std::string source = InsideApp(app, episode_source), staging = InsideApp(app, root / ".install-staging"); + if (source.empty() || staging.empty()) { + why_not = "The episode isn't in the app's folder"; + return Outcome::kUnavailable; + } + return SendJob("kind=episode\nstaging=" + staging + "\nsource=" + source + + "\nsource_kind=" + (episode_plan.package ? "package" : "folder") + "\nroot=" + + episode_plan.root + "\nname=" + gta4::install::EpisodeDirectory(gta4::install::Episode(episode)) + + "\nneeded=" + std::to_string(EpisodeSpaceNeeded()) + "\n", + why_not); + } + + uint64_t EpisodeSpaceNeeded() const { return episode_plan.copy_bytes + (256ull << 20); } + + Outcome WithPayload(std::string& why_not) { + if (episode >= 0) return EpisodeWithPayload(why_not); + // (the payload finds the job at /UserData/LibertyRecomp) + if (root.lexically_normal() != (app / "UserData" / "LibertyRecomp").lexically_normal()) { + why_not = "The install folder isn't where the installer payload looks for it"; + return Outcome::kUnavailable; + } + const std::string image = InsideApp(app, found.disc), update = InsideApp(app, found.update), + staging = InsideApp(app, root / ".install-staging"); + if (image.empty() || update.empty() || staging.empty()) { + why_not = "The disc image isn't in the app's folder"; + return Outcome::kUnavailable; + } + gta4::install::Selection selection; + selection.game_source = found.disc; + selection.update_source = found.update; + gta4::install::InstallPlan plan; + if (!gta4::install::PlanInstall(selection, plan, failure)) return Outcome::kFailed; + std::error_code kind_ec; + return SendJob("kind=install\nstaging=" + staging + "\nimage=" + image + "\nimage_kind=" + + (fs::is_directory(found.disc, kind_ec) ? "folder" : "iso") + "\ndisc_root=" + plan.disc_root + + "\nupdate=" + update + "\nupdate_kind=" + (plan.update_package ? "package" : "xexp") + + "\nupdate_root=" + plan.update_root + "\nupdate_xexp=" + plan.update_xexp + + "\nkey=" + Hex(plan.aes_key) + "\nneeded=" + std::to_string(SpaceNeeded(found.disc_bytes)) + + "\nthreads=3\n", + why_not); + } + + // The payload's work checked against the disc image, and put in place. + void Finish(const std::string& token) { + { + std::lock_guard lock(m); + st.step = 3; + st.progress = std::max(st.progress, 0.99f); + st.eta = -1; + st.rate = 0; + st.stopping = false; + st.diag_file.clear(); + } + gta4::install::Selection selection; + selection.game_source = found.disc; + selection.update_source = found.update; + const auto t0 = Clock::now(); + // (the staging folder carries the token of the job that filled it) + std::string mark; + gta4::install::Result result; + if (!ReadText(root / ".install-staging" / ".token", mark) || mark != token) { + result.error = "The installed files aren't from this install. Press Cross to install again."; + } else if (episode >= 0) { + result = gta4::install::FinishEpisode(episode_source, gta4::install::Episode(episode), root); + } else { + result = gta4::install::FinishInstall(selection, root); + } + ClearJob(); + { + std::lock_guard lock(m); + st.seconds = std::chrono::duration(Clock::now() - started).count(); + } + if (result.success) { + REXLOG_INFO("PS5 installer: done in {:.0f} s (checked and put in place in {:.1f} s)", st.seconds, + std::chrono::duration(Clock::now() - t0).count()); + Set(Phase::kDone); + } else { + REXLOG_ERROR("PS5 installer: the installed game didn't check out: {}", result.error); + Set(Phase::kFailed, result.error); + } + } + + void Conclude(Outcome outcome, const std::string& token) { + switch (outcome) { + case Outcome::kDone: + Finish(token); + break; + case Outcome::kFailed: + // (a payload that stopped answering may still be there: what it wrote is left + // to the next install, which empties the staging folder first) + REXLOG_ERROR("PS5 installer: failed: {}", failure); + ClearJob(); + Set(Phase::kFailed, failure); + break; + case Outcome::kStopped: + REXLOG_INFO("PS5 installer: stopped"); + ClearJob(); + Set(Phase::kCancelled); + break; + case Outcome::kLeft: + case Outcome::kUnavailable: + break; + } + } + + // An install from before: the app was closed while the payload worked. One + // still going is followed again; one that finished is checked and put in + // place. True when it took over. + bool Resume() { + const fs::path dir = JobDir(); + std::string job; + PayloadStatus s; + if (!ReadText(dir / "job.taken", job) || !ReadStatus(dir, s) || s.token != Field(job, "token")) return false; + // (only this kind of install's: the game's, or this episode's) + const std::string kind = Field(job, "kind"); + if (episode >= 0 ? kind != "episode" || + Field(job, "name") != gta4::install::EpisodeDirectory(gta4::install::Episode(episode)) + : kind == "episode") + return false; + token = s.token; + std::error_code ec; + if (s.phase == "done") { + const fs::path staged = episode >= 0 ? root / ".install-staging" / "dlc" / + gta4::install::EpisodeDirectory(gta4::install::Episode(episode)) + : root / ".install-staging" / "game"; + if (!fs::is_directory(staged, ec)) return false; + REXLOG_INFO("PS5 installer: the payload finished an install while the app was closed; finishing it"); + { + std::lock_guard lock(m); + st.phase = Phase::kInstalling; + started = Clock::now(); + } + Finish(token); + return true; + } + if (!s.Working() || Age(dir / "status.txt") > 15) return false; + if (s.phase == "cleaning") { + // (one that was stopped, still removing its files: waited for) + REXLOG_INFO("PS5 installer: waiting for an earlier install to finish removing its files"); + for (int i = 0; i < 1200 && !quit.load(); ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + if (!ReadStatus(dir, s) || s.phase != "cleaning" || Age(dir / "status.txt") > 15) break; + } + return false; + } + REXLOG_INFO("PS5 installer: an install from before is still going ({}); following it", s.phase); + { + std::lock_guard lock(m); + st.phase = Phase::kInstalling; + started = Clock::now() - std::chrono::milliseconds(int64_t(s.seconds * 1000)); + } + Conclude(Follow(token, true), token); + return true; + } + + // ---------------------------------------------- in the app (no payload loader) + + // While installing: once a second, the numbers for the screen; every 10 s, + // a line in the log. On its own thread, so the screen never waits on the + // drive. + void Monitor() { + uint64_t copy_total = 0, unpack_guess = 0; + float shown = 0, rate = 0, read_rate = 0, write_rate = 0; + Clock::time_point rate_at = Clock::now(), io_at = rate_at, logged_at{}; + uint64_t rate_bytes = 0, io_rb = 0, io_rns = 0, io_wb = 0, io_wns = 0; + while (monitoring.load()) { + for (int i = 0; i < 10 && monitoring.load(); ++i) std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (!monitoring.load()) break; + const auto now = Clock::now(); + const uint64_t done = progress.copied_bytes.load(), total = progress.total_bytes.load(); + // Two steps: the disc image's files are copied (the total known up + // front), then the archives are unpacked (their total grows as each one + // is opened, so it's guessed from the archives' sizes). + if (!copy_total && total) copy_total = total; + int step = 1; + float f = 0; + if (episode >= 0) { + f = total ? float(double(done) / double(total)) : 0; // (an episode: copying, and that's all) + } else if (copy_total && total > copy_total) { + if (!unpack_guess) { + const fs::path staged = root / ".install-staging" / "game"; + std::error_code ec; + for (const char* rpf : {"common.rpf", "xbox360.rpf", "audio.rpf"}) unpack_guess += fs::file_size(staged / rpf, ec); + unpack_guess = unpack_guess * 23 / 20; + } + const uint64_t unpack_total = std::max(total - copy_total, unpack_guess); + step = 2; + f = 0.45f + 0.55f * float(double(done - std::min(done, copy_total)) / double(std::max(unpack_total, 1))); + } else if (copy_total) { + f = 0.45f * float(double(done) / double(copy_total)); + } + shown = std::max(shown, std::min(f, 0.995f)); + const float dt = std::chrono::duration(now - rate_at).count(); + if (dt >= 1.0f) { + const float r = float(double(done - std::min(done, rate_bytes)) / dt); + rate = rate > 0 ? rate * 0.6f + r * 0.4f : r; + rate_at = now, rate_bytes = done; + } + const auto& io = gta4::install::io::GetStats(); + const uint64_t rb = io.read_bytes.load(), rns = io.read_ns.load(), wb = io.write_bytes.load(), + wns = io.write_ns.load(); + if (now - io_at >= std::chrono::seconds(2)) { + // (bytes over the time spent at it: one thread's speed) + if (rns > io_rns) read_rate = float(double(rb - io_rb) / (double(rns - io_rns) / 1e9)); + if (wns > io_wns) write_rate = float(double(wb - io_wb) / (double(wns - io_wns) / 1e9)); + io_at = now, io_rb = rb, io_rns = rns, io_wb = wb, io_wns = wns; + } + size_t flexible = 0; +#if defined(__PROSPERO__) + sceKernelAvailableFlexibleMemorySize(&flexible); +#endif + std::string current; + uint64_t current_size = 0; + gta4::install::io::Current(current, current_size); + const float seconds = std::chrono::duration(now - started).count(); + char times[256]; + std::snprintf(times, sizeof(times), + "Time spent: reading %.0f s, writing %.0f s, flushing %.0f s (%llu), sizing files %.0f s " + "\xC2\xB7 %s memory free", + double(rns) / 1e9, double(wns) / 1e9, double(io.sync_ns.load()) / 1e9, + (unsigned long long)io.syncs.load(), double(io.reserve_ns.load()) / 1e9, Bytes(flexible).c_str()); + { + std::lock_guard lock(m); + st.written = done; + st.seconds = seconds; + st.step = step; + st.progress = shown; + st.rate = rate; + st.read_rate = read_rate; + st.write_rate = write_rate; + st.diag_drive = where; + st.diag_time = times; + st.diag_file = current.empty() ? std::string() + : "Writing " + fs::path(current).filename().string() + " (" + + Bytes(current_size) + ")"; + // time left: what's still to write, at the speed of late + st.eta = -1; + if (copy_total && rate > 0 && seconds > 10) { + const uint64_t unpack = episode >= 0 ? 0 + : total > copy_total ? std::max(total - copy_total, unpack_guess) + : copy_total * 23 / 20; + const uint64_t expected = copy_total + unpack; + st.eta = float(double(expected - std::min(expected, done)) / double(rate)); + } + } + if (now - logged_at >= std::chrono::seconds(10)) { + logged_at = now; + REXLOG_INFO("PS5 installer: step {} {:.1f}% {} of {} in {:.0f} s ({:.0f} MB/s now, read {:.0f} / write {:.0f} " + "MB/s per thread); {}; {} ({})", + step, shown * 100, Bytes(done), Bytes(total), seconds, double(rate) / double(1 << 20), + double(read_rate) / double(1 << 20), double(write_rate) / double(1 << 20), times, current, + Bytes(current_size)); + } + } + } + + void InApp() { + { + // how to write, for the drive it writes to (gta4_install_io.h): the + // internal storage (ufs) past the file cache, three files at a time; + // any other drive (exFAT and the like) one file at a time, each made + // bigger in 128 MB steps ahead of the writing + auto& policy = gta4::install::io::Policy(); + const bool internal = drive.internal(); + policy.preallocate = !internal; + policy.reserve_step = 128ull << 20; + policy.direct = internal; + policy.threads = internal ? 3 : 1; + std::lock_guard lock(m); + st.drive = drive.Name(); + REXLOG_INFO("PS5 installer: writing with {} thread(s), {} writes, {}", policy.threads, + policy.direct ? "direct" : "cached", policy.preallocate ? "files sized in 128 MB steps" : "no sizing"); + } + monitoring = true; + monitor = std::thread([this] { Monitor(); }); + gta4::install::Selection selection; + if (episode >= 0) { + // (an episode on its own: the installed game is checked, and kept) + selection.dlc_sources.push_back({gta4::install::Episode(episode), episode_source}); + } else { + selection.game_source = found.disc; + selection.update_source = found.update; + } + const auto result = gta4::install::Install(selection, root, progress); + monitoring = false; + if (monitor.joinable()) monitor.join(); + if (result.success) { + REXLOG_INFO("PS5 installer: done in {:.0f} s", std::chrono::duration(Clock::now() - started).count()); + Set(Phase::kDone); + } else if (progress.cancel_requested.load()) { + REXLOG_INFO("PS5 installer: cancelled"); + Set(Phase::kCancelled); + } else { + REXLOG_ERROR("PS5 installer: failed: {}", result.error); + Set(Phase::kFailed, result.error); + } + } + + // ---------------------------------------------- the job + + // An episode: its package checked, then installed as the game is. + void RunEpisode() { + const char* name = EpisodeName(episode); + episode_source = found.episode[episode]; + // (a package whose name didn't say which episode: opened to see) + for (size_t i = 0; episode_source.empty() && i < found.other_episodes.size(); ++i) { + gta4::install::EpisodePlan plan; + std::string why; + if (gta4::install::PlanEpisode(found.other_episodes[i], plan, why) && int(plan.episode) == episode) + episode_source = found.other_episodes[i]; + } + if (!episode_source.empty()) { + std::error_code ec; + std::lock_guard lock(m); + st.source = episode_source.filename().string(); + st.source_bytes = fs::is_directory(episode_source, ec) ? FolderSize(episode_source) : fs::file_size(episode_source, ec); + } + REXLOG_INFO("PS5 installer: episode {} from '{}'", name, episode_source.string()); + std::string reason; + if (!gta4::install::IsInstallReady(GameRoot(app), &reason)) { + Set(Phase::kProblem, hub::lang::T("Install GTA IV first. The episodes are added to the installed game.")); + return; + } + if (episode_source.empty()) { + Set(Phase::kProblem, + hub::lang::F("There's no package for {} in the install folder. Copy it from your Xbox 360 (on its drive " + "it's in Content/0000000000000000/545407F2/00000002) into /data/homebrew/PPSA94004/install/ " + "and press Triangle.", + {name})); + return; + } + std::string why; + if (!gta4::install::PlanEpisode(episode_source, episode_plan, why) || int(episode_plan.episode) != episode) { + Set(Phase::kProblem, hub::lang::F("This package can't be used: {}", {why})); + return; + } + { + std::lock_guard lock(m); + st.disc = std::string(hub::lang::T(episode_plan.package ? "Xbox 360 package" : "Folder")) + " \xC2\xB7 " + + hub::lang::Units(Bytes(episode_plan.copy_bytes)); + } + drive = DriveOf(root); + where = (drive.folder.empty() ? root.string() : drive.folder) + " \xC2\xB7 " + + (drive.type.empty() ? std::string("drive not known") : drive.type + " " + drive.device) + " \xC2\xB7 " + + Bytes(drive.free) + " free"; + if (Resume() || quit.load()) return; + const uint64_t needed = EpisodeSpaceNeeded(); + if (drive.free && drive.free < needed && drive.free + FolderSize(root / ".install-staging") < needed) { + Set(Phase::kProblem, + hub::lang::F("There isn't enough free space. The install needs about {} and the drive has {} free.", + {hub::lang::Units(Bytes(needed)), hub::lang::Units(Bytes(drive.free))})); + return; + } + Set(Phase::kReady); + { + std::unique_lock lock(m); + cv.wait(lock, [this] { return go || quit.load(); }); + if (quit.load()) return; + st.phase = Phase::kInstalling; + started = Clock::now(); + } + std::string why_not; + const Outcome outcome = WithPayload(why_not); + if (outcome != Outcome::kUnavailable) return Conclude(outcome, token); + if (stop.load()) return Conclude(Outcome::kStopped, token); + REXLOG_WARN("PS5 installer: {}; installing the episode in the app instead", why_not); + { + std::lock_guard lock(m); + st.note = why_not + ", so the app is installing by itself. That's much slower."; + st.diag_time.clear(); + } + InApp(); + } + + void Run() { + if (episode >= 0) return RunEpisode(); + REXLOG_INFO("PS5 installer: disc '{}', title update '{}'", found.disc.string(), found.update.string()); + if (found.disc.empty()) { + Set(Phase::kProblem, + hub::lang::T("There's no disc image in the install folder. Copy your GTA IV disc image (.iso) into " + "/data/homebrew/PPSA94004/install/ and press Triangle.")); + return; + } + const auto inspected = gta4::install::InspectGameSource(found.disc); + if (!inspected.supported()) { + Set(Phase::kProblem, + hub::lang::F("This disc image can't be used: {}\nIt has to be the USA disc of GTA IV, the original release.", + {inspected.rejection_reason})); + return; + } + { + std::lock_guard lock(m); + st.disc = gta4::install::FormatXexRegion(inspected.region) + " \xC2\xB7 " + inspected.release_label; + } + if (found.update.empty()) { + Set(Phase::kProblem, + hub::lang::T("Title Update 8 is missing. Copy TU_1A581VI_000000K000000.0000000000205 into the install " + "folder, next to the disc image, and press Triangle.")); + return; + } + drive = DriveOf(root); + const Drive disc_drive = DriveOf(found.disc); + where = (drive.folder.empty() ? root.string() : drive.folder) + " \xC2\xB7 " + + (drive.type.empty() ? std::string("drive not known") : drive.type + " " + drive.device) + " \xC2\xB7 " + + Bytes(drive.free) + " free \xC2\xB7 disc image on " + + (disc_drive.type.empty() ? std::string("?") : disc_drive.type + " " + disc_drive.device); + REXLOG_INFO("PS5 installer: installs to {}", where); + // an install from before: still going, or finished while the app was closed + if (Resume() || quit.load()) return; + // (an earlier try's files in the staging folder are removed first: they count as free) + const uint64_t needed = SpaceNeeded(found.disc_bytes); + if (drive.free && drive.free < needed && drive.free + FolderSize(root / ".install-staging") < needed) { + Set(Phase::kProblem, + hub::lang::F("There isn't enough free space. The install needs about {} and the drive has {} free.", + {hub::lang::Units(Bytes(needed)), hub::lang::Units(Bytes(drive.free))})); + return; + } + Set(Phase::kReady); + { + std::unique_lock lock(m); + cv.wait(lock, [this] { return go || quit.load(); }); + if (quit.load()) return; + st.phase = Phase::kInstalling; + started = Clock::now(); + } + std::string why_not; + const Outcome outcome = WithPayload(why_not); + if (outcome != Outcome::kUnavailable) return Conclude(outcome, token); + if (stop.load()) return Conclude(Outcome::kStopped, token); + REXLOG_WARN("PS5 installer: {}; installing in the app instead", why_not); + { + std::lock_guard lock(m); + st.note = why_not + ", so the app is installing by itself. That's much slower."; + st.diag_time.clear(); + } + InApp(); + } +}; + +Job::Job(const fs::path& app, const fs::path& install_root, int episode) : impl_(std::make_unique()) { + impl_->app = app; + impl_->root = install_root; + impl_->episode = episode >= 0 && episode < kEpisodeCount ? episode : -1; + impl_->found = Find(app); + impl_->thread = std::thread([impl = impl_.get()] { impl->Run(); }); +} + +Job::~Job() { + { + std::lock_guard lock(impl_->m); + impl_->quit = true; + } + impl_->progress.cancel_requested = true; + impl_->cv.notify_all(); + if (impl_->thread.joinable()) impl_->thread.join(); + impl_->monitoring = false; + if (impl_->monitor.joinable()) impl_->monitor.join(); +} + +const Sources& Job::Found() const { return impl_->found; } + +void Job::Start() { + { + std::lock_guard lock(impl_->m); + if (impl_->st.phase != Phase::kReady) return; + impl_->go = true; + } + impl_->cv.notify_all(); +} + +void Job::Cancel() { + impl_->stop = true; + impl_->progress.cancel_requested = true; +} + +// (only what was last worked out: the screen never touches the drive) +Job::Status Job::Get() const { + std::lock_guard lock(impl_->m); + Status s = impl_->st; + if (s.phase == Phase::kDone) s.progress = 1; + return s; +} + +} // namespace gta4ps5::hub::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.h new file mode 100644 index 00000000..cb123449 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_install.h @@ -0,0 +1,134 @@ +// GTA IV PS5 - the hub's check of the game's files, and its installer: the +// game is installed from the disc image and Title Update 8 in the app's +// install folder; after that it runs from UserData/LibertyRecomp/game. The +// episodes (The Lost and Damned, The Ballad of Gay Tony) are installed the same +// way from their Xbox 360 content packages, into UserData/LibertyRecomp/dlc. +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace gta4ps5::hub::install { + +namespace fs = std::filesystem; + +// Where the disc image and the title update go (/install). +fs::path InstallFolder(const fs::path& app); +// The installed game (/UserData/LibertyRecomp/game). +fs::path GameRoot(const fs::path& app); + +// The episodes, in gta4::install::Episode's order. +constexpr int kEpisodeCount = 2; +// "The Lost and Damned", "The Ballad of Gay Tony" (as the game names them, in +// every language). +const char* EpisodeName(int episode); + +struct FileProblem { + std::string path; + uint64_t expected = 0, found = 0; + bool missing = false; +}; + +// What the app folder has. +struct State { + bool installed = false; // the game is installed and ready + bool iso = false; // a disc image waits in install/ + bool update = false; // ...and a title update next to it + std::string reason; // why it isn't ready (from the installer's check) + std::vector problems; + bool Installed() const { return installed || iso; } + bool xex_ok = true; + bool episode[kEpisodeCount] = {}; // installed + bool episode_found[kEpisodeCount] = {}; // its package waits in install/ +}; +State Look(const fs::path& app); + +// A folder's entries (none if it can't be read; never throws). +std::vector ListDir(const fs::path& dir); + +// "6.6 GB" etc. for messages. +std::string Bytes(uint64_t n); + +// What's in install/: the disc image (or an extracted game folder), the +// title update, and the episodes' content packages (or folders of their files). +struct Sources { + fs::path disc, update; + uint64_t disc_bytes = 0, update_bytes = 0; + fs::path episode[kEpisodeCount]; + uint64_t episode_bytes[kEpisodeCount] = {}; + std::vector other_episodes; // episode packages whose header's name doesn't say which +}; +Sources Find(const fs::path& app); + +// Free space where the game goes, in bytes. +uint64_t FreeSpace(const fs::path& install_root); + +// The drive the game installs to: its filesystem ("ufs" the internal storage, +// "exfatfs" an external drive), the real folder behind /app0, its free space. +struct Drive { + std::string type; // empty: not known + std::string folder; // the install folder's real path (empty: not known) + std::string device; // what's mounted ("/dev/...", the real folder of a nullfs) + uint64_t free = 0; + bool internal() const { return type == "ufs"; } + // "internal storage", "an exFAT drive"... + std::string Name() const; +}; +Drive DriveOf(const fs::path& path); // (a folder or a file) + +// About how much room the install takes, from the disc image's size. +inline uint64_t SpaceNeeded(uint64_t disc_bytes) { return disc_bytes * 9 / 4 + (512ull << 20); } + +// The installer's work, on a thread of its own: it checks the disc image +// right away (the USA disc, the original release), and installs once started. +// The writing is done by the installer payload (ps5/payload/installer), sent to +// the console's payload loader on port 9021: the PS5 lets a title write only +// about a gigabyte quickly before slowing it to a few MB/s, and a payload +// isn't held back. With no loader the app installs by itself (slowly). +class Job { + public: + enum class Phase { kChecking, kReady, kProblem, kInstalling, kDone, kFailed, kCancelled }; + struct Status { + Phase phase = Phase::kChecking; + std::string disc; // what the disc image is ("USA · Retail 1.00"), or the episode's package + std::string source; // an episode's package (its file name), once found + uint64_t source_bytes = 0; + std::string problem; // why it can't install, or why the install stopped + // installing: 1 copying from the disc image, 2 unpacking the archives, + // 3 checking the installed game and putting it in place + int step = 0; + bool stopping = false; // stopped: what was copied is being removed + std::string note; // something to know about this install (no payload loader...) + float progress = 0; // 0..1 + uint64_t written = 0; // bytes so far + float seconds = 0; // since the install started + float rate = 0; // bytes a second, lately + float eta = -1; // seconds left (-1: not known yet) + float read_rate = 0, write_rate = 0; // one thread's reading and writing speed, lately + std::string drive; // what it installs to ("an exFAT drive") + // what's going on, in detail (small print on the install screen) + std::string diag_drive, diag_time, diag_file; + }; + // `install_root`: UserData/LibertyRecomp (the game goes in its game/). + // `episode`: one of the episodes instead (into its dlc/), 0 or 1. + Job(const fs::path& app, const fs::path& install_root, int episode = -1); + ~Job(); + Job(const Job&) = delete; + Job& operator=(const Job&) = delete; + const Sources& Found() const; + Status Get() const; + void Start(); // once kReady + void Cancel(); // while installing (what was copied is removed) + + struct Impl; + + private: + std::unique_ptr impl_; +}; + +} // namespace gta4ps5::hub::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_installer_page.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_installer_page.cpp new file mode 100644 index 00000000..4c2145f4 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_installer_page.cpp @@ -0,0 +1,376 @@ +// GTA IV PS5 - the hub's installer (see hub_ui.h, hub_install.h): PLAY +// opens it while the game isn't installed, and the Episodes page for an +// episode. It shows what's in install/ and the disc image's (or the episode +// package's) check, then installs with a progress bar. All on the controller; +// the hub's music keeps playing. +#include +#include +#include +#include + +#include "hub_lang.h" +#include "hub_style.h" +#include "hub_ui.h" + +namespace gta4ps5::hub { + +using lang::F; +using lang::T; + +namespace { + +using Phase = install::Job::Phase; + +enum RowStatus { kRowChecking, kRowOk, kRowBad, kRowNone }; + +int Minutes(float seconds) { return int(std::ceil(seconds / 60.0f)); } + +std::string Rate(float bytes_per_second) { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.0f MB/s", double(bytes_per_second) / double(1 << 20)); + return lang::Units(buf); +} + +} // namespace + +std::filesystem::path Hub::InstallRoot() const { + return paths_.install.empty() ? paths_.app / "UserData" / "LibertyRecomp" : paths_.install; +} + +bool Hub::Busy() const { return installer_ && job_phase_ == Phase::kInstalling; } + +void Hub::OpenInstaller(int episode) { + installer_ = true; + installer_episode_ = episode; + job_ = std::make_unique(paths_.app, InstallRoot(), episode); + job_phase_ = Phase::kChecking; + drive_ = install::DriveOf(InstallRoot()); + free_space_ = drive_.type.empty() ? install::FreeSpace(InstallRoot()) : drive_.free; + shown_progress_ = 0; + page_in_ = 0; + page_dir_ = 1; + focus_y_ = -1; +} + +void Hub::CloseInstaller() { + installer_ = false; + job_.reset(); + LookAgain(); + page_in_ = 0; + page_dir_ = -1; +} + +// ---------------------------------------------------------------- drawing + +void Hub::Spinner(float cx, float cy, float r, ImU32 col) { + const float a = time_ * 5.5f; + dl_->PathArcTo(P(cx, cy), r * s_, a, a + 4.4f, 32); + dl_->PathStroke(col, 0, 3.2f * s_); +} + +void Hub::Tick(float cx, float cy, float size, ImU32 col) { + const float k = size / 20; + dl_->PathLineTo(P(cx - 9 * k, cy + 0.5f * k)); + dl_->PathLineTo(P(cx - 3 * k, cy + 6.5f * k)); + dl_->PathLineTo(P(cx + 9.5f * k, cy - 6.5f * k)); + dl_->PathStroke(col, 0, 3.4f * k * s_); +} + +void Hub::XMark(float cx, float cy, float size, ImU32 col) { + const float k = size / 20 * 7; + dl_->AddLine(P(cx - k, cy - k), P(cx + k, cy + k), col, 3.4f * size / 20 * s_); + dl_->AddLine(P(cx - k, cy + k), P(cx + k, cy - k), col, 3.4f * size / 20 * s_); +} + +// One of the setup's rows: what it is, the file, and how it checked out +// (a grey heading, the name, the result at the right; a rule under it). +void Hub::Row3(float y, const char* label, const std::string& name, int status, const std::string& note) { + const float x = kListX, w = kPanelX - 90 - kListX, h = 104; + TextMid(Bold(), 21, x, y + 24, kDimGray, Upper(T(label)), 0, 1.6f); + const float note_w = note.empty() ? 0 : TextWidth(Bold(), 27, note) + 60; + TextMid(Bold(), 34, x, y + 64, name.empty() ? kDimGray : kText, + Fit(Bold(), 34, name.empty() ? T("Not found") : name, w - std::max(note_w, 90.0f))); + const float cy = y + 64, icon_x = x + w - 18; + switch (status) { + case kRowChecking: Spinner(icon_x, cy, 13, kMenuGray); break; + case kRowOk: Tick(icon_x, cy, 24, pal_.title); break; + case kRowBad: XMark(icon_x, cy, 22, kBad); break; + default: break; + } + if (!note.empty()) TextMid(Bold(), 27, icon_x - 34, cy, status == kRowBad ? kBad : kMenuGray, note, 1); + Rect(x, y + h - 1, w, 2, Rgba(40, 40, 40)); +} + +void Hub::DrawInstaller() { + const install::Job::Status st = job_->Get(); + // what changed since the last frame + if (st.phase != job_phase_) { + switch (st.phase) { + case Phase::kProblem: + case Phase::kFailed: Play(Sound::kError); break; + case Phase::kDone: + Play(Sound::kOn); + LookAgain(); + break; + default: break; + } + job_phase_ = st.phase; + panel_in_ = 0; + if (st.phase == Phase::kCancelled) { + // back to the setup, looked at again + job_ = std::make_unique(paths_.app, InstallRoot(), installer_episode_); + job_phase_ = Phase::kChecking; + shown_progress_ = 0; + Toast(T("Install stopped. Nothing was kept.")); + return; + } + } + const install::Sources& found = job_->Found(); + const int ep = installer_episode_; + // (an episode: its package as the installer found it, else as the folder's scan did) + const std::string episode_file = ep < 0 ? std::string() : !st.source.empty() ? st.source : found.episode[ep].filename().string(); + const uint64_t episode_bytes = ep < 0 ? 0 : st.source_bytes ? st.source_bytes : found.episode_bytes[ep]; + const uint64_t needed = ep >= 0 ? episode_bytes + (256ull << 20) : install::SpaceNeeded(found.disc_bytes); + + // ---- the setup: what's there, and whether it can install + if (st.phase == Phase::kChecking || st.phase == Phase::kReady || st.phase == Phase::kProblem) { + const bool checking = st.phase == Phase::kChecking; + float y = kListY + 130; + if (ep >= 0) { + Heading(T(install::EpisodeName(ep)), T("One time only: the episode is copied off its Xbox 360 package.")); + const bool bad = st.phase == Phase::kProblem && (episode_file.empty() || st.disc.empty()); + Row3(y, "Episode Package", episode_file, checking ? kRowChecking : bad ? kRowBad : kRowOk, + checking ? T("Checking") : bad ? T(episode_file.empty() ? "Missing" : "Can't use") : st.disc); + y += 110; + } else { + Heading(T("Your game files"), T("One time only: the game is copied off your Xbox 360 disc image.")); + const bool disc_bad = st.phase == Phase::kProblem && (found.disc.empty() || st.disc.empty()); + Row3(y, found.disc.empty() || found.disc.has_extension() ? "Disc Image" : "Game Folder", + found.disc.filename().string(), checking ? kRowChecking : disc_bad ? kRowBad : kRowOk, + checking ? T("Checking") : disc_bad ? T(found.disc.empty() ? "Missing" : "Can't use") : st.disc); + y += 110; + Row3(y, "Title Update 8", found.update.filename().string(), found.update.empty() ? kRowBad : kRowOk, + found.update.empty() ? T("Missing") : lang::Units(install::Bytes(found.update_bytes))); + y += 110; + } + // (the free space, when it can be known: a PS5 title can't ask) + const bool room = !free_space_ || free_space_ >= needed; + Row3(y, "Space", F("Needs about {} free", {lang::Units(install::Bytes(needed))}), + !free_space_ ? kRowNone : room ? kRowOk : kRowBad, + free_space_ ? F("{} free", {lang::Units(install::Bytes(free_space_))}) : ""); + Panel([&](float px, float py, float pw) { + py += 130; // (under the heading's line) + const ImU32 text = Rgba(196, 196, 196); + if (checking) { + TextMid(Bold(), 40, px, py + 20, kText, T("Checking")); + Wrapped(Regular(), 28, px, py + 62, pw, text, + T(ep >= 0 ? "Making sure the package is the episode. This takes a few seconds." + : "Making sure the disc image is the USA disc of GTA IV. This takes a few seconds."), + 1.38f); + } else if (st.phase == Phase::kReady) { + TextMid(Bold(), 40, px, py + 20, pal_.title, T("Ready")); + py += 62; + py += Wrapped(Regular(), 28, px, py, pw, text, + T(ep >= 0 ? "Press Cross to install. Keep the app open until it's done. The package isn't changed." + : "Press Cross to install. Keep the app open until it's done. Your disc image isn't changed."), + 1.38f) + 26; + TextMid(Bold(), 21, px, py + 12, kDimGray, Upper(T("Installs To")), 0, 1.6f); + const std::string folder = drive_.folder.empty() ? "/data/homebrew/PPSA94004/UserData" : drive_.folder; + py += 36; + py += Wrapped(Bold(), 27, px, py, pw, kText, folder, 1.25f); + if (!drive_.type.empty()) { + std::string on = + F("On {}", {drive_.Name()}) + (drive_.device.empty() ? "" : " (" + drive_.type + ", " + drive_.device + ")"); + Wrapped(Regular(), 26, px, py + 6, pw, kMenuGray, on); + } + } else { + TextMid(Bold(), 40, px, py + 20, kBad, T("Can't install yet")); + py += 62; + py += Wrapped(Regular(), 28, px, py, pw, text, st.problem, 1.38f) + 26; + TextMid(Bold(), 21, px, py + 12, kDimGray, Upper(T("The Install Folder")), 0, 1.6f); + TextMid(Bold(), 27, px, py + 52, kText, "/data/homebrew/PPSA94004/install/"); + } + }); + return; + } + + // ---- installing, done, or stopped + if (st.phase == Phase::kFailed) { + Heading(T("The install stopped"), T("What was copied has been removed, so nothing is left half done.")); + const float x = kListX, w = 1100; + float y = kListY + 140; + TextMid(Bold(), 21, x, y + 12, kBad, Upper(T("What Happened")), 0, 1.6f); + y += 40; + y += Wrapped(Regular(), 30, x, y, w, kText, st.problem, 1.38f) + 30; + Wrapped(Regular(), 27, x, y, w, kMenuGray, + T("Press Cross to try again. If it stops again, send the log from /data/homebrew/PPSA94004/logs."), 1.38f); + return; + } + + const bool done = st.phase == Phase::kDone; + Ease(shown_progress_, done ? 1.0f : st.progress, 3.5f); + if (done && shown_progress_ > 0.98f) shown_progress_ = 1; + const float p = std::clamp(shown_progress_, 0.0f, 1.0f); + if (done && ep >= 0) + Heading(T("Installed"), F("{} is added to your game. When the game starts it asks if you want to play it.", + {T(install::EpisodeName(ep))})); + else if (done) Heading(T("Installed"), T("GTA IV is set up on your PS5. Press Cross to play.")); + else Heading(ep >= 0 ? F("Installing {}", {T(install::EpisodeName(ep))}) : std::string(T("Installing")), + T("Keep the app open until it's done. The PS5 won't go to sleep.")); + // the percentage, big + char pct[8]; + std::snprintf(pct, sizeof(pct), "%d%%", done ? 100 : std::min(99, int(p * 100 + 0.0001f))); + TextMid(Bold(), 120, kPageL - 4, 462, kText, pct); + // the step + const std::string step = Upper(T(done ? "All Done" + : st.stopping ? "Stopping" + : st.step >= 3 ? "Finishing" + : ep >= 0 ? "Copying" + : st.step == 2 ? "Step 2 of 2" + : "Step 1 of 2")); + const char* doing = T(done ? (ep >= 0 ? "The episode's files are in place" : "The game's files are in place") + : st.stopping ? "Removing what was copied" + : st.step >= 3 ? (ep >= 0 ? "Checking the episode and putting it in place" + : "Checking the game and putting it in place") + : ep >= 0 ? "Copying the episode from its package" + : st.step == 2 ? "Unpacking the game's archives" + : "Copying the game from your disc image"); + TextMid(Bold(), 21, kPageL, 566, kDimGray, step, 0, 1.6f); + TextMid(Bold(), 34, kPageL, 604, kText, doing); + // the bar + const float bx = kPageL, by = 652, bw = kPageR - kPageL, bh = 14; + Rect(bx, by, bw, bh, kBarBack); + const float fw = std::max(bh, bw * p); + Rect(bx, by, fw, bh, pal_.title); + if (!done) { // a light running along the filled part + dl_->PushClipRect(P(bx, by), P(bx + fw, by + bh), true); + const float run = std::fmod(time_ * 520.0f, fw + 400.0f) - 200.0f; + dl_->AddRectFilledMultiColor(P(bx + run - 140, by), P(bx + run, by + bh), White(0), White(0.35f), White(0.35f), + White(0)); + dl_->AddRectFilledMultiColor(P(bx + run, by), P(bx + run + 140, by + bh), White(0.35f), White(0), White(0), + White(0.35f)); + dl_->PopClipRect(); + } + // the numbers under it + std::string left = F("{} written", {lang::Units(install::Bytes(st.written))}); + if (done) { + const int m = Minutes(st.seconds > 0 ? st.seconds : 1); + left = m <= 1 ? std::string(T("Took under a minute")) : F("Took {} min", {std::to_string(m)}); + } + Text(Regular(), 27, bx, by + 34, kMenuGray, left); + // (stopping, or checking at the end: nothing more being written, no speed or time left) + if (!done && !st.stopping && st.step < 3) { + std::string right; + if (st.rate > 0) right = Rate(st.rate); + if (st.eta >= 0) { + const int m = Minutes(st.eta); + right += (right.empty() ? "" : " \xC2\xB7 ") + + (m <= 1 ? std::string(T("Less than a minute left")) : F("About {} min left", {std::to_string(m)})); + } else if (st.seconds > 2) { + right += (right.empty() ? "" : " \xC2\xB7 ") + std::string(T("Working out the time left")); + } + Text(Regular(), 27, bx + bw, by + 34, kMenuGray, right, 1); + // (what the time goes to: reading the disc image or writing the game) + if (st.read_rate > 0 || st.write_rate > 0) { + Text(Regular(), 23, bx, by + 74, kDimGray, + F("Reading at {} \xC2\xB7 writing at {}", {Rate(st.read_rate), Rate(st.write_rate)}) + + (st.drive.empty() ? "" : F(" to {}", {st.drive}))); + } + // something to know about this install (no payload loader: installing in the app) + float dy = by + 108; + if (!st.note.empty()) { + Text(Regular(), 23, bx, dy, kMenuGray, Fit(Regular(), 23, st.note, bw)); + dy += 32; + } + // the details, small: what it's writing, where the time goes, the drive + for (const std::string* line : {&st.diag_file, &st.diag_time, &st.diag_drive}) { + if (line->empty()) continue; + Text(Regular(), 20, bx, dy, Rgba(88, 88, 88), Fit(Regular(), 20, *line, bw)); + dy += 26; + } + } +} + +void Hub::InstallerHints(float r) { + switch (job_phase_) { + case Phase::kChecking: Hint(r, kCircle, "Back"); break; + case Phase::kReady: + r = Hint(r, kCircle, "Back"); + r = Hint(r, kTriangle, "Look Again"); + Hint(r, kCross, "Install"); + break; + case Phase::kProblem: + r = Hint(r, kCircle, "Back"); + Hint(r, kTriangle, "Look Again"); + break; + case Phase::kInstalling: Hint(r, kCircle, "Cancel"); break; + case Phase::kDone: + r = Hint(r, kCircle, "Home"); + Hint(r, kCross, "Play"); + break; + case Phase::kFailed: + case Phase::kCancelled: + r = Hint(r, kCircle, "Back"); + Hint(r, kCross, "Try Again"); + break; + } +} + +// ---------------------------------------------------------------- controls + +void Hub::InstallerInput(uint32_t p) { + auto again = [this] { + job_ = std::make_unique(paths_.app, InstallRoot(), installer_episode_); + job_phase_ = Phase::kChecking; + drive_ = install::DriveOf(InstallRoot()); + free_space_ = drive_.type.empty() ? install::FreeSpace(InstallRoot()) : drive_.free; + shown_progress_ = 0; + panel_in_ = 0; + Play(Sound::kSelect); + }; + switch (job_phase_) { + case Phase::kChecking: + if (p & kCircle) Play(Sound::kBack), CloseInstaller(); + break; + case Phase::kReady: + if (p & kCross) { + job_->Start(); + press_ = 1; + Play(Sound::kStart); + } else if (p & kTriangle) { + again(); + } else if (p & kCircle) { + Play(Sound::kBack), CloseInstaller(); + } + break; + case Phase::kProblem: + if (p & (kTriangle | kCross)) again(); + else if (p & kCircle) Play(Sound::kBack), CloseInstaller(); + break; + case Phase::kInstalling: + if (p & kCircle) { + Play(Sound::kSelect); + Confirm(T("Stop installing?"), T("What's been copied so far is removed. You can start over later."), + T("Stop"), + [this] { + if (job_) job_->Cancel(); + }, + T("Keep Going"), nullptr); + } + break; + case Phase::kDone: + if (p & kCross) { + press_ = 1, launch_ = true; // (Draw fades out and starts the game) + } else if (p & kCircle) { + Play(Sound::kBack), CloseInstaller(); + } + break; + case Phase::kFailed: + case Phase::kCancelled: + if (p & kCross) again(); + else if (p & kCircle) Play(Sound::kBack), CloseInstaller(); + break; + } +} + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.cpp new file mode 100644 index 00000000..f02c5760 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.cpp @@ -0,0 +1,1232 @@ +// GTA IV PS5 - the hub's languages (see hub_lang.h). +#include "hub_lang.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(__PROSPERO__) +extern "C" int sceSystemServiceParamGetInt(int param_id, int* value); +#endif + +namespace gta4ps5::hub::lang { + +namespace { + +std::atomic g_lang{kEnglish}; + +// English, then French, German, Spanish and Italian. French puts a +// no-break space ( ) before : ; ? ! and %. +struct Entry { + const char* text[kLangCount]; +}; + +// clang-format off +const Entry kTable[] = { + // ---- the tabs and the footer + {{"Home", "Accueil", "Start", "Inicio", "Home"}}, + {{"Display", "Affichage", "Anzeige", "Pantalla", "Schermo"}}, + {{"Game", "Jeu", "Spiel", "Juego", "Gioco"}}, + {{"Saves", "Sauvegardes", "Spielstände", "Partidas", "Salvataggi"}}, + {{"Trophies", "Trophées", "Trophäen", "Trofeos", "Trofei"}}, + {{"About", "À propos", "Info", "Acerca de", "Info"}}, + {{"Debug", "Débogage", "Debug", "Depuración", "Debug"}}, + {{"Install|tab", "Installation", "Installation", "Instalación", "Installazione"}}, + {{"Play", "Jouer", "Spielen", "Jugar", "Gioca"}}, + {{"Install", "Installer", "Installieren", "Instalar", "Installa"}}, + {{"Back", "Retour", "Zurück", "Volver", "Indietro"}}, + {{"Reset All", "Tout réinitialiser", "Alles zurücksetzen", "Restablecer todo", "Ripristina tutto"}}, + {{"Adjust", "Modifier", "Ändern", "Cambiar", "Modifica"}}, + {{"Move", "Naviguer", "Navigieren", "Navegar", "Naviga"}}, + {{"Back Up", "Copier", "Sichern", "Copiar", "Backup"}}, + {{"Import", "Importer", "Importieren", "Importar", "Importa"}}, + {{"Restore", "Restaurer", "Wiederherstellen", "Restaurar", "Ripristina"}}, + {{"Tab", "Onglet", "Seite", "Pestaña", "Scheda"}}, + {{"Look Again", "Revérifier", "Neu prüfen", "Volver a buscar", "Ricontrolla"}}, + {{"Cancel", "Annuler", "Abbrechen", "Cancelar", "Annulla"}}, + {{"Try Again", "Réessayer", "Nochmal", "Reintentar", "Riprova"}}, + {{"Reset", "Réinitialiser", "Zurücksetzen", "Restablecer", "Ripristina"}}, + {{"Stop", "Arrêter", "Stoppen", "Detener", "Interrompi"}}, + {{"Keep Going", "Continuer", "Weitermachen", "Continuar", "Continua"}}, + + // ---- settings: names and values + {{"Language", "Langue", "Sprache", "Idioma", "Lingua"}}, + {{"Preset", "Préréglage", "Voreinstellung", "Preajuste", "Preimpostazione"}}, + {{"Recommended", "Recommandé", "Empfohlen", "Recomendado", "Consigliato"}}, + {{"Performance", "Performances", "Leistung", "Rendimiento", "Prestazioni"}}, + {{"Quality", "Qualité", "Qualität", "Calidad", "Qualità"}}, + {{"Custom", "Personnalisé", "Eigene", "Personalizado", "Personalizzato"}}, + {{"Resolution", "Résolution", "Auflösung", "Resolución", "Risoluzione"}}, + {{"Draw Distance", "Distance d'affichage", "Sichtweite", "Distancia de dibujado", "Distanza visiva"}}, + {{"Frame Limit", "Limite d'images", "Bildlimit", "Límite de fotogramas", "Limite fotogrammi"}}, + {{"FPS Counter", "Compteur FPS", "FPS-Anzeige", "Contador de FPS", "Contatore FPS"}}, + {{"Refresh Rate", "Fréquence d'affichage", "Bildwiederholrate", "Frecuencia de actualización", "Frequenza di aggiornamento"}}, + {{"Highest Detail Models", "Modèles les plus détaillés", "Höchste Modelldetails", "Modelos al máximo detalle", "Modelli al massimo dettaglio"}}, + {{"Texture Filtering", "Filtrage des textures", "Texturfilterung", "Filtrado de texturas", "Filtro texture"}}, + {{"Enhanced Effects", "Effets améliorés", "Verbesserte Effekte", "Efectos mejorados", "Effetti migliorati"}}, + {{"Shadow Quality", "Qualité des ombres", "Schattenqualität", "Calidad de sombras", "Qualità ombre"}}, + {{"Frame Generation", "Génération d'images", "Bildgenerierung", "Generación de fotogramas", "Generazione fotogrammi"}}, + {{"Anti-Aliasing", "Anticrénelage", "Kantenglättung", "Antialiasing", "Antialiasing"}}, + {{"Motion Blur", "Flou de mouvement", "Bewegungsunschärfe", "Desenfoque de movimiento", "Sfocatura di movimento"}}, + {{"Reflections", "Reflets", "Spiegelungen", "Reflejos", "Riflessi"}}, + {{"Shadow Distance", "Distance des ombres", "Schattenweite", "Distancia de sombras", "Distanza ombre"}}, + {{"Small Object Shadows", "Ombres des petits objets", "Schatten kleiner Objekte", "Sombras de objetos pequeños", "Ombre oggetti piccoli"}}, + {{"Car Reflections", "Reflets des voitures", "Fahrzeugspiegelungen", "Reflejos de coches", "Riflessi auto"}}, + {{"Skip Intro", "Passer l'intro", "Intro überspringen", "Saltar intro", "Salta intro"}}, + {{"PS5 Trophies", "Trophées PS5", "PS5-Trophäen", "Trofeos de PS5", "Trofei PS5"}}, + {{"Centered Car Camera", "Caméra de véhicule centrée", "Zentrierte Fahrzeugkamera", "Cámara de vehículo centrada", + "Telecamera veicolo centrata"}}, + {{"Car Camera Return", "Retour de la caméra de véhicule", "Rückkehr der Fahrzeugkamera", + "Retorno de la cámara de vehículo", "Ritorno della telecamera veicolo"}}, + {{"Car Camera Follow Speed", "Vitesse de suivi de la caméra de véhicule", "Nachführtempo der Fahrzeugkamera", + "Velocidad de seguimiento de la cámara de vehículo", "Velocità di inseguimento della telecamera veicolo"}}, + {{"Water Reflections", "Reflets de l'eau", "Wasserspiegelungen", "Reflejos del agua", "Riflessi dell'acqua"}}, + {{"Film Grain", "Grain de film", "Filmkorn", "Grano de película", "Grana pellicola"}}, + {{"DualSense Features", "Fonctions DualSense", "DualSense-Funktionen", "Funciones DualSense", "Funzioni DualSense"}}, + {{"Menu Music", "Musique du menu", "Menümusik", "Música del menú", "Musica del menu"}}, + {{"Light Bar", "Barre lumineuse", "Lichtleiste", "Barra de luz", "Barra luminosa"}}, + {{"Audio Delay", "Délai du son", "Tonverzögerung", "Retraso del sonido", "Ritardo audio"}}, + {{"Lowest (32 ms)", "Minimal (32\u00A0ms)", "Minimal (32 ms)", "Mínimo (32 ms)", "Minimo (32 ms)"}}, + {{"Low (64 ms)", "Faible (64\u00A0ms)", "Niedrig (64 ms)", "Bajo (64 ms)", "Basso (64 ms)"}}, + {{"Steady (128 ms)", "Stable (128\u00A0ms)", "Stabil (128 ms)", "Estable (128 ms)", "Stabile (128 ms)"}}, + {{"Original (341 ms)", "D'origine (341\u00A0ms)", "Original (341 ms)", "Original (341 ms)", + "Originale (341 ms)"}}, + {{"Adaptive Triggers", "Gâchettes adaptatives", "Adaptive Trigger", "Gatillos adaptativos", "Grilletti adattivi"}}, + {{"Extra Rumble", "Vibrations supplémentaires", "Zusätzliche Vibration", "Vibración extra", "Vibrazione extra"}}, + {{"Feedback Strength", "Intensité du retour", "Feedback-Stärke", "Intensidad de respuesta", + "Intensità del feedback"}}, + {{"Light", "Légère", "Leicht", "Suave", "Leggera"}}, + {{"Medium", "Moyenne", "Mittel", "Media", "Media"}}, + {{"Strong", "Forte", "Stark", "Fuerte", "Forte"}}, + {{"Max", "Maximale", "Maximal", "Máxima", "Massima"}}, + {{"Save Logs", "Enregistrer les journaux", "Logs speichern", "Guardar registros", "Salva log"}}, + {{"Log Detail", "Détail des journaux", "Log-Detail", "Detalle del registro", "Dettaglio log"}}, + {{"Renderer Threads", "Threads du rendu", "Renderer-Threads", "Hilos del renderizador", "Thread del renderer"}}, + {{"Game Thread Capture", "Capture du thread de jeu", "Spielthread-Erfassung", "Captura del hilo del juego", "Cattura thread di gioco"}}, + {{"Frame Time Capture", "Temps d'image", "Bildzeit-Aufzeichnung", "Tiempos de fotograma", "Tempi fotogramma"}}, + {{"Freeze Reports", "Rapports de blocage", "Hänger-Berichte", "Informes de bloqueo", "Report blocchi"}}, + {{"GPU Texture Unpacking", "Textures sur GPU", "Texturen auf GPU", "Texturas en GPU", "Texture su GPU"}}, + {{"Frame Timeline", "Chronologie des images", "Bild-Zeitleiste", "Cronología de fotogramas", "Timeline fotogrammi"}}, + {{"Frame Delivery", "Envoi des images", "Bildausgabe", "Envío de fotogramas", "Invio dei fotogrammi"}}, + {{"As Finished", "Dès qu'elles sont prêtes", "Sobald fertig", "Al terminar", "Appena pronti"}}, + {{"Every Vblank", "À chaque rafraîchissement", "Bei jedem Vblank", "En cada refresco", "A ogni refresh"}}, + {{"Texture Watch", "Surveillance des textures", "Texturüberwachung", "Vigilancia de texturas", "Controllo texture"}}, + {{"Light Test", "Test des lumières", "Lichttest", "Prueba de luces", "Test luci"}}, + {{"Cutscenes at 30", "Cinématiques à 30", "Zwischensequenzen mit 30", "Cinemáticas a 30", "Filmati a 30"}}, + {{"No Stencil", "Sans stencil", "Ohne Stencil", "Sin stencil", "Senza stencil"}}, + {{"No Clamp", "Sans clamp", "Ohne Clamp", "Sin clamp", "Senza clamp"}}, + {{"Auto", "Auto", "Auto", "Auto", "Auto"}}, + {{"Off", "Désactivé", "Aus", "Desactivado", "Disattivato"}}, + {{"On", "Activé", "An", "Activado", "Attivato"}}, + {{"Original", "D'origine", "Original", "Original", "Originale"}}, + {{"720p (Original)", "720p (d'origine)", "720p (Original)", "720p (original)", "720p (originale)"}}, + {{"Shorter", "Plus courte", "Kürzer", "Más corta", "Più corta"}}, + {{"1.5x", "1,5x", "1,5x", "1,5x", "1,5x"}}, + {{"30 to 60", "30 à 60", "30 zu 60", "30 a 60", "30 a 60"}}, + {{"60 to 120", "60 à 120", "60 zu 120", "60 a 120", "60 a 120"}}, + {{"FSR 3 (Experimental)", "FSR 3 (expérimental)", "FSR 3 (experimentell)", "FSR 3 (experimental)", "FSR 3 (sperimentale)"}}, + {{"60 Hz", "60\u00A0Hz", "60 Hz", "60 Hz", "60 Hz"}}, + {{"120 Hz", "120\u00A0Hz", "120 Hz", "120 Hz", "120 Hz"}}, + {{"120 Hz with VRR", "120\u00A0Hz avec VRR", "120 Hz mit VRR", "120 Hz con VRR", "120 Hz con VRR"}}, + {{"MSAA 4x + SMAA", "MSAA 4x + SMAA", "MSAA 4x + SMAA", "MSAA 4x + SMAA", "MSAA 4x + SMAA"}}, + {{"High", "Élevée", "Hoch", "Alta", "Alta"}}, + {{"Very High", "Très élevée", "Sehr hoch", "Muy alta", "Molto alta"}}, + {{"Every Frame", "Chaque image", "Jedes Bild", "Cada fotograma", "Ogni fotogramma"}}, + {{"Every Other Frame", "Une image sur deux", "Jedes zweite Bild", "Cada dos fotogramas", "Un fotogramma su due"}}, + {{"After 1 Second", "Après 1\u00A0seconde", "Nach 1 Sekunde", "Tras 1 segundo", "Dopo 1 secondo"}}, + {{"After 2 Seconds", "Après 2\u00A0secondes", "Nach 2 Sekunden", "Tras 2 segundos", "Dopo 2 secondi"}}, + {{"After 3 Seconds", "Après 3\u00A0secondes", "Nach 3 Sekunden", "Tras 3 segundos", "Dopo 3 secondi"}}, + {{"Faster", "Plus rapide", "Schneller", "Más rápida", "Più veloce"}}, + {{"Much Faster", "Bien plus rapide", "Viel schneller", "Mucho más rápida", "Molto più veloce"}}, + {{"Fastest", "La plus rapide", "Am schnellsten", "La más rápida", "La più veloce"}}, + {{"Normal", "Normal", "Normal", "Normal", "Normale"}}, + {{"Detailed", "Détaillé", "Ausführlich", "Detallado", "Dettagliato"}}, + {{"Simple", "Simple", "Einfach", "Simple", "Semplice"}}, + {{"Everything", "Tout", "Alles", "Todo", "Tutto"}}, + {{"Own Cores", "Cœurs dédiés", "Eigene Kerne", "Núcleos propios", "Core dedicati"}}, + {{"Anywhere", "Partout", "Überall", "Cualquiera", "Ovunque"}}, + {{"Compare", "Comparer", "Vergleichen", "Comparar", "Confronta"}}, + {{"Lean", "Allégée", "Schlank", "Ligera", "Leggera"}}, + {{"Classic", "Classique", "Klassisch", "Clásica", "Classica"}}, + + // ---- settings: what each does + {{"The language of the game's text (menus, subtitles and missions) and of this hub. Auto follows the PS5's " + "language. Voices stay in English, as on the Xbox 360.", + "La langue des textes du jeu (menus, sous-titres et missions) et de ce hub. Auto suit la langue de la PS5. " + "Les voix restent en anglais, comme sur Xbox 360.", + "Die Sprache der Spieltexte (Menüs, Untertitel und Missionen) und dieses Hubs. Auto folgt der Sprache der " + "PS5. Die Stimmen bleiben auf Englisch, wie auf der Xbox 360.", + "El idioma de los textos del juego (menús, subtítulos y misiones) y de este hub. Auto sigue el idioma de la " + "PS5. Las voces siguen en inglés, como en Xbox 360.", + "La lingua dei testi del gioco (menu, sottotitoli e missioni) e di questo hub. Auto segue la lingua della " + "PS5. Le voci restano in inglese, come su Xbox 360."}}, + {{"Sets the Display settings below in one go, all with a 60 FPS limit. Recommended is the game as it comes, in 4K. Performance draws at 1440p with fewer cars and people, for the steadiest 60. Quality adds sharper and longer shadows, more detail in the distance, SMAA and the enhanced effects, and can drop below 60 in busy streets. Changing any of them makes it Custom.", + "Règle d'un coup les réglages d'affichage ci-dessous, tous limités à 60 images par seconde. Recommandé, c'est le jeu tel qu'il est livré, en 4K. Performances affiche en 1440p avec moins de voitures et de passants, pour les 60 images les plus stables. Qualité ajoute des ombres plus nettes et plus lointaines, plus de détails au loin, le SMAA et les effets améliorés, et peut passer sous 60 dans les rues animées. Changer l'un d'eux passe en Personnalisé.", + "Stellt die Anzeige-Einstellungen darunter auf einmal ein, alle mit 60 Bildern pro Sekunde als Grenze. Empfohlen ist das Spiel, wie es kommt, in 4K. Leistung zeichnet in 1440p mit weniger Autos und Passanten, für die stabilsten 60. Qualität bringt schärfere und weitere Schatten, mehr Details in der Ferne, SMAA und die verbesserten Effekte und kann in belebten Straßen unter 60 fallen. Wer eine davon ändert, hat Eigene.", + "Ajusta de una vez los ajustes de pantalla de abajo, todos con un límite de 60 FPS. Recomendado es el juego tal como viene, en 4K. Rendimiento dibuja a 1440p con menos coches y gente, para los 60 más estables. Calidad añade sombras más nítidas y lejanas, más detalle a lo lejos, SMAA y los efectos mejorados, y puede bajar de 60 en calles concurridas. Cambiar cualquiera de ellos lo deja en Personalizado.", + "Imposta in un colpo le impostazioni dello schermo qui sotto, tutte con un limite di 60 FPS. Consigliato è il gioco così com'è, in 4K. Prestazioni disegna a 1440p con meno auto e persone, per i 60 più stabili. Qualità aggiunge ombre più nitide e lontane, più dettagli in lontananza, SMAA e gli effetti migliorati, e può scendere sotto 60 nelle strade affollate. Cambiarne uno lo porta su Personalizzato."}}, + {{"How sharp the game is drawn. 720p is the Xbox 360's. The PS5's graphics card has room for 4K; lower only " + "helps if the picture stutters.", + "La netteté de l'image du jeu. 720p est celle de la Xbox 360. La carte graphique de la PS5 a de la " + "marge pour la 4K ; plus bas n'aide que si l'image saccade.", + "Wie scharf das Spiel gezeichnet wird. 720p ist die Auflösung der Xbox 360. Die Grafikeinheit der PS5 " + "schafft 4K; weniger hilft nur, wenn das Bild ruckelt.", + "La nitidez con la que se dibuja el juego. 720p es la de Xbox 360. La gráfica de PS5 tiene margen para 4K; " + "bajarla solo ayuda si la imagen da tirones.", + "La nitidezza con cui viene disegnato il gioco. 720p è quella di Xbox 360. La scheda grafica di PS5 regge " + "il 4K; abbassarla serve solo se l'immagine scatta."}}, + {{"How far out buildings and the city are drawn in detail. Original is the Xbox 360's. Shorter draws less of " + "the city and runs faster in busy streets, with more pop-in. Farther costs frame rate, and past 1.5x the " + "game can run short of memory.", + "Jusqu'où les bâtiments et la ville sont affichés en détail. D'origine correspond à la Xbox 360. Plus " + "courte affiche moins de ville et tourne plus vite dans les rues animées, avec plus d'apparitions soudaines. " + "Plus loin coûte des images par seconde, et au-delà de 1,5x le jeu peut manquer de mémoire.", + "Wie weit Gebäude und die Stadt detailliert gezeichnet werden. Original entspricht der Xbox 360. Kürzer " + "zeichnet weniger von der Stadt und läuft in belebten Straßen schneller, mit mehr Pop-in. Weiter kostet " + "Bildrate, und über 1,5x kann dem Spiel der Speicher ausgehen.", + "Hasta dónde se dibujan con detalle los edificios y la ciudad. Original es la de Xbox 360. Más corta dibuja " + "menos ciudad y va más fluida en calles concurridas, con más elementos que aparecen de golpe. Más lejos " + "cuesta fotogramas, y por encima de 1,5x el juego puede quedarse sin memoria.", + "Fino a che distanza edifici e città vengono disegnati nel dettaglio. Originale è quella di Xbox 360. Più " + "corta disegna meno città e va più veloce nelle strade affollate, con più pop-in. Più lontana costa " + "fotogrammi, e oltre 1,5x il gioco può restare senza memoria."}}, + {{"Shows the frame rate at the left edge of the screen while you play. Simple is frames a second and the " + "frame time. Detailed adds the slowest frames, a graph of the last ones, the resolution, the TV's refresh " + "rate, the frame limit and how long the renderer takes.", + "Affiche la fréquence d'images sur le bord gauche de l'écran pendant que vous jouez. Simple : images par " + "seconde et durée d'image. Détaillé ajoute les images les plus lentes, un graphique des dernières, la " + "résolution, la fréquence de la TV, la limite d'images et le temps du rendu.", + "Zeigt beim Spielen am linken Bildschirmrand die Bildrate. Einfach: Bilder pro Sekunde und Bildzeit. " + "Ausführlich zeigt dazu die langsamsten Bilder, eine Grafik der letzten, die Auflösung, die Bildwiederholrate " + "des Fernsehers, die Bildratenbegrenzung und die Zeit des Renderers.", + "Muestra la tasa de fotogramas en el borde izquierdo de la pantalla mientras juegas. Simple: fotogramas por " + "segundo y tiempo de fotograma. Detallado añade los fotogramas más lentos, una gráfica de los últimos, la " + "resolución, la frecuencia de la TV, el límite de fotogramas y el tiempo del renderizador.", + "Mostra la frequenza dei fotogrammi sul bordo sinistro dello schermo mentre giochi. Semplice: fotogrammi al " + "secondo e tempo per fotogramma. Dettagliato aggiunge i fotogrammi più lenti, un grafico degli ultimi, la " + "risoluzione, la frequenza della TV, il limite di fotogrammi e il tempo del renderer."}}, + {{"The most frames a second the game draws. 30 is what the Xbox 360 aimed for and holds steady everywhere; 60 " + "runs at 50 to 60 in the busiest streets. 40 and 120 are for a 120 Hz Refresh Rate: 40 holds steady and is " + "smoother than 30; 120 is only reached in quieter places.", + "Le nombre maximal d'images par seconde. 30 est l'objectif de la Xbox 360 et reste stable partout\u00A0; " + "60 tourne entre 50 et 60 dans les rues les plus animées. 40 et 120 vont avec une fréquence d'affichage de " + "120\u00A0Hz\u00A0: 40 reste stable et plus fluide que 30\u00A0; 120 n'est atteint que dans les " + "endroits calmes.", + "Die meisten Bilder pro Sekunde, die das Spiel zeichnet. 30 war das Ziel der Xbox 360 und bleibt überall " + "stabil; 60 läuft in den belebtesten Straßen mit 50 bis 60. 40 und 120 sind für eine Bildwiederholrate von " + "120 Hz: 40 bleibt stabil und ist flüssiger als 30; 120 wird nur an ruhigeren Orten erreicht.", + "El máximo de fotogramas por segundo. 30 es lo que buscaba Xbox 360 y se mantiene estable en todas partes; " + "60 va entre 50 y 60 en las calles más concurridas. 40 y 120 son para una frecuencia de actualización de " + "120 Hz: 40 se mantiene estable y es más fluido que 30; 120 solo se alcanza en sitios tranquilos.", + "Il massimo di fotogrammi al secondo. 30 è l'obiettivo di Xbox 360 e resta stabile ovunque; 60 gira tra " + "50 e 60 nelle strade più affollate. 40 e 120 sono per una frequenza di aggiornamento di 120 Hz: 40 resta " + "stabile ed è più fluido di 30; 120 si raggiunge solo nei posti più tranquilli."}}, + {{"How many times a second the TV shows a new picture. 120 Hz needs a TV that takes it and 120 Hz output on " + "in the PS5's settings; without them the game stays at 60 Hz. It's what lets the 40 and 120 FPS limits run " + "evenly. It switches as soon as you choose it.", + "Combien de fois par seconde la TV affiche une nouvelle image. 120\u00A0Hz demande une TV compatible et la " + "sortie 120\u00A0Hz activée dans les paramètres de la PS5\u00A0; sinon le jeu reste à 60\u00A0Hz. C'est " + "ce qui rend régulières les limites de 40 et 120 images par seconde. Le changement se fait dès que vous le " + "choisissez.", + "Wie oft pro Sekunde der Fernseher ein neues Bild zeigt. 120 Hz braucht einen Fernseher, der es kann, und " + "die 120-Hz-Ausgabe in den Einstellungen der PS5; sonst bleibt das Spiel bei 60 Hz. Erst damit laufen die " + "Limits 40 und 120 FPS gleichmäßig. Wechselt sofort, wenn du es auswählst.", + "Cuántas veces por segundo la tele muestra una imagen nueva. 120 Hz necesita una tele compatible y la salida " + "a 120 Hz activada en los ajustes de la PS5; si no, el juego se queda en 60 Hz. Es lo que hace que los " + "límites de 40 y 120 FPS vayan regulares. Cambia en cuanto lo eliges.", + "Quante volte al secondo la TV mostra un'immagine nuova. 120 Hz richiede una TV compatibile e l'uscita a " + "120 Hz attiva nelle impostazioni della PS5; altrimenti il gioco resta a 60 Hz. È ciò che rende regolari i " + "limiti di 40 e 120 FPS. Cambia appena lo scegli."}}, + {{"Switch to 120 Hz?", "Passer à 120\u00A0Hz\u00A0?", "Auf 120 Hz umschalten?", "¿Cambiar a 120 Hz?", + "Passare a 120 Hz?"}}, + {{"The screen goes dark for a moment while the TV changes over.", + "L'écran devient noir un instant pendant que la TV change de mode.", + "Der Bildschirm wird kurz schwarz, während der Fernseher umschaltet.", + "La pantalla se queda en negro un momento mientras la tele cambia.", + "Lo schermo diventa nero per un attimo mentre la TV cambia modalità."}}, + {{"Switch", "Changer", "Umschalten", "Cambiar", "Cambia"}}, + {{"Later", "Plus tard", "Später", "Más tarde", "Più tardi"}}, + {{"120 Hz from the next start", "120\u00A0Hz au prochain lancement", "120 Hz ab dem nächsten Start", + "120 Hz desde el próximo inicio", "120 Hz dal prossimo avvio"}}, + {{"Now at 120 Hz", "Maintenant à 120\u00A0Hz", "Jetzt mit 120 Hz", "Ahora a 120 Hz", "Ora a 120 Hz"}}, + {{"Back to 60 Hz", "Retour à 60\u00A0Hz", "Zurück auf 60 Hz", "De vuelta a 60 Hz", "Di nuovo a 60 Hz"}}, + {{"Still at 60 Hz", "Toujours à 60\u00A0Hz", "Weiter bei 60 Hz", "Sigue a 60 Hz", "Ancora a 60 Hz"}}, + {{"Right now: {}", "En ce moment\u00A0: {}", "Gerade: {}", "Ahora mismo: {}", "In questo momento: {}"}}, + {{"The TV didn't take 120 Hz. Check that 120 Hz output is on in the PS5's settings (Screen and Video, Video " + "Output) and that the TV runs 120 Hz at the resolution the PS5 sends it. 4K at 120 Hz needs HDMI 2.1.", + "La TV n'a pas accepté le 120\u00A0Hz. Vérifiez que la sortie 120\u00A0Hz est activée dans les paramètres " + "de la PS5 (Écran et vidéo, Sortie vidéo) et que la TV accepte le 120\u00A0Hz dans la résolution envoyée " + "par la PS5. La 4K à 120\u00A0Hz demande le HDMI 2.1.", + "Der Fernseher hat 120 Hz nicht angenommen. Prüf, ob die 120-Hz-Ausgabe in den Einstellungen der PS5 an ist " + "(Bildschirm und Video, Videoausgabe) und ob der Fernseher 120 Hz in der Auflösung kann, die die PS5 " + "schickt. 4K mit 120 Hz braucht HDMI 2.1.", + "La tele no ha aceptado los 120 Hz. Comprueba que la salida a 120 Hz está activada en los ajustes de la PS5 " + "(Pantalla y vídeo, Salida de vídeo) y que la tele admite 120 Hz en la resolución que le manda la PS5. 4K a " + "120 Hz necesita HDMI 2.1.", + "La TV non ha accettato i 120 Hz. Controlla che l'uscita a 120 Hz sia attiva nelle impostazioni della PS5 " + "(Schermo e video, Uscita video) e che la TV regga i 120 Hz alla risoluzione che le manda la PS5. Il 4K a " + "120 Hz richiede l'HDMI 2.1."}}, + {{"Shows buildings, cars and people at their most detailed as soon as that detail has loaded, near or far, " + "instead of simpler versions in the distance. Can cost frame rate in busy streets.", + "Affiche bâtiments, voitures et passants dans leur version la plus détaillée dès qu'elle est chargée, de " + "près comme de loin, au lieu de versions simplifiées au loin. Peut coûter des images par seconde dans les " + "rues animées.", + "Zeigt Gebäude, Autos und Menschen in ihrer detailliertesten Form, sobald diese geladen ist, nah wie fern, " + "statt einfacherer Versionen in der Ferne. Kann in belebten Straßen Bildrate kosten.", + "Muestra edificios, coches y gente con su máximo detalle en cuanto ese detalle se ha cargado, cerca o " + "lejos, en vez de versiones más simples a distancia. Puede costar fotogramas en calles concurridas.", + "Mostra edifici, auto e persone al massimo dettaglio appena quel dettaglio è caricato, vicino o lontano, " + "invece delle versioni più semplici in lontananza. Può costare fotogrammi nelle strade affollate."}}, + {{"AMD FSR 3 adds a frame between each two the game draws. 30 to 60 holds the game at 30 FPS and shows 60, on any TV. 60 to 120 needs a 120 Hz TV and switches to it: the game runs at 60 and shows 120, and drops to 30 to 60 by itself in places it can't hold 60. Frame Limit doesn't apply while it's on. Adds a little input lag and turns on FSR 3 anti-aliasing. Experimental: tell us how it feels.", + "AMD FSR 3 ajoute une image entre chaque paire d'images du jeu. 30 à 60 tient le jeu à 30\u00A0images par seconde et en affiche 60, sur n'importe quelle TV. 60 à 120 demande une TV 120\u00A0Hz et y passe\u00A0: le jeu tourne à 60 et en affiche 120, et repasse de lui-même en 30 à 60 là où il ne tient pas 60. La limite d'images ne s'applique pas tant qu'elle est active. Ajoute un peu de latence et active l'anticrénelage FSR 3. Expérimental\u00A0: dites-nous ce que vous en pensez.", + "AMD FSR 3 fügt zwischen je zwei gezeichneten Bildern eines ein. 30 zu 60 hält das Spiel bei 30 Bildern pro Sekunde und zeigt 60, an jedem Fernseher. 60 zu 120 braucht einen 120-Hz-Fernseher und schaltet auf 120 Hz: Das Spiel läuft mit 60 und zeigt 120 und wechselt dort, wo es keine 60 hält, von selbst auf 30 zu 60. Die Bildratenbegrenzung gilt dann nicht. Etwas mehr Eingabeverzögerung; schaltet auch FSR-3-Kantenglättung ein. Experimentell: Sag uns, wie es sich anfühlt.", + "AMD FSR 3 añade un fotograma entre cada dos que dibuja el juego. 30 a 60 mantiene el juego a 30 FPS y muestra 60, en cualquier tele. 60 a 120 necesita una tele de 120 Hz y cambia a ella: el juego va a 60 y muestra 120, y pasa solo a 30 a 60 donde no aguanta 60. El límite de fotogramas no se aplica mientras está activa. Añade un poco de retraso de entrada y activa el antialiasing FSR 3. Experimental: cuéntanos qué tal va.", + "AMD FSR 3 aggiunge un fotogramma tra ogni due disegnati dal gioco. 30 a 60 tiene il gioco a 30 FPS e ne mostra 60, su qualsiasi TV. 60 a 120 richiede una TV a 120 Hz e ci passa: il gioco gira a 60 e ne mostra 120, e torna da solo a 30 a 60 dove non tiene i 60. Il limite fotogrammi non vale mentre è attiva. Aggiunge un po' di ritardo nei comandi e attiva l'antialiasing FSR 3. Sperimentale: facci sapere com'è."}}, + {{"Smooths jagged edges. FXAA is the lightest; SMAA is sharper and costs a little more. MSAA 4x + SMAA also " + "cleans up thin edges in the distance, like stairs and wires, but is heavy on the graphics card: new on the " + "PS5, try 1440p with it if the game slows down.", + "Adoucit les bords crénelés. FXAA est le plus léger\u00A0; SMAA est plus net et coûte un peu plus. " + "MSAA 4x + SMAA lisse aussi les bords fins au loin, comme les escaliers et les câbles, mais pèse lourd sur " + "la carte graphique\u00A0: nouveau sur PS5, essayez-le en 1440p si le jeu ralentit.", + "Glättet gezackte Kanten. FXAA ist am leichtesten; SMAA ist schärfer und kostet etwas mehr. MSAA 4x + SMAA " + "glättet auch dünne Kanten in der Ferne, etwa Treppen und Kabel, fordert die Grafikeinheit aber stark: neu " + "auf der PS5, probier dazu 1440p, wenn das Spiel langsamer wird.", + "Suaviza los bordes dentados. FXAA es el más ligero; SMAA es más nítido y cuesta un poco más. MSAA 4x + SMAA " + "también limpia los bordes finos a lo lejos, como escaleras y cables, pero exige mucho a la gráfica: es " + "nuevo en PS5, pruébalo a 1440p si el juego se ralentiza.", + "Ammorbidisce i bordi seghettati. FXAA è il più leggero; SMAA è più nitido e costa un po' di più. " + "MSAA 4x + SMAA pulisce anche i bordi sottili in lontananza, come scale e cavi, ma pesa molto sulla scheda " + "grafica: è nuovo su PS5, provalo a 1440p se il gioco rallenta."}}, + {{"Keeps textures seen at an angle sharp, like roads and pavements going into the distance. Original is the " + "Xbox 360's; 16x is the sharpest and costs very little.", + "Garde nettes les textures vues en biais, comme les routes et les trottoirs qui partent au loin. D'origine " + "correspond à la Xbox 360\u00A0; 16x est le plus net et ne coûte presque rien.", + "Hält schräg gesehene Texturen scharf, etwa Straßen und Gehwege, die in die Ferne laufen. Original entspricht " + "der Xbox 360; 16x ist am schärfsten und kostet sehr wenig.", + "Mantiene nítidas las texturas vistas en ángulo, como carreteras y aceras que se pierden a lo lejos. " + "Original es el de Xbox 360; 16x es el más nítido y apenas cuesta.", + "Mantiene nitide le texture viste di sbieco, come strade e marciapiedi che si perdono in lontananza. " + "Originale è quello di Xbox 360; 16x è il più nitido e costa pochissimo."}}, + {{"The shaders from FusionFix, the PC mod: depth of field, a better sky, bloom and fog, and nicer water and " + "light beams. Off is the Xbox 360's look. New on the PS5: tell us if something looks wrong.", + "Les shaders de FusionFix, le mod PC\u00A0: profondeur de champ, un ciel, un bloom et un brouillard plus " + "réussis, une eau et des faisceaux lumineux plus beaux. Désactivé garde le rendu de la Xbox 360. Nouveau " + "sur PS5\u00A0: dites-nous si quelque chose cloche.", + "Die Shader aus FusionFix, der PC-Mod: Tiefenschärfe, ein besserer Himmel, Bloom und Nebel sowie schöneres " + "Wasser und Lichtstrahlen. Aus ist der Look der Xbox 360. Neu auf der PS5: Sag uns, wenn etwas falsch " + "aussieht.", + "Los shaders de FusionFix, el mod de PC: profundidad de campo, mejor cielo, bloom y niebla, y agua y haces " + "de luz más bonitos. Desactivado es el aspecto de Xbox 360. Es nuevo en PS5: avísanos si algo se ve mal.", + "Gli shader di FusionFix, la mod per PC: profondità di campo, cielo, bloom e nebbia migliori, acqua e fasci " + "di luce più belli. Disattivato è l'aspetto di Xbox 360. È nuovo su PS5: dicci se qualcosa non va."}}, + {{"How sharp shadows are. High is twice the Xbox 360's detail; Very High doubles it again and asks more of the " + "graphics card.", + "La netteté des ombres. Élevée offre deux fois le détail de la Xbox 360\u00A0; Très élevée le double " + "encore et demande plus à la carte graphique.", + "Wie scharf Schatten sind. Hoch hat doppelt so viel Detail wie die Xbox 360; Sehr hoch verdoppelt es noch " + "einmal und fordert die Grafikeinheit stärker.", + "La nitidez de las sombras. Alta tiene el doble de detalle que Xbox 360; Muy alta lo vuelve a duplicar y " + "exige más a la gráfica.", + "La nitidezza delle ombre. Alta ha il doppio del dettaglio di Xbox 360; Molto alta lo raddoppia ancora e " + "chiede di più alla scheda grafica."}}, + {{"The blur when driving fast or turning quickly.", + "Le flou quand on roule vite ou qu'on tourne rapidement.", + "Die Unschärfe bei schneller Fahrt oder schnellen Drehungen.", + "El desenfoque al conducir rápido o girar de golpe.", + "La sfocatura quando guidi veloce o giri rapidamente."}}, + {{"How sharp water, mirror and car reflections are. Original is the Xbox 360's; 1080p is cleaner.", + "La netteté des reflets de l'eau, des miroirs et des voitures. D'origine correspond à la Xbox 360 ; " + "1080p est plus propre.", + "Wie scharf Spiegelungen auf Wasser, in Spiegeln und auf Autos sind. Original entspricht der Xbox 360; " + "1080p ist sauberer.", + "La nitidez de los reflejos del agua, los espejos y los coches. Original es la de Xbox 360; 1080p se ve más " + "limpio.", + "La nitidezza dei riflessi su acqua, specchi e auto. Originale è quella di Xbox 360; 1080p è più pulito."}}, + {{"How far out shadows are drawn. Original is the Xbox 360's; 2x reaches further but costs frame rate in the " + "city.", + "Jusqu'où les ombres sont affichées. D'origine correspond à la Xbox 360 ; 2x va plus loin mais " + "coûte des images par seconde en ville.", + "Wie weit Schatten gezeichnet werden. Original entspricht der Xbox 360; 2x reicht weiter, kostet in der " + "Stadt aber Bildrate.", + "Hasta dónde se dibujan las sombras. Original es la de Xbox 360; 2x llega más lejos pero cuesta fotogramas " + "en la ciudad.", + "Fino a che distanza vengono disegnate le ombre. Originale è quella di Xbox 360; 2x arriva più lontano ma " + "costa fotogrammi in città."}}, + {{"Shadows of small things like signs, bins and debris, about a fifth of what is drawn in busy streets. Off " + "skips them for a higher frame rate.", + "Les ombres des petites choses comme les panneaux, les poubelles et les débris, environ un cinquième de ce " + "qui est affiché dans les rues animées. Désactivé les ignore pour plus d'images par seconde.", + "Schatten kleiner Dinge wie Schilder, Mülltonnen und Trümmer, etwa ein Fünftel dessen, was in belebten " + "Straßen gezeichnet wird. Aus lässt sie weg, für eine höhere Bildrate.", + "Las sombras de cosas pequeñas como señales, papeleras y escombros, cerca de una quinta parte de lo que se " + "dibuja en calles concurridas. Desactivado las omite para ganar fotogramas.", + "Le ombre di piccoli oggetti come cartelli, bidoni e detriti, circa un quinto di ciò che viene disegnato " + "nelle strade affollate. Disattivato le salta per più fotogrammi al secondo."}}, + {{"How often the reflections on cars and glass are redrawn. Every other frame looks the same in motion and " + "leaves more time for the rest of the frame.", + "La fréquence à laquelle les reflets sur les voitures et les vitres sont redessinés. Une image sur deux " + "paraît identique en mouvement et laisse plus de temps au reste de l'image.", + "Wie oft die Spiegelungen auf Autos und Glas neu gezeichnet werden. Jedes zweite Bild sieht in Bewegung " + "gleich aus und lässt dem Rest des Bildes mehr Zeit.", + "Cada cuánto se redibujan los reflejos en coches y cristales. Cada dos fotogramas se ve igual en movimiento " + "y deja más tiempo para el resto del fotograma.", + "Ogni quanto vengono ridisegnati i riflessi su auto e vetri. Un fotogramma su due sembra uguale in " + "movimento e lascia più tempo al resto del fotogramma."}}, + {{"How often the reflection on water is redrawn. Every other frame leaves more time for the rest of the frame; " + "when the camera turns quickly the reflection can trail a frame behind.", + "La fréquence à laquelle le reflet sur l'eau est redessiné. Une image sur deux laisse plus de temps au reste " + "de l'image\u00A0; quand la caméra tourne vite, le reflet peut avoir une image de retard.", + "Wie oft die Spiegelung auf dem Wasser neu gezeichnet wird. Jedes zweite Bild lässt dem Rest des Bildes mehr " + "Zeit; dreht sich die Kamera schnell, kann die Spiegelung ein Bild hinterherhängen.", + "Cada cuánto se redibuja el reflejo del agua. Cada dos fotogramas deja más tiempo para el resto del " + "fotograma; si la cámara gira deprisa, el reflejo puede ir un fotograma por detrás.", + "Ogni quanto viene ridisegnato il riflesso sull'acqua. Un fotogramma su due lascia più tempo al resto del " + "fotogramma; quando la telecamera gira in fretta il riflesso può restare indietro di un fotogramma."}}, + {{"The grainy, film-like look of The Lost and Damned. Off gives it a clean picture; GTA IV and The Ballad of " + "Gay Tony have none.", + "L'aspect granuleux façon pellicule de The Lost and Damned. Désactivé, l'image est nette\u00A0; GTA IV et " + "The Ballad of Gay Tony n'en ont pas.", + "Der körnige Filmlook von The Lost and Damned. Aus ergibt ein klares Bild; GTA IV und The Ballad of Gay " + "Tony haben keinen.", + "El aspecto granulado de película de The Lost and Damned. Desactivado deja la imagen limpia; GTA IV y The " + "Ballad of Gay Tony no lo tienen.", + "L'aspetto granuloso da pellicola di The Lost and Damned. Disattivato dà un'immagine pulita; GTA IV e The " + "Ballad of Gay Tony non ce l'hanno."}}, + {{"How long the car camera stays where you pointed it with the right stick before it swings back behind the " + "car. Original is the Xbox 360's: it starts swinging back right away.", + "Combien de temps la caméra de véhicule reste là où vous l'avez tournée avec le stick droit avant de revenir " + "derrière le véhicule. D'origine, c'est celle de la Xbox 360\u00A0: elle revient tout de suite.", + "Wie lange die Fahrzeugkamera dort bleibt, wohin du sie mit dem rechten Stick gedreht hast, bevor sie hinter " + "das Fahrzeug zurückschwenkt. Original entspricht der Xbox 360: Sie schwenkt sofort zurück.", + "Cuánto tiempo se queda la cámara de vehículo donde la apuntaste con el stick derecho antes de volver detrás " + "del vehículo. Original es la de Xbox 360: empieza a volver enseguida.", + "Quanto resta la telecamera veicolo dove l'hai girata con la levetta destra prima di tornare dietro al " + "veicolo. Originale è quella di Xbox 360: inizia a tornare subito."}}, + {{"How quickly the car camera swings round to follow the car through turns and back behind it. Original is " + "the Xbox 360's.", + "La vitesse à laquelle la caméra de véhicule pivote pour suivre le véhicule dans les virages et revenir " + "derrière lui. D'origine, c'est celle de la Xbox 360.", + "Wie schnell die Fahrzeugkamera dem Fahrzeug durch Kurven folgt und wieder hinter es schwenkt. Original " + "entspricht der Xbox 360.", + "La rapidez con la que la cámara de vehículo gira para seguir al vehículo en las curvas y volver detrás de " + "él. Original es la de Xbox 360.", + "Quanto velocemente la telecamera veicolo gira per seguire il veicolo in curva e tornargli dietro. " + "Originale è quella di Xbox 360."}}, + {{"Puts the camera straight behind the car instead of a little off to the side, like FusionFix's option on " + "PC. Off is the Xbox 360's.", + "Place la caméra juste derrière le véhicule au lieu d'un peu sur le côté, comme l'option de FusionFix sur " + "PC. Désactivé, c'est celle de la Xbox 360.", + "Setzt die Kamera direkt hinter das Fahrzeug statt etwas zur Seite, wie die Option von FusionFix am PC. " + "Aus entspricht der Xbox 360.", + "Coloca la cámara justo detrás del vehículo en vez de un poco a un lado, como la opción de FusionFix en PC. " + "Desactivada es la de Xbox 360.", + "Mette la telecamera proprio dietro al veicolo invece che un po' di lato, come l'opzione di FusionFix su PC. " + "Disattivata è quella di Xbox 360."}}, + {{"Goes straight to the game's start screen, past the logos and the legal screens.", + "Va directement à l'écran titre du jeu, sans les logos ni les mentions légales.", + "Geht direkt zum Startbildschirm des Spiels, vorbei an den Logos und Rechtshinweisen.", + "Va directo a la pantalla de inicio del juego, sin los logotipos ni los avisos legales.", + "Va direttamente alla schermata iniziale del gioco, saltando loghi e note legali."}}, + {{"Adds the game's trophies to the PS5's own Trophies list, with the ones you've already earned, and shows " + "the PS5's trophy pop-ups as you earn them. Needs a payload loader (etaHEN, elfldr...).", + "Ajoute les trophées du jeu à la liste Trophées de la PS5, avec ceux que vous avez déjà obtenus, et affiche " + "les notifications de trophée de la PS5 quand vous les obtenez. Nécessite un chargeur de payloads (etaHEN, " + "elfldr...).", + "Fügt die Trophäen des Spiels der Trophäenliste der PS5 hinzu, samt den bereits verdienten, und zeigt die " + "Trophäen-Meldungen der PS5, sobald du sie verdienst. Benötigt einen Payload-Loader (etaHEN, elfldr...).", + "Añade los trofeos del juego a la lista de Trofeos de la PS5, con los que ya hayas conseguido, y muestra los " + "avisos de trofeo de la PS5 al conseguirlos. Necesita un cargador de payloads (etaHEN, elfldr...).", + "Aggiunge i trofei del gioco all'elenco Trofei della PS5, con quelli già ottenuti, e mostra le notifiche dei " + "trofei della PS5 quando li ottieni. Richiede un caricatore di payload (etaHEN, elfldr...)."}}, + {{"Turns the controller extras below on or off together: the light bar, adaptive triggers and extra rumble.", + "Active ou coupe d'un coup les fonctions de la manette ci-dessous\u00A0: barre lumineuse, gâchettes " + "adaptatives et vibrations supplémentaires.", + "Schaltet die Controller-Extras darunter gemeinsam an oder aus: Lichtleiste, adaptive Trigger und zusätzliche " + "Vibration.", + "Activa o desactiva a la vez los extras del mando de abajo: la barra de luz, los gatillos adaptativos y la " + "vibración extra.", + "Attiva o disattiva insieme le funzioni del controller qui sotto: barra luminosa, grilletti adattivi e " + "vibrazione extra."}}, + {{"The controller's light: blue in GTA IV, and flashing red and blue like police lights while you're wanted.", + "La lumière de la manette\u00A0: bleue dans GTA IV, et rouge et bleu clignotants comme un gyrophare quand " + "vous êtes recherché.", + "Das Licht des Controllers: blau in GTA IV und rot-blau blinkend wie ein Blaulicht, solange du gesucht wirst.", + "La luz del mando: azul en GTA IV, y roja y azul intermitente como las de la policía mientras te buscan.", + "La luce del controller: blu in GTA IV, e rossa e blu lampeggiante come una sirena della polizia quando sei " + "ricercato."}}, + {{"R2 pulls like a real gun's trigger and kicks with each shot, L2 pushes back while you aim, and both feel " + "like pedals when you drive.", + "R2 se presse comme la détente d'une vraie arme et recule à chaque tir, L2 résiste quand vous visez, et les " + "deux font office de pédales en voiture.", + "R2 zieht sich wie der Abzug einer echten Waffe und schlägt bei jedem Schuss zurück, L2 drückt beim Zielen " + "dagegen, und beim Fahren fühlen sich beide wie Pedale an.", + "R2 se aprieta como el gatillo de un arma real y retrocede con cada disparo, L2 opone resistencia al apuntar, " + "y al conducir los dos parecen pedales.", + "R2 si preme come il grilletto di un'arma vera e rincula a ogni colpo, L2 oppone resistenza quando miri, e " + "alla guida entrambi sembrano pedali."}}, + {{"Adds rumble when you shoot, get hit or are near an explosion, on top of the game's own.", + "Ajoute des vibrations quand vous tirez, êtes touché ou près d'une explosion, en plus de celles du jeu.", + "Fügt Vibration hinzu, wenn du schießt, getroffen wirst oder eine Explosion in der Nähe ist, zusätzlich zur " + "eigenen des Spiels.", + "Añade vibración al disparar, recibir un impacto o estar cerca de una explosión, además de la del propio " + "juego.", + "Aggiunge vibrazione quando spari, vieni colpito o sei vicino a un'esplosione, oltre a quella del gioco."}}, + {{"How far the sound runs behind the picture. Lower is closer to the action; if the sound crackles or cuts " + "out, pick the next one up. Wireless headsets add a little delay of their own.", + "Le retard du son sur l'image. Plus bas, le son colle mieux à l'action\u00A0; s'il grésille ou se coupe, " + "prenez le réglage suivant. Les casques sans fil ajoutent leur propre petit délai.", + "Wie weit der Ton dem Bild hinterherläuft. Niedriger ist näher am Geschehen; wenn der Ton knistert oder " + "aussetzt, nimm die nächsthöhere Stufe. Kabellose Headsets fügen selbst etwas Verzögerung hinzu.", + "Cuánto va el sonido por detrás de la imagen. Más bajo se acerca más a la acción; si el sonido chisporrotea " + "o se corta, elige el siguiente. Los auriculares inalámbricos añaden un poco de retraso propio.", + "Quanto il suono è in ritardo rispetto all'immagine. Più basso è più vicino all'azione; se l'audio gracchia " + "o si interrompe, scegli il livello successivo. Le cuffie wireless aggiungono un po' di ritardo proprio."}}, + {{"How strong the adaptive triggers and the extra rumble feel.", + "La force des gâchettes adaptatives et des vibrations supplémentaires.", + "Wie stark sich die adaptiven Trigger und die zusätzliche Vibration anfühlen.", + "La fuerza de los gatillos adaptativos y de la vibración extra.", + "Quanto sono forti i grilletti adattivi e la vibrazione extra."}}, + {{"How loud the music is here in the hub. It changes right away.", + "Le volume de la musique ici, dans le hub. Le changement est immédiat.", + "Wie laut die Musik hier im Hub ist. Ändert sich sofort.", + "El volumen de la música aquí en el hub. Cambia al momento.", + "Il volume della musica qui nell'hub. Cambia subito."}}, + {{"Writes what the game does to /app0/logs while it runs. Send these with a bug report. Takes effect the next " + "time the app starts.", + "Écrit ce que fait le jeu dans /app0/logs pendant qu'il tourne. Joignez ces fichiers à un rapport de bug. " + "Prend effet au prochain lancement de l'application.", + "Schreibt mit, was das Spiel tut, nach /app0/logs. Schick diese Dateien mit einem Fehlerbericht mit. Wirkt " + "ab dem nächsten Start der App.", + "Escribe lo que hace el juego en /app0/logs mientras funciona. Envíalos con cada informe de error. Se " + "aplica la próxima vez que se abra la aplicación.", + "Scrive ciò che fa il gioco in /app0/logs mentre è in esecuzione. Inviali con una segnalazione di bug. Ha " + "effetto al prossimo avvio dell'app."}}, + {{"How much goes into the log. Normal is enough for most reports; Detailed and Everything help with crashes " + "and freezes but make big files and can slow the game.", + "Quantité d'informations dans le journal. Normal suffit pour la plupart des rapports ; Détaillé et Tout " + "aident pour les plantages et les blocages mais créent de gros fichiers et peuvent ralentir le jeu.", + "Wie viel ins Log kommt. Normal reicht für die meisten Berichte; Ausführlich und Alles helfen bei Abstürzen " + "und Hängern, erzeugen aber große Dateien und können das Spiel bremsen.", + "Cuánto se escribe en el registro. Normal basta para la mayoría de informes; Detallado y Todo ayudan con " + "cuelgues y bloqueos, pero crean archivos grandes y pueden ralentizar el juego.", + "Quanto finisce nel log. Normale basta per la maggior parte delle segnalazioni; Dettagliato e Tutto " + "aiutano con crash e blocchi ma creano file grandi e possono rallentare il gioco."}}, + {{"Own Cores gives the game's render thread and the two renderer threads a CPU core each and moves everything " + "else off them (about 10% faster than Anywhere). Compare switches between two versions of Own Cores every 10 " + "seconds and notes each in the log.", + "Cœurs dédiés donne au thread de rendu du jeu et aux deux threads du moteur de rendu un cœur de processeur " + "chacun et en écarte tout le reste (environ 10 % plus rapide que Partout). Comparer alterne entre deux " + "versions de Cœurs dédiés toutes les 10 secondes et note chacune dans le journal.", + "Eigene Kerne gibt dem Render-Thread des Spiels und den beiden Renderer-Threads je einen CPU-Kern und hält " + "alles andere davon fern (etwa 10 % schneller als Überall). Vergleichen wechselt alle 10 Sekunden zwischen " + "zwei Varianten von Eigene Kerne und vermerkt jede im Log.", + "Núcleos propios da al hilo de renderizado del juego y a los dos hilos del renderizador un núcleo de CPU a " + "cada uno y aparta todo lo demás (alrededor de un 10 % más rápido que Cualquiera). Comparar alterna cada " + "10 segundos entre dos versiones de Núcleos propios y anota cada una en el registro.", + "Core dedicati dà al thread di rendering del gioco e ai due thread del renderer un core della CPU ciascuno e " + "sposta tutto il resto altrove (circa il 10% più veloce di Ovunque). Confronta alterna ogni 10 secondi due " + "versioni di Core dedicati e annota ciascuna nel log."}}, + {{"How the game's render thread hands each draw to the renderer. Lean holds every buffer and texture once for " + "two frames instead of once per draw and fetches what a capture reads ahead of time. Same picture either way. " + "Compare stays on Lean and switches its fetching ahead off and on every 10 seconds, noting each in the log.", + "Comment le thread de rendu du jeu transmet chaque dessin au moteur de rendu. Allégée retient chaque tampon " + "et texture une fois pour deux images au lieu d'une fois par dessin, et charge à l'avance ce que la capture " + "va lire. Même image dans les deux cas. Comparer reste sur Allégée et active ou coupe ce chargement anticipé " + "toutes les 10 secondes, en le notant dans le journal.", + "Wie der Render-Thread des Spiels jeden Zeichenaufruf an den Renderer übergibt. Schlank hält jeden Puffer " + "und jede Textur einmal für zwei Bilder statt einmal pro Zeichenaufruf und lädt vorab, was die Erfassung " + "liest. Das Bild ist in beiden Fällen gleich. Vergleichen bleibt bei Schlank und schaltet das Vorabladen " + "alle 10 Sekunden aus und ein, jeweils mit Vermerk im Log.", + "Cómo pasa el hilo de renderizado del juego cada dibujo al renderizador. Ligera retiene cada búfer y textura " + "una vez cada dos fotogramas en lugar de una vez por dibujo, y adelanta la lectura de lo que va a capturar. " + "La imagen es la misma en ambos casos. Comparar se queda en Ligera y activa y desactiva esa lectura " + "anticipada cada 10 segundos, anotándolo en el registro.", + "Come il thread di rendering del gioco passa ogni disegno al renderer. Leggera trattiene ogni buffer e " + "texture una volta ogni due fotogrammi invece che a ogni disegno e legge in anticipo ciò che la cattura " + "userà. L'immagine è la stessa in entrambi i casi. Confronta resta su Leggera e attiva e disattiva la " + "lettura anticipata ogni 10 secondi, annotandolo nel log."}}, + {{"Records every frame's time to a CSV file in /app0/logs, for checking stutter and slowdowns.", + "Enregistre le temps de chaque image dans un fichier CSV dans /app0/logs, pour étudier les saccades et les " + "ralentissements.", + "Zeichnet die Zeit jedes Bildes in einer CSV-Datei in /app0/logs auf, um Ruckler und Einbrüche zu prüfen.", + "Guarda el tiempo de cada fotograma en un archivo CSV en /app0/logs, para revisar tirones y bajones.", + "Registra il tempo di ogni fotogramma in un file CSV in /app0/logs, per controllare scatti e " + "rallentamenti."}}, + {{"When the game stops drawing for a few seconds, writes where every thread is to the log.", + "Quand le jeu cesse d'afficher pendant quelques secondes, écrit dans le journal où en est chaque thread.", + "Wenn das Spiel einige Sekunden lang nichts mehr zeichnet, schreibt es ins Log, wo jeder Thread steht.", + "Si el juego deja de dibujar durante unos segundos, escribe en el registro dónde está cada hilo.", + "Quando il gioco smette di disegnare per qualche secondo, scrive nel log dove si trova ogni thread."}}, + {{"Unpacks the game's textures on the graphics card. Off does it on the CPU the old way: slower, but a test " + "for broken or missing textures.", + "Décompresse les textures du jeu sur la carte graphique. Désactivé le fait sur le processeur comme " + "avant : plus lent, mais utile pour tester des textures cassées ou manquantes.", + "Entpackt die Texturen des Spiels auf der Grafikeinheit. Aus macht es wie früher auf der CPU: langsamer, " + "aber ein Test bei kaputten oder fehlenden Texturen.", + "Desempaqueta las texturas del juego en la gráfica. Desactivado lo hace en la CPU como antes: más lento, " + "pero sirve para probar texturas rotas o que faltan.", + "Decomprime le texture del gioco sulla scheda grafica. Disattivato lo fa sulla CPU come prima: più lento, " + "ma utile per verificare texture rotte o mancanti."}}, + {{"Logs when each stage of every frame finished, to find what holds the frame rate back. The log grows " + "quickly with it on.", + "Note quand chaque étape de chaque image se termine, pour trouver ce qui limite les images par seconde. Le " + "journal grossit vite quand c'est activé.", + "Protokolliert, wann jede Stufe jedes Bildes fertig war, um zu finden, was die Bildrate bremst. Das Log " + "wächst damit schnell.", + "Anota cuándo termina cada etapa de cada fotograma, para encontrar qué frena los fotogramas. El registro " + "crece rápido si está activado.", + "Annota quando finisce ogni fase di ogni fotogramma, per capire cosa frena il frame rate. Con questa " + "opzione il log cresce in fretta."}}, + {{"How the game's frames reach the TV. As Finished sends each frame as soon as it's ready. Every Vblank " + "redraws the screen at every refresh, as Beta 6 did, with two refreshes more input lag. For comparing " + "which feels smoother.", + "Comment les images du jeu arrivent à la TV. Dès qu'elles sont prêtes envoie chaque image aussitôt " + "terminée. À chaque rafraîchissement redessine l'écran à chaque rafraîchissement, comme la bêta 6, avec " + "deux rafraîchissements de latence en plus. Pour comparer ce qui paraît le plus fluide.", + "Wie die Bilder des Spiels zum Fernseher kommen. Sobald fertig schickt jedes Bild, sobald es bereit ist. " + "Bei jedem Vblank zeichnet den Bildschirm bei jeder Bildwiederholung neu, wie Beta 6, mit zwei " + "Bildwiederholungen mehr Eingabeverzögerung. Zum Vergleichen, was flüssiger wirkt.", + "Cómo llegan los fotogramas del juego a la TV. Al terminar envía cada fotograma en cuanto está listo. En " + "cada refresco redibuja la pantalla en cada refresco, como la beta 6, con dos refrescos más de retraso de " + "entrada. Para comparar cuál se siente más fluido.", + "Come arrivano alla TV i fotogrammi del gioco. Appena pronti invia ogni fotogramma non appena è finito. A " + "ogni refresh ridisegna lo schermo a ogni refresh, come la beta 6, con due refresh in più di input lag. Per " + "confrontare quale sembra più fluido."}}, + {{"Re-reads textures that change in memory without the game saying so. Off brings back the wrong map and " + "radar tiles. Only for comparing performance.", + "Relit les textures qui changent en mémoire sans que le jeu le signale. Désactivé fait revenir les " + "mauvaises tuiles de la carte et du radar. Seulement pour comparer les performances.", + "Liest Texturen neu, die sich im Speicher ändern, ohne dass das Spiel es meldet. Aus bringt die falschen " + "Karten- und Radarkacheln zurück. Nur zum Vergleichen der Leistung.", + "Vuelve a leer las texturas que cambian en memoria sin que el juego lo diga. Desactivado trae de vuelta " + "las casillas equivocadas del mapa y del radar. Solo para comparar el rendimiento.", + "Rilegge le texture che cambiano in memoria senza che il gioco lo segnali. Disattivato riporta i riquadri " + "sbagliati della mappa e del radar. Solo per confrontare le prestazioni."}}, + + // ---- Reset All + {{"Reset all settings?", "Tout réinitialiser ?", "Alle Einstellungen zurücksetzen?", + "¿Restablecer todos los ajustes?", "Ripristinare tutte le impostazioni?"}}, + {{"Everything on the Display, Game and Debug pages goes back to the recommended settings. The language stays " + "as it is.", + "Tout ce qui se trouve dans Affichage, Jeu et Débogage revient aux réglages recommandés. La langue ne change " + "pas.", + "Alles auf den Seiten Anzeige, Spiel und Debug wird auf die empfohlenen Einstellungen zurückgesetzt. Die " + "Sprache bleibt, wie sie ist.", + "Todo lo de Pantalla, Juego y Depuración vuelve a los ajustes recomendados. El idioma no cambia.", + "Tutto nelle schede Schermo, Gioco e Debug torna alle impostazioni consigliate. La lingua resta com'è."}}, + {{"Settings reset", "Réglages réinitialisés", "Einstellungen zurückgesetzt", "Ajustes restablecidos", + "Impostazioni ripristinate"}}, + + // ---- Home + {{"Not installed yet", "Pas encore installé", "Noch nicht installiert", "Aún no instalado", + "Non ancora installato"}}, + {{"Press X to set it up from your disc image and title update.", + "Appuyez sur X pour l'installer depuis votre image disque et la mise à jour du titre.", + "Drücke X, um es aus deinem Disc-Image und dem Title Update einzurichten.", + "Pulsa X para instalarlo desde tu imagen de disco y la actualización del título.", + "Premi X per installarlo dalla tua immagine disco e dall'aggiornamento del titolo."}}, + + // ---- Saves + {{"Restore a backup", "Restaurer une copie", "Backup wiederherstellen", "Restaurar una copia", + "Ripristina un backup"}}, + {{"Your saves {}", "Vos sauvegardes {}", "Deine Spielstände {}", "Tus partidas {}", + "I tuoi salvataggi {}"}}, + {{"Back Up Now", "Faire une copie maintenant", "Jetzt sichern", "Hacer copia ahora", "Esegui backup ora"}}, + {{"Import Xbox 360 Saves", "Importer des sauvegardes Xbox 360", "Xbox-360-Spielstände importieren", + "Importar partidas de Xbox 360", "Importa salvataggi Xbox 360"}}, + {{"{} waiting", "{} en attente", "{} bereit", "{} pendientes", "{} in attesa"}}, + {{"None waiting", "Aucune en attente", "Keine bereit", "Ninguna pendiente", "Nessuno in attesa"}}, + {{"Copies all of your GTA IV saves into SaveBackups, named by the date and time.", + "Copie toutes vos sauvegardes de GTA IV dans SaveBackups, avec la date et l'heure pour nom.", + "Kopiert alle deine GTA-IV-Spielstände nach SaveBackups, benannt nach Datum und Uhrzeit.", + "Copia todas tus partidas de GTA IV en SaveBackups, con la fecha y la hora como nombre.", + "Copia tutti i tuoi salvataggi di GTA IV in SaveBackups, con data e ora come nome."}}, + {{"Bring your Xbox 360 progress over. Copy the save files from your 360 (on its drive they're in " + "Content//545407F2/00000001) into this folder on the PS5:", + "Récupérez votre progression Xbox 360. Copiez les fichiers de sauvegarde de votre 360 (sur son disque, " + "ils sont dans Content//545407F2/00000001) dans ce dossier de la PS5 :", + "Hol deinen Xbox-360-Fortschritt rüber. Kopiere die Spielstand-Dateien deiner 360 (auf ihrer Festplatte " + "unter Content//545407F2/00000001) in diesen Ordner auf der PS5:", + "Trae tu progreso de Xbox 360. Copia los archivos de guardado de tu 360 (en su disco están en " + "Content//545407F2/00000001) en esta carpeta de la PS5:", + "Porta qui i tuoi progressi di Xbox 360. Copia i file di salvataggio della tua 360 (sul suo disco sono in " + "Content//545407F2/00000001) in questa cartella della PS5:"}}, + {{"Press Cross to import them. Your saves are backed up first, and imported files move to import/done.", + "Appuyez sur X pour les importer. Vos sauvegardes sont d'abord copiées, et les fichiers importés vont dans " + "import/done.", + "Drücke X, um sie zu importieren. Deine Spielstände werden vorher gesichert, und importierte Dateien kommen " + "nach import/done.", + "Pulsa X para importarlas. Antes se hace una copia de tus partidas, y los archivos importados pasan a " + "import/done.", + "Premi X per importarli. Prima viene fatto un backup dei tuoi salvataggi, e i file importati vengono " + "spostati in import/done."}}, + {{"Files", "Fichiers", "Dateien", "Archivos", "File"}}, + {{"Size", "Taille", "Größe", "Tamaño", "Dimensione"}}, + {{"Type", "Type", "Typ", "Tipo", "Tipo"}}, + {{"Changed", "Modifié", "Geändert", "Modificado", "Modificato"}}, + {{"Restoring puts these saves back. The saves you have now are backed up first.", + "La restauration remet ces sauvegardes en place. Celles que vous avez maintenant sont copiées d'abord.", + "Beim Wiederherstellen kommen diese Spielstände zurück. Deine aktuellen Spielstände werden vorher gesichert.", + "Al restaurar vuelven estas partidas. Antes se hace una copia de las que tienes ahora.", + "Il ripristino rimette questi salvataggi. Prima viene fatto un backup di quelli che hai ora."}}, + {{"Saved game", "Partie sauvegardée", "Spielstand", "Partida guardada", "Partita salvata"}}, + {{"Game data", "Données de jeu", "Spieldaten", "Datos del juego", "Dati di gioco"}}, + {{"Extra Data", "Données supplémentaires", "Zusatzdaten", "Datos extra", "Dati extra"}}, + {{"Saves backed up", "Sauvegardes copiées", "Spielstände gesichert", "Copia hecha", "Backup completato"}}, + {{"The backup couldn't be made", "La copie n'a pas pu être faite", "Das Backup konnte nicht erstellt werden", + "No se pudo hacer la copia", "Impossibile creare il backup"}}, + {{"Backup restored", "Copie restaurée", "Backup wiederhergestellt", "Copia restaurada", "Backup ripristinato"}}, + {{"Restoring didn't finish: your saves are in the newest backup", + "La restauration n'a pas abouti : vos sauvegardes sont dans la copie la plus récente", + "Wiederherstellen nicht fertig: Deine Spielstände sind im neuesten Backup", + "La restauración no terminó: tus partidas están en la copia más reciente", + "Ripristino non completato: i tuoi salvataggi sono nel backup più recente"}}, + {{"Nothing to import", "Rien à importer", "Nichts zu importieren", "Nada que importar", "Niente da importare"}}, + {{"Copy your Xbox 360 save files into /data/homebrew/PPSA94004/import/ on the PS5 first, then come back here.", + "Copiez d'abord vos fichiers de sauvegarde Xbox 360 dans /data/homebrew/PPSA94004/import/ sur la PS5, " + "puis revenez ici.", + "Kopiere zuerst deine Xbox-360-Spielstände nach /data/homebrew/PPSA94004/import/ auf der PS5 und komm dann " + "hierher zurück.", + "Primero copia tus archivos de guardado de Xbox 360 en /data/homebrew/PPSA94004/import/ en la PS5 y luego " + "vuelve aquí.", + "Prima copia i file di salvataggio Xbox 360 in /data/homebrew/PPSA94004/import/ sulla PS5, poi torna qui."}}, + {{"Xbox 360 saves", "Sauvegardes Xbox 360", "Xbox-360-Spielstände", "Partidas de Xbox 360", + "Salvataggi Xbox 360"}}, + {{"Restore this backup?", "Restaurer cette copie ?", "Dieses Backup wiederherstellen?", + "¿Restaurar esta copia?", "Ripristinare questo backup?"}}, + {{"Your saves are replaced by the ones from {}. The saves you have now are backed up first, so nothing is " + "lost.", + "Vos sauvegardes sont remplacées par celles de {}. Celles que vous avez maintenant sont copiées d'abord, " + "rien n'est perdu.", + "Deine Spielstände werden durch die aus {} ersetzt. Deine aktuellen werden vorher gesichert, es geht also " + "nichts verloren.", + "Tus partidas se sustituyen por las de {}. Antes se hace una copia de las que tienes ahora, así que no se " + "pierde nada.", + "I tuoi salvataggi vengono sostituiti da quelli di {}. Prima viene fatto un backup di quelli attuali, quindi " + "non si perde nulla."}}, + {{"{} save imported.", "{} sauvegarde importée.", "{} Spielstand importiert.", "{} partida importada.", + "{} salvataggio importato."}}, + {{"{} saves imported.", "{} sauvegardes importées.", "{} Spielstände importiert.", "{} partidas importadas.", + "{} salvataggi importati."}}, + {{"Load them from the game's menu.", "Chargez-les depuis le menu du jeu.", "Lade sie über das Menü des Spiels.", + "Cárgalas desde el menú del juego.", "Caricali dal menu del gioco."}}, + {{"No saves were imported.", "Aucune sauvegarde n'a été importée.", "Es wurden keine Spielstände importiert.", + "No se importó ninguna partida.", "Nessun salvataggio importato."}}, + {{"...the log lists the rest.", "...le journal indique le reste.", "...der Rest steht im Log.", + "...el resto aparece en el registro.", "...il resto è nel log."}}, + + // ---- Trophies + {{"Earned", "Obtenus", "Erhalten", "Obtenidos", "Ottenuti"}}, + {{"Earned|date", "Obtenu le", "Erhalten am", "Obtenido el", "Ottenuto il"}}, + {{"Not earned yet", "Pas encore obtenus", "Noch nicht erhalten", "Aún no obtenidos", "Non ancora ottenuti"}}, + {{"Not earned yet|status", "Pas encore obtenu", "Noch nicht erhalten", "Aún no obtenido", + "Non ancora ottenuto"}}, + {{"{} of {} earned", "{} sur {} obtenus", "{} von {} erhalten", "{} de {} obtenidos", "{} su {} ottenuti"}}, + {{"Start the game once", "Lancez le jeu une fois", "Starte das Spiel einmal", "Inicia el juego una vez", + "Avvia il gioco una volta"}}, + {{"The trophy list comes from the game itself: start it once, and the next time you open the hub your " + "trophies are here, with the ones you've earned.", + "La liste des trophées vient du jeu lui-même : lancez-le une fois, et la prochaine fois que vous " + "ouvrirez le hub, vos trophées seront ici, avec ceux que vous avez obtenus.", + "Die Trophäenliste kommt aus dem Spiel selbst: Starte es einmal, und wenn du den Hub das nächste Mal " + "öffnest, sind deine Trophäen hier, samt denen, die du erhalten hast.", + "La lista de trofeos sale del propio juego: inícialo una vez y, la próxima vez que abras el hub, tus " + "trofeos estarán aquí, con los que hayas obtenido.", + "L'elenco dei trofei viene dal gioco stesso: avvialo una volta e, la prossima volta che apri l'hub, i tuoi " + "trofei saranno qui, con quelli che hai ottenuto."}}, + {{"Grand Theft Auto IV Platinum", "Platine Grand Theft Auto IV", "Grand Theft Auto IV Platin", + "Platino de Grand Theft Auto IV", "Platino di Grand Theft Auto IV"}}, + {{"Hidden trophy", "Trophée caché", "Versteckte Trophäe", "Trofeo oculto", "Trofeo nascosto"}}, + {{"Earn every other GTA IV trophy ({}).", "Obtenez tous les autres trophées de GTA IV ({}).", + "Erhalte alle anderen GTA-IV-Trophäen ({}).", "Consigue todos los demás trofeos de GTA IV ({}).", + "Ottieni tutti gli altri trofei di GTA IV ({})."}}, + {{"Keep playing to find out what this one is.", "Continuez à jouer pour découvrir ce trophée.", + "Spiel weiter, um herauszufinden, was es ist.", "Sigue jugando para descubrir cuál es.", + "Continua a giocare per scoprire di cosa si tratta."}}, + {{"Bronze Trophy", "Trophée de bronze", "Bronze-Trophäe", "Trofeo de bronce", "Trofeo di bronzo"}}, + {{"Silver Trophy", "Trophée d'argent", "Silber-Trophäe", "Trofeo de plata", "Trofeo d'argento"}}, + {{"Gold Trophy", "Trophée d'or", "Gold-Trophäe", "Trofeo de oro", "Trofeo d'oro"}}, + {{"Platinum Trophy", "Trophée de platine", "Platin-Trophäe", "Trofeo de platino", "Trofeo di platino"}}, + {{"Status", "Statut", "Status", "Estado", "Stato"}}, + {{"{} to go", "Encore {}", "Noch {}", "Faltan {}", "Ne mancano {}"}}, + + // ---- About + {{"Created Liberty Recompiled, the GTA IV recompilation this is built on, with its contributors.", + "A créé Liberty Recompiled, la recompilation de GTA IV sur laquelle tout ceci repose, avec ses " + "contributeurs.", + "Hat Liberty Recompiled geschaffen, die GTA-IV-Rekompilierung, auf der das hier aufbaut, zusammen mit ihren " + "Mitwirkenden.", + "Creó Liberty Recompiled, la recompilación de GTA IV en la que se basa esto, junto a sus colaboradores.", + "Ha creato Liberty Recompiled, la ricompilazione di GTA IV su cui si basa tutto questo, insieme ai suoi " + "collaboratori."}}, + {{"Brought it to PlayStation 5: the PS5 port and this hub.", + "L'a amené sur PlayStation 5 : le portage PS5 et ce hub.", + "Hat es auf die PlayStation 5 gebracht: die PS5-Portierung und diesen Hub.", + "Lo trajo a PlayStation 5: la versión de PS5 y este hub.", + "L'ha portato su PlayStation 5: il port per PS5 e questo hub."}}, + {{"LibertyRecompAndroid: the fixes for running it on handheld hardware.", + "LibertyRecompAndroid : les correctifs pour le faire tourner sur des appareils portables.", + "LibertyRecompAndroid: die Fixes, damit es auf Handheld-Hardware läuft.", + "LibertyRecompAndroid: los arreglos para que funcione en dispositivos portátiles.", + "LibertyRecompAndroid: le correzioni per farlo girare su dispositivi portatili."}}, + {{"RecompTrophy: the native PS5 trophies and their pop-ups.", + "RecompTrophy : les trophées PS5 natifs et leurs notifications.", + "RecompTrophy: die nativen PS5-Trophäen und ihre Einblendungen.", + "RecompTrophy: los trofeos nativos de PS5 y sus avisos.", + "RecompTrophy: i trofei nativi di PS5 e le loro notifiche."}}, + {{"The static recompiler and runtime that turn the Xbox 360 game into a native program.", + "Le recompilateur statique et l'environnement d'exécution qui transforment le jeu Xbox 360 en programme " + "natif.", + "Der statische Recompiler und die Laufzeitumgebung, die das Xbox-360-Spiel in ein natives Programm " + "verwandeln.", + "El recompilador estático y el entorno de ejecución que convierten el juego de Xbox 360 en un programa " + "nativo.", + "Il ricompilatore statico e il runtime che trasformano il gioco Xbox 360 in un programma nativo."}}, + {{"Built With", "Réalisé avec", "Erstellt mit", "Hecho con", "Realizzato con"}}, + {{"stb_image, stb_vorbis, dr_mp3 and dr_flac", "stb_image, stb_vorbis, dr_mp3 et dr_flac", + "stb_image, stb_vorbis, dr_mp3 und dr_flac", "stb_image, stb_vorbis, dr_mp3 y dr_flac", + "stb_image, stb_vorbis, dr_mp3 e dr_flac"}}, + {{"Oswald and Inter (SIL Open Font License)", "Oswald et Inter (SIL Open Font License)", + "Oswald und Inter (SIL Open Font License)", "Oswald e Inter (SIL Open Font License)", + "Oswald e Inter (SIL Open Font License)"}}, + {{"Please Note", "À noter", "Bitte beachten", "Ten en cuenta", "Nota bene"}}, + {{"An unofficial fan project, not affiliated with or endorsed by Rockstar Games, Take-Two " + "Interactive, Microsoft or Sony. Grand Theft Auto and all related names belong to their owners. No game " + "files are included: you need your own copy of GTA IV for Xbox 360.", + "Un projet de fan non officiel, sans lien avec Rockstar Games, Take-Two Interactive, " + "Microsoft ou Sony, ni approuvé par eux. Grand Theft Auto et tous les noms associés appartiennent à leurs " + "propriétaires. Aucun fichier du jeu n'est inclus : il vous faut votre propre copie de GTA IV pour " + "Xbox 360.", + "Ein inoffizielles Fanprojekt, weder mit Rockstar Games, Take-Two Interactive, Microsoft " + "oder Sony verbunden noch von ihnen unterstützt. Grand Theft Auto und alle zugehörigen Namen gehören ihren " + "Inhabern. Es sind keine Spieldateien enthalten: Du brauchst deine eigene Kopie von GTA IV für die " + "Xbox 360.", + "Un proyecto de fans no oficial, sin relación con Rockstar Games, Take-Two Interactive, " + "Microsoft ni Sony, ni respaldado por ellos. Grand Theft Auto y todos los nombres relacionados pertenecen a " + "sus dueños. No incluye archivos del juego: necesitas tu propia copia de GTA IV para Xbox 360.", + "Un progetto amatoriale non ufficiale, non affiliato né approvato da Rockstar Games, " + "Take-Two Interactive, Microsoft o Sony. Grand Theft Auto e tutti i nomi correlati appartengono ai " + "rispettivi proprietari. Non è incluso alcun file del gioco: ti serve una tua copia di GTA IV per " + "Xbox 360."}}, + {{"Version {}", "Version {}", "Version {}", "Versión {}", "Versione {}"}}, + // (holding Cross on About for a few seconds: the Debug page and the + // Display page's rarer settings, shown or hidden) + {{"Debug options on", "Options de débogage activées", "Debug-Optionen an", "Opciones de depuración activadas", + "Opzioni di debug attive"}}, + {{"Debug options off", "Options de débogage désactivées", "Debug-Optionen aus", + "Opciones de depuración desactivadas", "Opzioni di debug disattivate"}}, + + // ---- the installer + {{"Your game files", "Vos fichiers de jeu", "Deine Spieldateien", "Tus archivos del juego", + "I tuoi file di gioco"}}, + {{"One time only: the game is copied off your Xbox 360 disc image.", + "Une seule fois : le jeu est copié depuis votre image disque Xbox 360.", + "Nur einmal: Das Spiel wird aus deinem Xbox-360-Disc-Image kopiert.", + "Solo una vez: el juego se copia desde tu imagen de disco de Xbox 360.", + "Una volta sola: il gioco viene copiato dalla tua immagine disco Xbox 360."}}, + {{"Disc Image", "Image disque", "Disc-Image", "Imagen de disco", "Immagine disco"}}, + {{"Game Folder", "Dossier du jeu", "Spielordner", "Carpeta del juego", "Cartella del gioco"}}, + {{"Checking", "Vérification", "Prüfung läuft", "Comprobando", "Verifica in corso"}}, + {{"Missing", "Absent", "Fehlt", "Falta", "Mancante"}}, + {{"Can't use", "Inutilisable", "Unbrauchbar", "No sirve", "Non utilizzabile"}}, + {{"Space", "Espace", "Speicherplatz", "Espacio", "Spazio"}}, + {{"Needs about {} free", "Il faut environ {} libres", "Braucht etwa {} frei", "Necesita unos {} libres", + "Servono circa {} liberi"}}, + {{"{} free", "{} libres", "{} frei", "{} libres", "{} liberi"}}, + {{"Not found", "Introuvable", "Nicht gefunden", "No encontrado", "Non trovato"}}, + {{"Making sure the disc image is the USA disc of GTA IV. This takes a few seconds.", + "Vérification que l'image disque est bien le disque américain de GTA IV. Cela prend quelques secondes.", + "Prüft, ob das Disc-Image die US-Disc von GTA IV ist. Das dauert ein paar Sekunden.", + "Comprobando que la imagen de disco es el disco estadounidense de GTA IV. Tarda unos segundos.", + "Verifica che l'immagine disco sia il disco USA di GTA IV. Ci vuole qualche secondo."}}, + {{"Ready", "Prêt", "Bereit", "Listo", "Pronto"}}, + {{"Press Cross to install. Keep the app open until it's done. Your disc image isn't changed.", + "Appuyez sur X pour installer. Laissez l'application ouverte jusqu'à la fin. Votre image disque n'est pas " + "modifiée.", + "Drücke X zum Installieren. Lass die App offen, bis es fertig ist. Dein Disc-Image wird nicht verändert.", + "Pulsa X para instalar. Deja la aplicación abierta hasta que termine. Tu imagen de disco no se modifica.", + "Premi X per installare. Tieni aperta l'app finché non ha finito. La tua immagine disco non viene " + "modificata."}}, + {{"Installs To", "Installé dans", "Installiert nach", "Se instala en", "Installato in"}}, + {{"On {}", "Sur {}", "Auf {}", "En {}", "In {}"}}, + {{"the app's drive", "le disque de l'application", "dem Laufwerk der App", "la unidad de la aplicación", + "unità dell'app"}}, + {{"internal storage", "le stockage interne", "dem internen Speicher", "el almacenamiento interno", + "memoria interna"}}, + {{"an exFAT drive", "un disque exFAT", "einem exFAT-Laufwerk", "una unidad exFAT", "unità exFAT"}}, + {{"a FAT drive", "un disque FAT", "einem FAT-Laufwerk", "una unidad FAT", "unità FAT"}}, + {{"a {} drive", "un disque {}", "einem {}-Laufwerk", "una unidad {}", "unità {}"}}, + {{"Can't install yet", "Pas encore installable", "Noch keine Installation möglich", + "Aún no se puede instalar", "Non è ancora possibile installare"}}, + {{"The Install Folder", "Le dossier d'installation", "Der Installationsordner", "La carpeta de instalación", + "La cartella di installazione"}}, + {{"The install stopped", "L'installation s'est arrêtée", "Die Installation wurde abgebrochen", + "La instalación se detuvo", "L'installazione si è interrotta"}}, + {{"What was copied has been removed, so nothing is left half done.", + "Ce qui avait été copié a été supprimé, rien n'est laissé à moitié.", + "Was kopiert wurde, ist wieder entfernt, es bleibt nichts halb fertig.", + "Lo que se copió se ha borrado, así que no queda nada a medias.", + "Ciò che era stato copiato è stato rimosso, quindi non resta nulla a metà."}}, + {{"What Happened", "Ce qui s'est passé", "Was passiert ist", "Qué ha pasado", "Cosa è successo"}}, + {{"Press Cross to try again. If it stops again, send the log from /data/homebrew/PPSA94004/logs.", + "Appuyez sur X pour réessayer. Si ça s'arrête encore, envoyez le journal de /data/homebrew/PPSA94004/logs.", + "Drücke X für einen neuen Versuch. Wenn es wieder abbricht, schick das Log aus " + "/data/homebrew/PPSA94004/logs.", + "Pulsa X para reintentarlo. Si vuelve a detenerse, envía el registro de /data/homebrew/PPSA94004/logs.", + "Premi X per riprovare. Se si ferma di nuovo, invia il log da /data/homebrew/PPSA94004/logs."}}, + {{"Installed", "Installé", "Installiert", "Instalado", "Installato"}}, + {{"GTA IV is set up on your PS5. Press Cross to play.", "GTA IV est installé sur votre PS5. Appuyez sur X pour " + "jouer.", "GTA IV ist auf deiner PS5 eingerichtet. Drücke X zum Spielen.", + "GTA IV está instalado en tu PS5. Pulsa X para jugar.", "GTA IV è installato sulla tua PS5. Premi X per " + "giocare."}}, + {{"Installing", "Installation en cours", "Wird installiert", "Instalando", "Installazione in corso"}}, + {{"Keep the app open until it's done. The PS5 won't go to sleep.", + "Laissez l'application ouverte jusqu'à la fin. La PS5 ne se mettra pas en veille.", + "Lass die App offen, bis es fertig ist. Die PS5 geht nicht in den Ruhemodus.", + "Deja la aplicación abierta hasta que termine. La PS5 no entrará en reposo.", + "Tieni aperta l'app finché non ha finito. La PS5 non andrà in standby."}}, + {{"All Done", "Terminé", "Fertig", "Terminado", "Completato"}}, + {{"Stopping", "Arrêt", "Wird gestoppt", "Deteniendo", "Interruzione"}}, + {{"Finishing", "Finalisation", "Abschluss", "Terminando", "Completamento"}}, + {{"Step 1 of 2", "Étape 1 sur 2", "Schritt 1 von 2", "Paso 1 de 2", "Passo 1 di 2"}}, + {{"Step 2 of 2", "Étape 2 sur 2", "Schritt 2 von 2", "Paso 2 de 2", "Passo 2 di 2"}}, + {{"The game's files are in place", "Les fichiers du jeu sont en place", "Die Spieldateien sind an Ort und Stelle", + "Los archivos del juego están en su sitio", "I file del gioco sono al loro posto"}}, + {{"Removing what was copied", "Suppression de ce qui a été copié", "Entferne, was kopiert wurde", + "Borrando lo que se copió", "Rimozione di ciò che è stato copiato"}}, + {{"Checking the game and putting it in place", "Vérification du jeu et mise en place", + "Prüfe das Spiel und lege es ab", "Comprobando el juego y colocándolo", "Verifica del gioco e sistemazione"}}, + {{"Unpacking the game's archives", "Décompression des archives du jeu", "Entpacke die Archive des Spiels", + "Descomprimiendo los archivos del juego", "Estrazione degli archivi del gioco"}}, + {{"Copying the game from your disc image", "Copie du jeu depuis votre image disque", + "Kopiere das Spiel aus deinem Disc-Image", "Copiando el juego desde tu imagen de disco", + "Copia del gioco dalla tua immagine disco"}}, + {{"{} written", "{} écrits", "{} geschrieben", "{} escritos", "{} scritti"}}, + {{"Took under a minute", "Moins d'une minute", "Unter einer Minute", "Tardó menos de un minuto", + "Meno di un minuto"}}, + {{"Took {} min", "Durée : {} min", "Dauer: {} Min.", "Tardó {} min", "Durata: {} min"}}, + {{"Less than a minute left", "Moins d'une minute restante", "Weniger als eine Minute übrig", + "Queda menos de un minuto", "Manca meno di un minuto"}}, + {{"About {} min left", "Encore {} min environ", "Noch etwa {} Min.", "Quedan unos {} min", + "Mancano circa {} min"}}, + {{"Working out the time left", "Calcul du temps restant", "Restzeit wird berechnet", + "Calculando el tiempo restante", "Calcolo del tempo rimanente"}}, + {{"Reading at {} \xC2\xB7 writing at {}", "Lecture à {} \xC2\xB7 écriture à {}", + "Lesen mit {} \xC2\xB7 Schreiben mit {}", "Leyendo a {} \xC2\xB7 escribiendo a {}", + "Lettura a {} \xC2\xB7 scrittura a {}"}}, + {{" to {}", " vers {}", " auf {}", " en {}", " in {}"}}, + {{"Install stopped. Nothing was kept.", "Installation arrêtée. Rien n'a été conservé.", + "Installation gestoppt. Nichts wurde behalten.", "Instalación detenida. No se ha guardado nada.", + "Installazione interrotta. Non è stato tenuto nulla."}}, + {{"Stop installing?", "Arrêter l'installation ?", "Installation stoppen?", "¿Detener la instalación?", + "Interrompere l'installazione?"}}, + {{"What's been copied so far is removed. You can start over later.", + "Ce qui a été copié jusqu'ici est supprimé. Vous pourrez recommencer plus tard.", + "Was bisher kopiert wurde, wird entfernt. Du kannst später neu anfangen.", + "Se borra lo copiado hasta ahora. Puedes empezar de nuevo más tarde.", + "Ciò che è stato copiato finora viene rimosso. Puoi ricominciare più tardi."}}, + {{"There's no disc image in the install folder. Copy your GTA IV disc image (.iso) into " + "/data/homebrew/PPSA94004/install/ and press Triangle.", + "Il n'y a pas d'image disque dans le dossier d'installation. Copiez votre image disque de GTA IV (.iso) dans " + "/data/homebrew/PPSA94004/install/ et appuyez sur Triangle.", + "Im Installationsordner ist kein Disc-Image. Kopiere dein GTA-IV-Disc-Image (.iso) nach " + "/data/homebrew/PPSA94004/install/ und drücke Dreieck.", + "No hay ninguna imagen de disco en la carpeta de instalación. Copia tu imagen de disco de GTA IV (.iso) en " + "/data/homebrew/PPSA94004/install/ y pulsa Triángulo.", + "Nella cartella di installazione non c'è nessuna immagine disco. Copia la tua immagine disco di GTA IV (.iso) " + "in /data/homebrew/PPSA94004/install/ e premi Triangolo."}}, + {{"This disc image can't be used: {}\nIt has to be the USA disc of GTA IV, the original release.", + "Cette image disque n'est pas utilisable : {}\nIl faut le disque américain de GTA IV, l'édition " + "d'origine.", + "Dieses Disc-Image ist nicht verwendbar: {}\nEs muss die US-Disc von GTA IV sein, die " + "Originalveröffentlichung.", + "Esta imagen de disco no sirve: {}\nTiene que ser el disco estadounidense de GTA IV, la edición original.", + "Questa immagine disco non si può usare: {}\nDeve essere il disco USA di GTA IV, l'edizione originale."}}, + {{"Title Update 8 is missing. Copy TU_1A581VI_000000K000000.0000000000205 into the install folder, next to the " + "disc image, and press Triangle.", + "Il manque le Title Update 8. Copiez TU_1A581VI_000000K000000.0000000000205 dans le dossier d'installation, " + "à côté de l'image disque, et appuyez sur Triangle.", + "Title Update 8 fehlt. Kopiere TU_1A581VI_000000K000000.0000000000205 in den Installationsordner neben das " + "Disc-Image und drücke Dreieck.", + "Falta el Title Update 8. Copia TU_1A581VI_000000K000000.0000000000205 en la carpeta de instalación, junto a " + "la imagen de disco, y pulsa Triángulo.", + "Manca il Title Update 8. Copia TU_1A581VI_000000K000000.0000000000205 nella cartella di installazione, " + "accanto all'immagine disco, e premi Triangolo."}}, + {{"There isn't enough free space. The install needs about {} and the drive has {} free.", + "Il n'y a pas assez d'espace libre. L'installation a besoin d'environ {} et le disque a {} de libre.", + "Nicht genug freier Speicher. Die Installation braucht etwa {}, das Laufwerk hat {} frei.", + "No hay suficiente espacio libre. La instalación necesita unos {} y la unidad tiene {} libres.", + "Spazio libero insufficiente. L'installazione richiede circa {} e l'unità ha {} liberi."}}, + // ---- the episodes + {{"Episodes", "Épisodes", "Episoden", "Episodios", "Episodi"}}, + {{"The Lost and Damned", "The Lost and Damned", "The Lost and Damned", "The Lost and Damned", "The Lost and Damned"}}, + {{"The Ballad of Gay Tony", "The Ballad of Gay Tony", "The Ballad of Gay Tony", "The Ballad of Gay Tony", + "The Ballad of Gay Tony"}}, + {{"Ready to install|episode", "Prêt à installer", "Bereit zur Installation", "Listo para instalar", + "Pronto da installare"}}, + {{"Not installed", "Non installé", "Nicht installiert", "No instalado", "Non installato"}}, + {{"{} is installed", "{} est installé", "{} ist installiert", "{} está instalado", "{} è installato"}}, + {{"Johnny Klebitz and The Lost MC. The first of the two episodes.", + "Johnny Klebitz et The Lost MC. Le premier des deux épisodes.", + "Johnny Klebitz und The Lost MC. Die erste der beiden Episoden.", + "Johnny Klebitz y The Lost MC. El primero de los dos episodios.", + "Johnny Klebitz e The Lost MC. Il primo dei due episodi."}}, + {{"Luis Lopez and nightclub owner Tony Prince. The second of the two episodes.", + "Luis Lopez et le patron de boîtes de nuit Tony Prince. Le second des deux épisodes.", + "Luis Lopez und Nachtclubbesitzer Tony Prince. Die zweite der beiden Episoden.", + "Luis Lopez y Tony Prince, dueño de discotecas. El segundo de los dos episodios.", + "Luis Lopez e Tony Prince, proprietario di locali notturni. Il secondo dei due episodi."}}, + {{"Installed. When the game starts it asks if you want to play it, the first time.", + "Installé. Au premier lancement, le jeu vous propose d'y jouer.", + "Installiert. Beim ersten Start fragt das Spiel, ob du es spielen willst.", + "Instalado. La primera vez que se inicie, el juego te preguntará si quieres jugarlo.", + "Installato. Al primo avvio il gioco ti chiede se vuoi giocarlo."}}, + {{"Install GTA IV first. The episodes are added to the installed game.", + "Installez d'abord GTA IV. Les épisodes s'ajoutent au jeu installé.", + "Installiere zuerst GTA IV. Die Episoden kommen zum installierten Spiel dazu.", + "Instala primero GTA IV. Los episodios se añaden al juego instalado.", + "Installa prima GTA IV. Gli episodi si aggiungono al gioco installato."}}, + {{"Copy the episode's package from your Xbox 360 (on its drive it's in " + "Content/0000000000000000/545407F2/00000002) into this folder on the PS5:", + "Copiez le paquet de l'épisode depuis votre Xbox 360 (sur son disque, il est dans " + "Content/0000000000000000/545407F2/00000002) dans ce dossier de la PS5 :", + "Kopiere das Paket der Episode von deiner Xbox 360 (auf ihrer Festplatte liegt es unter " + "Content/0000000000000000/545407F2/00000002) in diesen Ordner auf der PS5:", + "Copia el paquete del episodio desde tu Xbox 360 (en su disco está en " + "Content/0000000000000000/545407F2/00000002) a esta carpeta de la PS5:", + "Copia il pacchetto dell'episodio dalla tua Xbox 360 (sul suo disco si trova in " + "Content/0000000000000000/545407F2/00000002) in questa cartella della PS5:"}}, + {{"Its package is there. Press Cross to install it.", "Son paquet est là. Appuyez sur X pour l'installer.", + "Das Paket ist da. Drücke X, um es zu installieren.", "Su paquete ya está ahí. Pulsa X para instalarlo.", + "Il pacchetto c'è. Premi X per installarlo."}}, + {{"Then press Cross to install it.", "Puis appuyez sur X pour l'installer.", "Dann drücke X, um es zu installieren.", + "Después pulsa X para instalarlo.", "Poi premi X per installarlo."}}, + {{"There's no package for {} in the install folder. Copy it from your Xbox 360 (on its drive it's in " + "Content/0000000000000000/545407F2/00000002) into /data/homebrew/PPSA94004/install/ and press Triangle.", + "Il n'y a pas de paquet pour {} dans le dossier d'installation. Copiez-le depuis votre Xbox 360 (sur son " + "disque, il est dans Content/0000000000000000/545407F2/00000002) dans /data/homebrew/PPSA94004/install/ et " + "appuyez sur Triangle.", + "Im Installationsordner ist kein Paket für {}. Kopiere es von deiner Xbox 360 (auf ihrer Festplatte liegt es " + "unter Content/0000000000000000/545407F2/00000002) nach /data/homebrew/PPSA94004/install/ und drücke " + "Dreieck.", + "No hay ningún paquete de {} en la carpeta de instalación. Cópialo desde tu Xbox 360 (en su disco está en " + "Content/0000000000000000/545407F2/00000002) a /data/homebrew/PPSA94004/install/ y pulsa Triángulo.", + "Nella cartella di installazione non c'è nessun pacchetto di {}. Copialo dalla tua Xbox 360 (sul suo disco " + "si trova in Content/0000000000000000/545407F2/00000002) in /data/homebrew/PPSA94004/install/ e premi " + "Triangolo."}}, + {{"This package can't be used: {}", "Ce paquet n'est pas utilisable : {}", "Dieses Paket ist nicht verwendbar: {}", + "Este paquete no sirve: {}", "Questo pacchetto non si può usare: {}"}}, + {{"Episode Package", "Paquet de l'épisode", "Episodenpaket", "Paquete del episodio", "Pacchetto dell'episodio"}}, + {{"Xbox 360 package", "Paquet Xbox 360", "Xbox-360-Paket", "Paquete de Xbox 360", "Pacchetto Xbox 360"}}, + {{"Folder", "Dossier", "Ordner", "Carpeta", "Cartella"}}, + {{"One time only: the episode is copied off its Xbox 360 package.", + "Une seule fois : l'épisode est copié depuis son paquet Xbox 360.", + "Nur einmal: Die Episode wird aus ihrem Xbox-360-Paket kopiert.", + "Solo una vez: el episodio se copia desde su paquete de Xbox 360.", + "Una volta sola: l'episodio viene copiato dal suo pacchetto Xbox 360."}}, + {{"Making sure the package is the episode. This takes a few seconds.", + "Vérification que le paquet est bien l'épisode. Cela prend quelques secondes.", + "Prüft, ob das Paket die Episode ist. Das dauert ein paar Sekunden.", + "Comprobando que el paquete es el episodio. Tarda unos segundos.", + "Verifica che il pacchetto sia l'episodio. Ci vuole qualche secondo."}}, + {{"Press Cross to install. Keep the app open until it's done. The package isn't changed.", + "Appuyez sur X pour installer. Laissez l'application ouverte jusqu'à la fin. Le paquet n'est pas modifié.", + "Drücke X zum Installieren. Lass die App offen, bis es fertig ist. Das Paket wird nicht verändert.", + "Pulsa X para instalar. Deja la aplicación abierta hasta que termine. El paquete no se modifica.", + "Premi X per installare. Tieni aperta l'app finché non ha finito. Il pacchetto non viene modificato."}}, + {{"Installing {}", "Installation de {}", "{} wird installiert", "Instalando {}", "Installazione di {}"}}, + {{"{} is added to your game. When the game starts it asks if you want to play it.", + "{} est ajouté à votre jeu. Au lancement, le jeu vous propose d'y jouer.", + "{} ist jetzt in deinem Spiel. Beim Start fragt das Spiel, ob du es spielen willst.", + "{} se ha añadido a tu juego. Al iniciarse, el juego te preguntará si quieres jugarlo.", + "{} è stato aggiunto al tuo gioco. All'avvio il gioco ti chiede se vuoi giocarlo."}}, + {{"Copying", "Copie", "Kopieren", "Copiando", "Copia"}}, + {{"The episode's files are in place", "Les fichiers de l'épisode sont en place", + "Die Dateien der Episode sind an Ort und Stelle", "Los archivos del episodio están en su sitio", + "I file dell'episodio sono al loro posto"}}, + {{"Checking the episode and putting it in place", "Vérification de l'épisode et mise en place", + "Prüfe die Episode und lege sie ab", "Comprobando el episodio y colocándolo", + "Verifica dell'episodio e sistemazione"}}, + {{"Copying the episode from its package", "Copie de l'épisode depuis son paquet", + "Kopiere die Episode aus ihrem Paket", "Copiando el episodio desde su paquete", + "Copia dell'episodio dal suo pacchetto"}}, + // ---- City Population + {{"City Population", "Population de la ville", "Stadtbevölkerung", "Población de la ciudad", + "Popolazione della città"}}, + {{"Fewest", "Minimale", "Am wenigsten", "Mínima", "Minima"}}, + {{"Fewer", "Réduite", "Weniger", "Menos", "Ridotta"}}, + {{"More", "Plus dense", "Mehr", "Más", "Maggiore"}}, + {{"How many cars and people are out in the city. Original is the Xbox 360's. Fewer takes load off the game in " + "busy streets and helps it hold its frame rate; More fills the streets and costs frame rate.", + "Le nombre de voitures et de passants en ville. D'origine correspond à la Xbox 360. Réduite allège le jeu dans " + "les rues animées et l'aide à rester fluide\u00A0; Plus dense remplit les rues et coûte des images par seconde.", + "Wie viele Autos und Menschen in der Stadt unterwegs sind. Original entspricht der Xbox 360. Weniger entlastet " + "das Spiel in belebten Straßen und hilft, die Bildrate zu halten; Mehr füllt die Straßen und kostet Bildrate.", + "Cuántos coches y peatones hay en la ciudad. Original es la de Xbox 360. Menos alivia el juego en las calles " + "concurridas y le ayuda a mantener los fotogramas; Más llena las calles y cuesta fotogramas.", + "Quante auto e persone ci sono in città. Originale è quella di Xbox 360. Ridotta alleggerisce il gioco nelle " + "strade affollate e lo aiuta a tenere i fotogrammi; Maggiore riempie le strade e costa fotogrammi."}}, +}; +// clang-format on + +struct Found { + const Entry* entry; + const char* english; // what English shows (the key without its note) +}; + +const std::unordered_map& Index() { + static const std::unordered_map index = [] { + static std::vector heads; // (kept: T hands out pointers into them) + heads.reserve(16); + std::unordered_map m; + for (const Entry& e : kTable) { + const char* key = e.text[kEnglish]; + const char* shown = key; + if (const char* bar = std::strchr(key, '|')) { + heads.emplace_back(key, bar); + shown = heads.back().c_str(); + } + m.emplace(key, Found{&e, shown}); + } + return m; + }(); + return index; +} + +} // namespace + +Lang System() { + int v = 1; // (SCE_SYSTEM_PARAM_LANG_ENGLISH_US) +#if defined(__PROSPERO__) + if (sceSystemServiceParamGetInt(1 /* SCE_SYSTEM_SERVICE_PARAM_ID_LANG */, &v) != 0) v = 1; +#else + if (const char* e = std::getenv("HUB_SYSTEM_LANG")) v = std::atoi(e); // (the preview) +#endif + switch (v) { + case 2: // French + case 22: // French (Canada) + return kFrench; + case 3: // Spanish + case 20: // Spanish (Latin America) + return kSpanish; + case 4: return kGerman; + case 5: return kItalian; + default: return kEnglish; + } +} + +Lang FromSetting(const std::string& value) { + if (value == "english") return kEnglish; + if (value == "french") return kFrench; + if (value == "german") return kGerman; + if (value == "spanish") return kSpanish; + if (value == "italian") return kItalian; + return System(); +} + +uint32_t XboxLanguage(Lang l) { + switch (l) { + case kFrench: return 4; + case kGerman: return 3; + case kSpanish: return 5; + case kItalian: return 6; + default: return 1; + } +} + +const char* Name(Lang l) { + static const char* const names[kLangCount] = {"English", "Français", "Deutsch", "Español", "Italiano"}; + return names[l < kLangCount ? l : kEnglish]; +} + +void Set(Lang l) { g_lang.store(l < kLangCount ? l : kEnglish, std::memory_order_relaxed); } +Lang Current() { return Lang(g_lang.load(std::memory_order_relaxed)); } + +const char* T(const char* english) { + if (!english) return ""; + const Lang l = Current(); + const auto& index = Index(); + if (auto it = index.find(english); it != index.end()) { + const char* text = it->second.entry->text[l]; + return l != kEnglish && text && *text ? text : it->second.english; + } + return english; +} + +bool Has(const char* english) { return english && Index().count(english); } + +std::string F(const char* english, std::initializer_list args) { + std::string out = T(english); + size_t from = 0; + for (const std::string& a : args) { + const size_t at = out.find("{}", from); + if (at == std::string::npos) break; + out.replace(at, 2, a); + from = at + a.size(); + } + return out; +} + +std::string Units(std::string s) { + const Lang l = Current(); + if (l == kEnglish) return s; + for (char& c : s) + if (c == '.') c = ','; + if (l == kFrench) { + for (const auto& [from, to] : {std::pair{"GB", "Go"}, {"MB", "Mo"}, {"KB", "Ko"}}) { + for (size_t at = s.find(from); at != std::string::npos; at = s.find(from, at + 2)) s.replace(at, 2, to); + } + if (const size_t sp = s.find(' '); sp != std::string::npos) s.replace(sp, 1, " "); + } + return s; +} + +std::string Date(int64_t t) { + if (t <= 0) return "-"; + const std::time_t tt = std::time_t(t); + std::tm tm{}; + localtime_r(&tt, &tm); + char buf[48]; + const Lang l = Current(); + const char* format = l == kEnglish ? "%b %d, %Y %H:%M" : l == kGerman ? "%d.%m.%Y %H:%M" : "%d/%m/%Y %H:%M"; + std::strftime(buf, sizeof(buf), format, &tm); + return buf; +} + +std::string Clock(int64_t t) { + // clang-format off + static const char* const kDays[kLangCount][7] = { + {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}, + {"DIM", "LUN", "MAR", "MER", "JEU", "VEN", "SAM"}, + {"SO", "MO", "DI", "MI", "DO", "FR", "SA"}, + {"DOM", "LUN", "MAR", "MI\u00C9", "JUE", "VIE", "S\u00C1B"}, + {"DOM", "LUN", "MAR", "MER", "GIO", "VEN", "SAB"}}; + static const char* const kMonths[kLangCount][12] = { + {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}, + {"JANV", "F\u00C9VR", "MARS", "AVR", "MAI", "JUIN", "JUIL", "AO\u00DBT", "SEPT", "OCT", "NOV", "D\u00C9C"}, + {"JAN", "FEB", "M\u00C4R", "APR", "MAI", "JUN", "JUL", "AUG", "SEP", "OKT", "NOV", "DEZ"}, + {"ENE", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "OCT", "NOV", "DIC"}, + {"GEN", "FEB", "MAR", "APR", "MAG", "GIU", "LUG", "AGO", "SET", "OTT", "NOV", "DIC"}}; + // clang-format on + const std::time_t tt = std::time_t(t); + std::tm tm{}; + localtime_r(&tt, &tm); + const Lang l = Current(); + const int hour = tm.tm_hour % 12 ? tm.tm_hour % 12 : 12; + char buf[64]; + if (l == kEnglish) + std::snprintf(buf, sizeof(buf), "%s / %s %d / %d:%02d %s", kDays[l][tm.tm_wday % 7], kMonths[l][tm.tm_mon % 12], + tm.tm_mday, hour, tm.tm_min, tm.tm_hour < 12 ? "AM" : "PM"); + else // (the day before the month) + std::snprintf(buf, sizeof(buf), "%s / %d %s / %d:%02d %s", kDays[l][tm.tm_wday % 7], tm.tm_mday, + kMonths[l][tm.tm_mon % 12], hour, tm.tm_min, tm.tm_hour < 12 ? "AM" : "PM"); + return buf; +} + +} // namespace gta4ps5::hub::lang diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.h new file mode 100644 index 00000000..b187c5c0 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_lang.h @@ -0,0 +1,44 @@ +// GTA IV PS5 - languages: the hub's text in English, French, German, Spanish +// and Italian (the five the Xbox 360 disc has the game's text in), and the +// Language setting (ps5_language) that picks one for the hub and the game. +#pragma once + +#include +#include +#include + +namespace gta4ps5::hub::lang { + +enum Lang { kEnglish, kFrench, kGerman, kSpanish, kItalian, kLangCount }; + +// The PS5's own language, as one of the five (English when it's another). +Lang System(); +// The setting's value ("auto", "english", "french"...): auto is the PS5's. +Lang FromSetting(const std::string& value); +// What the game is told (the Xbox 360's XCONFIG user language): the game +// reads its text from the matching .gxt in common.rpf. +uint32_t XboxLanguage(Lang l); +// The language's name, in itself ("Français"). +const char* Name(Lang l); + +void Set(Lang l); +Lang Current(); + +// `english` in the hub's language (itself when there's no translation). +// A key may carry a note after '|' ("Earned|date") when one English word +// needs two translations; the note isn't shown. +const char* T(const char* english); +// Whether `english` has translations (tools/hub_preview checks the hub's text). +bool Has(const char* english); +// T, then each "{}" replaced by the next argument. +std::string F(const char* english, std::initializer_list args); +// A size or rate as the language writes it ("1.5 GB" -> "1,5 Go"). +std::string Units(std::string s); +// A date and time (unix time; "-" for none). +std::string Date(int64_t t); +// The footer's clock, as the game's pause menu has its day and time: the +// day of the week, the date and the time on a 12-hour clock ("WED / OCT 21 / +// 8:45 PM"), in capitals. +std::string Clock(int64_t t); + +} // namespace gta4ps5::hub::lang diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.cpp new file mode 100644 index 00000000..6ef1d980 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.cpp @@ -0,0 +1,100 @@ +// GTA IV PS5 - the trophy chime, made here: a struck bell or two (see +// hub_sfx.h). +#include "hub_sfx.h" + +#include +#include +#include + +namespace gta4ps5::hub { +namespace { + +constexpr float kRate = 48000; +constexpr float kPi = 3.14159265f; + +struct Buffer { + std::vector mono; + explicit Buffer(float seconds) : mono(size_t(seconds * kRate), 0.0f) {} + // A tone from `start` s: frequency gliding f0 -> f1, attack, then an exponential decay. + void Tone(float start, float length, float f0, float f1, float amp, float attack, float decay, float bright = 0.25f) { + const size_t a = size_t(start * kRate), n = size_t(length * kRate); + float phase = 0; + for (size_t i = 0; i < n && a + i < mono.size(); ++i) { + const float t = float(i) / kRate; + const float f = f0 + (f1 - f0) * std::min(1.0f, t / length); + phase += 2 * kPi * f / kRate; + const float env = std::min(1.0f, t / attack) * std::exp(-t / decay); + // (a touch of the 2nd and 3rd harmonics: rounder than a bare sine) + const float s = std::sin(phase) + bright * 0.5f * std::sin(2 * phase) + bright * 0.25f * std::sin(3 * phase); + mono[a + i] += amp * env * s; + } + } + // Noise through a band (two one-pole filters), shaped by rise / fall. + void Noise(float start, float length, float low_hz, float high_hz, float amp, float rise, float fall) { + uint32_t seed = 0x9E3779B9u; + float lp = 0, hp_in = 0, hp = 0; + const float a_lp = 1 - std::exp(-2 * kPi * high_hz / kRate), a_hp = std::exp(-2 * kPi * low_hz / kRate); + const size_t a = size_t(start * kRate), n = size_t(length * kRate); + for (size_t i = 0; i < n && a + i < mono.size(); ++i) { + seed = seed * 1664525u + 1013904223u; + const float w = float(int32_t(seed)) / 2147483648.0f; + lp += a_lp * (w - lp); + hp = a_hp * (hp + lp - hp_in); + hp_in = lp; + const float t = float(i) / kRate; + const float env = t < rise ? t / rise : std::exp(-(t - rise) / fall); + mono[a + i] += amp * env * hp; + } + } + // A little room: two short echoes, quiet. + void Room() { + const size_t d1 = size_t(0.031f * kRate), d2 = size_t(0.047f * kRate); + std::vector dry = mono; + for (size_t i = 0; i < mono.size(); ++i) { + float wet = 0; + if (i >= d1) wet += 0.16f * mono[i - d1]; + if (i >= d2) wet += 0.10f * mono[i - d2]; + mono[i] = dry[i] + wet; + } + } + std::vector Stereo() const { + // (the end faded over 5 ms: no click) + std::vector lr(mono.size() * 2); + const size_t tail = size_t(0.005f * kRate); + for (size_t i = 0; i < mono.size(); ++i) { + float s = mono[i] * 1.6f; // (made quiet above, brought to sit just under the music) + if (i + tail > mono.size()) s *= float(mono.size() - i) / float(tail); + lr[i * 2] = lr[i * 2 + 1] = s; + } + return lr; + } +}; + +} // namespace + +std::vector TrophyChime(bool platinum) { + // A bell: each note a struck tone with a quieter, quicker partial above it (2.76x: a bell's, not a string's). + auto bell = [](Buffer& b, float at, float f, float amp, float decay) { + b.Tone(at, decay * 5, f, f, amp, 0.002f, decay, 0.12f); + b.Tone(at, decay * 2.5f, f * 2.76f, f * 2.76f, amp * 0.28f, 0.001f, decay * 0.35f, 0.0f); + b.Tone(at, decay * 4, f * 0.5f, f * 0.5f, amp * 0.18f, 0.004f, decay * 0.8f, 0.0f); + }; + if (!platinum) { // two notes up a fourth, then a soft shimmer + Buffer b(1.6f); + bell(b, 0.00f, 1174.66f, 0.13f, 0.20f); // D6 + bell(b, 0.09f, 1567.98f, 0.15f, 0.32f); // G6 + b.Noise(0.09f, 0.7f, 5000, 12000, 0.025f, 0.12f, 0.22f); + b.Room(); + return b.Stereo(); + } + // the platinum: up a major chord, a low warm note under it + Buffer b(2.6f); + const float notes[] = {1046.50f, 1318.51f, 1567.98f, 2093.00f}; // C6 E6 G6 C7 + for (int k = 0; k < 4; ++k) bell(b, k * 0.085f, notes[k], 0.11f + 0.015f * k, 0.22f + 0.12f * k); + b.Tone(0.0f, 2.2f, 261.63f, 261.63f, 0.07f, 0.03f, 0.6f, 0.3f); // C4 + b.Noise(0.25f, 1.4f, 5000, 13000, 0.03f, 0.25f, 0.4f); + b.Room(); + return b.Stereo(); +} + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.h new file mode 100644 index 00000000..07d3d923 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_sfx.h @@ -0,0 +1,13 @@ +// GTA IV PS5 - the trophy chime (hub_sfx.cpp): made in code at 48 kHz, +// stereo interleaved. (The hub's menu sounds are recordings: hub/sfx, +// ps5_hub.cpp.) +#pragma once + +#include + +namespace gta4ps5::hub { + +// A trophy's chime (the popup in the game); `platinum`: a fuller one. +std::vector TrophyChime(bool platinum); + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_style.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_style.h new file mode 100644 index 00000000..cd4eed9e --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_style.h @@ -0,0 +1,144 @@ +// GTA IV PS5 - the PS5 hub's colours and small helpers +// (hub_ui.cpp). +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "hub_lang.h" +#include "imgui.h" + +namespace gta4ps5::hub { + +constexpr float kW = 1920, kH = 1080; +// The frame, after GTA IV's pause menu: the tabs across the top, a rule under +// them and one over the footer, the page between them on black. +constexpr float kTabsY = 156; // the tabs' capitals, centred on this +constexpr float kRuleTop = 206; // the rule under the tabs +constexpr float kRuleBottom = 873; // the rule over the footer +constexpr float kHintsY = 913; // the footer's first line of hints (the second: + 40) +constexpr float kPageL = 128, kPageR = 1792; // the page's left and right edges +// the page's list on the left (a row: label, then its value from kValueX), +// the details at the right +constexpr float kListX = kPageL, kListW = 900, kListY = 224, kListView = kRuleBottom - 26 - kListY; +constexpr float kValueX = 560; +constexpr float kPanelX = 1100, kPanelW = kPageR - kPanelX, kPanelY = 230; + +inline ImU32 Rgba(int r, int g, int b, int a = 255) { return IM_COL32(r, g, b, a); } +inline ImU32 White(float a) { return IM_COL32(255, 255, 255, int(std::clamp(a, 0.0f, 1.0f) * 255)); } +inline ImU32 Black(float a) { return IM_COL32(0, 0, 0, int(std::clamp(a, 0.0f, 1.0f) * 255)); } +inline ImU32 WithAlpha(ImU32 c, float a) { + const int base = int((c >> IM_COL32_A_SHIFT) & 0xFF); + return (c & ~IM_COL32_A_MASK) | (ImU32(std::clamp(int(base * a), 0, 255)) << IM_COL32_A_SHIFT); +} +inline float Smooth(float t) { + t = std::clamp(t, 0.0f, 1.0f); + return t * t * (3 - 2 * t); +} +constexpr ImU32 Mix(ImU32 a, ImU32 b, float t) { + t = t < 0 ? 0 : t > 1 ? 1 : t; + ImU32 out = 0; + for (int sh = 0; sh < 32; sh += 8) { + const float ca = float((a >> sh) & 0xFF), cb = float((b >> sh) & 0xFF); + out |= ImU32(ca + (cb - ca) * t + 0.5f) << sh; + } + return out; +} + +const ImU32 kInk = Rgba(12, 13, 16); // text on a focused (white) row +const ImU32 kAccent = Rgba(221, 155, 17); // the menu's gold (#DD9B11): the page, the row, what's on +const ImU32 kText = Rgba(236, 236, 236); +const ImU32 kMenuGray = Rgba(170, 170, 170); // a row, a tab not chosen +const ImU32 kDimGray = Rgba(112, 112, 112); // headings, what's not on +const ImU32 kRuleGray = Rgba(96, 96, 96); // the rules +const ImU32 kBarBack = Rgba(58, 58, 58); // a level's bar, behind +const ImU32 kBad = Rgba(232, 58, 46); // something wrong (the installer) + +// Each game's menu, as its own pause menu: the hub takes on the look of the +// game chosen on Home (0 GTA IV, 1 The Lost and Damned, 2 The Ballad of Gay +// Tony), easing from one to the next. GTA IV's is the gold and grey above; The +// Lost and Damned's a dark red over grey, the tabs scuffed; The Ballad of Gay +// Tony's a rainbow (each tab its colour, the rules a gradient), pink rows, sky +// blue hints, white for what's chosen. +struct Palette { + ImU32 tab_off, tab_on; // a tab, and the chosen one + ImU32 row, focus; // a row of a page, and the focused one + ImU32 title; // titles and paths + ImU32 hint; // the footer's labels + ImU32 toast; // the message at the footer's left + ImU32 rule_top, rule_bottom; // the rules (The Ballad of Gay Tony: its gradient over them) +}; +inline const Palette& GamePalette(int game) { + static const Palette kPalettes[3] = { + {Rgba(148, 148, 148), kAccent, kMenuGray, kAccent, kAccent, kMenuGray, kAccent, kRuleGray, kRuleGray}, + {Rgba(146, 146, 146), Rgba(104, 14, 14), Rgba(128, 128, 128), Rgba(156, 26, 26), Rgba(156, 26, 26), + Rgba(112, 112, 112), Rgba(156, 26, 26), Rgba(52, 52, 52), Rgba(40, 40, 40)}, + {Rgba(192, 18, 96), Rgba(244, 244, 244), Rgba(192, 18, 96), Rgba(244, 244, 244), Rgba(208, 28, 108), + Rgba(88, 116, 168), Rgba(206, 162, 46), Rgba(140, 70, 110), Rgba(90, 44, 70)}, + }; + return kPalettes[std::clamp(game, 0, 2)]; +} +// The Ballad of Gay Tony's colours, left to right (its tabs, from the game's +// Map to Game) and its rule under the tabs (the one over the footer is darker). +constexpr ImU32 kTbogtTabs[] = {IM_COL32(192, 150, 36, 255), IM_COL32(204, 120, 42, 255), IM_COL32(198, 72, 66, 255), + IM_COL32(192, 18, 96, 255), IM_COL32(150, 30, 120, 255), IM_COL32(120, 60, 132, 255), + IM_COL32(78, 102, 150, 255)}; +constexpr ImU32 kTbogtRule[] = {IM_COL32(202, 190, 58, 255), IM_COL32(213, 154, 54, 255), IM_COL32(208, 99, 71, 255), + IM_COL32(193, 34, 101, 255), IM_COL32(163, 33, 121, 255), IM_COL32(128, 69, 137, 255), + IM_COL32(94, 106, 157, 255), IM_COL32(72, 131, 165, 255)}; +// A colour along one of those, 0..1. +template +inline ImU32 Along(const ImU32 (&stops)[N], float t) { + const float f = std::clamp(t, 0.0f, 1.0f) * float(N - 1); + const size_t i = std::min(N - 2, size_t(f)); + return Mix(stops[i], stops[i + 1], f - float(i)); +} + +// Capitals, accented letters too (UTF-8: à..þ to À..Þ, œ to Œ, ß to SS). +inline std::string Upper(const std::string& s) { + std::string out; + out.reserve(s.size() + 2); + for (size_t i = 0; i < s.size(); ++i) { + const unsigned char c = static_cast(s[i]); + const unsigned char n = i + 1 < s.size() ? static_cast(s[i + 1]) : 0; + if (c == 0xC3 && n >= 0xA0 && n <= 0xBE && n != 0xB7) { + out += char(0xC3), out += char(n - 0x20), ++i; + } else if (c == 0xC3 && n == 0x9F) { + out += "SS", ++i; + } else if (c == 0xC5 && n == 0x93) { + out += char(0xC5), out += char(0x92), ++i; + } else if (c >= 0xC0) { // (another letter: as it is) + out += char(c); + while (i + 1 < s.size() && (static_cast(s[i + 1]) & 0xC0) == 0x80) out += s[++i]; + } else { + out += char(std::toupper(c)); + } + } + return out; +} + +inline std::string Size(uint64_t bytes) { + char buf[32]; + if (bytes >= (1ull << 30)) std::snprintf(buf, sizeof(buf), "%.1f GB", double(bytes) / double(1ull << 30)); + else if (bytes >= (1ull << 20)) std::snprintf(buf, sizeof(buf), "%.1f MB", double(bytes) / double(1ull << 20)); + else std::snprintf(buf, sizeof(buf), "%.0f KB", double(bytes) / 1024.0); + return lang::Units(buf); +} + +// A folder's files: total size and count. +inline uint64_t FolderBytes(const std::filesystem::path& dir, uint32_t* files = nullptr) { + uint64_t total = 0; + uint32_t n = 0; + std::error_code ec; + for (std::filesystem::recursive_directory_iterator it(dir, ec), end; !ec && it != end; it.increment(ec)) { + if (it->is_regular_file(ec)) total += it->file_size(ec), ++n; + } + if (files) *files = n; + return total; +} + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.cpp new file mode 100644 index 00000000..b002483c --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.cpp @@ -0,0 +1,344 @@ +// GTA IV PS5 - trophies (see hub_trophies.h). +#include "hub_trophies.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gta4ps5::trophies { + +namespace fs = std::filesystem; + +namespace { + +constexpr char kCatalogName[] = "545407F2-trophies.txt"; +constexpr char kUnlocksName[] = "545407F2.toml"; // (the runtime's) + +std::string Clean(std::string s) { + for (char& c : s) + if (c == '\t' || c == '\n' || c == '\r') c = ' '; + return s; +} + +std::vector Split(const std::string& line, char sep) { + std::vector out; + size_t a = 0; + for (;;) { + const size_t b = line.find(sep, a); + out.push_back(line.substr(a, b == std::string::npos ? std::string::npos : b - a)); + if (b == std::string::npos) return out; + a = b + 1; + } +} + +std::string ReadFile(const fs::path& p) { + std::ifstream in(p, std::ios::binary); + return std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); +} + +// The runtime's unlocks: [unlocked.] filetime = . +std::map Unlocks(const fs::path& file) { + std::map out; + std::ifstream in(file); + int64_t id = -1; + for (std::string line; std::getline(in, line);) { + const size_t a = line.find_first_not_of(" \t"); + if (a == std::string::npos || line[a] == '#') continue; + if (line.compare(a, 10, "[unlocked.") == 0) { + id = std::strtoll(line.c_str() + a + 10, nullptr, 10); + if (id > 0) out[uint32_t(id)] = 1; // (earned; the time below) + continue; + } + if (line[a] == '[') { + id = -1; + continue; + } + if (id > 0 && line.compare(a, 8, "filetime") == 0) { + const size_t eq = line.find('='); + if (eq != std::string::npos) { + const int64_t t = UnixTime(std::strtoull(line.c_str() + eq + 1, nullptr, 10)); + if (t > 0) out[uint32_t(id)] = t; + } + } + } + return out; +} + +} // namespace + +Grade GradeFor(uint32_t gamerscore) { + if (gamerscore <= 15) return Grade::kBronze; + if (gamerscore <= 30) return Grade::kSilver; + return Grade::kGold; +} + +const char* GradeName(Grade grade) { + switch (grade) { + case Grade::kBronze: return "Bronze"; + case Grade::kSilver: return "Silver"; + case Grade::kGold: return "Gold"; + case Grade::kPlatinum: return "Platinum"; + } + return ""; +} + +bool Here(uint32_t) { return true; } // (every achievement in the XEX is GTA IV's) + +int64_t UnixTime(uint64_t filetime) { + constexpr uint64_t kEpoch = 116444736000000000ull; // 1970 in Windows file time + return filetime > kEpoch ? int64_t((filetime - kEpoch) / 10000000ull) : 0; +} + +int List::Percent() const { + if (!points) return 0; + if (Platinum()) return 100; + const int p = int(std::lround(100.0 * points_earned / points)); + return std::clamp(p, points_earned ? 1 : 0, 99); +} + +fs::path Folder(const fs::path& user_data) { return user_data / "achievements"; } + +fs::path IconFile(const fs::path& user_data, uint32_t image_id) { + return Folder(user_data) / "545407F2-icons" / (std::to_string(image_id) + ".png"); +} + +fs::path TitleIconFile(const fs::path& user_data) { return Folder(user_data) / "545407F2-icons" / "title.png"; } + +List Load(const fs::path& user_data) { + List list; + std::ifstream in(Folder(user_data) / kCatalogName); + for (std::string line; std::getline(in, line);) { + if (!line.empty() && line.back() == '\r') line.pop_back(); + if (line.empty() || line[0] == '#') continue; + const auto f = Split(line, '\t'); + if (f.size() < 7) continue; + Trophy t; + t.id = uint32_t(std::strtoul(f[0].c_str(), nullptr, 10)); + t.gamerscore = uint32_t(std::strtoul(f[1].c_str(), nullptr, 10)); + t.flags = uint32_t(std::strtoul(f[2].c_str(), nullptr, 10)); + t.image_id = uint32_t(std::strtoul(f[3].c_str(), nullptr, 10)); + t.name = f[4], t.description = f[5], t.locked_description = f[6]; + if (Here(t.id)) list.trophies.push_back(std::move(t)); + } + list.loaded = !list.trophies.empty(); + const auto unlocks = Unlocks(Folder(user_data) / kUnlocksName); + for (Trophy& t : list.trophies) { + if (auto it = unlocks.find(t.id); it != unlocks.end()) t.earned = it->second; + const int g = int(t.grade()); + ++list.earnable, ++list.grade_total[g]; + list.points += t.gamerscore; + if (t.earned) { + ++list.earned, ++list.grade_earned[g]; + list.points_earned += t.gamerscore; + list.last_earned = std::max(list.last_earned, t.earned); + } + } + return list; +} + +bool WriteCatalog(const fs::path& user_data, const std::vector& trophies) { + std::string text = + "# GTA IV's achievements (the game writes this when it starts; the hub's Trophies page reads it)\n" + "# id, gamerscore, flags, image, name, description, description while locked (a tab between each)\n"; + for (const Trophy& t : trophies) + text += std::to_string(t.id) + "\t" + std::to_string(t.gamerscore) + "\t" + std::to_string(t.flags) + "\t" + + std::to_string(t.image_id) + "\t" + Clean(t.name) + "\t" + Clean(t.description) + "\t" + + Clean(t.locked_description) + "\n"; + const fs::path file = Folder(user_data) / kCatalogName; + if (ReadFile(file) == text) return true; + std::error_code ec; + fs::create_directories(file.parent_path(), ec); + const fs::path tmp = file.string() + ".tmp"; + { + std::ofstream out(tmp, std::ios::binary | std::ios::trunc); + if (!out) return false; + out << text; + } + fs::rename(tmp, file, ec); + return !ec; +} + +// ----------------------------------------------------------------- the cup + +namespace { + +struct Shades { + ImU32 base, light, dark; +}; + +ImU32 Col(int r, int g, int b, float a) { return IM_COL32(r, g, b, int(std::clamp(a, 0.0f, 1.0f) * 255)); } + +ImU32 WithAlphaOf(ImU32 c, float k) { + const int a = int((c >> IM_COL32_A_SHIFT) & 0xFF); + return (c & ~IM_COL32_A_MASK) | (ImU32(std::clamp(int(a * k), 0, 255)) << IM_COL32_A_SHIFT); +} + +// An ellipse as a polygon (from angle a0 to a1). +std::vector Ellipse(const std::function& P, float cx, float cy, float rx, float ry, + float a0 = 0, float a1 = 6.2831853f) { + std::vector pts; + for (int i = 0; i <= 20; ++i) { + const float t = a0 + (a1 - a0) * float(i) / 20.0f; + pts.push_back(P(cx + rx * std::cos(t), cy + ry * std::sin(t))); + } + return pts; +} + +Shades ShadesOf(Grade g, float a) { + switch (g) { + case Grade::kBronze: return {Col(206, 128, 78, a), Col(244, 184, 136, a), Col(146, 82, 44, a)}; + case Grade::kSilver: return {Col(186, 192, 204, a), Col(246, 248, 252, a), Col(124, 130, 144, a)}; + case Grade::kGold: return {Col(234, 182, 60, a), Col(255, 232, 146, a), Col(172, 122, 24, a)}; + case Grade::kPlatinum: return {Col(168, 198, 234, a), Col(240, 247, 255, a), Col(104, 134, 176, a)}; + } + return {}; +} + +} // namespace + +ImU32 GradeColor(Grade grade, float alpha) { return ShadesOf(grade, alpha).base; } + +namespace { + +ImU32 MixCol(ImU32 a, ImU32 b, float t) { + ImU32 out = 0; + for (int sh = 0; sh < 32; sh += 8) { + const float ca = float((a >> sh) & 0xFF), cb = float((b >> sh) & 0xFF); + out |= ImU32(ca + (cb - ca) * t + 0.5f) << sh; + } + return out; +} + +void CupShapes(ImDrawList* dl, ImVec2 c, float height, const Shades& s, bool shine) { + const float u = height / 24.0f; + const std::function P = [&](float x, float y) { return ImVec2(c.x + x * u, c.y + y * u); }; + // the handles (behind the bowl) + for (int side : {-1, 1}) { + dl->PathClear(); + const float cx = 6.6f * side, cy = -6.4f; + const float a0 = side > 0 ? -1.75f : 1.39f, a1 = side > 0 ? 1.75f : 4.89f; + dl->PathArcTo(P(cx, cy), 3.7f * u, a0, a1, 18); + dl->PathStroke(s.dark, 0, 1.9f * u); + } + // the bowl: a rim, then down to a rounded bottom + std::vector bowl; + for (int i = 0; i <= 24; ++i) { + const float t = 3.14159265f * float(i) / 24.0f; + const float k = std::max(0.0f, std::sin(t)); // (sin(pi) is a hair below 0: no NaN from the pow below) + // (straighter sides near the rim than a half ellipse) + const float x = 7.6f * std::cos(t) * (0.82f + 0.18f * k), y = -10.8f + 13.2f * std::pow(k, 1.25f); + bowl.push_back(P(x, y)); + } + dl->AddConvexPolyFilled(bowl.data(), int(bowl.size()), s.base); + dl->AddRectFilled(P(-8.4f, -12.2f), P(8.4f, -10.4f), s.light, 0.6f * u); + if (shine) { // a slim curve of light down the left of the bowl, and a little shade on the right + const auto shade = Ellipse(P, 3.3f, -4.8f, 2.2f, 4.8f, -1.5708f, 1.5708f); + dl->AddConvexPolyFilled(shade.data(), int(shade.size()), WithAlphaOf(s.dark, 0.45f)); + const auto glint = Ellipse(P, -4.0f, -5.6f, 1.1f, 3.4f); + dl->AddConvexPolyFilled(glint.data(), int(glint.size()), WithAlphaOf(s.light, 0.75f)); + } + // the stem, its collar and the base + const ImVec2 stem[] = {P(-1.25f, 2.0f), P(1.25f, 2.0f), P(1.9f, 6.4f), P(-1.9f, 6.4f)}; + dl->AddConvexPolyFilled(stem, 4, s.dark); + dl->AddRectFilled(P(-3.8f, 5.9f), P(3.8f, 7.5f), s.base, 0.7f * u); + dl->AddRectFilled(P(-6.4f, 7.8f), P(6.4f, 11.6f), s.base, 1.0f * u); + if (shine) dl->AddRectFilled(P(-6.4f, 10.2f), P(6.4f, 11.6f), WithAlphaOf(s.dark, 0.6f), 1.0f * u, ImDrawFlags_RoundCornersBottom); +} + +} // namespace + +void DrawCup(ImDrawList* dl, ImVec2 c, float height, Grade grade, float alpha, bool lit, bool on_light) { + Shades s = ShadesOf(grade, alpha); + if (!lit) { // not earned: its colours, greyed and quieter + const ImU32 grey = Col(96, 98, 106, alpha); + s = {MixCol(s.base, grey, 0.6f), MixCol(s.light, grey, 0.6f), MixCol(s.dark, grey, 0.6f)}; + } + if (on_light) { // on a light row: a soft dark edge first, so a silver one still reads + const ImU32 edge = Col(0, 0, 0, 0.22f * alpha); + CupShapes(dl, ImVec2(c.x, c.y + height * 0.01f), height * 1.12f, {edge, edge, edge}, false); + } + CupShapes(dl, c, height, s, lit); +} + +// --------------------------------------------------------------- the card + +namespace { + +ImU32 WhiteA(float a) { return IM_COL32(255, 255, 255, int(std::clamp(a, 0.0f, 1.0f) * 255)); } +ImU32 BlackA(float a) { return IM_COL32(0, 0, 0, int(std::clamp(a, 0.0f, 1.0f) * 255)); } + +// `t` cut to `width` (with "..."), in whole UTF-8 letters. +std::string FitText(ImFont* f, float size, std::string t, float width) { + auto w = [&](const std::string& s) { return f->CalcTextSizeA(size, FLT_MAX, 0, s.c_str()).x; }; + if (w(t) <= width) return t; + while (!t.empty() && w(t + "...") > width) { + t.pop_back(); + while (!t.empty() && (static_cast(t.back()) & 0xC0) == 0x80) t.pop_back(); + } + return t + "..."; +} + +// Letter-spaced text (the small capitals line). +void Spaced(ImDrawList* dl, ImFont* f, float size, ImVec2 p, ImU32 col, const char* t, float spacing) { + for (const char* c = t; *c; ++c) { + dl->AddText(f, size, p, col, c, c + 1); + p.x += f->CalcTextSizeA(size, FLT_MAX, 0, c, c + 1).x + spacing; + } +} + +} // namespace + +void DrawCard(ImDrawList* dl, ImVec2 display, const Card& card, float age, ImFont* body, ImFont* bold) { + constexpr float kIn = 0.45f, kOut = 0.45f; + const float show = card.Seconds(); + if (age < 0 || age >= show) return; + // in from the right, easing out; away to the right, fading + float alpha = 1, slide = 0; + const float k = display.y / 1080.0f; + if (age < kIn) { + const float t = age / kIn, e = 1 - (1 - t) * (1 - t) * (1 - t); + alpha = e, slide = (1 - e) * 70 * k; + } else if (age > show - kOut) { + const float t = std::clamp((show - age) / kOut, 0.0f, 1.0f); + alpha = t * t * (3 - 2 * t), slide = (1 - t) * 26 * k; + } + const float w = 548 * k, h = 106 * k, r = 18 * k; + const ImVec2 p0(display.x - 54 * k - w + slide, 50 * k), p1(p0.x + w, p0.y + h); + // a soft shadow, the card, a fine edge + for (int i = 1; i <= 6; ++i) { + const float g = float(i) * 3.0f * k; + dl->AddRectFilled(ImVec2(p0.x - g, p0.y - g + 5 * k), ImVec2(p1.x + g, p1.y + g + 5 * k), BlackA(0.05f * alpha), r + g); + } + dl->AddRectFilled(p0, p1, IM_COL32(30, 31, 36, int(242 * alpha)), r); + dl->AddRectFilledMultiColor(ImVec2(p0.x + r, p0.y + 1 * k), ImVec2(p1.x - r, p0.y + 2.2f * k), WhiteA(0.07f * alpha), + WhiteA(0.07f * alpha), WhiteA(0), WhiteA(0)); + dl->AddRect(p0, p1, WhiteA(0.09f * alpha), r, 0, 1.2f * k); + + // the picture + const float is = 78 * k; + const ImVec2 i0(p0.x + 14 * k, p0.y + (h - is) * 0.5f), i1(i0.x + is, i0.y + is); + if (card.picture) { + dl->AddImageRounded(card.picture, i0, i1, ImVec2(0, 0), ImVec2(1, 1), WhiteA(alpha), 13 * k); + } else { + dl->AddRectFilled(i0, i1, IM_COL32(46, 48, 56, int(255 * alpha)), 13 * k); + DrawCup(dl, ImVec2((i0.x + i1.x) * 0.5f, (i0.y + i1.y) * 0.5f), is * 0.56f, card.grade, alpha); + } + + // the words: what it is (with the grade's cup), its name, and its grade + const float tx = i1.x + 20 * k, tw = p1.x - 22 * k - tx; + DrawCup(dl, ImVec2(tx + 8 * k, p0.y + 27 * k), 20 * k, card.grade, alpha); + Spaced(dl, bold, 15 * k, ImVec2(tx + 24 * k, p0.y + 18 * k), WhiteA(0.62f * alpha), + card.platinum ? "PLATINUM TROPHY" : "TROPHY EARNED", 1.8f * k); + dl->AddText(bold, 25 * k, ImVec2(tx, p0.y + 40 * k), WhiteA(alpha), FitText(bold, 25 * k, card.name, tw).c_str()); + const std::string sub = card.platinum ? "Every GTA IV trophy" + : std::string(GradeName(card.grade)) + " \xC2\xB7 Grand Theft Auto IV"; + dl->AddText(body, 17 * k, ImVec2(tx, p0.y + 74 * k), GradeColor(card.grade, 0.9f * alpha), + FitText(body, 17 * k, sub, tw).c_str()); +} + +} // namespace gta4ps5::trophies diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.h new file mode 100644 index 00000000..26b22864 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies.h @@ -0,0 +1,85 @@ +// GTA IV PS5 - its Xbox 360 achievements as PS5-style trophies: the +// hub's Trophies page (hub_trophies_page.cpp) and the popup in the game +// (ps5/trophies_game.cpp). +// +// The achievements are GTA IV's (in its default.xex), and every one of them +// counts toward the platinum. The game unlocks them as on the Xbox 360 and +// the runtime keeps them in user_data/achievements/545407F2.toml. When the +// game starts, the port also writes their list and pictures there +// (WriteCatalog), so the hub, which opens before the game, can show them. +// +// Grades by points: up to 15 bronze, up to 30 silver, more gold. +// +// Plain C++ and ImGui (the hub's preview builds it on a PC). +#pragma once + +#include +#include +#include +#include + +#include "imgui.h" + +namespace gta4ps5::trophies { + +constexpr uint32_t kTitleId = 0x545407F2; +// (the achievement's flags: its description shows while it's locked; without it, it's a hidden one) +constexpr uint32_t kShowUnachieved = 0x8; + +enum class Grade { kBronze, kSilver, kGold, kPlatinum }; + +Grade GradeFor(uint32_t gamerscore); +const char* GradeName(Grade grade); // "Bronze"... +// The achievements that count (all of GTA IV's). +bool Here(uint32_t id); + +struct Trophy { + uint32_t id = 0, gamerscore = 0, flags = 0, image_id = 0; + std::string name, description, locked_description; + int64_t earned = 0; // unix time; 0: not earned + Grade grade() const { return GradeFor(gamerscore); } + bool hidden() const { return !(flags & kShowUnachieved); } +}; + +struct List { + bool loaded = false; // the list was there (the game has started once) + std::vector trophies; // the game's order + int earnable = 0, earned = 0; + int grade_total[3] = {}, grade_earned[3] = {}; + uint32_t points = 0, points_earned = 0; + int64_t last_earned = 0; + // The progress, 0-100 (by points, as the game weighs them). + int Percent() const; + bool Platinum() const { return earnable > 0 && earned == earnable; } +}; + +// UserData/achievements and its files. +std::filesystem::path Folder(const std::filesystem::path& user_data); +std::filesystem::path IconFile(const std::filesystem::path& user_data, uint32_t image_id); +std::filesystem::path TitleIconFile(const std::filesystem::path& user_data); + +// The list with what's been earned (the runtime's unlocks file). +List Load(const std::filesystem::path& user_data); +// The list's file, written when it changed (the game, as it starts). +bool WriteCatalog(const std::filesystem::path& user_data, const std::vector& trophies); + +int64_t UnixTime(uint64_t filetime); + +// The popup's card (top right), `age` seconds into its showing: in from the +// right, then away. `display`: the screen's size. +struct Card { + ImTextureID picture = 0; // the achievement's (the platinum: the game's); 0: a cup + std::string name; + Grade grade = Grade::kBronze; + bool platinum = false; + float Seconds() const { return platinum ? 7.0f : 5.5f; } +}; +void DrawCard(ImDrawList* dl, ImVec2 display, const Card& card, float age, ImFont* body, ImFont* bold); + +// A trophy cup, `height` tall, centred on `c`: in its grade's colours (not +// lit: greyed, for one not earned yet); `on_light`: on a light background. +void DrawCup(ImDrawList* dl, ImVec2 c, float height, Grade grade, float alpha = 1, bool lit = true, + bool on_light = false); +ImU32 GradeColor(Grade grade, float alpha = 1); + +} // namespace gta4ps5::trophies diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies_page.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies_page.cpp new file mode 100644 index 00000000..00324a8c --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_trophies_page.cpp @@ -0,0 +1,207 @@ +// GTA IV PS5 - the hub's Trophies page (see hub_ui.h, hub_trophies.h): +// how far along you are, then the trophies - earned (newest first), then not +// yet - with the focused one's details. +#include +#include +#include +#include +#include + +#include "hub_style.h" +#include "hub_ui.h" + +namespace gta4ps5::hub { + +namespace { + +constexpr float kRowH = 62; + +std::string When(int64_t t) { return lang::Date(t); } + +const char* kGroupNames[] = {"Earned", "Not earned yet"}; + +} // namespace + +void Hub::LoadTrophies() { + if (trophies_load_.valid()) return; // (one at a time) + trophies_load_ = + std::async(std::launch::async, [user_data = paths_.user_data] { return trophies::Load(user_data); }); +} + +void Hub::TakeTrophies(trophies::List list) { + trophies_ = std::move(list); + trophy_items_.clear(); + if (!trophies_.loaded) return; + const auto& all = trophies_.trophies; + // earned: the platinum first, then the newest + std::vector earned, rest; + for (int i = 0; i < int(all.size()); ++i) { + const auto& t = all[size_t(i)]; + if (t.earned) earned.push_back({i, 0}); + else rest.push_back({i, 1}); + } + std::stable_sort(earned.begin(), earned.end(), [&](const TrophyItem& a, const TrophyItem& b) { + return all[size_t(a.index)].earned > all[size_t(b.index)].earned; + }); + const bool plat = trophies_.Platinum(); + if (plat) trophy_items_.push_back({-1, 0}); + trophy_items_.insert(trophy_items_.end(), earned.begin(), earned.end()); + if (!plat) trophy_items_.push_back({-1, 1}); + trophy_items_.insert(trophy_items_.end(), rest.begin(), rest.end()); + item_[kTrophiesTab] = std::clamp(item_[kTrophiesTab], 0, int(trophy_items_.size()) - 1); +} + +// A trophy's picture (the platinum's: the game's own), loaded the first time it's drawn. +Image Hub::TrophyPicture(int index) { + const uint32_t key = index < 0 ? 0 : std::max(1u, trophies_.trophies[size_t(index)].image_id); + if (auto it = trophy_pictures_.find(key); it != trophy_pictures_.end()) return it->second; + // (two a frame at most: a page of them at once held the hub up when the + // page first came by; the rest show the frame after) + if (pictures_this_frame_ >= 2) return Image{}; + ++pictures_this_frame_; + Image img; + if (load_image_) + img = load_image_(index < 0 ? trophies::TitleIconFile(paths_.user_data) + : trophies::IconFile(paths_.user_data, trophies_.trophies[size_t(index)].image_id)); + trophy_pictures_[key] = img; + return img; +} + +// The trophy's picture in a rounded square: dimmed until it's earned, and a +// question mark for a hidden one. +void Hub::TrophyIcon(int index, float x, float y, float size, float over) { + const bool plat = index < 0; + const trophies::Trophy* t = plat ? nullptr : &trophies_.trophies[size_t(index)]; + const bool earned = plat ? trophies_.Platinum() : t->earned != 0; + const float r = size * 0.06f; // (square, as the menu's boxes) + if (t && !earned && t->hidden()) { + Rect(x, y, size, size, Mix(Rgba(38, 40, 46), Rgba(206, 208, 214), over), r); + TextMid(Bold(), size * 0.6f, x + size * 0.5f, y + size * 0.5f, Mix(White(0.5f), Black(0.45f), over), "?", 0.5f); + return; + } + const Image img = TrophyPicture(index); + if (img) { + const ImU32 tint = earned ? White(1) : Mix(White(0.34f), White(0.55f), over); + dl_->AddImageRounded(img.id, P(x, y), P(x + size, y + size), ImVec2(0, 0), ImVec2(1, 1), tint, r * s_); + } else { + Rect(x, y, size, size, Mix(Rgba(38, 40, 46), Rgba(206, 208, 214), over), r); + trophies::DrawCup(dl_, P(x + size * 0.5f, y + size * 0.5f), size * 0.56f * s_, + plat ? trophies::Grade::kPlatinum : t->grade(), 1, earned); + } + if (!earned) { // a padlock in the corner + const float lx = x + size - size * 0.2f, ly = y + size - size * 0.2f, k = size / 64.0f; + dl_->AddCircleFilled(P(lx, ly), 13 * k * s_, Black(0.72f), 24); + dl_->AddRectFilled(P(lx - 5.5f * k, ly - 1.5f * k), P(lx + 5.5f * k, ly + 6.5f * k), White(0.85f), 1.5f * k * s_); + dl_->PathClear(); + dl_->PathArcTo(P(lx, ly - 2 * k), 3.8f * k * s_, 3.14159f, 6.28318f, 12); + dl_->PathStroke(White(0.85f), 0, 1.8f * k * s_); + dl_->AddLine(P(lx - 3.8f * k, ly - 2 * k), P(lx - 3.8f * k, ly - 1.5f * k), White(0.85f), 1.8f * k * s_); + dl_->AddLine(P(lx + 3.8f * k, ly - 2 * k), P(lx + 3.8f * k, ly - 1.5f * k), White(0.85f), 1.8f * k * s_); + } +} + +// How far along (the details' top): the percentage, earned of all, and the +// bar under it in the menu's yellow. +float Hub::TrophySummary() { + const float x = kPanelX, w = kPanelW, y = kPanelY; + const bool plat = trophies_.Platinum(); + const std::string pct = std::to_string(trophies_.Percent()) + "%"; + TextMid(Bold(), 64, x, y + 30, kText, pct); + const float pw = TextWidth(Bold(), 64, pct); + TextMid(Regular(), 28, x + pw + 26, y + 38, kMenuGray, + lang::F("{} of {} earned", {std::to_string(trophies_.earned), std::to_string(trophies_.earnable)})); + // each grade's cup and its count, at the right + float rx = x + w; + for (int g = 0; g < 3; ++g) { // (bronze at the right, gold at the left) + const std::string n = std::to_string(trophies_.grade_earned[g]) + "/" + std::to_string(trophies_.grade_total[g]); + const float tw = TextWidth(Bold(), 26, n); + TextMid(Bold(), 26, rx - tw, y + 38, trophies_.grade_earned[g] ? kText : kDimGray, n); + trophies::DrawCup(dl_, P(rx - tw - 20, y + 38), 28 * s_, trophies::Grade(g), 1, trophies_.grade_earned[g] > 0); + rx -= tw + 60; + } + const float by = y + 82; + Rect(x, by, w, 10, kBarBack); + const float f = std::clamp(trophies_.points ? float(trophies_.points_earned) / float(trophies_.points) : 0.0f, 0.0f, 1.0f); + if (f > 0) Rect(x, by, std::max(4.0f, w * f), 10, plat ? trophies::GradeColor(trophies::Grade::kPlatinum) : pal_.title); + return by + 10; +} + +// A page with nothing to show yet: what to do. +void Hub::Empty(const char* title, const std::string& text) { + TextMid(Bold(), 40, kListX, kListY + 30, kText, title); + Wrapped(Regular(), 29, kListX, kListY + 76, 900, kMenuGray, text, 1.38f); +} + +void Hub::DrawTrophies() { + if (!trophies_.loaded) { + Empty(lang::T("Start the game once"), + lang::T("The trophy list comes from the game itself: start it once, and the next time you open the hub " + "your trophies are here, with the ones you've earned.")); + return; + } + const int n = int(trophy_items_.size()); + if (n == 0) return; + const int sel = item_[tab_] = std::clamp(item_[tab_], 0, n - 1); + std::vector rows; + int counts[2] = {}; + for (const TrophyItem& it : trophy_items_) ++counts[it.group]; + for (int i = 0; i < n; ++i) { + const int g = trophy_items_[size_t(i)].group; + if (i == 0 || trophy_items_[size_t(i) - 1].group != g) + rows.push_back({-1, std::string(lang::T(kGroupNames[g])) + " " + std::to_string(counts[g])}); + rows.push_back({i}); + } + auto name_of = [&](const TrophyItem& it) -> std::string { + if (it.index < 0) return lang::T("Grand Theft Auto IV Platinum"); + const auto& t = trophies_.trophies[size_t(it.index)]; + return (!t.earned && t.hidden()) ? lang::T("Hidden trophy") : t.name; + }; + auto text_of = [&](const TrophyItem& it) -> std::string { + if (it.index < 0) return lang::F("Earn every other GTA IV trophy ({}).", {std::to_string(trophies_.earnable)}); + const auto& t = trophies_.trophies[size_t(it.index)]; + if (!t.earned && t.hidden()) return lang::T("Keep playing to find out what this one is."); + return t.earned || t.locked_description.empty() ? t.description : t.locked_description; + }; + const float x = kListX, right = kPanelX - 90; + List(rows, sel, kRowH, [&](int i, float ry, float over) { + const TrophyItem& it = trophy_items_[size_t(i)]; + const bool plat = it.index < 0; + const bool earned = plat ? trophies_.Platinum() : trophies_.trophies[size_t(it.index)].earned != 0; + const float cy = ry + kRowH * 0.5f; + TrophyIcon(it.index, x, cy - 24, 48, 0); + const ImU32 col = Mix(earned ? pal_.row : kDimGray, pal_.focus, over); + TextMid(Bold(), 31, x + 70, cy, col, Fit(Bold(), 31, name_of(it), right - x - 70 - 50)); + const trophies::Grade g = plat ? trophies::Grade::kPlatinum : trophies_.trophies[size_t(it.index)].grade(); + trophies::DrawCup(dl_, P(right - 14, cy), 30 * s_, g, 1, earned); + }); + + // how far along, then the focused trophy + const float top = TrophySummary() + 40; + const TrophyItem& it = trophy_items_[size_t(sel)]; + const int v0 = dl_->VtxBuffer.Size; + { + const float px = kPanelX + (1 - Smooth(panel_in_)) * 12, pw = kPanelW; + float py = top; + const bool plat = it.index < 0; + const trophies::Trophy* t = plat ? nullptr : &trophies_.trophies[size_t(it.index)]; + const bool earned = plat ? trophies_.Platinum() : t->earned != 0; + const trophies::Grade g = plat ? trophies::Grade::kPlatinum : t->grade(); + TrophyIcon(it.index, px, py, 112, 0); + const float nx = px + 138, nw = pw - 138; + TextMid(Bold(), 21, nx, py + 14, trophies::GradeColor(g), + Upper(lang::T((std::string(trophies::GradeName(g)) + " Trophy").c_str())), 0, 1.6f); + const float nh = Wrapped(Bold(), 36, nx, py + 34, nw, kText, name_of(it), 1.1f); + py += std::max(112.0f, 34 + nh) + 24; + py += Wrapped(Regular(), 28, px, py, pw, Rgba(196, 196, 196), text_of(it), 1.38f) + 22; + std::vector> facts; + if (earned) facts.push_back({lang::T("Earned|date"), When(plat ? trophies_.last_earned : t->earned)}); + else if (plat) + facts.push_back({lang::T("Status"), lang::F("{} to go", {std::to_string(trophies_.earnable - trophies_.earned)})}); + else facts.push_back({lang::T("Status"), lang::T("Not earned yet|status")}); + if (t) facts.push_back({"Gamerscore", std::to_string(t->gamerscore) + "G"}); + Facts(px, py, pw, facts); + } + Fade(v0, Smooth(panel_in_)); +} + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.cpp new file mode 100644 index 00000000..9f5d64ec --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.cpp @@ -0,0 +1,2263 @@ +// GTA IV PS5 - the hub (see hub_ui.h): the frame, the Home, Settings, +// Saves and About pages, and the controls. +#include "hub_ui.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hub_lang.h" +#include "hub_style.h" + +namespace gta4ps5::hub { + +namespace fs = std::filesystem; +using lang::F; +using lang::T; + +static const char* const kTabNames[kTabCount] = {"Home", "Display", "Game", "Episodes", "Saves", "About", "Debug", "Trophies"}; + +namespace { + +std::string Unquote(std::string v) { + if (v.size() >= 2 && v.front() == '"' && v.back() == '"') v = v.substr(1, v.size() - 2); + return v; +} + +// A folder's entries (none when it can't be read). +std::vector ListDir(const fs::path& dir) { + std::vector out; + std::error_code ec; + for (fs::directory_iterator it(dir, ec), end; !ec && it != end; it.increment(ec)) out.push_back(*it); + return out; +} + +bool Hex(const std::string& s, size_t n) { + if (s.size() != n) return false; + for (char c : s) + if (!std::isxdigit(static_cast(c))) return false; + return true; +} + +int64_t Modified(const fs::path& p) { + struct stat st {}; + return stat(p.string().c_str(), &st) == 0 ? int64_t(st.st_mtime) : 0; +} + +// The newest change in a folder (its files'). +int64_t NewestIn(const fs::path& dir) { + int64_t t = Modified(dir); + std::error_code ec; + for (fs::recursive_directory_iterator it(dir, ec), end; !ec && it != end; it.increment(ec)) + if (it->is_regular_file(ec)) t = std::max(t, Modified(it->path())); + return t; +} + +std::string Date(int64_t t) { return lang::Date(t); } + +// The profile folders in UserData (16 hex digits: the game's saves are in them). +std::vector Profiles(const fs::path& user_data) { + std::vector out; + std::error_code ec; + for (const auto& e : ListDir(user_data)) + if (e.is_directory(ec) && Hex(e.path().filename().string(), 16)) out.push_back(e.path()); + return out; +} + +// Copies a folder's contents (recursively) into another. False on any error. +bool CopyInto(const fs::path& from, const fs::path& to) { + std::error_code ec; + fs::create_directories(to, ec); + if (ec) return false; + for (const auto& e : ListDir(from)) { + std::error_code ec2; + fs::copy(e.path(), to / e.path().filename(), fs::copy_options::recursive | fs::copy_options::overwrite_existing, + ec2); + if (ec2) return false; + } + return true; +} + +} // namespace + +// ----------------------------------------------------------------- settings + +struct Hub::Setting { + const char* section; + const char* key; + const char* label; + const char* short_label; // (Home's numbers; null: not shown there) + const char* help; + bool quoted; // a TOML string + std::vector values; // as written to the file + std::vector labels; + const char* fallback; // the value when the file has none +}; + +// What the PC launcher's Video, Language and Controls pages had that matters on +// the PS5. The PS5 always sends 4K to the TV. +static const std::vector& Settings() { + static const std::vector rows = { + // (not a setting of its own: what the ones it sets match, PresetValues) + {"DISPLAY", "hub_display_preset", "Preset", nullptr, + "Sets the Display settings below in one go, all with a 60 FPS limit. Recommended is the game as it " + "comes, in 4K. Performance draws at 1440p with fewer cars and people, for the steadiest 60. Quality " + "adds sharper and longer shadows, more detail in the distance, SMAA and the enhanced effects, and can " + "drop below 60 in busy streets. Changing any of them makes it Custom.", + false, {"recommended", "performance", "quality", "custom"}, {"Recommended", "Performance", "Quality", "Custom"}, + "recommended"}, + {"DISPLAY", "resolution", "Resolution", "RESOLUTION", + "How sharp the game is drawn. 720p is the Xbox 360's. The PS5's graphics card has room for 4K; " + "lower only helps if the picture stutters.", + true, {"720p", "1080p", "1440p", "4k"}, {"720p (Original)", "1080p", "1440p", "4K"}, "4k"}, + // (switched right away, and when the app starts: game/ps5_display.cpp) + {"DISPLAY", "ps5_refresh_rate", "Refresh Rate", nullptr, + "How many times a second the TV shows a new picture. 120 Hz needs a TV that takes it and 120 Hz output " + "on in the PS5's settings; without them the game stays at 60 Hz. It's what lets the 40 and 120 FPS " + "limits run evenly. It switches as soon as you choose it.", + false, {"60", "120"}, {"60 Hz", "120 Hz"}, "60"}, + {"DISPLAY", "gta4_draw_distance_scale", "Draw Distance", nullptr, + "How far out buildings and the city are drawn in detail. Original is the Xbox 360's. Shorter draws " + "less of the city and runs faster in busy streets, with more pop-in. Farther costs frame rate, and " + "past 1.5x the game can run short of memory.", + false, {"0.75", "1.0", "1.5", "2.0", "3.0"}, {"Shorter", "Original", "1.5x", "2x", "3x"}, "1.0"}, + // (the game's four population scales: gta4_ps5_app.cpp) + {"DISPLAY", "ps5_population", "City Population", nullptr, + "How many cars and people are out in the city. Original is the Xbox 360's. Fewer takes load off the game " + "in busy streets and helps it hold its frame rate; More fills the streets and costs frame rate.", + false, {"0.5", "0.75", "1.0", "1.25"}, {"Fewest", "Fewer", "Original", "More"}, "1.0"}, + {"DISPLAY", "gta4_force_highest_lod", "Highest Detail Models", nullptr, + "Shows buildings, cars and people at their most detailed as soon as that detail has loaded, near or " + "far, instead of simpler versions in the distance. Can cost frame rate in busy streets.", + false, {"false", "true"}, {"Off", "On"}, "false"}, + {"DISPLAY", "gta4_frame_limit", "Frame Limit", nullptr, + "The most frames a second the game draws. 30 is what the Xbox 360 aimed for and holds steady " + "everywhere; 60 runs at 50 to 60 in the busiest streets. 40 and 120 are for a 120 Hz Refresh Rate: " + "40 holds steady and is smoother than 30; 120 is only reached in quieter places.", + false, {"30", "40", "60", "120"}, {"30 FPS", "40 FPS", "60 FPS", "120 FPS"}, "60"}, + {"DISPLAY", "ps5_smooth_40", "Smooth Heavy Areas", nullptr, + "With a 120 Hz Refresh Rate and a 60 FPS limit: in the busiest places, where 60 can't hold, the game " + "switches to a steady 40 FPS instead of bouncing between 40 and 60, and goes back to 60 when it can.", + false, {"true", "false"}, {"On", "Off"}, "false"}, + // (game/ps5_overlay.cpp) + {"DISPLAY", "ps5_fps_counter", "FPS Counter", nullptr, + "Shows the frame rate at the left edge of the screen while you play. Simple is frames a second and the " + "frame time. Detailed adds the slowest frames, a graph of the last ones, the resolution, the TV's refresh " + "rate, the frame limit and how long the renderer takes.", + true, {"false", "simple", "true"}, {"Off", "Simple", "Detailed"}, "false"}, +// (Frame Generation: off and its row gone until it makes frames on the PS5: +// 1.0.3 Test 4 still made none, every frame short of motion vectors) +#if LIBERTY_HAS_FSR3 && LIBERTY_PS5_FRAME_GENERATION + {"DISPLAY", "ps5_frame_generation", "Frame Generation (Experimental)", nullptr, + "AMD FSR 3 adds a frame between each two the game draws. 30 to 60 holds the game at 30 FPS and shows " + "60, on any TV. 60 to 120 needs a 120 Hz TV and switches to it: the game runs at 60 and shows 120, " + "and drops to 30 to 60 by itself in places it can't hold 60. Frame Limit doesn't apply while it's on. " + "Adds a little input lag and turns on FSR 3 anti-aliasing. Experimental: tell us how it feels.", + true, {"off", "30-60", "60-120"}, {"Off", "30 to 60", "60 to 120"}, "off"}, +#endif + {"DISPLAY", "gta4_native_anti_aliasing", "Anti-Aliasing", nullptr, + "Smooths jagged edges. FXAA is the lightest; SMAA is sharper and costs a little more. MSAA 4x + SMAA " + "also cleans up thin edges in the distance, like stairs and wires, but is heavy on the graphics card: " + "new on the PS5, try 1440p with it if the game slows down.", + true, {"off", "fxaa", "smaa", "msaa4x_smaa"}, {"Off", "FXAA", "SMAA", "MSAA 4x + SMAA"}, "fxaa"}, + {"DISPLAY", "gta4_anisotropic_filtering", "Texture Filtering", nullptr, + "Keeps textures seen at an angle sharp, like roads and pavements going into the distance. Original is " + "the Xbox 360's; 16x is the sharpest and costs very little.", + true, {"1x", "4x", "8x", "16x"}, {"Original", "4x", "8x", "16x"}, "1x"}, + // (FusionFix's shaders, built in: LibertyRecompLib/shader_overrides) + {"DISPLAY", "gta4_modern_shaders", "Enhanced Effects", nullptr, + "The shaders from FusionFix, the PC mod: depth of field, a better sky, bloom and fog, and nicer water " + "and light beams. Off is the Xbox 360's look. New on the PS5: tell us if something looks wrong.", + false, {"false", "true"}, {"Off", "On"}, "false"}, + {"DISPLAY", "gta4_motion_blur", "Motion Blur", nullptr, "The blur when driving fast or turning quickly.", + false, {"false", "true"}, {"Off", "On"}, "true"}, + {"DISPLAY", "gta4_disable_tlad_film_grain", "Film Grain", nullptr, + "The grainy, film-like look of The Lost and Damned. Off gives it a clean picture; GTA IV and The " + "Ballad of Gay Tony have none.", + false, {"false", "true"}, {"On", "Off"}, "false"}, + {"DISPLAY", "gta4_reflection_resolution", "Reflections", nullptr, + "How sharp water, mirror and car reflections are. Original is the Xbox 360's; 1080p is cleaner.", + true, {"original", "1080p"}, {"Original", "1080p"}, "1080p"}, + // (the Xbox 360's is 256) + {"DISPLAY", "gta4_shadow_map_base_size", "Shadow Quality", nullptr, + "How sharp shadows are. High is twice the Xbox 360's detail; Very High doubles it again and asks more " + "of the graphics card.", + false, {"512", "1024"}, {"High", "Very High"}, "512"}, + {"DISPLAY", "gta4_shadow_distance_scale", "Shadow Distance", nullptr, + "How far out shadows are drawn. Original is the Xbox 360's; 2x reaches further but costs frame " + "rate in the city.", + false, {"1.0", "2.0"}, {"Original", "2x"}, "1.0"}, + {"DISPLAY", "gta4_native_shadow_min_indices", "Small Object Shadows", nullptr, + "Shadows of small things like signs, bins and debris, about a fifth of what is drawn in " + "busy streets. Off skips them for a higher frame rate.", + false, {"0", "300"}, {"On", "Off"}, "300"}, + {"DISPLAY", "ps5_environment_reflection_every", "Car Reflections", nullptr, + "How often the reflections on cars and glass are redrawn. Every other frame looks the same " + "in motion and leaves more time for the rest of the frame.", + false, {"1", "2"}, {"Every Frame", "Every Other Frame"}, "2"}, + // (Water Reflections every other frame is held back: the city froze + // loading with it on, 0.5b7. gta4_native_hooks.cpp ignores the setting.) + // (the hub's language too; the game is told it as the Xbox 360's language: + // gta4_ps5_app.cpp) + {"GAME", "ps5_language", "Language", nullptr, + "The language of the game's text (menus, subtitles and missions) and of this hub. Auto follows the PS5's " + "language. Voices stay in English, as on the Xbox 360.", + true, {"auto", "english", "french", "german", "spanish", "italian"}, + {"Auto", "English", "Fran\u00E7ais", "Deutsch", "Espa\u00F1ol", "Italiano"}, "auto"}, + {"GAME", "gta4_skip_intro", "Skip Intro", nullptr, + "Goes straight to the game's start screen, past the logos and the legal screens.", false, + {"false", "true"}, {"Off", "On"}, "false"}, + // (the car camera's sideways offset: gta4_camera_hooks.cpp) + {"GAME", "gta4_centered_vehicle_camera", "Centered Car Camera", nullptr, + "Puts the camera straight behind the car instead of a little off to the side, like FusionFix's option " + "on PC. Off is the Xbox 360's.", + false, {"false", "true"}, {"Off", "On"}, "false"}, + // (FusionFix's centering delay and turn speed: gta4_camera_hooks.cpp) + {"GAME", "gta4_vehicle_camera_return_delay", "Car Camera Return", nullptr, + "How long the car camera stays where you pointed it with the right stick before it swings back " + "behind the car. Original is the Xbox 360's: it starts swinging back right away.", + false, {"0", "1000", "2000", "3000"}, {"Original", "After 1 Second", "After 2 Seconds", "After 3 Seconds"}, + "0"}, + {"GAME", "gta4_vehicle_camera_turn_speed", "Car Camera Follow Speed", nullptr, + "How quickly the car camera swings round to follow the car through turns and back behind it. " + "Original is the Xbox 360's.", + false, {"0", "2", "4", "6"}, {"Original", "Faster", "Much Faster", "Fastest"}, "0"}, + // (trophies_native/native_trophies.cpp: RecompTrophy's set, relay and watcher) + {"GAME", "ps5_trophies", "PS5 Trophies", nullptr, + "Adds the game's trophies to the PS5's own Trophies list, with the ones you've already earned, and shows " + "the PS5's trophy pop-ups as you earn them. Needs a payload loader (etaHEN, elfldr...).", + false, {"false", "true"}, {"Off", "On"}, "true"}, + {"CONTROLS", "gta4_sony_enabled", "DualSense Features", nullptr, + "Turns the controller extras below on or off together: the light bar, adaptive triggers and extra rumble.", + false, {"false", "true"}, {"Off", "On"}, "true"}, + // (rex::input::sony, the PC port's, through the PS5 driver: ps5_input_driver.cpp) + {"CONTROLS", "gta4_sony_lighting", "Light Bar", nullptr, + "The controller's light: blue in GTA IV, and flashing red and blue like police lights while you're wanted.", + false, {"false", "true"}, {"Off", "On"}, "true"}, + {"CONTROLS", "gta4_sony_triggers", "Adaptive Triggers", nullptr, + "R2 pulls like a real gun's trigger and kicks with each shot, L2 pushes back while you aim, and both feel " + "like pedals when you drive.", + false, {"false", "true"}, {"Off", "On"}, "true"}, + {"CONTROLS", "gta4_sony_rumble", "Extra Rumble", nullptr, + "Adds rumble when you shoot, get hit or are near an explosion, on top of the game's own.", false, + {"false", "true"}, {"Off", "On"}, "true"}, + {"CONTROLS", "gta4_sony_intensity", "Feedback Strength", nullptr, + "How strong the adaptive triggers and the extra rumble feel.", false, {"0.25", "0.5", "0.75", "1.0"}, + {"Light", "Medium", "Strong", "Max"}, "0.5"}, + {"AUDIO", "ps5_audio_frames", "Audio Delay", nullptr, + "How far the sound runs behind the picture. Lower is closer to the action; if the sound crackles or cuts " + "out, pick the next one up. Wireless headsets add a little delay of their own.", + false, {"6", "12", "24", "64"}, {"Lowest (32 ms)", "Low (64 ms)", "Steady (128 ms)", "Original (341 ms)"}, + "12"}, + {"AUDIO", "hub_music_volume", "Menu Music", nullptr, + "How loud the music is here in the hub. It changes right away.", false, + {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"}, + {"Off", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"}, "50"}, + {"DEBUG", "ps5_save_logs", "Save Logs", nullptr, + "Writes what the game does to /app0/logs while it runs. Send these with a bug report. Takes effect " + "the next time the app starts.", + false, {"false", "true"}, {"Off", "On"}, "true"}, + {"DEBUG", "log_level", "Log Detail", nullptr, + "How much goes into the log. Normal is enough for most reports; Detailed and Everything help with " + "crashes and freezes but make big files and can slow the game.", + true, {"info", "debug", "trace"}, {"Normal", "Detailed", "Everything"}, "info"}, + {"DEBUG", "ps5_renderer_cpus", "Renderer Threads", nullptr, + "Own Cores gives the game's render thread and the two renderer threads a CPU core each and " + "moves everything else off them (about 10% faster than Anywhere). Compare switches between " + "two versions of Own Cores every 10 seconds and notes each in the log.", + true, {"cores", "any", "compare"}, {"Own Cores", "Anywhere", "Compare"}, "cores"}, + {"DEBUG", "ps5_lean_capture", "Game Thread Capture", nullptr, + "How the game's render thread hands each draw to the renderer. Own Core gives that work to a " + "thread on a core of its own, so the game's thread only copies what changed and moves on. " + "Lean does it on the game's thread, holding every buffer and texture once for a few frames. " + "Compare switches between Own Core and Lean every 10 seconds and notes each in the log.", + true, {"on", "core", "off", "compare"}, {"Lean", "Own Core", "Classic", "Compare"}, "on"}, + {"DEBUG", "ps5_frametime_capture", "Frame Time Capture", nullptr, + "Records every frame's time to a CSV file in /app0/logs, for checking stutter and slowdowns.", false, + {"false", "true"}, {"Off", "On"}, "false"}, + {"DEBUG", "ps5_hang_reports", "Freeze Reports", nullptr, + "When the game stops drawing for a few seconds, writes where every thread is to the log.", false, + {"false", "true"}, {"Off", "On"}, "true"}, + {"DEBUG", "gta4_native_gpu_detile", "GPU Texture Unpacking", nullptr, + "Unpacks the game's textures on the graphics card. Off does it on the CPU the old way: slower, " + "but a test for broken or missing textures.", + false, {"true", "false"}, {"On", "Off"}, "true"}, + {"DEBUG", "gta4_native_frame_timeline", "Frame Timeline", nullptr, + "Logs when each stage of every frame finished, to find what holds the frame rate back. The log " + "grows quickly with it on.", + false, {"false", "true"}, {"Off", "On"}, "false"}, + // (game/ps5_overlay.cpp: the overlay repainting every vblank, as up to 0.5b6) + {"DEBUG", "ps5_overlay_every_vblank", "Frame Delivery", nullptr, + "How the game's frames reach the TV. As Finished sends each frame as soon as it's ready. Every Vblank " + "redraws the screen at every refresh, as Beta 6 did, with two refreshes more input lag. For comparing " + "which feels smoother.", + false, {"false", "true"}, {"As Finished", "Every Vblank"}, "false"}, + {"DEBUG", "gta4_native_texture_watch", "Texture Watch", nullptr, + "Re-reads textures that change in memory without the game saying so. Off brings back the wrong map and " + "radar tiles. Only for comparing performance.", + false, {"true", "false"}, {"On", "Off"}, "true"}, + // (gta4_native_hooks.cpp: Ps5LimitFrameRate) + {"DEBUG", "ps5_cutscene_30", "Cutscenes at 30", nullptr, + "Holds the game at 30 FPS during cutscenes and loading screens, as on the Xbox 360. Off plays them at " + "the full frame rate.", + false, {"false", "true"}, {"Off", "On"}, "false"}, + // (gta4_native/graphics_system.cpp: lights that go dark near the camera) + {"DEBUG", "ps5_light_test", "Light Test", nullptr, + "For lights that go dark close to the camera, like in tunnels or over the pool table. No Stencil lets " + "every lamp light all it reaches, even through walls. No Clamp changes how a lamp's area is marked. " + "Tell us which one brings the light back. Applies at the next start.", + true, {"off", "no-stencil", "no-clamp"}, {"Off", "No Stencil", "No Clamp"}, "off"}, + }; + return rows; +} + +// --------------------------------------------------------------------- data + +void Hub::Init(const Paths& paths, const Assets& assets, std::string version) { + paths_ = paths; + a_ = assets; + version_ = std::move(version); + Load(); +} + +void Hub::Load() { + config_.clear(); + std::ifstream in(paths_.config); + for (std::string line; std::getline(in, line);) { + const size_t a = line.find_first_not_of(" \t"); + if (a == std::string::npos || line[a] == '#') continue; + if (line[a] == '[') break; // (top-level keys only) + const size_t eq = line.find('='); + if (eq == std::string::npos) continue; + std::string k = line.substr(a, eq - a), v = line.substr(eq + 1); + k.erase(k.find_last_not_of(" \t") + 1); + v.erase(0, v.find_first_not_of(" \t")); + if (const size_t hash = v.find(" #"); hash != std::string::npos && v[0] != '"') v.erase(hash); + v.erase(v.find_last_not_of(" \t\r") + 1); + config_.emplace_back(k, v); + } + lang::Set(lang::FromSetting(Value("ps5_language", "auto"))); + // [ps5 port] 1.1: the Debug page is closed at every start (About: hold + // Cross to open it for this session): its settings back to their defaults. + debug_options_ = false; + if (Value("hub_debug_options", "false") != "false") Save("hub_debug_options", "false"); + KeepHiddenSettings(); + LoadSaves(); + // (no Trophies tab since 1.1: not loaded) + // (the game's files come from Recomp Store: only whether they're there) + state_ = install::Look(paths_.app); +} + +Pixels Shrink(const unsigned char* rgba, int w, int h, int max_h) { + Pixels out; + if (h <= max_h) { + out.w = w, out.h = h; + out.rgba.assign(rgba, rgba + size_t(w) * size_t(h) * 4); + return out; + } + const double scale = double(h) / max_h; + out.h = max_h, out.w = std::max(1, int(w / scale + 0.5)); + out.rgba.resize(size_t(out.w) * size_t(out.h) * 4); + for (int y = 0; y < out.h; ++y) { + const int y0 = int(y * scale), y1 = std::min(h, std::max(y0 + 1, int((y + 1) * scale))); + for (int x = 0; x < out.w; ++x) { + const int x0 = int(x * scale), x1 = std::min(w, std::max(x0 + 1, int((x + 1) * scale))); + uint32_t sum[4] = {}; + for (int sy = y0; sy < y1; ++sy) + for (int sx = x0; sx < x1; ++sx) + for (int c = 0; c < 4; ++c) sum[c] += rgba[(size_t(sy) * w + sx) * 4 + c]; + const uint32_t n = uint32_t((y1 - y0) * (x1 - x0)); + for (int c = 0; c < 4; ++c) out.rgba[(size_t(y) * out.w + x) * 4 + c] = uint8_t(sum[c] / n); + } + } + return out; +} + +Pixels Grey(const Pixels& p) { + Pixels out = p; + for (size_t i = 0; i + 3 < out.rgba.size(); i += 4) { + const uint8_t v = uint8_t((out.rgba[i] * 54 + out.rgba[i + 1] * 183 + out.rgba[i + 2] * 19) >> 8); + out.rgba[i] = out.rgba[i + 1] = out.rgba[i + 2] = v; + } + return out; +} + +ImU32 Accent(const Pixels& p) { + // the hue most of its vivid pixels share (by 10 degrees, with their + // neighbours), its pixels' average, made bright and strong. Skin's hues + // count for less: the people would make every picture's colour the same. + constexpr int kBins = 36; + double weight[kBins] = {}, sum[kBins][3] = {}; + const size_t n = p.rgba.size() / 4, stride = std::max(1, n / 60000); + for (size_t k = 0; k < n; k += stride) { + if (p.rgba[k * 4 + 3] < 128) continue; // (a logo's clear parts) + const float r = p.rgba[k * 4] / 255.0f, g = p.rgba[k * 4 + 1] / 255.0f, b = p.rgba[k * 4 + 2] / 255.0f; + const float mx = std::max({r, g, b}), mn = std::min({r, g, b}); + const float s = mx > 0 ? (mx - mn) / mx : 0; + if (s < 0.2f || mx < 0.2f) continue; + float h = mx == r ? (g - b) / (mx - mn) : mx == g ? 2 + (b - r) / (mx - mn) : 4 + (r - g) / (mx - mn); + h = std::fmod(h * 60 + 360, 360.0f); + const int bin = std::min(kBins - 1, int(h / (360.0f / kBins))); + const bool skin = h < 50 || h > 350; + const double w = double(s) * s * s * mx * (skin ? 0.05 : 1.0); + weight[bin] += w, sum[bin][0] += r * w, sum[bin][1] += g * w, sum[bin][2] += b * w; + } + int best = -1; + double best_w = 0; + for (int i = 0; i < kBins; ++i) { + const double w = weight[(i + kBins - 1) % kBins] + weight[i] + weight[(i + 1) % kBins]; + if (w > best_w) best_w = w, best = i; + } + if (best < 0) return 0; + double c[3] = {}; + for (int d = -1; d <= 1; ++d) + for (int k = 0; k < 3; ++k) c[k] += sum[(best + d + kBins) % kBins][k]; + for (double& v : c) v /= best_w; + // (as HSV: the hue kept, strong and full bright) + const double mx = std::max({c[0], c[1], c[2]}), mn = std::min({c[0], c[1], c[2]}); + if (mx <= 0) return 0; + const double s = std::clamp((mx - mn) / mx * 1.3, 0.55, 0.9); + int out[3]; + for (int k = 0; k < 3; ++k) { + const double t = mx > mn ? (c[k] - mn) / (mx - mn) : 1; // 0 at the weakest channel, 1 at the strongest + out[k] = int(std::lround(255 * (1 - s * (1 - t)))); + } + return Rgba(out[0], out[1], out[2]); +} + +std::vector Hub::HomeGames() const { + // (as the Complete Edition's cover: The Lost and Damned, GTA IV, The Ballad of Gay Tony) + std::vector games; + if (state_.installed && state_.episode[0]) games.push_back(1); + games.push_back(0); + if (state_.installed && state_.episode[1]) games.push_back(2); + return games; +} + +int Hub::HomeGame() const { + const std::vector games = HomeGames(); + return std::find(games.begin(), games.end(), home_game_) != games.end() ? home_game_ : 0; +} + +Hub::SavesScan Hub::ScanSaves(const Paths& paths, const std::function& pending) { + SavesScan out; + std::error_code ec; + // UserData///<content type>/<name>: one save each + for (const fs::path& profile : Profiles(paths.user_data)) { + for (const auto& title : ListDir(profile)) { + if (!title.is_directory(ec) || !Hex(title.path().filename().string(), 8)) continue; + for (const auto& type : ListDir(title.path())) { + const std::string t = type.path().filename().string(); + if (!type.is_directory(ec) || !Hex(t, 8)) continue; + for (const auto& pkg : ListDir(type.path())) { + if (!pkg.is_directory(ec)) continue; + SaveItem s; + s.folder = pkg.path(); + s.name = pkg.path().filename().string(); + // (the game's folder names, for people: "0Saved Game" -> "Saved Game", "exdata" -> "Extra Data") + if (s.name.size() > 1 && std::isdigit(static_cast<unsigned char>(s.name[0])) && + std::isalpha(static_cast<unsigned char>(s.name[1]))) + s.name.erase(0, 1); + if (s.name == "exdata") s.name = "Extra Data"; + s.kind = t == "00000001" ? "Saved game" : "Game data"; + s.bytes = FolderBytes(pkg.path(), &s.files); + s.modified = NewestIn(pkg.path()); + out.saves.push_back(std::move(s)); + } + } + } + } + std::sort(out.saves.begin(), out.saves.end(), [](const SaveItem& a, const SaveItem& b) { return a.name < b.name; }); + for (const auto& e : ListDir(paths.app / "SaveBackups")) { + if (!e.is_directory(ec)) continue; + Backup b; + b.name = e.path().filename().string(); + b.bytes = FolderBytes(e.path(), &b.files); + out.backups.push_back(std::move(b)); + } + // newest first (their names start with the date) + std::sort(out.backups.begin(), out.backups.end(), [](const Backup& a, const Backup& b) { return a.name > b.name; }); + out.pending = pending ? pending() : 0; + return out; +} + +// (in the background: Draw takes the lists when they're made) +void Hub::LoadSaves() { + if (saves_scan_.valid()) { + saves_again_ = true; + return; + } + saves_scan_ = std::async(std::launch::async, + [paths = paths_, pending = save_import_.pending] { return ScanSaves(paths, pending); }); +} + +void Hub::BackUpSaves(const std::string& why) { + const std::time_t now = std::time(nullptr); + std::tm tm{}; + localtime_r(&now, &tm); + char stamp[32]; + std::strftime(stamp, sizeof(stamp), "%Y-%m-%d %H-%M-%S", &tm); + const fs::path root = paths_.app / "SaveBackups"; + fs::path dest = root / (std::string(stamp) + why); + std::error_code ec; + for (int k = 2; fs::exists(dest, ec); ++k) dest = root / (std::string(stamp) + why + " " + std::to_string(k)); + bool ok = true; + for (const fs::path& profile : Profiles(paths_.user_data)) ok = CopyInto(profile, dest / profile.filename()) && ok; + if (ok) { + if (why.empty()) Toast(T("Saves backed up")); + } else { + Toast(T("The backup couldn't be made")); + Play(Sound::kError); + } + LoadSaves(); +} + +void Hub::RestoreSaves(const Backup& b) { + BackUpSaves(" (before restore)"); + for (const fs::path& profile : Profiles(paths_.user_data)) { + std::error_code ec; + fs::remove_all(profile, ec); + } + if (CopyInto(paths_.app / "SaveBackups" / b.name, paths_.user_data)) { + Toast(T("Backup restored")); + Play(Sound::kOn); + } else { + Toast(T("Restoring didn't finish: your saves are in the newest backup")); + Play(Sound::kError); + } + LoadSaves(); + item_[kSavesTab] = 0; +} + +float Hub::MusicVolume() const { + const int pct = std::atoi(Value("hub_music_volume", "50").c_str()); + return std::clamp(pct, 0, 100) / 100.0f; +} + +std::string Hub::Value(const std::string& key, const std::string& fallback) const { + for (const auto& [k, v] : config_) + if (k == key) return Unquote(v); + return fallback; +} + +void Hub::Save(const std::string& key, const std::string& value) { + bool found = false; + for (auto& [k, v] : config_) + if (k == key) v = value, found = true; + if (!found) config_.emplace_back(key, value); + // The file: that key's line replaced (or added before the first [section]). + std::vector<std::string> lines; + { + std::ifstream in(paths_.config); + for (std::string line; std::getline(in, line);) lines.push_back(line); + } + const std::string entry = key + " = " + value; + size_t insert_at = lines.size(); + bool done = false; + for (size_t i = 0; i < lines.size(); ++i) { + const std::string& l = lines[i]; + const size_t first = l.find_first_not_of(" \t"); + if (first == std::string::npos) continue; + if (l[first] == '[') { + insert_at = std::min(insert_at, i); + break; + } + const size_t eq = l.find('='); + if (eq == std::string::npos || l[first] == '#') continue; + std::string k = l.substr(first, eq - first); + k.erase(k.find_last_not_of(" \t") + 1); + if (k == key) { + lines[i] = entry; + done = true; + break; + } + } + if (!done) { + // (before the comment lines over the first [section]) + while (insert_at > 0 && insert_at < lines.size() && + lines[insert_at - 1].find_first_not_of(" \t") != std::string::npos && + lines[insert_at - 1][lines[insert_at - 1].find_first_not_of(" \t")] == '#') + --insert_at; + lines.insert(lines.begin() + std::ptrdiff_t(insert_at), entry); + } + const fs::path tmp = paths_.config.string() + ".tmp"; + { + std::ofstream out(tmp, std::ios::trunc); + for (const std::string& l : lines) out << l << "\n"; + } + std::error_code ec; + fs::rename(tmp, paths_.config, ec); +} + +void Hub::NowPlaying(const std::string&) { + // (songs change quietly: no name on the screen) +} + +// The menu type, baked at the sizes the hub draws it at (on a 1080p screen +// and on a 4K one, where everything is twice as big): the tabs and titles, +// the rows, the headings and hints. The largest sizes hold only plain ASCII, +// and one only what's drawn that big (a percentage), to keep the atlas small; +// 100 and 120 have the accented letters too (the tabs and titles at 4K, in +// French, German, Spanish and Italian). +// The condensed letters get a little air between them (kBoldTrack and +// kRegularTrack, of the size): set as tight as the font comes, rows of it +// read cramped on a TV. +void AddMenuFonts(ImFontAtlas* atlas, const void* bold, int bold_bytes, const void* regular, int regular_bytes, + Assets& a) { + constexpr float kBoldTrack = 0.03f, kRegularTrack = 0.02f; + static const ImWchar kLatin[] = {0x0020, 0x00FF, 0x0152, 0x0153, 0x2010, 0x2027, 0}; + static const ImWchar kLetters[] = {0x0020, 0x007E, 0x00A0, 0x00FF, 0x0152, 0x0153, 0}; + static const ImWchar kAscii[] = {0x0020, 0x007E, 0}; + static const ImWchar kDigits[] = {'%', '%', '0', '9', 'H', 'H', 0}; + auto add = [atlas](const void* data, int bytes, float px, const ImWchar* ranges, float track) { + ImFontConfig cfg; + cfg.FontDataOwnedByAtlas = false; // (built into the app; shared by every size) + cfg.OversampleH = px < 40 ? 2 : 1; + cfg.OversampleV = 1; + cfg.GlyphRanges = ranges; + cfg.GlyphExtraAdvanceX = px * track; + return atlas->AddFontFromMemoryTTF(const_cast<void*>(data), bytes, px, &cfg); + }; + a.menu_sizes.clear(); + a.menu_regular_sizes.clear(); + for (float px : {24.0f, 32.0f, 42.0f, 52.0f, 62.0f, 72.0f, 84.0f}) + if (ImFont* f = add(bold, bold_bytes, px, kLatin, kBoldTrack)) a.menu_sizes.push_back(f); + for (float px : {100.0f, 120.0f}) + if (ImFont* f = add(bold, bold_bytes, px, kLetters, kBoldTrack)) a.menu_sizes.push_back(f); + if (ImFont* f = add(bold, bold_bytes, 144.0f, kAscii, kBoldTrack)) a.menu_sizes.push_back(f); + if (ImFont* f = add(bold, bold_bytes, 244.0f, kDigits, kBoldTrack)) a.menu_sizes.push_back(f); + for (float px : {24.0f, 30.0f, 36.0f, 42.0f, 48.0f, 56.0f, 64.0f}) + if (ImFont* f = add(regular, regular_bytes, px, kLatin, kRegularTrack)) a.menu_regular_sizes.push_back(f); + a.menu = a.menu_sizes.size() < 2 ? nullptr : a.menu_sizes[a.menu_sizes.size() - 2]; // (144: a full one) + a.menu_regular = a.menu_regular_sizes.empty() ? nullptr : a.menu_regular_sizes.back(); +} + +// Helvetica Neue Heavy for the tabs, at the sizes they're drawn at +// (66 or less on a 1080p screen, twice that at 4K). The handle is the largest. +void AddTabFonts(ImFontAtlas* atlas, const void* data, int bytes, Assets& a) { + static const ImWchar kLetters[] = {0x0020, 0x007E, 0x00A0, 0x00FF, 0x0152, 0x0153, 0}; + a.tab_sizes.clear(); + for (float px : {40.0f, 52.0f, 66.0f, 84.0f, 104.0f, 132.0f}) { + ImFontConfig cfg; + cfg.FontDataOwnedByAtlas = false; // (built into the app; shared by every size) + cfg.OversampleH = px < 60 ? 2 : 1; + cfg.OversampleV = 1; + cfg.GlyphRanges = kLetters; + if (ImFont* f = atlas->AddFontFromMemoryTTF(const_cast<void*>(data), bytes, px, &cfg)) a.tab_sizes.push_back(f); + } + a.tab = a.tab_sizes.empty() ? nullptr : a.tab_sizes.back(); +} + +// The Lost and Damned's scuffs (see hub_ui.h): soft blotches where the paint +// is thinner, specks, and short scratches, as its menu's type has. A fixed +// seed: the tabs look the same each time. +Pixels Grunge(int w, int h) { + Pixels p; + p.w = w, p.h = h; + std::vector<float> dark(size_t(w) * size_t(h), 0.0f); + uint32_t seed = 0x2C1B3C6Du; + auto rnd = [&] { + seed ^= seed << 13, seed ^= seed >> 17, seed ^= seed << 5; + return float(seed & 0xFFFFFF) / float(0x1000000); + }; + // blotches: value noise on two scales, only its peaks + auto lattice = [](int x, int y, uint32_t salt) { + uint32_t v = uint32_t(x) * 0x8DA6B343u ^ uint32_t(y) * 0xD8163841u ^ salt * 0xCB1AB31Fu; + v ^= v >> 13, v *= 0x5BD1E995u, v ^= v >> 15; + return float(v & 0xFFFF) / 65535.0f; + }; + auto noise = [&](float x, float y, float cell, uint32_t salt) { + const float fx = x / cell, fy = y / cell; + const int ix = int(std::floor(fx)), iy = int(std::floor(fy)); + const float tx = Smooth(fx - float(ix)), ty = Smooth(fy - float(iy)); + const float a = lattice(ix, iy, salt), b = lattice(ix + 1, iy, salt); + const float c = lattice(ix, iy + 1, salt), d = lattice(ix + 1, iy + 1, salt); + return (a + (b - a) * tx) * (1 - ty) + (c + (d - c) * tx) * ty; + }; + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) { + const float n = 0.6f * noise(float(x), float(y), 22, 1) + 0.4f * noise(float(x), float(y), 7, 2); + // (and a fine grit all over) + const float grit = noise(float(x), float(y), 2.2f, 3); + dark[size_t(y) * w + x] = 1 - (1 - std::clamp((n - 0.5f) / 0.3f, 0.0f, 1.0f) * 0.55f) * + (1 - std::clamp((grit - 0.55f) / 0.45f, 0.0f, 1.0f) * 0.35f); + } + auto blend = [&](int x, int y, float v) { + if (x < 0 || y < 0 || x >= w || y >= h) return; + float& d = dark[size_t(y) * w + x]; + d = 1 - (1 - d) * (1 - std::clamp(v, 0.0f, 1.0f)); + }; + // specks + const int specks = w * h / 110; + for (int i = 0; i < specks; ++i) { + const float cx = rnd() * float(w), cy = rnd() * float(h), r = 0.5f + rnd() * rnd() * 2.2f, v = 0.45f + 0.5f * rnd(); + for (int y = int(cy - r - 1); y <= int(cy + r + 1); ++y) + for (int x = int(cx - r - 1); x <= int(cx + r + 1); ++x) { + const float dist = std::hypot(float(x) + 0.5f - cx, float(y) + 0.5f - cy); + blend(x, y, v * std::clamp(r + 0.5f - dist, 0.0f, 1.0f)); + } + } + // scratches: short, thin and wandering + const int scratches = w / 6; + for (int i = 0; i < scratches; ++i) { + float x = rnd() * float(w), y = rnd() * float(h), angle = rnd() * 6.2832f; + const int steps = 6 + int(rnd() * 34); + const float v = 0.5f + 0.45f * rnd(); + for (int k = 0; k < steps; ++k) { + blend(int(x), int(y), v); + if (rnd() < 0.35f) blend(int(x) + 1, int(y), v * 0.5f); + angle += (rnd() - 0.5f) * 0.5f; + x += std::cos(angle), y += std::sin(angle); + } + } + p.rgba.resize(size_t(w) * size_t(h) * 4); + for (size_t i = 0; i < dark.size(); ++i) { + p.rgba[i * 4 + 0] = p.rgba[i * 4 + 1] = p.rgba[i * 4 + 2] = 255; + p.rgba[i * 4 + 3] = static_cast<unsigned char>(std::clamp(dark[i], 0.0f, 1.0f) * 255.0f + 0.5f); + } + return p; +} + +// ------------------------------------------------------------------ drawing + +float Hub::Ease(float& v, float target, float speed) { + v = v < -0.5f ? target : v + (target - v) * (1.0f - std::exp(-dt_ * speed)); + return v; +} + +void Hub::Fade(int first_vertex, float alpha) { + if (alpha >= 0.999f) return; + for (int i = first_vertex; i < dl_->VtxBuffer.Size; ++i) { + ImU32& c = dl_->VtxBuffer[i].col; + c = WithAlpha(c, alpha); + } +} + +ImFont* Hub::Pick(ImFont* f, float size, const std::string* t) const { + const std::vector<ImFont*>* sizes = f == a_.menu ? &a_.menu_sizes + : f == a_.menu_regular ? &a_.menu_regular_sizes + : f == a_.tab ? &a_.tab_sizes + : nullptr; + if (!sizes || sizes->empty()) return f; + const float want = size * s_ * 0.94f; // (a touch smaller is still sharp) + auto has_all = [&](ImFont* face) { + if (!t) return true; + ImFontBaked* baked = face->GetFontBaked(face->LegacySize); + if (!baked) return false; + for (size_t i = 0; i < t->size();) { // (UTF-8: up to three bytes a letter here) + const unsigned char c = static_cast<unsigned char>((*t)[i]); + unsigned int cp = c; + size_t n = 1; + if (c >= 0xE0 && i + 2 < t->size()) cp = ((c & 0x0Fu) << 12) | ((((*t)[i + 1]) & 0x3Fu) << 6) | (((*t)[i + 2]) & 0x3Fu), n = 3; + else if (c >= 0xC0 && i + 1 < t->size()) cp = ((c & 0x1Fu) << 6) | (((*t)[i + 1]) & 0x3Fu), n = 2; + i += n; + if (cp >= 0x20 && cp != '\n' && !baked->FindGlyphNoFallback(ImWchar(cp))) return false; + } + return true; + }; + for (ImFont* face : *sizes) + if (face->LegacySize >= want && has_all(face)) return face; + for (auto it = sizes->rbegin(); it != sizes->rend(); ++it) // (bigger than any: the largest with the letters) + if (has_all(*it)) return *it; + return f; +} + +void Hub::Text(ImFont* f, float size, float x, float y, ImU32 col, const std::string& t, float align, float spacing) { + if (!f) f = ImGui::GetFont(); + f = Pick(f, size, &t); + float px = x - TextWidth(f, size, t, spacing) * align; + if (spacing <= 0) { + dl_->AddText(f, size * s_, P(px, y), col, t.c_str()); + return; + } + // (letter by letter: a UTF-8 letter's bytes stay together) + for (size_t i = 0; i < t.size();) { + size_t n = 1; + const unsigned char c = static_cast<unsigned char>(t[i]); + if (c >= 0xF0) n = 4; + else if (c >= 0xE0) n = 3; + else if (c >= 0xC0) n = 2; + const char* a = t.c_str() + i; + const char* b = t.c_str() + std::min(t.size(), i + n); + dl_->AddText(f, size * s_, P(px, y), col, a, b); + px += f->CalcTextSizeA(size, FLT_MAX, 0, a, b).x + spacing; + i += n; + } +} + +float Hub::TextWidth(ImFont* f, float size, const std::string& t, float spacing) const { + if (!f) f = ImGui::GetFont(); + f = Pick(f, size, &t); + if (spacing <= 0) return f->CalcTextSizeA(size, FLT_MAX, 0, t.c_str()).x; + float w = 0; + for (size_t i = 0; i < t.size();) { + size_t n = 1; + const unsigned char c = static_cast<unsigned char>(t[i]); + if (c >= 0xF0) n = 4; + else if (c >= 0xE0) n = 3; + else if (c >= 0xC0) n = 2; + w += f->CalcTextSizeA(size, FLT_MAX, 0, t.c_str() + i, t.c_str() + std::min(t.size(), i + n)).x + spacing; + i += n; + } + return t.empty() ? 0 : w - spacing; +} + +std::string Hub::Fit(ImFont* f, float size, const std::string& t, float width) const { + if (TextWidth(f, size, t) <= width) return t; + std::string s = t; + while (!s.empty() && TextWidth(f, size, s + "...") > width) { + s.pop_back(); + while (!s.empty() && (static_cast<unsigned char>(s.back()) & 0xC0) == 0x80) s.pop_back(); // (whole UTF-8 letters) + } + return s + "..."; +} + +// Text broken into lines of at most `width`; returns the height used. +// Text wrapped to a width, a line per '\n' at least. +std::vector<std::string> Hub::WrapLines(ImFont* f, float size, float width, const std::string& t) { + std::vector<std::string> lines; + size_t start = 0; + for (;;) { + const size_t nl = t.find('\n', start); + std::istringstream words(t.substr(start, nl == std::string::npos ? std::string::npos : nl - start)); + std::string word, cur; + while (words >> word) { + const std::string next = cur.empty() ? word : cur + " " + word; + if (!cur.empty() && TextWidth(f, size, next) > width) { + lines.push_back(cur); + cur = word; + } else { + cur = next; + } + } + if (!cur.empty()) lines.push_back(Fit(f, size, cur, width)); + if (nl == std::string::npos) break; + start = nl + 1; + } + return lines; +} + +float Hub::Wrapped(ImFont* f, float size, float x, float y, float width, ImU32 col, const std::string& t, float line) { + float yy = y; + for (const std::string& l : WrapLines(f, size, width, t)) Text(f, size, x, yy, col, l), yy += size * line; + return yy - y; +} + +void Hub::Rect(float x, float y, float w, float h, ImU32 col, float r) { + dl_->AddRectFilled(P(x, y), P(x + w, y + h), col, r * s_); +} + +namespace { +const ImFontGlyph* CapGlyph(ImFont* f) { + ImFontBaked* baked = f->GetFontBaked(f->LegacySize); + return baked ? baked->FindGlyphNoFallback('H') : nullptr; +} +} // namespace + +float Hub::CapMid(ImFont* f, float size) const { + if (!f) f = ImGui::GetFont(); + f = Pick(f, size); + const ImFontGlyph* g = CapGlyph(f); + return g ? (g->Y0 + g->Y1) * 0.5f * size / f->LegacySize : size * 0.5f; +} + +float Hub::Baseline(ImFont* f, float size) const { + if (!f) f = ImGui::GetFont(); + f = Pick(f, size); + const ImFontGlyph* g = CapGlyph(f); + return g ? g->Y1 * size / f->LegacySize : size * 0.8f; +} + +void Hub::TextMid(ImFont* f, float size, float x, float cy, ImU32 col, const std::string& t, float align, + float spacing) { + Text(f, size, x, cy - CapMid(f, size), col, t, align, spacing); +} + +// The DualSense's buttons: the pictures in hub/buttons (each drawn centred in +// its square, ps5/tools/hub_buttons.py), else drawn in one colour. `size` is +// the button's longer side: a face button's width, the options button's height. +float Hub::GlyphWidth(uint32_t button, float size) const { + if (button == (kUp | kDown)) return size * 0.62f; // (drawn: the menu's arrows) + if (button == (kLeft | kRight)) return size * 1.05f; + if (auto it = a_.buttons.find(button); it != a_.buttons.end() && it->second) + return size * it->second.fill_w / std::max(it->second.fill_w, it->second.fill_h); + switch (button) { + case kL1: + case kR1: return size; + case kOptions: return size * 26 / 22; + default: return size; + } +} + +float Hub::Glyph(uint32_t button, float cx, float cy, ImU32 col, float size) { + // (the d-pad's directions are drawn, as the game's arrows: its picture is the whole pad) + const bool arrows = button == (kUp | kDown) || button == (kLeft | kRight); + if (auto it = a_.buttons.find(button); !arrows && it != a_.buttons.end() && it->second) { + const Image& img = it->second; + const float h = size * 0.5f / std::max(img.fill_w, img.fill_h); + dl_->AddImage(img.id, P(cx - h, cy - h), P(cx + h, cy + h), ImVec2(0, 0), ImVec2(1, 1), col); + return GlyphWidth(button, size); + } + const ImVec2 c = P(cx, cy); + // (drawn: a face button 30 across, L1 / R1 40, the options button 22 tall) + const float u = size / (button == kL1 || button == kR1 ? 40 : button == kOptions ? 22 : 30) * s_; + const float R = 15 * u, k = 6.2f * u, t = 2.2f * u; + switch (button) { + case kCross: + case kCircle: + case kSquare: + case kTriangle: + dl_->AddCircle(c, R, col, 40, 1.6f * u); + if (button == kCross) { + dl_->AddLine(ImVec2(c.x - k, c.y - k), ImVec2(c.x + k, c.y + k), col, t); + dl_->AddLine(ImVec2(c.x - k, c.y + k), ImVec2(c.x + k, c.y - k), col, t); + } else if (button == kCircle) { + dl_->AddCircle(c, k * 1.1f, col, 28, t); + } else if (button == kSquare) { + dl_->AddRect(ImVec2(c.x - k * 0.95f, c.y - k * 0.95f), ImVec2(c.x + k * 0.95f, c.y + k * 0.95f), col, 0, 0, t); + } else { + dl_->AddTriangle(ImVec2(c.x, c.y - k * 1.15f), ImVec2(c.x + k * 1.1f, c.y + k * 0.8f), + ImVec2(c.x - k * 1.1f, c.y + k * 0.8f), col, t); + } + break; + case kLeft | kRight: { + const float a = 8 * u, gap = 3.5f * u; + dl_->AddTriangleFilled(ImVec2(c.x - gap, c.y - a), ImVec2(c.x - gap, c.y + a), ImVec2(c.x - gap - a * 1.2f, c.y), col); + dl_->AddTriangleFilled(ImVec2(c.x + gap, c.y - a), ImVec2(c.x + gap, c.y + a), ImVec2(c.x + gap + a * 1.2f, c.y), col); + break; + } + case kUp | kDown: { + const float a = 8 * u, gap = 3.5f * u; + dl_->AddTriangleFilled(ImVec2(c.x - a, c.y - gap), ImVec2(c.x + a, c.y - gap), ImVec2(c.x, c.y - gap - a * 1.2f), col); + dl_->AddTriangleFilled(ImVec2(c.x - a, c.y + gap), ImVec2(c.x + a, c.y + gap), ImVec2(c.x, c.y + gap + a * 1.2f), col); + break; + } + case kOptions: + dl_->AddRect(ImVec2(c.x - 13 * u, c.y - 11 * u), ImVec2(c.x + 13 * u, c.y + 11 * u), col, 6 * u, 0, 1.6f * u); + for (int i = -1; i <= 1; ++i) + dl_->AddLine(ImVec2(c.x - 6 * u, c.y + i * 4.5f * u), ImVec2(c.x + 6 * u, c.y + i * 4.5f * u), col, 1.8f * u); + break; + case kR3: + dl_->AddCircle(c, R, col, 40, 1.6f * u); + dl_->AddCircleFilled(c, R - 4 * u, col, 40); + TextMid(a_.body_bold, 13 * u / s_, cx, cy, Black(1), "R3", 0.5f); + break; + case kL1: + case kR1: + dl_->AddRect(ImVec2(c.x - 20 * u, c.y - 13 * u), ImVec2(c.x + 20 * u, c.y + 13 * u), col, 7 * u, 0, 1.6f * u); + TextMid(a_.body_bold, 15 * u / s_, cx, cy, col, button == kL1 ? "L1" : "R1", 0.5f); + break; + } + return GlyphWidth(button, size); +} + +// One hint in the footer, right-aligned at `right`: the button, then what it +// does in capitals, both centred on the line (as the game's MOVE / ADJUST / BACK). +float Hub::Hint(float right, uint32_t button, const char* label, int line) { + constexpr float kSize = 23, kButton = 31, kGap = 12, kBetween = 46, kSpacing = 0.8f; + const float cy = kHintsY + 40 * float(line); + const std::string text = Upper(T(label)); + const float tw = TextWidth(Regular(), kSize, text, kSpacing); + const float gw = GlyphWidth(button, kButton); + const float x = right - tw; + TextMid(Regular(), kSize, x, cy, pal_.hint, text, 0, kSpacing); + Glyph(button, x - kGap - gw * 0.5f, cy, White(0.92f), kButton); + return x - kGap - gw - kBetween; +} + +// A title over a page of its own (the installer), in the menu's yellow. +void Hub::Heading(const std::string& title, const std::string& subtitle) { + TextMid(Bold(), 54, kPageL, kListY + 30, pal_.title, title); + if (!subtitle.empty()) TextMid(Regular(), 28, kPageL, kListY + 84, kMenuGray, subtitle); +} + +// A scrolled list between the rules: rows of the page's items (the focused +// one in yellow, the colour following the focus as it moves), and small grey +// headings over groups of them. +void Hub::List(std::vector<Row>& rows, int sel, float rh, const std::function<void(int, float, float)>& draw) { + const float hh = 44, view = kListView, x = kListX, y0 = kListY; + float yy = 0; + int sel_row = -1; + for (size_t k = 0; k < rows.size(); ++k) { + Row& r = rows[k]; + if (r.item < 0) { + yy += k ? 14 : 0; + r.y = yy; + yy += hh; + } else { + r.y = yy; + if (r.item == sel) sel_row = int(k); + yy += rh; + } + } + const float total = yy; + if (sel_row < 0) return; + // keep the focused row in view (with its heading, when it's the first under it) + { + float top = rows[size_t(sel_row)].y, bottom = top + rh; + if (sel_row > 0 && rows[size_t(sel_row) - 1].item < 0) top = rows[size_t(sel_row) - 1].y; + float target = scroll_px_; + if (top < target) target = top; + if (bottom > target + view) { + target = bottom - view; + for (const Row& r : rows) + if (r.y >= target - 0.5f) { + target = r.y; + break; + } + } + scroll_px_ = std::clamp(target, 0.0f, std::max(0.0f, total - view)); + } + Ease(scroll_f_, scroll_px_, 28); + Ease(focus_y_, rows[size_t(sel_row)].y, 36); + dl_->PushClipRect(P(0, y0 - 6), P(kW, y0 + view), true); + for (size_t k = 0; k < rows.size(); ++k) { + const Row& row = rows[k]; + const float ry = y0 + row.y - scroll_f_; + if (ry < y0 - rh - 20 || ry > y0 + view + 20) continue; + if (row.item < 0) { + TextMid(Bold(), 21, x, ry + hh * 0.5f, kDimGray, Upper(row.heading), 0, 1.6f); + continue; + } + const float over = std::clamp(1 - std::fabs(focus_y_ - row.y) / (rh * 0.85f), 0.0f, 1.0f); + draw(row.item, ry, over); + } + dl_->PopClipRect(); + if (total > view) { // where the list is: a thin bar at the list's right + const float bx = kPanelX - 56; + const float bh = std::max(40.0f, view * view / total); + const float by = y0 + (view - bh) * scroll_f_ / (total - view); + Rect(bx, y0, 2, view, kRuleGray); + Rect(bx - 1, by, 4, bh, kMenuGray); + } +} + +void Hub::Panel(const std::function<void(float, float, float)>& draw) { + const int v0 = dl_->VtxBuffer.Size; + draw(kPanelX + (1 - Smooth(panel_in_)) * 12, kPanelY, kPanelW); + Fade(v0, Smooth(panel_in_)); +} + +float Hub::Facts(float x, float y, float w, const std::vector<std::pair<std::string, std::string>>& facts) { + for (const auto& [k, v] : facts) { + TextMid(Bold(), 20, x, y + 17, kDimGray, Upper(k), 0, 1.6f); + y += std::max(46.0f, Wrapped(Bold(), 28, x + 190, y, w - 190, kText, v, 1.25f) + 14); + } + return y; +} + +// Black, as the game's menu: the pages draw on it. +void Hub::DrawBackdrop() { + const ImVec2 size = ImGui::GetIO().DisplaySize; + dl_->AddRectFilled(ImVec2(0, 0), size, Black(1)); +} + +// The tabs across the top, as the game's: the chosen one in yellow (easing +// over as L1 and R1 change it), the others grey; in an episode's look, as that +// episode's menu (DrawTabs). Over the installer, its one. +void Hub::DrawTopBar() { + for (int i = 0; i < kTabCount; ++i) Ease(tab_lit_[i], i == tab_ ? 1.0f : 0.0f, 26); + for (int g = 0; g < 3; ++g) + if (theme_[g] > 0.004f) DrawTabs(g, Smooth(theme_[g])); +} + +// The tabs in one game's look, faded by `alpha` (two of them while the look +// changes), all three in Helvetica Neue Heavy: GTA IV's grey and the chosen +// one gold; The Lost and Damned's grey and the chosen one dark red, scuffed +// (Grunge); The Ballad of Gay Tony's each in its colour along the rainbow, the +// chosen one white. +void Hub::DrawTabs(int game, float alpha) { + constexpr float kSize = 66, kGap = 60; + ImFont* const f = a_.tab ? a_.tab : Bold(); + const Palette& p = GamePalette(game); + std::vector<std::string> names; + if (installer_) { + names.push_back(T("Install|tab")); + } else { + for (int i = 0; i < TabsShown(); ++i) names.push_back(T(kTabNames[i])); + } + // (longer names in some languages, a wider type: smaller, to fit the page's width) + float total = 0; + for (size_t i = 0; i < names.size(); ++i) + total += TextWidth(f, kSize, names[i]) + (i ? kGap : 0); + const float fit = std::min(1.0f, (kPageR - kPageL) / std::max(1.0f, total)); + const float size = kSize * fit, gap = kGap * fit; + float x = kPageL; + for (size_t i = 0; i < names.size(); ++i) { + const float lit = installer_ ? 1.0f : tab_lit_[i]; + const ImU32 off = game == 2 ? Along(kTbogtTabs, names.size() > 1 ? float(i) / float(names.size() - 1) : 0.0f) + : p.tab_off; + const float w = TextWidth(f, size, names[i]); + TextMid(f, size, x, kTabsY, WithAlpha(Mix(off, p.tab_on, lit), alpha), names[i]); + if (game == 1 && a_.grunge) { + // (black over black outside the letters: only they show it) + const float x0 = x - 4, x1 = x + w + 4, y0 = kTabsY - size * 0.62f, y1 = kTabsY + size * 0.46f; + const float u0 = (x0 - kPageL) / (kPageR - kPageL + 40), u1 = (x1 - kPageL) / (kPageR - kPageL + 40); + dl_->AddImage(a_.grunge.id, P(x0, y0), P(x1, y1), ImVec2(u0, 0), ImVec2(u1, 1), Black(0.92f * alpha)); + } + x += w + gap; + } +} + +// Each game's share of the look eases to the one chosen on Home (quickly: the +// pillars open as fast), and this frame's colours are mixed from them. +void Hub::Theme() { + const int game = HomeGames().size() > 1 ? HomeGame() : 0; + for (int g = 0; g < 3; ++g) Ease(theme_[g], g == game ? 1.0f : 0.0f, 9); + float sum = 0; + for (float w : theme_) sum += w; + auto mix = [&](ImU32 Palette::*field) { + float c[4] = {}; + for (int g = 0; g < 3; ++g) { + const ImU32 v = GamePalette(g).*field; + for (int k = 0; k < 4; ++k) c[k] += float((v >> (8 * k)) & 0xFF) * theme_[g] / std::max(sum, 1e-4f); + } + ImU32 out = 0; + for (int k = 0; k < 4; ++k) out |= ImU32(std::clamp(c[k] + 0.5f, 0.0f, 255.0f)) << (8 * k); + return out; + }; + for (ImU32 Palette::*field : {&Palette::tab_off, &Palette::tab_on, &Palette::row, &Palette::focus, &Palette::title, + &Palette::hint, &Palette::toast, &Palette::rule_top, &Palette::rule_bottom}) + pal_.*field = mix(field); +} + +// The rules under the tabs and over the footer (across the whole screen; The +// Ballad of Gay Tony's in its rainbow, the lower one darker), and the clock. +void Hub::DrawFooter() { + const ImVec2 size = ImGui::GetIO().DisplaySize; + for (const float y : {kRuleTop, kRuleBottom}) { + const float y0 = P(0, y).y, y1 = P(0, y + 2).y; + dl_->AddRectFilled(ImVec2(0, y0), ImVec2(size.x, y1), y == kRuleTop ? pal_.rule_top : pal_.rule_bottom); + if (theme_[2] > 0.004f) { + constexpr int kStops = int(sizeof(kTbogtRule) / sizeof(kTbogtRule[0])); + const float a = Smooth(theme_[2]) * (y == kRuleTop ? 1.0f : 0.62f); + for (int k = 0; k + 1 < kStops; ++k) { + const float xa = size.x * float(k) / float(kStops - 1), xb = size.x * float(k + 1) / float(kStops - 1); + const ImU32 ca = WithAlpha(kTbogtRule[k], a), cb = WithAlpha(kTbogtRule[k + 1], a); + dl_->AddRectFilledMultiColor(ImVec2(xa, y0), ImVec2(xb, y1), ca, cb, cb, ca); + } + } + } + // At the footer's left, as the game's pause menu has its day, time and + // money: the day, the date and the time. A message takes its place for a + // while (Toast). + // (a touch heavier than the regular type: drawn twice, half a pixel apart) + const float message = ToastShown(); + if (message < 0.999f) { + const std::string clock = lang::Clock(int64_t(std::time(nullptr))); + for (const float dx : {0.0f, 0.5f}) + TextMid(Regular(), 32, kPageL + dx, kHintsY, WithAlpha(pal_.toast, 1 - message), clock, 0, 0.6f); + } +} + +float Hub::ToastShown() const { + return toast_time_ > 0 ? Smooth(std::min(toast_time_ * 3, (3.0f - toast_time_) * 6)) : 0.0f; +} + +void Hub::DrawHints() { + float r = kPageR; + if (confirm_) return; // (the question has its own, under it) + if (installer_) { + InstallerHints(r); + } else { + switch (tab_) { + case kHome: + r = Hint(r, kCross, state_.installed ? "Play" : "Install"); + if (HomeGames().size() > 1) r = Hint(r, kLeft | kRight, "Move"); + break; + case kDisplayTab: + case kGameTab: + case kDebugTab: + r = Hint(r, kCircle, "Back"); + r = Hint(r, kTriangle, "Reset All"); + r = Hint(r, kLeft | kRight, "Adjust"); + r = Hint(r, kUp | kDown, "Move"); + break; + case kSavesTab: + r = Hint(r, kCircle, "Back"); + if (item_[tab_] <= int(backups_.size())) + r = Hint(r, kCross, item_[tab_] == 0 ? "Back Up" : item_[tab_] == 1 ? "Import" : "Restore"); + r = Hint(r, kUp | kDown, "Move"); + break; + case kTrophiesTab: + r = Hint(r, kCircle, "Back"); + if (!trophy_items_.empty()) r = Hint(r, kUp | kDown, "Move"); + break; + case kEpisodesTab: + r = Hint(r, kCircle, "Back"); + if (!state_.episode[std::clamp(item_[tab_], 0, install::kEpisodeCount - 1)]) r = Hint(r, kCross, "Install"); + r = Hint(r, kUp | kDown, "Move"); + break; + case kAboutTab: r = Hint(r, kCircle, "Back"); break; + } + // the second line: what works on every page + float r2 = kPageR; + if (tab_ != kHome) r2 = Hint(r2, kOptions, "Play", 1); + { // L1 and R1 together: the tabs + constexpr float kSize = 23, kButton = 31, kGap = 12, kSpacing = 0.8f; + const float cy = kHintsY + 40; + const std::string text = Upper(T("Tab")); + const float tw = TextWidth(Regular(), kSize, text, kSpacing); + const float x = r2 - tw; + TextMid(Regular(), kSize, x, cy, pal_.hint, text, 0, kSpacing); + const float r1 = GlyphWidth(kR1, kButton), l1 = GlyphWidth(kL1, kButton); + Glyph(kR1, x - kGap - r1 * 0.5f, cy, White(0.92f), kButton); + Glyph(kL1, x - kGap - r1 - 8 - l1 * 0.5f, cy, White(0.92f), kButton); + } + } + // a message: capitals at the footer's left, in the clock's place (as the + // game's PAUSED) + if (const float a = ToastShown(); a > 0) + TextMid(Bold(), 28, kPageL + (1 - a) * 10, kHintsY, WithAlpha(pal_.toast, a), Upper(toast_), 0, 0.8f); +} + +// A question over everything (restore a backup?): the screen goes dark and it +// sits in the middle, as the game's warnings, with its buttons under it. +void Hub::DrawConfirm() { + Ease(confirm_in_, confirm_ ? 1.0f : 0.0f, 14); + if (confirm_in_ < 0.01f) return; + const float a = Smooth(confirm_in_); + const ImVec2 size = ImGui::GetIO().DisplaySize; + dl_->AddRectFilled(ImVec2(0, 0), size, Black(0.97f * a)); + const int v0 = dl_->VtxBuffer.Size; + const float w = 1080, cx = kW * 0.5f; + const std::vector<std::string> lines = WrapLines(Regular(), 31, w, confirm_text_); + const float line_h = 31 * 1.45f; + const float h = 74 + float(lines.size()) * line_h + 40 + 40; + float y = (kH - h) * 0.5f - 20 + (1 - a) * 16; + TextMid(Bold(), 56, cx, y + 26, pal_.title, confirm_title_, 0.5f); + y += 74; + for (const std::string& l : lines) Text(Regular(), 31, cx, y, kText, l, 0.5f), y += line_h; + y += 40; + // the buttons, centred: Cross and what it does, then Circle's + constexpr float kSize = 25, kButton = 33, kGap = 12, kBetween = 56, kSpacing = 0.8f; + struct Choice { + uint32_t button; + std::string text; + }; + std::vector<Choice> choices = {{kCross, Upper(confirm_yes_)}}; + if (confirm_yes_ != "OK") choices.push_back({kCircle, Upper(confirm_no_label_.empty() ? T("Cancel") : confirm_no_label_)}); + float total = 0; + for (const Choice& c : choices) total += GlyphWidth(c.button, kButton) + kGap + TextWidth(Regular(), kSize, c.text, kSpacing); + total += kBetween * float(choices.size() - 1); + float x = cx - total * 0.5f; + for (const Choice& c : choices) { + const float gw = GlyphWidth(c.button, kButton); + Glyph(c.button, x + gw * 0.5f, y + 16, White(0.92f), kButton); + x += gw + kGap; + TextMid(Regular(), kSize, x, y + 16, pal_.hint, c.text, 0, kSpacing); + x += TextWidth(Regular(), kSize, c.text, kSpacing) + kBetween; + } + Fade(v0, a); +} + +// ------------------------------------------------------------------- pages + +void Hub::DrawHome() { + const std::vector<int> games = HomeGames(); + if (games.size() > 1) { + DrawHomePanels(games); + return; + } + const ImVec2 size = ImGui::GetIO().DisplaySize; + // The picture fills the space between the rules (as the Map tab's map), + // a little closer than it fits and drifting slowly from side to side, as + // the game's own loading art does. X plays (the hint at the bottom). + const float top = P(0, kRuleTop + 2).y, bottom = P(0, kRuleBottom).y; + const Image& hero = a_.hero; + if (hero) { + const float breath = 0.5f - 0.5f * std::cos(time_ * 6.2832f / 90.0f); + const float zoom = 1.10f + 0.04f * breath; + const float band = size.x / std::max(1.0f, bottom - top), img = hero.width / hero.height; + float uw = 1, vh = img / band; // (the band is wider than the picture: its full width, a strip of its height) + if (vh > 1) uw = 1 / vh, vh = 1; + uw /= zoom, vh /= zoom; + // (across most of the room the zoom leaves, there and back in a minute) + const float cx = 0.5f + (1 - uw) * 0.5f * 0.9f * std::sin(time_ * 6.2832f / 60.0f); + const float cy = 0.27f; // (the face and the skyline, room over the head) + const ImVec2 uv0(cx - uw * 0.5f, std::clamp(cy - vh * 0.5f, 0.0f, 1 - vh)); + const ImVec2 uv1(uv0.x + uw, uv0.y + vh); + dl_->AddImage(hero.id, ImVec2(0, top), ImVec2(size.x, bottom), uv0, uv1); + dl_->AddRectFilled(ImVec2(0, top), ImVec2(size.x, bottom), Black(0.22f)); + const float fade_to = P(1000, 0).x; + dl_->AddRectFilledMultiColor(ImVec2(0, top), ImVec2(fade_to, bottom), Black(0.7f), Black(0), Black(0), Black(0.7f)); + dl_->AddRectFilledMultiColor(ImVec2(0, P(0, kRuleBottom - 200).y), ImVec2(size.x, bottom), Black(0), Black(0), + Black(0.6f), Black(0.6f)); + } + if (!state_.installed) { + const float in = Smooth((time_ - 0.2f) / 0.6f); + const int v0 = dl_->VtxBuffer.Size; + TextMid(Bold(), 40, kPageL, kRuleBottom - 104, Rgba(255, 255, 255), T("Not installed yet")); + TextMid(Regular(), 30, kPageL, kRuleBottom - 58, Rgba(200, 200, 200), + T("Press X to set it up from your disc image and title update.")); + Fade(v0, in); + } +} + +namespace { +// Where the person is in each game's Home picture (hub/home: gta4, tlad, +// tbogt), across and down: the face, nudged toward the middle of the figure. +// A closed pillar is centred on it. +constexpr ImVec2 kHomeFocus[3] = {{0.468f, 0.16f}, {0.485f, 0.07f}, {0.452f, 0.13f}}; +// Each logo's size (The Ballad of Gay Tony's is wide and heavy: smaller). +constexpr float kHomeLogoScale[3] = {1.0f, 1.0f, 0.8f}; + +// The fade up from the chosen pillar's foot, under its logo: dark under GTA +// IV's white logo, white under The Lost and Damned's red one (its picture is +// light), a deep blue under The Ballad of Gay Tony's neon. +ImU32 FootColor(int game) { + return game == 1 ? Rgba(255, 255, 255) : game == 2 ? Rgba(10, 16, 60) : Rgba(0, 0, 0); +} + +// Each game's colour, for the glow round its pillar when its logo and +// picture have none. +ImU32 GameColor(int game) { + return game == 1 ? Rgba(214, 92, 38) // The Lost and Damned: rust + : game == 2 ? Rgba(232, 72, 150) // The Ballad of Gay Tony: pink + : kAccent; // GTA IV: the menu's gold +} + +// Quick, then settling (a choice opening). +float EaseOut(float t) { + const float u = 1 - std::clamp(t, 0.0f, 1.0f); + return 1 - u * u * u * u; +} + +// The part of a convex polygon inside a convex quad, both clockwise on the +// screen (Sutherland-Hodgman). +std::vector<ImVec2> ClipToQuad(std::vector<ImVec2> poly, const ImVec2 (&quad)[4]) { + for (int e = 0; e < 4 && !poly.empty(); ++e) { + const ImVec2 a = quad[e], b = quad[(e + 1) % 4]; + auto side = [&](ImVec2 p) { return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x); }; + std::vector<ImVec2> out; + for (size_t i = 0; i < poly.size(); ++i) { + const ImVec2 p = poly[i], q = poly[(i + 1) % poly.size()]; + const float sp = side(p), sq = side(q); + if (sp >= 0) out.push_back(p); + if ((sp >= 0) != (sq >= 0)) { + const float t = sp / (sp - sq); + out.emplace_back(p.x + (q.x - p.x) * t, p.y + (q.y - p.y) * t); + } + } + poly = std::move(out); + } + return poly; +} +} // namespace + +// Home once an episode is installed: a pillar for each game, side by side +// across the band between the rules, all leaning the same way (as the +// Complete Edition's cover). The chosen one opens to twice the others' width, +// in colour, its logo over a fade at its foot, a glow in the logo's colour at +// its edges that breathes, and now and then a glare across it; the others are +// narrower, black and white and dimmer, each centred on its person. The +// chosen one's picture moves as the game's loading art does (closer and back, +// side to side, slowly). A choice opens quickly and settles (EaseOut), its +// glow flashing; a press past the last pillar nudges it. Left and Right +// choose, X plays. +void Hub::DrawHomePanels(const std::vector<int>& games) { + constexpr float kOpen = 1.0f; // (the chosen one's extra width, in the others' widths) + constexpr float kLean = 70.0f; // (how far a pillar's top leans right, at 1080p: about 6 degrees) + constexpr float kSwitch = 0.42f; // (seconds a choice takes to open) + const ImVec2 size = ImGui::GetIO().DisplaySize; + const float top = P(0, kRuleTop + 2).y, bottom = P(0, kRuleBottom).y; + const float gap = std::round(10 * s_), lean = kLean * s_; + const int chosen = HomeGame(); + if (chosen != home_shown_) { // (a new choice: each pillar from where it is now) + for (int g = 0; g < 3; ++g) home_from_[g] = home_wide_[g]; + home_shown_ = chosen, home_switch_ = 0, home_pop_ = 1, home_glare_ = 0; + } + home_switch_ = std::min(1.0f, home_switch_ + dt_ / kSwitch); + home_nudge_ = std::min(1.0f, home_nudge_ + dt_ / 0.32f); + home_glare_ += dt_; + Ease(home_pop_, 0, 4); + const float open = EaseOut(home_switch_); + float shares = 0; + for (const int g : games) { + home_wide_[g] = home_from_[g] + ((g == chosen ? 1.0f : 0.0f) - home_from_[g]) * open; + shares += 1 + home_wide_[g] * kOpen; + } + // (the first pillar's foot starts off the screen, so its top meets the screen's edge) + const float room = size.x + lean - gap * float(games.size() - 1); + dl_->PushClipRect(ImVec2(0, top), ImVec2(size.x, bottom), true); + float x = -lean; + for (const int g : games) { + const float w = room * (1 + home_wide_[g] * kOpen) / shares; + DrawHomePanel(g, x, x + w, top, bottom, lean, Smooth(home_wide_[g])); + x += w + gap; + } + dl_->PopClipRect(); +} + +// A quad in four colours (top left, top right, bottom right, bottom left). +void Hub::Quad(ImVec2 tl, ImVec2 tr, ImVec2 br, ImVec2 bl, ImU32 ctl, ImU32 ctr, ImU32 cbr, ImU32 cbl) { + const ImVec2 uv = ImGui::GetFontTexUvWhitePixel(); + const ImDrawIdx i = ImDrawIdx(dl_->_VtxCurrentIdx); + dl_->PrimReserve(6, 4); + dl_->PrimWriteIdx(i), dl_->PrimWriteIdx(ImDrawIdx(i + 1)), dl_->PrimWriteIdx(ImDrawIdx(i + 2)); + dl_->PrimWriteIdx(i), dl_->PrimWriteIdx(ImDrawIdx(i + 2)), dl_->PrimWriteIdx(ImDrawIdx(i + 3)); + dl_->PrimWriteVtx(tl, uv, ctl), dl_->PrimWriteVtx(tr, uv, ctr); + dl_->PrimWriteVtx(br, uv, cbr), dl_->PrimWriteVtx(bl, uv, cbl); +} + +// A convex polygon, each corner its colour. +void Hub::FillPoly(const std::vector<ImVec2>& poly, const std::function<ImU32(ImVec2)>& color) { + const int n = int(poly.size()); + if (n < 3) return; + const ImVec2 uv = ImGui::GetFontTexUvWhitePixel(); + const ImDrawIdx i = ImDrawIdx(dl_->_VtxCurrentIdx); + dl_->PrimReserve((n - 2) * 3, n); + for (int k = 1; k + 1 < n; ++k) + dl_->PrimWriteIdx(i), dl_->PrimWriteIdx(ImDrawIdx(i + k)), dl_->PrimWriteIdx(ImDrawIdx(i + k + 1)); + for (const ImVec2& p : poly) dl_->PrimWriteVtx(p, uv, color(p)); +} + +// A pillar: its foot from x0 to x1 on the lower rule, its top `lean` over. +void Hub::DrawHomePanel(int game, float x0, float x1, float top, float bottom, float lean, float on) { + const Assets::HomeArt& art = a_.home[game]; + const float h = bottom - top; + const ImVec2 tl(x0 + lean, top), tr(x1 + lean, top), br(x1, bottom), bl(x0, bottom); + const ImVec2 pillar[4] = {tl, tr, br, bl}; + // (a point up its sides: t 0 at the foot, 1 at the top) + auto side = [&](float x, float t) { return ImVec2(x + lean * t, bottom - h * t); }; + // a press past the last pillar: the chosen one's picture and logo nudge that way and back + const float nudge = on * float(home_nudge_dir_) * 16 * s_ * std::sin(3.1416f * home_nudge_) * (1 - home_nudge_); + if (art.picture) { + // the picture covers the pillar (upright, the pillar cut from it), a + // little closer than it fits, the person in the middle (kHomeFocus); + // the chosen one moving in the room that leaves + const float left = std::min(x0, x0 + lean), width = x1 - x0 + std::fabs(lean); + const float phase = float(game) * 2.1f; + const float breath = 0.5f - 0.5f * std::cos(time_ * 6.2832f / 26.0f + phase); + const float zoom = 1.08f + on * (0.04f + 0.05f * breath + 0.03f * home_pop_); + const float shape = width / std::max(1.0f, h), picture = art.picture.width / std::max(1.0f, art.picture.height); + float uw = 1, vh = 1; + if (picture > shape) uw = shape / picture; + else vh = picture / shape; + uw /= zoom, vh /= zoom; + const ImVec2 focus = kHomeFocus[game]; + // down: the face a fifth of the way down + const float v0 = std::clamp(focus.y - 0.2f * vh + on * 0.012f * std::sin(time_ * 6.2832f / 41.0f + phase), 0.0f, + 1 - vh); + // across: the face in the middle of the pillar's part on the screen, at + // the face's height (the pillar leans) + const float face = (focus.y - v0) / vh; // (0 at the top) + const float screen_w = ImGui::GetIO().DisplaySize.x; + const float middle = (std::max(0.0f, x0 + lean * (1 - face)) + std::min(screen_w, x1 + lean * (1 - face))) * 0.5f; + const float sway = on * 0.06f * std::sin(time_ * 6.2832f / 34.0f + phase) - nudge / width; + const float u0 = std::clamp(focus.x - (middle - left) / width * uw + sway * uw, 0.0f, 1 - uw); + const ImVec2 uv0(u0, v0); + auto uv = [&](ImVec2 p) { return ImVec2(uv0.x + (p.x - left) / width * uw, uv0.y + (p.y - top) / h * vh); }; + if (!art.grey || on < 0.999f) + dl_->AddImageQuad((art.grey ? art.grey : art.picture).id, tl, tr, br, bl, uv(tl), uv(tr), uv(br), uv(bl)); + if (art.grey && on > 0.001f) + dl_->AddImageQuad(art.picture.id, tl, tr, br, bl, uv(tl), uv(tr), uv(br), uv(bl), White(on)); + } else { + // (no picture: a dark pillar) + Quad(tl, tr, br, bl, Rgba(38, 38, 42), Rgba(38, 38, 42), Rgba(12, 12, 14), Rgba(12, 12, 14)); + } + // the others dimmer; under the chosen one's logo, a fade up from its foot + dl_->AddQuadFilled(tl, tr, br, bl, Black(0.5f * (1 - on))); + const ImU32 foot = FootColor(game); + Quad(side(x0, 0.55f), side(x1, 0.55f), br, bl, WithAlpha(foot, 0), WithAlpha(foot, 0), WithAlpha(foot, 0.82f * on), + WithAlpha(foot, 0.82f * on)); + // the logo (the game's name without one), at the foot of the chosen one, + // a touch bigger as it's chosen + if (on > 0.01f) { + const float cx = ((x0 + x1) * 0.5f + lean * 0.22f + nudge - ox_) / s_, hw = (x1 - x0) / s_; + const float foot_y = (bottom - oy_) / s_ - 56; + const float scale = kHomeLogoScale[game] * (1 + 0.05f * home_pop_); + if (art.logo) { + const float lw = std::min(hw * 0.72f, 620.0f) * scale; + const float lh = std::min(lw * art.logo.height / art.logo.width, 230.0f * scale); + const float fw = lh * art.logo.width / art.logo.height; + dl_->AddImage(art.logo.id, P(cx - fw * 0.5f, foot_y - lh), P(cx + fw * 0.5f, foot_y), ImVec2(0, 0), + ImVec2(1, 1), White(on)); + } else { + const std::string name = game ? T(install::EpisodeName(game - 1)) : "Grand Theft Auto IV"; + TextMid(Bold(), 52, cx, foot_y - 30, White(on), Fit(Bold(), 52, name, hw - 80), 0.5f); + } + } + // a glare across the chosen one: just after it's chosen, then every few + // seconds, a soft band of light steeper than the pillar, cut by its sides + if (on > 0.01f) { + constexpr float kEvery = 5.5f, kSweep = 0.9f; + const float t = std::fmod(std::max(0.0f, home_glare_ - 0.12f), kEvery) / kSweep; + if (t < 1) { + // (a wide soft band and a bright narrow one in its middle) + const float g = h * 0.55f, wide = 0.12f * (x1 - x0) + 40 * s_, core = 0.022f * (x1 - x0) + 8 * s_; + const float c = (x0 - g - wide) + (x1 + lean + 2 * wide + g - x0) * Smooth(t); + auto u = [&](ImVec2 p) { return p.x - g * (bottom - p.y) / h; }; + for (const auto& [half, peak] : {std::pair{wide, 0.2f}, std::pair{core, 0.3f}}) { + auto light = [&](ImVec2 p) { return White(peak * on * std::max(0.0f, 1 - std::fabs(u(p) - c) / half)); }; + for (const float a : {c - half, c}) { + const float b = a + half; + FillPoly(ClipToQuad({ImVec2(a + g, top), ImVec2(b + g, top), ImVec2(b, bottom), ImVec2(a, bottom)}, pillar), + light); + } + } + } + } + // the glow round the chosen one, in its colour: bright at the edge, fading + // in, breathing, and flashing as it's chosen + if (on > 0.01f) { + const ImU32 col = art.color ? art.color : GameColor(game); + const float pulse = 0.85f + 0.15f * std::sin(time_ * 6.2832f / 2.6f); + const float strength = on * std::min(1.0f, pulse + 0.6f * home_pop_); + const float step = std::max(1.0f, 1.5f * s_); + const int rings = 22 + int(10 * home_pop_); + for (int k = 0; k < rings; ++k) { + const float t = 1 - float(k) / float(rings), in = k * step, up = in / h; + auto at = [&](float x, float t, float dx) { const ImVec2 p = side(x, t); return ImVec2(p.x + dx, p.y); }; + const ImVec2 ring[4] = {at(x0, 1 - up, in), at(x1, 1 - up, -in), at(x1, up, -in), at(x0, up, in)}; + dl_->AddPolyline(ring, 4, WithAlpha(col, strength * (k ? 0.75f * t * t : 1.0f)), ImDrawFlags_Closed, + k ? step : 2 * step); + } + } +} + +namespace { +bool IsPreset(const Hub::Setting& r) { return std::string_view(r.key) == "hub_display_preset"; } + +// What each preset sets (Recommended, Performance, Quality). Recommended is +// every one's own default. Frame Generation and the Debug options' Display +// rows aren't in them: hidden, those stay at their recommended values. +struct PresetValue { + const char* key; + const char* value; +}; +const std::vector<PresetValue>& PresetValues(int preset) { + static const std::vector<PresetValue> kPresets[3] = { + {{"resolution", "4k"}, + {"gta4_draw_distance_scale", "1.0"}, + {"ps5_population", "1.0"}, + {"gta4_force_highest_lod", "false"}, + {"gta4_frame_limit", "60"}, + {"gta4_native_anti_aliasing", "fxaa"}, + {"gta4_anisotropic_filtering", "1x"}, + {"gta4_modern_shaders", "false"}, + {"gta4_reflection_resolution", "1080p"}, + {"gta4_shadow_map_base_size", "512"}, + {"gta4_shadow_distance_scale", "1.0"}}, + {{"resolution", "1440p"}, + {"gta4_draw_distance_scale", "1.0"}, + {"ps5_population", "0.75"}, + {"gta4_force_highest_lod", "false"}, + {"gta4_frame_limit", "60"}, + {"gta4_native_anti_aliasing", "fxaa"}, + {"gta4_anisotropic_filtering", "1x"}, + {"gta4_modern_shaders", "false"}, + {"gta4_reflection_resolution", "original"}, + {"gta4_shadow_map_base_size", "512"}, + {"gta4_shadow_distance_scale", "1.0"}}, + {{"resolution", "4k"}, + {"gta4_draw_distance_scale", "1.5"}, + {"ps5_population", "1.0"}, + {"gta4_force_highest_lod", "true"}, + {"gta4_frame_limit", "60"}, + {"gta4_native_anti_aliasing", "smaa"}, + {"gta4_anisotropic_filtering", "16x"}, + {"gta4_modern_shaders", "true"}, + {"gta4_reflection_resolution", "1080p"}, + {"gta4_shadow_map_base_size", "1024"}, + {"gta4_shadow_distance_scale", "2.0"}}, + }; + return kPresets[std::clamp(preset, 0, 2)]; +} +const Hub::Setting* FindSetting(std::string_view key) { + for (const Hub::Setting& r : Settings()) + if (key == r.key) return &r; + return nullptr; +} +} // namespace + +int Hub::PresetNow() const { + for (int p = 0; p < kPresetCustom; ++p) { + bool all = true; + for (const PresetValue& pv : PresetValues(p)) { + const Setting* r = FindSetting(pv.key); + if (!r) continue; + const std::string want = pv.value; + const int k = SettingIndex(*r); + if (k < 0 || k >= int(r->values.size()) || want != r->values[size_t(k)]) { + all = false; + break; + } + } + if (all) return p; + } + return kPresetCustom; +} + +void Hub::ApplyPreset(int preset) { + for (const PresetValue& pv : PresetValues(preset)) { + const Setting* r = FindSetting(pv.key); + if (!r) continue; + const std::string v = pv.value; + Save(r->key, r->quoted ? "\"" + v + "\"" : v); + } +} + +int Hub::SettingIndex(const Setting& r) const { + if (IsPreset(r)) return PresetNow(); + const std::string v = Value(r.key, r.fallback); + for (size_t k = 0; k < r.values.size(); ++k) + if (v == r.values[k]) return int(k); + // (a number the game wrote itself: "1.000000" for "1.0") + if (char* end = nullptr; !r.quoted && !v.empty()) { + const double n = std::strtod(v.c_str(), &end); + if (end && *end == '\0') + for (size_t k = 0; k < r.values.size(); ++k) + if (std::fabs(std::strtod(r.values[k], nullptr) - n) < 1e-6 && std::isdigit((unsigned char)r.values[k][0])) + return int(k); + } + for (size_t k = 0; k < r.values.size(); ++k) + if (std::string(r.fallback) == r.values[k]) return int(k); + return 0; +} + +std::vector<int> Hub::SettingsOn(int tab) const { + std::vector<int> out; + const auto& set = Settings(); + for (int i = 0; i < int(set.size()); ++i) { + const std::string_view section = set[size_t(i)].section; + const int t = section == "DISPLAY" ? kDisplayTab : section == "DEBUG" ? kDebugTab : kGameTab; + if (t == tab && Shown(set[size_t(i)])) out.push_back(i); + } + return out; +} + +namespace { +// A level (Menu Music): drawn as a bar, as the game's SFX and music levels. +bool IsLevel(const Hub::Setting& r) { return std::string_view(r.key) == "hub_music_volume"; } +bool IsLanguage(const Hub::Setting& r) { return std::string_view(r.key) == "ps5_language"; } +// A value as shown: Auto says which language it is. +std::string ValueLabel(const Hub::Setting& r, int k) { + if (IsLanguage(r) && std::string_view(r.values[size_t(k)]) == "auto") + return std::string(T("Auto")) + " (" + lang::Name(lang::System()) + ")"; + return T(r.labels[size_t(k)]); +} +} // namespace + +// Display, Game and Debug: the game's settings page - a label, its value +// across from it - and the focused one explained at the right. +void Hub::DrawSettings() { + constexpr float kRowH = 46, kSize = 34; + const auto& set = Settings(); + const std::vector<int> on = SettingsOn(tab_); + if (on.empty()) return; + const int sel = item_[tab_] = std::clamp(item_[tab_], 0, int(on.size()) - 1); + std::vector<Row> rows; + for (int i = 0; i < int(on.size()); ++i) rows.push_back({i}); + List(rows, sel, kRowH, [&](int i, float ry, float over) { + const auto& r = set[size_t(on[size_t(i)])]; + const float cy = ry + kRowH * 0.5f; + const ImU32 col = Mix(pal_.row, pal_.focus, over); + // (a long name, in some languages: smaller, to stay clear of its value) + const std::string label = T(r.label); + const float room = kValueX - kListX - 28, lw = TextWidth(Bold(), kSize, label); + TextMid(Bold(), lw > room ? std::max(24.0f, kSize * room / lw) : kSize, kListX, cy, col, label); + const int idx = SettingIndex(r); + if (IsLevel(r)) { + const float bw = 280, bh = 18, by = cy - bh * 0.5f; + const float f = r.values.size() > 1 ? float(idx) / float(r.values.size() - 1) : 0; + Rect(kValueX, by, bw, bh, kBarBack); + if (f > 0) Rect(kValueX, by, bw * f, bh, Mix(Rgba(206, 206, 206), pal_.focus, over)); + return; + } + // the value just changed: it slides in from the side it was changed toward + const float in = i == sel ? Smooth(value_in_) : 1; + const int v0 = dl_->VtxBuffer.Size; + TextMid(Bold(), kSize, kValueX + (1 - in) * 16 * value_dir_, cy, col, ValueLabel(r, idx)); + Fade(v0, 0.2f + 0.8f * in); + }); + // the focused setting, explained, and its choices + const auto& r = set[size_t(on[size_t(sel)])]; + Panel([&](float px, float py, float pw) { + py += Wrapped(Regular(), 29, px, py + 6, pw, Rgba(196, 196, 196), T(r.help), 1.32f) + 34; + if (std::string_view(r.key) == "ps5_refresh_rate" && display_.high_refresh) { + const char* now = !display_.high_refresh() ? "60 Hz" + : display_.vrr && display_.vrr() ? "120 Hz with VRR" + : "120 Hz"; + TextMid(Bold(), 29, px, py + 18, kText, F("Right now: {}", {T(now)})); + py += 44 + 20; + } + if (IsLevel(r) || r.labels.size() > 6) return; + const int cur = SettingIndex(r); + for (int k = 0; k < int(r.labels.size()); ++k) { + TextMid(Bold(), 29, px, py + 18, k == cur ? pal_.focus : kDimGray, ValueLabel(r, k)); + py += 44; + } + }); +} + +void Hub::DrawSaves() { + constexpr float kRowH = 46, kSize = 33, kSavesValueX = 840; + const int nb = int(backups_.size()), nf = int(save_items_.size()); + const int n = 2 + nb + nf; + const int sel = item_[tab_] = std::clamp(item_[tab_], 0, n - 1); + std::vector<Row> rows; + rows.push_back({0}); + rows.push_back({1}); + if (nb) { + rows.push_back({-1, T("Restore a backup")}); + for (int i = 0; i < nb; ++i) rows.push_back({2 + i}); + } + if (nf) { + rows.push_back({-1, F("Your saves {}", {std::to_string(nf)})}); + for (int i = 0; i < nf; ++i) rows.push_back({2 + nb + i}); + } + List(rows, sel, kRowH, [&](int i, float ry, float over) { + std::string name, value; + if (i == 0) { + name = T("Back Up Now"); + } else if (i == 1) { + name = T("Import Xbox 360 Saves"); + value = pending_imports_ ? F("{} waiting", {std::to_string(pending_imports_)}) : T("None waiting"); + } else if (i < 2 + nb) { + const Backup& b = backups_[size_t(i - 2)]; + name = b.name, value = Size(b.bytes); + } else { + const SaveItem& f = save_items_[size_t(i - 2 - nb)]; + name = T(f.name.c_str()), value = T(f.kind.c_str()); + } + const float cy = ry + kRowH * 0.5f; + const ImU32 col = Mix(pal_.row, pal_.focus, over); + TextMid(Bold(), kSize, kListX, cy, col, Fit(Bold(), kSize, name, kSavesValueX - kListX - 30)); + if (!value.empty()) TextMid(Bold(), kSize, kSavesValueX, cy, col, Fit(Bold(), kSize, value, kPanelX - 50 - kSavesValueX)); + }); + Panel([&](float px, float py, float pw) { + const ImU32 text = Rgba(196, 196, 196); + if (sel == 0) { + Wrapped(Regular(), 29, px, py + 6, pw, text, + T("Copies all of your GTA IV saves into SaveBackups, named by the date and time."), 1.38f); + } else if (sel == 1) { + py += Wrapped(Regular(), 29, px, py + 6, pw, text, + T("Bring your Xbox 360 progress over. Copy the save files from your 360 (on its drive they're " + "in Content/<your profile>/545407F2/00000001) into this folder on the PS5:"), 1.38f) + 22; + py += Wrapped(Bold(), 28, px, py, pw, pal_.title, "/data/homebrew/PPSA94004/import/") + 26; + Wrapped(Regular(), 27, px, py, pw, kMenuGray, + T("Press Cross to import them. Your saves are backed up first, and imported files move to " + "import/done."), 1.38f); + } else if (sel < 2 + nb) { + const Backup& b = backups_[size_t(sel - 2)]; + py += Wrapped(Bold(), 36, px, py, pw, kText, b.name, 1.15f) + 22; + py = Facts(px, py, pw, {{T("Files"), std::to_string(b.files)}, {T("Size"), Size(b.bytes)}}); + Wrapped(Regular(), 27, px, py + 16, pw, kMenuGray, + T("Restoring puts these saves back. The saves you have now are backed up first."), 1.38f); + } else { + const SaveItem& f = save_items_[size_t(sel - 2 - nb)]; + py += Wrapped(Bold(), 36, px, py, pw, kText, T(f.name.c_str()), 1.15f) + 22; + Facts(px, py, pw, + {{T("Type"), T(f.kind.c_str())}, + {T("Files"), std::to_string(f.files)}, + {T("Size"), Size(f.bytes)}, + {T("Changed"), Date(f.modified)}}); + } + }); +} + +// The episodes: each, and whether it's installed; the focused one explained, +// and where its package goes. Cross installs it (the installer). +void Hub::DrawEpisodes() { + constexpr float kRowH = 46, kSize = 33, kEpisodeValueX = 760; + const int n = install::kEpisodeCount; + const int sel = item_[tab_] = std::clamp(item_[tab_], 0, n - 1); + std::vector<Row> rows; + for (int i = 0; i < n; ++i) rows.push_back({i}); + auto status = [&](int e) { + return T(state_.episode[e] ? "Installed" : state_.episode_found[e] ? "Ready to install|episode" : "Not installed"); + }; + List(rows, sel, kRowH, [&](int i, float ry, float over) { + const float cy = ry + kRowH * 0.5f; + const ImU32 col = Mix(pal_.row, pal_.focus, over); + TextMid(Bold(), kSize, kListX, cy, col, Fit(Bold(), kSize, T(install::EpisodeName(i)), kEpisodeValueX - kListX - 30)); + TextMid(Bold(), kSize, kEpisodeValueX, cy, col, Fit(Bold(), kSize, status(i), kPanelX - 50 - kEpisodeValueX)); + }); + Panel([&](float px, float py, float pw) { + const ImU32 text = Rgba(196, 196, 196); + py += Wrapped(Bold(), 36, px, py, pw, kText, T(install::EpisodeName(sel)), 1.15f) + 18; + py += Wrapped(Regular(), 29, px, py, pw, text, + T(sel == 0 ? "Johnny Klebitz and The Lost MC. The first of the two episodes." + : "Luis Lopez and nightclub owner Tony Prince. The second of the two episodes."), + 1.38f) + 26; + if (state_.episode[sel]) { + Wrapped(Regular(), 27, px, py, pw, kMenuGray, + T("Installed. When the game starts it asks if you want to play it, the first time."), 1.38f); + return; + } + if (!state_.installed) { + Wrapped(Regular(), 27, px, py, pw, kMenuGray, + T("Install GTA IV first. The episodes are added to the installed game."), 1.38f); + return; + } + py += Wrapped(Regular(), 27, px, py, pw, kMenuGray, + T("Copy the episode's package from your Xbox 360 (on its drive it's in " + "Content/0000000000000000/545407F2/00000002) into this folder on the PS5:"), + 1.38f) + 22; + py += Wrapped(Bold(), 28, px, py, pw, pal_.title, "/data/homebrew/PPSA94004/install/") + 26; + Wrapped(Regular(), 27, px, py, pw, kMenuGray, + T(state_.episode_found[sel] ? "Its package is there. Press Cross to install it." + : "Then press Cross to install it."), + 1.38f); + }); +} + +void Hub::DrawAbout() { + struct Credit { + const char* who; + const char* what; + }; + const Credit people[] = { + {"OZORDI", "Created Liberty Recompiled, the GTA IV recompilation this is built on, with its contributors."}, + {"@elripalda", "Brought it to PlayStation 5: the PS5 port and this hub."}, + {"vaduur", "LibertyRecompAndroid: the fixes for running it on handheld hardware."}, + {"ReXGlue", "The static recompiler and runtime that turn the Xbox 360 game into a native program."}, + {"lamarking", "RecompTrophy: the native PS5 trophies and their pop-ups."}, + }; + // (five now: a little closer together, to fit above the footer) + float y = kListY + 6; + for (const Credit& c : people) { + TextMid(Bold(), 35, kListX, y + 18, kText, c.who); + y += 42; + y += Wrapped(Regular(), 28, kListX, y, 860, kMenuGray, T(c.what), 1.35f) + 22; + } + const float px = kPanelX; + float py = kPanelY + 4; + TextMid(Bold(), 21, px, py + 12, kDimGray, Upper(T("Built With")), 0, 1.6f); + py += 44; + const char* parts[] = {"PS5 Payload SDK", "Mesa RADV (Vulkan)", "Dear ImGui", "stb_image, stb_vorbis, dr_mp3 and dr_flac", + "TeX Gyre Heros (GUST Font License)", "Oswald and Inter (SIL Open Font License)"}; + for (const char* part : parts) { + TextMid(Regular(), 28, px, py + 14, Rgba(196, 196, 196), T(part)); + py += 40; + } + py += 26; + TextMid(Bold(), 21, px, py + 12, kDimGray, Upper(T("Please Note")), 0, 1.6f); + py += 44; + Wrapped(Regular(), 26, px, py, kPanelW, kMenuGray, + T("An unofficial fan project, not affiliated with or endorsed by Rockstar Games, Take-Two " + "Interactive, Microsoft or Sony. Grand Theft Auto and all related names belong to their owners. No game " + "files are included: you need your own copy of GTA IV for Xbox 360."), + 1.36f); + if (!version_.empty()) TextMid(Regular(), 24, px, kRuleBottom - 34, kDimGray, F("Version {}", {VersionLabel(version_)})); + // Cross held here: a line filling under the version until the debug + // options switch (Navigate), once it has been held a moment. + if (about_hold_ > kUnlockShow && !about_hold_done_) { + const float f = std::clamp((about_hold_ - kUnlockShow) / (kUnlockHold - kUnlockShow), 0.0f, 1.0f); + Rect(px, kRuleBottom - 16, 240, 3, Rgba(58, 58, 58)); + Rect(px, kRuleBottom - 16, 240 * f, 3, pal_.focus); + } +} + +// --------------------------------------------------------------- behaviour + +namespace { +// The Display page's rarer settings: shown with the debug options (Frame +// Generation is experimental; the other two only trade a little picture for +// frame rate, and are set for it). +bool Advanced(const Hub::Setting& r) { + const std::string_view key = r.key; + return key == "ps5_frame_generation" || key == "gta4_native_shadow_min_indices" || + key == "ps5_environment_reflection_every"; +} +} // namespace + +bool Hub::Shown(const Setting& r) const { + return debug_options_ || (!Advanced(r) && std::string_view(r.section) != "DEBUG"); +} + +// What isn't shown is at its recommended value: the Debug page's and the rarer +// Display settings (as Reset All sets them), and Light Color Fix, which no +// longer has a row (always on). Run at the start and when the debug options +// are hidden again. +void Hub::KeepHiddenSettings() { + for (const Setting& r : Settings()) { + if (Shown(r) || Value(r.key, r.fallback) == r.fallback) continue; + Save(r.key, r.quoted ? std::string("\"") + r.fallback + "\"" : std::string(r.fallback)); + if (std::string_view(r.key) == "ps5_frame_generation") { + Save("gta4_native_frame_generation", "\"off\""); + Save("gta4_native_upscaler", "\"native\""); + Save("gta4_temporal_upscaler_quality", "\"native\""); + } + } + // (frame generation off for everyone, whatever turned it on before) + if (Value("ps5_frame_generation", "off") != "off" || Value("gta4_native_frame_generation", "off") != "off" || + Value("gta4_native_upscaler", "native") != "native") { + Save("ps5_frame_generation", "\"off\""); + Save("gta4_native_frame_generation", "\"off\""); + Save("gta4_native_upscaler", "\"native\""); + Save("gta4_temporal_upscaler_quality", "\"native\""); + } + if (Value("gta4_native_guest_texture_swizzle", "false") != "false") + Save("gta4_native_guest_texture_swizzle", "false"); +} + +void Hub::SetDebugOptions(bool on) { + debug_options_ = on; + Save("hub_debug_options", on ? "true" : "false"); + if (!on) KeepHiddenSettings(); + value_in_ = 0; + Play(on ? Sound::kOn : Sound::kOff); + Toast(T(on ? "Debug options on" : "Debug options off")); +} + +// "1.0t1" -> "1.0 Test 1", "0.5b7" -> "0.5 Beta 7". +std::string Hub::VersionLabel(const std::string& v) { + const size_t at = v.find_first_of("tb"); + if (at == std::string::npos || at == 0 || at + 1 >= v.size()) return v; + return v.substr(0, at) + (v[at] == 't' ? " Test " : " Beta ") + v.substr(at + 1); +} + +int Hub::Items() const { + switch (tab_) { + case kDisplayTab: + case kGameTab: + case kDebugTab: return int(SettingsOn(tab_).size()); + case kSavesTab: return 2 + int(backups_.size()) + int(save_items_.size()); + case kEpisodesTab: return install::kEpisodeCount; + case kTrophiesTab: return int(trophy_items_.size()); + default: return 0; + } +} + +void Hub::ResetSettings() { + for (const Setting& r : Settings()) { + if (IsLanguage(r)) continue; // (a choice, not a recommendation: it stays) + if (IsPreset(r)) continue; // (the rest's defaults are Recommended) + Save(r.key, r.quoted ? std::string("\"") + r.fallback + "\"" : std::string(r.fallback)); + } + // (what frame generation's setting sets with it: off) + Save("gta4_native_frame_generation", "\"off\""); + Save("gta4_native_upscaler", "\"native\""); + Save("gta4_temporal_upscaler_quality", "\"native\""); + value_in_ = 0; + Play(Sound::kSelect); + Toast(T("Settings reset")); + if (display_.high_refresh && display_.high_refresh()) display_.set_high_refresh(false); +} + +void Hub::SwitchRefresh(bool high) { + if (display_.set_high_refresh(high)) { + Toast(high ? T("Now at 120 Hz") : T("Back to 60 Hz")); + return; + } + if (!high) return; + Save("ps5_refresh_rate", "60"); // (it would only be refused again at the next start) + if (Value("ps5_frame_generation", "off") == "60-120") Save("ps5_frame_generation", "\"30-60\""); + value_in_ = 0; + Play(Sound::kError); + Notice(T("Still at 60 Hz"), + T("The TV didn't take 120 Hz. Check that 120 Hz output is on in the PS5's settings (Screen and Video, " + "Video Output) and that the TV runs 120 Hz at the resolution the PS5 sends it. 4K at 120 Hz needs " + "HDMI 2.1.")); +} + +void Hub::Activate(int dir) { + const int item = item_[tab_]; + switch (tab_) { + case kDisplayTab: + case kGameTab: + case kDebugTab: { + const std::vector<int> on = SettingsOn(tab_); + if (item < 0 || item >= int(on.size())) break; + const auto& r = Settings()[size_t(on[size_t(item)])]; + if (IsPreset(r)) { + // Recommended, Performance and Quality in turn (Custom only shows) + const int now = PresetNow(); + ApplyPreset(now == kPresetCustom ? (dir > 0 ? 0 : kPresetCustom - 1) + : (now + dir + kPresetCustom) % kPresetCustom); + value_in_ = 0; + value_dir_ = float(dir); + Play(Sound::kSelect); + break; + } + const int n = int(r.values.size()); + const int idx = (SettingIndex(r) + dir + n) % n; + Save(r.key, r.quoted ? std::string("\"") + r.values[size_t(idx)] + "\"" : std::string(r.values[size_t(idx)])); + if (IsLanguage(r)) lang::Set(lang::FromSetting(r.values[size_t(idx)])); // (the hub, right away) + if (std::string_view(r.key) == "ps5_refresh_rate" && display_.set_high_refresh) { + if (std::string_view(r.values[size_t(idx)]) == "120") { + Confirm(T("Switch to 120 Hz?"), T("The screen goes dark for a moment while the TV changes over."), + T("Switch"), [this] { SwitchRefresh(true); }, T("Later"), + [this] { Toast(T("120 Hz from the next start")); }); + } else { + SwitchRefresh(false); + } + } + if (std::string_view(r.key) == "ps5_refresh_rate" && std::string_view(r.values[size_t(idx)]) == "60" && + Value("ps5_frame_generation", "off") == "60-120") + Save("ps5_frame_generation", "\"30-60\""); // (60 to 120 needs 120 Hz) + if (std::string_view(r.key) == "ps5_frame_generation") { + // Frame generation needs FSR 3.1 reconstruction; its Native preset + // keeps the chosen resolution (anti-aliasing only). 60 to 120 takes + // the TV to 120 Hz; 30 to 60 works at either rate. + const std::string_view mode = r.values[size_t(idx)]; + const bool on = mode != "off"; + Save("gta4_native_frame_generation", on ? "\"fsr3\"" : "\"off\""); + Save("gta4_native_upscaler", on ? "\"fsr3\"" : "\"native\""); + Save("gta4_temporal_upscaler_quality", "\"native\""); + if (mode == "60-120" && Value("ps5_refresh_rate", "60") != "120") { + Save("ps5_refresh_rate", "120"); + if (display_.set_high_refresh) + Confirm(T("Switch to 120 Hz?"), T("The screen goes dark for a moment while the TV changes over."), + T("Switch"), [this] { SwitchRefresh(true); }, T("Later"), + [this] { Toast(T("120 Hz from the next start")); }); + } + } + value_in_ = 0; + value_dir_ = float(dir); + Play(Sound::kSelect); + break; + } + case kEpisodesTab: { + const int e = std::clamp(item, 0, install::kEpisodeCount - 1); + if (state_.episode[e]) { + Play(Sound::kError); + Toast(F("{} is installed", {T(install::EpisodeName(e))})); + break; + } + Play(Sound::kSelect); + OpenInstaller(e); + break; + } + case kSavesTab: { + if (item == 0) { + BackUpSaves(""); + Play(Sound::kSelect); + } else if (item == 1) { + if (!save_import_.run) break; + pending_imports_ = save_import_.pending ? save_import_.pending() : 0; + if (!pending_imports_) { + Play(Sound::kError); + Notice(T("Nothing to import"), T("Copy your Xbox 360 save files into /data/homebrew/PPSA94004/import/ on " + "the PS5 first, then come back here.")); + break; + } + Play(Sound::kSelect); + BackUpSaves(" (before import)"); + const std::string message = save_import_.run(); + pending_imports_ = save_import_.pending ? save_import_.pending() : 0; + LoadSaves(); + Notice(T("Xbox 360 saves"), message); + } else if (item - 2 < int(backups_.size())) { + const Backup b = backups_[size_t(item - 2)]; + Play(Sound::kSelect); + Confirm(T("Restore this backup?"), + F("Your saves are replaced by the ones from {}. The saves you have now are backed up first, so " + "nothing is lost.", + {b.name}), + T("Restore"), [this, b] { RestoreSaves(b); }); + } + break; + } + } +} + +void Hub::Navigate(const Pad& pad) { + const uint32_t p = pad.pressed; + if (leaving_) return; + if (confirm_) { // a question: Cross does it, Circle doesn't (or does its own thing) + if ((p & kCross) && confirm_in_ > 0.6f) { // (not before it has been seen) + auto action = std::move(confirm_); + confirm_ = nullptr; + action(); + } else if ((p & kCircle) && confirm_in_ > 0.6f) { + auto no = std::move(confirm_no_); + confirm_ = nullptr, confirm_no_ = nullptr; + Play(Sound::kBack); + if (no) no(); + } + return; + } + // the installer, over everything + if (installer_) { + InstallerInput(p); + return; + } + // PLAY: the game, once installed; before that, the installer + auto start = [&] { + state_ = install::Look(paths_.app); + if (state_.installed) { + // (the game reads it at its start: gta4_start_episode, -1 the game's own way) + Save("gta4_start_episode", HomeGames().size() > 1 ? std::to_string(HomeGame()) : "-1"); + press_ = 1, launch_ = true; + } else { + Play(Sound::kSelect); + OpenInstaller(); + } + }; + if (p & kOptions) { + start(); + return; + } + // About: Cross held for a few seconds shows (or hides) the Debug page and + // the Display page's rarer settings. + if (tab_ == kAboutTab && (pad.held & kCross)) { + if (!about_hold_done_ && (about_hold_ += dt_) >= kUnlockHold) { + about_hold_done_ = true; + SetDebugOptions(!debug_options_); + } + } else { + about_hold_ = 0, about_hold_done_ = false; + } + if (p & (kL1 | kR1)) { + const int n = TabsShown(); + GoTo((tab_ + ((p & kR1) ? 1 : n - 1)) % n, (p & kR1) ? 1.0f : -1.0f); + Play(Sound::kPage); + return; + } + if ((p & kCircle) && tab_ != kHome) { + GoTo(kHome, -1); + Play(Sound::kBack); + return; + } + if (tab_ == kHome) { + const std::vector<int> games = HomeGames(); + if (games.size() > 1 && (p & (kLeft | kRight))) { + const int at = int(std::find(games.begin(), games.end(), HomeGame()) - games.begin()); + const int to = at + ((p & kRight) ? 1 : -1); + if (to >= 0 && to < int(games.size())) { + home_game_ = games[size_t(to)]; + Play(Sound::kMove); + } else { // (past the last one: a nudge) + home_nudge_ = 0, home_nudge_dir_ = (p & kRight) ? 1 : -1; + } + } + if (p & kCross) start(); + return; + } + const int n = Items(); + if (n == 0) return; + int& sel = item_[tab_]; + const int before = sel; + if (p & kUp) sel = (sel + n - 1) % n; + if (p & kDown) sel = (sel + 1) % n; + if (sel != before) Play(Sound::kMove); + switch (tab_) { + case kDisplayTab: + case kGameTab: + case kDebugTab: + if (p & kTriangle) { + Play(Sound::kSelect); + Confirm(T("Reset all settings?"), + T("Everything on the Display, Game and Debug pages goes back to the recommended settings. The " + "language stays as it is."), + T("Reset"), [this] { ResetSettings(); }); + return; + } + if (p & kLeft) Activate(-1); + if (p & (kRight | kCross)) Activate(1); + if (p & (kLeft | kRight | kCross)) press_ = 0.6f; + break; + case kSavesTab: + case kEpisodesTab: + if (p & kCross) press_ = 1, Activate(0); + break; + } +} + +void Hub::GoTo(int tab, float dir) { + if (tab == tab_) return; + tab_ = tab; + scroll_f_ = -1; + scroll_px_ = 0; + focus_y_ = -1; // (the highlight starts where the page's row is) + page_in_ = 0; + page_dir_ = dir; + panel_in_ = 1; + panel_item_ = item_[tab_]; + // (what these pages show can change while the hub is open: looked at again) + if (tab == kSavesTab) LoadSaves(); + if (tab == kTrophiesTab) LoadTrophies(); + if (tab == kEpisodesTab) LookAgain(); +} + +void Hub::LookAgain() { + if (looking_.valid()) return; // (one at a time) + looking_ = std::async(std::launch::async, [app = paths_.app] { return install::Look(app); }); +} + +bool Hub::Draw(const Pad& pad, float dt) { + if (looking_.valid() && looking_.wait_for(std::chrono::seconds(0)) == std::future_status::ready) state_ = looking_.get(); + if (saves_scan_.valid() && saves_scan_.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + SavesScan scan = saves_scan_.get(); + save_items_ = std::move(scan.saves); + backups_ = std::move(scan.backups); + pending_imports_ = scan.pending; + if (saves_again_) saves_again_ = false, LoadSaves(); + } + if (trophies_load_.valid() && trophies_load_.wait_for(std::chrono::seconds(0)) == std::future_status::ready) + TakeTrophies(trophies_load_.get()); + pictures_this_frame_ = 0; + dt_ = std::clamp(dt, 0.0f, 0.1f); + time_ += dt_; + toast_time_ = std::max(0.0f, toast_time_ - dt_); + // The Refresh Rate at 120: the TV switched once it's open, from here (no + // frame is flipped while the switch measures the vblanks: ps5_display.h). + if (!refresh_checked_ && display_.ready && display_.ready()) { + refresh_checked_ = true; + if (Value("ps5_refresh_rate", "60") == "120" && display_.high_refresh && !display_.high_refresh()) + SwitchRefresh(true); + } + Navigate(pad); + if (launch_ && !leaving_) { + leaving_ = true, press_ = 1; + Play(Sound::kStart); + } + if (item_[tab_] != panel_item_) panel_item_ = item_[tab_], panel_in_ = 0; + Ease(panel_in_, 1, 16); + Ease(value_in_, 1, 18); + Ease(press_, 0, 9); + const ImVec2 size = ImGui::GetIO().DisplaySize; + s_ = std::min(size.x / kW, size.y / kH); + ox_ = (size.x - kW * s_) * 0.5f; + oy_ = (size.y - kH * s_) * 0.5f; + dl_ = ImGui::GetForegroundDrawList(); + Theme(); + DrawBackdrop(); + DrawTopBar(); + // the page comes up quickly (the game's menu just switches), from a touch + // to the side it came from + Ease(page_in_, 1, 24); + const int first = dl_->VtxBuffer.Size; + const float shift = (1 - page_in_) * 14 * page_dir_; + ox_ += shift * s_; + if (installer_) { + DrawInstaller(); + } else { + switch (tab_) { + case kHome: DrawHome(); break; + case kDisplayTab: + case kGameTab: + case kDebugTab: DrawSettings(); break; + case kSavesTab: DrawSaves(); break; + case kEpisodesTab: DrawEpisodes(); break; + case kTrophiesTab: DrawTrophies(); break; + case kAboutTab: DrawAbout(); break; + } + } + ox_ -= shift * s_; + Fade(first, Smooth(page_in_)); + DrawFooter(); + DrawHints(); + DrawConfirm(); + // opening: up from black; PLAY: down to black + const float open = Smooth(time_ / 0.7f); + if (leaving_) leave_ = std::min(1.0f, leave_ + dt_ / 0.6f); + const float black = std::max(1 - open, Smooth(leave_)); + if (black > 0.002f) dl_->AddRectFilled(ImVec2(0, 0), size, Black(black)); + return leaving_ && leave_ >= 1; +} + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.h new file mode 100644 index 00000000..f04227a6 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/hub_ui.h @@ -0,0 +1,460 @@ +// GTA IV PS5 - the hub: the screen the app opens on, before the game +// (the PC launcher's settings, driven by the controller), drawn after GTA IV's +// own pause menu: tabs across the top in its condensed type, the chosen one in +// yellow, rows of settings under them, the hints in the footer. Home, Display, +// Game, Saves, Trophies, About and Debug, and menu music (one song after +// another). +// +// This part is plain ImGui + the standard library, so it also builds on a +// PC for previews (tools/hub_preview). ps5_hub.cpp puts it on the PS5's +// screen, reads the DualSense, plays the music and starts the game. +#pragma once + +#include <future> +#include <cstdint> +#include <filesystem> +#include <functional> +#include <map> +#include <memory> +#include <string> +#include <vector> + +#include "hub_install.h" +#include "hub_style.h" +#include "hub_trophies.h" +#include "imgui.h" + +namespace gta4ps5::hub { + +// The controller, as the hub sees it. +enum Button : uint32_t { + kUp = 1u << 0, + kDown = 1u << 1, + kLeft = 1u << 2, + kRight = 1u << 3, + kCross = 1u << 4, + kCircle = 1u << 5, + kSquare = 1u << 6, + kTriangle = 1u << 7, + kL1 = 1u << 8, + kR1 = 1u << 9, + kOptions = 1u << 10, + kR3 = 1u << 11, +}; + +struct Pad { + uint32_t held = 0; // down now + uint32_t pressed = 0; // down now, not before (with key repeat for the d-pad) +}; + +struct Image { + ImTextureID id = 0; + float width = 0, height = 0; + // the drawing's share of the picture, across and down (a button picture's + // drawing is centred in its square: ps5/tools/hub_buttons.py) + float fill_w = 1, fill_h = 1; + explicit operator bool() const { return id != 0; } +}; + +// The part of a picture that isn't clear (alpha above 8), as fill_w / fill_h. +inline void MeasureFill(Image& img, const unsigned char* rgba, int w, int h) { + int x0 = w, y0 = h, x1 = -1, y1 = -1; + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + if (rgba[(size_t(y) * w + x) * 4 + 3] > 8) { + x0 = x < x0 ? x : x0, x1 = x > x1 ? x : x1; + y0 = y < y0 ? y : y0, y1 = y > y1 ? y : y1; + } + if (x1 >= 0) img.fill_w = float(x1 - x0 + 1) / float(w), img.fill_h = float(y1 - y0 + 1) / float(h); +} + +struct Assets { + ImFont* display = nullptr; // Oswald Bold (the game's overlay and dialogs) + ImFont* nav = nullptr; // Oswald SemiBold + ImFont* body = nullptr; // Inter Regular + ImFont* body_bold = nullptr; // Inter SemiBold + // The hub's type, a Helvetica Condensed (TeX Gyre Heros Cn) as in GTA IV's + // menus: bold for the tabs, rows and titles, regular for text. The atlas is + // baked once (each font at one size) and a font drawn at another size is + // scaled, which pinches small text: so each face is baked at the sizes the + // hub draws it at, and the hub draws from the nearest (AddMenuFonts). + ImFont* menu = nullptr; // (a face's handle: its largest size) + ImFont* menu_regular = nullptr; + std::vector<ImFont*> menu_sizes, menu_regular_sizes; // ascending + // The tabs: Helvetica Neue Heavy (null when the app was built without it: + // the tabs in the menu type), at its sizes. + ImFont* tab = nullptr; + std::vector<ImFont*> tab_sizes; + // The Lost and Damned's scuffs over its tabs (Grunge): white, the alpha how dark. + Image grunge; + Image logo; // the game's logo + Image hero; // the picture behind it all + // Home once an episode is installed: a panel for each game, by the game's + // numbers (0 GTA IV, 1 The Lost and Damned, 2 The Ballad of Gay Tony): its + // picture, a black and white copy, and its logo. hub/home/gta4.jpg (or + // .png), tlad, tbogt, and gta4-logo.png, tlad-logo.png, tbogt-logo.png. + struct HomeArt { + Image picture, grey, logo; + ImU32 color = 0; // the glow's colour: the logo's (Accent), else the picture's + }; + HomeArt home[3]; + std::map<uint32_t, Image> buttons; // the PS5's button pictures, by Button (kLeft | kRight: the d-pad) +}; + +// Adds the menu type (TeX Gyre Heros Cn bold and regular, the files' bytes) +// to the atlas at the hub's sizes, for a 1080p and a 4K screen. +void AddMenuFonts(ImFontAtlas* atlas, const void* bold, int bold_bytes, const void* regular, int regular_bytes, + Assets& a); + +// Adds the tabs' type (Helvetica Neue Heavy, the file's bytes) at the sizes +// the tabs are drawn at. +void AddTabFonts(ImFontAtlas* atlas, const void* data, int bytes, Assets& a); + +// A picture's pixels (RGBA). +struct Pixels { + int w = 0, h = 0; + std::vector<unsigned char> rgba; +}; +// The Lost and Damned's scuffed type: blotches, specks and scratches over the +// width of the tabs (white; the alpha is how much darker), the same each time. +Pixels Grunge(int w, int h); +constexpr int kGrungeW = 2048, kGrungeH = 96; +// At most max_h tall (each pixel the average of those it covers): Home's +// pictures need no more than the 4K screen's band. +Pixels Shrink(const unsigned char* rgba, int w, int h, int max_h); +// Black and white. +Pixels Grey(const Pixels& p); +// The picture's strongest colour, brightened (0 for one with none: black and white). +ImU32 Accent(const Pixels& p); +constexpr int kHomePictureHeight = 1400; + +struct Paths { + std::filesystem::path app; // the app folder (/data/homebrew/PPSA94004) + std::filesystem::path config; // native.toml (the game's settings, which the hub also writes) + std::filesystem::path user_data; // UserData: the saves are the game's content folders in it + std::filesystem::path install; // UserData/LibertyRecomp: the game is installed in its game/ +}; + +// The sounds the hub asks for (ps5_hub.cpp plays them). +enum class Sound { kMove, kSelect, kBack, kPage, kOn, kOff, kStart, kError }; + +// What the hub plays through (none: silent, as in tools/hub_preview). +struct Audio { + std::function<void(Sound)> sfx; + // The menu music: the next song (returns its name; empty: no songs). (Unused: + // songs follow each other on their own.) + std::function<std::string()> next_song; +}; + +// The pages, in the order of the top bar. Display, Game and Debug are the +// settings, by section. Debug is shown only with the debug options (Hub's +// debug_options_). +// [ps5 port] 1.1: the Trophies tab is no longer shown (its trophies are read +// from the game by a separate PS5 app); it stays last, after Debug. +enum Tab { kHome, kDisplayTab, kGameTab, kEpisodesTab, kSavesTab, kAboutTab, kDebugTab, kTrophiesTab, kTabCount }; + +class Hub { + public: + void Init(const Paths& paths, const Assets& assets, std::string version); + void SetAudio(Audio audio) { audio_ = std::move(audio); } + // Draws the hub over the whole display; true when the game should start. + bool Draw(const Pad& pad, float dt); + // From PLAY until Draw returns true: the hub fades out (and its music). + bool Leaving() const { return leaving_; } + // A song started (not shown). + void NowPlaying(const std::string& name); + bool HasMusic() const { return music_; } + // The game is installed (PLAY starts it; else it opens the installer). + bool Installed() const { return state_.installed; } + // The game chosen on Home (0 GTA IV, 1 The Lost and Damned, 2 The Ballad of + // Gay Tony): the hub has its look and plays its menu music, on every page. + int Game() const { return HomeGame(); } + // Installing: the PS5 should stay awake. + bool Busy() const; + void SetHasMusic(bool on) { music_ = on; } + // Settings' Menu Music (hub_music_volume, 50% when not set), 0..1. + float MusicVolume() const; + // What loads a picture (a PNG) for the hub: the trophies' (none: drawn without them). + // Settings' Saves page: Xbox 360 saves waiting in the import folder, and + // importing them (returns a message for the player). + struct SaveImport { + std::function<int()> pending; + std::function<std::string()> run; + }; + void SetSaveImport(SaveImport import) { save_import_ = std::move(import); } + // Display's Refresh Rate, switched as soon as it's chosen: whether the TV is + // at 120 Hz, and the switch (false: it stayed where it was). + struct Display { + std::function<bool()> high_refresh; + std::function<bool(bool)> set_high_refresh; + std::function<bool()> vrr; // (the 120 Hz output a VRR one) + std::function<bool()> ready; // (the TV open: a switch can be made) + }; + void SetDisplay(Display display) { display_ = std::move(display); } + void SetImageLoader(std::function<Image(const std::filesystem::path&)> load) { load_image_ = std::move(load); } + + struct Setting; + + private: + struct SaveItem { // a content folder of the game's (a save) + std::string name, kind; + std::filesystem::path folder; + uint32_t files = 0; + uint64_t bytes = 0; + int64_t modified = 0; // unix time + }; + struct Backup { + std::string name; + uint32_t files = 0; + uint64_t bytes = 0; + }; + // The Saves page's lists, made off the hub's thread: every save and backup + // folder walked for its size (a backup is a whole copy of the saves), which + // held the hub up each time the page came by. + struct SavesScan { + std::vector<SaveItem> saves; + std::vector<Backup> backups; + int pending = 0; // Xbox 360 saves waiting in import/ + }; + static SavesScan ScanSaves(const Paths& paths, const std::function<int()>& pending); + std::future<SavesScan> saves_scan_; + int pictures_this_frame_ = 0; // trophy pictures loaded this frame + bool saves_again_ = false; // (asked for again while one was being made) + // A row of a list: an item, or a heading over the next ones. + struct Row { + int item = -1; // -1: a heading + std::string heading; + float y = 0; + }; + + void Load(); + void LoadSaves(); + std::string Value(const std::string& key, const std::string& fallback) const; + void Save(const std::string& key, const std::string& value); + int SettingIndex(const Setting& r) const; + // The Display page's presets (the Preset row): which one every setting it + // sets matches (kPresetCustom: none), and choosing one. + static constexpr int kPresetCustom = 3; + int PresetNow() const; + void ApplyPreset(int preset); + + // pages + void DrawBackdrop(); + void DrawTopBar(); + void DrawFooter(); + void DrawHints(); + void DrawConfirm(); + void DrawHome(); + // Home: the game PLAY starts in, by the game's numbers (gta4_start_episode): + // 0 GTA IV, 1 The Lost and Damned, 2 The Ballad of Gay Tony. A choice once + // an episode is installed; kept in native.toml for the next time. + int home_game_ = 0; // (GTA IV at each start) + float home_wide_[3] = {1, 0, 0}; // how far each pillar is open (the chosen one) + float home_from_[3] = {1, 0, 0}; // (where each was when the choice was made) + int home_shown_ = 0; // (the choice the pillars are opening to) + float home_switch_ = 1; // how far the choice has opened (0 to 1, over kSwitch) + float home_pop_ = 0; // the choice's flash (1, fading) + float home_glare_ = 0; // seconds since the choice (the glare's sweeps) + float home_nudge_ = 1; // a press past the last pillar (0 to 1) + int home_nudge_dir_ = 0; + std::vector<int> HomeGames() const; // GTA IV and the episodes installed, left to right + int HomeGame() const; // home_game_, or GTA IV if it's gone + void DrawHomePanels(const std::vector<int>& games); + void DrawHomePanel(int game, float x0, float x1, float top, float bottom, float lean, float on); + void FillPoly(const std::vector<ImVec2>& poly, const std::function<ImU32(ImVec2)>& color); + void Quad(ImVec2 tl, ImVec2 tr, ImVec2 br, ImVec2 bl, ImU32 ctl, ImU32 ctr, ImU32 cbr, ImU32 cbl); + void DrawSettings(); + void DrawSaves(); + void DrawEpisodes(); + void DrawAbout(); + // The settings on a page (Display, Game, Debug): indices into Settings(). + std::vector<int> SettingsOn(int tab) const; + // The debug options (hub_debug_options): the Debug page and the Display + // page's rarer settings, shown by holding Cross on About for a few seconds + // (and hidden again the same way). Hidden, they keep their recommended values. + bool debug_options_ = false; + float about_hold_ = 0; // how long Cross has been held on About + bool about_hold_done_ = false; // (switched: not again until it's let go) + static constexpr float kUnlockHold = 3, kUnlockShow = 0.6f; + int TabsShown() const { return debug_options_ ? kDebugTab + 1 : kDebugTab; } // (Debug the last shown) + bool Shown(const Setting& r) const; + void KeepHiddenSettings(); + void SetDebugOptions(bool on); + static std::string VersionLabel(const std::string& v); + static bool IsSettingsTab(int tab) { return tab == kDisplayTab || tab == kGameTab || tab == kDebugTab; } + // the hub's two faces (the overlay's when the menu type didn't load) + ImFont* Bold() const { return a_.menu ? a_.menu : a_.body_bold; } + ImFont* Regular() const { return a_.menu_regular ? a_.menu_regular : a_.body; } + // A title over a page of its own (the installer): yellow, as the game's. + void Heading(const std::string& title, const std::string& subtitle); + // the installer (hub_installer_page.cpp): over the pages, from PLAY + // while the game isn't installed + // The installer: for the game, or (0, 1) one of the episodes. + void OpenInstaller(int episode = -1); + void CloseInstaller(); + void DrawInstaller(); + void InstallerInput(uint32_t pressed); + void InstallerHints(float right); + void Row3(float y, const char* label, const std::string& name, int status, const std::string& note); + void Spinner(float cx, float cy, float r, ImU32 col); + void Tick(float cx, float cy, float size, ImU32 col); + void XMark(float cx, float cy, float size, ImU32 col); + // the Trophies page (hub_trophies_page.cpp) + struct TrophyItem { + int index; // in trophies_.trophies; -1: the platinum + int group; // 0 earned, 1 not yet + }; + // The trophies read again (when the page comes by), off the hub's thread: + // the page takes them when they're there (TakeTrophies). + void LoadTrophies(); + void TakeTrophies(trophies::List list); + std::future<trophies::List> trophies_load_; + void DrawTrophies(); + float TrophySummary(); // returns the y under it + void TrophyIcon(int index, float x, float y, float size, float over); + Image TrophyPicture(int index); + void Empty(const char* title, const std::string& text); + void GoTo(int tab, float dir); + // A scrolled list with headings at the page's left: rows' items are + // 0..n-1 in order; `draw` draws item i's row at y (`over`: how much the + // focus highlight covers it, 0..1). + void List(std::vector<Row>& rows, int sel, float rh, const std::function<void(int, float, float)>& draw); + // The details panel at the right; `draw` from its top, faded in. + void Panel(const std::function<void(float x, float y, float w)>& draw); + // Label: value lines of the panel; returns the y after them. + float Facts(float x, float y, float w, const std::vector<std::pair<std::string, std::string>>& facts); + + void Navigate(const Pad& pad); + int Items() const; + void Activate(int dir); + void Play(Sound s) { + if (audio_.sfx) audio_.sfx(s); + } + void Toast(std::string text) { + toast_ = std::move(text); + toast_time_ = 3; + toast_song_ = false; + } + void Confirm(std::string title, std::string text, std::string yes, std::function<void()> action) { + confirm_title_ = std::move(title), confirm_text_ = std::move(text), confirm_yes_ = std::move(yes); + confirm_ = std::move(action); + confirm_no_ = nullptr; + confirm_no_label_.clear(); + confirm_in_ = 0; + } + // ...with Circle doing something too (`no_label` in the hints instead of Cancel). + void Confirm(std::string title, std::string text, std::string yes, std::function<void()> action, std::string no_label, + std::function<void()> no_action) { + Confirm(std::move(title), std::move(text), std::move(yes), std::move(action)); + confirm_no_label_ = std::move(no_label); + confirm_no_ = std::move(no_action); + } + // A message (Cross closes it). + void Notice(std::string title, std::string text) { Confirm(std::move(title), std::move(text), "OK", [] {}); } + void BackUpSaves(const std::string& why); + // Every setting (Display, Game and Debug) back to the recommended one. + void ResetSettings(); + // The TV to 120 Hz or back to 60, now, and what came of it. + void SwitchRefresh(bool high); + // The Refresh Rate at 120: switched at the hub's first frames (Draw). + bool refresh_checked_ = false; + void RestoreSaves(const Backup& b); + + Paths paths_; + Assets a_; + Audio audio_; + std::string version_; + // layout: virtual 1920 x 1080, scaled to the display + float s_ = 1, ox_ = 0, oy_ = 0; + ImDrawList* dl_ = nullptr; + float time_ = 0, dt_ = 0; + + int tab_ = 0; + int item_[8] = {}; // the focused row on each page + // trophies + trophies::List trophies_; + std::vector<TrophyItem> trophy_items_; + std::map<uint32_t, Image> trophy_pictures_; // by image ID (0: the game's own picture, the platinum's) + std::function<Image(const std::filesystem::path&)> load_image_; + bool launch_ = false; + std::string toast_; + float toast_time_ = 0; + bool toast_song_ = false; // (the toast is a song's name) + std::string pending_song_; // (shown once the hub has faded in) + bool music_ = false; + // a question (restore a backup?): Cross does it, Circle doesn't (or does confirm_no_) + std::function<void()> confirm_, confirm_no_; + std::string confirm_title_, confirm_text_, confirm_yes_, confirm_no_label_; + float confirm_in_ = 0; + // animation (eased toward their targets) + float tab_x_ = -1, tab_w_ = 0, focus_y_ = -1, shade_ = 0.3f, page_in_ = 1; + float tab_lit_[kTabCount] = {}; // each tab's yellow, easing in and out + float page_dir_ = 1; // the page slides in from this side (+1: the right) + float panel_in_ = 1; // the details panel, after the focus moves + int panel_item_ = -1; + float value_in_ = 1, value_dir_ = 0; // a value, after it changes + float press_ = 0; // a flash on the focused thing when it's pressed + float hero_x_ = -1; // the picture drifts sideways with the pages + // Each game's share of the look (easing to the one chosen on Home), and + // the colours mixed from them for this frame. + float theme_[3] = {1, 0, 0}; + Palette pal_ = GamePalette(0); + void Theme(); + void DrawTabs(int game, float alpha); + float ToastShown() const; // how much the footer's message shows (0..1) + bool leaving_ = false; // PLAY: fading out + float leave_ = 0; + float scroll_f_ = 0, scroll_px_ = 0; // the page's list: its scroll, shown and wanted + + std::vector<std::pair<std::string, std::string>> config_; // top-level key = value + std::vector<SaveItem> save_items_; + SaveImport save_import_; + int pending_imports_ = 0; + Display display_; + // the game's files + install::State state_; + // the installer + std::unique_ptr<install::Job> job_; + bool installer_ = false; + install::Job::Phase job_phase_ = install::Job::Phase::kChecking; + // The game's files looked at again, off the hub's thread (the drive can be + // slow): state_ takes the answer when it's there. + std::future<install::State> looking_; + void LookAgain(); + int installer_episode_ = -1; // what the installer installs: the game (-1) or an episode + float shown_progress_ = 0; + uint64_t free_space_ = 0; + install::Drive drive_; // where the game installs + std::filesystem::path InstallRoot() const; + std::vector<Backup> backups_; + // helpers (virtual coordinates) + ImVec2 P(float x, float y) const { return ImVec2(ox_ + x * s_, oy_ + y * s_); } + // The face to draw `f` at `size` with: of the menu type's sizes, the nearest + // baked one at or above it on the screen (one that has all of `t`'s letters). + ImFont* Pick(ImFont* f, float size, const std::string* t = nullptr) const; + float Ease(float& v, float target, float speed = 16); + void Text(ImFont* f, float size, float x, float y, ImU32 col, const std::string& t, float align = 0, + float spacing = 0); + float TextWidth(ImFont* f, float size, const std::string& t, float spacing = 0) const; + // `t` cut to fit `width` (with "..."). + std::string Fit(ImFont* f, float size, const std::string& t, float width) const; + std::vector<std::string> WrapLines(ImFont* f, float size, float width, const std::string& t); + float Wrapped(ImFont* f, float size, float x, float y, float width, ImU32 col, const std::string& t, + float line = 1.4f); + void Rect(float x, float y, float w, float h, ImU32 col, float r = 0); + // Text with its capitals centred on `cy` (Text's y is the top of its line). + void TextMid(ImFont* f, float size, float x, float cy, ImU32 col, const std::string& t, float align = 0, + float spacing = 0); + float CapMid(ImFont* f, float size) const; // the capitals' middle, below the line's top + float Baseline(ImFont* f, float size) const; // the baseline, below the line's top + // A PlayStation button centred on (cx, cy), its picture's square `size` + // across; returns the width of the button drawn in it. + float Glyph(uint32_t button, float cx, float cy, ImU32 col, float size); + float GlyphWidth(uint32_t button, float size) const; + // One hint in the footer, right-aligned at `right` on line `line` (0, 1): + // the button, then what it does. Returns its left edge. + float Hint(float right, uint32_t button, const char* label, int line = 0); + void Fade(int first_vertex, float alpha); // fades what was drawn since +}; + +} // namespace gta4ps5::hub diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.cpp new file mode 100644 index 00000000..bdfaf426 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.cpp @@ -0,0 +1,461 @@ +// GTA IV PS5 - the hub on the console (see hub_ui.h): +// shown before the game is set up (ReXApp::OnFinalizePaths, asynchronous), +// driven by the DualSense through scePad, with each game's menu music +// (hub/music) and the menu sounds (hub/sfx), and PLAY resumes the start. +#if defined(__PROSPERO__) + +#include "ps5_hub.h" + +#include <algorithm> +#include <array> +#include <chrono> +#include <cmath> +#include <cstring> +#include <memory> +#include <string> +#include <vector> + +#include <rex/logging.h> +#include <rex/ui/imgui_dialog.h> +#include <rex/ui/imgui_drawer.h> +#include <rex/ui/immediate_drawer.h> +#include <rex/ui/pad_dialog_style.h> + +#include "hub_sfx.h" +#include "hub_ui.h" +#include "../game/sound.h" +#include "imgui.h" +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#define STBI_ONLY_PNG +#define STBI_ONLY_JPEG +#include "stb_image.h" + +extern "C" { +int sceUserServiceInitialize(void* params); +int sceUserServiceGetInitialUser(int32_t* user_id); +int scePadInit(void); +int scePadOpen(int32_t user_id, int32_t type, int32_t index, const void* param); +int scePadGetHandle(int32_t user_id, int32_t type, int32_t index); +int scePadReadState(int32_t handle, void* data); +int sceSystemServicePowerTick(void); +} + +// The fonts, built into the app (fonts/: Oswald and Inter under the SIL Open +// Font License, TeX Gyre Heros Cn under the GUST Font License; Helvetica Neue +// Heavy, the tabs, when the build has it: GTA4PS5_TAB_FONT). +#define SVR_INCBIN(name, file) \ + __asm__(".section .rodata\n.global " #name "\n.balign 16\n" #name ":\n.incbin \"" \ + file "\"\n.global " #name "_end\n" #name "_end:\n.byte 0\n.previous\n"); \ + extern "C" const unsigned char name[]; \ + extern "C" const unsigned char name##_end[]; +SVR_INCBIN(gta4ps5_font_oswald_bold, GTA4PS5_FONTS_DIR "/oswald-latin-700-normal.ttf") +SVR_INCBIN(gta4ps5_font_oswald, GTA4PS5_FONTS_DIR "/oswald-latin-600-normal.ttf") +SVR_INCBIN(gta4ps5_font_inter, GTA4PS5_FONTS_DIR "/inter-latin-400-normal.ttf") +SVR_INCBIN(gta4ps5_font_inter_bold, GTA4PS5_FONTS_DIR "/inter-latin-600-normal.ttf") +SVR_INCBIN(gta4ps5_font_menu, GTA4PS5_FONTS_DIR "/texgyreheroscn-bold.otf") +SVR_INCBIN(gta4ps5_font_menu_regular, GTA4PS5_FONTS_DIR "/texgyreheroscn-regular.otf") +#if GTA4PS5_TAB_FONT +SVR_INCBIN(gta4ps5_font_tab, GTA4PS5_FONTS_DIR "/HelveticaNeue-Heavy.ttf") +#endif + +namespace gta4ps5::hub { +namespace { + +namespace fs = std::filesystem; + +constexpr uint32_t kPadR3 = 0x00000004u, kPadOptions = 0x00000008u, kPadUp = 0x00000010u, kPadRight = 0x00000020u, + kPadDown = 0x00000040u, kPadLeft = 0x00000080u, kPadL1 = 0x00000400u, kPadR1 = 0x00000800u, + kPadTriangle = 0x00001000u, kPadCircle = 0x00002000u, kPadCross = 0x00004000u, + kPadSquare = 0x00008000u; +struct PadData { + uint32_t buttons; + uint8_t lx, ly, rx, ry, l2, r2; + uint16_t padding; + uint8_t rest[240]; +}; + +Assets g_assets; +Hub::SaveImport g_save_import; +Hub::Display g_display; +// The hub's pictures: kept for the app's life. (The dialog is deleted right +// after the frame START GAME was pressed on, and that frame's draw list, +// rendered afterwards, still uses them.) +std::vector<std::unique_ptr<rex::ui::ImmediateTexture>>* g_textures = + new std::vector<std::unique_ptr<rex::ui::ImmediateTexture>>(); + +// The pad: a handle of our own (the input driver later gets the same one). +class Pad5 { + public: + Pad read() { + if (handle_ < 0) { + sceUserServiceInitialize(nullptr); + scePadInit(); + int32_t user = -1; + if (sceUserServiceGetInitialUser(&user) == 0 && user >= 0) { + handle_ = scePadOpen(user, 0, 0, nullptr); + if (handle_ < 0) handle_ = scePadGetHandle(user, 0, 0); + } + if (handle_ < 0) return {}; + } + PadData d{}; + if (scePadReadState(handle_, &d) != 0) return {}; + uint32_t b = 0; + if ((d.buttons & kPadUp) || d.ly < 64) b |= kUp; + if ((d.buttons & kPadDown) || d.ly > 192) b |= kDown; + if ((d.buttons & kPadLeft) || d.lx < 64) b |= kLeft; + if ((d.buttons & kPadRight) || d.lx > 192) b |= kRight; + if (d.buttons & kPadCross) b |= kCross; + if (d.buttons & kPadCircle) b |= kCircle; + if (d.buttons & kPadSquare) b |= kSquare; + if (d.buttons & kPadTriangle) b |= kTriangle; + if (d.buttons & kPadL1) b |= kL1; + if (d.buttons & kPadR1) b |= kR1; + if (d.buttons & kPadOptions) b |= kOptions; + if (d.buttons & kPadR3) b |= kR3; + Pad p; + p.held = b; + p.pressed = b & ~last_; + // (held directions repeat: after 0.3 s, every 0.06 s) + const auto now = std::chrono::steady_clock::now(); + constexpr uint32_t kDirs = kUp | kDown | kLeft | kRight; + if (b & kDirs) { + if ((b & kDirs) != (last_ & kDirs)) { + repeat_at_ = now + std::chrono::milliseconds(300); + } else if (now >= repeat_at_) { + p.pressed |= b & kDirs; + repeat_at_ = now + std::chrono::milliseconds(60); + } + } + last_ = b; + // The first read: ignore what was already held (the button that started the app). + if (first_) { + first_ = false; + p.pressed = 0; + } + return p; + } + + private: + int32_t handle_ = -1; + uint32_t last_ = 0; + bool first_ = true; + std::chrono::steady_clock::time_point repeat_at_{}; +}; + +// The menu sounds: three recordings in hub/sfx (click, tab and confirm, as +// .ogg, .wav, .mp3 or .flac), decoded once and kept in memory. Moving, choosing +// and changing a setting click; L1 and R1 beep; PLAY (and choosing the game on +// Home) confirms. Nothing else makes a sound. +class MenuClips { + public: + explicit MenuClips(const fs::path& hub) { + const char* names[] = {"click", "tab", "confirm"}; + for (int c = 0; c < 3; ++c) clips_[c] = Decode(hub / "sfx", names[c]); + } + std::shared_ptr<const std::vector<float>> For(Sound s) const { + switch (s) { + case Sound::kPage: return clips_[1]; + case Sound::kStart: return clips_[2]; + case Sound::kError: return nullptr; + default: return clips_[0]; + } + } + + private: + static std::shared_ptr<const std::vector<float>> Decode(const fs::path& dir, const char* name) { + for (const char* ext : {".ogg", ".wav", ".mp3", ".flac"}) { + const fs::path file = dir / (std::string(name) + ext); + std::error_code ec; + if (!fs::is_regular_file(file, ec)) continue; + std::unique_ptr<gta4ps5::sound::Source> src = gta4ps5::sound::Open(file); + if (!src) break; + auto lr = std::make_shared<std::vector<float>>(); + std::vector<float> chunk(4096 * 2); + while (const size_t n = src->Read(chunk.data(), 4096)) lr->insert(lr->end(), chunk.begin(), chunk.begin() + n * 2); + return lr; + } + REXLOG_WARN("PS5 hub: no {} sound in {}", name, dir.string()); + return nullptr; + } + std::array<std::shared_ptr<const std::vector<float>>, 3> clips_; +}; + +// The hub's sound (ps5/sound.h): each game's menu song (hub/music: "GTA IV +// Menu", "The Lost and Damned Menu", "The Ballad of Gay Tony Menu", any of the +// four types), all three playing from the start and looping, the one of the +// game chosen on Home heard: moving to another fades it over without starting +// either song again. A game without its song has GTA IV's. And the menu +// sounds (MenuClips). +class HubAudio { + public: + HubAudio(const fs::path& hub, float level) : level_(level), clips_(hub) { + if (!gta4ps5::sound::StartOutput()) REXLOG_WARN("PS5 hub: no audio port for the hub's sounds"); + for (auto& v : sfx_) v = gta4ps5::sound::NewVoice(); + const char* songs[3] = {"GTA IV Menu", "The Lost and Damned Menu", "The Ballad of Gay Tony Menu"}; + for (int g = 0; g < 3; ++g) { + for (const char* ext : {".mp3", ".ogg", ".flac", ".wav"}) { + const fs::path file = hub / "music" / (std::string(songs[g]) + ext); + std::error_code ec; + if (fs::is_regular_file(file, ec)) { + names_[g] = file.stem().string(); + if (auto src = gta4ps5::sound::Open(file)) { + music_[g] = gta4ps5::sound::NewVoice(); + music_[g]->SetVolume(0); + music_[g]->Play(std::move(src), true); + } + break; + } + } + REXLOG_INFO("PS5 hub: {}: {}", songs[g], music_[g] ? "playing" : "none"); + } + } + bool HasMusic() const { return music_[0] || music_[1] || music_[2]; } + std::string Current() const { return names_[Heard(game_)]; } + + void Sfx(Sound s) { + auto clip = clips_.For(s); + if (!clip) return; + auto* v = sfx_[next_++ % sfx_.size()]; // (a few at once: quick presses overlap) + v->SetVolume(1); + v->Play(gta4ps5::sound::Memory(std::move(clip)), false); + } + + // (each game has one song now: no next one) + std::string Next() { return {}; } + + // Every frame, with the game chosen on Home: its song up to the Menu Music + // level, the others down, over about half a second. + void Tick(float dt, int game) { + game_ = game; + const int heard = Heard(game); + for (int g = 0; g < 3; ++g) { + if (!music_[g]) continue; + const float target = !leaving_ && g == heard ? level_ : 0.0f; + gain_[g] += (target - gain_[g]) * (1.0f - std::exp(-dt * 7.0f)); + if (std::fabs(target - gain_[g]) < 0.002f) gain_[g] = target; + music_[g]->SetVolume(gain_[g]); + } + } + + // Settings' Menu Music level (0..1): the song follows it right away. + void SetLevel(float level) { level_ = level; } + + void FadeOut() { + leaving_ = true; + for (auto* v : music_) + if (v) v->FadeOut(0.6f); + } + + private: + int Heard(int game) const { return game >= 0 && game < 3 && music_[game] ? game : 0; } + + float level_ = 0.6f; + MenuClips clips_; + std::array<gta4ps5::sound::Voice*, 3> music_{}; + std::array<float, 3> gain_{}; + std::array<std::string, 3> names_; + int game_ = 0; + std::array<gta4ps5::sound::Voice*, 6> sfx_{}; + size_t next_ = 0; + bool leaving_ = false; +}; + +// The menu sounds for the game's own dialogs (the on-screen keyboard), after +// the hub has gone: the hub's (MenuClips), a few voices of their own. +class MenuSfx { + public: + explicit MenuSfx(const fs::path& hub) : clips_(hub) { + for (auto& v : voices_) v = gta4ps5::sound::NewVoice(); + } + void Play(int k) { + if (k < 0 || k > int(Sound::kError)) return; + auto clip = clips_.For(Sound(k)); + if (!clip) return; + auto* v = voices_[next_++ % voices_.size()]; + v->SetVolume(0.85f); + v->Play(gta4ps5::sound::Memory(std::move(clip)), false); + } + + private: + MenuClips clips_; + std::array<gta4ps5::sound::Voice*, 4> voices_{}; + size_t next_ = 0; +}; + +// The game's on-screen keyboard in the hub's style (rex/ui/pad_dialog_style.h). +void StyleGameDialogs(const Assets& a, const fs::path& hub) { + static MenuSfx* sfx = new MenuSfx(hub); // (for the app's life) + rex::ui::PadDialogStyle st; + st.title = a.display; + st.body = a.body; + st.bold = a.body_bold; + const std::pair<uint32_t, uint32_t> map[] = {{kCross, 0x1000}, {kCircle, 0x2000}, {kSquare, 0x4000}, + {kTriangle, 0x8000}, {kL1, 0x0100}, {kR1, 0x0200}, + {kOptions, 0x0010}, {kLeft | kRight, 0x000C}}; + for (const auto& [hub, xbox] : map) + if (auto it = a.buttons.find(hub); it != a.buttons.end() && it->second) st.buttons[xbox] = it->second.id; + st.sound = [](int k) { sfx->Play(k); }; + rex::ui::SetPadDialogStyle(std::move(st)); +} + +class HubDialog final : public rex::ui::ImGuiDialog { + public: + HubDialog(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, const Paths& paths, + std::function<void()> launch) + : ImGuiDialog(drawer), launch_(std::move(launch)) { + const fs::path hub = paths.app / "hub"; + Assets a = g_assets; + a.logo = Load(immediate, hub / "logo.png"); + a.hero = Load(immediate, hub / "hero.jpg"); + if (!a.hero) a.hero = Load(immediate, hub / "hero.png"); + // Home's panels (hub/home; GTA IV's: the hero and the logo without them) + const char* games[] = {"gta4", "tlad", "tbogt"}; + for (int g = 0; g < 3; ++g) { + auto& art = a.home[g]; + // (the logo first: its colour is the glow's, the picture's without one) + art.logo = Load(immediate, hub / "home" / (std::string(games[g]) + "-logo.png"), &art.color); + for (const char* ext : {".jpg", ".png"}) + if (!art.picture) LoadHomePicture(immediate, hub / "home" / (std::string(games[g]) + ext), art); + } + if (!a.home[0].picture) { + LoadHomePicture(immediate, hub / "hero.jpg", a.home[0]); + if (!a.home[0].picture) LoadHomePicture(immediate, hub / "hero.png", a.home[0]); + } + if (!a.home[0].logo) a.home[0].logo = a.logo; + // The Lost and Damned's scuffs (made here) + a.grunge = Texture(immediate, Grunge(kGrungeW, kGrungeH)); + // the button pictures (hub/buttons/<name>.png; drawn ones where missing) + const std::pair<uint32_t, const char*> names[] = { + {kCross, "Cross"}, {kCircle, "Circle"}, {kSquare, "Square"}, {kTriangle, "Triangle"}, {kL1, "L1"}, + {kR1, "R1"}, {kOptions, "Options"}, {kLeft | kRight, "D-Pad"}, {kR3, "R3"}}; + for (const auto& [b, n] : names) + if (Image img = Load(immediate, hub / "buttons" / (std::string(n) + ".png"))) a.buttons[b] = img; + hub_.SetImageLoader([this, immediate](const fs::path& file) { return Load(immediate, file); }); + hub_.SetSaveImport(g_save_import); + hub_.SetDisplay(g_display); + hub_.Init(paths, a, GTA4PS5_VERSION); + StyleGameDialogs(a, hub); + audio_ = std::make_unique<HubAudio>(hub, hub_.MusicVolume()); + Audio hooks; + hooks.sfx = [this](Sound s) { audio_->Sfx(s); }; + hooks.next_song = [this] { return audio_->Next(); }; + hub_.SetAudio(std::move(hooks)); + hub_.SetHasMusic(audio_->HasMusic()); + hub_.NowPlaying(audio_->Current()); + } + + protected: + void OnDraw(ImGuiIO& io) override { + if (done_) return; + const Pad pad = pad_.read(); + audio_->SetLevel(hub_.MusicVolume()); + audio_->Tick(io.DeltaTime, hub_.Game()); + const bool go = hub_.Draw(pad, io.DeltaTime); + // installing: the PS5 stays awake (no rest mode, no screen dimming) + if (hub_.Busy() && (awake_ -= io.DeltaTime) <= 0) { + sceSystemServicePowerTick(); + awake_ = 10; + } + if (hub_.Leaving() && !faded_) audio_->FadeOut(), faded_ = true; + if (go) { + done_ = true; + Close(); + launch_(); + } + } + + private: + Image Texture(rex::ui::ImmediateDrawer* immediate, const Pixels& p) { + auto tex = immediate->CreateTexture(uint32_t(p.w), uint32_t(p.h), rex::ui::ImmediateTextureFilter::kLinear, false, + p.rgba.data()); + if (!tex) return {}; + Image img{reinterpret_cast<ImTextureID>(tex.get()), float(p.w), float(p.h)}; + g_textures->push_back(std::move(tex)); + return img; + } + + // A Home picture, no taller than the 4K screen's band, its black and white copy and its colour. + void LoadHomePicture(rex::ui::ImmediateDrawer* immediate, const fs::path& file, Assets::HomeArt& art) { + if (!immediate) return; + int w = 0, h = 0, n = 0; + unsigned char* px = stbi_load(file.string().c_str(), &w, &h, &n, 4); + if (!px) return; + const Pixels p = Shrink(px, w, h, kHomePictureHeight); + stbi_image_free(px); + art.picture = Texture(immediate, p); + if (art.picture) art.grey = Texture(immediate, Grey(p)); + if (art.picture && !art.color) art.color = Accent(p); + REXLOG_INFO("PS5 hub: Home picture {} ({}x{}, shown at {}x{})", file.filename().string(), w, h, p.w, p.h); + } + + // (accent: the picture's colour (Accent), when asked for) + Image Load(rex::ui::ImmediateDrawer* immediate, const fs::path& file, ImU32* accent = nullptr) { + if (!immediate) return {}; + int w = 0, h = 0, n = 0; + unsigned char* px = stbi_load(file.string().c_str(), &w, &h, &n, 4); + if (!px) return {}; + auto tex = immediate->CreateTexture(uint32_t(w), uint32_t(h), rex::ui::ImmediateTextureFilter::kLinear, false, px); + Image img{reinterpret_cast<ImTextureID>(tex.get()), float(w), float(h)}; + MeasureFill(img, px, w, h); + if (accent) { + Pixels p; + p.w = w, p.h = h; + p.rgba.assign(px, px + size_t(w) * size_t(h) * 4); + *accent = Accent(p); + } + stbi_image_free(px); + if (!tex) return {}; + g_textures->push_back(std::move(tex)); + return img; + } + + Hub hub_; + Pad5 pad_; + std::unique_ptr<HubAudio> audio_; // (its voices outlive it: the last sounds finish in the game) + bool faded_ = false; + bool done_ = false; + float awake_ = 0; + std::function<void()> launch_; +}; + +ImFont* AddFont(ImFontAtlas* atlas, const unsigned char* begin, const unsigned char* end, float size) { + ImFontConfig cfg; + cfg.FontDataOwnedByAtlas = false; // (built into the app) + cfg.OversampleH = 2; + return atlas->AddFontFromMemoryTTF(const_cast<unsigned char*>(begin), int(end - begin), size, &cfg); +} + +} // namespace + +void ConfigureFonts(ImFontAtlas* atlas) { + // Sized for the 4K screen (the hub scales them to the display). + g_assets.display = AddFont(atlas, gta4ps5_font_oswald_bold, gta4ps5_font_oswald_bold_end, 72.0f * 2); + g_assets.nav = AddFont(atlas, gta4ps5_font_oswald, gta4ps5_font_oswald_end, 34.0f * 2); + g_assets.body = AddFont(atlas, gta4ps5_font_inter, gta4ps5_font_inter_end, 26.0f * 2); + g_assets.body_bold = AddFont(atlas, gta4ps5_font_inter_bold, gta4ps5_font_inter_bold_end, 26.0f * 2); + // the hub's menu type (GTA IV's: a Helvetica Condensed), at its sizes + AddMenuFonts(atlas, gta4ps5_font_menu, int(gta4ps5_font_menu_end - gta4ps5_font_menu), gta4ps5_font_menu_regular, + int(gta4ps5_font_menu_regular_end - gta4ps5_font_menu_regular), g_assets); +#if GTA4PS5_TAB_FONT + // the tabs (Helvetica Neue Heavy) + AddTabFonts(atlas, gta4ps5_font_tab, int(gta4ps5_font_tab_end - gta4ps5_font_tab), g_assets); +#endif +} + +const Assets& Fonts() { return g_assets; } + +void SetSaveImport(Hub::SaveImport import) { g_save_import = std::move(import); } +void SetDisplay(Hub::Display display) { g_display = std::move(display); } + +const char* Version() { return GTA4PS5_VERSION; } + +void Show(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, const Paths& paths, + std::function<void()> launch) { + REXLOG_INFO("PS5 hub: shown (app folder {})", paths.app.string()); + new HubDialog(drawer, immediate, paths, std::move(launch)); // (the dialog owns itself) +} + +} // namespace gta4ps5::hub + +#endif // __PROSPERO__ diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.h new file mode 100644 index 00000000..4d7f0937 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/ps5_hub.h @@ -0,0 +1,30 @@ +// GTA IV PS5 - the PS5 hub on the console (ps5_hub.cpp). +#pragma once + +#if defined(__PROSPERO__) +#include <functional> + +#include "hub_ui.h" + +struct ImFontAtlas; +namespace rex::ui { +class ImGuiDrawer; +class ImmediateDrawer; +} // namespace rex::ui + +namespace gta4ps5::hub { +// The hub's fonts (from OnConfigureFonts). +void ConfigureFonts(ImFontAtlas* atlas); +// The hub's fonts, for the port's things in the game (the trophy popup). +const Assets& Fonts(); +// Shows the hub; `launch` runs (on the UI thread) on START GAME. +void Show(rex::ui::ImGuiDrawer* drawer, rex::ui::ImmediateDrawer* immediate, const Paths& paths, + std::function<void()> launch); +// Xbox 360 save import on the Saves page (before Show). +void SetSaveImport(Hub::SaveImport import); +// The Refresh Rate's switch on the Display page (before Show). +void SetDisplay(Hub::Display display); +// The port's version ("1.0t1": the hub shows "1.0 Test 1"), from ps5/VERSION.txt. +const char* Version(); +} // namespace gta4ps5::hub +#endif diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/stb_vorbis.c b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/stb_vorbis.c new file mode 100644 index 00000000..3e5c2504 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/hub/stb_vorbis.c @@ -0,0 +1,5584 @@ +// Ogg Vorbis audio decoder - v1.22 - public domain +// http://nothings.org/stb_vorbis/ +// +// Original version written by Sean Barrett in 2007. +// +// Originally sponsored by RAD Game Tools. Seeking implementation +// sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker, +// Elias Software, Aras Pranckevicius, and Sean Barrett. +// +// LICENSE +// +// See end of file for license information. +// +// Limitations: +// +// - floor 0 not supported (used in old ogg vorbis files pre-2004) +// - lossless sample-truncation at beginning ignored +// - cannot concatenate multiple vorbis streams +// - sample positions are 32-bit, limiting seekable 192Khz +// files to around 6 hours (Ogg supports 64-bit) +// +// Feature contributors: +// Dougall Johnson (sample-exact seeking) +// +// Bugfix/warning contributors: +// Terje Mathisen Niklas Frykholm Andy Hill +// Casey Muratori John Bolton Gargaj +// Laurent Gomila Marc LeBlanc Ronny Chevalier +// Bernhard Wodo Evan Balster github:alxprd +// Tom Beaumont Ingo Leitgeb Nicolas Guillemot +// Phillip Bennefall Rohit Thiago Goulart +// github:manxorist Saga Musix github:infatum +// Timur Gagiev Maxwell Koo Peter Waller +// github:audinowho Dougall Johnson David Reid +// github:Clownacy Pedro J. Estebanez Remi Verschelde +// AnthoFoxo github:morlat Gabriel Ravier +// +// Partial history: +// 1.22 - 2021-07-11 - various small fixes +// 1.21 - 2021-07-02 - fix bug for files with no comments +// 1.20 - 2020-07-11 - several small fixes +// 1.19 - 2020-02-05 - warnings +// 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc. +// 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure) +// 1.16 - 2019-03-04 - fix warnings +// 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found +// 1.14 - 2018-02-11 - delete bogus dealloca usage +// 1.13 - 2018-01-29 - fix truncation of last frame (hopefully) +// 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files +// 1.11 - 2017-07-23 - fix MinGW compilation +// 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory +// 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version +// 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame +// 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const +// 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson) +// some crash fixes when out of memory or with corrupt files +// fix some inappropriately signed shifts +// 1.05 - 2015-04-19 - don't define __forceinline if it's redundant +// 1.04 - 2014-08-27 - fix missing const-correct case in API +// 1.03 - 2014-08-07 - warning fixes +// 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows +// 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct) +// 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel; +// (API change) report sample rate for decode-full-file funcs +// +// See end of file for full version history. + + +////////////////////////////////////////////////////////////////////////////// +// +// HEADER BEGINS HERE +// + +#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H +#define STB_VORBIS_INCLUDE_STB_VORBIS_H + +#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) +#define STB_VORBIS_NO_STDIO 1 +#endif + +#ifndef STB_VORBIS_NO_STDIO +#include <stdio.h> +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/////////// THREAD SAFETY + +// Individual stb_vorbis* handles are not thread-safe; you cannot decode from +// them from multiple threads at the same time. However, you can have multiple +// stb_vorbis* handles and decode from them independently in multiple thrads. + + +/////////// MEMORY ALLOCATION + +// normally stb_vorbis uses malloc() to allocate memory at startup, +// and alloca() to allocate temporary memory during a frame on the +// stack. (Memory consumption will depend on the amount of setup +// data in the file and how you set the compile flags for speed +// vs. size. In my test files the maximal-size usage is ~150KB.) +// +// You can modify the wrapper functions in the source (setup_malloc, +// setup_temp_malloc, temp_malloc) to change this behavior, or you +// can use a simpler allocation model: you pass in a buffer from +// which stb_vorbis will allocate _all_ its memory (including the +// temp memory). "open" may fail with a VORBIS_outofmem if you +// do not pass in enough data; there is no way to determine how +// much you do need except to succeed (at which point you can +// query get_info to find the exact amount required. yes I know +// this is lame). +// +// If you pass in a non-NULL buffer of the type below, allocation +// will occur from it as described above. Otherwise just pass NULL +// to use malloc()/alloca() + +typedef struct +{ + char *alloc_buffer; + int alloc_buffer_length_in_bytes; +} stb_vorbis_alloc; + + +/////////// FUNCTIONS USEABLE WITH ALL INPUT MODES + +typedef struct stb_vorbis stb_vorbis; + +typedef struct +{ + unsigned int sample_rate; + int channels; + + unsigned int setup_memory_required; + unsigned int setup_temp_memory_required; + unsigned int temp_memory_required; + + int max_frame_size; +} stb_vorbis_info; + +typedef struct +{ + char *vendor; + + int comment_list_length; + char **comment_list; +} stb_vorbis_comment; + +// get general information about the file +extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f); + +// get ogg comments +extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f); + +// get the last error detected (clears it, too) +extern int stb_vorbis_get_error(stb_vorbis *f); + +// close an ogg vorbis file and free all memory in use +extern void stb_vorbis_close(stb_vorbis *f); + +// this function returns the offset (in samples) from the beginning of the +// file that will be returned by the next decode, if it is known, or -1 +// otherwise. after a flush_pushdata() call, this may take a while before +// it becomes valid again. +// NOT WORKING YET after a seek with PULLDATA API +extern int stb_vorbis_get_sample_offset(stb_vorbis *f); + +// returns the current seek point within the file, or offset from the beginning +// of the memory buffer. In pushdata mode it returns 0. +extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f); + +/////////// PUSHDATA API + +#ifndef STB_VORBIS_NO_PUSHDATA_API + +// this API allows you to get blocks of data from any source and hand +// them to stb_vorbis. you have to buffer them; stb_vorbis will tell +// you how much it used, and you have to give it the rest next time; +// and stb_vorbis may not have enough data to work with and you will +// need to give it the same data again PLUS more. Note that the Vorbis +// specification does not bound the size of an individual frame. + +extern stb_vorbis *stb_vorbis_open_pushdata( + const unsigned char * datablock, int datablock_length_in_bytes, + int *datablock_memory_consumed_in_bytes, + int *error, + const stb_vorbis_alloc *alloc_buffer); +// create a vorbis decoder by passing in the initial data block containing +// the ogg&vorbis headers (you don't need to do parse them, just provide +// the first N bytes of the file--you're told if it's not enough, see below) +// on success, returns an stb_vorbis *, does not set error, returns the amount of +// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes; +// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed +// if returns NULL and *error is VORBIS_need_more_data, then the input block was +// incomplete and you need to pass in a larger block from the start of the file + +extern int stb_vorbis_decode_frame_pushdata( + stb_vorbis *f, + const unsigned char *datablock, int datablock_length_in_bytes, + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples + ); +// decode a frame of audio sample data if possible from the passed-in data block +// +// return value: number of bytes we used from datablock +// +// possible cases: +// 0 bytes used, 0 samples output (need more data) +// N bytes used, 0 samples output (resynching the stream, keep going) +// N bytes used, M samples output (one frame of data) +// note that after opening a file, you will ALWAYS get one N-bytes,0-sample +// frame, because Vorbis always "discards" the first frame. +// +// Note that on resynch, stb_vorbis will rarely consume all of the buffer, +// instead only datablock_length_in_bytes-3 or less. This is because it wants +// to avoid missing parts of a page header if they cross a datablock boundary, +// without writing state-machiney code to record a partial detection. +// +// The number of channels returned are stored in *channels (which can be +// NULL--it is always the same as the number of channels reported by +// get_info). *output will contain an array of float* buffers, one per +// channel. In other words, (*output)[0][0] contains the first sample from +// the first channel, and (*output)[1][0] contains the first sample from +// the second channel. +// +// *output points into stb_vorbis's internal output buffer storage; these +// buffers are owned by stb_vorbis and application code should not free +// them or modify their contents. They are transient and will be overwritten +// once you ask for more data to get decoded, so be sure to grab any data +// you need before then. + +extern void stb_vorbis_flush_pushdata(stb_vorbis *f); +// inform stb_vorbis that your next datablock will not be contiguous with +// previous ones (e.g. you've seeked in the data); future attempts to decode +// frames will cause stb_vorbis to resynchronize (as noted above), and +// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it +// will begin decoding the _next_ frame. +// +// if you want to seek using pushdata, you need to seek in your file, then +// call stb_vorbis_flush_pushdata(), then start calling decoding, then once +// decoding is returning you data, call stb_vorbis_get_sample_offset, and +// if you don't like the result, seek your file again and repeat. +#endif + + +////////// PULLING INPUT API + +#ifndef STB_VORBIS_NO_PULLDATA_API +// This API assumes stb_vorbis is allowed to pull data from a source-- +// either a block of memory containing the _entire_ vorbis stream, or a +// FILE * that you or it create, or possibly some other reading mechanism +// if you go modify the source to replace the FILE * case with some kind +// of callback to your code. (But if you don't support seeking, you may +// just want to go ahead and use pushdata.) + +#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) +extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output); +#endif +#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION) +extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output); +#endif +// decode an entire file and output the data interleaved into a malloc()ed +// buffer stored in *output. The return value is the number of samples +// decoded, or -1 if the file could not be opened or was not an ogg vorbis file. +// When you're done with it, just free() the pointer returned in *output. + +extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from an ogg vorbis stream in memory (note +// this must be the entire stream!). on failure, returns NULL and sets *error + +#ifndef STB_VORBIS_NO_STDIO +extern stb_vorbis * stb_vorbis_open_filename(const char *filename, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from a filename via fopen(). on failure, +// returns NULL and sets *error (possibly to VORBIS_file_open_failure). + +extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from an open FILE *, looking for a stream at +// the _current_ seek point (ftell). on failure, returns NULL and sets *error. +// note that stb_vorbis must "own" this stream; if you seek it in between +// calls to stb_vorbis, it will become confused. Moreover, if you attempt to +// perform stb_vorbis_seek_*() operations on this file, it will assume it +// owns the _entire_ rest of the file after the start point. Use the next +// function, stb_vorbis_open_file_section(), to limit it. + +extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len); +// create an ogg vorbis decoder from an open FILE *, looking for a stream at +// the _current_ seek point (ftell); the stream will be of length 'len' bytes. +// on failure, returns NULL and sets *error. note that stb_vorbis must "own" +// this stream; if you seek it in between calls to stb_vorbis, it will become +// confused. +#endif + +extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number); +extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); +// these functions seek in the Vorbis file to (approximately) 'sample_number'. +// after calling seek_frame(), the next call to get_frame_*() will include +// the specified sample. after calling stb_vorbis_seek(), the next call to +// stb_vorbis_get_samples_* will start with the specified sample. If you +// do not need to seek to EXACTLY the target sample when using get_samples_*, +// you can also use seek_frame(). + +extern int stb_vorbis_seek_start(stb_vorbis *f); +// this function is equivalent to stb_vorbis_seek(f,0) + +extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); +extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f); +// these functions return the total length of the vorbis stream + +extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output); +// decode the next frame and return the number of samples. the number of +// channels returned are stored in *channels (which can be NULL--it is always +// the same as the number of channels reported by get_info). *output will +// contain an array of float* buffers, one per channel. These outputs will +// be overwritten on the next call to stb_vorbis_get_frame_*. +// +// You generally should not intermix calls to stb_vorbis_get_frame_*() +// and stb_vorbis_get_samples_*(), since the latter calls the former. + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts); +extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples); +#endif +// decode the next frame and return the number of *samples* per channel. +// Note that for interleaved data, you pass in the number of shorts (the +// size of your array), but the return value is the number of samples per +// channel, not the total number of samples. +// +// The data is coerced to the number of channels you request according to the +// channel coercion rules (see below). You must pass in the size of your +// buffer(s) so that stb_vorbis will not overwrite the end of the buffer. +// The maximum buffer size needed can be gotten from get_info(); however, +// the Vorbis I specification implies an absolute maximum of 4096 samples +// per channel. + +// Channel coercion rules: +// Let M be the number of channels requested, and N the number of channels present, +// and Cn be the nth channel; let stereo L be the sum of all L and center channels, +// and stereo R be the sum of all R and center channels (channel assignment from the +// vorbis spec). +// M N output +// 1 k sum(Ck) for all k +// 2 * stereo L, stereo R +// k l k > l, the first l channels, then 0s +// k l k <= l, the first k channels +// Note that this is not _good_ surround etc. mixing at all! It's just so +// you get something useful. + +extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats); +extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples); +// gets num_samples samples, not necessarily on a frame boundary--this requires +// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES. +// Returns the number of samples stored per channel; it may be less than requested +// at the end of the file. If there are no more samples in the file, returns 0. + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts); +extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples); +#endif +// gets num_samples samples, not necessarily on a frame boundary--this requires +// buffering so you have to supply the buffers. Applies the coercion rules above +// to produce 'channels' channels. Returns the number of samples stored per channel; +// it may be less than requested at the end of the file. If there are no more +// samples in the file, returns 0. + +#endif + +//////// ERROR CODES + +enum STBVorbisError +{ + VORBIS__no_error, + + VORBIS_need_more_data=1, // not a real error + + VORBIS_invalid_api_mixing, // can't mix API modes + VORBIS_outofmem, // not enough memory + VORBIS_feature_not_supported, // uses floor 0 + VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small + VORBIS_file_open_failure, // fopen() failed + VORBIS_seek_without_length, // can't seek in unknown-length file + + VORBIS_unexpected_eof=10, // file is truncated? + VORBIS_seek_invalid, // seek past EOF + + // decoding errors (corrupt/invalid stream) -- you probably + // don't care about the exact details of these + + // vorbis errors: + VORBIS_invalid_setup=20, + VORBIS_invalid_stream, + + // ogg errors: + VORBIS_missing_capture_pattern=30, + VORBIS_invalid_stream_structure_version, + VORBIS_continued_packet_flag_invalid, + VORBIS_incorrect_stream_serial_number, + VORBIS_invalid_first_page, + VORBIS_bad_packet_type, + VORBIS_cant_find_last_page, + VORBIS_seek_failed, + VORBIS_ogg_skeleton_not_supported +}; + + +#ifdef __cplusplus +} +#endif + +#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H +// +// HEADER ENDS HERE +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef STB_VORBIS_HEADER_ONLY + +// global configuration settings (e.g. set these in the project/makefile), +// or just set them in this file at the top (although ideally the first few +// should be visible when the header file is compiled too, although it's not +// crucial) + +// STB_VORBIS_NO_PUSHDATA_API +// does not compile the code for the various stb_vorbis_*_pushdata() +// functions +// #define STB_VORBIS_NO_PUSHDATA_API + +// STB_VORBIS_NO_PULLDATA_API +// does not compile the code for the non-pushdata APIs +// #define STB_VORBIS_NO_PULLDATA_API + +// STB_VORBIS_NO_STDIO +// does not compile the code for the APIs that use FILE *s internally +// or externally (implied by STB_VORBIS_NO_PULLDATA_API) +// #define STB_VORBIS_NO_STDIO + +// STB_VORBIS_NO_INTEGER_CONVERSION +// does not compile the code for converting audio sample data from +// float to integer (implied by STB_VORBIS_NO_PULLDATA_API) +// #define STB_VORBIS_NO_INTEGER_CONVERSION + +// STB_VORBIS_NO_FAST_SCALED_FLOAT +// does not use a fast float-to-int trick to accelerate float-to-int on +// most platforms which requires endianness be defined correctly. +//#define STB_VORBIS_NO_FAST_SCALED_FLOAT + + +// STB_VORBIS_MAX_CHANNELS [number] +// globally define this to the maximum number of channels you need. +// The spec does not put a restriction on channels except that +// the count is stored in a byte, so 255 is the hard limit. +// Reducing this saves about 16 bytes per value, so using 16 saves +// (255-16)*16 or around 4KB. Plus anything other memory usage +// I forgot to account for. Can probably go as low as 8 (7.1 audio), +// 6 (5.1 audio), or 2 (stereo only). +#ifndef STB_VORBIS_MAX_CHANNELS +#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone? +#endif + +// STB_VORBIS_PUSHDATA_CRC_COUNT [number] +// after a flush_pushdata(), stb_vorbis begins scanning for the +// next valid page, without backtracking. when it finds something +// that looks like a page, it streams through it and verifies its +// CRC32. Should that validation fail, it keeps scanning. But it's +// possible that _while_ streaming through to check the CRC32 of +// one candidate page, it sees another candidate page. This #define +// determines how many "overlapping" candidate pages it can search +// at once. Note that "real" pages are typically ~4KB to ~8KB, whereas +// garbage pages could be as big as 64KB, but probably average ~16KB. +// So don't hose ourselves by scanning an apparent 64KB page and +// missing a ton of real ones in the interim; so minimum of 2 +#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT +#define STB_VORBIS_PUSHDATA_CRC_COUNT 4 +#endif + +// STB_VORBIS_FAST_HUFFMAN_LENGTH [number] +// sets the log size of the huffman-acceleration table. Maximum +// supported value is 24. with larger numbers, more decodings are O(1), +// but the table size is larger so worse cache missing, so you'll have +// to probe (and try multiple ogg vorbis files) to find the sweet spot. +#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH +#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10 +#endif + +// STB_VORBIS_FAST_BINARY_LENGTH [number] +// sets the log size of the binary-search acceleration table. this +// is used in similar fashion to the fast-huffman size to set initial +// parameters for the binary search + +// STB_VORBIS_FAST_HUFFMAN_INT +// The fast huffman tables are much more efficient if they can be +// stored as 16-bit results instead of 32-bit results. This restricts +// the codebooks to having only 65535 possible outcomes, though. +// (At least, accelerated by the huffman table.) +#ifndef STB_VORBIS_FAST_HUFFMAN_INT +#define STB_VORBIS_FAST_HUFFMAN_SHORT +#endif + +// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH +// If the 'fast huffman' search doesn't succeed, then stb_vorbis falls +// back on binary searching for the correct one. This requires storing +// extra tables with the huffman codes in sorted order. Defining this +// symbol trades off space for speed by forcing a linear search in the +// non-fast case, except for "sparse" codebooks. +// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH + +// STB_VORBIS_DIVIDES_IN_RESIDUE +// stb_vorbis precomputes the result of the scalar residue decoding +// that would otherwise require a divide per chunk. you can trade off +// space for time by defining this symbol. +// #define STB_VORBIS_DIVIDES_IN_RESIDUE + +// STB_VORBIS_DIVIDES_IN_CODEBOOK +// vorbis VQ codebooks can be encoded two ways: with every case explicitly +// stored, or with all elements being chosen from a small range of values, +// and all values possible in all elements. By default, stb_vorbis expands +// this latter kind out to look like the former kind for ease of decoding, +// because otherwise an integer divide-per-vector-element is required to +// unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can +// trade off storage for speed. +//#define STB_VORBIS_DIVIDES_IN_CODEBOOK + +#ifdef STB_VORBIS_CODEBOOK_SHORTS +#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats" +#endif + +// STB_VORBIS_DIVIDE_TABLE +// this replaces small integer divides in the floor decode loop with +// table lookups. made less than 1% difference, so disabled by default. + +// STB_VORBIS_NO_INLINE_DECODE +// disables the inlining of the scalar codebook fast-huffman decode. +// might save a little codespace; useful for debugging +// #define STB_VORBIS_NO_INLINE_DECODE + +// STB_VORBIS_NO_DEFER_FLOOR +// Normally we only decode the floor without synthesizing the actual +// full curve. We can instead synthesize the curve immediately. This +// requires more memory and is very likely slower, so I don't think +// you'd ever want to do it except for debugging. +// #define STB_VORBIS_NO_DEFER_FLOOR + + + + +////////////////////////////////////////////////////////////////////////////// + +#ifdef STB_VORBIS_NO_PULLDATA_API + #define STB_VORBIS_NO_INTEGER_CONVERSION + #define STB_VORBIS_NO_STDIO +#endif + +#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) + #define STB_VORBIS_NO_STDIO 1 +#endif + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT + + // only need endianness for fast-float-to-int, which we don't + // use for pushdata + + #ifndef STB_VORBIS_BIG_ENDIAN + #define STB_VORBIS_ENDIAN 0 + #else + #define STB_VORBIS_ENDIAN 1 + #endif + +#endif +#endif + + +#ifndef STB_VORBIS_NO_STDIO +#include <stdio.h> +#endif + +#ifndef STB_VORBIS_NO_CRT + #include <stdlib.h> + #include <string.h> + #include <assert.h> + #include <math.h> + + // find definition of alloca if it's not in stdlib.h: + #if defined(_MSC_VER) || defined(__MINGW32__) + #include <malloc.h> + #endif + #if defined(__linux__) || defined(__linux) || defined(__sun__) || defined(__EMSCRIPTEN__) || defined(__NEWLIB__) + #include <alloca.h> + #endif +#else // STB_VORBIS_NO_CRT + #define NULL 0 + #define malloc(s) 0 + #define free(s) ((void) 0) + #define realloc(s) 0 +#endif // STB_VORBIS_NO_CRT + +#include <limits.h> + +#ifdef __MINGW32__ + // eff you mingw: + // "fixed": + // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/ + // "no that broke the build, reverted, who cares about C": + // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/ + #ifdef __forceinline + #undef __forceinline + #endif + #define __forceinline + #ifndef alloca + #define alloca __builtin_alloca + #endif +#elif !defined(_MSC_VER) + #if __GNUC__ + #define __forceinline inline + #else + #define __forceinline + #endif +#endif + +#if STB_VORBIS_MAX_CHANNELS > 256 +#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range" +#endif + +#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24 +#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range" +#endif + + +#if 0 +#include <crtdbg.h> +#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1]) +#else +#define CHECK(f) ((void) 0) +#endif + +#define MAX_BLOCKSIZE_LOG 13 // from specification +#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG) + + +typedef unsigned char uint8; +typedef signed char int8; +typedef unsigned short uint16; +typedef signed short int16; +typedef unsigned int uint32; +typedef signed int int32; + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +typedef float codetype; + +#ifdef _MSC_VER +#define STBV_NOTUSED(v) (void)(v) +#else +#define STBV_NOTUSED(v) (void)sizeof(v) +#endif + +// @NOTE +// +// Some arrays below are tagged "//varies", which means it's actually +// a variable-sized piece of data, but rather than malloc I assume it's +// small enough it's better to just allocate it all together with the +// main thing +// +// Most of the variables are specified with the smallest size I could pack +// them into. It might give better performance to make them all full-sized +// integers. It should be safe to freely rearrange the structures or change +// the sizes larger--nothing relies on silently truncating etc., nor the +// order of variables. + +#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH) +#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1) + +typedef struct +{ + int dimensions, entries; + uint8 *codeword_lengths; + float minimum_value; + float delta_value; + uint8 value_bits; + uint8 lookup_type; + uint8 sequence_p; + uint8 sparse; + uint32 lookup_values; + codetype *multiplicands; + uint32 *codewords; + #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; + #else + int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; + #endif + uint32 *sorted_codewords; + int *sorted_values; + int sorted_entries; +} Codebook; + +typedef struct +{ + uint8 order; + uint16 rate; + uint16 bark_map_size; + uint8 amplitude_bits; + uint8 amplitude_offset; + uint8 number_of_books; + uint8 book_list[16]; // varies +} Floor0; + +typedef struct +{ + uint8 partitions; + uint8 partition_class_list[32]; // varies + uint8 class_dimensions[16]; // varies + uint8 class_subclasses[16]; // varies + uint8 class_masterbooks[16]; // varies + int16 subclass_books[16][8]; // varies + uint16 Xlist[31*8+2]; // varies + uint8 sorted_order[31*8+2]; + uint8 neighbors[31*8+2][2]; + uint8 floor1_multiplier; + uint8 rangebits; + int values; +} Floor1; + +typedef union +{ + Floor0 floor0; + Floor1 floor1; +} Floor; + +typedef struct +{ + uint32 begin, end; + uint32 part_size; + uint8 classifications; + uint8 classbook; + uint8 **classdata; + int16 (*residue_books)[8]; +} Residue; + +typedef struct +{ + uint8 magnitude; + uint8 angle; + uint8 mux; +} MappingChannel; + +typedef struct +{ + uint16 coupling_steps; + MappingChannel *chan; + uint8 submaps; + uint8 submap_floor[15]; // varies + uint8 submap_residue[15]; // varies +} Mapping; + +typedef struct +{ + uint8 blockflag; + uint8 mapping; + uint16 windowtype; + uint16 transformtype; +} Mode; + +typedef struct +{ + uint32 goal_crc; // expected crc if match + int bytes_left; // bytes left in packet + uint32 crc_so_far; // running crc + int bytes_done; // bytes processed in _current_ chunk + uint32 sample_loc; // granule pos encoded in page +} CRCscan; + +typedef struct +{ + uint32 page_start, page_end; + uint32 last_decoded_sample; +} ProbedPage; + +struct stb_vorbis +{ + // user-accessible info + unsigned int sample_rate; + int channels; + + unsigned int setup_memory_required; + unsigned int temp_memory_required; + unsigned int setup_temp_memory_required; + + char *vendor; + int comment_list_length; + char **comment_list; + + // input config +#ifndef STB_VORBIS_NO_STDIO + FILE *f; + uint32 f_start; + int close_on_free; +#endif + + uint8 *stream; + uint8 *stream_start; + uint8 *stream_end; + + uint32 stream_len; + + uint8 push_mode; + + // the page to seek to when seeking to start, may be zero + uint32 first_audio_page_offset; + + // p_first is the page on which the first audio packet ends + // (but not necessarily the page on which it starts) + ProbedPage p_first, p_last; + + // memory management + stb_vorbis_alloc alloc; + int setup_offset; + int temp_offset; + + // run-time results + int eof; + enum STBVorbisError error; + + // user-useful data + + // header info + int blocksize[2]; + int blocksize_0, blocksize_1; + int codebook_count; + Codebook *codebooks; + int floor_count; + uint16 floor_types[64]; // varies + Floor *floor_config; + int residue_count; + uint16 residue_types[64]; // varies + Residue *residue_config; + int mapping_count; + Mapping *mapping; + int mode_count; + Mode mode_config[64]; // varies + + uint32 total_samples; + + // decode buffer + float *channel_buffers[STB_VORBIS_MAX_CHANNELS]; + float *outputs [STB_VORBIS_MAX_CHANNELS]; + + float *previous_window[STB_VORBIS_MAX_CHANNELS]; + int previous_length; + + #ifndef STB_VORBIS_NO_DEFER_FLOOR + int16 *finalY[STB_VORBIS_MAX_CHANNELS]; + #else + float *floor_buffers[STB_VORBIS_MAX_CHANNELS]; + #endif + + uint32 current_loc; // sample location of next frame to decode + int current_loc_valid; + + // per-blocksize precomputed data + + // twiddle factors + float *A[2],*B[2],*C[2]; + float *window[2]; + uint16 *bit_reverse[2]; + + // current page/packet/segment streaming info + uint32 serial; // stream serial number for verification + int last_page; + int segment_count; + uint8 segments[255]; + uint8 page_flag; + uint8 bytes_in_seg; + uint8 first_decode; + int next_seg; + int last_seg; // flag that we're on the last segment + int last_seg_which; // what was the segment number of the last seg? + uint32 acc; + int valid_bits; + int packet_bytes; + int end_seg_with_known_loc; + uint32 known_loc_for_packet; + int discard_samples_deferred; + uint32 samples_output; + + // push mode scanning + int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching +#ifndef STB_VORBIS_NO_PUSHDATA_API + CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT]; +#endif + + // sample-access + int channel_buffer_start; + int channel_buffer_end; +}; + +#if defined(STB_VORBIS_NO_PUSHDATA_API) + #define IS_PUSH_MODE(f) FALSE +#elif defined(STB_VORBIS_NO_PULLDATA_API) + #define IS_PUSH_MODE(f) TRUE +#else + #define IS_PUSH_MODE(f) ((f)->push_mode) +#endif + +typedef struct stb_vorbis vorb; + +static int error(vorb *f, enum STBVorbisError e) +{ + f->error = e; + if (!f->eof && e != VORBIS_need_more_data) { + f->error=e; // breakpoint for debugging + } + return 0; +} + + +// these functions are used for allocating temporary memory +// while decoding. if you can afford the stack space, use +// alloca(); otherwise, provide a temp buffer and it will +// allocate out of those. + +#define array_size_required(count,size) (count*(sizeof(void *)+(size))) + +#define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size)) +#define temp_free(f,p) (void)0 +#define temp_alloc_save(f) ((f)->temp_offset) +#define temp_alloc_restore(f,p) ((f)->temp_offset = (p)) + +#define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size) + +// given a sufficiently large block of memory, make an array of pointers to subblocks of it +static void *make_block_array(void *mem, int count, int size) +{ + int i; + void ** p = (void **) mem; + char *q = (char *) (p + count); + for (i=0; i < count; ++i) { + p[i] = q; + q += size; + } + return p; +} + +static void *setup_malloc(vorb *f, int sz) +{ + sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. + f->setup_memory_required += sz; + if (f->alloc.alloc_buffer) { + void *p = (char *) f->alloc.alloc_buffer + f->setup_offset; + if (f->setup_offset + sz > f->temp_offset) return NULL; + f->setup_offset += sz; + return p; + } + return sz ? malloc(sz) : NULL; +} + +static void setup_free(vorb *f, void *p) +{ + if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack + free(p); +} + +static void *setup_temp_malloc(vorb *f, int sz) +{ + sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. + if (f->alloc.alloc_buffer) { + if (f->temp_offset - sz < f->setup_offset) return NULL; + f->temp_offset -= sz; + return (char *) f->alloc.alloc_buffer + f->temp_offset; + } + return malloc(sz); +} + +static void setup_temp_free(vorb *f, void *p, int sz) +{ + if (f->alloc.alloc_buffer) { + f->temp_offset += (sz+7)&~7; + return; + } + free(p); +} + +#define CRC32_POLY 0x04c11db7 // from spec + +static uint32 crc_table[256]; +static void crc32_init(void) +{ + int i,j; + uint32 s; + for(i=0; i < 256; i++) { + for (s=(uint32) i << 24, j=0; j < 8; ++j) + s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0); + crc_table[i] = s; + } +} + +static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) +{ + return (crc << 8) ^ crc_table[byte ^ (crc >> 24)]; +} + + +// used in setup, and for huffman that doesn't go fast path +static unsigned int bit_reverse(unsigned int n) +{ + n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); + n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2); + n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4); + n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8); + return (n >> 16) | (n << 16); +} + +static float square(float x) +{ + return x*x; +} + +// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3 +// as required by the specification. fast(?) implementation from stb.h +// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup +static int ilog(int32 n) +{ + static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 }; + + if (n < 0) return 0; // signed n returns 0 + + // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29) + if (n < (1 << 14)) + if (n < (1 << 4)) return 0 + log2_4[n ]; + else if (n < (1 << 9)) return 5 + log2_4[n >> 5]; + else return 10 + log2_4[n >> 10]; + else if (n < (1 << 24)) + if (n < (1 << 19)) return 15 + log2_4[n >> 15]; + else return 20 + log2_4[n >> 20]; + else if (n < (1 << 29)) return 25 + log2_4[n >> 25]; + else return 30 + log2_4[n >> 30]; +} + +#ifndef M_PI + #define M_PI 3.14159265358979323846264f // from CRC +#endif + +// code length assigned to a value with no huffman encoding +#define NO_CODE 255 + +/////////////////////// LEAF SETUP FUNCTIONS ////////////////////////// +// +// these functions are only called at setup, and only a few times +// per file + +static float float32_unpack(uint32 x) +{ + // from the specification + uint32 mantissa = x & 0x1fffff; + uint32 sign = x & 0x80000000; + uint32 exp = (x & 0x7fe00000) >> 21; + double res = sign ? -(double)mantissa : (double)mantissa; + return (float) ldexp((float)res, (int)exp-788); +} + + +// zlib & jpeg huffman tables assume that the output symbols +// can either be arbitrarily arranged, or have monotonically +// increasing frequencies--they rely on the lengths being sorted; +// this makes for a very simple generation algorithm. +// vorbis allows a huffman table with non-sorted lengths. This +// requires a more sophisticated construction, since symbols in +// order do not map to huffman codes "in order". +static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) +{ + if (!c->sparse) { + c->codewords [symbol] = huff_code; + } else { + c->codewords [count] = huff_code; + c->codeword_lengths[count] = len; + values [count] = symbol; + } +} + +static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) +{ + int i,k,m=0; + uint32 available[32]; + + memset(available, 0, sizeof(available)); + // find the first entry + for (k=0; k < n; ++k) if (len[k] < NO_CODE) break; + if (k == n) { assert(c->sorted_entries == 0); return TRUE; } + assert(len[k] < 32); // no error return required, code reading lens checks this + // add to the list + add_entry(c, 0, k, m++, len[k], values); + // add all available leaves + for (i=1; i <= len[k]; ++i) + available[i] = 1U << (32-i); + // note that the above code treats the first case specially, + // but it's really the same as the following code, so they + // could probably be combined (except the initial code is 0, + // and I use 0 in available[] to mean 'empty') + for (i=k+1; i < n; ++i) { + uint32 res; + int z = len[i], y; + if (z == NO_CODE) continue; + assert(z < 32); // no error return required, code reading lens checks this + // find lowest available leaf (should always be earliest, + // which is what the specification calls for) + // note that this property, and the fact we can never have + // more than one free leaf at a given level, isn't totally + // trivial to prove, but it seems true and the assert never + // fires, so! + while (z > 0 && !available[z]) --z; + if (z == 0) { return FALSE; } + res = available[z]; + available[z] = 0; + add_entry(c, bit_reverse(res), i, m++, len[i], values); + // propagate availability up the tree + if (z != len[i]) { + for (y=len[i]; y > z; --y) { + assert(available[y] == 0); + available[y] = res + (1 << (32-y)); + } + } + } + return TRUE; +} + +// accelerated huffman table allows fast O(1) match of all symbols +// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH +static void compute_accelerated_huffman(Codebook *c) +{ + int i, len; + for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i) + c->fast_huffman[i] = -1; + + len = c->sparse ? c->sorted_entries : c->entries; + #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + if (len > 32767) len = 32767; // largest possible value we can encode! + #endif + for (i=0; i < len; ++i) { + if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) { + uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i]; + // set table entries for all bit combinations in the higher bits + while (z < FAST_HUFFMAN_TABLE_SIZE) { + c->fast_huffman[z] = i; + z += 1 << c->codeword_lengths[i]; + } + } + } +} + +#ifdef _MSC_VER +#define STBV_CDECL __cdecl +#else +#define STBV_CDECL +#endif + +static int STBV_CDECL uint32_compare(const void *p, const void *q) +{ + uint32 x = * (uint32 *) p; + uint32 y = * (uint32 *) q; + return x < y ? -1 : x > y; +} + +static int include_in_sort(Codebook *c, uint8 len) +{ + if (c->sparse) { assert(len != NO_CODE); return TRUE; } + if (len == NO_CODE) return FALSE; + if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE; + return FALSE; +} + +// if the fast table above doesn't work, we want to binary +// search them... need to reverse the bits +static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) +{ + int i, len; + // build a list of all the entries + // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN. + // this is kind of a frivolous optimization--I don't see any performance improvement, + // but it's like 4 extra lines of code, so. + if (!c->sparse) { + int k = 0; + for (i=0; i < c->entries; ++i) + if (include_in_sort(c, lengths[i])) + c->sorted_codewords[k++] = bit_reverse(c->codewords[i]); + assert(k == c->sorted_entries); + } else { + for (i=0; i < c->sorted_entries; ++i) + c->sorted_codewords[i] = bit_reverse(c->codewords[i]); + } + + qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare); + c->sorted_codewords[c->sorted_entries] = 0xffffffff; + + len = c->sparse ? c->sorted_entries : c->entries; + // now we need to indicate how they correspond; we could either + // #1: sort a different data structure that says who they correspond to + // #2: for each sorted entry, search the original list to find who corresponds + // #3: for each original entry, find the sorted entry + // #1 requires extra storage, #2 is slow, #3 can use binary search! + for (i=0; i < len; ++i) { + int huff_len = c->sparse ? lengths[values[i]] : lengths[i]; + if (include_in_sort(c,huff_len)) { + uint32 code = bit_reverse(c->codewords[i]); + int x=0, n=c->sorted_entries; + while (n > 1) { + // invariant: sc[x] <= code < sc[x+n] + int m = x + (n >> 1); + if (c->sorted_codewords[m] <= code) { + x = m; + n -= (n>>1); + } else { + n >>= 1; + } + } + assert(c->sorted_codewords[x] == code); + if (c->sparse) { + c->sorted_values[x] = values[i]; + c->codeword_lengths[x] = huff_len; + } else { + c->sorted_values[x] = i; + } + } + } +} + +// only run while parsing the header (3 times) +static int vorbis_validate(uint8 *data) +{ + static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' }; + return memcmp(data, vorbis, 6) == 0; +} + +// called from setup only, once per code book +// (formula implied by specification) +static int lookup1_values(int entries, int dim) +{ + int r = (int) floor(exp((float) log((float) entries) / dim)); + if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning; + ++r; // floor() to avoid _ftol() when non-CRT + if (pow((float) r+1, dim) <= entries) + return -1; + if ((int) floor(pow((float) r, dim)) > entries) + return -1; + return r; +} + +// called twice per file +static void compute_twiddle_factors(int n, float *A, float *B, float *C) +{ + int n4 = n >> 2, n8 = n >> 3; + int k,k2; + + for (k=k2=0; k < n4; ++k,k2+=2) { + A[k2 ] = (float) cos(4*k*M_PI/n); + A[k2+1] = (float) -sin(4*k*M_PI/n); + B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f; + B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f; + } + for (k=k2=0; k < n8; ++k,k2+=2) { + C[k2 ] = (float) cos(2*(k2+1)*M_PI/n); + C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n); + } +} + +static void compute_window(int n, float *window) +{ + int n2 = n >> 1, i; + for (i=0; i < n2; ++i) + window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI))); +} + +static void compute_bitreverse(int n, uint16 *rev) +{ + int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + int i, n8 = n >> 3; + for (i=0; i < n8; ++i) + rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2; +} + +static int init_blocksize(vorb *f, int b, int n) +{ + int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3; + f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2); + f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2); + f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4); + if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem); + compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]); + f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2); + if (!f->window[b]) return error(f, VORBIS_outofmem); + compute_window(n, f->window[b]); + f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8); + if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem); + compute_bitreverse(n, f->bit_reverse[b]); + return TRUE; +} + +static void neighbors(uint16 *x, int n, int *plow, int *phigh) +{ + int low = -1; + int high = 65536; + int i; + for (i=0; i < n; ++i) { + if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; } + if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; } + } +} + +// this has been repurposed so y is now the original index instead of y +typedef struct +{ + uint16 x,id; +} stbv__floor_ordering; + +static int STBV_CDECL point_compare(const void *p, const void *q) +{ + stbv__floor_ordering *a = (stbv__floor_ordering *) p; + stbv__floor_ordering *b = (stbv__floor_ordering *) q; + return a->x < b->x ? -1 : a->x > b->x; +} + +// +/////////////////////// END LEAF SETUP FUNCTIONS ////////////////////////// + + +#if defined(STB_VORBIS_NO_STDIO) + #define USE_MEMORY(z) TRUE +#else + #define USE_MEMORY(z) ((z)->stream) +#endif + +static uint8 get8(vorb *z) +{ + if (USE_MEMORY(z)) { + if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; } + return *z->stream++; + } + + #ifndef STB_VORBIS_NO_STDIO + { + int c = fgetc(z->f); + if (c == EOF) { z->eof = TRUE; return 0; } + return c; + } + #endif +} + +static uint32 get32(vorb *f) +{ + uint32 x; + x = get8(f); + x += get8(f) << 8; + x += get8(f) << 16; + x += (uint32) get8(f) << 24; + return x; +} + +static int getn(vorb *z, uint8 *data, int n) +{ + if (USE_MEMORY(z)) { + if (z->stream+n > z->stream_end) { z->eof = 1; return 0; } + memcpy(data, z->stream, n); + z->stream += n; + return 1; + } + + #ifndef STB_VORBIS_NO_STDIO + if (fread(data, n, 1, z->f) == 1) + return 1; + else { + z->eof = 1; + return 0; + } + #endif +} + +static void skip(vorb *z, int n) +{ + if (USE_MEMORY(z)) { + z->stream += n; + if (z->stream >= z->stream_end) z->eof = 1; + return; + } + #ifndef STB_VORBIS_NO_STDIO + { + long x = ftell(z->f); + fseek(z->f, x+n, SEEK_SET); + } + #endif +} + +static int set_file_offset(stb_vorbis *f, unsigned int loc) +{ + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) return 0; + #endif + f->eof = 0; + if (USE_MEMORY(f)) { + if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) { + f->stream = f->stream_end; + f->eof = 1; + return 0; + } else { + f->stream = f->stream_start + loc; + return 1; + } + } + #ifndef STB_VORBIS_NO_STDIO + if (loc + f->f_start < loc || loc >= 0x80000000) { + loc = 0x7fffffff; + f->eof = 1; + } else { + loc += f->f_start; + } + if (!fseek(f->f, loc, SEEK_SET)) + return 1; + f->eof = 1; + fseek(f->f, f->f_start, SEEK_END); + return 0; + #endif +} + + +static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 }; + +static int capture_pattern(vorb *f) +{ + if (0x4f != get8(f)) return FALSE; + if (0x67 != get8(f)) return FALSE; + if (0x67 != get8(f)) return FALSE; + if (0x53 != get8(f)) return FALSE; + return TRUE; +} + +#define PAGEFLAG_continued_packet 1 +#define PAGEFLAG_first_page 2 +#define PAGEFLAG_last_page 4 + +static int start_page_no_capturepattern(vorb *f) +{ + uint32 loc0,loc1,n; + if (f->first_decode && !IS_PUSH_MODE(f)) { + f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4; + } + // stream structure version + if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version); + // header flag + f->page_flag = get8(f); + // absolute granule position + loc0 = get32(f); + loc1 = get32(f); + // @TODO: validate loc0,loc1 as valid positions? + // stream serial number -- vorbis doesn't interleave, so discard + get32(f); + //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number); + // page sequence number + n = get32(f); + f->last_page = n; + // CRC32 + get32(f); + // page_segments + f->segment_count = get8(f); + if (!getn(f, f->segments, f->segment_count)) + return error(f, VORBIS_unexpected_eof); + // assume we _don't_ know any the sample position of any segments + f->end_seg_with_known_loc = -2; + if (loc0 != ~0U || loc1 != ~0U) { + int i; + // determine which packet is the last one that will complete + for (i=f->segment_count-1; i >= 0; --i) + if (f->segments[i] < 255) + break; + // 'i' is now the index of the _last_ segment of a packet that ends + if (i >= 0) { + f->end_seg_with_known_loc = i; + f->known_loc_for_packet = loc0; + } + } + if (f->first_decode) { + int i,len; + len = 0; + for (i=0; i < f->segment_count; ++i) + len += f->segments[i]; + len += 27 + f->segment_count; + f->p_first.page_end = f->p_first.page_start + len; + f->p_first.last_decoded_sample = loc0; + } + f->next_seg = 0; + return TRUE; +} + +static int start_page(vorb *f) +{ + if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern); + return start_page_no_capturepattern(f); +} + +static int start_packet(vorb *f) +{ + while (f->next_seg == -1) { + if (!start_page(f)) return FALSE; + if (f->page_flag & PAGEFLAG_continued_packet) + return error(f, VORBIS_continued_packet_flag_invalid); + } + f->last_seg = FALSE; + f->valid_bits = 0; + f->packet_bytes = 0; + f->bytes_in_seg = 0; + // f->next_seg is now valid + return TRUE; +} + +static int maybe_start_packet(vorb *f) +{ + if (f->next_seg == -1) { + int x = get8(f); + if (f->eof) return FALSE; // EOF at page boundary is not an error! + if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (!start_page_no_capturepattern(f)) return FALSE; + if (f->page_flag & PAGEFLAG_continued_packet) { + // set up enough state that we can read this packet if we want, + // e.g. during recovery + f->last_seg = FALSE; + f->bytes_in_seg = 0; + return error(f, VORBIS_continued_packet_flag_invalid); + } + } + return start_packet(f); +} + +static int next_segment(vorb *f) +{ + int len; + if (f->last_seg) return 0; + if (f->next_seg == -1) { + f->last_seg_which = f->segment_count-1; // in case start_page fails + if (!start_page(f)) { f->last_seg = 1; return 0; } + if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid); + } + len = f->segments[f->next_seg++]; + if (len < 255) { + f->last_seg = TRUE; + f->last_seg_which = f->next_seg-1; + } + if (f->next_seg >= f->segment_count) + f->next_seg = -1; + assert(f->bytes_in_seg == 0); + f->bytes_in_seg = len; + return len; +} + +#define EOP (-1) +#define INVALID_BITS (-1) + +static int get8_packet_raw(vorb *f) +{ + if (!f->bytes_in_seg) { // CLANG! + if (f->last_seg) return EOP; + else if (!next_segment(f)) return EOP; + } + assert(f->bytes_in_seg > 0); + --f->bytes_in_seg; + ++f->packet_bytes; + return get8(f); +} + +static int get8_packet(vorb *f) +{ + int x = get8_packet_raw(f); + f->valid_bits = 0; + return x; +} + +static int get32_packet(vorb *f) +{ + uint32 x; + x = get8_packet(f); + x += get8_packet(f) << 8; + x += get8_packet(f) << 16; + x += (uint32) get8_packet(f) << 24; + return x; +} + +static void flush_packet(vorb *f) +{ + while (get8_packet_raw(f) != EOP); +} + +// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important +// as the huffman decoder? +static uint32 get_bits(vorb *f, int n) +{ + uint32 z; + + if (f->valid_bits < 0) return 0; + if (f->valid_bits < n) { + if (n > 24) { + // the accumulator technique below would not work correctly in this case + z = get_bits(f, 24); + z += get_bits(f, n-24) << 24; + return z; + } + if (f->valid_bits == 0) f->acc = 0; + while (f->valid_bits < n) { + int z = get8_packet_raw(f); + if (z == EOP) { + f->valid_bits = INVALID_BITS; + return 0; + } + f->acc += z << f->valid_bits; + f->valid_bits += 8; + } + } + + assert(f->valid_bits >= n); + z = f->acc & ((1 << n)-1); + f->acc >>= n; + f->valid_bits -= n; + return z; +} + +// @OPTIMIZE: primary accumulator for huffman +// expand the buffer to as many bits as possible without reading off end of packet +// it might be nice to allow f->valid_bits and f->acc to be stored in registers, +// e.g. cache them locally and decode locally +static __forceinline void prep_huffman(vorb *f) +{ + if (f->valid_bits <= 24) { + if (f->valid_bits == 0) f->acc = 0; + do { + int z; + if (f->last_seg && !f->bytes_in_seg) return; + z = get8_packet_raw(f); + if (z == EOP) return; + f->acc += (unsigned) z << f->valid_bits; + f->valid_bits += 8; + } while (f->valid_bits <= 24); + } +} + +enum +{ + VORBIS_packet_id = 1, + VORBIS_packet_comment = 3, + VORBIS_packet_setup = 5 +}; + +static int codebook_decode_scalar_raw(vorb *f, Codebook *c) +{ + int i; + prep_huffman(f); + + if (c->codewords == NULL && c->sorted_codewords == NULL) + return -1; + + // cases to use binary search: sorted_codewords && !c->codewords + // sorted_codewords && c->entries > 8 + if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) { + // binary search + uint32 code = bit_reverse(f->acc); + int x=0, n=c->sorted_entries, len; + + while (n > 1) { + // invariant: sc[x] <= code < sc[x+n] + int m = x + (n >> 1); + if (c->sorted_codewords[m] <= code) { + x = m; + n -= (n>>1); + } else { + n >>= 1; + } + } + // x is now the sorted index + if (!c->sparse) x = c->sorted_values[x]; + // x is now sorted index if sparse, or symbol otherwise + len = c->codeword_lengths[x]; + if (f->valid_bits >= len) { + f->acc >>= len; + f->valid_bits -= len; + return x; + } + + f->valid_bits = 0; + return -1; + } + + // if small, linear search + assert(!c->sparse); + for (i=0; i < c->entries; ++i) { + if (c->codeword_lengths[i] == NO_CODE) continue; + if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) { + if (f->valid_bits >= c->codeword_lengths[i]) { + f->acc >>= c->codeword_lengths[i]; + f->valid_bits -= c->codeword_lengths[i]; + return i; + } + f->valid_bits = 0; + return -1; + } + } + + error(f, VORBIS_invalid_stream); + f->valid_bits = 0; + return -1; +} + +#ifndef STB_VORBIS_NO_INLINE_DECODE + +#define DECODE_RAW(var, f,c) \ + if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \ + prep_huffman(f); \ + var = f->acc & FAST_HUFFMAN_TABLE_MASK; \ + var = c->fast_huffman[var]; \ + if (var >= 0) { \ + int n = c->codeword_lengths[var]; \ + f->acc >>= n; \ + f->valid_bits -= n; \ + if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \ + } else { \ + var = codebook_decode_scalar_raw(f,c); \ + } + +#else + +static int codebook_decode_scalar(vorb *f, Codebook *c) +{ + int i; + if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) + prep_huffman(f); + // fast huffman table lookup + i = f->acc & FAST_HUFFMAN_TABLE_MASK; + i = c->fast_huffman[i]; + if (i >= 0) { + f->acc >>= c->codeword_lengths[i]; + f->valid_bits -= c->codeword_lengths[i]; + if (f->valid_bits < 0) { f->valid_bits = 0; return -1; } + return i; + } + return codebook_decode_scalar_raw(f,c); +} + +#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c); + +#endif + +#define DECODE(var,f,c) \ + DECODE_RAW(var,f,c) \ + if (c->sparse) var = c->sorted_values[var]; + +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c) +#else + #define DECODE_VQ(var,f,c) DECODE(var,f,c) +#endif + + + + + + +// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case +// where we avoid one addition +#define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_BASE(c) (0) + +static int codebook_decode_start(vorb *f, Codebook *c) +{ + int z = -1; + + // type 0 is only legal in a scalar context + if (c->lookup_type == 0) + error(f, VORBIS_invalid_stream); + else { + DECODE_VQ(z,f,c); + if (c->sparse) assert(z < c->sorted_entries); + if (z < 0) { // check for EOP + if (!f->bytes_in_seg) + if (f->last_seg) + return z; + error(f, VORBIS_invalid_stream); + } + } + return z; +} + +static int codebook_decode(vorb *f, Codebook *c, float *output, int len) +{ + int i,z = codebook_decode_start(f,c); + if (z < 0) return FALSE; + if (len > c->dimensions) len = c->dimensions; + +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + float last = CODEBOOK_ELEMENT_BASE(c); + int div = 1; + for (i=0; i < len; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + output[i] += val; + if (c->sequence_p) last = val + c->minimum_value; + div *= c->lookup_values; + } + return TRUE; + } +#endif + + z *= c->dimensions; + if (c->sequence_p) { + float last = CODEBOOK_ELEMENT_BASE(c); + for (i=0; i < len; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + output[i] += val; + last = val + c->minimum_value; + } + } else { + float last = CODEBOOK_ELEMENT_BASE(c); + for (i=0; i < len; ++i) { + output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last; + } + } + + return TRUE; +} + +static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) +{ + int i,z = codebook_decode_start(f,c); + float last = CODEBOOK_ELEMENT_BASE(c); + if (z < 0) return FALSE; + if (len > c->dimensions) len = c->dimensions; + +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int div = 1; + for (i=0; i < len; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + output[i*step] += val; + if (c->sequence_p) last = val; + div *= c->lookup_values; + } + return TRUE; + } +#endif + + z *= c->dimensions; + for (i=0; i < len; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + output[i*step] += val; + if (c->sequence_p) last = val; + } + + return TRUE; +} + +static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) +{ + int c_inter = *c_inter_p; + int p_inter = *p_inter_p; + int i,z, effective = c->dimensions; + + // type 0 is only legal in a scalar context + if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream); + + while (total_decode > 0) { + float last = CODEBOOK_ELEMENT_BASE(c); + DECODE_VQ(z,f,c); + #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + assert(!c->sparse || z < c->sorted_entries); + #endif + if (z < 0) { + if (!f->bytes_in_seg) + if (f->last_seg) return FALSE; + return error(f, VORBIS_invalid_stream); + } + + // if this will take us off the end of the buffers, stop short! + // we check by computing the length of the virtual interleaved + // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter), + // and the length we'll be using (effective) + if (c_inter + p_inter*ch + effective > len * ch) { + effective = len*ch - (p_inter*ch - c_inter); + } + + #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int div = 1; + for (i=0; i < effective; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + if (c->sequence_p) last = val; + div *= c->lookup_values; + } + } else + #endif + { + z *= c->dimensions; + if (c->sequence_p) { + for (i=0; i < effective; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + last = val; + } + } else { + for (i=0; i < effective; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + } + } + } + + total_decode -= effective; + } + *c_inter_p = c_inter; + *p_inter_p = p_inter; + return TRUE; +} + +static int predict_point(int x, int x0, int x1, int y0, int y1) +{ + int dy = y1 - y0; + int adx = x1 - x0; + // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86? + int err = abs(dy) * (x - x0); + int off = err / adx; + return dy < 0 ? y0 - off : y0 + off; +} + +// the following table is block-copied from the specification +static float inverse_db_table[256] = +{ + 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, + 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, + 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, + 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, + 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, + 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, + 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, + 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, + 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, + 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, + 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, + 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, + 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, + 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, + 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, + 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, + 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, + 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, + 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, + 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, + 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, + 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, + 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, + 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, + 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, + 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, + 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, + 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, + 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, + 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, + 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, + 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, + 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, + 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, + 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, + 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, + 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, + 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, + 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, + 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, + 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, + 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, + 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, + 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, + 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, + 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, + 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, + 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, + 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, + 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, + 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, + 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, + 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, + 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, + 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, + 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, + 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, + 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, + 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, + 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, + 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, + 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, + 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, + 0.82788260f, 0.88168307f, 0.9389798f, 1.0f +}; + + +// @OPTIMIZE: if you want to replace this bresenham line-drawing routine, +// note that you must produce bit-identical output to decode correctly; +// this specific sequence of operations is specified in the spec (it's +// drawing integer-quantized frequency-space lines that the encoder +// expects to be exactly the same) +// ... also, isn't the whole point of Bresenham's algorithm to NOT +// have to divide in the setup? sigh. +#ifndef STB_VORBIS_NO_DEFER_FLOOR +#define LINE_OP(a,b) a *= b +#else +#define LINE_OP(a,b) a = b +#endif + +#ifdef STB_VORBIS_DIVIDE_TABLE +#define DIVTAB_NUMER 32 +#define DIVTAB_DENOM 64 +int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB +#endif + +static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) +{ + int dy = y1 - y0; + int adx = x1 - x0; + int ady = abs(dy); + int base; + int x=x0,y=y0; + int err = 0; + int sy; + +#ifdef STB_VORBIS_DIVIDE_TABLE + if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) { + if (dy < 0) { + base = -integer_divide_table[ady][adx]; + sy = base-1; + } else { + base = integer_divide_table[ady][adx]; + sy = base+1; + } + } else { + base = dy / adx; + if (dy < 0) + sy = base - 1; + else + sy = base+1; + } +#else + base = dy / adx; + if (dy < 0) + sy = base - 1; + else + sy = base+1; +#endif + ady -= abs(base) * adx; + if (x1 > n) x1 = n; + if (x < x1) { + LINE_OP(output[x], inverse_db_table[y&255]); + for (++x; x < x1; ++x) { + err += ady; + if (err >= adx) { + err -= adx; + y += sy; + } else + y += base; + LINE_OP(output[x], inverse_db_table[y&255]); + } + } +} + +static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) +{ + int k; + if (rtype == 0) { + int step = n / book->dimensions; + for (k=0; k < step; ++k) + if (!codebook_decode_step(f, book, target+offset+k, n-offset-k, step)) + return FALSE; + } else { + for (k=0; k < n; ) { + if (!codebook_decode(f, book, target+offset, n-k)) + return FALSE; + k += book->dimensions; + offset += book->dimensions; + } + } + return TRUE; +} + +// n is 1/2 of the blocksize -- +// specification: "Correct per-vector decode length is [n]/2" +static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) +{ + int i,j,pass; + Residue *r = f->residue_config + rn; + int rtype = f->residue_types[rn]; + int c = r->classbook; + int classwords = f->codebooks[c].dimensions; + unsigned int actual_size = rtype == 2 ? n*2 : n; + unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size); + unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size); + int n_read = limit_r_end - limit_r_begin; + int part_read = n_read / r->part_size; + int temp_alloc_point = temp_alloc_save(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + uint8 ***part_classdata = (uint8 ***) temp_block_array(f,f->channels, part_read * sizeof(**part_classdata)); + #else + int **classifications = (int **) temp_block_array(f,f->channels, part_read * sizeof(**classifications)); + #endif + + CHECK(f); + + for (i=0; i < ch; ++i) + if (!do_not_decode[i]) + memset(residue_buffers[i], 0, sizeof(float) * n); + + if (rtype == 2 && ch != 1) { + for (j=0; j < ch; ++j) + if (!do_not_decode[j]) + break; + if (j == ch) + goto done; + + for (pass=0; pass < 8; ++pass) { + int pcount = 0, class_set = 0; + if (ch == 2) { + while (pcount < part_read) { + int z = r->begin + pcount*r->part_size; + int c_inter = (z & 1), p_inter = z>>1; + if (pass == 0) { + Codebook *c = f->codebooks+r->classbook; + int q; + DECODE(q,f,c); + if (q == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[0][class_set] = r->classdata[q]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[0][i+pcount] = q % r->classifications; + q /= r->classifications; + } + #endif + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + int z = r->begin + pcount*r->part_size; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[0][class_set][i]; + #else + int c = classifications[0][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + Codebook *book = f->codebooks + b; + #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + #else + // saves 1% + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + #endif + } else { + z += r->part_size; + c_inter = z & 1; + p_inter = z >> 1; + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } else if (ch > 2) { + while (pcount < part_read) { + int z = r->begin + pcount*r->part_size; + int c_inter = z % ch, p_inter = z/ch; + if (pass == 0) { + Codebook *c = f->codebooks+r->classbook; + int q; + DECODE(q,f,c); + if (q == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[0][class_set] = r->classdata[q]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[0][i+pcount] = q % r->classifications; + q /= r->classifications; + } + #endif + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + int z = r->begin + pcount*r->part_size; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[0][class_set][i]; + #else + int c = classifications[0][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + Codebook *book = f->codebooks + b; + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + } else { + z += r->part_size; + c_inter = z % ch; + p_inter = z / ch; + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } + } + goto done; + } + CHECK(f); + + for (pass=0; pass < 8; ++pass) { + int pcount = 0, class_set=0; + while (pcount < part_read) { + if (pass == 0) { + for (j=0; j < ch; ++j) { + if (!do_not_decode[j]) { + Codebook *c = f->codebooks+r->classbook; + int temp; + DECODE(temp,f,c); + if (temp == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[j][class_set] = r->classdata[temp]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[j][i+pcount] = temp % r->classifications; + temp /= r->classifications; + } + #endif + } + } + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + for (j=0; j < ch; ++j) { + if (!do_not_decode[j]) { + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[j][class_set][i]; + #else + int c = classifications[j][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + float *target = residue_buffers[j]; + int offset = r->begin + pcount * r->part_size; + int n = r->part_size; + Codebook *book = f->codebooks + b; + if (!residue_decode(f, book, target, offset, n, rtype)) + goto done; + } + } + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } + done: + CHECK(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + temp_free(f,part_classdata); + #else + temp_free(f,classifications); + #endif + temp_alloc_restore(f,temp_alloc_point); +} + + +#if 0 +// slow way for debugging +void inverse_mdct_slow(float *buffer, int n) +{ + int i,j; + int n2 = n >> 1; + float *x = (float *) malloc(sizeof(*x) * n2); + memcpy(x, buffer, sizeof(*x) * n2); + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n2; ++j) + // formula from paper: + //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); + // formula from wikipedia + //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); + // these are equivalent, except the formula from the paper inverts the multiplier! + // however, what actually works is NO MULTIPLIER!?! + //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); + acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); + buffer[i] = acc; + } + free(x); +} +#elif 0 +// same as above, but just barely able to run in real time on modern machines +void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) +{ + float mcos[16384]; + int i,j; + int n2 = n >> 1, nmask = (n << 2) -1; + float *x = (float *) malloc(sizeof(*x) * n2); + memcpy(x, buffer, sizeof(*x) * n2); + for (i=0; i < 4*n; ++i) + mcos[i] = (float) cos(M_PI / 2 * i / n); + + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n2; ++j) + acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask]; + buffer[i] = acc; + } + free(x); +} +#elif 0 +// transform to use a slow dct-iv; this is STILL basically trivial, +// but only requires half as many ops +void dct_iv_slow(float *buffer, int n) +{ + float mcos[16384]; + float x[2048]; + int i,j; + int n2 = n >> 1, nmask = (n << 3) - 1; + memcpy(x, buffer, sizeof(*x) * n); + for (i=0; i < 8*n; ++i) + mcos[i] = (float) cos(M_PI / 4 * i / n); + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n; ++j) + acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask]; + buffer[i] = acc; + } +} + +void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) +{ + int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4; + float temp[4096]; + + memcpy(temp, buffer, n2 * sizeof(float)); + dct_iv_slow(temp, n2); // returns -c'-d, a-b' + + for (i=0; i < n4 ; ++i) buffer[i] = temp[i+n4]; // a-b' + for ( ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d' + for ( ; i < n ; ++i) buffer[i] = -temp[i - n3_4]; // c'+d +} +#endif + +#ifndef LIBVORBIS_MDCT +#define LIBVORBIS_MDCT 0 +#endif + +#if LIBVORBIS_MDCT +// directly call the vorbis MDCT using an interface documented +// by Jeff Roberts... useful for performance comparison +typedef struct +{ + int n; + int log2n; + + float *trig; + int *bitrev; + + float scale; +} mdct_lookup; + +extern void mdct_init(mdct_lookup *lookup, int n); +extern void mdct_clear(mdct_lookup *l); +extern void mdct_backward(mdct_lookup *init, float *in, float *out); + +mdct_lookup M1,M2; + +void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) +{ + mdct_lookup *M; + if (M1.n == n) M = &M1; + else if (M2.n == n) M = &M2; + else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } + else { + if (M2.n) __asm int 3; + mdct_init(&M2, n); + M = &M2; + } + + mdct_backward(M, buffer, buffer); +} +#endif + + +// the following were split out into separate functions while optimizing; +// they could be pushed back up but eh. __forceinline showed no change; +// they're probably already being inlined. +static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) +{ + float *ee0 = e + i_off; + float *ee2 = ee0 + k_off; + int i; + + assert((n & 3) == 0); + for (i=(n>>2); i > 0; --i) { + float k00_20, k01_21; + k00_20 = ee0[ 0] - ee2[ 0]; + k01_21 = ee0[-1] - ee2[-1]; + ee0[ 0] += ee2[ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0]; + ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1]; + ee2[ 0] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-1] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-2] - ee2[-2]; + k01_21 = ee0[-3] - ee2[-3]; + ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3]; + ee2[-2] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-3] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-4] - ee2[-4]; + k01_21 = ee0[-5] - ee2[-5]; + ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5]; + ee2[-4] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-5] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-6] - ee2[-6]; + k01_21 = ee0[-7] - ee2[-7]; + ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7]; + ee2[-6] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-7] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + ee0 -= 8; + ee2 -= 8; + } +} + +static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) +{ + int i; + float k00_20, k01_21; + + float *e0 = e + d0; + float *e2 = e0 + k_off; + + for (i=lim >> 2; i > 0; --i) { + k00_20 = e0[-0] - e2[-0]; + k01_21 = e0[-1] - e2[-1]; + e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0]; + e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1]; + e2[-0] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-1] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-2] - e2[-2]; + k01_21 = e0[-3] - e2[-3]; + e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2]; + e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3]; + e2[-2] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-3] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-4] - e2[-4]; + k01_21 = e0[-5] - e2[-5]; + e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4]; + e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5]; + e2[-4] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-5] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-6] - e2[-6]; + k01_21 = e0[-7] - e2[-7]; + e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6]; + e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7]; + e2[-6] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-7] = (k01_21)*A[0] + (k00_20) * A[1]; + + e0 -= 8; + e2 -= 8; + + A += k1; + } +} + +static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) +{ + int i; + float A0 = A[0]; + float A1 = A[0+1]; + float A2 = A[0+a_off]; + float A3 = A[0+a_off+1]; + float A4 = A[0+a_off*2+0]; + float A5 = A[0+a_off*2+1]; + float A6 = A[0+a_off*3+0]; + float A7 = A[0+a_off*3+1]; + + float k00,k11; + + float *ee0 = e +i_off; + float *ee2 = ee0+k_off; + + for (i=n; i > 0; --i) { + k00 = ee0[ 0] - ee2[ 0]; + k11 = ee0[-1] - ee2[-1]; + ee0[ 0] = ee0[ 0] + ee2[ 0]; + ee0[-1] = ee0[-1] + ee2[-1]; + ee2[ 0] = (k00) * A0 - (k11) * A1; + ee2[-1] = (k11) * A0 + (k00) * A1; + + k00 = ee0[-2] - ee2[-2]; + k11 = ee0[-3] - ee2[-3]; + ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] = ee0[-3] + ee2[-3]; + ee2[-2] = (k00) * A2 - (k11) * A3; + ee2[-3] = (k11) * A2 + (k00) * A3; + + k00 = ee0[-4] - ee2[-4]; + k11 = ee0[-5] - ee2[-5]; + ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] = ee0[-5] + ee2[-5]; + ee2[-4] = (k00) * A4 - (k11) * A5; + ee2[-5] = (k11) * A4 + (k00) * A5; + + k00 = ee0[-6] - ee2[-6]; + k11 = ee0[-7] - ee2[-7]; + ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] = ee0[-7] + ee2[-7]; + ee2[-6] = (k00) * A6 - (k11) * A7; + ee2[-7] = (k11) * A6 + (k00) * A7; + + ee0 -= k0; + ee2 -= k0; + } +} + +static __forceinline void iter_54(float *z) +{ + float k00,k11,k22,k33; + float y0,y1,y2,y3; + + k00 = z[ 0] - z[-4]; + y0 = z[ 0] + z[-4]; + y2 = z[-2] + z[-6]; + k22 = z[-2] - z[-6]; + + z[-0] = y0 + y2; // z0 + z4 + z2 + z6 + z[-2] = y0 - y2; // z0 + z4 - z2 - z6 + + // done with y0,y2 + + k33 = z[-3] - z[-7]; + + z[-4] = k00 + k33; // z0 - z4 + z3 - z7 + z[-6] = k00 - k33; // z0 - z4 - z3 + z7 + + // done with k33 + + k11 = z[-1] - z[-5]; + y1 = z[-1] + z[-5]; + y3 = z[-3] + z[-7]; + + z[-1] = y1 + y3; // z1 + z5 + z3 + z7 + z[-3] = y1 - y3; // z1 + z5 - z3 - z7 + z[-5] = k11 - k22; // z1 - z5 + z2 - z6 + z[-7] = k11 + k22; // z1 - z5 - z2 + z6 +} + +static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) +{ + int a_off = base_n >> 3; + float A2 = A[0+a_off]; + float *z = e + i_off; + float *base = z - 16 * n; + + while (z > base) { + float k00,k11; + float l00,l11; + + k00 = z[-0] - z[ -8]; + k11 = z[-1] - z[ -9]; + l00 = z[-2] - z[-10]; + l11 = z[-3] - z[-11]; + z[ -0] = z[-0] + z[ -8]; + z[ -1] = z[-1] + z[ -9]; + z[ -2] = z[-2] + z[-10]; + z[ -3] = z[-3] + z[-11]; + z[ -8] = k00; + z[ -9] = k11; + z[-10] = (l00+l11) * A2; + z[-11] = (l11-l00) * A2; + + k00 = z[ -4] - z[-12]; + k11 = z[ -5] - z[-13]; + l00 = z[ -6] - z[-14]; + l11 = z[ -7] - z[-15]; + z[ -4] = z[ -4] + z[-12]; + z[ -5] = z[ -5] + z[-13]; + z[ -6] = z[ -6] + z[-14]; + z[ -7] = z[ -7] + z[-15]; + z[-12] = k11; + z[-13] = -k00; + z[-14] = (l11-l00) * A2; + z[-15] = (l00+l11) * -A2; + + iter_54(z); + iter_54(z-8); + z -= 16; + } +} + +static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) +{ + int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; + int ld; + // @OPTIMIZE: reduce register pressure by using fewer variables? + int save_point = temp_alloc_save(f); + float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2)); + float *u=NULL,*v=NULL; + // twiddle factors + float *A = f->A[blocktype]; + + // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" + // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function. + + // kernel from paper + + + // merged: + // copy and reflect spectral data + // step 0 + + // note that it turns out that the items added together during + // this step are, in fact, being added to themselves (as reflected + // by step 0). inexplicable inefficiency! this became obvious + // once I combined the passes. + + // so there's a missing 'times 2' here (for adding X to itself). + // this propagates through linearly to the end, where the numbers + // are 1/2 too small, and need to be compensated for. + + { + float *d,*e, *AA, *e_stop; + d = &buf2[n2-2]; + AA = A; + e = &buffer[0]; + e_stop = &buffer[n2]; + while (e != e_stop) { + d[1] = (e[0] * AA[0] - e[2]*AA[1]); + d[0] = (e[0] * AA[1] + e[2]*AA[0]); + d -= 2; + AA += 2; + e += 4; + } + + e = &buffer[n2-3]; + while (d >= buf2) { + d[1] = (-e[2] * AA[0] - -e[0]*AA[1]); + d[0] = (-e[2] * AA[1] + -e[0]*AA[0]); + d -= 2; + AA += 2; + e -= 4; + } + } + + // now we use symbolic names for these, so that we can + // possibly swap their meaning as we change which operations + // are in place + + u = buffer; + v = buf2; + + // step 2 (paper output is w, now u) + // this could be in place, but the data ends up in the wrong + // place... _somebody_'s got to swap it, so this is nominated + { + float *AA = &A[n2-8]; + float *d0,*d1, *e0, *e1; + + e0 = &v[n4]; + e1 = &v[0]; + + d0 = &u[n4]; + d1 = &u[0]; + + while (AA >= A) { + float v40_20, v41_21; + + v41_21 = e0[1] - e1[1]; + v40_20 = e0[0] - e1[0]; + d0[1] = e0[1] + e1[1]; + d0[0] = e0[0] + e1[0]; + d1[1] = v41_21*AA[4] - v40_20*AA[5]; + d1[0] = v40_20*AA[4] + v41_21*AA[5]; + + v41_21 = e0[3] - e1[3]; + v40_20 = e0[2] - e1[2]; + d0[3] = e0[3] + e1[3]; + d0[2] = e0[2] + e1[2]; + d1[3] = v41_21*AA[0] - v40_20*AA[1]; + d1[2] = v40_20*AA[0] + v41_21*AA[1]; + + AA -= 8; + + d0 += 4; + d1 += 4; + e0 += 4; + e1 += 4; + } + } + + // step 3 + ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + + // optimized step 3: + + // the original step3 loop can be nested r inside s or s inside r; + // it's written originally as s inside r, but this is dumb when r + // iterates many times, and s few. So I have two copies of it and + // switch between them halfway. + + // this is iteration 0 of step 3 + imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A); + imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A); + + // this is iteration 1 of step 3 + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16); + + l=2; + for (; l < (ld-3)>>1; ++l) { + int k0 = n >> (l+2), k0_2 = k0>>1; + int lim = 1 << (l+1); + int i; + for (i=0; i < lim; ++i) + imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3)); + } + + for (; l < ld-6; ++l) { + int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1; + int rlim = n >> (l+6), r; + int lim = 1 << (l+1); + int i_off; + float *A0 = A; + i_off = n2-1; + for (r=rlim; r > 0; --r) { + imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0); + A0 += k1*4; + i_off -= 8; + } + } + + // iterations with count: + // ld-6,-5,-4 all interleaved together + // the big win comes from getting rid of needless flops + // due to the constants on pass 5 & 4 being all 1 and 0; + // combining them to be simultaneous to improve cache made little difference + imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n); + + // output is u + + // step 4, 5, and 6 + // cannot be in-place because of step 5 + { + uint16 *bitrev = f->bit_reverse[blocktype]; + // weirdly, I'd have thought reading sequentially and writing + // erratically would have been better than vice-versa, but in + // fact that's not what my testing showed. (That is, with + // j = bitreverse(i), do you read i and write j, or read j and write i.) + + float *d0 = &v[n4-4]; + float *d1 = &v[n2-4]; + while (d0 >= v) { + int k4; + + k4 = bitrev[0]; + d1[3] = u[k4+0]; + d1[2] = u[k4+1]; + d0[3] = u[k4+2]; + d0[2] = u[k4+3]; + + k4 = bitrev[1]; + d1[1] = u[k4+0]; + d1[0] = u[k4+1]; + d0[1] = u[k4+2]; + d0[0] = u[k4+3]; + + d0 -= 4; + d1 -= 4; + bitrev += 2; + } + } + // (paper output is u, now v) + + + // data must be in buf2 + assert(v == buf2); + + // step 7 (paper output is v, now v) + // this is now in place + { + float *C = f->C[blocktype]; + float *d, *e; + + d = v; + e = v + n2 - 4; + + while (d < e) { + float a02,a11,b0,b1,b2,b3; + + a02 = d[0] - e[2]; + a11 = d[1] + e[3]; + + b0 = C[1]*a02 + C[0]*a11; + b1 = C[1]*a11 - C[0]*a02; + + b2 = d[0] + e[ 2]; + b3 = d[1] - e[ 3]; + + d[0] = b2 + b0; + d[1] = b3 + b1; + e[2] = b2 - b0; + e[3] = b1 - b3; + + a02 = d[2] - e[0]; + a11 = d[3] + e[1]; + + b0 = C[3]*a02 + C[2]*a11; + b1 = C[3]*a11 - C[2]*a02; + + b2 = d[2] + e[ 0]; + b3 = d[3] - e[ 1]; + + d[2] = b2 + b0; + d[3] = b3 + b1; + e[0] = b2 - b0; + e[1] = b1 - b3; + + C += 4; + d += 4; + e -= 4; + } + } + + // data must be in buf2 + + + // step 8+decode (paper output is X, now buffer) + // this generates pairs of data a la 8 and pushes them directly through + // the decode kernel (pushing rather than pulling) to avoid having + // to make another pass later + + // this cannot POSSIBLY be in place, so we refer to the buffers directly + + { + float *d0,*d1,*d2,*d3; + + float *B = f->B[blocktype] + n2 - 8; + float *e = buf2 + n2 - 8; + d0 = &buffer[0]; + d1 = &buffer[n2-4]; + d2 = &buffer[n2]; + d3 = &buffer[n-4]; + while (e >= v) { + float p0,p1,p2,p3; + + p3 = e[6]*B[7] - e[7]*B[6]; + p2 = -e[6]*B[6] - e[7]*B[7]; + + d0[0] = p3; + d1[3] = - p3; + d2[0] = p2; + d3[3] = p2; + + p1 = e[4]*B[5] - e[5]*B[4]; + p0 = -e[4]*B[4] - e[5]*B[5]; + + d0[1] = p1; + d1[2] = - p1; + d2[1] = p0; + d3[2] = p0; + + p3 = e[2]*B[3] - e[3]*B[2]; + p2 = -e[2]*B[2] - e[3]*B[3]; + + d0[2] = p3; + d1[1] = - p3; + d2[2] = p2; + d3[1] = p2; + + p1 = e[0]*B[1] - e[1]*B[0]; + p0 = -e[0]*B[0] - e[1]*B[1]; + + d0[3] = p1; + d1[0] = - p1; + d2[3] = p0; + d3[0] = p0; + + B -= 8; + e -= 8; + d0 += 4; + d2 += 4; + d1 -= 4; + d3 -= 4; + } + } + + temp_free(f,buf2); + temp_alloc_restore(f,save_point); +} + +#if 0 +// this is the original version of the above code, if you want to optimize it from scratch +void inverse_mdct_naive(float *buffer, int n) +{ + float s; + float A[1 << 12], B[1 << 12], C[1 << 11]; + int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; + int n3_4 = n - n4, ld; + // how can they claim this only uses N words?! + // oh, because they're only used sparsely, whoops + float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13]; + // set up twiddle factors + + for (k=k2=0; k < n4; ++k,k2+=2) { + A[k2 ] = (float) cos(4*k*M_PI/n); + A[k2+1] = (float) -sin(4*k*M_PI/n); + B[k2 ] = (float) cos((k2+1)*M_PI/n/2); + B[k2+1] = (float) sin((k2+1)*M_PI/n/2); + } + for (k=k2=0; k < n8; ++k,k2+=2) { + C[k2 ] = (float) cos(2*(k2+1)*M_PI/n); + C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n); + } + + // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" + // Note there are bugs in that pseudocode, presumably due to them attempting + // to rename the arrays nicely rather than representing the way their actual + // implementation bounces buffers back and forth. As a result, even in the + // "some formulars corrected" version, a direct implementation fails. These + // are noted below as "paper bug". + + // copy and reflect spectral data + for (k=0; k < n2; ++k) u[k] = buffer[k]; + for ( ; k < n ; ++k) u[k] = -buffer[n - k - 1]; + // kernel from paper + // step 1 + for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) { + v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3])*A[k2+1]; + v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3])*A[k2]; + } + // step 2 + for (k=k4=0; k < n8; k+=1, k4+=4) { + w[n2+3+k4] = v[n2+3+k4] + v[k4+3]; + w[n2+1+k4] = v[n2+1+k4] + v[k4+1]; + w[k4+3] = (v[n2+3+k4] - v[k4+3])*A[n2-4-k4] - (v[n2+1+k4]-v[k4+1])*A[n2-3-k4]; + w[k4+1] = (v[n2+1+k4] - v[k4+1])*A[n2-4-k4] + (v[n2+3+k4]-v[k4+3])*A[n2-3-k4]; + } + // step 3 + ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + for (l=0; l < ld-3; ++l) { + int k0 = n >> (l+2), k1 = 1 << (l+3); + int rlim = n >> (l+4), r4, r; + int s2lim = 1 << (l+2), s2; + for (r=r4=0; r < rlim; r4+=4,++r) { + for (s2=0; s2 < s2lim; s2+=2) { + u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4]; + u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4]; + u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1] + - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1]; + u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1] + + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1]; + } + } + if (l+1 < ld-3) { + // paper bug: ping-ponging of u&w here is omitted + memcpy(w, u, sizeof(u)); + } + } + + // step 4 + for (i=0; i < n8; ++i) { + int j = bit_reverse(i) >> (32-ld+3); + assert(j < n8); + if (i == j) { + // paper bug: original code probably swapped in place; if copying, + // need to directly copy in this case + int i8 = i << 3; + v[i8+1] = u[i8+1]; + v[i8+3] = u[i8+3]; + v[i8+5] = u[i8+5]; + v[i8+7] = u[i8+7]; + } else if (i < j) { + int i8 = i << 3, j8 = j << 3; + v[j8+1] = u[i8+1], v[i8+1] = u[j8 + 1]; + v[j8+3] = u[i8+3], v[i8+3] = u[j8 + 3]; + v[j8+5] = u[i8+5], v[i8+5] = u[j8 + 5]; + v[j8+7] = u[i8+7], v[i8+7] = u[j8 + 7]; + } + } + // step 5 + for (k=0; k < n2; ++k) { + w[k] = v[k*2+1]; + } + // step 6 + for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) { + u[n-1-k2] = w[k4]; + u[n-2-k2] = w[k4+1]; + u[n3_4 - 1 - k2] = w[k4+2]; + u[n3_4 - 2 - k2] = w[k4+3]; + } + // step 7 + for (k=k2=0; k < n8; ++k, k2 += 2) { + v[n2 + k2 ] = ( u[n2 + k2] + u[n-2-k2] + C[k2+1]*(u[n2+k2]-u[n-2-k2]) + C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2; + v[n-2 - k2] = ( u[n2 + k2] + u[n-2-k2] - C[k2+1]*(u[n2+k2]-u[n-2-k2]) - C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2; + v[n2+1+ k2] = ( u[n2+1+k2] - u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; + v[n-1 - k2] = (-u[n2+1+k2] + u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; + } + // step 8 + for (k=k2=0; k < n4; ++k,k2 += 2) { + X[k] = v[k2+n2]*B[k2 ] + v[k2+1+n2]*B[k2+1]; + X[n2-1-k] = v[k2+n2]*B[k2+1] - v[k2+1+n2]*B[k2 ]; + } + + // decode kernel to output + // determined the following value experimentally + // (by first figuring out what made inverse_mdct_slow work); then matching that here + // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?) + s = 0.5; // theoretically would be n4 + + // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code, + // so it needs to use the "old" B values to behave correctly, or else + // set s to 1.0 ]]] + for (i=0; i < n4 ; ++i) buffer[i] = s * X[i+n4]; + for ( ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1]; + for ( ; i < n ; ++i) buffer[i] = -s * X[i - n3_4]; +} +#endif + +static float *get_window(vorb *f, int len) +{ + len <<= 1; + if (len == f->blocksize_0) return f->window[0]; + if (len == f->blocksize_1) return f->window[1]; + return NULL; +} + +#ifndef STB_VORBIS_NO_DEFER_FLOOR +typedef int16 YTYPE; +#else +typedef int YTYPE; +#endif +static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) +{ + int n2 = n >> 1; + int s = map->chan[i].mux, floor; + floor = map->submap_floor[s]; + if (f->floor_types[floor] == 0) { + return error(f, VORBIS_invalid_stream); + } else { + Floor1 *g = &f->floor_config[floor].floor1; + int j,q; + int lx = 0, ly = finalY[0] * g->floor1_multiplier; + for (q=1; q < g->values; ++q) { + j = g->sorted_order[q]; + #ifndef STB_VORBIS_NO_DEFER_FLOOR + STBV_NOTUSED(step2_flag); + if (finalY[j] >= 0) + #else + if (step2_flag[j]) + #endif + { + int hy = finalY[j] * g->floor1_multiplier; + int hx = g->Xlist[j]; + if (lx != hx) + draw_line(target, lx,ly, hx,hy, n2); + CHECK(f); + lx = hx, ly = hy; + } + } + if (lx < n2) { + // optimization of: draw_line(target, lx,ly, n,ly, n2); + for (j=lx; j < n2; ++j) + LINE_OP(target[j], inverse_db_table[ly]); + CHECK(f); + } + } + return TRUE; +} + +// The meaning of "left" and "right" +// +// For a given frame: +// we compute samples from 0..n +// window_center is n/2 +// we'll window and mix the samples from left_start to left_end with data from the previous frame +// all of the samples from left_end to right_start can be output without mixing; however, +// this interval is 0-length except when transitioning between short and long frames +// all of the samples from right_start to right_end need to be mixed with the next frame, +// which we don't have, so those get saved in a buffer +// frame N's right_end-right_start, the number of samples to mix with the next frame, +// has to be the same as frame N+1's left_end-left_start (which they are by +// construction) + +static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) +{ + Mode *m; + int i, n, prev, next, window_center; + f->channel_buffer_start = f->channel_buffer_end = 0; + + retry: + if (f->eof) return FALSE; + if (!maybe_start_packet(f)) + return FALSE; + // check packet type + if (get_bits(f,1) != 0) { + if (IS_PUSH_MODE(f)) + return error(f,VORBIS_bad_packet_type); + while (EOP != get8_packet(f)); + goto retry; + } + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + + i = get_bits(f, ilog(f->mode_count-1)); + if (i == EOP) return FALSE; + if (i >= f->mode_count) return FALSE; + *mode = i; + m = f->mode_config + i; + if (m->blockflag) { + n = f->blocksize_1; + prev = get_bits(f,1); + next = get_bits(f,1); + } else { + prev = next = 0; + n = f->blocksize_0; + } + +// WINDOWING + + window_center = n >> 1; + if (m->blockflag && !prev) { + *p_left_start = (n - f->blocksize_0) >> 2; + *p_left_end = (n + f->blocksize_0) >> 2; + } else { + *p_left_start = 0; + *p_left_end = window_center; + } + if (m->blockflag && !next) { + *p_right_start = (n*3 - f->blocksize_0) >> 2; + *p_right_end = (n*3 + f->blocksize_0) >> 2; + } else { + *p_right_start = window_center; + *p_right_end = n; + } + + return TRUE; +} + +static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) +{ + Mapping *map; + int i,j,k,n,n2; + int zero_channel[256]; + int really_zero_channel[256]; + +// WINDOWING + + STBV_NOTUSED(left_end); + n = f->blocksize[m->blockflag]; + map = &f->mapping[m->mapping]; + +// FLOORS + n2 = n >> 1; + + CHECK(f); + + for (i=0; i < f->channels; ++i) { + int s = map->chan[i].mux, floor; + zero_channel[i] = FALSE; + floor = map->submap_floor[s]; + if (f->floor_types[floor] == 0) { + return error(f, VORBIS_invalid_stream); + } else { + Floor1 *g = &f->floor_config[floor].floor1; + if (get_bits(f, 1)) { + short *finalY; + uint8 step2_flag[256]; + static int range_list[4] = { 256, 128, 86, 64 }; + int range = range_list[g->floor1_multiplier-1]; + int offset = 2; + finalY = f->finalY[i]; + finalY[0] = get_bits(f, ilog(range)-1); + finalY[1] = get_bits(f, ilog(range)-1); + for (j=0; j < g->partitions; ++j) { + int pclass = g->partition_class_list[j]; + int cdim = g->class_dimensions[pclass]; + int cbits = g->class_subclasses[pclass]; + int csub = (1 << cbits)-1; + int cval = 0; + if (cbits) { + Codebook *c = f->codebooks + g->class_masterbooks[pclass]; + DECODE(cval,f,c); + } + for (k=0; k < cdim; ++k) { + int book = g->subclass_books[pclass][cval & csub]; + cval = cval >> cbits; + if (book >= 0) { + int temp; + Codebook *c = f->codebooks + book; + DECODE(temp,f,c); + finalY[offset++] = temp; + } else + finalY[offset++] = 0; + } + } + if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec + step2_flag[0] = step2_flag[1] = 1; + for (j=2; j < g->values; ++j) { + int low, high, pred, highroom, lowroom, room, val; + low = g->neighbors[j][0]; + high = g->neighbors[j][1]; + //neighbors(g->Xlist, j, &low, &high); + pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]); + val = finalY[j]; + highroom = range - pred; + lowroom = pred; + if (highroom < lowroom) + room = highroom * 2; + else + room = lowroom * 2; + if (val) { + step2_flag[low] = step2_flag[high] = 1; + step2_flag[j] = 1; + if (val >= room) + if (highroom > lowroom) + finalY[j] = val - lowroom + pred; + else + finalY[j] = pred - val + highroom - 1; + else + if (val & 1) + finalY[j] = pred - ((val+1)>>1); + else + finalY[j] = pred + (val>>1); + } else { + step2_flag[j] = 0; + finalY[j] = pred; + } + } + +#ifdef STB_VORBIS_NO_DEFER_FLOOR + do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag); +#else + // defer final floor computation until _after_ residue + for (j=0; j < g->values; ++j) { + if (!step2_flag[j]) + finalY[j] = -1; + } +#endif + } else { + error: + zero_channel[i] = TRUE; + } + // So we just defer everything else to later + + // at this point we've decoded the floor into buffer + } + } + CHECK(f); + // at this point we've decoded all floors + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + + // re-enable coupled channels if necessary + memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels); + for (i=0; i < map->coupling_steps; ++i) + if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) { + zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE; + } + + CHECK(f); +// RESIDUE DECODE + for (i=0; i < map->submaps; ++i) { + float *residue_buffers[STB_VORBIS_MAX_CHANNELS]; + int r; + uint8 do_not_decode[256]; + int ch = 0; + for (j=0; j < f->channels; ++j) { + if (map->chan[j].mux == i) { + if (zero_channel[j]) { + do_not_decode[ch] = TRUE; + residue_buffers[ch] = NULL; + } else { + do_not_decode[ch] = FALSE; + residue_buffers[ch] = f->channel_buffers[j]; + } + ++ch; + } + } + r = map->submap_residue[i]; + decode_residue(f, residue_buffers, ch, n2, r, do_not_decode); + } + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + CHECK(f); + +// INVERSE COUPLING + for (i = map->coupling_steps-1; i >= 0; --i) { + int n2 = n >> 1; + float *m = f->channel_buffers[map->chan[i].magnitude]; + float *a = f->channel_buffers[map->chan[i].angle ]; + for (j=0; j < n2; ++j) { + float a2,m2; + if (m[j] > 0) + if (a[j] > 0) + m2 = m[j], a2 = m[j] - a[j]; + else + a2 = m[j], m2 = m[j] + a[j]; + else + if (a[j] > 0) + m2 = m[j], a2 = m[j] + a[j]; + else + a2 = m[j], m2 = m[j] - a[j]; + m[j] = m2; + a[j] = a2; + } + } + CHECK(f); + + // finish decoding the floors +#ifndef STB_VORBIS_NO_DEFER_FLOOR + for (i=0; i < f->channels; ++i) { + if (really_zero_channel[i]) { + memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); + } else { + do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL); + } + } +#else + for (i=0; i < f->channels; ++i) { + if (really_zero_channel[i]) { + memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); + } else { + for (j=0; j < n2; ++j) + f->channel_buffers[i][j] *= f->floor_buffers[i][j]; + } + } +#endif + +// INVERSE MDCT + CHECK(f); + for (i=0; i < f->channels; ++i) + inverse_mdct(f->channel_buffers[i], n, f, m->blockflag); + CHECK(f); + + // this shouldn't be necessary, unless we exited on an error + // and want to flush to get to the next packet + flush_packet(f); + + if (f->first_decode) { + // assume we start so first non-discarded sample is sample 0 + // this isn't to spec, but spec would require us to read ahead + // and decode the size of all current frames--could be done, + // but presumably it's not a commonly used feature + f->current_loc = 0u - n2; // start of first frame is positioned for discard (NB this is an intentional unsigned overflow/wrap-around) + // we might have to discard samples "from" the next frame too, + // if we're lapping a large block then a small at the start? + f->discard_samples_deferred = n - right_end; + f->current_loc_valid = TRUE; + f->first_decode = FALSE; + } else if (f->discard_samples_deferred) { + if (f->discard_samples_deferred >= right_start - left_start) { + f->discard_samples_deferred -= (right_start - left_start); + left_start = right_start; + *p_left = left_start; + } else { + left_start += f->discard_samples_deferred; + *p_left = left_start; + f->discard_samples_deferred = 0; + } + } else if (f->previous_length == 0 && f->current_loc_valid) { + // we're recovering from a seek... that means we're going to discard + // the samples from this packet even though we know our position from + // the last page header, so we need to update the position based on + // the discarded samples here + // but wait, the code below is going to add this in itself even + // on a discard, so we don't need to do it here... + } + + // check if we have ogg information about the sample # for this packet + if (f->last_seg_which == f->end_seg_with_known_loc) { + // if we have a valid current loc, and this is final: + if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) { + uint32 current_end = f->known_loc_for_packet; + // then let's infer the size of the (probably) short final frame + if (current_end < f->current_loc + (right_end-left_start)) { + if (current_end < f->current_loc) { + // negative truncation, that's impossible! + *len = 0; + } else { + *len = current_end - f->current_loc; + } + *len += left_start; // this doesn't seem right, but has no ill effect on my test files + if (*len > right_end) *len = right_end; // this should never happen + f->current_loc += *len; + return TRUE; + } + } + // otherwise, just set our sample loc + // guess that the ogg granule pos refers to the _middle_ of the + // last frame? + // set f->current_loc to the position of left_start + f->current_loc = f->known_loc_for_packet - (n2-left_start); + f->current_loc_valid = TRUE; + } + if (f->current_loc_valid) + f->current_loc += (right_start - left_start); + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + *len = right_end; // ignore samples after the window goes to 0 + CHECK(f); + + return TRUE; +} + +static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) +{ + int mode, left_end, right_end; + if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0; + return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left); +} + +static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) +{ + int prev,i,j; + // we use right&left (the start of the right- and left-window sin()-regions) + // to determine how much to return, rather than inferring from the rules + // (same result, clearer code); 'left' indicates where our sin() window + // starts, therefore where the previous window's right edge starts, and + // therefore where to start mixing from the previous buffer. 'right' + // indicates where our sin() ending-window starts, therefore that's where + // we start saving, and where our returned-data ends. + + // mixin from previous window + if (f->previous_length) { + int i,j, n = f->previous_length; + float *w = get_window(f, n); + if (w == NULL) return 0; + for (i=0; i < f->channels; ++i) { + for (j=0; j < n; ++j) + f->channel_buffers[i][left+j] = + f->channel_buffers[i][left+j]*w[ j] + + f->previous_window[i][ j]*w[n-1-j]; + } + } + + prev = f->previous_length; + + // last half of this data becomes previous window + f->previous_length = len - right; + + // @OPTIMIZE: could avoid this copy by double-buffering the + // output (flipping previous_window with channel_buffers), but + // then previous_window would have to be 2x as large, and + // channel_buffers couldn't be temp mem (although they're NOT + // currently temp mem, they could be (unless we want to level + // performance by spreading out the computation)) + for (i=0; i < f->channels; ++i) + for (j=0; right+j < len; ++j) + f->previous_window[i][j] = f->channel_buffers[i][right+j]; + + if (!prev) + // there was no previous packet, so this data isn't valid... + // this isn't entirely true, only the would-have-overlapped data + // isn't valid, but this seems to be what the spec requires + return 0; + + // truncate a short frame + if (len < right) right = len; + + f->samples_output += right-left; + + return right - left; +} + +static int vorbis_pump_first_frame(stb_vorbis *f) +{ + int len, right, left, res; + res = vorbis_decode_packet(f, &len, &left, &right); + if (res) + vorbis_finish_frame(f, len, left, right); + return res; +} + +#ifndef STB_VORBIS_NO_PUSHDATA_API +static int is_whole_packet_present(stb_vorbis *f) +{ + // make sure that we have the packet available before continuing... + // this requires a full ogg parse, but we know we can fetch from f->stream + + // instead of coding this out explicitly, we could save the current read state, + // read the next packet with get8() until end-of-packet, check f->eof, then + // reset the state? but that would be slower, esp. since we'd have over 256 bytes + // of state to restore (primarily the page segment table) + + int s = f->next_seg, first = TRUE; + uint8 *p = f->stream; + + if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag + for (; s < f->segment_count; ++s) { + p += f->segments[s]; + if (f->segments[s] < 255) // stop at first short segment + break; + } + // either this continues, or it ends it... + if (s == f->segment_count) + s = -1; // set 'crosses page' flag + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + first = FALSE; + } + for (; s == -1;) { + uint8 *q; + int n; + + // check that we have the page header ready + if (p + 26 >= f->stream_end) return error(f, VORBIS_need_more_data); + // validate the page + if (memcmp(p, ogg_page_header, 4)) return error(f, VORBIS_invalid_stream); + if (p[4] != 0) return error(f, VORBIS_invalid_stream); + if (first) { // the first segment must NOT have 'continued_packet', later ones MUST + if (f->previous_length) + if ((p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); + // if no previous length, we're resynching, so we can come in on a continued-packet, + // which we'll just drop + } else { + if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); + } + n = p[26]; // segment counts + q = p+27; // q points to segment table + p = q + n; // advance past header + // make sure we've read the segment table + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + for (s=0; s < n; ++s) { + p += q[s]; + if (q[s] < 255) + break; + } + if (s == n) + s = -1; // set 'crosses page' flag + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + first = FALSE; + } + return TRUE; +} +#endif // !STB_VORBIS_NO_PUSHDATA_API + +static int start_decoder(vorb *f) +{ + uint8 header[6], x,y; + int len,i,j,k, max_submaps = 0; + int longest_floorlist=0; + + // first page, first packet + f->first_decode = TRUE; + + if (!start_page(f)) return FALSE; + // validate page flag + if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page); + // check for expected packet length + if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); + if (f->segments[0] != 30) { + // check for the Ogg skeleton fishead identifying header to refine our error + if (f->segments[0] == 64 && + getn(f, header, 6) && + header[0] == 'f' && + header[1] == 'i' && + header[2] == 's' && + header[3] == 'h' && + header[4] == 'e' && + header[5] == 'a' && + get8(f) == 'd' && + get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported); + else + return error(f, VORBIS_invalid_first_page); + } + + // read packet + // check packet header + if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page); + if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_first_page); + // vorbis_version + if (get32(f) != 0) return error(f, VORBIS_invalid_first_page); + f->channels = get8(f); if (!f->channels) return error(f, VORBIS_invalid_first_page); + if (f->channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels); + f->sample_rate = get32(f); if (!f->sample_rate) return error(f, VORBIS_invalid_first_page); + get32(f); // bitrate_maximum + get32(f); // bitrate_nominal + get32(f); // bitrate_minimum + x = get8(f); + { + int log0,log1; + log0 = x & 15; + log1 = x >> 4; + f->blocksize_0 = 1 << log0; + f->blocksize_1 = 1 << log1; + if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup); + if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup); + if (log0 > log1) return error(f, VORBIS_invalid_setup); + } + + // framing_flag + x = get8(f); + if (!(x & 1)) return error(f, VORBIS_invalid_first_page); + + // second packet! + if (!start_page(f)) return FALSE; + + if (!start_packet(f)) return FALSE; + + if (!next_segment(f)) return FALSE; + + if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup); + for (i=0; i < 6; ++i) header[i] = get8_packet(f); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); + //file vendor + len = get32_packet(f); + f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1)); + if (f->vendor == NULL) return error(f, VORBIS_outofmem); + for(i=0; i < len; ++i) { + f->vendor[i] = get8_packet(f); + } + f->vendor[len] = (char)'\0'; + //user comments + f->comment_list_length = get32_packet(f); + f->comment_list = NULL; + if (f->comment_list_length > 0) + { + f->comment_list = (char**) setup_malloc(f, sizeof(char*) * (f->comment_list_length)); + if (f->comment_list == NULL) return error(f, VORBIS_outofmem); + } + + for(i=0; i < f->comment_list_length; ++i) { + len = get32_packet(f); + f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1)); + if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem); + + for(j=0; j < len; ++j) { + f->comment_list[i][j] = get8_packet(f); + } + f->comment_list[i][len] = (char)'\0'; + } + + // framing_flag + x = get8_packet(f); + if (!(x & 1)) return error(f, VORBIS_invalid_setup); + + + skip(f, f->bytes_in_seg); + f->bytes_in_seg = 0; + + do { + len = next_segment(f); + skip(f, len); + f->bytes_in_seg = 0; + } while (len); + + // third packet! + if (!start_packet(f)) return FALSE; + + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (IS_PUSH_MODE(f)) { + if (!is_whole_packet_present(f)) { + // convert error in ogg header to write type + if (f->error == VORBIS_invalid_stream) + f->error = VORBIS_invalid_setup; + return FALSE; + } + } + #endif + + crc32_init(); // always init it, to avoid multithread race conditions + + if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup); + for (i=0; i < 6; ++i) header[i] = get8_packet(f); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); + + // codebooks + + f->codebook_count = get_bits(f,8) + 1; + f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count); + if (f->codebooks == NULL) return error(f, VORBIS_outofmem); + memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count); + for (i=0; i < f->codebook_count; ++i) { + uint32 *values; + int ordered, sorted_count; + int total=0; + uint8 *lengths; + Codebook *c = f->codebooks+i; + CHECK(f); + x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); + c->dimensions = (get_bits(f, 8)<<8) + x; + x = get_bits(f, 8); + y = get_bits(f, 8); + c->entries = (get_bits(f, 8)<<16) + (y<<8) + x; + ordered = get_bits(f,1); + c->sparse = ordered ? 0 : get_bits(f,1); + + if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup); + + if (c->sparse) + lengths = (uint8 *) setup_temp_malloc(f, c->entries); + else + lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); + + if (!lengths) return error(f, VORBIS_outofmem); + + if (ordered) { + int current_entry = 0; + int current_length = get_bits(f,5) + 1; + while (current_entry < c->entries) { + int limit = c->entries - current_entry; + int n = get_bits(f, ilog(limit)); + if (current_length >= 32) return error(f, VORBIS_invalid_setup); + if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); } + memset(lengths + current_entry, current_length, n); + current_entry += n; + ++current_length; + } + } else { + for (j=0; j < c->entries; ++j) { + int present = c->sparse ? get_bits(f,1) : 1; + if (present) { + lengths[j] = get_bits(f, 5) + 1; + ++total; + if (lengths[j] == 32) + return error(f, VORBIS_invalid_setup); + } else { + lengths[j] = NO_CODE; + } + } + } + + if (c->sparse && total >= c->entries >> 2) { + // convert sparse items to non-sparse! + if (c->entries > (int) f->setup_temp_memory_required) + f->setup_temp_memory_required = c->entries; + + c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); + if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem); + memcpy(c->codeword_lengths, lengths, c->entries); + setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs! + lengths = c->codeword_lengths; + c->sparse = 0; + } + + // compute the size of the sorted tables + if (c->sparse) { + sorted_count = total; + } else { + sorted_count = 0; + #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH + for (j=0; j < c->entries; ++j) + if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE) + ++sorted_count; + #endif + } + + c->sorted_entries = sorted_count; + values = NULL; + + CHECK(f); + if (!c->sparse) { + c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries); + if (!c->codewords) return error(f, VORBIS_outofmem); + } else { + unsigned int size; + if (c->sorted_entries) { + c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries); + if (!c->codeword_lengths) return error(f, VORBIS_outofmem); + c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries); + if (!c->codewords) return error(f, VORBIS_outofmem); + values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries); + if (!values) return error(f, VORBIS_outofmem); + } + size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries; + if (size > f->setup_temp_memory_required) + f->setup_temp_memory_required = size; + } + + if (!compute_codewords(c, lengths, c->entries, values)) { + if (c->sparse) setup_temp_free(f, values, 0); + return error(f, VORBIS_invalid_setup); + } + + if (c->sorted_entries) { + // allocate an extra slot for sentinels + c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1)); + if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem); + // allocate an extra slot at the front so that c->sorted_values[-1] is defined + // so that we can catch that case without an extra if + c->sorted_values = ( int *) setup_malloc(f, sizeof(*c->sorted_values ) * (c->sorted_entries+1)); + if (c->sorted_values == NULL) return error(f, VORBIS_outofmem); + ++c->sorted_values; + c->sorted_values[-1] = -1; + compute_sorted_huffman(c, lengths, values); + } + + if (c->sparse) { + setup_temp_free(f, values, sizeof(*values)*c->sorted_entries); + setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries); + setup_temp_free(f, lengths, c->entries); + c->codewords = NULL; + } + + compute_accelerated_huffman(c); + + CHECK(f); + c->lookup_type = get_bits(f, 4); + if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup); + if (c->lookup_type > 0) { + uint16 *mults; + c->minimum_value = float32_unpack(get_bits(f, 32)); + c->delta_value = float32_unpack(get_bits(f, 32)); + c->value_bits = get_bits(f, 4)+1; + c->sequence_p = get_bits(f,1); + if (c->lookup_type == 1) { + int values = lookup1_values(c->entries, c->dimensions); + if (values < 0) return error(f, VORBIS_invalid_setup); + c->lookup_values = (uint32) values; + } else { + c->lookup_values = c->entries * c->dimensions; + } + if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup); + mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values); + if (mults == NULL) return error(f, VORBIS_outofmem); + for (j=0; j < (int) c->lookup_values; ++j) { + int q = get_bits(f, c->value_bits); + if (q == EOP) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); } + mults[j] = q; + } + +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int len, sparse = c->sparse; + float last=0; + // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop + if (sparse) { + if (c->sorted_entries == 0) goto skip; + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions); + } else + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions); + if (c->multiplicands == NULL) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } + len = sparse ? c->sorted_entries : c->entries; + for (j=0; j < len; ++j) { + unsigned int z = sparse ? c->sorted_values[j] : j; + unsigned int div=1; + for (k=0; k < c->dimensions; ++k) { + int off = (z / div) % c->lookup_values; + float val = mults[off]*c->delta_value + c->minimum_value + last; + c->multiplicands[j*c->dimensions + k] = val; + if (c->sequence_p) + last = val; + if (k+1 < c->dimensions) { + if (div > UINT_MAX / (unsigned int) c->lookup_values) { + setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); + return error(f, VORBIS_invalid_setup); + } + div *= c->lookup_values; + } + } + } + c->lookup_type = 2; + } + else +#endif + { + float last=0; + CHECK(f); + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values); + if (c->multiplicands == NULL) { setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } + for (j=0; j < (int) c->lookup_values; ++j) { + float val = mults[j] * c->delta_value + c->minimum_value + last; + c->multiplicands[j] = val; + if (c->sequence_p) + last = val; + } + } +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + skip:; +#endif + setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); + + CHECK(f); + } + CHECK(f); + } + + // time domain transfers (notused) + + x = get_bits(f, 6) + 1; + for (i=0; i < x; ++i) { + uint32 z = get_bits(f, 16); + if (z != 0) return error(f, VORBIS_invalid_setup); + } + + // Floors + f->floor_count = get_bits(f, 6)+1; + f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config)); + if (f->floor_config == NULL) return error(f, VORBIS_outofmem); + for (i=0; i < f->floor_count; ++i) { + f->floor_types[i] = get_bits(f, 16); + if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup); + if (f->floor_types[i] == 0) { + Floor0 *g = &f->floor_config[i].floor0; + g->order = get_bits(f,8); + g->rate = get_bits(f,16); + g->bark_map_size = get_bits(f,16); + g->amplitude_bits = get_bits(f,6); + g->amplitude_offset = get_bits(f,8); + g->number_of_books = get_bits(f,4) + 1; + for (j=0; j < g->number_of_books; ++j) + g->book_list[j] = get_bits(f,8); + return error(f, VORBIS_feature_not_supported); + } else { + stbv__floor_ordering p[31*8+2]; + Floor1 *g = &f->floor_config[i].floor1; + int max_class = -1; + g->partitions = get_bits(f, 5); + for (j=0; j < g->partitions; ++j) { + g->partition_class_list[j] = get_bits(f, 4); + if (g->partition_class_list[j] > max_class) + max_class = g->partition_class_list[j]; + } + for (j=0; j <= max_class; ++j) { + g->class_dimensions[j] = get_bits(f, 3)+1; + g->class_subclasses[j] = get_bits(f, 2); + if (g->class_subclasses[j]) { + g->class_masterbooks[j] = get_bits(f, 8); + if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } + for (k=0; k < 1 << g->class_subclasses[j]; ++k) { + g->subclass_books[j][k] = (int16)get_bits(f,8)-1; + if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } + } + g->floor1_multiplier = get_bits(f,2)+1; + g->rangebits = get_bits(f,4); + g->Xlist[0] = 0; + g->Xlist[1] = 1 << g->rangebits; + g->values = 2; + for (j=0; j < g->partitions; ++j) { + int c = g->partition_class_list[j]; + for (k=0; k < g->class_dimensions[c]; ++k) { + g->Xlist[g->values] = get_bits(f, g->rangebits); + ++g->values; + } + } + // precompute the sorting + for (j=0; j < g->values; ++j) { + p[j].x = g->Xlist[j]; + p[j].id = j; + } + qsort(p, g->values, sizeof(p[0]), point_compare); + for (j=0; j < g->values-1; ++j) + if (p[j].x == p[j+1].x) + return error(f, VORBIS_invalid_setup); + for (j=0; j < g->values; ++j) + g->sorted_order[j] = (uint8) p[j].id; + // precompute the neighbors + for (j=2; j < g->values; ++j) { + int low = 0,hi = 0; + neighbors(g->Xlist, j, &low,&hi); + g->neighbors[j][0] = low; + g->neighbors[j][1] = hi; + } + + if (g->values > longest_floorlist) + longest_floorlist = g->values; + } + } + + // Residue + f->residue_count = get_bits(f, 6)+1; + f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0])); + if (f->residue_config == NULL) return error(f, VORBIS_outofmem); + memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0])); + for (i=0; i < f->residue_count; ++i) { + uint8 residue_cascade[64]; + Residue *r = f->residue_config+i; + f->residue_types[i] = get_bits(f, 16); + if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup); + r->begin = get_bits(f, 24); + r->end = get_bits(f, 24); + if (r->end < r->begin) return error(f, VORBIS_invalid_setup); + r->part_size = get_bits(f,24)+1; + r->classifications = get_bits(f,6)+1; + r->classbook = get_bits(f,8); + if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup); + for (j=0; j < r->classifications; ++j) { + uint8 high_bits=0; + uint8 low_bits=get_bits(f,3); + if (get_bits(f,1)) + high_bits = get_bits(f,5); + residue_cascade[j] = high_bits*8 + low_bits; + } + r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications); + if (r->residue_books == NULL) return error(f, VORBIS_outofmem); + for (j=0; j < r->classifications; ++j) { + for (k=0; k < 8; ++k) { + if (residue_cascade[j] & (1 << k)) { + r->residue_books[j][k] = get_bits(f, 8); + if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } else { + r->residue_books[j][k] = -1; + } + } + } + // precompute the classifications[] array to avoid inner-loop mod/divide + // call it 'classdata' since we already have r->classifications + r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); + if (!r->classdata) return error(f, VORBIS_outofmem); + memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); + for (j=0; j < f->codebooks[r->classbook].entries; ++j) { + int classwords = f->codebooks[r->classbook].dimensions; + int temp = j; + r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords); + if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem); + for (k=classwords-1; k >= 0; --k) { + r->classdata[j][k] = temp % r->classifications; + temp /= r->classifications; + } + } + } + + f->mapping_count = get_bits(f,6)+1; + f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping)); + if (f->mapping == NULL) return error(f, VORBIS_outofmem); + memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping)); + for (i=0; i < f->mapping_count; ++i) { + Mapping *m = f->mapping + i; + int mapping_type = get_bits(f,16); + if (mapping_type != 0) return error(f, VORBIS_invalid_setup); + m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan)); + if (m->chan == NULL) return error(f, VORBIS_outofmem); + if (get_bits(f,1)) + m->submaps = get_bits(f,4)+1; + else + m->submaps = 1; + if (m->submaps > max_submaps) + max_submaps = m->submaps; + if (get_bits(f,1)) { + m->coupling_steps = get_bits(f,8)+1; + if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup); + for (k=0; k < m->coupling_steps; ++k) { + m->chan[k].magnitude = get_bits(f, ilog(f->channels-1)); + m->chan[k].angle = get_bits(f, ilog(f->channels-1)); + if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup); + if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup); + if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup); + } + } else + m->coupling_steps = 0; + + // reserved field + if (get_bits(f,2)) return error(f, VORBIS_invalid_setup); + if (m->submaps > 1) { + for (j=0; j < f->channels; ++j) { + m->chan[j].mux = get_bits(f, 4); + if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup); + } + } else + // @SPECIFICATION: this case is missing from the spec + for (j=0; j < f->channels; ++j) + m->chan[j].mux = 0; + + for (j=0; j < m->submaps; ++j) { + get_bits(f,8); // discard + m->submap_floor[j] = get_bits(f,8); + m->submap_residue[j] = get_bits(f,8); + if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup); + if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup); + } + } + + // Modes + f->mode_count = get_bits(f, 6)+1; + for (i=0; i < f->mode_count; ++i) { + Mode *m = f->mode_config+i; + m->blockflag = get_bits(f,1); + m->windowtype = get_bits(f,16); + m->transformtype = get_bits(f,16); + m->mapping = get_bits(f,8); + if (m->windowtype != 0) return error(f, VORBIS_invalid_setup); + if (m->transformtype != 0) return error(f, VORBIS_invalid_setup); + if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup); + } + + flush_packet(f); + + f->previous_length = 0; + + for (i=0; i < f->channels; ++i) { + f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1); + f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); + f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist); + if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem); + memset(f->channel_buffers[i], 0, sizeof(float) * f->blocksize_1); + #ifdef STB_VORBIS_NO_DEFER_FLOOR + f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); + if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem); + #endif + } + + if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE; + if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE; + f->blocksize[0] = f->blocksize_0; + f->blocksize[1] = f->blocksize_1; + +#ifdef STB_VORBIS_DIVIDE_TABLE + if (integer_divide_table[1][1]==0) + for (i=0; i < DIVTAB_NUMER; ++i) + for (j=1; j < DIVTAB_DENOM; ++j) + integer_divide_table[i][j] = i / j; +#endif + + // compute how much temporary memory is needed + + // 1. + { + uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1); + uint32 classify_mem; + int i,max_part_read=0; + for (i=0; i < f->residue_count; ++i) { + Residue *r = f->residue_config + i; + unsigned int actual_size = f->blocksize_1 / 2; + unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size; + unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size; + int n_read = limit_r_end - limit_r_begin; + int part_read = n_read / r->part_size; + if (part_read > max_part_read) + max_part_read = part_read; + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *)); + #else + classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *)); + #endif + + // maximum reasonable partition size is f->blocksize_1 + + f->temp_memory_required = classify_mem; + if (imdct_mem > f->temp_memory_required) + f->temp_memory_required = imdct_mem; + } + + + if (f->alloc.alloc_buffer) { + assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes); + // check if there's enough temp memory so we don't error later + if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset) + return error(f, VORBIS_outofmem); + } + + // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page + // without PAGEFLAG_continued_packet, so this either points to the first page, or + // the page after the end of the headers. It might be cleaner to point to a page + // in the middle of the headers, when that's the page where the first audio packet + // starts, but we'd have to also correctly skip the end of any continued packet in + // stb_vorbis_seek_start. + if (f->next_seg == -1) { + f->first_audio_page_offset = stb_vorbis_get_file_offset(f); + } else { + f->first_audio_page_offset = 0; + } + + return TRUE; +} + +static void vorbis_deinit(stb_vorbis *p) +{ + int i,j; + + setup_free(p, p->vendor); + for (i=0; i < p->comment_list_length; ++i) { + setup_free(p, p->comment_list[i]); + } + setup_free(p, p->comment_list); + + if (p->residue_config) { + for (i=0; i < p->residue_count; ++i) { + Residue *r = p->residue_config+i; + if (r->classdata) { + for (j=0; j < p->codebooks[r->classbook].entries; ++j) + setup_free(p, r->classdata[j]); + setup_free(p, r->classdata); + } + setup_free(p, r->residue_books); + } + } + + if (p->codebooks) { + CHECK(p); + for (i=0; i < p->codebook_count; ++i) { + Codebook *c = p->codebooks + i; + setup_free(p, c->codeword_lengths); + setup_free(p, c->multiplicands); + setup_free(p, c->codewords); + setup_free(p, c->sorted_codewords); + // c->sorted_values[-1] is the first entry in the array + setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL); + } + setup_free(p, p->codebooks); + } + setup_free(p, p->floor_config); + setup_free(p, p->residue_config); + if (p->mapping) { + for (i=0; i < p->mapping_count; ++i) + setup_free(p, p->mapping[i].chan); + setup_free(p, p->mapping); + } + CHECK(p); + for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) { + setup_free(p, p->channel_buffers[i]); + setup_free(p, p->previous_window[i]); + #ifdef STB_VORBIS_NO_DEFER_FLOOR + setup_free(p, p->floor_buffers[i]); + #endif + setup_free(p, p->finalY[i]); + } + for (i=0; i < 2; ++i) { + setup_free(p, p->A[i]); + setup_free(p, p->B[i]); + setup_free(p, p->C[i]); + setup_free(p, p->window[i]); + setup_free(p, p->bit_reverse[i]); + } + #ifndef STB_VORBIS_NO_STDIO + if (p->close_on_free) fclose(p->f); + #endif +} + +void stb_vorbis_close(stb_vorbis *p) +{ + if (p == NULL) return; + vorbis_deinit(p); + setup_free(p,p); +} + +static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) +{ + memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start + if (z) { + p->alloc = *z; + p->alloc.alloc_buffer_length_in_bytes &= ~7; + p->temp_offset = p->alloc.alloc_buffer_length_in_bytes; + } + p->eof = 0; + p->error = VORBIS__no_error; + p->stream = NULL; + p->codebooks = NULL; + p->page_crc_tests = -1; + #ifndef STB_VORBIS_NO_STDIO + p->close_on_free = FALSE; + p->f = NULL; + #endif +} + +int stb_vorbis_get_sample_offset(stb_vorbis *f) +{ + if (f->current_loc_valid) + return f->current_loc; + else + return -1; +} + +stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) +{ + stb_vorbis_info d; + d.channels = f->channels; + d.sample_rate = f->sample_rate; + d.setup_memory_required = f->setup_memory_required; + d.setup_temp_memory_required = f->setup_temp_memory_required; + d.temp_memory_required = f->temp_memory_required; + d.max_frame_size = f->blocksize_1 >> 1; + return d; +} + +stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f) +{ + stb_vorbis_comment d; + d.vendor = f->vendor; + d.comment_list_length = f->comment_list_length; + d.comment_list = f->comment_list; + return d; +} + +int stb_vorbis_get_error(stb_vorbis *f) +{ + int e = f->error; + f->error = VORBIS__no_error; + return e; +} + +static stb_vorbis * vorbis_alloc(stb_vorbis *f) +{ + stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p)); + return p; +} + +#ifndef STB_VORBIS_NO_PUSHDATA_API + +void stb_vorbis_flush_pushdata(stb_vorbis *f) +{ + f->previous_length = 0; + f->page_crc_tests = 0; + f->discard_samples_deferred = 0; + f->current_loc_valid = FALSE; + f->first_decode = FALSE; + f->samples_output = 0; + f->channel_buffer_start = 0; + f->channel_buffer_end = 0; +} + +static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) +{ + int i,n; + for (i=0; i < f->page_crc_tests; ++i) + f->scan[i].bytes_done = 0; + + // if we have room for more scans, search for them first, because + // they may cause us to stop early if their header is incomplete + if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) { + if (data_len < 4) return 0; + data_len -= 3; // need to look for 4-byte sequence, so don't miss + // one that straddles a boundary + for (i=0; i < data_len; ++i) { + if (data[i] == 0x4f) { + if (0==memcmp(data+i, ogg_page_header, 4)) { + int j,len; + uint32 crc; + // make sure we have the whole page header + if (i+26 >= data_len || i+27+data[i+26] >= data_len) { + // only read up to this page start, so hopefully we'll + // have the whole page header start next time + data_len = i; + break; + } + // ok, we have it all; compute the length of the page + len = 27 + data[i+26]; + for (j=0; j < data[i+26]; ++j) + len += data[i+27+j]; + // scan everything up to the embedded crc (which we must 0) + crc = 0; + for (j=0; j < 22; ++j) + crc = crc32_update(crc, data[i+j]); + // now process 4 0-bytes + for ( ; j < 26; ++j) + crc = crc32_update(crc, 0); + // len is the total number of bytes we need to scan + n = f->page_crc_tests++; + f->scan[n].bytes_left = len-j; + f->scan[n].crc_so_far = crc; + f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24); + // if the last frame on a page is continued to the next, then + // we can't recover the sample_loc immediately + if (data[i+27+data[i+26]-1] == 255) + f->scan[n].sample_loc = ~0; + else + f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24); + f->scan[n].bytes_done = i+j; + if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT) + break; + // keep going if we still have room for more + } + } + } + } + + for (i=0; i < f->page_crc_tests;) { + uint32 crc; + int j; + int n = f->scan[i].bytes_done; + int m = f->scan[i].bytes_left; + if (m > data_len - n) m = data_len - n; + // m is the bytes to scan in the current chunk + crc = f->scan[i].crc_so_far; + for (j=0; j < m; ++j) + crc = crc32_update(crc, data[n+j]); + f->scan[i].bytes_left -= m; + f->scan[i].crc_so_far = crc; + if (f->scan[i].bytes_left == 0) { + // does it match? + if (f->scan[i].crc_so_far == f->scan[i].goal_crc) { + // Houston, we have page + data_len = n+m; // consumption amount is wherever that scan ended + f->page_crc_tests = -1; // drop out of page scan mode + f->previous_length = 0; // decode-but-don't-output one frame + f->next_seg = -1; // start a new page + f->current_loc = f->scan[i].sample_loc; // set the current sample location + // to the amount we'd have decoded had we decoded this page + f->current_loc_valid = f->current_loc != ~0U; + return data_len; + } + // delete entry + f->scan[i] = f->scan[--f->page_crc_tests]; + } else { + ++i; + } + } + + return data_len; +} + +// return value: number of bytes we used +int stb_vorbis_decode_frame_pushdata( + stb_vorbis *f, // the file we're decoding + const uint8 *data, int data_len, // the memory available for decoding + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples + ) +{ + int i; + int len,right,left; + + if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + if (f->page_crc_tests >= 0) { + *samples = 0; + return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len); + } + + f->stream = (uint8 *) data; + f->stream_end = (uint8 *) data + data_len; + f->error = VORBIS__no_error; + + // check that we have the entire packet in memory + if (!is_whole_packet_present(f)) { + *samples = 0; + return 0; + } + + if (!vorbis_decode_packet(f, &len, &left, &right)) { + // save the actual error we encountered + enum STBVorbisError error = f->error; + if (error == VORBIS_bad_packet_type) { + // flush and resynch + f->error = VORBIS__no_error; + while (get8_packet(f) != EOP) + if (f->eof) break; + *samples = 0; + return (int) (f->stream - data); + } + if (error == VORBIS_continued_packet_flag_invalid) { + if (f->previous_length == 0) { + // we may be resynching, in which case it's ok to hit one + // of these; just discard the packet + f->error = VORBIS__no_error; + while (get8_packet(f) != EOP) + if (f->eof) break; + *samples = 0; + return (int) (f->stream - data); + } + } + // if we get an error while parsing, what to do? + // well, it DEFINITELY won't work to continue from where we are! + stb_vorbis_flush_pushdata(f); + // restore the error that actually made us bail + f->error = error; + *samples = 0; + return 1; + } + + // success! + len = vorbis_finish_frame(f, len, left, right); + for (i=0; i < f->channels; ++i) + f->outputs[i] = f->channel_buffers[i] + left; + + if (channels) *channels = f->channels; + *samples = len; + *output = f->outputs; + return (int) (f->stream - data); +} + +stb_vorbis *stb_vorbis_open_pushdata( + const unsigned char *data, int data_len, // the memory available for decoding + int *data_used, // only defined if result is not NULL + int *error, const stb_vorbis_alloc *alloc) +{ + stb_vorbis *f, p; + vorbis_init(&p, alloc); + p.stream = (uint8 *) data; + p.stream_end = (uint8 *) data + data_len; + p.push_mode = TRUE; + if (!start_decoder(&p)) { + if (p.eof) + *error = VORBIS_need_more_data; + else + *error = p.error; + vorbis_deinit(&p); + return NULL; + } + f = vorbis_alloc(&p); + if (f) { + *f = p; + *data_used = (int) (f->stream - data); + *error = 0; + return f; + } else { + vorbis_deinit(&p); + return NULL; + } +} +#endif // STB_VORBIS_NO_PUSHDATA_API + +unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) +{ + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) return 0; + #endif + if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start); + #ifndef STB_VORBIS_NO_STDIO + return (unsigned int) (ftell(f->f) - f->f_start); + #endif +} + +#ifndef STB_VORBIS_NO_PULLDATA_API +// +// DATA-PULLING API +// + +static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) +{ + for(;;) { + int n; + if (f->eof) return 0; + n = get8(f); + if (n == 0x4f) { // page header candidate + unsigned int retry_loc = stb_vorbis_get_file_offset(f); + int i; + // check if we're off the end of a file_section stream + if (retry_loc - 25 > f->stream_len) + return 0; + // check the rest of the header + for (i=1; i < 4; ++i) + if (get8(f) != ogg_page_header[i]) + break; + if (f->eof) return 0; + if (i == 4) { + uint8 header[27]; + uint32 i, crc, goal, len; + for (i=0; i < 4; ++i) + header[i] = ogg_page_header[i]; + for (; i < 27; ++i) + header[i] = get8(f); + if (f->eof) return 0; + if (header[4] != 0) goto invalid; + goal = header[22] + (header[23] << 8) + (header[24]<<16) + ((uint32)header[25]<<24); + for (i=22; i < 26; ++i) + header[i] = 0; + crc = 0; + for (i=0; i < 27; ++i) + crc = crc32_update(crc, header[i]); + len = 0; + for (i=0; i < header[26]; ++i) { + int s = get8(f); + crc = crc32_update(crc, s); + len += s; + } + if (len && f->eof) return 0; + for (i=0; i < len; ++i) + crc = crc32_update(crc, get8(f)); + // finished parsing probable page + if (crc == goal) { + // we could now check that it's either got the last + // page flag set, OR it's followed by the capture + // pattern, but I guess TECHNICALLY you could have + // a file with garbage between each ogg page and recover + // from it automatically? So even though that paranoia + // might decrease the chance of an invalid decode by + // another 2^32, not worth it since it would hose those + // invalid-but-useful files? + if (end) + *end = stb_vorbis_get_file_offset(f); + if (last) { + if (header[5] & 0x04) + *last = 1; + else + *last = 0; + } + set_file_offset(f, retry_loc-1); + return 1; + } + } + invalid: + // not a valid page, so rewind and look for next one + set_file_offset(f, retry_loc); + } + } +} + + +#define SAMPLE_unknown 0xffffffff + +// seeking is implemented with a binary search, which narrows down the range to +// 64K, before using a linear search (because finding the synchronization +// pattern can be expensive, and the chance we'd find the end page again is +// relatively high for small ranges) +// +// two initial interpolation-style probes are used at the start of the search +// to try to bound either side of the binary search sensibly, while still +// working in O(log n) time if they fail. + +static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) +{ + uint8 header[27], lacing[255]; + int i,len; + + // record where the page starts + z->page_start = stb_vorbis_get_file_offset(f); + + // parse the header + getn(f, header, 27); + if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S') + return 0; + getn(f, lacing, header[26]); + + // determine the length of the payload + len = 0; + for (i=0; i < header[26]; ++i) + len += lacing[i]; + + // this implies where the page ends + z->page_end = z->page_start + 27 + header[26] + len; + + // read the last-decoded sample out of the data + z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24); + + // restore file state to where we were + set_file_offset(f, z->page_start); + return 1; +} + +// rarely used function to seek back to the preceding page while finding the +// start of a packet +static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) +{ + unsigned int previous_safe, end; + + // now we want to seek back 64K from the limit + if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset) + previous_safe = limit_offset - 65536; + else + previous_safe = f->first_audio_page_offset; + + set_file_offset(f, previous_safe); + + while (vorbis_find_page(f, &end, NULL)) { + if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset) + return 1; + set_file_offset(f, end); + } + + return 0; +} + +// implements the search logic for finding a page and starting decoding. if +// the function succeeds, current_loc_valid will be true and current_loc will +// be less than or equal to the provided sample number (the closer the +// better). +static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) +{ + ProbedPage left, right, mid; + int i, start_seg_with_known_loc, end_pos, page_start; + uint32 delta, stream_length, padding, last_sample_limit; + double offset = 0.0, bytes_per_sample = 0.0; + int probe = 0; + + // find the last page and validate the target sample + stream_length = stb_vorbis_stream_length_in_samples(f); + if (stream_length == 0) return error(f, VORBIS_seek_without_length); + if (sample_number > stream_length) return error(f, VORBIS_seek_invalid); + + // this is the maximum difference between the window-center (which is the + // actual granule position value), and the right-start (which the spec + // indicates should be the granule position (give or take one)). + padding = ((f->blocksize_1 - f->blocksize_0) >> 2); + if (sample_number < padding) + last_sample_limit = 0; + else + last_sample_limit = sample_number - padding; + + left = f->p_first; + while (left.last_decoded_sample == ~0U) { + // (untested) the first page does not have a 'last_decoded_sample' + set_file_offset(f, left.page_end); + if (!get_seek_page_info(f, &left)) goto error; + } + + right = f->p_last; + assert(right.last_decoded_sample != ~0U); + + // starting from the start is handled differently + if (last_sample_limit <= left.last_decoded_sample) { + if (stb_vorbis_seek_start(f)) { + if (f->current_loc > sample_number) + return error(f, VORBIS_seek_failed); + return 1; + } + return 0; + } + + while (left.page_end != right.page_start) { + assert(left.page_end < right.page_start); + // search range in bytes + delta = right.page_start - left.page_end; + if (delta <= 65536) { + // there's only 64K left to search - handle it linearly + set_file_offset(f, left.page_end); + } else { + if (probe < 2) { + if (probe == 0) { + // first probe (interpolate) + double data_bytes = right.page_end - left.page_start; + bytes_per_sample = data_bytes / right.last_decoded_sample; + offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample); + } else { + // second probe (try to bound the other side) + double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample; + if (error >= 0 && error < 8000) error = 8000; + if (error < 0 && error > -8000) error = -8000; + offset += error * 2; + } + + // ensure the offset is valid + if (offset < left.page_end) + offset = left.page_end; + if (offset > right.page_start - 65536) + offset = right.page_start - 65536; + + set_file_offset(f, (unsigned int) offset); + } else { + // binary search for large ranges (offset by 32K to ensure + // we don't hit the right page) + set_file_offset(f, left.page_end + (delta / 2) - 32768); + } + + if (!vorbis_find_page(f, NULL, NULL)) goto error; + } + + for (;;) { + if (!get_seek_page_info(f, &mid)) goto error; + if (mid.last_decoded_sample != ~0U) break; + // (untested) no frames end on this page + set_file_offset(f, mid.page_end); + assert(mid.page_start < right.page_start); + } + + // if we've just found the last page again then we're in a tricky file, + // and we're close enough (if it wasn't an interpolation probe). + if (mid.page_start == right.page_start) { + if (probe >= 2 || delta <= 65536) + break; + } else { + if (last_sample_limit < mid.last_decoded_sample) + right = mid; + else + left = mid; + } + + ++probe; + } + + // seek back to start of the last packet + page_start = left.page_start; + set_file_offset(f, page_start); + if (!start_page(f)) return error(f, VORBIS_seek_failed); + end_pos = f->end_seg_with_known_loc; + assert(end_pos >= 0); + + for (;;) { + for (i = end_pos; i > 0; --i) + if (f->segments[i-1] != 255) + break; + + start_seg_with_known_loc = i; + + if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet)) + break; + + // (untested) the final packet begins on an earlier page + if (!go_to_page_before(f, page_start)) + goto error; + + page_start = stb_vorbis_get_file_offset(f); + if (!start_page(f)) goto error; + end_pos = f->segment_count - 1; + } + + // prepare to start decoding + f->current_loc_valid = FALSE; + f->last_seg = FALSE; + f->valid_bits = 0; + f->packet_bytes = 0; + f->bytes_in_seg = 0; + f->previous_length = 0; + f->next_seg = start_seg_with_known_loc; + + for (i = 0; i < start_seg_with_known_loc; i++) + skip(f, f->segments[i]); + + // start decoding (optimizable - this frame is generally discarded) + if (!vorbis_pump_first_frame(f)) + return 0; + if (f->current_loc > sample_number) + return error(f, VORBIS_seek_failed); + return 1; + +error: + // try to restore the file to a valid state + stb_vorbis_seek_start(f); + return error(f, VORBIS_seek_failed); +} + +// the same as vorbis_decode_initial, but without advancing +static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) +{ + int bits_read, bytes_read; + + if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode)) + return 0; + + // either 1 or 2 bytes were read, figure out which so we can rewind + bits_read = 1 + ilog(f->mode_count-1); + if (f->mode_config[*mode].blockflag) + bits_read += 2; + bytes_read = (bits_read + 7) / 8; + + f->bytes_in_seg += bytes_read; + f->packet_bytes -= bytes_read; + skip(f, -bytes_read); + if (f->next_seg == -1) + f->next_seg = f->segment_count - 1; + else + f->next_seg--; + f->valid_bits = 0; + + return 1; +} + +int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) +{ + uint32 max_frame_samples; + + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + // fast page-level search + if (!seek_to_sample_coarse(f, sample_number)) + return 0; + + assert(f->current_loc_valid); + assert(f->current_loc <= sample_number); + + // linear search for the relevant packet + max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2; + while (f->current_loc < sample_number) { + int left_start, left_end, right_start, right_end, mode, frame_samples; + if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode)) + return error(f, VORBIS_seek_failed); + // calculate the number of samples returned by the next frame + frame_samples = right_start - left_start; + if (f->current_loc + frame_samples > sample_number) { + return 1; // the next frame will contain the sample + } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) { + // there's a chance the frame after this could contain the sample + vorbis_pump_first_frame(f); + } else { + // this frame is too early to be relevant + f->current_loc += frame_samples; + f->previous_length = 0; + maybe_start_packet(f); + flush_packet(f); + } + } + // the next frame should start with the sample + if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed); + return 1; +} + +int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) +{ + if (!stb_vorbis_seek_frame(f, sample_number)) + return 0; + + if (sample_number != f->current_loc) { + int n; + uint32 frame_start = f->current_loc; + stb_vorbis_get_frame_float(f, &n, NULL); + assert(sample_number > frame_start); + assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end); + f->channel_buffer_start += (sample_number - frame_start); + } + + return 1; +} + +int stb_vorbis_seek_start(stb_vorbis *f) +{ + if (IS_PUSH_MODE(f)) { return error(f, VORBIS_invalid_api_mixing); } + set_file_offset(f, f->first_audio_page_offset); + f->previous_length = 0; + f->first_decode = TRUE; + f->next_seg = -1; + return vorbis_pump_first_frame(f); +} + +unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) +{ + unsigned int restore_offset, previous_safe; + unsigned int end, last_page_loc; + + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + if (!f->total_samples) { + unsigned int last; + uint32 lo,hi; + char header[6]; + + // first, store the current decode position so we can restore it + restore_offset = stb_vorbis_get_file_offset(f); + + // now we want to seek back 64K from the end (the last page must + // be at most a little less than 64K, but let's allow a little slop) + if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset) + previous_safe = f->stream_len - 65536; + else + previous_safe = f->first_audio_page_offset; + + set_file_offset(f, previous_safe); + // previous_safe is now our candidate 'earliest known place that seeking + // to will lead to the final page' + + if (!vorbis_find_page(f, &end, &last)) { + // if we can't find a page, we're hosed! + f->error = VORBIS_cant_find_last_page; + f->total_samples = 0xffffffff; + goto done; + } + + // check if there are more pages + last_page_loc = stb_vorbis_get_file_offset(f); + + // stop when the last_page flag is set, not when we reach eof; + // this allows us to stop short of a 'file_section' end without + // explicitly checking the length of the section + while (!last) { + set_file_offset(f, end); + if (!vorbis_find_page(f, &end, &last)) { + // the last page we found didn't have the 'last page' flag + // set. whoops! + break; + } + //previous_safe = last_page_loc+1; // NOTE: not used after this point, but note for debugging + last_page_loc = stb_vorbis_get_file_offset(f); + } + + set_file_offset(f, last_page_loc); + + // parse the header + getn(f, (unsigned char *)header, 6); + // extract the absolute granule position + lo = get32(f); + hi = get32(f); + if (lo == 0xffffffff && hi == 0xffffffff) { + f->error = VORBIS_cant_find_last_page; + f->total_samples = SAMPLE_unknown; + goto done; + } + if (hi) + lo = 0xfffffffe; // saturate + f->total_samples = lo; + + f->p_last.page_start = last_page_loc; + f->p_last.page_end = end; + f->p_last.last_decoded_sample = lo; + + done: + set_file_offset(f, restore_offset); + } + return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples; +} + +float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) +{ + return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate; +} + + + +int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) +{ + int len, right,left,i; + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + if (!vorbis_decode_packet(f, &len, &left, &right)) { + f->channel_buffer_start = f->channel_buffer_end = 0; + return 0; + } + + len = vorbis_finish_frame(f, len, left, right); + for (i=0; i < f->channels; ++i) + f->outputs[i] = f->channel_buffers[i] + left; + + f->channel_buffer_start = left; + f->channel_buffer_end = left+len; + + if (channels) *channels = f->channels; + if (output) *output = f->outputs; + return len; +} + +#ifndef STB_VORBIS_NO_STDIO + +stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) +{ + stb_vorbis *f, p; + vorbis_init(&p, alloc); + p.f = file; + p.f_start = (uint32) ftell(file); + p.stream_len = length; + p.close_on_free = close_on_free; + if (start_decoder(&p)) { + f = vorbis_alloc(&p); + if (f) { + *f = p; + vorbis_pump_first_frame(f); + return f; + } + } + if (error) *error = p.error; + vorbis_deinit(&p); + return NULL; +} + +stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) +{ + unsigned int len, start; + start = (unsigned int) ftell(file); + fseek(file, 0, SEEK_END); + len = (unsigned int) (ftell(file) - start); + fseek(file, start, SEEK_SET); + return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len); +} + +stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) +{ + FILE *f; +#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__) + if (0 != fopen_s(&f, filename, "rb")) + f = NULL; +#else + f = fopen(filename, "rb"); +#endif + if (f) + return stb_vorbis_open_file(f, TRUE, error, alloc); + if (error) *error = VORBIS_file_open_failure; + return NULL; +} +#endif // STB_VORBIS_NO_STDIO + +stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) +{ + stb_vorbis *f, p; + if (!data) { + if (error) *error = VORBIS_unexpected_eof; + return NULL; + } + vorbis_init(&p, alloc); + p.stream = (uint8 *) data; + p.stream_end = (uint8 *) data + len; + p.stream_start = (uint8 *) p.stream; + p.stream_len = len; + p.push_mode = FALSE; + if (start_decoder(&p)) { + f = vorbis_alloc(&p); + if (f) { + *f = p; + vorbis_pump_first_frame(f); + if (error) *error = VORBIS__no_error; + return f; + } + } + if (error) *error = p.error; + vorbis_deinit(&p); + return NULL; +} + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +#define PLAYBACK_MONO 1 +#define PLAYBACK_LEFT 2 +#define PLAYBACK_RIGHT 4 + +#define L (PLAYBACK_LEFT | PLAYBACK_MONO) +#define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO) +#define R (PLAYBACK_RIGHT | PLAYBACK_MONO) + +static int8 channel_position[7][6] = +{ + { 0 }, + { C }, + { L, R }, + { L, C, R }, + { L, R, L, R }, + { L, C, R, L, R }, + { L, C, R, L, R, C }, +}; + + +#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT + typedef union { + float f; + int i; + } float_conv; + typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4]; + #define FASTDEF(x) float_conv x + // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round + #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT)) + #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22)) + #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s)) + #define check_endianness() +#else + #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s)))) + #define check_endianness() + #define FASTDEF(x) +#endif + +static void copy_samples(short *dest, float *src, int len) +{ + int i; + check_endianness(); + for (i=0; i < len; ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + dest[i] = v; + } +} + +static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) +{ + #define STB_BUFFER_SIZE 32 + float buffer[STB_BUFFER_SIZE]; + int i,j,o,n = STB_BUFFER_SIZE; + check_endianness(); + for (o = 0; o < len; o += STB_BUFFER_SIZE) { + memset(buffer, 0, sizeof(buffer)); + if (o + n > len) n = len - o; + for (j=0; j < num_c; ++j) { + if (channel_position[num_c][j] & mask) { + for (i=0; i < n; ++i) + buffer[i] += data[j][d_offset+o+i]; + } + } + for (i=0; i < n; ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + output[o+i] = v; + } + } + #undef STB_BUFFER_SIZE +} + +static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) +{ + #define STB_BUFFER_SIZE 32 + float buffer[STB_BUFFER_SIZE]; + int i,j,o,n = STB_BUFFER_SIZE >> 1; + // o is the offset in the source data + check_endianness(); + for (o = 0; o < len; o += STB_BUFFER_SIZE >> 1) { + // o2 is the offset in the output data + int o2 = o << 1; + memset(buffer, 0, sizeof(buffer)); + if (o + n > len) n = len - o; + for (j=0; j < num_c; ++j) { + int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT); + if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) { + for (i=0; i < n; ++i) { + buffer[i*2+0] += data[j][d_offset+o+i]; + buffer[i*2+1] += data[j][d_offset+o+i]; + } + } else if (m == PLAYBACK_LEFT) { + for (i=0; i < n; ++i) { + buffer[i*2+0] += data[j][d_offset+o+i]; + } + } else if (m == PLAYBACK_RIGHT) { + for (i=0; i < n; ++i) { + buffer[i*2+1] += data[j][d_offset+o+i]; + } + } + } + for (i=0; i < (n<<1); ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + output[o2+i] = v; + } + } + #undef STB_BUFFER_SIZE +} + +static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) +{ + int i; + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { + static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} }; + for (i=0; i < buf_c; ++i) + compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples); + } else { + int limit = buf_c < data_c ? buf_c : data_c; + for (i=0; i < limit; ++i) + copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples); + for ( ; i < buf_c; ++i) + memset(buffer[i]+b_offset, 0, sizeof(short) * samples); + } +} + +int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) +{ + float **output = NULL; + int len = stb_vorbis_get_frame_float(f, NULL, &output); + if (len > num_samples) len = num_samples; + if (len) + convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len); + return len; +} + +static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) +{ + int i; + check_endianness(); + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { + assert(buf_c == 2); + for (i=0; i < buf_c; ++i) + compute_stereo_samples(buffer, data_c, data, d_offset, len); + } else { + int limit = buf_c < data_c ? buf_c : data_c; + int j; + for (j=0; j < len; ++j) { + for (i=0; i < limit; ++i) { + FASTDEF(temp); + float f = data[i][d_offset+j]; + int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + *buffer++ = v; + } + for ( ; i < buf_c; ++i) + *buffer++ = 0; + } + } +} + +int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) +{ + float **output; + int len; + if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,&buffer, num_shorts); + len = stb_vorbis_get_frame_float(f, NULL, &output); + if (len) { + if (len*num_c > num_shorts) len = num_shorts / num_c; + convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len); + } + return len; +} + +int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) +{ + float **outputs; + int len = num_shorts / channels; + int n=0; + while (n < len) { + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + if (k) + convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k); + buffer += k*channels; + n += k; + f->channel_buffer_start += k; + if (n == len) break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + } + return n; +} + +int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) +{ + float **outputs; + int n=0; + while (n < len) { + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + if (k) + convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k); + n += k; + f->channel_buffer_start += k; + if (n == len) break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + } + return n; +} + +#ifndef STB_VORBIS_NO_STDIO +int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output) +{ + int data_len, offset, total, limit, error; + short *data; + stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); + if (v == NULL) return -1; + limit = v->channels * 4096; + *channels = v->channels; + if (sample_rate) + *sample_rate = v->sample_rate; + offset = data_len = 0; + total = limit; + data = (short *) malloc(total * sizeof(*data)); + if (data == NULL) { + stb_vorbis_close(v); + return -2; + } + for (;;) { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); + if (n == 0) break; + data_len += n; + offset += n * v->channels; + if (offset + limit > total) { + short *data2; + total *= 2; + data2 = (short *) realloc(data, total * sizeof(*data)); + if (data2 == NULL) { + free(data); + stb_vorbis_close(v); + return -2; + } + data = data2; + } + } + *output = data; + stb_vorbis_close(v); + return data_len; +} +#endif // NO_STDIO + +int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output) +{ + int data_len, offset, total, limit, error; + short *data; + stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL); + if (v == NULL) return -1; + limit = v->channels * 4096; + *channels = v->channels; + if (sample_rate) + *sample_rate = v->sample_rate; + offset = data_len = 0; + total = limit; + data = (short *) malloc(total * sizeof(*data)); + if (data == NULL) { + stb_vorbis_close(v); + return -2; + } + for (;;) { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); + if (n == 0) break; + data_len += n; + offset += n * v->channels; + if (offset + limit > total) { + short *data2; + total *= 2; + data2 = (short *) realloc(data, total * sizeof(*data)); + if (data2 == NULL) { + free(data); + stb_vorbis_close(v); + return -2; + } + data = data2; + } + } + *output = data; + stb_vorbis_close(v); + return data_len; +} +#endif // STB_VORBIS_NO_INTEGER_CONVERSION + +int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) +{ + float **outputs; + int len = num_floats / channels; + int n=0; + int z = f->channels; + if (z > channels) z = channels; + while (n < len) { + int i,j; + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + for (j=0; j < k; ++j) { + for (i=0; i < z; ++i) + *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j]; + for ( ; i < channels; ++i) + *buffer++ = 0; + } + n += k; + f->channel_buffer_start += k; + if (n == len) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; + } + return n; +} + +int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) +{ + float **outputs; + int n=0; + int z = f->channels; + if (z > channels) z = channels; + while (n < num_samples) { + int i; + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= num_samples) k = num_samples - n; + if (k) { + for (i=0; i < z; ++i) + memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start, sizeof(float)*k); + for ( ; i < channels; ++i) + memset(buffer[i]+n, 0, sizeof(float) * k); + } + n += k; + f->channel_buffer_start += k; + if (n == num_samples) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; + } + return n; +} +#endif // STB_VORBIS_NO_PULLDATA_API + +/* Version history + 1.17 - 2019-07-08 - fix CVE-2019-13217, -13218, -13219, -13220, -13221, -13222, -13223 + found with Mayhem by ForAllSecure + 1.16 - 2019-03-04 - fix warnings + 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found + 1.14 - 2018-02-11 - delete bogus dealloca usage + 1.13 - 2018-01-29 - fix truncation of last frame (hopefully) + 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files + 1.11 - 2017-07-23 - fix MinGW compilation + 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory + 1.09 - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version + 1.08 - 2016-04-02 - fixed multiple warnings; fix setup memory leaks; + avoid discarding last frame of audio data + 1.07 - 2015-01-16 - fixed some warnings, fix mingw, const-correct API + some more crash fixes when out of memory or with corrupt files + 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson) + some crash fixes when out of memory or with corrupt files + 1.05 - 2015-04-19 - don't define __forceinline if it's redundant + 1.04 - 2014-08-27 - fix missing const-correct case in API + 1.03 - 2014-08-07 - Warning fixes + 1.02 - 2014-07-09 - Declare qsort compare function _cdecl on windows + 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float + 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in multichannel + (API change) report sample rate for decode-full-file funcs + 0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila + 0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem + 0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence + 0.99993 - remove assert that fired on legal files with empty tables + 0.99992 - rewind-to-start + 0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo + 0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++ + 0.9998 - add a full-decode function with a memory source + 0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition + 0.9996 - query length of vorbis stream in samples/seconds + 0.9995 - bugfix to another optimization that only happened in certain files + 0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors + 0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation + 0.9992 - performance improvement of IMDCT; now performs close to reference implementation + 0.9991 - performance improvement of IMDCT + 0.999 - (should have been 0.9990) performance improvement of IMDCT + 0.998 - no-CRT support from Casey Muratori + 0.997 - bugfixes for bugs found by Terje Mathisen + 0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen + 0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen + 0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen + 0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen + 0.992 - fixes for MinGW warning + 0.991 - turn fast-float-conversion on by default + 0.990 - fix push-mode seek recovery if you seek into the headers + 0.98b - fix to bad release of 0.98 + 0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode + 0.97 - builds under c++ (typecasting, don't use 'class' keyword) + 0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code + 0.95 - clamping code for 16-bit functions + 0.94 - not publically released + 0.93 - fixed all-zero-floor case (was decoding garbage) + 0.92 - fixed a memory leak + 0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION + 0.90 - first public release +*/ + +#endif // STB_VORBIS_HEADER_ONLY + + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/game.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/game.h new file mode 100644 index 00000000..06ab1e2d --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/game.h @@ -0,0 +1,36 @@ +// From RecompTrophy by lamarking (https://github.com/cptlamarca/RecompTrophy), +// GPL-3.0, used with permission (commit f78630c). Changed for the port: in the +// recomptrophy namespace, and without Read(): the port fills Info from the +// running game's XDBF (native_trophies.cpp), the same fields +// RecompTrophy reads from default.xex. +// +// An Xbox 360 game's achievements, read from its disc image: the image's +// default.xex (the GDFX file system: sector 32 of the game partition holds +// the root folder), its executable decrypted and decompressed (the retail or +// devkit key; raw, basic or LZX compression) and the XDBF in its resources +// (XACH achievements, XSTR strings per language, PNG pictures). + +#pragma once + +#include <cstdint> +#include <string> +#include <vector> + +namespace recomptrophy::game { + +struct Achievement { + uint32_t id = 0; + std::string name[2], detail[2]; // English, Portuguese (English when none) + uint32_t gamerscore = 0; + bool hidden = false; // (no "show unachieved" flag) + std::vector<uint8_t> picture; // PNG (64x64 on the Xbox) +}; + +struct Info { + uint32_t title_id = 0; + std::string title; + std::vector<uint8_t> icon; // PNG + std::vector<Achievement> achievements; +}; + +} // namespace recomptrophy::game diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.cpp new file mode 100644 index 00000000..1518a54e --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.cpp @@ -0,0 +1,406 @@ +// From RecompTrophy by lamarking (https://github.com/cptlamarca/RecompTrophy), +// GPL-3.0, used with permission (commit f78630c). Changed for the port: see +// install.h. +// +// See install.h. + +#include "install.h" + +#include <arpa/inet.h> +#include <dirent.h> +#include <fcntl.h> +#include <netinet/in.h> +#include <poll.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <unistd.h> + +#include <algorithm> +#include <cerrno> +#include <chrono> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <ctime> +#include <fstream> +#include <mutex> +#include <sstream> +#include <thread> + +namespace recomptrophy::install { +namespace { + +// [ps5 port] the payloads, from the app's payloads/ folder (never freed: the +// thread using them may still run while the game exits) +std::mutex& payloads_mutex = *new std::mutex; +std::vector<uint8_t>& relay_elf = *new std::vector<uint8_t>; +std::vector<uint8_t>& watcher_elf = *new std::vector<uint8_t>; + +std::vector<uint8_t> ReadAll(const std::string& path) { + std::ifstream in(path, std::ios::binary); + return std::vector<uint8_t>((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); +} + +bool Exists(const std::string& path) { + FILE* f = std::fopen(path.c_str(), "rb"); + if (f) std::fclose(f); + return f != nullptr; +} + +// [ps5 port] With time limits, and a loader that hangs up can't end the game +// (no SIGPIPE). +bool SendPayload(const std::vector<uint8_t>& elf) { + if (elf.empty()) return false; + const int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) return false; + timeval limit{}; + limit.tv_sec = 5; + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &limit, sizeof(limit)); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &limit, sizeof(limit)); +#ifdef SO_NOSIGPIPE + const int one = 1; + setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); +#endif +#ifdef MSG_NOSIGNAL + const int flags = MSG_NOSIGNAL; +#else + const int flags = 0; +#endif + sockaddr_in address = {}; + address.sin_family = AF_INET; + address.sin_port = htons(9021); + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + // (connect with a 3 second limit; a plain connect when the socket can't + // be made non-blocking) + const int mode = fcntl(sock, F_GETFL, 0); + const bool nonblocking = mode >= 0 && fcntl(sock, F_SETFL, mode | O_NONBLOCK) == 0; + bool sent = connect(sock, reinterpret_cast<sockaddr*>(&address), sizeof(address)) == 0; + if (!sent && nonblocking && errno == EINPROGRESS) { + const auto until = std::chrono::steady_clock::now() + std::chrono::seconds(3); + pollfd p = {sock, POLLOUT, 0}; + int ready = -1; + while (ready < 0) { + const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(until - std::chrono::steady_clock::now()); + if (left.count() <= 0) break; + ready = poll(&p, 1, int(left.count())); + if (ready < 0 && errno != EINTR) break; + } + int error = 0; + socklen_t length = sizeof(error); + sent = ready == 1 && getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &length) == 0 && error == 0; + } + if (sent && nonblocking) sent = fcntl(sock, F_SETFL, mode) == 0; + const uint8_t* at = elf.data(); + const uint8_t* end = elf.data() + elf.size(); + while (sent && at < end) { + const ssize_t n = send(sock, at, size_t(end - at), flags); + if (n < 0 && errno == EINTR) continue; + sent = n > 0; + at += sent ? n : 0; + } + if (sent) shutdown(sock, SHUT_WR); + close(sock); + return sent; +} + +bool SendRelay() { + std::vector<uint8_t> elf; + { + std::lock_guard<std::mutex> lock(payloads_mutex); + elf = relay_elf; + } + return SendPayload(elf); +} + +// Files named `name` under `folder` (to `depth` folders down), skipping the +// big ones that never hold them (a game's files, caches). +void FindFiles(const std::string& folder, const std::string& name, int depth, std::vector<std::string>& out) { + DIR* dir = opendir(folder.c_str()); + if (!dir) return; + static const char* const kSkip[] = {"game", "assets", "cache", "shader_cache", "radv-shader-cache", "textures", + "logs", "dlc", "install", "sce_module", "shaders"}; + while (dirent* entry = readdir(dir)) { + const std::string n = entry->d_name; + if (n == "." || n == "..") continue; + const std::string path = folder + "/" + n; + if (n == name) { + out.push_back(path); + } else if (depth > 0 && (entry->d_type == DT_DIR || entry->d_type == DT_UNKNOWN) && + std::none_of(std::begin(kSkip), std::end(kSkip), [&](const char* k) { return n == k; })) { + FindFiles(path, name, depth - 1, out); + } + } + closedir(dir); +} + +uint32_t Be32(const uint8_t* p) { return uint32_t(p[0]) << 24 | uint32_t(p[1]) << 16 | uint32_t(p[2]) << 8 | p[3]; } + +// The achievements earned in an Xbox profile's game file (TITLEID.gpd, as the +// Xbox 360 and its emulators keep them): an XDBF whose section 1 entries are +// the achievements (size, id, picture, gamerscore, flags, unlock time; 0x20000 +// in the flags: earned). +std::vector<std::pair<uint32_t, int64_t>> EarnedInGpd(const std::string& path) { + std::vector<std::pair<uint32_t, int64_t>> out; + const std::vector<uint8_t> d = ReadAll(path); + if (d.size() < 24 || std::memcmp(d.data(), "XDBF", 4) != 0) return out; + const uint32_t table = Be32(&d[8]), used = Be32(&d[12]), free_table = Be32(&d[16]); + const size_t data = 24 + size_t(table) * 18 + size_t(free_table) * 8; + for (uint32_t i = 0; i < used && 24 + (i + 1) * 18 <= d.size(); ++i) { + const uint8_t* e = &d[24 + i * 18]; + if ((e[0] << 8 | e[1]) != 1) continue; + const size_t at = data + Be32(e + 10), size = Be32(e + 14); + if (size < 28 || at + size > d.size()) continue; + const uint8_t* a = &d[at]; + const uint32_t id = Be32(a + 4), flags = Be32(a + 16); + const uint64_t filetime = uint64_t(Be32(a + 20)) << 32 | Be32(a + 24); + if (!(flags & 0x20000) || id == 0 || id > 127) continue; + out.emplace_back(id, filetime ? int64_t(filetime / 10000000ull) - 11644473600ll : int64_t(std::time(nullptr))); + } + return out; +} + +} // namespace + +void SetPayloads(std::vector<uint8_t> relay, std::vector<uint8_t> watcher) { + std::lock_guard<std::mutex> lock(payloads_mutex); + relay_elf = std::move(relay); + watcher_elf = std::move(watcher); +} + +// "titleName" of a param.json (the first: its default language's). +std::string TitleName(const std::string& param) { + const std::string json(param); + const size_t key = json.find("\"titleName\""); + if (key == std::string::npos) return {}; + const size_t open = json.find('"', json.find(':', key) + 1); + if (open == std::string::npos) return {}; + std::string name; + for (size_t i = open + 1; i < json.size() && json[i] != '"'; ++i) { + if (json[i] == '\\' && i + 1 < json.size()) ++i; + name += json[i]; + } + return name; +} + +// [unlocked.N] / filetime = F (Windows file time) lines of a port's file. +std::vector<std::pair<uint32_t, int64_t>> EarnedInToml(const std::string& path) { + std::vector<std::pair<uint32_t, int64_t>> out; + std::ifstream in(path); + std::string line; + uint32_t id = 0; + bool in_entry = false; + while (std::getline(in, line)) { + unsigned n = 0; + unsigned long long filetime = 0; + if (std::sscanf(line.c_str(), "[unlocked.%u]", &n) == 1) { + id = n, in_entry = true; + } else if (in_entry && std::sscanf(line.c_str(), " filetime = %llu", &filetime) == 1) { + const int64_t unix_time = + filetime ? int64_t(filetime / 10000000ull) - 11644473600ll : int64_t(std::time(nullptr)); + out.emplace_back(id, unix_time); + in_entry = false; + } + } + return out; +} + +bool FindPort(uint32_t title_id, Port& port) { + char id[16]; + std::snprintf(id, sizeof(id), "%08X", title_id); + DIR* dir = opendir("/data/homebrew"); + if (!dir) return false; + std::vector<std::pair<uint32_t, int64_t>> earned; + bool found = false, named = false; + while (dirent* entry = readdir(dir)) { + if (entry->d_name[0] == '.') continue; + const std::string folder = std::string("/data/homebrew/") + entry->d_name; + // a ReXGlue port's (UserData/.../achievements/TITLEID.toml: its own + // folder, as the ports, or deeper, as LibertyRecomp's; made at the first + // achievement: also known by the achievement list beside it, + // TITLEID-trophies.txt, or the port's shaders - then the file's path is + // watched for before it's there) + std::vector<std::string> tomls, lists, gpds, profiles; + FindFiles(folder + "/UserData", std::string(id) + ".toml", 4, tomls); + FindFiles(folder + "/UserData", std::string(id) + "-trophies.txt", 4, lists); + for (const std::string& l : lists) { + const std::string t = l.substr(0, l.rfind('/') + 1) + id + ".toml"; + if (std::find(tomls.begin(), tomls.end(), t) == tomls.end()) tomls.push_back(t); + } + const bool own_shaders = Exists(folder + "/shaders/" + id + ".xsh"); + if (tomls.empty() && own_shaders) tomls.push_back(folder + "/UserData/achievements/" + id + ".toml"); + for (const std::string& t : tomls) { + if (t.find("/achievements/") == std::string::npos) continue; + for (const auto& e : EarnedInToml(t)) earned.push_back(e); + port.sources.push_back(t); + if (!named) { + // (its name and icon: the port's own app) + const std::vector<uint8_t> param = ReadAll(folder + "/sce_sys/param.json"); + port.folder = folder; + port.name = TitleName(std::string(param.begin(), param.end())); + port.icon = ReadAll(folder + "/sce_sys/icon0.png"); + named = true; + } + found = true; + } + // an Xbox 360 emulator's profiles (saves/XUID/FFFE07D1/00010000/XUID/: + // the profile's FFFE07D1.gpd and a TITLEID.gpd per game, made when it's + // first played): their achievements only (the emulator isn't the game) + FindFiles(folder + "/saves", "FFFE07D1.gpd", 5, profiles); + for (const std::string& profile : profiles) { + const std::string g = profile.substr(0, profile.rfind('/') + 1) + id + ".gpd"; + for (const auto& e : EarnedInGpd(g)) earned.push_back(e); + port.sources.push_back(g); + found = true; + } + } + closedir(dir); + // each achievement once, at the earliest it was earned + std::sort(earned.begin(), earned.end()); + for (const auto& e : earned) { + if (port.earned.empty() || port.earned.back().first != e.first) port.earned.push_back(e); + } + return found; +} + +Result Run(const std::string& set_id, int32_t user, const std::vector<std::pair<uint32_t, int64_t>>& earned, + size_t achievement_count) { + Result result; + const std::string folder = kJobFolder; + { + std::ofstream out(folder + "/unlocked.txt", std::ios::trunc); + int64_t last = 0; + for (const auto& [id, when] : earned) { + out << id << ' ' << when << '\n'; + last = std::max(last, when); + } + if (achievement_count && earned.size() >= achievement_count) out << 0 << ' ' << last << '\n'; + } + const std::string job_id = std::to_string(uint32_t(std::time(nullptr))); + std::remove((folder + "/result.txt").c_str()); + { + std::ofstream out(folder + "/job.txt", std::ios::trunc); + char hex[16]; + std::snprintf(hex, sizeof(hex), "%x", unsigned(user)); + out << job_id << ' ' << set_id << ' ' << hex << '\n'; + } + if (!SendRelay()) { + result.loader = false; + result.error = "The payload loader (port 9021: etaHEN, elfldr...) didn't answer."; + return result; + } + const auto sent_at = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - sent_at < std::chrono::seconds(20)) { + std::ifstream in(folder + "/result.txt"); + std::string id, set, status; + if (in >> id >> set >> status >> result.set_state >> result.progress_state && id == job_id) { + result.ok = status == "OK"; + if (!result.ok) { + result.error = result.progress_state == "no-template" + ? "The profile has no PS5 game with trophies yet: any PS5 game with trophies has to " + "be played once first." + : "The console didn't take the trophies."; + } + return result; + } + std::this_thread::sleep_for(std::chrono::milliseconds(150)); + } + result.error = "The relay didn't answer."; + return result; +} + +bool Watch(const std::string& set_id, int32_t user, const std::string& name, + const std::vector<std::pair<uint32_t, uint32_t>>& trophies, const std::vector<int>& grades, + const std::vector<std::string>& names, const std::vector<std::string>& sources) { + const std::string folder = std::string(kWatchFolder) + "/" + set_id; + mkdir("/data/trophies360", 0777); + mkdir(kWatchFolder, 0777); + mkdir(folder.c_str(), 0777); + { + std::ofstream out(folder + "/game.txt", std::ios::trunc); + char hex[16]; + std::snprintf(hex, sizeof(hex), "%x", unsigned(user)); + out << set_id << ' ' << hex << '\n' << name << '\n'; + } + { + // "ACHIEVEMENT TROPHY GRADE NAME" + std::ofstream out(folder + "/map.txt", std::ios::trunc); + for (size_t i = 0; i < trophies.size(); ++i) { + out << trophies[i].first << ' ' << trophies[i].second << ' ' << grades[i] << ' ' << names[i] << '\n'; + } + } + std::ofstream out(folder + "/sources.txt", std::ios::trunc); + for (const std::string& source : sources) out << source << '\n'; + return bool(out); +} + +bool WatcherAlive() { + // (its heartbeat: the time it wrote last) + std::ifstream in(std::string(kWatchFolder) + "/alive"); + long long at = 0; + return in >> at && std::llabs(int64_t(std::time(nullptr)) - at) < 15; +} + +bool StartWatcher() { + if (WatcherAlive()) return true; + mkdir("/data/trophies360", 0777); + mkdir(kWatchFolder, 0777); + std::vector<uint8_t> elf; + { + std::lock_guard<std::mutex> lock(payloads_mutex); + elf = watcher_elf; + } + return SendPayload(elf); +} + +bool AddToAutoload() { + constexpr char kManager[] = "/data/pldmgr"; + struct stat st; + if (stat(kManager, &st) != 0 || !S_ISDIR(st.st_mode)) return false; // (no Payload Manager) + const std::string folder = std::string(kManager) + "/payloads/local/recomptrophy-watcher"; + const std::string elf = folder + "/RecompTrophy-Watcher.elf"; + mkdir((std::string(kManager) + "/payloads").c_str(), 0777); + mkdir((std::string(kManager) + "/payloads/local").c_str(), 0777); + mkdir(folder.c_str(), 0777); + // [ps5 port] the watcher, when there's none (RecompTrophy's own, maybe newer, + // is left alone) + if (!Exists(elf)) { + std::vector<uint8_t> watcher; + { + std::lock_guard<std::mutex> lock(payloads_mutex); + watcher = watcher_elf; + } + if (watcher.empty()) return false; + std::ofstream out(elf + ".tmp", std::ios::binary | std::ios::trunc); + out.write(reinterpret_cast<const char*>(watcher.data()), std::streamsize(watcher.size())); + out.close(); + if (!out || std::rename((elf + ".tmp").c_str(), elf.c_str()) != 0) return false; + } + if (!Exists(folder + "/meta.json")) { + char when[40]; + const std::time_t now = std::time(nullptr); + std::strftime(when, sizeof(when), "%Y-%m-%dT%H:%M:%S+0000", std::gmtime(&now)); + std::ofstream(folder + "/meta.json", std::ios::trunc) + << "{\n \"schema\": 2,\n \"kind\": \"local\",\n \"filename\": \"RecompTrophy-Watcher.elf\",\n" + << " \"installed_at\": \"" << when << "\",\n \"origin\": \"web_upload\",\n \"origin_detail\": \"\"\n}\n"; + } + return true; +} + +bool InAutoloadList() { + std::ifstream in("/data/pldmgr/autoload.txt"); + std::string line; + while (std::getline(in, line)) { + line.erase(line.find_last_not_of("\r\n") + 1); + if (line == "local/recomptrophy-watcher") return true; + } + return false; +} + +bool SendRelayAlone() { + // its line in the autoload list (root's only): by the relay, with no job + std::remove((std::string(kJobFolder) + "/job.txt").c_str()); + return SendRelay(); +} + +} // namespace recomptrophy::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.h new file mode 100644 index 00000000..8000f87b --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/install.h @@ -0,0 +1,93 @@ +// From RecompTrophy by lamarking (https://github.com/cptlamarca/RecompTrophy), +// GPL-3.0, used with permission (commit f78630c). Changed for the port: in the +// recomptrophy namespace; the payloads are read from the app's payloads/ +// folder (SetPayloads) instead of being built into the program; sends have +// time limits; messages in English only (they go to the log); the Payload +// Manager copy of the watcher is only written when there's none (so the port +// never replaces RecompTrophy's own), and its autoload line asked for apart +// (SendRelayAlone). +// +// What goes with a game's trophies: its port installed on the console, when +// there is one (its name, icon and the achievements already earned in it), +// and the installation itself, done by the relay payload (payload/relay.c) +// sent to the payload loader on port 9021, as an app can't write where the +// console keeps trophies. + +#pragma once + +#include <cstdint> +#include <string> +#include <utility> +#include <vector> + +namespace recomptrophy::install { + +constexpr char kJobFolder[] = "/data/trophies360/job"; + +// The relay and the watcher ELFs (ps5/payload/trophies). +void SetPayloads(std::vector<uint8_t> relay, std::vector<uint8_t> watcher); + +struct Port { + std::string folder; // /data/homebrew/TITLE + std::string name; // its titleName + std::vector<uint8_t> icon; // its sce_sys/icon0.png + std::vector<std::pair<uint32_t, int64_t>> earned; // achievement, unix time + std::vector<std::string> sources; // the files they're kept in (.toml of ReXGlue ports, .gpd of profiles) +}; + +// The port of an Xbox 360 title among /data/homebrew's apps (one with its +// achievements file, UserData/achievements/TITLEID.toml, or its shaders). +bool FindPort(uint32_t title_id, Port& port); + +// [ps5 port] [unlocked.N] / filetime lines of a port's achievements file. +std::vector<std::pair<uint32_t, int64_t>> EarnedInToml(const std::string& path); + +// [ps5 port] "titleName" of a param.json (its default language's). +std::string TitleName(const std::string& param); + +struct Result { + bool ok = false; + bool loader = true; // [ps5 port] false: nothing answered on port 9021 + std::string set_state; // installed, same, failed + std::string progress_state; // created, updated, unchanged, no-template, write-failed, none + std::string error; // a message for the player when !ok +}; + +// The set (kJobFolder/TROPHY.UCP and trophies.txt, made already) and the +// earned achievements (all of them: the platinum too) given to the relay. +Result Run(const std::string& set_id, int32_t user, const std::vector<std::pair<uint32_t, int64_t>>& earned, + size_t achievement_count); + +// The watcher (payload/watcher.c): a payload that stays loaded after the app +// closes and, every few seconds, looks at the achievement files of the games +// whose trophies were made (kWatchFolder/SET/: game.txt, map.txt, +// sources.txt), records each new one in the player's trophy list and shows +// the console's own trophy notification for it. Sent unless one runs already +// (its heartbeat, kWatchFolder/alive, is fresh). +constexpr char kWatchFolder[] = "/data/trophies360/watch"; + +// What the watcher needs of a game: its set, the player, its name, each +// achievement's trophy (id, trophy, grade, name) and the files to look at. +bool Watch(const std::string& set_id, int32_t user, const std::string& name, + const std::vector<std::pair<uint32_t, uint32_t>>& trophies, const std::vector<int>& grades, + const std::vector<std::string>& names, const std::vector<std::string>& sources); + +// [ps5 port] A watcher runs (its heartbeat is fresh). +bool WatcherAlive(); + +// The watcher sent to the loader unless it runs: true when it runs. +bool StartWatcher(); + +// The watcher in the Payload Manager's autoload list (/data/pldmgr): put in +// its payloads/local/recomptrophy-watcher/ here; its line in autoload.txt +// (root's only) the watcher adds itself when it starts, and the relay each +// time it's sent. [ps5 port] False when there's no Payload Manager. +bool AddToAutoload(); + +// [ps5 port] The line is in autoload.txt already (false when it can't be read). +bool InAutoloadList(); + +// [ps5 port] The relay sent with no job: it only adds the autoload line. +bool SendRelayAlone(); + +} // namespace recomptrophy::install diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.cpp new file mode 100644 index 00000000..7bdd3382 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.cpp @@ -0,0 +1,470 @@ +// [ps5 port] see native_trophies.h. Uses RecompTrophy by lamarking +// (https://github.com/cptlamarca/RecompTrophy), GPL-3.0, used with permission. +#include "native_trophies.h" + +#include <algorithm> +#include <atomic> +#include <chrono> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <ctime> +#include <exception> +#include <fstream> +#include <iterator> +#include <random> +#include <set> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <dirent.h> +#include <fcntl.h> +#include <pthread.h> +#include <sys/stat.h> +#include <unistd.h> +#if defined(__PROSPERO__) +#include <sys/mount.h> +extern "C" int _fstatfs(int fd, struct statfs* buf); +extern "C" int sceUserServiceGetInitialUser(int32_t* user_id); +extern "C" int sceUserServiceGetForegroundUser(int32_t* user_id); +extern "C" int32_t scePthreadSetaffinity(pthread_t thread, uint64_t mask); +#endif + +#include <rex/cvar.h> +#include <rex/filesystem.h> +#include <rex/frame_stats.h> +#include <rex/logging.h> +#include <rex/runtime.h> +#include <rex/system/kernel_state.h> +#include <rex/system/util/xdbf_utils.h> + +#include "game.h" +#include "install.h" +#include "trophyset.h" + +// The hub's PS5 Trophies (Game page). +REXCVAR_DEFINE_BOOL(ps5_trophies, true, "PS5", + "Put the game's trophies in the PS5's own Trophies list and show the PS5's trophy pop-ups " + "(needs a payload loader on port 9021)"); + +namespace gta4ps5 { + +namespace fs = std::filesystem; +namespace rt = recomptrophy; + +namespace { + +// Raised when what's made here changes: the set is made and installed again. +constexpr int kVersion = 1; +constexpr char kApp[] = "/app0"; +constexpr char kDoneFile[] = "/data/trophies360/done.txt"; // (RecompTrophy's: the sets made) + +using Bytes = std::vector<uint8_t>; +using Earned = std::vector<std::pair<uint32_t, int64_t>>; + +struct Job { + rt::game::Info info; + int32_t user = -1; + fs::path own; // this feature's files (/app0/UserData/LibertyRecomp/ps5_trophies) + std::string toml_rel; // the game's achievements file, inside the app's folder +}; + +// ------------------------------------------------------------------ files + +Bytes ReadFile(const fs::path& path) { + std::ifstream in(path, std::ios::binary); + return Bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); +} + +std::string ReadText(const fs::path& path) { + const Bytes b = ReadFile(path); + std::string s(b.begin(), b.end()); + s.erase(s.find_last_not_of(" \t\r\n") + 1); + return s; +} + +// Written beside, then renamed over. +bool WriteText(const fs::path& path, const std::string& text) { + const fs::path tmp = path.string() + ".tmp"; + { + std::ofstream out(tmp, std::ios::binary | std::ios::trunc); + if (!out.write(text.data(), std::streamsize(text.size()))) return false; + } + std::error_code ec; + fs::rename(tmp, path, ec); + return !ec; +} + +bool CopyFile(const fs::path& from, const fs::path& to) { + std::ifstream in(from, std::ios::binary); + std::ofstream out(to, std::ios::binary | std::ios::trunc); + if (!in || !out) return false; + out << in.rdbuf(); + out.close(); + return bool(out); +} + +bool Exists(const std::string& path) { + struct stat st; + return stat(path.c_str(), &st) == 0; +} + +// ---------------------------------------------------- the game's achievements + +// The fields RecompTrophy's game::Read takes from default.xex's XDBF, from the +// running game's (the same XDBF): English and Portuguese (English when none). +bool ReadAchievements(rex::system::KernelState* ks, rt::game::Info& info) { + using rex::system::XLanguage; + using rex::system::util::XdbfSection; + info.title_id = ks->title_id(); + const auto db = ks->title_xdbf(); + if (!db.is_valid()) return false; + const bool english = bool(db.GetEntry(XdbfSection::kStringTable, uint64_t(XLanguage::kEnglish))); + const bool portuguese = bool(db.GetEntry(XdbfSection::kStringTable, uint64_t(XLanguage::kPortuguese))); + auto en = [&](uint16_t id) { + return english ? db.GetStringTableEntry(XLanguage::kEnglish, id) + : portuguese ? db.GetStringTableEntry(XLanguage::kPortuguese, id) + : std::string(); + }; + auto pt = [&](uint16_t id) { return portuguese ? db.GetStringTableEntry(XLanguage::kPortuguese, id) : en(id); }; + info.title = en(0x8000); + if (const auto icon = db.icon()) info.icon.assign(icon.buffer, icon.buffer + icon.size); + for (const auto& e : db.GetAchievements()) { + rt::game::Achievement a; + a.id = uint16_t(e.id); + if (a.id < 1 || a.id > 127) continue; + a.name[0] = en(e.label_id), a.detail[0] = en(e.description_id); + a.name[1] = pt(e.label_id), a.detail[1] = pt(e.description_id); + if (a.name[1].empty()) a.name[1] = a.name[0], a.detail[1] = a.detail[0]; + a.gamerscore = uint16_t(e.gamerscore); + a.hidden = !(uint32_t(e.flags) & 8); + if (const auto picture = db.GetEntry(XdbfSection::kImage, uint32_t(e.image_id))) + a.picture.assign(picture.buffer, picture.buffer + picture.size); + info.achievements.push_back(std::move(a)); + } + if (info.title.empty()) info.title = "Xbox 360"; + return !info.achievements.empty(); +} + +int32_t CurrentUser() { + int32_t user = -1; +#if defined(__PROSPERO__) + if (sceUserServiceGetInitialUser(&user) != 0 || user < 0) sceUserServiceGetForegroundUser(&user); +#endif + return user; +} + +// What the set is made from (a change makes it again). +std::string Fingerprint(const rt::game::Info& info, const std::string& set, const std::string& name, + const Bytes& icon) { + uint64_t h = 14695981039346656037ull; + auto add = [&](const void* data, size_t size) { + const auto* p = static_cast<const uint8_t*>(data); + for (size_t i = 0; i < size; ++i) h = (h ^ p[i]) * 1099511628211ull; + }; + auto number = [&](uint64_t v) { add(&v, sizeof(v)); }; + auto text = [&](const std::string& s) { number(s.size()), add(s.data(), s.size()); }; + auto bytes = [&](const Bytes& b) { number(b.size()), add(b.data(), b.size()); }; + number(uint64_t(kVersion)), number(info.title_id), text(set), text(name), bytes(icon), bytes(info.icon); + for (const auto& a : info.achievements) { + number(a.id), number(a.gamerscore), number(a.hidden ? 1 : 0); + for (int l = 0; l < 2; ++l) text(a.name[l]), text(a.detail[l]); + bytes(a.picture); + } + char out[20]; + std::snprintf(out, sizeof(out), "%016llx", static_cast<unsigned long long>(h)); + return out; +} + +// --------------------------------------------- what the console was given + +bool IsDone(const std::string& set) { + std::ifstream in(kDoneFile); + std::string id; + while (in >> id) + if (id == set) return true; + return false; +} + +void MarkDone(const std::string& set) { // (as RecompTrophy marks a set made) + if (IsDone(set)) return; + std::ofstream(kDoneFile, std::ios::app) << set << '\n'; +} + +// state.txt: "VERSION SET FINGERPRINT USER" and the achievements brought in. +struct State { + int version = 0; + std::string set, fingerprint; + int32_t user = -1; + std::set<uint32_t> earned; +}; + +State ReadState(const fs::path& file) { + State s; + std::ifstream in(file); + std::string user; + if (!(in >> s.version >> s.set >> s.fingerprint >> user)) return {}; + s.user = int32_t(std::strtoul(user.c_str(), nullptr, 16)); + uint32_t id = 0; + while (in >> id) s.earned.insert(id); + return s; +} + +bool WriteState(const fs::path& file, const State& s) { + char user[16]; + std::snprintf(user, sizeof(user), "%x", unsigned(s.user)); + std::string text = std::to_string(s.version) + ' ' + s.set + ' ' + s.fingerprint + ' ' + user + '\n'; + for (const uint32_t id : s.earned) text += std::to_string(id) + ' '; + return WriteText(file, text + '\n'); +} + +// --------------------------------------------------------- the app's folder + +// Where the console keeps this app (/data/homebrew/PPSA94004...), which the +// game sees as /app0: a note written here is looked for in each app folder. +std::string RealAppFolder(const fs::path& own) { + const fs::path note = own / "here.txt"; + const std::string rel = note.lexically_relative(kApp).generic_string(); + if (rel.empty() || rel.rfind("..", 0) == 0) return {}; + std::mt19937_64 random(uint64_t(std::chrono::steady_clock::now().time_since_epoch().count()) ^ + uint64_t(std::time(nullptr)) ^ uint64_t(getpid())); + char token[24]; + std::snprintf(token, sizeof(token), "%016llx", static_cast<unsigned long long>(random())); + if (!WriteText(note, token)) return {}; + std::vector<std::string> places; + auto add = [&](std::string p) { + while (p.size() > 1 && p.back() == '/') p.pop_back(); + if (p.size() > 1 && p[0] == '/' && std::find(places.begin(), places.end(), p) == places.end()) + places.push_back(p); + }; + auto add_folders_in = [&](const std::string& dir) { + DIR* d = opendir(dir.c_str()); + if (!d) return; + while (dirent* e = readdir(d)) + if (e->d_name[0] != '.') add(dir + "/" + e->d_name); + closedir(d); + }; + // (as RecompTrophy names the ports first) + add_folders_in("/data/homebrew"); +#if defined(__PROSPERO__) + // where /app0 is mounted from + const int fd = ::open((std::string(kApp) + "/eboot.bin").c_str(), O_RDONLY); + if (fd >= 0) { + struct statfs st {}; + if (_fstatfs(fd, &st) == 0 && std::string_view(st.f_fstypename) == "nullfs") add(st.f_mntfromname); + ::close(fd); + } +#endif + for (const char* kind : {"ext", "usb"}) + for (int i = 0; i < 8; ++i) add_folders_in("/mnt/" + std::string(kind) + std::to_string(i) + "/homebrew"); + for (const std::string& p : places) + if (ReadText(p + "/" + rel) == token) return p; + return {}; +} + +// ------------------------------------------------------------------ the work + +int64_t MsSince(std::chrono::steady_clock::time_point start) { + return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count(); +} + +void Work(const Job& job) { + const auto started = std::chrono::steady_clock::now(); + const rt::game::Info& info = job.info; + const std::string set = rt::trophyset::SetId(info.title_id); + const int32_t user = job.user; + if (user < 0) { + REXLOG_WARN("ps5 trophies: no user signed in; skipped"); + return; + } + const fs::path payloads = fs::path(kApp) / "payloads"; + Bytes relay = ReadFile(payloads / "recomptrophy-relay.elf"); + Bytes watcher = ReadFile(payloads / "recomptrophy-watcher.elf"); + if (relay.empty() || watcher.empty()) { + REXLOG_WARN("ps5 trophies: the app has no payloads/recomptrophy-*.elf; skipped"); + return; + } + rt::install::SetPayloads(std::move(relay), std::move(watcher)); + std::error_code ec; + fs::create_directories(job.own, ec); + + // The game among the console's apps, as RecompTrophy finds it (its name, + // icon, the achievements earned and the files they're in), and this app's + // achievements file by its real path, watched for before it's made. + rt::install::Port port; + const bool has_port = rt::install::FindPort(info.title_id, port); + const std::string real = RealAppFolder(job.own); + std::vector<std::string> sources = port.sources; + if (!real.empty()) { + const std::string toml = real + "/" + job.toml_rel; + if (std::find(sources.begin(), sources.end(), toml) == sources.end()) sources.push_back(toml); + } + // (and this app's earned ones through /app0, wherever the app is; each once, the earliest) + Earned all = port.earned; + for (const auto& e : rt::install::EarnedInToml(std::string(kApp) + "/" + job.toml_rel)) all.push_back(e); + std::sort(all.begin(), all.end()); + Earned earned; + for (const auto& e : all) + if (rt::trophyset::TrophyId(info, e.first) && (earned.empty() || earned.back().first != e.first)) + earned.push_back(e); + const Bytes own_param = ReadFile(fs::path(kApp) / "sce_sys" / "param.json"); + std::string name = has_port && !port.name.empty() ? port.name + : rt::install::TitleName(std::string(own_param.begin(), own_param.end())); + if (name.empty()) name = info.title; + const Bytes icon = has_port && !port.icon.empty() ? port.icon : ReadFile(fs::path(kApp) / "sce_sys" / "icon0.png"); + const std::string fingerprint = Fingerprint(info, set, name, icon); + REXLOG_INFO("ps5 trophies: {} ({:08X}), set {}, user {:x}: {} achievements, {} earned; app folder {}; {} file(s) " + "to watch", + name, info.title_id, set, unsigned(user), info.achievements.size(), earned.size(), + real.empty() ? "not found" : real, sources.size()); + + // Installed when it wasn't, when it changed, or when there are earned ones + // the console wasn't given yet (RecompTrophy's done.txt, and this + // feature's state.txt). The relay itself leaves a set already there alone. + const fs::path state_file = job.own / "state.txt"; + const State last = ReadState(state_file); + const bool done = IsDone(set); + const bool same = last.version == kVersion && last.set == set && last.fingerprint == fingerprint && last.user == user; + size_t fresh = 0; + for (const auto& e : earned) fresh += last.earned.count(e.first) ? 0 : 1; + bool relay_sent = false; + if (!done || !same || fresh) { + REXLOG_INFO("ps5 trophies: installing ({})", !done ? "not on the PS5 yet" + : last.version == 0 ? "first time from the game" + : !same ? "out of date" + : "earned achievements to bring in"); + // the set, made again only when what it's made from changed + const fs::path ucp = job.own / "TROPHY.UCP", list = job.own / "trophies.txt", built = job.own / "built.txt"; + if (ReadText(built) != fingerprint || !fs::exists(ucp, ec) || !fs::exists(list, ec)) { + const auto t = std::chrono::steady_clock::now(); + fs::remove(built, ec); + if (!rt::trophyset::Build(info, set, name, icon, ucp.string(), list.string(), nullptr)) { + REXLOG_WARN("ps5 trophies: the set couldn't be written to {}", job.own.string()); + return; + } + WriteText(built, fingerprint); + REXLOG_INFO("ps5 trophies: set made ({} trophies, {} KB) in {} ms", info.achievements.size() + 1, + fs::file_size(ucp, ec) >> 10, MsSince(t)); + } + const std::string job_folder = rt::install::kJobFolder; + mkdir("/data/trophies360", 0777); + mkdir(job_folder.c_str(), 0777); + if (!CopyFile(ucp, job_folder + "/TROPHY.UCP") || !CopyFile(list, job_folder + "/trophies.txt")) { + REXLOG_WARN("ps5 trophies: {} can't be written; skipped", job_folder); + return; + } + // (the watcher beside the Payload Manager first: the relay adds its autoload line) + rt::install::AddToAutoload(); + Earned trophies; + for (const auto& [achievement, when] : earned) trophies.emplace_back(rt::trophyset::TrophyId(info, achievement), when); + const rt::install::Result result = rt::install::Run(set, user, trophies, info.achievements.size()); + if (!result.loader) { + REXLOG_INFO("ps5 trophies: no payload loader answered on port 9021 (etaHEN, elfldr...); skipped"); + return; + } + relay_sent = true; + if (!result.ok) { + REXLOG_WARN("ps5 trophies: not installed: {} (set {}, progress {})", result.error, + result.set_state.empty() ? "?" : result.set_state, + result.progress_state.empty() ? "?" : result.progress_state); + return; + } + MarkDone(set); + State now{kVersion, set, fingerprint, user, {}}; + for (const auto& e : earned) now.earned.insert(e.first); + if (!WriteState(state_file, now)) REXLOG_WARN("ps5 trophies: {} can't be written", state_file.string()); + REXLOG_INFO("ps5 trophies: installed (set {}, progress {}), {} earned brought in", result.set_state, + result.progress_state, trophies.size()); + } else { + REXLOG_INFO("ps5 trophies: on the PS5 and up to date"); + } + + // The PS5's own pop-ups: RecompTrophy's watcher, for this game's files. + if (set == "NPWR99990_00" || Exists("/data/trophies360/native/" + set)) { + REXLOG_INFO("ps5 trophies: /data/trophies360/native/{} is there: no pop-ups from the watcher", set); + return; + } + if (sources.empty()) { + REXLOG_WARN("ps5 trophies: the game's achievements file wasn't found on the console: no pop-ups"); + return; + } + std::vector<std::pair<uint32_t, uint32_t>> map; + std::vector<int> grades; + std::vector<std::string> names; + for (const auto& a : info.achievements) { + map.emplace_back(a.id, rt::trophyset::TrophyId(info, a.id)); + grades.push_back(a.gamerscore <= 10 ? 4 : a.gamerscore <= 25 ? 3 : 2); + names.push_back(a.name[0]); + } + if (!rt::install::Watch(set, user, name, map, grades, names, sources)) + REXLOG_WARN("ps5 trophies: {}/{} can't be written", rt::install::kWatchFolder, set); + if (Exists(std::string(rt::install::kWatchFolder) + "/" + set + "/off")) + REXLOG_INFO("ps5 trophies: this game's pop-ups are turned off in RecompTrophy"); + const bool manager = rt::install::AddToAutoload(); + if (rt::install::WatcherAlive()) { + // (its autoload line, when the Payload Manager's list doesn't show it) + if (manager && !relay_sent && !rt::install::InAutoloadList()) rt::install::SendRelayAlone(); + REXLOG_INFO("ps5 trophies: the watcher runs already{}; done in {} ms", + manager ? "" : " (no Payload Manager: not loaded at the console's start)", MsSince(started)); + } else if (rt::install::StartWatcher()) { + std::this_thread::sleep_for(std::chrono::seconds(4)); + REXLOG_INFO("ps5 trophies: watcher sent, {}{}; done in {} ms", rt::install::WatcherAlive() ? "running" : "not running yet", + manager ? "" : " (no Payload Manager: not loaded at the console's start)", MsSince(started)); + } else { + REXLOG_INFO("ps5 trophies: no payload loader answered on port 9021 for the watcher; skipped"); + } +} + +void Worker(Job job) { +#if defined(__PROSPERO__) + // (with the game's other threads, off the renderer's own cores) + scePthreadSetaffinity(pthread_self(), rex::frame_stats::kOtherThreadCpus); +#endif + try { + Work(job); + } catch (const std::exception& e) { + REXLOG_WARN("ps5 trophies: stopped: {}", e.what()); + } catch (...) { + REXLOG_WARN("ps5 trophies: stopped by an error"); + } +} + +} // namespace + +void StartNativeTrophies(rex::Runtime* runtime, const fs::path& user_data) { + static std::atomic<bool> started{false}; + if (started.exchange(true)) return; + try { + if (!REXCVAR_GET(ps5_trophies)) { + REXLOG_INFO("ps5 trophies: off (the hub's PS5 Trophies)"); + return; + } + auto* ks = runtime ? runtime->kernel_state() : nullptr; + if (!ks) return; + // (copied here: the worker never touches the runtime) + Job job; + if (!ReadAchievements(ks, job.info)) { + REXLOG_WARN("ps5 trophies: the game's achievements couldn't be read; skipped"); + return; + } + job.user = CurrentUser(); + job.own = rex::filesystem::GetUserFolder() / "LibertyRecomp" / "ps5_trophies"; + // (where the runtime keeps them: kernel_state.cpp) + const fs::path toml = user_data / "achievements" / fmt::format("{:08X}.toml", job.info.title_id); + job.toml_rel = toml.lexically_normal().lexically_relative(kApp).generic_string(); + if (job.toml_rel.empty() || job.toml_rel.rfind("..", 0) == 0) { + REXLOG_WARN("ps5 trophies: {} isn't in the app's folder; skipped", toml.string()); + return; + } + std::thread(Worker, std::move(job)).detach(); + } catch (const std::exception& e) { + REXLOG_WARN("ps5 trophies: not started: {}", e.what()); + } catch (...) { + REXLOG_WARN("ps5 trophies: not started"); + } +} + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.h new file mode 100644 index 00000000..4ddc90d3 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/native_trophies.h @@ -0,0 +1,30 @@ +// [ps5 port] GTA IV in the PS5's own Trophies list, set up by the game itself: +// at each start, on a thread of its own, it does what pressing Cross for GTA IV +// in RecompTrophy does (its code: install and trophyset here, its payloads: +// ps5/payload/trophies), when something changed since the last time: +// +// - makes the game's trophy set from its achievements (the running game's +// XDBF, the same data RecompTrophy reads from default.xex); +// - has RecompTrophy's relay install it and bring in the achievements +// already earned, with their dates; +// - sets up RecompTrophy's watcher for the game's achievements file (by its +// real path, /data/homebrew/...), starts it, and puts it in the Payload +// Manager's autoload list: from then on the PS5's own trophy pop-ups. +// +// Needs a payload loader on port 9021 (etaHEN, elfldr...); without one it +// does nothing. The hub's PS5 Trophies setting (ps5_trophies) turns it off. +// Logged as "ps5 trophies: ...". +#pragma once + +#include <filesystem> + +namespace rex { +class Runtime; +} + +namespace gta4ps5 { + +// From the game's setup (OnGameSetUp): returns at once. +void StartNativeTrophies(rex::Runtime* runtime, const std::filesystem::path& user_data); + +} // namespace gta4ps5 diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/third_party/miniz.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/third_party/miniz.h new file mode 100644 index 00000000..0cb0b338 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/third_party/miniz.h @@ -0,0 +1,5099 @@ +/* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing + See "unlicense" statement at the end of this file. + Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013 + Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt + + Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define + MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). + + * Change History + 10/13/13 v1.15 r4 - Interim bugfix release while I work on the next major release with Zip64 support (almost there!): + - Critical fix for the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com) which could cause locate files to not find files. This bug + would only have occurred in earlier versions if you explicitly used this flag, OR if you used mz_zip_extract_archive_file_to_heap() or mz_zip_add_mem_to_archive_file_in_place() + (which used this flag). If you can't switch to v1.15 but want to fix this bug, just remove the uses of this flag from both helper funcs (and of course don't use the flag). + - Bugfix in mz_zip_reader_extract_to_mem_no_alloc() from kymoon when pUser_read_buf is not NULL and compressed size is > uncompressed size + - Fixing mz_zip_reader_extract_*() funcs so they don't try to extract compressed data from directory entries, to account for weird zipfiles which contain zero-size compressed data on dir entries. + Hopefully this fix won't cause any issues on weird zip archives, because it assumes the low 16-bits of zip external attributes are DOS attributes (which I believe they always are in practice). + - Fixing mz_zip_reader_is_file_a_directory() so it doesn't check the internal attributes, just the filename and external attributes + - mz_zip_reader_init_file() - missing MZ_FCLOSE() call if the seek failed + - Added cmake support for Linux builds which builds all the examples, tested with clang v3.3 and gcc v4.6. + - Clang fix for tdefl_write_image_to_png_file_in_memory() from toffaletti + - Merged MZ_FORCEINLINE fix from hdeanclark + - Fix <time.h> include before config #ifdef, thanks emil.brink + - Added tdefl_write_image_to_png_file_in_memory_ex(): supports Y flipping (super useful for OpenGL apps), and explicit control over the compression level (so you can + set it to 1 for real-time compression). + - Merged in some compiler fixes from paulharris's github repro. + - Retested this build under Windows (VS 2010, including static analysis), tcc 0.9.26, gcc v4.6 and clang v3.3. + - Added example6.c, which dumps an image of the mandelbrot set to a PNG file. + - Modified example2 to help test the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY flag more. + - In r3: Bugfix to mz_zip_writer_add_file() found during merge: Fix possible src file fclose() leak if alignment bytes+local header file write faiiled + - In r4: Minor bugfix to mz_zip_writer_add_from_zip_reader(): Was pushing the wrong central dir header offset, appears harmless in this release, but it became a problem in the zip64 branch + 5/20/12 v1.14 - MinGW32/64 GCC 4.6.1 compiler fixes: added MZ_FORCEINLINE, #include <time.h> (thanks fermtect). + 5/19/12 v1.13 - From jason@cornsyrup.org and kelwert@mtu.edu - Fix mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bit. + - Temporarily/locally slammed in "typedef unsigned long mz_ulong" and re-ran a randomized regression test on ~500k files. + - Eliminated a bunch of warnings when compiling with GCC 32-bit/64. + - Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze (static analysis) option and fixed all warnings (except for the silly + "Use of the comma-operator in a tested expression.." analysis warning, which I purposely use to work around a MSVC compiler warning). + - Created 32-bit and 64-bit Codeblocks projects/workspace. Built and tested Linux executables. The codeblocks workspace is compatible with Linux+Win32/x64. + - Added miniz_tester solution/project, which is a useful little app derived from LZHAM's tester app that I use as part of the regression test. + - Ran miniz.c and tinfl.c through another series of regression testing on ~500,000 files and archives. + - Modified example5.c so it purposely disables a bunch of high-level functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the MINIZ_NO_STDIO bug report.) + - Fix ftell() usage in examples so they exit with an error on files which are too large (a limitation of the examples, not miniz itself). + 4/12/12 v1.12 - More comments, added low-level example5.c, fixed a couple minor level_and_flags issues in the archive API's. + level_and_flags can now be set to MZ_DEFAULT_COMPRESSION. Thanks to Bruce Dawson <bruced@valvesoftware.com> for the feedback/bug report. + 5/28/11 v1.11 - Added statement from unlicense.org + 5/27/11 v1.10 - Substantial compressor optimizations: + - Level 1 is now ~4x faster than before. The L1 compressor's throughput now varies between 70-110MB/sec. on a + - Core i7 (actual throughput varies depending on the type of data, and x64 vs. x86). + - Improved baseline L2-L9 compression perf. Also, greatly improved compression perf. issues on some file types. + - Refactored the compression code for better readability and maintainability. + - Added level 10 compression level (L10 has slightly better ratio than level 9, but could have a potentially large + drop in throughput on some files). + 5/15/11 v1.09 - Initial stable release. + + * Low-level Deflate/Inflate implementation notes: + + Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or + greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses + approximately as well as zlib. + + Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function + coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or into a memory + block large enough to hold the entire file. + + The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation. + + * zlib-style API notes: + + miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in + zlib replacement in many apps: + The z_stream struct, optional memory allocation callbacks + deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound + inflateInit/inflateInit2/inflate/inflateEnd + compress, compress2, compressBound, uncompress + CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines. + Supports raw deflate streams or standard zlib streams with adler-32 checking. + + Limitations: + The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries. + I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but + there are no guarantees that miniz.c pulls this off perfectly. + + * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by + Alex Evans. Supports 1-4 bytes/pixel images. + + * ZIP archive API notes: + + The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to + get the job done with minimal fuss. There are simple API's to retrieve file information, read files from + existing archives, create new archives, append new files to existing archives, or clone archive data from + one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h), + or you can specify custom file read/write callbacks. + + - Archive reading: Just call this function to read a single file from a disk archive: + + void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, + size_t *pSize, mz_uint zip_flags); + + For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central + directory is located and read as-is into memory, and subsequent file access only occurs when reading individual files. + + - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file: + + int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); + + The locate operation can optionally check file comments too, which (as one example) can be used to identify + multiple versions of the same file in an archive. This function uses a simple linear search through the central + directory, so it's not very fast. + + Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and + retrieve detailed info on each file by calling mz_zip_reader_file_stat(). + + - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data + to disk and builds an exact image of the central directory in memory. The central directory image is written + all at once at the end of the archive file when the archive is finalized. + + The archive writer can optionally align each file's local header and file data to any power of 2 alignment, + which can be useful when the archive will be read from optical media. Also, the writer supports placing + arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still + readable by any ZIP tool. + + - Archive appending: The simple way to add a single file to an archive is to call this function: + + mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, + const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); + + The archive will be created if it doesn't already exist, otherwise it'll be appended to. + Note the appending is done in-place and is not an atomic operation, so if something goes wrong + during the operation it's possible the archive could be left without a central directory (although the local + file headers and file data will be fine, so the archive will be recoverable). + + For more complex archive modification scenarios: + 1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to + preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the + compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and + you're done. This is safe but requires a bunch of temporary disk space or heap memory. + + 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(), + append new files as needed, then finalize the archive which will write an updated central directory to the + original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a + possibility that the archive's central directory could be lost with this method if anything goes wrong, though. + + - ZIP archive support limitations: + No zip64 or spanning support. Extraction functions can only handle unencrypted, stored or deflated files. + Requires streams capable of seeking. + + * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the + below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. + + * Important: For best perf. be sure to customize the below macros for your target platform: + #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 + #define MINIZ_LITTLE_ENDIAN 1 + #define MINIZ_HAS_64BIT_REGISTERS 1 + + * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before including miniz.c to ensure miniz + uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be able to process large files + (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). +*/ + +#ifndef MINIZ_HEADER_INCLUDED +#define MINIZ_HEADER_INCLUDED + +/*#include <stdlib.h>*/ + +// Defines to completely disable specific portions of miniz.c: +// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. + +// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. +#define MINIZ_NO_STDIO + +// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or +// get/set file times, and the C run-time funcs that get/set times won't be called. +// The current downside is the times written to your archives will be from 1979. +#define MINIZ_NO_TIME + +// Define MINIZ_NO_DEFLATE_APIS to disable all compression API's. +//#define MINIZ_NO_DEFLATE_APIS + +// Define MINIZ_NO_INFLATE_APIS to disable all decompression API's. +#define MINIZ_NO_INFLATE_APIS + +// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. +#define MINIZ_NO_ARCHIVE_APIS + +// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's. +#define MINIZ_NO_ARCHIVE_WRITING_APIS + +// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. +#define MINIZ_NO_ZLIB_APIS + +// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. +#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES + +// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. +// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc +// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user +// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. +//#define MINIZ_NO_MALLOC +#define MINIZ_SDL_MALLOC + +#ifdef MINIZ_NO_INFLATE_APIS +#define MINIZ_NO_ARCHIVE_APIS +#endif + +#ifdef MINIZ_NO_DEFLATE_APIS +#define MINIZ_NO_ARCHIVE_WRITING_APIS +#endif + +// Define MINIZ_STATIC_FUNCTIONS to make all functions static. You probably +// want this if you're using miniz in a library +#define MINIZ_STATIC_FUNCTIONS + +#if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) + // TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux + #define MINIZ_NO_TIME +#endif + +#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) + #include <time.h> +#endif + +#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__) +// MINIZ_X86_OR_X64_CPU is only used to help set the below macros. +#define MINIZ_X86_OR_X64_CPU 1 +#endif + +#ifndef MINIZ_LITTLE_ENDIAN /* if not defined by SDL */ +#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU +// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. +#define MINIZ_LITTLE_ENDIAN 1 +#endif +#endif // MINIZ_LITTLE_ENDIAN /* if not defined by SDL */ + +#ifndef MINIZ_USE_UNALIGNED_LOADS_AND_STORES +#if MINIZ_X86_OR_X64_CPU +// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 +#endif +#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES + +#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__) +// Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). +#define MINIZ_HAS_64BIT_REGISTERS 1 +#endif + +#ifdef MINIZ_STATIC_FUNCTIONS +#define MINIZ_STATIC static +#else +#define MINIZ_STATIC +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// ------------------- zlib-style API Definitions. + +// For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! +typedef unsigned long mz_ulong; + +// mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap. +MINIZ_STATIC void mz_free(void *p); + +#define MZ_ADLER32_INIT (1) +// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. +MINIZ_STATIC mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len); + +#define MZ_CRC32_INIT (0) +// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. +MINIZ_STATIC mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len); + +// Compression strategies. +enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 }; + +// Method +#define MZ_DEFLATED 8 + +#ifndef MINIZ_NO_ZLIB_APIS + +// Heap allocation callbacks. +// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long. +typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); +typedef void (*mz_free_func)(void *opaque, void *address); +typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size); + +#define MZ_VERSION "9.1.15" +#define MZ_VERNUM 0x91F0 +#define MZ_VER_MAJOR 9 +#define MZ_VER_MINOR 1 +#define MZ_VER_REVISION 15 +#define MZ_VER_SUBREVISION 0 + +// Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer to the zlib docs). +enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 }; + +// Return status codes. MZ_PARAM_ERROR is non-standard. +enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 }; + +// Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. +enum { MZ_NO_COMPRESSION = 0, MZ_BEST_SPEED = 1, MZ_BEST_COMPRESSION = 9, MZ_UBER_COMPRESSION = 10, MZ_DEFAULT_LEVEL = 6, MZ_DEFAULT_COMPRESSION = -1 }; + +// Window bits +#define MZ_DEFAULT_WINDOW_BITS 15 + +struct mz_internal_state; + +// Compression/decompression stream struct. +typedef struct mz_stream_s +{ + const unsigned char *next_in; // pointer to next byte to read + unsigned int avail_in; // number of bytes available at next_in + mz_ulong total_in; // total number of bytes consumed so far + + unsigned char *next_out; // pointer to next byte to write + unsigned int avail_out; // number of bytes that can be written to next_out + mz_ulong total_out; // total number of bytes produced so far + + char *msg; // error msg (unused) + struct mz_internal_state *state; // internal state, allocated by zalloc/zfree + + mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc) + mz_free_func zfree; // optional heap free function (defaults to free) + void *opaque; // heap alloc function user pointer + + int data_type; // data_type (unused) + mz_ulong adler; // adler32 of the source or uncompressed data + mz_ulong reserved; // not used +} mz_stream; + +typedef mz_stream *mz_streamp; + +// Returns the version string of miniz.c. +MINIZ_STATIC const char *mz_version(void); + +#ifndef MINIZ_NO_DEFLATE_APIS + +// mz_deflateInit() initializes a compressor with default options: +// Parameters: +// pStream must point to an initialized mz_stream struct. +// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. +// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. +// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) +// Return values: +// MZ_OK on success. +// MZ_STREAM_ERROR if the stream is bogus. +// MZ_PARAM_ERROR if the input parameters are bogus. +// MZ_MEM_ERROR on out of memory. +MINIZ_STATIC int mz_deflateInit(mz_streamp pStream, int level); + +// mz_deflateInit2() is like mz_deflate(), except with more control: +// Additional parameters: +// method must be MZ_DEFLATED +// window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer) +// mem_level must be between [1, 9] (it's checked but ignored by miniz.c) +MINIZ_STATIC int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy); + +// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). +MINIZ_STATIC int mz_deflateReset(mz_streamp pStream); + +// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. +// Parameters: +// pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. +// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. +// Return values: +// MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be written but the output buffer is full). +// MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the stream anymore. +// MZ_STREAM_ERROR if the stream is bogus. +// MZ_PARAM_ERROR if one of the parameters is invalid. +// MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the input buffer or free up some output space and try again.) +MINIZ_STATIC int mz_deflate(mz_streamp pStream, int flush); + +// mz_deflateEnd() deinitializes a compressor: +// Return values: +// MZ_OK on success. +// MZ_STREAM_ERROR if the stream is bogus. +MINIZ_STATIC int mz_deflateEnd(mz_streamp pStream); + +// mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH. +MINIZ_STATIC mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); + +// Single-call compression functions mz_compress() and mz_compress2(): +// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. +MINIZ_STATIC int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); +MINIZ_STATIC int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level); + +// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). +MINIZ_STATIC mz_ulong mz_compressBound(mz_ulong source_len); + +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS + +// Initializes a decompressor. +MINIZ_STATIC int mz_inflateInit(mz_streamp pStream); + +// mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer: +// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). +MINIZ_STATIC int mz_inflateInit2(mz_streamp pStream, int window_bits); + +// Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible. +// Parameters: +// pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. +// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. +// On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster). +// MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data. +// Return values: +// MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full. +// MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified. +// MZ_STREAM_ERROR if the stream is bogus. +// MZ_DATA_ERROR if the deflate stream is invalid. +// MZ_PARAM_ERROR if one of the parameters is invalid. +// MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again +// with more input data, or with more room in the output buffer (except when using single call decompression, described above). +MINIZ_STATIC int mz_inflate(mz_streamp pStream, int flush); + +// Deinitializes a decompressor. +MINIZ_STATIC int mz_inflateEnd(mz_streamp pStream); + +// Single-call decompression. +// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. +MINIZ_STATIC int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + +// Returns a string description of the specified error code, or NULL if the error code is invalid. +MINIZ_STATIC const char *mz_error(int err); + +// Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset of zlib that miniz.c supports. +// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. +#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES + typedef unsigned char Byte; + typedef unsigned int uInt; + typedef mz_ulong uLong; + typedef Byte Bytef; + typedef uInt uIntf; + typedef char charf; + typedef int intf; + typedef void *voidpf; + typedef uLong uLongf; + typedef void *voidp; + typedef void *const voidpc; + #define Z_NULL 0 + #define Z_NO_FLUSH MZ_NO_FLUSH + #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH + #define Z_SYNC_FLUSH MZ_SYNC_FLUSH + #define Z_FULL_FLUSH MZ_FULL_FLUSH + #define Z_FINISH MZ_FINISH + #define Z_BLOCK MZ_BLOCK + #define Z_OK MZ_OK + #define Z_STREAM_END MZ_STREAM_END + #define Z_NEED_DICT MZ_NEED_DICT + #define Z_ERRNO MZ_ERRNO + #define Z_STREAM_ERROR MZ_STREAM_ERROR + #define Z_DATA_ERROR MZ_DATA_ERROR + #define Z_MEM_ERROR MZ_MEM_ERROR + #define Z_BUF_ERROR MZ_BUF_ERROR + #define Z_VERSION_ERROR MZ_VERSION_ERROR + #define Z_PARAM_ERROR MZ_PARAM_ERROR + #define Z_NO_COMPRESSION MZ_NO_COMPRESSION + #define Z_BEST_SPEED MZ_BEST_SPEED + #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION + #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION + #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY + #define Z_FILTERED MZ_FILTERED + #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY + #define Z_RLE MZ_RLE + #define Z_FIXED MZ_FIXED + #define Z_DEFLATED MZ_DEFLATED + #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS + #define alloc_func mz_alloc_func + #define free_func mz_free_func + #define internal_state mz_internal_state + #define z_stream mz_stream +#ifndef MINIZ_NO_DEFLATE_APIS + #define deflateInit mz_deflateInit + #define deflateInit2 mz_deflateInit2 + #define deflateReset mz_deflateReset + #define deflate mz_deflate + #define deflateEnd mz_deflateEnd + #define deflateBound mz_deflateBound + #define compress mz_compress + #define compress2 mz_compress2 + #define compressBound mz_compressBound +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS + #define inflateInit mz_inflateInit + #define inflateInit2 mz_inflateInit2 + #define inflate mz_inflate + #define inflateEnd mz_inflateEnd + #define uncompress mz_uncompress +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + #define crc32 mz_crc32 + #define adler32 mz_adler32 + #define MAX_WBITS 15 + #define MAX_MEM_LEVEL 9 + #define zError mz_error + #define ZLIB_VERSION MZ_VERSION + #define ZLIB_VERNUM MZ_VERNUM + #define ZLIB_VER_MAJOR MZ_VER_MAJOR + #define ZLIB_VER_MINOR MZ_VER_MINOR + #define ZLIB_VER_REVISION MZ_VER_REVISION + #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION + #define zlibVersion mz_version + #define zlib_version mz_version() +#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES + +#endif // MINIZ_NO_ZLIB_APIS + +// ------------------- Types and macros + +typedef unsigned char mz_uint8; +typedef signed short mz_int16; +typedef unsigned short mz_uint16; +typedef unsigned int mz_uint32; +typedef unsigned int mz_uint; +typedef long long mz_int64; +typedef unsigned long long mz_uint64; +typedef int mz_bool; + +#define MZ_FALSE (0) +#define MZ_TRUE (1) + +// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message. +#ifdef _MSC_VER + #define MZ_MACRO_END while (0, 0) +#else + #define MZ_MACRO_END while (0) +#endif + +// ------------------- ZIP archive reading/writing + +#ifndef MINIZ_NO_ARCHIVE_APIS + +enum +{ + MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024, + MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260, + MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256 +}; + +typedef struct +{ + mz_uint32 m_file_index; + mz_uint32 m_central_dir_ofs; + mz_uint16 m_version_made_by; + mz_uint16 m_version_needed; + mz_uint16 m_bit_flag; + mz_uint16 m_method; +#ifndef MINIZ_NO_TIME + time_t m_time; +#endif + mz_uint32 m_crc32; + mz_uint64 m_comp_size; + mz_uint64 m_uncomp_size; + mz_uint16 m_internal_attr; + mz_uint32 m_external_attr; + mz_uint64 m_local_header_ofs; + mz_uint32 m_comment_size; + char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; + char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; +} mz_zip_archive_file_stat; + +typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n); +typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n); + +struct mz_zip_internal_state_tag; +typedef struct mz_zip_internal_state_tag mz_zip_internal_state; + +typedef enum +{ + MZ_ZIP_MODE_INVALID = 0, + MZ_ZIP_MODE_READING = 1, + MZ_ZIP_MODE_WRITING = 2, + MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 +} mz_zip_mode; + +typedef struct mz_zip_archive_tag +{ + mz_uint64 m_archive_size; + mz_uint64 m_central_directory_file_ofs; + mz_uint m_total_files; + mz_zip_mode m_zip_mode; + + mz_uint m_file_offset_alignment; + + mz_alloc_func m_pAlloc; + mz_free_func m_pFree; + mz_realloc_func m_pRealloc; + void *m_pAlloc_opaque; + + mz_file_read_func m_pRead; + mz_file_write_func m_pWrite; + void *m_pIO_opaque; + + mz_zip_internal_state *m_pState; + +} mz_zip_archive; + +typedef enum +{ + MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, + MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, + MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, + MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800 +} mz_zip_flags; + +// ZIP archive reading + +// Inits a ZIP archive reader. +// These functions read and validate the archive's central directory. +MINIZ_STATIC mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags); +MINIZ_STATIC mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags); + +#ifndef MINIZ_NO_STDIO +MINIZ_STATIC mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags); +#endif + +// Returns the total number of files in the archive. +MINIZ_STATIC mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip); + +// Returns detailed information about an archive file entry. +MINIZ_STATIC mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat); + +// Determines if an archive file entry is a directory entry. +MINIZ_STATIC mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index); +MINIZ_STATIC mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index); + +// Retrieves the filename of an archive file entry. +// Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of bytes needed to fully store the filename. +MINIZ_STATIC mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size); + +// Attempts to locates a file in the archive's central directory. +// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH +// Returns -1 if the file cannot be found. +MINIZ_STATIC int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); + +// Extracts a archive file to a memory buffer using no memory allocation. +MINIZ_STATIC mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); +MINIZ_STATIC mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); + +// Extracts a archive file to a memory buffer. +MINIZ_STATIC mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags); +MINIZ_STATIC mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags); + +// Extracts a archive file to a dynamically allocated heap buffer. +MINIZ_STATIC void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags); +MINIZ_STATIC void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags); + +// Extracts a archive file using a callback function to output the file's data. +MINIZ_STATIC mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); +MINIZ_STATIC mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); + +#ifndef MINIZ_NO_STDIO +// Extracts a archive file to a disk file and sets its last accessed and modified times. +// This function only extracts files, not archive directory records. +MINIZ_STATIC mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags); +MINIZ_STATIC mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags); +#endif + +// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used. +MINIZ_STATIC mz_bool mz_zip_reader_end(mz_zip_archive *pZip); + +// ZIP archive writing + +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +// Inits a ZIP archive writer. +MINIZ_STATIC mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size); +MINIZ_STATIC mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size); + +#ifndef MINIZ_NO_STDIO +MINIZ_STATIC mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning); +#endif + +// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive. +// For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for writing. If the file can't be reopened, mz_zip_reader_end() will be called. +// For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which defaults to realloc unless you've overridden it). +// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL. +// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before +// the archive is finalized the file's central directory will be hosed. +MINIZ_STATIC mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename); + +// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. +// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer. +// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. +MINIZ_STATIC mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags); +MINIZ_STATIC mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); + +#ifndef MINIZ_NO_STDIO +// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. +// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. +MINIZ_STATIC mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); +#endif + +// Adds a file to an archive by fully cloning the data from another archive. +// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields. +MINIZ_STATIC mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index); + +// Finalizes the archive by writing the central directory records followed by the end of central directory record. +// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). +// An archive must be manually finalized by calling this function for it to be valid. +MINIZ_STATIC mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); +MINIZ_STATIC mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize); + +// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. +// Note for the archive to be valid, it must have been finalized before ending. +MINIZ_STATIC mz_bool mz_zip_writer_end(mz_zip_archive *pZip); + +// Misc. high-level helper functions: + +// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. +// level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. +MINIZ_STATIC mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); + +// Reads a single file from an archive into a heap block. +// Returns NULL on failure. +MINIZ_STATIC void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags); + +#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +#endif // #ifndef MINIZ_NO_ARCHIVE_APIS + +// ------------------- Low-level Decompression API Definitions +#ifndef MINIZ_NO_INFLATE_APIS + +// Decompression flags used by tinfl_decompress(). +// TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. +// TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. +// TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). +// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. +enum +{ + TINFL_FLAG_PARSE_ZLIB_HEADER = 1, + TINFL_FLAG_HAS_MORE_INPUT = 2, + TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, + TINFL_FLAG_COMPUTE_ADLER32 = 8 +}; + +// High level decompression functions: +// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). +// On entry: +// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. +// On return: +// Function returns a pointer to the decompressed data, or NULL on failure. +// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. +// The caller must call mz_free() on the returned block when it's no longer needed. +MINIZ_STATIC void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); + +// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. +// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. +#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) +MINIZ_STATIC size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); + +// tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. +// Returns 1 on success or 0 on failure. +typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); +MINIZ_STATIC int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); + +struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor; + +// Max size of LZ dictionary. +#define TINFL_LZ_DICT_SIZE 32768 + +// Return status. +typedef enum +{ + TINFL_STATUS_BAD_PARAM = -3, + TINFL_STATUS_ADLER32_MISMATCH = -2, + TINFL_STATUS_FAILED = -1, + TINFL_STATUS_DONE = 0, + TINFL_STATUS_NEEDS_MORE_INPUT = 1, + TINFL_STATUS_HAS_MORE_OUTPUT = 2 +} tinfl_status; + +// Initializes the decompressor to its initial state. +#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END +#define tinfl_get_adler32(r) (r)->m_check_adler32 + +// Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. +// This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. +MINIZ_STATIC tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags); + +// Internal/private bits follow. +enum +{ + TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19, + TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS +}; + +typedef struct +{ + mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; + mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; +} tinfl_huff_table; + +#if MINIZ_HAS_64BIT_REGISTERS + #define TINFL_USE_64BIT_BITBUF 1 +#endif + +#if TINFL_USE_64BIT_BITBUF + typedef mz_uint64 tinfl_bit_buf_t; + #define TINFL_BITBUF_SIZE (64) +#else + typedef mz_uint32 tinfl_bit_buf_t; + #define TINFL_BITBUF_SIZE (32) +#endif + +struct tinfl_decompressor_tag +{ + mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; + tinfl_bit_buf_t m_bit_buf; + size_t m_dist_from_out_buf_start; + tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; + mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; +}; +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + +// ------------------- Low-level Compression API Definitions +#ifndef MINIZ_NO_DEFLATE_APIS + +// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). +#define TDEFL_LESS_MEMORY 0 + +// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): +// TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). +enum +{ + TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF +}; + +// TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data. +// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). +// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. +// TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory). +// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) +// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. +// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. +// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. +// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). +enum +{ + TDEFL_WRITE_ZLIB_HEADER = 0x01000, + TDEFL_COMPUTE_ADLER32 = 0x02000, + TDEFL_GREEDY_PARSING_FLAG = 0x04000, + TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, + TDEFL_RLE_MATCHES = 0x10000, + TDEFL_FILTER_MATCHES = 0x20000, + TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, + TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 +}; + +#ifndef MINIZ_SDL_NOUNUSED +// High level compression functions: +// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). +// On entry: +// pSrc_buf, src_buf_len: Pointer and size of source block to compress. +// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression. +// On return: +// Function returns a pointer to the compressed data, or NULL on failure. +// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. +// The caller must free() the returned block when it's no longer needed. +MINIZ_STATIC void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); + +// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. +// Returns 0 on failure. +MINIZ_STATIC size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); +#endif /* MINIZ_SDL_NOUNUSED */ + +// Compresses an image to a compressed PNG file in memory. +// On entry: +// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. +// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory. +// level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL +// If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). +// On return: +// Function returns a pointer to the compressed data, or NULL on failure. +// *pLen_out will be set to the size of the PNG image file. +// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed. +MINIZ_STATIC void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, int bpl, size_t *pLen_out, mz_uint level, mz_bool flip, mz_uint8 *plte, int plte_size, mz_uint8 *trns, int trns_size); +#ifndef MINIZ_SDL_NOUNUSED +MINIZ_STATIC void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, int bpl, size_t *pLen_out); +#endif + +// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. +typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); + +#ifndef MINIZ_SDL_NOUNUSED +// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. +MINIZ_STATIC mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); +#endif + +//enum { TDEFL_MAX_HUFF_TABLES = 3, TDEFL_MAX_HUFF_SYMBOLS_0 = 288, TDEFL_MAX_HUFF_SYMBOLS_1 = 32, TDEFL_MAX_HUFF_SYMBOLS_2 = 19, TDEFL_LZ_DICT_SIZE = 32768, TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, TDEFL_MIN_MATCH_LEN = 3, TDEFL_MAX_MATCH_LEN = 258 }; +#define TDEFL_MAX_HUFF_TABLES 3 +#define TDEFL_MAX_HUFF_SYMBOLS_0 288 +#define TDEFL_MAX_HUFF_SYMBOLS_1 32 +#define TDEFL_MAX_HUFF_SYMBOLS_2 19 +#define TDEFL_LZ_DICT_SIZE 32768 +#define TDEFL_LZ_DICT_SIZE_MASK ( TDEFL_LZ_DICT_SIZE - 1 ) +#define TDEFL_MIN_MATCH_LEN 3 +#define TDEFL_MAX_MATCH_LEN 258 + +// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes). +#if TDEFL_LESS_MEMORY +enum { TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 12, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS }; +#else +enum { TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 15, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS }; +#endif + +// The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions. +typedef enum +{ + TDEFL_STATUS_BAD_PARAM = -2, + TDEFL_STATUS_PUT_BUF_FAILED = -1, + TDEFL_STATUS_OKAY = 0, + TDEFL_STATUS_DONE = 1, +} tdefl_status; + +// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums +typedef enum +{ + TDEFL_NO_FLUSH = 0, + TDEFL_SYNC_FLUSH = 2, + TDEFL_FULL_FLUSH = 3, + TDEFL_FINISH = 4 +} tdefl_flush; + +// tdefl's compression state structure. +typedef struct +{ + tdefl_put_buf_func_ptr m_pPut_buf_func; + void *m_pPut_buf_user; + mz_uint m_flags, m_max_probes[2]; + int m_greedy_parsing; + mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; + mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; + mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer; + mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish; + tdefl_status m_prev_return_status; + const void *m_pIn_buf; + void *m_pOut_buf; + size_t *m_pIn_buf_size, *m_pOut_buf_size; + tdefl_flush m_flush; + const mz_uint8 *m_pSrc; + size_t m_src_buf_left, m_out_buf_ofs; + mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; + mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; + mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; + mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; + mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; + mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; +} tdefl_compressor; + +// Initializes the compressor. +// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. +// pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call the tdefl_compress_buffer() API for compression. +// If pBut_buf_func is NULL the user should always call the tdefl_compress() API. +// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) +MINIZ_STATIC tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); + +// Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible. +MINIZ_STATIC tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush); + +// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. +// tdefl_compress_buffer() always consumes the entire input buffer. +MINIZ_STATIC tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush); + +#ifndef MINIZ_SDL_NOUNUSED +MINIZ_STATIC tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d); +MINIZ_STATIC mz_uint32 tdefl_get_adler32(tdefl_compressor *d); +#endif + +// Can't use tdefl_create_comp_flags_from_zip_params if MINIZ_NO_ZLIB_APIS isn't defined, because it uses some of its macros. +#ifndef MINIZ_NO_ZLIB_APIS +// Create tdefl_compress() flags given zlib-style compression parameters. +// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) +// window_bits may be -15 (raw deflate) or 15 (zlib) +// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED +MINIZ_STATIC mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy); +#endif // #ifndef MINIZ_NO_ZLIB_APIS + +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifdef __cplusplus +} +#endif + +#endif // MINIZ_HEADER_INCLUDED + +// ------------------- End of Header: Implementation follows. (If you only want the header, define MINIZ_HEADER_FILE_ONLY.) + +#ifndef MINIZ_HEADER_FILE_ONLY + +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable:4505) // unreferenced local function has been removed +#endif + +typedef unsigned char mz_validate_uint16[sizeof(mz_uint16)==2 ? 1 : -1]; +typedef unsigned char mz_validate_uint32[sizeof(mz_uint32)==4 ? 1 : -1]; +typedef unsigned char mz_validate_uint64[sizeof(mz_uint64)==8 ? 1 : -1]; + +/*#include <string.h>*/ + +#ifndef MZ_ASSERT +#include <assert.h> +#define MZ_ASSERT(x) assert(x) +#endif + +#ifdef MINIZ_NO_MALLOC + #define MZ_MALLOC(x) NULL + #define MZ_FREE(x) (void)x, ((void)0) + #define MZ_REALLOC(p, x) NULL +#elif defined(MINIZ_SDL_MALLOC) + #define MZ_MALLOC(x) SDL_malloc(x) + #define MZ_FREE(x) SDL_free(x) + #define MZ_REALLOC(p, x) SDL_realloc(p, x) +#else + #define MZ_MALLOC(x) malloc(x) + #define MZ_FREE(x) free(x) + #define MZ_REALLOC(p, x) realloc(p, x) +#endif + +#define MZ_MAX(a,b) (((a)>(b))?(a):(b)) +#define MZ_MIN(a,b) (((a)<(b))?(a):(b)) +#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) +#define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj)) +#define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj)) + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN + #define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) + #define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) +#else + #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U)) + #define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U)) +#endif + +#ifdef _MSC_VER + #define MZ_FORCEINLINE __forceinline +#elif defined(__GNUC__) + #define MZ_FORCEINLINE inline __attribute__((__always_inline__)) +#else + #define MZ_FORCEINLINE inline +#endif + +#ifdef __cplusplus + extern "C" { +#endif + +// ------------------- zlib-style API's + +mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) +{ + mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); size_t block_len = buf_len % 5552; + if (!ptr) return MZ_ADLER32_INIT; + while (buf_len) { + for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { + s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1; + s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1; + } + for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1; + s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552; + } + return (s2 << 16) + s1; +} + +// Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/ +mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) +{ + static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; + mz_uint32 crcu32 = (mz_uint32)crc; + if (!ptr) return MZ_CRC32_INIT; + crcu32 = ~crcu32; while (buf_len--) { mz_uint8 b = *ptr++; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; } + return ~crcu32; +} + +MINIZ_STATIC void mz_free(void *p) +{ + MZ_FREE(p); +} + +#ifndef MINIZ_NO_ZLIB_APIS + +static void *def_alloc_func(void *opaque, size_t items, size_t size) { (void)opaque, (void)items, (void)size; return MZ_MALLOC(items * size); } +static void def_free_func(void *opaque, void *address) { (void)opaque, (void)address; MZ_FREE(address); } +static void *def_realloc_func(void *opaque, void *address, size_t items, size_t size) { (void)opaque, (void)address, (void)items, (void)size; return MZ_REALLOC(address, items * size); } + +const char *mz_version(void) +{ + return MZ_VERSION; +} + +#ifndef MINIZ_NO_DEFLATE_APIS + +int mz_deflateInit(mz_streamp pStream, int level) +{ + return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); +} + +int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) +{ + tdefl_compressor *pComp; + mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); + + if (!pStream) return MZ_STREAM_ERROR; + if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) return MZ_PARAM_ERROR; + + pStream->data_type = 0; + pStream->adler = MZ_ADLER32_INIT; + pStream->msg = NULL; + pStream->reserved = 0; + pStream->total_in = 0; + pStream->total_out = 0; + if (!pStream->zalloc) pStream->zalloc = def_alloc_func; + if (!pStream->zfree) pStream->zfree = def_free_func; + + pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor)); + if (!pComp) + return MZ_MEM_ERROR; + + pStream->state = (struct mz_internal_state *)pComp; + + if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) + { + mz_deflateEnd(pStream); + return MZ_PARAM_ERROR; + } + + return MZ_OK; +} + +int mz_deflateReset(mz_streamp pStream) +{ + if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) return MZ_STREAM_ERROR; + pStream->total_in = pStream->total_out = 0; + tdefl_init((tdefl_compressor*)pStream->state, NULL, NULL, ((tdefl_compressor*)pStream->state)->m_flags); + return MZ_OK; +} + +int mz_deflate(mz_streamp pStream, int flush) +{ + size_t in_bytes, out_bytes; + mz_ulong orig_total_in, orig_total_out; + int mz_status = MZ_OK; + + if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) return MZ_STREAM_ERROR; + if (!pStream->avail_out) return MZ_BUF_ERROR; + + if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; + + if (((tdefl_compressor*)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE) + return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; + + orig_total_in = pStream->total_in; orig_total_out = pStream->total_out; + for ( ; ; ) + { + tdefl_status defl_status; + in_bytes = pStream->avail_in; out_bytes = pStream->avail_out; + + defl_status = tdefl_compress((tdefl_compressor*)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush); + pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; + pStream->total_in += (mz_uint)in_bytes; pStream->adler = tdefl_get_adler32((tdefl_compressor*)pStream->state); + + pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; + pStream->total_out += (mz_uint)out_bytes; + + if (defl_status < 0) + { + mz_status = MZ_STREAM_ERROR; + break; + } + else if (defl_status == TDEFL_STATUS_DONE) + { + mz_status = MZ_STREAM_END; + break; + } + else if (!pStream->avail_out) + break; + else if ((!pStream->avail_in) && (flush != MZ_FINISH)) + { + if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) + break; + return MZ_BUF_ERROR; // Can't make forward progress without some input. + } + } + return mz_status; +} + +int mz_deflateEnd(mz_streamp pStream) +{ + if (!pStream) return MZ_STREAM_ERROR; + if (pStream->state) + { + pStream->zfree(pStream->opaque, pStream->state); + pStream->state = NULL; + } + return MZ_OK; +} + +mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) +{ + (void)pStream; + // This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.) + return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); +} + +int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level) +{ + int status; + mz_stream stream; + memset(&stream, 0, sizeof(stream)); + + // In case mz_ulong is 64-bits (argh I hate longs). + if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; + + stream.next_in = pSource; + stream.avail_in = (mz_uint32)source_len; + stream.next_out = pDest; + stream.avail_out = (mz_uint32)*pDest_len; + + status = mz_deflateInit(&stream, level); + if (status != MZ_OK) return status; + + status = mz_deflate(&stream, MZ_FINISH); + if (status != MZ_STREAM_END) + { + mz_deflateEnd(&stream); + return (status == MZ_OK) ? MZ_BUF_ERROR : status; + } + + *pDest_len = stream.total_out; + return mz_deflateEnd(&stream); +} + +int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) +{ + return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION); +} + +mz_ulong mz_compressBound(mz_ulong source_len) +{ + return mz_deflateBound(NULL, source_len); +} + +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS + +typedef struct +{ + tinfl_decompressor m_decomp; + mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; int m_window_bits; + mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; + tinfl_status m_last_status; +} inflate_state; + +int mz_inflateInit2(mz_streamp pStream, int window_bits) +{ + inflate_state *pDecomp; + if (!pStream) return MZ_STREAM_ERROR; + if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) return MZ_PARAM_ERROR; + + pStream->data_type = 0; + pStream->adler = 0; + pStream->msg = NULL; + pStream->total_in = 0; + pStream->total_out = 0; + pStream->reserved = 0; + if (!pStream->zalloc) pStream->zalloc = def_alloc_func; + if (!pStream->zfree) pStream->zfree = def_free_func; + + pDecomp = (inflate_state*)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state)); + if (!pDecomp) return MZ_MEM_ERROR; + + pStream->state = (struct mz_internal_state *)pDecomp; + + tinfl_init(&pDecomp->m_decomp); + pDecomp->m_dict_ofs = 0; + pDecomp->m_dict_avail = 0; + pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; + pDecomp->m_first_call = 1; + pDecomp->m_has_flushed = 0; + pDecomp->m_window_bits = window_bits; + + return MZ_OK; +} + +int mz_inflateInit(mz_streamp pStream) +{ + return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); +} + +int mz_inflate(mz_streamp pStream, int flush) +{ + inflate_state* pState; + mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; + size_t in_bytes, out_bytes, orig_avail_in; + tinfl_status status; + + if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR; + if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; + if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) return MZ_STREAM_ERROR; + + pState = (inflate_state*)pStream->state; + if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; + orig_avail_in = pStream->avail_in; + + first_call = pState->m_first_call; pState->m_first_call = 0; + if (pState->m_last_status < 0) return MZ_DATA_ERROR; + + if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR; + pState->m_has_flushed |= (flush == MZ_FINISH); + + if ((flush == MZ_FINISH) && (first_call)) + { + // MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. + decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; + in_bytes = pStream->avail_in; out_bytes = pStream->avail_out; + status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags); + pState->m_last_status = status; + pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_bytes; + pStream->adler = tinfl_get_adler32(&pState->m_decomp); + pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; pStream->total_out += (mz_uint)out_bytes; + + if (status < 0) + return MZ_DATA_ERROR; + else if (status != TINFL_STATUS_DONE) + { + pState->m_last_status = TINFL_STATUS_FAILED; + return MZ_BUF_ERROR; + } + return MZ_STREAM_END; + } + // flush != MZ_FINISH then we must assume there's more input. + if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; + + if (pState->m_dict_avail) + { + n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); + memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); + pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n; + pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); + return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; + } + + for ( ; ; ) + { + in_bytes = pStream->avail_in; + out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; + + status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags); + pState->m_last_status = status; + + pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; + pStream->total_in += (mz_uint)in_bytes; pStream->adler = tinfl_get_adler32(&pState->m_decomp); + + pState->m_dict_avail = (mz_uint)out_bytes; + + n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); + memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); + pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n; + pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); + + if (status < 0) + return MZ_DATA_ERROR; // Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). + else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) + return MZ_BUF_ERROR; // Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. + else if (flush == MZ_FINISH) + { + // The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. + if (status == TINFL_STATUS_DONE) + return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; + // status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. + else if (!pStream->avail_out) + return MZ_BUF_ERROR; + } + else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) + break; + } + + return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; +} + +int mz_inflateEnd(mz_streamp pStream) +{ + if (!pStream) + return MZ_STREAM_ERROR; + if (pStream->state) + { + pStream->zfree(pStream->opaque, pStream->state); + pStream->state = NULL; + } + return MZ_OK; +} + +int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) +{ + mz_stream stream; + int status; + memset(&stream, 0, sizeof(stream)); + + // In case mz_ulong is 64-bits (argh I hate longs). + if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; + + stream.next_in = pSource; + stream.avail_in = (mz_uint32)source_len; + stream.next_out = pDest; + stream.avail_out = (mz_uint32)*pDest_len; + + status = mz_inflateInit(&stream); + if (status != MZ_OK) + return status; + + status = mz_inflate(&stream, MZ_FINISH); + if (status != MZ_STREAM_END) + { + mz_inflateEnd(&stream); + return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; + } + *pDest_len = stream.total_out; + + return mz_inflateEnd(&stream); +} + +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + +const char *mz_error(int err) +{ + static struct { int m_err; const char *m_pDesc; } s_error_descs[] = + { + { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, + { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" } + }; + mz_uint i; for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc; + return NULL; +} + +#endif //MINIZ_NO_ZLIB_APIS + +// ------------------- Low-level Decompression (completely independent from all compression API's) +#ifndef MINIZ_NO_INFLATE_APIS + +#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l) +#define TINFL_MEMSET(p, c, l) memset(p, c, l) + +#define TINFL_CR_BEGIN switch(r->m_state) { case 0: +#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END +#define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END +#define TINFL_CR_FINISH } + +// TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never +// reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario. +#define TINFL_GET_BYTE(state_index, c) do { \ + if (pIn_buf_cur >= pIn_buf_end) { \ + for ( ; ; ) { \ + if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \ + TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \ + if (pIn_buf_cur < pIn_buf_end) { \ + c = *pIn_buf_cur++; \ + break; \ + } \ + } else { \ + c = 0; \ + break; \ + } \ + } \ + } else c = *pIn_buf_cur++; } MZ_MACRO_END + +#define TINFL_NEED_BITS(state_index, n) do { mz_uint c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (mz_uint)(n)) +#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END +#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END + +// TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. +// It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a +// Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the +// bit buffer contains >=15 bits (deflate's max. Huffman code size). +#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ + do { \ + temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ + if (temp >= 0) { \ + code_len = temp >> 9; \ + if ((code_len) && (num_bits >= code_len)) \ + break; \ + } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \ + code_len = TINFL_FAST_LOOKUP_BITS; \ + do { \ + temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ + } while ((temp < 0) && (num_bits >= (code_len + 1))); \ + if (temp >= 0) break; \ + } \ + TINFL_GET_BYTE(state_index, c); \ + bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ + num_bits += 8; \ + } while (num_bits < 15); + +// TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read +// beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully +// decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. +// The slow path is only executed at the very end of the input buffer. +#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \ + int temp; mz_uint code_len, c; \ + if (num_bits < 15) { \ + if ((pIn_buf_end - pIn_buf_cur) < 2) { \ + TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ + } else { \ + bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \ + } \ + } \ + if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ + code_len = temp >> 9, temp &= 511; \ + else { \ + code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \ + } sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END + +tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags) +{ + static const int s_length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 }; + static const int s_length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; + static const int s_dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; + static const int s_dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + static const mz_uint8 s_length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; + static const int s_min_table_sizes[3] = { 257, 1, 4 }; + + tinfl_status status = TINFL_STATUS_FAILED; mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf; + const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; + mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; + size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; + + // Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). + if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; } + + num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start; + TINFL_CR_BEGIN + + bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; r->m_z_adler32 = r->m_check_adler32 = 1; + if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) + { + TINFL_GET_BYTE(1, r->m_zhdr0); TINFL_GET_BYTE(2, r->m_zhdr1); + counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); + if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); + if (counter) { TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); } + } + + do + { + TINFL_GET_BITS(3, r->m_final, 3); r->m_type = r->m_final >> 1; + if (r->m_type == 0) + { + TINFL_SKIP_BITS(5, num_bits & 7); + for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(7, r->m_raw_header[counter]); } + if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); } + while ((counter) && (num_bits)) + { + TINFL_GET_BITS(51, dist, 8); + while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); } + *pOut_buf_cur++ = (mz_uint8)dist; + counter--; + } + while (counter) + { + size_t n; while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); } + while (pIn_buf_cur >= pIn_buf_end) + { + if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) + { + TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT); + } + else + { + TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED); + } + } + n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter); + TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (mz_uint)n; + } + } + else if (r->m_type == 3) + { + TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); + } + else + { + if (r->m_type == 1) + { + mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i; + r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); + for ( i = 0; i <= 143; ++i) *p++ = 8; + for ( ; i <= 255; ++i) *p++ = 9; + for ( ; i <= 279; ++i) *p++ = 7; + for ( ; i <= 287; ++i) *p++ = 8; + } + else + { + for (counter = 0; counter < 3; counter++) { TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; } + MZ_CLEAR_ARR(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; TINFL_GET_BITS(14, s, 3); r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; } + r->m_table_sizes[2] = 19; + } + for ( ; (int)r->m_type >= 0; r->m_type--) + { + int tree_next, tree_cur; tinfl_huff_table *pTable; + mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; MZ_CLEAR_ARR(total_syms); MZ_CLEAR_ARR(pTable->m_look_up); MZ_CLEAR_ARR(pTable->m_tree); + for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++; + used_syms = 0, total = 0; next_code[0] = next_code[1] = 0; + for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); } + if ((65536 != total) && (used_syms > 1)) + { + TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); + } + for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) + { + mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; if (!code_size) continue; + cur_code = next_code[code_size]++; for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1); + if (code_size <= TINFL_FAST_LOOKUP_BITS) { mz_int16 k = (mz_int16)((code_size << 9) | sym_index); while (rev_code < TINFL_FAST_LOOKUP_SIZE) { pTable->m_look_up[rev_code] = k; rev_code += (1 << code_size); } continue; } + if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } + rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); + for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) + { + tree_cur -= ((rev_code >>= 1) & 1); + if (!pTable->m_tree[-tree_cur - 1]) { pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } else tree_cur = pTable->m_tree[-tree_cur - 1]; + } + tree_cur -= ((rev_code >>= 1) & 1); pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; + } + if (r->m_type == 2) + { + for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]); ) + { + mz_uint s; TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); if (dist < 16) { r->m_len_codes[counter++] = (mz_uint8)dist; continue; } + if ((dist == 16) && (!counter)) + { + TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); + } + num_extra = "\02\03\07"[dist - 16]; TINFL_GET_BITS(18, s, num_extra); s += "\03\03\013"[dist - 16]; + TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); counter += s; + } + if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) + { + TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); + } + TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); + } + } + for ( ; ; ) + { + mz_uint8 *pSrc; + for ( ; ; ) + { + if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) + { + TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); + if (counter >= 256) + break; + while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); } + *pOut_buf_cur++ = (mz_uint8)counter; + } + else + { + int sym2; mz_uint code_len; +#if TINFL_USE_64BIT_BITBUF + if (num_bits < 30) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); pIn_buf_cur += 4; num_bits += 32; } +#else + if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; } +#endif + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) + code_len = sym2 >> 9; + else + { + code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0); + } + counter = sym2; bit_buf >>= code_len; num_bits -= code_len; + if (counter & 256) + break; + +#if !TINFL_USE_64BIT_BITBUF + if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; } +#endif + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) + code_len = sym2 >> 9; + else + { + code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0); + } + bit_buf >>= code_len; num_bits -= code_len; + + pOut_buf_cur[0] = (mz_uint8)counter; + if (sym2 & 256) + { + pOut_buf_cur++; + counter = sym2; + break; + } + pOut_buf_cur[1] = (mz_uint8)sym2; + pOut_buf_cur += 2; + } + } + if ((counter &= 511) == 256) break; + + num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257]; + if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; } + + TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); + num_extra = s_dist_extra[dist]; dist = s_dist_base[dist]; + if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; } + + dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; + if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) + { + TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); + } + + pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask); + + if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) + { + while (counter--) + { + while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); } + *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask]; + } + continue; + } +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES + else if ((counter >= 9) && (counter <= dist)) + { + const mz_uint8 *pSrc_end = pSrc + (counter & ~7); + do + { + ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0]; + ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1]; + pOut_buf_cur += 8; + } while ((pSrc += 8) < pSrc_end); + if ((counter &= 7) < 3) + { + if (counter) + { + pOut_buf_cur[0] = pSrc[0]; + if (counter > 1) + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur += counter; + } + continue; + } + } +#endif + do + { + pOut_buf_cur[0] = pSrc[0]; + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur[2] = pSrc[2]; + pOut_buf_cur += 3; pSrc += 3; + } while ((int)(counter -= 3) > 2); + if ((int)counter > 0) + { + pOut_buf_cur[0] = pSrc[0]; + if ((int)counter > 1) + pOut_buf_cur[1] = pSrc[1]; + pOut_buf_cur += counter; + } + } + } + } while (!(r->m_final & 1)); + if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) + { + TINFL_SKIP_BITS(32, num_bits & 7); for (counter = 0; counter < 4; ++counter) { mz_uint s; if (num_bits) TINFL_GET_BITS(41, s, 8); else TINFL_GET_BYTE(42, s); r->m_z_adler32 = (r->m_z_adler32 << 8) | s; } + } + TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); + TINFL_CR_FINISH + +common_exit: + r->m_num_bits = num_bits; r->m_bit_buf = bit_buf; r->m_dist = dist; r->m_counter = counter; r->m_num_extra = num_extra; r->m_dist_from_out_buf_start = dist_from_out_buf_start; + *pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next; + if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) + { + const mz_uint8 *ptr = pOut_buf_next; size_t buf_len = *pOut_buf_size; + mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; size_t block_len = buf_len % 5552; + while (buf_len) + { + for (i = 0; i + 7 < block_len; i += 8, ptr += 8) + { + s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1; + s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1; + } + for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1; + s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552; + } + r->m_check_adler32 = (s2 << 16) + s1; if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) status = TINFL_STATUS_ADLER32_MISMATCH; + } + return status; +} + +// Higher level helper functions. +void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) +{ + tinfl_decompressor decomp; void *pBuf = NULL, *pNew_buf; size_t src_buf_ofs = 0, out_buf_capacity = 0; + *pOut_len = 0; + tinfl_init(&decomp); + for ( ; ; ) + { + size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity; + tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8*)pBuf, pBuf ? (mz_uint8*)pBuf + *pOut_len : NULL, &dst_buf_size, + (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); + if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) + { + MZ_FREE(pBuf); *pOut_len = 0; return NULL; + } + src_buf_ofs += src_buf_size; + *pOut_len += dst_buf_size; + if (status == TINFL_STATUS_DONE) break; + new_out_buf_capacity = out_buf_capacity * 2; if (new_out_buf_capacity < 128) new_out_buf_capacity = 128; + pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity); + if (!pNew_buf) + { + MZ_FREE(pBuf); *pOut_len = 0; return NULL; + } + pBuf = pNew_buf; out_buf_capacity = new_out_buf_capacity; + } + return pBuf; +} + +size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags) +{ + tinfl_decompressor decomp; tinfl_status status; tinfl_init(&decomp); + status = tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf, &src_buf_len, (mz_uint8*)pOut_buf, (mz_uint8*)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); + return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len; +} + +int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + int result = 0; + tinfl_decompressor decomp; + mz_uint8 *pDict = (mz_uint8*)MZ_MALLOC(TINFL_LZ_DICT_SIZE); size_t in_buf_ofs = 0, dict_ofs = 0; + if (!pDict) + return TINFL_STATUS_FAILED; + memset(pDict,0,TINFL_LZ_DICT_SIZE); + tinfl_init(&decomp); + for ( ; ; ) + { + size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; + tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8*)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size, + (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))); + in_buf_ofs += in_buf_size; + if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) + break; + if (status != TINFL_STATUS_HAS_MORE_OUTPUT) + { + result = (status == TINFL_STATUS_DONE); + break; + } + dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1); + } + MZ_FREE(pDict); + *pIn_buf_size = in_buf_ofs; + return result; +} +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + +// ------------------- Low-level Compression (independent from all decompression API's) +#ifndef MINIZ_NO_DEFLATE_APIS + +// Purposely making these tables static for faster init and thread safety. +static const mz_uint16 s_tdefl_len_sym[256] = { + 257,258,259,260,261,262,263,264,265,265,266,266,267,267,268,268,269,269,269,269,270,270,270,270,271,271,271,271,272,272,272,272, + 273,273,273,273,273,273,273,273,274,274,274,274,274,274,274,274,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,276, + 277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278, + 279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280, + 281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281, + 282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282, + 283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283, + 284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,285 }; + +static const mz_uint8 s_tdefl_len_extra[256] = { + 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0 }; + +static const mz_uint8 s_tdefl_small_dist_sym[512] = { + 0,1,2,3,4,4,5,5,6,6,6,6,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11, + 11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13, + 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14, + 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, + 14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,16, + 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, + 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, + 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,17,17,17,17,17,17, + 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, + 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, + 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17 }; + +static const mz_uint8 s_tdefl_small_dist_extra[512] = { + 0,0,0,0,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7 }; + +static const mz_uint8 s_tdefl_large_dist_sym[128] = { + 0,0,18,19,20,20,21,21,22,22,22,22,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,26,26,26,26, + 26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28, + 28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29 }; + +static const mz_uint8 s_tdefl_large_dist_extra[128] = { + 0,0,8,8,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, + 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, + 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13 }; + +// Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. +typedef struct { mz_uint16 m_key, m_sym_index; } tdefl_sym_freq; +static tdefl_sym_freq* tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq* pSyms0, tdefl_sym_freq* pSyms1) +{ + mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; tdefl_sym_freq* pCur_syms = pSyms0, *pNew_syms = pSyms1; MZ_CLEAR_ARR(hist); + for (i = 0; i < num_syms; i++) { mz_uint freq = pSyms0[i].m_key; hist[freq & 0xFF]++; hist[256 + ((freq >> 8) & 0xFF)]++; } + while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) total_passes--; + for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) + { + const mz_uint32* pHist = &hist[pass << 8]; + mz_uint offsets[256], cur_ofs = 0; + for (i = 0; i < 256; i++) { offsets[i] = cur_ofs; cur_ofs += pHist[i]; } + for (i = 0; i < num_syms; i++) pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i]; + { tdefl_sym_freq* t = pCur_syms; pCur_syms = pNew_syms; pNew_syms = t; } + } + return pCur_syms; +} + +// tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. +static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n) +{ + int root, leaf, next, avbl, used, dpth; + if (n==0) return; else if (n==1) { A[0].m_key = 1; return; } + A[0].m_key += A[1].m_key; root = 0; leaf = 2; + for (next=1; next < n-1; next++) + { + if (leaf>=n || A[root].m_key<A[leaf].m_key) { A[next].m_key = A[root].m_key; A[root++].m_key = (mz_uint16)next; } else A[next].m_key = A[leaf++].m_key; + if (leaf>=n || (root<next && A[root].m_key<A[leaf].m_key)) { A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key); A[root++].m_key = (mz_uint16)next; } else A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key); + } + A[n-2].m_key = 0; for (next=n-3; next>=0; next--) A[next].m_key = A[A[next].m_key].m_key+1; + avbl = 1; used = dpth = 0; root = n-2; next = n-1; + while (avbl>0) + { + while (root>=0 && (int)A[root].m_key==dpth) { used++; root--; } + while (avbl>used) { A[next--].m_key = (mz_uint16)(dpth); avbl--; } + avbl = 2*used; dpth++; used = 0; + } +} + +// Limits canonical Huffman code table's max code size. +enum { TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 }; +static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size) +{ + int i; mz_uint32 total = 0; if (code_list_len <= 1) return; + for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) pNum_codes[max_code_size] += pNum_codes[i]; + for (i = max_code_size; i > 0; i--) total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i)); + while (total != (1UL << max_code_size)) + { + pNum_codes[max_code_size]--; + for (i = max_code_size - 1; i > 0; i--) if (pNum_codes[i]) { pNum_codes[i]--; pNum_codes[i + 1] += 2; break; } + total--; + } +} + +static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table) +{ + int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; MZ_CLEAR_ARR(num_codes); + if (static_table) + { + for (i = 0; i < table_len; i++) num_codes[d->m_huff_code_sizes[table_num][i]]++; + } + else + { + tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms; + int num_used_syms = 0; + const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0]; + for (i = 0; i < table_len; i++) if (pSym_count[i]) { syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; syms0[num_used_syms++].m_sym_index = (mz_uint16)i; } + + pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); tdefl_calculate_minimum_redundancy(pSyms, num_used_syms); + + for (i = 0; i < num_used_syms; i++) num_codes[pSyms[i].m_key]++; + + tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit); + + MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]); MZ_CLEAR_ARR(d->m_huff_codes[table_num]); + for (i = 1, j = num_used_syms; i <= code_size_limit; i++) + for (l = num_codes[i]; l > 0; l--) d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); + } + + next_code[1] = 0; for (j = 0, i = 2; i <= code_size_limit; i++) next_code[i] = j = ((j + num_codes[i - 1]) << 1); + + for (i = 0; i < table_len; i++) + { + mz_uint rev_code = 0, code, code_size; if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) continue; + code = next_code[code_size]++; for (l = code_size; l > 0; l--, code >>= 1) rev_code = (rev_code << 1) | (code & 1); + d->m_huff_codes[table_num][i] = (mz_uint16)rev_code; + } +} + +#define TDEFL_PUT_BITS(b, l) do { \ + mz_uint bits = b; mz_uint len = l; MZ_ASSERT(bits <= ((1U << len) - 1U)); \ + d->m_bit_buffer |= (bits << d->m_bits_in); d->m_bits_in += len; \ + while (d->m_bits_in >= 8) { \ + if (d->m_pOutput_buf < d->m_pOutput_buf_end) \ + *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \ + d->m_bit_buffer >>= 8; \ + d->m_bits_in -= 8; \ + } \ +} MZ_MACRO_END + +#define TDEFL_RLE_PREV_CODE_SIZE() do { \ + if (rle_repeat_count) { \ + if (rle_repeat_count < 3) { \ + d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \ + while (rle_repeat_count--) packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \ + } else { \ + d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); packed_code_sizes[num_packed_code_sizes++] = 16; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \ + } rle_repeat_count = 0; } \ +} MZ_MACRO_END + +#define TDEFL_RLE_ZERO_CODE_SIZE() do { \ + if (rle_z_count) { \ + if (rle_z_count < 3) { \ + d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); while (rle_z_count--) packed_code_sizes[num_packed_code_sizes++] = 0; \ + } else if (rle_z_count <= 10) { \ + d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); packed_code_sizes[num_packed_code_sizes++] = 17; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \ + } else { \ + d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); packed_code_sizes[num_packed_code_sizes++] = 18; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \ + } rle_z_count = 0; } \ +} MZ_MACRO_END + +static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + +static void tdefl_start_dynamic_block(tdefl_compressor *d) +{ + int num_lit_codes, num_dist_codes, num_bit_lengths; mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index; + mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF; + + d->m_huff_count[0][256] = 1; + + tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE); + tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE); + + for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) if (d->m_huff_code_sizes[0][num_lit_codes - 1]) break; + for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) if (d->m_huff_code_sizes[1][num_dist_codes - 1]) break; + + memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes); + memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes); + total_code_sizes_to_pack = num_lit_codes + num_dist_codes; num_packed_code_sizes = 0; rle_z_count = 0; rle_repeat_count = 0; + + memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2); + for (i = 0; i < total_code_sizes_to_pack; i++) + { + mz_uint8 code_size = code_sizes_to_pack[i]; + if (!code_size) + { + TDEFL_RLE_PREV_CODE_SIZE(); + if (++rle_z_count == 138) { TDEFL_RLE_ZERO_CODE_SIZE(); } + } + else + { + TDEFL_RLE_ZERO_CODE_SIZE(); + if (code_size != prev_code_size) + { + TDEFL_RLE_PREV_CODE_SIZE(); + d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1); packed_code_sizes[num_packed_code_sizes++] = code_size; + } + else if (++rle_repeat_count == 6) + { + TDEFL_RLE_PREV_CODE_SIZE(); + } + } + prev_code_size = code_size; + } + if (rle_repeat_count) { TDEFL_RLE_PREV_CODE_SIZE(); } else { TDEFL_RLE_ZERO_CODE_SIZE(); } + + tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE); + + TDEFL_PUT_BITS(2, 2); + + TDEFL_PUT_BITS(num_lit_codes - 257, 5); + TDEFL_PUT_BITS(num_dist_codes - 1, 5); + + for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) break; + num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1)); TDEFL_PUT_BITS(num_bit_lengths - 4, 4); + for (i = 0; (int)i < num_bit_lengths; i++) TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); + + for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes; ) + { + mz_uint code = packed_code_sizes[packed_code_sizes_index++]; MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2); + TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); + if (code >= 16) TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]); + } +} + +static void tdefl_start_static_block(tdefl_compressor *d) +{ + mz_uint i; + mz_uint8 *p = &d->m_huff_code_sizes[0][0]; + + for (i = 0; i <= 143; ++i) *p++ = 8; + for ( ; i <= 255; ++i) *p++ = 9; + for ( ; i <= 279; ++i) *p++ = 7; + for ( ; i <= 287; ++i) *p++ = 8; + + memset(d->m_huff_code_sizes[1], 5, 32); + + tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE); + tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE); + + TDEFL_PUT_BITS(1, 2); +} + +static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS +static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) +{ + mz_uint flags; + mz_uint8 *pLZ_codes; + mz_uint8 *pOutput_buf = d->m_pOutput_buf; + mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf; + mz_uint64 bit_buffer = d->m_bit_buffer; + mz_uint bits_in = d->m_bits_in; + +#define TDEFL_PUT_BITS_FAST(b, l) { bit_buffer |= (((mz_uint64)(b)) << bits_in); bits_in += (l); } + + flags = 1; + for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) + { + if (flags == 1) + flags = *pLZ_codes++ | 0x100; + + if (flags & 1) + { + mz_uint s0, s1, n0, n1, sym, num_extra_bits; + mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16 *)(pLZ_codes + 1); pLZ_codes += 3; + + MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); + + // This sequence coaxes MSVC into using cmov's vs. jmp's. + s0 = s_tdefl_small_dist_sym[match_dist & 511]; + n0 = s_tdefl_small_dist_extra[match_dist & 511]; + s1 = s_tdefl_large_dist_sym[match_dist >> 8]; + n1 = s_tdefl_large_dist_extra[match_dist >> 8]; + sym = (match_dist < 512) ? s0 : s1; + num_extra_bits = (match_dist < 512) ? n0 : n1; + + MZ_ASSERT(d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); + } + else + { + mz_uint lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + + if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) + { + flags >>= 1; + lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + + if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) + { + flags >>= 1; + lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + } + } + } + + if (pOutput_buf >= d->m_pOutput_buf_end) + return MZ_FALSE; + + *(mz_uint64*)pOutput_buf = bit_buffer; + pOutput_buf += (bits_in >> 3); + bit_buffer >>= (bits_in & ~7); + bits_in &= 7; + } + +#undef TDEFL_PUT_BITS_FAST + + d->m_pOutput_buf = pOutput_buf; + d->m_bits_in = 0; + d->m_bit_buffer = 0; + + while (bits_in) + { + mz_uint32 n = MZ_MIN(bits_in, 16); + TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n); + bit_buffer >>= n; + bits_in -= n; + } + + TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); + + return (d->m_pOutput_buf < d->m_pOutput_buf_end); +} +#else +static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) +{ + mz_uint flags; + mz_uint8 *pLZ_codes; + + flags = 1; + for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) + { + if (flags == 1) + flags = *pLZ_codes++ | 0x100; + if (flags & 1) + { + mz_uint sym, num_extra_bits; + mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); pLZ_codes += 3; + + MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); + TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); + + if (match_dist < 512) + { + sym = s_tdefl_small_dist_sym[match_dist]; num_extra_bits = s_tdefl_small_dist_extra[match_dist]; + } + else + { + sym = s_tdefl_large_dist_sym[match_dist >> 8]; num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8]; + } + MZ_ASSERT(d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); + TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); + } + else + { + mz_uint lit = *pLZ_codes++; + MZ_ASSERT(d->m_huff_code_sizes[0][lit]); + TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); + } + } + + TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); + + return (d->m_pOutput_buf < d->m_pOutput_buf_end); +} +#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS + +static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) +{ + if (static_block) + tdefl_start_static_block(d); + else + tdefl_start_dynamic_block(d); + return tdefl_compress_lz_codes(d); +} + +static int tdefl_flush_block(tdefl_compressor *d, int flush) +{ + mz_uint saved_bit_buf, saved_bits_in; + mz_uint8 *pSaved_output_buf; + mz_bool comp_block_succeeded = MZ_FALSE; + int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; + mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf; + + d->m_pOutput_buf = pOutput_buf_start; + d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16; + + MZ_ASSERT(!d->m_output_flush_remaining); + d->m_output_flush_ofs = 0; + d->m_output_flush_remaining = 0; + + *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left); + d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); + + if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) + { + TDEFL_PUT_BITS(0x78, 8); TDEFL_PUT_BITS(0x01, 8); + } + + TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); + + pSaved_output_buf = d->m_pOutput_buf; saved_bit_buf = d->m_bit_buffer; saved_bits_in = d->m_bits_in; + + if (!use_raw_block) + comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48)); + + // If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. + if ( ((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && + ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size) ) + { + mz_uint i; d->m_pOutput_buf = pSaved_output_buf; d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; + TDEFL_PUT_BITS(0, 2); + if (d->m_bits_in) { TDEFL_PUT_BITS(0, 8 - d->m_bits_in); } + for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) + { + TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); + } + for (i = 0; i < d->m_total_lz_bytes; ++i) + { + TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8); + } + } + // Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. + else if (!comp_block_succeeded) + { + d->m_pOutput_buf = pSaved_output_buf; d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; + tdefl_compress_block(d, MZ_TRUE); + } + + if (flush) + { + if (flush == TDEFL_FINISH) + { + if (d->m_bits_in) { TDEFL_PUT_BITS(0, 8 - d->m_bits_in); } + if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) { mz_uint i, a = d->m_adler32; for (i = 0; i < 4; i++) { TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); a <<= 8; } } + } + else + { + mz_uint i, z = 0; TDEFL_PUT_BITS(0, 3); if (d->m_bits_in) { TDEFL_PUT_BITS(0, 8 - d->m_bits_in); } for (i = 2; i; --i, z ^= 0xFFFF) { TDEFL_PUT_BITS(z & 0xFFFF, 16); } + } + } + + MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end); + + memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); + memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); + + d->m_pLZ_code_buf = d->m_lz_code_buf + 1; d->m_pLZ_flags = d->m_lz_code_buf; d->m_num_flags_left = 8; d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes; d->m_total_lz_bytes = 0; d->m_block_index++; + + if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) + { + if (d->m_pPut_buf_func) + { + *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; + if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user)) + return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED); + } + else if (pOutput_buf_start == d->m_output_buf) + { + int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs)); + memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy); + d->m_out_buf_ofs += bytes_to_copy; + if ((n -= bytes_to_copy) != 0) + { + d->m_output_flush_ofs = bytes_to_copy; + d->m_output_flush_remaining = n; + } + } + else + { + d->m_out_buf_ofs += n; + } + } + + return d->m_output_flush_remaining; +} + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES +#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p) +static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) +{ + mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len; + mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; + const mz_uint16 *s = (const mz_uint16*)(d->m_dict + pos), *p, *q; + mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s); + MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); if (max_match_len <= match_len) return; + for ( ; ; ) + { + for ( ; ; ) + { + if (--num_probes_left == 0) return; + #define TDEFL_PROBE \ + next_probe_pos = d->m_next[probe_pos]; \ + if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \ + probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ + if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) break; + TDEFL_PROBE + TDEFL_PROBE + TDEFL_PROBE + } + if (!dist) break; + q = (const mz_uint16*)(d->m_dict + probe_pos); + if (TDEFL_READ_UNALIGNED_WORD(q) != s01) continue; + p = s; probe_len = 32; + do { } while ( (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && + (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0) ); + if (!probe_len) + { + *pMatch_dist = dist; *pMatch_len = MZ_MIN(max_match_len, TDEFL_MAX_MATCH_LEN); break; + } + else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8*)p == *(const mz_uint8*)q)) > match_len) + { + *pMatch_dist = dist; if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len) break; + c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]); + } + } +} +#else +static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len) +{ + mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len; + mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; + const mz_uint8 *s = d->m_dict + pos, *p, *q; + mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1]; + MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); if (max_match_len <= match_len) return; + for ( ; ; ) + { + for ( ; ; ) + { + if (--num_probes_left == 0) return; + #define TDEFL_PROBE \ + next_probe_pos = d->m_next[probe_pos]; \ + if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \ + probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ + if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) break; + TDEFL_PROBE + TDEFL_PROBE + TDEFL_PROBE + } + if (!dist) break; + p = s; q = d->m_dict + probe_pos; + for (probe_len = 0; probe_len < max_match_len; probe_len++) { + if (*p++ != *q++) break; + } + if (probe_len > match_len) + { + *pMatch_dist = dist; + if ((*pMatch_len = match_len = probe_len) == max_match_len) return; + c0 = d->m_dict[pos + match_len]; c1 = d->m_dict[pos + match_len - 1]; + } + } +} +#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN +static mz_bool tdefl_compress_fast(tdefl_compressor *d) +{ + // Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. + mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left; + mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags; + mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; + + while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) + { + const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096; + mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; + mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size); + d->m_src_buf_left -= num_bytes_to_process; + lookahead_size += num_bytes_to_process; + + while (num_bytes_to_process) + { + mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process); + memcpy(d->m_dict + dst_pos, d->m_pSrc, n); + if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) + memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos)); + d->m_pSrc += n; + dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK; + num_bytes_to_process -= n; + } + + dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size); + if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE)) break; + + while (lookahead_size >= 4) + { + mz_uint cur_match_dist, cur_match_len = 1; + mz_uint8 *pCur_dict = d->m_dict + cur_pos; + mz_uint first_trigram = (*(const mz_uint32 *)pCur_dict) & 0xFFFFFF; + mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK; + mz_uint probe_pos = d->m_hash[hash]; + d->m_hash[hash] = (mz_uint16)lookahead_pos; + + if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((*(const mz_uint32 *)(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram)) + { + const mz_uint16 *p = (const mz_uint16 *)pCur_dict; + const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos); + mz_uint32 probe_len = 32; + do { } while ( (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && + (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0) ); + cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q); + if (!probe_len) + cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0; + + if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U*1024U))) + { + cur_match_len = 1; + *pLZ_code_buf++ = (mz_uint8)first_trigram; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + d->m_huff_count[0][(mz_uint8)first_trigram]++; + } + else + { + mz_uint32 s0, s1; + cur_match_len = MZ_MIN(cur_match_len, lookahead_size); + + MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE)); + + cur_match_dist--; + + pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN); + *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist; + pLZ_code_buf += 3; + *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80); + + s0 = s_tdefl_small_dist_sym[cur_match_dist & 511]; + s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8]; + d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++; + + d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++; + } + } + else + { + *pLZ_code_buf++ = (mz_uint8)first_trigram; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + d->m_huff_count[0][(mz_uint8)first_trigram]++; + } + + if (--num_flags_left == 0) { num_flags_left = 8; pLZ_flags = pLZ_code_buf++; } + + total_lz_bytes += cur_match_len; + lookahead_pos += cur_match_len; + dict_size = MZ_MIN(dict_size + cur_match_len, TDEFL_LZ_DICT_SIZE); + cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK; + MZ_ASSERT(lookahead_size >= cur_match_len); + lookahead_size -= cur_match_len; + + if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) + { + int n; + d->m_lookahead_pos = lookahead_pos; d->m_lookahead_size = lookahead_size; d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; d->m_pLZ_code_buf = pLZ_code_buf; d->m_pLZ_flags = pLZ_flags; d->m_num_flags_left = num_flags_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + total_lz_bytes = d->m_total_lz_bytes; pLZ_code_buf = d->m_pLZ_code_buf; pLZ_flags = d->m_pLZ_flags; num_flags_left = d->m_num_flags_left; + } + } + + while (lookahead_size) + { + mz_uint8 lit = d->m_dict[cur_pos]; + + total_lz_bytes++; + *pLZ_code_buf++ = lit; + *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); + if (--num_flags_left == 0) { num_flags_left = 8; pLZ_flags = pLZ_code_buf++; } + + d->m_huff_count[0][lit]++; + + lookahead_pos++; + dict_size = MZ_MIN(dict_size + 1, TDEFL_LZ_DICT_SIZE); + cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; + lookahead_size--; + + if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) + { + int n; + d->m_lookahead_pos = lookahead_pos; d->m_lookahead_size = lookahead_size; d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; d->m_pLZ_code_buf = pLZ_code_buf; d->m_pLZ_flags = pLZ_flags; d->m_num_flags_left = num_flags_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + total_lz_bytes = d->m_total_lz_bytes; pLZ_code_buf = d->m_pLZ_code_buf; pLZ_flags = d->m_pLZ_flags; num_flags_left = d->m_num_flags_left; + } + } + } + + d->m_lookahead_pos = lookahead_pos; d->m_lookahead_size = lookahead_size; d->m_dict_size = dict_size; + d->m_total_lz_bytes = total_lz_bytes; d->m_pLZ_code_buf = pLZ_code_buf; d->m_pLZ_flags = pLZ_flags; d->m_num_flags_left = num_flags_left; + return MZ_TRUE; +} +#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN + +static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit) +{ + d->m_total_lz_bytes++; + *d->m_pLZ_code_buf++ = lit; + *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1); if (--d->m_num_flags_left == 0) { d->m_num_flags_left = 8; d->m_pLZ_flags = d->m_pLZ_code_buf++; } + d->m_huff_count[0][lit]++; +} + +static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist) +{ + mz_uint32 s0, s1; + + MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE)); + + d->m_total_lz_bytes += match_len; + + d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN); + + match_dist -= 1; + d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF); + d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8); d->m_pLZ_code_buf += 3; + + *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80); if (--d->m_num_flags_left == 0) { d->m_num_flags_left = 8; d->m_pLZ_flags = d->m_pLZ_code_buf++; } + + s0 = s_tdefl_small_dist_sym[match_dist & 511]; s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127]; + d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; + + if (match_len >= TDEFL_MIN_MATCH_LEN) d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; +} + +static mz_bool tdefl_compress_normal(tdefl_compressor *d) +{ + const mz_uint8 *pSrc = d->m_pSrc; size_t src_buf_left = d->m_src_buf_left; + tdefl_flush flush = d->m_flush; + + while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) + { + mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos; + // Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. + if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) + { + mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2; + mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]; + mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); + const mz_uint8 *pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL; + src_buf_left -= num_bytes_to_process; + d->m_lookahead_size += num_bytes_to_process; + while (pSrc != pSrc_end) + { + mz_uint8 c = *pSrc++; d->m_dict[dst_pos] = c; if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; + hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); + d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; d->m_hash[hash] = (mz_uint16)(ins_pos); + dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; ins_pos++; + } + } + else + { + while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) + { + mz_uint8 c = *pSrc++; + mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; + src_buf_left--; + d->m_dict[dst_pos] = c; + if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) + d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; + if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) + { + mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2; + mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); + d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; d->m_hash[hash] = (mz_uint16)(ins_pos); + } + } + } + d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size); + if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) + break; + + // Simple lazy/greedy parsing state machine. + len_to_move = 1; cur_match_dist = 0; cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1); cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; + if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) + { + if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) + { + mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK]; + cur_match_len = 0; while (cur_match_len < d->m_lookahead_size) { if (d->m_dict[cur_pos + cur_match_len] != c) break; cur_match_len++; } + if (cur_match_len < TDEFL_MIN_MATCH_LEN) cur_match_len = 0; else cur_match_dist = 1; + } + } + else + { + tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len); + } + if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U*1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) + { + cur_match_dist = cur_match_len = 0; + } + if (d->m_saved_match_len) + { + if (cur_match_len > d->m_saved_match_len) + { + tdefl_record_literal(d, (mz_uint8)d->m_saved_lit); + if (cur_match_len >= 128) + { + tdefl_record_match(d, cur_match_len, cur_match_dist); + d->m_saved_match_len = 0; len_to_move = cur_match_len; + } + else + { + d->m_saved_lit = d->m_dict[cur_pos]; d->m_saved_match_dist = cur_match_dist; d->m_saved_match_len = cur_match_len; + } + } + else + { + tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist); + len_to_move = d->m_saved_match_len - 1; d->m_saved_match_len = 0; + } + } + else if (!cur_match_dist) + tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]); + else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) + { + tdefl_record_match(d, cur_match_len, cur_match_dist); + len_to_move = cur_match_len; + } + else + { + d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]; d->m_saved_match_dist = cur_match_dist; d->m_saved_match_len = cur_match_len; + } + // Move the lookahead forward by len_to_move bytes. + d->m_lookahead_pos += len_to_move; + MZ_ASSERT(d->m_lookahead_size >= len_to_move); + d->m_lookahead_size -= len_to_move; + d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, TDEFL_LZ_DICT_SIZE); + // Check if it's time to flush the current LZ codes to the internal output buffer. + if ( (d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || + ( (d->m_total_lz_bytes > 31*1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) ) + { + int n; + d->m_pSrc = pSrc; d->m_src_buf_left = src_buf_left; + if ((n = tdefl_flush_block(d, 0)) != 0) + return (n < 0) ? MZ_FALSE : MZ_TRUE; + } + } + + d->m_pSrc = pSrc; d->m_src_buf_left = src_buf_left; + return MZ_TRUE; +} + +static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d) +{ + if (d->m_pIn_buf_size) + { + *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf; + } + + if (d->m_pOut_buf_size) + { + size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining); + memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n); + d->m_output_flush_ofs += (mz_uint)n; + d->m_output_flush_remaining -= (mz_uint)n; + d->m_out_buf_ofs += n; + + *d->m_pOut_buf_size = d->m_out_buf_ofs; + } + + return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY; +} + +tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush) +{ + if (!d) + { + if (pIn_buf_size) *pIn_buf_size = 0; + if (pOut_buf_size) *pOut_buf_size = 0; + return TDEFL_STATUS_BAD_PARAM; + } + + d->m_pIn_buf = pIn_buf; d->m_pIn_buf_size = pIn_buf_size; + d->m_pOut_buf = pOut_buf; d->m_pOut_buf_size = pOut_buf_size; + d->m_pSrc = (const mz_uint8 *)(pIn_buf); d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0; + d->m_out_buf_ofs = 0; + d->m_flush = flush; + + if ( ((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || + (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf) ) + { + if (pIn_buf_size) *pIn_buf_size = 0; + if (pOut_buf_size) *pOut_buf_size = 0; + return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM); + } + d->m_wants_to_finish |= (flush == TDEFL_FINISH); + + if ((d->m_output_flush_remaining) || (d->m_finished)) + return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); + +#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN + if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) && + ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) && + ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0)) + { + if (!tdefl_compress_fast(d)) + return d->m_prev_return_status; + } + else +#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN + { + if (!tdefl_compress_normal(d)) + return d->m_prev_return_status; + } + + if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf)) + d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf); + + if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) + { + if (tdefl_flush_block(d, flush) < 0) + return d->m_prev_return_status; + d->m_finished = (flush == TDEFL_FINISH); + if (flush == TDEFL_FULL_FLUSH) { MZ_CLEAR_ARR(d->m_hash); MZ_CLEAR_ARR(d->m_next); d->m_dict_size = 0; } + } + + return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); +} + +tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush) +{ + MZ_ASSERT(d->m_pPut_buf_func); return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush); +} + +tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + d->m_pPut_buf_func = pPut_buf_func; d->m_pPut_buf_user = pPut_buf_user; + d->m_flags = (mz_uint)(flags); d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3; d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; + d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; + if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_ARR(d->m_hash); + d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0; + d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0; + d->m_pLZ_code_buf = d->m_lz_code_buf + 1; d->m_pLZ_flags = d->m_lz_code_buf; d->m_num_flags_left = 8; + d->m_pOutput_buf = d->m_output_buf; d->m_pOutput_buf_end = d->m_output_buf; d->m_prev_return_status = TDEFL_STATUS_OKAY; + d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0; d->m_adler32 = 1; + d->m_pIn_buf = NULL; d->m_pOut_buf = NULL; + d->m_pIn_buf_size = NULL; d->m_pOut_buf_size = NULL; + d->m_flush = TDEFL_NO_FLUSH; d->m_pSrc = NULL; d->m_src_buf_left = 0; d->m_out_buf_ofs = 0; + memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); + memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); + return TDEFL_STATUS_OKAY; +} + +#ifndef MINIZ_SDL_NOUNUSED +tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d) +{ + return d->m_prev_return_status; +} + +mz_uint32 tdefl_get_adler32(tdefl_compressor *d) +{ + return d->m_adler32; +} + +mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) +{ + tdefl_compressor *pComp; mz_bool succeeded; if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) return MZ_FALSE; + pComp = (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); if (!pComp) return MZ_FALSE; + succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY); + succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE); + MZ_FREE(pComp); return succeeded; +} +#endif /* MINIZ_SDL_NOUNUSED */ + +typedef struct +{ + size_t m_size, m_capacity; + mz_uint8 *m_pBuf; + mz_bool m_expandable; +} tdefl_output_buffer; + +static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser) +{ + tdefl_output_buffer *p = (tdefl_output_buffer *)pUser; + size_t new_size = p->m_size + len; + if (new_size > p->m_capacity) + { + size_t new_capacity = p->m_capacity; mz_uint8 *pNew_buf; if (!p->m_expandable) return MZ_FALSE; + do { new_capacity = MZ_MAX(128U, new_capacity << 1U); } while (new_size > new_capacity); + pNew_buf = (mz_uint8*)MZ_REALLOC(p->m_pBuf, new_capacity); if (!pNew_buf) return MZ_FALSE; + p->m_pBuf = pNew_buf; p->m_capacity = new_capacity; + } + memcpy((mz_uint8*)p->m_pBuf + p->m_size, pBuf, len); p->m_size = new_size; + return MZ_TRUE; +} + +#ifndef MINIZ_SDL_NOUNUSED +void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) +{ + tdefl_output_buffer out_buf; MZ_CLEAR_OBJ(out_buf); + if (!pOut_len) return MZ_FALSE; else *pOut_len = 0; + out_buf.m_expandable = MZ_TRUE; + if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) return NULL; + *pOut_len = out_buf.m_size; return out_buf.m_pBuf; +} + +size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags) +{ + tdefl_output_buffer out_buf; MZ_CLEAR_OBJ(out_buf); + if (!pOut_buf) return 0; + out_buf.m_pBuf = (mz_uint8*)pOut_buf; out_buf.m_capacity = out_buf_len; + if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) return 0; + return out_buf.m_size; +} +#endif /* MINIZ_SDL_NOUNUSED */ + +#ifndef MINIZ_NO_ZLIB_APIS +static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; + +// level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). +mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy) +{ + mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); + if (window_bits > 0) comp_flags |= TDEFL_WRITE_ZLIB_HEADER; + + if (!level) comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; + else if (strategy == MZ_FILTERED) comp_flags |= TDEFL_FILTER_MATCHES; + else if (strategy == MZ_HUFFMAN_ONLY) comp_flags &= ~TDEFL_MAX_PROBES_MASK; + else if (strategy == MZ_FIXED) comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS; + else if (strategy == MZ_RLE) comp_flags |= TDEFL_RLE_MATCHES; + + return comp_flags; +} +#endif //MINIZ_NO_ZLIB_APIS + +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable:4204) // nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) +#endif + +// Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at +// http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/. +// This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. +MINIZ_STATIC void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, int bpl, size_t *pLen_out, mz_uint level, mz_bool flip, mz_uint8 *plte, int plte_size, mz_uint8 *trns, int trns_size) +{ + // Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. + static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; + tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); tdefl_output_buffer out_buf; int i, y, z = 0; mz_uint32 c; *pLen_out = 0; size_t data_start, data_size; + if (!pComp) return NULL; + MZ_CLEAR_OBJ(out_buf); out_buf.m_expandable = MZ_TRUE; + out_buf.m_capacity = 57+MZ_MAX(64, (1+w*num_chans)*h); + if (plte_size > 0) + out_buf.m_capacity += 12+plte_size; + if (trns_size > 0) + out_buf.m_capacity += 12+trns_size; + if (NULL == (out_buf.m_pBuf = (mz_uint8*)MZ_MALLOC(out_buf.m_capacity))) { MZ_FREE(pComp); return NULL; } + // write header + { + static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06}; + + mz_uint8 pnghdr[33]={ 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a, + 0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + + pnghdr[18] = (mz_uint8)(w>>8); + pnghdr[19] = (mz_uint8)(w>>0); + + pnghdr[22] = (mz_uint8)(h>>8); + pnghdr[23] = (mz_uint8)(h>>0); + + if (num_chans == 1 && plte_size > 0) + pnghdr[25] = 3; + else + pnghdr[25] = (chans[num_chans]); + + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT,pnghdr+12,17); + for (i=0; i<4; ++i, c<<=8) ((mz_uint8*)(pnghdr+29))[i] = (mz_uint8)(c>>24); + if (!tdefl_output_buffer_putter(pnghdr, sizeof(pnghdr), &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + } + // write PLTE chunk + if (plte_size > 0) + { + mz_uint8 hdr[8]={0x00,0x00,0x00,0x00,0x50,0x4c,0x54,0x45}; + + hdr[0] = (mz_uint8)(plte_size>>24); + hdr[1] = (mz_uint8)(plte_size>>16); + hdr[2] = (mz_uint8)(plte_size>> 8); + hdr[3] = (mz_uint8)(plte_size>> 0); + if (!tdefl_output_buffer_putter(hdr, sizeof(hdr), &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + data_start = out_buf.m_size; + if (!tdefl_output_buffer_putter(plte, plte_size, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + if (!tdefl_output_buffer_putter("\0\0\0\0", 4, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT,out_buf.m_pBuf+data_start-4, plte_size+4); + for (i=0; i<4; ++i, c<<=8) (out_buf.m_pBuf+out_buf.m_size-4)[i] = (mz_uint8)(c >> 24); + } + // write tRNS chunk + if (trns_size > 0) + { + mz_uint8 hdr[8]={0x00,0x00,0x00,0x00,0x74,0x52,0x4E,0x53}; + + hdr[0] = (mz_uint8)(trns_size>>24); + hdr[1] = (mz_uint8)(trns_size>>16); + hdr[2] = (mz_uint8)(trns_size>> 8); + hdr[3] = (mz_uint8)(trns_size>> 0); + if (!tdefl_output_buffer_putter(hdr, sizeof(hdr), &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + data_start = out_buf.m_size; + if (!tdefl_output_buffer_putter(trns, trns_size, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + if (!tdefl_output_buffer_putter("\0\0\0\0", 4, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT,out_buf.m_pBuf+data_start-4, trns_size+4); + for (i=0; i<4; ++i, c<<=8) (out_buf.m_pBuf+out_buf.m_size-4)[i] = (mz_uint8)(c >> 24); + } + // write IDAT chunk + if (!tdefl_output_buffer_putter("\0\0\0\0\x49\x44\x41\x54", 8, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + data_start = out_buf.m_size; + // compress image data + tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER); + for (y = 0; y < h; ++y) { tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH); tdefl_compress_buffer(pComp, (mz_uint8*)pImage + (flip ? (h - 1 - y) : y) * bpl, w * num_chans, TDEFL_NO_FLUSH); } + if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE) { MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + // write IDAT size + data_size = out_buf.m_size-data_start; + (out_buf.m_pBuf+data_start-8)[0] = (mz_uint8)(data_size>>24); + (out_buf.m_pBuf+data_start-8)[1] = (mz_uint8)(data_size>>16); + (out_buf.m_pBuf+data_start-8)[2] = (mz_uint8)(data_size>> 8); + (out_buf.m_pBuf+data_start-8)[3] = (mz_uint8)(data_size>> 0); + // write footer (IDAT CRC-32, followed by IEND chunk) + if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) { *pLen_out = 0; MZ_FREE(pComp); MZ_FREE(out_buf.m_pBuf); return NULL; } + c = (mz_uint32)mz_crc32(MZ_CRC32_INIT,out_buf.m_pBuf+data_start-4, data_size+4); + for (i=0; i<4; ++i, c<<=8) (out_buf.m_pBuf+out_buf.m_size-16)[i] = (mz_uint8)(c >> 24); + // compute final size of file, grab compressed data buffer and return + *pLen_out = out_buf.m_size; + MZ_FREE(pComp); + return out_buf.m_pBuf; +} +#ifndef MINIZ_SDL_NOUNUSED +MINIZ_STATIC void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, int bpl, size_t *pLen_out) +{ + // Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out) + return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, bpl, pLen_out, 6, MZ_FALSE, NULL, 0, NULL, 0); +} +#endif + +#ifdef _MSC_VER +#pragma warning (pop) +#endif +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +// ------------------- .ZIP archive reading + +#ifndef MINIZ_NO_ARCHIVE_APIS + +#ifdef MINIZ_NO_STDIO + #define MZ_FILE void * +#else + #include <stdio.h> + #include <sys/stat.h> + + #if defined(_MSC_VER) || defined(__MINGW64__) + static FILE *mz_fopen(const char *pFilename, const char *pMode) + { + FILE* pFile = NULL; + fopen_s(&pFile, pFilename, pMode); + return pFile; + } + static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) + { + FILE* pFile = NULL; + if (freopen_s(&pFile, pPath, pMode, pStream)) + return NULL; + return pFile; + } + #ifndef MINIZ_NO_TIME + #include <sys/utime.h> + #endif + #define MZ_FILE FILE + #define MZ_FOPEN mz_fopen + #define MZ_FCLOSE fclose + #define MZ_FREAD fread + #define MZ_FWRITE fwrite + #define MZ_FTELL64 _ftelli64 + #define MZ_FSEEK64 _fseeki64 + #define MZ_FILE_STAT_STRUCT _stat + #define MZ_FILE_STAT _stat + #define MZ_FFLUSH fflush + #define MZ_FREOPEN mz_freopen + #define MZ_DELETE_FILE remove + #elif defined(__MINGW32__) + #ifndef MINIZ_NO_TIME + #include <sys/utime.h> + #endif + #define MZ_FILE FILE + #define MZ_FOPEN(f, m) fopen(f, m) + #define MZ_FCLOSE fclose + #define MZ_FREAD fread + #define MZ_FWRITE fwrite + #define MZ_FTELL64 ftello64 + #define MZ_FSEEK64 fseeko64 + #define MZ_FILE_STAT_STRUCT _stat + #define MZ_FILE_STAT _stat + #define MZ_FFLUSH fflush + #define MZ_FREOPEN(f, m, s) freopen(f, m, s) + #define MZ_DELETE_FILE remove + #elif defined(__TINYC__) + #ifndef MINIZ_NO_TIME + #include <sys/utime.h> + #endif + #define MZ_FILE FILE + #define MZ_FOPEN(f, m) fopen(f, m) + #define MZ_FCLOSE fclose + #define MZ_FREAD fread + #define MZ_FWRITE fwrite + #define MZ_FTELL64 ftell + #define MZ_FSEEK64 fseek + #define MZ_FILE_STAT_STRUCT stat + #define MZ_FILE_STAT stat + #define MZ_FFLUSH fflush + #define MZ_FREOPEN(f, m, s) freopen(f, m, s) + #define MZ_DELETE_FILE remove + #elif defined(__GNUC__) && _LARGEFILE64_SOURCE + #ifndef MINIZ_NO_TIME + #include <utime.h> + #endif + #define MZ_FILE FILE + #define MZ_FOPEN(f, m) fopen64(f, m) + #define MZ_FCLOSE fclose + #define MZ_FREAD fread + #define MZ_FWRITE fwrite + #define MZ_FTELL64 ftello64 + #define MZ_FSEEK64 fseeko64 + #define MZ_FILE_STAT_STRUCT stat64 + #define MZ_FILE_STAT stat64 + #define MZ_FFLUSH fflush + #define MZ_FREOPEN(p, m, s) freopen64(p, m, s) + #define MZ_DELETE_FILE remove + #else + #ifndef MINIZ_NO_TIME + #include <utime.h> + #endif + #define MZ_FILE FILE + #define MZ_FOPEN(f, m) fopen(f, m) + #define MZ_FCLOSE fclose + #define MZ_FREAD fread + #define MZ_FWRITE fwrite + #define MZ_FTELL64 ftello + #define MZ_FSEEK64 fseeko + #define MZ_FILE_STAT_STRUCT stat + #define MZ_FILE_STAT stat + #define MZ_FFLUSH fflush + #define MZ_FREOPEN(f, m, s) freopen(f, m, s) + #define MZ_DELETE_FILE remove + #endif // #ifdef _MSC_VER +#endif // #ifdef MINIZ_NO_STDIO + +#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c)) + +// Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff. +enum +{ + // ZIP archive identifiers and record sizes + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50, + MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22, + // Central directory header record offsets + MZ_ZIP_CDH_SIG_OFS = 0, MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4, MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6, MZ_ZIP_CDH_BIT_FLAG_OFS = 8, + MZ_ZIP_CDH_METHOD_OFS = 10, MZ_ZIP_CDH_FILE_TIME_OFS = 12, MZ_ZIP_CDH_FILE_DATE_OFS = 14, MZ_ZIP_CDH_CRC32_OFS = 16, + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20, MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, MZ_ZIP_CDH_FILENAME_LEN_OFS = 28, MZ_ZIP_CDH_EXTRA_LEN_OFS = 30, + MZ_ZIP_CDH_COMMENT_LEN_OFS = 32, MZ_ZIP_CDH_DISK_START_OFS = 34, MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36, MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38, MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42, + // Local directory header offsets + MZ_ZIP_LDH_SIG_OFS = 0, MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4, MZ_ZIP_LDH_BIT_FLAG_OFS = 6, MZ_ZIP_LDH_METHOD_OFS = 8, MZ_ZIP_LDH_FILE_TIME_OFS = 10, + MZ_ZIP_LDH_FILE_DATE_OFS = 12, MZ_ZIP_LDH_CRC32_OFS = 14, MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18, MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22, + MZ_ZIP_LDH_FILENAME_LEN_OFS = 26, MZ_ZIP_LDH_EXTRA_LEN_OFS = 28, + // End of central directory offsets + MZ_ZIP_ECDH_SIG_OFS = 0, MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4, MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8, + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12, MZ_ZIP_ECDH_CDIR_OFS_OFS = 16, MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20, +}; + +typedef struct +{ + void *m_p; + size_t m_size, m_capacity; + mz_uint m_element_size; +} mz_zip_array; + +struct mz_zip_internal_state_tag +{ + mz_zip_array m_central_dir; + mz_zip_array m_central_dir_offsets; + mz_zip_array m_sorted_central_dir_offsets; + MZ_FILE *m_pFile; + void *m_pMem; + size_t m_mem_size; + size_t m_mem_capacity; +}; + +#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size +#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index] + +static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray) +{ + pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p); + memset(pArray, 0, sizeof(mz_zip_array)); +} + +static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing) +{ + void *pNew_p; size_t new_capacity = min_new_capacity; MZ_ASSERT(pArray->m_element_size); if (pArray->m_capacity >= min_new_capacity) return MZ_TRUE; + if (growing) { new_capacity = MZ_MAX(1, pArray->m_capacity); while (new_capacity < min_new_capacity) new_capacity *= 2; } + if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity))) return MZ_FALSE; + pArray->m_p = pNew_p; pArray->m_capacity = new_capacity; + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing) +{ + if (new_capacity > pArray->m_capacity) { if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) return MZ_FALSE; } + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing) +{ + if (new_size > pArray->m_capacity) { if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) return MZ_FALSE; } + pArray->m_size = new_size; + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n) +{ + return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE); +} + +static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n) +{ + size_t orig_size = pArray->m_size; if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) return MZ_FALSE; + memcpy((mz_uint8*)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size); + return MZ_TRUE; +} + +#ifndef MINIZ_NO_TIME +static time_t mz_zip_dos_to_time_t(int dos_time, int dos_date) +{ + struct tm tm; + memset(&tm, 0, sizeof(tm)); tm.tm_isdst = -1; + tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900; tm.tm_mon = ((dos_date >> 5) & 15) - 1; tm.tm_mday = dos_date & 31; + tm.tm_hour = (dos_time >> 11) & 31; tm.tm_min = (dos_time >> 5) & 63; tm.tm_sec = (dos_time << 1) & 62; + return mktime(&tm); +} + +static void mz_zip_time_to_dos_time(time_t time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date) +{ +#ifdef _MSC_VER + struct tm tm_struct; + struct tm *tm = &tm_struct; + errno_t err = localtime_s(tm, &time); + if (err) + { + *pDOS_date = 0; *pDOS_time = 0; + return; + } +#else + struct tm *tm = localtime(&time); +#endif + *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1)); + *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday); +} +#endif + +#ifndef MINIZ_NO_STDIO +static mz_bool mz_zip_get_file_modified_time(const char *pFilename, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date) +{ +#ifdef MINIZ_NO_TIME + (void)pFilename; *pDOS_date = *pDOS_time = 0; +#else + struct MZ_FILE_STAT_STRUCT file_stat; + // On Linux with x86 glibc, this call will fail on large files (>= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. + if (MZ_FILE_STAT(pFilename, &file_stat) != 0) + return MZ_FALSE; + mz_zip_time_to_dos_time(file_stat.st_mtime, pDOS_time, pDOS_date); +#endif // #ifdef MINIZ_NO_TIME + return MZ_TRUE; +} + +#ifndef MINIZ_NO_TIME +static mz_bool mz_zip_set_file_times(const char *pFilename, time_t access_time, time_t modified_time) +{ + struct utimbuf t; t.actime = access_time; t.modtime = modified_time; + return !utime(pFilename, &t); +} +#endif // #ifndef MINIZ_NO_TIME +#endif // #ifndef MINIZ_NO_STDIO + +static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint32 flags) +{ + (void)flags; + if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) + return MZ_FALSE; + + if (!pZip->m_pAlloc) pZip->m_pAlloc = def_alloc_func; + if (!pZip->m_pFree) pZip->m_pFree = def_free_func; + if (!pZip->m_pRealloc) pZip->m_pRealloc = def_realloc_func; + + pZip->m_zip_mode = MZ_ZIP_MODE_READING; + pZip->m_archive_size = 0; + pZip->m_central_directory_file_ofs = 0; + pZip->m_total_files = 0; + + if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) + return MZ_FALSE; + memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); + return MZ_TRUE; +} + +static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index) +{ + const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE; + const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index)); + mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS); + mz_uint8 l = 0, r = 0; + pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + pE = pL + MZ_MIN(l_len, r_len); + while (pL < pE) + { + if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) + break; + pL++; pR++; + } + return (pL == pE) ? (l_len < r_len) : (l < r); +} + +#define MZ_SWAP_UINT32(a, b) do { mz_uint32 t = a; a = b; b = t; } MZ_MACRO_END + +// Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.) +static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip) +{ + mz_zip_internal_state *pState = pZip->m_pState; + const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; + const mz_zip_array *pCentral_dir = &pState->m_central_dir; + mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); + const int size = pZip->m_total_files; + int start = (size - 2) >> 1, end; + while (start >= 0) + { + int child, root = start; + for ( ; ; ) + { + if ((child = (root << 1) + 1) >= size) + break; + child += (((child + 1) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1]))); + if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) + break; + MZ_SWAP_UINT32(pIndices[root], pIndices[child]); root = child; + } + start--; + } + + end = size - 1; + while (end > 0) + { + int child, root = 0; + MZ_SWAP_UINT32(pIndices[end], pIndices[0]); + for ( ; ; ) + { + if ((child = (root << 1) + 1) >= end) + break; + child += (((child + 1) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1])); + if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) + break; + MZ_SWAP_UINT32(pIndices[root], pIndices[child]); root = child; + } + end--; + } +} + +static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint32 flags) +{ + mz_uint cdir_size, num_this_disk, cdir_disk_index; + mz_uint64 cdir_ofs; + mz_int64 cur_file_ofs; + const mz_uint8 *p; + mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; mz_uint8 *pBuf = (mz_uint8 *)buf_u32; + mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0); + // Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there. + if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return MZ_FALSE; + // Find the end of central directory record by scanning the file from the end towards the beginning. + cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0); + for ( ; ; ) + { + int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n) + return MZ_FALSE; + for (i = n - 4; i >= 0; --i) + if (MZ_READ_LE32(pBuf + i) == MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) + break; + if (i >= 0) + { + cur_file_ofs += i; + break; + } + if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (0xFFFF + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE))) + return MZ_FALSE; + cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); + } + // Read and verify the end of central directory record. + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) + return MZ_FALSE; + if ((MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) || + ((pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS)) != MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS))) + return MZ_FALSE; + + num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS); + cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS); + if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) + return MZ_FALSE; + + if ((cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS)) < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) + return MZ_FALSE; + + cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS); + if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size) + return MZ_FALSE; + + pZip->m_central_directory_file_ofs = cdir_ofs; + + if (pZip->m_total_files) + { + mz_uint i, n; + + // Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and another to hold the sorted indices. + if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || + (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE))) + return MZ_FALSE; + + if (sort_central_dir) + { + if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) + return MZ_FALSE; + } + + if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size) + return MZ_FALSE; + + // Now create an index into the central directory file records, do some basic sanity checking on each record, and check for zip64 entries (which are not yet supported). + p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p; + for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) + { + mz_uint total_header_size, comp_size, decomp_size, disk_index; + if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) + return MZ_FALSE; + MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p); + if (sort_central_dir) + MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i; + comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size) || (decomp_size == 0xFFFFFFFF) || (comp_size == 0xFFFFFFFF)) + return MZ_FALSE; + disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS); + if ((disk_index != num_this_disk) && (disk_index != 1)) + return MZ_FALSE; + if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size) + return MZ_FALSE; + if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n) + return MZ_FALSE; + n -= total_header_size; p += total_header_size; + } + } + + if (sort_central_dir) + mz_zip_reader_sort_central_dir_offsets_by_filename(pZip); + + return MZ_TRUE; +} + +mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags) +{ + if ((!pZip) || (!pZip->m_pRead)) + return MZ_FALSE; + if (!mz_zip_reader_init_internal(pZip, flags)) + return MZ_FALSE; + pZip->m_archive_size = size; + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end(pZip); + return MZ_FALSE; + } + return MZ_TRUE; +} + +static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n); + memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s); + return s; +} + +mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags) +{ + if (!mz_zip_reader_init_internal(pZip, flags)) + return MZ_FALSE; + pZip->m_archive_size = size; + pZip->m_pRead = mz_zip_mem_read_func; + pZip->m_pIO_opaque = pZip; +#ifdef __cplusplus + pZip->m_pState->m_pMem = const_cast<void *>(pMem); +#else + pZip->m_pState->m_pMem = (void *)pMem; +#endif + pZip->m_pState->m_mem_size = size; + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end(pZip); + return MZ_FALSE; + } + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); + if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) + return 0; + return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile); +} + +mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags) +{ + mz_uint64 file_size; + MZ_FILE *pFile = MZ_FOPEN(pFilename, "rb"); + if (!pFile) + return MZ_FALSE; + if (MZ_FSEEK64(pFile, 0, SEEK_END)) + { + MZ_FCLOSE(pFile); + return MZ_FALSE; + } + file_size = MZ_FTELL64(pFile); + if (!mz_zip_reader_init_internal(pZip, flags)) + { + MZ_FCLOSE(pFile); + return MZ_FALSE; + } + pZip->m_pRead = mz_zip_file_read_func; + pZip->m_pIO_opaque = pZip; + pZip->m_pState->m_pFile = pFile; + pZip->m_archive_size = file_size; + if (!mz_zip_reader_read_central_dir(pZip, flags)) + { + mz_zip_reader_end(pZip); + return MZ_FALSE; + } + return MZ_TRUE; +} +#endif // #ifndef MINIZ_NO_STDIO + +mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip) +{ + return pZip ? pZip->m_total_files : 0; +} + +static MZ_FORCEINLINE const mz_uint8 *mz_zip_reader_get_cdh(mz_zip_archive *pZip, mz_uint file_index) +{ + if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + return NULL; + return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); +} + +mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index) +{ + mz_uint m_bit_flag; + const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); + if (!p) + return MZ_FALSE; + m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + return (m_bit_flag & 1); +} + +mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index) +{ + mz_uint filename_len, external_attr; + const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); + if (!p) + return MZ_FALSE; + + // First see if the filename ends with a '/' character. + filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + if (filename_len) + { + if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/') + return MZ_TRUE; + } + + // Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. + // Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS directory flag and ignore the source OS ID in the created by field. + // FIXME: Remove this check? Is it necessary - we already check the filename. + external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); + if ((external_attr & 0x10) != 0) + return MZ_TRUE; + + return MZ_FALSE; +} + +mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat) +{ + mz_uint n; + const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); + if ((!p) || (!pStat)) + return MZ_FALSE; + + // Unpack the central directory record. + pStat->m_file_index = file_index; + pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index); + pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS); + pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS); + pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); + pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); +#ifndef MINIZ_NO_TIME + pStat->m_time = mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS)); +#endif + pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS); + pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS); + pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); + pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); + + // Copy as much of the filename and comment as possible. + n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1); + memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); pStat->m_filename[n] = '\0'; + + n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS); n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1); + pStat->m_comment_size = n; + memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), n); pStat->m_comment[n] = '\0'; + + return MZ_TRUE; +} + +mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size) +{ + mz_uint n; + const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); + if (!p) { if (filename_buf_size) pFilename[0] = '\0'; return 0; } + n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); + if (filename_buf_size) + { + n = MZ_MIN(n, filename_buf_size - 1); + memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); + pFilename[n] = '\0'; + } + return n + 1; +} + +static MZ_FORCEINLINE mz_bool mz_zip_reader_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags) +{ + mz_uint i; + if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) + return 0 == memcmp(pA, pB, len); + for (i = 0; i < len; ++i) + if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) + return MZ_FALSE; + return MZ_TRUE; +} + +static MZ_FORCEINLINE int mz_zip_reader_filename_compare(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, mz_uint r_len) +{ + const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE; + mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS); + mz_uint8 l = 0, r = 0; + pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + pE = pL + MZ_MIN(l_len, r_len); + while (pL < pE) + { + if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) + break; + pL++; pR++; + } + return (pL == pE) ? (int)(l_len - r_len) : (l - r); +} + +static int mz_zip_reader_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename) +{ + mz_zip_internal_state *pState = pZip->m_pState; + const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets; + const mz_zip_array *pCentral_dir = &pState->m_central_dir; + mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); + const int size = pZip->m_total_files; + const mz_uint filename_len = (mz_uint)strlen(pFilename); + int l = 0, h = size - 1; + while (l <= h) + { + int m = (l + h) >> 1, file_index = pIndices[m], comp = mz_zip_reader_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len); + if (!comp) + return file_index; + else if (comp < 0) + l = m + 1; + else + h = m - 1; + } + return -1; +} + +int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags) +{ + mz_uint file_index; size_t name_len, comment_len; + if ((!pZip) || (!pZip->m_pState) || (!pName) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + return -1; + if (((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) + return mz_zip_reader_locate_file_binary_search(pZip, pName); + name_len = strlen(pName); if (name_len > 0xFFFF) return -1; + comment_len = pComment ? strlen(pComment) : 0; if (comment_len > 0xFFFF) return -1; + for (file_index = 0; file_index < pZip->m_total_files; file_index++) + { + const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); + mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS); + const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; + if (filename_len < name_len) + continue; + if (comment_len) + { + mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS); + const char *pFile_comment = pFilename + filename_len + file_extra_len; + if ((file_comment_len != comment_len) || (!mz_zip_reader_string_equal(pComment, pFile_comment, file_comment_len, flags))) + continue; + } + if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) + { + int ofs = filename_len - 1; + do + { + if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) + break; + } while (--ofs >= 0); + ofs++; + pFilename += ofs; filename_len -= ofs; + } + if ((filename_len == name_len) && (mz_zip_reader_string_equal(pName, pFilename, filename_len, flags))) + return file_index; + } + return -1; +} + +mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +{ + int status = TINFL_STATUS_DONE; + mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; + mz_zip_archive_file_stat file_stat; + void *pRead_buf; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + tinfl_decompressor inflator; + + if ((buf_size) && (!pBuf)) + return MZ_FALSE; + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + // Empty file, or a directory (but not always a directory - I've seen odd zips with directories that have compressed data which inflates to 0 bytes) + if (!file_stat.m_comp_size) + return MZ_TRUE; + + // Entry is a subdirectory (I've seen old zips with dir entries which have compressed deflate data which inflates to 0 bytes, but these entries claim to uncompress to 512 bytes in the headers). + // I'm torn how to handle this case - should it fail instead? + if (mz_zip_reader_is_file_a_directory(pZip, file_index)) + return MZ_TRUE; + + // Encryption and patch files are not supported. + if (file_stat.m_bit_flag & (1 | 32)) + return MZ_FALSE; + + // This function only supports stored and deflate. + if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) + return MZ_FALSE; + + // Ensure supplied output buffer is large enough. + needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; + if (buf_size < needed_size) + return MZ_FALSE; + + // Read and parse the local directory entry. + cur_file_ofs = file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return MZ_FALSE; + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return MZ_FALSE; + + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) + return MZ_FALSE; + + if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) + { + // The file is stored or the caller has requested the compressed data. + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size) + return MZ_FALSE; + return ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) != 0) || (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) == file_stat.m_crc32); + } + + // Decompress the file either directly from memory or from a file input buffer. + tinfl_init(&inflator); + + if (pZip->m_pState->m_pMem) + { + // Read directly from the archive in memory. + pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; + read_buf_size = read_buf_avail = file_stat.m_comp_size; + comp_remaining = 0; + } + else if (pUser_read_buf) + { + // Use a user provided read buffer. + if (!user_read_buf_size) + return MZ_FALSE; + pRead_buf = (mz_uint8 *)pUser_read_buf; + read_buf_size = user_read_buf_size; + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + else + { + // Temporarily allocate a read buffer. + read_buf_size = MZ_MIN(file_stat.m_comp_size, MZ_ZIP_MAX_IO_BUF_SIZE); +#ifdef _MSC_VER + if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) +#else + if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) +#endif + return MZ_FALSE; + if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) + return MZ_FALSE; + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + + do + { + size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs); + if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + status = TINFL_STATUS_FAILED; + break; + } + cur_file_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + read_buf_ofs = 0; + } + in_buf_size = (size_t)read_buf_avail; + status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0)); + read_buf_avail -= in_buf_size; + read_buf_ofs += in_buf_size; + out_buf_ofs += out_buf_size; + } while (status == TINFL_STATUS_NEEDS_MORE_INPUT); + + if (status == TINFL_STATUS_DONE) + { + // Make sure the entire file was decompressed, and check its CRC. + if ((out_buf_ofs != file_stat.m_uncomp_size) || (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)) + status = TINFL_STATUS_FAILED; + } + + if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + + return status == TINFL_STATUS_DONE; +} + +mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +{ + int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); + if (file_index < 0) + return MZ_FALSE; + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size); +} + +mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags) +{ + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); +} + +mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags) +{ + return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0); +} + +void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags) +{ + mz_uint64 comp_size, uncomp_size, alloc_size; + const mz_uint8 *p = mz_zip_reader_get_cdh(pZip, file_index); + void *pBuf; + + if (pSize) + *pSize = 0; + if (!p) + return NULL; + + comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); + + alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; +#ifdef _MSC_VER + if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) +#else + if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) +#endif + return NULL; + if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) + return NULL; + + if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return NULL; + } + + if (pSize) *pSize = (size_t)alloc_size; + return pBuf; +} + +void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags) +{ + int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); + if (file_index < 0) + { + if (pSize) *pSize = 0; + return MZ_FALSE; + } + return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags); +} + +mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags) +{ + int status = TINFL_STATUS_DONE; mz_uint file_crc32 = MZ_CRC32_INIT; + mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs; + mz_zip_archive_file_stat file_stat; + void *pRead_buf = NULL; void *pWrite_buf = NULL; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + + // Empty file, or a directory (but not always a directory - I've seen odd zips with directories that have compressed data which inflates to 0 bytes) + if (!file_stat.m_comp_size) + return MZ_TRUE; + + // Entry is a subdirectory (I've seen old zips with dir entries which have compressed deflate data which inflates to 0 bytes, but these entries claim to uncompress to 512 bytes in the headers). + // I'm torn how to handle this case - should it fail instead? + if (mz_zip_reader_is_file_a_directory(pZip, file_index)) + return MZ_TRUE; + + // Encryption and patch files are not supported. + if (file_stat.m_bit_flag & (1 | 32)) + return MZ_FALSE; + + // This function only supports stored and deflate. + if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) + return MZ_FALSE; + + // Read and parse the local directory entry. + cur_file_ofs = file_stat.m_local_header_ofs; + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return MZ_FALSE; + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return MZ_FALSE; + + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) + return MZ_FALSE; + + // Decompress the file either directly from memory or from a file input buffer. + if (pZip->m_pState->m_pMem) + { + pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs; + read_buf_size = read_buf_avail = file_stat.m_comp_size; + comp_remaining = 0; + } + else + { + read_buf_size = MZ_MIN(file_stat.m_comp_size, MZ_ZIP_MAX_IO_BUF_SIZE); + if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) + return MZ_FALSE; + read_buf_avail = 0; + comp_remaining = file_stat.m_comp_size; + } + + if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) + { + // The file is stored or the caller has requested the compressed data. + if (pZip->m_pState->m_pMem) + { +#ifdef _MSC_VER + if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > 0xFFFFFFFF)) +#else + if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > 0xFFFFFFFF)) +#endif + return MZ_FALSE; + if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) + status = TINFL_STATUS_FAILED; + else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)file_stat.m_comp_size); + cur_file_ofs += file_stat.m_comp_size; + out_buf_ofs += file_stat.m_comp_size; + comp_remaining = 0; + } + else + { + while (comp_remaining) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + status = TINFL_STATUS_FAILED; + break; + } + + if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail); + + if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + status = TINFL_STATUS_FAILED; + break; + } + cur_file_ofs += read_buf_avail; + out_buf_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + } + } + } + else + { + tinfl_decompressor inflator; + tinfl_init(&inflator); + + if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) + status = TINFL_STATUS_FAILED; + else + { + do + { + mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); + if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) + { + read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); + if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) + { + status = TINFL_STATUS_FAILED; + break; + } + cur_file_ofs += read_buf_avail; + comp_remaining -= read_buf_avail; + read_buf_ofs = 0; + } + + in_buf_size = (size_t)read_buf_avail; + status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); + read_buf_avail -= in_buf_size; + read_buf_ofs += in_buf_size; + + if (out_buf_size) + { + if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) + { + status = TINFL_STATUS_FAILED; + break; + } + file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size); + if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) + { + status = TINFL_STATUS_FAILED; + break; + } + } + } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT)); + } + } + + if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) + { + // Make sure the entire file was decompressed, and check its CRC. + if ((out_buf_ofs != file_stat.m_uncomp_size) || (file_crc32 != file_stat.m_crc32)) + status = TINFL_STATUS_FAILED; + } + + if (!pZip->m_pState->m_pMem) + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + if (pWrite_buf) + pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf); + + return status == TINFL_STATUS_DONE; +} + +mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags) +{ + int file_index = mz_zip_reader_locate_file(pZip, pFilename, NULL, flags); + if (file_index < 0) + return MZ_FALSE; + return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags); +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n) +{ + (void)ofs; return MZ_FWRITE(pBuf, 1, n, (MZ_FILE*)pOpaque); +} + +mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags) +{ + mz_bool status; + mz_zip_archive_file_stat file_stat; + MZ_FILE *pFile; + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; + pFile = MZ_FOPEN(pDst_filename, "wb"); + if (!pFile) + return MZ_FALSE; + status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags); + if (MZ_FCLOSE(pFile) == EOF) + return MZ_FALSE; +#ifndef MINIZ_NO_TIME + if (status) + mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time); +#endif + return status; +} +#endif // #ifndef MINIZ_NO_STDIO + +mz_bool mz_zip_reader_end(mz_zip_archive *pZip) +{ + if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + return MZ_FALSE; + + if (pZip->m_pState) + { + mz_zip_internal_state *pState = pZip->m_pState; pZip->m_pState = NULL; + mz_zip_array_clear(pZip, &pState->m_central_dir); + mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); + mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); + +#ifndef MINIZ_NO_STDIO + if (pState->m_pFile) + { + MZ_FCLOSE(pState->m_pFile); + pState->m_pFile = NULL; + } +#endif // #ifndef MINIZ_NO_STDIO + + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + } + pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; + + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO +mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags) +{ + int file_index = mz_zip_reader_locate_file(pZip, pArchive_filename, NULL, flags); + if (file_index < 0) + return MZ_FALSE; + return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags); +} +#endif + +// ------------------- .ZIP archive writing + +#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +static void mz_write_le16(mz_uint8 *p, mz_uint16 v) { p[0] = (mz_uint8)v; p[1] = (mz_uint8)(v >> 8); } +static void mz_write_le32(mz_uint8 *p, mz_uint32 v) { p[0] = (mz_uint8)v; p[1] = (mz_uint8)(v >> 8); p[2] = (mz_uint8)(v >> 16); p[3] = (mz_uint8)(v >> 24); } +#define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v)) +#define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v)) + +mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size) +{ + if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) + return MZ_FALSE; + + if (pZip->m_file_offset_alignment) + { + // Ensure user specified file offset alignment is a power of 2. + if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1)) + return MZ_FALSE; + } + + if (!pZip->m_pAlloc) pZip->m_pAlloc = def_alloc_func; + if (!pZip->m_pFree) pZip->m_pFree = def_free_func; + if (!pZip->m_pRealloc) pZip->m_pRealloc = def_realloc_func; + + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; + pZip->m_archive_size = existing_size; + pZip->m_central_directory_file_ofs = 0; + pZip->m_total_files = 0; + + if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) + return MZ_FALSE; + memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); + MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); + return MZ_TRUE; +} + +static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_zip_internal_state *pState = pZip->m_pState; + mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size); +#ifdef _MSC_VER + if ((!n) || ((0, sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))) +#else + if ((!n) || ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))) +#endif + return 0; + if (new_size > pState->m_mem_capacity) + { + void *pNew_block; + size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity); while (new_capacity < new_size) new_capacity *= 2; + if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) + return 0; + pState->m_pMem = pNew_block; pState->m_mem_capacity = new_capacity; + } + memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n); + pState->m_mem_size = (size_t)new_size; + return n; +} + +mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size) +{ + pZip->m_pWrite = mz_zip_heap_write_func; + pZip->m_pIO_opaque = pZip; + if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning)) + return MZ_FALSE; + if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) + { + if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size))) + { + mz_zip_writer_end(pZip); + return MZ_FALSE; + } + pZip->m_pState->m_mem_capacity = initial_allocation_size; + } + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO +static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n) +{ + mz_zip_archive *pZip = (mz_zip_archive *)pOpaque; + mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); + if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) + return 0; + return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile); +} + +mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning) +{ + MZ_FILE *pFile; + pZip->m_pWrite = mz_zip_file_write_func; + pZip->m_pIO_opaque = pZip; + if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning)) + return MZ_FALSE; + if (NULL == (pFile = MZ_FOPEN(pFilename, "wb"))) + { + mz_zip_writer_end(pZip); + return MZ_FALSE; + } + pZip->m_pState->m_pFile = pFile; + if (size_to_reserve_at_beginning) + { + mz_uint64 cur_ofs = 0; char buf[4096]; MZ_CLEAR_ARR(buf); + do + { + size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) + { + mz_zip_writer_end(pZip); + return MZ_FALSE; + } + cur_ofs += n; size_to_reserve_at_beginning -= n; + } while (size_to_reserve_at_beginning); + } + return MZ_TRUE; +} +#endif // #ifndef MINIZ_NO_STDIO + +mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename) +{ + mz_zip_internal_state *pState; + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) + return MZ_FALSE; + // No sense in trying to write to an archive that's already at the support max size + if ((pZip->m_total_files == 0xFFFF) || ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)) + return MZ_FALSE; + + pState = pZip->m_pState; + + if (pState->m_pFile) + { +#ifdef MINIZ_NO_STDIO + pFilename; return MZ_FALSE; +#else + // Archive is being read from stdio - try to reopen as writable. + if (pZip->m_pIO_opaque != pZip) + return MZ_FALSE; + if (!pFilename) + return MZ_FALSE; + pZip->m_pWrite = mz_zip_file_write_func; + if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) + { + // The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. + mz_zip_reader_end(pZip); + return MZ_FALSE; + } +#endif // #ifdef MINIZ_NO_STDIO + } + else if (pState->m_pMem) + { + // Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. + if (pZip->m_pIO_opaque != pZip) + return MZ_FALSE; + pState->m_mem_capacity = pState->m_mem_size; + pZip->m_pWrite = mz_zip_heap_write_func; + } + // Archive is being read via a user provided read function - make sure the user has specified a write function too. + else if (!pZip->m_pWrite) + return MZ_FALSE; + + // Start writing new files at the archive's current central directory location. + pZip->m_archive_size = pZip->m_central_directory_file_ofs; + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; + pZip->m_central_directory_file_ofs = 0; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags) +{ + return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0); +} + +typedef struct +{ + mz_zip_archive *m_pZip; + mz_uint64 m_cur_archive_file_ofs; + mz_uint64 m_comp_size; +} mz_zip_writer_add_state; + +static mz_bool mz_zip_writer_add_put_buf_callback(const void* pBuf, int len, void *pUser) +{ + mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser; + if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len) + return MZ_FALSE; + pState->m_cur_archive_file_ofs += len; + pState->m_comp_size += len; + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_create_local_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date) +{ + (void)pZip; + memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, comp_size); + MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, uncomp_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size); + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, mz_uint32 ext_attributes) +{ + (void)pZip; + memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, comp_size); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, uncomp_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size); + MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes); + MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_header_ofs); + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size, const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, mz_uint32 ext_attributes) +{ + mz_zip_internal_state *pState = pZip->m_pState; + mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size; + size_t orig_central_dir_size = pState->m_central_dir.m_size; + mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; + + // No zip64 support yet + if ((local_header_ofs > 0xFFFFFFFF) || (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + comment_size) > 0xFFFFFFFF)) + return MZ_FALSE; + + if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, extra_size, comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes)) + return MZ_FALSE; + + if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || + (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, ¢ral_dir_ofs, 1))) + { + // Try to push the central directory array back into its original state. + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return MZ_FALSE; + } + + return MZ_TRUE; +} + +static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name) +{ + // Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a drive letter, and cannot use DOS-style backward slashes. + if (*pArchive_name == '/') + return MZ_FALSE; + while (*pArchive_name) + { + if ((*pArchive_name == '\\') || (*pArchive_name == ':')) + return MZ_FALSE; + pArchive_name++; + } + return MZ_TRUE; +} + +static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip) +{ + mz_uint32 n; + if (!pZip->m_file_offset_alignment) + return 0; + n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1)); + return (pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1); +} + +static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n) +{ + char buf[4096]; + memset(buf, 0, MZ_MIN(sizeof(buf), n)); + while (n) + { + mz_uint32 s = MZ_MIN(sizeof(buf), n); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s) + return MZ_FALSE; + cur_file_ofs += s; n -= s; + } + return MZ_TRUE; +} + +mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32) +{ + mz_uint16 method = 0, dos_time = 0, dos_date = 0; + mz_uint level, ext_attributes = 0, num_alignment_padding_bytes; + mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0; + size_t archive_name_size; + mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; + tdefl_compressor *pComp = NULL; + mz_bool store_data_uncompressed; + mz_zip_internal_state *pState; + + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + level = level_and_flags & 0xF; + store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (pZip->m_total_files == 0xFFFF) || (level > MZ_UBER_COMPRESSION)) + return MZ_FALSE; + + pState = pZip->m_pState; + + if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) + return MZ_FALSE; + // No zip64 support yet + if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) + return MZ_FALSE; + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + return MZ_FALSE; + +#ifndef MINIZ_NO_TIME + { + time_t cur_time; time(&cur_time); + mz_zip_time_to_dos_time(cur_time, &dos_time, &dos_date); + } +#endif // #ifndef MINIZ_NO_TIME + + archive_name_size = strlen(pArchive_name); + if (archive_name_size > 0xFFFF) + return MZ_FALSE; + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + // no zip64 support yet + if ((pZip->m_total_files == 0xFFFF) || ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + comment_size + archive_name_size) > 0xFFFFFFFF)) + return MZ_FALSE; + + if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) + { + // Set DOS Subdirectory attribute bit. + ext_attributes |= 0x10; + // Subdirectories cannot contain data. + if ((buf_size) || (uncomp_size)) + return MZ_FALSE; + } + + // Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A good idea if we're doing an in-place modification.) + if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size)) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) + return MZ_FALSE; + + if ((!store_data_uncompressed) && (buf_size)) + { + if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)))) + return MZ_FALSE; + } + + if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes + sizeof(local_dir_header))) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return MZ_FALSE; + } + local_dir_header_ofs += num_alignment_padding_bytes; + if (pZip->m_file_offset_alignment) { MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); } + cur_archive_file_ofs += num_alignment_padding_bytes + sizeof(local_dir_header); + + MZ_CLEAR_ARR(local_dir_header); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return MZ_FALSE; + } + cur_archive_file_ofs += archive_name_size; + + if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) + { + uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, buf_size); + uncomp_size = buf_size; + if (uncomp_size <= 3) + { + level = 0; + store_data_uncompressed = MZ_TRUE; + } + } + + if (store_data_uncompressed) + { + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return MZ_FALSE; + } + + cur_archive_file_ofs += buf_size; + comp_size = buf_size; + + if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) + method = MZ_DEFLATED; + } + else if (buf_size) + { + mz_zip_writer_add_state state; + + state.m_pZip = pZip; + state.m_cur_archive_file_ofs = cur_archive_file_ofs; + state.m_comp_size = 0; + + if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) || + (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + return MZ_FALSE; + } + + comp_size = state.m_comp_size; + cur_archive_file_ofs = state.m_cur_archive_file_ofs; + + method = MZ_DEFLATED; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + pComp = NULL; + + // no zip64 support yet + if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF)) + return MZ_FALSE; + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date)) + return MZ_FALSE; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return MZ_FALSE; + + if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes)) + return MZ_FALSE; + + pZip->m_total_files++; + pZip->m_archive_size = cur_archive_file_ofs; + + return MZ_TRUE; +} + +#ifndef MINIZ_NO_STDIO +mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags) +{ + mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; + mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; + mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; + size_t archive_name_size; + mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; + MZ_FILE *pSrc_file = NULL; + + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + level = level_and_flags & 0xF; + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) + return MZ_FALSE; + if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) + return MZ_FALSE; + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + return MZ_FALSE; + + archive_name_size = strlen(pArchive_name); + if (archive_name_size > 0xFFFF) + return MZ_FALSE; + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + // no zip64 support yet + if ((pZip->m_total_files == 0xFFFF) || ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + comment_size + archive_name_size) > 0xFFFFFFFF)) + return MZ_FALSE; + + if (!mz_zip_get_file_modified_time(pSrc_filename, &dos_time, &dos_date)) + return MZ_FALSE; + + pSrc_file = MZ_FOPEN(pSrc_filename, "rb"); + if (!pSrc_file) + return MZ_FALSE; + MZ_FSEEK64(pSrc_file, 0, SEEK_END); + uncomp_size = MZ_FTELL64(pSrc_file); + MZ_FSEEK64(pSrc_file, 0, SEEK_SET); + + if (uncomp_size > 0xFFFFFFFF) + { + // No zip64 support yet + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + if (uncomp_size <= 3) + level = 0; + + if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes + sizeof(local_dir_header))) + { + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + local_dir_header_ofs += num_alignment_padding_bytes; + if (pZip->m_file_offset_alignment) { MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); } + cur_archive_file_ofs += num_alignment_padding_bytes + sizeof(local_dir_header); + + MZ_CLEAR_ARR(local_dir_header); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + cur_archive_file_ofs += archive_name_size; + + if (uncomp_size) + { + mz_uint64 uncomp_remaining = uncomp_size; + void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE); + if (!pRead_buf) + { + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + + if (!level) + { + while (uncomp_remaining) + { + mz_uint n = (mz_uint)MZ_MIN(MZ_ZIP_MAX_IO_BUF_SIZE, uncomp_remaining); + if ((MZ_FREAD(pRead_buf, 1, n, pSrc_file) != n) || (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); + uncomp_remaining -= n; + cur_archive_file_ofs += n; + } + comp_size = uncomp_size; + } + else + { + mz_bool result = MZ_FALSE; + mz_zip_writer_add_state state; + tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)); + if (!pComp) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + + state.m_pZip = pZip; + state.m_cur_archive_file_ofs = cur_archive_file_ofs; + state.m_comp_size = 0; + + if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + + for ( ; ; ) + { + size_t in_buf_size = (mz_uint32)MZ_MIN(uncomp_remaining, MZ_ZIP_MAX_IO_BUF_SIZE); + tdefl_status status; + + if (MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size) + break; + + uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, in_buf_size); + uncomp_remaining -= in_buf_size; + + status = tdefl_compress_buffer(pComp, pRead_buf, in_buf_size, uncomp_remaining ? TDEFL_NO_FLUSH : TDEFL_FINISH); + if (status == TDEFL_STATUS_DONE) + { + result = MZ_TRUE; + break; + } + else if (status != TDEFL_STATUS_OKAY) + break; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); + + if (!result) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + MZ_FCLOSE(pSrc_file); + return MZ_FALSE; + } + + comp_size = state.m_comp_size; + cur_archive_file_ofs = state.m_cur_archive_file_ofs; + + method = MZ_DEFLATED; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + } + + MZ_FCLOSE(pSrc_file); pSrc_file = NULL; + + // no zip64 support yet + if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF)) + return MZ_FALSE; + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date)) + return MZ_FALSE; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return MZ_FALSE; + + if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes)) + return MZ_FALSE; + + pZip->m_total_files++; + pZip->m_archive_size = cur_archive_file_ofs; + + return MZ_TRUE; +} +#endif // #ifndef MINIZ_NO_STDIO + +mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index) +{ + mz_uint n, bit_flags, num_alignment_padding_bytes; + mz_uint64 comp_bytes_remaining, local_dir_header_ofs; + mz_uint64 cur_src_file_ofs, cur_dst_file_ofs; + mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32; + mz_uint8 central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; + size_t orig_central_dir_size; + mz_zip_internal_state *pState; + void *pBuf; const mz_uint8 *pSrc_central_header; + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) + return MZ_FALSE; + if (NULL == (pSrc_central_header = mz_zip_reader_get_cdh(pSource_zip, file_index))) + return MZ_FALSE; + pState = pZip->m_pState; + + num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); + + // no zip64 support yet + if ((pZip->m_total_files == 0xFFFF) || ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)) + return MZ_FALSE; + + cur_src_file_ofs = MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS); + cur_dst_file_ofs = pZip->m_archive_size; + + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return MZ_FALSE; + if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) + return MZ_FALSE; + cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; + + if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes)) + return MZ_FALSE; + cur_dst_file_ofs += num_alignment_padding_bytes; + local_dir_header_ofs = cur_dst_file_ofs; + if (pZip->m_file_offset_alignment) { MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); } + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + return MZ_FALSE; + cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; + + n = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); + comp_bytes_remaining = n + MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); + + if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(sizeof(mz_uint32) * 4, MZ_MIN(MZ_ZIP_MAX_IO_BUF_SIZE, comp_bytes_remaining))))) + return MZ_FALSE; + + while (comp_bytes_remaining) + { + n = (mz_uint)MZ_MIN(MZ_ZIP_MAX_IO_BUF_SIZE, comp_bytes_remaining); + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return MZ_FALSE; + } + cur_src_file_ofs += n; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return MZ_FALSE; + } + cur_dst_file_ofs += n; + + comp_bytes_remaining -= n; + } + + bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); + if (bit_flags & 8) + { + // Copy data descriptor + if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return MZ_FALSE; + } + + n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == 0x08074b50) ? 4 : 3); + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + return MZ_FALSE; + } + + cur_src_file_ofs += n; + cur_dst_file_ofs += n; + } + pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); + + // no zip64 support yet + if (cur_dst_file_ofs > 0xFFFFFFFF) + return MZ_FALSE; + + orig_central_dir_size = pState->m_central_dir.m_size; + + memcpy(central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); + MZ_WRITE_LE32(central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs); + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) + return MZ_FALSE; + + n = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS); + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n)) + { + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return MZ_FALSE; + } + + if (pState->m_central_dir.m_size > 0xFFFFFFFF) + return MZ_FALSE; + n = (mz_uint32)orig_central_dir_size; + if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) + { + mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); + return MZ_FALSE; + } + + pZip->m_total_files++; + pZip->m_archive_size = cur_dst_file_ofs; + + return MZ_TRUE; +} + +mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) +{ + mz_zip_internal_state *pState; + mz_uint64 central_dir_ofs, central_dir_size; + mz_uint8 hdr[MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE]; + + if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) + return MZ_FALSE; + + pState = pZip->m_pState; + + // no zip64 support yet + if ((pZip->m_total_files > 0xFFFF) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)) + return MZ_FALSE; + + central_dir_ofs = 0; + central_dir_size = 0; + if (pZip->m_total_files) + { + // Write central directory + central_dir_ofs = pZip->m_archive_size; + central_dir_size = pState->m_central_dir.m_size; + pZip->m_central_directory_file_ofs = central_dir_ofs; + if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size) + return MZ_FALSE; + pZip->m_archive_size += central_dir_size; + } + + // Write end of central directory record + MZ_CLEAR_ARR(hdr); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); + MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files); + MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, central_dir_size); + MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, central_dir_ofs); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, sizeof(hdr)) != sizeof(hdr)) + return MZ_FALSE; +#ifndef MINIZ_NO_STDIO + if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF)) + return MZ_FALSE; +#endif // #ifndef MINIZ_NO_STDIO + + pZip->m_archive_size += sizeof(hdr); + + pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED; + return MZ_TRUE; +} + +mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize) +{ + if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pSize)) + return MZ_FALSE; + if (pZip->m_pWrite != mz_zip_heap_write_func) + return MZ_FALSE; + if (!mz_zip_writer_finalize_archive(pZip)) + return MZ_FALSE; + + *pBuf = pZip->m_pState->m_pMem; + *pSize = pZip->m_pState->m_mem_size; + pZip->m_pState->m_pMem = NULL; + pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0; + return MZ_TRUE; +} + +mz_bool mz_zip_writer_end(mz_zip_archive *pZip) +{ + mz_zip_internal_state *pState; + mz_bool status = MZ_TRUE; + if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) + return MZ_FALSE; + + pState = pZip->m_pState; + pZip->m_pState = NULL; + mz_zip_array_clear(pZip, &pState->m_central_dir); + mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); + mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); + +#ifndef MINIZ_NO_STDIO + if (pState->m_pFile) + { + MZ_FCLOSE(pState->m_pFile); + pState->m_pFile = NULL; + } +#endif // #ifndef MINIZ_NO_STDIO + + if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem); + pState->m_pMem = NULL; + } + + pZip->m_pFree(pZip->m_pAlloc_opaque, pState); + pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; + return status; +} + +#ifndef MINIZ_NO_STDIO +mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags) +{ + mz_bool status, created_new_archive = MZ_FALSE; + mz_zip_archive zip_archive; + struct MZ_FILE_STAT_STRUCT file_stat; + MZ_CLEAR_OBJ(zip_archive); + if ((int)level_and_flags < 0) + level_and_flags = MZ_DEFAULT_LEVEL; + if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) + return MZ_FALSE; + if (!mz_zip_writer_validate_archive_name(pArchive_name)) + return MZ_FALSE; + if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) + { + // Create a new archive. + if (!mz_zip_writer_init_file(&zip_archive, pZip_filename, 0)) + return MZ_FALSE; + created_new_archive = MZ_TRUE; + } + else + { + // Append to an existing archive. + if (!mz_zip_reader_init_file(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) + return MZ_FALSE; + if (!mz_zip_writer_init_from_reader(&zip_archive, pZip_filename)) + { + mz_zip_reader_end(&zip_archive); + return MZ_FALSE; + } + } + status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0); + // Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always succeed, but we can try.) + if (!mz_zip_writer_finalize_archive(&zip_archive)) + status = MZ_FALSE; + if (!mz_zip_writer_end(&zip_archive)) + status = MZ_FALSE; + if ((!status) && (created_new_archive)) + { + // It's a new archive and something went wrong, so just delete it. + int ignoredStatus = MZ_DELETE_FILE(pZip_filename); + (void)ignoredStatus; + } + return status; +} + +void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags) +{ + int file_index; + mz_zip_archive zip_archive; + void *p = NULL; + + if (pSize) + *pSize = 0; + + if ((!pZip_filename) || (!pArchive_name)) + return NULL; + + MZ_CLEAR_OBJ(zip_archive); + if (!mz_zip_reader_init_file(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) + return NULL; + + if ((file_index = mz_zip_reader_locate_file(&zip_archive, pArchive_name, NULL, flags)) >= 0) + p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags); + + mz_zip_reader_end(&zip_archive); + return p; +} + +#endif // #ifndef MINIZ_NO_STDIO + +#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS + +#endif // #ifndef MINIZ_NO_ARCHIVE_APIS + +#ifdef __cplusplus +} +#endif + +#ifdef _MSC_VER +#pragma warning (pop) +#endif + +#endif // MINIZ_HEADER_FILE_ONLY + +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.cpp new file mode 100644 index 00000000..33fcd9d3 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.cpp @@ -0,0 +1,255 @@ +// From RecompTrophy by lamarking (https://github.com/cptlamarca/RecompTrophy), +// GPL-3.0, used with permission (commit f78630c). Changed for the port: in the +// recomptrophy namespace; stb_image static (the port has its own copy built +// elsewhere); miniz from third_party/. +// +// See trophyset.h. + +#include "trophyset.h" + +#include <algorithm> +#include <cmath> +#include <cstdio> +#include <cstring> +#include <sstream> + +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_STATIC +#define STBI_ONLY_PNG +#define STBI_NO_STDIO +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" // (static: the parts not used) +#include "stb_image.h" +#pragma clang diagnostic pop + +#define MINIZ_NO_STDIO +#define MINIZ_NO_ARCHIVE_APIS +#define MINIZ_NO_TIME +#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES +#define MINIZ_LITTLE_ENDIAN 1 +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 +#define MINIZ_SDL_NOUNUSED +// (SDL3's copy of miniz names SDL's allocator) +#include <cstdlib> +#define SDL_malloc malloc +#define SDL_free free +#define SDL_realloc realloc +#include "third_party/miniz.h" + +namespace recomptrophy::trophyset { +namespace { + +using Bytes = std::vector<uint8_t>; + +void Sha1(const uint8_t* data, size_t size, uint8_t out[20]) { + uint32_t h[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; + auto rol = [](uint32_t x, int n) { return (x << n) | (x >> (32 - n)); }; + auto block = [&](const uint8_t* p) { + uint32_t w[80]; + for (int i = 0; i < 16; ++i) { + w[i] = uint32_t(p[i * 4]) << 24 | uint32_t(p[i * 4 + 1]) << 16 | uint32_t(p[i * 4 + 2]) << 8 | p[i * 4 + 3]; + } + for (int i = 16; i < 80; ++i) w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); + uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4]; + for (int i = 0; i < 80; ++i) { + uint32_t f, k; + if (i < 20) f = (b & c) | (~b & d), k = 0x5A827999; + else if (i < 40) f = b ^ c ^ d, k = 0x6ED9EBA1; + else if (i < 60) f = (b & c) | (b & d) | (c & d), k = 0x8F1BBCDC; + else f = b ^ c ^ d, k = 0xCA62C1D6; + const uint32_t t = rol(a, 5) + f + e + k + w[i]; + e = d, d = c, c = rol(b, 30), b = a, a = t; + } + h[0] += a, h[1] += b, h[2] += c, h[3] += d, h[4] += e; + }; + size_t at = 0; + for (; at + 64 <= size; at += 64) block(data + at); + uint8_t tail[128] = {}; + const size_t rest = size - at; + std::memcpy(tail, data + at, rest); + tail[rest] = 0x80; + const size_t tail_size = rest + 9 <= 64 ? 64 : 128; + const uint64_t bits = uint64_t(size) * 8; + for (int i = 0; i < 8; ++i) tail[tail_size - 1 - i] = uint8_t(bits >> (i * 8)); + for (size_t o = 0; o < tail_size; o += 64) block(tail + o); + for (int i = 0; i < 5; ++i) { + out[i * 4] = uint8_t(h[i] >> 24), out[i * 4 + 1] = uint8_t(h[i] >> 16); + out[i * 4 + 2] = uint8_t(h[i] >> 8), out[i * 4 + 3] = uint8_t(h[i]); + } +} + +// Catmull-Rom scaling of RGBA pixels. +Bytes Scale(const uint8_t* src, int w, int h, int size) { + auto weight = [](float x) { + x = std::fabs(x); + if (x < 1) return 1.5f * x * x * x - 2.5f * x * x + 1; + if (x < 2) return -0.5f * x * x * x + 2.5f * x * x - 4 * x + 2; + return 0.0f; + }; + Bytes dst(size_t(size) * size * 4); + for (int y = 0; y < size; ++y) { + const float fy = (y + 0.5f) * h / size - 0.5f; + const int iy = int(std::floor(fy)); + for (int x = 0; x < size; ++x) { + const float fx = (x + 0.5f) * w / size - 0.5f; + const int ix = int(std::floor(fx)); + float sum[4] = {}, total = 0; + for (int j = -1; j <= 2; ++j) { + const float wy = weight(fy - (iy + j)); + const int sy = std::clamp(iy + j, 0, h - 1); + for (int i = -1; i <= 2; ++i) { + const float wgt = wy * weight(fx - (ix + i)); + const uint8_t* p = &src[(size_t(sy) * w + std::clamp(ix + i, 0, w - 1)) * 4]; + for (int c = 0; c < 4; ++c) sum[c] += p[c] * wgt; + total += wgt; + } + } + for (int c = 0; c < 4; ++c) { + dst[(size_t(y) * size + x) * 4 + c] = uint8_t(std::clamp(sum[c] / total + 0.5f, 0.0f, 255.0f)); + } + } + } + return dst; +} + +// A PNG scaled to size x size (opaque, as the console's trophy pictures). +Bytes Picture(const Bytes& png, int size) { + if (png.empty()) return {}; + int w = 0, h = 0, n = 0; + uint8_t* rgba = stbi_load_from_memory(png.data(), int(png.size()), &w, &h, &n, 4); + if (!rgba) return {}; + Bytes scaled = (w == size && h == size) ? Bytes(rgba, rgba + size_t(w) * h * 4) : Scale(rgba, w, h, size); + stbi_image_free(rgba); + for (size_t i = 3; i < scaled.size(); i += 4) scaled[i] = 255; + size_t length = 0; + void* out = tdefl_write_image_to_png_file_in_memory_ex(scaled.data(), size, size, 4, size * 4, &length, 6, MZ_FALSE, + nullptr, 0, nullptr, 0); + if (!out) return {}; + Bytes result(static_cast<uint8_t*>(out), static_cast<uint8_t*>(out) + length); + mz_free(out); + return result; +} + +std::string Json(const std::string& text) { + std::string out; + for (unsigned char c : text) { + if (c == '"' || c == '\\') out += '\\', out += char(c); + else if (c < 0x20) out += ' '; + else out += char(c); + } + return out; +} + +void Put32(uint8_t* p, uint32_t v) { p[0] = uint8_t(v >> 24), p[1] = uint8_t(v >> 16), p[2] = uint8_t(v >> 8), p[3] = uint8_t(v); } +void Put64(uint8_t* p, uint64_t v) { Put32(p, uint32_t(v >> 32)), Put32(p + 4, uint32_t(v)); } + +bool WriteFile(const std::string& path, const Bytes& data) { + const std::string tmp = path + ".tmp"; + FILE* f = std::fopen(tmp.c_str(), "wb"); + if (!f) return false; + bool ok = std::fwrite(data.data(), 1, data.size(), f) == data.size(); + ok = std::fclose(f) == 0 && ok; + return ok && std::rename(tmp.c_str(), path.c_str()) == 0; +} + +} // namespace + +std::string SetId(uint32_t title_id) { + if (title_id == 0x454109AB) return "NPWR99990_00"; + char id[16]; + std::snprintf(id, sizeof(id), "NPWR%05u_00", 90000u + title_id % 9989u); + return id; +} + +uint32_t TrophyId(const game::Info& info, uint32_t achievement_id) { + uint32_t rank = 0; + bool found = false; + for (const auto& a : info.achievements) { + if (a.id < achievement_id) ++rank; + found = found || a.id == achievement_id; + } + return found ? rank + 1 : 0; +} + +bool Build(const game::Info& info, const std::string& set_id, const std::string& title_name, const Bytes& icon_png, + const std::string& ucp_path, const std::string& list_path, float* progress) { + struct File { + std::string name; + Bytes data; + }; + std::vector<File> files; + Bytes icon = Picture(icon_png, 512); + if (icon.empty()) icon = Picture(info.icon, 512); + files.push_back({"icon0_en-US.png", icon}); + files.push_back({"trop0000.png", icon}); + + auto grade_of = [](uint32_t gamerscore) { return gamerscore <= 10 ? 4 : gamerscore <= 25 ? 3 : 2; }; + static const char kGradeLetters[] = "?PGSB"; + std::ostringstream conf, list, meta[2]; + conf << "{\"schemaVersion\":\"1.00\",\"trophyDefinitionRevision\":1,\"platform\":[\"PS5\"]," + << "\"trophyNpCommId\":\"" << set_id << "\",\"udsNpCommId\":\"" << set_id << "\"," + << "\"trophySetVersion\":\"01.00\",\"defaultLanguage\":\"en-US\",\"languages\":[\"en-US\",\"pt-BR\"]," + << "\"trophies\":[{\"id\":\"0000\",\"hidden\":false,\"grade\":\"P\",\"hasReward\":false}"; + list << "0 1 0 -1\n"; + for (int l = 0; l < 2; ++l) { + meta[l] << "{\"schemaVersion\":\"0.90\",\"trophyDefinitionRevision\":1,\"trophySetVersion\":\"01.00\"," + << "\"trophyNpCommId\":\"" << set_id << "\",\"metadata\":{\"titleMetadata\":{\"name\":\"" + << Json(title_name) << "\"},\"trophyMetadata\":[{\"id\":\"0000\",\"name\":\"" + << Json(title_name) << "\",\"detail\":\"" + << (l ? "Desbloqueie todos os troféus" : "Unlock all trophies") << "\"}"; + } + size_t done = 0; + for (const auto& a : info.achievements) { + const uint32_t trophy = TrophyId(info, a.id); + char tid[8]; + std::snprintf(tid, sizeof(tid), "%04u", trophy); + const int grade = grade_of(a.gamerscore); + Bytes picture = Picture(a.picture, 512); + files.push_back({std::string("trop") + tid + ".png", picture.empty() ? icon : picture}); + conf << ",{\"id\":\"" << tid << "\",\"hidden\":" << (a.hidden ? "true" : "false") << ",\"grade\":\"" + << kGradeLetters[grade] << "\",\"hasReward\":false,\"platinumTrophyId\":\"0000\"," + << "\"unlockCondition\":{\"udsStatId\":\"" << trophy + << "\",\"comparator\":\"ge\",\"targetValue\":\"1\",\"progressive\":false," + << "\"locations\":[{\"unlockType\":\"client\",\"platform\":[\"PS5\"]}]}}"; + list << trophy << ' ' << grade << ' ' << (a.hidden ? 1 : 0) << " 0\n"; + for (int l = 0; l < 2; ++l) { + meta[l] << ",{\"id\":\"" << tid << "\",\"name\":\"" << Json(a.name[l]) << "\",\"detail\":\"" + << Json(a.detail[l]) << "\"}"; + } + if (progress) *progress = float(++done) / float(info.achievements.size()) * 0.95f; + } + conf << "]}"; + for (auto& m : meta) m << "]}}"; + auto text = [](const std::string& s) { return Bytes(s.begin(), s.end()); }; + files.push_back({"tropconf.json", text(conf.str())}); + files.push_back({"tropmeta_en-US.json", text(meta[0].str())}); + files.push_back({"tropmeta_pt-BR.json", text(meta[1].str())}); + + const size_t table_end = 0x60 + files.size() * 0x40; + size_t pos = (table_end + 0xF) & ~size_t(0xF); + std::vector<size_t> offsets; + for (const auto& f : files) { + offsets.push_back(pos); + pos = (pos + f.data.size() + 0x10) & ~size_t(0xF); + } + Bytes ucp(pos); + for (size_t i = 0; i < files.size(); ++i) { + uint8_t* e = &ucp[0x60 + i * 0x40]; + std::memcpy(e, files[i].name.data(), std::min<size_t>(files[i].name.size(), 31)); + Put64(e + 32, offsets[i]); + Put64(e + 40, files[i].data.size()); + if (!files[i].data.empty()) std::memcpy(&ucp[offsets[i]], files[i].data.data(), files[i].data.size()); + } + Put32(&ucp[0], 0xB228C60A); + Put32(&ucp[4], 1); + Put64(&ucp[8], ucp.size()); + Put32(&ucp[0x10], uint32_t(files.size())); + Put32(&ucp[0x14], 0x40); + Sha1(ucp.data(), ucp.size(), &ucp[0x1C]); + const std::string l = list.str(); + const bool ok = WriteFile(ucp_path, ucp) && WriteFile(list_path, Bytes(l.begin(), l.end())); + if (progress) *progress = 1.0f; + return ok; +} + +} // namespace recomptrophy::trophyset diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.h b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.h new file mode 100644 index 00000000..86042473 --- /dev/null +++ b/glue/rexglue-sdk-main/gta4-recomp/src/ps5/trophies_native/trophyset.h @@ -0,0 +1,45 @@ +// From RecompTrophy by lamarking (https://github.com/cptlamarca/RecompTrophy), +// GPL-3.0, used with permission (commit f78630c). Changed for the port: in the +// recomptrophy namespace. +// +// A game's PS5 trophy set (TROPHY.UCP) from its achievements: trophies 1..N +// are its achievements in the order of their ids (TrophyId; the console's +// trophy list wants them numbered without gaps, and some games' achievements +// have gaps: Ghost Recon Future Soldier's are 13..65), plus a platinum (0) +// for all of them; the grade by gamerscore +// (<=10 bronze, <=25 silver, else gold); pictures scaled to 512x512 (bicubic). +// +// The container (no key): a 0x60 byte header (magic B228C60A, version 1, size, +// count, entry size 0x40, the SHA-1 of the file with it zeroed at 0x1C), a +// table of 64 byte entries (32 byte name, offset, size) and the files, each +// padded to 16 bytes: icon0_en-US.png, trop%04d.png, tropconf.json, +// tropmeta_<lang>.json. The same as the ports make (aotdc-ps5's +// src/ps5_trophy_set.cpp), so a port and this app make the same set. +// +// Also trophies.txt ("ID GRADE HIDDEN PLATINUM_ID", grade 1 platinum .. 4 +// bronze): the list the relay makes the player's progress file from. + +#pragma once + +#include <cstdint> +#include <string> +#include <vector> + +#include "game.h" + +namespace recomptrophy::trophyset { + +// The trophy set id of an Xbox 360 title: a made-up NPWR9xxxx_00 from its +// title id (the ports use the same: Army of Two: The Devil's Cartel's, +// published before the rule, keeps NPWR99990_00). +std::string SetId(uint32_t title_id); + +// The trophy of an achievement (0 when it isn't one of the game's). +uint32_t TrophyId(const game::Info& info, uint32_t achievement_id); + +// icon_png: the set's picture (a port's icon), else the game's from its XDBF. +bool Build(const game::Info& info, const std::string& set_id, const std::string& title_name, + const std::vector<uint8_t>& icon_png, const std::string& ucp_path, const std::string& list_path, + float* progress); + +} // namespace recomptrophy::trophyset diff --git a/glue/rexglue-sdk-main/gta4-recomp/src/rpf_button_prompts.cpp b/glue/rexglue-sdk-main/gta4-recomp/src/rpf_button_prompts.cpp index 56e95edb..377e5048 100644 --- a/glue/rexglue-sdk-main/gta4-recomp/src/rpf_button_prompts.cpp +++ b/glue/rexglue-sdk-main/gta4-recomp/src/rpf_button_prompts.cpp @@ -403,6 +403,11 @@ std::string Lowercase(std::string_view value) { } std::string SelectVariant() { +#if REX_PLATFORM_PS5 + // [ps5 port] Always the DualSense's (SDL has no PS5 pads to look at). + REXLOG_INFO("Button prompts: PS5 -> ps5"); + return "ps5"; +#endif SDL_UpdateGamepads(); int count = 0; SDL_JoystickID* gamepads = SDL_GetGamepads(&count); diff --git a/glue/rexglue-sdk-main/include/rex/audio/ps5/ps5_audio_driver.h b/glue/rexglue-sdk-main/include/rex/audio/ps5/ps5_audio_driver.h new file mode 100644 index 00000000..e9e38eb5 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/audio/ps5/ps5_audio_driver.h @@ -0,0 +1,52 @@ +#pragma once +/** + * [ps5 port] Audio out through sceAudioOut: the Xbox 360's 48 kHz frames of + * 256 samples x 6 channels, folded to stereo 16-bit (the port format the + * UnleashedRecomp PS5 port verified), pushed by a thread of its own. + */ +#include <atomic> +#include <condition_variable> +#include <mutex> +#include <queue> +#include <stack> +#include <thread> + +#include <rex/audio/audio_driver.h> +#include <rex/thread.h> + +namespace rex::audio::ps5 { + +class Ps5AudioDriver : public AudioDriver { + public: + Ps5AudioDriver(memory::Memory* memory, rex::thread::Semaphore* semaphore); + ~Ps5AudioDriver() override; + + bool Initialize(); + void SubmitFrame(uint32_t frame_ptr) override; + // How far ahead of the speakers the game may be (ps5_audio_frames): each + // frame queued is 5.3 ms later that a sound is heard. + uint32_t RecommendedInitialCredits(uint32_t configured_maximum) const override; + void Pause() override { paused_.store(true, std::memory_order_relaxed); } + void Resume() override { paused_.store(false, std::memory_order_relaxed); } + void Shutdown(); + + private: + void OutputThread(); + + static constexpr uint32_t kFrequency = 48000; + static constexpr uint32_t kChannels = 6; + static constexpr uint32_t kChannelSamples = 256; + static constexpr uint32_t kFrameSamples = kChannels * kChannelSamples; + + rex::thread::Semaphore* semaphore_ = nullptr; + int32_t port_ = -1; + std::thread thread_; + std::atomic<bool> running_{false}; + std::atomic<bool> paused_{false}; + + std::queue<float*> frames_queued_; + std::stack<float*> frames_unused_; + std::mutex frames_mutex_; +}; + +} // namespace rex::audio::ps5 diff --git a/glue/rexglue-sdk-main/include/rex/chrono/chrono.h b/glue/rexglue-sdk-main/include/rex/chrono/chrono.h index 6f4b3f0a..0744c006 100644 --- a/glue/rexglue-sdk-main/include/rex/chrono/chrono.h +++ b/glue/rexglue-sdk-main/include/rex/chrono/chrono.h @@ -17,6 +17,21 @@ #include <rex/chrono/clock.h> +// [ps5 port] The PS5's libc++ 18 has no clock_cast / clock_time_conversion. +#if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L +namespace std::chrono { + +template <typename DestClock, typename SourceClock> +struct clock_time_conversion; + +template <typename DestClock, typename SourceClock, typename Duration> +auto clock_cast(const std::chrono::time_point<SourceClock, Duration>& t) { + return clock_time_conversion<DestClock, SourceClock>{}(t); +} + +} // namespace std::chrono +#endif + namespace rex { using hundrednano = std::ratio<1, 10000000>; diff --git a/glue/rexglue-sdk-main/include/rex/filesystem/devices/disc_image_device.h b/glue/rexglue-sdk-main/include/rex/filesystem/devices/disc_image_device.h index f97559e9..5c773086 100644 --- a/glue/rexglue-sdk-main/include/rex/filesystem/devices/disc_image_device.h +++ b/glue/rexglue-sdk-main/include/rex/filesystem/devices/disc_image_device.h @@ -20,6 +20,9 @@ namespace rex::filesystem { +namespace direct { +class Reader; +} class DiscImageEntry; class DiscImageDevice : public Device { @@ -43,12 +46,16 @@ class DiscImageDevice : public Device { uint64_t total_file_size() const { return total_file_size_; } const DiscInfo& disc_info() const { return disc_info_; } + // Copies `length` bytes at `offset` in the image (false past its end or on a + // read error). + bool ReadAt(size_t offset, void* out, size_t length) const; + const std::string& name() const override { return name_; } uint32_t attributes() const override { return 0; } uint32_t component_name_max_length() const override { return 255; } uint32_t total_allocation_units() const override { - return uint32_t(mmap_->size() / sectors_per_allocation_unit() / bytes_per_sector()); + return uint32_t(host_size_ / sectors_per_allocation_unit() / bytes_per_sector()); } uint32_t available_allocation_units() const override { return 0; } uint32_t sectors_per_allocation_unit() const override { return 1; } @@ -67,6 +74,8 @@ class DiscImageDevice : public Device { std::filesystem::path host_path_; std::unique_ptr<Entry> root_entry_; std::unique_ptr<memory::MappedMemory> mmap_; + std::unique_ptr<direct::Reader> reader_; // [ps5 port] the image, read past the cache (never mapped) + size_t host_size_ = 0; DiscInfo disc_info_{}; uint64_t file_count_ = 0; uint64_t total_file_size_ = 0; diff --git a/glue/rexglue-sdk-main/include/rex/filesystem/direct_io.h b/glue/rexglue-sdk-main/include/rex/filesystem/direct_io.h new file mode 100644 index 00000000..87e2fd2e --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/filesystem/direct_io.h @@ -0,0 +1,182 @@ +/** + * [ps5 port] Reading past the system's file cache (O_DIRECT). + * + * On the PS5 everything read or written through the file cache stays in + * memory, and while an app runs the console has little to spare: after a + * couple of GB the cache is full and every read and write waits on the + * console freeing memory, which it does slowly (an install fell from 85 MB/s + * to a few). Games read past the cache, and so do these. + * + * A direct read is widened to 64 KiB-aligned bounds into an aligned buffer + * (kept for the next read) and copied out. Where the kernel refuses a direct + * transfer, the file goes on through the cache. + */ +#pragma once + +#if !defined(_WIN32) + +#include <algorithm> +#include <atomic> +#include <cerrno> +#include <cstdint> +#include <cstdlib> +#include <cstring> +#include <mutex> +#include <vector> + +#include <fcntl.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <rex/platform.h> + +namespace rex::filesystem::direct { + +constexpr size_t kAlign = size_t(64) << 10; + +// Past the cache only on the PS5 (elsewhere the cache is the fast path). +#if REX_PLATFORM_PS5 +constexpr bool kEnabled = true; +#else +constexpr bool kEnabled = false; +#endif + +inline uint8_t* AlignedAlloc(size_t bytes) { + void* p = nullptr; + return posix_memalign(&p, kAlign, std::max<size_t>(bytes, kAlign)) == 0 ? static_cast<uint8_t*>(p) + : nullptr; +} + +// From here on, `fd` goes through the cache. +inline void TurnOff(int fd) { + const int flags = fcntl(fd, F_GETFL); + if (flags != -1) fcntl(fd, F_SETFL, flags & ~O_DIRECT); +} + +inline uint64_t AlignDown(uint64_t v) { return v & ~uint64_t(kAlign - 1); } +inline uint64_t AlignUp(uint64_t v) { return (v + kAlign - 1) & ~uint64_t(kAlign - 1); } + +// A file read at any offset, from several threads at once. +class Reader { + public: + Reader() = default; + Reader(const Reader&) = delete; + Reader& operator=(const Reader&) = delete; + ~Reader() { + Close(); + for (auto& buffer : free_) std::free(buffer.data); + } + + bool Open(const char* path) { + Close(); + if (kEnabled) { + fd_ = ::open(path, O_RDONLY | O_DIRECT); + direct_ = fd_ >= 0; + } + if (fd_ < 0) fd_ = ::open(path, O_RDONLY); + if (fd_ < 0) return false; + struct stat st {}; + if (::fstat(fd_, &st) != 0) { + Close(); + return false; + } + size_ = uint64_t(st.st_size); + return true; + } + + void Close() { + if (fd_ >= 0) ::close(fd_); + fd_ = -1; + } + + int fd() const { return fd_; } + uint64_t size() const { return size_; } + bool direct() const { return direct_.load(std::memory_order_relaxed); } + + // Exactly `length` bytes at `offset` (false past the end or on an error). + bool ReadAt(uint64_t offset, void* out, size_t length) { + if (length == 0) return true; + if (offset > size_ || length > size_ - offset) return false; + if (direct_.load(std::memory_order_relaxed)) { + const uint64_t start = AlignDown(offset), end = AlignUp(offset + length); + const size_t window = size_t(end - start), need = size_t(offset + length - start); + Buffer buffer = Take(window); + if (buffer.data) { + size_t got = 0; + bool refused = false; + while (got < need) { + const ssize_t n = ::pread(fd_, buffer.data + got, window - got, off_t(start + got)); + if (n < 0 && errno == EINTR) continue; + if (n < 0 && errno == EINVAL) { + refused = true; + break; + } + if (n <= 0) break; + got += size_t(n); + } + const bool ok = got >= need; + if (ok) std::memcpy(out, buffer.data + (offset - start), length); + Give(buffer); + if (ok) return true; + if (!refused) return false; + // The kernel won't read this file directly: through the cache from now on. + if (direct_.exchange(false)) TurnOff(fd_); + } + } + auto* p = static_cast<uint8_t*>(out); + while (length) { + const ssize_t n = ::pread(fd_, p, length, off_t(offset)); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) return false; + p += n, offset += uint64_t(n), length -= size_t(n); + } + return true; + } + + private: + struct Buffer { + uint8_t* data = nullptr; + size_t size = 0; + }; + + Buffer Take(size_t bytes) { + { + std::lock_guard lock(mutex_); + for (size_t i = 0; i < free_.size(); ++i) { + if (free_[i].size >= bytes) { + const Buffer buffer = free_[i]; + free_.erase(free_.begin() + std::ptrdiff_t(i)); + return buffer; + } + } + } + const size_t size = (bytes + (size_t(1) << 20) - 1) & ~((size_t(1) << 20) - 1); // (1 MiB steps) + Buffer buffer{AlignedAlloc(size), size}; + if (!buffer.data) buffer.size = 0; + return buffer; + } + + void Give(Buffer buffer) { + if (!buffer.data) return; + std::lock_guard lock(mutex_); + if (free_.size() < 8) { + free_.push_back(buffer); + return; + } + // (full: the smallest goes) + auto smallest = std::min_element(free_.begin(), free_.end(), + [](const Buffer& a, const Buffer& b) { return a.size < b.size; }); + if (smallest->size < buffer.size) std::swap(*smallest, buffer); + std::free(buffer.data); + } + + int fd_ = -1; + uint64_t size_ = 0; + std::atomic<bool> direct_{false}; + std::mutex mutex_; + std::vector<Buffer> free_; +}; + +} // namespace rex::filesystem::direct + +#endif // !_WIN32 diff --git a/glue/rexglue-sdk-main/include/rex/frame_stats.h b/glue/rexglue-sdk-main/include/rex/frame_stats.h new file mode 100644 index 00000000..724bcff4 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/frame_stats.h @@ -0,0 +1,334 @@ +// [ps5 port] The guest's presented frames, for the PS5 build's frame rate +// counter and frame time capture: the renderer notes each present. +#pragma once + +#include <atomic> +#include <chrono> +#include <cstdint> +#include <string_view> + +#include <pthread.h> + +#include <rex/chrono/clock.h> + +namespace rex::frame_stats { + +inline std::atomic<uint64_t> g_presents{0}; +// Images sent to the TV (more than the game's frames with frame generation). +inline std::atomic<uint64_t> g_display_presents{0}; +inline std::atomic<int64_t> g_last_present_ns{0}; +inline std::atomic<uint32_t> g_last_frame_us{0}; +// The last kHistory frames' times (frame n at n % kHistory), for the graph. +inline constexpr uint32_t kHistory = 256; +inline std::atomic<uint32_t> g_history_us[kHistory]; +// [ps5 port] What the TV showed (ps5_display's pacing thread, from VideoOut's +// flips): how long each frame stayed on screen (frame n at n % kTvHistory), +// how many it showed, and frames that came and went between two looks. The +// FPS counter reads these: a frame held three vblanks is a stutter there even +// when the game finished it on time. +inline constexpr uint32_t kTvHistory = 512; +inline std::atomic<uint32_t> g_tv_hold_us[kTvHistory]; +inline std::atomic<uint64_t> g_tv_shown{0}; +inline std::atomic<uint64_t> g_tv_missed{0}; + +// [ps5 port] Set while FSR 3 frame generation is chosen and the device can +// run it; with it off (or unavailable) presentation is decoupled. +inline std::atomic<bool> g_frame_generation_active{false}; +// [ps5 port] Frame generation's cadence: how many vblanks each image (generated +// or real) stays on screen. 1: the game at half the TV's rate (30 -> 60 at +// 60 Hz, 60 -> 120 at 120 Hz); 2 at 120 Hz while the game can't hold 60 (30 -> +// 60). Set by the game's frame limiter; the display follows it through the +// hook, which re-applies VideoOut's flip rate. +inline std::atomic<uint32_t> g_fg_vblanks_per_image{1}; +inline std::atomic<void (*)(const char*)> g_display_pacing_changed{nullptr}; +// [ps5 port] Called by the presenter right before it queues an image to the +// TV (ps5_display.cpp: holds it until the image before has had its full time). +inline std::atomic<void (*)()> g_before_present{nullptr}; +// Whether the image about to be presented carries a new game frame (not the +// one on the TV again: the presenter re-presents it at the TV's rate). +inline std::atomic<bool> g_present_new_frame{true}; +// UI-thread repaints of the frame already on the TV that were skipped. +inline std::atomic<uint64_t> g_repeat_paints_skipped{0}; +// [ps5 port] Asks the presenter for one paint of the frame on the TV (set by +// the presenter), and how many were made: ps5_display.cpp's refresh while the +// game is behind. +inline std::atomic<void (*)()> g_request_keepalive_paint{nullptr}; +inline std::atomic<uint64_t> g_keepalive_paints{0}; +// The frame limit in effect: the setting's, or 40 while Smooth Heavy Areas +// holds a 60 limit there (the game's frame limiter sets it; 0 until then). +inline std::atomic<uint32_t> g_effective_frame_limit{0}; +// Images presented as generated + real pairs, and real frames that went out +// alone (no generated partner), for the timing line. +inline std::atomic<uint64_t> g_fg_pairs{0}, g_fg_unpaired{0}; +// [ps5 port] Why a scene went out without a generated partner, counted for the +// 10 s frame generation line (the first gate each scene failed), and the +// pairs handed to the presenter. Names: kFgDropNames. +enum class FgDrop : uint32_t { + kNoScene, // the frame had no 3D scene (menus, loading, the phone) + kSceneSize, // the scene's size didn't match the temporal setup + kAssets, // the temporal images couldn't be made + kSpatialMotion, // a draw's motion couldn't be followed: FSR fell back to spatial + kSpatialWorld, // no world draws seen + kSpatialDepth, // no depth + kSpatialCamera, // the camera wasn't taken up + kSpatialTime, // no time passed since the last scene + kSpatialEncode, // the upscaler refused the frame + kSecondComposite, // a second final composite in one frame + kEncodeGate, // motion, camera or presenter state at the generation step + kPresenter, // the presenter can't show generated frames + kAudit, // the motion audit failed + kConfigure, // frame generation couldn't be set up + kCamera, // the camera matrices don't invert + kEncodeRefused, // frame generation refused the inputs (its status is logged) + kHistoryReset, // FSR reset its history: a real frame only + kHudNotExact, // a HUD draw couldn't be replayed onto the pair + kDisplayStale, // the display image wasn't this frame's + kConvert, // converting the pair for the TV failed + kPublishRefused, // the presenter wouldn't take the pair + kCount +}; +inline constexpr const char* kFgDropNames[] = { + "no scene", "scene size", "assets", "spatial: motion", "spatial: no world", "spatial: depth", + "spatial: camera", "spatial: time", "spatial: upscaler", "second composite", "generation gate", + "presenter", "audit", "configure", "camera", "refused", "history reset", "HUD not exact", + "display stale", "convert", "publish refused"}; +inline std::atomic<uint32_t> g_fg_drop[uint32_t(FgDrop::kCount)]; +inline std::atomic<uint64_t> g_fg_published{0}, g_fg_sequence_gaps{0}; +inline void NoteFgDrop(FgDrop why) { + if (g_frame_generation_active.load(std::memory_order_relaxed)) + g_fg_drop[uint32_t(why)].fetch_add(1, std::memory_order_relaxed); +} + +inline int64_t NowNs() { + return std::chrono::duration_cast<std::chrono::nanoseconds>( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); +} + +// Renderer stage times, summed until the PS5's 10 s timing line reads and +// clears them: the recorder's time per frame (recording and submitting the +// Vulkan commands) and the command worker's wait for the recorder. +inline std::atomic<uint64_t> g_recorder_us{0}, g_recorder_frames{0}, g_recorder_max_us{0}; +inline std::atomic<uint64_t> g_handoff_wait_us{0}; +// Time the renderer spent waiting for the graphics card to finish an earlier +// frame (a frame slot to reuse): high means the GPU is the limit. +inline std::atomic<uint64_t> g_gpu_wait_us{0}; +inline void NoteRecorderFrame(uint64_t us) { + g_recorder_us.fetch_add(us, std::memory_order_relaxed); + g_recorder_frames.fetch_add(1, std::memory_order_relaxed); + uint64_t max = g_recorder_max_us.load(std::memory_order_relaxed); + while (us > max && !g_recorder_max_us.compare_exchange_weak(max, us, std::memory_order_relaxed)) { + } +} + +// Where a frame's time went, for the 10 s timing line and the hitch log. +// The title (game) thread's waits for a free frame slot (the renderer behind +// it), summed for the timing line and for its current frame. +inline std::atomic<uint64_t> g_title_slot_wait_us{0}, g_title_frame_slot_wait_us{0}; +inline std::atomic<uint32_t> g_title_last_slot_wait_us{0}; +// The game's render thread (set at its first present) and the time it spent +// blocked on the game's own other threads: guest waits, sleeps and lock +// spins. Its frame less these and the renderer waits is its work. +inline std::atomic<uintptr_t> g_title_thread{0}; +inline std::atomic<uint64_t> g_title_guest_wait_ticks{0}; +// The same, for the current frame only (the hitch line takes and clears it). +inline std::atomic<uint64_t> g_title_frame_guest_wait_ticks{0}; +// The game's main thread (its entry point's, which runs the game's update: +// set when it starts) and its time in the same waits, for the timing line. +inline std::atomic<uintptr_t> g_game_main_thread{0}; +inline std::atomic<uint64_t> g_main_guest_wait_ticks{0}; +inline bool IsTitleThread() { + const uintptr_t title = g_title_thread.load(std::memory_order_relaxed); + return title && title == reinterpret_cast<uintptr_t>(pthread_self()); +} +class TitleWaitScope { + public: + TitleWaitScope() { + const uintptr_t self = reinterpret_cast<uintptr_t>(pthread_self()); + if (self == g_title_thread.load(std::memory_order_relaxed)) { + main_ = false; + } else if (self == g_game_main_thread.load(std::memory_order_relaxed)) { + main_ = true; + } else { + return; + } + begin_ = rex::chrono::Clock::QueryHostTickCount(); + } + ~TitleWaitScope() { + if (!begin_) return; + const uint64_t ticks = rex::chrono::Clock::QueryHostTickCount() - begin_; + if (main_) { + g_main_guest_wait_ticks.fetch_add(ticks, std::memory_order_relaxed); + return; + } + g_title_guest_wait_ticks.fetch_add(ticks, std::memory_order_relaxed); + g_title_frame_guest_wait_ticks.fetch_add(ticks, std::memory_order_relaxed); + } + TitleWaitScope(const TitleWaitScope&) = delete; + TitleWaitScope& operator=(const TitleWaitScope&) = delete; + + private: + uint64_t begin_ = 0; + bool main_ = false; +}; + +// [ps5 port] The CPUs the renderer's threads ran on since the last timing +// line (a bit per CPU), and the CPUs they are kept on (0: anywhere). +inline std::atomic<uint32_t> g_cpus_title{0}, g_cpus_worker{0}, g_cpus_recorder{0}, g_cpus_capture{0}; +inline std::atomic<uint64_t> g_renderer_cpu_mask{0}; +// The CPUs a game thread may use (13 of the 16), and the first cluster's 8. +inline constexpr uint64_t kGameCpus = 0x1FFF, kFirstClusterCpus = 0xFF; +// Renderer Threads on Own Cores (a layout, not a mask): the game's render +// thread, the recorder and the worker each get a core of their own (CPUs 0, +// 2 and 4; a core's two hardware threads are numbered side by side), and +// every other thread runs on CPUs 5-12, so CPUs 1 and 3 stay empty (sharing +// the render thread's core with the recycling thread measured slower). The +// thread that recycles finished commands writes the commands and reference +// counts the render thread fills next: kept on CPUs 5-7, the same half of +// the CPU (and the same L3 cache) as the renderer's three, those come over +// within that half instead of from the other one. "Apart" leaves it on 5-12. +// g_main_core gives the game's main thread a core of its own as well (CPU 6, +// 7 left empty, the rest on 5 and 8-12). Off: 0.6b2 turned it on and off +// every 10 s under Compare, and both of the first sessions froze a few +// seconds after one of those moves. +inline constexpr uint64_t kOwnCoresLayout = uint64_t(1) << 63; +inline constexpr uint64_t kOwnCoresApartLayout = uint64_t(1) << 62; +inline constexpr uint64_t kTitleCpu = 1u << 0, kRecorderCpu = 1u << 2, kWorkerCpu = 1u << 4, + kRetirerCpus = 0xE0, kMainCpu = 1u << 6, kMainCoreCpus = 0xC0; +inline constexpr uint64_t kOtherThreadCpus = kGameCpus & ~uint64_t(0x1F); +// The main thread's own core, under Own Cores (off; see above). +inline std::atomic<bool> g_main_core{false}; +// Capture on its own core (Game Thread Capture "Own Core"): the capture thread +// takes CPU 6 (8 with the main thread's own core), which the other threads +// then leave to it; its core's other hardware thread stays theirs. +inline std::atomic<bool> g_capture_on_core{false}; +inline std::atomic<bool> g_capture_core_compare{false}; +// Whether the capture thread's CPU is kept for it: set once at start (Own Core +// or Compare), not with Compare's turns, so the other threads aren't moved +// every 10 s (moving them was what 0.6b2's frozen sessions had in common). +inline std::atomic<bool> g_capture_cpu_reserved{false}; +inline uint64_t CaptureCpu() { + return g_main_core.load(std::memory_order_relaxed) ? uint64_t(1) << 8 : uint64_t(1) << 6; +} +// Time the game's render thread waited for room in the capture ring, or for +// the capture thread to finish a frame it was still capturing (renderer time). +inline std::atomic<uint64_t> g_capture_ring_wait_us{0}; +inline bool IsOwnCoresLayout(uint64_t layout) { + return layout == kOwnCoresLayout || layout == kOwnCoresApartLayout; +} +enum class RendererRole { kTitle, kWorker, kRecorder, kRetirer, kMain, kOther, kCapture }; +// The CPUs one thread is kept on under a layout (0: anywhere). +inline uint64_t RendererRoleMask(uint64_t layout, RendererRole role) { + if (IsOwnCoresLayout(layout)) { + const uint64_t main_core = g_main_core.load(std::memory_order_relaxed) ? kMainCoreCpus : 0; + const uint64_t capture = g_capture_cpu_reserved.load(std::memory_order_relaxed) ? CaptureCpu() : 0; + switch (role) { + case RendererRole::kTitle: return kTitleCpu; + case RendererRole::kRecorder: return kRecorderCpu; + case RendererRole::kWorker: return kWorkerCpu; + case RendererRole::kCapture: return CaptureCpu(); + case RendererRole::kRetirer: + return (layout == kOwnCoresLayout ? kRetirerCpus : kOtherThreadCpus) & ~main_core & ~capture; + case RendererRole::kMain: + if (main_core) return kMainCpu; + return kOtherThreadCpus & ~capture; + default: return kOtherThreadCpus & ~main_core & ~capture; + } + } + if ((role == RendererRole::kOther || role == RendererRole::kMain) && layout != kFirstClusterCpus) + return kGameCpus; + return layout ? layout : kGameCpus; +} +// Renderer Threads on Compare: the timing report switches between own cores +// with the recycling thread on the renderer's half of the CPU and anywhere +// every 10 s, so one session measures both. +inline std::atomic<bool> g_renderer_cpu_compare{false}; +// Game Thread Capture (ps5_lean_capture): lean (each resource held once for +// two frames, with prefetching) or classic. On Compare it stays lean and the +// timing report switches lean's prefetching off and on every 10 s; the game's +// render thread picks the change up at its next frame. +inline std::atomic<bool> g_lean_capture{true}; +inline std::atomic<bool> g_lean_capture_compare{false}; +inline std::atomic<bool> g_capture_prefetch{true}; +// The layout the Renderer Threads setting asks for at start. +inline uint64_t RendererCpuMaskFor(std::string_view setting) { + if (setting == "any") return 0; + if (setting == "together") return kFirstClusterCpus; + if (setting == "apart") return kOwnCoresApartLayout; + return kOwnCoresLayout; // "cores" and "compare" +} +#if defined(__PROSPERO__) +extern "C" int32_t sceKernelGetCurrentCpu(void); +extern "C" int32_t scePthreadSetaffinity(pthread_t thread, uint64_t mask); +inline void NoteCpu(std::atomic<uint32_t>& cpus) { + const int32_t cpu = sceKernelGetCurrentCpu(); + if (cpu >= 0 && cpu < 32) cpus.fetch_or(1u << cpu, std::memory_order_relaxed); +} +// The game's render thread, the worker and the recorder hand each other +// every draw; kept on one of the CPU's two clusters, they share its cache. +inline void KeepRendererThreadOnItsCpus() { + scePthreadSetaffinity(pthread_self(), RendererRoleMask(g_renderer_cpu_mask.load(std::memory_order_relaxed), + RendererRole::kRetirer)); +} +// Once a frame on each renderer thread: applies its CPUs when the layout changed. +inline void FollowRendererCpuMask(uint64_t& applied, RendererRole role) { + const uint64_t mask = g_renderer_cpu_mask.load(std::memory_order_relaxed); + if (mask == applied) return; + applied = mask; + scePthreadSetaffinity(pthread_self(), RendererRoleMask(mask, role)); +} +#else +inline void NoteCpu(std::atomic<uint32_t>&) {} +inline void KeepRendererThreadOnItsCpus() {} +inline void FollowRendererCpuMask(uint64_t&, RendererRole) {} +#endif + +inline void NoteTitleSlotWait(uint64_t us) { + g_title_slot_wait_us.fetch_add(us, std::memory_order_relaxed); + g_title_frame_slot_wait_us.fetch_add(us, std::memory_order_relaxed); +} +// The recorder holding a finished frame back for the frame limit (time that +// is not work), summed and for its current frame; and its last frame's work. +inline std::atomic<uint64_t> g_pacer_wait_us{0}, g_frame_pacer_us{0}; +// The recorder waiting for its output image to be free (the presenter still +// using it: with frame generation, for the generated frame). +inline std::atomic<uint64_t> g_output_image_wait_us{0}; +// The recorder's frame: recording the commands, and the whole output step +// around it (with frame generation and FSR 3 encoding and submission). +inline std::atomic<uint64_t> g_record_commands_us{0}, g_output_callback_us{0}; +inline std::atomic<uint32_t> g_last_recorder_busy_us{0}; +// Small draws into the sun shadow map dropped before they reach the renderer +// (Small Object Shadows off), for the capture line. +inline std::atomic<uint64_t> g_title_small_shadows_dropped{0}; +// Environment reflection draws, clears and resolves left out on the frames +// between its redraws (ps5_environment_reflection_every). +inline std::atomic<uint64_t> g_title_reflection_commands_skipped{0}; + +// [ps5 port] The game frame (its guest output publication) each image sent to +// the TV carried, by the image's number from 1: VideoOut numbers its flips the +// same way (RADV's flip argument), so the pacing log (ps5_display.cpp) can tell +// a new frame on the screen from a repeat of the last one. +inline constexpr uint32_t kImageHistory = 1024; +inline std::atomic<uint64_t> g_images_sent{0}; +inline std::atomic<uint64_t> g_image_frame[kImageHistory]; +// From the presenter, for each image it queued to the TV. +inline void NoteImageSent(uint64_t frame) { + const uint64_t n = g_images_sent.load(std::memory_order_relaxed) + 1; + g_image_frame[n % kImageHistory].store(frame, std::memory_order_relaxed); + g_images_sent.store(n, std::memory_order_release); +} + +// From the renderer, once per guest frame. +inline void NotePresent() { + const int64_t now = NowNs(); + const int64_t last = g_last_present_ns.exchange(now, std::memory_order_acq_rel); + const uint32_t us = last ? uint32_t((now - last) / 1000) : 0; + if (last) g_last_frame_us.store(us, std::memory_order_relaxed); + g_title_last_slot_wait_us.store(uint32_t(g_title_frame_slot_wait_us.exchange(0, std::memory_order_relaxed)), + std::memory_order_relaxed); + const uint64_t n = g_presents.load(std::memory_order_relaxed); + g_history_us[n % kHistory].store(us, std::memory_order_relaxed); + g_presents.store(n + 1, std::memory_order_release); +} + +} // namespace rex::frame_stats diff --git a/glue/rexglue-sdk-main/include/rex/graphics/pipeline/texture/cache.h b/glue/rexglue-sdk-main/include/rex/graphics/pipeline/texture/cache.h index d1580fca..a73a2ea1 100644 --- a/glue/rexglue-sdk-main/include/rex/graphics/pipeline/texture/cache.h +++ b/glue/rexglue-sdk-main/include/rex/graphics/pipeline/texture/cache.h @@ -66,6 +66,13 @@ class TextureCache { TextureCache& operator=(const TextureCache& texture_cache) = delete; virtual ~TextureCache(); + // [ps5 port] Called with a binding's fetch constant right before the + // texture it needs is loaded from guest memory. The callback may rewrite + // that guest memory (it must trigger the physical memory callbacks for what + // it changes). Set once, before any drawing. + using BeforeTextureReadCallback = void (*)(const xenos::xe_gpu_texture_fetch_t& fetch); + static void SetBeforeTextureReadCallback(BeforeTextureReadCallback callback); + // Returns whether the actual scale is not smaller than the requested one. static bool GetConfigDrawResolutionScale(uint32_t& x_out, uint32_t& y_out); uint32_t draw_resolution_scale_x() const { return draw_resolution_scale_x_; } @@ -220,15 +227,15 @@ class TextureCache { static constexpr uint32_t kOutdatedBitMips = UINT32_C(1) << 1; uint32_t outdated_mask() const { return outdated_mask_.load(std::memory_order_acquire); } - bool base_outdated(const std::unique_lock<std::recursive_mutex>& global_lock) const { + bool base_outdated(const rex::thread::global_lock_t& global_lock) const { return base_outdated_; } - bool mips_outdated(const std::unique_lock<std::recursive_mutex>& global_lock) const { + bool mips_outdated(const rex::thread::global_lock_t& global_lock) const { return mips_outdated_; } - void MakeUpToDateAndWatch(const std::unique_lock<std::recursive_mutex>& global_lock); + void MakeUpToDateAndWatch(const rex::thread::global_lock_t& global_lock); - void WatchCallback(const std::unique_lock<std::recursive_mutex>& global_lock, bool is_mip); + void WatchCallback(const rex::thread::global_lock_t& global_lock, bool is_mip); // For LRU caching - updates the last usage frame and moves the texture to // the end of the usage queue. Must be called any time the texture is @@ -563,7 +570,7 @@ class TextureCache { void UpdateTexturesTotalHostMemoryUsage(uint64_t add, uint64_t subtract); // Shared memory callback for texture data invalidation. - static void WatchCallback(const std::unique_lock<std::recursive_mutex>& global_lock, + static void WatchCallback(const rex::thread::global_lock_t& global_lock, void* context, void* data, uint64_t argument, bool invalidated_by_gpu); // Checks if there are any pages that contain scaled resolve data within the @@ -572,9 +579,9 @@ class TextureCache { // Global shared memory invalidation callback for invalidating scaled resolved // texture data. static void ScaledResolveGlobalWatchCallbackThunk( - const std::unique_lock<std::recursive_mutex>& global_lock, void* context, + const rex::thread::global_lock_t& global_lock, void* context, uint32_t address_first, uint32_t address_last, bool invalidated_by_gpu); - void ScaledResolveGlobalWatchCallback(const std::unique_lock<std::recursive_mutex>& global_lock, + void ScaledResolveGlobalWatchCallback(const rex::thread::global_lock_t& global_lock, uint32_t address_first, uint32_t address_last, bool invalidated_by_gpu); diff --git a/glue/rexglue-sdk-main/include/rex/graphics/shared_memory.h b/glue/rexglue-sdk-main/include/rex/graphics/shared_memory.h index b05df6aa..a00e62df 100644 --- a/glue/rexglue-sdk-main/include/rex/graphics/shared_memory.h +++ b/glue/rexglue-sdk-main/include/rex/graphics/shared_memory.h @@ -35,7 +35,7 @@ class SharedMemory { void SetSystemPageBlocksValidWithGpuDataWritten(); void InvalidateAllPages(); - typedef void (*GlobalWatchCallback)(const std::unique_lock<std::recursive_mutex>& global_lock, + typedef void (*GlobalWatchCallback)(const rex::thread::global_lock_t& global_lock, void* context, uint32_t address_first, uint32_t address_last, bool invalidated_by_gpu); typedef void* GlobalWatchHandle; @@ -49,7 +49,7 @@ class SharedMemory { // The callback is called within the global critical region. GlobalWatchHandle RegisterGlobalWatch(GlobalWatchCallback callback, void* callback_context); void UnregisterGlobalWatch(GlobalWatchHandle handle); - typedef void (*WatchCallback)(const std::unique_lock<std::recursive_mutex>& global_lock, + typedef void (*WatchCallback)(const rex::thread::global_lock_t& global_lock, void* context, void* data, uint64_t argument, bool invalidated_by_gpu); typedef void* WatchHandle; @@ -96,6 +96,12 @@ class SharedMemory { // regions in those pages. void RangeWrittenByGpu(uint32_t start, uint32_t length); + // [ps5 port] Checks pages_per_frame pages of the GPU's copy of guest memory + // (round robin) against a hash of the guest data taken when each was + // uploaded: a page that changed while no write watch fired is reloaded. + // Returns how many were out of date. + uint32_t CheckStalePages(uint32_t pages_per_frame); + protected: SharedMemory(memory::Memory& memory); // Call in implementation-specific initialization. @@ -115,6 +121,9 @@ class SharedMemory { uint32_t page_size_log2() const { return page_size_log2_; } + // [ps5 port] Hashes the guest data of pages just uploaded (CheckStalePages). + void RecordPageHashes(uint32_t page_first, uint32_t page_count); + uint32_t host_gpu_memory_sparse_granularity_log2() const { return host_gpu_memory_sparse_granularity_log2_; } @@ -172,6 +181,13 @@ class SharedMemory { void* memory_invalidation_callback_handle_ = nullptr; void* memory_data_provider_handle_ = nullptr; + // [ps5 port] CheckStalePages: each page's guest data hash when it was + // uploaded (0: not uploaded or since invalidated), the next page to check, + // and how many stale pages were found. + std::vector<uint64_t> page_hashes_; + uint32_t stale_check_cursor_ = 0; + uint64_t stale_pages_found_ = 0; + // Ranges that need to be uploaded, generated by GetRangesToUpload (a // persistently allocated vector). std::vector<std::pair<uint32_t, uint32_t>> upload_ranges_; diff --git a/glue/rexglue-sdk-main/include/rex/input/input_driver.h b/glue/rexglue-sdk-main/include/rex/input/input_driver.h index 77a963b9..ab187d90 100644 --- a/glue/rexglue-sdk-main/include/rex/input/input_driver.h +++ b/glue/rexglue-sdk-main/include/rex/input/input_driver.h @@ -36,6 +36,11 @@ class InputDriver { virtual X_RESULT GetCapabilities(uint32_t user_index, uint32_t flags, X_INPUT_CAPABILITIES* out_caps) = 0; virtual X_RESULT GetState(uint32_t user_index, X_INPUT_STATE* out_state) = 0; + // [ps5 port] The state for the system dialogs: like GetState, but not + // switched off while the game's input is. + virtual X_RESULT GetStateUi(uint32_t user_index, X_INPUT_STATE* out_state) { + return GetState(user_index, out_state); + } virtual X_RESULT SetState(uint32_t user_index, X_INPUT_VIBRATION* vibration) = 0; virtual X_RESULT GetKeystroke(uint32_t user_index, uint32_t flags, X_INPUT_KEYSTROKE* out_keystroke) = 0; diff --git a/glue/rexglue-sdk-main/include/rex/input/input_system.h b/glue/rexglue-sdk-main/include/rex/input/input_system.h index dd57db44..10f767bb 100644 --- a/glue/rexglue-sdk-main/include/rex/input/input_system.h +++ b/glue/rexglue-sdk-main/include/rex/input/input_system.h @@ -42,6 +42,8 @@ class InputSystem : public system::IInputSystem { X_RESULT GetCapabilities(uint32_t user_index, uint32_t flags, X_INPUT_CAPABILITIES* out_caps); X_RESULT GetState(uint32_t user_index, X_INPUT_STATE* out_state); + // [ps5 port] Every pad merged, for the system dialogs. + bool GetUiGamepadState(X_INPUT_GAMEPAD* out_gamepad); // Returns the most recent state produced by GetState without polling any // driver. Game-specific bridges use this to observe controller buttons at a // deterministic guest poll boundary without consuming transient input a diff --git a/glue/rexglue-sdk-main/include/rex/input/ps5/ps5_input_driver.h b/glue/rexglue-sdk-main/include/rex/input/ps5/ps5_input_driver.h new file mode 100644 index 00000000..30672588 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/input/ps5/ps5_input_driver.h @@ -0,0 +1,67 @@ +#pragma once +/** + * [ps5 port] DualSense controllers through scePad: one Xbox 360 pad per + * logged-in PS5 user that has a controller. + */ +#include <array> +#include <cstdint> +#include <mutex> + +#include <rex/input/input_driver.h> +#include <rex/input/sony_feedback.h> + +namespace rex::input::ps5 { + +class Ps5InputDriver final : public InputDriver { + public: + explicit Ps5InputDriver(rex::ui::Window* window, size_t window_z_order); + ~Ps5InputDriver() override; + + X_STATUS Setup() override; + X_RESULT GetCapabilities(uint32_t user_index, uint32_t flags, + X_INPUT_CAPABILITIES* out_caps) override; + X_RESULT GetState(uint32_t user_index, X_INPUT_STATE* out_state) override; + X_RESULT GetStateUi(uint32_t user_index, X_INPUT_STATE* out_state) override; + X_RESULT SetState(uint32_t user_index, X_INPUT_VIBRATION* vibration) override; + X_RESULT GetKeystroke(uint32_t user_index, uint32_t flags, + X_INPUT_KEYSTROKE* out_keystroke) override; + + private: + static constexpr size_t kMaxPads = 4; + struct Pad { + int32_t user_id = -1; + int32_t handle = -1; + uint32_t packet_number = 0; + X_INPUT_GAMEPAD last_gamepad{}; + uint32_t keystroke_buttons = 0; // the buttons the keystroke events last reported + // DualSense features (rex::input::sony, the PC port's): the game's own + // rumble, and what was last sent to the controller. + uint64_t generation = 0; // (its connection to the feature service) + uint16_t native_low = 0, native_high = 0; + uint16_t pulse_low = 0, pulse_high = 0; + uint8_t sent_large = 0, sent_small = 0; + bool light_set = false; + std::array<uint8_t, 3> color{}; + bool triggers_set = false; + std::array<uint8_t, 120> trigger_param{}; // (the last ScePadTriggerEffectParam sent) + int trigger_failures = 0; + uint64_t next_feedback_ms = 0; + }; + // A pad by the game's user index (0..3: the PS5 users in login order). + Pad* PadFor(uint32_t user_index); + void RefreshPads(); + X_RESULT ReadState(uint32_t user_index, bool active, X_INPUT_STATE* out_state); + // The light bar, trigger effects and added rumble, sent with the game's own + // reads (every 8 ms at most; a controller is told only what changed): every + // scePad call on a controller is made from the thread that reads it, under + // mutex_, never from two threads at once. + void ApplyFeedback(uint32_t user_index, Pad& pad); + void SendVibration(Pad& pad); // (the game's rumble with the added pulses) + + std::mutex mutex_; + std::array<Pad, kMaxPads> pads_{}; + uint64_t last_refresh_ms_ = 0; + uint64_t next_generation_ = 1; +}; + +} // namespace rex::input::ps5 diff --git a/glue/rexglue-sdk-main/include/rex/input/sony_feedback.h b/glue/rexglue-sdk-main/include/rex/input/sony_feedback.h index 0d19033f..783c7399 100644 --- a/glue/rexglue-sdk-main/include/rex/input/sony_feedback.h +++ b/glue/rexglue-sdk-main/include/rex/input/sony_feedback.h @@ -57,12 +57,30 @@ struct GameState { int episode = 0; int wanted = 0; TriggerContext trigger_context = TriggerContext::kNone; + // GTA IV's weapon slot (2 handgun, 3 shotgun, 4 SMG, 5 rifle, 6 sniper, + // 7 heavy, 8 thrown) and how fast the player (or their vehicle) moves, m/s. + int weapon_slot = 0; + float speed = 0; uint32_t player_identity = 0; uint32_t context_identity = 0; }; using Trigger = std::array<uint8_t, 11>; using EffectPacket = std::array<uint8_t, 47>; +// What a trigger's effect is for, with its strength (0..1), alongside the +// DualSense report bytes: a driver with the console's own trigger modes (the +// PS5's scePadSetTriggerEffect) picks the mode that feels like it. +// kFeedback/kWeapon/kVibration name the console's mode directly, with its +// parameters in a, b, c: feedback (position 0-9, strength 1-8), weapon (start +// 2-7, end start+1..8, strength 0-8), vibration (position 0-9, amplitude 0-8, +// frequency Hz). +struct TriggerFeel { + enum Kind : uint8_t { kOff, kPedal, kAim, kGun, kRecoil, kFeedback, kWeapon, kVibration }; + Kind kind = kOff; + float strength = 0; + uint8_t a = 0, b = 0, c = 0; + bool operator==(const TriggerFeel&) const = default; +}; struct Output { bool active = false; bool suppress_native = false; @@ -72,6 +90,7 @@ struct Output { uint16_t high_motor = 0; Trigger left{}; Trigger right{}; + TriggerFeel left_feel{}, right_feel{}; }; Trigger Resistance(uint8_t position, uint8_t strength); @@ -105,6 +124,11 @@ class Service { }; struct PendingGesture { Gesture gesture; uint64_t timestamp; }; struct Pulse { uint64_t until_ms = 0; float strength = 0; }; + struct Shots { + uint64_t last_ms = 0; + float interval_ms = 0; // between the last shots (0: one shot) + int slot = 0; // the weapon slot that fired + }; struct Slot { Device device{}; GameState game{}; @@ -118,6 +142,9 @@ class Service { bool multi_touch = false; uint64_t multi_started_ms = 0; std::array<Pulse, 3> pulses{}; + Shots shots{}; + uint64_t shot_rumble_until_ms = 0; + float shot_low = 0, shot_high = 0; }; static bool Fresh(const Slot& slot, uint64_t now_ms); static void Cancel(Slot& slot); diff --git a/glue/rexglue-sdk-main/include/rex/platform.h b/glue/rexglue-sdk-main/include/rex/platform.h index fd32c512..068e0a55 100644 --- a/glue/rexglue-sdk-main/include/rex/platform.h +++ b/glue/rexglue-sdk-main/include/rex/platform.h @@ -49,6 +49,11 @@ #elif defined(__gnu_linux__) #define REX_PLATFORM_GNU_LINUX 1 #define REX_PLATFORM_LINUX 1 +#elif defined(__PROSPERO__) +// [ps5 port] PlayStation 5 homebrew (FreeBSD-based). Shares the POSIX paths +// with Linux, like Android does; PS5 differences are under REX_PLATFORM_PS5. +#define REX_PLATFORM_PS5 1 +#define REX_PLATFORM_LINUX 1 #else #error Unsupported target OS. #endif @@ -82,6 +87,9 @@ #ifndef REX_PLATFORM_LINUX #define REX_PLATFORM_LINUX 0 #endif +#ifndef REX_PLATFORM_PS5 +#define REX_PLATFORM_PS5 0 +#endif #if defined(__clang__) #define REX_COMPILER_CLANG 1 diff --git a/glue/rexglue-sdk-main/include/rex/ppc/context.h b/glue/rexglue-sdk-main/include/rex/ppc/context.h index 2d571e3d..abeebda4 100644 --- a/glue/rexglue-sdk-main/include/rex/ppc/context.h +++ b/glue/rexglue-sdk-main/include/rex/ppc/context.h @@ -306,6 +306,16 @@ struct alignas(0x40) PPCContext { */ uint32_t last_indirect_target = 0; + /** + * [ps5 port] Interrupt-disabled depth of this guest thread (its global + * critical region nesting): mfmsr reads it, mtmsrd counts it. Here rather + * than in a thread_local, which on the PS5 costs a lookup through the system + * three times per guest interlocked operation. A guest thread has one + * context, and the recompiled code takes and leaves the region within one + * function, so it is the same count. + */ + int32_t global_lock_depth = 0; + PPCRegister f0; PPCRegister f1; PPCRegister f2; diff --git a/glue/rexglue-sdk-main/include/rex/ppc/function.h b/glue/rexglue-sdk-main/include/rex/ppc/function.h index 611a52f4..7038c1a2 100644 --- a/glue/rexglue-sdk-main/include/rex/ppc/function.h +++ b/glue/rexglue-sdk-main/include/rex/ppc/function.h @@ -419,8 +419,7 @@ __attribute__((noinline)) void HostToGuestFunction(PPCContext& ctx, uint8_t* bas if constexpr (std::is_pointer<ret_t>()) { if (v != nullptr) { - ctx.r3.u64 = - static_cast<uint32_t>(reinterpret_cast<size_t>(v) - reinterpret_cast<size_t>(base)); + ctx.r3.u64 = rex::memory::GuestAddressFromHost(v, base); } else { ctx.r3.u64 = 0; } diff --git a/glue/rexglue-sdk-main/include/rex/ps5_compat.h b/glue/rexglue-sdk-main/include/rex/ps5_compat.h new file mode 100644 index 00000000..85c3c3ca --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/ps5_compat.h @@ -0,0 +1,33 @@ +/** + * @file rex/ps5_compat.h + * @brief [ps5 port] Linux names the POSIX code uses, mapped to the PS5's + * FreeBSD equivalents. Force-included on PS5 builds + * (-include rex/ps5_compat.h); a no-op everywhere else. + * + * Macros only, no includes: it is force-included ahead of the precompiled + * headers, and declaring anything here (even via <stdio.h>) would declare + * clang's __va_list_tag twice and break va_list in PCH'd files. + */ +#pragma once + +#if defined(__PROSPERO__) + +// FreeBSD's off_t is always 64-bit: the *64 large-file names are the plain ones. +#define fseeko64 fseeko +#define ftello64 ftello +#define ftruncate64 ftruncate +#define lseek64 lseek +#define mmap64 mmap +#define pread64 pread +#define pwrite64 pwrite +#define fopen64 fopen +#define open64 open +#define fstat64 fstat +#define stat64 stat +#define lstat64 lstat +#define off64_t off_t + +// Linux-only clock; the monotonic clock is the closest PS5 equivalent. +#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC + +#endif // __PROSPERO__ diff --git a/glue/rexglue-sdk-main/include/rex/ps5_diag.h b/glue/rexglue-sdk-main/include/rex/ps5_diag.h new file mode 100644 index 00000000..fca98725 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/ps5_diag.h @@ -0,0 +1,89 @@ +#pragma once +/** + * [ps5 port] Progress counters for bringing the game up on the PS5: a + * monitor thread logs them every 10 s (windowed_app_main_sdl.cpp), so a + * black screen shows which stage stopped moving. Cheap relaxed atomics. + */ +#include <atomic> +#include <cstdint> + +namespace rex::ps5diag { +enum Counter : int { + kAccessViolations, // faults reaching the MMIO handler + kMmioAccesses, // of those, GPU register reads / writes emulated + kWatchFaults, // of those, write watches / other handlers + kUnhandledFaults, // of those, not handled (a real crash) + kVblanks, // GPU vblanks + kInterrupts, // guest GPU interrupt callbacks run + kWritePointer, // ring buffer write pointer updates by the game + kPrimaryBuffers, // ring buffer batches executed + kCount +}; +extern std::atomic<uint64_t> counters[kCount]; +// The reserved guest address space (all views live inside it). +extern uintptr_t guest_arena_lo, guest_arena_hi; +inline void Add(Counter c) { counters[c].fetch_add(1, std::memory_order_relaxed); } + +// Guest threads blocked in a kernel wait: one slot per host thread, filled on +// entering XObject::Wait / WaitMultiple / SignalAndWait and cleared on leaving. +struct WaitSlot { + std::atomic<uint64_t> since_ms{0}; // 0: not waiting + uint32_t thread_id = 0; // guest thread id + uint32_t object_type = 0; // XObject::Type (first object) + uint32_t handle = 0; // its handle + uint32_t count = 0; // objects waited on + uint32_t guest_lr = 0; // the game code that waits + uint32_t guest_sp = 0; // its stack pointer, and its stack [lo, hi): the + uint32_t guest_stack_lo = 0, guest_stack_hi = 0; // monitor walks the callers from them + uint32_t timeout_ms = 0; // 0xFFFFFFFF: infinite + const void* critical_section = nullptr; + uint32_t critical_section_guest = 0; + uint32_t critical_section_lr = 0; // the game code that entered it // host address of a contended X_RTL_CRITICAL_SECTION +}; +constexpr int kWaitSlots = 256; +extern WaitSlot wait_slots[kWaitSlots]; +WaitSlot* CurrentWaitSlot(); // (nullptr when out of slots) +uint64_t NowMs(); +const char* ObjectTypeName(uint32_t type); + +// Host stack samples: every rex thread registers itself; SampleAllThreads() +// signals each one (SIGPROF), records its rip and the return-address-looking +// words on its stack, and logs them relative to Anchor() so they can be +// symbolized against the linked ELF offline. +void RegisterThread(const char* what); +// Before the calling thread exits: out of the samples and the CPU moves. +void UnregisterThread(); +// [ps5 port] Keeps every registered thread except the renderer's (the +// "worker" and "recorder", and `title_thread`) on `mask`, and the game's main +// thread on `main_mask` when one is given. +void KeepOtherThreadsOn(uint64_t mask, uintptr_t title_thread, uintptr_t main_thread = 0, + uint64_t main_mask = 0); +void SampleAllThreads(); +// A sampling CPU profiler on the same signal: ProfileTick() signals every +// registered thread once, each records where it was; ProfileReport() logs, +// per busy thread, the code addresses seen most (relative to Anchor(), like +// the stack samples). Cheap enough to leave running. +void ProfileTick(); +void ProfileReport(); +// A host fault no handler took (a crash in some thread): the first one is +// written to the end of the log file with the thread's return addresses +// (without the heap); the thread then re-faults, and that calls crash_hook. +void UnhandledHostFault(uintptr_t rip, uintptr_t address, uintptr_t rsp); +// Called (once) when a thread keeps faulting: tells the player and ends the +// game rather than leave it hanging. Set by main. It runs in the fault +// handler: no heap (no logging) before it flushes the log. +extern void (*crash_hook)(const char* what); +// When the first unhandled fault was reported (NowMs() | 1; 0: none): a +// watchdog ends the game if the crash hook doesn't. +extern std::atomic<uint64_t> crashed_at_ms; +// The log file, for the crash report (written without the logger). +void SetLogPath(const char* path); +// The game asked to quit (to the dashboard): the app's answer (restart into +// its own menu). Set: it doesn't return when it succeeds. +extern void (*title_exit_hook)(); +uintptr_t Anchor(); + +// Critical sections the game initialized (guest address -> game code). +void NoteCriticalSectionInit(uint32_t guest, uint32_t lr); +uint32_t CriticalSectionInitBy(uint32_t guest); // 0: never seen +} // namespace rex::ps5diag diff --git a/glue/rexglue-sdk-main/include/rex/string/numeric.h b/glue/rexglue-sdk-main/include/rex/string/numeric.h index 8fb3d978..0aaf689b 100644 --- a/glue/rexglue-sdk-main/include/rex/string/numeric.h +++ b/glue/rexglue-sdk-main/include/rex/string/numeric.h @@ -11,6 +11,9 @@ #pragma once #include <charconv> +#include <cerrno> +#include <cstdlib> +#include <string> #include <cstdint> #include <cstring> #include <string> @@ -22,6 +25,24 @@ #include <rex/string/utf8.h> #include <rex/vec128.h> +namespace rex::string::detail_float { +// [ps5 port] The PS5's libc++ 18 has no floating-point std::from_chars. +template <typename T> +inline std::from_chars_result FloatFromChars(const char* first, const char* last, T& value) { +#if defined(__PROSPERO__) + std::string text(first, last); + char* end = nullptr; + errno = 0; + const double d = std::strtod(text.c_str(), &end); + if (end == text.c_str()) return {first, std::errc::invalid_argument}; + value = static_cast<T>(d); + return {first + (end - text.c_str()), errno == ERANGE ? std::errc::result_out_of_range : std::errc()}; +#else + return std::from_chars(first, last, value, std::chars_format::general); +#endif +} +} // namespace rex::string::detail_float + namespace rex::string { inline std::string to_hex_string(uint32_t value) { @@ -133,8 +154,7 @@ inline T fpfs(const std::string_view value, bool force_hex) { } std::memcpy(&result, &pun, sizeof(PUN)); } else { - auto [p, error] = std::from_chars(range.data(), range.data() + range.size(), result, - std::chars_format::general); + auto [p, error] = rex::string::detail_float::FloatFromChars(range.data(), range.data() + range.size(), result); // TODO(gibbed): do something more with errors? if (error != std::errc()) { assert_always(); @@ -253,7 +273,7 @@ inline vec128_t from_string<vec128_t>(const std::string_view value, bool force_h assert_always(); return vec128_t(); } - auto result = std::from_chars(p, end, v.f32[i], std::chars_format::general); + auto result = rex::string::detail_float::FloatFromChars(p, end, v.f32[i]); if (result.ec != std::errc()) { assert_always(); return vec128_t(); diff --git a/glue/rexglue-sdk-main/include/rex/system/mmio_handler.h b/glue/rexglue-sdk-main/include/rex/system/mmio_handler.h index 7ae42a89..c9c3115b 100644 --- a/glue/rexglue-sdk-main/include/rex/system/mmio_handler.h +++ b/glue/rexglue-sdk-main/include/rex/system/mmio_handler.h @@ -45,7 +45,7 @@ class MMIOHandler { typedef uint32_t (*HostToGuestVirtual)(const void* context, const void* host_address); typedef bool (*AccessViolationCallback)( - std::unique_lock<std::recursive_mutex> global_lock_locked_once, void* context, + rex::thread::global_lock_t global_lock_locked_once, void* context, void* host_address, bool is_write); // access_violation_callback is called with global_critical_region locked once diff --git a/glue/rexglue-sdk-main/include/rex/system/xmemory.h b/glue/rexglue-sdk-main/include/rex/system/xmemory.h index 72613f7f..3ded49fd 100644 --- a/glue/rexglue-sdk-main/include/rex/system/xmemory.h +++ b/glue/rexglue-sdk-main/include/rex/system/xmemory.h @@ -18,6 +18,7 @@ #include <vector> #include <rex/memory/utils.h> +#include <rex/platform.h> #include <rex/memory/access_epoch.h> #include <rex/ppc/context.h> // PPCFunc type (minimal header) #include <rex/system/mmio_handler.h> @@ -32,8 +33,11 @@ namespace rex::memory::detail { /// Compensates for Windows 64KB allocation granularity on the 0xE0 physical heap. /// The backing file maps the 0xE0 heap at a 0x1000-byte offset, but MapViewOfFileEx /// rounds down to 64KB boundaries. Linux mmap handles 4KB offsets natively. +/// [ps5 port] The PS5 too (16 KB pages, 64 KB direct memory): the recompiled +/// code's REX_PHYS_HOST_OFFSET adds it there, and this must match, or kernel +/// calls given a 0xE0000000 pointer read and write 4 KB away from the game. constexpr u32 PhysicalHostOffset([[maybe_unused]] u32 guest_addr) noexcept { -#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC +#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC || REX_PLATFORM_PS5 return (guest_addr >= 0xE0000000u) ? 0x1000u : 0u; #else return 0u; @@ -52,6 +56,14 @@ inline T GuestPtr(u8* base, u32 guest_address) noexcept { return reinterpret_cast<T>(base + guest_address + detail::PhysicalHostOffset(guest_address)); } +/// GuestPtr's inverse: a host pointer into guest memory back to its guest +/// address (the 0xE0000000 range's host offset taken off again). +inline u32 GuestAddressFromHost(const void* host, const u8* base) noexcept { + uint64_t offset = uint64_t(reinterpret_cast<const u8*>(host) - base); + if (detail::PhysicalHostOffset(0xE0000000u) && offset >= 0xE0001000ull) offset -= 0x1000; + return static_cast<u32>(offset); +} + class Memory; enum SystemHeapFlag : uint32_t { @@ -215,6 +227,11 @@ class BaseHeap { // Queries the currently strictest readability and writability for the entire // range. rex::memory::PageAccess QueryRangeAccess(uint32_t low_address, uint32_t high_address); + // [ps5 port] The same without the heap lock, for checks made for every draw + // the game builds (the UI layout hooks', the controller's): the answer is as + // current as the locked one's, which can change as soon as the lock is + // released. Each page's entry is read whole, atomically. + rex::memory::PageAccess QueryRangeAccessUnlocked(uint32_t low_address, uint32_t high_address) const; // Stable only outside a mutation; may validate a cached permissions query. uint64_t access_epoch() const { return access_epoch_.Read(); } @@ -289,7 +306,7 @@ class PhysicalHeap : public BaseHeap { void EnableAccessCallbacks(uint32_t physical_address, uint32_t length, bool enable_invalidation_notifications, bool enable_data_providers); // Returns true if any page in the range was watched. - bool TriggerCallbacks(std::unique_lock<std::recursive_mutex> global_lock_locked_once, + bool TriggerCallbacks(rex::thread::global_lock_t global_lock_locked_once, uint32_t virtual_address, uint32_t length, bool is_write, bool unwatch_exact_range, bool unprotect = true); @@ -472,7 +489,7 @@ class Memory { // TODO(Triang3l): Implement data providers - this is why locking depth of 1 // will be required in the future. bool TriggerPhysicalMemoryCallbacks( - std::unique_lock<std::recursive_mutex> global_lock_locked_once, uint32_t virtual_address, + rex::thread::global_lock_t global_lock_locked_once, uint32_t virtual_address, uint32_t length, bool is_write, bool unwatch_exact_range, bool unprotect = true); // Allocates virtual memory from the 'system' heap. @@ -525,16 +542,19 @@ class Memory { private: #if REX_PLATFORM_MAC int MapViewsMac(); +#endif +#if REX_PLATFORM_PS5 + int MapViewsPs5(); // [ps5 port] guest memory in PS5 direct memory #endif int MapViews(uint8_t* mapping_base); void UnmapViews(); static uint32_t HostToGuestVirtualThunk(const void* context, const void* host_address); - bool AccessViolationCallback(std::unique_lock<std::recursive_mutex> global_lock_locked_once, + bool AccessViolationCallback(rex::thread::global_lock_t global_lock_locked_once, void* host_address, bool is_write); static bool AccessViolationCallbackThunk( - std::unique_lock<std::recursive_mutex> global_lock_locked_once, void* context, + rex::thread::global_lock_t global_lock_locked_once, void* context, void* host_address, bool is_write); std::filesystem::path file_name_; diff --git a/glue/rexglue-sdk-main/include/rex/system/xthread.h b/glue/rexglue-sdk-main/include/rex/system/xthread.h index 8fdfb270..336a347c 100644 --- a/glue/rexglue-sdk-main/include/rex/system/xthread.h +++ b/glue/rexglue-sdk-main/include/rex/system/xthread.h @@ -392,6 +392,10 @@ class XThread : public XObject { pending_mutant_acquires_.push_back(mutant); } + // [ps5 port] the guest stack's range [limit, base) (diagnostics) + uint32_t guest_stack_base() const { return stack_base_; } + uint32_t guest_stack_limit() const { return stack_limit_; } + protected: bool AllocateStack(uint32_t size); void FreeStack(); diff --git a/glue/rexglue-sdk-main/include/rex/thread.h b/glue/rexglue-sdk-main/include/rex/thread.h index 37919991..d28971db 100644 --- a/glue/rexglue-sdk-main/include/rex/thread.h +++ b/glue/rexglue-sdk-main/include/rex/thread.h @@ -202,6 +202,11 @@ class WaitHandle { // This value is platform specific. virtual void* native_handle() const = 0; + // [ps5 port] The platform's waitable object behind this handle (POSIX: its + // PosixWaitHandle), found without a dynamic_cast: a cross-cast walks the + // type info, comparing names, on every wait. Null where there's none. + virtual void* platform_wait_handle() { return nullptr; } + protected: WaitHandle() = default; }; diff --git a/glue/rexglue-sdk-main/include/rex/thread/fiber.h b/glue/rexglue-sdk-main/include/rex/thread/fiber.h index 953ab146..c986c402 100644 --- a/glue/rexglue-sdk-main/include/rex/thread/fiber.h +++ b/glue/rexglue-sdk-main/include/rex/thread/fiber.h @@ -20,7 +20,9 @@ // before including <ucontext.h>. #define _XOPEN_SOURCE 700 #endif +#if !REX_PLATFORM_PS5 // [ps5 port] the PS5's ucontext functions don't switch: fiber_posix.cpp has its own #include <ucontext.h> +#endif #include <cstdint> #include <vector> #endif @@ -67,7 +69,11 @@ struct Fiber { static void Trampoline(); #elif REX_PLATFORM_LINUX || REX_PLATFORM_MAC +#if REX_PLATFORM_PS5 + void* sp_ = nullptr; // [ps5 port] saved stack pointer (fiber_posix.cpp: x86-64 switch) +#else ucontext_t context_{}; +#endif // REX_PLATFORM_PS5 std::vector<uint8_t> stack_; void (*entry_)(void*) = nullptr; void* arg_ = nullptr; diff --git a/glue/rexglue-sdk-main/include/rex/thread/mutex.h b/glue/rexglue-sdk-main/include/rex/thread/mutex.h index f02059b9..41e38426 100644 --- a/glue/rexglue-sdk-main/include/rex/thread/mutex.h +++ b/glue/rexglue-sdk-main/include/rex/thread/mutex.h @@ -17,8 +17,80 @@ #include <immintrin.h> #endif +#if defined(__PROSPERO__) +#include <pthread.h> + +#include <atomic> +#include <cstdint> +#endif + namespace rex::thread { +#if defined(__PROSPERO__) +// [ps5 port] The global critical region's lock on the PS5: a recursive spin +// lock (owner thread + depth). The guest takes this lock constantly - every +// interrupt-disabled stretch and lwarx/stwcx. retry loop of the recompiled +// code enters and leaves it - and std::recursive_mutex costs the PS5's +// kernel a round trip each time; under contention its sleeping waiters wake +// late, and the whole game ran slow (frame rate fine, game speed off). +// Uncontended, this is a compare-exchange; contended, it spins, then +// yields, then naps 50 us at a time (mutex.cpp). +class global_recursive_mutex { + public: + void lock() { + const uintptr_t self = Self(); + if (owner_.load(std::memory_order_relaxed) == self) { + ++depth_; + return; + } + uintptr_t expected = 0; + if (!owner_.compare_exchange_strong(expected, self, std::memory_order_acquire, + std::memory_order_relaxed)) { + LockSlow(self); + } + depth_ = 1; + } + bool try_lock() { + const uintptr_t self = Self(); + if (owner_.load(std::memory_order_relaxed) == self) { + ++depth_; + return true; + } + uintptr_t expected = 0; + if (!owner_.compare_exchange_strong(expected, self, std::memory_order_acquire, + std::memory_order_relaxed)) { + return false; + } + depth_ = 1; + return true; + } + void unlock() { + if (owner_.load(std::memory_order_relaxed) != Self()) { + return; // (not this thread's: nothing to release) + } + if (--depth_ == 0) { + owner_.store(0, std::memory_order_release); + } + } + // The guest's MSR[EE] check: inside the region only on the thread holding it. + bool owned_by_current_thread() const { return owner_.load(std::memory_order_relaxed) == Self(); } + + private: + // (pthread_self(): 0.6b2 read the thread pointer at %fs:0 instead, and on + // the PS5 two threads could see the same value there - the lock let both in.) + static uintptr_t Self() { return reinterpret_cast<uintptr_t>(pthread_self()); } + void LockSlow(uintptr_t self); + + std::atomic<uintptr_t> owner_{0}; + uint32_t depth_ = 0; +}; +using global_mutex = global_recursive_mutex; +#else +using global_mutex = std::recursive_mutex; +#endif +// A held lock of the global critical region (callbacks that run inside it get one). +using global_lock_t = std::unique_lock<global_mutex>; + // The global critical region mutex singleton. // This must guard any operation that may suspend threads or be sensitive to // being suspended such as global table locks and such. @@ -59,13 +131,17 @@ namespace rex::thread { // }; class global_critical_region { public: - static std::recursive_mutex& mutex(); + static global_mutex& mutex(); // Locks, spinning briefly before blocking. Guest interrupt-disabled // sections last a few instructions; sleeping on the futex for one (and // waking the sleeper on unlock) costs far more than the wait. static void LockSpinning() { - std::recursive_mutex& global_mutex = mutex(); +#if defined(__PROSPERO__) + mutex().lock(); // [ps5 port] global_recursive_mutex spins on its own + return; +#endif + auto& global_mutex = mutex(); for (int i = 0; i < 128; ++i) { if (global_mutex.try_lock()) return; #if defined(__aarch64__) @@ -81,24 +157,24 @@ class global_critical_region { // Use this when keeping an instance is not possible. Otherwise, prefer // to keep an instance of global_critical_region near the members requiring // it to keep things readable. - static std::unique_lock<std::recursive_mutex> AcquireDirect() { - return std::unique_lock<std::recursive_mutex>(mutex()); + static std::unique_lock<global_mutex> AcquireDirect() { + return std::unique_lock<global_mutex>(mutex()); } // Acquires a lock on the global critical section. - inline std::unique_lock<std::recursive_mutex> Acquire() { - return std::unique_lock<std::recursive_mutex>(mutex()); + inline std::unique_lock<global_mutex> Acquire() { + return std::unique_lock<global_mutex>(mutex()); } // Acquires a deferred lock on the global critical section. - inline std::unique_lock<std::recursive_mutex> AcquireDeferred() { - return std::unique_lock<std::recursive_mutex>(mutex(), std::defer_lock); + inline std::unique_lock<global_mutex> AcquireDeferred() { + return std::unique_lock<global_mutex>(mutex(), std::defer_lock); } // Tries to acquire a lock on the glboal critical section. // Check owns_lock() to see if the lock was successfully acquired. - inline std::unique_lock<std::recursive_mutex> TryAcquire() { - return std::unique_lock<std::recursive_mutex>(mutex(), std::try_to_lock); + inline std::unique_lock<global_mutex> TryAcquire() { + return std::unique_lock<global_mutex>(mutex(), std::try_to_lock); } }; diff --git a/glue/rexglue-sdk-main/include/rex/ui/pad_dialog_style.h b/glue/rexglue-sdk-main/include/rex/ui/pad_dialog_style.h new file mode 100644 index 00000000..8f4f1848 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/ui/pad_dialog_style.h @@ -0,0 +1,36 @@ +/** + * [ps5 port] How the console's system dialogs look. A console has no mouse or + * keyboard: its on-screen keyboard (XamShowKeyboardUI) is driven by the + * controller. An app with a menu of its own (its fonts, its button pictures, + * its menu sounds) gives them here, and the keyboard is drawn in that style; + * unset, it is a plain ImGui window. + */ +#pragma once + +#include <cstdint> +#include <functional> +#include <map> + +#include <imgui.h> + +namespace rex::ui { + +struct PadDialogStyle { + ImFont* title = nullptr; // headings + ImFont* body = nullptr; // text + ImFont* bold = nullptr; // keys, labels + // The controller's button pictures, by Xbox button bit: 0x1000 Cross (A), + // 0x2000 Circle (B), 0x4000 Square (X), 0x8000 Triangle (Y), 0x0100 L1, + // 0x0200 R1, 0x0010 Options (Start), 0x000C the d-pad. (Missing: drawn.) + std::map<uint32_t, ImTextureID> buttons; + // Sounds: 0 move, 1 select, 2 back, 3 page, 4 on, 5 off, 6 start, 7 error. + std::function<void(int)> sound; + ImU32 accent = IM_COL32(226, 36, 46, 255); +}; + +// Sets the style (from the UI thread, before a dialog opens). +void SetPadDialogStyle(PadDialogStyle style); +// The style, or null when none was set (or it has no fonts). +const PadDialogStyle* GetPadDialogStyle(); + +} // namespace rex::ui diff --git a/glue/rexglue-sdk-main/include/rex/ui/presenter.h b/glue/rexglue-sdk-main/include/rex/ui/presenter.h index 586f51d3..ff6d6c09 100644 --- a/glue/rexglue-sdk-main/include/rex/ui/presenter.h +++ b/glue/rexglue-sdk-main/include/rex/ui/presenter.h @@ -368,6 +368,13 @@ class Presenter { Presenter& operator=(const Presenter& presenter) = delete; virtual ~Presenter(); +#if REX_PLATFORM_PS5 + // [ps5 port] From any thread: one paint of the frame already on the TV, + // past the guard that otherwise skips those while the game publishes (a + // refresh timed by ps5_display.cpp while the game is behind). + void RequestKeepAlivePaint(); +#endif + virtual Surface::TypeFlags GetSupportedSurfaceTypes() const = 0; // For calling from the Window for the Presenter attached to it. @@ -928,6 +935,10 @@ class Presenter { void PaintFromUIThreadImpl(bool force_paint, bool display_opportunity); bool ui_paint_pending_ = true; // UI owner; callbacks do not manufacture work. PublicationProgress publication_progress_; + // [ps5 port] When the game last published a frame (FramePacerNowNs), for + // telling a repaint of the frame already on the TV from one that's needed. + std::atomic<uint64_t> last_active_publication_ns_{0}; + std::atomic<bool> keepalive_requested_{false}; void HandleUIDrawersChangeFromUIThread(bool drawers_were_empty); void InvalidateGuestOutputTransform() const; diff --git a/glue/rexglue-sdk-main/include/rex/ui/surface.h b/glue/rexglue-sdk-main/include/rex/ui/surface.h index a45e67b2..1e0e9834 100644 --- a/glue/rexglue-sdk-main/include/rex/ui/surface.h +++ b/glue/rexglue-sdk-main/include/rex/ui/surface.h @@ -35,6 +35,10 @@ class Surface { kTypeIndex_Win32Hwnd, // macOS — CAMetalLayer presented via MoltenVK (VK_EXT_metal_surface). kTypeIndex_CAMetalLayer, + // macOS. + kTypeIndex_SDLMetalView, + // [ps5 port] PS5: the whole TV, a VK_KHR_display plane surface. + kTypeIndex_Ps5Display, }; using TypeFlags = uint32_t; enum : TypeFlags { @@ -42,6 +46,7 @@ class Surface { kTypeFlag_XcbWindow = TypeFlags(1) << kTypeIndex_XcbWindow, kTypeFlag_Win32Hwnd = TypeFlags(1) << kTypeIndex_Win32Hwnd, kTypeFlag_CAMetalLayer = TypeFlags(1) << kTypeIndex_CAMetalLayer, + kTypeFlag_Ps5Display = TypeFlags(1) << kTypeIndex_Ps5Display, // [ps5 port] }; Surface(const Surface& surface) = delete; diff --git a/glue/rexglue-sdk-main/include/rex/ui/surface_ps5.h b/glue/rexglue-sdk-main/include/rex/ui/surface_ps5.h new file mode 100644 index 00000000..61d85885 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/ui/surface_ps5.h @@ -0,0 +1,37 @@ +#pragma once +/** + * [ps5 port] PS5: the TV, through VK_KHR_display (PS5_Mesa's RADV winsys). + * There is no window: the surface is the whole display, sized by the display + * mode the presenter picks (4K when the TV offers it). + */ + +#include <cstdint> + +#include <rex/ui/surface.h> + +namespace rex { +namespace ui { + +class Ps5DisplaySurface final : public Surface { + public: + Ps5DisplaySurface(uint32_t width, uint32_t height) : width_(width), height_(height) {} + TypeIndex GetType() const override { return kTypeIndex_Ps5Display; } + void SetSize(uint32_t width, uint32_t height) { + width_ = width; + height_ = height; + } + + protected: + bool GetSizeImpl(uint32_t& width_out, uint32_t& height_out) const override { + width_out = width_; + height_out = height_; + return true; + } + + private: + uint32_t width_; + uint32_t height_; +}; + +} // namespace ui +} // namespace rex diff --git a/glue/rexglue-sdk-main/include/rex/ui/vulkan/device.h b/glue/rexglue-sdk-main/include/rex/ui/vulkan/device.h index 984e5e23..81b3132a 100644 --- a/glue/rexglue-sdk-main/include/rex/ui/vulkan/device.h +++ b/glue/rexglue-sdk-main/include/rex/ui/vulkan/device.h @@ -24,6 +24,10 @@ namespace vulkan { // TEMP (Android perf investigation): per-frame averages of counted Vulkan commands // since the previous call. std::string ConsumeCommandCounters(uint32_t frames); +// [ps5 port] Reuse counts of the device-memory recycler (PS5 only). +std::string MemoryRecyclerSummary(); +// Gives back everything the recycler holds (PS5 only; no-op elsewhere). +void MemoryRecyclerReleaseAll(); class VulkanDevice { public: diff --git a/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/device_1_0.inc b/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/device_1_0.inc index 54d0f323..c5c28605 100644 --- a/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/device_1_0.inc +++ b/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/device_1_0.inc @@ -85,6 +85,7 @@ XE_UI_VULKAN_FUNCTION(vkGetPipelineCacheData) XE_UI_VULKAN_FUNCTION(vkGetQueryPoolResults) XE_UI_VULKAN_FUNCTION(vkInvalidateMappedMemoryRanges) XE_UI_VULKAN_FUNCTION(vkMapMemory) +XE_UI_VULKAN_FUNCTION(vkMergePipelineCaches) XE_UI_VULKAN_FUNCTION(vkResetCommandPool) XE_UI_VULKAN_FUNCTION(vkResetDescriptorPool) XE_UI_VULKAN_FUNCTION(vkResetFences) diff --git a/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/instance_khr_display.inc b/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/instance_khr_display.inc new file mode 100644 index 00000000..4df17f05 --- /dev/null +++ b/glue/rexglue-sdk-main/include/rex/ui/vulkan/functions/instance_khr_display.inc @@ -0,0 +1,7 @@ +// [ps5 port] VK_KHR_display functions: the PS5 presents to a display plane. +XE_UI_VULKAN_FUNCTION(vkGetPhysicalDeviceDisplayPropertiesKHR) +XE_UI_VULKAN_FUNCTION(vkGetPhysicalDeviceDisplayPlanePropertiesKHR) +XE_UI_VULKAN_FUNCTION(vkGetDisplayPlaneSupportedDisplaysKHR) +XE_UI_VULKAN_FUNCTION(vkGetDisplayModePropertiesKHR) +XE_UI_VULKAN_FUNCTION(vkGetDisplayPlaneCapabilitiesKHR) +XE_UI_VULKAN_FUNCTION(vkCreateDisplayPlaneSurfaceKHR) diff --git a/glue/rexglue-sdk-main/include/rex/ui/vulkan/instance.h b/glue/rexglue-sdk-main/include/rex/ui/vulkan/instance.h index 1b560bd3..2978b54d 100644 --- a/glue/rexglue-sdk-main/include/rex/ui/vulkan/instance.h +++ b/glue/rexglue-sdk-main/include/rex/ui/vulkan/instance.h @@ -71,6 +71,10 @@ class VulkanInstance { // VK_EXT_metal_surface (#217) #ifdef VK_USE_PLATFORM_METAL_EXT #include <rex/ui/vulkan/functions/instance_ext_metal_surface.inc> +#endif + // VK_KHR_display (#3) - [ps5 port] +#if REX_PLATFORM_PS5 +#include <rex/ui/vulkan/functions/instance_khr_display.inc> #endif // VK_KHR_get_physical_device_properties2 (#60, promoted to 1.1) #include <rex/ui/vulkan/functions/instance_1_1_khr_get_physical_device_properties2.inc> @@ -99,6 +103,9 @@ class VulkanInstance { #endif #ifdef VK_USE_PLATFORM_METAL_EXT bool ext_EXT_metal_surface = false; // #217 +#endif +#if REX_PLATFORM_PS5 + bool ext_KHR_display = false; // #3 [ps5 port] #endif bool ext_1_1_KHR_get_physical_device_properties2 = false; // #60 bool ext_EXT_debug_utils = false; // #129 diff --git a/glue/rexglue-sdk-main/patches/ffmpeg-ps5.patch b/glue/rexglue-sdk-main/patches/ffmpeg-ps5.patch new file mode 100644 index 00000000..3a63d8d3 --- /dev/null +++ b/glue/rexglue-sdk-main/patches/ffmpeg-ps5.patch @@ -0,0 +1,2727 @@ +diff --git a/config.h b/config.h +index b6a504a..0a1d111 100644 +--- a/config.h ++++ b/config.h +@@ -16,6 +16,8 @@ + #else + #include "config_linux_x86_64.h" + #endif ++#elif defined(__PROSPERO__) // [ps5 port] ++#include "config_ps5_x86_64.h" + #elif defined(__ANDROID__) + #if defined(__aarch64__) + #include "config_android_aarch64.h" +diff --git a/config_ps5_x86_64.h b/config_ps5_x86_64.h +new file mode 100644 +index 0000000..2dbb5d3 +--- /dev/null ++++ b/config_ps5_x86_64.h +@@ -0,0 +1,2708 @@ ++/* [ps5 port] config_android_x86_64.h adapted for the PS5 (FreeBSD-based, x86-64, no external asm). */ ++/* Automatically generated by configure - do not modify! */ ++#ifndef FFMPEG_CONFIG_H ++#define FFMPEG_CONFIG_H ++#define FFMPEG_CONFIGURATION "--cc=/home/joel/Documents/dev/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android24-clang --enable-cross-compile --arch=x86_64 --target-os=android --disable-everything --disable-programs --disable-all --disable-x86asm --disable-autodetect --disable-network --enable-avcodec --enable-avformat --enable-avutil --enable-decoder='mp3,mp3float,wmav2,xmaframes' --enable-parser=mpegaudio --enable-demuxer='asf,mp3' --enable-protocol=file" ++#define FFMPEG_LICENSE "LGPL version 2.1 or later" ++#define CONFIG_THIS_YEAR 2021 ++#define FFMPEG_DATADIR "/usr/local/share/ffmpeg" ++#define AVCONV_DATADIR "/usr/local/share/ffmpeg" ++#define CC_IDENT "Android (7284624, based on r416183b) clang version 12.0.5 (https://android.googlesource.com/toolchain/llvm-project c935d99d7cf2016289302412d708641d52d2f7ee)" ++#define OS_NAME ps5 ++#define av_restrict restrict ++#define EXTERN_PREFIX "" ++#define EXTERN_ASM ++#define BUILDSUF "" ++#define SLIBSUF ".so" ++#define HAVE_MMX2 HAVE_MMXEXT ++#define SWS_MAX_FILTER_SIZE 256 ++#define ARCH_AARCH64 0 ++#define ARCH_ALPHA 0 ++#define ARCH_ARM 0 ++#define ARCH_AVR32 0 ++#define ARCH_AVR32_AP 0 ++#define ARCH_AVR32_UC 0 ++#define ARCH_BFIN 0 ++#define ARCH_IA64 0 ++#define ARCH_M68K 0 ++#define ARCH_MIPS 0 ++#define ARCH_MIPS64 0 ++#define ARCH_PARISC 0 ++#define ARCH_PPC 0 ++#define ARCH_PPC64 0 ++#define ARCH_S390 0 ++#define ARCH_SH4 0 ++#define ARCH_SPARC 0 ++#define ARCH_SPARC64 0 ++#define ARCH_TILEGX 0 ++#define ARCH_TILEPRO 0 ++#define ARCH_TOMI 0 ++#define ARCH_X86 1 ++#define ARCH_X86_32 0 ++#define ARCH_X86_64 1 ++#define HAVE_ARMV5TE 0 ++#define HAVE_ARMV6 0 ++#define HAVE_ARMV6T2 0 ++#define HAVE_ARMV8 0 ++#define HAVE_NEON 0 ++#define HAVE_VFP 0 ++#define HAVE_VFPV3 0 ++#define HAVE_SETEND 0 ++#define HAVE_ALTIVEC 0 ++#define HAVE_DCBZL 0 ++#define HAVE_LDBRX 0 ++#define HAVE_POWER8 0 ++#define HAVE_PPC4XX 0 ++#define HAVE_VSX 0 ++#define HAVE_AESNI 1 ++#define HAVE_AMD3DNOW 1 ++#define HAVE_AMD3DNOWEXT 1 ++#define HAVE_AVX 1 ++#define HAVE_AVX2 1 ++#define HAVE_AVX512 1 ++#define HAVE_FMA3 1 ++#define HAVE_FMA4 1 ++#define HAVE_MMX 1 ++#define HAVE_MMXEXT 1 ++#define HAVE_SSE 1 ++#define HAVE_SSE2 1 ++#define HAVE_SSE3 1 ++#define HAVE_SSE4 1 ++#define HAVE_SSE42 1 ++#define HAVE_SSSE3 1 ++#define HAVE_XOP 1 ++#define HAVE_CPUNOP 1 ++#define HAVE_I686 1 ++#define HAVE_MIPSFPU 0 ++#define HAVE_MIPS32R2 0 ++#define HAVE_MIPS32R5 0 ++#define HAVE_MIPS64R2 0 ++#define HAVE_MIPS32R6 0 ++#define HAVE_MIPS64R6 0 ++#define HAVE_MIPSDSP 0 ++#define HAVE_MIPSDSPR2 0 ++#define HAVE_MSA 0 ++#define HAVE_MSA2 0 ++#define HAVE_LOONGSON2 0 ++#define HAVE_LOONGSON3 0 ++#define HAVE_MMI 0 ++#define HAVE_ARMV5TE_EXTERNAL 0 ++#define HAVE_ARMV6_EXTERNAL 0 ++#define HAVE_ARMV6T2_EXTERNAL 0 ++#define HAVE_ARMV8_EXTERNAL 0 ++#define HAVE_NEON_EXTERNAL 0 ++#define HAVE_VFP_EXTERNAL 0 ++#define HAVE_VFPV3_EXTERNAL 0 ++#define HAVE_SETEND_EXTERNAL 0 ++#define HAVE_ALTIVEC_EXTERNAL 0 ++#define HAVE_DCBZL_EXTERNAL 0 ++#define HAVE_LDBRX_EXTERNAL 0 ++#define HAVE_POWER8_EXTERNAL 0 ++#define HAVE_PPC4XX_EXTERNAL 0 ++#define HAVE_VSX_EXTERNAL 0 ++#define HAVE_AESNI_EXTERNAL 0 ++#define HAVE_AMD3DNOW_EXTERNAL 0 ++#define HAVE_AMD3DNOWEXT_EXTERNAL 0 ++#define HAVE_AVX_EXTERNAL 0 ++#define HAVE_AVX2_EXTERNAL 0 ++#define HAVE_AVX512_EXTERNAL 0 ++#define HAVE_FMA3_EXTERNAL 0 ++#define HAVE_FMA4_EXTERNAL 0 ++#define HAVE_MMX_EXTERNAL 0 ++#define HAVE_MMXEXT_EXTERNAL 0 ++#define HAVE_SSE_EXTERNAL 0 ++#define HAVE_SSE2_EXTERNAL 0 ++#define HAVE_SSE3_EXTERNAL 0 ++#define HAVE_SSE4_EXTERNAL 0 ++#define HAVE_SSE42_EXTERNAL 0 ++#define HAVE_SSSE3_EXTERNAL 0 ++#define HAVE_XOP_EXTERNAL 0 ++#define HAVE_CPUNOP_EXTERNAL 0 ++#define HAVE_I686_EXTERNAL 0 ++#define HAVE_MIPSFPU_EXTERNAL 0 ++#define HAVE_MIPS32R2_EXTERNAL 0 ++#define HAVE_MIPS32R5_EXTERNAL 0 ++#define HAVE_MIPS64R2_EXTERNAL 0 ++#define HAVE_MIPS32R6_EXTERNAL 0 ++#define HAVE_MIPS64R6_EXTERNAL 0 ++#define HAVE_MIPSDSP_EXTERNAL 0 ++#define HAVE_MIPSDSPR2_EXTERNAL 0 ++#define HAVE_MSA_EXTERNAL 0 ++#define HAVE_MSA2_EXTERNAL 0 ++#define HAVE_LOONGSON2_EXTERNAL 0 ++#define HAVE_LOONGSON3_EXTERNAL 0 ++#define HAVE_MMI_EXTERNAL 0 ++#define HAVE_ARMV5TE_INLINE 0 ++#define HAVE_ARMV6_INLINE 0 ++#define HAVE_ARMV6T2_INLINE 0 ++#define HAVE_ARMV8_INLINE 0 ++#define HAVE_NEON_INLINE 0 ++#define HAVE_VFP_INLINE 0 ++#define HAVE_VFPV3_INLINE 0 ++#define HAVE_SETEND_INLINE 0 ++#define HAVE_ALTIVEC_INLINE 0 ++#define HAVE_DCBZL_INLINE 0 ++#define HAVE_LDBRX_INLINE 0 ++#define HAVE_POWER8_INLINE 0 ++#define HAVE_PPC4XX_INLINE 0 ++#define HAVE_VSX_INLINE 0 ++#define HAVE_AESNI_INLINE 1 ++#define HAVE_AMD3DNOW_INLINE 1 ++#define HAVE_AMD3DNOWEXT_INLINE 1 ++#define HAVE_AVX_INLINE 1 ++#define HAVE_AVX2_INLINE 1 ++#define HAVE_AVX512_INLINE 1 ++#define HAVE_FMA3_INLINE 1 ++#define HAVE_FMA4_INLINE 1 ++#define HAVE_MMX_INLINE 1 ++#define HAVE_MMXEXT_INLINE 1 ++#define HAVE_SSE_INLINE 1 ++#define HAVE_SSE2_INLINE 1 ++#define HAVE_SSE3_INLINE 1 ++#define HAVE_SSE4_INLINE 1 ++#define HAVE_SSE42_INLINE 1 ++#define HAVE_SSSE3_INLINE 1 ++#define HAVE_XOP_INLINE 1 ++#define HAVE_CPUNOP_INLINE 0 ++#define HAVE_I686_INLINE 0 ++#define HAVE_MIPSFPU_INLINE 0 ++#define HAVE_MIPS32R2_INLINE 0 ++#define HAVE_MIPS32R5_INLINE 0 ++#define HAVE_MIPS64R2_INLINE 0 ++#define HAVE_MIPS32R6_INLINE 0 ++#define HAVE_MIPS64R6_INLINE 0 ++#define HAVE_MIPSDSP_INLINE 0 ++#define HAVE_MIPSDSPR2_INLINE 0 ++#define HAVE_MSA_INLINE 0 ++#define HAVE_MSA2_INLINE 0 ++#define HAVE_LOONGSON2_INLINE 0 ++#define HAVE_LOONGSON3_INLINE 0 ++#define HAVE_MMI_INLINE 0 ++#define HAVE_ALIGNED_STACK 1 ++#define HAVE_FAST_64BIT 1 ++#define HAVE_FAST_CLZ 1 ++#define HAVE_FAST_CMOV 1 ++#define HAVE_LOCAL_ALIGNED 1 ++#define HAVE_SIMD_ALIGN_16 1 ++#define HAVE_SIMD_ALIGN_32 1 ++#define HAVE_SIMD_ALIGN_64 1 ++#define HAVE_ATOMIC_CAS_PTR 0 ++#define HAVE_MACHINE_RW_BARRIER 0 ++#define HAVE_MEMORYBARRIER 0 ++#define HAVE_MM_EMPTY 1 ++#define HAVE_RDTSC 0 ++#define HAVE_SEM_TIMEDWAIT 0 ++#define HAVE_SYNC_VAL_COMPARE_AND_SWAP 1 ++#define HAVE_CABS 0 ++#define HAVE_CEXP 0 ++#define HAVE_INLINE_ASM 1 ++#define HAVE_SYMVER 0 ++#define HAVE_X86ASM 0 ++#define HAVE_BIGENDIAN 0 ++#define HAVE_FAST_UNALIGNED 1 ++#define HAVE_ARPA_INET_H 1 ++#define HAVE_ASM_TYPES_H 0 ++#define HAVE_CDIO_PARANOIA_H 0 ++#define HAVE_CDIO_PARANOIA_PARANOIA_H 0 ++#define HAVE_CUDA_H 0 ++#define HAVE_DISPATCH_DISPATCH_H 0 ++#define HAVE_DEV_BKTR_IOCTL_BT848_H 0 ++#define HAVE_DEV_BKTR_IOCTL_METEOR_H 0 ++#define HAVE_DEV_IC_BT8XX_H 0 ++#define HAVE_DEV_VIDEO_BKTR_IOCTL_BT848_H 0 ++#define HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H 0 ++#define HAVE_DIRECT_H 0 ++#define HAVE_DIRENT_H 1 ++#define HAVE_DXGIDEBUG_H 0 ++#define HAVE_DXVA_H 0 ++#define HAVE_ES2_GL_H 0 ++#define HAVE_GSM_H 0 ++#define HAVE_IO_H 0 ++#define HAVE_LINUX_DMA_BUF_H 0 ++#define HAVE_LINUX_PERF_EVENT_H 1 ++#define HAVE_MACHINE_IOCTL_BT848_H 0 ++#define HAVE_MACHINE_IOCTL_METEOR_H 0 ++#define HAVE_MALLOC_H 0 ++#define HAVE_OPENCV2_CORE_CORE_C_H 0 ++#define HAVE_OPENGL_GL3_H 0 ++#define HAVE_POLL_H 1 ++#define HAVE_SYS_PARAM_H 1 ++#define HAVE_SYS_RESOURCE_H 1 ++#define HAVE_SYS_SELECT_H 1 ++#define HAVE_SYS_SOUNDCARD_H 0 ++#define HAVE_SYS_TIME_H 1 ++#define HAVE_SYS_UN_H 1 ++#define HAVE_SYS_VIDEOIO_H 0 ++#define HAVE_TERMIOS_H 0 ++#define HAVE_UDPLITE_H 0 ++#define HAVE_UNISTD_H 1 ++#define HAVE_VALGRIND_VALGRIND_H 0 ++#define HAVE_WINDOWS_H 0 ++#define HAVE_WINSOCK2_H 0 ++#define HAVE_INTRINSICS_NEON 0 ++#define HAVE_ATANF 1 ++#define HAVE_ATAN2F 1 ++#define HAVE_CBRT 1 ++#define HAVE_CBRTF 1 ++#define HAVE_COPYSIGN 1 ++#define HAVE_COSF 1 ++#define HAVE_ERF 1 ++#define HAVE_EXP2 1 ++#define HAVE_EXP2F 1 ++#define HAVE_EXPF 1 ++#define HAVE_HYPOT 1 ++#define HAVE_ISFINITE 1 ++#define HAVE_ISINF 1 ++#define HAVE_ISNAN 1 ++#define HAVE_LDEXPF 1 ++#define HAVE_LLRINT 1 ++#define HAVE_LLRINTF 1 ++#define HAVE_LOG2 1 ++#define HAVE_LOG2F 1 ++#define HAVE_LOG10F 1 ++#define HAVE_LRINT 1 ++#define HAVE_LRINTF 1 ++#define HAVE_POWF 1 ++#define HAVE_RINT 1 ++#define HAVE_ROUND 1 ++#define HAVE_ROUNDF 1 ++#define HAVE_SINF 1 ++#define HAVE_TRUNC 1 ++#define HAVE_TRUNCF 1 ++#define HAVE_DOS_PATHS 0 ++#define HAVE_LIBC_MSVCRT 0 ++#define HAVE_MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS 0 ++#define HAVE_SECTION_DATA_REL_RO 1 ++#define HAVE_THREADS 1 ++#define HAVE_UWP 0 ++#define HAVE_WINRT 0 ++#define HAVE_ACCESS 1 ++#define HAVE_ALIGNED_MALLOC 0 ++#define HAVE_ARC4RANDOM 0 ++#define HAVE_CLOCK_GETTIME 1 ++#define HAVE_CLOSESOCKET 0 ++#define HAVE_COMMANDLINETOARGVW 0 ++#define HAVE_FCNTL 1 ++#define HAVE_GETADDRINFO 1 ++#define HAVE_GETHRTIME 0 ++#define HAVE_GETOPT 1 ++#define HAVE_GETMODULEHANDLE 0 ++#define HAVE_GETPROCESSAFFINITYMASK 0 ++#define HAVE_GETPROCESSMEMORYINFO 0 ++#define HAVE_GETPROCESSTIMES 0 ++#define HAVE_GETRUSAGE 0 ++#define HAVE_GETSTDHANDLE 0 ++#define HAVE_GETSYSTEMTIMEASFILETIME 0 ++#define HAVE_GETTIMEOFDAY 1 ++#define HAVE_GLOB 0 ++#define HAVE_GLXGETPROCADDRESS 0 ++#define HAVE_GMTIME_R 1 ++#define HAVE_INET_ATON 1 ++#define HAVE_ISATTY 1 ++#define HAVE_KBHIT 0 ++#define HAVE_LOCALTIME_R 1 ++#define HAVE_LSTAT 1 ++#define HAVE_LZO1X_999_COMPRESS 0 ++#define HAVE_MACH_ABSOLUTE_TIME 0 ++#define HAVE_MAPVIEWOFFILE 0 ++#define HAVE_MEMALIGN 0 ++#define HAVE_MKSTEMP 1 ++#define HAVE_MMAP 1 ++#define HAVE_MPROTECT 1 ++#define HAVE_NANOSLEEP 1 ++#define HAVE_PEEKNAMEDPIPE 0 ++#define HAVE_POSIX_MEMALIGN 1 ++#define HAVE_PTHREAD_CANCEL 0 ++#define HAVE_SCHED_GETAFFINITY 0 ++#define HAVE_SECITEMIMPORT 0 ++#define HAVE_SETCONSOLETEXTATTRIBUTE 0 ++#define HAVE_SETCONSOLECTRLHANDLER 0 ++#define HAVE_SETDLLDIRECTORY 0 ++#define HAVE_SETMODE 0 ++#define HAVE_SETRLIMIT 1 ++#define HAVE_SLEEP 0 ++#define HAVE_STRERROR_R 1 ++#define HAVE_SYSCONF 1 ++#define HAVE_SYSCTL 0 ++#define HAVE_USLEEP 1 ++#define HAVE_UTGETOSTYPEFROMSTRING 0 ++#define HAVE_VIRTUALALLOC 0 ++#define HAVE_WGLGETPROCADDRESS 0 ++#define HAVE_BCRYPT 0 ++#define HAVE_VAAPI_DRM 0 ++#define HAVE_VAAPI_X11 0 ++#define HAVE_VDPAU_X11 0 ++#define HAVE_PTHREADS 1 ++#define HAVE_OS2THREADS 0 ++#define HAVE_W32THREADS 0 ++#define HAVE_AS_ARCH_DIRECTIVE 0 ++#define HAVE_AS_DN_DIRECTIVE 0 ++#define HAVE_AS_FPU_DIRECTIVE 0 ++#define HAVE_AS_FUNC 0 ++#define HAVE_AS_OBJECT_ARCH 0 ++#define HAVE_ASM_MOD_Q 0 ++#define HAVE_BLOCKS_EXTENSION 0 ++#define HAVE_EBP_AVAILABLE 1 ++#define HAVE_EBX_AVAILABLE 1 ++#define HAVE_GNU_AS 0 ++#define HAVE_GNU_WINDRES 0 ++#define HAVE_IBM_ASM 0 ++#define HAVE_INLINE_ASM_DIRECT_SYMBOL_REFS 1 ++#define HAVE_INLINE_ASM_LABELS 1 ++#define HAVE_INLINE_ASM_NONLOCAL_LABELS 1 ++#define HAVE_PRAGMA_DEPRECATED 1 ++#define HAVE_RSYNC_CONTIMEOUT 1 ++#define HAVE_SYMVER_ASM_LABEL 1 ++#define HAVE_SYMVER_GNU_ASM 1 ++#define HAVE_VFP_ARGS 0 ++#define HAVE_XFORM_ASM 0 ++#define HAVE_XMM_CLOBBERS 1 ++#define HAVE_KCMVIDEOCODECTYPE_HEVC 0 ++#define HAVE_KCMVIDEOCODECTYPE_HEVCWITHALPHA 0 ++#define HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE 0 ++#define HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ 0 ++#define HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG 0 ++#define HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR 0 ++#define HAVE_SOCKLEN_T 1 ++#define HAVE_STRUCT_ADDRINFO 1 ++#define HAVE_STRUCT_GROUP_SOURCE_REQ 1 ++#define HAVE_STRUCT_IP_MREQ_SOURCE 1 ++#define HAVE_STRUCT_IPV6_MREQ 1 ++#define HAVE_STRUCT_MSGHDR_MSG_FLAGS 1 ++#define HAVE_STRUCT_POLLFD 1 ++#define HAVE_STRUCT_RUSAGE_RU_MAXRSS 1 ++#define HAVE_STRUCT_SCTP_EVENT_SUBSCRIBE 0 ++#define HAVE_STRUCT_SOCKADDR_IN6 1 ++#define HAVE_STRUCT_SOCKADDR_SA_LEN 0 ++#define HAVE_STRUCT_SOCKADDR_STORAGE 1 ++#define HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 1 ++#define HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE 0 ++#define HAVE_LIBDRM_GETFB2 0 ++#define HAVE_MAKEINFO 0 ++#define HAVE_MAKEINFO_HTML 0 ++#define HAVE_OPENCL_D3D11 0 ++#define HAVE_OPENCL_DRM_ARM 0 ++#define HAVE_OPENCL_DRM_BEIGNET 0 ++#define HAVE_OPENCL_DXVA2 0 ++#define HAVE_OPENCL_VAAPI_BEIGNET 0 ++#define HAVE_OPENCL_VAAPI_INTEL_MEDIA 0 ++#define HAVE_PERL 1 ++#define HAVE_POD2MAN 1 ++#define HAVE_TEXI2HTML 0 ++#define CONFIG_DOC 0 ++#define CONFIG_HTMLPAGES 0 ++#define CONFIG_MANPAGES 1 ++#define CONFIG_PODPAGES 1 ++#define CONFIG_TXTPAGES 0 ++#define CONFIG_AVIO_LIST_DIR_EXAMPLE 1 ++#define CONFIG_AVIO_READING_EXAMPLE 1 ++#define CONFIG_DECODE_AUDIO_EXAMPLE 1 ++#define CONFIG_DECODE_VIDEO_EXAMPLE 1 ++#define CONFIG_DEMUXING_DECODING_EXAMPLE 0 ++#define CONFIG_ENCODE_AUDIO_EXAMPLE 1 ++#define CONFIG_ENCODE_VIDEO_EXAMPLE 1 ++#define CONFIG_EXTRACT_MVS_EXAMPLE 0 ++#define CONFIG_FILTER_AUDIO_EXAMPLE 0 ++#define CONFIG_FILTERING_AUDIO_EXAMPLE 0 ++#define CONFIG_FILTERING_VIDEO_EXAMPLE 0 ++#define CONFIG_HTTP_MULTICLIENT_EXAMPLE 0 ++#define CONFIG_HW_DECODE_EXAMPLE 0 ++#define CONFIG_METADATA_EXAMPLE 0 ++#define CONFIG_MUXING_EXAMPLE 0 ++#define CONFIG_QSVDEC_EXAMPLE 0 ++#define CONFIG_REMUXING_EXAMPLE 0 ++#define CONFIG_RESAMPLING_AUDIO_EXAMPLE 0 ++#define CONFIG_SCALING_VIDEO_EXAMPLE 0 ++#define CONFIG_TRANSCODE_AAC_EXAMPLE 0 ++#define CONFIG_TRANSCODING_EXAMPLE 0 ++#define CONFIG_VAAPI_ENCODE_EXAMPLE 0 ++#define CONFIG_VAAPI_TRANSCODE_EXAMPLE 0 ++#define CONFIG_AVISYNTH 0 ++#define CONFIG_FREI0R 0 ++#define CONFIG_LIBCDIO 0 ++#define CONFIG_LIBDAVS2 0 ++#define CONFIG_LIBRUBBERBAND 0 ++#define CONFIG_LIBVIDSTAB 0 ++#define CONFIG_LIBX264 0 ++#define CONFIG_LIBX265 0 ++#define CONFIG_LIBXAVS 0 ++#define CONFIG_LIBXAVS2 0 ++#define CONFIG_LIBXVID 0 ++#define CONFIG_DECKLINK 0 ++#define CONFIG_LIBFDK_AAC 0 ++#define CONFIG_OPENSSL 0 ++#define CONFIG_LIBTLS 0 ++#define CONFIG_GMP 0 ++#define CONFIG_LIBARIBB24 0 ++#define CONFIG_LIBLENSFUN 0 ++#define CONFIG_LIBOPENCORE_AMRNB 0 ++#define CONFIG_LIBOPENCORE_AMRWB 0 ++#define CONFIG_LIBVO_AMRWBENC 0 ++#define CONFIG_MBEDTLS 0 ++#define CONFIG_RKMPP 0 ++#define CONFIG_LIBSMBCLIENT 0 ++#define CONFIG_CHROMAPRINT 0 ++#define CONFIG_GCRYPT 0 ++#define CONFIG_GNUTLS 0 ++#define CONFIG_JNI 0 ++#define CONFIG_LADSPA 0 ++#define CONFIG_LIBAOM 0 ++#define CONFIG_LIBASS 0 ++#define CONFIG_LIBBLURAY 0 ++#define CONFIG_LIBBS2B 0 ++#define CONFIG_LIBCACA 0 ++#define CONFIG_LIBCELT 0 ++#define CONFIG_LIBCODEC2 0 ++#define CONFIG_LIBDAV1D 0 ++#define CONFIG_LIBDC1394 0 ++#define CONFIG_LIBDRM 0 ++#define CONFIG_LIBFLITE 0 ++#define CONFIG_LIBFONTCONFIG 0 ++#define CONFIG_LIBFREETYPE 0 ++#define CONFIG_LIBFRIBIDI 0 ++#define CONFIG_LIBGLSLANG 0 ++#define CONFIG_LIBGME 0 ++#define CONFIG_LIBGSM 0 ++#define CONFIG_LIBIEC61883 0 ++#define CONFIG_LIBILBC 0 ++#define CONFIG_LIBJACK 0 ++#define CONFIG_LIBKLVANC 0 ++#define CONFIG_LIBKVAZAAR 0 ++#define CONFIG_LIBMODPLUG 0 ++#define CONFIG_LIBMP3LAME 0 ++#define CONFIG_LIBMYSOFA 0 ++#define CONFIG_LIBOPENCV 0 ++#define CONFIG_LIBOPENH264 0 ++#define CONFIG_LIBOPENJPEG 0 ++#define CONFIG_LIBOPENMPT 0 ++#define CONFIG_LIBOPENVINO 0 ++#define CONFIG_LIBOPUS 0 ++#define CONFIG_LIBPULSE 0 ++#define CONFIG_LIBRABBITMQ 0 ++#define CONFIG_LIBRAV1E 0 ++#define CONFIG_LIBRIST 0 ++#define CONFIG_LIBRSVG 0 ++#define CONFIG_LIBRTMP 0 ++#define CONFIG_LIBSHINE 0 ++#define CONFIG_LIBSMBCLIENT 0 ++#define CONFIG_LIBSNAPPY 0 ++#define CONFIG_LIBSOXR 0 ++#define CONFIG_LIBSPEEX 0 ++#define CONFIG_LIBSRT 0 ++#define CONFIG_LIBSSH 0 ++#define CONFIG_LIBSVTAV1 0 ++#define CONFIG_LIBTENSORFLOW 0 ++#define CONFIG_LIBTESSERACT 0 ++#define CONFIG_LIBTHEORA 0 ++#define CONFIG_LIBTWOLAME 0 ++#define CONFIG_LIBUAVS3D 0 ++#define CONFIG_LIBV4L2 0 ++#define CONFIG_LIBVMAF 0 ++#define CONFIG_LIBVORBIS 0 ++#define CONFIG_LIBVPX 0 ++#define CONFIG_LIBWEBP 0 ++#define CONFIG_LIBXML2 0 ++#define CONFIG_LIBZIMG 0 ++#define CONFIG_LIBZMQ 0 ++#define CONFIG_LIBZVBI 0 ++#define CONFIG_LV2 0 ++#define CONFIG_MEDIACODEC 0 ++#define CONFIG_OPENAL 0 ++#define CONFIG_OPENGL 0 ++#define CONFIG_POCKETSPHINX 0 ++#define CONFIG_VAPOURSYNTH 0 ++#define CONFIG_ALSA 0 ++#define CONFIG_APPKIT 0 ++#define CONFIG_AVFOUNDATION 0 ++#define CONFIG_BZLIB 0 ++#define CONFIG_COREIMAGE 0 ++#define CONFIG_ICONV 0 ++#define CONFIG_LIBXCB 0 ++#define CONFIG_LIBXCB_SHM 0 ++#define CONFIG_LIBXCB_SHAPE 0 ++#define CONFIG_LIBXCB_XFIXES 0 ++#define CONFIG_LZMA 0 ++#define CONFIG_MEDIAFOUNDATION 0 ++#define CONFIG_SCHANNEL 0 ++#define CONFIG_SDL2 0 ++#define CONFIG_SECURETRANSPORT 0 ++#define CONFIG_SNDIO 0 ++#define CONFIG_XLIB 0 ++#define CONFIG_ZLIB 0 ++#define CONFIG_CUDA_NVCC 0 ++#define CONFIG_CUDA_SDK 0 ++#define CONFIG_LIBNPP 0 ++#define CONFIG_LIBMFX 0 ++#define CONFIG_MMAL 0 ++#define CONFIG_OMX 0 ++#define CONFIG_OPENCL 0 ++#define CONFIG_VULKAN 0 ++#define CONFIG_AMF 0 ++#define CONFIG_AUDIOTOOLBOX 0 ++#define CONFIG_CRYSTALHD 0 ++#define CONFIG_CUDA 0 ++#define CONFIG_CUDA_LLVM 0 ++#define CONFIG_CUVID 0 ++#define CONFIG_D3D11VA 0 ++#define CONFIG_DXVA2 0 ++#define CONFIG_FFNVCODEC 0 ++#define CONFIG_NVDEC 0 ++#define CONFIG_NVENC 0 ++#define CONFIG_VAAPI 0 ++#define CONFIG_VDPAU 0 ++#define CONFIG_VIDEOTOOLBOX 0 ++#define CONFIG_V4L2_M2M 0 ++#define CONFIG_XVMC 0 ++#define CONFIG_FTRAPV 0 ++#define CONFIG_GRAY 0 ++#define CONFIG_HARDCODED_TABLES 0 ++#define CONFIG_OMX_RPI 0 ++#define CONFIG_RUNTIME_CPUDETECT 1 ++#define CONFIG_SAFE_BITSTREAM_READER 1 ++#define CONFIG_SHARED 0 ++#define CONFIG_SMALL 0 ++#define CONFIG_STATIC 1 ++#define CONFIG_SWSCALE_ALPHA 1 ++#define CONFIG_GPL 0 ++#define CONFIG_NONFREE 0 ++#define CONFIG_VERSION3 0 ++#define CONFIG_AVDEVICE 0 ++#define CONFIG_AVFILTER 0 ++#define CONFIG_SWSCALE 0 ++#define CONFIG_POSTPROC 0 ++#define CONFIG_AVFORMAT 1 ++#define CONFIG_AVCODEC 1 ++#define CONFIG_SWRESAMPLE 0 ++#define CONFIG_AVRESAMPLE 0 ++#define CONFIG_AVUTIL 1 ++#define CONFIG_FFPLAY 0 ++#define CONFIG_FFPROBE 0 ++#define CONFIG_FFMPEG 0 ++#define CONFIG_DCT 1 ++#define CONFIG_DWT 0 ++#define CONFIG_ERROR_RESILIENCE 0 ++#define CONFIG_FAAN 1 ++#define CONFIG_FAST_UNALIGNED 1 ++#define CONFIG_FFT 1 ++#define CONFIG_LSP 0 ++#define CONFIG_LZO 0 ++#define CONFIG_MDCT 1 ++#define CONFIG_PIXELUTILS 0 ++#define CONFIG_NETWORK 0 ++#define CONFIG_RDFT 1 ++#define CONFIG_AUTODETECT 0 ++#define CONFIG_FONTCONFIG 0 ++#define CONFIG_LARGE_TESTS 1 ++#define CONFIG_LINUX_PERF 0 ++#define CONFIG_MEMORY_POISONING 0 ++#define CONFIG_NEON_CLOBBER_TEST 0 ++#define CONFIG_OSSFUZZ 0 ++#define CONFIG_PIC 1 ++#define CONFIG_THUMB 0 ++#define CONFIG_VALGRIND_BACKTRACE 0 ++#define CONFIG_XMM_CLOBBER_TEST 0 ++#define CONFIG_BSFS 1 ++#define CONFIG_DECODERS 1 ++#define CONFIG_ENCODERS 0 ++#define CONFIG_HWACCELS 0 ++#define CONFIG_PARSERS 1 ++#define CONFIG_INDEVS 0 ++#define CONFIG_OUTDEVS 0 ++#define CONFIG_FILTERS 0 ++#define CONFIG_DEMUXERS 1 ++#define CONFIG_MUXERS 0 ++#define CONFIG_PROTOCOLS 1 ++#define CONFIG_AANDCTTABLES 0 ++#define CONFIG_AC3DSP 0 ++#define CONFIG_ADTS_HEADER 0 ++#define CONFIG_ATSC_A53 0 ++#define CONFIG_AUDIO_FRAME_QUEUE 0 ++#define CONFIG_AUDIODSP 0 ++#define CONFIG_BLOCKDSP 0 ++#define CONFIG_BSWAPDSP 0 ++#define CONFIG_CABAC 0 ++#define CONFIG_CBS 0 ++#define CONFIG_CBS_AV1 0 ++#define CONFIG_CBS_H264 0 ++#define CONFIG_CBS_H265 0 ++#define CONFIG_CBS_JPEG 0 ++#define CONFIG_CBS_MPEG2 0 ++#define CONFIG_CBS_VP9 0 ++#define CONFIG_DIRAC_PARSE 0 ++#define CONFIG_DNN 0 ++#define CONFIG_DVPROFILE 0 ++#define CONFIG_EXIF 0 ++#define CONFIG_FAANDCT 1 ++#define CONFIG_FAANIDCT 1 ++#define CONFIG_FDCTDSP 1 ++#define CONFIG_FLACDSP 0 ++#define CONFIG_FMTCONVERT 0 ++#define CONFIG_FRAME_THREAD_ENCODER 0 ++#define CONFIG_G722DSP 0 ++#define CONFIG_GOLOMB 0 ++#define CONFIG_GPLV3 0 ++#define CONFIG_H263DSP 0 ++#define CONFIG_H264CHROMA 0 ++#define CONFIG_H264DSP 0 ++#define CONFIG_H264PARSE 0 ++#define CONFIG_H264PRED 0 ++#define CONFIG_H264QPEL 0 ++#define CONFIG_HEVCPARSE 0 ++#define CONFIG_HPELDSP 0 ++#define CONFIG_HUFFMAN 0 ++#define CONFIG_HUFFYUVDSP 0 ++#define CONFIG_HUFFYUVENCDSP 0 ++#define CONFIG_IDCTDSP 1 ++#define CONFIG_IIRFILTER 0 ++#define CONFIG_MDCT15 0 ++#define CONFIG_INTRAX8 0 ++#define CONFIG_ISO_MEDIA 0 ++#define CONFIG_IVIDSP 0 ++#define CONFIG_JPEGTABLES 0 ++#define CONFIG_LGPLV3 0 ++#define CONFIG_LIBX262 0 ++#define CONFIG_LLAUDDSP 0 ++#define CONFIG_LLVIDDSP 0 ++#define CONFIG_LLVIDENCDSP 0 ++#define CONFIG_LPC 0 ++#define CONFIG_LZF 0 ++#define CONFIG_ME_CMP 0 ++#define CONFIG_MPEG_ER 0 ++#define CONFIG_MPEGAUDIO 1 ++#define CONFIG_MPEGAUDIODSP 1 ++#define CONFIG_MPEGAUDIOHEADER 1 ++#define CONFIG_MPEGVIDEO 0 ++#define CONFIG_MPEGVIDEOENC 0 ++#define CONFIG_MSS34DSP 0 ++#define CONFIG_PIXBLOCKDSP 0 ++#define CONFIG_QPELDSP 0 ++#define CONFIG_QSV 0 ++#define CONFIG_QSVDEC 0 ++#define CONFIG_QSVENC 0 ++#define CONFIG_QSVVPP 0 ++#define CONFIG_RANGECODER 0 ++#define CONFIG_RIFFDEC 1 ++#define CONFIG_RIFFENC 0 ++#define CONFIG_RTPDEC 0 ++#define CONFIG_RTPENC_CHAIN 0 ++#define CONFIG_RV34DSP 0 ++#define CONFIG_SCENE_SAD 0 ++#define CONFIG_SINEWIN 1 ++#define CONFIG_SNAPPY 0 ++#define CONFIG_SRTP 0 ++#define CONFIG_STARTCODE 0 ++#define CONFIG_TEXTUREDSP 0 ++#define CONFIG_TEXTUREDSPENC 0 ++#define CONFIG_TPELDSP 0 ++#define CONFIG_VAAPI_1 0 ++#define CONFIG_VAAPI_ENCODE 0 ++#define CONFIG_VC1DSP 0 ++#define CONFIG_VIDEODSP 0 ++#define CONFIG_VP3DSP 0 ++#define CONFIG_VP56DSP 0 ++#define CONFIG_VP8DSP 0 ++#define CONFIG_WMA_FREQS 1 ++#define CONFIG_WMV2DSP 0 ++#define CONFIG_AAC_ADTSTOASC_BSF 0 ++#define CONFIG_AV1_FRAME_MERGE_BSF 0 ++#define CONFIG_AV1_FRAME_SPLIT_BSF 0 ++#define CONFIG_AV1_METADATA_BSF 0 ++#define CONFIG_CHOMP_BSF 0 ++#define CONFIG_DUMP_EXTRADATA_BSF 0 ++#define CONFIG_DCA_CORE_BSF 0 ++#define CONFIG_EAC3_CORE_BSF 0 ++#define CONFIG_EXTRACT_EXTRADATA_BSF 0 ++#define CONFIG_FILTER_UNITS_BSF 0 ++#define CONFIG_H264_METADATA_BSF 0 ++#define CONFIG_H264_MP4TOANNEXB_BSF 0 ++#define CONFIG_H264_REDUNDANT_PPS_BSF 0 ++#define CONFIG_HAPQA_EXTRACT_BSF 0 ++#define CONFIG_HEVC_METADATA_BSF 0 ++#define CONFIG_HEVC_MP4TOANNEXB_BSF 0 ++#define CONFIG_IMX_DUMP_HEADER_BSF 0 ++#define CONFIG_MJPEG2JPEG_BSF 0 ++#define CONFIG_MJPEGA_DUMP_HEADER_BSF 0 ++#define CONFIG_MP3_HEADER_DECOMPRESS_BSF 0 ++#define CONFIG_MPEG2_METADATA_BSF 0 ++#define CONFIG_MPEG4_UNPACK_BFRAMES_BSF 0 ++#define CONFIG_MOV2TEXTSUB_BSF 0 ++#define CONFIG_NOISE_BSF 0 ++#define CONFIG_NULL_BSF 1 ++#define CONFIG_OPUS_METADATA_BSF 0 ++#define CONFIG_PCM_RECHUNK_BSF 0 ++#define CONFIG_PRORES_METADATA_BSF 0 ++#define CONFIG_REMOVE_EXTRADATA_BSF 0 ++#define CONFIG_SETTS_BSF 0 ++#define CONFIG_TEXT2MOVSUB_BSF 0 ++#define CONFIG_TRACE_HEADERS_BSF 0 ++#define CONFIG_TRUEHD_CORE_BSF 0 ++#define CONFIG_VP9_METADATA_BSF 0 ++#define CONFIG_VP9_RAW_REORDER_BSF 0 ++#define CONFIG_VP9_SUPERFRAME_BSF 0 ++#define CONFIG_VP9_SUPERFRAME_SPLIT_BSF 0 ++#define CONFIG_AASC_DECODER 0 ++#define CONFIG_AIC_DECODER 0 ++#define CONFIG_ALIAS_PIX_DECODER 0 ++#define CONFIG_AGM_DECODER 0 ++#define CONFIG_AMV_DECODER 0 ++#define CONFIG_ANM_DECODER 0 ++#define CONFIG_ANSI_DECODER 0 ++#define CONFIG_APNG_DECODER 0 ++#define CONFIG_ARBC_DECODER 0 ++#define CONFIG_ARGO_DECODER 0 ++#define CONFIG_ASV1_DECODER 0 ++#define CONFIG_ASV2_DECODER 0 ++#define CONFIG_AURA_DECODER 0 ++#define CONFIG_AURA2_DECODER 0 ++#define CONFIG_AVRP_DECODER 0 ++#define CONFIG_AVRN_DECODER 0 ++#define CONFIG_AVS_DECODER 0 ++#define CONFIG_AVUI_DECODER 0 ++#define CONFIG_AYUV_DECODER 0 ++#define CONFIG_BETHSOFTVID_DECODER 0 ++#define CONFIG_BFI_DECODER 0 ++#define CONFIG_BINK_DECODER 0 ++#define CONFIG_BITPACKED_DECODER 0 ++#define CONFIG_BMP_DECODER 0 ++#define CONFIG_BMV_VIDEO_DECODER 0 ++#define CONFIG_BRENDER_PIX_DECODER 0 ++#define CONFIG_C93_DECODER 0 ++#define CONFIG_CAVS_DECODER 0 ++#define CONFIG_CDGRAPHICS_DECODER 0 ++#define CONFIG_CDTOONS_DECODER 0 ++#define CONFIG_CDXL_DECODER 0 ++#define CONFIG_CFHD_DECODER 0 ++#define CONFIG_CINEPAK_DECODER 0 ++#define CONFIG_CLEARVIDEO_DECODER 0 ++#define CONFIG_CLJR_DECODER 0 ++#define CONFIG_CLLC_DECODER 0 ++#define CONFIG_COMFORTNOISE_DECODER 0 ++#define CONFIG_CPIA_DECODER 0 ++#define CONFIG_CRI_DECODER 0 ++#define CONFIG_CSCD_DECODER 0 ++#define CONFIG_CYUV_DECODER 0 ++#define CONFIG_DDS_DECODER 0 ++#define CONFIG_DFA_DECODER 0 ++#define CONFIG_DIRAC_DECODER 0 ++#define CONFIG_DNXHD_DECODER 0 ++#define CONFIG_DPX_DECODER 0 ++#define CONFIG_DSICINVIDEO_DECODER 0 ++#define CONFIG_DVAUDIO_DECODER 0 ++#define CONFIG_DVVIDEO_DECODER 0 ++#define CONFIG_DXA_DECODER 0 ++#define CONFIG_DXTORY_DECODER 0 ++#define CONFIG_DXV_DECODER 0 ++#define CONFIG_EACMV_DECODER 0 ++#define CONFIG_EAMAD_DECODER 0 ++#define CONFIG_EATGQ_DECODER 0 ++#define CONFIG_EATGV_DECODER 0 ++#define CONFIG_EATQI_DECODER 0 ++#define CONFIG_EIGHTBPS_DECODER 0 ++#define CONFIG_EIGHTSVX_EXP_DECODER 0 ++#define CONFIG_EIGHTSVX_FIB_DECODER 0 ++#define CONFIG_ESCAPE124_DECODER 0 ++#define CONFIG_ESCAPE130_DECODER 0 ++#define CONFIG_EXR_DECODER 0 ++#define CONFIG_FFV1_DECODER 0 ++#define CONFIG_FFVHUFF_DECODER 0 ++#define CONFIG_FIC_DECODER 0 ++#define CONFIG_FITS_DECODER 0 ++#define CONFIG_FLASHSV_DECODER 0 ++#define CONFIG_FLASHSV2_DECODER 0 ++#define CONFIG_FLIC_DECODER 0 ++#define CONFIG_FLV_DECODER 0 ++#define CONFIG_FMVC_DECODER 0 ++#define CONFIG_FOURXM_DECODER 0 ++#define CONFIG_FRAPS_DECODER 0 ++#define CONFIG_FRWU_DECODER 0 ++#define CONFIG_G2M_DECODER 0 ++#define CONFIG_GDV_DECODER 0 ++#define CONFIG_GIF_DECODER 0 ++#define CONFIG_H261_DECODER 0 ++#define CONFIG_H263_DECODER 0 ++#define CONFIG_H263I_DECODER 0 ++#define CONFIG_H263P_DECODER 0 ++#define CONFIG_H263_V4L2M2M_DECODER 0 ++#define CONFIG_H264_DECODER 0 ++#define CONFIG_H264_CRYSTALHD_DECODER 0 ++#define CONFIG_H264_V4L2M2M_DECODER 0 ++#define CONFIG_H264_MEDIACODEC_DECODER 0 ++#define CONFIG_H264_MMAL_DECODER 0 ++#define CONFIG_H264_QSV_DECODER 0 ++#define CONFIG_H264_RKMPP_DECODER 0 ++#define CONFIG_HAP_DECODER 0 ++#define CONFIG_HEVC_DECODER 0 ++#define CONFIG_HEVC_QSV_DECODER 0 ++#define CONFIG_HEVC_RKMPP_DECODER 0 ++#define CONFIG_HEVC_V4L2M2M_DECODER 0 ++#define CONFIG_HNM4_VIDEO_DECODER 0 ++#define CONFIG_HQ_HQA_DECODER 0 ++#define CONFIG_HQX_DECODER 0 ++#define CONFIG_HUFFYUV_DECODER 0 ++#define CONFIG_HYMT_DECODER 0 ++#define CONFIG_IDCIN_DECODER 0 ++#define CONFIG_IFF_ILBM_DECODER 0 ++#define CONFIG_IMM4_DECODER 0 ++#define CONFIG_IMM5_DECODER 0 ++#define CONFIG_INDEO2_DECODER 0 ++#define CONFIG_INDEO3_DECODER 0 ++#define CONFIG_INDEO4_DECODER 0 ++#define CONFIG_INDEO5_DECODER 0 ++#define CONFIG_INTERPLAY_VIDEO_DECODER 0 ++#define CONFIG_IPU_DECODER 0 ++#define CONFIG_JPEG2000_DECODER 0 ++#define CONFIG_JPEGLS_DECODER 0 ++#define CONFIG_JV_DECODER 0 ++#define CONFIG_KGV1_DECODER 0 ++#define CONFIG_KMVC_DECODER 0 ++#define CONFIG_LAGARITH_DECODER 0 ++#define CONFIG_LOCO_DECODER 0 ++#define CONFIG_LSCR_DECODER 0 ++#define CONFIG_M101_DECODER 0 ++#define CONFIG_MAGICYUV_DECODER 0 ++#define CONFIG_MDEC_DECODER 0 ++#define CONFIG_MIMIC_DECODER 0 ++#define CONFIG_MJPEG_DECODER 0 ++#define CONFIG_MJPEGB_DECODER 0 ++#define CONFIG_MMVIDEO_DECODER 0 ++#define CONFIG_MOBICLIP_DECODER 0 ++#define CONFIG_MOTIONPIXELS_DECODER 0 ++#define CONFIG_MPEG1VIDEO_DECODER 0 ++#define CONFIG_MPEG2VIDEO_DECODER 0 ++#define CONFIG_MPEG4_DECODER 0 ++#define CONFIG_MPEG4_CRYSTALHD_DECODER 0 ++#define CONFIG_MPEG4_V4L2M2M_DECODER 0 ++#define CONFIG_MPEG4_MMAL_DECODER 0 ++#define CONFIG_MPEGVIDEO_DECODER 0 ++#define CONFIG_MPEG1_V4L2M2M_DECODER 0 ++#define CONFIG_MPEG2_MMAL_DECODER 0 ++#define CONFIG_MPEG2_CRYSTALHD_DECODER 0 ++#define CONFIG_MPEG2_V4L2M2M_DECODER 0 ++#define CONFIG_MPEG2_QSV_DECODER 0 ++#define CONFIG_MPEG2_MEDIACODEC_DECODER 0 ++#define CONFIG_MSA1_DECODER 0 ++#define CONFIG_MSCC_DECODER 0 ++#define CONFIG_MSMPEG4V1_DECODER 0 ++#define CONFIG_MSMPEG4V2_DECODER 0 ++#define CONFIG_MSMPEG4V3_DECODER 0 ++#define CONFIG_MSMPEG4_CRYSTALHD_DECODER 0 ++#define CONFIG_MSP2_DECODER 0 ++#define CONFIG_MSRLE_DECODER 0 ++#define CONFIG_MSS1_DECODER 0 ++#define CONFIG_MSS2_DECODER 0 ++#define CONFIG_MSVIDEO1_DECODER 0 ++#define CONFIG_MSZH_DECODER 0 ++#define CONFIG_MTS2_DECODER 0 ++#define CONFIG_MV30_DECODER 0 ++#define CONFIG_MVC1_DECODER 0 ++#define CONFIG_MVC2_DECODER 0 ++#define CONFIG_MVDV_DECODER 0 ++#define CONFIG_MVHA_DECODER 0 ++#define CONFIG_MWSC_DECODER 0 ++#define CONFIG_MXPEG_DECODER 0 ++#define CONFIG_NOTCHLC_DECODER 0 ++#define CONFIG_NUV_DECODER 0 ++#define CONFIG_PAF_VIDEO_DECODER 0 ++#define CONFIG_PAM_DECODER 0 ++#define CONFIG_PBM_DECODER 0 ++#define CONFIG_PCX_DECODER 0 ++#define CONFIG_PFM_DECODER 0 ++#define CONFIG_PGM_DECODER 0 ++#define CONFIG_PGMYUV_DECODER 0 ++#define CONFIG_PGX_DECODER 0 ++#define CONFIG_PHOTOCD_DECODER 0 ++#define CONFIG_PICTOR_DECODER 0 ++#define CONFIG_PIXLET_DECODER 0 ++#define CONFIG_PNG_DECODER 0 ++#define CONFIG_PPM_DECODER 0 ++#define CONFIG_PRORES_DECODER 0 ++#define CONFIG_PROSUMER_DECODER 0 ++#define CONFIG_PSD_DECODER 0 ++#define CONFIG_PTX_DECODER 0 ++#define CONFIG_QDRAW_DECODER 0 ++#define CONFIG_QPEG_DECODER 0 ++#define CONFIG_QTRLE_DECODER 0 ++#define CONFIG_R10K_DECODER 0 ++#define CONFIG_R210_DECODER 0 ++#define CONFIG_RASC_DECODER 0 ++#define CONFIG_RAWVIDEO_DECODER 0 ++#define CONFIG_RL2_DECODER 0 ++#define CONFIG_ROQ_DECODER 0 ++#define CONFIG_RPZA_DECODER 0 ++#define CONFIG_RSCC_DECODER 0 ++#define CONFIG_RV10_DECODER 0 ++#define CONFIG_RV20_DECODER 0 ++#define CONFIG_RV30_DECODER 0 ++#define CONFIG_RV40_DECODER 0 ++#define CONFIG_S302M_DECODER 0 ++#define CONFIG_SANM_DECODER 0 ++#define CONFIG_SCPR_DECODER 0 ++#define CONFIG_SCREENPRESSO_DECODER 0 ++#define CONFIG_SGA_DECODER 0 ++#define CONFIG_SGI_DECODER 0 ++#define CONFIG_SGIRLE_DECODER 0 ++#define CONFIG_SHEERVIDEO_DECODER 0 ++#define CONFIG_SIMBIOSIS_IMX_DECODER 0 ++#define CONFIG_SMACKER_DECODER 0 ++#define CONFIG_SMC_DECODER 0 ++#define CONFIG_SMVJPEG_DECODER 0 ++#define CONFIG_SNOW_DECODER 0 ++#define CONFIG_SP5X_DECODER 0 ++#define CONFIG_SPEEDHQ_DECODER 0 ++#define CONFIG_SRGC_DECODER 0 ++#define CONFIG_SUNRAST_DECODER 0 ++#define CONFIG_SVQ1_DECODER 0 ++#define CONFIG_SVQ3_DECODER 0 ++#define CONFIG_TARGA_DECODER 0 ++#define CONFIG_TARGA_Y216_DECODER 0 ++#define CONFIG_TDSC_DECODER 0 ++#define CONFIG_THEORA_DECODER 0 ++#define CONFIG_THP_DECODER 0 ++#define CONFIG_TIERTEXSEQVIDEO_DECODER 0 ++#define CONFIG_TIFF_DECODER 0 ++#define CONFIG_TMV_DECODER 0 ++#define CONFIG_TRUEMOTION1_DECODER 0 ++#define CONFIG_TRUEMOTION2_DECODER 0 ++#define CONFIG_TRUEMOTION2RT_DECODER 0 ++#define CONFIG_TSCC_DECODER 0 ++#define CONFIG_TSCC2_DECODER 0 ++#define CONFIG_TXD_DECODER 0 ++#define CONFIG_ULTI_DECODER 0 ++#define CONFIG_UTVIDEO_DECODER 0 ++#define CONFIG_V210_DECODER 0 ++#define CONFIG_V210X_DECODER 0 ++#define CONFIG_V308_DECODER 0 ++#define CONFIG_V408_DECODER 0 ++#define CONFIG_V410_DECODER 0 ++#define CONFIG_VB_DECODER 0 ++#define CONFIG_VBLE_DECODER 0 ++#define CONFIG_VC1_DECODER 0 ++#define CONFIG_VC1_CRYSTALHD_DECODER 0 ++#define CONFIG_VC1IMAGE_DECODER 0 ++#define CONFIG_VC1_MMAL_DECODER 0 ++#define CONFIG_VC1_QSV_DECODER 0 ++#define CONFIG_VC1_V4L2M2M_DECODER 0 ++#define CONFIG_VCR1_DECODER 0 ++#define CONFIG_VMDVIDEO_DECODER 0 ++#define CONFIG_VMNC_DECODER 0 ++#define CONFIG_VP3_DECODER 0 ++#define CONFIG_VP4_DECODER 0 ++#define CONFIG_VP5_DECODER 0 ++#define CONFIG_VP6_DECODER 0 ++#define CONFIG_VP6A_DECODER 0 ++#define CONFIG_VP6F_DECODER 0 ++#define CONFIG_VP7_DECODER 0 ++#define CONFIG_VP8_DECODER 0 ++#define CONFIG_VP8_RKMPP_DECODER 0 ++#define CONFIG_VP8_V4L2M2M_DECODER 0 ++#define CONFIG_VP9_DECODER 0 ++#define CONFIG_VP9_RKMPP_DECODER 0 ++#define CONFIG_VP9_V4L2M2M_DECODER 0 ++#define CONFIG_VQA_DECODER 0 ++#define CONFIG_WEBP_DECODER 0 ++#define CONFIG_WCMV_DECODER 0 ++#define CONFIG_WRAPPED_AVFRAME_DECODER 0 ++#define CONFIG_WMV1_DECODER 0 ++#define CONFIG_WMV2_DECODER 0 ++#define CONFIG_WMV3_DECODER 0 ++#define CONFIG_WMV3_CRYSTALHD_DECODER 0 ++#define CONFIG_WMV3IMAGE_DECODER 0 ++#define CONFIG_WNV1_DECODER 0 ++#define CONFIG_XAN_WC3_DECODER 0 ++#define CONFIG_XAN_WC4_DECODER 0 ++#define CONFIG_XBM_DECODER 0 ++#define CONFIG_XFACE_DECODER 0 ++#define CONFIG_XL_DECODER 0 ++#define CONFIG_XPM_DECODER 0 ++#define CONFIG_XWD_DECODER 0 ++#define CONFIG_Y41P_DECODER 0 ++#define CONFIG_YLC_DECODER 0 ++#define CONFIG_YOP_DECODER 0 ++#define CONFIG_YUV4_DECODER 0 ++#define CONFIG_ZERO12V_DECODER 0 ++#define CONFIG_ZEROCODEC_DECODER 0 ++#define CONFIG_ZLIB_DECODER 0 ++#define CONFIG_ZMBV_DECODER 0 ++#define CONFIG_AAC_DECODER 0 ++#define CONFIG_AAC_FIXED_DECODER 0 ++#define CONFIG_AAC_LATM_DECODER 0 ++#define CONFIG_AC3_DECODER 0 ++#define CONFIG_AC3_FIXED_DECODER 0 ++#define CONFIG_ACELP_KELVIN_DECODER 0 ++#define CONFIG_ALAC_DECODER 0 ++#define CONFIG_ALS_DECODER 0 ++#define CONFIG_AMRNB_DECODER 0 ++#define CONFIG_AMRWB_DECODER 0 ++#define CONFIG_APE_DECODER 0 ++#define CONFIG_APTX_DECODER 0 ++#define CONFIG_APTX_HD_DECODER 0 ++#define CONFIG_ATRAC1_DECODER 0 ++#define CONFIG_ATRAC3_DECODER 0 ++#define CONFIG_ATRAC3AL_DECODER 0 ++#define CONFIG_ATRAC3P_DECODER 0 ++#define CONFIG_ATRAC3PAL_DECODER 0 ++#define CONFIG_ATRAC9_DECODER 0 ++#define CONFIG_BINKAUDIO_DCT_DECODER 0 ++#define CONFIG_BINKAUDIO_RDFT_DECODER 0 ++#define CONFIG_BMV_AUDIO_DECODER 0 ++#define CONFIG_COOK_DECODER 0 ++#define CONFIG_DCA_DECODER 0 ++#define CONFIG_DOLBY_E_DECODER 0 ++#define CONFIG_DSD_LSBF_DECODER 0 ++#define CONFIG_DSD_MSBF_DECODER 0 ++#define CONFIG_DSD_LSBF_PLANAR_DECODER 0 ++#define CONFIG_DSD_MSBF_PLANAR_DECODER 0 ++#define CONFIG_DSICINAUDIO_DECODER 0 ++#define CONFIG_DSS_SP_DECODER 0 ++#define CONFIG_DST_DECODER 0 ++#define CONFIG_EAC3_DECODER 0 ++#define CONFIG_EVRC_DECODER 0 ++#define CONFIG_FASTAUDIO_DECODER 0 ++#define CONFIG_FFWAVESYNTH_DECODER 0 ++#define CONFIG_FLAC_DECODER 0 ++#define CONFIG_G723_1_DECODER 0 ++#define CONFIG_G729_DECODER 0 ++#define CONFIG_GSM_DECODER 0 ++#define CONFIG_GSM_MS_DECODER 0 ++#define CONFIG_HCA_DECODER 0 ++#define CONFIG_HCOM_DECODER 0 ++#define CONFIG_IAC_DECODER 0 ++#define CONFIG_ILBC_DECODER 0 ++#define CONFIG_IMC_DECODER 0 ++#define CONFIG_INTERPLAY_ACM_DECODER 0 ++#define CONFIG_MACE3_DECODER 0 ++#define CONFIG_MACE6_DECODER 0 ++#define CONFIG_METASOUND_DECODER 0 ++#define CONFIG_MLP_DECODER 0 ++#define CONFIG_MP1_DECODER 0 ++#define CONFIG_MP1FLOAT_DECODER 0 ++#define CONFIG_MP2_DECODER 0 ++#define CONFIG_MP2FLOAT_DECODER 0 ++#define CONFIG_MP3FLOAT_DECODER 1 ++#define CONFIG_MP3_DECODER 1 ++#define CONFIG_MP3ADUFLOAT_DECODER 0 ++#define CONFIG_MP3ADU_DECODER 0 ++#define CONFIG_MP3ON4FLOAT_DECODER 0 ++#define CONFIG_MP3ON4_DECODER 0 ++#define CONFIG_MPC7_DECODER 0 ++#define CONFIG_MPC8_DECODER 0 ++#define CONFIG_NELLYMOSER_DECODER 0 ++#define CONFIG_ON2AVC_DECODER 0 ++#define CONFIG_OPUS_DECODER 0 ++#define CONFIG_PAF_AUDIO_DECODER 0 ++#define CONFIG_QCELP_DECODER 0 ++#define CONFIG_QDM2_DECODER 0 ++#define CONFIG_QDMC_DECODER 0 ++#define CONFIG_RA_144_DECODER 0 ++#define CONFIG_RA_288_DECODER 0 ++#define CONFIG_RALF_DECODER 0 ++#define CONFIG_SBC_DECODER 0 ++#define CONFIG_SHORTEN_DECODER 0 ++#define CONFIG_SIPR_DECODER 0 ++#define CONFIG_SIREN_DECODER 0 ++#define CONFIG_SMACKAUD_DECODER 0 ++#define CONFIG_SONIC_DECODER 0 ++#define CONFIG_TAK_DECODER 0 ++#define CONFIG_TRUEHD_DECODER 0 ++#define CONFIG_TRUESPEECH_DECODER 0 ++#define CONFIG_TTA_DECODER 0 ++#define CONFIG_TWINVQ_DECODER 0 ++#define CONFIG_VMDAUDIO_DECODER 0 ++#define CONFIG_VORBIS_DECODER 0 ++#define CONFIG_WAVPACK_DECODER 0 ++#define CONFIG_WMALOSSLESS_DECODER 0 ++#define CONFIG_WMAPRO_DECODER 1 ++#define CONFIG_WMAV1_DECODER 0 ++#define CONFIG_WMAV2_DECODER 1 ++#define CONFIG_WMAVOICE_DECODER 0 ++#define CONFIG_WS_SND1_DECODER 0 ++#define CONFIG_XMA1_DECODER 0 ++#define CONFIG_XMA2_DECODER 0 ++#define CONFIG_XMAFRAMES_DECODER 1 ++#define CONFIG_PCM_ALAW_DECODER 0 ++#define CONFIG_PCM_BLURAY_DECODER 0 ++#define CONFIG_PCM_DVD_DECODER 0 ++#define CONFIG_PCM_F16LE_DECODER 0 ++#define CONFIG_PCM_F24LE_DECODER 0 ++#define CONFIG_PCM_F32BE_DECODER 0 ++#define CONFIG_PCM_F32LE_DECODER 0 ++#define CONFIG_PCM_F64BE_DECODER 0 ++#define CONFIG_PCM_F64LE_DECODER 0 ++#define CONFIG_PCM_LXF_DECODER 0 ++#define CONFIG_PCM_MULAW_DECODER 0 ++#define CONFIG_PCM_S8_DECODER 0 ++#define CONFIG_PCM_S8_PLANAR_DECODER 0 ++#define CONFIG_PCM_S16BE_DECODER 0 ++#define CONFIG_PCM_S16BE_PLANAR_DECODER 0 ++#define CONFIG_PCM_S16LE_DECODER 0 ++#define CONFIG_PCM_S16LE_PLANAR_DECODER 0 ++#define CONFIG_PCM_S24BE_DECODER 0 ++#define CONFIG_PCM_S24DAUD_DECODER 0 ++#define CONFIG_PCM_S24LE_DECODER 0 ++#define CONFIG_PCM_S24LE_PLANAR_DECODER 0 ++#define CONFIG_PCM_S32BE_DECODER 0 ++#define CONFIG_PCM_S32LE_DECODER 0 ++#define CONFIG_PCM_S32LE_PLANAR_DECODER 0 ++#define CONFIG_PCM_S64BE_DECODER 0 ++#define CONFIG_PCM_S64LE_DECODER 0 ++#define CONFIG_PCM_SGA_DECODER 0 ++#define CONFIG_PCM_U8_DECODER 0 ++#define CONFIG_PCM_U16BE_DECODER 0 ++#define CONFIG_PCM_U16LE_DECODER 0 ++#define CONFIG_PCM_U24BE_DECODER 0 ++#define CONFIG_PCM_U24LE_DECODER 0 ++#define CONFIG_PCM_U32BE_DECODER 0 ++#define CONFIG_PCM_U32LE_DECODER 0 ++#define CONFIG_PCM_VIDC_DECODER 0 ++#define CONFIG_DERF_DPCM_DECODER 0 ++#define CONFIG_GREMLIN_DPCM_DECODER 0 ++#define CONFIG_INTERPLAY_DPCM_DECODER 0 ++#define CONFIG_ROQ_DPCM_DECODER 0 ++#define CONFIG_SDX2_DPCM_DECODER 0 ++#define CONFIG_SOL_DPCM_DECODER 0 ++#define CONFIG_XAN_DPCM_DECODER 0 ++#define CONFIG_ADPCM_4XM_DECODER 0 ++#define CONFIG_ADPCM_ADX_DECODER 0 ++#define CONFIG_ADPCM_AFC_DECODER 0 ++#define CONFIG_ADPCM_AGM_DECODER 0 ++#define CONFIG_ADPCM_AICA_DECODER 0 ++#define CONFIG_ADPCM_ARGO_DECODER 0 ++#define CONFIG_ADPCM_CT_DECODER 0 ++#define CONFIG_ADPCM_DTK_DECODER 0 ++#define CONFIG_ADPCM_EA_DECODER 0 ++#define CONFIG_ADPCM_EA_MAXIS_XA_DECODER 0 ++#define CONFIG_ADPCM_EA_R1_DECODER 0 ++#define CONFIG_ADPCM_EA_R2_DECODER 0 ++#define CONFIG_ADPCM_EA_R3_DECODER 0 ++#define CONFIG_ADPCM_EA_XAS_DECODER 0 ++#define CONFIG_ADPCM_G722_DECODER 0 ++#define CONFIG_ADPCM_G726_DECODER 0 ++#define CONFIG_ADPCM_G726LE_DECODER 0 ++#define CONFIG_ADPCM_IMA_AMV_DECODER 0 ++#define CONFIG_ADPCM_IMA_ALP_DECODER 0 ++#define CONFIG_ADPCM_IMA_APC_DECODER 0 ++#define CONFIG_ADPCM_IMA_APM_DECODER 0 ++#define CONFIG_ADPCM_IMA_CUNNING_DECODER 0 ++#define CONFIG_ADPCM_IMA_DAT4_DECODER 0 ++#define CONFIG_ADPCM_IMA_DK3_DECODER 0 ++#define CONFIG_ADPCM_IMA_DK4_DECODER 0 ++#define CONFIG_ADPCM_IMA_EA_EACS_DECODER 0 ++#define CONFIG_ADPCM_IMA_EA_SEAD_DECODER 0 ++#define CONFIG_ADPCM_IMA_ISS_DECODER 0 ++#define CONFIG_ADPCM_IMA_MOFLEX_DECODER 0 ++#define CONFIG_ADPCM_IMA_MTF_DECODER 0 ++#define CONFIG_ADPCM_IMA_OKI_DECODER 0 ++#define CONFIG_ADPCM_IMA_QT_DECODER 0 ++#define CONFIG_ADPCM_IMA_RAD_DECODER 0 ++#define CONFIG_ADPCM_IMA_SSI_DECODER 0 ++#define CONFIG_ADPCM_IMA_SMJPEG_DECODER 0 ++#define CONFIG_ADPCM_IMA_WAV_DECODER 0 ++#define CONFIG_ADPCM_IMA_WS_DECODER 0 ++#define CONFIG_ADPCM_MS_DECODER 0 ++#define CONFIG_ADPCM_MTAF_DECODER 0 ++#define CONFIG_ADPCM_PSX_DECODER 0 ++#define CONFIG_ADPCM_SBPRO_2_DECODER 0 ++#define CONFIG_ADPCM_SBPRO_3_DECODER 0 ++#define CONFIG_ADPCM_SBPRO_4_DECODER 0 ++#define CONFIG_ADPCM_SWF_DECODER 0 ++#define CONFIG_ADPCM_THP_DECODER 0 ++#define CONFIG_ADPCM_THP_LE_DECODER 0 ++#define CONFIG_ADPCM_VIMA_DECODER 0 ++#define CONFIG_ADPCM_XA_DECODER 0 ++#define CONFIG_ADPCM_YAMAHA_DECODER 0 ++#define CONFIG_ADPCM_ZORK_DECODER 0 ++#define CONFIG_SSA_DECODER 0 ++#define CONFIG_ASS_DECODER 0 ++#define CONFIG_CCAPTION_DECODER 0 ++#define CONFIG_DVBSUB_DECODER 0 ++#define CONFIG_DVDSUB_DECODER 0 ++#define CONFIG_JACOSUB_DECODER 0 ++#define CONFIG_MICRODVD_DECODER 0 ++#define CONFIG_MOVTEXT_DECODER 0 ++#define CONFIG_MPL2_DECODER 0 ++#define CONFIG_PGSSUB_DECODER 0 ++#define CONFIG_PJS_DECODER 0 ++#define CONFIG_REALTEXT_DECODER 0 ++#define CONFIG_SAMI_DECODER 0 ++#define CONFIG_SRT_DECODER 0 ++#define CONFIG_STL_DECODER 0 ++#define CONFIG_SUBRIP_DECODER 0 ++#define CONFIG_SUBVIEWER_DECODER 0 ++#define CONFIG_SUBVIEWER1_DECODER 0 ++#define CONFIG_TEXT_DECODER 0 ++#define CONFIG_VPLAYER_DECODER 0 ++#define CONFIG_WEBVTT_DECODER 0 ++#define CONFIG_XSUB_DECODER 0 ++#define CONFIG_AAC_AT_DECODER 0 ++#define CONFIG_AC3_AT_DECODER 0 ++#define CONFIG_ADPCM_IMA_QT_AT_DECODER 0 ++#define CONFIG_ALAC_AT_DECODER 0 ++#define CONFIG_AMR_NB_AT_DECODER 0 ++#define CONFIG_EAC3_AT_DECODER 0 ++#define CONFIG_GSM_MS_AT_DECODER 0 ++#define CONFIG_ILBC_AT_DECODER 0 ++#define CONFIG_MP1_AT_DECODER 0 ++#define CONFIG_MP2_AT_DECODER 0 ++#define CONFIG_MP3_AT_DECODER 0 ++#define CONFIG_PCM_ALAW_AT_DECODER 0 ++#define CONFIG_PCM_MULAW_AT_DECODER 0 ++#define CONFIG_QDMC_AT_DECODER 0 ++#define CONFIG_QDM2_AT_DECODER 0 ++#define CONFIG_LIBARIBB24_DECODER 0 ++#define CONFIG_LIBCELT_DECODER 0 ++#define CONFIG_LIBCODEC2_DECODER 0 ++#define CONFIG_LIBDAV1D_DECODER 0 ++#define CONFIG_LIBDAVS2_DECODER 0 ++#define CONFIG_LIBFDK_AAC_DECODER 0 ++#define CONFIG_LIBGSM_DECODER 0 ++#define CONFIG_LIBGSM_MS_DECODER 0 ++#define CONFIG_LIBILBC_DECODER 0 ++#define CONFIG_LIBOPENCORE_AMRNB_DECODER 0 ++#define CONFIG_LIBOPENCORE_AMRWB_DECODER 0 ++#define CONFIG_LIBOPENJPEG_DECODER 0 ++#define CONFIG_LIBOPUS_DECODER 0 ++#define CONFIG_LIBRSVG_DECODER 0 ++#define CONFIG_LIBSPEEX_DECODER 0 ++#define CONFIG_LIBUAVS3D_DECODER 0 ++#define CONFIG_LIBVORBIS_DECODER 0 ++#define CONFIG_LIBVPX_VP8_DECODER 0 ++#define CONFIG_LIBVPX_VP9_DECODER 0 ++#define CONFIG_LIBZVBI_TELETEXT_DECODER 0 ++#define CONFIG_BINTEXT_DECODER 0 ++#define CONFIG_XBIN_DECODER 0 ++#define CONFIG_IDF_DECODER 0 ++#define CONFIG_LIBAOM_AV1_DECODER 0 ++#define CONFIG_AV1_DECODER 0 ++#define CONFIG_AV1_CUVID_DECODER 0 ++#define CONFIG_AV1_QSV_DECODER 0 ++#define CONFIG_LIBOPENH264_DECODER 0 ++#define CONFIG_H264_CUVID_DECODER 0 ++#define CONFIG_HEVC_CUVID_DECODER 0 ++#define CONFIG_HEVC_MEDIACODEC_DECODER 0 ++#define CONFIG_MJPEG_CUVID_DECODER 0 ++#define CONFIG_MJPEG_QSV_DECODER 0 ++#define CONFIG_MPEG1_CUVID_DECODER 0 ++#define CONFIG_MPEG2_CUVID_DECODER 0 ++#define CONFIG_MPEG4_CUVID_DECODER 0 ++#define CONFIG_MPEG4_MEDIACODEC_DECODER 0 ++#define CONFIG_VC1_CUVID_DECODER 0 ++#define CONFIG_VP8_CUVID_DECODER 0 ++#define CONFIG_VP8_MEDIACODEC_DECODER 0 ++#define CONFIG_VP8_QSV_DECODER 0 ++#define CONFIG_VP9_CUVID_DECODER 0 ++#define CONFIG_VP9_MEDIACODEC_DECODER 0 ++#define CONFIG_VP9_QSV_DECODER 0 ++#define CONFIG_A64MULTI_ENCODER 0 ++#define CONFIG_A64MULTI5_ENCODER 0 ++#define CONFIG_ALIAS_PIX_ENCODER 0 ++#define CONFIG_AMV_ENCODER 0 ++#define CONFIG_APNG_ENCODER 0 ++#define CONFIG_ASV1_ENCODER 0 ++#define CONFIG_ASV2_ENCODER 0 ++#define CONFIG_AVRP_ENCODER 0 ++#define CONFIG_AVUI_ENCODER 0 ++#define CONFIG_AYUV_ENCODER 0 ++#define CONFIG_BMP_ENCODER 0 ++#define CONFIG_CFHD_ENCODER 0 ++#define CONFIG_CINEPAK_ENCODER 0 ++#define CONFIG_CLJR_ENCODER 0 ++#define CONFIG_COMFORTNOISE_ENCODER 0 ++#define CONFIG_DNXHD_ENCODER 0 ++#define CONFIG_DPX_ENCODER 0 ++#define CONFIG_DVVIDEO_ENCODER 0 ++#define CONFIG_EXR_ENCODER 0 ++#define CONFIG_FFV1_ENCODER 0 ++#define CONFIG_FFVHUFF_ENCODER 0 ++#define CONFIG_FITS_ENCODER 0 ++#define CONFIG_FLASHSV_ENCODER 0 ++#define CONFIG_FLASHSV2_ENCODER 0 ++#define CONFIG_FLV_ENCODER 0 ++#define CONFIG_GIF_ENCODER 0 ++#define CONFIG_H261_ENCODER 0 ++#define CONFIG_H263_ENCODER 0 ++#define CONFIG_H263P_ENCODER 0 ++#define CONFIG_HAP_ENCODER 0 ++#define CONFIG_HUFFYUV_ENCODER 0 ++#define CONFIG_JPEG2000_ENCODER 0 ++#define CONFIG_JPEGLS_ENCODER 0 ++#define CONFIG_LJPEG_ENCODER 0 ++#define CONFIG_MAGICYUV_ENCODER 0 ++#define CONFIG_MJPEG_ENCODER 0 ++#define CONFIG_MPEG1VIDEO_ENCODER 0 ++#define CONFIG_MPEG2VIDEO_ENCODER 0 ++#define CONFIG_MPEG4_ENCODER 0 ++#define CONFIG_MSMPEG4V2_ENCODER 0 ++#define CONFIG_MSMPEG4V3_ENCODER 0 ++#define CONFIG_MSVIDEO1_ENCODER 0 ++#define CONFIG_PAM_ENCODER 0 ++#define CONFIG_PBM_ENCODER 0 ++#define CONFIG_PCX_ENCODER 0 ++#define CONFIG_PFM_ENCODER 0 ++#define CONFIG_PGM_ENCODER 0 ++#define CONFIG_PGMYUV_ENCODER 0 ++#define CONFIG_PNG_ENCODER 0 ++#define CONFIG_PPM_ENCODER 0 ++#define CONFIG_PRORES_ENCODER 0 ++#define CONFIG_PRORES_AW_ENCODER 0 ++#define CONFIG_PRORES_KS_ENCODER 0 ++#define CONFIG_QTRLE_ENCODER 0 ++#define CONFIG_R10K_ENCODER 0 ++#define CONFIG_R210_ENCODER 0 ++#define CONFIG_RAWVIDEO_ENCODER 0 ++#define CONFIG_ROQ_ENCODER 0 ++#define CONFIG_RPZA_ENCODER 0 ++#define CONFIG_RV10_ENCODER 0 ++#define CONFIG_RV20_ENCODER 0 ++#define CONFIG_S302M_ENCODER 0 ++#define CONFIG_SGI_ENCODER 0 ++#define CONFIG_SNOW_ENCODER 0 ++#define CONFIG_SPEEDHQ_ENCODER 0 ++#define CONFIG_SUNRAST_ENCODER 0 ++#define CONFIG_SVQ1_ENCODER 0 ++#define CONFIG_TARGA_ENCODER 0 ++#define CONFIG_TIFF_ENCODER 0 ++#define CONFIG_UTVIDEO_ENCODER 0 ++#define CONFIG_V210_ENCODER 0 ++#define CONFIG_V308_ENCODER 0 ++#define CONFIG_V408_ENCODER 0 ++#define CONFIG_V410_ENCODER 0 ++#define CONFIG_VC2_ENCODER 0 ++#define CONFIG_WRAPPED_AVFRAME_ENCODER 0 ++#define CONFIG_WMV1_ENCODER 0 ++#define CONFIG_WMV2_ENCODER 0 ++#define CONFIG_XBM_ENCODER 0 ++#define CONFIG_XFACE_ENCODER 0 ++#define CONFIG_XWD_ENCODER 0 ++#define CONFIG_Y41P_ENCODER 0 ++#define CONFIG_YUV4_ENCODER 0 ++#define CONFIG_ZLIB_ENCODER 0 ++#define CONFIG_ZMBV_ENCODER 0 ++#define CONFIG_AAC_ENCODER 0 ++#define CONFIG_AC3_ENCODER 0 ++#define CONFIG_AC3_FIXED_ENCODER 0 ++#define CONFIG_ALAC_ENCODER 0 ++#define CONFIG_APTX_ENCODER 0 ++#define CONFIG_APTX_HD_ENCODER 0 ++#define CONFIG_DCA_ENCODER 0 ++#define CONFIG_EAC3_ENCODER 0 ++#define CONFIG_FLAC_ENCODER 0 ++#define CONFIG_G723_1_ENCODER 0 ++#define CONFIG_MLP_ENCODER 0 ++#define CONFIG_MP2_ENCODER 0 ++#define CONFIG_MP2FIXED_ENCODER 0 ++#define CONFIG_NELLYMOSER_ENCODER 0 ++#define CONFIG_OPUS_ENCODER 0 ++#define CONFIG_RA_144_ENCODER 0 ++#define CONFIG_SBC_ENCODER 0 ++#define CONFIG_SONIC_ENCODER 0 ++#define CONFIG_SONIC_LS_ENCODER 0 ++#define CONFIG_TRUEHD_ENCODER 0 ++#define CONFIG_TTA_ENCODER 0 ++#define CONFIG_VORBIS_ENCODER 0 ++#define CONFIG_WAVPACK_ENCODER 0 ++#define CONFIG_WMAV1_ENCODER 0 ++#define CONFIG_WMAV2_ENCODER 0 ++#define CONFIG_PCM_ALAW_ENCODER 0 ++#define CONFIG_PCM_DVD_ENCODER 0 ++#define CONFIG_PCM_F32BE_ENCODER 0 ++#define CONFIG_PCM_F32LE_ENCODER 0 ++#define CONFIG_PCM_F64BE_ENCODER 0 ++#define CONFIG_PCM_F64LE_ENCODER 0 ++#define CONFIG_PCM_MULAW_ENCODER 0 ++#define CONFIG_PCM_S8_ENCODER 0 ++#define CONFIG_PCM_S8_PLANAR_ENCODER 0 ++#define CONFIG_PCM_S16BE_ENCODER 0 ++#define CONFIG_PCM_S16BE_PLANAR_ENCODER 0 ++#define CONFIG_PCM_S16LE_ENCODER 0 ++#define CONFIG_PCM_S16LE_PLANAR_ENCODER 0 ++#define CONFIG_PCM_S24BE_ENCODER 0 ++#define CONFIG_PCM_S24DAUD_ENCODER 0 ++#define CONFIG_PCM_S24LE_ENCODER 0 ++#define CONFIG_PCM_S24LE_PLANAR_ENCODER 0 ++#define CONFIG_PCM_S32BE_ENCODER 0 ++#define CONFIG_PCM_S32LE_ENCODER 0 ++#define CONFIG_PCM_S32LE_PLANAR_ENCODER 0 ++#define CONFIG_PCM_S64BE_ENCODER 0 ++#define CONFIG_PCM_S64LE_ENCODER 0 ++#define CONFIG_PCM_U8_ENCODER 0 ++#define CONFIG_PCM_U16BE_ENCODER 0 ++#define CONFIG_PCM_U16LE_ENCODER 0 ++#define CONFIG_PCM_U24BE_ENCODER 0 ++#define CONFIG_PCM_U24LE_ENCODER 0 ++#define CONFIG_PCM_U32BE_ENCODER 0 ++#define CONFIG_PCM_U32LE_ENCODER 0 ++#define CONFIG_PCM_VIDC_ENCODER 0 ++#define CONFIG_ROQ_DPCM_ENCODER 0 ++#define CONFIG_ADPCM_ADX_ENCODER 0 ++#define CONFIG_ADPCM_ARGO_ENCODER 0 ++#define CONFIG_ADPCM_G722_ENCODER 0 ++#define CONFIG_ADPCM_G726_ENCODER 0 ++#define CONFIG_ADPCM_G726LE_ENCODER 0 ++#define CONFIG_ADPCM_IMA_AMV_ENCODER 0 ++#define CONFIG_ADPCM_IMA_ALP_ENCODER 0 ++#define CONFIG_ADPCM_IMA_APM_ENCODER 0 ++#define CONFIG_ADPCM_IMA_QT_ENCODER 0 ++#define CONFIG_ADPCM_IMA_SSI_ENCODER 0 ++#define CONFIG_ADPCM_IMA_WAV_ENCODER 0 ++#define CONFIG_ADPCM_MS_ENCODER 0 ++#define CONFIG_ADPCM_SWF_ENCODER 0 ++#define CONFIG_ADPCM_YAMAHA_ENCODER 0 ++#define CONFIG_SSA_ENCODER 0 ++#define CONFIG_ASS_ENCODER 0 ++#define CONFIG_DVBSUB_ENCODER 0 ++#define CONFIG_DVDSUB_ENCODER 0 ++#define CONFIG_MOVTEXT_ENCODER 0 ++#define CONFIG_SRT_ENCODER 0 ++#define CONFIG_SUBRIP_ENCODER 0 ++#define CONFIG_TEXT_ENCODER 0 ++#define CONFIG_TTML_ENCODER 0 ++#define CONFIG_WEBVTT_ENCODER 0 ++#define CONFIG_XSUB_ENCODER 0 ++#define CONFIG_AAC_AT_ENCODER 0 ++#define CONFIG_ALAC_AT_ENCODER 0 ++#define CONFIG_ILBC_AT_ENCODER 0 ++#define CONFIG_PCM_ALAW_AT_ENCODER 0 ++#define CONFIG_PCM_MULAW_AT_ENCODER 0 ++#define CONFIG_LIBAOM_AV1_ENCODER 0 ++#define CONFIG_LIBCODEC2_ENCODER 0 ++#define CONFIG_LIBFDK_AAC_ENCODER 0 ++#define CONFIG_LIBGSM_ENCODER 0 ++#define CONFIG_LIBGSM_MS_ENCODER 0 ++#define CONFIG_LIBILBC_ENCODER 0 ++#define CONFIG_LIBMP3LAME_ENCODER 0 ++#define CONFIG_LIBOPENCORE_AMRNB_ENCODER 0 ++#define CONFIG_LIBOPENJPEG_ENCODER 0 ++#define CONFIG_LIBOPUS_ENCODER 0 ++#define CONFIG_LIBRAV1E_ENCODER 0 ++#define CONFIG_LIBSHINE_ENCODER 0 ++#define CONFIG_LIBSPEEX_ENCODER 0 ++#define CONFIG_LIBSVTAV1_ENCODER 0 ++#define CONFIG_LIBTHEORA_ENCODER 0 ++#define CONFIG_LIBTWOLAME_ENCODER 0 ++#define CONFIG_LIBVO_AMRWBENC_ENCODER 0 ++#define CONFIG_LIBVORBIS_ENCODER 0 ++#define CONFIG_LIBVPX_VP8_ENCODER 0 ++#define CONFIG_LIBVPX_VP9_ENCODER 0 ++#define CONFIG_LIBWEBP_ANIM_ENCODER 0 ++#define CONFIG_LIBWEBP_ENCODER 0 ++#define CONFIG_LIBX262_ENCODER 0 ++#define CONFIG_LIBX264_ENCODER 0 ++#define CONFIG_LIBX264RGB_ENCODER 0 ++#define CONFIG_LIBX265_ENCODER 0 ++#define CONFIG_LIBXAVS_ENCODER 0 ++#define CONFIG_LIBXAVS2_ENCODER 0 ++#define CONFIG_LIBXVID_ENCODER 0 ++#define CONFIG_AAC_MF_ENCODER 0 ++#define CONFIG_AC3_MF_ENCODER 0 ++#define CONFIG_H263_V4L2M2M_ENCODER 0 ++#define CONFIG_LIBOPENH264_ENCODER 0 ++#define CONFIG_H264_AMF_ENCODER 0 ++#define CONFIG_H264_MF_ENCODER 0 ++#define CONFIG_H264_NVENC_ENCODER 0 ++#define CONFIG_H264_OMX_ENCODER 0 ++#define CONFIG_H264_QSV_ENCODER 0 ++#define CONFIG_H264_V4L2M2M_ENCODER 0 ++#define CONFIG_H264_VAAPI_ENCODER 0 ++#define CONFIG_H264_VIDEOTOOLBOX_ENCODER 0 ++#define CONFIG_NVENC_ENCODER 0 ++#define CONFIG_NVENC_H264_ENCODER 0 ++#define CONFIG_NVENC_HEVC_ENCODER 0 ++#define CONFIG_HEVC_AMF_ENCODER 0 ++#define CONFIG_HEVC_MF_ENCODER 0 ++#define CONFIG_HEVC_NVENC_ENCODER 0 ++#define CONFIG_HEVC_QSV_ENCODER 0 ++#define CONFIG_HEVC_V4L2M2M_ENCODER 0 ++#define CONFIG_HEVC_VAAPI_ENCODER 0 ++#define CONFIG_HEVC_VIDEOTOOLBOX_ENCODER 0 ++#define CONFIG_LIBKVAZAAR_ENCODER 0 ++#define CONFIG_MJPEG_QSV_ENCODER 0 ++#define CONFIG_MJPEG_VAAPI_ENCODER 0 ++#define CONFIG_MP3_MF_ENCODER 0 ++#define CONFIG_MPEG2_QSV_ENCODER 0 ++#define CONFIG_MPEG2_VAAPI_ENCODER 0 ++#define CONFIG_MPEG4_OMX_ENCODER 0 ++#define CONFIG_MPEG4_V4L2M2M_ENCODER 0 ++#define CONFIG_VP8_V4L2M2M_ENCODER 0 ++#define CONFIG_VP8_VAAPI_ENCODER 0 ++#define CONFIG_VP9_VAAPI_ENCODER 0 ++#define CONFIG_VP9_QSV_ENCODER 0 ++#define CONFIG_AV1_D3D11VA_HWACCEL 0 ++#define CONFIG_AV1_D3D11VA2_HWACCEL 0 ++#define CONFIG_AV1_DXVA2_HWACCEL 0 ++#define CONFIG_AV1_NVDEC_HWACCEL 0 ++#define CONFIG_AV1_VAAPI_HWACCEL 0 ++#define CONFIG_H263_VAAPI_HWACCEL 0 ++#define CONFIG_H263_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_H264_D3D11VA_HWACCEL 0 ++#define CONFIG_H264_D3D11VA2_HWACCEL 0 ++#define CONFIG_H264_DXVA2_HWACCEL 0 ++#define CONFIG_H264_NVDEC_HWACCEL 0 ++#define CONFIG_H264_VAAPI_HWACCEL 0 ++#define CONFIG_H264_VDPAU_HWACCEL 0 ++#define CONFIG_H264_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_HEVC_D3D11VA_HWACCEL 0 ++#define CONFIG_HEVC_D3D11VA2_HWACCEL 0 ++#define CONFIG_HEVC_DXVA2_HWACCEL 0 ++#define CONFIG_HEVC_NVDEC_HWACCEL 0 ++#define CONFIG_HEVC_VAAPI_HWACCEL 0 ++#define CONFIG_HEVC_VDPAU_HWACCEL 0 ++#define CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_MJPEG_NVDEC_HWACCEL 0 ++#define CONFIG_MJPEG_VAAPI_HWACCEL 0 ++#define CONFIG_MPEG1_NVDEC_HWACCEL 0 ++#define CONFIG_MPEG1_VDPAU_HWACCEL 0 ++#define CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_MPEG1_XVMC_HWACCEL 0 ++#define CONFIG_MPEG2_D3D11VA_HWACCEL 0 ++#define CONFIG_MPEG2_D3D11VA2_HWACCEL 0 ++#define CONFIG_MPEG2_NVDEC_HWACCEL 0 ++#define CONFIG_MPEG2_DXVA2_HWACCEL 0 ++#define CONFIG_MPEG2_VAAPI_HWACCEL 0 ++#define CONFIG_MPEG2_VDPAU_HWACCEL 0 ++#define CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_MPEG2_XVMC_HWACCEL 0 ++#define CONFIG_MPEG4_NVDEC_HWACCEL 0 ++#define CONFIG_MPEG4_VAAPI_HWACCEL 0 ++#define CONFIG_MPEG4_VDPAU_HWACCEL 0 ++#define CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL 0 ++#define CONFIG_VC1_D3D11VA_HWACCEL 0 ++#define CONFIG_VC1_D3D11VA2_HWACCEL 0 ++#define CONFIG_VC1_DXVA2_HWACCEL 0 ++#define CONFIG_VC1_NVDEC_HWACCEL 0 ++#define CONFIG_VC1_VAAPI_HWACCEL 0 ++#define CONFIG_VC1_VDPAU_HWACCEL 0 ++#define CONFIG_VP8_NVDEC_HWACCEL 0 ++#define CONFIG_VP8_VAAPI_HWACCEL 0 ++#define CONFIG_VP9_D3D11VA_HWACCEL 0 ++#define CONFIG_VP9_D3D11VA2_HWACCEL 0 ++#define CONFIG_VP9_DXVA2_HWACCEL 0 ++#define CONFIG_VP9_NVDEC_HWACCEL 0 ++#define CONFIG_VP9_VAAPI_HWACCEL 0 ++#define CONFIG_VP9_VDPAU_HWACCEL 0 ++#define CONFIG_WMV3_D3D11VA_HWACCEL 0 ++#define CONFIG_WMV3_D3D11VA2_HWACCEL 0 ++#define CONFIG_WMV3_DXVA2_HWACCEL 0 ++#define CONFIG_WMV3_NVDEC_HWACCEL 0 ++#define CONFIG_WMV3_VAAPI_HWACCEL 0 ++#define CONFIG_WMV3_VDPAU_HWACCEL 0 ++#define CONFIG_AAC_PARSER 0 ++#define CONFIG_AAC_LATM_PARSER 0 ++#define CONFIG_AC3_PARSER 0 ++#define CONFIG_ADX_PARSER 0 ++#define CONFIG_AV1_PARSER 0 ++#define CONFIG_AVS2_PARSER 0 ++#define CONFIG_AVS3_PARSER 0 ++#define CONFIG_BMP_PARSER 0 ++#define CONFIG_CAVSVIDEO_PARSER 0 ++#define CONFIG_COOK_PARSER 0 ++#define CONFIG_CRI_PARSER 0 ++#define CONFIG_DCA_PARSER 0 ++#define CONFIG_DIRAC_PARSER 0 ++#define CONFIG_DNXHD_PARSER 0 ++#define CONFIG_DOLBY_E_PARSER 0 ++#define CONFIG_DPX_PARSER 0 ++#define CONFIG_DVAUDIO_PARSER 0 ++#define CONFIG_DVBSUB_PARSER 0 ++#define CONFIG_DVDSUB_PARSER 0 ++#define CONFIG_DVD_NAV_PARSER 0 ++#define CONFIG_FLAC_PARSER 0 ++#define CONFIG_G723_1_PARSER 0 ++#define CONFIG_G729_PARSER 0 ++#define CONFIG_GIF_PARSER 0 ++#define CONFIG_GSM_PARSER 0 ++#define CONFIG_H261_PARSER 0 ++#define CONFIG_H263_PARSER 0 ++#define CONFIG_H264_PARSER 0 ++#define CONFIG_HEVC_PARSER 0 ++#define CONFIG_IPU_PARSER 0 ++#define CONFIG_JPEG2000_PARSER 0 ++#define CONFIG_MJPEG_PARSER 0 ++#define CONFIG_MLP_PARSER 0 ++#define CONFIG_MPEG4VIDEO_PARSER 0 ++#define CONFIG_MPEGAUDIO_PARSER 1 ++#define CONFIG_MPEGVIDEO_PARSER 0 ++#define CONFIG_OPUS_PARSER 0 ++#define CONFIG_PNG_PARSER 0 ++#define CONFIG_PNM_PARSER 0 ++#define CONFIG_RV30_PARSER 0 ++#define CONFIG_RV40_PARSER 0 ++#define CONFIG_SBC_PARSER 0 ++#define CONFIG_SIPR_PARSER 0 ++#define CONFIG_TAK_PARSER 0 ++#define CONFIG_VC1_PARSER 0 ++#define CONFIG_VORBIS_PARSER 0 ++#define CONFIG_VP3_PARSER 0 ++#define CONFIG_VP8_PARSER 0 ++#define CONFIG_VP9_PARSER 0 ++#define CONFIG_WEBP_PARSER 0 ++#define CONFIG_XBM_PARSER 0 ++#define CONFIG_XMA_PARSER 0 ++#define CONFIG_ALSA_INDEV 0 ++#define CONFIG_ANDROID_CAMERA_INDEV 0 ++#define CONFIG_AVFOUNDATION_INDEV 0 ++#define CONFIG_BKTR_INDEV 0 ++#define CONFIG_DECKLINK_INDEV 0 ++#define CONFIG_DSHOW_INDEV 0 ++#define CONFIG_FBDEV_INDEV 0 ++#define CONFIG_GDIGRAB_INDEV 0 ++#define CONFIG_IEC61883_INDEV 0 ++#define CONFIG_JACK_INDEV 0 ++#define CONFIG_KMSGRAB_INDEV 0 ++#define CONFIG_LAVFI_INDEV 0 ++#define CONFIG_OPENAL_INDEV 0 ++#define CONFIG_OSS_INDEV 0 ++#define CONFIG_PULSE_INDEV 0 ++#define CONFIG_SNDIO_INDEV 0 ++#define CONFIG_V4L2_INDEV 0 ++#define CONFIG_VFWCAP_INDEV 0 ++#define CONFIG_XCBGRAB_INDEV 0 ++#define CONFIG_LIBCDIO_INDEV 0 ++#define CONFIG_LIBDC1394_INDEV 0 ++#define CONFIG_ALSA_OUTDEV 0 ++#define CONFIG_AUDIOTOOLBOX_OUTDEV 0 ++#define CONFIG_CACA_OUTDEV 0 ++#define CONFIG_DECKLINK_OUTDEV 0 ++#define CONFIG_FBDEV_OUTDEV 0 ++#define CONFIG_OPENGL_OUTDEV 0 ++#define CONFIG_OSS_OUTDEV 0 ++#define CONFIG_PULSE_OUTDEV 0 ++#define CONFIG_SDL2_OUTDEV 0 ++#define CONFIG_SNDIO_OUTDEV 0 ++#define CONFIG_V4L2_OUTDEV 0 ++#define CONFIG_XV_OUTDEV 0 ++#define CONFIG_ABENCH_FILTER 0 ++#define CONFIG_ACOMPRESSOR_FILTER 0 ++#define CONFIG_ACONTRAST_FILTER 0 ++#define CONFIG_ACOPY_FILTER 0 ++#define CONFIG_ACUE_FILTER 0 ++#define CONFIG_ACROSSFADE_FILTER 0 ++#define CONFIG_ACROSSOVER_FILTER 0 ++#define CONFIG_ACRUSHER_FILTER 0 ++#define CONFIG_ADECLICK_FILTER 0 ++#define CONFIG_ADECLIP_FILTER 0 ++#define CONFIG_ADELAY_FILTER 0 ++#define CONFIG_ADENORM_FILTER 0 ++#define CONFIG_ADERIVATIVE_FILTER 0 ++#define CONFIG_AECHO_FILTER 0 ++#define CONFIG_AEMPHASIS_FILTER 0 ++#define CONFIG_AEVAL_FILTER 0 ++#define CONFIG_AEXCITER_FILTER 0 ++#define CONFIG_AFADE_FILTER 0 ++#define CONFIG_AFFTDN_FILTER 0 ++#define CONFIG_AFFTFILT_FILTER 0 ++#define CONFIG_AFIR_FILTER 0 ++#define CONFIG_AFORMAT_FILTER 0 ++#define CONFIG_AFREQSHIFT_FILTER 0 ++#define CONFIG_AGATE_FILTER 0 ++#define CONFIG_AIIR_FILTER 0 ++#define CONFIG_AINTEGRAL_FILTER 0 ++#define CONFIG_AINTERLEAVE_FILTER 0 ++#define CONFIG_ALIMITER_FILTER 0 ++#define CONFIG_ALLPASS_FILTER 0 ++#define CONFIG_ALOOP_FILTER 0 ++#define CONFIG_AMERGE_FILTER 0 ++#define CONFIG_AMETADATA_FILTER 0 ++#define CONFIG_AMIX_FILTER 0 ++#define CONFIG_AMULTIPLY_FILTER 0 ++#define CONFIG_ANEQUALIZER_FILTER 0 ++#define CONFIG_ANLMDN_FILTER 0 ++#define CONFIG_ANLMS_FILTER 0 ++#define CONFIG_ANULL_FILTER 0 ++#define CONFIG_APAD_FILTER 0 ++#define CONFIG_APERMS_FILTER 0 ++#define CONFIG_APHASER_FILTER 0 ++#define CONFIG_APHASESHIFT_FILTER 0 ++#define CONFIG_APULSATOR_FILTER 0 ++#define CONFIG_AREALTIME_FILTER 0 ++#define CONFIG_ARESAMPLE_FILTER 0 ++#define CONFIG_AREVERSE_FILTER 0 ++#define CONFIG_ARNNDN_FILTER 0 ++#define CONFIG_ASELECT_FILTER 0 ++#define CONFIG_ASENDCMD_FILTER 0 ++#define CONFIG_ASETNSAMPLES_FILTER 0 ++#define CONFIG_ASETPTS_FILTER 0 ++#define CONFIG_ASETRATE_FILTER 0 ++#define CONFIG_ASETTB_FILTER 0 ++#define CONFIG_ASHOWINFO_FILTER 0 ++#define CONFIG_ASIDEDATA_FILTER 0 ++#define CONFIG_ASOFTCLIP_FILTER 0 ++#define CONFIG_ASPLIT_FILTER 0 ++#define CONFIG_ASR_FILTER 0 ++#define CONFIG_ASTATS_FILTER 0 ++#define CONFIG_ASTREAMSELECT_FILTER 0 ++#define CONFIG_ASUBBOOST_FILTER 0 ++#define CONFIG_ASUBCUT_FILTER 0 ++#define CONFIG_ASUPERCUT_FILTER 0 ++#define CONFIG_ASUPERPASS_FILTER 0 ++#define CONFIG_ASUPERSTOP_FILTER 0 ++#define CONFIG_ATEMPO_FILTER 0 ++#define CONFIG_ATRIM_FILTER 0 ++#define CONFIG_AXCORRELATE_FILTER 0 ++#define CONFIG_AZMQ_FILTER 0 ++#define CONFIG_BANDPASS_FILTER 0 ++#define CONFIG_BANDREJECT_FILTER 0 ++#define CONFIG_BASS_FILTER 0 ++#define CONFIG_BIQUAD_FILTER 0 ++#define CONFIG_BS2B_FILTER 0 ++#define CONFIG_CHROMABER_VULKAN_FILTER 0 ++#define CONFIG_CHANNELMAP_FILTER 0 ++#define CONFIG_CHANNELSPLIT_FILTER 0 ++#define CONFIG_CHORUS_FILTER 0 ++#define CONFIG_COMPAND_FILTER 0 ++#define CONFIG_COMPENSATIONDELAY_FILTER 0 ++#define CONFIG_CROSSFEED_FILTER 0 ++#define CONFIG_CRYSTALIZER_FILTER 0 ++#define CONFIG_DCSHIFT_FILTER 0 ++#define CONFIG_DEESSER_FILTER 0 ++#define CONFIG_DRMETER_FILTER 0 ++#define CONFIG_DYNAUDNORM_FILTER 0 ++#define CONFIG_EARWAX_FILTER 0 ++#define CONFIG_EBUR128_FILTER 0 ++#define CONFIG_EQUALIZER_FILTER 0 ++#define CONFIG_EXTRASTEREO_FILTER 0 ++#define CONFIG_FIREQUALIZER_FILTER 0 ++#define CONFIG_FLANGER_FILTER 0 ++#define CONFIG_HAAS_FILTER 0 ++#define CONFIG_HDCD_FILTER 0 ++#define CONFIG_HEADPHONE_FILTER 0 ++#define CONFIG_HIGHPASS_FILTER 0 ++#define CONFIG_HIGHSHELF_FILTER 0 ++#define CONFIG_JOIN_FILTER 0 ++#define CONFIG_LADSPA_FILTER 0 ++#define CONFIG_LOUDNORM_FILTER 0 ++#define CONFIG_LOWPASS_FILTER 0 ++#define CONFIG_LOWSHELF_FILTER 0 ++#define CONFIG_LV2_FILTER 0 ++#define CONFIG_MCOMPAND_FILTER 0 ++#define CONFIG_PAN_FILTER 0 ++#define CONFIG_REPLAYGAIN_FILTER 0 ++#define CONFIG_RESAMPLE_FILTER 0 ++#define CONFIG_RUBBERBAND_FILTER 0 ++#define CONFIG_SIDECHAINCOMPRESS_FILTER 0 ++#define CONFIG_SIDECHAINGATE_FILTER 0 ++#define CONFIG_SILENCEDETECT_FILTER 0 ++#define CONFIG_SILENCEREMOVE_FILTER 0 ++#define CONFIG_SOFALIZER_FILTER 0 ++#define CONFIG_SPEECHNORM_FILTER 0 ++#define CONFIG_STEREOTOOLS_FILTER 0 ++#define CONFIG_STEREOWIDEN_FILTER 0 ++#define CONFIG_SUPEREQUALIZER_FILTER 0 ++#define CONFIG_SURROUND_FILTER 0 ++#define CONFIG_TREBLE_FILTER 0 ++#define CONFIG_TREMOLO_FILTER 0 ++#define CONFIG_VIBRATO_FILTER 0 ++#define CONFIG_VOLUME_FILTER 0 ++#define CONFIG_VOLUMEDETECT_FILTER 0 ++#define CONFIG_AEVALSRC_FILTER 0 ++#define CONFIG_AFIRSRC_FILTER 0 ++#define CONFIG_ANOISESRC_FILTER 0 ++#define CONFIG_ANULLSRC_FILTER 0 ++#define CONFIG_FLITE_FILTER 0 ++#define CONFIG_HILBERT_FILTER 0 ++#define CONFIG_SINC_FILTER 0 ++#define CONFIG_SINE_FILTER 0 ++#define CONFIG_ANULLSINK_FILTER 0 ++#define CONFIG_ADDROI_FILTER 0 ++#define CONFIG_ALPHAEXTRACT_FILTER 0 ++#define CONFIG_ALPHAMERGE_FILTER 0 ++#define CONFIG_AMPLIFY_FILTER 0 ++#define CONFIG_ASS_FILTER 0 ++#define CONFIG_ATADENOISE_FILTER 0 ++#define CONFIG_AVGBLUR_FILTER 0 ++#define CONFIG_AVGBLUR_OPENCL_FILTER 0 ++#define CONFIG_AVGBLUR_VULKAN_FILTER 0 ++#define CONFIG_BBOX_FILTER 0 ++#define CONFIG_BENCH_FILTER 0 ++#define CONFIG_BILATERAL_FILTER 0 ++#define CONFIG_BITPLANENOISE_FILTER 0 ++#define CONFIG_BLACKDETECT_FILTER 0 ++#define CONFIG_BLACKFRAME_FILTER 0 ++#define CONFIG_BLEND_FILTER 0 ++#define CONFIG_BM3D_FILTER 0 ++#define CONFIG_BOXBLUR_FILTER 0 ++#define CONFIG_BOXBLUR_OPENCL_FILTER 0 ++#define CONFIG_BWDIF_FILTER 0 ++#define CONFIG_CAS_FILTER 0 ++#define CONFIG_CHROMAHOLD_FILTER 0 ++#define CONFIG_CHROMAKEY_FILTER 0 ++#define CONFIG_CHROMANR_FILTER 0 ++#define CONFIG_CHROMASHIFT_FILTER 0 ++#define CONFIG_CIESCOPE_FILTER 0 ++#define CONFIG_CODECVIEW_FILTER 0 ++#define CONFIG_COLORBALANCE_FILTER 0 ++#define CONFIG_COLORCHANNELMIXER_FILTER 0 ++#define CONFIG_COLORCONTRAST_FILTER 0 ++#define CONFIG_COLORCORRECT_FILTER 0 ++#define CONFIG_COLORIZE_FILTER 0 ++#define CONFIG_COLORKEY_FILTER 0 ++#define CONFIG_COLORKEY_OPENCL_FILTER 0 ++#define CONFIG_COLORHOLD_FILTER 0 ++#define CONFIG_COLORLEVELS_FILTER 0 ++#define CONFIG_COLORMATRIX_FILTER 0 ++#define CONFIG_COLORSPACE_FILTER 0 ++#define CONFIG_COLORTEMPERATURE_FILTER 0 ++#define CONFIG_CONVOLUTION_FILTER 0 ++#define CONFIG_CONVOLUTION_OPENCL_FILTER 0 ++#define CONFIG_CONVOLVE_FILTER 0 ++#define CONFIG_COPY_FILTER 0 ++#define CONFIG_COREIMAGE_FILTER 0 ++#define CONFIG_COVER_RECT_FILTER 0 ++#define CONFIG_CROP_FILTER 0 ++#define CONFIG_CROPDETECT_FILTER 0 ++#define CONFIG_CUE_FILTER 0 ++#define CONFIG_CURVES_FILTER 0 ++#define CONFIG_DATASCOPE_FILTER 0 ++#define CONFIG_DBLUR_FILTER 0 ++#define CONFIG_DCTDNOIZ_FILTER 0 ++#define CONFIG_DEBAND_FILTER 0 ++#define CONFIG_DEBLOCK_FILTER 0 ++#define CONFIG_DECIMATE_FILTER 0 ++#define CONFIG_DECONVOLVE_FILTER 0 ++#define CONFIG_DEDOT_FILTER 0 ++#define CONFIG_DEFLATE_FILTER 0 ++#define CONFIG_DEFLICKER_FILTER 0 ++#define CONFIG_DEINTERLACE_QSV_FILTER 0 ++#define CONFIG_DEINTERLACE_VAAPI_FILTER 0 ++#define CONFIG_DEJUDDER_FILTER 0 ++#define CONFIG_DELOGO_FILTER 0 ++#define CONFIG_DENOISE_VAAPI_FILTER 0 ++#define CONFIG_DERAIN_FILTER 0 ++#define CONFIG_DESHAKE_FILTER 0 ++#define CONFIG_DESHAKE_OPENCL_FILTER 0 ++#define CONFIG_DESPILL_FILTER 0 ++#define CONFIG_DETELECINE_FILTER 0 ++#define CONFIG_DILATION_FILTER 0 ++#define CONFIG_DILATION_OPENCL_FILTER 0 ++#define CONFIG_DISPLACE_FILTER 0 ++#define CONFIG_DNN_PROCESSING_FILTER 0 ++#define CONFIG_DOUBLEWEAVE_FILTER 0 ++#define CONFIG_DRAWBOX_FILTER 0 ++#define CONFIG_DRAWGRAPH_FILTER 0 ++#define CONFIG_DRAWGRID_FILTER 0 ++#define CONFIG_DRAWTEXT_FILTER 0 ++#define CONFIG_EDGEDETECT_FILTER 0 ++#define CONFIG_ELBG_FILTER 0 ++#define CONFIG_ENTROPY_FILTER 0 ++#define CONFIG_EPX_FILTER 0 ++#define CONFIG_EQ_FILTER 0 ++#define CONFIG_EROSION_FILTER 0 ++#define CONFIG_EROSION_OPENCL_FILTER 0 ++#define CONFIG_ESTDIF_FILTER 0 ++#define CONFIG_EXPOSURE_FILTER 0 ++#define CONFIG_EXTRACTPLANES_FILTER 0 ++#define CONFIG_FADE_FILTER 0 ++#define CONFIG_FFTDNOIZ_FILTER 0 ++#define CONFIG_FFTFILT_FILTER 0 ++#define CONFIG_FIELD_FILTER 0 ++#define CONFIG_FIELDHINT_FILTER 0 ++#define CONFIG_FIELDMATCH_FILTER 0 ++#define CONFIG_FIELDORDER_FILTER 0 ++#define CONFIG_FILLBORDERS_FILTER 0 ++#define CONFIG_FIND_RECT_FILTER 0 ++#define CONFIG_FLOODFILL_FILTER 0 ++#define CONFIG_FORMAT_FILTER 0 ++#define CONFIG_FPS_FILTER 0 ++#define CONFIG_FRAMEPACK_FILTER 0 ++#define CONFIG_FRAMERATE_FILTER 0 ++#define CONFIG_FRAMESTEP_FILTER 0 ++#define CONFIG_FREEZEDETECT_FILTER 0 ++#define CONFIG_FREEZEFRAMES_FILTER 0 ++#define CONFIG_FREI0R_FILTER 0 ++#define CONFIG_FSPP_FILTER 0 ++#define CONFIG_GBLUR_FILTER 0 ++#define CONFIG_GEQ_FILTER 0 ++#define CONFIG_GRADFUN_FILTER 0 ++#define CONFIG_GRAPHMONITOR_FILTER 0 ++#define CONFIG_GREYEDGE_FILTER 0 ++#define CONFIG_HALDCLUT_FILTER 0 ++#define CONFIG_HFLIP_FILTER 0 ++#define CONFIG_HISTEQ_FILTER 0 ++#define CONFIG_HISTOGRAM_FILTER 0 ++#define CONFIG_HQDN3D_FILTER 0 ++#define CONFIG_HQX_FILTER 0 ++#define CONFIG_HSTACK_FILTER 0 ++#define CONFIG_HUE_FILTER 0 ++#define CONFIG_HWDOWNLOAD_FILTER 0 ++#define CONFIG_HWMAP_FILTER 0 ++#define CONFIG_HWUPLOAD_FILTER 0 ++#define CONFIG_HWUPLOAD_CUDA_FILTER 0 ++#define CONFIG_HYSTERESIS_FILTER 0 ++#define CONFIG_IDENTITY_FILTER 0 ++#define CONFIG_IDET_FILTER 0 ++#define CONFIG_IL_FILTER 0 ++#define CONFIG_INFLATE_FILTER 0 ++#define CONFIG_INTERLACE_FILTER 0 ++#define CONFIG_INTERLEAVE_FILTER 0 ++#define CONFIG_KERNDEINT_FILTER 0 ++#define CONFIG_KIRSCH_FILTER 0 ++#define CONFIG_LAGFUN_FILTER 0 ++#define CONFIG_LENSCORRECTION_FILTER 0 ++#define CONFIG_LENSFUN_FILTER 0 ++#define CONFIG_LIBVMAF_FILTER 0 ++#define CONFIG_LIMITER_FILTER 0 ++#define CONFIG_LOOP_FILTER 0 ++#define CONFIG_LUMAKEY_FILTER 0 ++#define CONFIG_LUT_FILTER 0 ++#define CONFIG_LUT1D_FILTER 0 ++#define CONFIG_LUT2_FILTER 0 ++#define CONFIG_LUT3D_FILTER 0 ++#define CONFIG_LUTRGB_FILTER 0 ++#define CONFIG_LUTYUV_FILTER 0 ++#define CONFIG_MASKEDCLAMP_FILTER 0 ++#define CONFIG_MASKEDMAX_FILTER 0 ++#define CONFIG_MASKEDMERGE_FILTER 0 ++#define CONFIG_MASKEDMIN_FILTER 0 ++#define CONFIG_MASKEDTHRESHOLD_FILTER 0 ++#define CONFIG_MASKFUN_FILTER 0 ++#define CONFIG_MCDEINT_FILTER 0 ++#define CONFIG_MEDIAN_FILTER 0 ++#define CONFIG_MERGEPLANES_FILTER 0 ++#define CONFIG_MESTIMATE_FILTER 0 ++#define CONFIG_METADATA_FILTER 0 ++#define CONFIG_MIDEQUALIZER_FILTER 0 ++#define CONFIG_MINTERPOLATE_FILTER 0 ++#define CONFIG_MIX_FILTER 0 ++#define CONFIG_MONOCHROME_FILTER 0 ++#define CONFIG_MPDECIMATE_FILTER 0 ++#define CONFIG_MSAD_FILTER 0 ++#define CONFIG_NEGATE_FILTER 0 ++#define CONFIG_NLMEANS_FILTER 0 ++#define CONFIG_NLMEANS_OPENCL_FILTER 0 ++#define CONFIG_NNEDI_FILTER 0 ++#define CONFIG_NOFORMAT_FILTER 0 ++#define CONFIG_NOISE_FILTER 0 ++#define CONFIG_NORMALIZE_FILTER 0 ++#define CONFIG_NULL_FILTER 0 ++#define CONFIG_OCR_FILTER 0 ++#define CONFIG_OCV_FILTER 0 ++#define CONFIG_OSCILLOSCOPE_FILTER 0 ++#define CONFIG_OVERLAY_FILTER 0 ++#define CONFIG_OVERLAY_OPENCL_FILTER 0 ++#define CONFIG_OVERLAY_QSV_FILTER 0 ++#define CONFIG_OVERLAY_VULKAN_FILTER 0 ++#define CONFIG_OVERLAY_CUDA_FILTER 0 ++#define CONFIG_OWDENOISE_FILTER 0 ++#define CONFIG_PAD_FILTER 0 ++#define CONFIG_PAD_OPENCL_FILTER 0 ++#define CONFIG_PALETTEGEN_FILTER 0 ++#define CONFIG_PALETTEUSE_FILTER 0 ++#define CONFIG_PERMS_FILTER 0 ++#define CONFIG_PERSPECTIVE_FILTER 0 ++#define CONFIG_PHASE_FILTER 0 ++#define CONFIG_PHOTOSENSITIVITY_FILTER 0 ++#define CONFIG_PIXDESCTEST_FILTER 0 ++#define CONFIG_PIXSCOPE_FILTER 0 ++#define CONFIG_PP_FILTER 0 ++#define CONFIG_PP7_FILTER 0 ++#define CONFIG_PREMULTIPLY_FILTER 0 ++#define CONFIG_PREWITT_FILTER 0 ++#define CONFIG_PREWITT_OPENCL_FILTER 0 ++#define CONFIG_PROCAMP_VAAPI_FILTER 0 ++#define CONFIG_PROGRAM_OPENCL_FILTER 0 ++#define CONFIG_PSEUDOCOLOR_FILTER 0 ++#define CONFIG_PSNR_FILTER 0 ++#define CONFIG_PULLUP_FILTER 0 ++#define CONFIG_QP_FILTER 0 ++#define CONFIG_RANDOM_FILTER 0 ++#define CONFIG_READEIA608_FILTER 0 ++#define CONFIG_READVITC_FILTER 0 ++#define CONFIG_REALTIME_FILTER 0 ++#define CONFIG_REMAP_FILTER 0 ++#define CONFIG_REMOVEGRAIN_FILTER 0 ++#define CONFIG_REMOVELOGO_FILTER 0 ++#define CONFIG_REPEATFIELDS_FILTER 0 ++#define CONFIG_REVERSE_FILTER 0 ++#define CONFIG_RGBASHIFT_FILTER 0 ++#define CONFIG_ROBERTS_FILTER 0 ++#define CONFIG_ROBERTS_OPENCL_FILTER 0 ++#define CONFIG_ROTATE_FILTER 0 ++#define CONFIG_SAB_FILTER 0 ++#define CONFIG_SCALE_FILTER 0 ++#define CONFIG_SCALE_CUDA_FILTER 0 ++#define CONFIG_SCALE_NPP_FILTER 0 ++#define CONFIG_SCALE_QSV_FILTER 0 ++#define CONFIG_SCALE_VAAPI_FILTER 0 ++#define CONFIG_SCALE_VULKAN_FILTER 0 ++#define CONFIG_SCALE2REF_FILTER 0 ++#define CONFIG_SCDET_FILTER 0 ++#define CONFIG_SCROLL_FILTER 0 ++#define CONFIG_SELECT_FILTER 0 ++#define CONFIG_SELECTIVECOLOR_FILTER 0 ++#define CONFIG_SENDCMD_FILTER 0 ++#define CONFIG_SEPARATEFIELDS_FILTER 0 ++#define CONFIG_SETDAR_FILTER 0 ++#define CONFIG_SETFIELD_FILTER 0 ++#define CONFIG_SETPARAMS_FILTER 0 ++#define CONFIG_SETPTS_FILTER 0 ++#define CONFIG_SETRANGE_FILTER 0 ++#define CONFIG_SETSAR_FILTER 0 ++#define CONFIG_SETTB_FILTER 0 ++#define CONFIG_SHARPNESS_VAAPI_FILTER 0 ++#define CONFIG_SHEAR_FILTER 0 ++#define CONFIG_SHOWINFO_FILTER 0 ++#define CONFIG_SHOWPALETTE_FILTER 0 ++#define CONFIG_SHUFFLEFRAMES_FILTER 0 ++#define CONFIG_SHUFFLEPIXELS_FILTER 0 ++#define CONFIG_SHUFFLEPLANES_FILTER 0 ++#define CONFIG_SIDEDATA_FILTER 0 ++#define CONFIG_SIGNALSTATS_FILTER 0 ++#define CONFIG_SIGNATURE_FILTER 0 ++#define CONFIG_SMARTBLUR_FILTER 0 ++#define CONFIG_SOBEL_FILTER 0 ++#define CONFIG_SOBEL_OPENCL_FILTER 0 ++#define CONFIG_SPLIT_FILTER 0 ++#define CONFIG_SPP_FILTER 0 ++#define CONFIG_SR_FILTER 0 ++#define CONFIG_SSIM_FILTER 0 ++#define CONFIG_STEREO3D_FILTER 0 ++#define CONFIG_STREAMSELECT_FILTER 0 ++#define CONFIG_SUBTITLES_FILTER 0 ++#define CONFIG_SUPER2XSAI_FILTER 0 ++#define CONFIG_SWAPRECT_FILTER 0 ++#define CONFIG_SWAPUV_FILTER 0 ++#define CONFIG_TBLEND_FILTER 0 ++#define CONFIG_TELECINE_FILTER 0 ++#define CONFIG_THISTOGRAM_FILTER 0 ++#define CONFIG_THRESHOLD_FILTER 0 ++#define CONFIG_THUMBNAIL_FILTER 0 ++#define CONFIG_THUMBNAIL_CUDA_FILTER 0 ++#define CONFIG_TILE_FILTER 0 ++#define CONFIG_TINTERLACE_FILTER 0 ++#define CONFIG_TLUT2_FILTER 0 ++#define CONFIG_TMEDIAN_FILTER 0 ++#define CONFIG_TMIDEQUALIZER_FILTER 0 ++#define CONFIG_TMIX_FILTER 0 ++#define CONFIG_TONEMAP_FILTER 0 ++#define CONFIG_TONEMAP_OPENCL_FILTER 0 ++#define CONFIG_TONEMAP_VAAPI_FILTER 0 ++#define CONFIG_TPAD_FILTER 0 ++#define CONFIG_TRANSPOSE_FILTER 0 ++#define CONFIG_TRANSPOSE_NPP_FILTER 0 ++#define CONFIG_TRANSPOSE_OPENCL_FILTER 0 ++#define CONFIG_TRANSPOSE_VAAPI_FILTER 0 ++#define CONFIG_TRIM_FILTER 0 ++#define CONFIG_UNPREMULTIPLY_FILTER 0 ++#define CONFIG_UNSHARP_FILTER 0 ++#define CONFIG_UNSHARP_OPENCL_FILTER 0 ++#define CONFIG_UNTILE_FILTER 0 ++#define CONFIG_USPP_FILTER 0 ++#define CONFIG_V360_FILTER 0 ++#define CONFIG_VAGUEDENOISER_FILTER 0 ++#define CONFIG_VECTORSCOPE_FILTER 0 ++#define CONFIG_VFLIP_FILTER 0 ++#define CONFIG_VFRDET_FILTER 0 ++#define CONFIG_VIBRANCE_FILTER 0 ++#define CONFIG_VIDSTABDETECT_FILTER 0 ++#define CONFIG_VIDSTABTRANSFORM_FILTER 0 ++#define CONFIG_VIF_FILTER 0 ++#define CONFIG_VIGNETTE_FILTER 0 ++#define CONFIG_VMAFMOTION_FILTER 0 ++#define CONFIG_VPP_QSV_FILTER 0 ++#define CONFIG_VSTACK_FILTER 0 ++#define CONFIG_W3FDIF_FILTER 0 ++#define CONFIG_WAVEFORM_FILTER 0 ++#define CONFIG_WEAVE_FILTER 0 ++#define CONFIG_XBR_FILTER 0 ++#define CONFIG_XFADE_FILTER 0 ++#define CONFIG_XFADE_OPENCL_FILTER 0 ++#define CONFIG_XMEDIAN_FILTER 0 ++#define CONFIG_XSTACK_FILTER 0 ++#define CONFIG_YADIF_FILTER 0 ++#define CONFIG_YADIF_CUDA_FILTER 0 ++#define CONFIG_YAEPBLUR_FILTER 0 ++#define CONFIG_ZMQ_FILTER 0 ++#define CONFIG_ZOOMPAN_FILTER 0 ++#define CONFIG_ZSCALE_FILTER 0 ++#define CONFIG_ALLRGB_FILTER 0 ++#define CONFIG_ALLYUV_FILTER 0 ++#define CONFIG_CELLAUTO_FILTER 0 ++#define CONFIG_COLOR_FILTER 0 ++#define CONFIG_COREIMAGESRC_FILTER 0 ++#define CONFIG_FREI0R_SRC_FILTER 0 ++#define CONFIG_GRADIENTS_FILTER 0 ++#define CONFIG_HALDCLUTSRC_FILTER 0 ++#define CONFIG_LIFE_FILTER 0 ++#define CONFIG_MANDELBROT_FILTER 0 ++#define CONFIG_MPTESTSRC_FILTER 0 ++#define CONFIG_NULLSRC_FILTER 0 ++#define CONFIG_OPENCLSRC_FILTER 0 ++#define CONFIG_PAL75BARS_FILTER 0 ++#define CONFIG_PAL100BARS_FILTER 0 ++#define CONFIG_RGBTESTSRC_FILTER 0 ++#define CONFIG_SIERPINSKI_FILTER 0 ++#define CONFIG_SMPTEBARS_FILTER 0 ++#define CONFIG_SMPTEHDBARS_FILTER 0 ++#define CONFIG_TESTSRC_FILTER 0 ++#define CONFIG_TESTSRC2_FILTER 0 ++#define CONFIG_YUVTESTSRC_FILTER 0 ++#define CONFIG_NULLSINK_FILTER 0 ++#define CONFIG_ABITSCOPE_FILTER 0 ++#define CONFIG_ADRAWGRAPH_FILTER 0 ++#define CONFIG_AGRAPHMONITOR_FILTER 0 ++#define CONFIG_AHISTOGRAM_FILTER 0 ++#define CONFIG_APHASEMETER_FILTER 0 ++#define CONFIG_AVECTORSCOPE_FILTER 0 ++#define CONFIG_CONCAT_FILTER 0 ++#define CONFIG_SHOWCQT_FILTER 0 ++#define CONFIG_SHOWFREQS_FILTER 0 ++#define CONFIG_SHOWSPATIAL_FILTER 0 ++#define CONFIG_SHOWSPECTRUM_FILTER 0 ++#define CONFIG_SHOWSPECTRUMPIC_FILTER 0 ++#define CONFIG_SHOWVOLUME_FILTER 0 ++#define CONFIG_SHOWWAVES_FILTER 0 ++#define CONFIG_SHOWWAVESPIC_FILTER 0 ++#define CONFIG_SPECTRUMSYNTH_FILTER 0 ++#define CONFIG_AMOVIE_FILTER 0 ++#define CONFIG_MOVIE_FILTER 0 ++#define CONFIG_AFIFO_FILTER 0 ++#define CONFIG_FIFO_FILTER 0 ++#define CONFIG_AA_DEMUXER 0 ++#define CONFIG_AAC_DEMUXER 0 ++#define CONFIG_AAX_DEMUXER 0 ++#define CONFIG_AC3_DEMUXER 0 ++#define CONFIG_ACE_DEMUXER 0 ++#define CONFIG_ACM_DEMUXER 0 ++#define CONFIG_ACT_DEMUXER 0 ++#define CONFIG_ADF_DEMUXER 0 ++#define CONFIG_ADP_DEMUXER 0 ++#define CONFIG_ADS_DEMUXER 0 ++#define CONFIG_ADX_DEMUXER 0 ++#define CONFIG_AEA_DEMUXER 0 ++#define CONFIG_AFC_DEMUXER 0 ++#define CONFIG_AIFF_DEMUXER 0 ++#define CONFIG_AIX_DEMUXER 0 ++#define CONFIG_ALP_DEMUXER 0 ++#define CONFIG_AMR_DEMUXER 0 ++#define CONFIG_AMRNB_DEMUXER 0 ++#define CONFIG_AMRWB_DEMUXER 0 ++#define CONFIG_ANM_DEMUXER 0 ++#define CONFIG_APC_DEMUXER 0 ++#define CONFIG_APE_DEMUXER 0 ++#define CONFIG_APM_DEMUXER 0 ++#define CONFIG_APNG_DEMUXER 0 ++#define CONFIG_APTX_DEMUXER 0 ++#define CONFIG_APTX_HD_DEMUXER 0 ++#define CONFIG_AQTITLE_DEMUXER 0 ++#define CONFIG_ARGO_ASF_DEMUXER 0 ++#define CONFIG_ARGO_BRP_DEMUXER 0 ++#define CONFIG_ASF_DEMUXER 1 ++#define CONFIG_ASF_O_DEMUXER 0 ++#define CONFIG_ASS_DEMUXER 0 ++#define CONFIG_AST_DEMUXER 0 ++#define CONFIG_AU_DEMUXER 0 ++#define CONFIG_AV1_DEMUXER 0 ++#define CONFIG_AVI_DEMUXER 0 ++#define CONFIG_AVISYNTH_DEMUXER 0 ++#define CONFIG_AVR_DEMUXER 0 ++#define CONFIG_AVS_DEMUXER 0 ++#define CONFIG_AVS2_DEMUXER 0 ++#define CONFIG_AVS3_DEMUXER 0 ++#define CONFIG_BETHSOFTVID_DEMUXER 0 ++#define CONFIG_BFI_DEMUXER 0 ++#define CONFIG_BINTEXT_DEMUXER 0 ++#define CONFIG_BINK_DEMUXER 0 ++#define CONFIG_BINKA_DEMUXER 0 ++#define CONFIG_BIT_DEMUXER 0 ++#define CONFIG_BMV_DEMUXER 0 ++#define CONFIG_BFSTM_DEMUXER 0 ++#define CONFIG_BRSTM_DEMUXER 0 ++#define CONFIG_BOA_DEMUXER 0 ++#define CONFIG_C93_DEMUXER 0 ++#define CONFIG_CAF_DEMUXER 0 ++#define CONFIG_CAVSVIDEO_DEMUXER 0 ++#define CONFIG_CDG_DEMUXER 0 ++#define CONFIG_CDXL_DEMUXER 0 ++#define CONFIG_CINE_DEMUXER 0 ++#define CONFIG_CODEC2_DEMUXER 0 ++#define CONFIG_CODEC2RAW_DEMUXER 0 ++#define CONFIG_CONCAT_DEMUXER 0 ++#define CONFIG_DASH_DEMUXER 0 ++#define CONFIG_DATA_DEMUXER 0 ++#define CONFIG_DAUD_DEMUXER 0 ++#define CONFIG_DCSTR_DEMUXER 0 ++#define CONFIG_DERF_DEMUXER 0 ++#define CONFIG_DFA_DEMUXER 0 ++#define CONFIG_DHAV_DEMUXER 0 ++#define CONFIG_DIRAC_DEMUXER 0 ++#define CONFIG_DNXHD_DEMUXER 0 ++#define CONFIG_DSF_DEMUXER 0 ++#define CONFIG_DSICIN_DEMUXER 0 ++#define CONFIG_DSS_DEMUXER 0 ++#define CONFIG_DTS_DEMUXER 0 ++#define CONFIG_DTSHD_DEMUXER 0 ++#define CONFIG_DV_DEMUXER 0 ++#define CONFIG_DVBSUB_DEMUXER 0 ++#define CONFIG_DVBTXT_DEMUXER 0 ++#define CONFIG_DXA_DEMUXER 0 ++#define CONFIG_EA_DEMUXER 0 ++#define CONFIG_EA_CDATA_DEMUXER 0 ++#define CONFIG_EAC3_DEMUXER 0 ++#define CONFIG_EPAF_DEMUXER 0 ++#define CONFIG_FFMETADATA_DEMUXER 0 ++#define CONFIG_FILMSTRIP_DEMUXER 0 ++#define CONFIG_FITS_DEMUXER 0 ++#define CONFIG_FLAC_DEMUXER 0 ++#define CONFIG_FLIC_DEMUXER 0 ++#define CONFIG_FLV_DEMUXER 0 ++#define CONFIG_LIVE_FLV_DEMUXER 0 ++#define CONFIG_FOURXM_DEMUXER 0 ++#define CONFIG_FRM_DEMUXER 0 ++#define CONFIG_FSB_DEMUXER 0 ++#define CONFIG_FWSE_DEMUXER 0 ++#define CONFIG_G722_DEMUXER 0 ++#define CONFIG_G723_1_DEMUXER 0 ++#define CONFIG_G726_DEMUXER 0 ++#define CONFIG_G726LE_DEMUXER 0 ++#define CONFIG_G729_DEMUXER 0 ++#define CONFIG_GDV_DEMUXER 0 ++#define CONFIG_GENH_DEMUXER 0 ++#define CONFIG_GIF_DEMUXER 0 ++#define CONFIG_GSM_DEMUXER 0 ++#define CONFIG_GXF_DEMUXER 0 ++#define CONFIG_H261_DEMUXER 0 ++#define CONFIG_H263_DEMUXER 0 ++#define CONFIG_H264_DEMUXER 0 ++#define CONFIG_HCA_DEMUXER 0 ++#define CONFIG_HCOM_DEMUXER 0 ++#define CONFIG_HEVC_DEMUXER 0 ++#define CONFIG_HLS_DEMUXER 0 ++#define CONFIG_HNM_DEMUXER 0 ++#define CONFIG_ICO_DEMUXER 0 ++#define CONFIG_IDCIN_DEMUXER 0 ++#define CONFIG_IDF_DEMUXER 0 ++#define CONFIG_IFF_DEMUXER 0 ++#define CONFIG_IFV_DEMUXER 0 ++#define CONFIG_ILBC_DEMUXER 0 ++#define CONFIG_IMAGE2_DEMUXER 0 ++#define CONFIG_IMAGE2PIPE_DEMUXER 0 ++#define CONFIG_IMAGE2_ALIAS_PIX_DEMUXER 0 ++#define CONFIG_IMAGE2_BRENDER_PIX_DEMUXER 0 ++#define CONFIG_INGENIENT_DEMUXER 0 ++#define CONFIG_IPMOVIE_DEMUXER 0 ++#define CONFIG_IPU_DEMUXER 0 ++#define CONFIG_IRCAM_DEMUXER 0 ++#define CONFIG_ISS_DEMUXER 0 ++#define CONFIG_IV8_DEMUXER 0 ++#define CONFIG_IVF_DEMUXER 0 ++#define CONFIG_IVR_DEMUXER 0 ++#define CONFIG_JACOSUB_DEMUXER 0 ++#define CONFIG_JV_DEMUXER 0 ++#define CONFIG_KUX_DEMUXER 0 ++#define CONFIG_KVAG_DEMUXER 0 ++#define CONFIG_LMLM4_DEMUXER 0 ++#define CONFIG_LOAS_DEMUXER 0 ++#define CONFIG_LUODAT_DEMUXER 0 ++#define CONFIG_LRC_DEMUXER 0 ++#define CONFIG_LVF_DEMUXER 0 ++#define CONFIG_LXF_DEMUXER 0 ++#define CONFIG_M4V_DEMUXER 0 ++#define CONFIG_MCA_DEMUXER 0 ++#define CONFIG_MCC_DEMUXER 0 ++#define CONFIG_MATROSKA_DEMUXER 0 ++#define CONFIG_MGSTS_DEMUXER 0 ++#define CONFIG_MICRODVD_DEMUXER 0 ++#define CONFIG_MJPEG_DEMUXER 0 ++#define CONFIG_MJPEG_2000_DEMUXER 0 ++#define CONFIG_MLP_DEMUXER 0 ++#define CONFIG_MLV_DEMUXER 0 ++#define CONFIG_MM_DEMUXER 0 ++#define CONFIG_MMF_DEMUXER 0 ++#define CONFIG_MODS_DEMUXER 0 ++#define CONFIG_MOFLEX_DEMUXER 0 ++#define CONFIG_MOV_DEMUXER 0 ++#define CONFIG_MP3_DEMUXER 1 ++#define CONFIG_MPC_DEMUXER 0 ++#define CONFIG_MPC8_DEMUXER 0 ++#define CONFIG_MPEGPS_DEMUXER 0 ++#define CONFIG_MPEGTS_DEMUXER 0 ++#define CONFIG_MPEGTSRAW_DEMUXER 0 ++#define CONFIG_MPEGVIDEO_DEMUXER 0 ++#define CONFIG_MPJPEG_DEMUXER 0 ++#define CONFIG_MPL2_DEMUXER 0 ++#define CONFIG_MPSUB_DEMUXER 0 ++#define CONFIG_MSF_DEMUXER 0 ++#define CONFIG_MSNWC_TCP_DEMUXER 0 ++#define CONFIG_MSP_DEMUXER 0 ++#define CONFIG_MTAF_DEMUXER 0 ++#define CONFIG_MTV_DEMUXER 0 ++#define CONFIG_MUSX_DEMUXER 0 ++#define CONFIG_MV_DEMUXER 0 ++#define CONFIG_MVI_DEMUXER 0 ++#define CONFIG_MXF_DEMUXER 0 ++#define CONFIG_MXG_DEMUXER 0 ++#define CONFIG_NC_DEMUXER 0 ++#define CONFIG_NISTSPHERE_DEMUXER 0 ++#define CONFIG_NSP_DEMUXER 0 ++#define CONFIG_NSV_DEMUXER 0 ++#define CONFIG_NUT_DEMUXER 0 ++#define CONFIG_NUV_DEMUXER 0 ++#define CONFIG_OBU_DEMUXER 0 ++#define CONFIG_OGG_DEMUXER 0 ++#define CONFIG_OMA_DEMUXER 0 ++#define CONFIG_PAF_DEMUXER 0 ++#define CONFIG_PCM_ALAW_DEMUXER 0 ++#define CONFIG_PCM_MULAW_DEMUXER 0 ++#define CONFIG_PCM_VIDC_DEMUXER 0 ++#define CONFIG_PCM_F64BE_DEMUXER 0 ++#define CONFIG_PCM_F64LE_DEMUXER 0 ++#define CONFIG_PCM_F32BE_DEMUXER 0 ++#define CONFIG_PCM_F32LE_DEMUXER 0 ++#define CONFIG_PCM_S32BE_DEMUXER 0 ++#define CONFIG_PCM_S32LE_DEMUXER 0 ++#define CONFIG_PCM_S24BE_DEMUXER 0 ++#define CONFIG_PCM_S24LE_DEMUXER 0 ++#define CONFIG_PCM_S16BE_DEMUXER 0 ++#define CONFIG_PCM_S16LE_DEMUXER 0 ++#define CONFIG_PCM_S8_DEMUXER 0 ++#define CONFIG_PCM_U32BE_DEMUXER 0 ++#define CONFIG_PCM_U32LE_DEMUXER 0 ++#define CONFIG_PCM_U24BE_DEMUXER 0 ++#define CONFIG_PCM_U24LE_DEMUXER 0 ++#define CONFIG_PCM_U16BE_DEMUXER 0 ++#define CONFIG_PCM_U16LE_DEMUXER 0 ++#define CONFIG_PCM_U8_DEMUXER 0 ++#define CONFIG_PJS_DEMUXER 0 ++#define CONFIG_PMP_DEMUXER 0 ++#define CONFIG_PP_BNK_DEMUXER 0 ++#define CONFIG_PVA_DEMUXER 0 ++#define CONFIG_PVF_DEMUXER 0 ++#define CONFIG_QCP_DEMUXER 0 ++#define CONFIG_R3D_DEMUXER 0 ++#define CONFIG_RAWVIDEO_DEMUXER 0 ++#define CONFIG_REALTEXT_DEMUXER 0 ++#define CONFIG_REDSPARK_DEMUXER 0 ++#define CONFIG_RL2_DEMUXER 0 ++#define CONFIG_RM_DEMUXER 0 ++#define CONFIG_ROQ_DEMUXER 0 ++#define CONFIG_RPL_DEMUXER 0 ++#define CONFIG_RSD_DEMUXER 0 ++#define CONFIG_RSO_DEMUXER 0 ++#define CONFIG_RTP_DEMUXER 0 ++#define CONFIG_RTSP_DEMUXER 0 ++#define CONFIG_S337M_DEMUXER 0 ++#define CONFIG_SAMI_DEMUXER 0 ++#define CONFIG_SAP_DEMUXER 0 ++#define CONFIG_SBC_DEMUXER 0 ++#define CONFIG_SBG_DEMUXER 0 ++#define CONFIG_SCC_DEMUXER 0 ++#define CONFIG_SDP_DEMUXER 0 ++#define CONFIG_SDR2_DEMUXER 0 ++#define CONFIG_SDS_DEMUXER 0 ++#define CONFIG_SDX_DEMUXER 0 ++#define CONFIG_SEGAFILM_DEMUXER 0 ++#define CONFIG_SER_DEMUXER 0 ++#define CONFIG_SGA_DEMUXER 0 ++#define CONFIG_SHORTEN_DEMUXER 0 ++#define CONFIG_SIFF_DEMUXER 0 ++#define CONFIG_SIMBIOSIS_IMX_DEMUXER 0 ++#define CONFIG_SLN_DEMUXER 0 ++#define CONFIG_SMACKER_DEMUXER 0 ++#define CONFIG_SMJPEG_DEMUXER 0 ++#define CONFIG_SMUSH_DEMUXER 0 ++#define CONFIG_SOL_DEMUXER 0 ++#define CONFIG_SOX_DEMUXER 0 ++#define CONFIG_SPDIF_DEMUXER 0 ++#define CONFIG_SRT_DEMUXER 0 ++#define CONFIG_STR_DEMUXER 0 ++#define CONFIG_STL_DEMUXER 0 ++#define CONFIG_SUBVIEWER1_DEMUXER 0 ++#define CONFIG_SUBVIEWER_DEMUXER 0 ++#define CONFIG_SUP_DEMUXER 0 ++#define CONFIG_SVAG_DEMUXER 0 ++#define CONFIG_SVS_DEMUXER 0 ++#define CONFIG_SWF_DEMUXER 0 ++#define CONFIG_TAK_DEMUXER 0 ++#define CONFIG_TEDCAPTIONS_DEMUXER 0 ++#define CONFIG_THP_DEMUXER 0 ++#define CONFIG_THREEDOSTR_DEMUXER 0 ++#define CONFIG_TIERTEXSEQ_DEMUXER 0 ++#define CONFIG_TMV_DEMUXER 0 ++#define CONFIG_TRUEHD_DEMUXER 0 ++#define CONFIG_TTA_DEMUXER 0 ++#define CONFIG_TXD_DEMUXER 0 ++#define CONFIG_TTY_DEMUXER 0 ++#define CONFIG_TY_DEMUXER 0 ++#define CONFIG_V210_DEMUXER 0 ++#define CONFIG_V210X_DEMUXER 0 ++#define CONFIG_VAG_DEMUXER 0 ++#define CONFIG_VC1_DEMUXER 0 ++#define CONFIG_VC1T_DEMUXER 0 ++#define CONFIG_VIVIDAS_DEMUXER 0 ++#define CONFIG_VIVO_DEMUXER 0 ++#define CONFIG_VMD_DEMUXER 0 ++#define CONFIG_VOBSUB_DEMUXER 0 ++#define CONFIG_VOC_DEMUXER 0 ++#define CONFIG_VPK_DEMUXER 0 ++#define CONFIG_VPLAYER_DEMUXER 0 ++#define CONFIG_VQF_DEMUXER 0 ++#define CONFIG_W64_DEMUXER 0 ++#define CONFIG_WAV_DEMUXER 0 ++#define CONFIG_WC3_DEMUXER 0 ++#define CONFIG_WEBM_DASH_MANIFEST_DEMUXER 0 ++#define CONFIG_WEBVTT_DEMUXER 0 ++#define CONFIG_WSAUD_DEMUXER 0 ++#define CONFIG_WSD_DEMUXER 0 ++#define CONFIG_WSVQA_DEMUXER 0 ++#define CONFIG_WTV_DEMUXER 0 ++#define CONFIG_WVE_DEMUXER 0 ++#define CONFIG_WV_DEMUXER 0 ++#define CONFIG_XA_DEMUXER 0 ++#define CONFIG_XBIN_DEMUXER 0 ++#define CONFIG_XMV_DEMUXER 0 ++#define CONFIG_XVAG_DEMUXER 0 ++#define CONFIG_XWMA_DEMUXER 0 ++#define CONFIG_YOP_DEMUXER 0 ++#define CONFIG_YUV4MPEGPIPE_DEMUXER 0 ++#define CONFIG_IMAGE_BMP_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_CRI_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_DDS_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_DPX_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_EXR_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_GIF_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_J2K_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_JPEG_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_JPEGLS_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PAM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PBM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PCX_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PGMYUV_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PGM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PGX_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PHOTOCD_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PICTOR_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PNG_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PPM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_PSD_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_QDRAW_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_SGI_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_SVG_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_SUNRAST_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_TIFF_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_WEBP_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_XBM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_XPM_PIPE_DEMUXER 0 ++#define CONFIG_IMAGE_XWD_PIPE_DEMUXER 0 ++#define CONFIG_LIBGME_DEMUXER 0 ++#define CONFIG_LIBMODPLUG_DEMUXER 0 ++#define CONFIG_LIBOPENMPT_DEMUXER 0 ++#define CONFIG_VAPOURSYNTH_DEMUXER 0 ++#define CONFIG_A64_MUXER 0 ++#define CONFIG_AC3_MUXER 0 ++#define CONFIG_ADTS_MUXER 0 ++#define CONFIG_ADX_MUXER 0 ++#define CONFIG_AIFF_MUXER 0 ++#define CONFIG_ALP_MUXER 0 ++#define CONFIG_AMR_MUXER 0 ++#define CONFIG_AMV_MUXER 0 ++#define CONFIG_APM_MUXER 0 ++#define CONFIG_APNG_MUXER 0 ++#define CONFIG_APTX_MUXER 0 ++#define CONFIG_APTX_HD_MUXER 0 ++#define CONFIG_ARGO_ASF_MUXER 0 ++#define CONFIG_ASF_MUXER 0 ++#define CONFIG_ASS_MUXER 0 ++#define CONFIG_AST_MUXER 0 ++#define CONFIG_ASF_STREAM_MUXER 0 ++#define CONFIG_AU_MUXER 0 ++#define CONFIG_AVI_MUXER 0 ++#define CONFIG_AVM2_MUXER 0 ++#define CONFIG_AVS2_MUXER 0 ++#define CONFIG_BIT_MUXER 0 ++#define CONFIG_CAF_MUXER 0 ++#define CONFIG_CAVSVIDEO_MUXER 0 ++#define CONFIG_CODEC2_MUXER 0 ++#define CONFIG_CODEC2RAW_MUXER 0 ++#define CONFIG_CRC_MUXER 0 ++#define CONFIG_DASH_MUXER 0 ++#define CONFIG_DATA_MUXER 0 ++#define CONFIG_DAUD_MUXER 0 ++#define CONFIG_DIRAC_MUXER 0 ++#define CONFIG_DNXHD_MUXER 0 ++#define CONFIG_DTS_MUXER 0 ++#define CONFIG_DV_MUXER 0 ++#define CONFIG_EAC3_MUXER 0 ++#define CONFIG_F4V_MUXER 0 ++#define CONFIG_FFMETADATA_MUXER 0 ++#define CONFIG_FIFO_MUXER 0 ++#define CONFIG_FIFO_TEST_MUXER 0 ++#define CONFIG_FILMSTRIP_MUXER 0 ++#define CONFIG_FITS_MUXER 0 ++#define CONFIG_FLAC_MUXER 0 ++#define CONFIG_FLV_MUXER 0 ++#define CONFIG_FRAMECRC_MUXER 0 ++#define CONFIG_FRAMEHASH_MUXER 0 ++#define CONFIG_FRAMEMD5_MUXER 0 ++#define CONFIG_G722_MUXER 0 ++#define CONFIG_G723_1_MUXER 0 ++#define CONFIG_G726_MUXER 0 ++#define CONFIG_G726LE_MUXER 0 ++#define CONFIG_GIF_MUXER 0 ++#define CONFIG_GSM_MUXER 0 ++#define CONFIG_GXF_MUXER 0 ++#define CONFIG_H261_MUXER 0 ++#define CONFIG_H263_MUXER 0 ++#define CONFIG_H264_MUXER 0 ++#define CONFIG_HASH_MUXER 0 ++#define CONFIG_HDS_MUXER 0 ++#define CONFIG_HEVC_MUXER 0 ++#define CONFIG_HLS_MUXER 0 ++#define CONFIG_ICO_MUXER 0 ++#define CONFIG_ILBC_MUXER 0 ++#define CONFIG_IMAGE2_MUXER 0 ++#define CONFIG_IMAGE2PIPE_MUXER 0 ++#define CONFIG_IPOD_MUXER 0 ++#define CONFIG_IRCAM_MUXER 0 ++#define CONFIG_ISMV_MUXER 0 ++#define CONFIG_IVF_MUXER 0 ++#define CONFIG_JACOSUB_MUXER 0 ++#define CONFIG_KVAG_MUXER 0 ++#define CONFIG_LATM_MUXER 0 ++#define CONFIG_LRC_MUXER 0 ++#define CONFIG_M4V_MUXER 0 ++#define CONFIG_MD5_MUXER 0 ++#define CONFIG_MATROSKA_MUXER 0 ++#define CONFIG_MATROSKA_AUDIO_MUXER 0 ++#define CONFIG_MICRODVD_MUXER 0 ++#define CONFIG_MJPEG_MUXER 0 ++#define CONFIG_MLP_MUXER 0 ++#define CONFIG_MMF_MUXER 0 ++#define CONFIG_MOV_MUXER 0 ++#define CONFIG_MP2_MUXER 0 ++#define CONFIG_MP3_MUXER 0 ++#define CONFIG_MP4_MUXER 0 ++#define CONFIG_MPEG1SYSTEM_MUXER 0 ++#define CONFIG_MPEG1VCD_MUXER 0 ++#define CONFIG_MPEG1VIDEO_MUXER 0 ++#define CONFIG_MPEG2DVD_MUXER 0 ++#define CONFIG_MPEG2SVCD_MUXER 0 ++#define CONFIG_MPEG2VIDEO_MUXER 0 ++#define CONFIG_MPEG2VOB_MUXER 0 ++#define CONFIG_MPEGTS_MUXER 0 ++#define CONFIG_MPJPEG_MUXER 0 ++#define CONFIG_MXF_MUXER 0 ++#define CONFIG_MXF_D10_MUXER 0 ++#define CONFIG_MXF_OPATOM_MUXER 0 ++#define CONFIG_NULL_MUXER 0 ++#define CONFIG_NUT_MUXER 0 ++#define CONFIG_OGA_MUXER 0 ++#define CONFIG_OGG_MUXER 0 ++#define CONFIG_OGV_MUXER 0 ++#define CONFIG_OMA_MUXER 0 ++#define CONFIG_OPUS_MUXER 0 ++#define CONFIG_PCM_ALAW_MUXER 0 ++#define CONFIG_PCM_MULAW_MUXER 0 ++#define CONFIG_PCM_VIDC_MUXER 0 ++#define CONFIG_PCM_F64BE_MUXER 0 ++#define CONFIG_PCM_F64LE_MUXER 0 ++#define CONFIG_PCM_F32BE_MUXER 0 ++#define CONFIG_PCM_F32LE_MUXER 0 ++#define CONFIG_PCM_S32BE_MUXER 0 ++#define CONFIG_PCM_S32LE_MUXER 0 ++#define CONFIG_PCM_S24BE_MUXER 0 ++#define CONFIG_PCM_S24LE_MUXER 0 ++#define CONFIG_PCM_S16BE_MUXER 0 ++#define CONFIG_PCM_S16LE_MUXER 0 ++#define CONFIG_PCM_S8_MUXER 0 ++#define CONFIG_PCM_U32BE_MUXER 0 ++#define CONFIG_PCM_U32LE_MUXER 0 ++#define CONFIG_PCM_U24BE_MUXER 0 ++#define CONFIG_PCM_U24LE_MUXER 0 ++#define CONFIG_PCM_U16BE_MUXER 0 ++#define CONFIG_PCM_U16LE_MUXER 0 ++#define CONFIG_PCM_U8_MUXER 0 ++#define CONFIG_PSP_MUXER 0 ++#define CONFIG_RAWVIDEO_MUXER 0 ++#define CONFIG_RM_MUXER 0 ++#define CONFIG_ROQ_MUXER 0 ++#define CONFIG_RSO_MUXER 0 ++#define CONFIG_RTP_MUXER 0 ++#define CONFIG_RTP_MPEGTS_MUXER 0 ++#define CONFIG_RTSP_MUXER 0 ++#define CONFIG_SAP_MUXER 0 ++#define CONFIG_SBC_MUXER 0 ++#define CONFIG_SCC_MUXER 0 ++#define CONFIG_SEGAFILM_MUXER 0 ++#define CONFIG_SEGMENT_MUXER 0 ++#define CONFIG_STREAM_SEGMENT_MUXER 0 ++#define CONFIG_SINGLEJPEG_MUXER 0 ++#define CONFIG_SMJPEG_MUXER 0 ++#define CONFIG_SMOOTHSTREAMING_MUXER 0 ++#define CONFIG_SOX_MUXER 0 ++#define CONFIG_SPX_MUXER 0 ++#define CONFIG_SPDIF_MUXER 0 ++#define CONFIG_SRT_MUXER 0 ++#define CONFIG_STREAMHASH_MUXER 0 ++#define CONFIG_SUP_MUXER 0 ++#define CONFIG_SWF_MUXER 0 ++#define CONFIG_TEE_MUXER 0 ++#define CONFIG_TG2_MUXER 0 ++#define CONFIG_TGP_MUXER 0 ++#define CONFIG_MKVTIMESTAMP_V2_MUXER 0 ++#define CONFIG_TRUEHD_MUXER 0 ++#define CONFIG_TTA_MUXER 0 ++#define CONFIG_TTML_MUXER 0 ++#define CONFIG_UNCODEDFRAMECRC_MUXER 0 ++#define CONFIG_VC1_MUXER 0 ++#define CONFIG_VC1T_MUXER 0 ++#define CONFIG_VOC_MUXER 0 ++#define CONFIG_W64_MUXER 0 ++#define CONFIG_WAV_MUXER 0 ++#define CONFIG_WEBM_MUXER 0 ++#define CONFIG_WEBM_DASH_MANIFEST_MUXER 0 ++#define CONFIG_WEBM_CHUNK_MUXER 0 ++#define CONFIG_WEBP_MUXER 0 ++#define CONFIG_WEBVTT_MUXER 0 ++#define CONFIG_WTV_MUXER 0 ++#define CONFIG_WV_MUXER 0 ++#define CONFIG_YUV4MPEGPIPE_MUXER 0 ++#define CONFIG_CHROMAPRINT_MUXER 0 ++#define CONFIG_ASYNC_PROTOCOL 0 ++#define CONFIG_BLURAY_PROTOCOL 0 ++#define CONFIG_CACHE_PROTOCOL 0 ++#define CONFIG_CONCAT_PROTOCOL 0 ++#define CONFIG_CRYPTO_PROTOCOL 0 ++#define CONFIG_DATA_PROTOCOL 0 ++#define CONFIG_FFRTMPCRYPT_PROTOCOL 0 ++#define CONFIG_FFRTMPHTTP_PROTOCOL 0 ++#define CONFIG_FILE_PROTOCOL 1 ++#define CONFIG_FTP_PROTOCOL 0 ++#define CONFIG_GOPHER_PROTOCOL 0 ++#define CONFIG_GOPHERS_PROTOCOL 0 ++#define CONFIG_HLS_PROTOCOL 0 ++#define CONFIG_HTTP_PROTOCOL 0 ++#define CONFIG_HTTPPROXY_PROTOCOL 0 ++#define CONFIG_HTTPS_PROTOCOL 0 ++#define CONFIG_ICECAST_PROTOCOL 0 ++#define CONFIG_MMSH_PROTOCOL 0 ++#define CONFIG_MMST_PROTOCOL 0 ++#define CONFIG_MD5_PROTOCOL 0 ++#define CONFIG_PIPE_PROTOCOL 0 ++#define CONFIG_PROMPEG_PROTOCOL 0 ++#define CONFIG_RTMP_PROTOCOL 0 ++#define CONFIG_RTMPE_PROTOCOL 0 ++#define CONFIG_RTMPS_PROTOCOL 0 ++#define CONFIG_RTMPT_PROTOCOL 0 ++#define CONFIG_RTMPTE_PROTOCOL 0 ++#define CONFIG_RTMPTS_PROTOCOL 0 ++#define CONFIG_RTP_PROTOCOL 0 ++#define CONFIG_SCTP_PROTOCOL 0 ++#define CONFIG_SRTP_PROTOCOL 0 ++#define CONFIG_SUBFILE_PROTOCOL 0 ++#define CONFIG_TEE_PROTOCOL 0 ++#define CONFIG_TCP_PROTOCOL 0 ++#define CONFIG_TLS_PROTOCOL 0 ++#define CONFIG_UDP_PROTOCOL 0 ++#define CONFIG_UDPLITE_PROTOCOL 0 ++#define CONFIG_UNIX_PROTOCOL 0 ++#define CONFIG_LIBAMQP_PROTOCOL 0 ++#define CONFIG_LIBRIST_PROTOCOL 0 ++#define CONFIG_LIBRTMP_PROTOCOL 0 ++#define CONFIG_LIBRTMPE_PROTOCOL 0 ++#define CONFIG_LIBRTMPS_PROTOCOL 0 ++#define CONFIG_LIBRTMPT_PROTOCOL 0 ++#define CONFIG_LIBRTMPTE_PROTOCOL 0 ++#define CONFIG_LIBSRT_PROTOCOL 0 ++#define CONFIG_LIBSSH_PROTOCOL 0 ++#define CONFIG_LIBSMBCLIENT_PROTOCOL 0 ++#define CONFIG_LIBZMQ_PROTOCOL 0 ++#endif /* FFMPEG_CONFIG_H */ diff --git a/glue/rexglue-sdk-main/resources/templates/codegen/init_h.inja b/glue/rexglue-sdk-main/resources/templates/codegen/init_h.inja index 05ba3178..7eb3a10b 100644 --- a/glue/rexglue-sdk-main/resources/templates/codegen/init_h.inja +++ b/glue/rexglue-sdk-main/resources/templates/codegen/init_h.inja @@ -118,8 +118,16 @@ extern PPCFuncMapping PPCFuncMappings[]; #if defined(REX_FUSED_VMADDFP) #include <simde/x86/fma.h> #define REX_VMADDFP(a, b, c) simde_mm_fmadd_ps((a), (b), (c)) +#if defined(__FMA__) +// [ps5 port] -(a*b - c) in one rounding: x86's fused multiply-subtract with +// the sign flipped (c - a*b gives +0 where the console gives -0, a*b == c). +#define REX_VNMSUBFP(a, b, c) \ + simde_mm_xor_ps(simde_mm_fmsub_ps((a), (b), (c)), \ + simde_mm_castsi128_ps(simde_mm_set1_epi32(int(0x80000000)))) +#else // c - a*b in one rounding (fmls); simde's fmsub is not fused. #define REX_VNMSUBFP(a, b, c) simde_mm_fnmadd_ps((a), (b), (c)) +#endif #else #define REX_VMADDFP(a, b, c) simde_mm_add_ps(simde_mm_mul_ps((a), (b)), (c)) #define REX_VNMSUBFP(a, b, c) \ @@ -150,7 +158,7 @@ extern PPCFuncMapping PPCFuncMappings[]; // Memory Access //============================================================================= -#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC +#if REX_PLATFORM_WIN32 || REX_PLATFORM_MAC || REX_PLATFORM_PS5 #define REX_PHYS_HOST_OFFSET(addr) (((u32)(addr) >= 0xE0000000u) ? 0x1000u : 0u) #else #define REX_PHYS_HOST_OFFSET(addr) 0u @@ -336,28 +344,38 @@ inline void ppc_trap(PPCContext& ctx, u8* base, u16 trap_type) { // Interrupt Locks //============================================================================= -// Interrupt-disabled depth of the calling thread. The count under the global -// lock is non-zero exactly when the caller holds the lock, so mfmsr reads this -// instead of taking the lock: guest sections are a few instructions -// (mfmsr/mtmsrd around lwarx/stwcx.), and the extra lock round trip doubled -// their contention. -inline thread_local i32 ppc_global_lock_depth_ = 0; - -#define REX_CHECK_GLOBAL_LOCK() (ppc_global_lock_depth_ ? u64{0} : u64{0x8000}) +// Interrupt-disabled depth of the calling thread (ctx.global_lock_depth, in +// the guest thread's context: a thread_local cost a system lookup on the PS5). +// The count under the global lock is non-zero exactly when the caller holds the +// lock, so mfmsr reads this instead of taking the lock: guest sections are a +// few instructions (mfmsr/mtmsrd around lwarx/stwcx.), and the extra lock round +// trip doubled their contention. +#define REX_CHECK_GLOBAL_LOCK() (ctx.global_lock_depth ? u64{0} : u64{0x8000}) #define REX_ENTER_GLOBAL_LOCK() \ do { \ rex::thread::global_critical_region::LockSpinning(); \ - ++ppc_global_lock_depth_; \ + ++ctx.global_lock_depth; \ } while (0) -#define REX_LEAVE_GLOBAL_LOCK() \ - do { \ - assert(ppc_global_lock_depth_ >= 1 && "LeaveGlobalLock called without EnterGlobalLock"); \ - --ppc_global_lock_depth_; \ - rex::thread::global_critical_region::mutex().unlock(); \ +#define REX_LEAVE_GLOBAL_LOCK() \ + do { \ + assert(ctx.global_lock_depth >= 1 && "LeaveGlobalLock called without EnterGlobalLock"); \ + --ctx.global_lock_depth; \ + rex::thread::global_critical_region::mutex().unlock(); \ } while (0) +#if defined(__PROSPERO__) && defined(REX_PS5_GUEST_CODE) +// [ps5 port] mfmsr and mtmsrd are the only instructions recompiled with a +// std::atomic_thread_fence, and each sits next to this thread's own lock depth +// or the global lock's locked compare-exchange (or the guest's stwcx. one), +// full barriers on x86 already: the mfence (three per guest interlocked +// operation) ordered nothing more. A compiler barrier keeps the code's order. +// Only in the recompiled game's own files (CMakeLists.txt), after every +// header they include. +#define atomic_thread_fence atomic_signal_fence +#endif + //============================================================================= // Function Declarations //============================================================================= diff --git a/glue/rexglue-sdk-main/resources/templates/test/ppc_config_h.inja b/glue/rexglue-sdk-main/resources/templates/test/ppc_config_h.inja index f0b9caa7..117293ae 100644 --- a/glue/rexglue-sdk-main/resources/templates/test/ppc_config_h.inja +++ b/glue/rexglue-sdk-main/resources/templates/test/ppc_config_h.inja @@ -72,7 +72,7 @@ #define REX_PPC_ISYNC() ((void)0) #endif -#if REX_PLATFORM_WIN32 +#if REX_PLATFORM_WIN32 || REX_PLATFORM_PS5 #define REX_PHYS_HOST_OFFSET(addr) (((u32)(addr) >= 0xE0000000u) ? 0x1000u : 0u) #else #define REX_PHYS_HOST_OFFSET(addr) 0u diff --git a/glue/rexglue-sdk-main/src/audio/CMakeLists.txt b/glue/rexglue-sdk-main/src/audio/CMakeLists.txt index 9ece70bb..24eb993b 100644 --- a/glue/rexglue-sdk-main/src/audio/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/audio/CMakeLists.txt @@ -13,6 +13,7 @@ add_library(rexaudio OBJECT # SDL backend sdl/sdl_audio_system.cpp sdl/sdl_audio_driver.cpp + $<$<BOOL:${PS5}>:ps5/ps5_audio_driver.cpp> ) add_library(rex::audio ALIAS rexaudio) @@ -40,3 +41,10 @@ if(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS") "-framework CoreAudio" ) endif() + +if(PS5) + # [ps5 port] With the PS5 target, clang's implicit __va_list_tag from the + # PCH and the one FFmpeg's <stdarg.h> use in this file don't unify + # (va_list "__va_list_tag *" != "__va_list_tag *"): no PCH here. + set_source_files_properties(xma_decoder.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON) +endif() diff --git a/glue/rexglue-sdk-main/src/audio/ps5/ps5_audio_driver.cpp b/glue/rexglue-sdk-main/src/audio/ps5/ps5_audio_driver.cpp new file mode 100644 index 00000000..48e82174 --- /dev/null +++ b/glue/rexglue-sdk-main/src/audio/ps5/ps5_audio_driver.cpp @@ -0,0 +1,158 @@ +/** + * [ps5 port] sceAudioOut audio driver (see ps5_audio_driver.h). + */ +#include <rex/audio/ps5/ps5_audio_driver.h> + +#include <algorithm> +#include <cstring> + +#include <rex/assert.h> +#include <rex/audio/conversion.h> +#include <rex/audio/flags.h> +#include <rex/logging.h> + +extern "C" { +int32_t sceAudioOutInit(void); +int32_t sceAudioOutOpen(int32_t user_id, int32_t type, int32_t index, uint32_t len, uint32_t freq, + uint32_t param); +int32_t sceAudioOutOutput(int32_t handle, const void* ptr); +int32_t sceAudioOutClose(int32_t handle); +} + +REXCVAR_DECLARE(bool, audio_mute); +// [ps5 port] The sound queued ahead of the speakers: 12 frames is 64 ms. The +// generic 64 (341 ms) put every sound a third of a second behind the picture +// and the controller. +REXCVAR_DEFINE_UINT32(ps5_audio_frames, 12, "PS5", + "Sound frames queued ahead of the speakers (5.3 ms each): fewer is less delay") + .range(2, 64); + +namespace rex::audio::ps5 { + +namespace { +constexpr int32_t kSystemUser = 0xFF; +constexpr int32_t kPortTypeMain = 0; +constexpr uint32_t kFormatS16Stereo = 1; +} // namespace + +Ps5AudioDriver::Ps5AudioDriver(memory::Memory* memory, rex::thread::Semaphore* semaphore) + : AudioDriver(memory), semaphore_(semaphore) {} + +Ps5AudioDriver::~Ps5AudioDriver() { Shutdown(); } + +bool Ps5AudioDriver::Initialize() { + int32_t rc = sceAudioOutInit(); + // (Already initialized by someone else in the process is fine.) + (void)rc; + port_ = sceAudioOutOpen(kSystemUser, kPortTypeMain, 0, kChannelSamples, kFrequency, + kFormatS16Stereo); + if (port_ < 0) { + REXAPU_ERROR("PS5 audio: sceAudioOutOpen failed ({:#x})", uint32_t(port_)); + return false; + } + REXAPU_INFO("PS5 audio: port {} (48000 Hz, 256-sample frames, s16 stereo)", port_); + running_ = true; + thread_ = std::thread([this] { OutputThread(); }); + return true; +} + +uint32_t Ps5AudioDriver::RecommendedInitialCredits(uint32_t configured_maximum) const { + const uint32_t frames = std::clamp<uint32_t>(REXCVAR_GET(ps5_audio_frames), 2, std::max(2u, configured_maximum)); + REXAPU_INFO("PS5 audio: {} frames ahead of the speakers ({:.0f} ms)", frames, + double(frames) * kChannelSamples * 1000.0 / kFrequency); + return frames; +} + +void Ps5AudioDriver::SubmitFrame(uint32_t frame_ptr) { + const auto input_frame = memory_->TranslateVirtual<float*>(frame_ptr); + float* frame; + { + std::lock_guard<std::mutex> guard(frames_mutex_); + if (frames_unused_.empty()) { + frame = new float[kFrameSamples]; + } else { + frame = frames_unused_.top(); + frames_unused_.pop(); + } + } + std::memcpy(frame, input_frame, kFrameSamples * sizeof(float)); + std::lock_guard<std::mutex> guard(frames_mutex_); + frames_queued_.push(frame); +} + +// sceAudioOutOutput blocks until the previous buffer has started playing, so +// this loop runs at exactly the output rate (one frame per 5.33 ms): a queued +// game frame when there is one (then the game may submit another: release +// the semaphore), else silence. +void Ps5AudioDriver::OutputThread() { + float stereo[kChannelSamples * 2]; + int16_t pcm[kChannelSamples * 2]; + // Gaps: the game's sound not there in time (silence played instead), while + // it isn't paused. Logged every 10 s there were any (a shorter queue, + // ps5_audio_frames, leaves less room for the game's threads to be late). + bool started = false, in_gap = false; + uint32_t gaps = 0, silent = 0, played = 0; + while (running_) { + float* frame = nullptr; + { + std::lock_guard<std::mutex> guard(frames_mutex_); + if (!frames_queued_.empty()) { + frame = frames_queued_.front(); + frames_queued_.pop(); + } + } + if (frame) { + started = true, in_gap = false; + } else if (started && !paused_.load(std::memory_order_relaxed)) { + gaps += in_gap ? 0 : 1; + in_gap = true; + ++silent; + } + if (++played == kFrequency * 10 / kChannelSamples) { + if (gaps) { + REXAPU_INFO("PS5 audio: {} gaps in 10 s ({:.0f} ms of silence); {} frames ahead", gaps, + double(silent) * kChannelSamples * 1000.0 / kFrequency, REXCVAR_GET(ps5_audio_frames)); + } + gaps = silent = played = 0; + } + if (frame && !REXCVAR_GET(audio_mute)) { + conversion::sequential_6_BE_to_interleaved_2_LE(stereo, frame, kChannelSamples); + for (uint32_t i = 0; i < kChannelSamples * 2; ++i) { + float s = std::clamp(stereo[i], -1.0f, 1.0f); + pcm[i] = int16_t(s * 32767.0f); + } + } else { + std::memset(pcm, 0, sizeof(pcm)); + } + if (frame) { + { + std::lock_guard<std::mutex> guard(frames_mutex_); + frames_unused_.push(frame); + } + semaphore_->Release(1, nullptr); + } + sceAudioOutOutput(port_, pcm); + } +} + +void Ps5AudioDriver::Shutdown() { + if (running_.exchange(false) && thread_.joinable()) { + thread_.join(); + } + if (port_ >= 0) { + sceAudioOutOutput(port_, nullptr); // (waits for the last buffer) + sceAudioOutClose(port_); + port_ = -1; + } + std::lock_guard<std::mutex> guard(frames_mutex_); + while (!frames_unused_.empty()) { + delete[] frames_unused_.top(); + frames_unused_.pop(); + } + while (!frames_queued_.empty()) { + delete[] frames_queued_.front(); + frames_queued_.pop(); + } +} + +} // namespace rex::audio::ps5 diff --git a/glue/rexglue-sdk-main/src/audio/sdl/sdl_audio_system.cpp b/glue/rexglue-sdk-main/src/audio/sdl/sdl_audio_system.cpp index 68da0cf9..fe72177d 100644 --- a/glue/rexglue-sdk-main/src/audio/sdl/sdl_audio_system.cpp +++ b/glue/rexglue-sdk-main/src/audio/sdl/sdl_audio_system.cpp @@ -11,6 +11,9 @@ #include <rex/audio/flags.h> #include <rex/audio/sdl/sdl_audio_driver.h> +#if REX_PLATFORM_PS5 +#include <rex/audio/ps5/ps5_audio_driver.h> +#endif #include <rex/audio/sdl/sdl_audio_system.h> namespace rex::audio::sdl { @@ -32,6 +35,16 @@ void SDLAudioSystem::Initialize() { X_STATUS SDLAudioSystem::CreateDriver([[maybe_unused]] size_t index, rex::thread::Semaphore* semaphore, AudioDriver** out_driver) { assert_not_null(out_driver); +#if REX_PLATFORM_PS5 + // [ps5 port] SDL has no PS5 audio backend: sceAudioOut directly. + auto ps5_driver = new ps5::Ps5AudioDriver(memory_, semaphore); + if (!ps5_driver->Initialize()) { + delete ps5_driver; + return X_STATUS_UNSUCCESSFUL; + } + *out_driver = ps5_driver; + return X_STATUS_SUCCESS; +#endif auto driver = new SDLAudioDriver(memory_, semaphore); if (!driver->Initialize()) { driver->Shutdown(); @@ -45,6 +58,13 @@ X_STATUS SDLAudioSystem::CreateDriver([[maybe_unused]] size_t index, void SDLAudioSystem::DestroyDriver(AudioDriver* driver) { assert_not_null(driver); +#if REX_PLATFORM_PS5 + if (auto* ps5_driver = dynamic_cast<ps5::Ps5AudioDriver*>(driver)) { + ps5_driver->Shutdown(); + delete ps5_driver; + return; + } +#endif auto sdldriver = dynamic_cast<SDLAudioDriver*>(driver); assert_not_null(sdldriver); sdldriver->Shutdown(); diff --git a/glue/rexglue-sdk-main/src/core/CMakeLists.txt b/glue/rexglue-sdk-main/src/core/CMakeLists.txt index 86d57cff..d269751f 100644 --- a/glue/rexglue-sdk-main/src/core/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/core/CMakeLists.txt @@ -132,7 +132,11 @@ if(REXGLUE_BUILD_TESTS) target_compile_definitions(rexcore PUBLIC REX_DIAGNOSTICS_TESTING=1) endif() -if(APPLE) +if(PS5) + # [ps5 port] the SDK's libc has pthreads; there is no librt / libdl. + target_link_libraries(rexcore PUBLIC SceSystemService SceUserService ScePad SceAudioOut ps5platform) + target_sources(rexcore PRIVATE ps5_libc_extras.c ps5_diag.cpp) +elseif(APPLE) target_link_libraries(rexcore PRIVATE pthread) elseif(ANDROID) target_link_libraries(rexcore PRIVATE dl log) diff --git a/glue/rexglue-sdk-main/src/core/clock.cpp b/glue/rexglue-sdk-main/src/core/clock.cpp index ae72b7ae..1b0c0ae2 100644 --- a/glue/rexglue-sdk-main/src/core/clock.cpp +++ b/glue/rexglue-sdk-main/src/core/clock.cpp @@ -10,8 +10,11 @@ */ #include <algorithm> +#include <atomic> #include <limits> +#include <memory> #include <mutex> +#include <vector> #include <rex/chrono/clock.h> #include <rex/cvar.h> @@ -42,6 +45,23 @@ uint64_t last_host_tick_count_ = Clock::QueryHostTickCount(); // Mutex to ensure last_host_tick_count_ and last_guest_tick_count_ are in sync std::mutex tick_mutex_; +#if REX_PLATFORM_PS5 +// [ps5 port] The guest clock as a function of the host's: guest ticks at a +// host tick are guest_base + (host - host_base) * numerator / denominator. +// Every mftb read took tick_mutex_ (a system call when another thread held +// it) and dropped the remainder of each step, so the clock lost time the more +// often the game read it. Reading it now takes no lock and loses nothing. +struct GuestClockMapping { + uint64_t host_base, guest_base, numerator, denominator; +}; +std::atomic<const GuestClockMapping*> guest_clock_mapping_{nullptr}; +inline uint64_t GuestTicksAt(const GuestClockMapping& mapping, uint64_t host) { + const uint64_t delta = host > mapping.host_base ? host - mapping.host_base : 0; + return mapping.guest_base + + uint64_t((unsigned __int128)delta * mapping.numerator / mapping.denominator); +} +#endif + void RecomputeGuestTickScalar() { // Create a rational number with numerator (first) and denominator (second) auto frac = std::make_pair(guest_tick_frequency_, Clock::QueryHostTickFrequency()); @@ -58,6 +78,26 @@ void RecomputeGuestTickScalar() { reduce_fraction(frac); std::lock_guard<std::mutex> lock(tick_mutex_); +#if REX_PLATFORM_PS5 + // Continue the guest clock from where it is now, at the new rate. + const uint64_t host_now = Clock::QueryHostTickCount(); + uint64_t guest_now; + if (const GuestClockMapping* current = guest_clock_mapping_.load(std::memory_order_acquire)) { + guest_now = GuestTicksAt(*current, host_now); + } else { + const uint64_t delta = host_now > last_host_tick_count_ ? host_now - last_host_tick_count_ : 0; + guest_now = last_guest_tick_count_ + + uint64_t((unsigned __int128)delta * guest_tick_ratio_.first / guest_tick_ratio_.second); + } + last_host_tick_count_ = host_now; + last_guest_tick_count_ = guest_now; + // Readers may still hold an older mapping, so mappings are never freed + // (the rate changes a handful of times a run). + static std::vector<std::unique_ptr<GuestClockMapping>> mappings; + mappings.push_back(std::make_unique<GuestClockMapping>( + GuestClockMapping{host_now, guest_now, frac.first, frac.second})); + guest_clock_mapping_.store(mappings.back().get(), std::memory_order_release); +#endif guest_tick_ratio_ = frac; } @@ -71,6 +111,11 @@ uint64_t UpdateGuestClock() { return host_tick_count * guest_tick_ratio_.first / guest_tick_ratio_.second; } +#if REX_PLATFORM_PS5 + if (const GuestClockMapping* mapping = guest_clock_mapping_.load(std::memory_order_acquire)) { + return GuestTicksAt(*mapping, host_tick_count); + } +#endif std::unique_lock<std::mutex> lock(tick_mutex_, std::defer_lock); if (lock.try_lock()) { // Translate host tick count to guest tick count. diff --git a/glue/rexglue-sdk-main/src/core/clock_posix.cpp b/glue/rexglue-sdk-main/src/core/clock_posix.cpp index 00f23828..f870b212 100644 --- a/glue/rexglue-sdk-main/src/core/clock_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/clock_posix.cpp @@ -18,21 +18,36 @@ static_assert(REX_PLATFORM_LINUX || REX_PLATFORM_MAC, "This file is POSIX-only") #include <mach/mach_time.h> #endif +#if REX_PLATFORM_PS5 +// [ps5 port] clock_gettime is a system call on the PS5; the guest reads the +// clock (mftb, KeQueryPerformanceCounter) tens of thousands of times a second. +// The TSC is invariant and in step across the cores, as titles rely on. +extern "C" uint64_t sceKernelReadTsc(void); +extern "C" uint64_t sceKernelGetTscFrequency(void); +namespace { +uint64_t Ps5TscFrequency() { + static const uint64_t frequency = sceKernelGetTscFrequency(); + return frequency; +} +} // namespace +#endif + namespace rex::chrono { uint64_t Clock::host_tick_frequency_platform() { +#if REX_PLATFORM_PS5 + if (const uint64_t frequency = Ps5TscFrequency()) return frequency; +#endif #ifdef __APPLE__ mach_timebase_info_data_t info; mach_timebase_info(&info); return (uint64_t)((1000000000ull * (uint64_t)info.denom) / (uint64_t)info.numer); #else - timespec res; - int error = clock_getres(CLOCK_MONOTONIC_RAW, &res); - assert_zero(error); - assert_zero(res.tv_sec); // Sub second resolution is required. - - // Convert nano seconds to hertz. Resolution is 1ns on most systems. - return 1000000000ull / res.tv_nsec; + // host_tick_count_platform() counts nanoseconds, so this is 1 GHz whatever + // the clock's resolution. (It used to be 1e9 / clock_getres(): 1 ns on Linux, + // but FreeBSD reports 1e9 / timecounter frequency + 1, more than 1 ns on a + // PS5, and the guest clock ran that many times too fast.) + return 1000000000ull; #endif } @@ -44,6 +59,9 @@ uint64_t Clock::host_tick_count_platform() { // inconsistent with the 24 MHz mach frequency and cause ~41x time acceleration. return mach_absolute_time(); #else +#if REX_PLATFORM_PS5 + if (Ps5TscFrequency()) return sceKernelReadTsc(); +#endif timespec tp; int error = clock_gettime(CLOCK_MONOTONIC_RAW, &tp); assert_zero(error); diff --git a/glue/rexglue-sdk-main/src/core/exception_handler_posix.cpp b/glue/rexglue-sdk-main/src/core/exception_handler_posix.cpp index 64450348..7f900b8c 100644 --- a/glue/rexglue-sdk-main/src/core/exception_handler_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/exception_handler_posix.cpp @@ -9,12 +9,21 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#if defined(__APPLE__) && !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE 700 +#endif + +#include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif #include <rex/exception_handler.h> #if REX_PLATFORM_LINUX || REX_PLATFORM_MAC #include <signal.h> +#include <cstdlib> #include <cstdint> #include <cstring> @@ -25,11 +34,44 @@ #include <ucontext.h> +#if REX_PLATFORM_PS5 +// [ps5 port] The PS5 kernel hands signal handlers the FreeBSD amd64 ucontext +// with 48 extra bytes in front: mcontext starts at +64, not at +16 where the +// SDK header puts it (measured on a 13.60 console: rdi +72, rax +120, rip +// +224). View it the way the Linux code below reads gregs / fpregs, as +// 64-bit slots of FreeBSD's struct __mcontext. +namespace { +using greg_t = uint64_t; +enum : int { + REG_RDI = 1, REG_RSI = 2, REG_RDX = 3, REG_RCX = 4, REG_R8 = 5, REG_R9 = 6, + REG_RAX = 7, REG_RBX = 8, REG_RBP = 9, REG_R10 = 10, REG_R11 = 11, REG_R12 = 12, + REG_R13 = 13, REG_R14 = 14, REG_R15 = 15, REG_ERR = 19, REG_RIP = 20, REG_EFL = 22, + REG_RSP = 23, +}; +constexpr size_t kPs5McontextOffset = 64; +constexpr size_t kPs5FpstateSlot = 28; // mc_fpstate: FXSAVE area, 16-byte aligned +struct Ps5FpRegs { + uint8_t fxsave_header[160]; // FCW ... MXCSR, ST0-ST7 + uint8_t _xmm[16][16]; +}; +struct Ps5MContext { + greg_t* gregs; + Ps5FpRegs* fpregs; + explicit Ps5MContext(void* signal_context) { + auto* base = reinterpret_cast<uint8_t*>(signal_context) + kPs5McontextOffset; + gregs = reinterpret_cast<greg_t*>(base); + fpregs = reinterpret_cast<Ps5FpRegs*>(base + kPs5FpstateSlot * sizeof(greg_t)); + } +}; +} // namespace +#endif + namespace rex::arch { bool signal_handlers_installed_ = false; struct sigaction original_sigill_handler_; struct sigaction original_sigsegv_handler_; +struct sigaction original_sigbus_handler_; // This can be as large as needed, but isn't often needed. // As we will be sometimes firing many exceptions we want to avoid having to @@ -40,9 +82,47 @@ constexpr size_t kMaxHandlerCount = 8; // Executed in order. std::pair<ExceptionHandler::Handler, void*> handlers_[kMaxHandlerCount]; +static void DispatchUnhandledSignal(int signal_number, siginfo_t* signal_info, + void* signal_context) { + struct sigaction* original_handler = nullptr; + if (signal_number == SIGILL) { + original_handler = &original_sigill_handler_; + } else if (signal_number == SIGSEGV) { + original_handler = &original_sigsegv_handler_; + } else if (signal_number == SIGBUS) { + original_handler = &original_sigbus_handler_; + } + + if (original_handler) { + if ((original_handler->sa_flags & SA_SIGINFO) && original_handler->sa_sigaction) { + original_handler->sa_sigaction(signal_number, signal_info, signal_context); + return; + } + if (original_handler->sa_handler == SIG_IGN) { + return; + } + if (original_handler->sa_handler != SIG_DFL && original_handler->sa_handler) { + original_handler->sa_handler(signal_number); + return; + } + + sigaction(signal_number, original_handler, nullptr); + } else { + signal(signal_number, SIG_DFL); + } + raise(signal_number); + std::_Exit(128 + signal_number); +} + static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, void* signal_context) { +#if REX_PLATFORM_MAC + mcontext_t mcontext = reinterpret_cast<ucontext_t*>(signal_context)->uc_mcontext; +#elif REX_PLATFORM_PS5 + Ps5MContext mcontext(signal_context); +#else mcontext_t& mcontext = reinterpret_cast<ucontext_t*>(signal_context)->uc_mcontext; +#endif HostThreadContext thread_context; @@ -70,6 +150,19 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, std::memcpy(thread_context.xmm_registers, mcontext.fpregs->_xmm, sizeof(thread_context.xmm_registers)); #elif REX_ARCH_ARM64 +#if REX_PLATFORM_MAC + auto& thread_state = mcontext->__ss; + auto& neon_state = mcontext->__ns; + std::memcpy(thread_context.x, thread_state.__x, sizeof(thread_state.__x)); + thread_context.x[29] = thread_state.__fp; + thread_context.x[30] = thread_state.__lr; + thread_context.sp = thread_state.__sp; + thread_context.pc = thread_state.__pc; + thread_context.pstate = thread_state.__cpsr; + thread_context.fpsr = neon_state.__fpsr; + thread_context.fpcr = neon_state.__fpcr; + std::memcpy(thread_context.v, neon_state.__v, sizeof(thread_context.v)); +#else std::memcpy(thread_context.x, mcontext.regs, sizeof(thread_context.x)); thread_context.sp = mcontext.sp; thread_context.pc = mcontext.pc; @@ -98,6 +191,7 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, thread_context.fpcr = mcontext_fpsimd->fpcr; std::memcpy(thread_context.v, mcontext_fpsimd->vregs, sizeof(thread_context.v)); } +#endif // REX_PLATFORM_MAC #endif // REX_ARCH Exception ex; @@ -105,7 +199,8 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, case SIGILL: ex.InitializeIllegalInstruction(&thread_context); break; - case SIGSEGV: { + case SIGSEGV: + case SIGBUS: { Exception::AccessViolationOperation access_violation_operation; #if REX_ARCH_AMD64 // x86_pf_error_code::X86_PF_WRITE @@ -114,6 +209,24 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, ? Exception::AccessViolationOperation::kWrite : Exception::AccessViolationOperation::kRead; #elif REX_ARCH_ARM64 +#if REX_PLATFORM_MAC + const uint32_t esr = mcontext->__es.__esr; + if (((esr >> 26) & 0b111110) == 0b100100) { + access_violation_operation = (esr & (UINT64_C(1) << 6)) + ? Exception::AccessViolationOperation::kWrite + : Exception::AccessViolationOperation::kRead; + } else { + bool instruction_is_store; + if (IsArm64LoadPrefetchStore(*reinterpret_cast<const uint32_t*>(thread_context.pc), + instruction_is_store)) { + access_violation_operation = instruction_is_store + ? Exception::AccessViolationOperation::kWrite + : Exception::AccessViolationOperation::kRead; + } else { + access_violation_operation = Exception::AccessViolationOperation::kUnknown; + } + } +#else // For a Data Abort (EC - ESR_EL1 bits 31:26 - 0b100100 from a lower // Exception Level, 0b100101 without a change in the Exception Level), // bit 6 is 0 for reading from a memory location, 1 for writing to a @@ -145,6 +258,7 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, access_violation_operation = Exception::AccessViolationOperation::kUnknown; } } +#endif // REX_PLATFORM_MAC #else access_violation_operation = Exception::AccessViolationOperation::kUnknown; #endif // REX_ARCH @@ -181,6 +295,34 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, &thread_context.xmm_registers[modified_register_index], sizeof(vec128_t)); } #elif REX_ARCH_ARM64 +#if REX_PLATFORM_MAC + auto& thread_state = mcontext->__ss; + auto& neon_state = mcontext->__ns; + uint32_t modified_register_index; + uint32_t modified_x_registers_remaining = ex.modified_x_registers(); + while (rex::bit_scan_forward(modified_x_registers_remaining, &modified_register_index)) { + modified_x_registers_remaining &= ~(UINT32_C(1) << modified_register_index); + if (modified_register_index < 29) { + thread_state.__x[modified_register_index] = + thread_context.x[modified_register_index]; + } else if (modified_register_index == 29) { + thread_state.__fp = thread_context.x[29]; + } else if (modified_register_index == 30) { + thread_state.__lr = thread_context.x[30]; + } + } + thread_state.__sp = thread_context.sp; + thread_state.__pc = thread_context.pc; + thread_state.__cpsr = uint32_t(thread_context.pstate); + neon_state.__fpsr = thread_context.fpsr; + neon_state.__fpcr = thread_context.fpcr; + uint32_t modified_v_registers_remaining = ex.modified_v_registers(); + while (rex::bit_scan_forward(modified_v_registers_remaining, &modified_register_index)) { + modified_v_registers_remaining &= ~(UINT32_C(1) << modified_register_index); + std::memcpy(&neon_state.__v[modified_register_index], + &thread_context.v[modified_register_index], sizeof(vec128_t)); + } +#else uint32_t modified_register_index; uint32_t modified_x_registers_remaining = ex.modified_x_registers(); while (rex::bit_scan_forward(modified_x_registers_remaining, &modified_register_index)) { @@ -201,28 +343,21 @@ static void ExceptionHandlerCallback(int signal_number, siginfo_t* signal_info, mcontext.regs[modified_register_index] = thread_context.x[modified_register_index]; } } +#endif // REX_PLATFORM_MAC #endif // REX_ARCH return; } } - - // Not a guest-memory fault. Returning would re-execute the faulting - // instruction forever (a silent 100% CPU hang); hand it to whoever was - // installed before us (debuggerd on Android) so it crashes with a report. - struct sigaction& original = - signal_number == SIGILL ? original_sigill_handler_ : original_sigsegv_handler_; - if ((original.sa_flags & SA_SIGINFO) && original.sa_sigaction) { - original.sa_sigaction(signal_number, signal_info, signal_context); - return; +#if REX_PLATFORM_PS5 + // [ps5 port] Nothing took it: the thread would re-execute the faulting + // instruction forever (a game stuck on a loading screen). Say where. + if (signal_number == SIGSEGV || signal_number == SIGBUS) { + rex::ps5diag::UnhandledHostFault(thread_context.rip, reinterpret_cast<uintptr_t>(signal_info->si_addr), + thread_context.rsp); } - if (original.sa_handler == SIG_IGN) { - return; - } - if (original.sa_handler != SIG_DFL && original.sa_handler) { - original.sa_handler(signal_number); - return; - } - signal(signal_number, SIG_DFL); +#endif + + DispatchUnhandledSignal(signal_number, signal_info, signal_context); } void ExceptionHandler::Install(Handler fn, void* data) { @@ -239,6 +374,9 @@ void ExceptionHandler::Install(Handler fn, void* data) { if (sigaction(SIGSEGV, &signal_handler, &original_sigsegv_handler_) != 0) { assert_always("Failed to install new SIGSEGV handler"); } + if (sigaction(SIGBUS, &signal_handler, &original_sigbus_handler_) != 0) { + assert_always("Failed to install new SIGBUS handler"); + } signal_handlers_installed_ = true; } @@ -279,6 +417,9 @@ void ExceptionHandler::Uninstall(Handler fn, void* data) { if (sigaction(SIGSEGV, &original_sigsegv_handler_, NULL) != 0) { assert_always("Failed to restore original SIGSEGV handler"); } + if (sigaction(SIGBUS, &original_sigbus_handler_, NULL) != 0) { + assert_always("Failed to restore original SIGBUS handler"); + } signal_handlers_installed_ = false; } } diff --git a/glue/rexglue-sdk-main/src/core/fiber_posix.cpp b/glue/rexglue-sdk-main/src/core/fiber_posix.cpp index 4fe1ca6f..67d3f4fd 100644 --- a/glue/rexglue-sdk-main/src/core/fiber_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/fiber_posix.cpp @@ -23,10 +23,74 @@ #include <cassert> +#if REX_PLATFORM_PS5 +// [ps5 port] The PS5's getcontext/makecontext/swapcontext don't switch (tested +// on 13.60: 0 switches). An x86-64 switch of the callee-saved registers plus +// MXCSR and the x87 control word, tested on the console (~0.03 us a round +// trip). rex_fiber_switch(&from_sp, to_sp): pushes rbp rbx r12-r15 and an +// 8-byte MXCSR/FCW slot, saves rsp, loads to_sp, pops the same frame, returns. +extern "C" void rex_fiber_switch(void** from_sp, void* to_sp); +__asm__( + ".text\n" + ".p2align 4\n" + ".globl rex_fiber_switch\n" + ".type rex_fiber_switch, @function\n" + "rex_fiber_switch:\n" + " pushq %rbp\n pushq %rbx\n pushq %r12\n pushq %r13\n pushq %r14\n pushq %r15\n" + " subq $8, %rsp\n stmxcsr (%rsp)\n fnstcw 4(%rsp)\n" + " movq %rsp, (%rdi)\n" + " movq %rsi, %rsp\n" + " ldmxcsr (%rsp)\n fldcw 4(%rsp)\n addq $8, %rsp\n" + " popq %r15\n popq %r14\n popq %r13\n popq %r12\n popq %rbx\n popq %rbp\n" + " ret\n" + ".size rex_fiber_switch, .-rex_fiber_switch\n"); +#endif + namespace rex::thread { thread_local Fiber* Fiber::tls_current_ = nullptr; +#if REX_PLATFORM_PS5 +Fiber* Fiber::ConvertCurrentThread() { + auto* f = new Fiber(); // (its sp_ is set by the first switch away) + f->is_thread_fiber_ = true; + tls_current_ = f; + return f; +} + +Fiber* Fiber::Create(size_t stack_size, void (*entry)(void*), void* arg) { + auto* f = new Fiber(); + f->entry_ = entry; + f->arg_ = arg; + f->stack_.resize(stack_size); + uintptr_t top = (reinterpret_cast<uintptr_t>(f->stack_.data()) + stack_size) & ~uintptr_t(15); + // The frame rex_fiber_switch pops (x86-64): [mxcsr|fcw] r15 r14 r13 r12 rbx + // rbp, then the return address = Trampoline. The return slot sits on a + // 16-byte boundary so Trampoline starts with rsp % 16 == 8, as after a call. + auto* frame = reinterpret_cast<uint64_t*>(top - 16 - 7 * 8); + for (int i = 0; i < 7; ++i) frame[i] = 0; + uint32_t mxcsr; + uint16_t fcw; + __asm__ volatile("stmxcsr %0\n fnstcw %1" : "=m"(mxcsr), "=m"(fcw)); + frame[0] = (uint64_t(fcw) << 32) | mxcsr; + frame[7] = reinterpret_cast<uint64_t>(&Fiber::Trampoline); + frame[8] = 0; + f->sp_ = frame; + return f; +} + +/*static*/ void Fiber::Trampoline() { + Fiber* f = tls_current_; + f->entry_(f->arg_); + __builtin_trap(); // (a fiber's entry never returns) +} + +void Fiber::SwitchTo(Fiber* target) { + Fiber* from = tls_current_; + tls_current_ = target; + rex_fiber_switch(&from->sp_, target->sp_); +} +#else Fiber* Fiber::ConvertCurrentThread() { auto* f = new Fiber(); if (getcontext(&f->context_) == -1) { @@ -68,6 +132,8 @@ void Fiber::SwitchTo(Fiber* target) { swapcontext(&from->context_, &target->context_); } +#endif // REX_PLATFORM_PS5 + void Fiber::Destroy() { // Thread fibers are destroyed from the owning thread itself. if (is_thread_fiber_) { diff --git a/glue/rexglue-sdk-main/src/core/filesystem.cpp b/glue/rexglue-sdk-main/src/core/filesystem.cpp index 08e04c7d..50d6f5a4 100644 --- a/glue/rexglue-sdk-main/src/core/filesystem.cpp +++ b/glue/rexglue-sdk-main/src/core/filesystem.cpp @@ -9,6 +9,7 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <rex/platform.h> #include <algorithm> #include <rex/filesystem.h> @@ -33,6 +34,9 @@ std::filesystem::path GetResourcesFolder() { return std::filesystem::path(*override_dir); } } +#if REX_PLATFORM_PS5 + return std::filesystem::path("/app0/Resources"); // [ps5 port] in the app package +#endif return GetExecutableFolder().parent_path() / "Resources"; } diff --git a/glue/rexglue-sdk-main/src/core/filesystem_posix.cpp b/glue/rexglue-sdk-main/src/core/filesystem_posix.cpp index d0d66569..8cd9f203 100644 --- a/glue/rexglue-sdk-main/src/core/filesystem_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/filesystem_posix.cpp @@ -61,6 +61,9 @@ std::filesystem::path GetExecutablePath() { if (dladdr(reinterpret_cast<const void*>(&GetExecutablePath), &info) && info.dli_fname) { return info.dli_fname; } +#elif REX_PLATFORM_PS5 + // [ps5 port] the app package is mounted at /app0. + return std::filesystem::path("/app0/eboot.bin"); #endif char buff[FILENAME_MAX] = ""; readlink("/proc/self/exe", buff, FILENAME_MAX); @@ -89,6 +92,11 @@ int OpenAndroidContentFileDescriptor(const std::string_view uri, const char*) { #endif // REX_PLATFORM_ANDROID std::filesystem::path GetUserFolder() { +#if REX_PLATFORM_PS5 + // [ps5 port] No HOME and no passwd database (getpwuid_r finds nothing): + // user data lives in the app's own folder. + return std::filesystem::path("/app0/UserData"); +#endif // get preferred data home if (auto xdg = rex::platform::env::get("XDG_DATA_HOME")) { return std::filesystem::path(*xdg); diff --git a/glue/rexglue-sdk-main/src/core/logging.cpp b/glue/rexglue-sdk-main/src/core/logging.cpp index 4af3479d..028655c4 100644 --- a/glue/rexglue-sdk-main/src/core/logging.cpp +++ b/glue/rexglue-sdk-main/src/core/logging.cpp @@ -27,6 +27,9 @@ #include <rex/logging.h> #include <rex/platform.h> #include <rex/platform/env.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif #if REX_PLATFORM_WIN32 #include <spdlog/sinks/msvc_sink.h> @@ -242,6 +245,9 @@ void InitLogging(const LogConfig& config) { sink->set_level(spdlog::level::trace); sink->set_pattern(config.file_pattern); g_file_sink = sink; +#if REX_PLATFORM_PS5 + rex::ps5diag::SetLogPath(resolved_path.c_str()); +#endif } g_extra_sinks = config.extra_sinks; diff --git a/glue/rexglue-sdk-main/src/core/mapped_memory_posix.cpp b/glue/rexglue-sdk-main/src/core/mapped_memory_posix.cpp index 92c06c89..26cd8d3d 100644 --- a/glue/rexglue-sdk-main/src/core/mapped_memory_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/mapped_memory_posix.cpp @@ -9,6 +9,9 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <algorithm> +#include <cerrno> +#include <cstdlib> #include <memory> #include <fcntl.h> @@ -18,10 +21,103 @@ #include <rex/filesystem.h> #include <rex/memory/mapped_memory.h> +#include <rex/logging.h> #include <rex/platform.h> namespace rex::memory { +#if REX_PLATFORM_PS5 +// [ps5 port] No file mappings on the PS5: paging a big file in through one +// (the 8 GB disc image) froze the whole console. A "mapping" here is a copy +// of the file's bytes in memory, written back on Flush/Close when writable. +namespace { + +constexpr size_t kMaxFileCopy = size_t(512) << 20; + +bool ReadAll(int fd, uint8_t* p, size_t length, size_t offset) { + while (length) { + const ssize_t n = pread(fd, p, length, off_t(offset)); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) return false; + p += n, offset += size_t(n), length -= size_t(n); + } + return true; +} + +bool WriteAll(int fd, const uint8_t* p, size_t length, size_t offset) { + while (length) { + const ssize_t n = pwrite(fd, p, length, off_t(offset)); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) return false; + p += n, offset += size_t(n), length -= size_t(n); + } + return true; +} + +class Ps5FileCopy : public MappedMemory { + public: + Ps5FileCopy(void* data, size_t size, int fd, size_t offset, bool writable) + : MappedMemory(data, size), fd_(fd), offset_(offset), writable_(writable) {} + ~Ps5FileCopy() override { Close(0); } + + void Close(uint64_t truncate_size) override { + if (data_) { + Flush(); + std::free(data_); + data_ = nullptr; + } + if (fd_ >= 0) { + if (truncate_size) ftruncate(fd_, off_t(truncate_size)); + close(fd_); + fd_ = -1; + } + } + + void Flush() override { + if (writable_ && data_ && fd_ >= 0) WriteAll(fd_, data(), size(), offset_); + } + + private: + int fd_; + size_t offset_; + bool writable_; +}; + +} // namespace + +std::unique_ptr<MappedMemory> MappedMemory::Open(const std::filesystem::path& path, Mode mode, + size_t offset, size_t length) { + const bool writable = mode == Mode::kReadWrite; + const int fd = open(path.c_str(), writable ? O_RDWR : O_RDONLY); + if (fd < 0) return nullptr; + size_t size = length; + if (!size) { + struct stat st {}; + if (fstat(fd, &st) != 0 || size_t(st.st_size) < offset) { + close(fd); + return nullptr; + } + size = size_t(st.st_size) - offset; + } + if (size > kMaxFileCopy) { + REXLOG_WARN("ps5: {} is too big to load whole ({} MB)", path.string(), size >> 20); + close(fd); + return nullptr; + } + auto* data = static_cast<uint8_t*>(std::calloc(std::max<size_t>(size, 1), 1)); + if (!data) { + close(fd); + return nullptr; + } + if (!ReadAll(fd, data, size, offset) && !length) { + std::free(data); + close(fd); + return nullptr; + } + return std::make_unique<Ps5FileCopy>(data, size, fd, offset, writable); +} +#endif // REX_PLATFORM_PS5 + class PosixMappedMemory : public MappedMemory { public: PosixMappedMemory(void* data, size_t size, int file_descriptor) @@ -88,6 +184,7 @@ class PosixMappedMemory : public MappedMemory { int file_descriptor_; }; +#if !REX_PLATFORM_PS5 std::unique_ptr<MappedMemory> MappedMemory::Open(const std::filesystem::path& path, Mode mode, size_t offset, size_t length) { int open_flags = 0; @@ -105,6 +202,7 @@ std::unique_ptr<MappedMemory> MappedMemory::Open(const std::filesystem::path& pa } return PosixMappedMemory::WrapFileDescriptor(file_descriptor, mode, offset, length); } +#endif // !REX_PLATFORM_PS5 #if REX_PLATFORM_ANDROID std::unique_ptr<MappedMemory> MappedMemory::OpenForAndroidContentUri(const std::string_view uri, diff --git a/glue/rexglue-sdk-main/src/core/memory_posix.cpp b/glue/rexglue-sdk-main/src/core/memory_posix.cpp index a9a6ce64..c9419ce9 100644 --- a/glue/rexglue-sdk-main/src/core/memory_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/memory_posix.cpp @@ -9,6 +9,12 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <atomic> +#include <rex/logging.h> +#include <rex/ps5_diag.h> +#endif #include <cerrno> #include <cstddef> #include <cstdio> @@ -199,6 +205,47 @@ void* AllocFixed(void* base_address, size_t length, AllocationType allocation_ty break; } +#if REX_PLATFORM_PS5 + // [ps5 port] No MAP_FIXED_NOREPLACE: a MAP_FIXED anonymous mmap would + // replace the guest views (direct memory, mirrored) with private memory + // from the small flexible pool. Over an existing mapping, reserve and commit + // only change the protection; mmap is for ranges nothing maps yet. + if (base_address != nullptr && + (allocation_type == AllocationType::kCommit || allocation_type == AllocationType::kReserve)) { + const int prot = allocation_type == AllocationType::kReserve ? PROT_NONE + : static_cast<int>(prot_requested); + if (mprotect(base_address, length, prot) == 0) { + return base_address; + } + } + // Inside the guest views, an anonymous MAP_FIXED mmap would silently swap a + // piece of the shared guest memory for private zeroed pages (wiping what was + // there and cutting it off from its mirrors). Only ever change protection + // there, over whole host pages. + { + const uintptr_t a = reinterpret_cast<uintptr_t>(base_address); + if (base_address && a >= rex::ps5diag::guest_arena_lo && a < rex::ps5diag::guest_arena_hi) { + const uintptr_t host_page = page_size(); + const uintptr_t lo = a & ~(host_page - 1); + const uintptr_t hi = (a + length + host_page - 1) & ~(host_page - 1); + const int prot = allocation_type == AllocationType::kReserve ? PROT_NONE + : static_cast<int>(prot_requested); + static std::atomic<int> logged{0}; + if (logged.fetch_add(1) < 16) { + REXLOG_WARN("ps5 memory: AllocFixed {:X}+{:X} type {} inside the guest views (errno {}): " + "protect whole host pages instead of mmap", + a, length, int(allocation_type), errno); + } + if (allocation_type == AllocationType::kReserve) { + return base_address; // (already mapped: never take access away from neighbours) + } + if (mprotect(reinterpret_cast<void*>(lo), hi - lo, prot) == 0) { + return base_address; + } + return nullptr; + } + } +#endif // Build flags - always use MAP_FIXED_NOREPLACE for fixed addresses int flags = MAP_PRIVATE | MAP_ANONYMOUS; #if defined(MAP_FIXED_NOREPLACE) @@ -389,7 +436,13 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, siz } oflag |= O_CREAT; auto full_path = MakeShmName(path); +#if REX_PLATFORM_PS5 + // [ps5 port] An anonymous shared memory object: the exact kind the 4.5 GB + // mirrored guest layout was tested with on a PS5 (13.60). + int ret = shm_open(SHM_ANON, oflag, 0600); +#else int ret = shm_open(full_path.c_str(), oflag, 0777); +#endif if (ret < 0) { return kFileMappingHandleInvalid; } diff --git a/glue/rexglue-sdk-main/src/core/mutex.cpp b/glue/rexglue-sdk-main/src/core/mutex.cpp index d639924c..7f325c0c 100644 --- a/glue/rexglue-sdk-main/src/core/mutex.cpp +++ b/glue/rexglue-sdk-main/src/core/mutex.cpp @@ -11,11 +11,40 @@ #include <rex/thread/mutex.h> +#if defined(__PROSPERO__) +#include <sched.h> +#include <time.h> +#endif + namespace rex::thread { -std::recursive_mutex& global_critical_region::mutex() { - static std::recursive_mutex global_mutex; - return global_mutex; +global_mutex& global_critical_region::mutex() { + static global_mutex the_mutex; + return the_mutex; +} + +#if defined(__PROSPERO__) +// Contended: spin (the holder is usually out within a few microseconds), +// then yield, then nap 50 us at a time. +void global_recursive_mutex::LockSlow(uintptr_t self) { + for (uint32_t tries = 0;; ++tries) { + if (owner_.load(std::memory_order_relaxed) == 0) { + uintptr_t expected = 0; + if (owner_.compare_exchange_strong(expected, self, std::memory_order_acquire, + std::memory_order_relaxed)) { + return; + } + } + if (tries < 2000) { + __builtin_ia32_pause(); + } else if (tries < 2200) { + sched_yield(); + } else { + const timespec nap{0, 50000}; + nanosleep(&nap, nullptr); + } + } } +#endif } // namespace rex::thread diff --git a/glue/rexglue-sdk-main/src/core/ps5_diag.cpp b/glue/rexglue-sdk-main/src/core/ps5_diag.cpp new file mode 100644 index 00000000..752f5201 --- /dev/null +++ b/glue/rexglue-sdk-main/src/core/ps5_diag.cpp @@ -0,0 +1,577 @@ +// [ps5 port] see rex/ps5_diag.h +#include <rex/ps5_diag.h> + +#include <algorithm> +#include <chrono> +#include <cstring> +#include <mutex> +#include <string> +#include <unordered_map> +#include <vector> + +#include <fcntl.h> +#include <pthread.h> +#include <pthread_np.h> +#include <sched.h> +#include <signal.h> +#include <time.h> +#include <unistd.h> + +#include <rex/chrono/clock.h> +#include <rex/frame_stats.h> +#include <rex/logging.h> + +namespace rex::ps5diag { +std::atomic<uint64_t> counters[kCount]; +WaitSlot wait_slots[kWaitSlots]; +uintptr_t guest_arena_lo = 0, guest_arena_hi = 0; +void (*crash_hook)(const char* what) = nullptr; +void (*title_exit_hook)() = nullptr; + +namespace { +std::atomic<int> next_slot{0}; +} + +WaitSlot* CurrentWaitSlot() { + thread_local int index = -1; + if (index < 0) index = next_slot.fetch_add(1); + return index < kWaitSlots ? &wait_slots[index] : nullptr; +} + +uint64_t NowMs() { + // The TSC: every guest kernel wait stamps its start with this, and a + // steady_clock read is a system call on the PS5. + const uint64_t per_ms = rex::chrono::Clock::QueryHostTickFrequency() / 1000; + return per_ms ? rex::chrono::Clock::QueryHostTickCount() / per_ms : 0; +} + +const char* ObjectTypeName(uint32_t type) { + static const char* const kNames[] = {"Undefined", "Enumerator", "Event", "File", + "IOCompletion", "Module", "Mutant", "NotifyListener", + "Semaphore", "Session", "Socket", "SymbolicLink", + "Thread", "Timer"}; + return type < sizeof(kNames) / sizeof(kNames[0]) ? kNames[type] : "?"; +} + +namespace { +struct ThreadEntry { + std::atomic<bool> used{false}; + pthread_t pthread{}; + int tid = 0; + uintptr_t stack_top = 0; + char what[24] = {}; + // filled by the signal handler + std::atomic<int> done{0}; + uintptr_t rip = 0, rsp = 0; + uintptr_t words[64] = {}; + int nwords = 0; + // the profiler's samples (rip relative to the anchor, 0: outside the game) + std::atomic<bool> want_stack{false}; + static constexpr int kProfileSamples = 8192; + uint32_t profile[kProfileSamples] = {}; + // For a sample in a library: the next return address into the game past + // the one it is charged to (who called the wrapper: std::mutex::lock's + // caller, not std::mutex::lock). + uint32_t profile_parent[kProfileSamples] = {}; + std::atomic<uint32_t> profile_count{0}; + uint32_t profile_reported = 0; + // [ps5 port] A library sample's own address (where in libkernel, libc or + // the GPU driver the thread was), for "library time by place". + static constexpr int kLibrarySamples = 2048; + uint64_t library_rip[kLibrarySamples] = {}; + std::atomic<uint32_t> library_count{0}; + uint32_t library_reported = 0; + // The CPUs KeepOtherThreadsOn last gave it (0: none yet). + uint64_t applied_mask = 0; +}; +constexpr int kThreads = 160; // (the game runs about 40) +ThreadEntry threads[kThreads]; +std::atomic<int> next_thread{0}; +thread_local ThreadEntry* my_entry = nullptr; + +constexpr uintptr_t kNear = 256ull << 20; // the eboot text is ~100 MB + +// The allocator's entry points, which only pass a call on: a sample in +// malloc is charged past them, to whoever asked for the memory. +} // namespace +} // namespace rex::ps5diag +extern "C" { +__attribute__((weak)) void* __wrap_malloc(size_t); +__attribute__((weak)) void __wrap_free(void*); +__attribute__((weak)) void* __wrap_calloc(size_t, size_t); +__attribute__((weak)) void* __wrap_realloc(void*, size_t); +__attribute__((weak)) int __wrap_posix_memalign(void**, size_t, size_t); +__attribute__((weak)) void* ps5_heap_malloc(size_t); +__attribute__((weak)) void* ps5_heap_calloc(size_t, size_t); +__attribute__((weak)) void* ps5_heap_memalign(size_t, size_t); +__attribute__((weak)) void* ps5_heap_realloc(void*, size_t); +__attribute__((weak)) void ps5_heap_free(void*); +} +namespace rex::ps5diag { +namespace { +struct PassThrough { + uint32_t begin = 0, end = 0; // anchor-relative words, like the samples +}; +PassThrough pass_through[16]; +int pass_through_count = 0; +void NotePassThrough(const void* function, uint32_t bytes) { + if (!function || pass_through_count >= 16) return; + const uint32_t word = uint32_t(reinterpret_cast<uintptr_t>(function) - Anchor() + 0x10000000); + pass_through[pass_through_count++] = {word, word + bytes}; +} +bool IsPassThrough(uint32_t word) { + for (int i = 0; i < pass_through_count; ++i) + if (word >= pass_through[i].begin && word < pass_through[i].end) return true; + return false; +} +void FindPassThroughs() { + NotePassThrough(reinterpret_cast<const void*>(&__wrap_malloc), 256); + NotePassThrough(reinterpret_cast<const void*>(&__wrap_free), 256); + NotePassThrough(reinterpret_cast<const void*>(&__wrap_calloc), 256); + NotePassThrough(reinterpret_cast<const void*>(&__wrap_realloc), 512); + NotePassThrough(reinterpret_cast<const void*>(&__wrap_posix_memalign), 256); + NotePassThrough(reinterpret_cast<const void*>(&ps5_heap_malloc), 1024); + NotePassThrough(reinterpret_cast<const void*>(&ps5_heap_calloc), 1024); + NotePassThrough(reinterpret_cast<const void*>(&ps5_heap_memalign), 1024); + NotePassThrough(reinterpret_cast<const void*>(&ps5_heap_realloc), 1024); + NotePassThrough(reinterpret_cast<const void*>(&ps5_heap_free), 1024); + void* (*new_one)(size_t) = &::operator new; + void* (*new_array)(size_t) = &::operator new[]; + void (*delete_one)(void*) noexcept = &::operator delete; + void (*delete_array)(void*) noexcept = &::operator delete[]; + void (*delete_sized)(void*, size_t) noexcept = &::operator delete; + NotePassThrough(reinterpret_cast<const void*>(new_one), 256); + NotePassThrough(reinterpret_cast<const void*>(new_array), 256); + NotePassThrough(reinterpret_cast<const void*>(delete_one), 128); + NotePassThrough(reinterpret_cast<const void*>(delete_array), 128); + NotePassThrough(reinterpret_cast<const void*>(delete_sized), 128); +} + +void SampleHandler(int, siginfo_t*, void* uc) { + ThreadEntry* e = my_entry; + if (!e) return; + auto* gregs = reinterpret_cast<uint64_t*>(reinterpret_cast<uint8_t*>(uc) + 64); + if (!e->want_stack.load(std::memory_order_acquire)) { + // a profiler sample: where the thread is, nothing else + // (The eboot loads low on the PS5, below kNear: anchor - kNear would + // wrap and drop every sample.) + const uintptr_t rip = gregs[20], anchor = Anchor(); + const uintptr_t lo = anchor > kNear ? anchor - kNear : 0x10000; + uint32_t rel = rip > lo && rip < anchor + kNear ? uint32_t(rip - anchor + 0x10000000) : 0; + uint32_t parent = 0; + if (rel) { + // In the game: note the nearest return address into it in another + // block, most often its caller (malloc's, a shared_ptr release's). + const uintptr_t rsp = gregs[23] & ~uintptr_t(7); + uintptr_t end = rsp + 24 * 8; + if (e->stack_top && e->stack_top < end) end = e->stack_top; + for (uintptr_t p = rsp; p + 8 <= end; p += 8) { + const uintptr_t v = *reinterpret_cast<const uintptr_t*>(p); + if (v > lo && v < anchor + kNear) { + const uint32_t word = uint32_t(v - anchor + 0x10000000); + if ((word & ~63u) != (rel & ~63u) && !IsPassThrough(word)) { + parent = word; + break; + } + } + } + } else { + { + const uint32_t k = e->library_count.load(std::memory_order_relaxed); + e->library_rip[k % ThreadEntry::kLibrarySamples] = rip; + e->library_count.store(k + 1, std::memory_order_release); + } + // In a library (libc's memcpy, a kernel wait...): charge the game code + // that called it, the nearest return address into the eboot on the + // stack, marked with the top bit; and note the next one in another + // block, its likely caller. + const uintptr_t rsp = gregs[23] & ~uintptr_t(7); + uintptr_t end = rsp + 40 * 8; + if (e->stack_top && e->stack_top < end) end = e->stack_top; + for (uintptr_t p = rsp; p + 8 <= end; p += 8) { + const uintptr_t v = *reinterpret_cast<const uintptr_t*>(p); + if (v > lo && v < anchor + kNear) { + const uint32_t word = uint32_t(v - anchor + 0x10000000); + if (!rel) { + if (p >= rsp + 24 * 8) break; + rel = word | 0x80000000u; + } else if ((word & ~63u) != (rel & 0x7FFFFFC0u) && !IsPassThrough(word)) { + parent = word; + break; + } + } + } + } + const uint32_t n = e->profile_count.load(std::memory_order_relaxed); + e->profile_parent[n % ThreadEntry::kProfileSamples] = parent; + e->profile[n % ThreadEntry::kProfileSamples] = rel; + e->profile_count.store(n + 1, std::memory_order_release); + return; + } + e->want_stack.store(false, std::memory_order_release); + e->rip = gregs[20]; + e->rsp = gregs[23]; + uintptr_t anchor = Anchor(); + uintptr_t end = e->rsp + 64 * 1024; + if (e->stack_top && e->stack_top < end) end = e->stack_top; + int n = 0; + for (uintptr_t p = e->rsp & ~uintptr_t(7); p + 8 <= end && n < 64; p += 8) { + uintptr_t v = *reinterpret_cast<uintptr_t*>(p); + if (v > (anchor > kNear ? anchor - kNear : 0x10000) && v < anchor + kNear) e->words[n++] = v; + } + e->nwords = n; + e->done.store(1, std::memory_order_release); +} +} // namespace + +namespace { +std::mutex cs_mutex; +std::unordered_map<uint32_t, uint32_t>& CsInits() { + static auto* m = new std::unordered_map<uint32_t, uint32_t>(); + return *m; +} +} // namespace +void NoteCriticalSectionInit(uint32_t guest, uint32_t lr) { + std::lock_guard<std::mutex> lock(cs_mutex); + CsInits()[guest] = lr ? lr : 1; +} +uint32_t CriticalSectionInitBy(uint32_t guest) { + std::lock_guard<std::mutex> lock(cs_mutex); + auto it = CsInits().find(guest); + return it == CsInits().end() ? 0 : it->second; +} + +namespace { +char log_path[512]; +// Appends to a fixed buffer without the heap (the report below runs in a +// fault handler, possibly inside the allocator). +struct RawLine { + char text[2048]; + size_t size = 0; + void Add(const char* s) { + while (*s && size + 1 < sizeof(text)) text[size++] = *s++; + } + void Hex(uint64_t v) { + char digits[17]; + int n = 0; + do { + digits[n++] = "0123456789ABCDEF"[v & 15]; + v >>= 4; + } while (v); + while (n && size + 1 < sizeof(text)) text[size++] = digits[--n]; + } + void Dec(uint64_t v) { + char digits[21]; + int n = 0; + do { + digits[n++] = char('0' + v % 10); + v /= 10; + } while (v); + while (n && size + 1 < sizeof(text)) text[size++] = digits[--n]; + } +}; +} // namespace + +std::atomic<uint64_t> crashed_at_ms{0}; + +void SetLogPath(const char* path) { + if (!path) return; + std::strncpy(log_path, path, sizeof(log_path) - 1); +} + +// [ps5 port] No heap and no logger here: the fault can be inside the +// allocator with its lock held (a heap corruption), where the old report's +// first string append waited on that lock forever and the game froze behind +// its loading screen. The line goes straight to the end of the log file. +void UnhandledHostFault(uintptr_t rip, uintptr_t address, uintptr_t rsp) { + static std::atomic<uint64_t> count{0}; + const uint64_t n = ++count; + if (n > 1) { + // Again (it retries the instruction) or another thread: the first report + // ends the game. + if (n == 2 && crash_hook) crash_hook("a thread crashed"); // (doesn't return) + return; + } + uint64_t expected = 0; + crashed_at_ms.compare_exchange_strong(expected, NowMs() | 1); + const uintptr_t anchor = Anchor(); + static RawLine line; + line.Add("\n[ps5 crash] thread "); + line.Dec(uint64_t(my_entry ? my_entry->tid : pthread_getthreadid_np())); + line.Add(" ("); + line.Add(my_entry ? my_entry->what : "unregistered"); + line.Add(") faulted at "); + line.Hex(address); + line.Add(" (rip "); + line.Hex(rip); + line.Add(", anchor-relative "); + line.Hex(rip - anchor + 0x10000000); + line.Add(") | stack:"); + if (ThreadEntry* e = my_entry) { + uintptr_t end = rsp + 64 * 1024; + if (e->stack_top && e->stack_top < end) end = e->stack_top; + int k = 0; + for (uintptr_t p = rsp & ~uintptr_t(7); p + 8 <= end && k < 48; p += 8) { + uintptr_t v = *reinterpret_cast<uintptr_t*>(p); + if (v > (anchor > kNear ? anchor - kNear : 0x10000) && v < anchor + kNear) { + line.Add(" "); + line.Hex(v - anchor + 0x10000000); + ++k; + } + } + } + line.Add("\n"); + const char* path = log_path[0] ? log_path : "/app0/logs/crash.log"; + const int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0666); + if (fd >= 0) { + (void)write(fd, line.text, line.size); + close(fd); + } +} + +uintptr_t Anchor() { return reinterpret_cast<uintptr_t>(&NowMs); } + +namespace { +void InstallSampleHandler() { + static std::once_flag once; + std::call_once(once, [] { + FindPassThroughs(); + struct sigaction sa = {}; + sa.sa_sigaction = SampleHandler; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sigemptyset(&sa.sa_mask); + sigaction(SIGPROF, &sa, nullptr); + }); +} +} // namespace + +void ProfileTick() { + InstallSampleHandler(); + const int count = std::min(next_thread.load(), kThreads); + for (int i = 0; i < count; ++i) { + ThreadEntry& e = threads[i]; + if (!e.used.load(std::memory_order_acquire) || e.want_stack.load()) continue; + pthread_kill(e.pthread, SIGPROF); + } +} + +void ProfileReport() { + struct Busy { + ThreadEntry* e; + uint32_t total, inside, via; + std::unordered_map<uint32_t, uint32_t> hits, callers; + std::unordered_map<uint64_t, uint32_t> pairs; // caller block << 32 | its caller's block + std::unordered_map<uint64_t, uint32_t> code_pairs; // block << 32 | its caller's block + std::unordered_map<uint64_t, uint32_t> places; // library samples by 16-byte address + }; + std::vector<Busy> busy; + const int count = std::min(next_thread.load(), kThreads); + // [ps5 port] The game's render thread (the one the frame limiter runs on) + // by its own name, not "rex" like every other game thread. + const uintptr_t title = rex::frame_stats::g_title_thread.load(std::memory_order_relaxed); + for (int i = 0; i < count; ++i) { + ThreadEntry& e = threads[i]; + if (!e.used.load(std::memory_order_acquire)) continue; + if (title && reinterpret_cast<uintptr_t>(e.pthread) == title && std::strcmp(e.what, "title") != 0) + std::strncpy(e.what, "title", sizeof(e.what) - 1); + const uint32_t end = e.profile_count.load(std::memory_order_acquire); + uint32_t begin = e.profile_reported; + if (end - begin > uint32_t(ThreadEntry::kProfileSamples)) + begin = end - ThreadEntry::kProfileSamples; + e.profile_reported = end; + Busy b{&e, end - begin, 0, 0, {}, {}, {}, {}, {}}; + { + const uint32_t lib_end = e.library_count.load(std::memory_order_acquire); + uint32_t lib_begin = e.library_reported; + if (lib_end - lib_begin > uint32_t(ThreadEntry::kLibrarySamples)) + lib_begin = lib_end - ThreadEntry::kLibrarySamples; + e.library_reported = lib_end; + for (uint32_t k = lib_begin; k != lib_end; ++k) + if (const uint64_t rip = e.library_rip[k % ThreadEntry::kLibrarySamples]) ++b.places[rip & ~uint64_t(15)]; + } + for (uint32_t k = begin; k != end; ++k) { + const uint32_t pc = e.profile[k % ThreadEntry::kProfileSamples]; + if (!pc) continue; + // by 64-byte block, so one function's instructions mostly add up + if (pc & 0x80000000u) { + ++b.callers[pc & 0x7FFFFFC0u], ++b.via; + const uint32_t parent = e.profile_parent[k % ThreadEntry::kProfileSamples]; + if (parent) ++b.pairs[uint64_t(pc & 0x7FFFFFC0u) << 32 | (parent & ~63u)]; + } else { + ++b.hits[pc & ~63u], ++b.inside; + const uint32_t parent = e.profile_parent[k % ThreadEntry::kProfileSamples]; + if (parent) ++b.code_pairs[uint64_t(pc & ~63u) << 32 | (parent & ~63u)]; + } + } + // Threads that ran game code; ones only ever blocked (idle job threads) + // would otherwise crowd the busy ones out of the report. + if (b.inside * 20 >= b.total) busy.push_back(std::move(b)); + } + std::sort(busy.begin(), busy.end(), [](const Busy& a, const Busy& b) { return a.inside > b.inside; }); + + REXLOG_INFO("ps5 prof: anchor {:X}, {} threads ran game code", Anchor(), busy.size()); + { + // Where some library functions are, to name the places below by: a place + // just past one of these is most likely in it. + static bool landmarks_logged = false; + if (!landmarks_logged) { + landmarks_logged = true; + void* (*mcpy)(void*, const void*, size_t) = &memcpy; + void* (*mmove)(void*, const void*, size_t) = &memmove; + void* (*mset)(void*, int, size_t) = &memset; + int (*lock)(pthread_mutex_t*) = &pthread_mutex_lock; + int (*unlock)(pthread_mutex_t*) = &pthread_mutex_unlock; + int (*cwait)(pthread_cond_t*, pthread_mutex_t*) = &pthread_cond_wait; + int (*csignal)(pthread_cond_t*) = &pthread_cond_signal; + int (*sleep)(const timespec*, timespec*) = &nanosleep; + int (*yield)() = &sched_yield; + void* (*get)(pthread_key_t) = &pthread_getspecific; + int (*clock)(clockid_t, timespec*) = &clock_gettime; + REXLOG_INFO("ps5 prof: landmarks memcpy {:X} memmove {:X} memset {:X} mutex_lock {:X} mutex_unlock {:X} " + "cond_wait {:X} cond_signal {:X} nanosleep {:X} sched_yield {:X} getspecific {:X} " + "clock_gettime {:X}", + uintptr_t(mcpy), uintptr_t(mmove), uintptr_t(mset), uintptr_t(lock), uintptr_t(unlock), + uintptr_t(cwait), uintptr_t(csignal), uintptr_t(sleep), uintptr_t(yield), uintptr_t(get), + uintptr_t(clock)); + } + } + // Every block seen twice or more, 120 to a line: the whole picture, not + // just the top (a frame's work is spread over many functions). + auto emit = [](const Busy& b, const char* kind, const std::unordered_map<uint32_t, uint32_t>& map, + uint32_t n) { + std::vector<std::pair<uint32_t, uint32_t>> all(map.begin(), map.end()); + std::sort(all.begin(), all.end(), [](const auto& x, const auto& y) { return x.second > y.second; }); + std::string line; + int entries = 0, part = 0; + for (const auto& [pc, hits] : all) { + if (hits < 2) break; + char buf[32]; + snprintf(buf, sizeof(buf), " %X:%u", pc, hits); + line += buf; + if (++entries == 120) { + REXLOG_INFO("ps5 prof: tid {} {}: {} samples, {} {} part {}:{}", b.e->tid, b.e->what, b.total, + n, kind, ++part, line); + line.clear(); + entries = 0; + } + } + if (entries || !part) + REXLOG_INFO("ps5 prof: tid {} {}: {} samples, {} {} part {}:{}", b.e->tid, b.e->what, b.total, n, + kind, ++part, line); + }; + // Library time by caller and that caller's caller ("A/B"), the busiest 120. + auto emit_pairs = [](const Busy& b, const std::unordered_map<uint64_t, uint32_t>& pairs, + const char* kind, uint32_t n) { + std::vector<std::pair<uint64_t, uint32_t>> all(pairs.begin(), pairs.end()); + std::sort(all.begin(), all.end(), [](const auto& x, const auto& y) { return x.second > y.second; }); + std::string line; + int entries = 0; + for (const auto& [key, hits] : all) { + if (hits < 3 || entries == 120) break; + char buf[40]; + snprintf(buf, sizeof(buf), " %X/%X:%u", uint32_t(key >> 32), uint32_t(key), hits); + line += buf; + ++entries; + } + if (entries) + REXLOG_INFO("ps5 prof: tid {} {}: {} samples, {} {} part 1:{}", b.e->tid, b.e->what, b.total, n, + kind, line); + }; + for (size_t t = 0; t < busy.size() && t < 10; ++t) { + emit(busy[t], "in game code", busy[t].hits, busy[t].inside); + emit(busy[t], "in libraries, by caller", busy[t].callers, busy[t].via); + emit_pairs(busy[t], busy[t].pairs, "in libraries, by caller/parent", busy[t].via); + emit_pairs(busy[t], busy[t].code_pairs, "in game code, by block/parent", busy[t].inside); + if (t < 5 && !busy[t].places.empty()) { + std::vector<std::pair<uint64_t, uint32_t>> all(busy[t].places.begin(), busy[t].places.end()); + std::sort(all.begin(), all.end(), [](const auto& x, const auto& y) { return x.second > y.second; }); + std::string line; + for (size_t i = 0; i < all.size() && i < 60 && all[i].second >= 2; ++i) { + char buf[40]; + snprintf(buf, sizeof(buf), " %llX:%u", static_cast<unsigned long long>(all[i].first), all[i].second); + line += buf; + } + REXLOG_INFO("ps5 prof: tid {} {}: library time by place:{}", busy[t].e->tid, busy[t].e->what, line); + } + } +} + +void RegisterThread(const char* what) { + if (my_entry) return; + int i = next_thread.fetch_add(1); + if (i >= kThreads) return; + ThreadEntry& e = threads[i]; + e.pthread = pthread_self(); + e.tid = pthread_getthreadid_np(); + int marker = 0; + e.stack_top = reinterpret_cast<uintptr_t>(&marker) + 256; // (a little above: the caller's frame) + std::strncpy(e.what, what ? what : "", sizeof(e.what) - 1); + my_entry = &e; + e.used.store(true, std::memory_order_release); +} + +void UnregisterThread() { + ThreadEntry* e = my_entry; + if (!e) return; + // No more samples or CPU moves for it: its pthread_t can be another + // thread's soon, and a sample landing after its thread-specific data is + // gone would take a lookup that allocates in a signal handler. + sigset_t prof; + sigemptyset(&prof); + sigaddset(&prof, SIGPROF); + pthread_sigmask(SIG_BLOCK, &prof, nullptr); + e->used.store(false, std::memory_order_release); + my_entry = nullptr; +} + +extern "C" int32_t scePthreadSetaffinity(pthread_t thread, uint64_t mask); +void KeepOtherThreadsOn(uint64_t mask, uintptr_t title_thread, uintptr_t main_thread, + uint64_t main_mask) { + int count = next_thread.load(); + if (count > kThreads) count = kThreads; + for (int i = 0; i < count; ++i) { + ThreadEntry& e = threads[i]; + if (!e.used.load(std::memory_order_acquire)) continue; + if (!std::strcmp(e.what, "worker") || !std::strcmp(e.what, "recorder") || + !std::strcmp(e.what, "capture")) + continue; + if (reinterpret_cast<uintptr_t>(e.pthread) == title_thread) continue; + const bool main = main_thread && reinterpret_cast<uintptr_t>(e.pthread) == main_thread; + const uint64_t want = main && main_mask ? main_mask : mask; + // Only when it changes: moving waiting threads every 10 s is the one thing + // 0.6b2's frozen sessions had in common. + if (e.applied_mask == want) continue; + e.applied_mask = want; + scePthreadSetaffinity(e.pthread, want); + } +} + +void SampleAllThreads() { + static std::mutex sampling; // the stall watcher and the hang monitor + std::lock_guard<std::mutex> one_at_a_time(sampling); + InstallSampleHandler(); + REXLOG_INFO("ps5 stack: anchor {:X}", Anchor()); + int count = next_thread.load(); + if (count > kThreads) count = kThreads; + for (int i = 0; i < count; ++i) { + ThreadEntry& e = threads[i]; + if (!e.used.load(std::memory_order_acquire)) continue; + e.done.store(0); + e.want_stack.store(true, std::memory_order_release); + if (pthread_kill(e.pthread, SIGPROF) != 0) continue; + for (int ms = 0; ms < 200 && !e.done.load(std::memory_order_acquire); ++ms) usleep(1000); + if (!e.done.load()) { + REXLOG_INFO("ps5 stack: tid {} {}: no answer", e.tid, e.what); + continue; + } + std::string line; + for (int k = 0; k < e.nwords; ++k) { + char buf[24]; + snprintf(buf, sizeof(buf), " %lX", (unsigned long)(e.words[k] - Anchor() + 0x10000000)); + line += buf; + } + REXLOG_INFO("ps5 stack: tid {} {}: rip {:X} |{}", e.tid, e.what, + e.rip - Anchor() + 0x10000000, line); + } +} +} // namespace rex::ps5diag diff --git a/glue/rexglue-sdk-main/src/core/ps5_libc_extras.c b/glue/rexglue-sdk-main/src/core/ps5_libc_extras.c new file mode 100644 index 00000000..95c028d7 --- /dev/null +++ b/glue/rexglue-sdk-main/src/core/ps5_libc_extras.c @@ -0,0 +1,128 @@ +/** + * [ps5 port] Standard functions the PS5's libc doesn't export, used by the + * runtime (ffs: math_gcc.cpp; timegm: STFS timestamps) and SDL3 (getresuid / + * getresgid: its GTK loader, never used on the PS5). isatty / mkstemp: the + * PS5 only exports them from libScePosixForWebKit, which a game can't use + * (first boot: spdlog's isatty call jumped to address 0). + */ +#if defined(__PROSPERO__) +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <sys/types.h> +#include <time.h> +#include <unistd.h> + +// [ps5 port] Imports only the system's libkernel_sys exports, which a game +// can't load: each one was a jump to address 0 (libc++'s create_hard_link +// called link() when the game reopened its save: a crash that looped behind +// "NOW LOADING"). They fail cleanly instead, and every caller has a fallback +// (hard link -> a copy). +int link(const char* from, const char* to) { + (void)from, (void)to; + errno = ENOTSUP; + return -1; +} +int symlink(const char* target, const char* path) { + (void)target, (void)path; + errno = ENOTSUP; + return -1; +} +ssize_t readlink(const char* path, char* buf, size_t size) { + (void)path, (void)buf, (void)size; + errno = EINVAL; // (not a symbolic link: there are none) + return -1; +} +long pathconf(const char* path, int name) { + (void)path; + switch (name) { + case _PC_NAME_MAX: return 255; + case _PC_PATH_MAX: return 1024; + case _PC_LINK_MAX: return 1; + default: errno = EINVAL; return -1; + } +} +pid_t fork(void) { + errno = ENOSYS; + return -1; +} +pid_t vfork(void) { + errno = ENOSYS; + return -1; +} +pid_t setsid(void) { + errno = EPERM; + return -1; +} + +// No terminals on a PS5. +int isatty(int fd) { + (void)fd; + errno = ENOTTY; + return 0; +} + +// mkstemp: replace the trailing XXXXXX until a new file can be created. +int mkstemp(char* path) { + static const char kChars[] = "abcdefghijklmnopqrstuvwxyz0123456789"; + size_t length = path ? strlen(path) : 0; + if (length < 6 || strcmp(path + length - 6, "XXXXXX") != 0) { + errno = EINVAL; + return -1; + } + static uint64_t counter = 0; + for (int attempt = 0; attempt < 1000; ++attempt) { + uint64_t v = (uint64_t)time(NULL) * 2654435761u + (++counter) * 0x9E3779B97F4A7C15ull; + for (int i = 0; i < 6; ++i) { + path[length - 6 + i] = kChars[v % 36]; + v /= 36; + } + int fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0 || errno != EEXIST) return fd; + } + errno = EEXIST; + return -1; +} + +int ffs(int value) { return __builtin_ffs(value); } + +// UTC broken-down time to seconds since the epoch (days_from_civil, Howard +// Hinnant's public-domain algorithm); normalizes like mktime does. +time_t timegm(struct tm* tm) { + int64_t year = (int64_t)tm->tm_year + 1900; + int64_t month = tm->tm_mon; // 0..11, may be out of range + year += month / 12; + month %= 12; + if (month < 0) { + month += 12; + --year; + } + int64_t m = month + 1; // 1..12 + int64_t y = year - (m <= 2); + int64_t era = (y >= 0 ? y : y - 399) / 400; + int64_t yoe = y - era * 400; + int64_t doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + tm->tm_mday - 1; + int64_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + int64_t days = era * 146097 + doe - 719468; + return (time_t)(days * 86400 + (int64_t)tm->tm_hour * 3600 + (int64_t)tm->tm_min * 60 + + tm->tm_sec); +} + +int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid) { + uid_t u = getuid(), e = geteuid(); + if (ruid) *ruid = u; + if (euid) *euid = e; + if (suid) *suid = e; + return 0; +} + +int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid) { + gid_t g = getgid(), e = getegid(); + if (rgid) *rgid = g; + if (egid) *egid = e; + if (sgid) *sgid = e; + return 0; +} +#endif diff --git a/glue/rexglue-sdk-main/src/core/threading_posix.cpp b/glue/rexglue-sdk-main/src/core/threading_posix.cpp index 8c31d93a..0556a09f 100644 --- a/glue/rexglue-sdk-main/src/core/threading_posix.cpp +++ b/glue/rexglue-sdk-main/src/core/threading_posix.cpp @@ -27,15 +27,23 @@ static_assert(REX_PLATFORM_LINUX || REX_PLATFORM_MAC, "This file is POSIX-only") #if REX_PLATFORM_ANDROID #include <android/log.h> #endif +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif #include <semaphore.h> +#if !REX_PLATFORM_PS5 #include <sys/eventfd.h> #include <sys/syscall.h> +#endif #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #include <rex/assert.h> +#include <rex/cvar.h> #include <rex/chrono/chrono_steady_cast.h> +#include <rex/chrono/clock.h> +#include <rex/frame_stats.h> #include <rex/logging.h> #include <rex/thread/timer_queue.h> @@ -49,7 +57,79 @@ static_assert(REX_PLATFORM_LINUX || REX_PLATFORM_MAC, "This file is POSIX-only") #include <rex/string.h> #endif -#if REX_PLATFORM_LINUX +#if REX_PLATFORM_PS5 +// [ps5 port] FreeBSD naming of the thread helpers the PS5 exports. +#include <pthread_np.h> +extern "C" int pthread_getname_np(pthread_t thread, char* name, size_t len); +static inline pid_t rex_system_tid() { return pid_t(pthread_getthreadid_np()); } + +// [ps5 port] Thread stacks in direct memory. libkernel takes a thread's stack +// from the title's flexible memory (about 400 MiB in all), and every guest +// thread asks for a 16 MiB host stack: around 20 of them and pthread_create +// fails (the game's thread creation returned STATUS_NO_MEMORY). Direct memory +// has gigabytes. A stack is kept for reuse once its thread has been joined. +#include <ps5platform/shm.h> +#include <mutex> +#include <vector> +namespace { +struct Ps5Stack { + void* base = nullptr; // the reservation: a guard, then the stack + size_t bytes = 0; // the stack's size +}; +constexpr size_t kPs5StackUnit = 64 * 1024; +constexpr size_t kPs5StackGuard = 64 * 1024; +std::mutex ps5_stack_mutex; +std::vector<Ps5Stack>& Ps5StackCache() { + static auto* cache = new std::vector<Ps5Stack>(); + return *cache; +} +bool Ps5StackAlloc(size_t bytes, Ps5Stack* out) { + bytes = (bytes + kPs5StackUnit - 1) & ~(kPs5StackUnit - 1); + { + std::lock_guard<std::mutex> lock(ps5_stack_mutex); + auto& cache = Ps5StackCache(); + for (size_t i = 0; i < cache.size(); ++i) { + if (cache[i].bytes == bytes) { + *out = cache[i]; + cache.erase(cache.begin() + i); + return true; + } + } + } + // A reserved range with an unmapped guard below the stack, and the stack a + // direct-memory object mapped into the rest of it. + void* base = nullptr; + if (ps5_vrange_reserve(bytes + kPs5StackGuard, nullptr, kPs5StackUnit, &base) != 0 || !base) { + return false; + } + struct ps5_shm shm = {}; + if (ps5_shm_create(bytes, &shm) != 0) { + ps5_vrange_release(base, bytes + kPs5StackGuard); + return false; + } + void* view = nullptr; + if (ps5_shm_map(&shm, 0, bytes, static_cast<uint8_t*>(base) + kPs5StackGuard, + PS5_SHM_READ | PS5_SHM_WRITE, PS5_SHM_FIXED, &view) != 0 || + view != static_cast<uint8_t*>(base) + kPs5StackGuard) { + ps5_shm_destroy(&shm); + ps5_vrange_release(base, bytes + kPs5StackGuard); + return false; + } + out->base = base; + out->bytes = bytes; + return true; +} +void Ps5StackFree(const Ps5Stack& stack) { + if (!stack.base) return; + std::lock_guard<std::mutex> lock(ps5_stack_mutex); + Ps5StackCache().push_back(stack); +} +} // namespace +#else +static inline pid_t rex_system_tid() { return pid_t(syscall(SYS_gettid)); } +#endif + +#if REX_PLATFORM_LINUX && !REX_PLATFORM_PS5 // SIGEV_THREAD_ID in timer_create(...) is a Linux extension #define REX_HAS_SIGEV_THREAD_ID 1 #ifdef __GLIBC__ @@ -62,6 +142,11 @@ static_assert(REX_PLATFORM_LINUX || REX_PLATFORM_MAC, "This file is POSIX-only") #define REX_HAS_SIGEV_THREAD_ID 0 #endif +#if REX_PLATFORM_PS5 +REXCVAR_DEFINE_INT32(ps5_wait_spin_us, 40, "PS5", + "Kernel object waits spin this long (us) for a signal before they sleep (0 = never spin)"); +#endif + namespace rex::thread { #if REX_PLATFORM_ANDROID @@ -115,12 +200,24 @@ enum class SignalType { }; int GetSystemSignal(SignalType num) { +#if REX_PLATFORM_PS5 // [ps5 port] no realtime signals on the PS5 + switch (num) { + case SignalType::kThreadSuspend: + return SIGUSR1; + case SignalType::kThreadUserCallback: + default: + return SIGUSR2; + } +#endif auto result = SIGRTMIN + static_cast<int>(num); assert_true(result < SIGRTMAX); return result; } SignalType GetSystemSignalType(int num) { +#if REX_PLATFORM_PS5 + return num == SIGUSR1 ? SignalType::kThreadSuspend : SignalType::kThreadUserCallback; +#endif return static_cast<SignalType>(num - SIGRTMIN); } @@ -150,7 +247,7 @@ void EnableAffinityConfiguration() {} // uint64_t ticks() { return mach_absolute_time(); } uint32_t current_thread_system_id() { - return static_cast<uint32_t>(syscall(SYS_gettid)); + return static_cast<uint32_t>(rex_system_tid()); } void MaybeYield() { @@ -163,6 +260,9 @@ void SyncMemory() { } void Sleep(std::chrono::microseconds duration) { +#if REX_PLATFORM_PS5 + const rex::frame_stats::TitleWaitScope title_wait; +#endif timespec rqtp = DurationToTimeSpec(duration); timespec rmtp = {}; auto p_rqtp = &rqtp; @@ -221,7 +321,7 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) { class PosixConditionBase { public: PosixConditionBase() { -#if REX_PLATFORM_LINUX && !REX_PLATFORM_ANDROID // Bionic has no robust mutexes +#if REX_PLATFORM_LINUX && !REX_PLATFORM_ANDROID && !REX_PLATFORM_PS5 // Bionic/PS5 have no robust mutexes // Use robust mutexes so waits can recover if owner thread terminates. pthread_mutexattr_t attr; if (pthread_mutexattr_init(&attr) == 0) { @@ -241,7 +341,7 @@ class PosixConditionBase { WaitResult Wait(std::chrono::milliseconds timeout) { bool executed; auto predicate = [this] { return this->signaled(); }; -#if REX_PLATFORM_LINUX && !REX_PLATFORM_ANDROID // Bionic has no robust mutexes +#if REX_PLATFORM_LINUX && !REX_PLATFORM_ANDROID && !REX_PLATFORM_PS5 // Bionic/PS5 have no robust mutexes auto native_mutex = static_cast<pthread_mutex_t*>(mutex_.native_handle()); int lock_result = pthread_mutex_lock(native_mutex); if (lock_result == EOWNERDEAD) { @@ -252,6 +352,11 @@ class PosixConditionBase { std::unique_lock<std::mutex> lock(mutex_, std::adopt_lock); #else std::unique_lock<std::mutex> lock(mutex_); +#endif +#if REX_PLATFORM_PS5 + if (!predicate() && timeout.count() > 0) { + SpinForSignal(lock); + } #endif if (predicate()) { executed = true; @@ -378,6 +483,9 @@ class PosixConditionBase { // Callers hold mutex_. Wakes every WaitMultiple currently including this // handle; lock order is always handle mutex_ then waiter mutex. void NotifyExternalWaiters() { +#if REX_PLATFORM_PS5 + signal_generation_.fetch_add(1, std::memory_order_release); +#endif for (ExternalWaiter* waiter : external_waiters_) { std::lock_guard<std::mutex> lock(waiter->mutex); ++waiter->generation; @@ -395,6 +503,41 @@ class PosixConditionBase { std::condition_variable cond_; std::mutex mutex_; std::vector<ExternalWaiter*> external_waiters_; // guarded by mutex_ + +#if REX_PLATFORM_PS5 + // [ps5 port] Before a wait sleeps, it watches the object's signal count for + // ps5_wait_spin_us without holding its lock: PS5 thread wake-ups are slow, + // and the game hands work between its threads constantly. Returns with the + // lock held. + void SpinForSignal(std::unique_lock<std::mutex>& lock) { + const int32_t spin_us = REXCVAR_GET(ps5_wait_spin_us); + if (spin_us <= 0) { + return; + } + uint32_t generation = signal_generation_.load(std::memory_order_relaxed); + lock.unlock(); + // The TSC, not steady_clock: a clock_gettime system call every few + // pauses made the spin itself expensive. + const uint64_t end = rex::chrono::Clock::QueryHostTickCount() + + rex::chrono::Clock::QueryHostTickFrequency() * uint64_t(spin_us) / 1000000; + do { + for (int i = 0; i < 8; ++i) { + __builtin_ia32_pause(); + } + const uint32_t now_generation = signal_generation_.load(std::memory_order_acquire); + if (now_generation != generation) { + lock.lock(); + if (signaled()) { + return; + } + lock.unlock(); + generation = now_generation; + } + } while (rex::chrono::Clock::QueryHostTickCount() < end); + lock.lock(); + } + std::atomic<uint32_t> signal_generation_{0}; // bumped on every signal +#endif }; // There really is no native POSIX handle for a single wait/signal construct @@ -617,10 +760,24 @@ class PosixCondition<Thread> : public PosixConditionBase { pthread_attr_t attr; if (pthread_attr_init(&attr) != 0) return false; +#if REX_PLATFORM_PS5 + if (params.stack_size > 2 * 1024 * 1024 && Ps5StackAlloc(params.stack_size, &ps5_stack_)) { + if (pthread_attr_setstack(&attr, static_cast<uint8_t*>(ps5_stack_.base) + kPs5StackGuard, + ps5_stack_.bytes) != 0) { + Ps5StackFree(ps5_stack_); + ps5_stack_ = {}; + } + } + if (!ps5_stack_.base && pthread_attr_setstacksize(&attr, params.stack_size) != 0) { + pthread_attr_destroy(&attr); + return false; + } +#else if (pthread_attr_setstacksize(&attr, params.stack_size) != 0) { pthread_attr_destroy(&attr); return false; } +#endif if (params.initial_priority != 0) { sched_param sched{}; sched.sched_priority = params.initial_priority + 1; @@ -633,7 +790,13 @@ class PosixCondition<Thread> : public PosixConditionBase { return false; } } - if (pthread_create(&thread_, &attr, ThreadStartRoutine, start_data) != 0) { + if (int err = pthread_create(&thread_, &attr, ThreadStartRoutine, start_data); err != 0) { +#if REX_PLATFORM_PS5 + REXLOG_ERROR("ps5: pthread_create failed: error {} (stack {} KB, {})", err, params.stack_size / 1024, + ps5_stack_.base ? "direct memory" : "libkernel's"); + Ps5StackFree(ps5_stack_); + ps5_stack_ = {}; +#endif pthread_attr_destroy(&attr); return false; } @@ -680,6 +843,8 @@ class PosixCondition<Thread> : public PosixConditionBase { std::lock_guard<std::mutex> lock(android_pre_api_26_name_mutex_); rex::string::copy_truncating(result.data(), android_pre_api_26_name_, result.size()); } +#elif REX_PLATFORM_PS5 + pthread_get_name_np(thread_, result.data(), result.size() - 1); #else if (pthread_getname_np(thread_, result.data(), result.size() - 1) != 0) { assert_always(); @@ -693,7 +858,11 @@ class PosixCondition<Thread> : public PosixConditionBase { WaitStarted(); std::unique_lock<std::mutex> lock(state_mutex_); if (state_ != State::kUninitialized && state_ != State::kFinished) { +#if REX_PLATFORM_PS5 + pthread_set_name_np(thread_, std::string(name).c_str()); +#else pthread_setname_np(thread_, std::string(name).c_str()); +#endif #if REX_PLATFORM_ANDROID SetAndroidPreApi26Name(name); #endif @@ -711,12 +880,26 @@ class PosixCondition<Thread> : public PosixConditionBase { } #endif +#if REX_PLATFORM_PS5 + // [ps5 port] the id the thread recorded when it started + uint32_t system_id() const { return static_cast<uint32_t>(tid_.load(std::memory_order_acquire)); } +#else uint32_t system_id() const { return static_cast<uint32_t>(thread_); } +#endif #if REX_PLATFORM_ANDROID uint32_t native_tid() const { return static_cast<uint32_t>(pthread_gettid_np(thread_)); } #endif uint64_t affinity_mask() { +#if REX_PLATFORM_PS5 + // [ps5 port] no Linux-style affinity masks: report every logical processor. + WaitStarted(); + auto ps5_cpu_count = std::min<uint32_t>(logical_processor_count(), 64); + if (ps5_cpu_count == 64) { + return std::numeric_limits<uint64_t>::max(); + } + return (uint64_t(1) << ps5_cpu_count) - 1; +#else WaitStarted(); cpu_set_t cpu_set; #if REX_PLATFORM_ANDROID @@ -735,9 +918,13 @@ class PosixCondition<Thread> : public PosixConditionBase { result |= set << i; } return result; +#endif } void set_affinity_mask(uint64_t mask) { +#if REX_PLATFORM_PS5 // [ps5 port] leave scheduling to the PS5 + (void)mask; +#else WaitStarted(); cpu_set_t cpu_set; CPU_ZERO(&cpu_set); @@ -754,6 +941,7 @@ class PosixCondition<Thread> : public PosixConditionBase { if (pthread_setaffinity_np(thread_, sizeof(cpu_set_t), &cpu_set) != 0) { assert_always(); } +#endif #endif } @@ -771,6 +959,14 @@ class PosixCondition<Thread> : public PosixConditionBase { void set_priority(int new_priority) { WaitStarted(); +#if REX_PLATFORM_PS5 + // [ps5 port] Only the guest's creation flags get here (other priorities + // are ignored, ignore_thread_priorities). SCHED_FIFO with Linux's 0/1 + // means nothing sensible on the PS5, whose priorities run 256 (highest) + // to 767: leave every thread at the default. + (void)new_priority; + return; +#endif #if REX_PLATFORM_ANDROID // Apps may not use SCHED_FIFO; only creation-flag requests land here. __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "thread-priority-flag tid=%d value=%d", @@ -817,6 +1013,9 @@ class PosixCondition<Thread> : public PosixConditionBase { #if REX_PLATFORM_ANDROID int result = sigqueue(pthread_gettid_np(thread_), GetSystemSignal(SignalType::kThreadUserCallback), value); +#elif REX_PLATFORM_PS5 // [ps5 port] no pthread_sigqueue + (void)value; + int result = pthread_kill(thread_, GetSystemSignal(SignalType::kThreadUserCallback)); #else int result = pthread_sigqueue(thread_, GetSystemSignal(SignalType::kThreadUserCallback), value); #endif @@ -920,6 +1119,9 @@ class PosixCondition<Thread> : public PosixConditionBase { NotifyExternalWaiters(); } if (is_current_thread) { +#if REX_PLATFORM_PS5 + rex::ps5diag::UnregisterThread(); +#endif pthread_exit(reinterpret_cast<void*>(exit_code)); } else { #if REX_PLATFORM_ANDROID @@ -949,16 +1151,29 @@ class PosixCondition<Thread> : public PosixConditionBase { void* native_handle() const override { return reinterpret_cast<void*>(thread_); } + // The kernel's id for the thread, recorded by the thread itself. + std::atomic<pid_t> tid_{0}; + private: static void* ThreadStartRoutine(void* parameter); inline bool signaled() const override { return signaled_; } inline void post_execution() override { if (thread_) { +#if REX_PLATFORM_PS5 + if (pthread_join(thread_, nullptr) == 0) { + Ps5StackFree(ps5_stack_); // (the thread is gone: its stack can serve another) + ps5_stack_ = {}; + } +#else pthread_join(thread_, nullptr); +#endif } sem_destroy(&suspend_sem_); } pthread_t thread_; +#if REX_PLATFORM_PS5 + Ps5Stack ps5_stack_; +#endif bool signaled_; int exit_code_; State state_; // Protected by state_mutex_ @@ -1056,6 +1271,7 @@ class PosixConditionHandle : public T, public PosixWaitHandle { PosixCondition<T>& condition() override { return handle_; } void* native_handle() const override { return handle_.native_handle(); } + void* platform_wait_handle() override { return static_cast<PosixWaitHandle*>(this); } protected: PosixCondition<T> handle_; @@ -1080,8 +1296,18 @@ PosixConditionHandle<Event>::PosixConditionHandle(bool manual_reset, bool initia template <> PosixConditionHandle<Thread>::PosixConditionHandle(pthread_t thread) : handle_(thread) {} +namespace { +// (the handle's PosixWaitHandle, as dynamic_cast<PosixWaitHandle*> found it) +PosixWaitHandle* AsPosixWaitHandle(WaitHandle* handle) { + return handle ? static_cast<PosixWaitHandle*>(handle->platform_wait_handle()) : nullptr; +} +} // namespace + WaitResult Wait(WaitHandle* wait_handle, bool is_alertable, std::chrono::milliseconds timeout) { - auto posix_wait_handle = dynamic_cast<PosixWaitHandle*>(wait_handle); +#if REX_PLATFORM_PS5 + const rex::frame_stats::TitleWaitScope title_wait; +#endif + auto posix_wait_handle = AsPosixWaitHandle(wait_handle); if (posix_wait_handle == nullptr) { return WaitResult::kFailed; } @@ -1108,9 +1334,12 @@ WaitResult Wait(WaitHandle* wait_handle, bool is_alertable, std::chrono::millise WaitResult SignalAndWait(WaitHandle* wait_handle_to_signal, WaitHandle* wait_handle_to_wait_on, bool is_alertable, std::chrono::milliseconds timeout) { +#if REX_PLATFORM_PS5 + const rex::frame_stats::TitleWaitScope title_wait; +#endif auto result = WaitResult::kFailed; - auto posix_wait_handle_to_signal = dynamic_cast<PosixWaitHandle*>(wait_handle_to_signal); - auto posix_wait_handle_to_wait_on = dynamic_cast<PosixWaitHandle*>(wait_handle_to_wait_on); + auto posix_wait_handle_to_signal = AsPosixWaitHandle(wait_handle_to_signal); + auto posix_wait_handle_to_wait_on = AsPosixWaitHandle(wait_handle_to_wait_on); if (posix_wait_handle_to_signal == nullptr || posix_wait_handle_to_wait_on == nullptr) { return WaitResult::kFailed; } @@ -1141,10 +1370,13 @@ WaitResult SignalAndWait(WaitHandle* wait_handle_to_signal, WaitHandle* wait_han std::pair<WaitResult, size_t> WaitMultiple(WaitHandle* wait_handles[], size_t wait_handle_count, bool wait_all, bool is_alertable, std::chrono::milliseconds timeout) { +#if REX_PLATFORM_PS5 + const rex::frame_stats::TitleWaitScope title_wait; +#endif std::vector<PosixConditionBase*> conditions; conditions.reserve(wait_handle_count); for (size_t i = 0u; i < wait_handle_count; ++i) { - auto handle = dynamic_cast<PosixWaitHandle*>(wait_handles[i]); + auto handle = AsPosixWaitHandle(wait_handles[i]); if (handle == nullptr) { return std::make_pair(WaitResult::kFailed, 0); } @@ -1346,6 +1578,10 @@ void* PosixCondition<Thread>::ThreadStartRoutine(void* parameter) { current_thread_ = thread; current_thread_condition_ = &thread->handle_; + thread->handle_.tid_.store(rex_system_tid(), std::memory_order_release); +#if REX_PLATFORM_PS5 + rex::ps5diag::RegisterThread("rex"); +#endif { std::unique_lock<std::mutex> lock(thread->handle_.state_mutex_); thread->handle_.suspend_count_ = create_suspended ? 1 : 0; @@ -1358,6 +1594,9 @@ void* PosixCondition<Thread>::ThreadStartRoutine(void* parameter) { } start_routine(); +#if REX_PLATFORM_PS5 + rex::ps5diag::UnregisterThread(); +#endif { std::unique_lock<std::mutex> lock(thread->handle_.state_mutex_); @@ -1430,7 +1669,11 @@ void Thread::Exit(int exit_code) { } void set_current_thread_name(const std::string_view name) { +#if REX_PLATFORM_PS5 + pthread_set_name_np(pthread_self(), std::string(name).c_str()); +#else pthread_setname_np(pthread_self(), std::string(name).c_str()); +#endif #if REX_PLATFORM_ANDROID if (!android_pthread_getname_np_ && current_thread_) { current_thread_->condition().SetAndroidPreApi26Name(name); diff --git a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_device.cpp b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_device.cpp index 46220c70..90bc0050 100644 --- a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_device.cpp +++ b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_device.cpp @@ -12,7 +12,15 @@ #include <rex/filesystem/devices/disc_image_device.h> #include <rex/filesystem/devices/disc_image_entry.h> +#include <cerrno> +#include <cstring> #include <limits> +#include <vector> + +#include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/filesystem/direct_io.h> +#endif #include <rex/literals.h> #include <rex/logging.h> @@ -53,22 +61,42 @@ DiscImageDevice::DiscImageDevice(const std::string_view mount_path, DiscImageDevice::~DiscImageDevice() = default; bool DiscImageDevice::Initialize() { +#if REX_PLATFORM_PS5 + // [ps5 port] The image is read with pread, not mapped (paging an 8 GB disc + // image in through a file mapping froze the whole console), and past the + // file cache (rex/filesystem/direct_io.h). + reader_ = std::make_unique<direct::Reader>(); + if (!reader_->Open(host_path_.c_str())) { + REXFS_ERROR("Disc image could not be opened"); + return false; + } + host_size_ = size_t(reader_->size()); + REXFS_INFO("Disc image {}: {} bytes, read {}", host_path_.string(), host_size_, + reader_->direct() ? "past the cache" : "through the cache"); +#else mmap_ = memory::MappedMemory::Open(host_path_, memory::MappedMemory::Mode::kRead); if (!mmap_) { REXFS_ERROR("Disc image could not be mapped"); return false; } + host_size_ = mmap_->size(); +#endif ParseState state = {}; - state.ptr = mmap_->data(); - state.size = mmap_->size(); + state.ptr = nullptr; + state.size = host_size_; auto result = Verify(&state); if (result != Error::kSuccess) { REXFS_ERROR("Failed to verify disc image header: {}", static_cast<int>(result)); return false; } - result = ReadAllEntries(&state, state.ptr + state.root_offset); + std::vector<uint8_t> root(state.root_size); + if (!ReadAt(state.root_offset, root.data(), root.size())) { + REXFS_ERROR("Failed to read the GDFX root directory"); + return false; + } + result = ReadAllEntries(&state, root.data()); if (result != Error::kSuccess) { REXFS_ERROR("Failed to read all GDFX entries: {}", static_cast<int>(result)); return false; @@ -85,6 +113,18 @@ void DiscImageDevice::Dump(string::StringBuffer* string_buffer) { disc_info_.root_size, disc_info_.host_size); } +bool DiscImageDevice::ReadAt(size_t offset, void* out, size_t length) const { + if (!RangeWithin(offset, length, host_size_)) { + return false; + } +#if REX_PLATFORM_PS5 + return reader_->ReadAt(offset, out, length); +#else + std::memcpy(out, mmap_->data() + offset, length); + return true; +#endif +} + Entry* DiscImageDevice::ResolvePath(const std::string_view path) { // The filesystem will have stripped our prefix off already, so the path will // be in the form: @@ -119,7 +159,11 @@ DiscImageDevice::Error DiscImageDevice::Verify(ParseState* state) { !RangeWithin(descriptor_offset, kVolumeDescriptorSize, state->size)) { return Error::kErrorReadError; } - uint8_t* fs_ptr = state->ptr + descriptor_offset; + uint8_t descriptor[kVolumeDescriptorSize]; + if (!ReadAt(descriptor_offset, descriptor, sizeof(descriptor))) { + return Error::kErrorReadError; + } + const uint8_t* fs_ptr = descriptor; state->root_sector = memory::load<uint32_t>(fs_ptr + 20); state->root_size = memory::load<uint32_t>(fs_ptr + 24); if (state->root_size < kMinimumRootDirectorySize || @@ -143,7 +187,9 @@ bool DiscImageDevice::VerifyMagic(ParseState* state, size_t offset) { } // Simple check to see if the given offset contains the magic value. - return std::memcmp(state->ptr + offset, "MICROSOFT*XBOX*MEDIA", kMagicSize) == 0; + uint8_t magic[kMagicSize]; + return ReadAt(offset, magic, kMagicSize) && + std::memcmp(magic, "MICROSOFT*XBOX*MEDIA", kMagicSize) == 0; } DiscImageDevice::Error DiscImageDevice::ReadAllEntries(ParseState* state, @@ -222,10 +268,13 @@ bool DiscImageDevice::ReadEntry(ParseState* state, const uint8_t* buffer, size_t !RangeWithin(folder_offset, length, state->size)) { return false; } - uint8_t* folder_ptr = state->ptr + folder_offset; + std::vector<uint8_t> folder(length); + if (!ReadAt(folder_offset, folder.data(), length)) { + return false; + } std::unordered_set<uint16_t> child_active_ordinals; std::unordered_set<uint16_t> child_visited_ordinals; - if (!ReadEntry(state, folder_ptr, length, 0, entry.get(), child_active_ordinals, + if (!ReadEntry(state, folder.data(), length, 0, entry.get(), child_active_ordinals, child_visited_ordinals, depth + 1)) { return false; } diff --git a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_entry.cpp b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_entry.cpp index 1986c71b..c46aa47e 100644 --- a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_entry.cpp +++ b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_entry.cpp @@ -13,6 +13,9 @@ #include <rex/filesystem/devices/disc_image_file.h> #include <algorithm> +#include <vector> + +#include <rex/filesystem/devices/disc_image_device.h> #include <rex/math.h> @@ -46,7 +49,29 @@ std::unique_ptr<memory::MappedMemory> DiscImageEntry::OpenMapped(memory::MappedM size_t real_offset = data_offset_ + offset; size_t real_length = length ? std::min(length, data_size_) : data_size_; - return mmap_->Slice(real_offset, real_length); + if (mmap_) { + return mmap_->Slice(real_offset, real_length); + } + // [ps5 port] No mapping of the image: the bytes, read into memory. + class Copy final : public memory::MappedMemory { + public: + explicit Copy(size_t n) : bytes_(n) { + data_ = bytes_.data(); + size_ = n; + } + + private: + std::vector<uint8_t> bytes_; + }; + if (offset > data_size_) { + return nullptr; + } + real_length = std::min(real_length, data_size_ - offset); + auto copy = std::make_unique<Copy>(real_length); + if (!static_cast<DiscImageDevice*>(device())->ReadAt(real_offset, copy->data(), real_length)) { + return nullptr; + } + return copy; } } // namespace rex::filesystem diff --git a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_file.cpp b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_file.cpp index 313c9af4..2a10037d 100644 --- a/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_file.cpp +++ b/glue/rexglue-sdk-main/src/filesystem/devices/disc_image_file.cpp @@ -9,6 +9,7 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <rex/filesystem/devices/disc_image_device.h> #include <rex/filesystem/devices/disc_image_entry.h> #include <rex/filesystem/devices/disc_image_file.h> @@ -32,7 +33,11 @@ X_STATUS DiscImageFile::ReadSync(std::span<uint8_t> buffer, size_t byte_offset, } size_t real_offset = entry_->data_offset() + byte_offset; size_t real_length = std::min(buffer.size(), entry_->data_size() - byte_offset); - std::memcpy(buffer.data(), entry_->mmap()->data() + real_offset, real_length); + auto* device = static_cast<DiscImageDevice*>(entry_->device()); + if (!device->ReadAt(real_offset, buffer.data(), real_length)) { + *out_bytes_read = 0; + return X_STATUS_UNSUCCESSFUL; + } *out_bytes_read = real_length; return X_STATUS_SUCCESS; } diff --git a/glue/rexglue-sdk-main/src/graphics/CMakeLists.txt b/glue/rexglue-sdk-main/src/graphics/CMakeLists.txt index af36910e..e307c59b 100644 --- a/glue/rexglue-sdk-main/src/graphics/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/graphics/CMakeLists.txt @@ -145,12 +145,16 @@ function(rexgpu_configure_plugin target export_name) endif() endfunction() +# [ps5 port] one static program on the PS5: only the gta4-native renderer, +# linked in (gpu_plugin_loader.cpp calls it directly). +if(NOT PS5) add_library(rexgpu-xenos SHARED plugin_main.cpp ${REXGPU_XENOS_SOURCES} ) add_library(rex::gpu-xenos ALIAS rexgpu-xenos) rexgpu_configure_plugin(rexgpu-xenos gpu-xenos) +endif() if(REXGLUE_USE_VULKAN) if(REXGLUE_BUILD_TESTS) @@ -288,7 +292,12 @@ if(REXGLUE_USE_VULKAN) "${CMAKE_CURRENT_BINARY_DIR}/gta4_native_zstd") endif() - add_library(rexgpu-gta4-native SHARED + if(PS5) + set(_gta4_native_library_type STATIC) + else() + set(_gta4_native_library_type SHARED) + endif() + add_library(rexgpu-gta4-native ${_gta4_native_library_type} gta4_native/graphics_system.cpp gta4_native/core/font_atlas.cpp gta4_native/shader_override_policy.h @@ -335,6 +344,11 @@ if(REXGLUE_USE_VULKAN) if(TARGET rexglue_fidelityfx_vulkan) target_link_libraries(rexgpu-gta4-native PRIVATE rexglue_fidelityfx_vulkan) endif() + # [ps5 port] Line tables only (no code change): the PS5 sampling + # profiler's addresses map to source lines in the renderer. + if(PS5) + target_compile_options(rexgpu-gta4-native PRIVATE -gline-tables-only) + endif() # ThinLTO inside the renderer plugin (recorder and render worker). if(ANDROID) option(LIBERTY_ANDROID_RENDERER_THINLTO "ThinLTO for the native renderer on Android" ON) diff --git a/glue/rexglue-sdk-main/src/graphics/command_processor.cpp b/glue/rexglue-sdk-main/src/graphics/command_processor.cpp index 80a8e5ae..837a4cf2 100644 --- a/glue/rexglue-sdk-main/src/graphics/command_processor.cpp +++ b/glue/rexglue-sdk-main/src/graphics/command_processor.cpp @@ -17,6 +17,7 @@ #include <fmt/format.h> +#include <rex/ps5_diag.h> #include <rex/cvar.h> #include <rex/dbg.h> #include <rex/perf/counter.h> @@ -328,13 +329,24 @@ void CommandProcessor::WorkerThreadMain() { assert_true(read_ptr_index_ != write_ptr_index); // Execute. Note that we handle wraparound transparently. + rex::ps5diag::Add(rex::ps5diag::kPrimaryBuffers); read_ptr_index_ = ExecutePrimaryBuffer(read_ptr_index_, write_ptr_index); // TODO(benvanik): use reader->Read_update_freq_ and only issue after moving // that many indices. if (read_ptr_writeback_ptr_) { + static std::atomic<int> logged{0}; + if (logged.fetch_add(1) < 3) { + REXGPU_INFO("ps5 diag: read pointer {} written back to physical {:08X}", read_ptr_index_, + read_ptr_writeback_ptr_); + } memory::store_and_swap<uint32_t>(memory_->TranslatePhysical(read_ptr_writeback_ptr_), read_ptr_index_); + } else { + static std::atomic<int> logged_none{0}; + if (logged_none.fetch_add(1) < 3) { + REXGPU_INFO("ps5 diag: read pointer {} (no write-back address set)", read_ptr_index_); + } } // FIXME: We're supposed to process the WAIT_UNTIL register at this point, @@ -417,6 +429,7 @@ void CommandProcessor::EnableReadPointerWriteBack(uint32_t ptr, uint32_t block_s } void CommandProcessor::UpdateWritePointer(uint32_t value) { + rex::ps5diag::Add(rex::ps5diag::kWritePointer); write_ptr_index_ = value; write_ptr_index_event_->Set(); } diff --git a/glue/rexglue-sdk-main/src/graphics/graphics_system.cpp b/glue/rexglue-sdk-main/src/graphics/graphics_system.cpp index 1d5402e9..1adeb3c1 100644 --- a/glue/rexglue-sdk-main/src/graphics/graphics_system.cpp +++ b/glue/rexglue-sdk-main/src/graphics/graphics_system.cpp @@ -9,6 +9,9 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <chrono> +#include <thread> +#include <rex/ps5_diag.h> #include <rex/graphics/graphics_system.h> #include <algorithm> @@ -371,6 +374,7 @@ void GraphicsSystem::DispatchInterruptCallback(uint32_t source, uint32_t cpu) { if (!interrupt_callback_) { return; } + rex::ps5diag::Add(rex::ps5diag::kInterrupts); auto thread = system::XThread::GetCurrentThread(); assert_not_null(thread); @@ -393,6 +397,7 @@ void GraphicsSystem::MarkVblank() { // TODO: Enable profiling once ported // SCOPE_profile_cpu_f("gpu"); + rex::ps5diag::Add(rex::ps5diag::kVblanks); // Increment vblank counter (so the game sees us making progress). if (command_processor_) { command_processor_->increment_counter(); diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/bulb_appearance_trace.inc b/glue/rexglue-sdk-main/src/graphics/gta4_native/bulb_appearance_trace.inc index 8699adc2..e34c0293 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/bulb_appearance_trace.inc +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/bulb_appearance_trace.inc @@ -39,6 +39,7 @@ void Gta4NativeGraphicsSystem::CaptureBulbSource(NativeCommand& command, } } // Independent observer copies. Never replace the normal dirty-state deltas. + if (source) command.trace_flags |= NativeCommand::kTraceBulb; command.bulb_trace = std::move(source); } diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/cached_cvar.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/cached_cvar.h new file mode 100644 index 00000000..0a57b4ce --- /dev/null +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/cached_cvar.h @@ -0,0 +1,32 @@ +#pragma once +// [ps5 port] A by-name setting read at most every 250 ms per thread and call +// site. rex::cvar::GetFlagByName and Query lock the registry every other +// thread shares and copy strings: called while frames are built, they cost +// the recorder up to a quarter of its time in 1.0.2 players' profiles. The +// settings read here change only from the hub (applied at the next start) or +// the frontend, where a quarter second late is fine. +#include <chrono> +#include <string> +#include <string_view> +#include <unordered_map> +#include <utility> + +#include <rex/cvar.h> + +namespace rex::graphics::gta4_native { + +template <typename T> +T CachedQuery(const char* name) { + thread_local std::unordered_map<const char*, std::pair<std::chrono::steady_clock::time_point, T>> cache; + const auto now = std::chrono::steady_clock::now(); + auto it = cache.find(name); + if (it == cache.end() || now - it->second.first > std::chrono::milliseconds(250)) { + T value = rex::cvar::Query<T>(name); + it = cache.insert_or_assign(name, std::make_pair(now, std::move(value))).first; + } + return it->second.second; +} + +inline std::string CachedFlagByName(const char* name) { return CachedQuery<std::string>(name); } + +} // namespace rex::graphics::gta4_native diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/capture_thread.inc b/glue/rexglue-sdk-main/src/graphics/gta4_native/capture_thread.inc new file mode 100644 index 00000000..1b508d80 --- /dev/null +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/capture_thread.inc @@ -0,0 +1,418 @@ +// [ps5 port] Capture on its own core (included by graphics_system.cpp). +// +// The game's render thread used to run the whole capture of every command it +// submitted: about 7 ms of each frame in busy streets, on the thread that +// limits the frame rate there. Now it only copies the command into a ring, +// with what the capture will read of the guest device: the 64-byte lines of +// the device's state that changed since it last sent them (texture fetches, +// render and fixed state, the bound resources), and the shader constant +// groups the draw's own dirty masks name (the same ones the capture copies). +// The capture thread keeps a copy of each device up to date from those lines +// and runs the same capture as before, reading that copy instead of the +// device. Guest objects (buffers, textures, surfaces) are still read where +// they are: the game reuses one only after the frame it was drawn in is +// complete, which happens after the capture (gta4_native_frame_complete_signal). +// +// Commands that need an answer now (texture locks) and the trace envelopes +// wait for everything already in the ring to be captured, then run here. + +namespace { + +struct CaptureRecordHeader { + uint32_t record_size = 0; // never NativeCaptureRing::kWrapMark + uint32_t command_size = 0; + uint32_t device = 0; // the device the lines belong to (0: none) + uint16_t line_count = 0; + uint16_t flags = 0; + uint32_t up_size = 0; // DrawPrimitiveUp vertex bytes after the lines + uint32_t object_count = 0; // guest objects (kCaptureObjectEntry each) after the payload + uint32_t reserved[2] = {}; +}; +static_assert(sizeof(CaptureRecordHeader) == 32); +constexpr uint16_t kCaptureRecordPresent = 1; + +constexpr uint32_t kCaptureLineBytes = 64; +constexpr uint32_t kCaptureDeviceLines = kGuestDeviceSize / kCaptureLineBytes; +static_assert(kGuestDeviceSize % kCaptureLineBytes == 0); +constexpr uint32_t kCaptureVertexConstantLine = kVertexConstantsOffset / kCaptureLineBytes; +constexpr uint32_t kCapturePixelConstantLine = kPixelConstantsOffset / kCaptureLineBytes; +constexpr uint32_t kCaptureConstantLines = (kVertexConstantsSize + kPixelConstantsSize) / kCaptureLineBytes; +static_assert(kVertexConstantsOffset % kCaptureLineBytes == 0 && kPixelConstantsOffset % kCaptureLineBytes == 0); +static_assert(kNativeConstantGroupBytes == kCaptureLineBytes); +static_assert(kVertexConstantsOffset + kVertexConstantsSize == kPixelConstantsOffset); + +// The lines compared on every draw: what the capture reads outside the +// constants (texture fetches; the boolean constants; clip planes through the +// viewport, with the stencil, blend and depth words, the bound surfaces, +// streams, index buffer and texture handles in between). +struct CaptureLineSpan { + uint32_t first, end; // device offsets, line aligned +}; +constexpr std::array<CaptureLineSpan, 3> kCaptureComparedSpans{{ + {0x480, 0x700}, // texture fetch constants (0x480 + 26 * 0x18 = 0x6F0) + {0x2780, 0x27C0}, // vertex and pixel boolean constants (0x2780, 0x2790) + {0x2800, 0x3180}, // clip planes (0x2820) ... viewport (to 12664) +}}; +constexpr bool CaptureLineCompared(uint32_t offset, uint32_t bytes = 4) { + for (const auto& span : kCaptureComparedSpans) + if (offset >= span.first && offset + bytes <= span.end) return true; + return false; +} +static_assert(CaptureLineCompared(kTextureFetchBase, kTextureStageCount * kTextureFetchSize)); +static_assert(CaptureLineCompared(0x2780) && CaptureLineCompared(0x2790)); +static_assert(CaptureLineCompared(kClipPlane0Offset, 16) && CaptureLineCompared(kPackedWindowOffset) && + CaptureLineCompared(kPackedScissorTopLeftOffset) && CaptureLineCompared(kPackedScissorBottomRightOffset) && + CaptureLineCompared(kPackedColorWriteMaskOffset) && CaptureLineCompared(kBlendConstantsOffset, 16) && + CaptureLineCompared(kAlphaReferenceOffset) && CaptureLineCompared(kDepthStencilPackedOffset) && + CaptureLineCompared(kAlphaTestFunctionPackedOffset) && CaptureLineCompared(kClipControlPackedOffset) && + CaptureLineCompared(kCullModePackedOffset) && CaptureLineCompared(kSampleMaskOffset) && + CaptureLineCompared(kDepthBiasOffset) && CaptureLineCompared(kSlopeScaledDepthBiasOffset) && + CaptureLineCompared(kBackDepthBiasOffset) && CaptureLineCompared(kBackSlopeScaledDepthBiasOffset) && + CaptureLineCompared(kBlendControlOffset) && CaptureLineCompared(kScissorEnableOffset) && + CaptureLineCompared(kDepthEnableOffset) && CaptureLineCompared(kStencilEnableOffset) && + CaptureLineCompared(kStencilReferenceOffset, 1) && CaptureLineCompared(kStencilMaskOffset, 1) && + CaptureLineCompared(kStencilWriteMaskOffset, 1) && CaptureLineCompared(kBackStencilReferenceOffset, 1) && + CaptureLineCompared(kBackStencilMaskOffset, 1) && CaptureLineCompared(kBackStencilWriteMaskOffset, 1)); +static_assert(CaptureLineCompared(kIndexBufferOffset) && CaptureLineCompared(kRenderTargetOffset, 16) && + CaptureLineCompared(kDepthStencilOffset) && + CaptureLineCompared(kStreamBufferBase, kVertexStreamCount * 4) && + CaptureLineCompared(kTextureHandleBase, kTextureStageCount * 4) && + CaptureLineCompared(kViewportXOffset, 24)); +static_assert([] { + for (uint32_t offset : kHardwareBlendPackedOffsets) + if (!CaptureLineCompared(offset)) return false; + return true; +}()); + +inline bool CaptureLinesEqual(const uint8_t* a, const uint8_t* b) { +#if defined(__AVX2__) + const __m256i a0 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a)); + const __m256i a1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a + 32)); + const __m256i b0 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(b)); + const __m256i b1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(b + 32)); + const __m256i same = _mm256_and_si256(_mm256_cmpeq_epi8(a0, b0), _mm256_cmpeq_epi8(a1, b1)); + return _mm256_movemask_epi8(same) == -1; +#else + return std::memcmp(a, b, kCaptureLineBytes) == 0; +#endif +} + +// Set on the capture thread: its waits for the renderer are not the game's. +thread_local bool t_capture_thread = false; +// Bigger DrawPrimitiveUp payloads (up to 4 MB) are captured here, after the ring. +constexpr uint32_t kCaptureRingUpLimit = 256u << 10; + +} // namespace + +Gta4NativeGraphicsSystem::CaptureDeviceCopy& Gta4NativeGraphicsSystem::CaptureMirror(uint32_t device) { + if (capture_last_mirror_ && capture_last_mirror_->device == device) return *capture_last_mirror_; + for (auto& mirror : capture_mirrors_) + if (mirror->device == device) return *(capture_last_mirror_ = mirror.get()); + auto& added = capture_mirrors_.emplace_back(std::make_unique<CaptureDeviceCopy>()); + added->device = device; + return *(capture_last_mirror_ = added.get()); +} + +Gta4NativeGraphicsSystem::CaptureDeviceCopy& Gta4NativeGraphicsSystem::CaptureShadow(uint32_t device) { + if (capture_last_shadow_ && capture_last_shadow_->device == device) return *capture_last_shadow_; + for (auto& shadow : capture_shadows_) + if (shadow->device == device) return *(capture_last_shadow_ = shadow.get()); + auto& added = capture_shadows_.emplace_back(std::make_unique<CaptureDeviceCopy>()); + added->device = device; + return *(capture_last_shadow_ = added.get()); +} + +bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t abi_version, + const void* command, size_t command_size) { + if (title_id != kTitleId || !command || !memory_) return false; + if (abi_version == kTitleCommandAbi && + (capture_on_core_flag_.load(std::memory_order_relaxed) || + rex::frame_stats::g_capture_on_core.load(std::memory_order_relaxed))) { + const int route = SubmitTitleCommandOnCore(command, command_size); + if (route >= 0) return route != 0; + } else if (capture_on_core_flag_.load(std::memory_order_relaxed)) { + // An envelope (traces, pass markers): after everything in the ring, here. + std::lock_guard push_lock(capture_push_mutex_); + if (capture_on_core_) { + capture_ring_->WaitProcessed(capture_ring_->head()); + // (it may take a draw's dirty state with it: every device sent afresh) + for (auto& mirror : capture_mirrors_) mirror->primed = false; + } + return SubmitTitleCommandHere(title_id, abi_version, command, command_size); + } + return SubmitTitleCommandHere(title_id, abi_version, command, command_size); +} + +void Gta4NativeGraphicsSystem::FinishCaptureOnCore() { + // (capture_push_mutex_ held) + if (capture_on_core_ && capture_ring_) capture_ring_->WaitProcessed(capture_ring_->head()); +} + +// 1: queued, 0: rejected, -1: not for the ring (captured here by the caller, +// with nothing in the ring left before it). +int Gta4NativeGraphicsSystem::SubmitTitleCommandOnCore(const void* command, size_t command_size) { + static_assert(kResourceSizeOffset + 4 <= kCaptureObjectBytes && + 40 + 4 <= kCaptureObjectBytes); // (the object words the capture reads) + if (command_size < sizeof(CommandHeader)) return 0; + CommandHeader header; + std::memcpy(&header, command, sizeof(header)); + const size_t expected_size = CommandSize(header.type); + if (!expected_size || command_size != expected_size || header.size != expected_size) { + // The capture's own rejection (and its log line), in order. + std::lock_guard push_lock(capture_push_mutex_); + FinishCaptureOnCore(); + return SubmitTitleCommandHere(kTitleId, kTitleCommandAbi, command, command_size) ? 1 : 0; + } + const CommandType type = header.type; + const bool draw = type == CommandType::kDrawPrimitive || type == CommandType::kDrawPrimitiveUp || + type == CommandType::kDrawIndexedPrimitive; + const bool device_state = draw || type == CommandType::kClear; + const uint32_t device = device_state ? CommandDevice(type, command) : 0; + const uint8_t* device_memory = device ? memory_->TranslateVirtual<const uint8_t*>(device) : nullptr; + const uint8_t* up_data = nullptr; + uint32_t up_size = 0; + if (type == CommandType::kDrawPrimitiveUp) { + const auto& up = *static_cast<const DrawPrimitiveUpCommand*>(command); + up_size = up.vertex_data_size; + if (up_size) up_data = up.vertex_data ? memory_->TranslateVirtual<const uint8_t*>(up.vertex_data) : nullptr; + } + // The profiler's transport timing, a device or payload that can't be read, + // a payload too big for the ring, or diagnostics that read the whole device + // (full snapshots, the bulb trace): here, after the ring. + const bool here = + (device_state && !device_memory) || (up_size && (!up_data || up_size > kCaptureRingUpLimit)) || + (CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeProfiler) && + (g_native_profile_transport_active.load(std::memory_order_relaxed) || + g_native_profile_capture_requested.load(std::memory_order_relaxed))) || + (device_state && + (CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) || + CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeProbes) || + CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeTranslucency) || + CachedDiagnosticsEnabled(rex::diagnostics::Category::kTransition) || + REXCVAR_GET(gta4_validate_native_dirty_snapshots) || EmissionTraceConfig().pipeline || + std::string_view(REXCVAR_GET(gta4_native_state_transport)) == "snapshot" || + std::string_view(REXCVAR_GET(gta4_native_state_transport)) == "compare")); + + std::lock_guard push_lock(capture_push_mutex_); + if (capture_stopped_) return 0; // shut down + const bool wanted = rex::frame_stats::g_capture_on_core.load(std::memory_order_relaxed); + if (!capture_on_core_) { + if (!wanted) return -1; + // Switching on: the thread (once), and every device's lines sent afresh. + if (!capture_ring_) { + auto ring = std::make_unique<NativeCaptureRing>(); + if (!ring->valid()) { + rex::frame_stats::g_capture_on_core.store(false, std::memory_order_relaxed); + REXLOG_ERROR("ps5 capture: no memory for the capture ring; capturing on the game's thread"); + return -1; + } + capture_ring_ = std::move(ring); + capture_ring_published_.store(capture_ring_.get(), std::memory_order_release); + } + if (!capture_thread_running_.exchange(true)) { + capture_thread_ = std::thread([this]() { CaptureThreadMain(); }); + } + for (auto& mirror : capture_mirrors_) mirror->primed = false; + capture_on_core_ = true; + capture_on_core_flag_.store(true, std::memory_order_relaxed); + } + const bool present = type == CommandType::kPresent; + if (here) { + FinishCaptureOnCore(); + // Captured here, it takes the draw's dirty constants (or starts the + // device afresh): that device's lines go out in full next time. + if (device_state && device) CaptureMirror(device).primed = false; + if (type == CommandType::kDeviceCreated || type == CommandType::kDeviceDestroyed) + CaptureMirror(CommandDevice(type, command)).primed = false; + const bool accepted = SubmitTitleCommandHere(kTitleId, kTitleCommandAbi, command, command_size); + if (present && !wanted) { + capture_on_core_ = false; + capture_on_core_flag_.store(false, std::memory_order_relaxed); + } + return accepted ? 1 : 0; + } + + // The lines to send. + std::array<uint16_t, kCaptureDeviceLines> lines; + uint32_t line_count = 0; + CaptureDeviceCopy* mirror = nullptr; + if (device_state) { + mirror = &CaptureMirror(device); + if (!mirror->primed) { + // A new copy: all of it, once. + for (uint32_t line = 0; line < kCaptureDeviceLines; ++line) lines[line_count++] = uint16_t(line); + std::memcpy(mirror->bytes.data(), device_memory, kGuestDeviceSize); + mirror->primed = true; + } else { + for (const auto& span : kCaptureComparedSpans) { + for (uint32_t offset = span.first; offset < span.end; offset += kCaptureLineBytes) { + uint8_t* kept = mirror->bytes.data() + offset; + if (CaptureLinesEqual(kept, device_memory + offset)) continue; + std::memcpy(kept, device_memory + offset, kCaptureLineBytes); + lines[line_count++] = uint16_t(offset / kCaptureLineBytes); + } + } + const NativeDirtyState* dirty = CommandDirtyState(type, command); + const bool full_constants = + draw && RequiresFullLightingConstants( + type == CommandType::kDrawPrimitive + ? static_cast<const DrawPrimitiveCommand*>(command)->lighting + : type == CommandType::kDrawPrimitiveUp + ? static_cast<const DrawPrimitiveUpCommand*>(command)->lighting + : static_cast<const DrawIndexedPrimitiveCommand*>(command)->lighting); + if (full_constants) { + for (uint32_t line = 0; line < kCaptureConstantLines; ++line) + lines[line_count++] = uint16_t(kCaptureVertexConstantLine + line); + } else if (dirty) { + // Xenos masks: group g is bit 63 - g (vertex: all 64; pixel: 56, bits 63..8). + for (uint64_t bits = dirty->words[0]; bits; bits &= bits - 1) { + const uint32_t group = 63u - uint32_t(std::countr_zero(bits)); + lines[line_count++] = uint16_t(kCaptureVertexConstantLine + group); + } + for (uint64_t bits = dirty->words[1] & ~uint64_t(0xFF); bits; bits &= bits - 1) { + const uint32_t group = 63u - uint32_t(std::countr_zero(bits)); + lines[line_count++] = uint16_t(kCapturePixelConstantLine + group); + } + } + } + } else if (type == CommandType::kDeviceCreated || type == CommandType::kDeviceDestroyed) { + // The capture starts that device's constants afresh: so do its lines. + CaptureMirror(CommandDevice(type, command)).primed = false; + } + + // The bound surfaces and buffers, as their objects are now. + std::array<uint32_t, kRenderTargetCount + 1 + kVertexStreamCount + 1> object_handles; + uint32_t object_count = 0; + if (device_state) { + const auto add = [&](uint32_t offset) { + uint32_t handle; + std::memcpy(&handle, device_memory + offset, sizeof(handle)); + handle = __builtin_bswap32(handle); + if (!handle || !memory_->TranslateVirtual<const uint8_t*>(handle)) return; + for (uint32_t i = 0; i < object_count; ++i) + if (object_handles[i] == handle) return; + object_handles[object_count++] = handle; + }; + for (uint32_t index = 0; index < kRenderTargetCount; ++index) + add(kRenderTargetOffset + index * sizeof(uint32_t)); + add(kDepthStencilOffset); + if (type == CommandType::kDrawPrimitive || type == CommandType::kDrawIndexedPrimitive) { + for (uint32_t stream = 0; stream < kVertexStreamCount; ++stream) + add(kStreamBufferBase + stream * sizeof(uint32_t)); + if (type == CommandType::kDrawIndexedPrimitive) add(kIndexBufferOffset); + } + } + const size_t command_bytes = NativeCaptureRing::Align(command_size); + const size_t index_bytes = NativeCaptureRing::Align(line_count * sizeof(uint16_t)); + const size_t record_size = sizeof(CaptureRecordHeader) + command_bytes + index_bytes + + size_t(line_count) * kCaptureLineBytes + NativeCaptureRing::Align(up_size) + + size_t(object_count) * kCaptureObjectEntry; + uint64_t waited_us = 0; + if (present && capture_ring_->presents_queued()) { + // At most one frame in the ring: the game waits for the capture thread to + // finish the last one (as it waited for the renderer before). + const auto begin = std::chrono::steady_clock::now(); + capture_ring_->WaitFor([&] { return capture_ring_->presents_queued() == 0; }); + waited_us += uint64_t(std::chrono::duration_cast<std::chrono::microseconds>( + std::chrono::steady_clock::now() - begin).count()); + } + uint8_t* out = capture_ring_->Reserve(record_size, &waited_us); + CaptureRecordHeader record{}; + record.record_size = uint32_t(record_size); + record.command_size = uint32_t(command_size); + record.device = device_state ? device : 0; + record.line_count = uint16_t(line_count); + record.flags = present ? kCaptureRecordPresent : 0; + record.up_size = up_size; + record.object_count = object_count; + std::memcpy(out, &record, sizeof(record)); + uint8_t* cursor = out + sizeof(record); + std::memcpy(cursor, command, command_size); + cursor += command_bytes; + if (line_count) { + std::memcpy(cursor, lines.data(), line_count * sizeof(uint16_t)); + cursor += index_bytes; + // From the device itself: the compared lines were just copied from it. + for (uint32_t i = 0; i < line_count; ++i, cursor += kCaptureLineBytes) + std::memcpy(cursor, device_memory + size_t(lines[i]) * kCaptureLineBytes, kCaptureLineBytes); + } + if (up_size) std::memcpy(cursor, up_data, up_size); + cursor += NativeCaptureRing::Align(up_size); + for (uint32_t i = 0; i < object_count; ++i, cursor += kCaptureObjectEntry) { + std::memcpy(cursor, &object_handles[i], sizeof(uint32_t)); + std::memcpy(cursor + 4, memory_->TranslateVirtual<const uint8_t*>(object_handles[i]), kCaptureObjectBytes); + } + capture_ring_->Commit(record_size, present); + if (waited_us) { + rex::frame_stats::NoteTitleSlotWait(waited_us); + rex::frame_stats::g_capture_ring_wait_us.fetch_add(waited_us, std::memory_order_relaxed); + } + if (present && !wanted) { + // Switching off, at the end of a frame: once it's captured, back here. + FinishCaptureOnCore(); + capture_on_core_ = false; + capture_on_core_flag_.store(false, std::memory_order_relaxed); + } + return 1; +} + +void Gta4NativeGraphicsSystem::CaptureThreadMain() { + rex::ps5diag::RegisterThread("capture"); + t_capture_thread = true; + rex::frame_stats::FollowRendererCpuMask(capture_cpu_mask_applied_, rex::frame_stats::RendererRole::kCapture); + NativeCaptureRing& ring = *capture_ring_; + while (ring.WaitForRecords(capture_thread_running_, 300)) { + while (const uint8_t* at = ring.Peek()) { + CaptureRecordHeader record; + std::memcpy(&record, at, sizeof(record)); + const uint8_t* command = at + sizeof(record); + const uint8_t* cursor = command + NativeCaptureRing::Align(record.command_size); + const uint8_t* shadow_bytes = nullptr; + if (record.device) { + CaptureDeviceCopy& shadow = CaptureShadow(record.device); + if (record.line_count) { + uint16_t lines[kCaptureDeviceLines]; + std::memcpy(lines, cursor, record.line_count * sizeof(uint16_t)); + cursor += NativeCaptureRing::Align(record.line_count * sizeof(uint16_t)); + for (uint32_t i = 0; i < record.line_count; ++i, cursor += kCaptureLineBytes) + std::memcpy(shadow.bytes.data() + size_t(lines[i]) * kCaptureLineBytes, cursor, kCaptureLineBytes); + } + shadow_bytes = shadow.bytes.data(); + } + const uint8_t* up_payload = record.up_size ? cursor : nullptr; + cursor += NativeCaptureRing::Align(record.up_size); + SubmitTitleCommandHere(kTitleId, kTitleCommandAbi, command, record.command_size, record.device, + shadow_bytes, up_payload, record.object_count ? cursor : nullptr, + record.object_count); + const bool present = record.flags & kCaptureRecordPresent; + ring.Pop(record.record_size, present); + if (present) { + rex::frame_stats::NoteCpu(rex::frame_stats::g_cpus_capture); + rex::frame_stats::FollowRendererCpuMask(capture_cpu_mask_applied_, + rex::frame_stats::RendererRole::kCapture); + } + } + } +} + +// At shutdown, after the render worker has stopped (a capture waiting for room +// in its queue gives up then): whatever is still in the ring is dropped. +void Gta4NativeGraphicsSystem::StopCaptureThread() { + rex::frame_stats::g_capture_on_core.store(false, std::memory_order_relaxed); + rex::frame_stats::g_capture_core_compare.store(false, std::memory_order_relaxed); + // First every wait on the ring (a submitter may hold the lock in one), then + // the lock: from here on submissions are dropped and no thread starts. + NativeCaptureRing* ring = capture_ring_published_.load(std::memory_order_acquire); + if (ring) ring->Stop(); + std::thread thread; + { + std::lock_guard push_lock(capture_push_mutex_); + capture_stopped_ = true; + capture_on_core_ = false; + capture_on_core_flag_.store(false, std::memory_order_relaxed); + capture_thread_running_.store(false); + thread = std::move(capture_thread_); + } + if (ring) ring->WakeConsumer(); + if (thread.joinable()) thread.join(); +} diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/dirty_state_delta.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/dirty_state_delta.h index 64bedd7b..d751e320 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/dirty_state_delta.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/dirty_state_delta.h @@ -275,6 +275,33 @@ void ForEachDirtyElement(const NativeDirtyWords& dirty_words, const DirtyCompone inline void BuildRangeSet(const NativeDirtyWords& dirty_words, const DirtyComponentLayout& layout, DirtyRangeSet& result, DirtyDeltaScratch& scratch) { + // [ps5 port] One span (the constant layouts): read the ascending runs + // straight off the bits. Reversed spans came out in descending order and + // went through a sort, its worst case, for every draw on the game thread. + if (layout.spans.size() == 1) { + const DirtyBitSpan& span = layout.spans[0]; + uint64_t bits = + (dirty_words[span.dirty_word] >> span.first_dirty_bit) & LowBitMask(span.bit_count); + result.ranges.clear(); + const auto ones = [](uint32_t count) { + return count >= 64 ? ~uint64_t{0} : (uint64_t{1} << count) - 1; + }; + while (bits) { + if (!span.reverse_bits) { + const uint32_t bit = uint32_t(std::countr_zero(bits)); + const uint32_t run = uint32_t(std::countr_one(bits >> bit)); + result.ranges.push_back({span.first_element + bit, run}); + bits &= ~(ones(run) << bit); + } else { + // Element numbers grow as bits fall: start from the highest bit. + const uint32_t top = 63u - uint32_t(std::countl_zero(bits)); + const uint32_t run = uint32_t(std::countl_one(bits << (63u - top))); + result.ranges.push_back({span.first_element + (uint32_t(span.bit_count) - 1u - top), run}); + bits &= ~(ones(run) << (top + 1u - run)); + } + } + return; + } std::vector<uint32_t>& dirty_elements = scratch.dirty_elements; dirty_elements.clear(); ForEachDirtyElement(dirty_words, layout, @@ -298,6 +325,12 @@ inline void BuildRangeSet(const NativeDirtyWords& dirty_words, const DirtyCompon inline void BuildElementMask(const NativeDirtyWords& dirty_words, const DirtyComponentLayout& layout, DirtyElementMask& result) { + if (layout.spans.empty() && !layout.element_count) { + // An empty layout: the same empty mask, without the assign. + result.element_count = 0; + result.words.clear(); + return; + } result.element_count = layout.element_count; const uint32_t bits_per_word = std::numeric_limits<uint64_t>::digits; result.words.assign( @@ -346,15 +379,19 @@ inline DirtyLayoutValidationResult BuildDirtyStateDelta(const NativeDirtyWords& inline void BuildDirtyStateDeltaUnchecked(const NativeDirtyWords& dirty_words, const DirtyStateLayout& layout, DirtyStateDelta& delta, DirtyDeltaScratch& scratch) { - - dirty_state_delta_internal::BuildRangeSet(dirty_words, layout.vertex_constants, - delta.vertex_constant_ranges, scratch); - dirty_state_delta_internal::BuildRangeSet(dirty_words, layout.pixel_constants, - delta.pixel_constant_ranges, scratch); - dirty_state_delta_internal::BuildRangeSet(dirty_words, layout.texture_fetch_stages, - delta.texture_fetch_stage_ranges, scratch); - dirty_state_delta_internal::BuildRangeSet(dirty_words, layout.sampler_stages, - delta.sampler_stage_ranges, scratch); + // [ps5 port] A component without spans (the native layout's texture and + // sampler stages) has no ranges: no call, sort or element pass per draw. + const auto build_ranges = [&](const DirtyComponentLayout& component, DirtyRangeSet& result) { + if (component.spans.empty()) { + result.ranges.clear(); + return; + } + dirty_state_delta_internal::BuildRangeSet(dirty_words, component, result, scratch); + }; + build_ranges(layout.vertex_constants, delta.vertex_constant_ranges); + build_ranges(layout.pixel_constants, delta.pixel_constant_ranges); + build_ranges(layout.texture_fetch_stages, delta.texture_fetch_stage_ranges); + build_ranges(layout.sampler_stages, delta.sampler_stage_ranges); dirty_state_delta_internal::BuildElementMask(dirty_words, layout.boolean_constants, delta.boolean_constant_mask); dirty_state_delta_internal::BuildElementMask(dirty_words, layout.integer_constants, diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/frame_constant_arena.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/frame_constant_arena.h index 328cbdfa..4493e525 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/frame_constant_arena.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/frame_constant_arena.h @@ -71,6 +71,15 @@ class FrameGenerationMap { return bucket ? &bucket->value : nullptr; } + // [ps5 port] Fetches the first bucket (and its stamp) a lookup of key + // probes, for a lookup a little later. + void Prefetch(const Key& key) const { + if (buckets_.empty()) return; + const size_t index = hasher_(key) & (buckets_.size() - 1); + __builtin_prefetch(&stamps_[index]); + __builtin_prefetch(&buckets_[index]); + } + InsertResult Insert(const Key& key, const Value& value) { if (Value* existing = Find(key)) { return {existing, false}; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.cpp b/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.cpp index 581a9d20..c6f4d1c2 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.cpp +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.cpp @@ -1,5 +1,10 @@ +#include <rex/frame_stats.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif #include <rex/graphics/gta4_native/fusion_tone_lut.h> #include <rex/graphics/gta4_native/modern_effect_constants.h> +#include "cached_cvar.h" #include "core/font_atlas.h" #include "native_profile_labels.h" #include "graphics_system.h" @@ -35,6 +40,7 @@ #include <algorithm> #include <bit> #include <cmath> +#include <cstdarg> #include <cstdio> #include <cstring> #include <ctime> @@ -203,7 +209,9 @@ REXCVAR_DEFINE_BOOL(gta4_native_fast_legacy_mul, true, "GTA IV/Graphics/Native R "equivalent cheaper form (applies when shaders load)"); // Same image, ~2% less GPU time at the spawn point; the reordering runs on a // background thread (draws keep the title's order until it is ready). -REXCVAR_DEFINE_BOOL(gta4_native_vertex_cache_order, true, "GTA IV/Graphics/Native Renderer", +// [ps5 port] Off there: the GPU is never the limit, and the per-draw lookups, +// copies and queueing cost the recorder, which is. +REXCVAR_DEFINE_BOOL(gta4_native_vertex_cache_order, !REX_PLATFORM_PS5, "GTA IV/Graphics/Native Renderer", "Reorder the triangles of order-independent indexed triangle-list draws for " "the GPU vertex cache (once per index range, on a background thread)"); REXCVAR_DEFINE_BOOL(gta4_native_water_reflection_on_demand, true, "GTA IV/Graphics/Reflections", @@ -224,7 +232,10 @@ REXCVAR_DEFINE_BOOL(gta4_native_frame_timeline, false, "GTA IV/Diagnostics", "Log per-frame stage times (guest present, worker handoff, recorder start, " "queue submit, publish end) on CLOCK_MONOTONIC, to join with SurfaceFlinger's " "frame-ready times"); -REXCVAR_DEFINE_UINT32(gta4_native_shadow_min_indices, 0, "GTA IV/Graphics/Shadows", +// [ps5 port] PS5: 300 (Small Object Shadows off): in busy streets about a +// fifth of every frame's draws are these, and both the game's render thread +// and the recorder were over a 60 fps frame with them. +REXCVAR_DEFINE_UINT32(gta4_native_shadow_min_indices, REX_PLATFORM_PS5 ? 300 : 0, "GTA IV/Graphics/Shadows", "Skip indexed draws with fewer indices than this in the shadow map passes " "(0 = draw everything)"); REXCVAR_DEFINE_BOOL(gta4_native_water_occlusion_queries, false, "GTA IV/Graphics/Reflections", @@ -285,6 +296,40 @@ REXCVAR_DEFINE_BOOL(gta4_native_stamped_protection, true, "GTA IV/Graphics/Nativ REXCVAR_DEFINE_BOOL(gta4_native_recheck_vertex_textures, true, "GTA IV/Graphics/Native Renderer", "Re-read textures sampled by vertex shaders once a frame: the title updates " "them in place without an unlock (vehicle deformation)"); +REXCVAR_DEFINE_BOOL(gta4_native_texture_watch, true, "GTA IV/Graphics/Native Renderer", + "Re-read cached textures whose memory changed without an unlock or release " + "(textures streamed in where others were: the map's and radar's tiles, " + "clothes)"); +REXCVAR_DECLARE(bool, protect_on_release); +REXCVAR_DEFINE_BOOL(gta4_native_fg_motion_audit, !REX_PLATFORM_PS5, "GTA IV/Graphics/Native Renderer", + "Before each generated frame, check on the GPU that every pixel has motion, " + "depth and history (the recorder waits for the GPU mid-frame to read it)"); + +// [ps5 port] With frame generation on: the first draws in a session whose +// motion couldn't be followed (FSR then falls back to spatial and no frame is +// generated), with their shaders, so one test session names them. +static void NoteFgMotionLost(const char* why, const void* vertex_shader, const void* pixel_shader, + uint64_t vertex_hash, uint64_t pixel_hash) { + // (each shader pair and reason once, up to 48: one draw can hide the next) + static std::mutex mutex; + static std::vector<std::pair<uint64_t, const char*>> seen; + if (!rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) return; + const uint64_t key = (vertex_shader ? vertex_hash : 0) * 31 ^ (pixel_shader ? pixel_hash : 0); + { + std::lock_guard<std::mutex> lock(mutex); + if (seen.size() >= 48) return; + for (const auto& [k, w] : seen) + if (k == key && w == why) return; + seen.emplace_back(key, why); + } + REXLOG_INFO("ps5 frame generation: motion lost at a draw ({}): vertex shader {:016X}, pixel shader {:016X}", why, + vertex_shader ? vertex_hash : 0, pixel_shader ? pixel_hash : 0); +} +#define NOTE_FG_MOTION_LOST(why, state) \ + NoteFgMotionLost(why, (state) ? (state)->vertex_shader_resource : nullptr, \ + (state) ? (state)->pixel_shader_resource : nullptr, \ + (state) && (state)->vertex_shader_resource ? (state)->vertex_shader_resource->hash : 0, \ + (state) && (state)->pixel_shader_resource ? (state)->pixel_shader_resource->hash : 0) REXCVAR_DEFINE_BOOL(gta4_native_vertex_texture_trace, false, "GTA IV/Diagnostics", "Log textures sampled by vertex shaders and every change of their contents"); REXCVAR_DEFINE_BOOL(gta4_native_buffer_validation_per_frame, true, "GTA IV/Graphics/Native Renderer", @@ -324,7 +369,9 @@ REXCVAR_DEFINE_UINT32(gta4_native_debug_gpu_attribution, 0, "GTA IV/Graphics/Nat "Measurement only, breaks the image: 1 = scene draws get an empty scissor " "(vertex and state work only), 2 = scene draws bind state but are not issued") .range(0, 2); -REXCVAR_DEFINE_BOOL(gta4_native_merge_indexed_draws, true, "GTA IV/Graphics/Native Renderer", +// [ps5 port] Off on the PS5: no draw ever merged in hours of logs, and the +// look-ahead compared every indexed draw with the next on the recorder. +REXCVAR_DEFINE_BOOL(gta4_native_merge_indexed_draws, !REX_PLATFORM_PS5, "GTA IV/Graphics/Native Renderer", "Record consecutive indexed triangle-list draws with identical state and " "adjacent index ranges as one draw"); REXCVAR_DEFINE_DOUBLE(gta4_native_depth_bias_constant_scale, 1.0, "GTA IV/Graphics/Native Renderer", @@ -404,6 +451,57 @@ REXCVAR_DEFINE_BOOL(gta4_native_pipelined_recording, true, "GTA IV/Graphics/Nati "Record and submit each frame on a second thread while the worker assembles the next"); REXCVAR_DEFINE_BOOL(gta4_native_async_command_retire, true, "GTA IV/Graphics/Native Renderer", "Destroy recorded frame commands on a background thread"); +// [ps5 port] Android's choice on the PS5 too: with the guest swizzle applied +// the lights' ARGB textures come out with red and blue swapped (yellow light +// drawn blue). +static std::atomic<uint64_t> g_ps5_texture_evictions{0}; +// [ps5 port] The recorder used to wait after each frame until the TV took it, +// so the game, the worker and the recorder ran one after another, in step with +// the 30 fps pacing: a frame of 40 ms missed its slot and took two (66.7 ms, +// 15 fps). Decoupled, the recorder hands the frame over and carries on, and +// the game is held to the frame limit on its own (gta4_native_hooks.cpp), so +// a heavy scene runs at its real 25 fps instead of 15. +REXCVAR_DEFINE_STRING(ps5_renderer_cpus, "cores", "PS5", + "Where the game's render thread, the render worker and the recorder run: any " + "CPU, together on the first cluster (CPUs 0-7, one shared cache), or compare " + "(the two in turn, 10 s each, for the timing report)") + .allowed({"any", "together", "cores", "apart", "compare"}); +// [ps5 port] How the game's render thread captures draws. Lean: each buffer +// and texture is held once a frame (not once per draw, a reference count change +// on a cache line the other renderer threads also write), and the lines a +// capture touches are fetched ahead. Same commands, same pictures; classic is +// the method before it, kept for comparison. +// Own core (core): lean, run by a capture thread on a core of its own, the +// game's thread only copying each command and what it changed into a ring +// (capture_thread.inc). Compare: own core and lean in turn, 10 s each. +REXCVAR_DEFINE_STRING(ps5_lean_capture, "on", "PS5", + "Game thread capture: on its own core (core), lean (on), classic (off), or " + "compare (own core and lean in turn, 10 s each, for the timing report)") + .allowed({"core", "on", "off", "compare"}); +// [ps5 port] Read by the draw, clear and resolve hooks (gta4_native_hooks.cpp): +// on the frames in between, the environment reflection map (car paint, glass) +// is not redrawn and keeps the last one; its commands never reach the renderer. +// Every other frame by default on the PS5 (hard to tell apart in motion). +REXCVAR_DEFINE_BOOL(ps5_fg_hud_alpha_guard, true, "PS5", + "Frame generation: a frame whose HUD blends with the screen's alpha (the radar) is shown as " + "the game drew it, without a generated frame"); +REXCVAR_DEFINE_UINT32(ps5_environment_reflection_every, REX_PLATFORM_PS5 ? 2 : 1, "PS5", + "Redraw the environment reflection map (cars, glass) every Nth frame") + .range(1, 4); +// [ps5 port] The same for the water's reflection (gta4_native_hooks.cpp), on +// the frames the environment map rests. Every frame by default: the water's +// mirror image lags a frame behind when it rests. +REXCVAR_DEFINE_UINT32(ps5_water_reflection_every, 1, "PS5", + "Redraw the water reflection every Nth frame") + .range(1, 4); +REXCVAR_DEFINE_BOOL(ps5_decoupled_presentation, true, "PS5", + "Don't hold the renderer until the TV shows each frame; limit the game's frame " + "rate on its own thread instead"); +REXCVAR_DEFINE_BOOL(ps5_hud_trace, false, "PS5", + "Log the radar's draws, triangle fans, and dropped draws (bounded)"); +REXCVAR_DEFINE_BOOL(gta4_native_guest_texture_swizzle, !(REX_PLATFORM_ANDROID || REX_PLATFORM_PS5), + "GTA IV/Graphics/Native Renderer", + "Apply the guest's texture swizzle in image views where the GPU allows it"); REXCVAR_DEFINE_BOOL(gta4_vertex_color_as_rgba, false, "GTA IV/Diagnostics", "Bind D3DCOLOR vertex elements as R8G8B8A8 instead of B8G8R8A8"); REXCVAR_DEFINE_BOOL(gta4_trace_startup_content, false, "GTA IV/Diagnostics", @@ -463,6 +561,14 @@ REXCVAR_DEFINE_STRING( "zpass-back", "zpass-both", "x890-zpass-back", "lequal-front", "lequal-back", "lequal-both"}) .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); +// [ps5 port] A test for local lights that go dark near the camera (tunnels, +// the bar's pool table): no-stencil draws every local light's contribution +// without the stencil its volume marked (lights reach past their walls); +// no-clamp marks the volumes without depth clamp. +REXCVAR_DEFINE_STRING(ps5_light_test, "off", "PS5", + "Local light test: off, no-stencil (contributions skip the volume's stencil) or " + "no-clamp (volumes marked without depth clamp)") + .allowed({"off", "no-stencil", "no-clamp"}); REXCVAR_DEFINE_BOOL(gta4_native_light_ps9_stencil_bypass, false, "GTA IV/Diagnostics", "Diagnostic-only stencil bypass for fillerVolumePoint PS9 accumulation draws") .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); @@ -533,6 +639,17 @@ REXCVAR_DEFINE_BOOL(gta4_native_batch_texture_uploads, true, "GTA IV/Graphics/Na "Batch independent texture uploads before publishing the frame's draw bindings"); REXCVAR_DEFINE_BOOL(gta4_validate_native_hot_caches, false, "GTA IV/Diagnostics", "Cross-check incremental resource protection and constant uploads against original inputs"); +// [ps5 port] The fixed-function state (some 400 bytes) is hashed when it is +// captured, again once the worker finalizes it and a third time before it is +// recorded, only to catch a change between those points: three hashes a draw. +// Validation runs keep the check. +static bool VerifyFixedStateSnapshots() { +#if REX_PLATFORM_PS5 + return REXCVAR_GET(gta4_validate_native_hot_caches); +#else + return true; +#endif +} REXCVAR_DEFINE_BOOL(gta4_profile_native_detailed_gpu, true, "GTA IV/Diagnostics", "Enable approximate per-pass timestamps; coarse GPU envelope is cheaper on Metal"); REXCVAR_DEFINE_BOOL(gta4_profile_native_moltenvk_detailed, false, "GTA IV/Diagnostics", @@ -542,7 +659,7 @@ REXCVAR_DEFINE_BOOL(gta4_profile_native_detailed_cpu, true, "GTA IV/Diagnostics" REXCVAR_DEFINE_UINT32(gta4_profile_native_gpu_query_budget, 512, "GTA IV/Diagnostics", "Maximum timestamp boundaries per sampled frame (more boundaries add GPU overhead)") .range(16, rex::graphics::gta4_native::performance::kMaximumGpuQueriesPerFrame); -REXCVAR_DEFINE_UINT32(gta4_profile_native_interval, 1, "GTA IV/Diagnostics", +REXCVAR_DEFINE_UINT32(gta4_profile_native_interval, REX_PLATFORM_PS5 ? 4 : 1, "GTA IV/Diagnostics", "Capture one native-renderer profile after this many submitted frames") .range(1, 3600); REXCVAR_DEFINE_UINT32(gta4_profile_native_samples, 600, "GTA IV/Diagnostics", @@ -660,6 +777,8 @@ void LogFrameTimeline(uint32_t frame) { if (++batched == 8) { #if REX_PLATFORM_ANDROID __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "frame-timeline:%s", batch.c_str()); +#elif REX_PLATFORM_PS5 + REXLOG_INFO("frame-timeline:{}", batch); #endif batch.clear(); batched = 0; @@ -707,6 +826,22 @@ std::array<std::atomic<uint32_t>, 32> g_light_reuse_failures{}; // Hot producer/worker/recorder critical sections last microseconds, but bionic parks a // contended locker in the kernel at once and the futex wakeup costs far more // than the wait. Spin briefly before blocking. +// The renderer's every-120-frames pipeline report (worker/recorder stalls, +// render passes, barriers, resolves): logcat on Android, the log on the PS5. +#if REX_PLATFORM_ANDROID +#define PerfReport(...) __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", __VA_ARGS__) +#elif REX_PLATFORM_PS5 +[[gnu::format(printf, 1, 2)]] void PerfReport(const char* format, ...) { + char line[2048]; + va_list args; + va_start(args, format); + std::vsnprintf(line, sizeof(line), format, args); + va_end(args); + // (debug: a dozen lines every 2 s, most of a log at Normal detail) + REXLOG_DEBUG("perf: {}", line); +} +#endif + uint32_t MaxQueuedTitlePresents() { return std::clamp<uint32_t>(REXCVAR_GET(gta4_native_max_queued_frames), 1, 4); } @@ -717,6 +852,8 @@ std::unique_lock<std::mutex> LockWithSpin(std::mutex& mutex) { if (lock.try_lock()) return lock; #if defined(__aarch64__) asm volatile("yield"); +#elif defined(__x86_64__) + __builtin_ia32_pause(); #endif } lock.lock(); @@ -728,16 +865,20 @@ namespace transition = rex::diagnostics::gta4_transition; // The diagnostics policy is configured once at startup and never changes, but // asking it is a call into the runtime library; the recorder asks dozens of // times per draw. Cached once the policy is configured. -bool CachedDiagnosticsEnabled(rex::diagnostics::Category category) noexcept { - static std::atomic<uint32_t> cached_mask{0}; // bit 31: cached - uint32_t mask = cached_mask.load(std::memory_order_relaxed); - if (!(mask & 0x80000000u)) [[unlikely]] { - if (!rex::diagnostics::IsConfigured()) return rex::diagnostics::IsEnabled(category); - mask = 0x80000000u; - for (uint32_t i = 0; i < uint32_t(rex::diagnostics::Category::kCount) && i < 31; ++i) - if (rex::diagnostics::IsEnabled(rex::diagnostics::Category(i))) mask |= 1u << i; - cached_mask.store(mask, std::memory_order_relaxed); - } +std::atomic<uint32_t> g_cached_diagnostics_mask{0}; // bit 31: cached +[[gnu::noinline]] bool CachedDiagnosticsEnabledSlow(rex::diagnostics::Category category) noexcept { + if (!rex::diagnostics::IsConfigured()) return rex::diagnostics::IsEnabled(category); + uint32_t mask = 0x80000000u; + for (uint32_t i = 0; i < uint32_t(rex::diagnostics::Category::kCount) && i < 31; ++i) + if (rex::diagnostics::IsEnabled(rex::diagnostics::Category(i))) mask |= 1u << i; + g_cached_diagnostics_mask.store(mask, std::memory_order_relaxed); + return (mask >> uint32_t(category)) & 1u; +} +// The fast path inlines (a load and a bit test): the recorder asks per draw +// and it showed as 1% of the PS5 recorder's samples as a call. +[[gnu::always_inline]] inline bool CachedDiagnosticsEnabled(rex::diagnostics::Category category) noexcept { + const uint32_t mask = g_cached_diagnostics_mask.load(std::memory_order_relaxed); + if (!(mask & 0x80000000u)) [[unlikely]] return CachedDiagnosticsEnabledSlow(category); return (mask >> uint32_t(category)) & 1u; } namespace gpu_flight = rex::diagnostics::gpu_flight; @@ -820,7 +961,7 @@ const char* ArtificialLightProbeName(uint64_t instance) { } uint64_t ConfiguredLightColorDeltaInstance() { - const std::string selection = REXCVAR_GET(gta4_native_light_color_delta_probe); + const std::string& selection = REXCVAR_GET(gta4_native_light_color_delta_probe); if (selection == "bulb-x892") { return kApartmentBulbX892Instance; } @@ -1565,7 +1706,10 @@ class NativeSubmissionCompletion final }; uint64_t NextNativeTraceDiagnosticCount(std::atomic<uint64_t>& counter) { - if (!CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace)) { + // [ps5 port] These are the renderer's own failure reports (a draw dropped, + // and why); they are bounded, and the PS5 log is the only way to see them. + if (!CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) && + !(REX_PLATFORM_PS5 && REXCVAR_GET(ps5_hud_trace))) { return UINT64_MAX; } return counter.fetch_add(1, std::memory_order_relaxed) + 1; @@ -1869,6 +2013,9 @@ constexpr uint32_t kD3dFmtA8R8G8B8 = 0x18280186; constexpr uint32_t kD3dFmtA16B16G16R16F2 = 0x1A2201BF; constexpr uint32_t kD3dFmtD24FS8 = 0x1A220197; constexpr uint32_t kD3dFmtR32F = 0x2DA2ABA4; +// (two floats: the GPU particles' position targets, "GPU particle Position XY", +// which The Lost and Damned draws to) +constexpr uint32_t kD3dFmtG32R32F = 0x2D22ABA5; constexpr uint32_t kD3dFmtG16R16F = 0x2D22AB9F; constexpr uint32_t kD3dFmtG16R16F2 = 0x2D20AB8D; constexpr uint32_t kClipPlane0Offset = 10272; @@ -1922,6 +2069,10 @@ constexpr VkDeviceSize kDefaultVertexDataSize = kNativeDefaultVertexRecordSize; // Driver pipeline caches are normally small. Bound untrusted on-disk input so // a corrupt cache cannot force an arbitrarily large allocation at startup. constexpr uint64_t kMaximumNativePipelineCacheFileSize = 256ull * 1024ull * 1024ull; +#if REX_PLATFORM_PS5 +// [ps5 port] Pipelines shipped with the app (ps5/package/shader_cache_seed). +const std::filesystem::path kNativePipelineSeedRoot = "/app0/shader_cache_seed"; +#endif bool IsAlphaTestRequested(uint32_t alpha_test_enable, uint32_t alpha_function) { return alpha_test_enable != 0 && alpha_function != uint32_t(xenos::CompareFunction::kAlways); } @@ -2976,6 +3127,8 @@ VkFormat ConvertTextureFormat(xenos::TextureFormat format) { return VK_FORMAT_R16G16_SFLOAT; case xenos::TextureFormat::k_32_FLOAT: return VK_FORMAT_R32_SFLOAT; + case xenos::TextureFormat::k_32_32_FLOAT: + return VK_FORMAT_R32G32_SFLOAT; case xenos::TextureFormat::k_24_8: return VK_FORMAT_D24_UNORM_S8_UINT; case xenos::TextureFormat::k_24_8_FLOAT: @@ -3114,6 +3267,8 @@ VkFormat ConvertSurfaceFormat(uint32_t raw_format, bool depth) { return VK_FORMAT_R16G16B16A16_SFLOAT; case kD3dFmtR32F: return VK_FORMAT_R32_SFLOAT; + case kD3dFmtG32R32F: + return VK_FORMAT_R32G32_SFLOAT; case kD3dFmtG16R16F: case kD3dFmtG16R16F2: return VK_FORMAT_R16G16_SFLOAT; @@ -3602,6 +3757,9 @@ void Gta4NativeGraphicsSystem::StageNativeFlightDescriptorSlot(NativeDescriptorS } void Gta4NativeGraphicsSystem::StageNativeTextureFlightResources(const NativeTextureImage& image) { + // (Off, as it usually is, every call below returns at once: not even the + // profile scope's clock reads then. About 1% of the PS5 recorder.) + if (!IsNativeFlightRecorderEnabled()) return; const profile::CpuScope profile_scope(profile::CpuOp::kLifetimeBookkeeping); const uint32_t guest_handle = image.source ? image.source->handle : 0; @@ -3627,6 +3785,7 @@ void Gta4NativeGraphicsSystem::StageNativeTextureFlightResources(const NativeTex } void Gta4NativeGraphicsSystem::StageNativeSurfaceFlightResources(const NativeSurfaceImage& image) { + if (!IsNativeFlightRecorderEnabled()) return; StageNativeFlightResource(NativeFlightResourceKind::kSurfaceImage, NativeVulkanHandleIdentity(image.resource.image), image.descriptor.handle, image.materialized_serial); @@ -3908,6 +4067,10 @@ uint32_t Gta4NativeGraphicsSystem::GetTitleCommandAbi(uint32_t title_id) const { return title_id == kTitleId ? kTitleCommandAbi : 0; } +#if REX_PLATFORM_PS5 +#include "capture_thread.inc" +#endif + void Gta4NativeGraphicsSystem::PublishStagedDrawCommandsLocked() { for (auto& staged : staged_draw_commands_) { staged->diagnostic_submit_sequence = ++diagnostic_submit_sequence_; @@ -3917,8 +4080,17 @@ void Gta4NativeGraphicsSystem::PublishStagedDrawCommandsLocked() { staged_draw_commands_.clear(); } +#if REX_PLATFORM_PS5 +// (SubmitTitleCommand itself: capture_thread.inc) +bool Gta4NativeGraphicsSystem::SubmitTitleCommandHere(uint32_t title_id, uint32_t abi_version, + const void* command, size_t command_size, + uint32_t shadow_device, const uint8_t* shadow, + const uint8_t* up_payload, const uint8_t* objects, + uint32_t object_count) { +#else bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t abi_version, const void* command, size_t command_size) { +#endif if (title_id != kTitleId || !command || !memory_) { return false; } @@ -3927,28 +4099,31 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab size_t title_command_size = command_size; uint32_t title_command_abi = abi_version; GpuPassOrigin gpu_pass_origin{}; - if(abi_version==kGpuPassEnvelopeAbi){ + const bool gpu_pass_envelope = abi_version == kGpuPassEnvelopeAbi; + if(gpu_pass_envelope){ if(!UnpackGpuPassEnvelope(title_command,title_command_size,title_command_abi,gpu_pass_origin))return false; abi_version=title_command_abi; } - FireTraceContext fire_context{}; + // [ps5 port] The trace contexts (about 1 KB together) exist only for their + // envelopes: zeroing them cost every submitted command, state ones included. + std::optional<FireTraceContext> fire_context_storage; const bool fire_envelope = abi_version == kFireTraceEnvelopeAbi; if (fire_envelope && (!FireTraceConfig().enabled || !UnpackFireTraceEnvelope( - title_command, title_command_size, title_command_abi, fire_context))) return false; - TvTraceContext tv_context{}; + title_command, title_command_size, title_command_abi, fire_context_storage.emplace()))) return false; + std::optional<TvTraceContext> tv_context_storage; const bool tv_envelope = abi_version == kTvTraceEnvelopeAbi; - if (tv_envelope && (!TvTraceConfig().enabled || !UnpackTvTraceEnvelope(title_command,title_command_size,title_command_abi,tv_context))) return false; - PhoneTraceContext phone_context{}; + if (tv_envelope && (!TvTraceConfig().enabled || !UnpackTvTraceEnvelope(title_command,title_command_size,title_command_abi,tv_context_storage.emplace()))) return false; + std::optional<PhoneTraceContext> phone_context_storage; const bool phone_envelope = abi_version == kPhoneTraceEnvelopeAbi; if (phone_envelope && !UnpackPhoneTraceEnvelope(title_command, title_command_size, - title_command_abi, phone_context)) { + title_command_abi, phone_context_storage.emplace())) { PhoneTraceLog("native-reject", "reason=invalid-envelope"); return false; } if (title_command_abi != kTitleCommandAbi) { if (phone_envelope) { PhoneTraceLog("native-reject", fmt::format("run={} event={} reason=title-abi abi={}", - phone_context.run, phone_context.event, + phone_context_storage->run, phone_context_storage->event, title_command_abi)); } return false; @@ -3962,18 +4137,67 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab g_native_profile_capture_requested.load(std::memory_order_acquire)); const uint64_t capture_begin = profile_transport ? profile::CpuTick() : 0; std::unique_lock capture_lock(command_capture_mutex_); +#if REX_PLATFORM_PS5 + // [ps5 port] The capture thread's copy of the device and the payload it was + // sent, for this command only (another thread's capture never sees them). + struct CaptureCopyScope { + Gta4NativeGraphicsSystem& self; + CaptureCopyScope(Gta4NativeGraphicsSystem& s, uint32_t device, const uint8_t* copy, const uint8_t* up, + const uint8_t* objects, uint32_t object_count) + : self(s) { + self.capture_shadow_device_ = copy ? device : 0; + self.capture_shadow_memory_ = copy; + self.capture_up_payload_ = up; + self.capture_objects_ = objects; + self.capture_object_count_ = objects ? object_count : 0; + } + ~CaptureCopyScope() { + self.capture_shadow_device_ = 0; + self.capture_shadow_memory_ = nullptr; + self.capture_up_payload_ = nullptr; + self.capture_objects_ = nullptr; + self.capture_object_count_ = 0; + } + } capture_copy_scope(*this, shadow_device, shadow, up_payload, objects, object_count); +#endif const uint64_t capture_acquired = profile_transport ? profile::CpuTick() : 0; + ++title_commands_submitted_; if (!phone_envelope && !tv_envelope && !fire_envelope && !profile_transport && REXCVAR_GET(gta4_native_coalesce_state_commands) && CoalesceStateCommand(title_command, title_command_size)) { + ++title_commands_coalesced_; return true; } + // [ps5 port] With FSR 3 on, the game reports every object it draws (a + // temporal instance): only noted for the next draw, never queued. It used to + // take a ~3 KB command slot, copy into it and destroy it again each time. + if (!phone_envelope && !tv_envelope && !fire_envelope && !profile_transport && + title_command_size == sizeof(TemporalCommand)) { + CommandHeader header{}; + std::memcpy(&header, title_command, sizeof(header)); + if (header.type == CommandType::kTemporalUpdate && header.size == sizeof(TemporalCommand) && + CommandSize(header.type) == sizeof(TemporalCommand)) { + TemporalCommand temporal_command{}; + std::memcpy(&temporal_command, title_command, sizeof(temporal_command)); + if (temporal_command.event == TemporalEvent::kInstance && + CommandDevice(header.type, title_command) && + temporal::ValidTemporalCommand(temporal_command)) { + temporal_command_capture_.Observe(temporal_command); + return true; + } + } + } auto native_command_owner = native_command_pool_.Make(); NativeCommand& native_command = *native_command_owner; + // [ps5 port] (Lean capture used to start fetching the next pooled command's + // constant buffers here, once each capture was done. Reading where they are, + // in a command the recycling thread last wrote, cost more than it saved: the + // most sampled line on the game's render thread. The pool still brings the + // next command's own lines over: NativeCommandPool::AllocateSlot.) if (!ValidateAndCopyCommand(title_command, title_command_size, native_command)) { if (phone_envelope) { PhoneTraceLog("native-reject", fmt::format("run={} event={} reason=validation", - phone_context.run, phone_context.event)); + phone_context_storage->run, phone_context_storage->event)); } return false; } @@ -3991,21 +4215,32 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab native_command.temporal_instance = temporal_command_capture_.Capture( CommandDevice(native_command.type, title_command)); } - native_command.gpu_pass_origin = gpu_pass_origin; + // (A new command's origin is already empty: its cache line is left alone.) + if (gpu_pass_envelope) native_command.gpu_pass_origin = gpu_pass_origin; if (phone_envelope) { - native_command.phone_trace = std::make_shared<PhoneTraceContext>(phone_context); + native_command.phone_trace = std::make_shared<PhoneTraceContext>(*phone_context_storage); + native_command.trace_flags |= NativeCommand::kTracePhone; + } + if (tv_envelope) { + native_command.tv_trace = std::make_shared<TvTraceContext>(*tv_context_storage); + native_command.trace_flags |= NativeCommand::kTraceTv; + } + if (fire_envelope) { + native_command.fire_trace = std::make_shared<FireTraceContext>(*fire_context_storage); + native_command.trace_flags |= NativeCommand::kTraceFire; } - if (tv_envelope) native_command.tv_trace = std::make_shared<TvTraceContext>(tv_context); - if (fire_envelope) native_command.fire_trace = std::make_shared<FireTraceContext>(fire_context); CommandHeader title_header{}; std::memcpy(&title_header, title_command, sizeof(title_header)); // Batch asynchronous commands on the producer side; anything published // immediately publishes the batch first, keeping the queue order identical. - constexpr size_t kStagedDrawBatch = 32; + // [ps5 port] 64 on the PS5: the worker keeps up, so nearly every batch found + // it asleep, and each wake is a system call on the game's render thread + // (about 150 a frame in the city at 32). + constexpr size_t kStagedDrawBatch = REX_PLATFORM_PS5 ? 64 : 32; // The worker sleeps only on an empty queue; wake it per batch and at frame // boundaries rather than per command (a futex round trip each). - constexpr size_t kWorkerWakeBatch = 32; + constexpr size_t kWorkerWakeBatch = kStagedDrawBatch; // Commands submitted here are asynchronous: the title never waits for them // (ExecuteTitleCommand is the synchronous path and publishes the batch). // Presents and device lifetime changes still publish immediately. @@ -4015,9 +4250,37 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab // Chasing every texture slot's resource happens here, before render_mutex_ // (which the worker also needs) is taken; the queue then retains from the // dense generation array. +#if REX_PLATFORM_PS5 + // [ps5 port] Lean capture: a hold lasts kHoldFrames frames. Every that many + // presents, the holds travel with the present (queued below) and the next + // frame holds afresh; in between, the next frame reuses this one's holds + // (taking each one was a reference count change on a line the worker and + // the recycling thread also write: 3% of the game's render thread). The + // method in use changes only here. + bool release_holds = false; + if (native_command.type == CommandType::kPresent) { + release_holds = ++hold_window_frames_ >= kHoldFrames; + if (release_holds) { + hold_window_frames_ = 0; + if (++hold_epoch_ == 0) hold_epoch_ = 1; + } + lean_capture_ = rex::frame_stats::g_lean_capture.load(std::memory_order_relaxed); + capture_prefetch_ = lean_capture_ && rex::frame_stats::g_capture_prefetch.load(std::memory_order_relaxed); + static const uint64_t kTitleLines = TitleTouchedCommandLines(); + native_command_pool_.SetPrefetchLines(lean_capture_ ? kTitleLines : 0); + holds_taken_reported_.store(holds_taken_, std::memory_order_relaxed); + held_uses_reported_.store(held_uses_, std::memory_order_relaxed); + } +#endif // State absorbed since the previous queued command travels with this one. if (!producer_pending_state_.empty()) native_command.pending_state.swap(producer_pending_state_); producer_pending_state_.clear(); +#if REX_PLATFORM_PS5 + // [ps5 port] The buffer swapped in is the one the worker read last; the + // next state command writes into it. + if (capture_prefetch_ && producer_pending_state_.capacity()) + __builtin_prefetch(producer_pending_state_.data(), 1); +#endif CollectTextureProtection(native_command); QueueTextureProtection(native_command, true); if (stageable && !phone_envelope && !tv_envelope && !fire_envelope && !profile_transport && @@ -4033,10 +4296,19 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab std::unique_lock lock = LockWithSpin(render_mutex_); const uint64_t queue_lock_end = profile_transport ? profile::CpuTick() : 0; producer_waiting_ = render_queue_.size() >= kMaximumQueuedCommands || queued_title_presents_ >= MaxQueuedTitlePresents(); +#if REX_PLATFORM_PS5 + const int64_t producer_wait_begin_ns = producer_waiting_ ? rex::frame_stats::NowNs() : 0; +#endif render_condition_.wait(lock, [this]() { return !render_worker_running_ || (render_queue_.size() < kMaximumQueuedCommands && queued_title_presents_ < MaxQueuedTitlePresents()); }); +#if REX_PLATFORM_PS5 + // the game held up by the renderer (a full queue); on the capture thread, + // the game waits on the ring instead (counted there) + if (producer_wait_begin_ns && !t_capture_thread) + rex::frame_stats::NoteTitleSlotWait(uint64_t(rex::frame_stats::NowNs() - producer_wait_begin_ns) / 1000); +#endif const uint64_t backpressure_end = profile_transport ? profile::CpuTick() : 0; producer_waiting_ = false; if (!render_worker_running_) { @@ -4051,14 +4323,14 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab } else { native_command.diagnostic_submit_sequence = ++diagnostic_submit_sequence_; native_command.diagnostic_producer_epoch = diagnostic_producer_epoch_; - if (fire_envelope && fire_context.guest_frame % FireTraceConfig().interval == 0) + if (fire_envelope && fire_context_storage->guest_frame % FireTraceConfig().interval == 0) FireTraceLog("native-queued", fmt::format("occurrence={} event={} seq={} epoch={} type={} queue-depth={}", - fire_context.occurrence, fire_context.event, native_command.diagnostic_submit_sequence, + fire_context_storage->occurrence, fire_context_storage->event, native_command.diagnostic_submit_sequence, native_command.diagnostic_producer_epoch, CommandTypeName(native_command.type), render_queue_.size())); if (native_command.phone_trace) { PhoneTraceLog("native-queued", fmt::format( "run={} event={} occurrence={} seq={} epoch={} type={} queue-depth={}", - phone_context.run, phone_context.event, phone_context.occurrence, + phone_context_storage->run, phone_context_storage->event, phone_context_storage->occurrence, native_command.diagnostic_submit_sequence, native_command.diagnostic_producer_epoch, CommandTypeName(native_command.type), render_queue_.size())); } @@ -4074,6 +4346,14 @@ bool Gta4NativeGraphicsSystem::SubmitTitleCommand(uint32_t title_id, uint32_t ab native_command.profile_transport = {enqueued, enqueued-capture_begin, capture_acquired-capture_begin, queue_lock_end-queue_lock_begin, backpressure_end-queue_lock_end}; } +#if REX_PLATFORM_PS5 + // [ps5 port] Lean capture: the holds travel with the present that ends + // their frames (above); the worker drops them once it has taken (and + // upgraded) every command queued before it. Only a queued present carries + // them. + if (native_command.type == CommandType::kPresent && release_holds && !held_resources_.empty()) + native_command.released_holds.swap(held_resources_); +#endif render_queue_.push_back(std::move(native_command_owner)); if (title_header.type == CommandType::kPresent) { ++queued_title_presents_; @@ -4143,8 +4423,8 @@ bool Gta4NativeGraphicsSystem::ExecuteTitleCommand(uint32_t title_id, uint32_t a } std::lock_guard lock(temporal_provider_mutex_); const auto aa = ResolveAntiAliasingConfiguration( - rex::cvar::Query<std::string>("gta4_native_anti_aliasing"), REXCVAR_GET(gta4_native_msaa), - REXCVAR_GET(gta4_native_spatial_aa), rex::cvar::Query<bool>("gta4_native_anti_aliasing_unified")); + CachedQuery<std::string>("gta4_native_anti_aliasing"), REXCVAR_GET(gta4_native_msaa), + REXCVAR_GET(gta4_native_spatial_aa), CachedQuery<bool>("gta4_native_anti_aliasing_unified")); const auto route = GetAntiAliasingRoute(aa.mode); if (route.scene_sample_count != 1 || route.supersampling_pixel_factor != 1) { answer.status = TemporalUpscalerStatus::kUnsupportedQuality; @@ -4230,6 +4510,12 @@ bool Gta4NativeGraphicsSystem::ExecuteTitleCommand(uint32_t title_id, uint32_t a return false; } +#if REX_PLATFORM_PS5 + // [ps5 port] With capture on its own core: after everything the game + // submitted before it has been captured, and no other submission until done. + std::unique_lock<CaptureMutex> push_lock(capture_push_mutex_); + FinishCaptureOnCore(); +#endif std::unique_lock capture_lock(command_capture_mutex_); auto native_command_owner = native_command_pool_.Make(); NativeCommand& native_command = *native_command_owner; @@ -4239,8 +4525,14 @@ bool Gta4NativeGraphicsSystem::ExecuteTitleCommand(uint32_t title_id, uint32_t a native_command.bytes.resize(sizeof(lock_command)); std::memcpy(native_command.bytes.data(), &lock_command, sizeof(lock_command)); native_command.synchronous = std::make_shared<SynchronousCommand>(); - if (phone_envelope) native_command.phone_trace = std::make_shared<PhoneTraceContext>(phone_context); - if (tv_envelope) native_command.tv_trace = std::make_shared<TvTraceContext>(tv_context); + if (phone_envelope) { + native_command.phone_trace = std::make_shared<PhoneTraceContext>(phone_context); + native_command.trace_flags |= NativeCommand::kTracePhone; + } + if (tv_envelope) { + native_command.tv_trace = std::make_shared<TvTraceContext>(tv_context); + native_command.trace_flags |= NativeCommand::kTraceTv; + } const auto synchronous = native_command.synchronous; { std::unique_lock lock = LockWithSpin(render_mutex_); @@ -4395,6 +4687,13 @@ Gta4NativeGraphicsSystem::DecodeFixedFunctionState(std::span<const uint8_t> devi return state; } +// [ps5 port] The device state the capture reads: on the capture thread, its +// copy of the device (capture_thread.inc); otherwise the device itself. +#if REX_PLATFORM_PS5 +#define CaptureDeviceMemoryOf(device) CaptureDeviceMemory(device) +#else +#define CaptureDeviceMemoryOf(device) memory_->TranslateVirtual<const uint8_t*>(device) +#endif bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_t command_size, NativeCommand& native_command) { auto reject = [command_size](CommandType type, const char* reason) { @@ -4523,6 +4822,7 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ std::lock_guard lock(texture_resource_mutex_); const VirtualResourceRegistrationResult result = virtual_resource_registry_.Register(registration); + BumpSurfaceLookupEpoch(); ForgetTextureCapture(registration.resource); const NativeVirtualResourceRecord* record = virtual_resource_registry_.Find(registration.resource); @@ -4530,7 +4830,7 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ texture_resources_.erase(registration.resource); dirty_texture_handles_.erase(registration.resource); } - REXLOG_INFO( + REXLOG_DEBUG( "gta4-native-virtual-resource: point=register handle={:08X} kind={} wrapper={:08X} " "companion={:08X} backing={}x{} logical={}x{} physical={}x{} domain={} " "caller={:08X} lifetime={} result={} packed-depth-source={:08X}", @@ -4580,7 +4880,11 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ dirty_buffer_handles_.insert(unlock.resource); ForgetBufferCapture(unlock.resource); } - REXLOG_INFO( + // [ps5 port] Debug, not info: logged for every buffer the game unlocks + // (hundreds a second while it streams), with the capture lock held. At + // info each one waited on the log file and the game's render thread on + // them (beta logs: 0.8-0.9 s stalls, again and again). + REXLOG_DEBUG( "gta4-native-virtual-resource: point=unlock handle={:08X} access={} flags={:08X} " "range={}+{} lock-caller={:08X} unlock-caller={:08X} lifetime={} decision={}", unlock.resource, uint32_t(unlock.access), unlock.flags, unlock.range_offset, @@ -4617,6 +4921,7 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ dirty_texture_handles_.erase(release.resource); vector_font_ids_.erase(release.resource); virtual_resource_registry_.Erase(release.resource); + BumpSurfaceLookupEpoch(); remaining_textures = texture_resources_.size(); } { @@ -4698,6 +5003,7 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ const bool metadata_valid = cache_entry && StockShaderCacheEntryMatchesStage(*cache_entry, registration.stage) && !(cache_entry->usedTextureMask & ~kSupportedShaderTextureMask); + ++producer_shader_metadata_generation_; if (metadata_valid) { producer_shader_metadata_[registration.shader] = {registration.stage, registration.hash, cache_entry->usedTextureMask}; @@ -4726,7 +5032,7 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ } } else if (header.type == CommandType::kReleaseResource) { const auto& release = *static_cast<const ReleaseResourceCommand*>(command); - producer_shader_metadata_.erase(release.resource); + if (producer_shader_metadata_.erase(release.resource)) ++producer_shader_metadata_generation_; } if (header.type == CommandType::kDeviceCreated || header.type == CommandType::kDeviceDestroyed) { @@ -4734,6 +5040,8 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ const auto& device_command = *static_cast<const DeviceCommand*>(command); if (header.type == CommandType::kDeviceDestroyed || device_command.mode != 2) { producer_device_shader_states_.erase(device); + producer_last_shader_device_ = 0; + producer_last_shader_state_ = nullptr; } std::lock_guard snapshot_lock(device_snapshot_mutex_); last_device_snapshots_.erase(device); @@ -4743,6 +5051,32 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ if (header.type == CommandType::kPresent) { const auto& present = *static_cast<const PresentCommand*>(command); MarkFrameTimeline(present.submitted_frame, kTimelineGuestPresent); + rex::frame_stats::NotePresent(); // [ps5 port] the frame rate counter + TakeChangedTextures(); +#if REX_PLATFORM_PS5 + { + // A frame over 100 ms in play: whether the game itself was slow, waited + // for the renderer, or waited on its own other threads (loading, say), + // at most one line a second. + static int64_t last_hitch_log_ns = 0; + const uint32_t frame_us = rex::frame_stats::g_last_frame_us.load(std::memory_order_relaxed); + const uint64_t guest_wait_ticks = + rex::frame_stats::g_title_frame_guest_wait_ticks.exchange(0, std::memory_order_relaxed); + const int64_t now_ns = rex::frame_stats::NowNs(); + if (frame_us > 100000 && rex::frame_stats::g_presents.load(std::memory_order_relaxed) > 600 && + now_ns - last_hitch_log_ns > 1000000000) { + last_hitch_log_ns = now_ns; + REXLOG_INFO("ps5 hitch: frame {} took {:.0f} ms (the game waited {:.0f} ms of it for the " + "renderer, {:.0f} ms on its own other threads); recorder's last frame {:.1f} ms " + "of work", + present.submitted_frame, frame_us / 1000.0, + rex::frame_stats::g_title_last_slot_wait_us.load(std::memory_order_relaxed) / 1000.0, + double(guest_wait_ticks) * 1000.0 / + double(rex::chrono::Clock::QueryHostTickFrequency()), + rex::frame_stats::g_last_recorder_busy_us.load(std::memory_order_relaxed) / 1000.0); + } + } +#endif const auto frontbuffer_fetch = std::bit_cast<xenos::xe_gpu_texture_fetch_t>(present.frontbuffer_fetch); auto fetch_matches = [&frontbuffer_fetch](const NativeTextureResource& resource) { @@ -4837,11 +5171,65 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ if (draw_command || header.type == CommandType::kClear) { const uint32_t device = CommandDevice(header.type, command); const NativeDirtyState* dirty_state = CommandDirtyState(header.type, command); - const uint8_t* device_memory = memory_->TranslateVirtual<const uint8_t*>(device); + const uint8_t* device_memory = CaptureDeviceMemoryOf(device); if (!device_memory) { return reject(header.type, "device-state-translation"); } const std::span<const uint8_t> device_state(device_memory, kGuestDeviceSize); +#if REX_PLATFORM_PS5 + if (draw_command) { + // [ps5 port] What the rest of this capture looks up, fetched now so the + // cache misses overlap the constant and state work in between: the + // capture-cache sets of the bound vertex and index buffers and of the + // textures the current shaders read, and the bound surfaces' objects. + // (Prefetches only: nothing here changes what is captured.) + const auto prefetch_set = [](const void* set) { + __builtin_prefetch(set); + __builtin_prefetch(static_cast<const uint8_t*>(set) + 64); + }; + // A buffer's set spans up to three lines (two 56-byte slots); its guest + // object (flags, address, size) is read first. + const auto prefetch_buffer = [&](uint32_t handle) { + const auto* set = static_cast<const uint8_t*>(CaptureCacheSet(buffer_capture_cache_, handle)); + prefetch_set(set); + __builtin_prefetch(set + 2 * sizeof(BufferCaptureCacheSlot) - 1); + if (capture_prefetch_) { + if (const uint8_t* object = memory_->TranslateVirtual<const uint8_t*>(handle)) { + __builtin_prefetch(object); + __builtin_prefetch(object + kResourceSizeOffset + 3); + } + } + }; + for (uint32_t stream = 0; stream < kVertexStreamCount; ++stream) { + if (const uint32_t handle = + LoadGuestWord(device_state, kStreamBufferBase + stream * sizeof(uint32_t))) + prefetch_buffer(handle); + } + if (header.type == CommandType::kDrawIndexedPrimitive) { + if (const uint32_t handle = LoadGuestWord(device_state, kIndexBufferOffset)) + prefetch_buffer(handle); + } + if (device == producer_last_shader_device_ && producer_last_shader_state_ && + producer_last_shader_state_->memo_generation == producer_shader_metadata_generation_) { + for (uint32_t mask = producer_last_shader_state_->memo_texture_mask; mask; mask &= mask - 1) { + const uint32_t stage = uint32_t(std::countr_zero(mask)); + if (stage >= kShaderTextureCount) break; + if (const uint32_t handle = + LoadGuestWord(device_state, kTextureHandleBase + stage * sizeof(uint32_t))) + prefetch_set(CaptureCacheSet(texture_capture_cache_, handle)); + } + } + for (uint32_t index = 0; index <= kRenderTargetCount; ++index) { + const uint32_t handle = LoadGuestWord( + device_state, index < kRenderTargetCount ? kRenderTargetOffset + index * sizeof(uint32_t) + : kDepthStencilOffset); + if (handle) { + if (const uint8_t* object = memory_->TranslateVirtual<const uint8_t*>(handle)) + __builtin_prefetch(object); + } + } + } +#endif // The half scene is resolved inside the composite phase, after its begin marker. // Capture its generation at the draw, never the previous frame's marker-time image. if (draw_command && producer_postfx_half_scene_) { @@ -4849,7 +5237,9 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ const auto half_scene = texture_resources_.find(producer_postfx_half_scene_); if (half_scene != texture_resources_.end()) native_command.postfx_half_scene = half_scene->second; } - const std::string state_transport = REXCVAR_GET(gta4_native_state_transport); + // [ps5 port] A view: comparing the std::string with a literal called into + // libc (length, compare) three times per draw; this compares sizes first. + const std::string_view state_transport = REXCVAR_GET(gta4_native_state_transport); const bool compare_transport = state_transport == "compare"; const bool diagnostic_snapshot = CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeTrace) || @@ -4889,7 +5279,9 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ last_device_snapshots_[device] = snapshot; } } - native_command.device_snapshot.storage = std::move(snapshot); + // A new command's snapshot is already empty: without a full snapshot (the + // usual case) its cache line is not touched at all. + if (snapshot) native_command.device_snapshot.storage = std::move(snapshot); if (draw_command || header.type == CommandType::kClear) { NativeDirtyWords dirty_words{}; @@ -4969,9 +5361,25 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ native_command.dirty_components |= DirtyStateComponentBit(DirtyStateComponent::kIntegerConstants); } +#if REX_PLATFORM_PS5 + // [ps5 port] Lean capture, second stage: the capture-cache sets fetched + // at the start are in by now; fetch the held resources they point to + // (read by the capture below, after the fixed state is decoded). + if (draw_command && capture_prefetch_) { + const uint32_t device_for_memo = CommandDevice(header.type, command); + const uint32_t texture_mask = + device_for_memo == producer_last_shader_device_ && producer_last_shader_state_ && + producer_last_shader_state_->memo_generation == producer_shader_metadata_generation_ + ? producer_last_shader_state_->memo_texture_mask + : 0; + PrefetchHeldCaptures(device_state, texture_mask, + header.type == CommandType::kDrawIndexedPrimitive); + } +#endif native_command.fixed_function_state = DecodeFixedFunctionState(device_state); - native_command.captured_fixed_function_state_hash = - HashFixedFunctionState(native_command.fixed_function_state); + if (VerifyFixedStateSnapshots()) + native_command.captured_fixed_function_state_hash = + HashFixedFunctionState(native_command.fixed_function_state); } for (uint32_t index = 0; index < kRenderTargetCount; ++index) { native_command.snapshot_render_targets[index] = CaptureSurfaceDescriptor( @@ -4988,17 +5396,26 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ return reject(header.type, "up-payload-range"); } if (draw.vertex_data_size) { +#if REX_PLATFORM_PS5 + // (on the capture thread: the copy the game's thread put in the ring) + const uint8_t* vertex_data = capture_up_payload_ ? capture_up_payload_ + : memory_->TranslateVirtual<const uint8_t*>(draw.vertex_data); +#else const uint8_t* vertex_data = memory_->TranslateVirtual<const uint8_t*>(draw.vertex_data); +#endif if (!vertex_data) { return reject(header.type, "up-payload-translation"); } - native_command.payload.resize(draw.vertex_data_size); - std::memcpy(native_command.payload.data(), vertex_data, draw.vertex_data_size); + // (one copy: resize would zero the bytes first, which in busy streets was + // up to 9% of the game's render thread; and into a recycled buffer when + // one is big enough, not a new allocation each draw) + LargeConstantPayloadStash::Reserve(native_command.payload, draw.vertex_data_size); + native_command.payload.assign(vertex_data, vertex_data + draw.vertex_data_size); } } else if (header.type == CommandType::kDrawPrimitive || header.type == CommandType::kDrawIndexedPrimitive) { const uint32_t device = CommandDevice(header.type, command); - const uint8_t* device_memory = memory_->TranslateVirtual<const uint8_t*>(device); + const uint8_t* device_memory = CaptureDeviceMemoryOf(device); if (!device_memory) { return reject(header.type, "vertex-state-translation"); } @@ -5007,13 +5424,17 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ const uint32_t handle = LoadGuestWord(device_state, kStreamBufferBase + stream * sizeof(uint32_t)); if (handle) { - native_command.vertex_buffers[stream] = CaptureBufferResource(handle); + bool held = false; + native_command.vertex_buffers[stream] = CaptureBufferResource(handle, lean_capture_, &held); + if (held) native_command.held_resource_mask |= uint64_t(1) << stream; } } if (header.type == CommandType::kDrawIndexedPrimitive) { const uint32_t handle = LoadGuestWord(device_state, kIndexBufferOffset); if (handle) { - native_command.index_buffer = CaptureBufferResource(handle); + bool held = false; + native_command.index_buffer = CaptureBufferResource(handle, lean_capture_, &held); + if (held) native_command.held_resource_mask |= uint64_t(1) << NativeCommand::kHeldIndexBufferBit; } CaptureBulbSource(native_command, device_state); } @@ -5022,32 +5443,57 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ if (header.type == CommandType::kDrawPrimitive || header.type == CommandType::kDrawPrimitiveUp || header.type == CommandType::kDrawIndexedPrimitive) { const uint32_t device = CommandDevice(header.type, command); - const auto producer_state = producer_device_shader_states_.find(device); + // [ps5 port] Three hash lookups per draw before: the device's shaders and + // each shader's metadata. The device's entry and the pair's masks are + // kept until a shader is set, registered or released. + ProducerDeviceShaderState* producer_state = nullptr; + if (device && device == producer_last_shader_device_ && producer_last_shader_state_) { + producer_state = producer_last_shader_state_; + } else if (const auto found = producer_device_shader_states_.find(device); + found != producer_device_shader_states_.end()) { + producer_state = &found->second; + producer_last_shader_device_ = device; + producer_last_shader_state_ = producer_state; + } uint32_t vertex_texture_mask = 0; - if (producer_state != producer_device_shader_states_.end()) { - const auto append_shader_usage = [this, &native_command, &vertex_texture_mask]( - uint32_t shader_handle, ShaderStage expected_stage) { - if (!shader_handle) { + if (producer_state) { + ProducerDeviceShaderState& state = *producer_state; + if (state.memo_generation != producer_shader_metadata_generation_ || + state.memo_pixel_shader != state.pixel_shader || + state.memo_vertex_shader != state.vertex_shader) { + uint32_t texture_mask = 0, vertex_mask = 0; + const auto append_shader_usage = [this, &texture_mask, &vertex_mask]( + uint32_t shader_handle, ShaderStage expected_stage) { + if (!shader_handle) { + return true; + } + const auto metadata = producer_shader_metadata_.find(shader_handle); + if (metadata == producer_shader_metadata_.end() || + metadata->second.stage != expected_stage) { + return false; + } + texture_mask |= metadata->second.used_texture_mask; + if (expected_stage == ShaderStage::kVertex) { + vertex_mask |= metadata->second.used_texture_mask; + } return true; - } - const auto metadata = producer_shader_metadata_.find(shader_handle); - if (metadata == producer_shader_metadata_.end() || - metadata->second.stage != expected_stage) { - return false; - } - native_command.used_texture_mask |= metadata->second.used_texture_mask; - if (expected_stage == ShaderStage::kVertex) { - vertex_texture_mask |= metadata->second.used_texture_mask; - } - return true; - }; - if (!append_shader_usage(producer_state->second.pixel_shader, ShaderStage::kPixel) || - !append_shader_usage(producer_state->second.vertex_shader, ShaderStage::kVertex)) { + }; + state.memo_valid = append_shader_usage(state.pixel_shader, ShaderStage::kPixel) && + append_shader_usage(state.vertex_shader, ShaderStage::kVertex); + state.memo_texture_mask = texture_mask; + state.memo_vertex_texture_mask = vertex_mask; + state.memo_pixel_shader = state.pixel_shader; + state.memo_vertex_shader = state.vertex_shader; + state.memo_generation = producer_shader_metadata_generation_; + } + if (!state.memo_valid) { return reject(header.type, "active-shader-texture-metadata"); } + native_command.used_texture_mask |= state.memo_texture_mask; + vertex_texture_mask |= state.memo_vertex_texture_mask; } - const uint8_t* device_memory = memory_->TranslateVirtual<const uint8_t*>(device); + const uint8_t* device_memory = CaptureDeviceMemoryOf(device); if (!device_memory) { return reject(header.type, "texture-state-translation"); } @@ -5089,7 +5535,9 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ dirty_texture_handles_.insert(handle); } } - native_command.textures[stage] = CaptureTextureResource(handle, fetch, stage); + bool held = false; + native_command.textures[stage] = CaptureTextureResource(handle, fetch, stage, lean_capture_, &held); + if (held) native_command.held_resource_mask |= uint64_t(1) << (NativeCommand::kHeldTextureBit + stage); const auto& texture = native_command.textures[stage]; const uint64_t generation = texture ? texture->generation : 0; if (REXCVAR_GET(gta4_native_vertex_texture_trace) && generation != recheck->second.generation) { @@ -5106,7 +5554,9 @@ bool Gta4NativeGraphicsSystem::ValidateAndCopyCommand(const void* command, size_ if (vertex_texture_rechecks_.size() > 4096) vertex_texture_rechecks_.clear(); continue; } - native_command.textures[stage] = CaptureTextureResource(handle, fetch, stage); + bool held = false; + native_command.textures[stage] = CaptureTextureResource(handle, fetch, stage, lean_capture_, &held); + if (held) native_command.held_resource_mask |= uint64_t(1) << (NativeCommand::kHeldTextureBit + stage); } } @@ -5213,7 +5663,7 @@ Gta4NativeGraphicsSystem::CreateResolvedTextureResource(const ResolveCommand& co dirty_texture_handles_.erase(command.destination_texture); virtual_resource_registry_.PublishHostWrite(command.destination_texture); if (virtual_lifetime) { - REXLOG_INFO( + REXLOG_DEBUG( "gta4-native-virtual-resource: point=resolve-publish handle={:08X} lifetime={} " "generation={} size={}x{} format={}", command.destination_texture, virtual_lifetime, resource->generation, info.width + 1, @@ -5229,8 +5679,83 @@ Gta4NativeGraphicsSystem::CreateResolvedTextureResource(const ResolveCommand& co return resource; } +// [ps5 port] Statistics only the producer writes (under the capture lock): +// a plain add, not a locked one, several times per captured buffer. +static inline void BumpProducerCounter(std::atomic<uint64_t>& counter) { + counter.store(counter.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); +} + +// [ps5 port] Lean capture: the resources this draw's buffers and textures +// were held as this frame, fetched before the capture reads their first line +// (dropped, last-used frame, generation). Prefetches only. +void Gta4NativeGraphicsSystem::PrefetchHeldCaptures(std::span<const uint8_t> device_state, + uint32_t texture_mask, bool indexed) { + const uint32_t epoch = hold_epoch_; + const auto buffer = [&](uint32_t handle) { + const auto* set = + static_cast<const BufferCaptureCacheSlot*>(CaptureCacheSet(buffer_capture_cache_, handle)); + for (size_t way = 0; way < 2; ++way) { + if (set[way].handle != handle) continue; + if (set[way].hold_epoch == epoch && set[way].held) __builtin_prefetch(set[way].held); + return; + } + }; + for (uint32_t stream = 0; stream < kVertexStreamCount; ++stream) { + if (const uint32_t handle = + LoadGuestWord(device_state, kStreamBufferBase + stream * sizeof(uint32_t))) + buffer(handle); + } + if (indexed) { + if (const uint32_t handle = LoadGuestWord(device_state, kIndexBufferOffset)) buffer(handle); + } + for (uint32_t mask = texture_mask; mask; mask &= mask - 1) { + const uint32_t stage = uint32_t(std::countr_zero(mask)); + if (stage >= kShaderTextureCount) break; + const uint32_t handle = + LoadGuestWord(device_state, kTextureHandleBase + stage * sizeof(uint32_t)); + if (!handle) continue; + const auto* set = + static_cast<const TextureCaptureCacheSlot*>(CaptureCacheSet(texture_capture_cache_, handle)); + for (size_t way = 0; way < 2; ++way) { + if (set[way].handle != handle) continue; + if (set[way].hold_epoch == epoch && set[way].held) __builtin_prefetch(set[way].held); + break; + } + } +} + +// [ps5 port] Lean capture: the render worker makes the plain resource pointers +// the game's render thread stored into owning references, as it takes the +// command and before anything can keep a copy. The producer holds every such +// resource until the frame's present is taken, after this command. +void Gta4NativeGraphicsSystem::UpgradeHeldResources(NativeCommand& command) { + const auto upgrade = [](auto& field) { + if (!field) return; + try { + field = field->shared_from_this(); + } catch (const std::bad_weak_ptr&) { + // Cannot happen while the hold protocol holds; drop the binding rather + // than keep a pointer nothing owns. + static std::atomic<uint32_t> reported{0}; + if (reported.fetch_add(1, std::memory_order_relaxed) < 8) + REXLOG_ERROR("gta4-native: lean capture: a held resource was already released"); + field.reset(); + } + }; + for (uint64_t mask = std::exchange(command.held_resource_mask, 0); mask; mask &= mask - 1) { + const uint32_t bit = uint32_t(std::countr_zero(mask)); + if (bit < kVertexStreamCount) { + upgrade(command.vertex_buffers[bit]); + } else if (bit == NativeCommand::kHeldIndexBufferBit) { + upgrade(command.index_buffer); + } else if (bit - NativeCommand::kHeldTextureBit < kTextureStageCount) { + upgrade(command.textures[bit - NativeCommand::kHeldTextureBit]); + } + } +} + std::shared_ptr<const Gta4NativeGraphicsSystem::NativeBufferResource> -Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { +Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle, bool hold, bool* held) { auto reject = [handle](const char* reason, uint32_t flags, uint32_t data_address, uint32_t data_size) { static std::atomic<uint64_t> rejection_count{0}; @@ -5244,7 +5769,11 @@ Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { return std::shared_ptr<const NativeBufferResource>{}; }; +#if REX_PLATFORM_PS5 + const uint8_t* object = CaptureObjectMemory(handle); // (as at the draw, on the capture thread) +#else const uint8_t* object = memory_->TranslateVirtual<const uint8_t*>(handle); +#endif if (!object) { return reject("object-translation", 0, 0, 0); } @@ -5274,51 +5803,119 @@ Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { // reclamation. BufferCaptureCacheSlot& cache_slot = BufferCaptureSlot(handle); const uint64_t cache_epoch = buffer_resources_epoch_.load(std::memory_order_acquire); + // Buffers seen changing without an unlock are compared in full on every + // capture and never enter the front cache. + const bool full_validation = !buffer_full_validation_handles_.empty() && + buffer_full_validation_handles_.contains(handle); auto remember = [&](const std::shared_ptr<const NativeBufferResource>& resource) { + if (full_validation) return; cache_slot.handle = handle; cache_slot.flags = flags; cache_slot.address = data_address; cache_slot.size = data_size; cache_slot.epoch = cache_epoch; cache_slot.resource = resource; + cache_slot.held = nullptr; + cache_slot.hold_epoch = 0; }; if (cache_slot.handle == handle && cache_slot.epoch == cache_epoch && cache_slot.flags == flags && cache_slot.address == data_address && cache_slot.size == data_size && !metadata->guest_locked && !buffer_fast_path_disabled_.load(std::memory_order_relaxed)) { - if (auto cached = cache_slot.resource.lock()) { + // [ps5 port] Lean capture: held for this frame already, so neither the + // weak_ptr lock nor the command's copy changes the reference count (a + // cache line the worker and the recycling thread also write). + const NativeBufferResource* held_resource = + hold && cache_slot.hold_epoch == hold_epoch_ ? cache_slot.held : nullptr; + if (held_resource && held_resource->dropped.load(std::memory_order_acquire)) { + held_resource = nullptr; + cache_slot.held = nullptr; + cache_slot.hold_epoch = 0; + } + std::shared_ptr<const NativeBufferResource> cached; + if (!held_resource) { + cached = cache_slot.resource.lock(); + if (cached && cached->dropped.load(std::memory_order_acquire)) cached.reset(); + } + const NativeBufferResource* const current = held_resource ? held_resource : cached.get(); + // What the fast path returns: with hold, a plain pointer to the resource + // held for the frame (taking the hold now if this is its first use). + const auto deliver = [&]() -> std::shared_ptr<const NativeBufferResource> { + if (!hold) return std::move(cached); + if (!held_resource) { + cache_slot.held = cached.get(); + cache_slot.hold_epoch = hold_epoch_; + held_resources_.push_back(std::move(cached)); + ++holds_taken_; + } + ++held_uses_; + if (held) *held = true; + return std::shared_ptr<const NativeBufferResource>(std::shared_ptr<const NativeBufferResource>(), + current); + }; + if (current) { // Already validated (or captured) this frame, and no unlock since: the // slot is dropped when the handle is dirtied. Meshes drawn many times a // frame then skip the cold shadow compare and the stores into the // resource that the recorder reads. if (REXCVAR_GET(gta4_native_buffer_validation_per_frame) && - cached->last_used_frame.load(std::memory_order_relaxed) == + current->last_used_frame.load(std::memory_order_relaxed) == g_native_memory_profile_event_frame.load(std::memory_order_relaxed)) { - return cached; + ++buffer_capture_stats_.front_hits; + return deliver(); } const uint8_t* cached_data = memory_->TranslateVirtual<const uint8_t*>(data_address); if (cached_data) { - buffer_fast_path_request_count_.fetch_add(1, std::memory_order_relaxed); + BumpProducerCounter(buffer_fast_path_request_count_); +#if REX_PLATFORM_PS5 + // [ps5 port] A clean buffer's slice compare runs once every four + // frames it's drawn in (a buffer drawn every frame is compared one + // frame in four; one drawn every other frame, like the car reflection + // map's, every other time). The compare's cache misses were 4-6% of the + // game's render thread in busy streets with it every frame, and still + // ~1.5% (the most of any one line of ours) at every other frame. A + // buffer the game changes without an unlock (none in a session's play + // at 0.5b7) is still caught, three frames later at most, and from then + // on is compared in full every time (not on this path). + constexpr uint32_t kSampledCompareEvery = 4; + const uint32_t frame = + uint32_t(g_native_memory_profile_event_frame.load(std::memory_order_relaxed)); + const uint32_t compared = current->last_compared_frame.load(std::memory_order_relaxed); + if (compared && frame > compared && frame - compared < kSampledCompareEvery) { + ++buffer_capture_stats_.front_hits; + current->last_used_frame.store(frame, std::memory_order_relaxed); + return deliver(); + } +#endif const NativeBufferShadowValidationRange validation_range = GetNativeBufferShadowValidationRange( - data_size, cached->shadow_validation_offset.load(std::memory_order_relaxed)); - buffer_shadow_validation_count_.fetch_add(1, std::memory_order_relaxed); - if (NativeBufferShadowPayloadRangeMatches(cached_data, cached->payload.data(), data_size, + data_size, current->shadow_validation_offset.load(std::memory_order_relaxed)); + BumpProducerCounter(buffer_shadow_validation_count_); + if (NativeBufferShadowPayloadRangeMatches(cached_data, current->payload.data(), data_size, validation_range)) { - cached->shadow_validation_offset.store(validation_range.next_offset, - std::memory_order_relaxed); - buffer_capture_reuse_count_.fetch_add(1, std::memory_order_relaxed); - cached->last_used_frame.store( - g_native_memory_profile_event_frame.load(std::memory_order_relaxed), - std::memory_order_relaxed); - return cached; + current->shadow_validation_offset.store(validation_range.next_offset, + std::memory_order_relaxed); + BumpProducerCounter(buffer_capture_reuse_count_); + ++buffer_capture_stats_.front_hits; + const uint32_t validated_frame = + uint32_t(g_native_memory_profile_event_frame.load(std::memory_order_relaxed)); + current->last_used_frame.store(validated_frame, std::memory_order_relaxed); +#if REX_PLATFORM_PS5 + current->last_compared_frame.store(validated_frame, std::memory_order_relaxed); +#endif + return deliver(); } } } - cache_slot.handle = 0; // Mismatch or gone: the full path below decides. + // Mismatch or gone: the full path below decides (and any hold of this + // slot's resource stays in held_resources_ until the frame's end). + cache_slot.handle = 0; + cache_slot.held = nullptr; + cache_slot.hold_epoch = 0; } std::shared_ptr<const NativeBufferResource> matching_cache_entry; bool can_reuse_clean_capture = false; + ++buffer_capture_stats_.lookups; { std::lock_guard lock(buffer_resource_mutex_); const auto existing = buffer_resources_.find(handle); @@ -5332,7 +5929,8 @@ Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { can_reuse_clean_capture = CanReuseNativeBufferCapture(cache_present, dirty_buffer_handles_.contains(handle), metadata_matches) && - !metadata->guest_locked && !buffer_fast_path_disabled_.load(std::memory_order_relaxed); + !metadata->guest_locked && !buffer_fast_path_disabled_.load(std::memory_order_relaxed) && + !full_validation; } const uint8_t* data = memory_->TranslateVirtual<const uint8_t*>(data_address); @@ -5343,6 +5941,10 @@ Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { // full validation. Compare against the immutable capture before allocating a // temporary payload and hashing it; unchanged bytes need neither operation. // Keep this separate from the sampled clean path and its mismatch safeguard. + if (!can_reuse_clean_capture && matching_cache_entry) { + ++buffer_capture_stats_.full_compares; + buffer_capture_stats_.full_compare_bytes += data_size; + } if (!can_reuse_clean_capture && matching_cache_entry && NativeBufferShadowPayloadMatches(data, matching_cache_entry->payload.data(), data_size)) { std::lock_guard lock(buffer_resource_mutex_); @@ -5358,37 +5960,55 @@ Gta4NativeGraphicsSystem::CaptureBufferResource(uint32_t handle) { } if (can_reuse_clean_capture) { const auto& clean_cache_entry = matching_cache_entry; - buffer_fast_path_request_count_.fetch_add(1, std::memory_order_relaxed); + BumpProducerCounter(buffer_fast_path_request_count_); const size_t validation_offset = clean_cache_entry->shadow_validation_offset.load(std::memory_order_relaxed); const NativeBufferShadowValidationRange validation_range = GetNativeBufferShadowValidationRange(data_size, validation_offset); - buffer_shadow_validation_count_.fetch_add(1, std::memory_order_relaxed); + BumpProducerCounter(buffer_shadow_validation_count_); if (NativeBufferShadowPayloadRangeMatches(data, clean_cache_entry->payload.data(), data_size, validation_range)) { clean_cache_entry->shadow_validation_offset.store(validation_range.next_offset, std::memory_order_relaxed); - buffer_capture_reuse_count_.fetch_add(1, std::memory_order_relaxed); + BumpProducerCounter(buffer_capture_reuse_count_); clean_cache_entry->last_used_frame.store( g_native_memory_profile_event_frame.load(std::memory_order_relaxed), std::memory_order_relaxed); remember(clean_cache_entry); return clean_cache_entry; } - buffer_shadow_mismatch_count_.fetch_add(1, std::memory_order_relaxed); - if (ShouldDisableNativeBufferFastPath(true, false) && + BumpProducerCounter(buffer_shadow_mismatch_count_); + // One buffer the title wrote without an unlock used to switch the sampled + // check off for the whole session: every capture of every buffer then + // compared all of its bytes (on the PS5, 20 ms more a frame from the + // first such write on). Only that buffer is compared in full from now on; + // the session-wide fallback is kept for a title doing it everywhere. + buffer_full_validation_handles_.insert(handle); + buffer_full_validation_count_.store(uint32_t(buffer_full_validation_handles_.size()), + std::memory_order_relaxed); + ForgetBufferCapture(handle); + constexpr size_t kMaximumFullValidationBuffers = 512; + if (buffer_full_validation_handles_.size() > kMaximumFullValidationBuffers && + ShouldDisableNativeBufferFastPath(true, false) && !buffer_fast_path_disabled_.exchange(true, std::memory_order_relaxed)) { buffer_fast_path_disable_count_.fetch_add(1, std::memory_order_relaxed); REXLOG_ERROR( - "gta4-native: buffer shadow mismatch for {:08X}; disabling clean-buffer fast path " - "for this session; backing={:08X} size={} flags={:08X} generation={} " + "gta4-native: {} buffers changed without an unlock; disabling the clean-buffer fast " + "path for this session", buffer_full_validation_handles_.size()); + } else if (buffer_full_validation_handles_.size() <= 32) { + REXLOG_WARN( + "gta4-native: buffer {:08X} changed without an unlock; comparing it in full from now " + "on ({} such buffers); backing={:08X} size={} flags={:08X} generation={} " "validation-offset={} validation-length={} captured-frame={} last-used-frame={}", - handle, data_address, data_size, flags, clean_cache_entry->generation, - validation_range.offset, validation_range.length, clean_cache_entry->created_frame, + handle, buffer_full_validation_handles_.size(), data_address, data_size, flags, + clean_cache_entry->generation, validation_range.offset, validation_range.length, + clean_cache_entry->created_frame, clean_cache_entry->last_used_frame.load(std::memory_order_relaxed)); } } + ++buffer_capture_stats_.copies; + buffer_capture_stats_.copy_bytes += data_size; std::vector<uint8_t> payload(data, data + data_size); const uint64_t content_hash = XXH3_64bits(payload.data(), payload.size()); std::lock_guard lock(buffer_resource_mutex_); @@ -5436,7 +6056,11 @@ SurfaceDescriptor Gta4NativeGraphicsSystem::CaptureSurfaceDescriptor(uint32_t ha if (!handle) { return descriptor; } +#if REX_PLATFORM_PS5 + const uint8_t* object = CaptureObjectMemory(handle); // (as at the draw, on the capture thread) +#else const uint8_t* object = memory_->TranslateVirtual<const uint8_t*>(handle); +#endif if (!object) { REXLOG_WARN("gta4-native-surface: unable to translate snapshot surface {:08X}", handle); return descriptor; @@ -5460,19 +6084,43 @@ SurfaceDescriptor Gta4NativeGraphicsSystem::CaptureSurfaceDescriptor(uint32_t ha std::shared_ptr<const Gta4NativeGraphicsSystem::NativeTextureResource> Gta4NativeGraphicsSystem::CaptureTextureResource(uint32_t handle, const xenos::xe_gpu_texture_fetch_t& fetch, - uint32_t stage) { + uint32_t stage, bool hold, bool* held) { // Sampler words (filters, clamps, LOD bias) change between draws of the same // image and select neither the resource nor its Prepare shape. const std::array<uint32_t, 6> fetch_words = NativeTextureImageFetchKey(fetch); TextureCaptureCacheSlot& slot = TextureCaptureSlot(handle); - const uint64_t epoch = texture_resources_epoch_.load(std::memory_order_acquire); + const uint32_t epoch = uint32_t(texture_resources_epoch_.load(std::memory_order_acquire)); if (slot.handle == handle && slot.epoch == epoch && slot.fetch == fetch_words && stage < kShaderTextureCount) { - if (auto cached = slot.resource.lock()) { - return cached; + // [ps5 port] Lean capture: as in CaptureBufferResource, a resource held + // for this frame is handed out as a plain pointer. + if (hold && slot.hold_epoch == hold_epoch_ && slot.held && + !__atomic_load_n(&slot.held->dropped, __ATOMIC_ACQUIRE)) { + ++texture_capture_stats_front_hits_; + ++held_uses_; + if (held) *held = true; + return std::shared_ptr<const NativeTextureResource>(std::shared_ptr<const NativeTextureResource>(), + slot.held); + } + if (auto cached = slot.resource.lock(); + cached && !__atomic_load_n(&cached->dropped, __ATOMIC_ACQUIRE)) { + ++texture_capture_stats_front_hits_; + if (!hold) return cached; + const NativeTextureResource* const resource = cached.get(); + slot.held = resource; + slot.hold_epoch = hold_epoch_; + held_resources_.push_back(std::move(cached)); + ++holds_taken_; + ++held_uses_; + if (held) *held = true; + return std::shared_ptr<const NativeTextureResource>(std::shared_ptr<const NativeTextureResource>(), + resource); } slot.handle = 0; + slot.held = nullptr; + slot.hold_epoch = 0; } + ++texture_capture_stats_lookups_; bool cacheable = false; auto resource = CaptureTextureResourceUncached(handle, fetch, stage, cacheable); if (cacheable && resource) { @@ -5480,6 +6128,8 @@ Gta4NativeGraphicsSystem::CaptureTextureResource(uint32_t handle, slot.fetch = fetch_words; slot.epoch = epoch; slot.resource = resource; + slot.held = nullptr; + slot.hold_epoch = 0; } return resource; } @@ -5618,7 +6268,7 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( } texture_resources_[handle] = alias; dirty_texture_handles_.erase(handle); - REXLOG_INFO("gta4-native-virtual-resource: point=packed-depth-alias-publish " + REXLOG_DEBUG("gta4-native-virtual-resource: point=packed-depth-alias-publish " "handle={:08X}@{} source={:08X}@{} lifetime={} logical={}x{}", handle, alias->generation, source->handle, source->generation, alias->virtual_lifetime, info.width + 1, info.height + 1); @@ -5668,7 +6318,7 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( handle, deterministic->generation, 0, 0, 0, 0, g_native_memory_profile_event_frame.load(std::memory_order_relaxed), uint32_t(deterministic->info.format)); - REXLOG_INFO( + REXLOG_DEBUG( "gta4-native-virtual-resource: point=pre-resolve-generation handle={:08X} " "lifetime={} generation={} backing={}x{} logical={}x{} physical={}x{} " "format={} initialization=transparent-black", @@ -5688,7 +6338,7 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( static std::atomic<uint64_t> guest_capture_count{0}; const uint64_t capture = NextNativeTraceDiagnosticCount(guest_capture_count); if (capture <= 64 || !(capture % 4096)) { - REXLOG_INFO( + REXLOG_DEBUG( "gta4-native-virtual-resource: point=capture-complete-guest-backing " "index={} stage={} handle={:08X} lifetime={} backing={}x{} logical={}x{} " "format={}", @@ -5984,8 +6634,9 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( job.packed_y = packed_offset_y; job.destination_pitch = host_extent.block_pitch_h; job.destination_slice = host_extent.block_height * host_extent.block_pitch_h; - raw_payload.resize(job.raw_offset + size_t(span)); - std::memcpy(raw_payload.data() + job.raw_offset, layer_source, size_t(span)); + // (the alignment gap zeroed, the layer copied once: not zeroed first) + raw_payload.resize(job.raw_offset); + raw_payload.insert(raw_payload.end(), layer_source, layer_source + size_t(span)); detile_jobs.push_back(job); } } @@ -6218,6 +6869,7 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( const bool replacing = existing != texture_resources_.end() && existing->second; texture_resources_[handle] = resource; dirty_texture_handles_.erase(handle); + if (!virtual_handle && !vector_font && !vector_font_id) WatchTexture(resource); RecordNativeMemoryLifecycle( memory::ResourceKind::kTextureResource, replacing ? memory::LifecycleAction::kReplace : memory::LifecycleAction::kCreate, @@ -6228,6 +6880,123 @@ Gta4NativeGraphicsSystem::CaptureTextureResourceUncached( return resource; } +// Eight 32-byte pieces spread over the texture's memory: a different texture +// in the same place differs in all of them. +uint64_t Gta4NativeGraphicsSystem::ProbeGuestTexture(uint32_t address, uint32_t size) const { + const uint8_t* bytes = memory_ ? memory_->TranslatePhysical<const uint8_t*>(address) : nullptr; + if (!bytes || !size) return 0; + constexpr uint32_t kPiece = 32, kPieces = 8; + if (size <= kPiece * kPieces) return XXH3_64bits(bytes, size); + std::array<uint8_t, kPiece * kPieces> sample; + const uint32_t step = ((size - kPiece) / (kPieces - 1)) & ~(kPiece - 1); + for (uint32_t i = 0; i < kPieces; ++i) std::memcpy(sample.data() + i * kPiece, bytes + i * step, kPiece); + return XXH3_64bits(sample.data(), sample.size()); +} + +// Called with texture_resource_mutex_ held, for a new guest-memory capture. +void Gta4NativeGraphicsSystem::WatchTexture(const std::shared_ptr<const NativeTextureResource>& resource) { + if (!REXCVAR_GET(gta4_native_texture_watch) || REXCVAR_GET(protect_on_release)) return; + const TextureInfo& info = resource->info; + // The memory read for the largest level captured. + const bool base = info.mip_min_level == 0 && info.memory.base_size; + const uint32_t address = base ? info.memory.base_address : info.memory.mip_address; + const uint32_t size = base ? info.memory.base_size : info.memory.mip_size; + if (!address || !size) return; + WatchedTexture watched; + watched.resource = resource; + watched.handle = resource->handle; + watched.generation = resource->generation; + watched.address = address; + watched.size = size; + watched.probe = ProbeGuestTexture(address, size); + std::lock_guard lock(texture_watch_mutex_); + if (texture_watch_stop_) return; + texture_watch_added_.push_back(std::move(watched)); + if (!texture_watch_thread_.joinable()) + texture_watch_thread_ = std::thread([this] { TextureWatchMain(); }); +} + +void Gta4NativeGraphicsSystem::TextureWatchMain() { + std::vector<WatchedTexture> watched; + std::vector<ChangedTexture> changed; + // [ps5 port] Off the renderer threads' cores (it was started from the game's + // render thread and would otherwise share its CPU). + uint64_t cpus_applied = UINT64_MAX; + std::unique_lock lock(texture_watch_mutex_); + while (!texture_watch_stop_) { + rex::frame_stats::FollowRendererCpuMask(cpus_applied, rex::frame_stats::RendererRole::kOther); + // Ten passes a second (a pass over a few thousand textures reads ~1 MB): + // the map's tiles, streamed in where others were as it zooms, are wrong + // for a moment at most. + texture_watch_condition_.wait_for(lock, std::chrono::milliseconds(100), + [this] { return texture_watch_stop_; }); + if (texture_watch_stop_) break; + for (WatchedTexture& added : texture_watch_added_) watched.push_back(std::move(added)); + texture_watch_added_.clear(); + lock.unlock(); + changed.clear(); + size_t kept = 0; + for (size_t i = 0; i < watched.size(); ++i) { + WatchedTexture& entry = watched[i]; + // Gone from the cache and from every queued frame: nothing to correct. + if (entry.resource.expired()) continue; + const uint64_t probe = ProbeGuestTexture(entry.address, entry.size); + if (probe != entry.probe) { + entry.probe = probe; + changed.push_back({entry.handle, entry.generation}); + } + if (kept != i) watched[kept] = std::move(entry); + ++kept; + } + watched.resize(kept); + lock.lock(); + if (!changed.empty()) { + // (once each, however many passes ran before the next present) + texture_watch_changed_.insert(texture_watch_changed_.end(), changed.begin(), changed.end()); + std::sort(texture_watch_changed_.begin(), texture_watch_changed_.end()); + texture_watch_changed_.erase(std::unique(texture_watch_changed_.begin(), texture_watch_changed_.end()), + texture_watch_changed_.end()); + texture_watch_changed_pending_.store(true, std::memory_order_release); + } + } +} + +// The producer, once a frame: the next capture of each changed texture takes +// the full path, which re-reads it and keeps the cached image if it matches. +void Gta4NativeGraphicsSystem::TakeChangedTextures() { + if (!texture_watch_changed_pending_.load(std::memory_order_acquire)) return; + std::vector<ChangedTexture> changed; + { + std::lock_guard lock(texture_watch_mutex_); + changed.swap(texture_watch_changed_); + texture_watch_changed_pending_.store(false, std::memory_order_relaxed); + } + if (changed.empty()) return; + std::lock_guard lock(texture_resource_mutex_); + uint64_t marked = 0; + for (const ChangedTexture& change : changed) { + // Only the capture that was watched, still current and still guest-owned + // (not since replaced, nor turned into a resolve target). + const auto current = texture_resources_.find(change.handle); + if (current == texture_resources_.end() || !current->second || + current->second->generation != change.generation || current->second->gpu_produced) + continue; + dirty_texture_handles_.insert(change.handle); + ForgetTextureCapture(change.handle); + ++marked; + } + texture_watch_changes_.fetch_add(marked, std::memory_order_relaxed); +} + +void Gta4NativeGraphicsSystem::StopTextureWatch() { + { + std::lock_guard lock(texture_watch_mutex_); + texture_watch_stop_ = true; + } + texture_watch_condition_.notify_all(); + if (texture_watch_thread_.joinable()) texture_watch_thread_.join(); +} + void Gta4NativeGraphicsSystem::StartRenderWorker() { if (render_worker_running_.exchange(true)) { return; @@ -6246,8 +7015,8 @@ void Gta4NativeGraphicsSystem::BeginModernShaderFrame() { postfx_filtered_scene_.reset(); // Registry queries synchronize with frontend writers. One complete title // frame sees one filtering policy; no per-draw string copies or mixed modes. - const auto filtering = rex::cvar::Query<std::string>("gta4_texture_filtering"); - const auto anisotropy = rex::cvar::Query<std::string>("gta4_anisotropic_filtering"); + const auto filtering = CachedQuery<std::string>("gta4_texture_filtering"); + const auto anisotropy = CachedQuery<std::string>("gta4_anisotropic_filtering"); if (filtering != active_texture_filtering_) { active_texture_filtering_ = filtering; texture_filtering_trace_pending_ = true; } @@ -6314,6 +7083,23 @@ void Gta4NativeGraphicsSystem::TraceModernShaderDraw( } void Gta4NativeGraphicsSystem::RenderWorkerMain() { +#if REX_PLATFORM_PS5 + rex::ps5diag::RegisterThread("worker"); + rex::frame_stats::g_renderer_cpu_mask.store(rex::frame_stats::RendererCpuMaskFor(REXCVAR_GET(ps5_renderer_cpus)), + std::memory_order_relaxed); + rex::frame_stats::g_renderer_cpu_compare.store(REXCVAR_GET(ps5_renderer_cpus) == "compare", + std::memory_order_relaxed); + rex::frame_stats::FollowRendererCpuMask(worker_cpu_mask_applied_, rex::frame_stats::RendererRole::kWorker); + rex::frame_stats::g_lean_capture.store(REXCVAR_GET(ps5_lean_capture) != "off", + std::memory_order_relaxed); + rex::frame_stats::g_capture_on_core.store(REXCVAR_GET(ps5_lean_capture) == "core" || + REXCVAR_GET(ps5_lean_capture) == "compare", + std::memory_order_relaxed); + rex::frame_stats::g_capture_core_compare.store(REXCVAR_GET(ps5_lean_capture) == "compare", + std::memory_order_relaxed); + rex::frame_stats::g_capture_cpu_reserved.store(rex::frame_stats::g_capture_on_core.load(std::memory_order_relaxed), + std::memory_order_relaxed); +#endif std::vector<std::pair<RenderPhase, uint32_t>> render_phase_stack; uint64_t startup_texture_lock_count = 0; bool startup_present_follows_texture_lock_flush = false; @@ -6479,7 +7265,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { bound.read_epoch = read_epoch; const auto mark_read = [read_epoch](const NativeTextureResource* resource) { if (resource) - std::atomic_ref(resource->assembly_read_epoch).store(read_epoch, std::memory_order_relaxed); + __atomic_store_n(&resource->assembly_read_epoch, read_epoch, __ATOMIC_RELAXED); // ([ps5 port] libc++ 18: no std::atomic_ref) }; for (const auto& texture : assembled.textures) { if (!texture) continue; @@ -6557,8 +7343,17 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { std::lock_guard protection_lock(worker_protection_mutex_); command_owner = worker_command_batch_.Take(worker_command_cursor_++); active_worker_command_ = command_owner.get(); + // [ps5 port] Lean capture: owning references before anything else here + // can copy one (under the lock the protection scans take). + if (command_owner->held_resource_mask) UpgradeHeldResources(*command_owner); } NativeCommand& command = *command_owner; + // [ps5 port] The frame's holds, carried by its present: every command + // before it has been taken (and upgraded) by now. + if (!command.released_holds.empty()) { + std::vector<std::shared_ptr<const void>> released = std::move(command.released_holds); + command.released_holds.clear(); + } // Commands are written on the guest core and are ~3 KB each; pulling the // next one in while this one records hides the cross-core misses. if (worker_command_cursor_ < worker_command_batch_.size()) { @@ -6621,7 +7416,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { if (recorder_sync) SwapAssemblyFrame(); }); - if (command.tv_trace) { + if ((command.trace_flags & NativeCommand::kTraceTv) && command.tv_trace) { if (!tv_lifecycle_trace_ || tv_lifecycle_trace_->run != command.tv_trace->run) tv_lifecycle_plane_handles_.clear(); tv_lifecycle_trace_ = command.tv_trace; @@ -6681,9 +7476,11 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { texture_resources_.clear(); dirty_texture_handles_.clear(); virtual_resource_registry_.Clear(); + BumpSurfaceLookupEpoch(); texture_resources_epoch_.fetch_add(1, std::memory_order_release); } reflection_resources_.clear(); + BumpSurfaceLookupEpoch(); break; case CommandType::kRegisterShader: { RegisterShaderCommand register_shader; @@ -6727,6 +7524,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { target.sample_count_override = registration.sample_count_override; const size_t retired_aliases = RegisterNativeReflectionTarget(reflection_resources_, target); + BumpSurfaceLookupEpoch(); const NativeSurfaceImage* cached_surface = nullptr; for (const auto& image : native_surface_images_) { if (image->descriptor.handle == registration.surface) { @@ -6807,6 +7605,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { } const size_t retired_aliases = EraseNativeReflectionRegistration(reflection_resources_, release.resource); + BumpSurfaceLookupEpoch(); // Draws already queued ahead of Release still own the surface. If the // frame has not been submitted, defer retirement until the next real // submission instead of forcing an extra present at the release site. @@ -6834,7 +7633,8 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { break; } case CommandType::kTextureLock: { - if (command.phone_trace) TracePhoneNativeCommand("worker-sync", command); + if ((command.trace_flags & NativeCommand::kTracePhone) && command.phone_trace) + TracePhoneNativeCommand("worker-sync", command); TextureLockCommand lock_command; std::memcpy(&lock_command, command.bytes.data(), sizeof(lock_command)); { @@ -6976,7 +7776,10 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { // Hand the assembled frame to the recorder and start on the next. MarkFrameTimeline(present.submitted_frame, kTimelineWorkerPresent); const uint64_t handoff_wait_begin = profile::CpuTick(); + const int64_t handoff_wait_ns = rex::frame_stats::NowNs(); WaitForFrameRecorderIdle(); + rex::frame_stats::g_handoff_wait_us.fetch_add( + uint64_t(rex::frame_stats::NowNs() - handoff_wait_ns) / 1000, std::memory_order_relaxed); pipeline_present_wait_ticks_ += profile::CpuTick() - handoff_wait_begin; MarkFrameTimeline(present.submitted_frame, kTimelineWorkerHandoff); SwapAssemblyFrame(); @@ -6984,7 +7787,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { recording_environmental_data_by_device_ = environmental_data_by_device_; assembly_modern_frame_pending_ = false; log_frame_batch("present", command, present.submitted_frame, queued_after_pop); -#if REX_PLATFORM_ANDROID +#if REX_PLATFORM_ANDROID || REX_PLATFORM_PS5 // TEMP: pipeline stall telemetry (perf investigation). if (++pipeline_report_frames_ == 120) { const double ms_per_tick = 1000.0 / double(rex::chrono::Clock::QueryHostTickFrequency()); @@ -6993,7 +7796,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { if (pipeline_sync_type_counts_[i]) types += fmt::format(" {}={}", CommandTypeName(CommandType(i)), pipeline_sync_type_counts_[i]); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "pipeline: 120 frames sync-waits=%llu sync-wait=%.1fms/f " "handoff-wait=%.1fms/f dd-factor=%.2f ring=%.0f/%.0f " "ring-check=%llu/%llu types:%s", @@ -7005,7 +7808,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { g_worker_ring_fallbacks.exchange(0) / 120.0, (unsigned long long)g_worker_ring_mismatches.exchange(0), (unsigned long long)g_worker_ring_checked.exchange(0), types.c_str()); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "vk: %s ubo-shaders=%u ubo-loads=%llu legacy-mul=%llu", ui::vulkan::ConsumeCommandCounters(120).c_str(), ubo_rewritten_shaders_, (unsigned long long)ubo_rewritten_loads_, @@ -7016,7 +7819,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { while (begin < report.size()) { size_t end = report.find('\n', begin); if (end == std::string::npos) end = report.size(); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "pipeline-stats: %s", + PerfReport( "pipeline-stats: %s", report.substr(begin, std::min<size_t>(end - begin, 1000)).c_str()); begin = end + 1; } @@ -7024,7 +7827,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { if (gpu_counters_.ready()) { const std::string report = gpu_counters_.Report(); for (size_t offset = 0; offset < report.size(); offset += 900) - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "gpu-counters: %s", + PerfReport( "gpu-counters: %s", report.substr(offset, 900).c_str()); } { @@ -7035,7 +7838,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { std::string top; for (size_t i = 0; i < sites.size() && i < 12; ++i) top += fmt::format(" L{}={:.0f}", sites[i].second, sites[i].first / 120.0); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "rendering-ends/frame:%s", top.c_str()); + PerfReport( "rendering-ends/frame:%s", top.c_str()); } { std::vector<std::pair<uint32_t, uint32_t>> sites; @@ -7045,7 +7848,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { std::string top; for (size_t i = 0; i < sites.size() && i < 12; ++i) top += fmt::format(" L{}={:.0f}", sites[i].second, sites[i].first / 120.0); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "barriers/frame:%s", top.c_str()); + PerfReport( "barriers/frame:%s", top.c_str()); } { std::vector<std::pair<uint32_t, uint32_t>> sites; @@ -7065,10 +7868,10 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { for (const auto& [key, n] : kinds) if (n >= 60) line += fmt::format(" [{}]={:.1f}", key, n / 120.0); for (size_t offset = 0; offset < line.size(); offset += 900) - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "resolve-kinds:%s", + PerfReport( "resolve-kinds:%s", line.substr(offset, 900).c_str()); } - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "skips/frame: invisible-draws=%.1f repeated-clears=%.1f " "water-reflection=%.2f water-culled=%.1f shadow-culled=%.1f " "lazy-resolves=%.1f " @@ -7101,11 +7904,11 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { if (const uint32_t n = g_present_surface_rejects[i].exchange(0)) a11 += fmt::format(" r{}={:.2f}", i, n / 120.0); } - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "swap-rejects/frame:%s | present-rejects/frame:%s", a2.c_str(), a11.c_str()); } - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "vertex-cache/frame: eligible draws=%.0f indices=%.0f rejected draws=%.0f indices=%.0f " "reordered draws=%.0f", g_vertex_cache_draws[1].exchange(0) / 120.0, @@ -7113,7 +7916,7 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { g_vertex_cache_draws[0].exchange(0) / 120.0, g_vertex_cache_indices[0].exchange(0) / 120.0, g_vertex_cache_reordered_draws.exchange(0) / 120.0); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", + PerfReport( "resolves/frame:%s reuse=%.1f/%.1f", top.c_str(), g_resolve_reuse_hits.exchange(0) / 120.0, g_resolve_reuse_candidates.exchange(0) / 120.0); @@ -7126,13 +7929,13 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { std::string top; for (size_t i = 0; i < reasons.size() && i < 10; ++i) top += fmt::format(" {:03X}={:.0f}", reasons[i].second, reasons[i].first / 120.0); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "split-reasons/frame:%s", top.c_str()); + PerfReport( "split-reasons/frame:%s", top.c_str()); } { std::string top; for (uint32_t r = 0; r < g_light_reuse_failures.size(); ++r) if (const uint32_t n = g_light_reuse_failures[r].exchange(0)) top += fmt::format(" f{}={:.0f}", r, n / 120.0); - __android_log_print(ANDROID_LOG_WARN, "LibertyRecomp", "light-reuse/frame:%s", top.c_str()); + PerfReport( "light-reuse/frame:%s", top.c_str()); } pipeline_report_frames_ = 0; pipelined_sync_waits_ = 0; @@ -7205,7 +8008,8 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { }; fixed.depth_clamp_enable = ShouldEnableNativeDepthClampForDraw(fixed.depth_clamp_enable != 0, - IsLocalStencilSetup(command.lighting), + IsLocalStencilSetup(command.lighting) && + REXCVAR_GET(ps5_light_test) != "no-clamp", stencil_volume_state) ? 1u : 0u; @@ -7218,8 +8022,9 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { command.type == CommandType::kDrawPrimitiveUp || command.type == CommandType::kDrawIndexedPrimitive || command.type == CommandType::kClear) { - command.recorded_fixed_function_state_hash = - HashFixedFunctionState(command.fixed_function_state); + if (VerifyFixedStateSnapshots()) + command.recorded_fixed_function_state_hash = + HashFixedFunctionState(command.fixed_function_state); const uint32_t device = CommandDevice(command.type, command.bytes.data()); if (!ApplyShaderConstantDelta(command, device)) { REXLOG_ERROR("gta4-native-constants: rejected worker command type={} device={:08X}", @@ -7254,7 +8059,8 @@ void Gta4NativeGraphicsSystem::RenderWorkerMain() { command.type == CommandType::kDrawIndexedPrimitive)) { TryPrewarmDrawPipeline(command); } - if (command.phone_trace) TracePhoneNativeCommand("worker-finalized", command); + if ((command.trace_flags & NativeCommand::kTracePhone) && command.phone_trace) + TracePhoneNativeCommand("worker-finalized", command); retain_assembled(command_owner, true); startup_present_follows_texture_lock_flush = false; break; @@ -7309,6 +8115,22 @@ void Gta4NativeGraphicsSystem::FinishPresent(NativeCommand& command, UpdateDynamicDrawDistance(); } +#if REX_PLATFORM_PS5 +// [ps5 port] An allocation failed for want of memory: evict unused textures +// under pressure for a while and hand back what the memory recycler holds. +void Gta4NativeGraphicsSystem::NotePs5MemoryShort(const char* what) { + ps5_memory_short_frames_ = 600; + texture_budget_pressure_active_ = true; + texture_eviction_remaining_ = std::max(texture_eviction_remaining_, texture_eviction_scan_.size()); + ui::vulkan::MemoryRecyclerReleaseAll(); + static uint32_t logged = 0; + if (logged++ < 16) { + REXLOG_WARN("ps5 memory: a {} allocation failed with {} texture images; evicting unused textures", + what, native_texture_images_.size()); + } +} +#endif + void Gta4NativeGraphicsSystem::UpdateDynamicDrawDistance() { // Steers gta4_draw_distance_dynamic_factor from the presented frame rate, // measured over one-second windows. Short streaming hitches and single @@ -7316,7 +8138,7 @@ void Gta4NativeGraphicsSystem::UpdateDynamicDrawDistance() { // between 1.0 and the minimum every ~30 s: it now drops only while two // windows in a row miss the 30 Hz target clearly, in small spaced steps, // and climbs back once the last three seconds average near the target. - if (rex::cvar::GetFlagByName("gta4_dynamic_draw_distance") != "true") { + if (CachedFlagByName("gta4_dynamic_draw_distance") != "true") { dynamic_draw_distance_factor_ = 1.0; return; } @@ -7331,7 +8153,7 @@ void Gta4NativeGraphicsSystem::UpdateDynamicDrawDistance() { dynamic_draw_distance_interval_ms_ * 0.9 + std::min(interval_ms, 100.0) * 0.1; double minimum = 0.7; try { - minimum = std::clamp(std::stod(rex::cvar::GetFlagByName("gta4_dynamic_draw_distance_min")), 0.25, 1.0); + minimum = std::clamp(std::stod(CachedFlagByName("gta4_dynamic_draw_distance_min")), 0.25, 1.0); } catch (...) { } double factor = dynamic_draw_distance_factor_; @@ -7444,6 +8266,12 @@ void Gta4NativeGraphicsSystem::SignalGuestFrameComplete(uint32_t device, uint32_ void Gta4NativeGraphicsSystem::FrameRecorderMain() { #if REX_PLATFORM_ANDROID pthread_setname_np(pthread_self(), "GtaRecorder"); +#endif +#if REX_PLATFORM_PS5 + rex::ps5diag::RegisterThread("recorder"); + rex::frame_stats::g_renderer_cpu_mask.store(rex::frame_stats::RendererCpuMaskFor(REXCVAR_GET(ps5_renderer_cpus)), + std::memory_order_relaxed); + rex::frame_stats::FollowRendererCpuMask(recorder_cpu_mask_applied_, rex::frame_stats::RendererRole::kRecorder); #endif while (true) { std::optional<NativeRecorderJob> job; @@ -7456,11 +8284,25 @@ void Gta4NativeGraphicsSystem::FrameRecorderMain() { frame_recorder_job_.reset(); } const uint32_t timeline_frame = job->present.submitted_frame; +#if REX_PLATFORM_PS5 + rex::frame_stats::NoteCpu(rex::frame_stats::g_cpus_recorder); + rex::frame_stats::FollowRendererCpuMask(recorder_cpu_mask_applied_, rex::frame_stats::RendererRole::kRecorder); +#endif recording_ring_serial_ = job->ring_serial; MarkFrameTimeline(timeline_frame, kTimelineRecorderStart); + const int64_t recorder_begin_ns = rex::frame_stats::NowNs(); + rex::frame_stats::g_frame_pacer_us.store(0, std::memory_order_relaxed); RunReleaseEffects(recording_release_effects_); BeginModernShaderFrame(); FinishPresent(*job->present_command, job->present); + { + // Its work: the frame's time less the frame limit's hold (presenter). + const uint64_t total_us = uint64_t(rex::frame_stats::NowNs() - recorder_begin_ns) / 1000; + const uint64_t paced_us = rex::frame_stats::g_frame_pacer_us.load(std::memory_order_relaxed); + const uint64_t busy_us = total_us > paced_us ? total_us - paced_us : 0; + rex::frame_stats::g_last_recorder_busy_us.store(uint32_t(busy_us), std::memory_order_relaxed); + rex::frame_stats::NoteRecorderFrame(busy_us); + } MarkFrameTimeline(timeline_frame, kTimelinePublishEnd); LogFrameTimeline(timeline_frame); job.reset(); @@ -7488,6 +8330,10 @@ void Gta4NativeGraphicsSystem::StopFrameRecorder() { } void Gta4NativeGraphicsSystem::SwapAssemblyFrame() { +#if REX_PLATFORM_PS5 + rex::frame_stats::NoteCpu(rex::frame_stats::g_cpus_worker); + rex::frame_stats::FollowRendererCpuMask(worker_cpu_mask_applied_, rex::frame_stats::RendererRole::kWorker); +#endif // Only while the recorder is idle: current_frame_ and its protection belong // to the recorder otherwise. current_frame_.swap(assembly_frame_); @@ -7527,6 +8373,7 @@ void Gta4NativeGraphicsSystem::ApplyReleaseResourceEffects(const NativeReleaseEf } const size_t retired_aliases = EraseNativeReflectionRegistration(reflection_resources_, release.resource); + BumpSurfaceLookupEpoch(); if (effect.frame_was_empty) { QueueSurfaceImageRelease(release.resource); } @@ -7571,13 +8418,23 @@ void Gta4NativeGraphicsSystem::ApplyStateCommand(const NativeCommand& command) { } void Gta4NativeGraphicsSystem::ApplyPendingStateCommands(const std::vector<uint8_t>& pending) { + uint64_t applied = 0, repeats = 0; for (size_t offset = 0; offset + sizeof(CommandHeader) <= pending.size();) { CommandHeader header; std::memcpy(&header, pending.data() + offset, sizeof(header)); if (header.size < sizeof(CommandHeader) || header.size > pending.size() - offset) break; + const uint64_t version = pipeline_state_.version; ApplyStateCommandBytes(header.type, pending.data() + offset); + ++applied; + repeats += pipeline_state_.version == version; offset += header.size; } + // [ps5 port] How many of the game's state changes set what was already set + // (for the capture line); this thread is the only writer. + state_commands_applied_.store(state_commands_applied_.load(std::memory_order_relaxed) + applied, + std::memory_order_relaxed); + state_commands_repeated_.store(state_commands_repeated_.load(std::memory_order_relaxed) + repeats, + std::memory_order_relaxed); } void Gta4NativeGraphicsSystem::RegisterVectorFontTexture(uint32_t texture, uint32_t vector_font_id) { @@ -7646,6 +8503,9 @@ bool Gta4NativeGraphicsSystem::CoalesceStateCommand(const void* command, size_t break; } const auto* bytes = static_cast<const uint8_t*>(command); + // The buffer comes from the last command it was handed to (a fresh one has + // none): one allocation up front rather than growing through 16, 32, 64... + if (producer_pending_state_.capacity() < 256) producer_pending_state_.reserve(256); producer_pending_state_.insert(producer_pending_state_.end(), bytes, bytes + command_size); return true; } @@ -8974,7 +9834,10 @@ void Gta4NativeGraphicsSystem::DestroyShaderResources() { { std::lock_guard capture_lock(command_capture_mutex_); producer_shader_metadata_.clear(); + ++producer_shader_metadata_generation_; producer_device_shader_states_.clear(); + producer_last_shader_device_ = 0; + producer_last_shader_state_ = nullptr; } pixel_shaders_by_hash_.clear(); vertex_shaders_by_hash_.clear(); @@ -9321,9 +10184,23 @@ bool Gta4NativeGraphicsSystem::GetOrCreatePersistentBuffer( return false; } const NativeBufferArenaAllocation& reserved = reservation.allocation; +#if REX_PLATFORM_PS5 + // [ps5 port] Out of memory, a new block was attempted for every draw, each + // a failing kernel allocation: frames took seconds. Wait a while instead, + // with texture eviction under pressure meanwhile; draws use the upload ring. + if (reserved.new_block && ps5_persistent_block_retry_frame_ && + active_texture_frame_ < ps5_persistent_block_retry_frame_) { + persistent_buffer_arena_->Cancel(reserved.id); + return false; + } +#endif if (reserved.new_block && !CreateNativePersistentBufferBlock( reserved.block_id, VkDeviceSize(reserved.block_capacity))) { persistent_buffer_arena_->Cancel(reserved.id); +#if REX_PLATFORM_PS5 + ps5_persistent_block_retry_frame_ = active_texture_frame_ + 120; + NotePs5MemoryShort("persistent buffer block"); +#endif return false; } const auto block_entry = persistent_buffer_blocks_.find(reserved.block_id); @@ -9911,7 +10788,10 @@ bool Gta4NativeGraphicsSystem::CompleteNativeFrameSlot(uint32_t slot, uint64_t s gpu_flight::Record("native.slot-wait-begin", slot, submission, active_texture_frame_); const uint64_t wait_begin = actual_wait_ticks ? rex::chrono::Clock::QueryHostTickCount() : 0; + const int64_t gpu_wait_begin_ns = rex::frame_stats::NowNs(); const bool completed = profile::CpuCall(profile::CpuOp::kFenceWait, [&] { return submission_tracker_->AwaitSubmissionCompletion(submission); }); + rex::frame_stats::g_gpu_wait_us.fetch_add( + uint64_t(rex::frame_stats::NowNs() - gpu_wait_begin_ns) / 1000, std::memory_order_relaxed); if (actual_wait_ticks) { *actual_wait_ticks += rex::chrono::Clock::QueryHostTickCount() - wait_begin; } @@ -17209,6 +18089,24 @@ bool Gta4NativeGraphicsSystem::InitializeNativePipelineCache() { native_pipeline_cache_path_.string()); } } +#if REX_PLATFORM_PS5 + // [ps5 port] The pipelines a long session built, shipped with the app + // (/app0/shader_cache_seed, same layout): taken as the cache when there is + // none yet, merged into it otherwise, so first sights of a car or a street + // don't stop the frame to build their pipelines. Every PS5 has the same GPU + // and driver; a different one fails the identity check and is skipped. + std::vector<uint8_t> seed_data; + native_pipeline_seed_path_ = GetNativePipelineCachePath(kNativePipelineSeedRoot, identity); + if (auto data = ReadBinaryFile(native_pipeline_seed_path_); + data && ValidateNativePipelineCacheData(*data, identity)) { + if (initial_data.empty()) { + initial_data = std::move(*data); + native_pipeline_cache_seeded_ = true; + } else { + seed_data = std::move(*data); + } + } +#endif VkPipelineCacheCreateInfo create_info{}; create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; @@ -17230,10 +18128,36 @@ bool Gta4NativeGraphicsSystem::InitializeNativePipelineCache() { native_pipeline_cache_ = VK_NULL_HANDLE; return false; } +#if REX_PLATFORM_PS5 + if (!seed_data.empty()) { + VkPipelineCacheCreateInfo seed_info{}; + seed_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; + seed_info.initialDataSize = seed_data.size(); + seed_info.pInitialData = seed_data.data(); + VkPipelineCache seed_cache = VK_NULL_HANDLE; + const auto& dfn = vulkan_device->functions(); + if (dfn.vkCreatePipelineCache(vulkan_device->device(), &seed_info, nullptr, &seed_cache) == + VK_SUCCESS) { + // (saved again only when the seed brought pipelines this cache lacked) + size_t before = 0, after = 0; + dfn.vkGetPipelineCacheData(vulkan_device->device(), native_pipeline_cache_, &before, nullptr); + if (dfn.vkMergePipelineCaches(vulkan_device->device(), native_pipeline_cache_, 1, + &seed_cache) == VK_SUCCESS) { + dfn.vkGetPipelineCacheData(vulkan_device->device(), native_pipeline_cache_, &after, + nullptr); + native_pipeline_cache_seeded_ = after > before; + } + dfn.vkDestroyPipelineCache(vulkan_device->device(), seed_cache, nullptr); + } + } + // (saved at the next checkpoint, with the seed in it) + if (native_pipeline_cache_seeded_) native_pipeline_cache_generation_.fetch_add(1); +#endif REXLOG_INFO( - "gta4-native: Vulkan pipeline cache {} ({})", + "gta4-native: Vulkan pipeline cache {} ({}){}", initial_data.empty() ? "started empty" : "loaded", - native_pipeline_cache_path_.empty() ? "memory only" : native_pipeline_cache_path_.string()); + native_pipeline_cache_path_.empty() ? "memory only" : native_pipeline_cache_path_.string(), + native_pipeline_cache_seeded_ ? ", with the shipped pipelines" : ""); return true; } @@ -17329,9 +18253,28 @@ void Gta4NativeGraphicsSystem::LoadNativePipelineRecipes() { } auto path = native_pipeline_cache_path_; path += ".recipes"; + LoadNativePipelineRecipeFile(path); +#if REX_PLATFORM_PS5 + // [ps5 port] And the shipped ones (InitializeNativePipelineCache): the ones + // this console hasn't learned yet are prewarmed too, and written into its + // own file at the next checkpoint. + if (!native_pipeline_seed_path_.empty()) { + auto seed = native_pipeline_seed_path_; + seed += ".recipes"; + const size_t added = LoadNativePipelineRecipeFile(seed); + if (added) { + native_pipeline_cache_generation_.fetch_add(1); + REXLOG_INFO("gta4-native: {} shipped pipeline recipes added to this console's", added); + } + } +#endif +} + +size_t Gta4NativeGraphicsSystem::LoadNativePipelineRecipeFile(const std::filesystem::path& path) { + size_t added = 0; const auto bytes = ReadBinaryFile(path); if (!bytes || bytes->size() < sizeof(NativePipelineRecipeFileHeader) || bytes->size() > 16777216) { - return; + return 0; } NativePipelineRecipeFileHeader header{}; std::memcpy(&header, bytes->data(), sizeof(header)); @@ -17341,7 +18284,7 @@ void Gta4NativeGraphicsSystem::LoadNativePipelineRecipes() { header.count > NativePipelineCompilerState::kMaximumRecipes || payload_size != header.count * sizeof(NativePipelineCompilerState::Record) || header.checksum != XXH3_64bits(bytes->data() + sizeof(header), payload_size)) { - return; + return 0; } for (size_t offset = sizeof(header); offset < bytes->size(); offset += sizeof(NativePipelineCompilerState::Record)) { @@ -17356,9 +18299,11 @@ void Gta4NativeGraphicsSystem::LoadNativePipelineRecipes() { std::lock_guard lock(native_pipeline_compiler_->records_mutex); if (native_pipeline_compiler_->records.emplace(record.key, record.recipe).second) { native_pipeline_compiler_->replay.push_back(record); + ++added; } } } + return added; } void Gta4NativeGraphicsSystem::ReplayNativePipelineRecipes() { @@ -18451,6 +19396,9 @@ bool Gta4NativeGraphicsSystem::AllocateNativeTextureImage(const VkImageCreateInf return false; } ++texture_allocation_retry_count_; +#if REX_PLATFORM_PS5 + NotePs5MemoryShort("texture"); +#endif TrimTextureAllocationPool(true); // Allocation recovery is exceptional backpressure. Complete the other // slot so retired generations can release real device memory rather than @@ -18714,12 +19662,14 @@ Gta4NativeGraphicsSystem::NativeTextureImage* Gta4NativeGraphicsSystem::GetOrCre view_info.components.b = VK_COMPONENT_SWIZZLE_ONE; view_info.components.a = VK_COMPONENT_SWIZZLE_R; } else if (vulkan_device->properties().portabilitySubset || - !vulkan_device->properties().imageViewFormatSwizzle || REX_PLATFORM_ANDROID) { + !vulkan_device->properties().imageViewFormatSwizzle || + !REXCVAR_GET(gta4_native_guest_texture_swizzle)) { // Ordinary native portability views deliberately retain the established // identity policy even when font-only swizzle support is enabled. - // Android follows the same policy: the native renderer and its shader - // overrides are tuned against these identity views, and applying the guest - // swizzle instead swaps red and blue in GTA IV's ARGB light textures. + // Android and the PS5 follow the same policy (gta4_native_guest_texture_swizzle): + // the native renderer and its shader overrides are tuned against these + // identity views, and applying the guest swizzle instead swaps red and blue + // in GTA IV's ARGB light textures. host_swizzle = xenos::XE_GPU_TEXTURE_SWIZZLE_RGBA; view_info.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}; @@ -19587,7 +20537,13 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff cached_draws.reserve(draw_count); } - const bool prefetch_commands = REXCVAR_GET(gta4_native_recorder_prefetch); + // [ps5 port] On Game Thread Capture's Compare, off and on with the game's + // render thread's prefetching (rex::frame_stats::g_capture_prefetch). + const bool prefetch_commands = REXCVAR_GET(gta4_native_recorder_prefetch) +#if REX_PLATFORM_PS5 + && rex::frame_stats::g_capture_prefetch.load(std::memory_order_relaxed) +#endif + ; for (size_t command_index = 0; command_index < texture_commands.size(); ++command_index) { if (prefetch_commands && command_index + 4 < texture_commands.size()) { // See RecordNativeFrame: the walk is bound by first touches of commands. @@ -19598,6 +20554,34 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff __builtin_prefetch(&ahead.texture_fetches); __builtin_prefetch(&ahead.resolve_destination); __builtin_prefetch(&ahead.texture_descriptor_indices); + // [ps5 port] The temporal binding and texture-input signature line, + // read for every draw. + __builtin_prefetch(&ahead.temporal_scene_binding); + } + if (prefetch_commands) { + // [ps5 port] Then, for the used stages of the commands closer ahead, the + // chain a texture's binding walks, one link per command: the texture + // resource (three ahead), the prepared-image bucket its generation + // selects (two), the prepared image itself (one). + const auto for_used_textures = [&](size_t distance, auto&& visit) { + if (command_index + distance >= texture_commands.size()) return; + const NativeCommand& ahead = *texture_commands[command_index + distance]; + for (uint32_t mask = ahead.used_texture_mask; mask; mask &= mask - 1) { + const uint32_t stage = uint32_t(std::countr_zero(mask)); + if (stage >= kShaderTextureCount) break; + if (const auto* texture = ahead.textures[stage].get()) visit(*texture); + } + }; + for_used_textures(3, [](const NativeTextureResource& texture) { __builtin_prefetch(&texture); }); + for_used_textures(2, [&](const NativeTextureResource& texture) { + prepared_texture_images_.Prefetch(texture.generation); + }); + for_used_textures(1, [&](const NativeTextureResource& texture) { + if (NativeTextureImage* const* image = std::as_const(prepared_texture_images_).Find(texture.generation)) { + __builtin_prefetch(*image); + __builtin_prefetch(reinterpret_cast<const uint8_t*>(*image) + 64); + } + }); } NativeCommand& command = *texture_commands[command_index]; NativeTextureImage* resolve_destination_image = @@ -19634,7 +20618,7 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff source = cached; } if (source) { - CopyNativePreparedTextureBindings(*source, command); + CopyNativePreparedTextureBindings(*source, command, room_light_bindings_issued_); AddNativeGpuProfileCounter(performance::Counter::kTextureBindingReuses); previous_prepared_draw = source; continue; @@ -19669,7 +20653,9 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff command.sampler_descriptor_indices.fill(0); command.draw_descriptor_sets.fill(VK_NULL_HANDLE); command.binding_realization.fill(NativeBindingRealization::kUnused); - command.room_light_input_bindings.reset(); + // [ps5 port] Null unless a probe ever created one (a recycled command + // starts empty): skip the cold cache line otherwise. + if (room_light_bindings_issued_) command.room_light_input_bindings.reset(); const bool capture_bulb_bindings = EmissionTraceConfig().pipeline_full_readback && !bulb_full_frame_ && EmissionTraceActive(submitted_frame) && command.pipeline_state && command.pipeline_state->pixel_shader_resource && @@ -19680,9 +20666,14 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff (REXCVAR_GET(gta4_native_light_color_delta_probe) == "room" && !room_light_probe_stop_requested_ && command.pipeline_state && command.pipeline_state->pixel_shader_resource && IsDeferredLightShaderFilename(command.pipeline_state->pixel_shader_resource->filename))) { + room_light_bindings_issued_ = true; command.room_light_input_bindings = std::make_shared<std::vector<NativeRoomLightInputBinding>>(); } + // [ps5 port] Null unless a probe ever made one: the stage loop below then + // never reads the cold field. + std::vector<NativeRoomLightInputBinding>* const room_light_bindings = + room_light_bindings_issued_ ? command.room_light_input_bindings.get() : nullptr; command.realized_image_mask = 0; command.realized_sampler_mask = 0; command.guest_null_texture_mask = 0; @@ -19717,6 +20708,9 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff NativeSampler* sampler = nullptr; const uint32_t stage_bit = 1u << stage; const bool required = (command.used_texture_mask & stage_bit) != 0; + // [ps5 port] An unused stage keeps the null bindings set above and its + // kUnused realization (filled before the loop): nothing below applies. + if (!required) continue; NativeTextureImage* image = required ? prepared_image(command.textures[stage]) : nullptr; if (image && command.temporal_scene_binding && command.temporal_scene_stage == stage) @@ -19987,9 +20981,13 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff } } } + // [ps5 port] The guest handle decides only when there is no snapshot: + // otherwise skip the pipeline state's texture line. + const bool snapshot = command.textures[stage] != nullptr; + const bool guest_nonnull = !snapshot && command.pipeline_state && + command.pipeline_state->textures[stage] != 0; const NativeBindingRealization realization = SelectNativeBindingRealization( - required, command.pipeline_state && command.pipeline_state->textures[stage] != 0, - command.textures[stage] != nullptr, image && image->resource.view, + required, guest_nonnull, snapshot, image && image->resource.view, sampler && sampler->sampler); command.binding_realization[stage] = realization; if (image && image->resource.view) { @@ -20003,14 +21001,22 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff } if (NativeBindingFailed(realization)) { command.failed_texture_mask |= stage_bit; - REXLOG_ERROR( - "gta4-native-cause: point=required-binding-failed frame={} cmd={} stage={} " - "status={} handle={:08X} generation={} requested-mask={:08X} " - "image-mask={:08X} sampler-mask={:08X}", - submitted_frame, command_index, stage, NativeBindingRealizationName(realization), - command.pipeline_state ? command.pipeline_state->textures[stage] : 0, - command.textures[stage] ? command.textures[stage]->generation : 0, - command.used_texture_mask, command.realized_image_mask, command.realized_sampler_mask); + // (the first 64, then one in 4096 with the count: a texture that + // can't be made fails every frame it's drawn with, and the log + // filled at a few hundred lines a second) + static std::atomic<uint64_t> failures{0}; + const uint64_t failure = failures.fetch_add(1, std::memory_order_relaxed) + 1; + if (failure <= 64 || (failure % 4096) == 0) { + REXLOG_ERROR( + "gta4-native-cause: point=required-binding-failed frame={} cmd={} stage={} " + "status={} handle={:08X} generation={} requested-mask={:08X} " + "image-mask={:08X} sampler-mask={:08X} (failure {})", + submitted_frame, command_index, stage, NativeBindingRealizationName(realization), + command.pipeline_state ? command.pipeline_state->textures[stage] : 0, + command.textures[stage] ? command.textures[stage]->generation : 0, + command.used_texture_mask, command.realized_image_mask, command.realized_sampler_mask, + failure); + } continue; } if (native_descriptor_backend_ == NativeDescriptorBackend::kIndexed && !native_descriptor_paging_) { @@ -20026,8 +21032,8 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff command.texture_descriptor_indices[stage] = image_handle.index; command.sampler_descriptor_indices[stage] = sampler_handle.index; } - if (required && command.room_light_input_bindings) { - auto& binding = command.room_light_input_bindings->emplace_back(); + if (required && room_light_bindings) { + auto& binding = room_light_bindings->emplace_back(); binding.texture_stage = stage; binding.image = image ? image->resource.image : VK_NULL_HANDLE; binding.view = image ? image->resource.view : null_texture_2d_.view; @@ -20242,24 +21248,47 @@ bool Gta4NativeGraphicsSystem::PrepareFrameTextures(VkCommandBuffer command_buff } void Gta4NativeGraphicsSystem::AddProtectedTextureGenerations( - const NativeCommand& command, std::unordered_set<uint64_t>& generations) { + const NativeCommand& command, NativeGenerationSet& generations) { VisitProtectedTextureGenerations(command, [&](uint64_t generation) { if (generation) generations.insert(generation); }); } void Gta4NativeGraphicsSystem::CollectTextureProtection(const NativeCommand& command) { command.protected_generation_count = 0; command.protected_generations_overflow = false; - VisitProtectedTextureGenerations(command, [&](uint64_t generation) { + const auto add = [&](uint64_t generation) { if (command.protected_generation_count < NativeCommand::kProtectedGenerationCapacity) { command.protected_generations[command.protected_generation_count++] = generation; } else { command.protected_generations_overflow = true; } - }); + }; + // [ps5 port] Called on the title thread right after capture, which sets + // texture slots only for the stages in used_texture_mask: walk those, not + // all 26 slots (seven cache lines of the command, most of them empty). + // Same generations, in the same order, as VisitProtectedTextureGenerations. + const auto texture = [&](const auto& resource) { + if (!resource) return; + add(resource->generation); + if (resource->packed_depth_source) add(resource->packed_depth_source->generation); + }; + texture(command.resolve_destination); + texture(command.depth_handoff_source); + texture(command.present_source); + texture(command.postfx_half_scene); + for (uint32_t mask = command.used_texture_mask; mask; mask &= mask - 1) { + const uint32_t stage = uint32_t(std::countr_zero(mask)); + if (stage >= command.textures.size()) break; + texture(command.textures[stage]); + } command.protected_generations_collected = true; } void Gta4NativeGraphicsSystem::QueueTextureProtection(const NativeCommand& command, bool retain) { + // Nothing to retain or release (most draws without textures): no lock. + if (retain && !command.protected_generations_collected) CollectTextureProtection(command); + if (command.protected_generations_collected && !command.protected_generation_count && + !command.protected_generations_overflow) + return; std::lock_guard lock(queued_protection_mutex_); QueueTextureProtectionLocked(command, retain); } @@ -20284,7 +21313,7 @@ void Gta4NativeGraphicsSystem::QueueTextureProtectionLocked(const NativeCommand& } void Gta4NativeGraphicsSystem::AppendWorkerTextureProtection( - std::unordered_set<uint64_t>& generations) const { + NativeGenerationSet& generations) const { // Callers that race the worker (the pipelined recorder) also hold // render_mutex_, so a command moving from the queue into the batch is seen // in exactly one of the two places. @@ -20295,7 +21324,7 @@ void Gta4NativeGraphicsSystem::AppendWorkerTextureProtection( generations.insert(assembly_texture_protection_.begin(), assembly_texture_protection_.end()); } -void Gta4NativeGraphicsSystem::AppendQueuedTextureProtection(std::unordered_set<uint64_t>& generations) const { +void Gta4NativeGraphicsSystem::AppendQueuedTextureProtection(NativeGenerationSet& generations) const { // Caller owns render_mutex_; fallback retains the previous exact scan policy. std::lock_guard lock(queued_protection_mutex_); if (queued_texture_protection_.valid()) queued_texture_protection_.AppendTo(generations); @@ -20323,21 +21352,20 @@ void Gta4NativeGraphicsSystem::ClearNativeFrameCommands() { } -// Fills `generations` in place: copy assignment reuses the set's nodes, where -// building a new set allocated (and freeing the old one released) a node per -// protected texture, twice a frame while streaming. +// Fills `generations` in place, keeping its table (no allocation once warm). void Gta4NativeGraphicsSystem::CollectProtectedTextureGenerations( const std::shared_ptr<const NativeTextureResource>& present_source, - std::unordered_set<uint64_t>& generations) { + NativeGenerationSet& generations) { const profile::CpuScope profile_scope(profile::CpuOp::kProtectedResources); - generations = frame_texture_protection_; + generations.clear(); + generations.insert(frame_texture_protection_.begin(), frame_texture_protection_.end()); { std::lock_guard lock(render_mutex_); AppendWorkerTextureProtection(generations); AppendQueuedTextureProtection(generations); if (REXCVAR_GET(gta4_validate_native_hot_caches)) { - std::unordered_set<uint64_t> expected; + NativeGenerationSet expected; for (const auto& command : current_frame_) AddProtectedTextureGenerations(command, expected); for (const auto& command : render_queue_) AddProtectedTextureGenerations(command, expected); AppendWorkerTextureProtection(expected); @@ -20409,8 +21437,20 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame } } +#if REX_PLATFORM_PS5 + // [ps5 port] Every texture is its own allocation in the kernel. Past about + // ten thousand of them the console stopped handing out memory at all (even + // 16 KB) while the driver's budget still showed half free, and the game + // fell to one frame every few seconds. Too many images counts as pressure. + constexpr size_t kPs5TextureImageSoftLimit = 6000; + const bool count_pressure = + native_texture_images_.size() > kPs5TextureImageSoftLimit || ps5_memory_short_frames_ > 0; + if (ps5_memory_short_frames_ > 0) --ps5_memory_short_frames_; +#else + constexpr bool count_pressure = false; +#endif if (!allocation_recovery) { - bool pressure_after_poll = false; + bool pressure_after_poll = count_pressure; for (uint32_t heap = 0; heap < budgets.heap_count; ++heap) { pressure_after_poll |= UpdateNativeTexturePressure(texture_budget_pressure_active_, budgets.available, @@ -20419,13 +21459,18 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame texture_budget_pressure_active_ = pressure_after_poll; } + // [ps5 port] An unused texture stays 40 s (it was 90 s, and the images + // piled up until the console ran out of memory), 20 s under pressure. + auto retention = [](bool pressure) -> uint32_t { + return REX_PLATFORM_PS5 && !pressure ? 1200 : kNativeTextureCacheRetentionFrames; + }; std::vector<Candidate> candidates; auto consider = [&](uint64_t generation, const NativeTextureImage* image) { if (!image || !image->source || !CanDiscardNativeTextureImageContents(image->source->gpu_produced, !image->source->payload.empty()) || protected_texture_generations_.contains(generation)) return; if (!allocation_recovery && !ShouldEvictNativeTextureCandidate(false, true, submitted_frame, - image->last_used_frame, kNativeTextureCacheRetentionFrames)) return; + image->last_used_frame, retention(texture_budget_pressure_active_))) return; candidates.push_back({{image->last_use_serial, generation}, image->memory_heap, image->allocation_size}); }; if (allocation_recovery) { @@ -20481,7 +21526,7 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame continue; } const bool budget_pressure = - allocation_recovery || + allocation_recovery || count_pressure || (candidate.memory_heap < budgets.heap_count && UpdateNativeTexturePressure(texture_budget_pressure_active_, budgets.available, budgets.usage[candidate.memory_heap], @@ -20489,7 +21534,7 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame if (!allocation_recovery && !ShouldEvictNativeTextureCandidate(false, budget_pressure, submitted_frame, image->second->last_used_frame, - kNativeTextureCacheRetentionFrames)) { + retention(budget_pressure))) { continue; } @@ -20509,6 +21554,7 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame configured_limit_bytes, candidates.size(), render_queue_.size())); } evicted_images.push_back(std::move(image->second)); + ++g_ps5_texture_evictions; const NativeTextureImage* evicted = evicted_images.back().get(); RecordNativeMemoryLifecycle( memory::ResourceKind::kTextureImage, memory::LifecycleAction::kEvict, @@ -20552,9 +21598,11 @@ void Gta4NativeGraphicsSystem::EvictNativeTextureImages(uint32_t submitted_frame handle, generation, 0, resource->second->payload.size(), resource->second->payload.capacity(), 0, submitted_frame, uint32_t(resource->second->info.format)); + // The producer's front cache checks this flag (evictions are steady + // on the PS5; an epoch bump each dropped every cached texture). + __atomic_store_n(&resource->second->dropped, true, __ATOMIC_RELEASE); texture_resources_.erase(resource); dirty_texture_handles_.erase(handle); - texture_resources_epoch_.fetch_add(1, std::memory_order_release); } } } @@ -20622,6 +21670,84 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedTextureImages( CollectProtectedTextureGenerations(present_source, protected_texture_generations_); EvictNativeTextureImages(submitted_frame, false); } +#if REX_PLATFORM_PS5 + // [ps5 port] Texture memory every ~20 s: textures come and go on the PS5. + if (submitted_frame % 600 == 0) { // one line every ~20 s + static bool settings_logged = false; + if (!settings_logged) { + settings_logged = true; + REXLOG_INFO("ps5 settings: resolution {}, draw distance {}, frame limit {}, shadow distance {}, " + "reflections {}, anti-aliasing {}, motion blur {}, frame generation {}, small " + "shadows skipped under {} indices, renderer threads {}, game thread capture {}", + rex::cvar::GetFlagByName("resolution"), rex::cvar::GetFlagByName("gta4_draw_distance_scale"), + rex::cvar::GetFlagByName("gta4_frame_limit"), + rex::cvar::GetFlagByName("gta4_shadow_distance_scale"), + rex::cvar::GetFlagByName("gta4_reflection_resolution"), + rex::cvar::GetFlagByName("gta4_native_anti_aliasing"), + rex::cvar::GetFlagByName("gta4_motion_blur"), + rex::cvar::GetFlagByName("gta4_native_frame_generation"), + REXCVAR_GET(gta4_native_shadow_min_indices), REXCVAR_GET(ps5_renderer_cpus), + REXCVAR_GET(ps5_lean_capture)); + } + REXLOG_INFO("ps5 textures: frame={} images={} heap-usage={} MB heap-budget={} MB pressure={} " + "alloc-retries={} alloc-failures={} pool-reuses={} evictions={} changed-in-memory={}; {}", + submitted_frame, native_texture_images_.size(), texture_heap_usage_ >> 20, + texture_heap_budget_ >> 20, texture_budget_pressure_active_, + texture_allocation_retry_count_, texture_allocation_failure_count_, + texture_allocation_reuses_, g_ps5_texture_evictions.load(), + texture_watch_changes_.load(std::memory_order_relaxed), + ui::vulkan::MemoryRecyclerSummary()); + // What the game thread's captures cost, per frame since the last line. + struct CaptureTotals { + uint64_t frame = 0, front = 0, lookups = 0, compares = 0, compare_bytes = 0, copies = 0, + copy_bytes = 0, texture_front = 0, texture_lookups = 0, submitted = 0, coalesced = 0, + shadows_dropped = 0, reflections_skipped = 0, state_applied = 0, state_repeated = 0, + holds = 0, held_uses = 0; + }; + static CaptureTotals last; + const CaptureTotals now{submitted_frame, + buffer_capture_stats_.front_hits.Get(), + buffer_capture_stats_.lookups.Get(), + buffer_capture_stats_.full_compares.Get(), + buffer_capture_stats_.full_compare_bytes.Get(), + buffer_capture_stats_.copies.Get(), + buffer_capture_stats_.copy_bytes.Get(), + texture_capture_stats_front_hits_.Get(), + texture_capture_stats_lookups_.Get(), + title_commands_submitted_.Get(), + title_commands_coalesced_.Get(), + rex::frame_stats::g_title_small_shadows_dropped.load(std::memory_order_relaxed), + rex::frame_stats::g_title_reflection_commands_skipped.load(std::memory_order_relaxed), + state_commands_applied_.load(std::memory_order_relaxed), + state_commands_repeated_.load(std::memory_order_relaxed), + holds_taken_reported_.load(std::memory_order_relaxed), + held_uses_reported_.load(std::memory_order_relaxed)}; + const double frames = now.frame > last.frame ? double(now.frame - last.frame) : 1.0; + REXLOG_INFO("ps5 capture: per frame, {:.0f} commands from the game ({:.0f} of them state " + "folded into the next); buffers {:.0f} cached + {:.0f} looked up ({:.0f} compared " + "in full, {:.0f} KB; {:.0f} copied, {:.0f} KB); textures {:.0f} cached + {:.0f} " + "looked up; {} buffers always compared{}; {:.0f} small shadow draws dropped; {:.0f} " + "environment reflection commands skipped; {:.0f}% of state changes repeat what was set; " + "{:.0f} resources held for the frame, {:.0f} uses through holds", + (now.submitted - last.submitted) / frames, (now.coalesced - last.coalesced) / frames, + (now.front - last.front) / frames, (now.lookups - last.lookups) / frames, + (now.compares - last.compares) / frames, + (now.compare_bytes - last.compare_bytes) / frames / 1024.0, + (now.copies - last.copies) / frames, (now.copy_bytes - last.copy_bytes) / frames / 1024.0, + (now.texture_front - last.texture_front) / frames, + (now.texture_lookups - last.texture_lookups) / frames, + buffer_full_validation_count_.load(std::memory_order_relaxed), + buffer_fast_path_disabled_.load(std::memory_order_relaxed) ? " (fast path off)" : "", + (now.shadows_dropped - last.shadows_dropped) / frames, + (now.reflections_skipped - last.reflections_skipped) / frames, + now.state_applied > last.state_applied + ? 100.0 * double(now.state_repeated - last.state_repeated) / + double(now.state_applied - last.state_applied) + : 0.0, + (now.holds - last.holds) / frames, (now.held_uses - last.held_uses) / frames); + last = now; + } +#endif } void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_frame) { @@ -20633,12 +21759,7 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f if (!poll_due && !buffer_cache_reclamation_pending_) return; buffer_cache_reclamation_pending_ = false; - struct Candidate { - uint32_t handle = 0; - uint64_t generation = 0; - uint32_t last_used_frame = 0; - uint64_t retained_bytes = 0; - }; + using Candidate = BufferReclamationCandidate; auto retained_bytes = [](const NativeBufferResource& resource) { uint64_t bytes = resource.payload.capacity(); bytes += resource.host_index16_payload.capacity(); @@ -20652,9 +21773,19 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f constexpr uint64_t kBytesPerMiB = 1048576ull; const uint64_t cache_limit = uint64_t(kNativeBufferCacheLimitMiB) * kBytesPerMiB; - std::vector<Candidate> candidates; - uint64_t cache_bytes = 0; - { + // A sweep cut short by its per-frame budget continues next frame from the + // same candidate list instead of walking (and sorting) the whole cache + // again: every candidate is checked against the live entry anyway, and the + // byte count only shrinks in between. A fresh walk happens at each poll. + std::vector<Candidate>& candidates = buffer_reclamation_candidates_; + uint64_t cache_bytes = buffer_reclamation_cache_bytes_; + const bool continue_sweep = !poll_due && buffer_reclamation_next_ < candidates.size(); + if (!continue_sweep) { + candidates.clear(); + buffer_reclamation_next_ = 0; + cache_bytes = 0; + } + if (!continue_sweep) { std::lock_guard lock(buffer_resource_mutex_); candidates.reserve(buffer_resources_.size()); for (const auto& [handle, resource] : buffer_resources_) { @@ -20672,7 +21803,7 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f // Under budget every eligible aged entry is reclaimed regardless of order. // Only pressure eviction needs oldest-first ordering to choose which younger // entries to discard before the byte budget is satisfied. - if (ShouldSortNativeBufferReclamation(cache_bytes, cache_limit)) { + if (!continue_sweep && ShouldSortNativeBufferReclamation(cache_bytes, cache_limit)) { std::sort(candidates.begin(), candidates.end(), [](const Candidate& left, const Candidate& right) { return left.last_used_frame < right.last_used_frame || @@ -20684,14 +21815,20 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f size_t reclaimed_resources = 0; NativeWorkBudget buffer_reclamation(64, 16777216); std::lock_guard lock(buffer_resource_mutex_); - for (const Candidate& candidate : candidates) { + size_t candidate_index = buffer_reclamation_next_; + for (; candidate_index < candidates.size(); ++candidate_index) { + const Candidate& candidate = candidates[candidate_index]; const auto entry = buffer_resources_.find(candidate.handle); if (entry == buffer_resources_.end() || !entry->second || entry->second->generation != candidate.generation || entry->second.use_count() != 1) { continue; } const bool over_budget = cache_bytes > cache_limit; - if (!ShouldReclaimNativeBuffer(true, over_budget, submitted_frame, candidate.last_used_frame, + // The live entry's use, not the candidate's: a continued sweep's list is + // a few frames old. + const uint32_t last_used_frame = + entry->second->last_used_frame.load(std::memory_order_relaxed); + if (!ShouldReclaimNativeBuffer(true, over_budget, submitted_frame, last_used_frame, kNativeBufferCacheRetentionFrames)) { continue; } @@ -20737,8 +21874,16 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f cache_bytes > candidate.retained_bytes ? cache_bytes - candidate.retained_bytes : 0; reclaimed_bytes += candidate.retained_bytes; ++reclaimed_resources; + // The producer's front cache checks this flag: bumping the shared epoch + // here dropped every cached buffer whenever one was reclaimed. + entry->second->dropped.store(true, std::memory_order_release); buffer_resources_.erase(entry); - buffer_resources_epoch_.fetch_add(1, std::memory_order_release); + } + buffer_reclamation_next_ = candidate_index; + buffer_reclamation_cache_bytes_ = cache_bytes; + if (!buffer_cache_reclamation_pending_) { + candidates.clear(); + buffer_reclamation_next_ = 0; } if (reclaimed_resources && @@ -20750,6 +21895,7 @@ void Gta4NativeGraphicsSystem::ReleaseUnusedBufferResources(uint32_t submitted_f } void Gta4NativeGraphicsSystem::DestroyNativeSurfaceImage(NativeSurfaceImage& image) { + BumpSurfaceLookupEpoch(); if (auto it = surface_images_by_handle_.find(image.descriptor.handle); it != surface_images_by_handle_.end()) { std::erase(it->second, &image); if (it->second.empty()) surface_images_by_handle_.erase(it); @@ -20820,6 +21966,7 @@ void Gta4NativeGraphicsSystem::QueueSurfaceImageRelease(uint32_t handle) { for (const auto& image : native_surface_images_) { if (image && image->descriptor.handle == handle) { pending_surface_release_ids_.insert(image->lifetime_id); + BumpSurfaceLookupEpoch(); if (NativeRendererEventTraceEnabled()) { TraceNativeRendererEvent( "surface-release-queued", @@ -20832,6 +21979,7 @@ void Gta4NativeGraphicsSystem::QueueSurfaceImageRelease(uint32_t handle) { for (auto owner = native_placement_owners_.begin(); owner != native_placement_owners_.end();) { if (owner->second.image && owner->second.image->descriptor.handle == handle) { owner = native_placement_owners_.erase(owner); + ++placement_owners_erase_epoch_; } else { ++owner; } @@ -20861,6 +22009,7 @@ void Gta4NativeGraphicsSystem::ReleasePendingSurfaceImages() { for (auto owner = native_placement_owners_.begin(); owner != native_placement_owners_.end();) { if (owner->second.image == retired_image) { owner = native_placement_owners_.erase(owner); + ++placement_owners_erase_epoch_; } else { ++owner; } @@ -20876,6 +22025,7 @@ void Gta4NativeGraphicsSystem::ReleasePendingSurfaceImages() { native_placement_owners_.size()); } pending_surface_release_ids_.swap(still_pending_ids); + BumpSurfaceLookupEpoch(); } Gta4NativeGraphicsSystem::NativeSurfaceImage* Gta4NativeGraphicsSystem::GetOrCreateSurfaceImage( @@ -20902,6 +22052,19 @@ Gta4NativeGraphicsSystem::NativeSurfaceImage* Gta4NativeGraphicsSystem::GetOrCre if (!descriptor.handle || !descriptor.width || !descriptor.height) { return reject("missing-handle-or-dimensions"); } + // [ps5 port] Consecutive draws ask for the same few targets; the lookups + // below (a lock, the virtual and reflection registries, the image map and + // the pending releases) give the same image until one of those changes, + // which bumps the epoch. + const uint64_t lookup_epoch = surface_lookup_epoch_.load(std::memory_order_acquire); + SurfaceLookupMemo& lookup_memo = + surface_lookup_memo_[((descriptor.handle >> 4) ^ (depth ? 5u : 0u)) % surface_lookup_memo_.size()]; + if (lookup_memo.image && lookup_memo.epoch == lookup_epoch && lookup_memo.depth == depth && + lookup_memo.sample_override == host_sample_override && + SurfaceDescriptorsEqual(lookup_memo.descriptor, descriptor)) { + MarkNativeSurfaceImageUsed(*lookup_memo.image); + return lookup_memo.image; + } const VkImageAspectFlags aspect = depth ? (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) : VK_IMAGE_ASPECT_COLOR_BIT; uint32_t image_width = descriptor.width; @@ -21008,6 +22171,7 @@ Gta4NativeGraphicsSystem::NativeSurfaceImage* Gta4NativeGraphicsSystem::GetOrCre image->is_reflection == is_reflection && (!is_reflection || SameNativeReflectionRegistration(image->reflection, reflection))) { MarkNativeSurfaceImageUsed(*image); + lookup_memo = {descriptor, depth, host_sample_override, lookup_epoch, image}; if (is_reflection) { using CacheSignature = std::tuple<uint32_t, VkImageAspectFlags, uint32_t, uint32_t, VkSampleCountFlagBits>; @@ -21156,6 +22320,7 @@ Gta4NativeGraphicsSystem::NativeSurfaceImage* Gta4NativeGraphicsSystem::GetOrCre NativeSurfaceImage* result = image.get(); MarkNativeSurfaceImageUsed(*result); surface_images_by_handle_[descriptor.handle].push_back(image.get()); + BumpSurfaceLookupEpoch(); native_surface_images_.push_back(std::move(image)); RecordNativeMemoryLifecycle(is_reflection ? memory::ResourceKind::kReflectionSurface : depth ? memory::ResourceKind::kDepthSurface @@ -21816,6 +22981,7 @@ VkPipeline Gta4NativeGraphicsSystem::GetOrCreatePipeline( if (selected_pixel.late_module) selected_pixel.late_module = GetTemporalShaderModule( state.pixel_shader_resource, shader_override_selection.pixel_override, true); if (!selected_vertex.early_module || !selected_pixel.early_module) { + if (temporal_motion_complete_) NOTE_FG_MOTION_LOST("no temporal shader variant", &state); temporal_motion_complete_ = false; return reject("temporal-shader-variant-unavailable"); } @@ -23064,8 +24230,12 @@ bool Gta4NativeGraphicsSystem::BindCommonDrawState( command.worker_vertex_constants_offset && command.worker_pixel_constants_offset && command.worker_ring_serial == recording_ring_serial_ && !target.temporal_shader && worker_ring_valid_.load(std::memory_order_acquire) && worker_constant_ring_.buffer; - (ring_constants ? g_worker_ring_draws : g_worker_ring_fallbacks) - .fetch_add(1, std::memory_order_relaxed); + { + // Telemetry: a plain add (a locked one per draw also fenced the recorder's + // outstanding loads); a count lost to the report's exchange is harmless. + auto& counter = ring_constants ? g_worker_ring_draws : g_worker_ring_fallbacks; + counter.store(counter.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); + } if (ring_constants) { const auto ring_allocation = [&](uint32_t offset, NativeUploadAllocation& allocation) { allocation.buffer = worker_constant_ring_.buffer; @@ -24140,6 +25310,26 @@ bool Gta4NativeGraphicsSystem::GetRequiredVertexStreams( return true; } +// [ps5 port] The recorder's draws mostly repeat the previous draw's vertex +// shader and declaration; their stream mask (a nested loop over inputs and +// elements) is kept for that pair. Declarations are told apart by generation, +// which no other declaration ever reuses. Recorder thread only. +bool Gta4NativeGraphicsSystem::RecorderRequiredVertexStreams( + const NativePipelineState& state, std::array<bool, kVertexStreamCount>& required_streams) { + const NativeShader* shader = state.vertex_shader_resource; + const NativeVertexDeclaration* declaration = state.vertex_declaration_resource.get(); + RequiredStreamsMemo& memo = recorder_required_streams_memo_; + if (shader && declaration && memo.valid && memo.shader == shader && + memo.shader_hash == shader->hash && memo.declaration_generation == declaration->generation) { + required_streams = memo.streams; + return memo.result; + } + const bool result = GetRequiredVertexStreams(state, required_streams); + if (shader && declaration) + memo = {shader, shader->hash, declaration->generation, required_streams, result, true}; + return result; +} + bool Gta4NativeGraphicsSystem::RecordPrimitive(VkCommandBuffer command_buffer, const NativeCommand& command, uint32_t width, uint32_t height, const NativeRenderingTarget& target, @@ -24183,7 +25373,7 @@ bool Gta4NativeGraphicsSystem::RecordPrimitive(VkCommandBuffer command_buffer, const VkDeviceSize default_vertex_offset = 0; profile::CpuCall(profile::CpuOp::kDriverBind, [&] { return BindNativeVertexBuffer(command_buffer, kDefaultVertexBinding, upload_buffer, default_vertex_offset); }); std::array<bool, kVertexStreamCount> required_streams{}; - if (!GetRequiredVertexStreams(*command.pipeline_state, required_streams)) { + if (!RecorderRequiredVertexStreams(*command.pipeline_state, required_streams)) { return fail("vertex-input-layout"); } for (uint32_t stream = 0; stream < kVertexStreamCount; ++stream) { @@ -24304,8 +25494,10 @@ bool Gta4NativeGraphicsSystem::RecordIndexedPrimitive(VkCommandBuffer command_bu if (!command.pipeline_state || !command.index_buffer || !command.shader_state) { return fail("command-state-or-index"); } + // [ps5 port] Only the fields before draw_id are read here: copy those (the + // payload's first cache line), not its dirty state and lighting behind them. DrawIndexedPrimitiveCommand draw{}; - std::memcpy(&draw, command.bytes.data(), sizeof(draw)); + std::memcpy(&draw, command.bytes.data(), offsetof(DrawIndexedPrimitiveCommand, draw_id)); if (indexed_merge_count_override_) draw.index_count = indexed_merge_count_override_; if (!draw.index_count) { return fail("zero-index-count"); @@ -24335,7 +25527,7 @@ bool Gta4NativeGraphicsSystem::RecordIndexedPrimitive(VkCommandBuffer command_bu const VkDeviceSize default_vertex_offset = 0; profile::CpuCall(profile::CpuOp::kDriverBind, [&] { return BindNativeVertexBuffer(command_buffer, kDefaultVertexBinding, upload_buffer, default_vertex_offset); }); std::array<bool, kVertexStreamCount> required_streams{}; - if (!GetRequiredVertexStreams(*command.pipeline_state, required_streams)) { + if (!RecorderRequiredVertexStreams(*command.pipeline_state, required_streams)) { return fail("vertex-input-layout"); } for (uint32_t stream = 0; stream < kVertexStreamCount; ++stream) { @@ -26618,8 +27810,18 @@ void Gta4NativeGraphicsSystem::ClaimSurfaceContent(NativeSurfaceImage& surface, const GuestPlacementKey key = GetGuestPlacementKey(view); surface.materialized_placement = key; surface.materialized_serial = serial; - native_placement_owners_[key] = { - &surface, view, serial, submitted_frame, diagnostic_command_index_, render_phase, write_kind}; + // [ps5 port] Claimed for every draw: reuse the entry this surface claimed + // last when nothing was erased since (map nodes stay put on insert). + NativePlacementOwner* entry = surface.claimed_owner_entry; + if (!entry || surface.claimed_owner_epoch != placement_owners_erase_epoch_ || + !(surface.claimed_owner_key == key)) { + entry = &native_placement_owners_[key]; + surface.claimed_owner_entry = entry; + surface.claimed_owner_key = key; + surface.claimed_owner_epoch = placement_owners_erase_epoch_; + } + *entry = {&surface, view, serial, submitted_frame, diagnostic_command_index_, render_phase, + write_kind}; if (NativeRendererEventTraceEnabled()) { TraceNativeRendererEvent( "placement-owner-claimed", @@ -28776,7 +29978,7 @@ bool Gta4NativeGraphicsSystem::RecordPresent( return false; } const SmaaQuality quality = - ParseSmaaQuality(rex::cvar::Query<std::string>("gta4_native_smaa_quality")); + ParseSmaaQuality(CachedQuery<std::string>("gta4_native_smaa_quality")); smaa_applied = smaa_pipeline_.Record( command_buffer, vulkan_device, frame_descriptor_pool_, native_pipeline_cache_, shader_source_image, shader_source_view, *shader_source_layout, @@ -28905,19 +30107,19 @@ bool Gta4NativeGraphicsSystem::RecordPresent( constants.destination_height = int32_t(presenter_height); constants.hdr_headroom = std::max(1.0f, hdr_headroom); const auto configured_hdr_mode = rex::graphics::gta4_native::ParseHdrMode( - rex::cvar::GetFlagByName("gta4_native_hdr_mode")); + CachedFlagByName("gta4_native_hdr_mode")); const auto effective_hdr_mode = hdr_output ? configured_hdr_mode.value_or(rex::graphics::gta4_native::HdrMode::kScRgb) : rex::graphics::gta4_native::HdrMode::kOff; constants.hdr_mode = uint32_t(effective_hdr_mode); constants.paper_white_nits = static_cast<float>( - std::clamp(rex::cvar::Query<double>("gta4_native_hdr_paper_white_nits"), 80.0, 500.0)); + std::clamp(CachedQuery<double>("gta4_native_hdr_paper_white_nits"), 80.0, 500.0)); constants.peak_nits = static_cast<float>( - std::clamp(rex::cvar::Query<double>("gta4_native_hdr_peak_nits"), 80.0, 2000.0)); + std::clamp(CachedQuery<double>("gta4_native_hdr_peak_nits"), 80.0, 2000.0)); constants.shoulder_start = static_cast<float>( - std::clamp(rex::cvar::Query<double>("gta4_native_auto_hdr_shoulder_start"), 0.0, 1.0)); + std::clamp(CachedQuery<double>("gta4_native_auto_hdr_shoulder_start"), 0.0, 1.0)); constants.shoulder_power = static_cast<float>( - std::clamp(rex::cvar::Query<double>("gta4_native_auto_hdr_shoulder_power"), 1.0, 10.0)); + std::clamp(CachedQuery<double>("gta4_native_auto_hdr_shoulder_power"), 1.0, 10.0)); const uint32_t aa_bits = temporal_resolved_ ? 0 : GetNativePresentationAABits(); const uint32_t ssaa_bits = ssaa_applied ? 32u : 0u; constants.output_mode = (hdr_output ? 1u : 0u) | aa_bits | ssaa_bits | 4u | @@ -28939,7 +30141,7 @@ bool Gta4NativeGraphicsSystem::RecordPresent( constants.shoulder_power, aa_bits, bool(ssaa_bits), constants.output_mode)); } if (!aa_bits && !ssaa_bits && - rex::cvar::Query<std::string>("gta4_native_upscaler") == "fsr1") { + CachedQuery<std::string>("gta4_native_upscaler") == "fsr1") { static std::atomic<bool> logged_fsr_without_aa{false}; if (!logged_fsr_without_aa.exchange(true)) { REXLOG_WARN( @@ -29618,6 +30820,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( CachedDiagnosticsEnabled(rex::diagnostics::Category::kTransition) && transition::IsEnabled() && transition::ActiveTransitionId(); const bool collect_frame_diagnostics = legacy_diagnostics || transition_trace; + const bool transition_notes = transition::IsEnabled(); uint64_t invocation = 0; if (collect_frame_diagnostics) { static std::atomic<uint64_t> invocation_counter{0}; @@ -30755,8 +31958,11 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( if (command.pipeline_state && command.pipeline_state->pixel_shader_resource) { uint16_t& memo = command.pipeline_state->translucent_range_memo; if (memo == 0xFFFF) { - memo = uint16_t(PerformanceRangeForTranslucentCategory(ClassifyTranslucentDiagnosticShader( - command.pipeline_state->pixel_shader_resource->filename))); + const NativeShader& ps = *command.pipeline_state->pixel_shader_resource; + if (ps.translucent_range_memo == 0xFFFF) + ps.translucent_range_memo = uint16_t(PerformanceRangeForTranslucentCategory( + ClassifyTranslucentDiagnosticShader(ps.filename))); + memo = ps.translucent_range_memo; } const auto translucent = performance::GpuRange(memo); if (translucent != performance::GpuRange::kCount) { @@ -30777,9 +31983,19 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( if (memo == 0xFFFF) { const auto* vs = command.pipeline_state->vertex_shader_resource; const auto* ps = command.pipeline_state->pixel_shader_resource; - memo = uint16_t(performance::ProfileShaderCategory( - vs ? std::string_view(vs->filename) : std::string_view{}, - ps ? std::string_view(ps->filename) : std::string_view{})); + if (ps && ps->category_pair == vs && ps->category_pair_memo != 0xFFFF && + ps->category_pair_hash == (vs ? vs->hash : 0)) { + memo = ps->category_pair_memo; + } else { + memo = uint16_t(performance::ProfileShaderCategory( + vs ? std::string_view(vs->filename) : std::string_view{}, + ps ? std::string_view(ps->filename) : std::string_view{})); + if (ps) { + ps->category_pair = vs; + ps->category_pair_hash = vs ? vs->hash : 0; + ps->category_pair_memo = memo; + } + } } return performance::GpuRange(memo); } @@ -31206,6 +32422,37 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( return command.snapshot_render_targets[0].width == 0 && depth.handle && depth.width >= 512 && depth.width == depth.height; }; +#if REX_PLATFORM_PS5 + if (submitted_frame % 600 == 300) { + // [ps5 port] Which passes the frame's draws are in, every ~20 s: every + // draw costs about the same on all three threads. + uint32_t total = 0, shadow = 0, water = 0, environment = 0, small_shadow = 0; + for (const NativeCommand& scan : current_frame_) { + if (scan.type != CommandType::kDrawPrimitive && scan.type != CommandType::kDrawPrimitiveUp && + scan.type != CommandType::kDrawIndexedPrimitive) + continue; + ++total; + const performance::GpuRange range = performance_range_for_command(scan); + if (range == performance::GpuRange::kRetailWarpShadow || shadow_map_draw(scan)) { + ++shadow; + if (scan.type == CommandType::kDrawIndexedPrimitive && + scan.bytes.size() >= sizeof(DrawIndexedPrimitiveCommand)) { + DrawIndexedPrimitiveCommand draw{}; + std::memcpy(&draw, scan.bytes.data(), sizeof(draw)); + if (draw.index_count < 300) ++small_shadow; + } + } else if (range == performance::GpuRange::kWaterReflections) { + ++water; + } else if (range == performance::GpuRange::kEnvironmentReflections) { + ++environment; + } + } + REXLOG_INFO("ps5 draws: frame {} has {} ({} shadows, {} of them under 300 indices; {} water " + "reflection; {} environment reflection; {} the rest)", + submitted_frame, total, shadow, small_shadow, water, environment, + total - shadow - water - environment); + } +#endif const bool lazy_resolves = REXCVAR_GET(gta4_native_lazy_resolves) && !diagnostic_frame && !fire_frame_ && !force_content_probe; { @@ -31231,7 +32478,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( if (pipelined_recording_ && frame_constant_bound_.valid && frame_constant_bound_.read_epoch && REXCVAR_GET(gta4_native_assembled_frame_scans)) { // A later frame's stamp only makes this conservative (an extra resolve). - read = std::atomic_ref(frontbuffer->assembly_read_epoch).load(std::memory_order_relaxed) >= + read = __atomic_load_n(&frontbuffer->assembly_read_epoch, __ATOMIC_RELAXED) >= frame_constant_bound_.read_epoch; } else for (const NativeCommand& scan : current_frame_) { for (const auto& texture : scan.textures) { @@ -31260,6 +32507,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( for (const NativeCommand& scan : current_frame_) { resolved_again |= scan.type == CommandType::kResolve && scan.resolve_destination && scan.resolve_destination->generation == generation; + if (resolved_again) break; } if (!resolved_again) RecordResolve(command_buffer, *deferred_frontbuffer_resolve_, submitted_frame); deferred_frontbuffer_resolve_.reset(); @@ -31270,7 +32518,14 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( // and the state they point to (cache misses): fetch the fields recording // reads a few commands ahead, and the state objects once their pointers are // in cache. - const bool prefetch_commands = REXCVAR_GET(gta4_native_recorder_prefetch); + // [ps5 port] On Game Thread Capture's Compare, off and on with the game's + // render thread's prefetching (rex::frame_stats::g_capture_prefetch). + const bool prefetch_commands = REXCVAR_GET(gta4_native_recorder_prefetch) +#if REX_PLATFORM_PS5 + && rex::frame_stats::g_capture_prefetch.load(std::memory_order_relaxed) +#endif + ; + const bool verify_fixed_state = VerifyFixedStateSnapshots(); const auto prefetch_command = [](const NativeCommand& ahead) { __builtin_prefetch(&ahead.type); __builtin_prefetch(&ahead.bytes); @@ -31281,16 +32536,88 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( __builtin_prefetch(&ahead.pipeline_state); __builtin_prefetch(&ahead.fixed_function_state); __builtin_prefetch(reinterpret_cast<const uint8_t*>(&ahead.fixed_function_state) + 64); + __builtin_prefetch(&ahead.vertex_buffers); + // [ps5 port] The shared-constant key reads the sampler indices and the + // used stages' fetch words (LOD bias). + __builtin_prefetch(&ahead.sampler_descriptor_indices); + __builtin_prefetch(&ahead.texture_fetches); + __builtin_prefetch(reinterpret_cast<const uint8_t*>(&ahead.texture_fetches) + 64); + // [ps5 port] Also read for every draw: the index buffer and signatures line + // (prefetch_state reads it two ahead), the fixed state's later lines. + __builtin_prefetch(&ahead.index_buffer); + __builtin_prefetch(&ahead.fixed_function_state.stencil_enable); + __builtin_prefetch(&ahead.fixed_function_state.color_write_mask); + }; + // [ps5 port] Recording an indexed draw reads its index buffer's and first + // vertex streams' resources (flags, payloads, host index copy, persistent + // memo: their first four lines), then the persistent entry the memo names. + const auto is_draw_command = [](const NativeCommand& ahead) { + return ahead.type == CommandType::kDrawIndexedPrimitive || + ahead.type == CommandType::kDrawPrimitive; }; - const auto prefetch_state = [](const NativeCommand& ahead) { - if (const auto* state = ahead.pipeline_state.get()) __builtin_prefetch(state); - if (const auto* state = ahead.shader_state.get()) __builtin_prefetch(state); + const auto prefetch_buffer_resource = [](const NativeBufferResource* resource) { + if (!resource) return; + const auto* bytes = reinterpret_cast<const uint8_t*>(resource); + for (size_t line = 0; line < 4; ++line) __builtin_prefetch(bytes + line * 64); + }; + // [ps5 port] The pipeline and shader state objects are a few hundred bytes + // (render targets, vertex streams, shader pointers, the pipeline memo, the + // constant versions): fetch all of each, once per distinct object (runs of + // draws share them). The first line alone left the shader pointers the + // next step reads, and the memo, to miss. + const void* prefetched_pipeline_state = nullptr; + const void* prefetched_shader_state = nullptr; + const auto prefetch_object = [](const void* object, size_t size) { + const auto* bytes = static_cast<const uint8_t*>(object); + for (size_t offset = 0; offset < size && offset < 16 * 64; offset += 64) + __builtin_prefetch(bytes + offset); + }; + const auto prefetch_state = [&](const NativeCommand& ahead) { + if (const auto* state = ahead.pipeline_state.get(); state && state != prefetched_pipeline_state) { + prefetched_pipeline_state = state; + prefetch_object(state, sizeof(*state)); + } + if (const auto* state = ahead.shader_state.get(); state && state != prefetched_shader_state) { + prefetched_shader_state = state; + prefetch_object(state, sizeof(*state)); + } + if (is_draw_command(ahead)) { + prefetch_buffer_resource(ahead.index_buffer.get()); + prefetch_buffer_resource(ahead.vertex_buffers[0].get()); + prefetch_buffer_resource(ahead.vertex_buffers[1].get()); + } + }; + // [ps5 port] One level further, a command later: what the state objects + // point to (the shaders, and the constant versions binding reads first). + const auto prefetch_pointees = [&](const NativeCommand& ahead) { + if (const auto* state = ahead.pipeline_state.get()) { + if (state->vertex_shader_resource) __builtin_prefetch(state->vertex_shader_resource); + if (state->pixel_shader_resource) __builtin_prefetch(state->pixel_shader_resource); + } + if (const auto* state = ahead.shader_state.get()) { + // [ps5 port] Both lines: binding reads byte_size and materialized. + for (const ConstantStateVersion* constants : + {state->vertex_constants.get(), state->pixel_constants.get()}) { + if (!constants) continue; + __builtin_prefetch(constants); + __builtin_prefetch(reinterpret_cast<const uint8_t*>(constants) + + offsetof(ConstantStateVersion, materialized)); + } + } + if (is_draw_command(ahead)) { + for (const NativeBufferResource* resource : + {ahead.index_buffer.get(), ahead.vertex_buffers[0].get(), ahead.vertex_buffers[1].get()}) { + if (resource && resource->persistent_memo_entry) + __builtin_prefetch(resource->persistent_memo_entry); + } + } }; for (size_t command_index = 0; command_index < current_frame_.size(); ++command_index) { breadcrumb_command = command_index; if (prefetch_commands) { if (command_index + 4 < current_frame_.size()) prefetch_command(current_frame_[command_index + 4]); if (command_index + 2 < current_frame_.size()) prefetch_state(current_frame_[command_index + 2]); + if (command_index + 1 < current_frame_.size()) prefetch_pointees(current_frame_[command_index + 1]); } if (command_index < merged_up_until) { continue; // Recorded as part of the previous DrawPrimitiveUp batch. @@ -31309,7 +32636,9 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( g_water_reflection_draws_culled.fetch_add(1, std::memory_order_relaxed); continue; } - if (range == performance::GpuRange::kRetailWarpShadow && + // (The pass tag reaches the renderer only with the profiler on; the + // shadow map's target identifies the pass otherwise.) + if ((range == performance::GpuRange::kRetailWarpShadow || shadow_map_draw(queued_command)) && draw.index_count < shadow_min_indices) { g_shadow_draws_culled.fetch_add(1, std::memory_order_relaxed); continue; @@ -31372,7 +32701,8 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( profile_state && profile_state->pixel_shader_resource ? profile_state->pixel_shader_resource->hash : 0}; }); const profile::CpuScope detail_command_scope(profile::CpuOp::kRecordCommand); - phone_frame_trace_ = queued_command.phone_trace; + if ((queued_command.trace_flags & NativeCommand::kTracePhone) || phone_frame_trace_) + phone_frame_trace_ = queued_command.phone_trace; phone_record_event_ = phone_frame_trace_ ? phone_frame_trace_->event : 0; phone_lineage_draw_active_ = PhoneTraceConfig().lineage && phone_probe_commands.contains(command_index); if (queued_command.type == CommandType::kReleaseResource) { @@ -31389,6 +32719,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( queued_command.type == CommandType::kDrawPrimitiveUp || queued_command.type == CommandType::kDrawIndexedPrimitive || queued_command.type == CommandType::kClear) && + verify_fixed_state && queued_command.recorded_fixed_function_state_hash != HashFixedFunctionState(queued_command.fixed_function_state)) { REXLOG_ERROR( @@ -31405,7 +32736,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( const NativeCommand* command_pointer = &queued_command; constexpr uint64_t kDeferredLightingPs9Hash = 0x23C88DA6F953E36Bull; constexpr uint32_t kFillerVolumePointTechnique = 11; - const std::string ps9_stencil_probe = REXCVAR_GET(gta4_native_light_ps9_stencil_probe); + const std::string& ps9_stencil_probe = REXCVAR_GET(gta4_native_light_ps9_stencil_probe); const bool ps9_accumulation_draw = queued_command.light_trace_technique == kFillerVolumePointTechnique && queued_command.light_trace_mode == 0 && queued_command.pipeline_state && @@ -31455,7 +32786,15 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( } } - const std::string stencil_face_probe = REXCVAR_GET(gta4_native_light_stencil_face_probe); + if (!light_stencil_bypass_command && queued_command.fixed_function_state.stencil_enable && + queued_command.lighting.role == LightPassRole::kLocalContribution && + REXCVAR_GET(ps5_light_test) == "no-stencil") { + light_stencil_bypass_command.emplace(queued_command); + light_stencil_bypass_command->fixed_function_state.stencil_enable = 0; + command_pointer = &*light_stencil_bypass_command; + } + + const std::string& stencil_face_probe = REXCVAR_GET(gta4_native_light_stencil_face_probe); constexpr uint64_t kDeferredLightingVs1Hash = 0xA97B1F043F720396ull; uint64_t stencil_face_probe_instance = 0; if (stencil_face_probe != "original" && queued_command.pipeline_state && @@ -31542,6 +32881,35 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( } const NativeCommand& command = *command_pointer; +#if REX_PLATFORM_PS5 + // [ps5 port] During the trace's bursts: every resolve and clear, so the + // texture the sky samples (a 256x256 target the game renders each frame) + // can be followed from its writes to its use. + if (REXCVAR_GET(ps5_hud_trace) && submitted_frame % 600 < 2 && + (command.type == CommandType::kResolve || command.type == CommandType::kClear)) { + if (command.type == CommandType::kResolve && command.bytes.size() >= sizeof(ResolveCommand)) { + ResolveCommand r{}; + std::memcpy(&r, command.bytes.data(), sizeof(r)); + REXLOG_INFO("ps5 hud: resolve frame={} cmd={} flags={:08X} source={:08X}/{}x{}/f{} " + "rect={}/{},{},{},{} dest={:08X} level={} phase={}", + submitted_frame, command_index, r.flags, r.source.handle, r.source.width, + r.source.height, r.source.format, r.source_rectangle_valid, + r.source_rectangle.left, r.source_rectangle.top, r.source_rectangle.right, + r.source_rectangle.bottom, r.destination_texture, r.destination_level, + uint32_t(command.render_phase)); + } else if (command.type == CommandType::kClear && command.bytes.size() >= sizeof(ClearCommand)) { + ClearCommand c{}; + std::memcpy(&c, command.bytes.data(), sizeof(c)); + const auto* st = command.pipeline_state.get(); + REXLOG_INFO("ps5 hud: clear frame={} cmd={} flags={:08X} rect={},{},{},{} color={:08X} " + "stencil={} rt0={:08X}/{}x{} ds={:08X}", + submitted_frame, command_index, c.flags, c.left, c.top, c.right, c.bottom, + c.color_bits[0], c.stencil, st ? st->render_targets[0].handle : 0, + st ? st->render_targets[0].width : 0, st ? st->render_targets[0].height : 0, + st ? st->depth_stencil.handle : 0); + } + } +#endif const bool descriptor_draw = command.type == CommandType::kDrawPrimitive || command.type == CommandType::kDrawPrimitiveUp || command.type == CommandType::kDrawIndexedPrimitive; @@ -31578,9 +32946,10 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( } diagnostic_submitted_frame_ = submitted_frame; diagnostic_command_index_ = command_index; - gpu_flight::Record("native.command", NativeVulkanHandleIdentity(command_buffer), - submission_tracker_ ? submission_tracker_->GetCurrentSubmission() : 0, - submitted_frame, command_index, uint64_t(command.type)); + if (native_flight_enabled_.load(std::memory_order_relaxed)) + gpu_flight::Record("native.command", NativeVulkanHandleIdentity(command_buffer), + submission_tracker_ ? submission_tracker_->GetCurrentSubmission() : 0, + submitted_frame, command_index, uint64_t(command.type)); diagnostic_render_phase_ = command.render_phase; diagnostic_render_phase_object_ = command.render_phase_object; fire_event_active_ = fire_frame_ && (command.fire_trace || command.type == CommandType::kResolve || @@ -31597,9 +32966,11 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( }); if (bulb_current_command_) TraceBulbCommand("record-begin", command, "queued-source-snapshot"); diagnostic_light_trace_id_ = command.light_trace_id; - if (command.phone_trace) TracePhoneNativeCommand("gpu-bound", command); - tv_frame_trace_ = TvCommandRole(command) ? command.tv_trace : nullptr; - if (command.tv_trace) { + if ((command.trace_flags & NativeCommand::kTracePhone) && command.phone_trace) + TracePhoneNativeCommand("gpu-bound", command); + if ((command.trace_flags & NativeCommand::kTraceTv) || tv_frame_trace_) + tv_frame_trace_ = TvCommandRole(command) ? command.tv_trace : nullptr; + if ((command.trace_flags & NativeCommand::kTraceTv) && command.tv_trace) { tv_lifecycle_trace_ = command.tv_trace; tv_lifecycle_command_sequence_ = command.diagnostic_submit_sequence; } @@ -33478,6 +34849,119 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( } std::fflush(stderr); } +#if REX_PLATFORM_PS5 + // [ps5 port] The radar's map and its health/armor arcs don't show on the + // PS5. Log the radar-phase draws and the triangle fans (the map sections + // are fans) with the state that decides whether they reach the screen. + if (is_draw && REXCVAR_GET(ps5_hud_trace)) { + uint32_t primitive = 0, count = 0; + if (command.type == CommandType::kDrawPrimitiveUp) { + DrawPrimitiveUpCommand d{}; + std::memcpy(&d, command.bytes.data(), std::min(sizeof(d), command.bytes.size())); + primitive = d.primitive_type, count = d.vertex_count; + } else if (command.type == CommandType::kDrawPrimitive) { + DrawPrimitiveCommand d{}; + std::memcpy(&d, command.bytes.data(), std::min(sizeof(d), command.bytes.size())); + primitive = d.primitive_type, count = d.vertex_count; + } else { + DrawIndexedPrimitiveCommand d{}; + std::memcpy(&d, command.bytes.data(), std::min(sizeof(d), command.bytes.size())); + primitive = d.primitive_type, count = d.index_count; + } + const bool radar = command.render_phase == RenderPhase::kRadarMap; + const bool fan = primitive == uint32_t(xenos::PrimitiveType::kTriangleFan); + const bool sky = command.pipeline_state && command.pipeline_state->pixel_shader_resource && + IsFusionSkyShader(command.pipeline_state->pixel_shader_resource->hash); + static std::atomic<uint64_t> radar_seen{0}, fan_seen{0}, sky_seen{0}; + // Draws into small (256x256) targets: what renders the sky's texture. + const bool rt256 = command.pipeline_state && + command.pipeline_state->render_targets[0].width == 256 && + command.pipeline_state->render_targets[0].height == 256 && + submitted_frame % 600 < 2; + static std::atomic<uint64_t> rt256_seen{0}; + const uint64_t n = radar ? ++radar_seen : sky ? ++sky_seen : fan ? ++fan_seen + : rt256 ? ++rt256_seen : 0; + // The first few, then every frame of a short burst every ~20 s (to + // catch what changes from frame to frame). + if (n && (n <= 40 || submitted_frame % 600 < 3) && command.pipeline_state) { + const NativePipelineState& st = *command.pipeline_state; + const NativeFixedFunctionState& f = command.fixed_function_state; + std::string tex; + for (uint32_t stage = 0; stage < kShaderTextureCount; ++stage) { + const auto& t = command.textures[stage]; + if (!st.textures[stage] && !t) continue; + tex += fmt::format(" s{}={:08X}", stage, st.textures[stage]); + if (t) { + tex += fmt::format("/f{}/{}x{}/tiled{}/gpu{}/g{}", uint32_t(t->info.format), + t->info.width + 1, t->info.height + 1, int(t->info.is_tiled), + int(t->gpu_produced), t->generation); + // The bytes the renderer captured (raw guest bytes when the GPU + // untiles them): a white texture should read FFFFFFFF. + uint32_t w0 = 0, w1 = 0; + if (t->payload.size() >= 8) { + std::memcpy(&w0, t->payload.data(), 4); + std::memcpy(&w1, t->payload.data() + 4, 4); + } + tex += fmt::format("/bytes{}/jobs{}/{:08X},{:08X}", t->payload.size(), + t->detile_jobs.size(), w0, w1); + // What guest memory holds there now, and 4 KB either side (the + // 0xE0000000 range's offset): stale capture or wrong address. + if (t->info.memory.base_address) { + const uint32_t base_address = t->info.memory.base_address; + auto word = [&](uint32_t address) { + uint32_t v = 0; + std::memcpy(&v, memory_->TranslatePhysical<const uint8_t*>(address), 4); + return v; + }; + tex += fmt::format("/base{:08X}/now{:08X}/-1000:{:08X}/+1000:{:08X}", base_address, + word(base_address), word(base_address - 0x1000), + word(base_address + 0x1000)); + } + } else + tex += "/none"; + } + // The first two vertices' words, for the draw-up commands (colors and + // alpha of the radar's mask shapes). + if (command.type == CommandType::kDrawPrimitiveUp && !command.payload.empty()) { + DrawPrimitiveUpCommand d{}; + std::memcpy(&d, command.bytes.data(), std::min(sizeof(d), command.bytes.size())); + tex += " verts="; + const size_t words = std::min<size_t>(d.stride / 4, 10); + for (size_t v = 0; v < 2 && v < d.vertex_count; ++v) { + tex += v ? "|" : ""; + for (size_t w = 0; w < words; ++w) { + const size_t at = v * d.stride + w * 4; + if (at + 4 > command.payload.size()) break; + uint32_t word; + std::memcpy(&word, command.payload.data() + at, 4); + tex += fmt::format("{}{:08X}", w ? "," : "", word); + } + } + } + REXLOG_INFO( + "ps5 hud: {} #{} frame={} cmd={} type={} prim={} count={} vs={:016X} ps={:016X} " + "consts={:016X}/{:016X} " + "depth={}/{}/{} stencil={}/f{}/ref{}/m{:X}/w{:X}/ops{}{}{} blend={}/{:08X} cwm={:X} " + "atest={}/{}/{:.3f} cull={} clip={:X} scissor={}:{},{},{},{} vp={:.1f},{:.1f},{:.1f},{:.1f} " + "rt0={:08X}/f{}/{}x{} ds={:08X} target={}x{}/{}x{} presenter={} reflection={}{}", + radar ? "radar" : sky ? "sky" : fan ? "fan" : "rt256", n, submitted_frame, command_index, CommandTypeName(command.type), + primitive, count, st.vertex_shader_resource ? st.vertex_shader_resource->hash : 0, + st.pixel_shader_resource ? st.pixel_shader_resource->hash : 0, + command.vertex_constants_hash, command.pixel_constants_hash, f.depth_enable, + f.depth_function, f.depth_write_enable, f.stencil_enable, f.stencil_function, + f.stencil_reference, f.stencil_mask, f.stencil_write_mask, f.stencil_fail, + f.stencil_depth_fail, f.stencil_pass, f.blend_enable, f.blend_controls[0], + f.color_write_mask, f.alpha_test_enable, f.alpha_function, f.alpha_reference, + f.cull_mode, f.user_clip_plane_enable_mask, f.scissor_enable, f.scissor[0], + f.scissor[1], f.scissor[2], f.scissor[3], std::bit_cast<float>(f.viewport_bits[0]), + std::bit_cast<float>(f.viewport_bits[1]), std::bit_cast<float>(f.viewport_bits[2]), + std::bit_cast<float>(f.viewport_bits[3]), st.render_targets[0].handle, + st.render_targets[0].format, st.render_targets[0].width, st.render_targets[0].height, + st.depth_stencil.handle, target.width, target.height, target.logical_width, + target.logical_height, target.uses_presenter, target.is_reflection, tex); + } + } +#endif if (legacy_diagnostics && is_draw && command.render_phase == RenderPhase::kRadarMap) { static std::atomic<uint64_t> radar_draw_count{0}; const uint64_t radar_draw_index = ++radar_draw_count; @@ -35069,6 +36553,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( !PhoneTraceConfig().readbacks && !selected_light_stencil_setup_draw && !light_color_delta_draw && !postfx_scheduler.scene_capture_pending() && !REXCVAR_GET(gta4_native_light_ps9_stencil_bypass) && + REXCVAR_GET(ps5_light_test) == "off" && REXCVAR_GET(gta4_native_light_ps9_stencil_probe) == "original" && REXCVAR_GET(gta4_native_light_stencil_face_probe) == "original" && !CachedDiagnosticsEnabled(rex::diagnostics::Category::kNativeProbes)) { @@ -36017,8 +37502,10 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( // tiny patches) become one indexed draw over their concatenated vertices. up_run.clear(); if (REXCVAR_GET(gta4_native_merge_up_draws) && &command == &queued_command && - !diagnostic_frame && !legacy_diagnostics && !command.fire_trace && !command.bulb_trace && - !command.phone_trace && command.pipeline_state && command.shader_state && + !diagnostic_frame && !legacy_diagnostics && + !(command.trace_flags & (NativeCommand::kTraceFire | NativeCommand::kTraceBulb | + NativeCommand::kTracePhone)) && + command.pipeline_state && command.shader_state && command.pipeline_state->vertex_shader_resource && !command.pipeline_state->vertex_shader_resource->uses_vertex_index) { DrawPrimitiveUpCommand head{}; @@ -36031,8 +37518,9 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( next < current_frame_.size() && up_run.size() < REXCVAR_GET(gta4_native_merge_up_max); ++next) { const NativeCommand& other = current_frame_[next]; - if (other.type != CommandType::kDrawPrimitiveUp || other.fire_trace || - other.bulb_trace || other.phone_trace || + if (other.type != CommandType::kDrawPrimitiveUp || + (other.trace_flags & (NativeCommand::kTraceFire | NativeCommand::kTraceBulb | + NativeCommand::kTracePhone)) || other.pipeline_state != command.pipeline_state || other.shader_state != command.shader_state || other.textures != command.textures || other.captured_vertex_constants_hash != command.captured_vertex_constants_hash || @@ -36079,8 +37567,8 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( size_t merged_count = 1; uint32_t merged_indices = 0; if (REXCVAR_GET(gta4_native_merge_indexed_draws) && &command == &queued_command && - !diagnostic_frame && !legacy_diagnostics && !command.fire_trace && !command.bulb_trace && - !command.phone_trace && !command.tv_trace && !command.temporal_scene_binding && + !diagnostic_frame && !legacy_diagnostics && !command.trace_flags && + !command.temporal_scene_binding && !command.temporal_instance && command.pipeline_state && command.shader_state && command.index_buffer && fire_query == UINT32_MAX) { DrawIndexedPrimitiveCommand head{}; @@ -36092,8 +37580,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( for (size_t next = command_index + 1; next < current_frame_.size() && merged_count < 64; ++next) { const NativeCommand& other = current_frame_[next]; - if (other.type != CommandType::kDrawIndexedPrimitive || other.fire_trace || - other.bulb_trace || other.phone_trace || other.tv_trace || + if (other.type != CommandType::kDrawIndexedPrimitive || other.trace_flags || other.temporal_scene_binding || other.temporal_instance || other.pipeline_state != command.pipeline_state || other.shader_state != command.shader_state || @@ -36161,7 +37648,7 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( if (collect_frame_diagnostics) { command_recorded ? ++successful_draws : ++failed_draws; } - if (command_recorded) { + if (command_recorded && transition_notes) { transition::NoteFirstIndexedDraw(diagnostic_guest_pc, diagnostic_guest_lr, submitted_frame, diagnostic_draw_id_); } @@ -36246,7 +37733,10 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( if (rendering) { end_rendering(__LINE__); rendering = false; } if (!ReplayTemporalDisplayDraw(command_buffer, command, target, resources)) return false; } - if (!command_recorded && target.temporal_shader) temporal_motion_complete_ = false; + if (!command_recorded && target.temporal_shader) { + if (temporal_motion_complete_) NOTE_FG_MOTION_LOST("draw not recorded", command.pipeline_state); + temporal_motion_complete_ = false; + } if (!command_recorded) TraceModernShaderFailure(command,semantic_lineage_ready?"draw.record":"draw.lighting-lineage", semantic_lineage_ready?"Draw or clear recording rejected":"Lighting aspect lineage rejected"); @@ -36890,6 +38380,9 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( temporal_texture(temporal_scene_.display(), temporal_display_texture_); temporal_present = &temporal_display_texture_; } else { + if (temporal_generated_frame_.has_generated_frame) + rex::frame_stats::NoteFgDrop(!temporal_ui_exact_ ? rex::frame_stats::FgDrop::kHudNotExact + : rex::frame_stats::FgDrop::kDisplayStale); temporal_generated_frame_.has_generated_frame = false; } NativeTextureImage surface_present; @@ -36934,8 +38427,10 @@ bool Gta4NativeGraphicsSystem::RecordNativeFrame( return converted; }; if (!convert(pair.generated, pair.generated_present) || !convert(pair.real, pair.real_present) || - !temporal_frame_generator_.FinishForPresentation(command_buffer, pair)) + !temporal_frame_generator_.FinishForPresentation(command_buffer, pair)) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kConvert); pair.has_generated_frame = false; + } } SwitchNativeGpuProfileRange(command_buffer, performance::GpuRange::kUnattributed); presenter_transfer_written = present_transfer_written; @@ -37338,6 +38833,14 @@ bool Gta4NativeGraphicsSystem::ClearGuestOutput( present](ui::Presenter::GuestOutputRefreshContext& context) -> bool { auto& vulkan_context = static_cast<ui::vulkan::VulkanPresenter::VulkanGuestOutputRefreshContext&>(context); +#if REX_PLATFORM_PS5 + const int64_t output_callback_begin_ns = rex::frame_stats::NowNs(); + const auto note_output_callback = MakeScopeExit([output_callback_begin_ns] { + rex::frame_stats::g_output_callback_us.fetch_add( + uint64_t(rex::frame_stats::NowNs() - output_callback_begin_ns) / 1000, + std::memory_order_relaxed); + }); +#endif const auto& dfn = vulkan_device->functions(); const VkDevice device = vulkan_device->device(); const bool native_profiler_enabled = @@ -37700,11 +39203,19 @@ bool Gta4NativeGraphicsSystem::ClearGuestOutput( WriteBreadcrumb(command_buffer_, fmt::format("frame {} start: textures prepared, recording begins", submitted_frame)); } +#if REX_PLATFORM_PS5 + const int64_t record_commands_begin_ns = rex::frame_stats::NowNs(); +#endif const bool frame_recorded = RecordNativeFrame( command_buffer_, width, height, vulkan_context.image(), vulkan_context.image_view(), submitted_frame, present_source, environmental_data, vulkan_context.hdr_output(), vulkan_context.hdr_headroom(), presenter_transfer_written, presenter_written, trace_stages, trace_sequence, present.diagnostic_force_content_probe != 0); +#if REX_PLATFORM_PS5 + rex::frame_stats::g_record_commands_us.fetch_add( + uint64_t(rex::frame_stats::NowNs() - record_commands_begin_ns) / 1000, + std::memory_order_relaxed); +#endif if (present.diagnostic_trace) { REXLOG_INFO( "gta4-tv-native-record: session={} present={} frame={} origin={} " @@ -37862,8 +39373,12 @@ bool Gta4NativeGraphicsSystem::ClearGuestOutput( MarkFrameTimeline(submitted_frame, kTimelineQueueSubmit); vulkan_context.MarkImageAccessSubmitted(); SubmitTemporalFrame(); - if (temporal_generated_frame_.has_generated_frame) - temporal_frame_generator_.Publish(vulkan_context, temporal_generated_frame_); + if (temporal_generated_frame_.has_generated_frame) { + if (temporal_frame_generator_.Publish(vulkan_context, temporal_generated_frame_)) + rex::frame_stats::g_fg_published.fetch_add(1, std::memory_order_relaxed); + else + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kPublishRefused); + } if (gpu_labels::Enabled() && (submitted_frame <= 3 || !(submitted_frame % 120))) { REXLOG_INFO("gta4-native-resolve-reuse: frame={} candidates={} skipped={} pixels={} shared-hdr={}", submitted_frame, resolve_reuse_candidates_, resolve_reuse_hits_, @@ -38033,8 +39548,19 @@ bool Gta4NativeGraphicsSystem::ClearGuestOutput( return presenter_written; }, ui::GuestOutputProvenance{ - .frame_rate_limit = rex::cvar::Query<uint32_t>("gta4_frame_limit"), - .producer_backpressure = present.device != 0, + // [ps5 port] Decoupled, the game keeps the limit itself: the TV + // shows each frame as it comes (one clock, not two). With frame + // generation too: the game runs at half the TV's rate and FIFO + // shows the generated and the real image on consecutive vblanks. + .frame_rate_limit = REX_PLATFORM_PS5 && REXCVAR_GET(ps5_decoupled_presentation) + ? 0u + : CachedQuery<uint32_t>("gta4_frame_limit"), + + // (Frame generation pairs frames through this hold: kept for it.) + .producer_backpressure = present.device != 0 && + !(REX_PLATFORM_PS5 && REXCVAR_GET(ps5_decoupled_presentation) && + !rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)), + .tv_session_id = present.diagnostic_tv_session_id, .title_present_id = present.diagnostic_present_id, .selected_generation = present_source ? present_source->generation : 0, @@ -38181,6 +39707,7 @@ void Gta4NativeGraphicsSystem::DestroyNativeRendererObjects() { surface_images_by_handle_.clear(); pending_surface_release_ids_.clear(); native_placement_owners_.clear(); + ++placement_owners_erase_epoch_; next_surface_write_serial_ = 1; next_reflection_capture_epoch_ = 1; reflection_resources_.clear(); @@ -38429,6 +39956,7 @@ void Gta4NativeGraphicsSystem::DestroyVulkanWorkerObjects() { } void Gta4NativeGraphicsSystem::Shutdown() { + StopTextureWatch(); if (presenter_) presenter_->CancelFramePacingWaits(); if (render_worker_running_.exchange(false)) { render_condition_.notify_all(); @@ -38436,6 +39964,9 @@ void Gta4NativeGraphicsSystem::Shutdown() { render_worker_.join(); } } +#if REX_PLATFORM_PS5 + StopCaptureThread(); +#endif ShutdownDeferredDiagnosticWorker(); { diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.h index 0b9e1311..91db815e 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/graphics_system.h @@ -63,6 +63,7 @@ #include "native_attachment_policy.h" #include "native_binding_policy.h" #include "native_descriptor_backend.h" +#include "native_capture_ring.h" #include "native_draw_state_cache.h" #include "native_frame_context.h" #include "native_frame_scheduling.h" @@ -173,6 +174,14 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::array<VkShaderModule, 4> colorless_modules{}; std::string filename; std::vector<NativeVertexInput> vertex_inputs; + // [ps5 port] The recorder's filename classifications, kept on the shader: + // the same memos on each pipeline state snapshot missed for most draws (a + // new snapshot per state change), so the names were searched per draw. + // Recorder thread only. Pixel shaders keep their last vertex partner's. + mutable uint16_t translucent_range_memo = 0xFFFF; + mutable uint16_t category_pair_memo = 0xFFFF; + mutable const NativeShader* category_pair = nullptr; + mutable uint64_t category_pair_hash = 0; // Reads gl_VertexIndex/gl_InstanceIndex: its draws cannot be concatenated. bool uses_vertex_index = false; // Bytes of this stage's guest constant bank any of its modules can read @@ -206,6 +215,12 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { struct ProducerDeviceShaderState { uint32_t pixel_shader = 0; uint32_t vertex_shader = 0; + // [ps5 port] The texture masks last resolved for this shader pair, valid + // while producer_shader_metadata_generation_ is unchanged. + uint32_t memo_pixel_shader = 0, memo_vertex_shader = 0; + uint64_t memo_generation = 0; + uint32_t memo_texture_mask = 0, memo_vertex_texture_mask = 0; + bool memo_valid = false; }; struct NativeVertexDeclaration { @@ -308,7 +323,10 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { struct NativePersistentBufferEntry; - struct NativeBufferResource { + // [ps5 port] enable_shared_from_this: the game's render thread holds each + // resource once a frame and hands commands plain pointers; the render + // worker turns those back into owning references (UpgradeHeldResources). + struct NativeBufferResource : std::enable_shared_from_this<NativeBufferResource> { struct ConvertedVertexPayload { uint64_t declaration_hash = 0; uint64_t shader_hash = 0; @@ -320,15 +338,22 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { uint32_t last_used_frame = 0; }; + // [ps5 port] What the game's render thread reads for every use (handle to + // dropped) shares the first cache line. uint32_t handle = 0; uint32_t flags = 0; uint32_t guest_address = 0; uint32_t guest_size = 0; + mutable std::atomic<uint32_t> last_used_frame{0}; + // Set when reclamation drops it from buffer_resources_ (front caches). + mutable std::atomic<bool> dropped{false}; + uint32_t created_frame = 0; uint64_t content_hash = 0; uint64_t generation = 0; - uint32_t created_frame = 0; - mutable std::atomic<uint32_t> last_used_frame{0}; mutable std::atomic<size_t> shadow_validation_offset{0}; + // [ps5 port] The frame its shadow slice was last compared in (producer + // only; 0: never): the next frame's first use skips the compare. + mutable std::atomic<uint32_t> last_compared_frame{0}; std::vector<uint8_t> payload; // Built only by the single render worker. Keeping conversions with the // immutable source generation makes their lifetime follow the guest @@ -336,19 +361,20 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { mutable std::vector<ConvertedVertexPayload> converted_vertex_payloads; mutable std::vector<uint8_t> host_index16_payload; mutable std::vector<uint8_t> host_index32_payload; - mutable NativeTriangleFanCache triangle_fan_cache; - // Recorder-only: host-endian index ranges reordered for the vertex cache - // (NativeVertexCacheOptimizer), keyed by start << 32 | count. - mutable std::unordered_map<uint64_t, std::shared_ptr<NativeVertexCacheRange>> - vertex_cache_ranges; - mutable NativeOwnerRetirementWatch<NativePersistentBufferEntry> persistent_retirement; // Recorder-only: the persistent buffer entry this resource resolved to // last (GetOrCreatePersistentBuffer), with its key's bytes and the map's // clear epoch. Entries are erased only after their owner expired, so the - // pointer stays valid while this resource exists. + // pointer stays valid while this resource exists. [ps5 port] Next to the + // payloads: with them, what recording a draw reads (the first four lines). mutable std::array<uint64_t, 6> persistent_memo_key{}; mutable NativePersistentBufferEntry* persistent_memo_entry = nullptr; mutable uint64_t persistent_memo_epoch = 0; + mutable NativeTriangleFanCache triangle_fan_cache; + // Recorder-only: host-endian index ranges reordered for the vertex cache + // (NativeVertexCacheOptimizer), keyed by start << 32 | count. + mutable std::unordered_map<uint64_t, std::shared_ptr<NativeVertexCacheRange>> + vertex_cache_ranges; + mutable NativeOwnerRetirementWatch<NativePersistentBufferEntry> persistent_retirement; }; enum class NativeTextureOrigin : uint8_t { @@ -358,7 +384,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { kPackedDepthAlias, }; - struct NativeTextureResource { + struct NativeTextureResource : std::enable_shared_from_this<NativeTextureResource> { struct MipLevel { uint32_t level = 0; uint32_t width = 0; @@ -391,8 +417,13 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { }; uint32_t handle = 0; + // Set (atomically) when eviction drops it from texture_resources_, for the + // producer's front cache. [ps5 port] With generation and the packed depth + // source, what the game's render thread reads per use: the first line. + mutable bool dropped = false; uint64_t content_hash = 0; uint64_t generation = 0; + std::shared_ptr<const NativeTextureResource> packed_depth_source; xenos::xe_gpu_texture_fetch_t fetch{}; TextureInfo info{}; // With detile_jobs, payload holds raw guest bytes (gta4_native_gpu_detile) @@ -408,7 +439,6 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { NativeTextureOrigin origin = NativeTextureOrigin::kGuestSnapshot; uint64_t virtual_lifetime = 0; bool gpu_produced = false; - std::shared_ptr<const NativeTextureResource> packed_depth_source; bool vector_font_replacement = false; uint32_t vector_font_id = 0; // Render worker only: the assembly epoch in which this resource's @@ -491,11 +521,23 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { if (kept.capacity() * sizeof(kept[0]) > kKeptBytes) kept = {}; return kept; }; + // Larger constant payloads go to the shared stash for the next capture. + const auto keep_payload = [](std::vector<uint8_t>& buffer) { + std::vector<uint8_t> kept = std::move(buffer); + kept.clear(); + if (kept.capacity() > kKeptBytes) { + LargeConstantPayloadStash::Put(std::move(kept)); + kept = {}; + } + return kept; + }; auto vertex_ranges = keep(shader_constant_delta.vertex_constants.ranges); - auto vertex_payload = keep(shader_constant_delta.vertex_constants.payload); + auto vertex_payload = keep_payload(shader_constant_delta.vertex_constants.payload); auto pixel_ranges = keep(shader_constant_delta.pixel_constants.ranges); - auto pixel_payload = keep(shader_constant_delta.pixel_constants.payload); + auto pixel_payload = keep_payload(shader_constant_delta.pixel_constants.payload); auto kept_pending_state = keep(pending_state); + // A draw's own vertex data (DrawPrimitiveUP) goes to the stash too. + if (payload.capacity()) LargeConstantPayloadStash::Put(std::move(payload)); std::destroy_at(this); std::construct_at(this); pending_state = std::move(kept_pending_state); @@ -515,6 +557,10 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { uint8_t temporal_composite_mode = 0; // Render-worker-only result; captured guest state stays immutable. mutable bool split_postfx_applied = false; + // [ps5 port] Which trace pointers below are set (kTraceFire...), so the + // per-command checks read this first line instead of the pointers' line. + uint8_t trace_flags = 0; + static constexpr uint8_t kTraceFire = 1, kTraceBulb = 2, kTracePhone = 4, kTraceTv = 8; RenderPhase render_phase = RenderPhase::kUnknown; uint32_t render_phase_object = 0; uint32_t used_texture_mask = 0; @@ -526,6 +572,15 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { // resolve as an image swap (A2); UINT32_MAX otherwise. uint32_t resolve_swap = UINT32_MAX; uint32_t temporal_scene_stage = UINT32_MAX; + // [ps5 port] Lean capture: resource fields the game's render thread filled + // with plain (non-owning) pointers to resources it holds for the frame. + // Bits 0-16 vertex_buffers, 17 index_buffer, 18+ textures[stage]. The + // render worker makes them owning references when it takes the command + // (UpgradeHeldResources), before anything else can keep a copy. + uint64_t held_resource_mask = 0; + static constexpr uint32_t kHeldIndexBufferBit = kVertexStreamCount, + kHeldTextureBit = kVertexStreamCount + 1; + static_assert(kHeldTextureBit + kTextureStageCount <= 64); NativeCommandBytes bytes; std::shared_ptr<const NativeShaderState> shader_state; std::shared_ptr<const NativePipelineState> pipeline_state; @@ -623,7 +678,48 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { // since the previous queued command. The worker applies them, in order, // before this command, exactly as if each had been queued on its own. std::vector<uint8_t> pending_state; - }; + // [ps5 port] The game's render thread's per-frame resource holds, carried + // by the frame's present: released by the render worker once every earlier + // command has had its held pointers made owning. + std::vector<std::shared_ptr<const void>> released_holds; + }; + // [ps5 port] The cache lines of a command the game's render thread writes + // or reads when it captures a draw: the pool brings only these to its core + // ahead of time (the rest it never touches). + static uint64_t TitleTouchedCommandLines() { + uint64_t lines = 0; + const auto add = [&](size_t offset, size_t size) { + for (size_t line = offset / 64; line <= (offset + size - 1) / 64 && line < 64; ++line) + lines |= uint64_t(1) << line; + }; +#define REX_NATIVE_COMMAND_FIELD(field) \ + add(offsetof(NativeCommand, field), sizeof(NativeCommand::field)) + // The first group: type through the shared state pointers and index buffer. + add(0, offsetof(NativeCommand, temporal_scene_binding)); + REX_NATIVE_COMMAND_FIELD(resolve_destination); + REX_NATIVE_COMMAND_FIELD(depth_handoff_source); + REX_NATIVE_COMMAND_FIELD(present_source); + REX_NATIVE_COMMAND_FIELD(postfx_half_scene); + REX_NATIVE_COMMAND_FIELD(temporal_instance); + REX_NATIVE_COMMAND_FIELD(lighting); + REX_NATIVE_COMMAND_FIELD(light_trace_id); + REX_NATIVE_COMMAND_FIELD(dirty_components); + REX_NATIVE_COMMAND_FIELD(fixed_function_state); + // The first streams and stages; the rest of each array is rarely used. + add(offsetof(NativeCommand, vertex_buffers), 2 * sizeof(NativeCommand::vertex_buffers[0])); + add(offsetof(NativeCommand, textures), 6 * sizeof(NativeCommand::textures[0])); + add(offsetof(NativeCommand, texture_fetches), 4 * sizeof(NativeCommand::texture_fetches[0])); + REX_NATIVE_COMMAND_FIELD(protected_generations); + REX_NATIVE_COMMAND_FIELD(protected_generation_count); + REX_NATIVE_COMMAND_FIELD(payload); + REX_NATIVE_COMMAND_FIELD(device_snapshot); + REX_NATIVE_COMMAND_FIELD(shader_constant_delta); + REX_NATIVE_COMMAND_FIELD(snapshot_render_targets); + REX_NATIVE_COMMAND_FIELD(snapshot_depth_stencil); + REX_NATIVE_COMMAND_FIELD(pending_state); +#undef REX_NATIVE_COMMAND_FIELD + return lines; + } struct NativeUploadBuffer { VkBuffer buffer = VK_NULL_HANDLE; @@ -1150,6 +1246,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { struct NativeCachedDescriptorState; struct NativePipelineCompilerState; + struct NativePlacementOwner; struct NativeSurfaceImage { uint64_t lifetime_id = 0; @@ -1178,6 +1275,11 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { NativeSurfaceAspectContent aspect_content{}; GuestPlacementKey materialized_placement{}; uint64_t materialized_serial = 0; + // [ps5 port] Recorder-only: the native_placement_owners_ entry this surface + // last claimed, for that key, while placement_owners_erase_epoch_ matches. + NativePlacementOwner* claimed_owner_entry = nullptr; + GuestPlacementKey claimed_owner_key{}; + uint64_t claimed_owner_epoch = 0; uint32_t created_frame = 0; uint32_t last_used_frame = 0; uint64_t last_used_submission = 0; @@ -1494,10 +1596,15 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { static uint64_t HashFixedFunctionState(const NativeFixedFunctionState& state); std::shared_ptr<const NativeTextureResource> CreateResolvedTextureResource( const ResolveCommand& command); - std::shared_ptr<const NativeBufferResource> CaptureBufferResource(uint32_t handle); + // hold (lean capture): may return a plain pointer to a resource held for the + // frame, wrapped in a shared_ptr that owns nothing; *held is then set. + std::shared_ptr<const NativeBufferResource> CaptureBufferResource(uint32_t handle, + bool hold = false, + bool* held = nullptr); SurfaceDescriptor CaptureSurfaceDescriptor(uint32_t handle) const; std::shared_ptr<const NativeTextureResource> CaptureTextureResource( - uint32_t handle, const xenos::xe_gpu_texture_fetch_t& fetch, uint32_t stage); + uint32_t handle, const xenos::xe_gpu_texture_fetch_t& fetch, uint32_t stage, + bool hold = false, bool* held = nullptr); std::shared_ptr<const NativeTextureResource> CaptureTextureResourceUncached( uint32_t handle, const xenos::xe_gpu_texture_fetch_t& fetch, uint32_t stage, bool& cacheable); @@ -1550,6 +1657,8 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { void StopNativePipelineCompiler(); void ReplayNativePipelineRecipes(); void LoadNativePipelineRecipes(); + // Adds a recipe file's records not known yet (for replay); how many. + size_t LoadNativePipelineRecipeFile(const std::filesystem::path& path); VkPipeline PublishNativePipeline(const NativePipelineKey& key, VkPipeline pipeline, uint64_t compile_ticks); void RecordNativePipelineTiming(uint64_t compile_ticks, uint64_t wait_ticks = 0); @@ -1775,13 +1884,13 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { void QueueTextureProtection(const NativeCommand& command, bool retain); void QueueTextureProtectionLocked(const NativeCommand& command, bool retain); static void CollectTextureProtection(const NativeCommand& command); - void AppendQueuedTextureProtection(std::unordered_set<uint64_t>& generations) const; + void AppendQueuedTextureProtection(NativeGenerationSet& generations) const; void ClearNativeFrameCommands(); static void AddProtectedTextureGenerations(const NativeCommand& command, - std::unordered_set<uint64_t>& generations); + NativeGenerationSet& generations); void CollectProtectedTextureGenerations( const std::shared_ptr<const NativeTextureResource>& present_source, - std::unordered_set<uint64_t>& generations); + NativeGenerationSet& generations); NativeTextureHeapBudgets QueryNativeTextureHeapBudgets() const; bool AllocateNativeTextureImage(const VkImageCreateInfo& image_info, NativeTextureImage& image); void EvictNativeTextureImages(uint32_t submitted_frame, bool allocation_recovery); @@ -1883,6 +1992,17 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { void TryPrewarmDrawPipeline(const NativeCommand& command); bool GetRequiredVertexStreams(const NativePipelineState& state, std::array<bool, kVertexStreamCount>& required_streams) const; + bool RecorderRequiredVertexStreams(const NativePipelineState& state, + std::array<bool, kVertexStreamCount>& required_streams); + struct RequiredStreamsMemo { + const NativeShader* shader = nullptr; + uint64_t shader_hash = 0; + uint64_t declaration_generation = 0; + std::array<bool, kVertexStreamCount> streams{}; + bool result = false; + bool valid = false; + }; + RequiredStreamsMemo recorder_required_streams_memo_; bool UploadBufferResource(const std::shared_ptr<const NativeBufferResource>& resource, VkCommandBuffer command_buffer, bool index_buffer, bool index32, const NativePipelineState* vertex_state, uint32_t vertex_stream, @@ -1989,7 +2109,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { // across presents, allocation recovery and synchronous readback boundaries. NativeOwnedCommands<NativeCommand> worker_command_batch_; size_t worker_command_cursor_ = 0; - void AppendWorkerTextureProtection(std::unordered_set<uint64_t>& generations) const; + void AppendWorkerTextureProtection(NativeGenerationSet& generations) const; // Its own lock, not render_mutex_: the title thread retains a command's // generations before queueing it and the worker releases them after taking // it, so neither holds the queue lock for this. In between, the command is @@ -2061,6 +2181,10 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { uint32_t size = 0; uint64_t epoch = 0; std::weak_ptr<const NativeBufferResource> resource; + // [ps5 port] Lean capture: the resource, held for hold epoch hold_epoch + // (the frame) by held_resources_; valid only while that epoch is current. + const NativeBufferResource* held = nullptr; + uint32_t hold_epoch = 0; bool recent = false; }; // Two-way sets under a multiplicative hash: far views draw a few thousand @@ -2082,6 +2206,14 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { second.recent = way == &second; return *way; } + // The set a handle's slots are in, for a prefetch (CaptureCacheWay also + // updates the ways' recent flags). + template <typename Slots> + static const void* CaptureCacheSet(const Slots& slots, uint32_t handle) { + constexpr size_t kSets = std::tuple_size_v<Slots> / 2; + const size_t set = size_t((handle * 0x9E3779B1u) >> (32 - std::countr_zero(kSets))); + return &slots[set * 2]; + } template <typename Slots> static void ForgetCaptureCache(Slots& slots, uint32_t handle) { constexpr size_t kSets = std::tuple_size_v<Slots> / 2; @@ -2089,7 +2221,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { for (size_t way = 0; way < 2; ++way) if (slots[set * 2 + way].handle == handle) slots[set * 2 + way].handle = 0; } - std::array<BufferCaptureCacheSlot, 8192> buffer_capture_cache_{}; // command_capture_mutex_ + std::array<BufferCaptureCacheSlot, 16384> buffer_capture_cache_{}; // command_capture_mutex_ std::atomic<uint64_t> buffer_resources_epoch_{1}; BufferCaptureCacheSlot& BufferCaptureSlot(uint32_t handle) { return CaptureCacheWay(buffer_capture_cache_, handle); @@ -2102,12 +2234,16 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { // that changes the maps bumps the epoch. struct TextureCaptureCacheSlot { uint32_t handle = 0; + uint32_t hold_epoch = 0; // [ps5 port] As in BufferCaptureCacheSlot. std::array<uint32_t, 6> fetch{}; - uint64_t epoch = 0; - std::weak_ptr<const NativeTextureResource> resource; + uint32_t epoch = 0; // texture_resources_epoch_'s low bits bool recent = false; + std::weak_ptr<const NativeTextureResource> resource; + const NativeTextureResource* held = nullptr; }; - std::array<TextureCaptureCacheSlot, 4096> texture_capture_cache_{}; // command_capture_mutex_ + static_assert(sizeof(TextureCaptureCacheSlot) == 64); + // A set is two slots, two whole cache lines. + alignas(64) std::array<TextureCaptureCacheSlot, 8192> texture_capture_cache_{}; // command_capture_mutex_ std::atomic<uint64_t> texture_resources_epoch_{1}; TextureCaptureCacheSlot& TextureCaptureSlot(uint32_t handle) { return CaptureCacheWay(texture_capture_cache_, handle); @@ -2169,7 +2305,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { NativeOwnedCommands<NativeCommand> assembly_frame_; // Worker-side protection visible to the recorder's eviction passes. Guarded // by worker_protection_mutex_ together with the worker batch and cursor. - std::unordered_set<uint64_t> assembly_texture_protection_; + NativeGenerationSet assembly_texture_protection_; // Bumped whenever assembly_texture_protection_ is replaced, so a resource // stamped with the current epoch is known to be in the set already. uint32_t assembly_protection_epoch_ = 1; @@ -2197,7 +2333,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { // render_mutex_. void PublishStagedDrawCommandsLocked(); NativeFrameResources recording_resources_; - std::unordered_set<uint64_t> frame_texture_protection_; + NativeGenerationSet frame_texture_protection_; const NativeCommand* active_worker_command_ = nullptr; NativeLightingLineageLedger semantic_light_setup_lineage_; std::unordered_map<uint32_t, EnvironmentalDataV2> environmental_data_by_device_; @@ -2210,6 +2346,11 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::unordered_map<uint32_t, NativeShader*> shader_handles_; std::unordered_map<uint32_t, ProducerShaderMetadata> producer_shader_metadata_; std::unordered_map<uint32_t, ProducerDeviceShaderState> producer_device_shader_states_; + // Bumped whenever producer_shader_metadata_ changes (memos above). + uint64_t producer_shader_metadata_generation_ = 1; + // The last device's entry (map nodes stay put until erased or cleared). + uint32_t producer_last_shader_device_ = 0; + ProducerDeviceShaderState* producer_last_shader_state_ = nullptr; std::unordered_map<uint32_t, std::shared_ptr<NativeVertexDeclaration>> vertex_declarations_; std::mutex buffer_resource_mutex_; std::unordered_map<uint32_t, std::shared_ptr<const NativeBufferResource>> buffer_resources_; @@ -2221,9 +2362,90 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::unordered_map<uint32_t, uint32_t> vector_font_ids_; NativeVirtualResourceRegistry virtual_resource_registry_; uint64_t next_texture_generation_ = 1; + // [ps5 port] A streamed texture can land where a freed one was, with the same + // header and fetch and no unlock or release, so the cache kept the old image + // (a jacket on Niko's face after changing clothes). A background thread + // samples each cached texture's memory and hands back the ones that changed. + struct WatchedTexture { + std::weak_ptr<const NativeTextureResource> resource; + uint32_t handle = 0, address = 0, size = 0; + uint64_t generation = 0, probe = 0; + }; + struct ChangedTexture { + uint32_t handle = 0; + uint64_t generation = 0; + auto operator<=>(const ChangedTexture&) const = default; + }; + void WatchTexture(const std::shared_ptr<const NativeTextureResource>& resource); + void TextureWatchMain(); + void TakeChangedTextures(); // producer + void StopTextureWatch(); + uint64_t ProbeGuestTexture(uint32_t address, uint32_t size) const; + std::mutex texture_watch_mutex_; + std::condition_variable texture_watch_condition_; + std::vector<WatchedTexture> texture_watch_added_; + std::vector<ChangedTexture> texture_watch_changed_; + std::atomic<bool> texture_watch_changed_pending_{false}; + bool texture_watch_stop_ = false; + std::thread texture_watch_thread_; + std::atomic<uint64_t> texture_watch_changes_{0}; +#if REX_PLATFORM_PS5 + // [ps5 port] Taken for every command the game submits (12,000-16,000 a + // frame in the city) and almost never contended: a spin lock instead of + // two libkernel calls each time. + class CaptureMutex { + public: + void lock() { + int spins = 0; + while (locked_.exchange(true, std::memory_order_acquire)) { + while (locked_.load(std::memory_order_relaxed)) { + if (++spins < 1024) { + __builtin_ia32_pause(); + } else { + std::this_thread::yield(); + spins = 0; + } + } + } + } + bool try_lock() { + return !locked_.load(std::memory_order_relaxed) && + !locked_.exchange(true, std::memory_order_acquire); + } + void unlock() { locked_.store(false, std::memory_order_release); } + + private: + std::atomic<bool> locked_{false}; + }; + CaptureMutex command_capture_mutex_; +#else std::mutex command_capture_mutex_; +#endif // State commands absorbed since the last queued command (command_capture_mutex_). std::vector<uint8_t> producer_pending_state_; + // [ps5 port] Lean capture (command_capture_mutex_). Each buffer and texture + // a frame's draws use is held once (held_resources_, one reference count + // change) instead of once per draw; the commands get plain pointers, made + // owning on the render worker. The frame's present carries the holds to the + // worker, which drops them after every earlier command; the epoch then + // moves on and the capture slots' held pointers go stale with it. + bool lean_capture_ = REX_PLATFORM_PS5; + // Lean capture's prefetching (the guest objects and held resources a draw + // reads, the next pooled command's buffers): on, or off for a Compare run. + bool capture_prefetch_ = REX_PLATFORM_PS5; + uint32_t hold_epoch_ = 1; + // Holds last this many frames (hold_window_frames_ counts the current one's). + // Four: about 1300 holds a frame in busy streets at two, each a reference + // count change on a line the worker also writes; half as many at four, for + // two frames more of keeping what the game let go of. + static constexpr uint32_t kHoldFrames = 4; + uint32_t hold_window_frames_ = 0; + std::vector<std::shared_ptr<const void>> held_resources_; + uint64_t holds_taken_ = 0, held_uses_ = 0; // for the capture line + std::atomic<uint64_t> holds_taken_reported_{0}, held_uses_reported_{0}; + void UpgradeHeldResources(NativeCommand& command); + void PrefetchHeldCaptures(std::span<const uint8_t> device_state, uint32_t texture_mask, + bool indexed); std::mutex device_snapshot_mutex_; std::unordered_map<uint32_t, std::shared_ptr<const std::vector<uint8_t>>> last_device_snapshots_; std::unordered_set<uint32_t> initialized_constant_capture_devices_; @@ -2498,6 +2720,22 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { } native_memory_profile_state_; PostFxResourcePool postfx_resource_pool_; temporal::CommandCapture temporal_command_capture_; + // Set once any draw got room-light input bindings (a diagnostic probe). + bool room_light_bindings_issued_ = false; + // State commands the worker applied, and those that changed nothing. + std::atomic<uint64_t> state_commands_applied_{0}, state_commands_repeated_{0}; + // GetOrCreateSurfaceImage's recent hits, valid for one surface lookup epoch + // (bumped by anything that changes what a lookup would return). + struct SurfaceLookupMemo { + SurfaceDescriptor descriptor{}; + bool depth = false; + VkSampleCountFlagBits sample_override = VK_SAMPLE_COUNT_FLAG_BITS_MAX_ENUM; + uint64_t epoch = 0; + NativeSurfaceImage* image = nullptr; + }; + std::array<SurfaceLookupMemo, 8> surface_lookup_memo_{}; + std::atomic<uint64_t> surface_lookup_epoch_{1}; + void BumpSurfaceLookupEpoch() { surface_lookup_epoch_.fetch_add(1, std::memory_order_acq_rel); } std::mutex temporal_provider_mutex_; temporal::FsrUpscalerVulkan fsr_upscaler_; temporal::DlssUpscaler dlss_upscaler_; @@ -2526,6 +2764,12 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { NativeSurfaceImage* temporal_depth_source_ = nullptr; uint64_t temporal_composite_scope_ = 0, temporal_last_sequence_ = 0, temporal_last_time_ns_ = 0, temporal_last_epoch_ = 0; + // [ps5 port] The scenes counted one after another for FSR's history (the + // guest's own frame numbers, last seen, can skip: PrepareTemporalFrame). + uint64_t temporal_history_sequence_ = 0, temporal_last_guest_sequence_ = 0; + uint64_t TemporalHistorySequence() const { + return REX_PLATFORM_PS5 ? temporal_history_sequence_ : temporal_metadata_.sequence; + } std::array<float, 16> temporal_previous_projection_{}, temporal_previous_inverse_{}; std::array<float, 2> temporal_half_pixel_{}; bool temporal_assets_checked_ = false, temporal_assets_ready_ = false, @@ -2701,6 +2945,9 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::filesystem::path native_pipeline_cache_path_; uint32_t native_pipeline_cache_title_id_ = 0; std::atomic<uint64_t> native_pipeline_cache_generation_{0}; + // [ps5 port] The pipelines shipped with the app (InitializeNativePipelineCache). + std::filesystem::path native_pipeline_seed_path_; + bool native_pipeline_cache_seeded_ = false; uint64_t native_pipeline_cache_saved_generation_ = 0; std::unique_ptr<NativePipelineCompilerState> native_pipeline_compiler_; uint64_t native_pipeline_unreported_compile_ticks_ = 0; @@ -2784,7 +3031,7 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::unordered_map<uint64_t, std::unique_ptr<NativeTextureImage>> native_texture_images_; std::map<uint64_t, std::vector<std::unique_ptr<NativeTextureImage>>> retired_texture_images_; size_t retired_texture_image_count_ = 0; - std::unordered_set<uint64_t> protected_texture_generations_; + NativeGenerationSet protected_texture_generations_; std::unordered_set<uint64_t> packed_alias_generations_; // Live aliases, not scene history. std::map<NativeFlightResourceKey, NativeFlightResourceReference> staged_native_flight_resources_; std::array<std::optional<NativeFlightSubmission>, NativeFrameContextRing::kSlotCount> @@ -2811,8 +3058,106 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { std::atomic<uint64_t> buffer_fast_path_disable_count_{0}; std::atomic<uint64_t> buffer_fast_path_request_count_{0}; std::atomic<bool> buffer_fast_path_disabled_{false}; + // Producer only (command_capture_mutex_): buffers the title changed without + // an unlock, compared in full on every capture. + std::unordered_set<uint32_t> buffer_full_validation_handles_; + // Capture counters: one writer (the producer), read by the PS5's report. + struct SingleWriterCounter { + std::atomic<uint64_t> value{0}; + void operator++() { value.store(value.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); } + void operator+=(uint64_t n) { value.store(value.load(std::memory_order_relaxed) + n, std::memory_order_relaxed); } + uint64_t Get() const { return value.load(std::memory_order_relaxed); } + }; + struct BufferCaptureStats { + SingleWriterCounter front_hits, lookups, full_compares, full_compare_bytes, copies, copy_bytes; + }; + BufferCaptureStats buffer_capture_stats_; + SingleWriterCounter texture_capture_stats_front_hits_, texture_capture_stats_lookups_; + // Commands the game submitted, and how many were state folded into the next. + SingleWriterCounter title_commands_submitted_, title_commands_coalesced_; + std::atomic<uint32_t> buffer_full_validation_count_{0}; NativePeriodicWorkSchedule buffer_cache_poll_schedule_{kNativeBufferCachePollPhaseFrames}; bool buffer_cache_reclamation_pending_ = false; +#if REX_PLATFORM_PS5 + // [ps5 port] Out-of-memory handling (NotePs5MemoryShort). + uint32_t ps5_persistent_block_retry_frame_ = 0; + // The Renderer Threads mask each thread last applied (FollowRendererCpuMask). + uint64_t worker_cpu_mask_applied_ = 0; + uint64_t recorder_cpu_mask_applied_ = 0; + uint64_t capture_cpu_mask_applied_ = 0; + // [ps5 port] Capture on its own core (capture_thread.inc). The game's render + // thread copies each command, with the device state lines it changed since + // the last draw, into capture_ring_; the capture thread keeps a copy of each + // device (capture_shadows_) up to date from them and runs the capture the + // render thread used to run, reading that copy instead of guest memory. + struct CaptureDeviceCopy { + uint32_t device = 0; + bool primed = false; + alignas(64) std::array<uint8_t, kGuestDeviceSize> bytes{}; + }; + int SubmitTitleCommandOnCore(const void* command, size_t command_size); + // (shadow_device/shadow/up_payload: from the capture thread, installed once + // the capture lock is held, for that one command) + bool SubmitTitleCommandHere(uint32_t title_id, uint32_t abi_version, const void* command, + size_t command_size, uint32_t shadow_device = 0, + const uint8_t* shadow = nullptr, const uint8_t* up_payload = nullptr, + const uint8_t* objects = nullptr, uint32_t object_count = 0); + void FinishCaptureOnCore(); // every command submitted so far has been captured + void CaptureThreadMain(); + void StopCaptureThread(); + CaptureDeviceCopy& CaptureMirror(uint32_t device); + CaptureDeviceCopy& CaptureShadow(uint32_t device); + const uint8_t* CaptureDeviceMemory(uint32_t device) const { + return device && device == capture_shadow_device_ ? capture_shadow_memory_ + : memory_->TranslateVirtual<const uint8_t*>(device); + } + std::unique_ptr<NativeCaptureRing> capture_ring_; + std::atomic<NativeCaptureRing*> capture_ring_published_{nullptr}; // (for StopCaptureThread, lock-free) + std::thread capture_thread_; + std::atomic<bool> capture_thread_running_{false}; + // The submitters' side (capture_push_mutex_): whether commands go to the + // ring, and each device's lines as last sent. + CaptureMutex capture_push_mutex_; + bool capture_on_core_ = false; + bool capture_stopped_ = false; // StopCaptureThread ran + std::atomic<bool> capture_on_core_flag_{false}; // capture_on_core_, readable without the lock + std::vector<std::unique_ptr<CaptureDeviceCopy>> capture_mirrors_; + CaptureDeviceCopy* capture_last_mirror_ = nullptr; + // The capture thread's side. + std::vector<std::unique_ptr<CaptureDeviceCopy>> capture_shadows_; + CaptureDeviceCopy* capture_last_shadow_ = nullptr; + uint32_t capture_shadow_device_ = 0; + const uint8_t* capture_shadow_memory_ = nullptr; + const uint8_t* capture_up_payload_ = nullptr; + // The bound surfaces' and buffers' guest objects as they were at the draw + // (handle, then the object's first kCaptureObjectBytes): the game re-points + // its render target objects from pass to pass. + static constexpr uint32_t kCaptureObjectBytes = 44; + static constexpr uint32_t kCaptureObjectEntry = 48; + const uint8_t* capture_objects_ = nullptr; + uint32_t capture_object_count_ = 0; + const uint8_t* CaptureObjectMemory(uint32_t handle) const { + for (uint32_t i = 0; i < capture_object_count_; ++i) { + const uint8_t* entry = capture_objects_ + size_t(i) * kCaptureObjectEntry; + uint32_t entry_handle; + std::memcpy(&entry_handle, entry, sizeof(entry_handle)); + if (entry_handle == handle) return entry + 4; + } + return memory_->TranslateVirtual<const uint8_t*>(handle); + } + uint32_t ps5_memory_short_frames_ = 0; + void NotePs5MemoryShort(const char* what); +#endif + // A sweep continued over several frames (ReleaseUnusedBufferResources). + struct BufferReclamationCandidate { + uint32_t handle = 0; + uint64_t generation = 0; + uint32_t last_used_frame = 0; + uint64_t retained_bytes = 0; + }; + std::vector<BufferReclamationCandidate> buffer_reclamation_candidates_; + size_t buffer_reclamation_next_ = 0; + uint64_t buffer_reclamation_cache_bytes_ = 0; uint64_t texture_image_eviction_count_ = 0; uint64_t texture_image_evicted_bytes_ = 0; uint64_t texture_allocation_retry_count_ = 0; @@ -2834,6 +3179,9 @@ class Gta4NativeGraphicsSystem final : public system::IGraphicsSystem { uint64_t next_surface_lifetime_id_ = 1; std::unordered_map<GuestPlacementKey, NativePlacementOwner, GuestPlacementKeyHash> native_placement_owners_; + // Bumped by every erase from native_placement_owners_ (inserts keep entries + // in place), so a surface's claimed_owner_entry is valid while it matches. + uint64_t placement_owners_erase_epoch_ = 1; uint64_t next_surface_write_serial_ = 1; uint64_t next_reflection_capture_epoch_ = 1; NativeReflectionRegistry reflection_resources_; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/modern_shader_options.cpp b/glue/rexglue-sdk-main/src/graphics/gta4_native/modern_shader_options.cpp index b5d2ffca..3b32d749 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/modern_shader_options.cpp +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/modern_shader_options.cpp @@ -1,3 +1,4 @@ +#include "cached_cvar.h" #include "modern_shader_options.h" #include "modern_shader_diagnostics.h" #include <rex/cvar.h> @@ -14,14 +15,14 @@ REXCVAR_DEFINE_BOOL(gta4_trace_modern_shader_gpu, false, "GTA IV/Diagnostics", namespace rex::graphics::gta4_native { ModernShaderSettings ReadModernShaderSettings() { - return {rex::cvar::Query<bool>("gta4_modern_shaders"), - rex::cvar::Query<bool>("gta4_disable_tlad_film_grain")}; + return {CachedQuery<bool>("gta4_modern_shaders"), + CachedQuery<bool>("gta4_disable_tlad_film_grain")}; } bool ModernShaderTraceEnabled() { - return rex::cvar::Query<bool>("gta4_trace_modern_shaders") || ModernShaderGpuProbeEnabled() || + return CachedQuery<bool>("gta4_trace_modern_shaders") || ModernShaderGpuProbeEnabled() || rex::diagnostics::IsEnabled(rex::diagnostics::Category::kNativeTrace); } -bool ModernShaderGpuProbeEnabled() { return rex::cvar::Query<bool>("gta4_trace_modern_shader_gpu"); } +bool ModernShaderGpuProbeEnabled() { return CachedQuery<bool>("gta4_trace_modern_shader_gpu"); } } // namespace rex::graphics::gta4_native namespace rex::graphics::gta4_native { diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_capture_ring.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_capture_ring.h new file mode 100644 index 00000000..b84e6e9e --- /dev/null +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_capture_ring.h @@ -0,0 +1,205 @@ +#ifndef REX_GRAPHICS_GTA4_NATIVE_NATIVE_CAPTURE_RING_H_ +#define REX_GRAPHICS_GTA4_NATIVE_NATIVE_CAPTURE_RING_H_ + +// [ps5 port] Capture on its own core: the byte ring between the game's render +// thread (and any other thread submitting title commands, one at a time under +// the submitters' lock) and the capture thread, which runs the capture the +// render thread used to run itself. +// +// Records are 16-byte aligned and never split across the end: a record that +// would not fit leaves a wrap mark (kWrapMark in its first word) and starts +// again at the front. Head and tail only grow; each side keeps a copy of the +// other's position and reads the real one only when that copy says the ring is +// full (or empty), so the two threads don't trade a cache line per record. + +#include <atomic> +#include <chrono> +#include <condition_variable> +#include <cstddef> +#include <cstdint> +#include <cstdlib> +#include <cstring> +#include <mutex> + +#if defined(__x86_64__) +#include <immintrin.h> +#endif + +namespace rex::graphics::gta4_native { + +class NativeCaptureRing { + public: + static constexpr size_t kCapacity = size_t(8) << 20; + static constexpr uint32_t kWrapMark = 0xFFFFFFFFu; + static constexpr size_t kAlign = 16; + + NativeCaptureRing() { + data_ = static_cast<uint8_t*>(std::aligned_alloc(4096, kCapacity)); + } + ~NativeCaptureRing() { std::free(data_); } + NativeCaptureRing(const NativeCaptureRing&) = delete; + NativeCaptureRing& operator=(const NativeCaptureRing&) = delete; + + bool valid() const { return data_ != nullptr; } + static constexpr size_t Align(size_t size) { return (size + kAlign - 1) & ~(kAlign - 1); } + + // ---- Producer (one at a time) ---- + // Room for `size` bytes (aligned), waiting for the capture thread to make + // some; the wait in microseconds is added to *waited_us. + uint8_t* Reserve(size_t size, uint64_t* waited_us) { + const size_t offset = size_t(head_ & (kCapacity - 1)); + skip_ = offset + size > kCapacity ? kCapacity - offset : 0; + const uint64_t end = head_ + skip_ + size; + if (end - cached_tail_ > kCapacity) { + cached_tail_ = tail_.load(std::memory_order_acquire); + if (end - cached_tail_ > kCapacity) { + const auto begin = std::chrono::steady_clock::now(); + WaitFor([&] { + cached_tail_ = tail_.load(std::memory_order_acquire); + return end - cached_tail_ <= kCapacity; + }); + if (waited_us) + *waited_us += uint64_t(std::chrono::duration_cast<std::chrono::microseconds>( + std::chrono::steady_clock::now() - begin) + .count()); + } + } + if (skip_) { + const uint32_t mark = kWrapMark; + std::memcpy(data_ + offset, &mark, sizeof(mark)); + } + return data_ + size_t((head_ + skip_) & (kCapacity - 1)); + } + // Publishes the record Reserve returned. A plain store: a locked one would + // wait for the capture thread's copy of the line every record. A present + // (and every record while the capture thread sleeps) is fenced, so the + // capture thread is never left asleep with a frame's end in the ring; a + // record whose wake raced the capture thread falling asleep is found by + // the next record, or within a millisecond (WaitForRecords). + void Commit(size_t size, bool present) { + head_ += skip_ + size; + skip_ = 0; + if (present) presents_.fetch_add(1, std::memory_order_relaxed); + head_published_.store(head_, std::memory_order_release); + if (present) std::atomic_thread_fence(std::memory_order_seq_cst); + if (consumer_sleeping_.load(std::memory_order_relaxed)) { + std::atomic_thread_fence(std::memory_order_seq_cst); + std::lock_guard lock(mutex_); + consumer_condition_.notify_one(); + } + } + uint64_t head() const { return head_; } + uint32_t presents_queued() const { return presents_.load(std::memory_order_acquire); } + // Waits (spinning a little, then sleeping) until `done` holds; the capture + // thread wakes producers after every record while one is waiting. + template <typename Done> + void WaitFor(Done&& done) { + for (int spin = 0; spin < 2000; ++spin) { + if (done() || stopped_.load(std::memory_order_relaxed)) return; + Pause(); + } + std::unique_lock lock(mutex_); + producer_waiting_.fetch_add(1, std::memory_order_seq_cst); + while (!done() && !stopped_.load(std::memory_order_acquire)) + producer_condition_.wait_for(lock, std::chrono::milliseconds(2)); + producer_waiting_.fetch_sub(1, std::memory_order_relaxed); + } + // At shutdown: nothing will be captured any more; every wait ends. + void Stop() { + stopped_.store(true, std::memory_order_release); + std::lock_guard lock(mutex_); + producer_condition_.notify_all(); + consumer_condition_.notify_all(); + } + bool stopped() const { return stopped_.load(std::memory_order_acquire); } + // Every record published before this call (by any thread) has been processed. + void WaitProcessed(uint64_t target) { + if (tail_.load(std::memory_order_acquire) >= target) return; + WakeConsumer(); + WaitFor([&] { return tail_.load(std::memory_order_acquire) >= target; }); + } + + // ---- Capture thread ---- + // The next record (nullptr when empty), past any wrap mark. + const uint8_t* Peek() { + for (;;) { + if (consumer_tail_ == cached_head_) { + cached_head_ = head_published_.load(std::memory_order_acquire); + if (consumer_tail_ == cached_head_) return nullptr; + } + const size_t offset = size_t(consumer_tail_ & (kCapacity - 1)); + uint32_t first; + std::memcpy(&first, data_ + offset, sizeof(first)); + if (first == kWrapMark) { + consumer_tail_ += kCapacity - offset; + continue; + } + return data_ + offset; + } + } + // Done with the record Peek returned (`size` aligned bytes). + // (Fenced as Commit is: a waiting producer also looks every 2 ms.) + void Pop(size_t size, bool present) { + consumer_tail_ += size; + if (present) presents_.fetch_sub(1, std::memory_order_release); + tail_.store(consumer_tail_, std::memory_order_release); + if (present) std::atomic_thread_fence(std::memory_order_seq_cst); + if (producer_waiting_.load(std::memory_order_relaxed)) { + std::atomic_thread_fence(std::memory_order_seq_cst); + std::lock_guard lock(mutex_); + producer_condition_.notify_all(); + } + } + // Waits for records (spinning about `spin_us` first); false once stopped. + bool WaitForRecords(const std::atomic<bool>& running, uint32_t spin_us) { + const auto spin_until = std::chrono::steady_clock::now() + std::chrono::microseconds(spin_us); + uint32_t round = 0; + while (running.load(std::memory_order_relaxed)) { + if (Ready()) return true; + Pause(); + if ((++round & 63) == 0 && std::chrono::steady_clock::now() >= spin_until) break; + } + std::unique_lock lock(mutex_); + consumer_sleeping_.store(true, std::memory_order_seq_cst); + while (running.load(std::memory_order_relaxed) && !Ready()) + consumer_condition_.wait_for(lock, std::chrono::milliseconds(1)); + consumer_sleeping_.store(false, std::memory_order_relaxed); + return running.load(std::memory_order_relaxed); + } + void WakeConsumer() { + std::lock_guard lock(mutex_); + consumer_condition_.notify_all(); + } + + private: + bool Ready() { + cached_head_ = head_published_.load(std::memory_order_acquire); + return consumer_tail_ != cached_head_; + } + static void Pause() { +#if defined(__x86_64__) + _mm_pause(); +#endif + } + + uint8_t* data_ = nullptr; + // Producer side. + alignas(64) uint64_t head_ = 0; + uint64_t cached_tail_ = 0; + size_t skip_ = 0; + alignas(64) std::atomic<uint64_t> head_published_{0}; + // Capture thread side. + alignas(64) uint64_t consumer_tail_ = 0; + uint64_t cached_head_ = 0; + alignas(64) std::atomic<uint64_t> tail_{0}; + alignas(64) std::atomic<uint32_t> presents_{0}; + std::atomic<bool> consumer_sleeping_{false}; + std::atomic<uint32_t> producer_waiting_{0}; + std::atomic<bool> stopped_{false}; + std::mutex mutex_; + std::condition_variable consumer_condition_, producer_condition_; +}; + +} // namespace rex::graphics::gta4_native + +#endif // REX_GRAPHICS_GTA4_NATIVE_NATIVE_CAPTURE_RING_H_ diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_cpu_profile_scope.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_cpu_profile_scope.h index cd5a375d..20ebfaaa 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_cpu_profile_scope.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_cpu_profile_scope.h @@ -1,4 +1,5 @@ #pragma once +#include <atomic> #include <rex/chrono/clock.h> #include <type_traits> #include <utility> @@ -6,12 +7,19 @@ namespace rex::graphics::gta4_native::profile { inline thread_local CpuRecorder* current_cpu_recorder = nullptr; +// How many threads have a recorder bound. Zero (no capture running) lets the +// scopes skip the thread-local read: on the PS5 every thread_local access is a +// call (emulated TLS), and the recorder enters thousands of scopes a frame. +inline std::atomic<uint32_t> bound_cpu_recorders{0}; +inline CpuRecorder* CurrentCpuRecorder() { + return bound_cpu_recorders.load(std::memory_order_relaxed) ? current_cpu_recorder : nullptr; +} inline uint64_t CpuTick() { return rex::chrono::Clock::QueryHostTickCount(); } class CpuScope { public: - explicit CpuScope(CpuOp op) : owner_(current_cpu_recorder) { + explicit CpuScope(CpuOp op) : owner_(CurrentCpuRecorder()) { if (owner_ && owner_->active()) token_ = owner_->Enter(op, CpuTick()); } @@ -31,7 +39,7 @@ class CpuScope { }; class CpuPhaseScope { public: - explicit CpuPhaseScope(CpuPhase phase) : owner_(current_cpu_recorder) { + explicit CpuPhaseScope(CpuPhase phase) : owner_(CurrentCpuRecorder()) { if (owner_) prior_ = owner_->SetPhase(phase); } @@ -50,7 +58,7 @@ class CpuPhaseScope { }; class CpuContextScope { public: - explicit CpuContextScope(CpuContext context) : owner_(current_cpu_recorder) { + explicit CpuContextScope(CpuContext context) : owner_(CurrentCpuRecorder()) { if (owner_) prior_ = owner_->SetContext(context); } @@ -59,7 +67,7 @@ class CpuContextScope { template <typename MakeContext, typename = std::enable_if_t<std::is_invocable_r_v<CpuContext, MakeContext&>>> explicit CpuContextScope(MakeContext&& make) - : owner_(current_cpu_recorder && current_cpu_recorder->active() ? current_cpu_recorder + : owner_(CurrentCpuRecorder() && current_cpu_recorder->active() ? current_cpu_recorder : nullptr) { if (owner_) prior_ = owner_->SetContext(make()); @@ -75,13 +83,19 @@ class CpuContextScope { }; class CpuRecorderBinding { public: - explicit CpuRecorderBinding(CpuRecorder* recorder) : prior_(current_cpu_recorder) { + explicit CpuRecorderBinding(CpuRecorder* recorder) + : prior_(current_cpu_recorder), bound_(recorder != nullptr) { + if (bound_) bound_cpu_recorders.fetch_add(1, std::memory_order_relaxed); current_cpu_recorder = recorder; } - ~CpuRecorderBinding() { current_cpu_recorder = prior_; } + ~CpuRecorderBinding() { + current_cpu_recorder = prior_; + if (bound_) bound_cpu_recorders.fetch_sub(1, std::memory_order_relaxed); + } private: CpuRecorder* prior_; + bool bound_; }; template <typename Fn> decltype(auto) CpuCall(CpuOp op, Fn&& fn) { diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_owned_commands.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_owned_commands.h index 179cbea5..44a82ea4 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_owned_commands.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_owned_commands.h @@ -1,5 +1,6 @@ #pragma once #include <atomic> +#include <bit> #include <cassert> #include <condition_variable> #include <cstddef> @@ -14,6 +15,10 @@ #include <utility> #include <vector> +#if defined(__PROSPERO__) +#include <rex/frame_stats.h> +#endif + namespace rex::graphics::gta4_native { // Commands keep stable addresses, with only owning handles crossing queues. // A fixed-size slab free list avoids libc++'s ad-hoc allocation list: small @@ -50,11 +55,14 @@ class NativeCommandPool { template <typename... Args> Owner Make(Args&&... args) { - void* storage = resource_.allocate(sizeof(T), alignof(T)); - auto* slot = static_cast<Slot*>(storage); - if (slot->constructed) { + // Whether the slot holds a recycled command travels in its free-list + // pointer: reading the slot's own flag (its last cache line, written by + // the retiring thread) was a cold miss on every command. + bool recycled_slot = false; + Slot* slot = resource_.AllocateSlot(recycled_slot); + void* storage = static_cast<void*>(slot->storage); + if (recycled_slot) { // Ordered after the retiring thread's writes by the free-list mutex. - slot->constructed = false; T* recycled = std::launder(reinterpret_cast<T*>(slot->storage)); if constexpr (sizeof...(Args) == 0) return Owner(recycled, Deleter{&resource_}); std::destroy_at(recycled); @@ -68,6 +76,9 @@ class NativeCommandPool { } } Statistics GetStatistics() const { return resource_.GetStatistics(); } + // Allocating thread only (see FixedResource). + const T* PeekNextRecycled() const { return resource_.PeekNextRecycled(); } + void SetPrefetchLines(uint64_t lines) { resource_.SetPrefetchLines(lines); } // Retire one completed batch without taking the shared free-list mutex for // each command. Destructors keep their original order and run outside the @@ -85,16 +96,18 @@ class NativeCommandPool { } T* object = owner.release(); auto* slot = reinterpret_cast<Slot*>(object); + bool recycled = false; if constexpr (kRecyclable) { if (recycling_.load(std::memory_order_relaxed)) { object->RecycleInPlace(); - slot->constructed = true; + recycled = true; } else { std::destroy_at(object); } } else { std::destroy_at(object); } + slot->constructed = recycled; slot->next = first; first = slot; if (!last) last = slot; @@ -115,14 +128,25 @@ class NativeCommandPool { Slab* next = nullptr; Slot slots[kSlotsPerSlab]; }; + // Free-list entries: a slot pointer, low bit set when the slot holds a + // recycled (constructed, default-state) T. + static_assert(alignof(Slot) >= 2); + static uintptr_t Entry(Slot* slot, bool constructed) { + return reinterpret_cast<uintptr_t>(slot) | uintptr_t(constructed); + } + static Slot* EntrySlot(uintptr_t entry) { return reinterpret_cast<Slot*>(entry & ~uintptr_t(1)); } + static bool EntryConstructed(uintptr_t entry) { return entry & 1; } + class FixedResource final : public std::pmr::memory_resource { public: ~FixedResource() override { // Recycled objects still own (empty but allocated) buffers. - for (Slot* slot : local_) - if (slot->constructed) std::destroy_at(std::launder(reinterpret_cast<T*>(slot->storage))); - for (Slot* slot : free_) - if (slot->constructed) std::destroy_at(std::launder(reinterpret_cast<T*>(slot->storage))); + for (uintptr_t entry : local_) + if (EntryConstructed(entry)) + std::destroy_at(std::launder(reinterpret_cast<T*>(EntrySlot(entry)->storage))); + for (uintptr_t entry : free_) + if (EntryConstructed(entry)) + std::destroy_at(std::launder(reinterpret_cast<T*>(EntrySlot(entry)->storage))); live_ -= local_.size(); // Taken by the allocator cache, never handed out. // Destruction occurs after the render worker has joined and owners drain. assert(live_ == 0); @@ -138,17 +162,12 @@ class NativeCommandPool { (void)last; std::lock_guard lock(mutex_); assert(count <= live_); - for (Slot* slot = first; slot; slot = slot->next) free_.push_back(slot); + for (Slot* slot = first; slot; slot = slot->next) free_.push_back(Entry(slot, slot->constructed)); live_ -= count; } - Statistics GetStatistics() const { - std::lock_guard lock(mutex_); - return {slab_count_, live_, slab_count_ * kSlotsPerSlab - live_, - slab_count_ * sizeof(Slab)}; - } - private: - void* do_allocate(size_t bytes, size_t alignment) override { - if (bytes != sizeof(T) || alignment != alignof(T)) throw std::bad_alloc(); + // The next free slot (single allocating thread), and whether it holds a + // recycled T, without touching the slot itself. + Slot* AllocateSlot(bool& constructed) { // Allocation is single-threaded (the producer holds its capture mutex), // while slots return from the retirement thread. The allocator swaps out // the whole shared free array under one lock and pops from a private @@ -164,22 +183,54 @@ class NativeCommandPool { slabs_ = slab; ++slab_count_; free_.reserve(free_.size() + kSlotsPerSlab); - for (Slot& slot : slab->slots) free_.push_back(&slot); + for (Slot& slot : slab->slots) free_.push_back(Entry(&slot, false)); } local_.swap(free_); live_ += local_.size(); // Counted as live while cached; never returned uncached. } - Slot* slot = local_.back(); + const uintptr_t entry = local_.back(); local_.pop_back(); - // Construction zero-fills the whole command, so start owning the next - // free slot's lines now; the following allocation is microseconds away. - // (Zeroing the slot here instead measured worse: the cost is the line - // ownership transfer from the recorder's core, not the stale data.) + // Construction or capture writes most of the command, so start owning + // the next free slot's lines now; the following allocation is + // microseconds away. (Zeroing the slot here instead measured worse: the + // cost is the line ownership transfer from the recorder's core, not the + // stale data.) if (!local_.empty()) { - const auto* next = reinterpret_cast<const char*>(local_.back()); - for (size_t offset = 0; offset < sizeof(Slot); offset += 64) - __builtin_prefetch(next + offset, 1); + const auto* next = reinterpret_cast<const char*>(EntrySlot(local_.back())); + // [ps5 port] Only the lines the allocating thread uses, when told + // which: fifty prefetches at once filled the core's miss buffers and + // stalled here. + const uint64_t lines = prefetch_lines_; + if (lines) { + for (uint64_t mask = lines; mask; mask &= mask - 1) + __builtin_prefetch(next + 64 * std::countr_zero(mask), 1); + } else { + for (size_t offset = 0; offset < sizeof(Slot); offset += 64) + __builtin_prefetch(next + offset, 1); + } } + constructed = EntryConstructed(entry); + return EntrySlot(entry); + } + // The object the next AllocateSlot returns, if it holds a recycled T + // (allocating thread only); its lines were prefetched by the last call. + const T* PeekNextRecycled() const { + if (local_.empty() || !EntryConstructed(local_.back())) return nullptr; + return std::launder(reinterpret_cast<const T*>(EntrySlot(local_.back())->storage)); + } + void SetPrefetchLines(uint64_t lines) { prefetch_lines_ = lines; } + Statistics GetStatistics() const { + std::lock_guard lock(mutex_); + return {slab_count_, live_, slab_count_ * kSlotsPerSlab - live_, + slab_count_ * sizeof(Slab)}; + } + private: + void* do_allocate(size_t bytes, size_t alignment) override { + if (bytes != sizeof(T) || alignment != alignof(T)) throw std::bad_alloc(); + bool constructed = false; + Slot* slot = AllocateSlot(constructed); + // Raw storage for a generic caller: a recycled T is destroyed first. + if (constructed) std::destroy_at(std::launder(reinterpret_cast<T*>(slot->storage))); return static_cast<void*>(slot->storage); } void do_deallocate(void* value, size_t bytes, size_t alignment) override { @@ -190,18 +241,19 @@ class NativeCommandPool { auto* slot = reinterpret_cast<Slot*>(value); std::lock_guard lock(mutex_); assert(live_ != 0); - free_.push_back(slot); + free_.push_back(Entry(slot, false)); --live_; } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; } mutable std::mutex mutex_; - std::vector<Slot*> local_; // Allocator-private free slots. - std::vector<Slot*> free_; // Shared, under mutex_. + std::vector<uintptr_t> local_; // Allocator-private free slots (Entry). + std::vector<uintptr_t> free_; // Shared, under mutex_. Slab* slabs_ = nullptr; size_t slab_count_ = 0; size_t live_ = 0; + uint64_t prefetch_lines_ = 0; // Allocating thread; 0 = every line. }; FixedResource resource_; std::atomic<bool> recycling_{true}; @@ -305,12 +357,29 @@ class NativeCommandRetirer { private: void Run() { std::unique_lock lock(mutex_); +#if defined(__PROSPERO__) + uint64_t applied_mask = 0; + bool applied_once = false, applied_main_core = false; +#endif for (;;) { condition_.wait(lock, [this] { return stopping_ || !pending_.empty(); }); if (pending_.empty()) return; // stopping and drained Batch batch = std::move(pending_.front()); pending_.pop_front(); lock.unlock(); +#if defined(__PROSPERO__) + // [ps5 port] Recycling writes every command the game's render thread + // fills next: with Renderer Threads on Together, on the same cluster. + // (and around the main thread's own core, when Compare turns it off or on) + if (const uint64_t mask = rex::frame_stats::g_renderer_cpu_mask.load(std::memory_order_relaxed); + mask != applied_mask || !applied_once || + rex::frame_stats::g_main_core.load(std::memory_order_relaxed) != applied_main_core) { + applied_once = true; + applied_mask = mask; + applied_main_core = rex::frame_stats::g_main_core.load(std::memory_order_relaxed); + rex::frame_stats::KeepRendererThreadOnItsCpus(); + } +#endif pool_.RetireBatch(batch); lock.lock(); } diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_lookup_memo.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_lookup_memo.h index e37e2915..bf81b1e4 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_lookup_memo.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_lookup_memo.h @@ -62,9 +62,9 @@ class NativePipelineLookupMemo { Pipeline Find(const void* owner, const FixedState& fixed, const Context& context) const { if (!owner || !context.lifetime) return Pipeline{}; - for (const auto& entry : entries_) { - if (entry.pipeline && owner == entry.owner && context == entry.context && - fixed == entry.fixed) return entry.pipeline; + for (size_t i = 0; i < kEntries; ++i) { + if (pipelines_[i] && owner == owners_[i] && context == contexts_[i] && fixed == fixed_[i]) + return pipelines_[i]; } return Pipeline{}; } @@ -73,9 +73,9 @@ class NativePipelineLookupMemo { // was captured (0 = none). Equal signatures stand for equal fixed states. Pipeline FindSigned(const void* owner, uint64_t signature, const Context& context) const { if (!owner || !context.lifetime || !signature) return Pipeline{}; - for (const auto& entry : entries_) { - if (entry.pipeline && entry.signature == signature && owner == entry.owner && - context == entry.context) return entry.pipeline; + for (size_t i = 0; i < kEntries; ++i) { + if (pipelines_[i] && signatures_[i] == signature && owner == owners_[i] && + context == contexts_[i]) return pipelines_[i]; } return Pipeline{}; } @@ -85,26 +85,30 @@ class NativePipelineLookupMemo { if (!owner || !context.lifetime || !pipeline) { return; } - for (auto& entry : entries_) { - if (entry.owner == owner && entry.context == context && entry.fixed == fixed) { - entry.pipeline = pipeline; - if (signature) entry.signature = signature; + for (size_t i = 0; i < kEntries; ++i) { + if (owners_[i] == owner && contexts_[i] == context && fixed_[i] == fixed) { + pipelines_[i] = pipeline; + if (signature) signatures_[i] = signature; return; } } - entries_[next_] = {owner, fixed, context, pipeline, signature}; - next_ = (next_ + 1) % entries_.size(); + owners_[next_] = owner; + fixed_[next_] = fixed; + contexts_[next_] = context; + pipelines_[next_] = pipeline; + signatures_[next_] = signature; + next_ = (next_ + 1) % kEntries; } private: - struct Entry { - const void* owner = nullptr; - FixedState fixed{}; - Context context{}; - Pipeline pipeline{}; - uint64_t signature = 0; - }; - std::array<Entry, 4> entries_{}; + // [ps5 port] By field, not by entry: a signed lookup reads the signatures, + // pipelines and owners, then only the matching entry's context. + static constexpr size_t kEntries = 4; + std::array<uint64_t, kEntries> signatures_{}; + std::array<Pipeline, kEntries> pipelines_{}; + std::array<const void*, kEntries> owners_{}; + std::array<Context, kEntries> contexts_{}; + std::array<FixedState, kEntries> fixed_{}; size_t next_ = 0; }; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_policy.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_policy.h index 4bb15138..127b7f94 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_policy.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_pipeline_policy.h @@ -156,7 +156,8 @@ struct NativeBlendConstantSelection { constexpr uint32_t NativeColorFormatComponentMask(VkFormat format) { switch (format) { case VK_FORMAT_R32_SFLOAT: return 0x1u; - case VK_FORMAT_R16G16_SFLOAT: return 0x3u; + case VK_FORMAT_R16G16_SFLOAT: + case VK_FORMAT_R32G32_SFLOAT: return 0x3u; case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_R16G16B16A16_SFLOAT: return 0xFu; default: return 0; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_prepared_bindings.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_prepared_bindings.h index 6ddc9eb2..0da4bb2e 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_prepared_bindings.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_prepared_bindings.h @@ -20,8 +20,11 @@ bool NativePreparedTextureInputsEqual(const Command& previous, const Command& ne } return true; } +// reset_room_light false: the caller knows no draw has room-light bindings +// (they exist only for one diagnostic probe), so that cold field is not read. template <typename Command> -void CopyNativePreparedTextureBindings(const Command& previous, Command& next) { +void CopyNativePreparedTextureBindings(const Command& previous, Command& next, + bool reset_room_light = true) { next.descriptor_page=previous.descriptor_page; next.descriptor_copy=previous.descriptor_copy; next.image_descriptor_epoch=previous.image_descriptor_epoch; next.sampler_descriptor_epoch=previous.sampler_descriptor_epoch; @@ -35,6 +38,6 @@ void CopyNativePreparedTextureBindings(const Command& previous, Command& next) { next.guest_null_texture_mask=previous.guest_null_texture_mask; next.failed_texture_mask=0; next.bindings_prepared=true; // Never copy diagnostic-owned objects from another draw. - next.room_light_input_bindings.reset(); + if (reset_room_light) next.room_light_input_bindings.reset(); } } // namespace rex::graphics::gta4_native diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_snapshot_resource.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_snapshot_resource.h index 1294764a..f0c5411f 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_snapshot_resource.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_snapshot_resource.h @@ -1,5 +1,6 @@ #pragma once #include <algorithm> +#include <atomic> #include <cstddef> #include <memory_resource> #include <mutex> @@ -9,11 +10,12 @@ namespace rex::graphics::gta4_native { // Pipeline and shader-state snapshots are allocated by the render worker for -// nearly every draw and released on the retirement threads. A synchronized -// pool took its mutex on both sides of every allocation. Here the single -// allocating thread pops from a private free list and takes the lock only to -// adopt everything returned since (or to grow); releases from any thread -// take it once each. Memory is kept for reuse until the resource dies. +// nearly every draw and released on the retirement threads. The single +// allocating thread pops from a private free list; releases push onto a +// lock-free list per size, which the allocator takes whole when its own runs +// out. (Releases used to take a mutex each, against the allocator's refills: +// on the PS5 the recorder spent a quarter of its time waiting on such locks.) +// Memory is kept for reuse until the resource dies. class NativeSnapshotResource final : public std::pmr::memory_resource { public: ~NativeSnapshotResource() override { @@ -24,16 +26,21 @@ class NativeSnapshotResource final : public std::pmr::memory_resource { static constexpr size_t kAlignment = 16; static constexpr size_t kChunksPerBlock = 64; static constexpr size_t kMaximumClasses = 8; + struct FreeChunk { + FreeChunk* next; + }; + static_assert(sizeof(FreeChunk) <= kAlignment); struct SizeClass { - size_t size = 0; - std::vector<void*> local; // Allocating thread only. - std::vector<void*> returned; // Under mutex_. + size_t size = 0; // set before class_count_ publishes it + std::vector<void*> local; // allocating thread only + std::atomic<FreeChunk*> returned{nullptr}; // pushed by any thread }; static size_t Rounded(size_t bytes) { return (bytes + kAlignment - 1) & ~(kAlignment - 1); } SizeClass* Find(size_t size) { - for (size_t i = 0; i < class_count_; ++i) + const size_t count = class_count_.load(std::memory_order_acquire); + for (size_t i = 0; i < count; ++i) if (classes_[i].size == size) return &classes_[i]; return nullptr; } @@ -43,21 +50,28 @@ class NativeSnapshotResource final : public std::pmr::memory_resource { const size_t size = Rounded(bytes); SizeClass* size_class = Find(size); if (!size_class) { - std::lock_guard lock(mutex_); - if (class_count_ == kMaximumClasses) { + const size_t count = class_count_.load(std::memory_order_relaxed); + if (count == kMaximumClasses) { return ::operator new(bytes, std::align_val_t(alignment)); } - size_class = &classes_[class_count_]; + size_class = &classes_[count]; size_class->size = size; - ++class_count_; // Published to this thread; other threads look up under mutex_. + class_count_.store(count + 1, std::memory_order_release); } if (size_class->local.empty()) { - std::lock_guard lock(mutex_); - size_class->local.swap(size_class->returned); + for (FreeChunk* chunk = size_class->returned.exchange(nullptr, std::memory_order_acquire); + chunk;) { + FreeChunk* next = chunk->next; + size_class->local.push_back(chunk); + chunk = next; + } if (size_class->local.empty()) { auto* block = static_cast<std::byte*>( ::operator new(size * kChunksPerBlock, std::align_val_t(kAlignment))); - blocks_.push_back(block); + { + std::lock_guard lock(mutex_); + blocks_.push_back(block); + } size_class->local.reserve(kChunksPerBlock); for (size_t i = 0; i < kChunksPerBlock; ++i) size_class->local.push_back(block + i * size); } @@ -72,26 +86,28 @@ class NativeSnapshotResource final : public std::pmr::memory_resource { ::operator delete(pointer, std::align_val_t(alignment)); return; } - const size_t size = Rounded(bytes); - std::lock_guard lock(mutex_); - for (size_t i = 0; i < class_count_; ++i) { - if (classes_[i].size == size) { - classes_[i].returned.push_back(pointer); - return; - } + SizeClass* size_class = Find(Rounded(bytes)); + if (!size_class) { + // Allocated after the class table filled up. + ::operator delete(pointer, std::align_val_t(alignment)); + return; } - // Allocated after the class table filled up. - ::operator delete(pointer, std::align_val_t(alignment)); + auto* chunk = static_cast<FreeChunk*>(pointer); + FreeChunk* head = size_class->returned.load(std::memory_order_relaxed); + do { + chunk->next = head; + } while (!size_class->returned.compare_exchange_weak(head, chunk, std::memory_order_release, + std::memory_order_relaxed)); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; } - std::mutex mutex_; + std::mutex mutex_; // blocks_ SizeClass classes_[kMaximumClasses]; - size_t class_count_ = 0; - std::vector<void*> blocks_; // Under mutex_. + std::atomic<size_t> class_count_{0}; + std::vector<void*> blocks_; }; } // namespace rex::graphics::gta4_native diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_texture_protection.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_texture_protection.h index 7372ea42..a9bb74cc 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_texture_protection.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_texture_protection.h @@ -1,6 +1,7 @@ #pragma once #include <cstddef> #include <cstdint> +#include <utility> #include <vector> #include <unordered_map> #include <unordered_set> @@ -23,6 +24,89 @@ void VisitNativeCommandTextureGenerations(const Command& command, Visitor&& visi for (const auto& resource : command.textures) texture(resource); } +// A set of texture generations: open addressing over one flat table, plus the +// members in insertion order for iteration. clear() is O(1) (a slot is full +// only in the epoch it was filled in) and keeps both allocations. +// [ps5 port] Replaces std::unordered_set for the protection sets, rebuilt and +// cleared every frame: a node per insert, prime-modulo buckets, chased nodes. +class NativeGenerationSet { + public: + using const_iterator = std::vector<uint64_t>::const_iterator; + + // Returns true when the key was not present. + bool insert(uint64_t key) { + if ((members_.size() + 1) * 2 > slots_.size()) Grow(members_.size() + 1); + Slot& slot = slots_[Probe(key)]; + if (slot.epoch == epoch_) return false; + slot.key = key; + slot.epoch = epoch_; + members_.push_back(key); + return true; + } + template <typename Iterator> + void insert(Iterator first, Iterator last) { + for (; first != last; ++first) insert(uint64_t(*first)); + } + bool contains(uint64_t key) const { + return !slots_.empty() && slots_[Probe(key)].epoch == epoch_; + } + void clear() { + members_.clear(); + if (++epoch_ == 0) { // wrapped: empty every slot explicitly + for (Slot& slot : slots_) slot.epoch = 0; + epoch_ = 1; + } + } + void reserve(size_t count) { + if (count * 2 > slots_.size()) Grow(count); + } + size_t size() const { return members_.size(); } + bool empty() const { return members_.empty(); } + const_iterator begin() const { return members_.begin(); } + const_iterator end() const { return members_.end(); } + void swap(NativeGenerationSet& other) noexcept { + slots_.swap(other.slots_); + members_.swap(other.members_); + std::swap(epoch_, other.epoch_); + } + // Same members, in any order. + bool operator==(const NativeGenerationSet& other) const { + if (size() != other.size()) return false; + for (uint64_t key : members_) + if (!other.contains(key)) return false; + return true; + } + + private: + struct Slot { + uint64_t key = 0; + uint32_t epoch = 0; + }; + static size_t Hash(uint64_t key) { return size_t((key * 0x9E3779B97F4A7C15ull) >> 17); } + // The slot holding key, or the empty slot where it goes (load stays <= 1/2). + size_t Probe(uint64_t key) const { + const size_t mask = slots_.size() - 1; + for (size_t i = Hash(key) & mask;; i = (i + 1) & mask) { + const Slot& slot = slots_[i]; + if (slot.epoch != epoch_ || slot.key == key) return i; + } + } + void Grow(size_t count) { + size_t capacity = slots_.empty() ? 1024 : slots_.size(); + while (count * 2 > capacity) capacity *= 2; + slots_.assign(capacity, Slot{}); + epoch_ = 1; + for (uint64_t key : members_) { + Slot& slot = slots_[Probe(key)]; + slot.key = key; + slot.epoch = epoch_; + } + } + std::vector<Slot> slots_; + std::vector<uint64_t> members_; + uint32_t epoch_ = 1; +}; + // Queue lock owns this index. Counts are logical references, not shared_ptrs or // GPU ownership. Every zero count is removed immediately. If an invariant is // violated the renderer falls back to its original queue scan, never guessing @@ -45,7 +129,8 @@ class NativeTextureProtectionIndex { if (!--values_[slot]) --live_; return true; } - void AppendTo(std::unordered_set<uint64_t>& destination) const { + template <typename Set> + void AppendTo(Set& destination) const { destination.reserve(destination.size() + live_); for (size_t i = 0; i < keys_.size(); ++i) if (keys_[i] && values_[i]) destination.insert(keys_[i]); diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_working_set.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_working_set.h index 7848e37e..d3b808b8 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/native_working_set.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/native_working_set.h @@ -94,10 +94,15 @@ class NativeDescriptorWorkingSet { out.page = uint32_t(active_pages_ - 1); Page& page = pages_[out.page]; for (size_t i = 0; i < Stages; ++i) { - out.images[i] = Insert(page.image_lookup, page.previous_images, page.images, images[i], - image_capacity_, page.image_free_cursor); - out.samplers[i] = Insert(page.sampler_lookup, page.previous_samplers, page.samplers, samplers[i], - sampler_capacity_, page.sampler_free_cursor); + // Identity zero (an unused stage, most of them) is index 0 on every + // page (inserted by Reset): no lookups for it. + out.images[i] = images[i] ? Insert(page.image_lookup, page.previous_images, page.images, + images[i], image_capacity_, page.image_free_cursor) + : 0; + out.samplers[i] = samplers[i] ? Insert(page.sampler_lookup, page.previous_samplers, + page.samplers, samplers[i], sampler_capacity_, + page.sampler_free_cursor) + : 0; if (out.images[i] == UINT32_MAX || out.samplers[i] == UINT32_MAX) return std::nullopt; } @@ -118,7 +123,7 @@ class NativeDescriptorWorkingSet { const std::array<uint64_t, Stages>& keys) { size_t count = 0; for (size_t i = 0; i < Stages; ++i) - if (!lookup.Find(keys[i]) && + if (keys[i] && !lookup.Find(keys[i]) && std::find(keys.begin(), keys.begin() + i, keys[i]) == keys.begin() + i) ++count; return count; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/stateful_constant_state.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/stateful_constant_state.h index d919e925..dba4459c 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/stateful_constant_state.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/stateful_constant_state.h @@ -3,11 +3,14 @@ #include <algorithm> #include <array> +#include <atomic> +#include <bit> #include <cstddef> #include <cstdint> #include <cstring> #include <limits> #include <memory> +#include <mutex> #include <span> #include <utility> #include <vector> @@ -28,6 +31,98 @@ struct ConstantDeltaRange { constexpr bool operator==(const ConstantDeltaRange&) const = default; }; +// Constant payloads larger than a recycled command keeps (full snapshots for +// lighting draws, bone palettes) were freed when their command was recycled +// and allocated again for the next one: 4 KB heap round trips on the game's +// render thread. A bounded number of them wait here instead. Put runs where +// commands are recycled, Take on the producer; a spin lock covers both. +// [ps5 port] Kept by power-of-two capacity class, a request taking from the +// smallest class that fits: with one LIFO list, a small buffer on top (a +// DrawPrimitiveUP's vertex data) sent every larger request to the heap. +class LargeConstantPayloadStash { + public: + static constexpr size_t kMaximumBuffers = 1024; // per class + static constexpr size_t kMaximumBytes = 16384; + static constexpr size_t kMinimumBytes = 64; + static constexpr size_t kMaximumStashedBytes = kMaximumBuffers * kMaximumBytes; + static void Put(std::vector<uint8_t>&& buffer) { + if (buffer.capacity() > kMaximumBytes || buffer.capacity() < kMinimumBytes) return; + buffer.clear(); + Stash& stash = Get(); + Lock(stash); + Keep(stash, buffer); + stash.lock.clear(std::memory_order_release); + } + // Gives `buffer` (when it is too small) a stashed buffer of at least `bytes`. + static void Reserve(std::vector<uint8_t>& buffer, size_t bytes) { + if (buffer.capacity() >= bytes) return; + if (bytes <= kMaximumBytes) { + Stash& stash = Get(); + Lock(stash); + for (size_t index = RequestClass(bytes); index < kClassCount; ++index) { + auto& list = stash.classes[index]; + if (list.empty()) continue; + std::vector<uint8_t> taken = std::move(list.back()); + list.pop_back(); + stash.bytes -= taken.capacity(); + buffer.swap(taken); + buffer.clear(); + // The smaller buffer it replaces waits for a smaller request. + if (taken.capacity() >= kMinimumBytes) { + taken.clear(); + Keep(stash, taken); + } + stash.lock.clear(std::memory_order_release); + return; + } + stash.lock.clear(std::memory_order_release); + // A whole class: the buffer also fits a slightly larger next request. + buffer.reserve(std::bit_ceil(std::max(bytes, kMinimumBytes))); + return; + } + buffer.reserve(bytes); + } + + private: + // Class k holds capacities [64 << k, 128 << k); 64 << 8 is kMaximumBytes. + static constexpr size_t kClassCount = 9; + static_assert((kMinimumBytes << (kClassCount - 1)) == kMaximumBytes); + static size_t CapacityClass(size_t capacity) { + return size_t(std::bit_width(capacity / kMinimumBytes)) - 1; + } + static size_t RequestClass(size_t bytes) { + return CapacityClass(std::bit_ceil(std::max(bytes, kMinimumBytes))); + } + struct Stash { + std::atomic_flag lock = ATOMIC_FLAG_INIT; + std::array<std::vector<std::vector<uint8_t>>, kClassCount> classes; + size_t bytes = 0; + Stash() { + for (auto& list : classes) list.reserve(kMaximumBuffers); + } + }; + // Lock held; `buffer` is empty, with kMinimumBytes..kMaximumBytes capacity. + static void Keep(Stash& stash, std::vector<uint8_t>& buffer) { + auto& list = stash.classes[CapacityClass(buffer.capacity())]; + if (list.size() < kMaximumBuffers && + stash.bytes + buffer.capacity() <= kMaximumStashedBytes) { + stash.bytes += buffer.capacity(); + list.push_back(std::move(buffer)); + } + } + static Stash& Get() { + static Stash stash; + return stash; + } + static void Lock(Stash& stash) { + while (stash.lock.test_and_set(std::memory_order_acquire)) { +#if defined(__x86_64__) + __builtin_ia32_pause(); +#endif + } + } +}; + struct ConstantPayloadDelta { std::vector<ConstantDeltaRange> ranges; std::vector<uint8_t> payload; @@ -81,7 +176,7 @@ inline bool CaptureConstantPayloadDelta(std::span<const uint8_t> source, { uint64_t total = 0; for (const DirtyElementRange& dirty : dirty_ranges.ranges) total += uint64_t(dirty.count) * bytes_per_element; - if (total <= source.size()) result.payload.reserve(size_t(total)); + if (total <= source.size()) LargeConstantPayloadStash::Reserve(result.payload, size_t(total)); result.ranges.reserve(dirty_ranges.ranges.size()); } for (const DirtyElementRange& dirty : dirty_ranges.ranges) { @@ -98,6 +193,14 @@ inline bool CaptureConstantPayloadDelta(std::span<const uint8_t> source, return false; } const uint32_t payload_offset = static_cast<uint32_t>(result.payload.size()); + // [ps5 port] Ranges must ascend. With this, the checks above make the delta + // valid by construction: ValidateConstantPayloadDelta is not run again here + // (the render worker still validates it before applying). + if (!result.ranges.empty() && + destination_offset < uint64_t(result.ranges.back().destination_offset) + + result.ranges.back().byte_count) { + return false; + } const bool coalesce = !result.ranges.empty() && result.ranges.back().destination_offset <= @@ -116,7 +219,7 @@ inline bool CaptureConstantPayloadDelta(std::span<const uint8_t> source, result.payload.insert(result.payload.end(), source.begin() + destination_offset, source.begin() + destination_offset + byte_count); } - return ValidateConstantPayloadDelta(result, source.size()); + return true; } inline bool CaptureCompleteConstantSnapshot(std::span<const uint8_t> source, @@ -126,9 +229,11 @@ inline bool CaptureCompleteConstantSnapshot(std::span<const uint8_t> source, return false; } result.ranges = {{0, 0, static_cast<uint32_t>(source.size())}}; + LargeConstantPayloadStash::Reserve(result.payload, source.size()); result.payload.assign(source.begin(), source.end()); result.complete_snapshot = true; - return ValidateConstantPayloadDelta(result, source.size(), true); + // [ps5 port] One range over the whole block: valid unless the block is empty. + return !source.empty(); } struct ConstantStateVersion { @@ -181,52 +286,98 @@ struct ConstantApplyResult { // stage. Published versions retain only copied deltas. A contiguous host view // is reconstructed lazily on first consumer use and then memoized. // Recycles the full-bank snapshot buffers taken for every draw that changes -// constants (two 3.5-4 KB heap blocks per draw otherwise). Releases may come -// from any thread (command retirement is asynchronous), hence the mutex. +// constants (two 3.5-4 KB heap blocks per draw otherwise). Releases come from +// any thread (command retirement is asynchronous). They push onto a lock-free +// list: a mutex shared with the allocating render worker, taken twice a draw +// on both sides, kept the PS5's recorder waiting on it. class ConstantSnapshotPool { + struct Node { + std::vector<uint8_t> bytes; + Node* next = nullptr; + }; + struct SizeList { + size_t size = 0; // set once, before count publishes it + std::atomic<Node*> returned{nullptr}; // pushed by any thread + std::atomic<uint32_t> returned_count{0}; + std::vector<Node*> local; // under State::allocate + }; + static constexpr size_t kMaximumLists = 16; + static constexpr uint32_t kMaximumReturned = 8192; + struct State { + std::mutex allocate; // Copy's side only (the render worker): uncontended + std::array<SizeList, kMaximumLists> lists; + std::atomic<size_t> count{0}; + }; + static State& state() { + static auto* instance = new State(); // never destroyed: buffers outlive statics + return *instance; + } + static SizeList* Find(State& st, size_t size) { + const size_t count = st.count.load(std::memory_order_acquire); + for (size_t i = 0; i < count; ++i) + if (st.lists[i].size == size) return &st.lists[i]; + return nullptr; + } + static void Release(Node* node) { + SizeList* list = Find(state(), node->bytes.size()); + if (!list || list->returned_count.fetch_add(1, std::memory_order_relaxed) >= kMaximumReturned) { + if (list) list->returned_count.fetch_sub(1, std::memory_order_relaxed); + delete node; + return; + } + Node* head = list->returned.load(std::memory_order_relaxed); + do { + node->next = head; + } while (!list->returned.compare_exchange_weak(head, node, std::memory_order_release, + std::memory_order_relaxed)); + } + public: static std::shared_ptr<const std::vector<uint8_t>> Copy(const std::vector<uint8_t>& source, size_t copy_bytes = SIZE_MAX) { copy_bytes = std::min(copy_bytes, source.size()); - std::vector<uint8_t>* buffer = nullptr; + State& st = state(); + Node* node = nullptr; { - std::lock_guard lock(mutex()); - auto& list = free_list(source.size()); - if (!list.empty()) { - buffer = list.back(); - list.pop_back(); + std::lock_guard lock(st.allocate); + SizeList* list = Find(st, source.size()); + if (!list) { + const size_t count = st.count.load(std::memory_order_relaxed); + if (count < kMaximumLists) { + list = &st.lists[count]; + list->size = source.size(); + st.count.store(count + 1, std::memory_order_release); + } + } + if (list) { + if (list->local.empty()) { + Node* taken = list->returned.exchange(nullptr, std::memory_order_acquire); + uint32_t taken_count = 0; + for (; taken; ++taken_count) { + Node* next = taken->next; + list->local.push_back(taken); + taken = next; + } + if (taken_count) list->returned_count.fetch_sub(taken_count, std::memory_order_relaxed); + } + if (!list->local.empty()) { + node = list->local.back(); + list->local.pop_back(); + } } } - if (buffer) { - std::memcpy(buffer->data(), source.data(), copy_bytes); + if (node) { + std::memcpy(node->bytes.data(), source.data(), copy_bytes); } else if (copy_bytes == source.size()) { - buffer = new std::vector<uint8_t>(source); + node = new Node{source}; } else { - buffer = new std::vector<uint8_t>(source.size()); - std::memcpy(buffer->data(), source.data(), copy_bytes); + node = new Node{std::vector<uint8_t>(source.size())}; + std::memcpy(node->bytes.data(), source.data(), copy_bytes); } return std::shared_ptr<const std::vector<uint8_t>>( - buffer, - [](const std::vector<uint8_t>* bytes) { - auto* owned = const_cast<std::vector<uint8_t>*>(bytes); - std::lock_guard lock(mutex()); - auto& list = free_list(owned->size()); - if (list.size() < 8192) list.push_back(owned); - else delete owned; - }, + &node->bytes, [node](const std::vector<uint8_t>*) { Release(node); }, std::pmr::polymorphic_allocator<std::byte>(&ConstantVersionResource())); } - - private: - static std::mutex& mutex() { static std::mutex instance; return instance; } - // One list per bank size (vertex, pixel and the rare others). - static std::vector<std::vector<uint8_t>*>& free_list(size_t size) { - static std::vector<std::pair<size_t, std::vector<std::vector<uint8_t>*>>> lists; - for (auto& [list_size, list] : lists) - if (list_size == size) return list; - lists.emplace_back(size, std::vector<std::vector<uint8_t>*>{}); - return lists.back().second; - } }; class AuthoritativeConstantState { diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/command_capture.h b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/command_capture.h index 981a0757..60c77cac 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/command_capture.h +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/command_capture.h @@ -1,6 +1,7 @@ #pragma once #include <algorithm> +#include <atomic> #include <cmath> #include <memory> @@ -43,6 +44,7 @@ class CommandCapture { public: void Observe(const TemporalCommand& command) { if (command.event == TemporalEvent::kInstance) { + observed_.store(true, std::memory_order_relaxed); pending_.owner = identity_; pending_.command = std::make_shared<const TemporalCommand>(command); } else if (command.event == TemporalEvent::kBeginScene || @@ -51,6 +53,9 @@ class CommandCapture { } } std::shared_ptr<const TemporalCommand> Capture(uint32_t device) const { + // [ps5 port] Without temporal output nothing is ever observed: skip the + // thread_local reads (calls on the PS5) and the weak_ptr lock every draw. + if (!observed_.load(std::memory_order_relaxed)) return {}; if (pending_.owner.lock() != identity_ || !pending_.command || pending_.command->device != device) return {}; return pending_.command; @@ -65,6 +70,7 @@ class CommandCapture { std::shared_ptr<const TemporalCommand> command; }; std::shared_ptr<const char> identity_ = std::make_shared<const char>(); + std::atomic<bool> observed_{false}; inline static thread_local Pending pending_; }; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/fsr_vulkan.cpp b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/fsr_vulkan.cpp index 9f96921d..e578179b 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/fsr_vulkan.cpp +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/fsr_vulkan.cpp @@ -10,8 +10,31 @@ #include <unordered_map> #include <utility> +#include <atomic> +#include <string> + +#include <rex/logging.h> +#include <rex/platform.h> #include <rex/ui/vulkan/device.h> +#if REX_PLATFORM_PS5 +// [ps5 port] The FidelityFX sources' error returns, with the line they are on +// (rexglue_fidelityfx.cmake): under 100000 the Vulkan backend (ffx_vk.cpp), +// then 100000 + line in the FSR 3 upscaler, 200000 + frame interpolation, +// 300000 + optical flow. +extern "C" int rexglue_ffx_report(int line, int code) { + static std::atomic<int> reported{0}; + if (reported.fetch_add(1, std::memory_order_relaxed) < 40) { + const char* file = line < 100000 ? "ffx_vk.cpp" + : line < 200000 ? "ffx_fsr3upscaler.cpp" + : line < 300000 ? "ffx_frameinterpolation.cpp" + : "ffx_opticalflow.cpp"; + REXLOG_WARN("FidelityFX: {} line {} failed (error {:#x})", file, line % 100000, uint32_t(code)); + } + return code; +} +#endif + #if defined(REX_HAS_FIDELITYFX_VULKAN) && REX_HAS_FIDELITYFX_VULKAN #include <ffx_api/ffx_api.h> #include <ffx_api/ffx_framegeneration.h> @@ -176,8 +199,16 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL ResolveFsrFunction(VkDevice device, con } void FsrMessage(uint32_t type, const wchar_t* message) { +#if REX_PLATFORM_PS5 + // [ps5 port] stderr goes nowhere on the PS5: into the log (ASCII is enough). + std::string text; + for (const wchar_t* c = message; c && *c && text.size() < 400; ++c) + text.push_back(*c >= 32 && *c < 127 ? char(*c) : '?'); + REXLOG_WARN("FidelityFX {}: {}", type == FFX_API_MESSAGE_TYPE_ERROR ? "error" : "warning", text); +#else std::fwprintf(stderr, L"FidelityFX %ls: %ls\n", type == FFX_API_MESSAGE_TYPE_ERROR ? L"error" : L"warning", message); +#endif } uint32_t QualityMode(Quality quality) { @@ -402,7 +433,8 @@ bool FsrUpscalerVulkan::Configure(const Configuration& configuration) { create = {}; create.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE; create.header.pNext = &state.backend.header; - create.flags = FFX_UPSCALE_ENABLE_DEBUG_CHECKING; + // [ps5 port] No debug checking: it validates every dispatch on the recorder. + create.flags = REX_PLATFORM_PS5 ? 0 : FFX_UPSCALE_ENABLE_DEBUG_CHECKING; if (configuration.hdr) create.flags |= FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE; if (configuration.reversed_depth) create.flags |= FFX_UPSCALE_ENABLE_DEPTH_INVERTED; if (configuration.infinite_depth) create.flags |= FFX_UPSCALE_ENABLE_DEPTH_INFINITE; @@ -567,7 +599,7 @@ bool FsrFrameGenerationVulkan::Configure(const Configuration& configuration, VkF create = {}; create.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION; create.header.pNext = &state.backend.header; - create.flags = FFX_FRAMEGENERATION_ENABLE_DEBUG_CHECKING; + create.flags = REX_PLATFORM_PS5 ? 0 : FFX_FRAMEGENERATION_ENABLE_DEBUG_CHECKING; if (configuration.hdr) create.flags |= FFX_FRAMEGENERATION_ENABLE_HIGH_DYNAMIC_RANGE; if (configuration.reversed_depth) create.flags |= FFX_FRAMEGENERATION_ENABLE_DEPTH_INVERTED; if (configuration.infinite_depth) create.flags |= FFX_FRAMEGENERATION_ENABLE_DEPTH_INFINITE; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/native_renderer.inc b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/native_renderer.inc index ede9fd26..ee862c46 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/native_renderer.inc +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/native_renderer.inc @@ -29,22 +29,53 @@ uint32_t Gta4NativeGraphicsSystem::QueryTemporalCapabilities() { // contexts; never reconfigure history used by an asynchronously recorded CB. if (temporal_capabilities_checked_) return temporal_capabilities_; temporal_capabilities_checked_ = true; +#if REX_PLATFORM_PS5 + // [ps5 port] FSR 3 on the PS5 is new: probe it (load its 100 MB shader + // archive, make test contexts) only when it was chosen in the hub. + if (rex::cvar::GetFlagByName("gta4_native_upscaler") != "fsr3") { + REXLOG_INFO("gta4-native-temporal: FSR 3 not selected; not probed"); + return 0; + } +#endif if (!EnsureTemporalAssets()) return 0; + auto* provider = static_cast<ui::vulkan::VulkanProvider*>(provider_.get()); const auto* device = provider->vulkan_device(); const temporal::Configuration probe{{128, 128}, {128, 128}, temporal::Quality::kNative, true, false, false}; temporal::VulkanScene scene_probe; if (!scene_probe.Initialize(device) || !scene_probe.Configure(probe)) { +#if REX_PLATFORM_PS5 + REXLOG_WARN("gta4-native-temporal: FSR 3 probe: the temporal scene could not be set up: {}", + scene_probe.error()); +#endif scene_probe.Shutdown(); return 0; } scene_probe.Shutdown(); const auto resolver = device->vulkan_instance()->functions().vkGetDeviceProcAddr; temporal::FsrUpscalerVulkan fsr_probe; +#if REX_PLATFORM_PS5 + { + // [ps5 port] Say which step turns FSR 3 down. + std::string reason; + const bool supported = temporal::FsrSupportsDevice(*device, &reason); + const bool initialized = + supported && fsr_probe.Initialize(device->physical_device(), device->device(), resolver); + const bool configured = initialized && fsr_probe.Configure(probe); + const bool upscaler = + configured && fsr_upscaler_.Initialize(device->physical_device(), device->device(), resolver); + REXLOG_INFO("gta4-native-temporal: FSR 3 probe: device {} ({}), context {}, configure {}, " + "upscaler {}; {}", + supported, reason, initialized, configured, upscaler, + !initialized || !configured ? fsr_probe.last_error() : fsr_upscaler_.last_error()); + if (upscaler) temporal_capabilities_ |= kCapabilityFsr3Upscaling; + } +#else if (temporal::FsrSupportsDevice(*device) && fsr_probe.Initialize(device->physical_device(), device->device(), resolver) && fsr_probe.Configure(probe) && fsr_upscaler_.Initialize(device->physical_device(), device->device(), resolver)) temporal_capabilities_ |= kCapabilityFsr3Upscaling; +#endif if (dlss_upscaler_.Initialize(*device)) temporal_capabilities_ |= kCapabilityDlssUpscaling; if (temporal_capabilities_ & (kCapabilityFsr3Upscaling | kCapabilityDlssUpscaling)) { temporal_capabilities_ |= kCapabilityTemporalInputs; @@ -53,6 +84,14 @@ uint32_t Gta4NativeGraphicsSystem::QueryTemporalCapabilities() { if (temporal::VulkanFrameGeneration::SupportsProvider(*device, temporal::FrameGenerationProvider::kDlss)) temporal_capabilities_ |= kCapabilityDlssFrameGeneration; } +#if REX_PLATFORM_PS5 + rex::frame_stats::g_frame_generation_active.store( + rex::cvar::GetFlagByName("gta4_native_frame_generation") == "fsr3" && + (temporal_capabilities_ & kCapabilityFsrFrameGeneration), + std::memory_order_relaxed); + REXLOG_INFO("gta4-native-temporal: capabilities {:#x}; frame generation {}", temporal_capabilities_, + rex::frame_stats::g_frame_generation_active.load() ? "on" : "off"); +#endif return temporal_capabilities_; } @@ -98,7 +137,10 @@ bool Gta4NativeGraphicsSystem::PrepareTemporalFrame() { // A frame without temporal updates has no scene, and its commands still // carry default temporal fields unless an earlier pass set them: skip the // walk, which read the first cache line of every command. - if (!frame_has_temporal_updates_ && !temporal_fields_set_) return true; + if (!frame_has_temporal_updates_ && !temporal_fields_set_) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kNoScene); + return true; + } temporal_fields_set_ = false; // One pass, and no stores into commands still at their defaults (every // command of a frame without temporal output): dirtying each command's @@ -117,14 +159,23 @@ bool Gta4NativeGraphicsSystem::PrepareTemporalFrame() { std::memcpy(&candidate, command.bytes.data(), sizeof(candidate)); if (candidate.event == TemporalEvent::kBeginScene) scene = candidate; } - if (!scene) return true; + if (!scene) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kNoScene); + return true; + } std::lock_guard lock(temporal_provider_mutex_); const auto selected = temporal_query_selections_.find(scene->sequence); if (selected == temporal_query_selections_.end() || selected->second.render != temporal::Extent{scene->width, scene->height} || - selected->second.output != temporal::Extent{scene->output_width, scene->output_height}) return true; + selected->second.output != temporal::Extent{scene->output_width, scene->output_height}) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kSceneSize); + return true; + } const auto selection = selected->second; - if (!EnsureTemporalAssets()) return true; + if (!EnsureTemporalAssets()) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kAssets); + return true; + } if (temporal_provider_recreate_) { if (!CompleteSecondaryNativeFrameSlot()) return false; temporal_provider_recreate_ = false; @@ -163,6 +214,14 @@ bool Gta4NativeGraphicsSystem::PrepareTemporalFrame() { } temporal_configuration_ = config; temporal_metadata_ = *scene; +#if REX_PLATFORM_PS5 + // [ps5 port] FSR's history (upscaling and frame generation) counts scenes, + // one after another: the guest's frame count can advance twice per scene. + if (temporal_last_guest_sequence_ && scene->sequence != temporal_last_guest_sequence_ + 1) + rex::frame_stats::g_fg_sequence_gaps.fetch_add(1, std::memory_order_relaxed); + temporal_last_guest_sequence_ = scene->sequence; + ++temporal_history_sequence_; +#endif temporal_provider_ = selection.provider; const bool configured = temporal_provider_ == TemporalUpscalerProvider::kFsr3 ? fsr_upscaler_.Configure(config) : dlss_upscaler_.Configure(config); @@ -257,7 +316,7 @@ void Gta4NativeGraphicsSystem::ClassifyTemporalDraw(const NativeCommand& command if (projection->reversed) temporal_metadata_.flags |= kTemporalDepthReversed; else temporal_metadata_.flags &= ~kTemporalDepthReversed; temporal_camera_adopted_ = temporal_geometry_history_.Begin(temporal_metadata_.epoch, - temporal_metadata_.view, temporal_metadata_.sequence, + temporal_metadata_.view, TemporalHistorySequence(), !temporal_history_valid_ || bool(temporal_metadata_.flags & kTemporalCameraCut)); } } @@ -283,8 +342,29 @@ void Gta4NativeGraphicsSystem::ClassifyTemporalDraw(const NativeCommand& command (blending || (command.pipeline_state->pixel_shader_resource && IsFusionSkyShader(command.pipeline_state->pixel_shader_resource->hash))); if (policy.motion_relevant && (target.color_write_mask || fixed.depth_write_enable) && - ((!target.temporal_motion && !preserves) || (target.temporal_motion && !target.temporal_jitter))) + ((!target.temporal_motion && !preserves) || (target.temporal_motion && !target.temporal_jitter))) { +#if REX_PLATFORM_PS5 + // [ps5 port] Two kinds of draw no longer cost the frame its generated + // twin: one with motion but drawn without the frame's jitter (gta_default + // vs4: at most a sub-pixel shimmer on it), and one with no motion that + // writes no depth (hair's sorted alpha: drawn as reactive, like the sky). + // Every frame had one, so frame generation never made a frame. + const bool tolerated = target.temporal_motion || (target.temporal_reactive && !fixed.depth_write_enable); + if (tolerated) { + NOTE_FG_MOTION_LOST(target.temporal_motion ? "motion without jitter, tolerated" + : "no motion, no depth, tolerated", + command.pipeline_state); + } else { + if (temporal_motion_complete_) NOTE_FG_MOTION_LOST("no motion, not blended", command.pipeline_state); + temporal_motion_complete_ = false; + } +#else + if (temporal_motion_complete_) + NOTE_FG_MOTION_LOST(!target.temporal_motion ? "no motion, not blended" : "motion without jitter", + command.pipeline_state); temporal_motion_complete_ = false; +#endif + } } bool Gta4NativeGraphicsSystem::BindTemporalDraw(VkCommandBuffer cb, const NativeCommand& command, @@ -346,8 +426,10 @@ bool Gta4NativeGraphicsSystem::BindTemporalDraw(VkCommandBuffer cb, const Native NativeSharedConstants constants{}; std::memcpy(&constants, shared.mapping, sizeof(constants)); temporal_half_pixel_ = {constants.half_pixel_offset_x, constants.half_pixel_offset_y}; - if (constants.modern_effects.depth_range[0] != 0.0f || constants.modern_effects.depth_range[1] != 1.0f) + if (constants.modern_effects.depth_range[0] != 0.0f || constants.modern_effects.depth_range[1] != 1.0f) { + if (temporal_motion_complete_) NOTE_FG_MOTION_LOST("depth range", command.pipeline_state); temporal_motion_complete_ = false; + } temporal_depth_source_ = target.depth_surface; ++temporal_world_draws_; } @@ -380,11 +462,17 @@ bool Gta4NativeGraphicsSystem::ResolveTemporalScene(VkCommandBuffer cb, const Na } const auto color_layout = color.layout; bool encoded = false; - const bool continuity = temporal_history_valid_ && temporal_last_epoch_ == temporal_metadata_.epoch && - temporal_last_sequence_ != UINT64_MAX && temporal_metadata_.sequence == temporal_last_sequence_ + 1 && - temporal_metadata_.time_ns > temporal_last_time_ns_; const float delta_ms = temporal_metadata_.time_ns > temporal_last_time_ns_ && temporal_last_time_ns_ ? float(double(temporal_metadata_.time_ns - temporal_last_time_ns_) / 1000000.0) : 0.0f; + const bool continuity = temporal_history_valid_ && temporal_last_epoch_ == temporal_metadata_.epoch && + temporal_last_sequence_ != UINT64_MAX && TemporalHistorySequence() == temporal_last_sequence_ + 1 && + temporal_metadata_.time_ns > temporal_last_time_ns_ +#if REX_PLATFORM_PS5 + // (scenes counted one after another: a long gap between them, menus or + // loading, breaks the history as a skipped number did) + && delta_ms <= 250.0f +#endif + ; if (temporal_motion_complete_ && temporal_world_draws_ && temporal_depth_source_ && temporal_camera_adopted_ && delta_ms > 0.0f) { temporal::VulkanScene::Camera camera{}; @@ -406,12 +494,18 @@ bool Gta4NativeGraphicsSystem::ResolveTemporalScene(VkCommandBuffer cb, const Na frame.reactive = temporal_scene_.reactive(); frame.output = temporal_scene_.output(); frame.render_extent = temporal_configuration_.render_extent; frame.output_extent = temporal_configuration_.output_extent; - frame.sequence = temporal_metadata_.sequence; frame.epoch = temporal_metadata_.epoch; + frame.sequence = TemporalHistorySequence(); frame.epoch = temporal_metadata_.epoch; frame.time_ns = temporal_metadata_.time_ns; frame.frame_time_ms = delta_ms; frame.jitter_x = temporal_metadata_.jitter[0]; frame.jitter_y = temporal_metadata_.jitter[1]; frame.camera_near = temporal_metadata_.near_plane; frame.camera_far = temporal_metadata_.far_plane; frame.vertical_fov_radians = temporal_metadata_.field_of_view * (std::numbers::pi_v<float> / 180.0f); frame.reset = !continuity || (temporal_metadata_.flags & kTemporalCameraCut); +#if REX_PLATFORM_PS5 + // [ps5 port] A hitch between the two frames (2.5 of the slowest + // cadence's 33 ms): interpolating across it would smear. + if (rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed) && delta_ms > 85.0f) + frame.reset = true; +#endif frame.motion_complete = frame.scene_without_ui = true; { std::lock_guard lock(temporal_provider_mutex_); @@ -429,6 +523,13 @@ bool Gta4NativeGraphicsSystem::ResolveTemporalScene(VkCommandBuffer cb, const Na } } native_draw_state_cache_.Reset(); + if (!encoded) + rex::frame_stats::NoteFgDrop(!temporal_motion_complete_ ? rex::frame_stats::FgDrop::kSpatialMotion + : !temporal_world_draws_ ? rex::frame_stats::FgDrop::kSpatialWorld + : !temporal_depth_source_ ? rex::frame_stats::FgDrop::kSpatialDepth + : !temporal_camera_adopted_ ? rex::frame_stats::FgDrop::kSpatialCamera + : delta_ms <= 0.0f ? rex::frame_stats::FgDrop::kSpatialTime + : rex::frame_stats::FgDrop::kSpatialEncode); if (!encoded && !temporal_scene_.SpatialFallback(cb, color)) return false; temporal_resolved_ = encoded; // A spatial frame is a valid composite input but cannot seed vendor history. @@ -493,6 +594,31 @@ bool Gta4NativeGraphicsSystem::ReplayTemporalDisplayDraw(VkCommandBuffer cb, temporal_ui_exact_ = temporal_display_ready_ = false; return true; } } +#if REX_PLATFORM_PS5 + // [ps5 port] A HUD draw that blends with the alpha already on the screen + // (the radar: its mask shapes write alpha, the map is drawn through it). + // The replay's target starts and keeps its alpha differently from the + // game's 8-bit backbuffer (float, cleared to 0, later clears not mirrored), + // so such a frame is shown as the game drew it, without a generated twin: + // frame generation broke the radar (1.0.1). + if (!composite && REXCVAR_GET(ps5_fg_hud_alpha_guard)) { + const uint32_t packed = command.fixed_function_state.blend_controls[0]; + if (IsNativeBlendControlEnabled(packed)) { + const NativeBlendControlState blend = DecodeNativeBlendControl(packed); + const auto reads_alpha = [](uint32_t f) { return f == 10 || f == 11 || f == 16; }; + const auto reads_any = [](uint32_t f) { return (f >= 8 && f <= 11) || f == 16; }; + if (reads_alpha(blend.source_color) || reads_alpha(blend.destination_color) || + reads_any(blend.source_alpha) || reads_any(blend.destination_alpha)) { + static std::atomic<uint32_t> noted{0}; + if (noted.fetch_add(1, std::memory_order_relaxed) < 4) + REXLOG_INFO("ps5 frame generation: a HUD draw blends with the screen's alpha (blend {:08X}): that " + "frame shown as drawn", packed); + temporal_ui_exact_ = temporal_display_ready_ = false; + return true; + } + } + } +#endif if (!ReplayTemporalDraw(cb, command, original, resources, temporal_scene_.display(), composite)) { temporal_ui_exact_ = temporal_display_ready_ = false; return true; @@ -558,7 +684,7 @@ bool Gta4NativeGraphicsSystem::CompleteTemporalCoverage(VkCommandBuffer cb, bool std::lock_guard lock(temporal_provider_mutex_); if (temporal_encoded_) { if (temporal_provider_ == TemporalUpscalerProvider::kFsr3) fsr_upscaler_.OnSubmitted(); - else dlss_upscaler_.OnSubmitted(temporal_metadata_.sequence); + else dlss_upscaler_.OnSubmitted(TemporalHistorySequence()); temporal_encoded_ = false; } } @@ -601,35 +727,57 @@ bool Gta4NativeGraphicsSystem::EncodeTemporalGeneration(VkCommandBuffer cb) { // lease and submission notification, but do not publish stale scene color. temporal_generated_frame_.has_generated_frame = false; temporal_frame_generator_.Reset(); + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kSecondComposite); + return true; + } + const auto selected = CachedFlagByName("gta4_native_frame_generation"); + if (selected != "fsr3" && selected != "dlss") return true; + if (!temporal_encoded_ || !temporal_motion_complete_ || !temporal_camera_adopted_ || + temporal_presenter_acquired_) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kEncodeGate); return true; } - const auto selected = rex::cvar::GetFlagByName("gta4_native_frame_generation"); - if ((selected != "fsr3" && selected != "dlss") || !temporal_encoded_ || - !temporal_motion_complete_ || !temporal_camera_adopted_ || - temporal_presenter_acquired_) return true; auto* presenter = static_cast<ui::vulkan::VulkanPresenter*>(presenter_.get()); - if (!presenter || !presenter->CanPresentGeneratedFrames()) return true; + if (!presenter || !presenter->CanPresentGeneratedFrames()) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kPresenter); + return true; + } auto* provider = static_cast<ui::vulkan::VulkanProvider*>(provider_.get()); const auto* device = provider->vulkan_device(); - bool complete_motion = false; - if (!CompleteTemporalCoverage(cb, complete_motion)) return false; - if (!complete_motion) { - temporal_frame_generator_.Reset(); - return true; + // [ps5 port] Off on the PS5: the audit submits half the frame and waits for + // the GPU on the recorder, CPU and GPU in series, and the upscaler has just + // used the same motion and depth unaudited. + if (REXCVAR_GET(gta4_native_fg_motion_audit)) { + bool complete_motion = false; + if (!CompleteTemporalCoverage(cb, complete_motion)) return false; + if (!complete_motion) { + temporal_frame_generator_.Reset(); + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kAudit); + return true; + } } if (!temporal_frame_generator_.Initialize(*device) || !temporal_frame_generator_.Configure(selected == "dlss" ? temporal::FrameGenerationProvider::kDlss : - temporal::FrameGenerationProvider::kFsr, temporal_configuration_, false)) return true; + temporal::FrameGenerationProvider::kFsr, temporal_configuration_, false)) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kConfigure); + return true; + } temporal::FrameGenerationCamera camera{}; const auto inverse_projection = temporal::InverseCameraMatrix(temporal_metadata_.projection); const auto previous_view = temporal::InverseCameraMatrix(temporal_previous_inverse_); - if (!inverse_projection || !previous_view) return true; + if (!inverse_projection || !previous_view) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kCamera); + return true; + } camera.view_to_clip = temporal_metadata_.projection; camera.clip_to_view = *inverse_projection; camera.clip_to_previous = temporal::MultiplyCameraMatrices(temporal::MultiplyCameraMatrices( temporal::MultiplyCameraMatrices(*inverse_projection, temporal_metadata_.view_inverse), *previous_view), temporal_previous_projection_); const auto inverse_reprojection = temporal::InverseCameraMatrix(camera.clip_to_previous); - if (!inverse_reprojection) return true; + if (!inverse_reprojection) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kCamera); + return true; + } camera.previous_to_clip = *inverse_reprojection; for (size_t i = 0; i < 3; ++i) { camera.right[i] = temporal_metadata_.view_inverse[i]; @@ -654,7 +802,16 @@ bool Gta4NativeGraphicsSystem::EncodeTemporalGeneration(VkCommandBuffer cb) { temporal::VulkanScene::Barrier(device, cb, frame.color, color_layout); temporal::VulkanScene::Barrier(device, cb, frame.output, output_layout); if (temporal_generated_frame_.lease()) temporal_frame_leases_[active_frame_slot_].push_back(temporal_generated_frame_.lease()); - if (!encoded) temporal_generated_frame_.has_generated_frame = false; + if (!encoded) { + temporal_generated_frame_.has_generated_frame = false; + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kEncodeRefused); + static std::atomic<uint32_t> refusals_logged{0}; + if (rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed) && + refusals_logged.fetch_add(1, std::memory_order_relaxed) < 8) + REXLOG_INFO("ps5 frame generation: refused ({})", temporal_frame_generator_.status()); + } else if (!temporal_generated_frame_.has_generated_frame) { + rex::frame_stats::NoteFgDrop(rex::frame_stats::FgDrop::kHistoryReset); + } native_draw_state_cache_.Reset(); return true; } @@ -664,12 +821,12 @@ void Gta4NativeGraphicsSystem::SubmitTemporalFrame() { std::lock_guard lock(temporal_provider_mutex_); if (temporal_encoded_) { if (temporal_provider_ == TemporalUpscalerProvider::kFsr3) fsr_upscaler_.OnSubmitted(); - else dlss_upscaler_.OnSubmitted(temporal_metadata_.sequence); + else dlss_upscaler_.OnSubmitted(TemporalHistorySequence()); } - if (temporal_generated_frame_.lease()) temporal_frame_generator_.OnSubmitted(temporal_metadata_.sequence); + if (temporal_generated_frame_.lease()) temporal_frame_generator_.OnSubmitted(TemporalHistorySequence()); temporal_history_valid_ = temporal_camera_adopted_ && temporal_motion_complete_; temporal_last_time_ns_ = temporal_metadata_.time_ns; - temporal_last_sequence_ = temporal_metadata_.sequence; + temporal_last_sequence_ = TemporalHistorySequence(); temporal_last_epoch_ = temporal_metadata_.epoch; temporal_previous_projection_ = temporal_metadata_.projection; temporal_previous_inverse_ = temporal_metadata_.view_inverse; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/vulkan_frame_generation.cpp b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/vulkan_frame_generation.cpp index 753d6651..7a93ac4f 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/vulkan_frame_generation.cpp +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/temporal/vulkan_frame_generation.cpp @@ -5,6 +5,7 @@ #include <vector> #include <rex/assert.h> +#include <rex/platform.h> #include <rex/ui/vulkan/util.h> #include "dlss_frame_generation.h" @@ -17,7 +18,10 @@ constexpr VkImageUsageFlags kUsage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; // Mailboxes + in-flight display submissions + retained real + current encoding. // Derived with Python from the presenter's bounded rings. Allocation is lazy. -constexpr size_t kMaximumRetainedPairs = 8; +// [ps5 port] Six on the PS5: a set is four display-sized FP16 images (265 MB +// at 4K), and with the pair presented right away about four are in use; a +// frame past the limit just goes out without a generated partner. +constexpr size_t kMaximumRetainedPairs = REX_PLATFORM_PS5 ? 6 : 8; bool WaitIdle(const ui::vulkan::VulkanDevice& device, bool* device_lost = nullptr) { std::vector<ui::vulkan::VulkanDevice::Queue::Acquisition> locks; diff --git a/glue/rexglue-sdk-main/src/graphics/gta4_native/tv_trace_native.inc b/glue/rexglue-sdk-main/src/graphics/gta4_native/tv_trace_native.inc index a3ce500d..5fe10475 100644 --- a/glue/rexglue-sdk-main/src/graphics/gta4_native/tv_trace_native.inc +++ b/glue/rexglue-sdk-main/src/graphics/gta4_native/tv_trace_native.inc @@ -1,6 +1,6 @@ // Diagnostic-only TV content lineage. Every GPU copy restores its source layout. uint32_t Gta4NativeGraphicsSystem::TvCommandRole(const NativeCommand& command) const { - if (!command.tv_trace) return 0; + if (!(command.trace_flags & NativeCommand::kTraceTv) || !command.tv_trace) return 0; const auto& c = *command.tv_trace; uint32_t role = 0; if (command.pipeline_state) { diff --git a/glue/rexglue-sdk-main/src/graphics/pipeline/texture/cache.cpp b/glue/rexglue-sdk-main/src/graphics/pipeline/texture/cache.cpp index c7c824e3..63f155fd 100644 --- a/glue/rexglue-sdk-main/src/graphics/pipeline/texture/cache.cpp +++ b/glue/rexglue-sdk-main/src/graphics/pipeline/texture/cache.cpp @@ -200,6 +200,14 @@ const TextureCache::LoadShaderInfo TextureCache::load_shader_info_[kLoadShaderCo {4, 4, 4, 3}, }; +namespace { +TextureCache::BeforeTextureReadCallback g_before_texture_read = nullptr; +} // namespace + +void TextureCache::SetBeforeTextureReadCallback(BeforeTextureReadCallback callback) { + g_before_texture_read = callback; +} + TextureCache::TextureCache(const RegisterFile& register_file, SharedMemory& shared_memory, uint32_t draw_resolution_scale_x, uint32_t draw_resolution_scale_y) : register_file_(register_file), @@ -549,6 +557,13 @@ void TextureCache::RequestTextures(uint32_t used_texture_mask) { } binding.texture_signed = nullptr; } + if (g_before_texture_read) { + Texture* loading = load_unsigned_data ? binding.texture + : (load_signed_data ? binding.texture_signed : nullptr); + if (loading && loading->outdated_mask()) { + g_before_texture_read(fetch); + } + } if (load_unsigned_data) { queue_pending_texture_load(binding.texture); } @@ -683,7 +698,7 @@ TextureCache::Texture::~Texture() { } void TextureCache::Texture::MakeUpToDateAndWatch( - const std::unique_lock<std::recursive_mutex>& global_lock) { + const rex::thread::global_lock_t& global_lock) { SharedMemory& shared_memory = texture_cache().shared_memory(); if (base_outdated_) { assert_not_zero(GetGuestBaseSize()); @@ -729,7 +744,7 @@ void TextureCache::Texture::MarkAsUsed() { } void TextureCache::Texture::WatchCallback( - [[maybe_unused]] const std::unique_lock<std::recursive_mutex>& global_lock, bool is_mip) { + [[maybe_unused]] const rex::thread::global_lock_t& global_lock, bool is_mip) { if (is_mip) { assert_not_zero(GetGuestMipsSize()); mips_outdated_ = true; @@ -743,7 +758,7 @@ void TextureCache::Texture::WatchCallback( } } -void TextureCache::WatchCallback(const std::unique_lock<std::recursive_mutex>& global_lock, +void TextureCache::WatchCallback(const rex::thread::global_lock_t& global_lock, void* context, void* data, uint64_t argument, bool invalidated_by_gpu) { Texture& texture = *static_cast<Texture*>(context); @@ -1060,7 +1075,7 @@ bool TextureCache::IsRangeScaledResolved(uint32_t start_unscaled, uint32_t lengt } void TextureCache::ScaledResolveGlobalWatchCallbackThunk( - const std::unique_lock<std::recursive_mutex>& global_lock, void* context, + const rex::thread::global_lock_t& global_lock, void* context, uint32_t address_first, uint32_t address_last, bool invalidated_by_gpu) { TextureCache* texture_cache = reinterpret_cast<TextureCache*>(context); texture_cache->ScaledResolveGlobalWatchCallback(global_lock, address_first, address_last, @@ -1068,7 +1083,7 @@ void TextureCache::ScaledResolveGlobalWatchCallbackThunk( } void TextureCache::ScaledResolveGlobalWatchCallback( - const std::unique_lock<std::recursive_mutex>& global_lock, uint32_t address_first, + const rex::thread::global_lock_t& global_lock, uint32_t address_first, uint32_t address_last, bool invalidated_by_gpu) { assert_true(IsDrawResolutionScaled()); if (invalidated_by_gpu) { diff --git a/glue/rexglue-sdk-main/src/graphics/shared_memory.cpp b/glue/rexglue-sdk-main/src/graphics/shared_memory.cpp index 333becb8..59967902 100644 --- a/glue/rexglue-sdk-main/src/graphics/shared_memory.cpp +++ b/glue/rexglue-sdk-main/src/graphics/shared_memory.cpp @@ -17,6 +17,8 @@ #include <rex/bit.h> #include <rex/dbg.h> #include <rex/graphics/shared_memory.h> +#include <rex/hash.h> +#include <rex/logging.h> #include <rex/math.h> #include <rex/memory.h> @@ -35,6 +37,8 @@ void SharedMemory::InitializeCommon() { valid_buffer_a_.assign(num_system_page_flags_, 0); valid_buffer_b_.assign(num_system_page_flags_, 0); system_page_flags_valid_and_gpu_written_.assign(num_system_page_flags_, 0); + page_hashes_.assign(kBufferSize >> page_size_log2_, 0); + stale_check_cursor_ = 0; active_valid_flags_.store(valid_buffer_a_.data(), std::memory_order_relaxed); staging_valid_flags_.store(valid_buffer_b_.data(), std::memory_order_relaxed); gpu_written_data_dirty_.store(false, std::memory_order_relaxed); @@ -578,6 +582,89 @@ bool SharedMemory::RequestRange(uint32_t start, uint32_t length) { return RequestRanges(&range, 1); } +void SharedMemory::RecordPageHashes(uint32_t page_first, uint32_t page_count) { + const size_t page_size = size_t(1) << page_size_log2_; + for (uint32_t page = page_first; page_count && page < page_hashes_.size(); ++page, --page_count) { + const uint64_t hash = + XXH3_64bits(memory_.TranslatePhysical((page << page_size_log2_) & (kBufferSize - 1)), page_size); + page_hashes_[page] = hash ? hash : 1; + } +} + +uint32_t SharedMemory::CheckStalePages(uint32_t pages_per_frame) { + const uint32_t page_count = kBufferSize >> page_size_log2_; + if (!pages_per_frame || page_hashes_.size() != page_count) { + return 0; + } + const uint32_t page_size = uint32_t(1) << page_size_log2_; + uint32_t cursor = stale_check_cursor_ & (page_count - 1); + uint32_t remaining = std::min(pages_per_frame, page_count); + uint32_t found = 0; + while (remaining) { + // (one 64-page block of flags at a time) + const uint32_t chunk = std::min({(cursor & ~uint32_t(63)) + 64 - cursor, page_count - cursor, remaining}); + const uint32_t block = cursor >> 6; + uint64_t candidates = 0; + { + auto global_lock = global_critical_region_.Acquire(); + const uint64_t* valid_flags = active_valid_flags_.load(std::memory_order_relaxed); + if (valid_flags) { + candidates = valid_flags[block] & ~system_page_flags_valid_and_gpu_written_[block]; + } + } + for (uint32_t page = cursor; page < cursor + chunk; ++page) { + const uint64_t stored = page_hashes_[page]; + const uint64_t bit = uint64_t(1) << (page & 63); + if (!stored || !(candidates & bit)) { + continue; + } + const uint32_t address = page << page_size_log2_; + { + // (under the lock: a page still valid is still committed - freeing + // guest memory invalidates it in the same lock) + auto global_lock = global_critical_region_.Acquire(); + const uint64_t* valid_flags = active_valid_flags_.load(std::memory_order_relaxed); + if (!valid_flags || !(valid_flags[block] & bit) || + (system_page_flags_valid_and_gpu_written_[block] & bit)) { + continue; // (a watch fired meanwhile) + } + // (a page the guest made unreadable keeps its GPU copy: reading it + // would fault) + uint32_t protect = 0; + auto* physical_heap = memory_.GetPhysicalHeap(); + if (!physical_heap || !physical_heap->QueryProtect(address, &protect) || + !(protect & memory::kMemoryProtectRead)) { + continue; + } + if (XXH3_64bits(memory_.TranslatePhysical(address), page_size) == stored) { + continue; + } + } + page_hashes_[page] = 0; + ++found; + ++stale_pages_found_; + if (stale_pages_found_ <= 64 || !(stale_pages_found_ & (stale_pages_found_ - 1))) { + REXGPU_WARN("GPU copy of physical {:08X} +{:X} out of date, no write watch fired ({} so far)", address, + page_size, stale_pages_found_); + } + // (each physical view keeps its own watches) + memory_.TriggerPhysicalMemoryCallbacks(rex::thread::global_critical_region::AcquireDirect(), + 0xA0000000u + address, page_size, true, true); + memory_.TriggerPhysicalMemoryCallbacks(rex::thread::global_critical_region::AcquireDirect(), + 0xC0000000u + address, page_size, true, true); + if (address >= 0x1000) { + memory_.TriggerPhysicalMemoryCallbacks(rex::thread::global_critical_region::AcquireDirect(), + 0xE0000000u + address - 0x1000, page_size, true, true); + } + MemoryInvalidationCallback(address, page_size, true); + } + cursor = (cursor + chunk) & (page_count - 1); + remaining -= chunk; + } + stale_check_cursor_ = cursor; + return found; +} + std::pair<uint32_t, uint32_t> SharedMemory::MemoryInvalidationCallbackThunk( void* context_ptr, uint32_t physical_address_start, uint32_t length, bool exact_range) { return reinterpret_cast<SharedMemory*>(context_ptr) diff --git a/glue/rexglue-sdk-main/src/graphics/vulkan/command_processor.cpp b/glue/rexglue-sdk-main/src/graphics/vulkan/command_processor.cpp index 185597a9..ddeec405 100644 --- a/glue/rexglue-sdk-main/src/graphics/vulkan/command_processor.cpp +++ b/glue/rexglue-sdk-main/src/graphics/vulkan/command_processor.cpp @@ -47,6 +47,9 @@ #include <rex/ui/vulkan/util.h> // Legacy backend compatibility aliases for shared readback controls. +REXCVAR_DEFINE_INT32(gpu_stale_check_pages_per_frame, 128, "GPU", + "[ps5 port] Pages of the GPU's copy of guest memory checked each frame for writes no " + "write watch saw (reloaded when found; 0: off)"); REXCVAR_DEFINE_BOOL(vulkan_readback_resolve, false, "GPU/Vulkan", "Read render-to-texture results on the CPU") .lifecycle(rex::cvar::Lifecycle::kHotReload); @@ -2285,6 +2288,9 @@ void VulkanCommandProcessor::OnGammaRampPWLValueWritten() { void VulkanCommandProcessor::IssueSwap(uint32_t frontbuffer_ptr, uint32_t frontbuffer_width, uint32_t frontbuffer_height) { SCOPE_profile_cpu_f("gpu"); + if (shared_memory_ && REXCVAR_GET(gpu_stale_check_pages_per_frame) > 0) { + shared_memory_->CheckStalePages(uint32_t(REXCVAR_GET(gpu_stale_check_pages_per_frame))); + } vertex_buffers_in_sync_[0] = 0; vertex_buffers_in_sync_[1] = 0; diff --git a/glue/rexglue-sdk-main/src/graphics/vulkan/shared_memory.cpp b/glue/rexglue-sdk-main/src/graphics/vulkan/shared_memory.cpp index b52ae15a..5fd4dec4 100644 --- a/glue/rexglue-sdk-main/src/graphics/vulkan/shared_memory.cpp +++ b/glue/rexglue-sdk-main/src/graphics/vulkan/shared_memory.cpp @@ -375,6 +375,9 @@ bool VulkanSharedMemory::UploadRanges( break; } MakeRangeValid(upload_range_start << page_size_log2(), uint32_t(upload_buffer_size), false); + // (hashed before the copy: a write no watch sees landing in between + // then shows up as stale - one extra reload - instead of being missed) + RecordPageHashes(upload_range_start, uint32_t(upload_buffer_size >> page_size_log2())); std::memcpy(upload_buffer_mapping, memory().TranslatePhysical(upload_range_start << page_size_log2()), upload_buffer_size); diff --git a/glue/rexglue-sdk-main/src/input/CMakeLists.txt b/glue/rexglue-sdk-main/src/input/CMakeLists.txt index 7016987a..90f2bb7c 100644 --- a/glue/rexglue-sdk-main/src/input/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/input/CMakeLists.txt @@ -12,6 +12,10 @@ add_library(rexinput OBJECT nop/nop_input_driver.cpp ) add_library(rex::input ALIAS rexinput) +if(PS5) + # [ps5 port] DualSense through scePad + target_sources(rexinput PRIVATE ps5/ps5_input_driver.cpp) +endif() target_include_directories(rexinput PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> diff --git a/glue/rexglue-sdk-main/src/input/input_system.cpp b/glue/rexglue-sdk-main/src/input/input_system.cpp index b2b99ec4..d96f32c1 100644 --- a/glue/rexglue-sdk-main/src/input/input_system.cpp +++ b/glue/rexglue-sdk-main/src/input/input_system.cpp @@ -20,6 +20,9 @@ #include <rex/input/input_system.h> #include <rex/input/input_trace.h> #include <rex/input/mnk/mnk_input_driver.h> +#if REX_PLATFORM_PS5 +#include <rex/input/ps5/ps5_input_driver.h> +#endif #include <rex/input/nop/nop_input_driver.h> #if REX_PLATFORM_NX #include "switch/switch_input_driver.h" @@ -349,6 +352,38 @@ bool InputSystem::TryGetMotionState(uint32_t user_index, MotionState* out_state) return false; } +bool InputSystem::GetUiGamepadState(X_INPUT_GAMEPAD* out_gamepad) { + X_INPUT_GAMEPAD merged = {}; + bool any = false; + + auto merge_axis = [](int16_t a, int16_t b) -> int16_t { + return (std::abs(static_cast<int>(a)) >= std::abs(static_cast<int>(b))) ? a : b; + }; + + for (auto& driver : drivers_) { + for (uint32_t user_index = 0; user_index < 4; ++user_index) { + X_INPUT_STATE state = {}; + if (driver->GetStateUi(user_index, &state) != X_ERROR_SUCCESS) { + continue; + } + any = true; + merged.buttons = static_cast<uint16_t>(merged.buttons) | + static_cast<uint16_t>(state.gamepad.buttons); + merged.left_trigger = std::max(merged.left_trigger, state.gamepad.left_trigger); + merged.right_trigger = std::max(merged.right_trigger, state.gamepad.right_trigger); + merged.thumb_lx = merge_axis(merged.thumb_lx, state.gamepad.thumb_lx); + merged.thumb_ly = merge_axis(merged.thumb_ly, state.gamepad.thumb_ly); + merged.thumb_rx = merge_axis(merged.thumb_rx, state.gamepad.thumb_rx); + merged.thumb_ry = merge_axis(merged.thumb_ry, state.gamepad.thumb_ry); + } + } + + if (out_gamepad) { + *out_gamepad = merged; + } + return any; +} + std::unique_ptr<InputSystem> CreateDefaultInputSystem(bool tool_mode) { auto input = std::make_unique<InputSystem>(nullptr); @@ -356,6 +391,10 @@ std::unique_ptr<InputSystem> CreateDefaultInputSystem(bool tool_mode) { #if REX_PLATFORM_NX auto switch_driver = std::make_unique<nx::SwitchInputDriver>(nullptr, 0); if (switch_driver->Setup() == X_STATUS_SUCCESS) input->AddDriver(std::move(switch_driver)); +#elif REX_PLATFORM_PS5 + // [ps5 port] DualSense pads; SDL has no PS5 controller backend. + auto ps5_driver = std::make_unique<ps5::Ps5InputDriver>(nullptr, 0); + if (ps5_driver->Setup() == X_STATUS_SUCCESS) input->AddDriver(std::move(ps5_driver)); #else const bool expose_sdl_gamepad_state = REXCVAR_GET(input_backend) == "sdl"; auto sdl_driver = std::make_unique<sdl::SDLInputDriver>(nullptr, 0, expose_sdl_gamepad_state); diff --git a/glue/rexglue-sdk-main/src/input/ps5/ps5_input_driver.cpp b/glue/rexglue-sdk-main/src/input/ps5/ps5_input_driver.cpp new file mode 100644 index 00000000..74462b71 --- /dev/null +++ b/glue/rexglue-sdk-main/src/input/ps5/ps5_input_driver.cpp @@ -0,0 +1,446 @@ +/** + * [ps5 port] DualSense input via scePad (layout verified on hardware by + * Nazky's ps5-plume-triangle). + * + * Mapping (Xbox 360 <- DualSense): A <- Cross, B <- Circle, X <- Square, + * Y <- Triangle, LB/RB <- L1/R1, LT/RT <- L2/R2 (analog), START <- Options, + * BACK <- touchpad click, stick clicks <- L3/R3, d-pad <- d-pad. + * + * And the DualSense features the PC port has (rex::input::sony, Settings' + * DualSense Features): the light bar (the game's colour, police lights while + * wanted), adaptive triggers (a gun's trigger pull and its recoil, aiming, + * pedals) and added rumble for shots, hits and explosions. Only the + * controller's output: the game's own rumble is kept as it was. Rumble needs + * the controller's compatible dual-motor mode (scePadSetVibrationMode 2, as + * Porpoise and ProsperoLight set it): without it the PS5 drops it. + */ +#include <rex/input/ps5/ps5_input_driver.h> + +#include <algorithm> +#include <chrono> +#include <cmath> +#include <cstring> + +#include <SDL3/SDL_timer.h> + +#include <rex/logging.h> +#include <rex/ui/virtual_key.h> + +extern "C" { +int sceUserServiceInitialize(void* params); +int sceUserServiceGetForegroundUser(int32_t* user_id); +int sceUserServiceGetInitialUser(int32_t* user_id); +struct SceUserServiceLoginUserIdList { + int32_t user_id[4]; +}; +int sceUserServiceGetLoginUserIdList(SceUserServiceLoginUserIdList* list); +int scePadInit(void); +int scePadOpen(int32_t user_id, int32_t type, int32_t index, const void* param); +int scePadGetHandle(int32_t user_id, int32_t type, int32_t index); +int scePadReadState(int32_t handle, void* data); +int scePadClose(int32_t handle); +struct ScePadVibrationParam { + uint8_t large_motor; + uint8_t small_motor; +}; +int scePadSetVibration(int32_t handle, const ScePadVibrationParam* param); +int scePadSetVibrationMode(int32_t handle, int32_t mode); +struct ScePadLightBarParam { + uint8_t r, g, b, reserved; +}; +int scePadSetLightBar(int32_t handle, const ScePadLightBarParam* param); +int scePadResetLightBar(int32_t handle); +// The adaptive triggers: one command per trigger, its mode's parameters in +// data (the layout Porpoise uses on the console, after ProsperoEden's +// ps5_pad.hpp). Modes: 0 off, 1 feedback (data: position 0-9, strength 1-8), +// 2 weapon (start 2-7, end start+1..8, strength 0-8), 3 vibration (position +// 0-9, amplitude 0-8, frequency 0-255 Hz). +struct ScePadTriggerEffectCommand { + int32_t mode; + int32_t reserved; + uint8_t data[48]; +}; +struct ScePadTriggerEffectParam { + uint8_t trigger_mask; // 1 L2, 2 R2 + uint8_t reserved[7]; + ScePadTriggerEffectCommand l2, r2; +}; +static_assert(sizeof(ScePadTriggerEffectCommand) == 56 && sizeof(ScePadTriggerEffectParam) == 120, + "the console's trigger effect layout"); +int scePadSetTriggerEffect(int32_t handle, const ScePadTriggerEffectParam* param); +} + +namespace rex::input::ps5 { + +namespace { + +constexpr int32_t kInvalidUser = -1; +constexpr int32_t kSystemUser = 0xFF; + +// The head of ScePadData (the rest - touch, motion, ... - is unused). +struct ScePadDataHead { + uint32_t buttons; + uint8_t left_stick_x; + uint8_t left_stick_y; + uint8_t right_stick_x; + uint8_t right_stick_y; + uint8_t analog_l2; + uint8_t analog_r2; + uint16_t padding; + uint8_t rest[240]; // room for the whole ScePadData +}; + +constexpr uint32_t kPadL3 = 0x00000002u; +constexpr uint32_t kPadR3 = 0x00000004u; +constexpr uint32_t kPadOptions = 0x00000008u; +constexpr uint32_t kPadUp = 0x00000010u; +constexpr uint32_t kPadRight = 0x00000020u; +constexpr uint32_t kPadDown = 0x00000040u; +constexpr uint32_t kPadLeft = 0x00000080u; +constexpr uint32_t kPadL1 = 0x00000400u; +constexpr uint32_t kPadR1 = 0x00000800u; +constexpr uint32_t kPadTriangle = 0x00001000u; +constexpr uint32_t kPadCircle = 0x00002000u; +constexpr uint32_t kPadCross = 0x00004000u; +constexpr uint32_t kPadSquare = 0x00008000u; +constexpr uint32_t kPadTouchpad = 0x00100000u; + +// 0..255 (128 centre) to the Xbox's -32768..32767, Y flipped so up is +. +int16_t AxisToXbox(uint8_t value, bool flip) { + int v = (int(value) - 128) * 256; + if (flip) v = -v - 1; + if (v > 32767) v = 32767; + if (v < -32768) v = -32768; + return int16_t(v); +} + +uint64_t NowMs() { + return uint64_t(std::chrono::duration_cast<std::chrono::milliseconds>( + std::chrono::steady_clock::now().time_since_epoch()) + .count()); +} + +// A strength (0..1) on the console's scale lo..hi. +uint8_t Level(float strength, int lo, int hi) { + const float s = std::isfinite(strength) ? std::clamp(strength, 0.0f, 1.0f) : 0.0f; + return uint8_t(std::clamp(int(std::lround(float(lo) + float(hi - lo) * s)), lo, hi)); +} + +// A trigger's feel as the console's trigger modes: a gun's trigger gives way +// partway, as a real one breaks (weapon mode), and each shot kicks it +// (vibration); aiming and pedals push back evenly (feedback). +void FillTrigger(ScePadTriggerEffectCommand& c, const rex::input::sony::TriggerFeel& feel) { + using Feel = rex::input::sony::TriggerFeel; + std::memset(&c, 0, sizeof(c)); + switch (feel.kind) { + case Feel::kPedal: + c.mode = 1, c.data[0] = 1, c.data[1] = Level(feel.strength, 1, 8); + break; + case Feel::kAim: + c.mode = 1, c.data[0] = 2, c.data[1] = Level(feel.strength, 1, 4); + break; + case Feel::kGun: + c.mode = 2, c.data[0] = 4, c.data[1] = 6, c.data[2] = Level(feel.strength, 3, 8); + break; + case Feel::kRecoil: + c.mode = 3, c.data[0] = 2, c.data[1] = Level(feel.strength, 2, 8), c.data[2] = 40; + break; + // (the mode and its parameters as given: Service::Compose's guns and driving) + case Feel::kFeedback: + c.mode = 1, c.data[0] = std::min<uint8_t>(feel.a, 9), c.data[1] = std::clamp<uint8_t>(feel.b, 1, 8); + break; + case Feel::kWeapon: { + const uint8_t start = std::clamp<uint8_t>(feel.a, 2, 7); + c.mode = 2, c.data[0] = start, c.data[1] = std::clamp<uint8_t>(feel.b, uint8_t(start + 1), 8), + c.data[2] = std::min<uint8_t>(feel.c, 8); + break; + } + case Feel::kVibration: + if (!feel.b) break; + c.mode = 3, c.data[0] = std::min<uint8_t>(feel.a, 9), c.data[1] = std::min<uint8_t>(feel.b, 8), + c.data[2] = feel.c; + break; + default: break; // (mode 0: off) + } +} + +} // namespace + +Ps5InputDriver::Ps5InputDriver(rex::ui::Window* window, size_t window_z_order) + : InputDriver(window, window_z_order) {} + +Ps5InputDriver::~Ps5InputDriver() { + std::lock_guard<std::mutex> lock(mutex_); + for (uint32_t i = 0; i < kMaxPads; ++i) { + Pad& pad = pads_[i]; + if (pad.handle < 0) continue; + // (the controller as the system had it: its light, no trigger effects, still) + if (pad.light_set) scePadResetLightBar(pad.handle); + if (pad.triggers_set) { + ScePadTriggerEffectParam off{}; + off.trigger_mask = 1 | 2; + scePadSetTriggerEffect(pad.handle, &off); + } + ScePadVibrationParam still{0, 0}; + scePadSetVibration(pad.handle, &still); + rex::input::sony::GetService().Disconnect(uint32_t(pad.handle) + 1); + scePadClose(pad.handle); + } +} + +X_STATUS Ps5InputDriver::Setup() { + sceUserServiceInitialize(nullptr); + int rc = scePadInit(); + if (rc < 0) { + REXLOG_ERROR("PS5 input: scePadInit failed ({:#x})", uint32_t(rc)); + return X_STATUS_UNSUCCESSFUL; + } + rex::input::sony::GetService().SetFocused(true); + std::lock_guard<std::mutex> lock(mutex_); + RefreshPads(); + return X_STATUS_SUCCESS; +} + +// Opens a pad for every logged-in user that hasn't one yet (users log in and +// controllers connect while the game runs). Called at most twice a second. +void Ps5InputDriver::RefreshPads() { + SceUserServiceLoginUserIdList users; + for (int32_t& u : users.user_id) u = kInvalidUser; + if (sceUserServiceGetLoginUserIdList(&users) < 0) { + int32_t fg = kInvalidUser; + if (sceUserServiceGetForegroundUser(&fg) == 0) users.user_id[0] = fg; + } + for (int32_t user : users.user_id) { + if (user == kInvalidUser || user == kSystemUser) continue; + bool known = false; + for (const Pad& pad : pads_) known |= pad.user_id == user && pad.handle >= 0; + if (known) continue; + for (uint32_t i = 0; i < kMaxPads; ++i) { + Pad& pad = pads_[i]; + if (pad.handle >= 0) continue; + int32_t handle = scePadOpen(user, 0, 0, nullptr); + if (handle < 0) handle = scePadGetHandle(user, 0, 0); + if (handle < 0) break; + pad.user_id = user; + pad.packet_number = 0; + pad.generation = next_generation_++; + // the DualSense features: this controller has them all + rex::input::sony::Device device{}; + device.instance_id = uint32_t(handle) + 1; + device.generation = pad.generation; + device.model = rex::input::sony::Model::kDualSense; + device.light = device.rumble = device.triggers = true; + rex::input::sony::GetService().Connect(i, device); + pad.handle = handle; + const int mode = scePadSetVibrationMode(handle, 2); // (rumble: see the top) + REXLOG_INFO("PS5 input: DualSense for user {:#x} (handle {}, rumble mode {:#x})", uint32_t(user), handle, + uint32_t(mode)); + break; + } + } + last_refresh_ms_ = NowMs(); +} + +Ps5InputDriver::Pad* Ps5InputDriver::PadFor(uint32_t user_index) { + if (user_index >= kMaxPads) return nullptr; + if (NowMs() - last_refresh_ms_ > 500) RefreshPads(); + Pad& pad = pads_[user_index]; + return pad.handle >= 0 ? &pad : nullptr; +} + +X_RESULT Ps5InputDriver::ReadState(uint32_t user_index, bool active, X_INPUT_STATE* out_state) { + Pad* pad = PadFor(user_index); + if (!pad) return X_ERROR_DEVICE_NOT_CONNECTED; + ScePadDataHead data; + std::memset(&data, 0, sizeof(data)); + if (scePadReadState(pad->handle, &data) != 0) { + return X_ERROR_DEVICE_NOT_CONNECTED; + } + // (the pedals' push-back follows how far down they are) + rex::input::sony::GetService().UpdateAxes(user_index, data.analog_l2, data.analog_r2); + ApplyFeedback(user_index, *pad); + if (!out_state) return X_ERROR_SUCCESS; + const uint32_t b = active ? data.buttons : 0; + uint16_t buttons = 0; + if (b & kPadUp) buttons |= X_INPUT_GAMEPAD_DPAD_UP; + if (b & kPadDown) buttons |= X_INPUT_GAMEPAD_DPAD_DOWN; + if (b & kPadLeft) buttons |= X_INPUT_GAMEPAD_DPAD_LEFT; + if (b & kPadRight) buttons |= X_INPUT_GAMEPAD_DPAD_RIGHT; + if (b & kPadOptions) buttons |= X_INPUT_GAMEPAD_START; + if (b & kPadTouchpad) buttons |= X_INPUT_GAMEPAD_BACK; + if (b & kPadL3) buttons |= X_INPUT_GAMEPAD_LEFT_THUMB; + if (b & kPadR3) buttons |= X_INPUT_GAMEPAD_RIGHT_THUMB; + if (b & kPadL1) buttons |= X_INPUT_GAMEPAD_LEFT_SHOULDER; + if (b & kPadR1) buttons |= X_INPUT_GAMEPAD_RIGHT_SHOULDER; + if (b & kPadCross) buttons |= X_INPUT_GAMEPAD_A; + if (b & kPadCircle) buttons |= X_INPUT_GAMEPAD_B; + if (b & kPadSquare) buttons |= X_INPUT_GAMEPAD_X; + if (b & kPadTriangle) buttons |= X_INPUT_GAMEPAD_Y; + + X_INPUT_GAMEPAD gp{}; + gp.buttons = buttons; + gp.left_trigger = active ? data.analog_l2 : 0; + gp.right_trigger = active ? data.analog_r2 : 0; + gp.thumb_lx = active ? AxisToXbox(data.left_stick_x, false) : 0; + gp.thumb_ly = active ? AxisToXbox(data.left_stick_y, true) : 0; + gp.thumb_rx = active ? AxisToXbox(data.right_stick_x, false) : 0; + gp.thumb_ry = active ? AxisToXbox(data.right_stick_y, true) : 0; + // The packet number only moves when the state does, as XInput's does. + if (std::memcmp(&gp, &pad->last_gamepad, sizeof(gp)) != 0) { + ++pad->packet_number; + std::memcpy(&pad->last_gamepad, &gp, sizeof(gp)); + } + out_state->gamepad = gp; + out_state->packet_number = pad->packet_number; + return X_ERROR_SUCCESS; +} + +X_RESULT Ps5InputDriver::GetState(uint32_t user_index, X_INPUT_STATE* out_state) { + std::lock_guard<std::mutex> lock(mutex_); + return ReadState(user_index, is_active(), out_state); +} + +// The host's own menus (settings, dialogs) read the pad even while the game's +// input is held back. +X_RESULT Ps5InputDriver::GetStateUi(uint32_t user_index, X_INPUT_STATE* out_state) { + std::lock_guard<std::mutex> lock(mutex_); + return ReadState(user_index, true, out_state); +} + +X_RESULT Ps5InputDriver::GetCapabilities(uint32_t user_index, uint32_t flags, + X_INPUT_CAPABILITIES* out_caps) { + (void)flags; + std::lock_guard<std::mutex> lock(mutex_); + if (!PadFor(user_index)) return X_ERROR_DEVICE_NOT_CONNECTED; + if (out_caps) { + std::memset(out_caps, 0, sizeof(*out_caps)); + out_caps->type = 0x01; // XINPUT_DEVTYPE_GAMEPAD + out_caps->sub_type = 0x01; // XINPUT_DEVSUBTYPE_GAMEPAD + out_caps->gamepad.buttons = 0xF3FF; + out_caps->gamepad.left_trigger = 0xFF; + out_caps->gamepad.right_trigger = 0xFF; + out_caps->gamepad.thumb_lx = int16_t(0x7FFF); + out_caps->gamepad.thumb_ly = int16_t(0x7FFF); + out_caps->gamepad.thumb_rx = int16_t(0x7FFF); + out_caps->gamepad.thumb_ry = int16_t(0x7FFF); + out_caps->vibration.left_motor_speed = 0xFFFF; + out_caps->vibration.right_motor_speed = 0xFFFF; + } + return X_ERROR_SUCCESS; +} + +X_RESULT Ps5InputDriver::SetState(uint32_t user_index, X_INPUT_VIBRATION* vibration) { + std::lock_guard<std::mutex> lock(mutex_); + Pad* pad = PadFor(user_index); + if (!pad) return X_ERROR_DEVICE_NOT_CONNECTED; + if (vibration) { + pad->native_low = vibration->left_motor_speed; + pad->native_high = vibration->right_motor_speed; + SendVibration(*pad); + } + return X_ERROR_SUCCESS; +} + +// The game's rumble, with the added pulses over it (the stronger of each motor). +void Ps5InputDriver::SendVibration(Pad& pad) { + ScePadVibrationParam param; + param.large_motor = uint8_t(std::max(pad.native_low, pad.pulse_low) >> 8); + param.small_motor = uint8_t(std::max(pad.native_high, pad.pulse_high) >> 8); + scePadSetVibration(pad.handle, ¶m); + pad.sent_large = param.large_motor; + pad.sent_small = param.small_motor; +} + +// (under mutex_, with the game's read of this controller) +void Ps5InputDriver::ApplyFeedback(uint32_t user_index, Pad& pad) { + namespace sony = rex::input::sony; + const uint64_t now = SDL_GetTicks(); + if (now < pad.next_feedback_ms) return; + pad.next_feedback_ms = now + 8; + if (sony::GetService().ReadDevice(user_index).generation != pad.generation) return; + const sony::Options options = sony::GetOptions(); + const sony::Output out = sony::GetService().Compose(user_index, now, options); + const int32_t handle = pad.handle; + // rumble: the pulses, when they start and stop + const uint16_t low = out.active ? out.low_motor : 0, high = out.active ? out.high_motor : 0; + if (low != pad.pulse_low || high != pad.pulse_high) { + pad.pulse_low = low, pad.pulse_high = high; + SendVibration(pad); + } + // the light bar: the game's colour (police lights while wanted), else the system's + if (out.active && out.lighting) { + if (!pad.light_set || out.color != pad.color) { + const ScePadLightBarParam light{out.color[0], out.color[1], out.color[2], 0}; + if (scePadSetLightBar(handle, &light) == 0) pad.light_set = true, pad.color = out.color; + } + } else if (pad.light_set) { + scePadResetLightBar(handle); + pad.light_set = false; + } + // the triggers + if (pad.trigger_failures >= 3) return; + ScePadTriggerEffectParam param{}; + param.trigger_mask = 1 | 2; + FillTrigger(param.l2, out.active ? out.left_feel : sony::TriggerFeel{}); + FillTrigger(param.r2, out.active ? out.right_feel : sony::TriggerFeel{}); + const bool off = param.l2.mode == 0 && param.r2.mode == 0; + if ((!pad.triggers_set && off) || + std::memcmp(¶m, pad.trigger_param.data(), sizeof(param)) == 0) + return; + const int result = scePadSetTriggerEffect(handle, ¶m); + if (result == 0) { + std::memcpy(pad.trigger_param.data(), ¶m, sizeof(param)); + pad.triggers_set = !off; + pad.trigger_failures = 0; + } else if (++pad.trigger_failures >= 3) { + REXLOG_WARN("PS5 input: adaptive triggers refused ({:#x}); off for this controller", uint32_t(result)); + sony::GetService().DisableTriggers(user_index, pad.generation); + } +} + +// Button presses and releases as keystroke events, one per call (the XInput +// button field, then the triggers as buttons), like the SDL driver's. +X_RESULT Ps5InputDriver::GetKeystroke(uint32_t user_index, uint32_t flags, + X_INPUT_KEYSTROKE* out_keystroke) { + (void)flags; + if (!out_keystroke) return X_ERROR_BAD_ARGUMENTS; + static constexpr rex::ui::VirtualKey kVk[18] = { + rex::ui::VirtualKey::kXInputPadDpadUp, rex::ui::VirtualKey::kXInputPadDpadDown, + rex::ui::VirtualKey::kXInputPadDpadLeft, rex::ui::VirtualKey::kXInputPadDpadRight, + rex::ui::VirtualKey::kXInputPadStart, rex::ui::VirtualKey::kXInputPadBack, + rex::ui::VirtualKey::kXInputPadLThumbPress, rex::ui::VirtualKey::kXInputPadRThumbPress, + rex::ui::VirtualKey::kXInputPadLShoulder, rex::ui::VirtualKey::kXInputPadRShoulder, + rex::ui::VirtualKey::kNone, rex::ui::VirtualKey::kNone, + rex::ui::VirtualKey::kXInputPadA, rex::ui::VirtualKey::kXInputPadB, + rex::ui::VirtualKey::kXInputPadX, rex::ui::VirtualKey::kXInputPadY, + rex::ui::VirtualKey::kXInputPadLTrigger, rex::ui::VirtualKey::kXInputPadRTrigger, + }; + std::lock_guard<std::mutex> lock(mutex_); + const bool any = user_index == 0xFF; + bool connected = false; + for (uint32_t u = any ? 0 : user_index; u < (any ? kMaxPads : user_index + 1); ++u) { + X_INPUT_STATE state{}; + if (ReadState(u, is_active(), &state) != X_ERROR_SUCCESS) continue; + connected = true; + Pad& pad = pads_[u]; + uint32_t now = state.gamepad.buttons; + if (state.gamepad.left_trigger > 30) now |= 1u << 16; + if (state.gamepad.right_trigger > 30) now |= 1u << 17; + const uint32_t changed = now ^ pad.keystroke_buttons; + for (uint32_t bit = 0; bit < 18; ++bit) { + if (!(changed & (1u << bit)) || kVk[bit] == rex::ui::VirtualKey::kNone) continue; + const bool down = (now >> bit) & 1; + pad.keystroke_buttons ^= 1u << bit; + out_keystroke->virtual_key = uint16_t(kVk[bit]); + out_keystroke->unicode = 0; + out_keystroke->user_index = uint8_t(u); + out_keystroke->hid_code = 0; + out_keystroke->flags = down ? X_INPUT_KEYSTROKE_KEYDOWN : X_INPUT_KEYSTROKE_KEYUP; + return X_ERROR_SUCCESS; + } + pad.keystroke_buttons = now; + } + return connected ? X_ERROR_EMPTY : X_ERROR_DEVICE_NOT_CONNECTED; +} + +} // namespace rex::input::ps5 diff --git a/glue/rexglue-sdk-main/src/input/sony_feedback.cpp b/glue/rexglue-sdk-main/src/input/sony_feedback.cpp index c4809b38..5387ca20 100644 --- a/glue/rexglue-sdk-main/src/input/sony_feedback.cpp +++ b/glue/rexglue-sdk-main/src/input/sony_feedback.cpp @@ -15,7 +15,54 @@ float Unit(float value) { return std::isfinite(value) ? std::clamp(value, 0.0f, 1.0f) : 0.0f; } uint16_t Motor(float value) { return static_cast<uint16_t>(Unit(value) * 65535.0f); } -uint8_t Force(float value) { return static_cast<uint8_t>(Unit(value) * 110.0f); } + +// GTA IV's guns by weapon slot. Ready to fire, the trigger has a wall that +// gives way (weapon mode: where it starts and ends, how hard); a single shot +// kicks it (vibration: amplitude, Hz, how long); held automatic fire knocks +// it once a round (its frequency the measured rate of fire). Each shot also +// shakes the controller (low and high motors, how long). +struct GunFeel { + uint8_t start = 0, end = 0, strength = 0; // the trigger before a shot + uint8_t aim = 0; // L2 while aiming (feedback) + uint8_t kick = 0, kick_hz = 0; // a single shot's kick (0 Hz: 30) + uint16_t kick_ms = 0; + float low = 0, high = 0; // the body rumble of a shot + uint16_t rumble_ms = 0; +}; +constexpr std::array<GunFeel, 10> kGuns{{ + {}, // 0 unarmed + {}, // 1 melee + {3, 5, 5, 2, 8, 30, 70, 0.30f, 0.70f, 70}, // 2 handgun: light take-up, crisp break + {3, 6, 8, 3, 8, 18, 130, 1.00f, 0.80f, 150}, // 3 shotgun: heavy, a deep thump + {2, 4, 3, 2, 6, 0, 45, 0.18f, 0.50f, 45}, // 4 SMG: light, rattles while held + {3, 5, 4, 3, 7, 0, 55, 0.28f, 0.60f, 55}, // 5 rifle + {4, 6, 8, 4, 8, 22, 140, 0.90f, 0.95f, 170}, // 6 sniper: heavy, one big kick + {4, 7, 8, 5, 8, 14, 220, 1.00f, 0.70f, 300}, // 7 heavy: rockets + {2, 3, 2, 1, 0, 0, 0, 0.00f, 0.00f, 0}, // 8 thrown: a light pull + {}, // 9 special +}}; +const GunFeel& Gun(int slot) { return kGuns[size_t(std::clamp(slot, 0, 9))]; } + +// The console's trigger modes as DualSense report bytes, for the drivers that +// send those (the PS5's reads TriggerFeel itself). +Trigger Report(const TriggerFeel& feel) { + Trigger result{}; + switch (feel.kind) { + case TriggerFeel::kFeedback: + case TriggerFeel::kWeapon: + return Resistance(uint8_t(std::min(feel.a * 28, 255)), + uint8_t(std::min((feel.kind == TriggerFeel::kWeapon ? feel.c : feel.b) * 13, 110))); + case TriggerFeel::kVibration: + if (!feel.b) break; + result[0] = 0x06; + result[1] = feel.c; + result[2] = uint8_t(std::min(feel.b * 8, 63)); + result[3] = uint8_t(std::min(feel.a * 28, 255)); + return result; + default: break; + } + return Resistance(0, 0); +} } // namespace // These basic modes and byte layouts are exercised by the pinned SDL @@ -61,6 +108,7 @@ bool Service::Fresh(const Slot& slot, uint64_t now_ms) { void Service::Cancel(Slot& slot) { CancelGestures(slot); slot.pulses = {}; + slot.shot_rumble_until_ms = 0; } void Service::CancelGestures(Slot& slot) { @@ -235,6 +283,28 @@ void Service::Emit(uint32_t user, uint64_t generation, Event event, float streng if (user >= slots_.size() || index >= kPulseMs.size() || !generation) return; auto& slot = slots_[user]; if (!focused_ || slot.device.generation != generation || !Fresh(slot, now_ms) || !slot.game.active) return; + if (event == Event::kShot) { + // How fast the gun fires (shots under 0.4 s apart, same weapon slot), and + // the controller's shake for this shot by the kind of gun. In rapid fire + // each shake ends before the next shot, so the rounds are felt one by one. + const int weapon = std::clamp(slot.game.weapon_slot, 0, 9); + auto& shots = slot.shots; + if (shots.last_ms && now_ms > shots.last_ms && now_ms - shots.last_ms < 400 && shots.slot == weapon) { + const float gap = float(now_ms - shots.last_ms); + shots.interval_ms = shots.interval_ms > 0 ? shots.interval_ms * 0.6f + gap * 0.4f : gap; + } else { + shots.interval_ms = 0; + } + shots.last_ms = now_ms; + shots.slot = weapon; + const GunFeel& gun = Gun(weapon); + float ms = gun.rumble_ms ? gun.rumble_ms : 80.0f; + if (shots.interval_ms > 0) ms = std::min(ms, std::max(25.0f, shots.interval_ms * 0.55f)); + slot.shot_rumble_until_ms = now_ms + uint64_t(ms); + slot.shot_low = (gun.rumble_ms ? gun.low : 0.35f) * Unit(strength); + slot.shot_high = (gun.rumble_ms ? gun.high : 1.0f) * Unit(strength); + return; + } auto& pulse = slot.pulses[index]; const float previous = now_ms < pulse.until_ms ? pulse.strength : 0.0f; pulse = {now_ms + kPulseMs[index], std::max(previous, Unit(strength))}; @@ -263,19 +333,56 @@ Output Service::Compose(uint32_t user, uint64_t now_ms, const Options& options) const auto& value = slot.pulses[static_cast<size_t>(event)]; return now_ms < value.until_ms ? value.strength : 0.0f; }; - const float shot = pulse(Event::kShot), damage = pulse(Event::kDamage), explosion = pulse(Event::kExplosion); + const float damage = pulse(Event::kDamage), explosion = pulse(Event::kExplosion); + const bool shooting = now_ms < slot.shot_rumble_until_ms; + const bool driving = slot.game.trigger_context == TriggerContext::kVehicle && slot.game.driver; + const float speed = std::isfinite(slot.game.speed) ? std::max(slot.game.speed, 0.0f) : 0.0f; if (options.rumble && slot.device.rumble) { - output.low_motor = Motor(std::max({shot * 0.35f, damage * 0.75f, explosion}) * intensity); - output.high_motor = Motor(std::max({shot, damage * 0.5f, explosion * 0.65f}) * intensity); + const float low = std::max({shooting ? slot.shot_low : 0.0f, damage * 0.75f, explosion}); + const float high = std::max({shooting ? slot.shot_high : 0.0f, damage * 0.5f, explosion * 0.65f}); + output.low_motor = Motor(low * intensity); + output.high_motor = Motor(high * intensity); } if (options.triggers && slot.device.triggers && intensity > 0) { - if (slot.game.trigger_context == TriggerContext::kVehicle && slot.game.driver) { - output.left = Resistance(20, Force(intensity * (0.25f + 0.5f * slot.device.left_trigger / 255.0f))); - output.right = Resistance(20, Force(intensity * (0.25f + 0.5f * slot.device.right_trigger / 255.0f))); + using Feel = TriggerFeel; + // A strength on the console's 1-8 scale, by the intensity setting (as + // given at the default 50%, half at 0%, one and a half times at 100%). + auto scaled = [&](int value) { + return uint8_t(std::clamp(int(std::lround(float(value) * (0.5f + intensity))), 1, 8)); + }; + auto set = [&](Feel& feel, Trigger& report, Feel::Kind kind, uint8_t a, uint8_t b, uint8_t c) { + feel = {kind, intensity, a, b, c}; + report = Report(feel); + }; + if (driving) { + // Pedals, nothing shaking (1.0.3 Test 3's road hum and trigger buzz were + // too much): the gas pushes back more the further it goes, and harder + // the faster the car is going; the brake is a firm pedal throughout. + const float throttle = slot.device.right_trigger / 255.0f, brake = slot.device.left_trigger / 255.0f; + const float load = std::min(speed / 40.0f, 1.0f); + set(output.right_feel, output.right, Feel::kFeedback, 1, + scaled(1 + int(std::lround(3.0f * std::pow(throttle, 1.3f) + load))), 0); + set(output.left_feel, output.left, Feel::kFeedback, 2, + scaled(3 + int(std::lround(3.0f * brake))), 0); } else if (slot.game.trigger_context == TriggerContext::kWeapon) { - if (slot.game.aiming) output.left = Resistance(30, Force(intensity * 0.5f)); - if (slot.game.can_fire) output.right = Resistance(90, Force(intensity)); - if (shot > 0) output.right = Recoil(static_cast<uint8_t>(63.0f * intensity * shot)); + const GunFeel& gun = Gun(slot.game.weapon_slot); + if (slot.game.aiming && gun.aim) set(output.left_feel, output.left, Feel::kFeedback, 2, scaled(gun.aim), 0); + if (slot.game.can_fire) { + const auto& shots = slot.shots; + const uint64_t since = now_ms >= shots.last_ms ? now_ms - shots.last_ms : UINT64_MAX; + const bool rapid = shots.interval_ms > 0 && shots.interval_ms < 260 && + float(since) < shots.interval_ms * 1.6f + 40.0f; + if (rapid) { + // held automatic fire: a knock a round + set(output.right_feel, output.right, Feel::kVibration, 1, scaled(gun.kick ? gun.kick - 1 : 6), + uint8_t(std::clamp(int(std::lround(1000.0f / shots.interval_ms)), 5, 30))); + } else if (since < gun.kick_ms) { + set(output.right_feel, output.right, Feel::kVibration, 1, scaled(gun.kick), + gun.kick_hz ? gun.kick_hz : 30); + } else if (gun.strength) { + set(output.right_feel, output.right, Feel::kWeapon, gun.start, gun.end, scaled(gun.strength)); + } + } } } return output; diff --git a/glue/rexglue-sdk-main/src/kernel/crt/memory.cpp b/glue/rexglue-sdk-main/src/kernel/crt/memory.cpp index 31c53846..a3ef2b66 100644 --- a/glue/rexglue-sdk-main/src/kernel/crt/memory.cpp +++ b/glue/rexglue-sdk-main/src/kernel/crt/memory.cpp @@ -10,6 +10,107 @@ #include <cstring> #include <rex/hook.h> +#if REX_PLATFORM_PS5 +#include <algorithm> +#include <array> +#include <atomic> +#include <chrono> +#include <mutex> +#include <string> +#include <vector> + +#include <fmt/format.h> +#include <rex/cvar.h> +#include <rex/logging.h> + +REXCVAR_DEFINE_BOOL(ps5_memory_census, true, "PS5", + "Every 10 s, log which game code cleared and copied the most memory (memset, memcpy and " + "their Xbox variants), by caller"); + +namespace rex::kernel::crt::census { +// [ps5 port] 1.0.3 Test 1's profile had one of the game's threads spend about +// 60% of its time in memset. Which code clears (and copies) that much, how +// often and in what sizes: the guest return address of each call (passed by +// the hooks below), counted in a fixed table. No thread_local: on the PS5 +// each one is a lookup through the system, on the game's hottest call. +struct Slot { + std::atomic<uint32_t> caller{0}; + std::atomic<uint64_t> calls{0}, bytes{0}, largest{0}; +}; +constexpr uint32_t kSlots = 1024; +std::array<Slot, kSlots> g_set, g_copy; +std::atomic<uint64_t> g_set_bytes{0}, g_copy_bytes{0}; +std::atomic<int64_t> g_reported_ns{0}; +std::mutex g_report_mutex; + +int64_t NowNs() { + return std::chrono::duration_cast<std::chrono::nanoseconds>( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); +} + +// (returns the slot's call count before this one) +uint64_t Count(std::array<Slot, kSlots>& table, uint32_t caller, uint64_t n) { + uint32_t i = (caller * 2654435761u) >> 22; // (10 bits) + for (uint32_t probe = 0; probe < 16; ++probe, i = (i + 1) % kSlots) { + Slot& slot = table[i]; + uint32_t key = slot.caller.load(std::memory_order_relaxed); + if (key == 0 && slot.caller.compare_exchange_strong(key, caller, std::memory_order_relaxed)) key = caller; + if (key != caller) continue; + const uint64_t calls = slot.calls.fetch_add(1, std::memory_order_relaxed); + slot.bytes.fetch_add(n, std::memory_order_relaxed); + uint64_t largest = slot.largest.load(std::memory_order_relaxed); + while (n > largest && !slot.largest.compare_exchange_weak(largest, n, std::memory_order_relaxed)) {} + return calls; + } + return 0; +} + +void Report(const char* what, std::array<Slot, kSlots>& table, uint64_t total, double seconds) { + struct Row { uint32_t caller; uint64_t calls, bytes, largest; }; + std::vector<Row> rows; + for (Slot& slot : table) { + const uint32_t caller = slot.caller.load(std::memory_order_relaxed); + if (!caller) continue; + const Row row{caller, slot.calls.exchange(0, std::memory_order_relaxed), + slot.bytes.exchange(0, std::memory_order_relaxed), + slot.largest.exchange(0, std::memory_order_relaxed)}; + if (row.calls) rows.push_back(row); + } + std::sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) { return a.bytes > b.bytes; }); + std::string line; + for (size_t i = 0; i < rows.size() && i < 12; ++i) { + const Row& r = rows[i]; + line += fmt::format(" {:08X}: {} calls {:.1f} MB (avg {} B, largest {} KB);", r.caller, r.calls, + double(r.bytes) / 1048576.0, r.bytes / r.calls, r.largest / 1024); + } + REXLOG_INFO("ps5 {}: {:.0f} MB a second over {:.0f} s; by caller:{}", what, + double(total) / 1048576.0 / seconds, seconds, line); +} + +void Note(bool copy, uint64_t n, uint32_t caller) { + if (!REXCVAR_GET(ps5_memory_census)) return; + const uint64_t calls = Count(copy ? g_copy : g_set, caller, n); + (copy ? g_copy_bytes : g_set_bytes).fetch_add(n, std::memory_order_relaxed); + // every 10 s, from whichever thread gets here first (the clock read only + // on every 256th call from one caller: a system call on each would cost + // more than the count) + if ((calls & 255) != 255) return; + const int64_t now = NowNs(); + int64_t last = g_reported_ns.load(std::memory_order_relaxed); + if (!last) { + g_reported_ns.compare_exchange_strong(last, now, std::memory_order_relaxed); + return; + } + if (now - last < 10'000'000'000ll) return; + if (!g_reported_ns.compare_exchange_strong(last, now, std::memory_order_relaxed)) return; + std::lock_guard lock(g_report_mutex); + const double seconds = double(now - last) / 1e9; + Report("memset", g_set, g_set_bytes.exchange(0, std::memory_order_relaxed), seconds); + Report("memcpy", g_copy, g_copy_bytes.exchange(0, std::memory_order_relaxed), seconds); +} +} // namespace rex::kernel::crt::census +#endif namespace rex::kernel::crt { @@ -73,13 +174,62 @@ static int native_memmove_s(void* dst, size_t dstsz, const void* src, size_t cou } // namespace rex::kernel::crt +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_memcpy) { + rex::kernel::crt::census::Note(true, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_memcpy>(ctx, base); +} +#else REX_HOOK(rexcrt_memcpy, rex::kernel::crt::native_memcpy) +#endif +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_memmove) { + rex::kernel::crt::census::Note(true, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_memmove>(ctx, base); +} +#else REX_HOOK(rexcrt_memmove, rex::kernel::crt::native_memmove) +#endif +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_memset) { + rex::kernel::crt::census::Note(false, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_memset>(ctx, base); +} +#else REX_HOOK(rexcrt_memset, rex::kernel::crt::native_memset) +#endif REX_HOOK(rexcrt_memchr, rex::kernel::crt::native_memchr) +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_XMemCpy) { + rex::kernel::crt::census::Note(true, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_XMemCpy>(ctx, base); +} +#else REX_HOOK(rexcrt_XMemCpy, rex::kernel::crt::native_XMemCpy) +#endif +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_XMemSet) { + rex::kernel::crt::census::Note(false, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_XMemSet>(ctx, base); +} +#else REX_HOOK(rexcrt_XMemSet, rex::kernel::crt::native_XMemSet) +#endif +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_XMemSet128) { + rex::kernel::crt::census::Note(false, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_XMemSet128>(ctx, base); +} +#else REX_HOOK(rexcrt_XMemSet128, rex::kernel::crt::native_XMemSet128) +#endif +#if REX_PLATFORM_PS5 +REX_HOOK_RAW(rexcrt_memset_vmx) { + rex::kernel::crt::census::Note(false, ctx.r5.u32, uint32_t(ctx.lr)); + rex::ppc::HostToGuestFunction<rex::kernel::crt::native_memset_vmx>(ctx, base); +} +#else REX_HOOK(rexcrt_memset_vmx, rex::kernel::crt::native_memset_vmx) +#endif REX_HOOK(rexcrt_memcpy_s, rex::kernel::crt::native_memcpy_s) REX_HOOK(rexcrt_memmove_s, rex::kernel::crt::native_memmove_s) diff --git a/glue/rexglue-sdk-main/src/kernel/xam/xam_info.cpp b/glue/rexglue-sdk-main/src/kernel/xam/xam_info.cpp index 1fda0536..40f497b5 100644 --- a/glue/rexglue-sdk-main/src/kernel/xam/xam_info.cpp +++ b/glue/rexglue-sdk-main/src/kernel/xam/xam_info.cpp @@ -12,6 +12,10 @@ #include <rex/kernel/xam/module.h> #include <rex/kernel/xam/private.h> #include <rex/logging.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif +#include <rex/cvar.h> #include <rex/hook.h> #include <rex/types.h> #include <rex/string.h> @@ -281,6 +285,10 @@ void XamLoaderLaunchTitle_entry(mapped_string raw_name_ptr, u32 flags) { loader_data.launch_path = path; } } else { +#if REX_PLATFORM_PS5 + REXLOG_INFO("The game asked to quit (XamLoaderLaunchTitle, no title)"); + if (rex::ps5diag::title_exit_hook) rex::ps5diag::title_exit_hook(); +#endif assert_always("Game requested exit to dashboard via XamLoaderLaunchTitle"); } @@ -289,6 +297,10 @@ void XamLoaderLaunchTitle_entry(mapped_string raw_name_ptr, u32 flags) { } void XamLoaderTerminateTitle_entry() { +#if REX_PLATFORM_PS5 + REXLOG_INFO("The game asked to quit (XamLoaderTerminateTitle)"); + if (rex::ps5diag::title_exit_hook) rex::ps5diag::title_exit_hook(); +#endif // This function does not return. REX_KERNEL_STATE()->TerminateTitle(); } diff --git a/glue/rexglue-sdk-main/src/kernel/xam/xam_input.cpp b/glue/rexglue-sdk-main/src/kernel/xam/xam_input.cpp index 197d072d..887ab28e 100644 --- a/glue/rexglue-sdk-main/src/kernel/xam/xam_input.cpp +++ b/glue/rexglue-sdk-main/src/kernel/xam/xam_input.cpp @@ -114,6 +114,15 @@ u32 XamInputGetState_entry(u32 user_index, u32 flags, ppc_ptr_t<X_INPUT_STATE> i actual_user_index = 0; } +#if REX_PLATFORM_PS5 + // [ps5 port] While a system dialog (message box, keyboard) is up, it owns + // the controller - as on the console, the game sees no buttons - and only + // the dialog reads the pad. + if (xeXamIsUIActive() && input_state) { + std::memset(static_cast<X_INPUT_STATE*>(input_state), 0, sizeof(X_INPUT_STATE)); + return X_ERROR_SUCCESS; + } +#endif auto* is = input_system(); const u32 result = is->GetState(actual_user_index, input_state); const uint64_t causal_sequence = rex::input::InputTraceCausalSequence(); @@ -187,6 +196,9 @@ u32 XamInputGetKeystrokeEx_entry(mapped_u32 user_index_ptr, u32 flags, user_index = 0; } +#if REX_PLATFORM_PS5 + if (xeXamIsUIActive()) return X_ERROR_EMPTY; +#endif auto* is = input_system(); auto result = is->GetKeystroke(user_index, flags, keystroke); if (XSUCCEEDED(result)) { diff --git a/glue/rexglue-sdk-main/src/kernel/xam/xam_ui.cpp b/glue/rexglue-sdk-main/src/kernel/xam/xam_ui.cpp index a3984881..b61eee97 100644 --- a/glue/rexglue-sdk-main/src/kernel/xam/xam_ui.cpp +++ b/glue/rexglue-sdk-main/src/kernel/xam/xam_ui.cpp @@ -29,6 +29,13 @@ REXCVAR_DEFINE_BOOL(headless, false, "Kernel", #include <rex/ui/imgui_drawer.h> #include <rex/ui/window.h> #include <rex/ui/windowed_app_context.h> +#if REX_PLATFORM_PS5 +#include <algorithm> +#include <cctype> +#include <chrono> +#include <rex/input/input_system.h> +#include <rex/ui/pad_dialog_style.h> +#endif namespace rex { namespace kernel { @@ -209,6 +216,376 @@ u32 XamIsUIActive_entry() { return xeXamIsUIActive(); } + +#if REX_PLATFORM_PS5 +namespace { +// [ps5 port] The controller drives the system dialogs (a console has no mouse +// or keyboard for them): Poll() gives the buttons pressed since the last frame, +// with hold-to-repeat on the d-pad / left stick. Buttons already held when the +// dialog opens (the press that opened it) don't count. +class DialogPad { + public: + uint16_t Poll() { + using namespace rex::input; + using clock = std::chrono::steady_clock; + uint16_t now = 0; + auto* is = static_cast<InputSystem*>(REX_KERNEL_STATE()->emulator()->input_system()); + // The UI path: the game's own input is switched off while a system + // dialog is open (its active callback), the dialog's isn't. + X_INPUT_GAMEPAD gp{}; + if (is && is->GetUiGamepadState(&gp)) { + now = gp.buttons; + const int lx = gp.thumb_lx, ly = gp.thumb_ly; + if (lx < -16000) now |= X_INPUT_GAMEPAD_DPAD_LEFT; + if (lx > 16000) now |= X_INPUT_GAMEPAD_DPAD_RIGHT; + if (ly > 16000) now |= X_INPUT_GAMEPAD_DPAD_UP; + if (ly < -16000) now |= X_INPUT_GAMEPAD_DPAD_DOWN; + } + const auto t = clock::now(); + uint16_t pressed = now & ~held_; + constexpr uint16_t kDirs = 0x000F; + if (now & kDirs) { + if ((now & kDirs) != (held_ & kDirs)) { + repeat_at_ = t + std::chrono::milliseconds(380); + } else if (t >= repeat_at_) { + pressed |= now & kDirs; + repeat_at_ = t + std::chrono::milliseconds(85); + } + } + if (first_) { + pressed = 0; + first_ = false; + } + held_ = now; + return pressed; + } + + private: + uint16_t held_ = 0; + bool first_ = true; + std::chrono::steady_clock::time_point repeat_at_{}; +}; + +constexpr uint16_t kPadUp = 0x0001, kPadDown = 0x0002, kPadLeft = 0x0004, kPadRight = 0x0008, + kPadStart = 0x0010, kPadL1 = 0x0100, kPadR1 = 0x0200, kPadA = 0x1000, + kPadB = 0x2000, kPadX = 0x4000, kPadY = 0x8000; + +// A button drawn highlighted when it has the controller's focus. +bool PadButton(const char* label, bool focused, const ImVec2& size = ImVec2(0, 0)) { + if (focused) { + ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); + ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1, 1, 1, 1)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f); + } + bool clicked = ImGui::Button(label, size); + if (focused) { + ImGui::PopStyleVar(); + ImGui::PopStyleColor(2); + } + return clicked; +} + +// [ps5 port] The on-screen keyboard in the app's style (rex/ui/pad_dialog_style.h): +// a QWERTY board like the console's own, the focus gliding between the keys, +// and the controller's buttons shown for what they do. Laid out on a +// 1920 x 1080 page, scaled to the display. +class StyledPadKeyboard { + public: + StyledPadKeyboard(std::string title, std::string description, std::string text, size_t limit) + : title_(std::move(title)), description_(std::move(description)), text_(std::move(text)), limit_(limit) { + upper_ = text_.empty(); + } + const std::string& text() const { return text_; } + + // One frame; `pressed`: the buttons pressed since the last (Xbox bits). + // 0: still open, 1: done, -1: cancelled. + int Draw(const rex::ui::PadDialogStyle& st, uint16_t pressed, float dt) { + st_ = &st; + dt_ = std::clamp(dt, 0.0f, 0.1f); + time_ += dt_; + const int result = closing_ ? 0 : Input(pressed); + if (result) closing_ = result; + Ease(in_, closing_ ? 0.0f : 1.0f, closing_ ? 18.0f : 12.0f); + Ease(press_, 0, 10); + Ease(value_in_, 1, 14); + Render(); + return closing_ && in_ < 0.02f ? closing_ : 0; + } + + private: + static constexpr int kCols = 11; + static constexpr const char* kRows[2][4] = {{"1234567890-", "QWERTYUIOP'", "ASDFGHJKL.!", "ZXCVBNM,?&_"}, + {"1234567890-", "qwertyuiop'", "asdfghjkl.!", "zxcvbnm,?&_"}}; + // the last row: SHIFT, SPACE, DELETE, DONE - their widths in columns + static constexpr int kSpecialSpan[4] = {2, 5, 2, 2}; + static constexpr float kKeyW = 84, kKeyH = 70, kGap = 10; + static constexpr float kW = 1920, kH = 1080, kPanelW = 1152; + + // ------------------------------------------------------------ the keys + int SpecialAt(int col) const { // the special key under a column + int c = 0; + for (int k = 0; k < 4; ++k) { + c += kSpecialSpan[k]; + if (col < c) return k; + } + return 3; + } + int SpecialCol(int k) const { // a special key's middle column + int c = 0; + for (int i = 0; i < k; ++i) c += kSpecialSpan[i]; + return c + kSpecialSpan[k] / 2; + } + // the focused key's box (page coordinates) + void KeyBox(int row, int col, float* x, float* y, float* w) const { + const float gx = (kW - (kCols * kKeyW + (kCols - 1) * kGap)) * 0.5f, gy = GridY(); + *y = gy + row * (kKeyH + kGap); + if (row < 4) { + *x = gx + col * (kKeyW + kGap); + *w = kKeyW; + return; + } + const int k = SpecialAt(col); + int c0 = 0; + for (int i = 0; i < k; ++i) c0 += kSpecialSpan[i]; + *x = gx + c0 * (kKeyW + kGap); + *w = kSpecialSpan[k] * kKeyW + (kSpecialSpan[k] - 1) * kGap; + } + float PanelY() const { return 150 + (1 - Smooth(in_)) * 40; } + float GridY() const { return PanelY() + (description_.empty() ? 250 : 290); } + + void Sound(int s) { + if (st_->sound) st_->sound(s); + } + bool Full() const { return text_.size() >= limit_; } + void Type(char c) { + if (Full()) { + Sound(7); + return; + } + text_.push_back(c); + value_in_ = 0; + // (a capital first letter, then small ones - unless Shift is held down by the player) + if (upper_ && !caps_ && std::isalpha(static_cast<unsigned char>(c))) upper_ = false; + Sound(1); + } + void Erase() { + if (text_.empty()) { + Sound(7); + return; + } + text_.pop_back(); + if (text_.empty() && !caps_) upper_ = true; + Sound(2); + } + void Shift() { + // Shift once: the next letter; twice: caps lock; again: off + if (!upper_) upper_ = true, caps_ = false; + else if (!caps_) caps_ = true; + else upper_ = caps_ = false; + Sound(3); + } + + int Input(uint16_t p) { + const int before_row = row_, before_col = col_; + if (p & kPadLeft) { + if (row_ < 4) col_ = (col_ + kCols - 1) % kCols; + else col_ = SpecialCol((SpecialAt(col_) + 3) % 4); + } + if (p & kPadRight) { + if (row_ < 4) col_ = (col_ + 1) % kCols; + else col_ = SpecialCol((SpecialAt(col_) + 1) % 4); + } + if (p & kPadDown) { + row_ = (row_ + 1) % 5; + if (row_ == 4) col_ = SpecialCol(SpecialAt(col_)); + } + if (p & kPadUp) { + row_ = (row_ + 4) % 5; + if (row_ == 4) col_ = SpecialCol(SpecialAt(col_)); + } + if (row_ != before_row || col_ != before_col) Sound(0); + if (p & kPadL1) Shift(); + if (p & kPadX) press_ = 0.6f, Erase(); + if (p & kPadY) press_ = 0.6f, Type(' '); + if (p & kPadA) { + press_ = 1; + if (row_ < 4) { + Type(kRows[upper_ ? 0 : 1][row_][col_]); + } else { + switch (SpecialAt(col_)) { + case 0: Shift(); break; + case 1: Type(' '); break; + case 2: Erase(); break; + case 3: Sound(4); return 1; + } + } + } + if (p & kPadStart) { + Sound(4); + return 1; + } + if (p & kPadB) { + if (text_.empty()) { + Sound(2); + return -1; + } + Erase(); + } + return 0; + } + + // ------------------------------------------------------------ drawing + static float Smooth(float t) { + t = std::clamp(t, 0.0f, 1.0f); + return t * t * (3 - 2 * t); + } + void Ease(float& v, float target, float speed) { + v = v < -0.5f ? target : v + (target - v) * (1.0f - std::exp(-dt_ * speed)); + } + static ImU32 White(float a) { return IM_COL32(255, 255, 255, int(std::clamp(a, 0.0f, 1.0f) * 255)); } + static ImU32 Black(float a) { return IM_COL32(0, 0, 0, int(std::clamp(a, 0.0f, 1.0f) * 255)); } + static ImU32 Alpha(ImU32 c, float a) { + const int base = int((c >> IM_COL32_A_SHIFT) & 0xFF); + return (c & ~IM_COL32_A_MASK) | (ImU32(std::clamp(int(base * a), 0, 255)) << IM_COL32_A_SHIFT); + } + ImVec2 P(float x, float y) const { return ImVec2(ox_ + x * s_, oy_ + y * s_); } + float TextW(ImFont* f, float size, const std::string& t) const { + return (f ? f : ImGui::GetFont())->CalcTextSizeA(size, FLT_MAX, 0, t.c_str()).x; + } + void Text(ImFont* f, float size, float x, float y, ImU32 col, const std::string& t, float align = 0) { + if (!f) f = ImGui::GetFont(); + dl_->AddText(f, size * s_, P(x - TextW(f, size, t) * align, y), col, t.c_str()); + } + void Rect(float x, float y, float w, float h, ImU32 col, float r = 0) { + dl_->AddRectFilled(P(x, y), P(x + w, y + h), col, r * s_); + } + // A controller button: the app's picture, else drawn. + void Glyph(uint32_t button, float cx, float cy, ImU32 col) { + if (auto it = st_->buttons.find(button); it != st_->buttons.end() && it->second) { + const float h = 24; + dl_->AddImage(it->second, P(cx - h, cy - h), P(cx + h, cy + h), ImVec2(0, 0), ImVec2(1, 1), col); + return; + } + const char* label = button == kPadA ? "X" : button == kPadB ? "O" : button == kPadX ? "[]" : button == kPadY ? "/\\" + : button == kPadL1 ? "L1" : button == kPadStart ? "=" : "+"; + dl_->AddCircle(P(cx, cy), 15 * s_, col, 32, 1.6f * s_); + Text(st_->bold, 15, cx, cy - 9, col, label, 0.5f); + } + float Hint(float right, float y, uint32_t button, const char* label) { + const float tw = TextW(st_->bold, 20, label); + const float x = right - tw; + Text(st_->bold, 20, x, y - 13, White(0.78f), label); + Glyph(button, x - 12 - 15, y, White(0.9f)); + return x - 12 - 30 - 36; + } + + void Render() { + const ImVec2 size = ImGui::GetIO().DisplaySize; + s_ = std::min(size.x / kW, size.y / kH); + ox_ = (size.x - kW * s_) * 0.5f; + oy_ = (size.y - kH * s_) * 0.5f; + dl_ = ImGui::GetForegroundDrawList(); + const float a = Smooth(in_); + dl_->AddRectFilled(ImVec2(0, 0), size, Black(0.62f * a)); + const int v0 = dl_->VtxBuffer.Size; + + // the panel, with the accent at its left + const float px = (kW - kPanelW) * 0.5f, py = PanelY(); + const float ph = GridY() - py + 5 * (kKeyH + kGap) + 96; + Rect(px, py, kPanelW, ph, IM_COL32(24, 25, 29, 255), 16); + dl_->AddRectFilled(P(px, py), P(px + 6, py + ph), st_->accent, 16 * s_, ImDrawFlags_RoundCornersLeft); + float y = py + 40; + Text(st_->title, 40, px + 64, y, IM_COL32(244, 245, 247, 255), title_); + y += 70; + if (!description_.empty()) { + Text(st_->body, 21, px + 64, y, White(0.6f), description_); + y += 40; + } + + // the text so far, with a caret and the room left + const float fx = px + 64, fw = kPanelW - 128, fh = 76; + Rect(fx, y, fw, fh, White(0.07f), 12); + dl_->AddRect(P(fx, y), P(fx + fw, y + fh), Alpha(st_->accent, 0.55f + 0.25f * std::sin(time_ * 3)), 12 * s_, 0, + 2 * s_); + const float tx = fx + 28, ty = y + 18; + const float tw = TextW(st_->bold, 34, text_); + // (the newest letter rises in) + if (!text_.empty() && value_in_ < 0.999f) { + const std::string head = text_.substr(0, text_.size() - 1), last = text_.substr(text_.size() - 1); + Text(st_->bold, 34, tx, ty, IM_COL32(244, 245, 247, 255), head); + const int l0 = dl_->VtxBuffer.Size; + Text(st_->bold, 34, tx + TextW(st_->bold, 34, head), ty + (1 - Smooth(value_in_)) * 10, + IM_COL32(244, 245, 247, 255), last); + for (int i = l0; i < dl_->VtxBuffer.Size; ++i) dl_->VtxBuffer[i].col = Alpha(dl_->VtxBuffer[i].col, Smooth(value_in_)); + } else { + Text(st_->bold, 34, tx, ty, IM_COL32(244, 245, 247, 255), text_); + } + if (!Full() && (static_cast<int>(time_ * 2) & 1) == 0) Rect(tx + tw + 3, ty + 4, 3, 36, st_->accent, 1.5f); + char room[32]; + std::snprintf(room, sizeof(room), "%zu / %zu", text_.size(), limit_); + Text(st_->body, 20, fx + fw - 24, y + 26, Full() ? Alpha(st_->accent, 1) : White(0.45f), room, 1); + + // the keys; the focus glides over them + float kx, ky, kw; + KeyBox(row_, col_, &kx, &ky, &kw); + Ease(fx_, kx, 22), Ease(fy_, ky, 22), Ease(fw_, kw, 22); + for (int r = 0; r < 5; ++r) { + const int n = r < 4 ? kCols : 4; + for (int i = 0; i < n; ++i) { + float x, yy, w; + KeyBox(r, r < 4 ? i : SpecialCol(i), &x, &yy, &w); + const bool lit = r == 4 && ((i == 0 && upper_) || i == 3); + const ImU32 key_col = lit && i == 3 ? Alpha(st_->accent, 0.85f) : White(lit ? 0.16f : 0.07f); + Rect(x, yy, w, kKeyH, key_col, 10); + if (r == 4 && i == 0 && caps_) Rect(x + 14, yy + kKeyH - 12, w - 28, 4, st_->accent, 2); // (caps lock) + } + } + // the focus: white, giving a little when pressed + const float push = press_ * 5; + Rect(fx_ + push, fy_ + push, fw_ - push * 2, kKeyH - push * 2, IM_COL32(244, 245, 247, 255), 10); + for (int r = 0; r < 5; ++r) { + const int n = r < 4 ? kCols : 4; + for (int i = 0; i < n; ++i) { + float x, yy, w; + KeyBox(r, r < 4 ? i : SpecialCol(i), &x, &yy, &w); + const float over = std::clamp(1 - std::max(std::fabs(fx_ - x) / (w * 0.7f), std::fabs(fy_ - yy) / (kKeyH * 0.7f)), 0.0f, 1.0f); + const ImU32 ink = over > 0.5f ? IM_COL32(12, 13, 16, 255) : White(0.9f); + if (r < 4) { + Text(st_->bold, 30, x + w * 0.5f, yy + 17, ink, std::string(1, kRows[upper_ ? 0 : 1][r][i]), 0.5f); + } else { + static const char* kNames[4] = {"SHIFT", "SPACE", "DELETE", "DONE"}; + Text(st_->bold, 20, x + w * 0.5f, yy + 23, ink, kNames[i], 0.5f); + } + } + } + + // what the buttons do + const float hy = GridY() + 5 * (kKeyH + kGap) + 40; + float r = px + kPanelW - 48; + r = Hint(r, hy, kPadStart, "Done"); + r = Hint(r, hy, kPadB, text_.empty() ? "Cancel" : "Back"); + r = Hint(r, hy, kPadL1, caps_ ? "Caps Lock" : "Shift"); + r = Hint(r, hy, kPadY, "Space"); + r = Hint(r, hy, kPadX, "Delete"); + Hint(r, hy, kPadA, "Type"); + + for (int i = v0; i < dl_->VtxBuffer.Size; ++i) dl_->VtxBuffer[i].col = Alpha(dl_->VtxBuffer[i].col, a); + } + + const rex::ui::PadDialogStyle* st_ = nullptr; + std::string title_, description_, text_; + size_t limit_; + int row_ = 1, col_ = 0; + bool upper_ = true, caps_ = false; + int closing_ = 0; + float in_ = 0, press_ = 0, value_in_ = 1, time_ = 0, dt_ = 0; + float fx_ = -1, fy_ = -1, fw_ = -1; + float s_ = 1, ox_ = 0, oy_ = 0; + ImDrawList* dl_ = nullptr; +}; +} // namespace +#endif // REX_PLATFORM_PS5 + class MessageBoxDialog : public XamDialog { public: MessageBoxDialog(rex::ui::ImGuiDrawer* imgui_drawer, std::string title, std::string description, @@ -240,6 +617,31 @@ class MessageBoxDialog : public XamDialog { if (first_draw) { ImGui::SetKeyboardFocusHere(); } +#if REX_PLATFORM_PS5 + // [ps5 port] d-pad / stick picks a button, Cross presses it + const uint16_t pad = pad_.Poll(); + const size_t count = buttons_.size(); + if (count) { + if (focus_ >= count) focus_ = 0; + if (pad & (kPadLeft | kPadUp)) focus_ = (focus_ + count - 1) % count; + if (pad & (kPadRight | kPadDown)) focus_ = (focus_ + 1) % count; + } + for (size_t i = 0; i < count; ++i) { + ImGui::PushID(static_cast<int>(i)); + const bool focused = i == focus_; + const bool clicked = PadButton(buttons_[i].c_str(), focused); + ImGui::PopID(); + if (clicked || (focused && (pad & kPadA))) { + chosen_button_ = static_cast<uint32_t>(i); + ImGui::CloseCurrentPopup(); + Close(); + break; + } + ImGui::SameLine(); + } + ImGui::NewLine(); + ImGui::TextDisabled("D-pad: choose Cross: OK"); +#else for (size_t i = 0; i < buttons_.size(); ++i) { if (ImGui::Button(buttons_[i].c_str())) { chosen_button_ = static_cast<uint32_t>(i); @@ -249,6 +651,7 @@ class MessageBoxDialog : public XamDialog { ImGui::SameLine(); } ImGui::Spacing(); +#endif ImGui::Spacing(); ImGui::EndPopup(); } else { @@ -261,6 +664,10 @@ class MessageBoxDialog : public XamDialog { std::string title_; std::string description_; std::vector<std::string> buttons_; +#if REX_PLATFORM_PS5 + DialogPad pad_; // [ps5 port] + size_t focus_ = 0; +#endif uint32_t default_button_ = 0; uint32_t chosen_button_ = 0; }; @@ -368,6 +775,19 @@ class KeyboardInputDialog : public XamDialog { void OnDraw(ImGuiIO& io) override { bool first_draw = false; +#if REX_PLATFORM_PS5 + // [ps5 port] in the app's style, when it gave one (rex/ui/pad_dialog_style.h) + if (const auto* style = rex::ui::GetPadDialogStyle()) { + if (!styled_) styled_ = std::make_unique<StyledPadKeyboard>(title_, description_, text_, Limit()); + const int result = styled_->Draw(*style, pad_.Poll(), io.DeltaTime); + if (result) { + cancelled_ = result < 0; + text_ = cancelled_ ? std::string() : styled_->text(); + Close(); + } + return; + } +#endif if (!has_opened_) { ImGui::OpenPopup(title_.c_str()); has_opened_ = true; @@ -377,6 +797,12 @@ class KeyboardInputDialog : public XamDialog { if (description_.size()) { ImGui::TextWrapped("%s", description_.c_str()); } +#if REX_PLATFORM_PS5 + // [ps5 port] the controller types on an on-screen keyboard + DrawPadKeyboard(); + ImGui::EndPopup(); + return; +#endif if (first_draw) { ImGui::SetKeyboardFocusHere(); } @@ -416,6 +842,125 @@ class KeyboardInputDialog : public XamDialog { std::vector<char> text_buffer_; std::string text_ = ""; bool cancelled_ = true; + +#if REX_PLATFORM_PS5 + // [ps5 port] An on-screen keyboard for the controller, in the console's + // style: d-pad / stick move, Cross types, Square / Circle delete (Circle on + // an empty name cancels), Triangle adds a space, L1 / R1 switch case, + // Options finishes. + static constexpr int kCols = 13; + static constexpr int kSpecials = 5; // CAPS, SPACE, DELETE, DONE, CANCEL + int row_ = 0, col_ = 0; + bool lower_ = false; + DialogPad pad_; + std::unique_ptr<StyledPadKeyboard> styled_; + + size_t Limit() const { return max_length_ > 2 ? max_length_ - 2 : 1; } + void Type(char c) { + if (text_.size() < Limit()) text_.push_back(c); + } + void Erase() { + if (!text_.empty()) text_.pop_back(); + } + void Finish(bool cancel) { + cancelled_ = cancel; + if (cancel) text_.clear(); + ImGui::CloseCurrentPopup(); + Close(); + } + + void DrawPadKeyboard() { + static const char* kRows[2][3] = {{"ABCDEFGHIJKLM", "NOPQRSTUVWXYZ", "0123456789-_."}, + {"abcdefghijklm", "nopqrstuvwxyz", "0123456789!?'"}}; + static const char* kSpecial[kSpecials] = {"CAPS", "SPACE", "DELETE", "DONE", "CANCEL"}; + const ImGuiStyle& style = ImGui::GetStyle(); + const float key = ImGui::GetFrameHeight() * 1.5f; + const float grid_w = kCols * key + (kCols - 1) * style.ItemSpacing.x; + const float special_w = (grid_w - (kSpecials - 1) * style.ItemSpacing.x) / kSpecials; + + // the name so far, with a caret + { + std::string shown = text_; + const bool caret = (static_cast<int>(ImGui::GetTime() * 2.0) & 1) == 0; + if (text_.size() < Limit()) shown += caret ? "_" : " "; + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg)); + ImGui::BeginChild("##name", ImVec2(grid_w, ImGui::GetFrameHeight() + 4), false); + ImGui::SetCursorPos(ImVec2(style.FramePadding.x, 2 + style.FramePadding.y)); + ImGui::TextUnformatted(shown.c_str()); + ImGui::EndChild(); + ImGui::PopStyleColor(); + ImGui::TextDisabled("%zu / %zu", text_.size(), Limit()); + } + + const uint16_t pad = pad_.Poll(); + if (pad & kPadLeft) col_ = (col_ + (row_ < 3 ? kCols : kSpecials) - 1) % (row_ < 3 ? kCols : kSpecials); + if (pad & kPadRight) col_ = (col_ + 1) % (row_ < 3 ? kCols : kSpecials); + if (pad & kPadDown) { + if (row_ == 2) col_ = std::min(kSpecials - 1, col_ * kSpecials / kCols); + else if (row_ == 3) col_ = std::min(kCols - 1, col_ * kCols / kSpecials + 1); + row_ = (row_ + 1) % 4; + } + if (pad & kPadUp) { + if (row_ == 0) col_ = std::min(kSpecials - 1, col_ * kSpecials / kCols); + else if (row_ == 3) col_ = std::min(kCols - 1, col_ * kCols / kSpecials + 1); + row_ = (row_ + 3) % 4; + } + if (pad & (kPadL1 | kPadR1)) lower_ = !lower_; + + int pressed_row = -1, pressed_col = -1; + for (int r = 0; r < 3; ++r) { + const char* keys = kRows[lower_ ? 1 : 0][r]; + for (int c = 0; c < kCols; ++c) { + char label[2] = {keys[c], 0}; + ImGui::PushID(r * 32 + c); + if (PadButton(label, r == row_ && c == col_, ImVec2(key, key))) { + pressed_row = r; + pressed_col = c; + } + ImGui::PopID(); + if (c + 1 < kCols) ImGui::SameLine(); + } + } + for (int c = 0; c < kSpecials; ++c) { + const char* label = (c == 0 && lower_) ? "caps" : kSpecial[c]; + if (PadButton(label, row_ == 3 && c == col_, ImVec2(special_w, key))) { + pressed_row = 3; + pressed_col = c; + } + if (c + 1 < kSpecials) ImGui::SameLine(); + } + ImGui::TextDisabled( + "Cross: type Square: delete Triangle: space L1: caps Options: done Circle: back"); + + if (pad & kPadA) { + pressed_row = row_; + pressed_col = col_; + } + if (pressed_row >= 0) { + if (pressed_row < 3) { + Type(kRows[lower_ ? 1 : 0][pressed_row][pressed_col]); + } else { + switch (pressed_col) { + case 0: lower_ = !lower_; break; + case 1: Type(' '); break; + case 2: Erase(); break; + case 3: Finish(false); return; + case 4: Finish(true); return; + } + } + } + if (pad & kPadX) Erase(); + if (pad & kPadY) Type(' '); + if (pad & kPadB) { + if (text_.empty()) { + Finish(true); + return; + } + Erase(); + } + if (pad & kPadStart) Finish(false); + } +#endif // REX_PLATFORM_PS5 }; // https://www.se7ensins.com/forums/threads/release-how-to-use-xshowkeyboardui-release.906568/ @@ -775,3 +1320,17 @@ REX_EXPORT_STUB(__imp__XamShowVoiceSettingsUI); REX_EXPORT_STUB(__imp__XamShowWhatsOnUI); REX_EXPORT_STUB(__imp__XamShowWordRegisterUI); REX_EXPORT_STUB(__imp__XamSysUiDisableAutoClose); + +#if REX_PLATFORM_PS5 +namespace rex::ui { +namespace { +PadDialogStyle g_pad_dialog_style; +bool g_pad_dialog_style_set = false; +} // namespace +void SetPadDialogStyle(PadDialogStyle style) { + g_pad_dialog_style = std::move(style); + g_pad_dialog_style_set = g_pad_dialog_style.title && g_pad_dialog_style.body && g_pad_dialog_style.bold; +} +const PadDialogStyle* GetPadDialogStyle() { return g_pad_dialog_style_set ? &g_pad_dialog_style : nullptr; } +} // namespace rex::ui +#endif diff --git a/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_rtl.cpp b/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_rtl.cpp index 8b862fc7..1b7584fd 100644 --- a/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_rtl.cpp +++ b/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_rtl.cpp @@ -12,6 +12,14 @@ // Disable warnings about unused parameters for kernel functions #pragma GCC diagnostic ignored "-Wunused-parameter" +#include <chrono> +#include <rex/chrono/clock.h> +#include <rex/frame_stats.h> +#include <rex/cvar.h> +#include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#endif #include <algorithm> #include <string> @@ -31,6 +39,12 @@ #include <rex/thread.h> #include <rex/thread/atomic.h> +#if REX_PLATFORM_PS5 +REXCVAR_DEFINE_INT32(ps5_cs_spin_us, 40, "PS5", + "Contended critical sections spin this long (us) for the owner to leave before " + "they wait (0 = never spin)"); +#endif + namespace rex::kernel::xboxkrnl { using namespace rex::system; @@ -339,6 +353,13 @@ struct X_RTL_CRITICAL_SECTION { static_assert_size(X_RTL_CRITICAL_SECTION, 28); void xeRtlInitializeCriticalSection(X_RTL_CRITICAL_SECTION* cs, uint32_t cs_ptr) { +#if REX_PLATFORM_PS5 + { + auto* ct = XThread::GetCurrentThread(); + rex::ps5diag::NoteCriticalSectionInit( + cs_ptr, ct && ct->thread_state() && ct->thread_state()->context() ? uint32_t(ct->thread_state()->context()->lr) : 0); + } +#endif cs->header.type = 1; // EventSynchronizationObject (auto reset) cs->header.absolute = 0; // spin count div 256 cs->header.signal_state = 0; @@ -364,6 +385,13 @@ X_STATUS xeRtlInitializeCriticalSectionAndSpinCount(X_RTL_CRITICAL_SECTION* cs, cs->header.type = 1; // EventSynchronizationObject (auto reset) cs->header.absolute = spin_count_div_256; cs->header.signal_state = 0; +#if REX_PLATFORM_PS5 + { + auto* ct = XThread::GetCurrentThread(); + rex::ps5diag::NoteCriticalSectionInit( + cs_ptr, ct && ct->thread_state() && ct->thread_state()->context() ? uint32_t(ct->thread_state()->context()->lr) : 0); + } +#endif cs->lock_count = -1; cs->recursion_count = 0; cs->owning_thread = 0; @@ -376,8 +404,21 @@ u32 RtlInitializeCriticalSectionAndSpinCount_entry(ppc_ptr_t<X_RTL_CRITICAL_SECT return xeRtlInitializeCriticalSectionAndSpinCount(cs, cs.guest_address(), spin_count); } +namespace { +void EnterCs(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs, uint32_t cur_thread); +u32 TryEnterCs(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs, uint32_t thread); +} // namespace + void RtlEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { - uint32_t cur_thread = XThread::GetCurrentThread()->guest_object(); + EnterCs(cs, XThread::GetCurrentThread()->guest_object()); +} + +u32 RtlTryEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { + return TryEnterCs(cs, XThread::GetCurrentThread()->guest_object()); +} + +namespace { +void EnterCs(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs, uint32_t cur_thread) { uint32_t spin_count = cs->header.absolute * 256; if (cs->owning_thread == cur_thread) { @@ -389,6 +430,15 @@ void RtlEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { // Spin loop while (spin_count--) { +#if REX_PLATFORM_PS5 + // [ps5 port] Look before the compare-exchange: a locked instruction every + // turn kept taking the lock's cache line from the thread holding it. (A + // pause each turn too, for the other thread on this core.) + if (*reinterpret_cast<volatile int32_t*>(&cs->lock_count) != -1) { + __builtin_ia32_pause(); + continue; + } +#endif if (rex::thread::atomic_cas(-1, 0, &cs->lock_count)) { // Acquired. cs->owning_thread = cur_thread; @@ -397,9 +447,46 @@ void RtlEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { } } +#if REX_PLATFORM_PS5 + // [ps5 port] Keep watching the lock a little longer before queueing up as + // a waiter: the holders mostly let go within microseconds, and a waiter is + // woken through a kernel event (several microseconds on the PS5). + if (const int32_t spin_us = REXCVAR_GET(ps5_cs_spin_us); spin_us > 0) { + const rex::frame_stats::TitleWaitScope title_wait; + // The TSC: a steady_clock read is a system call on the PS5, and the game + // thread spent ~5% of its time in them here. + const uint64_t end = rex::chrono::Clock::QueryHostTickCount() + + rex::chrono::Clock::QueryHostTickFrequency() * uint64_t(spin_us) / 1000000; + do { + for (int i = 0; i < 8; ++i) { + __builtin_ia32_pause(); + } + if (*reinterpret_cast<volatile int32_t*>(&cs->lock_count) == -1 && + rex::thread::atomic_cas(-1, 0, &cs->lock_count)) { + cs->owning_thread = cur_thread; + cs->recursion_count = 1; + return; + } + } while (rex::chrono::Clock::QueryHostTickCount() < end); + } +#endif + if (rex::thread::atomic_inc(&cs->lock_count) != 0) { // Create a full waiter. +#if REX_PLATFORM_PS5 + auto* ps5_slot = rex::ps5diag::CurrentWaitSlot(); + if (ps5_slot) { + ps5_slot->critical_section = cs.host_address(); + ps5_slot->critical_section_guest = cs.guest_address(); + auto* ct = XThread::GetCurrentThread(); + ps5_slot->critical_section_lr = + ct && ct->thread_state() && ct->thread_state()->context() ? uint32_t(ct->thread_state()->context()->lr) : 0; + } +#endif xeKeWaitForSingleObject(reinterpret_cast<void*>(cs.host_address()), 8, 0, 0, nullptr); +#if REX_PLATFORM_PS5 + if (ps5_slot) ps5_slot->critical_section = nullptr; +#endif } assert_true(cs->owning_thread == 0); @@ -407,9 +494,7 @@ void RtlEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { cs->recursion_count = 1; } -u32 RtlTryEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { - uint32_t thread = XThread::GetCurrentThread()->guest_object(); - +u32 TryEnterCs(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs, uint32_t thread) { if (rex::thread::atomic_cas(-1, 0, &cs->lock_count)) { // Able to steal the lock right away. cs->owning_thread = thread; @@ -425,6 +510,7 @@ u32 RtlTryEnterCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { // Failed to acquire lock. return 0; } +} // namespace void RtlLeaveCriticalSection_entry(ppc_ptr_t<X_RTL_CRITICAL_SECTION> cs) { assert_true(cs->owning_thread == XThread::GetCurrentThread()->guest_object()); @@ -618,9 +704,33 @@ REX_EXPORT(__imp__RtlInitializeCriticalSection, rex::kernel::xboxkrnl::RtlInitializeCriticalSection_entry) REX_EXPORT(__imp__RtlInitializeCriticalSectionAndSpinCount, rex::kernel::xboxkrnl::RtlInitializeCriticalSectionAndSpinCount_entry) -REX_EXPORT(__imp__RtlEnterCriticalSection, rex::kernel::xboxkrnl::RtlEnterCriticalSection_entry) -REX_EXPORT(__imp__RtlTryEnterCriticalSection, - rex::kernel::xboxkrnl::RtlTryEnterCriticalSection_entry) +// [ps5 port] The game enters critical sections thousands of times a frame +// from its main and render threads. The calling thread's object comes from its +// processor control region (r13, as on the Xbox 360: KPCR.prcb_data.current_thread, +// set when the thread was made) rather than XThread::GetCurrentThread(), a +// thread_local: a call into the emulated TLS on the PS5 each time. +namespace { +uint32_t GuestCurrentThread(PPCContext& ctx, uint8_t* base) { + using rex::system::X_KPCR; + using rex::system::X_KPRCB; + return rex::memory::load_and_swap<uint32_t>(rex::memory::GuestPtr( + base, ctx.r13.u32 + uint32_t(offsetof(X_KPCR, prcb_data) + offsetof(X_KPRCB, current_thread)))); +} +} // namespace +extern "C" REX_FUNC(__imp__RtlEnterCriticalSection) { + rex::kernel::xboxkrnl::EnterCs( + rex::ppc::ArgTranslator::GetValue<rex::MappedPtr<rex::kernel::xboxkrnl::X_RTL_CRITICAL_SECTION>>(ctx, base, 0), + GuestCurrentThread(ctx, base)); +} +static rex::ppc::detail::PPCFuncRegistrar _ppc_reg___imp__RtlEnterCriticalSection( + "__imp__RtlEnterCriticalSection", &__imp__RtlEnterCriticalSection); +extern "C" REX_FUNC(__imp__RtlTryEnterCriticalSection) { + ctx.r3.u64 = rex::kernel::xboxkrnl::TryEnterCs( + rex::ppc::ArgTranslator::GetValue<rex::MappedPtr<rex::kernel::xboxkrnl::X_RTL_CRITICAL_SECTION>>(ctx, base, 0), + GuestCurrentThread(ctx, base)); +} +static rex::ppc::detail::PPCFuncRegistrar _ppc_reg___imp__RtlTryEnterCriticalSection( + "__imp__RtlTryEnterCriticalSection", &__imp__RtlTryEnterCriticalSection); REX_EXPORT(__imp__RtlLeaveCriticalSection, rex::kernel::xboxkrnl::RtlLeaveCriticalSection_entry) REX_EXPORT(__imp__RtlTimeToTimeFields, rex::kernel::xboxkrnl::RtlTimeToTimeFields_entry) REX_EXPORT(__imp__RtlTimeFieldsToTime, rex::kernel::xboxkrnl::RtlTimeFieldsToTime_entry) diff --git a/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_video.cpp b/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_video.cpp index 1254710a..0cdc758a 100644 --- a/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_video.cpp +++ b/glue/rexglue-sdk-main/src/kernel/xboxkrnl/xboxkrnl_video.cpp @@ -549,6 +549,26 @@ void RegisterVideoExports(rex::runtime::ExportResolver* export_resolver, export_resolver->SetVariableMapping("xboxkrnl.exe", 0x01C1, pVdHSIOCalibrationLock); auto hsio_lock = memory->TranslateVirtual<X_RTL_CRITICAL_SECTION*>(pVdHSIOCalibrationLock); xeRtlInitializeCriticalSectionAndSpinCount(hsio_lock, pVdHSIOCalibrationLock, 10000); +#if REX_PLATFORM_PS5 + // [ps5 port] Watch this lock's header: the game's GPU setup thread has been + // found blocked on a lock whose memory looks wiped. Log when it changes. + REXLOG_INFO("ps5 diag: VdHSIOCalibrationLock at {:08X} (host {})", pVdHSIOCalibrationLock, + static_cast<void*>(hsio_lock)); + std::thread([p = reinterpret_cast<volatile uint8_t*>(hsio_lock), guest = pVdHSIOCalibrationLock] { + uint8_t last0 = 1, last1 = 40; // (what RtlInitializeCriticalSectionAndSpinCount(10000) wrote) + for (int changes = 0; changes < 8;) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + uint8_t b0 = p[0], b1 = p[1]; + if (b0 != last0 || b1 != last1) { + REXLOG_INFO("ps5 diag: VdHSIOCalibrationLock {:08X} header changed: type {} -> {}, spin {} -> {}; bytes 8-15 {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X}", + guest, last0, b0, last1, b1, p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); + last0 = b0; + last1 = b1; + ++changes; + } + } + }).detach(); +#endif } } // namespace rex::kernel::xboxkrnl diff --git a/glue/rexglue-sdk-main/src/system/CMakeLists.txt b/glue/rexglue-sdk-main/src/system/CMakeLists.txt index 60ae68ec..1c4ac165 100644 --- a/glue/rexglue-sdk-main/src/system/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/system/CMakeLists.txt @@ -52,7 +52,12 @@ set(REXSYSTEM_SOURCES xex_module.cpp ) -add_library(rexruntime SHARED ${REXSYSTEM_SOURCES}) +# [ps5 port] one static app on the PS5 (no shared libraries of our own). +if(PS5) + add_library(rexruntime STATIC ${REXSYSTEM_SOURCES}) +else() + add_library(rexruntime SHARED ${REXSYSTEM_SOURCES}) +endif() add_library(rex::runtime ALIAS rexruntime) add_library(rex::system ALIAS rexruntime) add_library(rex::kernel ALIAS rexruntime) diff --git a/glue/rexglue-sdk-main/src/system/gpu_plugin_loader.cpp b/glue/rexglue-sdk-main/src/system/gpu_plugin_loader.cpp index 845f6fe0..923112e7 100644 --- a/glue/rexglue-sdk-main/src/system/gpu_plugin_loader.cpp +++ b/glue/rexglue-sdk-main/src/system/gpu_plugin_loader.cpp @@ -56,7 +56,28 @@ std::vector<platform::DynamicLibrary>& LoadedPlugins() { } // namespace +#if REX_PLATFORM_PS5 +// [ps5 port] No shared libraries of our own on the PS5: the gta4-native +// renderer is linked into the program and created directly. +extern "C" IGraphicsSystem* rex_gpu_create(uint32_t abi_version, const GpuCreateInfo* info); +#endif + std::unique_ptr<IGraphicsSystem> LoadGpuPlugin(std::string_view name, std::string_view backend) { +#if REX_PLATFORM_PS5 + { + std::string backend_str(backend); + GpuCreateInfo info{}; + info.struct_size = sizeof(GpuCreateInfo); + info.backend = backend_str.c_str(); + IGraphicsSystem* graphics_system = rex_gpu_create(kGpuPluginAbiVersion, &info); + if (!graphics_system) { + REXSYS_ERROR("GPU renderer '{}' (built in) returned no graphics system", name); + return nullptr; + } + REXSYS_INFO("GPU renderer '{}' (built in)", name); + return std::unique_ptr<IGraphicsSystem>(graphics_system); + } +#endif auto path = rex::filesystem::GetExecutableFolder() / PluginFileName(name); if (!std::filesystem::exists(path)) { REXSYS_ERROR( diff --git a/glue/rexglue-sdk-main/src/system/mmio_handler.cpp b/glue/rexglue-sdk-main/src/system/mmio_handler.cpp index 2b930058..f94a72ab 100644 --- a/glue/rexglue-sdk-main/src/system/mmio_handler.cpp +++ b/glue/rexglue-sdk-main/src/system/mmio_handler.cpp @@ -10,9 +10,11 @@ */ #include <algorithm> +#include <atomic> #include <cstring> #include <utility> +#include <rex/ps5_diag.h> #include <rex/assert.h> #include <rex/exception_handler.h> #include <rex/logging.h> @@ -372,6 +374,7 @@ bool MMIOHandler::ExceptionCallback(arch::Exception* ex) { if (ex->code() != arch::Exception::Code::kAccessViolation) { return false; } + rex::ps5diag::Add(rex::ps5diag::kAccessViolations); arch::Exception::AccessViolationOperation operation = ex->access_violation_operation(); if (operation != arch::Exception::AccessViolationOperation::kRead && operation != arch::Exception::AccessViolationOperation::kWrite) { @@ -417,12 +420,32 @@ bool MMIOHandler::ExceptionCallback(arch::Exception* ex) { // The address is not found within any range, so either a write watch or an // actual access violation. if (access_violation_callback_) { - return access_violation_callback_(std::move(lock), access_violation_callback_context_, - fault_host_address, is_write); + const bool handled = access_violation_callback_(std::move(lock), access_violation_callback_context_, + fault_host_address, is_write); + rex::ps5diag::Add(handled ? rex::ps5diag::kWatchFaults : rex::ps5diag::kUnhandledFaults); +#if REX_ARCH_ARM64 && !defined(_WIN32) + // [svr2011] where the guest code was (the first few unhandled faults) + static std::atomic<int> reported{0}; + if (!handled && reported.fetch_add(1) < 3) { + auto where = [](uint64_t a) { + Dl_info info{}; + if (a && dladdr(reinterpret_cast<void*>(a), &info) && info.dli_fname) + return fmt::format("{}+0x{:X}", std::strrchr(info.dli_fname, '/') ? std::strrchr(info.dli_fname, '/') + 1 + : info.dli_fname, + a - reinterpret_cast<uint64_t>(info.dli_fbase)); + return fmt::format("0x{:X}", a); + }; + const arch::HostThreadContext& c = *ex->thread_context(); + REXLOG_ERROR("Unhandled guest access at host pc {} (lr {}), x0 {:X} x1 {:X} x2 {:X}", where(ex->pc()), + where(c.x[30]), c.x[0], c.x[1], c.x[2]); + } +#endif + return handled; } return false; } + rex::ps5diag::Add(rex::ps5diag::kMmioAccesses); auto rip = ex->pc(); auto p = reinterpret_cast<const uint8_t*>(rip); DecodedLoadStore decoded_load_store; diff --git a/glue/rexglue-sdk-main/src/system/xmemory.cpp b/glue/rexglue-sdk-main/src/system/xmemory.cpp index 332e91e1..c941a2d1 100644 --- a/glue/rexglue-sdk-main/src/system/xmemory.cpp +++ b/glue/rexglue-sdk-main/src/system/xmemory.cpp @@ -9,14 +9,19 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/ps5_diag.h> +#include <rex/system/xthread.h> +#include <rex/system/thread_state.h> +#endif #include <algorithm> #include <cstring> #include <utility> -#if REX_PLATFORM_MAC +#if defined(__APPLE__) || defined(__PROSPERO__) // [ps5 port] #include <sys/mman.h> #endif - #include <fmt/format.h> #include <rex/assert.h> @@ -143,9 +148,15 @@ bool Memory::Initialize() { mapping_ = rex::memory::CreateFileMappingHandle(file_name_, mapping_size, rex::memory::PageAccess::kReadWrite, false); if (mapping_ == rex::memory::kFileMappingHandleInvalid) { +#if REX_PLATFORM_PS5 + // [ps5 port] Expected in a PS5 app: shared memory comes from the small + // flexible pool. MapViewsPs5 maps the guest in direct memory instead. + REXSYS_INFO("PS5: no shared memory object for the guest; using direct memory"); +#else REXSYS_ERROR("Unable to reserve the 4gb guest address space."); assert_always(); return false; +#endif } #if REX_PLATFORM_MAC @@ -161,6 +172,12 @@ bool Memory::Initialize() { // Attempt to create our views. This may fail at the first address // we pick, so try a few times. mapping_base_ = 0; +#if REX_PLATFORM_PS5 + // [ps5 port] Reserve the whole range, then fixed views into it (tested on a PS5). + if (!MapViewsPs5()) { + mapping_base_ = views_.all_views[0]; + } +#else for (size_t n = 32; n < 64; n++) { auto mapping_base = reinterpret_cast<uint8_t*>(1ull << n); if (!MapViews(mapping_base)) { @@ -168,6 +185,7 @@ bool Memory::Initialize() { break; } } +#endif // REX_PLATFORM_PS5 if (!mapping_base_) { REXSYS_ERROR("Unable to find a continuous block in the 64bit address space."); assert_always(); @@ -308,6 +326,157 @@ static const struct { 0x0000000100000000ull, }, }; + +namespace { + +constexpr size_t kGuestMappingLength = 0x120000000ull; + +} // namespace + +#if REX_PLATFORM_PS5 +// [ps5 port] Guest memory in PS5 direct memory through libps5platform's +// shared memory objects (as UnleashedRecomp-Prospero maps its guest): an +// app's ordinary shared/anonymous memory comes from a small flexible pool, +// too little for the Xbox 360's RAM. +#include <ps5platform/shm.h> +#endif + +#if REX_PLATFORM_PS5 +// [ps5 port] Reserve the guest range, then map each view into it with +// MAP_FIXED, in direct memory when there is room (as in the SvR 2011 PS5 port). +int Memory::MapViewsPs5() { + assert_true(rex::countof(map_info) == rex::countof(views_.all_views)); + + // Reserve a contiguous range first, then MAP_FIXED each view within it so + // the guest layout is stable across runs. + const size_t total_size = + static_cast<size_t>(map_info[rex::countof(map_info) - 1].virtual_address_end - + map_info[0].virtual_address_start + 1); + + // [ps5 port] Address space only (near 64 GiB, like Sonic's port). + void* reserved_base = nullptr; + if (ps5_vrange_reserve(total_size, reinterpret_cast<void*>(0x1000000000ull), 0x10000, + &reserved_base) != 0 || + !reserved_base) { + reserved_base = mmap(nullptr, total_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + } + if (reserved_base == MAP_FAILED) { + REXSYS_ERROR("MapViewsPs5: reserve failed: {}", std::strerror(errno)); + return 1; + } + rex::ps5diag::guest_arena_lo = reinterpret_cast<uintptr_t>(reserved_base); + rex::ps5diag::guest_arena_hi = reinterpret_cast<uintptr_t>(reserved_base) + total_size; + + uint8_t* mapping_base = reinterpret_cast<uint8_t*>(reserved_base); + uint64_t granularity_mask = ~uint64_t(system_allocation_granularity_ - 1); + + { + // One object for the whole 4.5 GB backing (offsets as map_info's) if + // direct memory has room for it, else one object per mirrored region: + // guest virtual (views 0-1), the XEX (3-4) and physical memory (2, 5-8). + struct Region { + uint64_t start, size; + ps5_shm shm; + bool ok; + }; + Region regions[3] = {{0x000000000ull, 0x07F000000ull, {}, false}, + {0x080000000ull, 0x010000000ull, {}, false}, + {0x100000000ull, 0x020000000ull + 0x10000, {}, false}}; + ps5_shm whole{}; + bool use_whole = ps5_shm_create(0x120000000ull + 0x10000, &whole) == 0; + bool regions_ok = use_whole; + if (!use_whole) { + regions_ok = true; + for (Region& r : regions) { + r.ok = ps5_shm_create(r.size, &r.shm) == 0; + regions_ok &= r.ok; + } + } + bool mapped = regions_ok; + for (size_t n = 0; mapped && n < rex::countof(map_info); n++) { + size_t view_size = static_cast<size_t>(map_info[n].virtual_address_end - + map_info[n].virtual_address_start + 1); + uint64_t target = map_info[n].target_address & granularity_mask; + const ps5_shm* shm = &whole; + uint64_t offset = target; + if (!use_whole) { + shm = nullptr; + for (Region& r : regions) { + if (target >= r.start && target + view_size <= r.start + r.size) { + shm = &r.shm; + offset = target - r.start; + } + } + } + void* view = nullptr; + void* wanted = mapping_base + map_info[n].virtual_address_start; + int rc = shm ? ps5_shm_map(shm, offset, view_size, wanted, PS5_SHM_READ | PS5_SHM_WRITE, + PS5_SHM_FIXED, &view) + : -1; + if (rc != 0 || view != wanted) { + REXSYS_ERROR("PS5: guest view {} (guest {:#x}) failed ({:#x})", n, + map_info[n].virtual_address_start, uint32_t(rc)); + mapped = false; + break; + } + views_.all_views[n] = reinterpret_cast<uint8_t*>(view); + } + if (mapped) { + REXSYS_INFO("PS5: guest memory in direct memory ({})", + use_whole ? "one object" : "three objects"); + // Self-test: the mirrors must show one memory. Write through one view, + // read through its mirror (GPU write-backs go through the raw physical + // view, the game reads them through 0xA0000000 / 0xC0000000 / 0xE0000000). + struct Pair { + int a, b; + const char* name; + } pairs[] = {{8, 5, "phys raw->A0"}, {8, 6, "phys raw->C0"}, {5, 6, "A0->C0"}, + {3, 4, "xex 80->90"}, {2, 8, "7F->phys raw"}, + {8, 7, "phys raw->E0"}}; + for (const Pair& p : pairs) { + volatile uint32_t* wa = reinterpret_cast<volatile uint32_t*>(views_.all_views[p.a] + 0x40); + volatile uint32_t* rb = reinterpret_cast<volatile uint32_t*>(views_.all_views[p.b] + 0x40); + uint32_t saved = *wa; + *wa = 0x5A17C0DE; + uint32_t seen = *rb; + *wa = saved; + REXSYS_INFO("PS5: mirror {}: {}", p.name, seen == 0x5A17C0DE ? "ok" : "BROKEN"); + } + return 0; + } + REXSYS_ERROR("PS5: no direct memory for the guest ({})", + regions_ok ? "mapping failed" : "allocation failed"); + if (mapping_ == rex::memory::kFileMappingHandleInvalid) { + return 1; + } + } + + for (size_t n = 0; n < rex::countof(map_info); n++) { + size_t view_size = static_cast<size_t>(map_info[n].virtual_address_end - + map_info[n].virtual_address_start + 1); + size_t file_offset = static_cast<size_t>(map_info[n].target_address & granularity_mask); + void* target_address = mapping_base + map_info[n].virtual_address_start; + void* result = mmap(target_address, view_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, + mapping_, file_offset); + if (result == MAP_FAILED || result != target_address) { + int err = errno; + REXSYS_ERROR( + "MapViewsPs5: map failed view {} addr 0x{:016X} size 0x{:X} offset 0x{:X} err {} ({})", n, + reinterpret_cast<uintptr_t>(target_address), view_size, file_offset, err, + std::strerror(err)); + munmap(reserved_base, total_size); + for (auto& view : views_.all_views) { + view = nullptr; + } + return 1; + } + views_.all_views[n] = reinterpret_cast<uint8_t*>(result); + } + + return 0; +} +#endif // REX_PLATFORM_PS5 + #if REX_PLATFORM_MAC int Memory::MapViewsMac() { assert_true(rex::countof(map_info) == rex::countof(views_.all_views)); @@ -513,7 +682,7 @@ runtime::MMIORange* Memory::LookupVirtualMappedRange(uint32_t virtual_address) { return mmio_handler_->LookupRange(virtual_address); } -bool Memory::AccessViolationCallback(std::unique_lock<std::recursive_mutex> global_lock_locked_once, +bool Memory::AccessViolationCallback(rex::thread::global_lock_t global_lock_locked_once, void* host_address, bool is_write) { // Access via physical_membase_ is special, when need to bypass everything // (for instance, for a data provider to actually write the data) so only @@ -616,14 +785,14 @@ bool Memory::AccessViolationCallback(std::unique_lock<std::recursive_mutex> glob } bool Memory::AccessViolationCallbackThunk( - std::unique_lock<std::recursive_mutex> global_lock_locked_once, void* context, + rex::thread::global_lock_t global_lock_locked_once, void* context, void* host_address, bool is_write) { return reinterpret_cast<memory::Memory*>(context)->AccessViolationCallback( std::move(global_lock_locked_once), host_address, is_write); } bool Memory::TriggerPhysicalMemoryCallbacks( - std::unique_lock<std::recursive_mutex> global_lock_locked_once, uint32_t virtual_address, + rex::thread::global_lock_t global_lock_locked_once, uint32_t virtual_address, uint32_t length, bool is_write, bool unwatch_exact_range, bool unprotect) { BaseHeap* heap = LookupHeap(virtual_address); if (!heap) { @@ -1324,6 +1493,29 @@ bool BaseHeap::AllocRange(uint32_t low_address, uint32_t high_address, uint32_t if (start_page_number == UINT_MAX || end_page_number == UINT_MAX) { // Out of memory. REXSYS_ERROR("BaseHeap::Alloc failed to find contiguous range"); +#if REX_PLATFORM_PS5 + { + static std::atomic<int> ps5_fail_logs{0}; + int k = ps5_fail_logs.fetch_add(1); + if (k < 12 || k % 100 == 0) { + uint32_t free_pages = 0, run = 0, best = 0, used = 0; + for (uint32_t i = low_page_number; i <= high_page_number; ++i) { + if (page_table_[i].state == 0) { + ++free_pages; + best = std::max(best, ++run); + } else { + ++used; + run = 0; + } + } + REXSYS_ERROR( + "ps5 heap {:08X}: wanted {:X} bytes ({} pages of {:X}, align {:X}, {}) in {:08X}-{:08X}: " + "{} pages free, {} used, largest free run {} pages", + heap_base_, size, page_count, page_size_, alignment, top_down ? "top down" : "bottom up", + low_address, high_address, free_pages, used, best); + } + } +#endif assert_always("Heap exhausted!"); return false; } @@ -1362,6 +1554,41 @@ bool BaseHeap::AllocRange(uint32_t low_address, uint32_t high_address, uint32_t } *out_address = heap_base_ + (start_page_number << page_size_shift_); +#if REX_PLATFORM_PS5 + if (heap_type_ == memory::HeapType::kGuestPhysical && heap_base_ == 0) { + // [ps5 port] Who takes the 512 MB of physical memory (the game later + // runs out): every physical allocation of 256 KB or more, and a total. + static std::atomic<uint64_t> ps5_small_bytes{0}, ps5_small_count{0}; + static std::atomic<int> ps5_big_logs{0}; + uint32_t bytes = page_count << page_size_shift_; + if (bytes >= 0x40000) { + if (ps5_big_logs.fetch_add(1) < 40) { + uint32_t lr = 0, lr1 = 0, lr2 = 0; + auto* t = rex::system::XThread::GetCurrentThread(); + if (t && t->thread_state() && t->thread_state()->context()) { + auto* ctx = t->thread_state()->context(); + lr = uint32_t(ctx->lr); + // Callers further up, from the guest stack's back chain (each frame + // stores the caller's r1 at 0(r1) and its return address at -8). + auto be32 = [this](uint32_t a) -> uint32_t { + if (a < 0x10000 || a >= 0xE0000000) return 0; + return rex::byte_swap(*memory_->TranslateVirtual<uint32_t*>(a)); + }; + uint32_t back1 = be32(ctx->r1.u32); + lr1 = back1 ? be32(back1 - 8) : 0; + uint32_t back2 = back1 ? be32(back1) : 0; + lr2 = back2 ? be32(back2 - 8) : 0; + } + REXSYS_INFO("ps5 phys: {:X} bytes at {:08X} ({}) from game code {:08X} < {:08X} < {:08X} thread {:X}; small so far {} KB in {}", + bytes, *out_address, top_down ? "top down" : "bottom up", lr, lr1, lr2, t ? t->handle() : 0, + ps5_small_bytes.load() / 1024, ps5_small_count.load()); + } + } else { + ps5_small_bytes += bytes; + ++ps5_small_count; + } + } +#endif return true; } @@ -1415,6 +1642,18 @@ bool BaseHeap::Release(uint32_t base_address, uint32_t* out_region_size) { if (out_region_size) { *out_region_size = (base_page_entry.region_page_count << page_size_shift_); } +#if REX_PLATFORM_PS5 + if (heap_type_ == memory::HeapType::kGuestPhysical && heap_base_ == 0) { + static std::atomic<uint64_t> ps5_freed_bytes{0}, ps5_freed_count{0}; + uint64_t bytes = uint64_t(base_page_entry.region_page_count) << page_size_shift_; + ps5_freed_bytes += bytes; + uint64_t n = ++ps5_freed_count; + if (n <= 10 || n % 50 == 0) { + REXSYS_INFO("ps5 phys: freed {:X} bytes at {:08X}; {} frees, {} KB in all", bytes, base_address, n, + ps5_freed_bytes.load() / 1024); + } + } +#endif // Release from host not needed as mapping reserves the range for us. // TODO(benvanik): protect with NOACCESS? @@ -1689,6 +1928,31 @@ rex::memory::PageAccess BaseHeap::QueryRangeAccess(uint32_t low_address, uint32_ } } +rex::memory::PageAccess BaseHeap::QueryRangeAccessUnlocked(uint32_t low_address, + uint32_t high_address) const { + if (low_address > high_address || low_address < heap_base_ || + (high_address - heap_base_) >= heap_size_) { + return rex::memory::PageAccess::kNoAccess; + } + const uint32_t low_page_number = (low_address - heap_base_) >> page_size_shift_; + const uint32_t high_page_number = (high_address - heap_base_) >> page_size_shift_; + bool all_readable = true; + bool all_writable = true; + for (uint32_t i = low_page_number; i <= high_page_number; ++i) { + PageEntry entry; + entry.qword = __atomic_load_n(&page_table_[i].qword, __ATOMIC_RELAXED); + const uint32_t page_protect = entry.current_protect; + if (!(page_protect & memory::kMemoryProtectRead)) all_readable = false; + if (!(page_protect & memory::kMemoryProtectWrite) && + !(page_protect & memory::kMemoryProtectWriteCombine)) { + all_writable = false; + } + } + if (all_readable && all_writable) return rex::memory::PageAccess::kReadWrite; + if (all_readable) return rex::memory::PageAccess::kReadOnly; + return rex::memory::PageAccess::kNoAccess; +} + VirtualHeap::VirtualHeap() = default; VirtualHeap::~VirtualHeap() = default; @@ -1995,7 +2259,7 @@ void PhysicalHeap::EnableAccessCallbacks(uint32_t physical_address, uint32_t len } } -bool PhysicalHeap::TriggerCallbacks(std::unique_lock<std::recursive_mutex> global_lock_locked_once, +bool PhysicalHeap::TriggerCallbacks(rex::thread::global_lock_t global_lock_locked_once, uint32_t virtual_address, uint32_t length, bool is_write, bool unwatch_exact_range, bool unprotect) { // TODO(Triang3l): Support read watches. diff --git a/glue/rexglue-sdk-main/src/system/xobject.cpp b/glue/rexglue-sdk-main/src/system/xobject.cpp index d358f399..012fab8d 100644 --- a/glue/rexglue-sdk-main/src/system/xobject.cpp +++ b/glue/rexglue-sdk-main/src/system/xobject.cpp @@ -26,6 +26,77 @@ #include <rex/system/xsemaphore.h> #include <rex/system/xsymboliclink.h> #include <rex/system/xthread.h> +#if REX_PLATFORM_PS5 +#include <pthread.h> +#include <atomic> +#include <string> +#include <rex/frame_stats.h> +#include <rex/logging.h> +#include <rex/ps5_diag.h> +#include <rex/system/thread_state.h> +#endif + +namespace { +#if REX_PLATFORM_PS5 +// [ps5 port] records this thread's kernel wait for the 10 s monitor +// (rex/ps5_diag.h); clears it when the wait ends. +struct Ps5WaitScope { + rex::ps5diag::WaitSlot* slot; + Ps5WaitScope(rex::system::XObject* first, uint32_t count, uint64_t* opt_timeout) { + slot = rex::ps5diag::CurrentWaitSlot(); + if (!slot) return; + auto* thread = rex::system::XThread::GetCurrentThread(); + slot->thread_id = thread ? thread->handle() : 0; // (as the "thread ... (F80000xx)" log lines) + slot->guest_lr = 0; + slot->guest_sp = slot->guest_stack_lo = slot->guest_stack_hi = 0; + if (thread && thread->thread_state() && thread->thread_state()->context()) { + auto* ctx = thread->thread_state()->context(); + slot->guest_lr = uint32_t(ctx->lr); + slot->guest_sp = uint32_t(ctx->r1.u32); + slot->guest_stack_lo = thread->guest_stack_limit(); + slot->guest_stack_hi = thread->guest_stack_base(); + } + slot->object_type = first ? uint32_t(first->type()) : 0; + slot->handle = first ? first->handle() : 0; + slot->count = count; + slot->timeout_ms = opt_timeout ? 0 : 0xFFFFFFFFu; + slot->since_ms.store(rex::ps5diag::NowMs(), std::memory_order_release); + } + ~Ps5WaitScope() { + if (!slot) return; + const uint64_t since = slot->since_ms.load(std::memory_order_relaxed); + slot->since_ms.store(0, std::memory_order_release); + // A long wait on the game's main thread (several frames' worth): what it + // waited on and from where, to tell the hitches apart. A line at most + // every 5 s, 100 in all (loading screens wait this long all the time). + const uint64_t now = rex::ps5diag::NowMs(); + if (!since || now - since < 60 || + reinterpret_cast<uintptr_t>(pthread_self()) != + rex::frame_stats::g_game_main_thread.load(std::memory_order_relaxed)) { + return; + } + static std::atomic<uint64_t> last_ms{0}; + static std::atomic<uint32_t> lines{0}; + uint64_t last = last_ms.load(std::memory_order_relaxed); + if ((last && now - last < 5000) || !last_ms.compare_exchange_strong(last, now) || + lines.fetch_add(1, std::memory_order_relaxed) >= 100) { + return; + } + REXLOG_INFO("ps5 hitch: the game's main thread waited {} ms on {} {} handle {:08X} from game " + "code {:08X}{}", + now - since, slot->count > 1 ? slot->count : 1, + rex::ps5diag::ObjectTypeName(slot->object_type), slot->handle, slot->guest_lr, + slot->critical_section + ? fmt::format(" (a critical section entered from {:08X})", slot->critical_section_lr) + : std::string()); + } +}; +#else +struct Ps5WaitScope { + Ps5WaitScope(rex::system::XObject*, uint32_t, uint64_t*) {} +}; +#endif +} // namespace namespace rex::system { @@ -213,6 +284,7 @@ X_STATUS XObject::Wait(uint32_t wait_reason, uint32_t processor_mode, uint32_t a : std::chrono::milliseconds::max(); XThread::CheckTitleTermination(); + Ps5WaitScope ps5_wait(this, 1, opt_timeout); auto result = rex::thread::Wait(wait_handle, alertable ? true : false, timeout_ms); XThread::CheckTitleTermination(); switch (result) { @@ -239,6 +311,7 @@ X_STATUS XObject::SignalAndWait(XObject* signal_object, XObject* wait_object, ui TimeoutTicksToMs(*opt_timeout))) : std::chrono::milliseconds::max(); + Ps5WaitScope ps5_wait(wait_object, 1, opt_timeout); auto result = rex::thread::SignalAndWait(signal_object->GetWaitHandle(), wait_object->GetWaitHandle(), alertable ? true : false, timeout_ms); @@ -273,6 +346,7 @@ X_STATUS XObject::WaitMultiple(uint32_t count, XObject** objects, uint32_t wait_ : std::chrono::milliseconds::max(); XThread::CheckTitleTermination(); + Ps5WaitScope ps5_wait(count ? objects[0] : nullptr, count, opt_timeout); if (wait_type) { auto result = rex::thread::WaitAny(std::move(wait_handles), alertable ? true : false, timeout_ms); diff --git a/glue/rexglue-sdk-main/src/system/xthread.cpp b/glue/rexglue-sdk-main/src/system/xthread.cpp index 91913215..9a631f8b 100644 --- a/glue/rexglue-sdk-main/src/system/xthread.cpp +++ b/glue/rexglue-sdk-main/src/system/xthread.cpp @@ -42,6 +42,10 @@ #include <sys/resource.h> #include <unistd.h> #endif +#if REX_PLATFORM_PS5 +#include <pthread.h> +#include <rex/frame_stats.h> +#endif REXCVAR_DEFINE_BOOL(ignore_thread_priorities, true, "Kernel", "Ignores game-specified thread priorities"); @@ -580,6 +584,13 @@ void XThread::Execute() { // Let the kernel know we are starting. kernel_state_->OnThreadExecute(this); +#if REX_PLATFORM_PS5 + // [ps5 port] The game's main thread, for the timing line and its own core. + if (main_thread_) { + rex::frame_stats::g_game_main_thread.store(reinterpret_cast<uintptr_t>(pthread_self()), + std::memory_order_relaxed); + } +#endif // Dispatch any APCs that were queued before the thread was created first. DeliverAPCs(); diff --git a/glue/rexglue-sdk-main/src/ui/CMakeLists.txt b/glue/rexglue-sdk-main/src/ui/CMakeLists.txt index 377172d5..6a0c90e6 100644 --- a/glue/rexglue-sdk-main/src/ui/CMakeLists.txt +++ b/glue/rexglue-sdk-main/src/ui/CMakeLists.txt @@ -78,6 +78,8 @@ elseif(APPLE) endif() elseif(ANDROID) set(REXUI_PLATFORM_SOURCES surface_android.cpp) +elseif(PS5) # [ps5 port] the TV through VK_KHR_display; SDL (dummy video) runs the app loop + set(REXUI_PLATFORM_SOURCES surface_ps5.cpp) elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") set(REXUI_PLATFORM_SOURCES surface_gnulinux.cpp @@ -166,7 +168,9 @@ if(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS") endif() # Platform-specific dependencies -if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT ANDROID) +if(PS5) + # [ps5 port] no X11 / Wayland / GTK; RADV is linked statically into the app. +elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT ANDROID) find_package(PkgConfig REQUIRED) pkg_check_modules(X11_XCB REQUIRED x11-xcb) diff --git a/glue/rexglue-sdk-main/src/ui/presenter.cpp b/glue/rexglue-sdk-main/src/ui/presenter.cpp index c81f558b..f9c45d0f 100644 --- a/glue/rexglue-sdk-main/src/ui/presenter.cpp +++ b/glue/rexglue-sdk-main/src/ui/presenter.cpp @@ -20,6 +20,9 @@ #include <rex/diagnostics/policy.h> #include <rex/logging.h> #include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/frame_stats.h> +#endif #include <rex/ui/presenter.h> #include <rex/ui/window.h> @@ -289,7 +292,29 @@ void Presenter::FatalErrorHostGpuLossCallback([[maybe_unused]] bool is_responsib rex::FatalError("Graphics device lost (probably due to an internal error)"); } +#if REX_PLATFORM_PS5 +namespace { +std::atomic<Presenter*> g_keepalive_presenter{nullptr}; +void KeepAlivePaint() { + if (Presenter* presenter = g_keepalive_presenter.load(std::memory_order_acquire)) + presenter->RequestKeepAlivePaint(); +} +} // namespace + +void Presenter::RequestKeepAlivePaint() { + std::lock_guard<std::mutex> lock(paint_mode_mutex_); + if (paint_mode_ != PaintMode::kUIThreadOnRequest || !window_ || !surface_) return; + keepalive_requested_.store(true, std::memory_order_release); + RequestPaintOrConnectionRecoveryViaWindow(true); +} +#endif + Presenter::~Presenter() { +#if REX_PLATFORM_PS5 + Presenter* self = this; + if (g_keepalive_presenter.compare_exchange_strong(self, nullptr)) + rex::frame_stats::g_request_keepalive_paint.store(nullptr, std::memory_order_release); +#endif CancelFramePacingWaits(); // No intrusive lifetime management must be performed from UI drawers - defer // it if needed. @@ -534,6 +559,31 @@ void Presenter::PaintFromUIThreadImpl(bool force_paint, bool display_opportunity // doesn't limit the frame rate. WaitForUITickFromUIThread(); +#if REX_PLATFORM_PS5 + // [ps5 port] While the game is publishing frames, the UI thread paints + // only to show a new one. A repaint asked for by an overlay (the trophy + // popup asked at every paint until 1.1) sent the frame already on the + // TV again: ~50 repeats a second at 60 FPS, each a flip that a new frame + // then waited behind, landing it a vblank early or late. The overlays + // are drawn with the next game frame instead (ui_paint_pending_ stays + // set); if none comes within 100 ms (the game stopped), this paint goes + // ahead then, so the UI never freezes. + if (g_keepalive_presenter.load(std::memory_order_relaxed) != this) { + g_keepalive_presenter.store(this, std::memory_order_release); + rex::frame_stats::g_request_keepalive_paint.store(&KeepAlivePaint, std::memory_order_release); + } + const bool keepalive = keepalive_requested_.exchange(false, std::memory_order_acq_rel); + const uint64_t last_frame_ns = last_active_publication_ns_.load(std::memory_order_relaxed); + const uint64_t paint_now_ns = FramePacerNowNs(); + if (keepalive && !publication_progress_.pending()) + rex::frame_stats::g_keepalive_paints.fetch_add(1, std::memory_order_relaxed); + if (!force_paint && !keepalive && !publication_progress_.pending() && last_frame_ns && + paint_now_ns >= last_frame_ns && paint_now_ns - last_frame_ns < 100'000'000ull && + !rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) { + rex::frame_stats::g_repeat_paints_skipped.fetch_add(1, std::memory_order_relaxed); + window_->RequestPaintAfterNanoseconds(100'000'000ull); + } else +#endif paint_result = PaintAndPresent(draw_ui); if (paint_result == PaintResult::kPresented || paint_result == PaintResult::kPresentedSuboptimal) ui_paint_pending_ = false; @@ -727,6 +777,9 @@ bool Presenter::RefreshGuestOutput( probe.guest_image, probe.guest_version, published_mailbox_index, probe.native_submission); } publication_progress_.Publish(publication_serial); +#if REX_PLATFORM_PS5 + if (is_active) last_active_publication_ns_.store(FramePacerNowNs(), std::memory_order_relaxed); +#endif // Trigger the presentation on the host. PaintResult paint_result = PaintResult::kNotPresented; PaintMode paint_mode_snapshot = PaintMode::kNone; @@ -875,6 +928,13 @@ bool Presenter::RefreshGuestOutput( paint_result != PaintResult::kGpuLostExternally) { const uint64_t begin = FramePacerNowNs(); const bool accepted = frame_publication_gate_.Wait(publication_serial); +#if REX_PLATFORM_PS5 + { + const uint64_t waited_us = (FramePacerNowNs() - begin) / 1000; + rex::frame_stats::g_pacer_wait_us.fetch_add(waited_us, std::memory_order_relaxed); + rex::frame_stats::g_frame_pacer_us.fetch_add(waited_us, std::memory_order_relaxed); + } +#endif if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kPresenter)) { REXLOG_INFO("FramePacer point=handoff serial={} frame={} released={} wait-ns={}", publication_serial, provenance.submitted_frame, accepted, FramePacerNowNs()-begin); @@ -1820,6 +1880,9 @@ Presenter::PaintResult Presenter::PaintAndPresent(bool execute_ui_drawers) { if (result == PaintResult::kPresented || result == PaintResult::kPresentedSuboptimal) { const uint64_t queue_end_ns = FramePacerNowNs(); frame_pacer_.Queued(queue_end_ns); +#if REX_PLATFORM_PS5 + rex::frame_stats::g_display_presents.fetch_add(1, std::memory_order_relaxed); +#endif ++host_rate_trace_count_; if (rex::diagnostics::IsEnabled(rex::diagnostics::Category::kPresenter)) { REXLOG_INFO("FramePacer point=queued count={} fps={} slot-ns={} paint-begin-ns={} " diff --git a/glue/rexglue-sdk-main/src/ui/rex_app.cpp b/glue/rexglue-sdk-main/src/ui/rex_app.cpp index 0f7c5043..eb30130c 100644 --- a/glue/rexglue-sdk-main/src/ui/rex_app.cpp +++ b/glue/rexglue-sdk-main/src/ui/rex_app.cpp @@ -53,6 +53,11 @@ #include <filesystem> #include <string_view> +#if REX_PLATFORM_PS5 +REXCVAR_DEFINE_BOOL(ps5_save_logs, true, "PS5", "Write the log to /app0/logs"); +REXCVAR_DEFINE_BOOL(ps5_perf_capture, false, "PS5", + "Profile the renderer for a few minutes (read at startup, see windowed_app_main_sdl.cpp)"); +#endif REXCVAR_DEFINE_STRING(gpu_plugin, "", "GPU", "Graphics plugin to load at startup (for example, 'xenos'); empty lets " "the title choose; changes take effect after restart") @@ -189,6 +194,12 @@ bool ReXApp::SetupEnvironment() { auto category_levels = rex::ParseCategoryLevelsFromConfig(config_path_); auto log_config = rex::BuildLogConfig(log_file_cvar.empty() ? nullptr : log_file_cvar.c_str(), log_level_str, category_levels); +#if REX_PLATFORM_PS5 + // [ps5 port] Save Logs (the hub's Debug settings): off, no log file at all. + if (!REXCVAR_GET(ps5_save_logs)) { + log_file_cvar.clear(); + } else +#endif if (log_file_cvar.empty()) { log_config.app_name = std::string(GetName()); #if REX_PLATFORM_MAC || defined(__ANDROID__) @@ -203,7 +214,20 @@ bool ReXApp::SetupEnvironment() { #endif } +#if REX_PLATFORM_PS5 + // [ps5 port] The log file is written every 2 s, not after every line: each + // line was a write to the drive by whichever thread logged it, sometimes one + // holding a lock the game's render thread then waited on (a beta tester's + // game stalled for 0.8-0.9 s at a time, again and again). Errors too: a + // tester's cutscenes ran at 8 FPS with the renderer writing out an error + // line per draw (0.5b6). A crash writes its own report and then the rest + // (ps5_diag.cpp, the crash hook). + log_config.flush_level = spdlog::level::critical; +#endif rex::InitLogging(log_config); +#if REX_PLATFORM_PS5 + if (REXCVAR_GET(log_flush_interval) <= 0) spdlog::flush_every(std::chrono::seconds(2)); +#endif rex::RegisterLogLevelCallback(); log_sink_ = std::make_shared<rex::LogCaptureSink>(); diff --git a/glue/rexglue-sdk-main/src/ui/surface_ps5.cpp b/glue/rexglue-sdk-main/src/ui/surface_ps5.cpp new file mode 100644 index 00000000..939a240d --- /dev/null +++ b/glue/rexglue-sdk-main/src/ui/surface_ps5.cpp @@ -0,0 +1,5 @@ +/** + * [ps5 port] PS5 display surface: all state lives in the header (the display + * has no window to query); this file keeps the per-platform source layout. + */ +#include <rex/ui/surface_ps5.h> diff --git a/glue/rexglue-sdk-main/src/ui/vulkan/command_counters.inc b/glue/rexglue-sdk-main/src/ui/vulkan/command_counters.inc index cec91ec7..96ec1fd7 100644 --- a/glue/rexglue-sdk-main/src/ui/vulkan/command_counters.inc +++ b/glue/rexglue-sdk-main/src/ui/vulkan/command_counters.inc @@ -85,7 +85,7 @@ std::string ConsumeCommandCounters(uint32_t frames) { } static void InstallCommandCounters(VulkanDevice::Functions& dfn) { -#if defined(__ANDROID__) +#if defined(__ANDROID__) || defined(__PROSPERO__) using namespace command_counters; #define REX_WRAP(field, saved, wrapper) \ if (dfn.field) { \ diff --git a/glue/rexglue-sdk-main/src/ui/vulkan/memory_recycler.inc b/glue/rexglue-sdk-main/src/ui/vulkan/memory_recycler.inc new file mode 100644 index 00000000..28bcdb26 --- /dev/null +++ b/glue/rexglue-sdk-main/src/ui/vulkan/memory_recycler.inc @@ -0,0 +1,190 @@ +// [ps5 port] Device-memory recycling. Included inside namespace +// rex::ui::vulkan by vulkan_device.cpp. +// +// On the PS5 every vkAllocateMemory is a direct-memory allocation and a +// mapping in the kernel, and every vkFreeMemory an unmap and a release. The +// renderer gives each texture and surface its own allocation, and streaming +// replaces textures constantly while driving, so the recorder spent about 8% +// of its time in those calls and some frames took 100-500 ms. Freed +// device-local memory of up to kLargest bytes is kept here instead (at most +// kBudget in all, oldest given back first) and handed to the next allocation +// of the same memory type and nearly the same size. Contents are undefined +// either way. Host-visible memory and allocations with extension structures +// other than a dedicated-allocation hint go straight to the driver. +namespace memory_recycler { + +constexpr VkDeviceSize kPage = 16 * 1024; // the PS5 driver's page +constexpr VkDeviceSize kLargest = 48ull * 1024 * 1024; // a 1440p RGBA16F target fits +constexpr VkDeviceSize kBudget = 128ull * 1024 * 1024; + +PFN_vkAllocateMemory allocate_fn; +PFN_vkFreeMemory free_fn; +VkDevice installed_device = VK_NULL_HANDLE; +uint32_t recyclable_types = 0; // device-local, nothing else + +using Key = std::pair<uint32_t, VkDeviceSize>; // (memory type, size) +struct Cached { + VkDeviceMemory memory; + uint64_t serial; +}; +std::mutex mutex; +// Ours, live: memory -> (size, type). +std::unordered_map<VkDeviceMemory, std::pair<VkDeviceSize, uint32_t>> live; +// Free, by key; each list oldest first (reuse takes the newest). +std::map<Key, std::vector<Cached>> cached; +// Every entry ever cached, oldest first; entries reused since are skipped. +std::deque<std::pair<uint64_t, Key>> order; +VkDeviceSize cached_bytes = 0; +size_t cached_count = 0; +uint64_t next_serial = 0; +std::atomic<uint64_t> hits{0}, misses{0}, given_back{0}; + +VkDeviceSize Rounded(VkDeviceSize size) { return (size + kPage - 1) & ~(kPage - 1); } + +// Under mutex: the oldest free entry gives its memory back to the driver. +bool ReleaseOldestLocked(VkDevice device) { + while (!order.empty()) { + const auto [serial, key] = order.front(); + order.pop_front(); + auto it = cached.find(key); + // A list's front is its oldest entry; an older serial was reused. + if (it == cached.end() || it->second.front().serial != serial) continue; + const VkDeviceMemory memory = it->second.front().memory; + it->second.erase(it->second.begin()); + if (it->second.empty()) cached.erase(it); + cached_bytes -= key.second; + --cached_count; + free_fn(device, memory, nullptr); + given_back.fetch_add(1, std::memory_order_relaxed); + return true; + } + return false; +} + +// Under mutex: drops the order's entries for memory reused since. +void CompactOrderLocked() { + if (order.size() < 4 * cached_count + 4096) return; + std::vector<std::pair<uint64_t, Key>> present; + present.reserve(cached_count); + for (const auto& [key, list] : cached) + for (const Cached& entry : list) present.emplace_back(entry.serial, key); + std::sort(present.begin(), present.end(), + [](const auto& a, const auto& b) { return a.first < b.first; }); + order.assign(present.begin(), present.end()); +} + +VKAPI_ATTR VkResult VKAPI_CALL Allocate(VkDevice device, const VkMemoryAllocateInfo* info, + const VkAllocationCallbacks* allocator, + VkDeviceMemory* memory) { + const bool plain_next = + !info->pNext || (static_cast<const VkBaseInStructure*>(info->pNext)->sType == + VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO && + !static_cast<const VkBaseInStructure*>(info->pNext)->pNext); + if (allocator || !plain_next || info->memoryTypeIndex >= 32 || + !(recyclable_types & (1u << info->memoryTypeIndex)) || info->allocationSize > kLargest) { + return allocate_fn(device, info, allocator, memory); + } + const VkDeviceSize size = Rounded(info->allocationSize); + { + std::lock_guard lock(mutex); + // The same size, or up to a quarter larger. + auto it = cached.lower_bound({info->memoryTypeIndex, size}); + if (it != cached.end() && it->first.first == info->memoryTypeIndex && + it->first.second <= size + size / 4) { + const VkDeviceSize found_size = it->first.second; + *memory = it->second.back().memory; + it->second.pop_back(); + if (it->second.empty()) cached.erase(it); + cached_bytes -= found_size; + --cached_count; + CompactOrderLocked(); + live.emplace(*memory, std::make_pair(found_size, info->memoryTypeIndex)); + hits.fetch_add(1, std::memory_order_relaxed); + return VK_SUCCESS; + } + } + // Without the dedicated hint the allocation can serve any image later. + VkMemoryAllocateInfo plain = *info; + plain.pNext = nullptr; + plain.allocationSize = size; + VkResult result = allocate_fn(device, &plain, nullptr, memory); + if (result != VK_SUCCESS) { + // Short of memory: give back everything held here and try once more. + { + std::lock_guard lock(mutex); + while (ReleaseOldestLocked(device)) { + } + } + result = allocate_fn(device, &plain, nullptr, memory); + if (result != VK_SUCCESS) return result; + } + misses.fetch_add(1, std::memory_order_relaxed); + std::lock_guard lock(mutex); + live.emplace(*memory, std::make_pair(size, info->memoryTypeIndex)); + return VK_SUCCESS; +} + +VKAPI_ATTR void VKAPI_CALL Free(VkDevice device, VkDeviceMemory memory, + const VkAllocationCallbacks* allocator) { + if (memory != VK_NULL_HANDLE) { + std::lock_guard lock(mutex); + auto it = live.find(memory); + if (it != live.end()) { + const auto [size, type] = it->second; + live.erase(it); + if (size <= kBudget) { + while (cached_bytes + size > kBudget && ReleaseOldestLocked(device)) { + } + const uint64_t serial = ++next_serial; + cached[{type, size}].push_back({memory, serial}); + order.emplace_back(serial, Key{type, size}); + cached_bytes += size; + ++cached_count; + return; + } + } + } + free_fn(device, memory, allocator); +} + +void FreeAll(VkDevice device) { + if (!free_fn) return; + std::lock_guard lock(mutex); + while (ReleaseOldestLocked(device)) { + } +} + +} // namespace memory_recycler + +void MemoryRecyclerReleaseAll() { + if (memory_recycler::installed_device) memory_recycler::FreeAll(memory_recycler::installed_device); +} + +std::string MemoryRecyclerSummary() { + using namespace memory_recycler; + VkDeviceSize held; + { + std::lock_guard lock(mutex); + held = cached_bytes; + } + return fmt::format("memory-recycler: reused={} new={} given-back={} held={} MB", + hits.load(std::memory_order_relaxed), misses.load(std::memory_order_relaxed), + given_back.load(std::memory_order_relaxed), held >> 20); +} + +static void InstallMemoryRecycler(VulkanDevice::Functions& dfn, + const VkPhysicalDeviceMemoryProperties& properties, + VkDevice device) { + using namespace memory_recycler; + if (allocate_fn) return; // one device + installed_device = device; + for (uint32_t i = 0; i < properties.memoryTypeCount && i < 32; ++i) { + if (properties.memoryTypes[i].propertyFlags == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) + recyclable_types |= 1u << i; + } + if (!recyclable_types || !dfn.vkAllocateMemory || !dfn.vkFreeMemory) return; + allocate_fn = dfn.vkAllocateMemory; + free_fn = dfn.vkFreeMemory; + dfn.vkAllocateMemory = Allocate; + dfn.vkFreeMemory = Free; +} diff --git a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_device.cpp b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_device.cpp index 7c25d612..5f485b90 100644 --- a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_device.cpp +++ b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_device.cpp @@ -16,7 +16,9 @@ #endif #include <algorithm> #include <cstring> +#include <deque> #include <map> +#include <mutex> #include <string> #include <unordered_map> #include <vector> @@ -31,7 +33,7 @@ #include "graphics/gta4_native/temporal/dlss_bootstrap.h" -REXCVAR_DEFINE_BOOL(vulkan_command_counters, false, "UI/Vulkan", +REXCVAR_DEFINE_BOOL(vulkan_command_counters, REX_PLATFORM_PS5 != 0, "UI/Vulkan", "Count barriers, renderings, draws, copies and pipeline binds per frame for " "the pipeline report (a wrapper on every such command)"); REXCVAR_DEFINE_BOOL(vulkan_native_queue_separation, true, "UI/Vulkan", @@ -62,6 +64,9 @@ namespace ui { namespace vulkan { #include "command_counters.inc" +#if REX_PLATFORM_PS5 +#include "memory_recycler.inc" +#endif template <typename Structure, VkStructureType StructureType> struct VulkanFeatures { @@ -1112,6 +1117,13 @@ std::unique_ptr<VulkanDevice> VulkanDevice::CreateIfSupported( return nullptr; } if (REXCVAR_GET(vulkan_command_counters)) InstallCommandCounters(dfn); +#if REX_PLATFORM_PS5 + { + VkPhysicalDeviceMemoryProperties recycler_memory_properties; + ifn.vkGetPhysicalDeviceMemoryProperties(physical_device, &recycler_memory_properties); + InstallMemoryRecycler(dfn, recycler_memory_properties, device->device_); + } +#endif // Get the queues. @@ -1147,6 +1159,16 @@ std::unique_ptr<VulkanDevice> VulkanDevice::CreateIfSupported( if (memory_type_flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { device->memory_types_.host_cached |= memory_type_bit; } +#if REX_PLATFORM_PS5 + // [ps5 port] The PS5's driver is a port; its memory types decide whether + // uploads need flushing and where textures live. + const uint32_t heap = memory_properties.memoryTypes[memory_type_index].heapIndex; + REXLOG_INFO("vulkan memory type {}: heap {} ({} MB{}) flags {:#x}", memory_type_index, heap, + memory_properties.memoryHeaps[heap].size >> 20, + (memory_properties.memoryHeaps[heap].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) + ? ", device local" : "", + uint32_t(memory_type_flags)); +#endif } return device; @@ -1154,6 +1176,9 @@ std::unique_ptr<VulkanDevice> VulkanDevice::CreateIfSupported( VulkanDevice::~VulkanDevice() { if (device_) { +#if REX_PLATFORM_PS5 + memory_recycler::FreeAll(device_); +#endif vulkan_instance_->functions().vkDestroyDevice(device_, nullptr); } } diff --git a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_instance.cpp b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_instance.cpp index eb0ad80b..d94f2fb2 100644 --- a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_instance.cpp +++ b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_instance.cpp @@ -9,7 +9,10 @@ * @modified Tom Clay, 2026 - Adapted for ReXGlue runtime */ +#include <atomic> #include <map> +#include <cstdlib> +#include <cstring> #include <sstream> #include <string> #include <unordered_map> @@ -41,6 +44,87 @@ namespace rex { namespace ui { namespace vulkan { +#if REX_PLATFORM_PS5 +} // namespace vulkan +} // namespace ui +} // namespace rex +extern "C" VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr( + VkInstance instance, const char* name) __attribute__((weak)); +namespace rex { +namespace ui { +namespace vulkan { +// [ps5 port] RADV's ICD entry point, with the core-1.1 names it only exports +// under their KHR aliases mapped (as plume-ps5 does). +static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL Ps5GetInstanceProcAddr(VkInstance instance, + const char* name) { + if (!vk_icdGetInstanceProcAddr || !name) return nullptr; + if (PFN_vkVoidFunction fn = vk_icdGetInstanceProcAddr(instance, name)) return fn; + if (std::strcmp(name, "vkGetPhysicalDeviceProperties2") == 0) + return vk_icdGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2KHR"); + if (std::strcmp(name, "vkGetPhysicalDeviceFeatures2") == 0) + return vk_icdGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2KHR"); + return nullptr; +} +// The app's instance, for the vk* functions below. +static std::atomic<VkInstance> g_ps5_vulkan_instance{VK_NULL_HANDLE}; +} // namespace vulkan +} // namespace ui +} // namespace rex +// [ps5 port] AMD's FidelityFX SDK (FSR 3) calls a few Vulkan functions by name +// rather than through the function pointers it is given. With no loader on the +// PS5, these forward to RADV through the app's instance (or the device). +namespace { +template <typename Fn> +Fn Ps5InstanceFunction(const char* name) { + return reinterpret_cast<Fn>(rex::ui::vulkan::Ps5GetInstanceProcAddr( + rex::ui::vulkan::g_ps5_vulkan_instance.load(std::memory_order_acquire), name)); +} +} // namespace +extern "C" { +VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties(VkPhysicalDevice d, VkPhysicalDeviceProperties* p) { + static const auto fn = Ps5InstanceFunction<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties"); + fn(d, p); +} +VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties2(VkPhysicalDevice d, VkPhysicalDeviceProperties2* p) { + static const auto fn = Ps5InstanceFunction<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2"); + fn(d, p); +} +VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFeatures(VkPhysicalDevice d, VkPhysicalDeviceFeatures* f) { + static const auto fn = Ps5InstanceFunction<PFN_vkGetPhysicalDeviceFeatures>("vkGetPhysicalDeviceFeatures"); + fn(d, f); +} +VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFeatures2(VkPhysicalDevice d, VkPhysicalDeviceFeatures2* f) { + static const auto fn = Ps5InstanceFunction<PFN_vkGetPhysicalDeviceFeatures2>("vkGetPhysicalDeviceFeatures2"); + fn(d, f); +} +VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice d, + VkPhysicalDeviceMemoryProperties* p) { + static const auto fn = + Ps5InstanceFunction<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties"); + fn(d, p); +} +VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice d, const char* layer, + uint32_t* count, + VkExtensionProperties* p) { + static const auto fn = + Ps5InstanceFunction<PFN_vkEnumerateDeviceExtensionProperties>("vkEnumerateDeviceExtensionProperties"); + return fn(d, layer, count, p); +} +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* name) { + static const auto fn = Ps5InstanceFunction<PFN_vkGetDeviceProcAddr>("vkGetDeviceProcAddr"); + return fn(device, name); +} +VKAPI_ATTR VkResult VKAPI_CALL vkCreateBuffer(VkDevice device, const VkBufferCreateInfo* info, + const VkAllocationCallbacks* allocator, VkBuffer* buffer) { + static const auto fn = reinterpret_cast<PFN_vkCreateBuffer>(vkGetDeviceProcAddr(device, "vkCreateBuffer")); + return fn(device, info, allocator, buffer); +} +} // extern "C" +namespace rex { +namespace ui { +namespace vulkan { +#endif + std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, const bool try_enable_validation) { std::unique_ptr<VulkanInstance> vulkan_instance(new VulkanInstance()); @@ -55,6 +139,13 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, bool functions_loaded = true; bool loader_loaded = false; +#if REX_PLATFORM_PS5 + // [ps5 port] No loader library: PS5_Mesa's RADV is linked into the app and + // its ICD entry point stands in for vkGetInstanceProcAddr (as in plume-ps5). + ifn.vkGetInstanceProcAddr = Ps5GetInstanceProcAddr; + functions_loaded &= vk_icdGetInstanceProcAddr != nullptr; + (void)loader_loaded; +#else #if REX_PLATFORM_MAC const MacOSVulkanRuntimePaths macos_runtime_paths = DetectMacOSVulkanRuntimePaths(); ConfigureMacOSVulkanEnvironment(macos_runtime_paths); @@ -97,6 +188,7 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, XE_VULKAN_LOAD_LOADER_FUNCTION(vkGetInstanceProcAddr); XE_VULKAN_LOAD_LOADER_FUNCTION(vkDestroyInstance); #undef XE_VULKAN_LOAD_LOADER_FUNCTION +#endif if (!functions_loaded) { REXLOG_ERROR("Failed to get Vulkan loader function pointers"); return nullptr; @@ -179,6 +271,10 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, // #217. requested_extensions.emplace("VK_EXT_metal_surface", &vulkan_instance->extensions_.ext_EXT_metal_surface); +#endif +#if REX_PLATFORM_PS5 + // #3. [ps5 port] + requested_extensions.emplace("VK_KHR_display", &vulkan_instance->extensions_.ext_KHR_display); #endif } @@ -429,6 +525,9 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, vk::to_string(vk::Result(instance_create_result))); return nullptr; } +#if REX_PLATFORM_PS5 + g_ps5_vulkan_instance.store(vulkan_instance->instance_, std::memory_order_release); +#endif const auto check_dlss_extensions = [&](const auto& requirements, bool& enabled, std::string& reason) { @@ -453,6 +552,12 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, // Load instance functions. +#if REX_PLATFORM_PS5 + ifn.vkDestroyInstance = PFN_vkDestroyInstance( + ifn.vkGetInstanceProcAddr(vulkan_instance->instance_, "vkDestroyInstance")); + functions_loaded &= ifn.vkDestroyInstance != nullptr; +#endif + #define XE_UI_VULKAN_FUNCTION(name) \ functions_loaded &= \ (ifn.name = PFN_##name(ifn.vkGetInstanceProcAddr(vulkan_instance->instance_, #name))) != \ @@ -499,6 +604,11 @@ std::unique_ptr<VulkanInstance> VulkanInstance::Create(const bool with_surface, if (vulkan_instance->extensions_.ext_EXT_metal_surface) { #include <rex/ui/vulkan/functions/instance_ext_metal_surface.inc> } +#endif +#if REX_PLATFORM_PS5 + if (vulkan_instance->extensions_.ext_KHR_display) { +#include <rex/ui/vulkan/functions/instance_khr_display.inc> + } #endif if (vulkan_instance->extensions_.ext_KHR_surface) { #include <rex/ui/vulkan/functions/instance_khr_surface.inc> diff --git a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_presenter.cpp b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_presenter.cpp index 541be7e0..f192bdca 100644 --- a/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_presenter.cpp +++ b/glue/rexglue-sdk-main/src/ui/vulkan/vulkan_presenter.cpp @@ -35,6 +35,9 @@ #include <rex/logging.h> #include <rex/math.h> #include <rex/platform.h> +#if REX_PLATFORM_PS5 +#include <rex/frame_stats.h> +#endif #if REX_PLATFORM_MAC || REX_PLATFORM_GNU_LINUX || REX_PLATFORM_ANDROID #include <time.h> #endif @@ -55,6 +58,102 @@ #if REX_PLATFORM_MAC #include <rex/ui/surface_mac.h> #endif +#if REX_PLATFORM_PS5 +#include <rex/ui/surface_ps5.h> +extern "C" int sceSystemServiceHideSplashScreen(); +#endif + +#if REX_PLATFORM_PS5 +namespace { +// [ps5 port] A VkSurfaceKHR on the TV: the first display that reports modes, +// its 3840x2160 mode if any (else its first), and a plane that can show that +// display. Updates the surface's size to the mode's visible region. After +// plume-ps5's createPs5DisplaySurface (Nazky, MIT). +VkResult CreatePs5DisplaySurface(const rex::ui::vulkan::VulkanInstance::Functions& ifn, + VkInstance instance, VkPhysicalDevice physical_device, + rex::ui::Ps5DisplaySurface& surface, VkSurfaceKHR& surface_out) { + sceSystemServiceHideSplashScreen(); + if (!ifn.vkGetPhysicalDeviceDisplayPropertiesKHR || !ifn.vkCreateDisplayPlaneSurfaceKHR) { + return VK_ERROR_EXTENSION_NOT_PRESENT; + } + uint32_t display_count = 0; + VkResult res = ifn.vkGetPhysicalDeviceDisplayPropertiesKHR(physical_device, &display_count, nullptr); + if (res != VK_SUCCESS || !display_count) { + REXLOG_ERROR("PS5 display: no displays ({})", int(res)); + return VK_ERROR_INITIALIZATION_FAILED; + } + std::vector<VkDisplayPropertiesKHR> displays(display_count); + res = ifn.vkGetPhysicalDeviceDisplayPropertiesKHR(physical_device, &display_count, displays.data()); + if (res != VK_SUCCESS) return res; + uint32_t plane_count = 0; + res = ifn.vkGetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &plane_count, nullptr); + if (res != VK_SUCCESS || !plane_count) { + REXLOG_ERROR("PS5 display: no display planes ({})", int(res)); + return VK_ERROR_INITIALIZATION_FAILED; + } + std::vector<VkDisplayPlanePropertiesKHR> planes(plane_count); + res = ifn.vkGetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &plane_count, planes.data()); + if (res != VK_SUCCESS) return res; + + VkDisplayKHR display = VK_NULL_HANDLE; + VkDisplayModeKHR mode = VK_NULL_HANDLE; + VkExtent2D extent = {3840, 2160}; + uint32_t refresh_millihertz = 0; + for (uint32_t i = 0; i < display_count && mode == VK_NULL_HANDLE; ++i) { + uint32_t mode_count = 0; + if (ifn.vkGetDisplayModePropertiesKHR(physical_device, displays[i].display, &mode_count, + nullptr) != VK_SUCCESS || + !mode_count) { + continue; + } + std::vector<VkDisplayModePropertiesKHR> modes(mode_count); + if (ifn.vkGetDisplayModePropertiesKHR(physical_device, displays[i].display, &mode_count, + modes.data()) != VK_SUCCESS) { + continue; + } + display = displays[i].display; + mode = modes[0].displayMode; + extent = modes[0].parameters.visibleRegion; + refresh_millihertz = modes[0].parameters.refreshRate; + for (const VkDisplayModePropertiesKHR& m : modes) { + if (m.parameters.visibleRegion.width == 3840 && m.parameters.visibleRegion.height == 2160) { + mode = m.displayMode; + extent = m.parameters.visibleRegion; + refresh_millihertz = m.parameters.refreshRate; + break; + } + } + } + if (mode == VK_NULL_HANDLE) { + REXLOG_ERROR("PS5 display: no usable display mode"); + return VK_ERROR_INITIALIZATION_FAILED; + } + uint32_t plane = 0; + for (uint32_t i = 0; i < plane_count; ++i) { + if (planes[i].currentDisplay == VK_NULL_HANDLE || planes[i].currentDisplay == display) { + plane = i; + break; + } + } + VkDisplaySurfaceCreateInfoKHR info = {}; + info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR; + info.displayMode = mode; + info.planeIndex = plane; + info.planeStackIndex = planes[plane].currentStackIndex; + info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; + info.globalAlpha = 1.0f; + info.alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR; + info.imageExtent = extent; + res = ifn.vkCreateDisplayPlaneSurfaceKHR(instance, &info, nullptr, &surface_out); + if (res == VK_SUCCESS) { + surface.SetSize(extent.width, extent.height); + REXLOG_INFO("PS5 display: {}x{} at {:.2f} Hz on plane {}", extent.width, extent.height, + refresh_millihertz / 1000.0, plane); + } + return res; +} +} // namespace +#endif REXCVAR_DEFINE_BOOL(present_render_pass_clear, true, "UI/Presenter", "Clear render pass during presentation"); @@ -367,6 +466,11 @@ Surface::TypeFlags VulkanPresenter::GetSurfaceTypesSupportedByInstance( if (instance_extensions.ext_EXT_metal_surface) { type_flags |= Surface::kTypeFlag_CAMetalLayer; } +#endif +#if REX_PLATFORM_PS5 + if (instance_extensions.ext_KHR_display) { + type_flags |= Surface::kTypeFlag_Ps5Display; + } #endif return type_flags; } @@ -821,6 +925,18 @@ VulkanPresenter::ConnectOrReconnectPaintingToSurfaceFromUIThread(Surface& new_su vulkan_surface_create_result = ifn.vkCreateMetalSurfaceEXT( instance, &surface_create_info, nullptr, &paint_context_.vulkan_surface); } break; +#endif +#if REX_PLATFORM_PS5 + case Surface::kTypeIndex_Ps5Display: { + // [ps5 port] The TV: the first display, its 4K mode when it has one + // (else its first mode), on a plane that can show it (after + // plume-ps5's createPs5DisplaySurface). + auto& ps5_surface = const_cast<Ps5DisplaySurface&>( + static_cast<const Ps5DisplaySurface&>(new_surface)); + vulkan_surface_create_result = + CreatePs5DisplaySurface(ifn, instance, vulkan_device_->physical_device(), ps5_surface, + paint_context_.vulkan_surface); + } break; #endif default: assert_unhandled_case(surface_type); @@ -1144,7 +1260,14 @@ bool VulkanPresenter::RefreshGuestOutputImpl( hdr_headroom = std::max(1.0f, window->GetHDRHeadroom()); sdr_white_level = std::max(1.0f, window->GetSDRWhiteLevel()); } +#if REX_PLATFORM_PS5 + const int64_t acquire_begin_ns = rex::frame_stats::NowNs(); +#endif auto image_access = image_instance.image->AcquireAccess(); +#if REX_PLATFORM_PS5 + rex::frame_stats::g_output_image_wait_us.fetch_add( + uint64_t(rex::frame_stats::NowNs() - acquire_begin_ns) / 1000, std::memory_order_relaxed); +#endif if (!image_access.ticket().valid()) return false; VulkanGuestOutputRefreshContext context( is_8bpc_out_ref, image_instance.image->image(), image_instance.image->view(), @@ -2149,7 +2272,13 @@ Presenter::PaintResult VulkanPresenter::PaintAndPresentImpl(bool execute_ui_draw // the new reference or is exclusively owned by main target painting (and // multiple threads can't paint the main target at the same time). } +#if REX_PLATFORM_PS5 + // [ps5 port] Half the game's frame time is off the TV's vblank grid: the + // game runs at half the refresh and FIFO spaces the pair a vblank apart. + frame_pacer().SetMinimumInterval(0); +#else frame_pacer().SetMinimumInterval(selected_generated_frame ? selected_generated_frame->interval_ns : 0); +#endif const auto& requested_frame_probe = guest_output_properties.provenance.frame_pixel_probe; if (requested_frame_probe.valid() && !selected_generated_frame) { @@ -2176,6 +2305,21 @@ Presenter::PaintResult VulkanPresenter::PaintAndPresentImpl(bool execute_ui_draw diagnostic_tv_provenance_.tv_session_id) { diagnostic_tv_paint_carry_remaining_ = 0; } + // [ps5 port] The same traced frame painted again (the game stopped making + // frames): a few lines, then one every 600 paints, or the log fills with + // them and a freeze's own report never reaches the end of it. + { + static uint64_t repeated_present = 0, repeats = 0; + if (log_tv_paint) { + if (tv_trace_provenance.title_present_id == repeated_present) { + ++repeats; + } else { + repeated_present = tv_trace_provenance.title_present_id; + repeats = 0; + } + if (repeats > 8 && repeats % 600) log_tv_paint = false; + } + } if (guest_output_image) { #if defined(REX_HAS_FIDELITYFX_FSR1) @@ -2946,6 +3090,16 @@ Presenter::PaintResult VulkanPresenter::PaintAndPresentImpl(bool execute_ui_draw gpu_flight::Record("present.queue-begin", uint64_t(uintptr_t(paint_context_.swapchain)), current_paint_submission_index, tv_trace_provenance.submitted_frame, swapchain_image_index, uint64_t(uintptr_t(present_semaphore))); +#if REX_PLATFORM_PS5 + // (the TV's previous image its full time first: ps5_display.cpp) + if (auto* before_present = rex::frame_stats::g_before_present.load(std::memory_order_acquire)) { + static uint64_t last_publication = 0; + const uint64_t publication = guest_output_properties.provenance.publication_serial; + rex::frame_stats::g_present_new_frame.store(publication != last_publication, std::memory_order_release); + last_publication = publication; + before_present(); + } +#endif { const VulkanDevice::Queue::Acquisition queue_acquisition = vulkan_device_->AcquireQueue(paint_context_.present_queue_family, 0); @@ -2962,6 +3116,11 @@ Presenter::PaintResult VulkanPresenter::PaintAndPresentImpl(bool execute_ui_draw } if (present_result == VK_SUCCESS || present_result == VK_SUBOPTIMAL_KHR) { const uint64_t publication = guest_output_properties.provenance.publication_serial; +#if REX_PLATFORM_PS5 + // (a generated image and its real partner are two pictures on the TV) + rex::frame_stats::NoteImageSent(publication * 2 + + (generated_stage == GeneratedFrameDelivery::Stage::kGenerated ? 0 : 1)); +#endif if (selected_generated_frame && !guest_output_pass_recorded) { // A successfully presented fallback clear is not delivery of the pair. // Keep the same stage pending and retry through the common pacer. @@ -2976,11 +3135,23 @@ Presenter::PaintResult VulkanPresenter::PaintAndPresentImpl(bool execute_ui_draw // Allow the next scene to render, retaining explicit paint demand until // this publication's real image has actually reached queue-present. AdmitPacedPublication(publication); +#if REX_PLATFORM_PS5 + // [ps5 port] The real image goes right behind it: FIFO shows it on the + // next vblank (two vblanks each at 30 -> 60 on 120 Hz). + rex::frame_stats::g_fg_pairs.fetch_add(1, std::memory_order_relaxed); + if (Window* window = connected_window()) window->RequestPaint(); +#else if (Window* window = connected_window()) window->RequestPaintAfterNanoseconds(frame_pacer().period()); +#endif } else { generated_delivery_.Presented(generated_stage, publication, diagnostic_swapchain_epoch_); AcceptPacedPublication(publication); +#if REX_PLATFORM_PS5 + if (generated_stage == GeneratedFrameDelivery::Stage::kUnpaired && guest_output_image && + rex::frame_stats::g_frame_generation_active.load(std::memory_order_relaxed)) + rex::frame_stats::g_fg_unpaired.fetch_add(1, std::memory_order_relaxed); +#endif if (generated_stage == GeneratedFrameDelivery::Stage::kReal) pending_generated_frame_.reset(); // A newer publication may have delivered the wakeup that painted this diff --git a/glue/rexglue-sdk-main/src/ui/window.cpp b/glue/rexglue-sdk-main/src/ui/window.cpp index 35162382..122f5cc9 100644 --- a/glue/rexglue-sdk-main/src/ui/window.cpp +++ b/glue/rexglue-sdk-main/src/ui/window.cpp @@ -51,7 +51,9 @@ REXCVAR_DEFINE_INT32(video_mode_height, 720, "Display", "Guest video mode height .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); // Android: native 720p (the console's resolution); the presenter scales it to the panel. -REXCVAR_DEFINE_STRING(resolution, REX_PLATFORM_ANDROID ? "720p" : "", "Display", +// [ps5 port] PS5: 4K, the output's own (the hub sets it; the PS5's graphics +// card has room for it while the game's render thread is the limit). +REXCVAR_DEFINE_STRING(resolution, REX_PLATFORM_ANDROID ? "720p" : REX_PLATFORM_PS5 ? "4k" : "", "Display", "Common resolution preset for both guest video mode and startup window (for " "example: 720p, 1080p, 1440p, 4k, 1280x720)") .lifecycle(rex::cvar::Lifecycle::kRequiresRestart); diff --git a/glue/rexglue-sdk-main/src/ui/window_sdl.cpp b/glue/rexglue-sdk-main/src/ui/window_sdl.cpp index 87f97744..5acee304 100644 --- a/glue/rexglue-sdk-main/src/ui/window_sdl.cpp +++ b/glue/rexglue-sdk-main/src/ui/window_sdl.cpp @@ -46,6 +46,8 @@ #endif #elif REX_PLATFORM_ANDROID #include <rex/ui/surface_android.h> +#elif REX_PLATFORM_PS5 +#include <rex/ui/surface_ps5.h> // [ps5 port] #elif REX_PLATFORM_GNU_LINUX #include <X11/Xlib-xcb.h> #include <rex/ui/surface_gnulinux.h> @@ -175,8 +177,15 @@ bool WindowSDL::OpenImpl() { #if REX_PLATFORM_MAC flags |= SDL_WINDOW_METAL; #endif +#if REX_PLATFORM_PS5 + // [ps5 port] The window is the dummy driver's: as big as the TV picture + // (3840x2160), never "fullscreen" (that would take the dummy display's + // 1024x768 mode and lay ImGui screens out 4:3). + sdl_window_ = SDL_CreateWindow(GetTitle().c_str(), 3840, 2160, flags & ~SDL_WINDOW_HIGH_PIXEL_DENSITY); +#else sdl_window_ = SDL_CreateWindow(GetTitle().c_str(), int(SizeToPhysical(GetDesiredLogicalWidth())), int(SizeToPhysical(GetDesiredLogicalHeight())), flags); +#endif if (!sdl_window_) { REXLOG_ERROR("SDL_CreateWindow failed: {}", SDL_GetError()); return false; @@ -211,7 +220,7 @@ bool WindowSDL::OpenImpl() { } } - if (IsFullscreen()) { + if (IsFullscreen() && !REX_PLATFORM_PS5) { // Borderless desktop fullscreen (a NULL display mode is SDL3's default). if (!SDL_SetWindowFullscreen(sdl_window_, true)) { REXLOG_ERROR("SDL_SetWindowFullscreen failed: {}", SDL_GetError()); @@ -234,7 +243,7 @@ bool WindowSDL::OpenImpl() { // Fullscreen transitions may be asynchronous. Synchronize when possible so // the first surface observes final geometry, but a timeout only means the // transition is still pending. SDL window events will update the surface. - if (IsFullscreen() && !SDL_SyncWindow(sdl_window_)) { + if (IsFullscreen() && !REX_PLATFORM_PS5 && !SDL_SyncWindow(sdl_window_)) { REXLOG_WARN("SDL_SyncWindow timed out while entering fullscreen; continuing asynchronously"); } @@ -379,6 +388,9 @@ uint32_t WindowSDL::GetLatestDpiImpl() const { } void WindowSDL::ApplyNewFullscreen() { +#if REX_PLATFORM_PS5 + return; // [ps5 port] the window stays the TV picture's size +#endif if (!sdl_window_) { return; } @@ -504,6 +516,12 @@ std::unique_ptr<Surface> WindowSDL::CreateSurfaceImpl(Surface::TypeFlags allowed return std::make_unique<CAMetalLayerSurface>(sdl_window_, layer); } } +#elif REX_PLATFORM_PS5 + // [ps5 port] SDL's window only carries events; the picture goes to the TV + // through VK_KHR_display. The presenter sets the real size from the mode. + if (allowed_types & Surface::kTypeFlag_Ps5Display) { + return std::make_unique<Ps5DisplaySurface>(3840, 2160); + } #elif REX_PLATFORM_ANDROID if (allowed_types & Surface::kTypeFlag_AndroidNativeWindow) { auto* native_window = static_cast<ANativeWindow*>(GetNativeWindowHandle()); diff --git a/glue/rexglue-sdk-main/src/ui/windowed_app_main_sdl.cpp b/glue/rexglue-sdk-main/src/ui/windowed_app_main_sdl.cpp index ac2d8dc4..26e127d6 100644 --- a/glue/rexglue-sdk-main/src/ui/windowed_app_main_sdl.cpp +++ b/glue/rexglue-sdk-main/src/ui/windowed_app_main_sdl.cpp @@ -11,6 +11,7 @@ #include <algorithm> #include <cstdio> +#include <cstring> #include <cstdlib> #include <map> #include <memory> @@ -23,6 +24,9 @@ #include <rex/platform.h> #include <rex/ui/windowed_app.h> #include <rex/ui/windowed_app_context_sdl.h> +#if REX_PLATFORM_PS5 +#include <SDL3/SDL_hints.h> +#endif REXCVAR_DEFINE_BOOL(diagnostics, false, "Diagnostics", "Enable diagnostic logging and instrumentation"); @@ -48,6 +52,10 @@ REXCVAR_DEFINE_STRING(diagnostics_categories, "", "Diagnostics", namespace { +#if REX_PLATFORM_PS5 +void Ps5EarlyFailure(const char* what); // (below) +#endif + int RunWindowedApp(int argc, char** argv) { auto remaining = rex::cvar::Init(argc, argv); rex::cvar::ApplyEnvironment(); @@ -56,10 +64,31 @@ int RunWindowedApp(int argc, char** argv) { // native-probe instrumentation are gated on it; without this call the // policy stays unconfigured and every log line is suppressed. { + bool diagnostics = REXCVAR_GET(diagnostics); + std::string categories = REXCVAR_GET(diagnostics_categories); +#if REX_PLATFORM_PS5 + // [ps5 port] No command line on the PS5: the log is on unless the hub's + // Save Logs setting is off (read straight from its config file, as the + // policy has to be set before the config is loaded). + // (The renderer's own profiler is not offered: it turns off the + // renderer's command coalescing while it runs and the PS5 drops to a few + // frames a second. The sampling profiler in rex/ps5_diag.h replaces it.) + if (!diagnostics) { + bool save_logs = true; + if (std::FILE* f = std::fopen("/app0/UserData/LibertyRecomp/native.toml", "rb")) { + char line[512]; + while (std::fgets(line, sizeof(line), f)) { + const char* start = line + std::strspn(line, " \t"); + const char* key = std::strstr(line, "ps5_save_logs"); + if (key && key == start && std::strstr(key, "false")) save_logs = false; + } + std::fclose(f); + } + if (save_logs) diagnostics = true, categories = "logging"; + } +#endif std::string diagnostics_error; - if (!rex::diagnostics::Configure(REXCVAR_GET(diagnostics), - REXCVAR_GET(diagnostics_categories), - &diagnostics_error)) { + if (!rex::diagnostics::Configure(diagnostics, categories, &diagnostics_error)) { std::fprintf(stderr, "diagnostics: %s\n", diagnostics_error.c_str()); #if REX_PLATFORM_ANDROID // stderr is discarded on Android; without this the app just closes. @@ -73,8 +102,16 @@ int RunWindowedApp(int argc, char** argv) { int result; { +#if REX_PLATFORM_PS5 + // [ps5 port] SDL has no PS5 video driver: its dummy driver keeps windows and + // events working; the picture goes to the TV through VK_KHR_display. + SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "dummy"); +#endif rex::ui::SDLWindowedAppContext app_context; if (!app_context.Initialize()) { +#if REX_PLATFORM_PS5 + Ps5EarlyFailure("SDL didn't start"); +#endif return EXIT_FAILURE; } @@ -97,6 +134,11 @@ int RunWindowedApp(int argc, char** argv) { app->SetParsedArguments(std::move(parsed)); result = app->OnInitialize() ? app_context.RunMainMessageLoop() : EXIT_FAILURE; +#if REX_PLATFORM_PS5 + // [ps5 port] Skip the teardown: guest threads can't be stopped reliably. + rex::FlushLogging(); + std::_Exit(result); +#endif app->InvokeOnDestroy(); } @@ -161,8 +203,370 @@ int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hinstance_prev, LPWSTR comman #include <SDL3/SDL_main.h> #endif +#if REX_PLATFORM_PS5 + +// [ps5 port] Work in the app's folder (logs/, UserData/, Saves/ go there; the +// process starts elsewhere, often read-only), and show a notification so the +// player can tell the app started at all. +#include <cstring> +#include <sys/stat.h> +#include <unistd.h> +extern "C" int sceKernelSendNotificationRequest(int, void*, size_t, int); +extern "C" int sceSystemServiceLoadExec(const char* path, char* const argv[]); +static void Ps5Notify(const char* text) { + struct { + char unused[45]; + char message[3075]; + } request; + std::memset(&request, 0, sizeof(request)); + std::strncpy(request.message, text, sizeof(request.message) - 1); + sceKernelSendNotificationRequest(0, &request, sizeof(request), 0); +} + +#include <SDL3/SDL_error.h> +namespace { +void Ps5EarlyFailure(const char* what) { + char text[600]; + std::snprintf(text, sizeof(text), "%s: %s", what, SDL_GetError()); + Ps5Notify(text); + // (the log file doesn't exist yet: the reason in logs/startup_error.log) + mkdir("/app0/logs", 0777); + if (FILE* f = std::fopen("/app0/logs/startup_error.log", "w")) { + std::fprintf(f, "%s\n", text); + std::fclose(f); + } +} +} // namespace + +// An uncaught C++ exception ends in abort() with its message on stderr, which +// nobody sees on a PS5: put it in the log and on screen first. +#include <exception> +#include <typeinfo> +static void Ps5Terminate() { + char text[600] = "Uncaught exception"; + if (auto current = std::current_exception()) { + try { + std::rethrow_exception(current); + } catch (const std::exception& e) { + std::snprintf(text, sizeof(text), "Uncaught %s: %s", typeid(e).name(), e.what()); + } catch (...) { + } + } + REXLOG_ERROR("{}", text); + Ps5Notify(text); + sleep(2); // (let the notification show) + std::abort(); +} + +// Every 10 s: the progress counters (rex/ps5_diag.h), so a black screen's +// log shows which stage stopped moving. +#include <rex/chrono/clock.h> +#include <rex/frame_stats.h> +#include <sys/param.h> +#include <sys/cpuset.h> +#include <rex/ps5_diag.h> +#include <time.h> +#include <rex/system/kernel_state.h> +#include <rex/system/xmemory.h> +#include <spdlog/spdlog.h> +#include <chrono> +#include <thread> +REXCVAR_DEFINE_BOOL(ps5_cpu_sampler, true, "PS5", + "Sample where each thread spends its time (100 times a second) and log the " + "busiest code every 30 s"); +// The sampling profiler's clock: from 30 s in, every 10 ms signal each thread +// once (rex/ps5_diag.h), and every 30 s log what they were doing. +static void Ps5Profiler() { + std::this_thread::sleep_for(std::chrono::seconds(30)); + auto report_at = std::chrono::steady_clock::now() + std::chrono::seconds(30); + while (REXCVAR_GET(ps5_cpu_sampler)) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + rex::ps5diag::ProfileTick(); + if (std::chrono::steady_clock::now() >= report_at) { + rex::ps5diag::ProfileReport(); + report_at += std::chrono::seconds(30); + } + } +} +REXCVAR_DEFINE_BOOL(ps5_hang_reports, true, "PS5", + "When the game stops drawing for a few seconds, log where every thread is"); +// A stall in play (no new frame for 300 ms after the game has been running): +// where every thread is while it lasts, at most once every 20 s. +static void Ps5HitchWatch() { + using namespace rex::frame_stats; + int64_t last_sample_ns = 0; + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + // A thread crashed: the crash hook ends the game; if it hangs (flushing + // the log behind a lock the crashed thread holds), this does. Nothing + // else here then: logging could wait on that thread's heap lock. + if (const uint64_t crashed = rex::ps5diag::crashed_at_ms.load(std::memory_order_acquire)) { + if (rex::ps5diag::NowMs() - crashed > 5000) _exit(1); + continue; + } + const uint64_t frames = g_presents.load(std::memory_order_acquire); + const int64_t last = g_last_present_ns.load(std::memory_order_acquire), now = NowNs(); + // (again every 20 s while the same frame stays up: a freeze's report + // must reach the end of the log, where a sent log is cut) + if (frames < 900 || !last || now - last < 300000000 || now - last_sample_ns < 20000000000ll || + !REXCVAR_GET(ps5_hang_reports)) + continue; + last_sample_ns = now; + REXLOG_INFO("ps5 hitch: no new frame for {} ms after frame {}; where the threads are:", + (now - last) / 1000000, frames); + rex::ps5diag::SampleAllThreads(); + } +} +static void Ps5DiagMonitor() { + using namespace rex::ps5diag; + for (int tick = 1;; ++tick) { + std::this_thread::sleep_for(std::chrono::seconds(10)); + if (tick == 1) { + timespec res{}; + clock_getres(CLOCK_MONOTONIC, &res); + REXLOG_INFO("ps5 diag: clock resolution {} ns, host tick frequency {} Hz", res.tv_nsec, + rex::chrono::Clock::QueryHostTickFrequency()); + // The CPUs this process may run on (the game's threads share them). + cpuset_t mask; + CPU_ZERO(&mask); + const int rc = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(mask), &mask); + std::string cpus; + for (int c = 0; c < 64; ++c) + if (CPU_ISSET(c, &mask)) cpus += (cpus.empty() ? "" : ",") + std::to_string(c); + REXLOG_INFO("ps5 diag: {} CPUs online, this process runs on [{}] ({}), page size {}", + sysconf(_SC_NPROCESSORS_ONLN), cpus, rc, getpagesize()); + } + // Game time against real time: the guest clock's seconds over these 10, + // the game's own step for its last frame, and the frames shown. + { + static uint64_t last_guest = 0, last_presents = 0; + static int64_t last_ns = 0; + const uint64_t guest = rex::chrono::Clock::QueryGuestTickCount(); + const uint64_t presents = rex::frame_stats::g_presents.load(); + static uint64_t last_shown = 0; + const uint64_t shown = rex::frame_stats::g_display_presents.load(); + const int64_t now_ns = rex::frame_stats::NowNs(); + float step = 0; + if (auto* kernel = rex::system::kernel_state()) { + constexpr uint32_t kGameplayTimeStep = 0x82C6C2AC; + const uint32_t bits = + rex::memory::load_and_swap<uint32_t>(kernel->memory()->TranslateVirtual(kGameplayTimeStep)); + std::memcpy(&step, &bits, sizeof(step)); + } + using namespace rex::frame_stats; + const uint64_t rec_us = g_recorder_us.exchange(0), rec_n = g_recorder_frames.exchange(0); + const uint64_t rec_max = g_recorder_max_us.exchange(0), wait_us = g_handoff_wait_us.exchange(0); + const uint64_t gpu_wait_us = g_gpu_wait_us.exchange(0); + const uint64_t image_wait_us = g_output_image_wait_us.exchange(0); + const uint64_t record_commands_us = g_record_commands_us.exchange(0); + const uint64_t output_callback_us = g_output_callback_us.exchange(0); + const uint64_t slot_wait_us = g_title_slot_wait_us.exchange(0), pacer_us = g_pacer_wait_us.exchange(0); + const uint64_t guest_wait_us = uint64_t(double(g_title_guest_wait_ticks.exchange(0)) * 1e6 / + double(rex::chrono::Clock::QueryHostTickFrequency())); + const uint64_t main_wait_us = uint64_t(double(g_main_guest_wait_ticks.exchange(0)) * 1e6 / + double(rex::chrono::Clock::QueryHostTickFrequency())); + if (last_ns) { + const double real = double(now_ns - last_ns) / 1e9; + const double guest_s = double(guest - last_guest) / double(rex::chrono::Clock::guest_tick_frequency()); + const uint64_t frames = presents - last_presents; + auto per_frame = [frames](uint64_t us) { return frames ? double(us) / 1000.0 / double(frames) : 0.0; }; + const double frame_ms = frames ? real * 1000.0 / double(frames) : 0.0; + // The game's own work per frame is its frame time less its waits for + // the renderer; the recorder's is its time less the frame limit's hold. + REXLOG_INFO("ps5 timing: real {:.2f}s, guest clock {:.2f}s, game step {:.1f} ms, frame {:.1f} ms " + "(TV {:.1f} fps); " + "game {:.1f} ms of work (+{:.1f} waiting for the renderer, {:.1f} waiting on other " + "threads), game's main thread {:.1f} ms of work (+{:.1f} waiting), recorder {:.1f} ms (worst {:.1f}, " + "waiting for the GPU {:.1f}, for its output image {:.1f}; recording {:.1f}, the rest " + "of the output step {:.1f}), held for the frame limit " + "{:.1f} ms, worker waiting on the recorder {:.1f} ms", + real, guest_s, step * 1000.0, frame_ms, double(shown - last_shown) / real, + std::max(0.0, frame_ms - per_frame(slot_wait_us) - per_frame(guest_wait_us) - + per_frame(pacer_us)), + per_frame(slot_wait_us), per_frame(guest_wait_us), + std::max(0.0, frame_ms - per_frame(main_wait_us)), per_frame(main_wait_us), + rec_n ? double(rec_us) / 1000.0 / double(rec_n) : 0.0, double(rec_max) / 1000.0, + rec_n ? double(gpu_wait_us) / 1000.0 / double(rec_n) : 0.0, + rec_n ? double(image_wait_us) / 1000.0 / double(rec_n) : 0.0, + rec_n ? double(record_commands_us) / 1000.0 / double(rec_n) : 0.0, + rec_n ? double(output_callback_us > record_commands_us ? output_callback_us - record_commands_us : 0) / + 1000.0 / double(rec_n) + : 0.0, + per_frame(pacer_us), + per_frame(wait_us)); + if (g_frame_generation_active.load()) { + const uint64_t pairs = g_fg_pairs.exchange(0), alone = g_fg_unpaired.exchange(0); + // (why scenes went out alone, by the first gate each failed) + std::string dropped; + for (uint32_t i = 0; i < uint32_t(FgDrop::kCount); ++i) + if (const uint32_t n = g_fg_drop[i].exchange(0)) + dropped += fmt::format("{}{} {}", dropped.empty() ? "" : ", ", n, kFgDropNames[i]); + REXLOG_INFO("ps5 frame generation: {:.1f} pairs a second (generated + real), {:.1f} real frames " + "alone; each image at least {} vblank{}; {} pairs made; alone because: {}; {} skips in " + "the game's frame numbers", + double(pairs) / real, double(alone) / real, g_fg_vblanks_per_image.load(), + g_fg_vblanks_per_image.load() == 1 ? "" : "s", g_fg_published.exchange(0), + dropped.empty() ? "nothing" : dropped, g_fg_sequence_gaps.exchange(0)); + } + const uint64_t kept = g_renderer_cpu_mask.load(); + const char* layout = kept == kOwnCoresLayout ? "own cores, recycling near" + : kept == kOwnCoresApartLayout ? "own cores, recycling anywhere" + : kept == kFirstClusterCpus ? "together" + : "anywhere"; + const bool lean = g_lean_capture.load(); + const bool prefetch = g_capture_prefetch.load(); + const bool main_core = IsOwnCoresLayout(kept) && g_main_core.load(); + const bool on_core = g_capture_on_core.load(); + const uint64_t ring_wait_us = g_capture_ring_wait_us.exchange(0); + REXLOG_INFO("ps5 cpus: game {:#x}, worker {:#x}, recorder {:#x}, capture {:#x} ({}{}{}; {} capture{}{}{}; " + "the game waited {:.1f} ms on the capture thread)", + g_cpus_title.exchange(0), g_cpus_worker.exchange(0), g_cpus_recorder.exchange(0), + g_cpus_capture.exchange(0), layout, main_core ? ", main thread on its own core" : "", + g_renderer_cpu_compare.load() ? ", comparing" : "", + lean ? "lean" : "classic", on_core ? " on its own core" : "", + lean && !prefetch ? " without prefetch" : "", + g_capture_core_compare.load() ? ", comparing" : "", double(ring_wait_us) / 1000.0); + // Game Thread Capture on Compare: own core and lean in turn, 10 s each. + if (g_capture_core_compare.load()) g_capture_on_core.store(!on_core); + // Renderer Threads on Compare: the other layout for the next 10 s. + uint64_t next = kept; + if (g_renderer_cpu_compare.load()) { + next = kept == kOwnCoresLayout ? kOwnCoresApartLayout : kOwnCoresLayout; + g_renderer_cpu_mask.store(next); + } + // The game's other threads leave the renderer's cores (or get them + // back), every 10 s so threads started since are moved too (a thread + // already on its CPUs is left alone). + static bool others_moved = false; + if (IsOwnCoresLayout(next) || others_moved) { + others_moved = IsOwnCoresLayout(next); + rex::ps5diag::KeepOtherThreadsOn(RendererRoleMask(next, RendererRole::kOther), + g_title_thread.load(), g_game_main_thread.load(), + RendererRoleMask(next, RendererRole::kMain)); + } + } + last_guest = guest; + last_presents = presents; + last_shown = shown; + last_ns = now_ns; + } + REXLOG_INFO( + "ps5 diag {}s: faults {} (mmio {}, watch {}, unhandled {}), vblanks {}, interrupts {}, " + "ring writes {}, ring batches {}", + tick * 10, counters[kAccessViolations].load(), counters[kMmioAccesses].load(), + counters[kWatchFaults].load(), counters[kUnhandledFaults].load(), counters[kVblanks].load(), + counters[kInterrupts].load(), counters[kWritePointer].load(), + counters[kPrimaryBuffers].load()); + // The game stopped presenting frames (the native renderer doesn't use the + // GPU ring, so frames are what show it): guest threads stuck in one kernel + // wait for over 5 s, and where every thread is. In play, threads waiting + // on idle job queues are normal and not listed. + static uint64_t last_presents_seen = 0, stuck_ticks = 0, samples = 0; + const uint64_t presents_now = rex::frame_stats::g_presents.load(); + stuck_ticks = presents_now == last_presents_seen ? stuck_ticks + 1 : 0; + last_presents_seen = presents_now; + const uint64_t now = NowMs(); + for (WaitSlot& w : wait_slots) { + if (!stuck_ticks) break; + uint64_t since = w.since_ms.load(std::memory_order_acquire); + if (since && since < now && now - since > 5000) { + REXLOG_INFO("ps5 diag: guest thread {:X} waiting {}s on {} {} handle {:08X}{} from game code {:08X}", + w.thread_id, (now - since) / 1000, w.count > 1 ? w.count : 1, + ObjectTypeName(w.object_type), w.handle, w.count > 1 ? " (first of several)" : "", + w.guest_lr); + // its callers: each frame's back chain at 0(r1), the caller's LR + // saved at -8 from the caller's frame (mflr r12; stw r12,-8(r1); + // stwu ...) - read only where the guest memory is committed + if (auto* kernel = rex::system::kernel_state(); kernel && w.guest_sp && w.guest_stack_lo) { + auto* memory = kernel->memory(); + auto readable = [&](uint32_t a) { + uint32_t protect = 0; + auto* heap = memory->LookupHeap(a); + return heap && heap->QueryProtect(a, &protect) && (protect & rex::memory::kMemoryProtectRead); + }; + std::string callers; + uint32_t sp = w.guest_sp; + for (int i = 0; i < 8; ++i) { + if (sp < w.guest_stack_lo || sp + 4 > w.guest_stack_hi || (sp & 3) || !readable(sp)) break; + const uint32_t back = rex::memory::load_and_swap<uint32_t>(memory->TranslateVirtual(sp)); + if (back <= sp || back < w.guest_stack_lo + 8 || back > w.guest_stack_hi || !readable(back - 8)) break; + callers += fmt::format(" < {:08X}", + rex::memory::load_and_swap<uint32_t>(memory->TranslateVirtual(back - 8))); + sp = back; + } + if (!callers.empty()) REXLOG_INFO("ps5 diag: called from{}", callers); + } + if (const auto* cs = static_cast<const uint8_t*>(w.critical_section)) { + auto be32 = [cs](int off) { + return uint32_t(cs[off]) << 24 | uint32_t(cs[off + 1]) << 16 | + uint32_t(cs[off + 2]) << 8 | uint32_t(cs[off + 3]); + }; + auto le32 = [cs](int off) { + return uint32_t(cs[off]) | uint32_t(cs[off + 1]) << 8 | uint32_t(cs[off + 2]) << 16 | + uint32_t(cs[off + 3]) << 24; + }; + std::string raw; + for (int b = 0; b < 28; ++b) raw += fmt::format("{:02X}{}", cs[b], b % 4 == 3 ? " " : ""); + REXLOG_INFO("ps5 diag: critical section {:08X} entered from {:08X}: lock count {}, recursion {}, " + "held by kthread {:08X}; initialized {}; bytes {}", + w.critical_section_guest, w.critical_section_lr, int32_t(le32(0x10)), + int32_t(be32(0x14)), be32(0x18), + CriticalSectionInitBy(w.critical_section_guest) + ? fmt::format("by game code {:08X}", CriticalSectionInitBy(w.critical_section_guest)) + : std::string("never (by RtlInitializeCriticalSection)"), + raw); + } + } + } + if ((stuck_ticks == 3 || stuck_ticks == 6) && samples < 2 && REXCVAR_GET(ps5_hang_reports)) { + ++samples; + SampleAllThreads(); + } + } +} + +int main(int argc, char* argv[]) { + chdir("/app0"); + rex::ps5diag::RegisterThread("main"); + // A thread that crashed (it would retry its faulting instruction forever, + // behind a frozen loading screen): say so and close the game. + // (It runs in the fault handler, maybe inside the allocator: no logging, + // which allocates. The crash line is already at the end of the log file.) + rex::ps5diag::crash_hook = [](const char*) { + Ps5Notify("The game crashed - send /app0/logs to the developer"); + spdlog::apply_all([](const std::shared_ptr<spdlog::logger>& l) { l->flush(); }); + sleep(2); + _exit(1); + }; + // EXIT in the game's main menu: start the app again, into its hub. + rex::ps5diag::title_exit_hook = [] { + REXLOG_INFO("back to the hub (restarting the app)"); + spdlog::apply_all([](const std::shared_ptr<spdlog::logger>& l) { l->flush(); }); + char* args[] = {nullptr}; + const int rc = sceSystemServiceLoadExec("/app0/eboot.bin", args); + REXLOG_ERROR("restarting failed ({:#x}) - closing", uint32_t(rc)); + spdlog::apply_all([](const std::shared_ptr<spdlog::logger>& l) { l->flush(); }); + _exit(0); + }; + std::set_terminate(Ps5Terminate); + std::thread(Ps5DiagMonitor).detach(); + std::thread(Ps5Profiler).detach(); + std::thread(Ps5HitchWatch).detach(); + int result = RunWindowedApp(argc, argv); + if (result != 0) { + Ps5Notify("Stopped with an error - see /app0/logs"); + sleep(2); // (let the notification show) + } + // Leave without the C runtime's exit handlers (with the game's threads + // still around, they end in a crash report). + _exit(result); +} + +#else + int main(int argc, char* argv[]) { return RunWindowedApp(argc, argv); } +#endif // REX_PLATFORM_PS5 #endif diff --git a/glue/rexglue-sdk-main/tests/unit/system/gta4_frontend_menu_policy_test.cpp b/glue/rexglue-sdk-main/tests/unit/system/gta4_frontend_menu_policy_test.cpp index dc0621c2..0dab989c 100644 --- a/glue/rexglue-sdk-main/tests/unit/system/gta4_frontend_menu_policy_test.cpp +++ b/glue/rexglue-sdk-main/tests/unit/system/gta4_frontend_menu_policy_test.cpp @@ -135,15 +135,24 @@ TEST_CASE("GTA IV frontend restores primary only when advanced leaves Display", TEST_CASE("GTA IV pause tabs advance, reverse and wrap", "[system][gta4][frontend][input]") { uint32_t target = 0; - CHECK(policy::ResolvePauseTab(3, policy::PauseTabDirection::kPrevious, target)); - CHECK(target == 9); - CHECK(policy::ResolvePauseTab(9, policy::PauseTabDirection::kNext, target)); + // retail order: 3, 2, 4, 5, 7, 8, 0 (network: 3, 2, 5, 7, 8, 1) + CHECK(policy::ResolvePauseTab(3, policy::PauseTabDirection::kPrevious, false, target)); + CHECK(target == 0); + CHECK(policy::ResolvePauseTab(0, policy::PauseTabDirection::kNext, false, target)); CHECK(target == 3); - CHECK(policy::ResolvePauseTab(7, policy::PauseTabDirection::kPrevious, target)); - CHECK(target == 6); - CHECK(policy::ResolvePauseTab(7, policy::PauseTabDirection::kNext, target)); + CHECK(policy::ResolvePauseTab(3, policy::PauseTabDirection::kNext, false, target)); + CHECK(target == 2); + CHECK(policy::ResolvePauseTab(7, policy::PauseTabDirection::kPrevious, false, target)); + CHECK(target == 5); + CHECK(policy::ResolvePauseTab(7, policy::PauseTabDirection::kNext, false, target)); CHECK(target == 8); - CHECK_FALSE(policy::ResolvePauseTab(0, policy::PauseTabDirection::kNext, target)); + CHECK(policy::ResolvePauseTab(2, policy::PauseTabDirection::kNext, true, target)); + CHECK(target == 5); + CHECK(policy::ResolvePauseTab(3, policy::PauseTabDirection::kPrevious, true, target)); + CHECK(target == 1); + CHECK_FALSE(policy::ResolvePauseTab(9, policy::PauseTabDirection::kNext, false, target)); + CHECK_FALSE(policy::ResolvePauseTab(6, policy::PauseTabDirection::kNext, false, target)); + CHECK_FALSE(policy::ResolvePauseTab(4, policy::PauseTabDirection::kNext, true, target)); } TEST_CASE("GTA IV pause tab shoulders use rising edges", "[system][gta4][frontend][input]") { diff --git a/glue/rexglue-sdk-main/thirdparty/CMakeLists.txt b/glue/rexglue-sdk-main/thirdparty/CMakeLists.txt index d77082cc..60aaa0db 100644 --- a/glue/rexglue-sdk-main/thirdparty/CMakeLists.txt +++ b/glue/rexglue-sdk-main/thirdparty/CMakeLists.txt @@ -302,7 +302,7 @@ set(SDL_DUMMYAUDIO OFF CACHE BOOL "" FORCE) set(SDL_DUMMYVIDEO OFF CACHE BOOL "" FORCE) # Linux audio backends -if(UNIX AND NOT APPLE AND NOT ANDROID) +if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT PS5) set(SDL_ALSA ON CACHE BOOL "" FORCE) set(SDL_ALSA_SHARED ON CACHE BOOL "" FORCE) set(SDL_PULSEAUDIO ON CACHE BOOL "" FORCE) diff --git a/glue/rexglue-sdk-main/thirdparty/crypto/sha256.cpp b/glue/rexglue-sdk-main/thirdparty/crypto/sha256.cpp index e96b4da1..df468e8a 100644 --- a/glue/rexglue-sdk-main/thirdparty/crypto/sha256.cpp +++ b/glue/rexglue-sdk-main/thirdparty/crypto/sha256.cpp @@ -14,6 +14,11 @@ #define __BYTE_ORDER __BYTE_ORDER__ #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__ #endif +#elif defined(__PROSPERO__) // [ps5 port] no <endian.h> +#if !defined(__BYTE_ORDER) && defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) +#define __BYTE_ORDER __BYTE_ORDER__ +#define __BIG_ENDIAN __ORDER_BIG_ENDIAN__ +#endif #else #include <endian.h> #endif diff --git a/glue/rexglue-sdk-main/thirdparty/renderdoc/renderdoc_app.h b/glue/rexglue-sdk-main/thirdparty/renderdoc/renderdoc_app.h index da323e9a..2d51df86 100644 --- a/glue/rexglue-sdk-main/thirdparty/renderdoc/renderdoc_app.h +++ b/glue/rexglue-sdk-main/thirdparty/renderdoc/renderdoc_app.h @@ -30,7 +30,7 @@ #if defined(WIN32) || defined(_WIN32) #define RENDERDOC_CC __cdecl -#elif defined(__linux__) +#elif defined(__linux__) || defined(__PROSPERO__) // [ps5 port] #define RENDERDOC_CC #else #error "Unknown platform"