Compare commits
33
Commits
v1.3.1
..
07a26b41fe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07a26b41fe | ||
|
|
3ea285e407 | ||
|
|
26e84569c2 | ||
|
|
faf8a921c2 | ||
|
|
0e98e91762 | ||
|
|
247e2a90b6 | ||
|
|
5c4f876411 | ||
|
|
aa1b43e8fa | ||
|
|
9e7185fde5 | ||
|
|
56d0b852e7 | ||
|
|
7df2c31060 | ||
|
|
8c96f7ac91 | ||
|
|
a1853f2dc5 | ||
|
|
428772eb87 | ||
|
|
6bc2c28856 | ||
|
|
704ea0897b | ||
|
|
c6859e6b02 | ||
|
|
afef6d2c52 | ||
|
|
ab7e815a4d | ||
|
|
b634326b5d | ||
|
|
cf50beafcb | ||
|
|
c8676d7aa3 | ||
|
|
b619dee523 | ||
|
|
7da3a5eb95 | ||
|
|
128286153e | ||
|
|
85105f21d4 | ||
|
|
329445ed25 | ||
|
|
887b3e317f | ||
|
|
35536b0bd4 | ||
|
|
9b41fedb14 | ||
|
|
65a08e544d | ||
|
|
7031b43284 | ||
|
|
aa3a519f79 |
No files matched your search
@@ -1,123 +0,0 @@
|
||||
|
||||
name: ci
|
||||
|
||||
# Build and test on every push and pull request.
|
||||
# Upload unpacked files as Actions artifacts.
|
||||
# Create ZIP archives only when publishing a release.
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
label: linux
|
||||
- os: macos-latest
|
||||
label: macos
|
||||
- os: windows-latest
|
||||
label: windows
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Check the tag against the declared version
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
shell: bash
|
||||
run: |
|
||||
declared=$(sed -n 's/.*JOC_VERSION_STRING *"\([^"]*\)".*/\1/p' include/joc_version.h)
|
||||
if [ -z "$declared" ] || [ "$GITHUB_REF_NAME" != "v$declared" ]; then
|
||||
echo "the tag $GITHUB_REF_NAME does not match JOC_VERSION_STRING (${declared:-unset})" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Configure
|
||||
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release --parallel
|
||||
|
||||
- name: Test
|
||||
run: ctest --test-dir build --output-on-failure -C Release
|
||||
|
||||
- name: Stage
|
||||
run: cmake --install build --config Release --prefix stage
|
||||
|
||||
# Place executables at the package root and libraries in lib/.
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p package/lib
|
||||
cp -r stage/bin/. package/
|
||||
cp -r stage/lib/. package/lib/
|
||||
|
||||
# GitHub automatically creates the artifact ZIP.
|
||||
# Upload files directly to avoid nested ZIP archives.
|
||||
- name: Upload
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: joc_cli_${{ matrix.label }}
|
||||
path: package/
|
||||
if-no-files-found: error
|
||||
|
||||
release:
|
||||
# Only tagged builds create releases.
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
steps:
|
||||
# Keep each platform's files in a separate directory.
|
||||
- name: Collect build artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
pattern: joc_cli_*
|
||||
path: dist
|
||||
merge-multiple: false
|
||||
|
||||
# Create flat ZIP archives from the artifact contents.
|
||||
# Restore executable permissions on Unix platforms.
|
||||
- name: Create release archives
|
||||
shell: bash
|
||||
run: |
|
||||
for platform in linux macos windows; do
|
||||
dir="dist/joc_cli_${platform}"
|
||||
|
||||
if [ ! -d "$dir" ]; then
|
||||
echo "Missing artifact: joc_cli_${platform}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$platform" != "windows" ]; then
|
||||
chmod +x "$dir/joc_cli"
|
||||
fi
|
||||
|
||||
(
|
||||
cd "$dir"
|
||||
cmake -E tar cf "../joc_cli_${platform}.zip" --format=zip -- *
|
||||
)
|
||||
done
|
||||
|
||||
- name: List release archives
|
||||
run: ls -lh dist/*.zip
|
||||
|
||||
# Publish the archives directly to GitHub Releases.
|
||||
- name: Publish
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1 \
|
||||
|| gh release create "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" \
|
||||
--title "$GITHUB_REF_NAME" --generate-notes
|
||||
gh release upload "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" \
|
||||
--clobber dist/*.zip
|
||||
+20
-11
@@ -1,12 +1,21 @@
|
||||
/build/
|
||||
/output/
|
||||
/out/
|
||||
/traces/
|
||||
/vectors/
|
||||
/testdata/
|
||||
/devtools/
|
||||
.vs/
|
||||
.vscode/
|
||||
CMakeUserPresets.json
|
||||
__pycache__/
|
||||
*.spool.f32
|
||||
*.py[cod]
|
||||
.pytest_cache/
|
||||
|
||||
.venv/
|
||||
venv/
|
||||
|
||||
output/
|
||||
tests/
|
||||
metadata_cache/
|
||||
|
||||
*.metadata.json
|
||||
*.report.json
|
||||
*.variant-error.json
|
||||
*.objects16.f32le
|
||||
|
||||
HRTF/
|
||||
|
||||
*.sofa
|
||||
*.personalized_headphone
|
||||
*.jochrtf
|
||||
-386
@@ -1,386 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
# The product version lives in include/joc_version.h so that the CMake build, the
|
||||
# MSBuild kernel the foobar2000 component vendors and the release workflow all read
|
||||
# the same numbers instead of keeping a copy each; parse it rather than repeat it.
|
||||
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/joc_version.h" JOC_VERSION_LINES
|
||||
REGEX "#define JOC_VERSION_(MAJOR|MINOR|PATCH) ")
|
||||
foreach(_joc_part MAJOR MINOR PATCH)
|
||||
foreach(_joc_line IN LISTS JOC_VERSION_LINES)
|
||||
if(_joc_line MATCHES "#define JOC_VERSION_${_joc_part} ([0-9]+)")
|
||||
set(JOC_VERSION_${_joc_part} "${CMAKE_MATCH_1}")
|
||||
endif()
|
||||
endforeach()
|
||||
endforeach()
|
||||
if(NOT JOC_VERSION_MAJOR MATCHES "^[0-9]+$" OR NOT JOC_VERSION_MINOR MATCHES "^[0-9]+$"
|
||||
OR NOT JOC_VERSION_PATCH MATCHES "^[0-9]+$")
|
||||
message(FATAL_ERROR "include/joc_version.h does not declare a numeric version")
|
||||
endif()
|
||||
project(joc_core VERSION "${JOC_VERSION_MAJOR}.${JOC_VERSION_MINOR}.${JOC_VERSION_PATCH}"
|
||||
LANGUAGES C CXX)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Products
|
||||
# joc_core shared library: the execution core (E-AC-3/EMDF/JOC/OAMD
|
||||
# bitstream, DSP, speaker and binaural rendering, ADM-BWF/WAV
|
||||
# writing, file task and the streaming push/pull surface)
|
||||
# joc_cli command line frontend for file tasks
|
||||
#
|
||||
# Public headers: include/joc_core.h engine, telemetry and file task
|
||||
# include/joc_stream.h embedder-facing streaming surface
|
||||
#
|
||||
# Options
|
||||
# JOC_BUILD_TESTS unit tests (CTest), on by default
|
||||
# JOC_BUILD_DEVTOOLS in-tree verification tools, off: those sources live in
|
||||
# devtools/, which is not part of the repository
|
||||
# JOC_ENABLE_AVX2 build the runtime-dispatched AVX2 kernels, on by default
|
||||
# JOC_ENABLE_AVX512 build the runtime-dispatched AVX-512 kernels, on by default
|
||||
#
|
||||
# Only the MSVC toolchain is validated locally; other platforms are built by CI.
|
||||
# The floating-point flags of the original native library are preserved on
|
||||
# purpose: /fp:precise on MSVC, -fno-fast-math elsewhere, and a static CRT so
|
||||
# that no redistributable is required.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
option(JOC_BUILD_TESTS "Build the unit tests" ON)
|
||||
option(JOC_BUILD_DEVTOOLS "Build the in-tree verification tools (needs devtools/)" OFF)
|
||||
# The DSP kernels are dispatched at run time (see src/simd/simd.h): the
|
||||
# baseline units stay on the ISA every x86-64 CPU has, and these two options
|
||||
# decide whether the wider units are linked in at all. Both are on by default.
|
||||
# The instruction set actually executed is chosen from CPUID/XGETBV (or the
|
||||
# AArch64 baseline) when the library is first used, so a binary carrying the
|
||||
# AVX-512 unit still runs on a CPU without it, and JOC_SIMD=scalar|sse2|avx2|
|
||||
# avx512|neon pins one tier for verification.
|
||||
option(JOC_ENABLE_AVX2 "Build the runtime-dispatched AVX2 kernels" ON)
|
||||
option(JOC_ENABLE_AVX512 "Build the runtime-dispatched AVX-512 kernels" ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
|
||||
endif()
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
# Ninja learns MSVC's header dependencies by parsing the compiler's /showIncludes
|
||||
# notes, and it only recognises the prefix it is told about. A localized MSVC
|
||||
# prints a translated prefix, and when CMake cannot detect it the notes are
|
||||
# silently dropped: the build then reuses stale objects after a header changes and
|
||||
# produces a binary that does not match its sources -- wrong, not just slow. This
|
||||
# only warns, because supplying the value is not always possible either: it has to
|
||||
# survive the cache code page to be usable, and a build driver can compensate more
|
||||
# reliably by dropping objects when a header is newer than they are.
|
||||
if(MSVC AND CMAKE_GENERATOR MATCHES "Ninja" AND NOT CMAKE_CL_SHOWINCLUDES_PREFIX)
|
||||
message(WARNING
|
||||
"No /showIncludes prefix was detected, so Ninja will not track header "
|
||||
"dependencies and a header change will not rebuild what includes it. "
|
||||
"Configure with -DCMAKE_CL_SHOWINCLUDES_PREFIX=<the text MSVC prints in "
|
||||
"front of each included file>, or make sure the compiler emits its "
|
||||
"messages in the language CMake probes for.")
|
||||
endif()
|
||||
|
||||
# Sources copied verbatim from the upstream native library; see
|
||||
# THIRD_PARTY_NOTICES.md. binaural_renderer.cpp is still an unmodified copy and
|
||||
# is never edited; the unchanged reused headers include/eac3joc_core.h and
|
||||
# src/joc_bitstream/joc_huffman_tables.h belong to the same set. The JOC core,
|
||||
# the speaker renderer and the remaining tables started as copies and are
|
||||
# maintained in-tree now, so they are listed as internal sources below.
|
||||
set(JOC_REUSED_SOURCES
|
||||
src/binaural/binaural_renderer.cpp
|
||||
)
|
||||
|
||||
set(JOC_INTERNAL_SOURCES
|
||||
src/adm/adm_metadata.cpp
|
||||
src/adm/adm_tracks.cpp
|
||||
src/binaural/jochrtf_runtime.cpp
|
||||
src/binaural/jochrtf_renderer.cpp
|
||||
src/eac3_transport/eac3_reader.cpp
|
||||
src/emdf/emdf_parser.cpp
|
||||
src/foundation/bit_reader.cpp
|
||||
src/foundation/fft.cpp
|
||||
src/foundation/fs_utf8.cpp
|
||||
src/foundation/mini_json.cpp
|
||||
src/foundation/sha256.cpp
|
||||
src/foundation/status.cpp
|
||||
src/hrtf/jochrtf.cpp
|
||||
src/hrtf/public_filterbank.cpp
|
||||
src/hrtf/rosella_bank.cpp
|
||||
src/hrtf/rosella_filterbank.cpp
|
||||
src/hrtf/rosella_model.cpp
|
||||
src/hrtf/rosella_renderer.cpp
|
||||
src/hrtf/kernel_tables.cpp
|
||||
src/io/adm_writer.cpp
|
||||
src/io/hdf5.cpp
|
||||
src/io/inflate.cpp
|
||||
src/io/npy.cpp
|
||||
src/io/npy_writer.cpp
|
||||
src/io/process.cpp
|
||||
src/io/wav_writer.cpp
|
||||
src/io/zip_reader.cpp
|
||||
src/joc_bitstream/bed_layout_map.cpp
|
||||
src/joc_bitstream/joc_parser.cpp
|
||||
src/joc_core/eac3joc_core.cpp
|
||||
src/joc_core/objects16.cpp
|
||||
src/oamd/oamd_math.cpp
|
||||
src/oamd/oamd_params.cpp
|
||||
src/oamd/oamd_parser.cpp
|
||||
src/speaker/speaker_renderer.cpp
|
||||
src/speaker/speaker_layout_lookup.cpp
|
||||
src/speaker/speaker_step.cpp
|
||||
src/speaker/speaker_trim.cpp
|
||||
src/stream/stream.cpp
|
||||
src/task/task.cpp
|
||||
src/telemetry/event_bus.cpp
|
||||
src/timeline/position_timeline.cpp
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Runtime-dispatched SIMD kernels (src/simd/simd.h explains the contract).
|
||||
#
|
||||
# One flat directory, the ISA in the file name (`kernels_intrin_<isa>.cpp`),
|
||||
# never in a subdirectory. MSVC has no function-level ISA attribute, so every
|
||||
# ISA lives in its own translation unit compiled with its own flag, and
|
||||
# dispatch.cpp -- built for the baseline ISA -- picks one when the library is
|
||||
# first used. Only a `kernels_intrin_*.cpp` unit ever gets a wider flag, so a
|
||||
# baseline unit cannot inherit one by accident.
|
||||
#
|
||||
# The x86-64 baseline is SSE2 and there is no SSE2 unit on purpose: a 128-bit
|
||||
# SSE2 register is the register a scalar double already occupies, so SSE2 cannot
|
||||
# widen double-precision arithmetic and hand-written SSE2 would only add moves.
|
||||
# 32-bit x86 does take the AVX2 unit: AVX2 is not an x86-64-only ISA, and it is
|
||||
# the only vector path such a build can have.
|
||||
# AArch64 needs no probe either; ASIMD is architectural, and the NEON unit is
|
||||
# how a vector path gets selected there.
|
||||
# ---------------------------------------------------------------------------
|
||||
set(JOC_SIMD_SOURCES
|
||||
src/simd/cpu_probe.cpp
|
||||
src/simd/dispatch.cpp
|
||||
src/simd/kernels_scalar.cpp
|
||||
)
|
||||
set(JOC_SIMD_DEFINES "")
|
||||
|
||||
set(JOC_ARCH "")
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64|EM64T)$")
|
||||
set(JOC_ARCH x86_64)
|
||||
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM64|arm64|aarch64|AARCH64)$")
|
||||
set(JOC_ARCH aarch64)
|
||||
endif()
|
||||
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|x86|X86|IA32)$")
|
||||
set(JOC_ARCH x86_32)
|
||||
endif()
|
||||
endif()
|
||||
if(NOT JOC_ARCH AND DEFINED CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
|
||||
if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "x64")
|
||||
set(JOC_ARCH x86_64)
|
||||
elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "ARM64")
|
||||
set(JOC_ARCH aarch64)
|
||||
elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "X86")
|
||||
set(JOC_ARCH x86_32)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(JOC_ARCH STREQUAL "x86_64")
|
||||
list(APPEND JOC_SIMD_DEFINES JOC_SIMD_HAVE_SSE2=1)
|
||||
if(JOC_ENABLE_AVX2)
|
||||
list(APPEND JOC_SIMD_SOURCES src/simd/kernels_intrin_avx2.cpp)
|
||||
list(APPEND JOC_SIMD_DEFINES JOC_SIMD_HAVE_AVX2=1)
|
||||
if(MSVC)
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx2.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
else()
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx2.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "-mavx2")
|
||||
endif()
|
||||
endif()
|
||||
if(JOC_ENABLE_AVX512)
|
||||
list(APPEND JOC_SIMD_SOURCES src/simd/kernels_intrin_avx512.cpp)
|
||||
list(APPEND JOC_SIMD_DEFINES JOC_SIMD_HAVE_AVX512=1)
|
||||
if(MSVC)
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx512.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
|
||||
else()
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx512.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "-mavx512f")
|
||||
endif()
|
||||
endif()
|
||||
elseif(JOC_ARCH STREQUAL "x86_32")
|
||||
# AVX2 only: 32-bit mode addresses ZMM0-7, so the AVX-512 unit is not a fit.
|
||||
if(JOC_ENABLE_AVX2)
|
||||
list(APPEND JOC_SIMD_SOURCES src/simd/kernels_intrin_avx2.cpp)
|
||||
list(APPEND JOC_SIMD_DEFINES JOC_SIMD_HAVE_AVX2=1)
|
||||
if(MSVC)
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx2.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
else()
|
||||
set_source_files_properties(src/simd/kernels_intrin_avx2.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "-mavx2")
|
||||
endif()
|
||||
endif()
|
||||
elseif(JOC_ARCH STREQUAL "aarch64")
|
||||
list(APPEND JOC_SIMD_DEFINES JOC_SIMD_HAVE_NEON=1)
|
||||
list(APPEND JOC_SIMD_SOURCES src/simd/kernels_intrin_neon.cpp)
|
||||
endif()
|
||||
|
||||
add_library(joc_simd OBJECT ${JOC_SIMD_SOURCES})
|
||||
target_include_directories(joc_simd PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
target_compile_definitions(joc_simd PRIVATE ${JOC_SIMD_DEFINES})
|
||||
# The objects are linked into the shared library as well, so they must be
|
||||
# position independent even though an object library does not inherit that.
|
||||
set_target_properties(joc_simd PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
# Static form of the engine, used by the in-tree tools and tests so they can use
|
||||
# internal modules without exporting them from the shared library.
|
||||
if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS)
|
||||
add_library(joc_core_impl STATIC ${JOC_INTERNAL_SOURCES} ${JOC_REUSED_SOURCES}
|
||||
$<TARGET_OBJECTS:joc_simd>)
|
||||
target_include_directories(joc_core_impl
|
||||
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src"
|
||||
)
|
||||
target_link_libraries(joc_core_impl PUBLIC Threads::Threads)
|
||||
# The same sources back both the shared library and this static archive. In
|
||||
# the archive the C entry points are plain definitions: EJOC_STATIC stops the
|
||||
# kernel sources that carry their own EJOC_BUILD_DLL define from marking them
|
||||
# as dllexports, which would collide with the DLL import library in a link
|
||||
# that uses both libraries (the tests do).
|
||||
target_compile_definitions(joc_core_impl PUBLIC EJOC_STATIC)
|
||||
endif()
|
||||
|
||||
add_library(joc_core SHARED
|
||||
src/api/joc_api.cpp
|
||||
src/api/joc_stream_api.cpp
|
||||
src/api/joc_task_api.cpp
|
||||
${JOC_INTERNAL_SOURCES}
|
||||
${JOC_REUSED_SOURCES}
|
||||
$<TARGET_OBJECTS:joc_simd>
|
||||
)
|
||||
target_include_directories(joc_core
|
||||
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src"
|
||||
)
|
||||
target_compile_definitions(joc_core PRIVATE JOC_BUILD_DLL)
|
||||
target_link_libraries(joc_core PRIVATE Threads::Threads)
|
||||
set_target_properties(joc_core PROPERTIES
|
||||
OUTPUT_NAME "joc_core"
|
||||
CXX_VISIBILITY_PRESET hidden
|
||||
VISIBILITY_INLINES_HIDDEN YES
|
||||
POSITION_INDEPENDENT_CODE YES
|
||||
)
|
||||
|
||||
# The frontend compiles the UTF-8 path shim itself: it is small, and the shared
|
||||
# library keeps its internals unexported.
|
||||
add_executable(joc_cli src/cli/joc_cli.cpp src/foundation/fs_utf8.cpp)
|
||||
target_link_libraries(joc_cli PRIVATE joc_core)
|
||||
target_include_directories(joc_cli PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
|
||||
# The install tree keeps the CLI in bin/ with the shared library next to it in
|
||||
# lib/, while the packaged archive puts the CLI at its root with the same lib/
|
||||
# below. ELF and Mach-O strip the build rpath on install, so both relative
|
||||
# lookups are recorded here or the CLI cannot find the library in one of them.
|
||||
if(UNIX)
|
||||
if(APPLE)
|
||||
set_target_properties(joc_cli PROPERTIES
|
||||
INSTALL_RPATH "@loader_path/lib;@loader_path/../lib")
|
||||
else()
|
||||
set_target_properties(joc_cli PROPERTIES
|
||||
INSTALL_RPATH "$ORIGIN/lib;$ORIGIN/../lib")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(JOC_BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_executable(joc_tests tests/test_core.cpp)
|
||||
# The stream tests reach the core the shared library exports, so the static
|
||||
# library alone is not enough to link them.
|
||||
target_link_libraries(joc_tests PRIVATE joc_core_impl joc_core)
|
||||
target_include_directories(joc_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
add_test(NAME core COMMAND joc_tests)
|
||||
add_executable(joc_rosella_filterbank_tests tests/test_rosella_filterbank.cpp)
|
||||
target_link_libraries(joc_rosella_filterbank_tests PRIVATE joc_core_impl)
|
||||
target_include_directories(joc_rosella_filterbank_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
add_test(NAME rosella_filterbank COMMAND joc_rosella_filterbank_tests)
|
||||
add_executable(joc_oamd_metadata_tests tests/test_oamd_metadata.cpp)
|
||||
target_link_libraries(joc_oamd_metadata_tests PRIVATE joc_core_impl)
|
||||
target_include_directories(joc_oamd_metadata_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
add_test(NAME oamd_metadata COMMAND joc_oamd_metadata_tests)
|
||||
# The public headers must stay valid C and C++: these targets exist to prove it.
|
||||
add_library(joc_headers_c OBJECT tests/test_headers.c)
|
||||
target_include_directories(joc_headers_c PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
add_library(joc_headers_cpp OBJECT tests/test_headers.cpp)
|
||||
target_link_libraries(joc_headers_cpp PRIVATE joc_core)
|
||||
endif()
|
||||
|
||||
if(JOC_BUILD_DEVTOOLS)
|
||||
add_executable(joc_dump devtools/joc_dump/main.cpp)
|
||||
target_link_libraries(joc_dump PRIVATE joc_core joc_core_impl)
|
||||
target_include_directories(joc_dump PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
# Rosella model probe: the C++ side of devtools/checks/check_rosella.py.
|
||||
add_executable(joc_rosella_probe devtools/rosella_probe/main.cpp)
|
||||
target_link_libraries(joc_rosella_probe PRIVATE joc_core_impl)
|
||||
target_include_directories(joc_rosella_probe PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||
# Probe for the streaming surface: the acceptance harness and the usage
|
||||
# example for joc_stream.h. Not shipped: a player integrates the library.
|
||||
add_executable(joc_stream_probe devtools/joc_stream/main.cpp)
|
||||
target_link_libraries(joc_stream_probe PRIVATE joc_core)
|
||||
endif()
|
||||
|
||||
set(JOC_MSVC_TARGETS joc_core joc_cli joc_simd)
|
||||
if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS)
|
||||
list(APPEND JOC_MSVC_TARGETS joc_core_impl)
|
||||
endif()
|
||||
if(JOC_BUILD_TESTS)
|
||||
list(APPEND JOC_MSVC_TARGETS joc_tests joc_rosella_filterbank_tests
|
||||
joc_oamd_metadata_tests joc_headers_c joc_headers_cpp)
|
||||
endif()
|
||||
if(JOC_BUILD_DEVTOOLS)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
set(JOC_MSVC_FLAGS /W4 /permissive- /Zc:__cplusplus /utf-8 /EHsc /GR- /fp:precise)
|
||||
# The writers use std::fopen for seekable, byte-exact output.
|
||||
set(JOC_MSVC_DEFINES _CRT_SECURE_NO_WARNINGS)
|
||||
# C4324 comes from the reused kernel's std::barrier member at /W4; it is
|
||||
# pre-existing behaviour of a verbatim file, so the warning is silenced
|
||||
# rather than the file edited.
|
||||
set_source_files_properties(src/joc_core/eac3joc_core.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "/wd4324")
|
||||
foreach(target IN LISTS JOC_MSVC_TARGETS)
|
||||
set_property(TARGET ${target} PROPERTY
|
||||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
||||
# No /arch here on purpose: the baseline units keep the architecture's
|
||||
# guaranteed ISA and only the src/simd `kernels_intrin_*` units carry a
|
||||
# wider one (their flags are set per source file above).
|
||||
target_compile_options(${target} PRIVATE ${JOC_MSVC_FLAGS}
|
||||
$<$<CONFIG:Release>:/O2>
|
||||
$<$<CONFIG:Release>:/Oi>)
|
||||
target_compile_definitions(${target} PRIVATE ${JOC_MSVC_DEFINES})
|
||||
endforeach()
|
||||
target_link_options(joc_core PRIVATE /INCREMENTAL:NO /OPT:REF /OPT:ICF)
|
||||
target_link_options(joc_cli PRIVATE /INCREMENTAL:NO)
|
||||
else()
|
||||
foreach(target IN LISTS JOC_MSVC_TARGETS)
|
||||
target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -fno-fast-math
|
||||
$<$<CONFIG:Release>:-O3>)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# Headers listed for IDE visibility.
|
||||
target_sources(joc_core PRIVATE
|
||||
include/joc_core.h
|
||||
include/joc_stream.h
|
||||
include/eac3joc_core.h
|
||||
src/joc_bitstream/joc_huffman_tables.h
|
||||
src/joc_core/qmf_tables.h
|
||||
src/oamd/oamd_parser.h
|
||||
src/speaker/speaker_layouts.h
|
||||
)
|
||||
|
||||
install(TARGETS joc_core joc_cli
|
||||
RUNTIME DESTINATION bin
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib
|
||||
)
|
||||
+262
-260
@@ -1,119 +1,248 @@
|
||||
# JustOneCacophony — C++ Core
|
||||
# JustOneCacophony — JOC
|
||||
|
||||
[中文](README.md) · [Mathematics](docs/math.en.md) · [Binaural rendering](docs/binaural.en.md) · [SIMD dispatch](docs/simd.en.md)
|
||||
[中文版](README.md)
|
||||
|
||||
The C++ implementation of JustOneCacophony: an execution core for E-AC-3 JOC
|
||||
bitstream parsing, object reconstruction and rendering. It extracts EMDF, ID14 JOC
|
||||
parameters and ID11 OAMD metadata from E-AC-3 syncframes, combines them with the
|
||||
core 5.1 PCM decoded by FFmpeg to rebuild the LFE and 15 object signals, and writes
|
||||
ADM BWF, a WAV for a chosen speaker layout, or a binaural WAV using a compiled HRTF
|
||||
directional field.
|
||||
> JustOneCacophony is an experimental/test implementation of E-AC-3 JOC for studying JOC parsing, reconstruction, rendering, and the associated mathematics.
|
||||
|
||||
This is research code, not a complete, standard-conformant or production JOC
|
||||
decoder. It covers the bitstream forms it implements and reports an explicit error
|
||||
on unknown variants instead of pretending everything is in harmony.
|
||||
The project can extract and parse EMDF, ID14 JOC parameters, and ID11 OAMD metadata from common E-AC-3 JOC streams. It combines those data with the core PCM decoded by FFmpeg (N=5 or 7 full-band channels), reconstructs LFE plus 15 object channels, and writes ADM BWF, a WAV file for a selected speaker layout, or binaural stereo (a Rosella personalized model, or a compiled `.jochrtf`).
|
||||
|
||||
## Building
|
||||
This is research code, not a complete, standards-compliant, or production-grade JOC decoder. It covers only the stream forms currently implemented. Unknown variants fail explicitly—because when the math goes wrong, all that may remain is the cacophony.
|
||||
|
||||
```powershell
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
ctest --test-dir build --output-on-failure
|
||||
## Current features
|
||||
|
||||
- Scan common contiguous EMDF containers in E-AC-3 sync frames.
|
||||
- Parse ID14 dense / sparse JOC parameters, Huffman data, differential matrices, and `joc_clipgain`.
|
||||
- Parse ID11 OAMD position updates and build object trajectories.
|
||||
- Read the bed layout the OAMD declares (5.1, 7.1, 7.1.2, 7.1.4, 9.1.4, 9.1.6, ...): for
|
||||
bed-only content the 15 object slots are the bed channels, each rendered as a point
|
||||
source at the fixed position its channel label defines (ETSI TS 103 420 table B.10);
|
||||
speaker and binaural output share those source positions and never re-place them per
|
||||
layout, while LFE keeps its separate path.
|
||||
- Reconstruct LFE plus 15 object channels through analysis QMF, parameter interpolation, the object matrix, and inverse QMF.
|
||||
- Write a 25-channel ADM BWF: a 10-channel 7.1.2 bed (silent except for LFE) plus 15 objects.
|
||||
- Render directly to `2.0`, `3.1`, `4.0`, `5.1`, `7.1`, `5.1.2`, `5.1.4`, `7.1.2`, `7.1.4`, `9.1.4`
|
||||
or `9.1.6` (`4.0` is the quad layout, a front and a back pair; see
|
||||
[Speaker layouts and channel order](#speaker-layouts-and-channel-order)).
|
||||
- Run binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF.
|
||||
- Keep binaural DSP in float64/complex128: the personalized route retains its existing filterbank and latency rules, the `.jochrtf` route keeps its 77-band field and cross-frame state.
|
||||
- Use a shared float32/PCM24 WAV writer and explicit PCM24 clipping policy for direct outputs.
|
||||
- Implement everything in pure Python on NumPy;
|
||||
- Read or write metadata sidecars and produce metadata, timing, and output reports.
|
||||
|
||||
## Processing flow
|
||||
|
||||
```text
|
||||
M4A / E-AC-3
|
||||
├─ FFmpeg extracts E-AC-3 and decodes the core PCM (N=5 or 7)
|
||||
├─ EMDF → ID14 JOC parameters → object matrix
|
||||
├─ core PCM → analysis QMF → parameter interpolation → inverse QMF
|
||||
├─ ID11 OAMD → object positions and timing
|
||||
└─ LFE + 15 objects
|
||||
├─ 25ch ADM BWF
|
||||
├─ speaker WAV for the selected layout
|
||||
└─ direct ID11 timeline + HRTF (Rosella / .jochrtf) → binaural WAV
|
||||
```
|
||||
|
||||
Only MSVC (VS 2022, static CRT) is validated locally; Linux and macOS are built and
|
||||
unit-tested by `.github/workflows/ci.yml`. Floating-point behaviour is part of the
|
||||
byte-exact acceptance, so fast-math is never enabled: `/fp:precise` on MSVC,
|
||||
`-fno-fast-math` elsewhere.
|
||||
JOC object reconstruction, speaker rendering, and binaural rendering follow the same mathematics: analysis QMF, parameter interpolation, the object matrix, and inverse QMF. Bitstream parsing, the OAMD timeline, and CLI behavior are in Python as well.
|
||||
|
||||
The heaviest binaural loops are **runtime-dispatched**: one binary carries several
|
||||
ISA variants and picks the widest one at startup from CPUID/XGETBV, with the output
|
||||
byte-identical either way (see [SIMD dispatch](docs/simd.en.md)). `JOC_ENABLE_AVX2` /
|
||||
`JOC_ENABLE_AVX512` (both ON by default) only decide whether those units are linked
|
||||
in: carrying the AVX2 unit buys 12.4% on the .jochrtf binaural kernel and 1.8% on
|
||||
Rosella, while a CPU without AVX2 still runs the baseline (SSE2) path. To produce a
|
||||
baseline-only binary:
|
||||
## Requirements
|
||||
|
||||
- Python 3.10+
|
||||
- NumPy 1.24+
|
||||
- h5py 3.8+
|
||||
- SciPy 1.10+
|
||||
- A standalone FFmpeg executable; `ffmpeg-python` is not required. FFmpeg is discovered through `PATH` by default or selected with `--ffmpeg`. On startup the decoder options are probed with `ffmpeg -h decoder=eac3`: a missing E-AC-3 decoder or `-drc_scale` is a hard error, while a missing `-target_level` only fails when `--eac3-target-level` is used
|
||||
|
||||
Install the Python dependency in a project-specific environment:
|
||||
|
||||
```powershell
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DJOC_ENABLE_AVX2=OFF
|
||||
python -m pip install -r requirements.txt
|
||||
```
|
||||
|
||||
gives a baseline-ISA (SSE2) build that runs on any x86-64. Non-MSVC builds never
|
||||
receive the flag.
|
||||
|
||||
### Paths and encoding
|
||||
|
||||
Every path inside the library is **UTF-8**, converted only at the OS boundary
|
||||
(`src/foundation/fs_utf8.*`): on Windows through `std::filesystem::path` (UTF-16
|
||||
inside) into `_wfopen`/`CreateProcessW`, and as plain bytes elsewhere. Command line
|
||||
arguments are re-parsed from `GetCommandLineW` + `CommandLineToArgvW` on Windows and
|
||||
the console code page is set to UTF-8, so non-ASCII paths (Japanese, Chinese, ...)
|
||||
work for the input, the ffmpeg child process and the output files alike; a
|
||||
non-ASCII path regression case runs in `ctest`.
|
||||
|
||||
## Artifacts
|
||||
|
||||
| Artifact | Purpose |
|
||||
|---|---|
|
||||
| `joc_core.dll` | The engine: bitstream parsing, JOC/OAMD, DSP, speaker and binaural rendering, ADM BWF/WAV writing, file task, streaming surface |
|
||||
| `joc_cli.exe` | Command line frontend for file tasks |
|
||||
| `include/joc_core.h` | Engine, telemetry and file-task interface (pure C) |
|
||||
| `include/joc_stream.h` | Embedder-facing streaming push/pull interface (pure C, self-contained) |
|
||||
|
||||
## Command line
|
||||
|
||||
The arguments match the reference Python CLI exactly: the input is positional,
|
||||
**ADM BWF is the default output**, `--speaker-layout` or `--binaural` selects the
|
||||
other two modes, and without `-o` the result lands in `output/`.
|
||||
If FFmpeg is not on `PATH`:
|
||||
|
||||
```powershell
|
||||
# Default: ADM BWF (inherently 24-bit, so there is no format option)
|
||||
joc_cli input.m4a
|
||||
# -> output/input.adm.wav
|
||||
|
||||
joc_cli input.m4a -o out/adm.wav # explicit output
|
||||
|
||||
# Speaker layout
|
||||
joc_cli input.m4a --speaker-layout 5.1 # -> output/<name>.5.1.wav
|
||||
joc_cli input.m4a --speaker-layout 7.1.4 --speaker-output out/714.wav --speaker-format int24
|
||||
|
||||
# Binaural (defaults to <exe>/HRTF/binaural.personalized_headphone)
|
||||
joc_cli input.m4a --binaural
|
||||
joc_cli input.m4a --binaural --compiled-hrtf-cache HRTF/subject.jochrtf # offline compiled field
|
||||
joc_cli input.m4a --binaural --compiled-hrtf-cache HRTF/subject.jochrtf --jochrtf-room off
|
||||
joc_cli input.m4a --binaural --personalized-headphone # Rosella personalisation
|
||||
# -> output/<name>.binaural.wav
|
||||
joc_cli input.m4a --binaural --personalized-headphone --rosella-filterbank 2 # captured bank
|
||||
|
||||
# Other common switches
|
||||
joc_cli input.m4a --duration 30 --gain-db -3 --trajectory-mode dense64
|
||||
joc_cli input.eac3 --metadata-only --print-metadata summary # parse and print metadata only
|
||||
python main.py input.m4a --ffmpeg C:\path\to\ffmpeg.exe
|
||||
```
|
||||
|
||||
`--speaker-format` / `--binaural-format` default to `float32`; an `int24` request that
|
||||
would clip follows `--clip-action` (default `ask`; a non-interactive terminal must
|
||||
pass `continue`, `float32` or `abort`). `--duration` is in **seconds**, and
|
||||
`--object-delay-samples`, `--speaker-metadata-offset`, `--binaural-tail-seconds` and
|
||||
`--binaural-tail-threshold` (1e-8, the binaural tail trim) keep the reference
|
||||
defaults. A run always writes `<output>.report.json` (`--report-json` overrides it).
|
||||
## Usage
|
||||
|
||||
`--rosella-filterbank 1|2` picks the filterbank of the Rosella `.personalized_headphone`
|
||||
path: `1` (default) is the public 13-tap / 6-slot bank, `2` is the captured 11-tap /
|
||||
3-slot bank. Their latencies are 961 and 448 samples, and each keeps its own
|
||||
coefficients, history and flush capacity. The option only applies to the Rosella
|
||||
model; the `.jochrtf` path rejects it. See
|
||||
[Rosella filterbank](docs/rosella_filterbank.en.md).
|
||||
Write a 25-channel ADM BWF by default:
|
||||
|
||||
**Differences from the reference:** `--backend python` and the metadata sidecars
|
||||
(`--metadata-dir`, `--metadata-cache`, `--metadata-backend sidecar`) are unavailable
|
||||
in this build and fail immediately with an explanation instead of being ignored.
|
||||
This build adds `--bed` (pre-decoded
|
||||
6-channel float32 PCM, which skips ffmpeg decoding), `--kernels`, `--work-dir`,
|
||||
`--report-json`, `--dry-run`, `--quiet` and `--rosella-filterbank`.
|
||||
```powershell
|
||||
python main.py input.m4a
|
||||
```
|
||||
|
||||
Select a backend or output path:
|
||||
|
||||
```powershell
|
||||
python main.py input.eac3 -o output.adm.wav
|
||||
```
|
||||
|
||||
Write a speaker-layout WAV directly:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --speaker-layout 2.0 --speaker-format float32
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24
|
||||
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
||||
```
|
||||
|
||||
Write binaural stereo directly. Two kinds of input are available:
|
||||
|
||||
```powershell
|
||||
# 1) Rosella .personalized_headphone (default; HRTF/binaural.personalized_headphone)
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||
|
||||
# 2) .jochrtf compiled field (reserved for high-precision offline rendering)
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room off
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
||||
```
|
||||
|
||||
- `.personalized_headphone` is a model produced by Dolby's official
|
||||
personalization scan; its JSON parsing is implemented by this project
|
||||
(`src/rosella_model.py`) and does not invoke any Dolby software. Near/Mid/Far
|
||||
and the room handling are unchanged.
|
||||
- `.jochrtf` is a directional field exported offline from a SOFA. This repository
|
||||
loads and renders it; the compiler is a separate offline tool and its output
|
||||
loads through `--compiled-hrtf-cache`.
|
||||
|
||||
The `.jochrtf` route keeps Near/Mid/Far point-source scales of approximately
|
||||
1, 2.193 and 6.402 m, default Mid. It renders only the field's direct binaural
|
||||
sound by default; `--jochrtf-room on` adds the project room (six first-order image
|
||||
reflections plus an FDN tail, computed in code with no asset files).
|
||||
input will retain its recorded room and ignore the additional room switch.
|
||||
See [Binaural rendering](docs/binaural.en.md).
|
||||
Because a cache contains transformed
|
||||
HRTF data, its use and redistribution remain subject to the source dataset's
|
||||
terms. See [Binaural Rendering](docs/binaural.en.md) and
|
||||
[Third-party notices](THIRD_PARTY_NOTICES.md) for format boundaries, formulas,
|
||||
state, timing, and distribution considerations.
|
||||
|
||||
Speaker and binaural output share peak analysis, the WAV writer, and clipping policy. When PCM24 may clip in a non-interactive environment, select a policy explicitly:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf `
|
||||
--binaural-format int24 --clip-action abort
|
||||
```
|
||||
|
||||
Metadata and diagnostics:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --print-metadata summary
|
||||
python main.py input.m4a --metadata-only --print-metadata frames
|
||||
python main.py input.m4a --metadata-cache metadata_cache
|
||||
python main.py input.m4a --metadata-dir metadata_cache
|
||||
```
|
||||
|
||||
### E-AC-3 decode-side dynamic range and level
|
||||
|
||||
By default FFmpeg applies the stream `dynrng` dynamic range compression when
|
||||
decoding E-AC-3 (`-drc_scale 1`). The core 5.1 PCM is the input of JOC object
|
||||
reconstruction, and `dynrng` is playback-time gain, so it is inherited linearly
|
||||
by every object and every output (ADM, speaker, binaural). This tool therefore
|
||||
decodes at **full dynamic range** by default:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a # default: -drc_scale 0, full range
|
||||
python main.py input.m4a --eac3-drc-scale 1 # reproduce consumer playback
|
||||
python main.py input.m4a --eac3-drc-scale 0.5 # apply half of it
|
||||
python main.py input.m4a --eac3-target-level -27 # dialnorm-referenced level
|
||||
```
|
||||
|
||||
- `--eac3-drc-scale` (`0`–`6`, default `0`) maps to FFmpeg `-drc_scale`: the gain
|
||||
of each E-AC-3 block is `dynrng factor ^ value`. `0` disables DRC, `1` is the
|
||||
author's intent, and `>1` is asymmetric (loud parts fully compressed, quiet
|
||||
parts enhanced).
|
||||
- `--eac3-target-level` (`-31`–`0`, default `0` = off) maps to FFmpeg
|
||||
`-target_level`: a static per-frame gain of about `target_level - dialnorm` dB,
|
||||
independent of and stackable with `--eac3-drc-scale`. dialnorm is a per-stream
|
||||
property, so the attenuation that `-27` applies varies from stream to stream.
|
||||
- The level change is expected: whether the gain is applied depends on the stream
|
||||
`dynrng`, so `output_clip.peak` and the PCM24 clipping decision in
|
||||
`.report.json` change accordingly.
|
||||
- `--gain-db` is a static gain applied **after** reconstruction (float64 on the
|
||||
binaural path) and is not the same thing as decode-side DRC, which is
|
||||
block-varying; do not use `--gain-db` to cancel it.
|
||||
- The `ffmpeg` field of `.report.json` records the FFmpeg version and the decode
|
||||
options that were actually passed (`version`, `eac3_decode_options`).
|
||||
|
||||
### Binaural render mode
|
||||
|
||||
`--binaural-mode off|near|mid|far` selects the binaural render mode; the default
|
||||
is `mid`, and both outputs share this single option:
|
||||
|
||||
- **Direct binaural rendering** (`--binaural`): `off` is rejected (error);
|
||||
near/mid/far apply, defaulting to `mid`;
|
||||
- **ADM BWF**: the low 3 binaural-render-mode bits of the last 15 JOC object
|
||||
entries in DBMD segment 10 carry `off=0/near=1/far=2/mid=3`, leaving the first
|
||||
10 bed entries unchanged; the default is `mid`, and `off` explicitly disables
|
||||
the binaural metadata hint.
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --binaural-mode mid
|
||||
python main.py input.m4a --binaural-mode off # ADM BWF only: disable the DBMD hint
|
||||
```
|
||||
|
||||
**The default `mid` is a human-specified rendering hint**; it is not original
|
||||
binaural metadata extracted or recovered from the input E-AC-3 JOC bitstream,
|
||||
nor does it represent the original mix's per-object binaural settings. The hint
|
||||
does not change PCM, object trajectories, or direct speaker rendering. The
|
||||
adjacent `.report.json` records `binaural_mode` (the mode name) and
|
||||
`binaural_mode_value` (the ADM code; `null` for direct binaural output).
|
||||
|
||||
### OAMD time alignment
|
||||
|
||||
Object trajectories and direct speaker rendering both default to a metadata delay of `1473 samples`. This value describes the theoretical mapping between decoder-output PCM and OAMD updates. The speaker renderer retains its existing 32-sample control block, so the default update lands on effective block boundary `1472`:
|
||||
|
||||
```text
|
||||
align32(1473) = 1472
|
||||
```
|
||||
|
||||
Override the two paths with `--object-delay-samples` and `--speaker-metadata-offset`, respectively. The 1473-sample timing offset is distinct from the 640-value inverse-QMF filter/window state; 640 is a QMF state length, not a metadata delay.
|
||||
|
||||
The direct binaural path uses `--object-delay-samples`. Each ID11/OAMD event is
|
||||
placed on an absolute sample timeline from its frame start, outer-subpayload
|
||||
offset, and block offset, then shifted by that delay. Each 1536-sample input
|
||||
frame is processed as three consecutive 512-sample blocks; the interpolated
|
||||
position, direction, and profile are updated at each block's absolute starting
|
||||
sample.
|
||||
|
||||
### Binaural calculation
|
||||
|
||||
See [Binaural Rendering Mathematics](docs/binaural.en.md) for QMF, hybrid processing, direction fields, distance, ITD, room processing, the 512-sample parameter updates above, and the selected filterbank's latency compensation (961 by default, 448 with `--rosella-filterbank 2`).
|
||||
|
||||
### Rosella filterbank
|
||||
|
||||
`--rosella-filterbank 1|2` selects the filterbank of the `.personalized_headphone`
|
||||
path:
|
||||
|
||||
- `1` (default): the public bank, 13-tap hybrid / 6-slot high-band delay,
|
||||
961 samples of latency;
|
||||
- `2`: the Rosella bank captured from the reference renderer, 11-tap hybrid /
|
||||
3-slot high-band delay, 448 samples of latency.
|
||||
|
||||
The option only applies to the Rosella model; the SOFA and `.jochrtf` paths
|
||||
reject it. See [Rosella filterbank](docs/rosella_filterbank.en.md) for the
|
||||
parameters, buffers, latency and validation of both banks.
|
||||
|
||||
For all options:
|
||||
|
||||
```powershell
|
||||
python main.py --help
|
||||
```
|
||||
|
||||
Without `-o`, output still goes to `output/` at the repository root. The directory move intentionally preserves this behavior.
|
||||
|
||||
## Speaker layouts and channel order
|
||||
|
||||
`--speaker-layout` accepts the twelve layouts below, and the PCM/WAV channel order is the order in
|
||||
`--speaker-layout` accepts the eleven layouts below, and the PCM/WAV channel order is the order in
|
||||
the table:
|
||||
|
||||
| Layout | Channel order |
|
||||
@@ -122,7 +251,6 @@ the table:
|
||||
| 3.1 | L, R, C, LFE |
|
||||
| 4.0 | L, R, Lrs, Rrs |
|
||||
| 5.1 | L, R, C, LFE, Ls, Rs |
|
||||
| 6.0 | L, R, Ls, Rs, Lrs, Rrs |
|
||||
| 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs |
|
||||
| 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm |
|
||||
| 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr |
|
||||
@@ -141,189 +269,63 @@ into mask-bit order. Players, ASIO, DAWs and other devices have to map the chann
|
||||
themselves; this project guarantees the content and numbering of each PCM channel, not that a
|
||||
downstream player recognises Dolby speaker positions on its own.
|
||||
|
||||
`--speaker-layout` accepts only the twelve names in the table; anything else fails as an unknown
|
||||
The command line accepts only the eleven names in the table; anything else is rejected as an unknown
|
||||
layout.
|
||||
|
||||
Speaker rendering reads warp, trim and front/back balance from the OAMD metadata; the CLI has no
|
||||
warp override. Warp 0/1 render directly; warp 2/3 render to a 7.1.x intermediate layout first,
|
||||
then fold back to 5.1.x with PLIIx or Standard (Lo/Ro) coefficients, and calibration always
|
||||
follows the final target layout. Source trajectories and binaural rendering do not pre-apply
|
||||
these controls; see [mathematics §12](docs/math.en.md) for the parameters and evaluation order.
|
||||
|
||||
Speaker rendering also applies the OAMD object attributes: per-block gain/inactive,
|
||||
zone/elevation, size/snap/divergence, the priority size budget, element 5 position
|
||||
refinement, ISF basis matrices, and bed distribution. ADM export and the binaural
|
||||
paths still place point objects and do not apply these speaker target coefficients.
|
||||
|
||||
Object position updates are scheduled from the complete `frame_state`: speaker and
|
||||
binaural rendering both accept multiple position blocks and programmes with more than
|
||||
16 objects and schedule every block at its own time and ramp. The binaural audio input
|
||||
is still one LFE plus fifteen object sources; the extra position tracks are followed
|
||||
but not panned. ADM export keeps the 15-track single-block position view and rejects
|
||||
anything larger as an unsupported variant.
|
||||
|
||||
## Library integration
|
||||
|
||||
The engine and the file task are exposed by `joc_core.h`: `joc_task_validate` /
|
||||
`joc_task_execute` run a file task and report state events through a callback, and
|
||||
`joc_task_result` carries frame counts, peak, byte count and SHA-256.
|
||||
|
||||
Players and decoder components use `joc_stream.h`: the caller pushes E-AC-3 bytes
|
||||
and the matching core PCM (or already-rebuilt objects16) at its own pace and pulls
|
||||
rendered PCM. Any chunking is allowed, and the result is byte-identical to the file
|
||||
task.
|
||||
|
||||
```c
|
||||
joc_stream_config config = {0};
|
||||
config.struct_size = sizeof(config);
|
||||
config.input = JOC_STREAM_IN_EAC3; /* or JOC_STREAM_IN_PCM_OBJECTS16 */
|
||||
config.output = JOC_STREAM_OUT_SPEAKER; /* or BINAURAL / PCM_OBJECTS16 */
|
||||
config.speaker_layout_name = "5.1";
|
||||
joc_stream* stream = NULL;
|
||||
joc_stream_create(&config, &stream);
|
||||
/* loop: joc_stream_push(...) / joc_stream_pull(...) */
|
||||
joc_stream_flush(stream);
|
||||
joc_stream_destroy(stream);
|
||||
```
|
||||
|
||||
Contract: state is instance-private, so streams coexist; push and pull on one
|
||||
instance must come from the same thread; rendering is stateful, so **this version
|
||||
offers no seek** - repositioning means decoding from the start of the stream. The
|
||||
kernel latency is 961 samples for speaker/binaural output and `joc_stream_flush`
|
||||
drains the binaural room tail.
|
||||
|
||||
## Layout
|
||||
## Repository layout
|
||||
|
||||
```text
|
||||
include/ public C ABI: joc_core.h (engine/file task), joc_stream.h (streaming),
|
||||
eac3joc_core.h (upstream ABI)
|
||||
src/ implementation: eac3_transport, emdf, joc_bitstream, joc_core, oamd,
|
||||
timeline, speaker, binaural, hrtf, adm, io, telemetry, task, stream,
|
||||
api, cli, simd
|
||||
tests/ unit tests (CTest, self-contained, no external data)
|
||||
docs/ mathematics, the binaural rendering flow and SIMD dispatch
|
||||
JustOneCacophony/
|
||||
├─ main.py command-line entry point
|
||||
├─ src/ Python implementation modules
|
||||
├─ data/ Python runtime table data
|
||||
├─ HRTF/ user HRTF data directory (create as needed, git-ignored)
|
||||
├─ output/ output directory (create as needed; the .jochrtf cache defaults to its hrtf-cache subdirectory)
|
||||
├─ docs/ math and binaural notes in both languages
|
||||
├─ requirements.txt Python dependency
|
||||
├─ README.md Chinese documentation
|
||||
└─ README.en.md English documentation
|
||||
```
|
||||
|
||||
The QMF and binaural tables and the JOC Huffman tables under `src/` are written from
|
||||
publicly documented standards; [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) records
|
||||
their sources.
|
||||
|
||||
The ISF compressed base coefficients and layout descriptors are obtained from
|
||||
lookup tables stored in the generated header (`src/oamd/isf_tables.h`), which is
|
||||
compiled into the binary with the code, so nothing reads a data file at run
|
||||
time. Gain, size, priority, position, divergence, delta and refinement values are
|
||||
computed in code, while the size kernel is generated from the speaker layout
|
||||
region geometry and bed routing is derived from those layouts.
|
||||
|
||||
## Mathematical implementation
|
||||
|
||||
The main documented stages are:
|
||||
|
||||
- dense JOC differential reconstruction and dequantization;
|
||||
- parameter-band mapping to 64 QMF subbands;
|
||||
- cross-frame parameter interpolation (hard cut when no previous state exists, zero-matrix publishing after an object disappears);
|
||||
- the frame-level JOC state machine (normal, hold, hard cut, core passthrough);
|
||||
- cross-frame parameter interpolation;
|
||||
- analysis/inverse QMF, surround delay, and FIR state;
|
||||
- the 1217-sample LFE delay with its sample-wise clipgain ramp;
|
||||
- the 1217-sample LFE delay;
|
||||
- OAMD Q15 coordinate conversion;
|
||||
- equal-power panning over target-layout regions;
|
||||
- OAMD metadata application (per-block gain and inactive, zone/elevation, size kernel, snap, divergence, position refinement, priority size budget, ISF matrices, bed distribution);
|
||||
- layout-dependent position compensation and sample-wise gain ramps;
|
||||
- float32 and PCM24 output quantization;
|
||||
- Rosella and compiled `.jochrtf` binaural rendering, with exactly-once delay/phase and special LFE.
|
||||
- SOFA canonical import, full-HRIR directional interpolation, explicit 64-QMF/77-hybrid compact export, `36×2×77` fifth-order fields, exactly-once delay/phase, and special LFE.
|
||||
|
||||
See the [mathematical notes](docs/math.en.md) for the equations used by the decoding
|
||||
and rendering process.
|
||||
See the [mathematical notes](docs/math.en.md) for the equations used by the decoding and rendering process.
|
||||
|
||||
## Compatibility note
|
||||
## Known limitations
|
||||
|
||||
### How `joc_clipgain` is applied
|
||||
- Only the common contiguous EMDF transport is covered. Fragmented transport across multiple audio-block skip fields is not covered.
|
||||
- All `joc_dmx_config_idx` values 0..4 are covered ($N=5,7,7,5,7$; the phase switch $\sigma(k)$ gates the Ls/Rs $-j$ rotation and the band-0 FIR together). The 7-channel configurations need an 8-channel bed and a dependent-substream `chanmap` to identify the extra pair: a missing independent substream or a missing `chanmap` is still a variant error, while a `chanmap` that disagrees with $k$ about the extra pair, or an OAMD bed mask that does, is recorded as a diagnostic and the frame still renders. The extra pair's **identity** is metadata only and never reaches the DSP (section 10.1); the bed mapping follows the channel codes the `chanmap` declares.
|
||||
- When one audio frame holds several EMDF containers carrying ID11/ID14 (one of them may be something other than the frame's JOC parameters: its ID14 may declare more objects than its bytes can hold, and its ID11 may carry no group id), the container is chosen by the association rule of ETSI TS 103 420 section 8.2 and its Table 56: **the container whose OAMD and JOC payloads share a group id and whose ID14 parses completely** wins, then one whose ID14 merely parses; only a unique best score is used, the others are recorded as a diagnostic (`emdf/extra_container_ignored`), and a tie or all-zero score stays a variant error.
|
||||
- The extra pair's identity follows TS 103 420 Table 47/Table 48 ($k=1\to$Lb/Rb, $k\in\{2,4\}\to$Tfl/Tfr, $N=5,7,7,5,7$); Annex E.3 requires a dependent-substream `chanmap`, when present, to indicate one of the Table 47 configurations, so `*_5_1_2top_*` (chanmap says Lts/Rts) does not satisfy that "shall". Such material is tolerated with a diagnostic instead of being refused.
|
||||
- An audio frame with no JOC EMDF at all (for example when the encoder wrote none for the first frame) emits silent object channels and is recorded in the `[diagnostics]` line and the JSON report's `diagnostics` field.
|
||||
- The speaker and SOFA binaural paths currently cover ordinary point objects; extent, spread, diffuse, divergence, channel lock, and similar controls are outside the supported scope.
|
||||
- OAMD trim elements are boundary-checked and skipped; warp, balance, and trim parameters are not applied to raw object trajectories or speaker rendering.
|
||||
- Multi-data-point streams, uncommon band configurations, and unusual OAMD scheduling have less coverage than common 12-band, single-data-point material.
|
||||
- `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with `g` from the
|
||||
`x:3 | y:5` field), so the x16 at the output cancels the 1/16 and the clamp sits **after** the gain.
|
||||
The scalar is kept per frame: the first 256 samples ramp linearly from the previous frame's value and
|
||||
the rest stays constant. For `g = 1` the interpolation is the identity and
|
||||
`clamp(x)·1 == clamp(x·1)`.
|
||||
- A speaker limiter is outside the current primary formula.
|
||||
- The SOFA importer currently supports the strict `SimpleFreeFieldHRIR` FIR subset; other SOFA conventions require explicit adapters.
|
||||
- The binaural runtime is fixed at 48 kHz, fifth order, and one measurement-radius shell at a time.
|
||||
- ADM output, speaker layouts, and binaural models still need broader interoperability checks across platforms and players.
|
||||
|
||||
`joc_clipgain` is folded into the **analysis-side scalar**: `scalar = 1/16 x g`, with
|
||||
`g = 2^(x-4)·(1 + y/32)` from the 8-bit field `x:3 | y:5`; the range is 0.0625..15.75. The x16 at the
|
||||
output cancels the 1/16, so the net effect is a multiply by `g` and the clamp therefore sits **after**
|
||||
the gain (`clamp(16·IQMF·g_interp)`). The scalar is kept in two per-frame slots: the first 256 samples
|
||||
of a frame ramp linearly from the previous value, the rest stays constant, and the first frame after a
|
||||
reset takes the new value directly. For `g = 1` the interpolation is the identity and
|
||||
`clamp(x)·1 == clamp(x·1)`. LFE bypasses the analysis but applies the same full `g` as a sample-wise
|
||||
ramp before its 1217-sample delay.
|
||||
## Documentation
|
||||
|
||||
`object_delay_samples` defaults to **1473**, preserving the behaviour of the existing
|
||||
implementation; it is a configurable field and changing it changes the OAMD/ADM time
|
||||
alignment. Upstream investigation suggests the value should be 0; this project keeps
|
||||
the current default to stay byte-identical.
|
||||
|
||||
### `joc_dmx_config_idx` 0..4
|
||||
|
||||
All five configurations are covered: $N=5,7,7,5,7$ with the phase switch
|
||||
$\sigma(k)=(k\in\{3,4\})$ gating the Ls/Rs $-j$ rotation and the band-0 21-tap FIR together.
|
||||
A 7-channel configuration needs an **8-channel** bed, mapped by channel code:
|
||||
|
||||
| $k$ | extra pair | decoder 8-channel order | JOC input (bed index) |
|
||||
|---|---|---|---|
|
||||
| 1 | Lb/Rb | FL FR FC LFE **BL BR** SL SR | 0 1 2 6 7 4 5 |
|
||||
| 2 / 4 | Tfl/Tfr | FL FR FC LFE SL SR TFL TFR | 0 1 2 4 5 6 7 |
|
||||
|
||||
ffmpeg gets `-ac 6` only for the $N=5$ configurations; $N=7$ keeps the decoder's own layout
|
||||
(`-ac 6` downmixes 5.1.2/7.1 into 5.1 and `-ac 8` remaps 5.1.2 into 7.1; both lose information).
|
||||
|
||||
**Cross-layer consistency** (section 11.2): the substreams' full-band channel count must equal $N$,
|
||||
a non-zero $joc\_ext\_config\_idx$ is an unmapped variant, and a declared object count above 15 is
|
||||
clamped to 15 and recorded as a diagnostic - those change the DSP input, so a mismatch is refused.
|
||||
The extra pair's **identity** never reaches the DSP (section 10.1): when the dependent-substream
|
||||
`chanmap` disagrees with $k$, the mismatch is recorded as a diagnostic and the frame still
|
||||
renders, with the bed mapping following the channel codes the `chanmap` declares. The bed
|
||||
layout the OAMD declares may hold more channels than the core substream codes (a 9.1.6 bed
|
||||
with a 5.1 core, for example): only a core channel missing from that layout counts as a
|
||||
mismatch. When one audio frame holds several EMDF containers carrying ID11/ID14, they
|
||||
are scored by the association rule of section 8.2 and its Table 56: a container whose OAMD and JOC
|
||||
payloads share a group id and whose ID14 parses completely wins, then one whose ID14 merely parses;
|
||||
only a unique best score is used and the others are recorded as a diagnostic
|
||||
(`emdf/extra_container_ignored`). The extra pair's 10-slot ADM skeleton placement ($k=1\to6/7$,
|
||||
$k\in\{2,4\}\to8/9$) is recorded, but bed channels are not output channels under the current
|
||||
contract.
|
||||
|
||||
### Frame status and the JOC decode state machine
|
||||
|
||||
Every frame carries a status; a non-zero status is reported as a diagnostic. A non-zero status
|
||||
makes the frame invalid for rendering, so the renderer holds the previous matrices or passes the
|
||||
core PCM through instead of decoding that frame's matrix: 2 unsupported downmix configuration,
|
||||
3 object count clamped to 15, 4 unknown parameter-band index, 5 invalid `offset_ts`, 6 trailing
|
||||
data after byte padding, and 7 an ID14 payload that ends inside `joc_data`. A file task refuses a
|
||||
`joc_dmx_config_idx` outside 0..4 earlier, in the metadata scan, as an unsupported variant.
|
||||
Status 3 still yields the clamped object list for diagnostics, status 2 and 4 stop the parse at
|
||||
the failing header field, and status 7 keeps the complete header, clipgain, sequence counter and
|
||||
object parameters, with only the object matrices missing.
|
||||
|
||||
Status 7 is classified only when the EMDF container itself is complete: a container length past
|
||||
the syncframe, or a payload too short for the 12-bit `joc_header`, stays a transport error, and
|
||||
the file task and stream API additionally require the container's ID11 payload to parse. A
|
||||
complete container whose payload ends inside `joc_data` takes the invalid-frame path (the
|
||||
previous matrices are held), so decoding continues instead of stopping; the stream statistics
|
||||
count those frames in `truncated_joc_frames`, and `joc_check_id14_padding` still reports
|
||||
`JOC_ERR_BITSTREAM_TRUNCATED` for them.
|
||||
|
||||
Rendering then runs a per-frame state machine: normal, hold, hard cut, and core passthrough. An
|
||||
unusable-JOC frame that arrives during normal rendering enters hold, where the previous frame-end
|
||||
matrices keep being published; six consecutive unusable frames move the internal hold state on but
|
||||
stay in hold, and a valid frame resynchronises the sequence number. A sequence number of 0 after
|
||||
the renderer left its initial state, a sequence mismatch, or a change of the declared core channel
|
||||
count is a hard cut: every object matrix is zeroed and each object's previous state becomes
|
||||
the zero matrix, while a core channel count change also clears the LFE delay ring but keeps
|
||||
the clipgain ramp state. Before the first valid frame the renderer passes the core PCM
|
||||
through.
|
||||
|
||||
A frame with no JOC EMDF at all (for example when the encoder wrote none for the first frame)
|
||||
always takes the passthrough path: the eight core planes L, R, C, LFE, Ls, Rs, (Lrs, Rrs) are
|
||||
written to output slots 0..7 by identity, each delayed by 1217 samples, slots 8..15 stay zero,
|
||||
and the 8-plane history keeps advancing. Those frames are counted separately in the statistics,
|
||||
and E-AC-3 input without JOC metadata (ID11 + ID14) is outside the supported scope. A frame whose
|
||||
ID14 payload ended inside `joc_data` is not one of these: it takes the hold path above and is
|
||||
counted separately in the stream statistics (`truncated_joc_frames`). While hold or
|
||||
passthrough runs without fresh parameters, the analysis scalar and the LFE ramp keep the
|
||||
currently held clipgain.
|
||||
|
||||
## License
|
||||
|
||||
MIT, see [LICENSE](LICENSE). Third-party provenance and patent boundaries are in
|
||||
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
||||
- [Mathematical notes](docs/math.en.md) · [中文](docs/math.md)
|
||||
- [Binaural rendering](docs/binaural.en.md) · [中文](docs/binaural.md)
|
||||
- [Rosella filterbank](docs/rosella_filterbank.en.md) · [中文](docs/rosella_filterbank.md)
|
||||
@@ -1,105 +1,215 @@
|
||||
# JustOneCacophony — C++ Core
|
||||
# JustOneCacophony — JOC
|
||||
|
||||
[English](README.en.md) · [数学说明](docs/math.md) · [双耳渲染](docs/binaural.md) · [SIMD 派发](docs/simd.md)
|
||||
[English](README.en.md)
|
||||
|
||||
JustOneCacophony 的 C++ 实现:E-AC-3 JOC 码流解析、对象重建与渲染的执行内核。它从 E-AC-3
|
||||
同步帧中提取 EMDF、ID14 JOC 参数与 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 PCM(N=5 或 7)
|
||||
重建 LFE 与 15 路对象 PCM,并输出 ADM BWF、指定扬声器布局的 WAV,或用编译好的 HRTF 方向场
|
||||
直接输出双耳 WAV。
|
||||
> JustOneCacophony 是一个 E-AC-3 JOC 的实验性 / 测试实现,用于研究 JOC 的解析、重建、渲染以及相关数学过程。
|
||||
|
||||
这是研究代码,不是完整、标准兼容或生产级的 JOC 解码器。它只覆盖已实现的码流形态,遇到未知
|
||||
变体时明确报错,而不是假装一切都很和谐。
|
||||
项目可以从常见 E-AC-3 JOC 码流中提取并解析 EMDF、ID14 JOC 参数和 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 PCM(N=5 或 7 个全频段通道)重建 LFE 与 15 路对象 PCM,并输出 ADM BWF、指定扬声器布局的 WAV,或输出双耳 WAV(Rosella 个性化模型,或已编译的 `.jochrtf`)。
|
||||
|
||||
## 构建
|
||||
这是研究代码,不是完整、标准兼容或生产级的 JOC 解码器。它只覆盖当前已实现的码流形态;遇到未知变体时会明确报错,而不是假装一切都很和谐——如果哪里算错了,它可能就真的只剩 cacophony 了。
|
||||
|
||||
```powershell
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
ctest --test-dir build --output-on-failure
|
||||
## 当前功能
|
||||
|
||||
- 扫描 E-AC-3 同步帧中的常见连续 EMDF 容器;
|
||||
- 解析 ID14 dense / sparse JOC 参数、Huffman 数据、差分矩阵与 `joc_clipgain`;
|
||||
- 解析 ID11 OAMD 位置更新并生成对象轨迹;
|
||||
- 读取 OAMD 声明的 bed 布局(5.1、7.1、7.1.2、7.1.4、9.1.4、9.1.6 等):bed-only 内容的
|
||||
15 路对象就是 bed 声道,每个声道按其声道标签的固定方位(ETSI TS 103 420 Table B.10)
|
||||
作为点声源渲染,扬声器与双耳共用同一组源位置,不随输出布局改变;LFE 独立处理;
|
||||
- 通过 analysis QMF、参数插值、对象矩阵和 inverse QMF 重建 LFE + 15 路对象 PCM;
|
||||
- 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象;
|
||||
- 直接渲染 `2.0`、`3.1`、`4.0`、`5.1`、`7.1`、`5.1.2`、`5.1.4`、`7.1.2`、`7.1.4`、`9.1.4`、
|
||||
`9.1.6`(`4.0` 是 Quad 布局,前后各一对;声道排列见[扬声器布局与声道顺序](#扬声器布局与声道顺序));
|
||||
- 从 `pcm16 + ID11/OAMD` 直接运行双耳渲染,不生成临时 ADM BWF;
|
||||
- 双耳 DSP 全程使用 float64/complex128;个性化路线保留原滤波器组与延迟规则,`.jochrtf` 路线保留 77 带方向场与跨帧状态;
|
||||
- 直接输出统一支持 float32 或 PCM24 WAV,并在 PCM24 削波前提供明确处理策略;
|
||||
- 全程使用 NumPy 的纯 Python 实现;
|
||||
- 读取或写入 metadata sidecar,并生成元数据、运行时间和输出摘要。
|
||||
|
||||
## 处理流程
|
||||
|
||||
```text
|
||||
M4A / E-AC-3
|
||||
├─ FFmpeg 提取 E-AC-3 并解码核心 PCM(N=5 或 7)
|
||||
├─ EMDF → ID14 JOC 参数 → 对象矩阵
|
||||
├─ 核心 PCM → analysis QMF → 参数插值 → inverse QMF
|
||||
├─ ID11 OAMD → 对象位置与时间轨迹
|
||||
└─ LFE + 15 objects
|
||||
├─ 25ch ADM BWF
|
||||
├─ 指定布局的扬声器 WAV
|
||||
└─ ID11 直接时间轴 + HRTF(Rosella / .jochrtf)→ 双耳 WAV
|
||||
```
|
||||
|
||||
Windows(MSVC / VS 2022,静态 CRT)、Linux 与 macOS 由 `.github/workflows/ci.yml` 同时构建并跑
|
||||
单元测试;本地只验证 MSVC。浮点行为是逐字节验收的一部分,因此不启用 fast-math:MSVC 用
|
||||
`/fp:precise`,其他编译器用 `-fno-fast-math`。
|
||||
JOC 对象重建、扬声器渲染与双耳渲染使用同一组数学过程:analysis QMF、
|
||||
参数插值、对象矩阵、inverse QMF。位流解析、OAMD 时间轴和命令行逻辑同样在 Python 中。
|
||||
|
||||
最重的双耳循环由**运行时派发**:同一份二进制里装多套 ISA 代码,启动时按 CPUID/XGETBV 选最宽
|
||||
的一套,输出逐字节不变(见 [SIMD 与运行时派发](docs/simd.md))。`JOC_ENABLE_AVX2` /
|
||||
`JOC_ENABLE_AVX512`(默认都 ON)只决定是否把这些单元编进二进制:带上 AVX2 单元让 `.jochrtf` 双耳
|
||||
内核快 12.4%、Rosella 快 1.8%,而没有 AVX2 的 CPU 仍走基线(SSE2)路径。要产出只带基线 ISA
|
||||
的二进制:
|
||||
## 环境
|
||||
|
||||
- Python 3.10+
|
||||
- NumPy 1.24+
|
||||
- h5py 3.8+
|
||||
- SciPy 1.10+
|
||||
- 独立的 FFmpeg 可执行程序;不需要 `ffmpeg-python`。默认从 `PATH` 查找,也可通过 `--ffmpeg` 指定可执行文件路径。启动时会探测 `ffmpeg -h decoder=eac3`:缺 E-AC-3 解码器或 `-drc_scale` 直接报错,缺 `-target_level` 只在使用 `--eac3-target-level` 时报错
|
||||
|
||||
建议在项目专用虚拟环境中安装依赖:
|
||||
|
||||
```powershell
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DJOC_ENABLE_AVX2=OFF
|
||||
python -m pip install -r requirements.txt
|
||||
```
|
||||
|
||||
得到基线 ISA(SSE2)、任何 x86-64 都能跑的产物。非 MSVC 构建永远不会带上这个开关。
|
||||
|
||||
### 路径与编码
|
||||
|
||||
库内部所有路径都是 **UTF-8**,只在系统边界转换(`src/foundation/fs_utf8.*`):Windows 上经
|
||||
`std::filesystem::path`(内部 UTF-16)落到 `_wfopen`/`CreateProcessW`,其他平台直接是字节。
|
||||
命令行参数在 Windows 上由 `GetCommandLineW` + `CommandLineToArgvW` 重新解析,控制台设为
|
||||
UTF-8,因此日文/中文等非 ASCII 路径(含 ffmpeg 子进程与输出文件)都能正常工作;单元测试里有
|
||||
一条非 ASCII 路径的回归用例守在 `ctest` 里。
|
||||
|
||||
## 产物
|
||||
|
||||
| 产物 | 说明 |
|
||||
|---|---|
|
||||
| `joc_core.dll` | 执行内核:码流解析、JOC/OAMD、DSP、扬声器与双耳渲染、ADM BWF/WAV 落盘、文件任务、流式接口 |
|
||||
| `joc_cli.exe` | 文件任务命令行前端 |
|
||||
| `include/joc_core.h` | 引擎、遥测与文件任务接口(纯 C) |
|
||||
| `include/joc_stream.h` | 面向嵌入者的流式 push/pull 接口(纯 C,仅包含它即可) |
|
||||
|
||||
## 命令行
|
||||
|
||||
参数与上游 Python CLI 完全一致:输入是位置参数,**默认输出 25 通道 ADM BWF**,用
|
||||
`--speaker-layout` 或 `--binaural` 切换到另外两种模式;未指定 `-o` 时产物落在 `output/`。
|
||||
如果 FFmpeg 不在 `PATH` 中:
|
||||
|
||||
```powershell
|
||||
# 默认:ADM BWF(本身就是 24-bit,没有也不需要格式参数)
|
||||
joc_cli input.m4a
|
||||
# -> output/input.adm.wav
|
||||
|
||||
joc_cli input.m4a -o out/adm.wav # 指定输出
|
||||
|
||||
# 扬声器布局
|
||||
joc_cli input.m4a --speaker-layout 5.1 # -> output/<名称>.5.1.wav
|
||||
joc_cli input.m4a --speaker-layout 7.1.4 --speaker-output out/714.wav --speaker-format int24
|
||||
|
||||
# 双耳(默认取 <exe>/HRTF/binaural.personalized_headphone)
|
||||
joc_cli input.m4a --binaural
|
||||
joc_cli input.m4a --binaural --personalized-headphone # Rosella 个性化模型
|
||||
joc_cli input.m4a --binaural --compiled-hrtf-cache HRTF/subject.jochrtf # 离线编译的 .jochrtf
|
||||
joc_cli input.m4a --binaural --compiled-hrtf-cache HRTF/subject.jochrtf --jochrtf-room off
|
||||
# -> output/<名称>.binaural.wav
|
||||
joc_cli input.m4a --binaural --personalized-headphone --rosella-filterbank 2 # 新版 Rosella 滤波器组
|
||||
|
||||
# 其它常用开关
|
||||
joc_cli input.m4a --duration 30 --gain-db -3 --trajectory-mode dense64
|
||||
joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打印元数据
|
||||
python main.py input.m4a --ffmpeg C:\path\to\ffmpeg.exe
|
||||
```
|
||||
|
||||
`--speaker-format` / `--binaural-format` 默认 `float32`;`int24` 若会削波按 `--clip-action`
|
||||
处理(默认 `ask`,非交互终端下需显式给出 `continue`/`float32`/`abort`)。`--duration` 以**秒**
|
||||
为单位;`--object-delay-samples`、`--speaker-metadata-offset`、`--binaural-tail-seconds`、
|
||||
`--binaural-tail-threshold`(默认 1e-8,双耳尾音裁切阈值)等默认值与上游一致。命令总是写出
|
||||
`<输出>.report.json`(`--report-json` 可改路径)。
|
||||
## 使用方法
|
||||
|
||||
`--rosella-filterbank 1|2` 选择 Rosella 个性化模型使用的滤波器组:`1`(默认)是 13 taps /
|
||||
6 slots 的公开滤波器组,`2` 是 11 taps / 3 slots 的捕获版。两者延迟分别为 961 / 448 samples,
|
||||
系数、历史缓冲与 flush 容量各自独立;该选项只作用于 `.personalized_headphone`,
|
||||
`.jochrtf` 路径给出时直接报错。详见 [Rosella 滤波器组](docs/rosella_filterbank.en.md)。
|
||||
默认输出 25 声道 ADM BWF:
|
||||
|
||||
**与上游参数的差异**:`--backend python` 与 metadata sidecar(`--metadata-dir`/
|
||||
`--metadata-cache`/`--metadata-backend sidecar`)在本构建中不可用,给出时立刻报错并说明原因,
|
||||
而不是静默忽略。本构建额外提供 `--bed`(已解码的 6 通道 float32 PCM,给出后不调用 ffmpeg
|
||||
解码)、`--kernels`(滤波器组表路径)、`--work-dir`、`--report-json`、`--dry-run`、
|
||||
`--quiet`、`--rosella-filterbank`。
|
||||
```powershell
|
||||
python main.py input.m4a
|
||||
```
|
||||
|
||||
指定输出路径:
|
||||
|
||||
```powershell
|
||||
python main.py input.eac3 -o output.adm.wav
|
||||
```
|
||||
|
||||
直接输出扬声器 WAV:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --speaker-layout 2.0 --speaker-format float32
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24
|
||||
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
||||
```
|
||||
|
||||
直接输出双耳渲染 WAV。双耳有两条输入:
|
||||
|
||||
```powershell
|
||||
# 1) Rosella .personalized_headphone(默认,缺省取 HRTF/binaural.personalized_headphone)
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||
|
||||
# 2) .jochrtf 编译缓存(保留项:高精度离线渲染用)
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room off
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
||||
```
|
||||
|
||||
- `.personalized_headphone` 是杜比官方软件个性化扫描得到的模型,其 JSON 解析由
|
||||
本项目自行实现(`src/rosella_model.py`),不调用杜比软件;Near/Mid/Far 与房间
|
||||
处理保持原行为。
|
||||
- `.jochrtf` 是从 SOFA 离线导出的方向场,本仓库负责加载与渲染;编译器是独立
|
||||
的离线工具,产出的缓存用 `--compiled-hrtf-cache` 加载。
|
||||
|
||||
`.jochrtf` 路线沿用 Near 约 1 m、Mid 约 2.193 m、Far 约 6.402 m 的点声源参考尺度,
|
||||
默认 Mid。默认只输出方向场的双耳直达声;`--jochrtf-room on` 启用项目自有房间
|
||||
(六路一阶镜像早反射 + FDN 晚期,直接在代码里计算,不读取资产文件)。
|
||||
|
||||
cache 含有源 HRTF 的变换数据,使用与再分发仍受源数据许可约束;
|
||||
格式边界、计算公式、状态、时间轴及发布注意事项见
|
||||
[双耳渲染](docs/binaural.md) 和 [第三方通知](THIRD_PARTY_NOTICES.md)。
|
||||
|
||||
扬声器和双耳输出共享峰值检查、writer 与削波策略。在非交互环境请求 PCM24 且可能削波时,需要显式选择处理方式:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf `
|
||||
--binaural-format int24 --clip-action abort
|
||||
```
|
||||
|
||||
元数据与诊断:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --print-metadata summary
|
||||
python main.py input.m4a --metadata-only --print-metadata frames
|
||||
python main.py input.m4a --metadata-cache metadata_cache
|
||||
python main.py input.m4a --metadata-dir metadata_cache
|
||||
```
|
||||
|
||||
### E-AC-3 解码级动态范围与电平
|
||||
|
||||
FFmpeg 解码 E-AC-3 时默认施加码流 `dynrng` 动态范围压缩(`-drc_scale 1`)。核心 5.1 PCM 是 JOC 对象重建的输入,而 `dynrng` 属于回放期增益,会被线性继承到全部对象与成品(ADM/扬声器/双耳),因此本工具默认按**全动态范围**解码:
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a # 默认:-drc_scale 0,全动态范围
|
||||
python main.py input.m4a --eac3-drc-scale 1 # 复现消费者回放(码流作者意图)
|
||||
python main.py input.m4a --eac3-drc-scale 0.5 # 施加一半
|
||||
python main.py input.m4a --eac3-target-level -27 # 按码流 dialnorm 归一化电平
|
||||
```
|
||||
|
||||
- `--eac3-drc-scale`(`0`~`6`,默认 `0`)对应 FFmpeg 的 `-drc_scale`:每个 E-AC-3 block 的增益为 `dynrng 因子 ^ 该值`。`0` 关闭 DRC;`1` 为码流作者意图;`>1` 非对称(响处全压、轻处增强)。
|
||||
- `--eac3-target-level`(`-31`~`0`,默认 `0` 不施加)对应 FFmpeg 的 `-target_level`:按每帧 dialnorm 施加静态增益,约 `target_level - dialnorm` dB,与 `--eac3-drc-scale` 相互独立、可叠加。dialnorm 是逐码流属性,取值随码流而异,因此 `-27` 对应的实际衰减也随码流变化。
|
||||
- 电平变化是预期的:是否施加增益取决于码流 `dynrng`,方向与幅度逐码流而异,`.report.json` 的 `output_clip.peak` 与 int24 削波判定会随之变化。
|
||||
- `--gain-db` 是**重建之后**的静态增益(双耳路径 float64),与解码级 DRC 不是一回事;解码级 DRC 是按 block 时变的,不要用 `--gain-db` 去抵消它。
|
||||
- `.report.json` 的 `ffmpeg` 字段记录 FFmpeg 版本与实际下发的解码选项(`version`、`eac3_decode_options`)。
|
||||
|
||||
### 双耳渲染模式
|
||||
|
||||
`--binaural-mode off|near|mid|far` 选择双耳渲染模式,默认 `mid`,两种输出共用这一个选项:
|
||||
|
||||
- **直接双耳渲染**(`--binaural`):`off` 不可用(报错),near/mid/far 生效,默认 `mid`;
|
||||
- **ADM BWF**:DBMD segment 10 中后 15 个 JOC 对象的 binaural render mode 写
|
||||
`off=0/near=1/far=2/mid=3`,前 10 个 bed 保持不变,默认 `mid`;`off` 用于显式
|
||||
关闭双耳元数据提示。
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --binaural-mode mid
|
||||
python main.py input.m4a --binaural-mode off # 仅 ADM BWF:关闭 DBMD 双耳提示
|
||||
```
|
||||
|
||||
**默认 `mid` 是本工具人为指定的渲染提示**,不是从输入 E-AC-3 JOC 码流中提取或
|
||||
还原的原始双耳元数据,也不代表原始混音中各对象的双耳设置。该提示不改变 PCM、
|
||||
对象轨迹或直接扬声器渲染。输出旁的 `.report.json` 用 `binaural_mode`(模式名)
|
||||
和 `binaural_mode_value`(ADM 编码值,直接双耳输出时为 `null`)记录。
|
||||
|
||||
### OAMD 时间对齐
|
||||
|
||||
对象轨迹和直接扬声器渲染的 metadata delay 默认均为 `1473 samples`。该值描述 decoder 输出 PCM 与 OAMD 更新之间的理论时间映射;扬声器 renderer 仍使用现有的 32-sample control block,因此默认更新的实际 block boundary 为 `1472`:
|
||||
|
||||
```text
|
||||
align32(1473) = 1472
|
||||
```
|
||||
|
||||
可分别用 `--object-delay-samples` 和 `--speaker-metadata-offset` 覆盖默认值。这里的 1473 不应与 inverse-QMF 的 640 项 filter/window state 混淆;后者是 QMF 状态长度,不是 metadata delay。
|
||||
|
||||
直接双耳路径使用 `--object-delay-samples`。每个 ID11/OAMD event 先按 frame start、
|
||||
outer subpayload offset 与 block offset 落到绝对 sample timeline,再加该 delay;每个
|
||||
1536-sample 输入帧按三个连续 512-sample block 处理,并在每块的绝对起始 sample
|
||||
查询插值后的位置、更新方向和 profile。
|
||||
|
||||
### 双耳计算
|
||||
|
||||
双耳路径的 QMF、hybrid、方向场、距离、ITD、room、上述 512-sample 参数更新和所选滤波器组的延迟补偿(默认 961,`--rosella-filterbank 2` 为 448 samples)见[双耳渲染数学](docs/binaural.md)。
|
||||
|
||||
### Rosella 滤波器组
|
||||
|
||||
`--rosella-filterbank 1|2` 选择 `.personalized_headphone` 路径使用的滤波器组:
|
||||
|
||||
- `1`(默认):公开滤波器组,13-tap hybrid / 6-slot 高频延迟,961 samples 延迟;
|
||||
- `2`:从参考渲染器捕获的 Rosella 滤波器组,11-tap hybrid / 3-slot 高频延迟,
|
||||
448 samples 延迟。
|
||||
|
||||
该选项只作用于 Rosella 模型;SOFA 与 `.jochrtf` 路径给出时报错。两套实现的参数、
|
||||
缓冲、延迟与验证见 [Rosella 滤波器组](docs/rosella_filterbank.md)。
|
||||
|
||||
更多参数可查看:
|
||||
|
||||
```powershell
|
||||
python main.py --help
|
||||
```
|
||||
|
||||
未指定 `-o` 时,输出仍写入仓库根目录的 `output/`。这是文件移动后特意保持的原有行为。
|
||||
|
||||
## 扬声器布局与声道顺序
|
||||
|
||||
`--speaker-layout` 只接受下表 12 个布局,PCM/WAV 的声道顺序就是表里的顺序:
|
||||
`--speaker-layout` 只接受下表 11 个布局,PCM/WAV 的声道顺序就是表里的顺序:
|
||||
|
||||
| 布局 | 声道顺序 |
|
||||
|---|---|
|
||||
@@ -107,7 +217,6 @@ joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打
|
||||
| 3.1 | L, R, C, LFE |
|
||||
| 4.0 | L, R, Lrs, Rrs |
|
||||
| 5.1 | L, R, C, LFE, Ls, Rs |
|
||||
| 6.0 | L, R, Ls, Rs, Lrs, Rrs |
|
||||
| 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs |
|
||||
| 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm |
|
||||
| 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr |
|
||||
@@ -123,146 +232,60 @@ joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打
|
||||
PCM 声道顺序严格按上表,不按掩码位序重排。播放器、ASIO、DAW 或其他设备需要按声道编号自行完成
|
||||
输出映射;本项目保证各 PCM 声道的内容和编号符合上表,不保证下游播放器自动识别杜比扬声器位置。
|
||||
|
||||
`--speaker-layout` 只接受上表 12 个名字;其它名字按未知布局报错。
|
||||
命令行只接受上表 11 个名字;其它名字按未知布局拒绝。
|
||||
|
||||
扬声器渲染从 OAMD 元数据读取 warp、trim 和 front/back balance;命令行不提供 warp 覆盖。
|
||||
warp 0/1 直接渲染;warp 2/3 先渲染到 7.1.x 中间布局,再按 PLIIx 或 Standard(Lo/Ro)
|
||||
折回 5.1.x,校准参数始终按最终目标布局选择。原始对象轨迹与双耳渲染不预处理这些控制,
|
||||
参数与求值顺序见[数学说明 §12](docs/math.md)。
|
||||
|
||||
扬声器渲染还会应用 OAMD 的对象属性:每块增益/inactive、zone/elevation、size/snap/divergence、
|
||||
priority 尺寸预算、element 5 位置细化、ISF 基矩阵和 bed distribute。ADM 导出与双耳路径仍按点
|
||||
对象放置,不应用这些扬声器目标系数。
|
||||
|
||||
对象位置的调度按完整 `frame_state` 进行:扬声器与双耳渲染都接受多 position block 和超过 16
|
||||
个对象的节目,按各块的时间与 ramp 逐个调度;双耳渲染的音频输入仍是 1 LFE + 15 对象,多出的
|
||||
位置轨道只跟踪不参与声像。ADM 导出仍使用 15 轨、单 block 的对象位置视图,超出时按变体报错。
|
||||
|
||||
## 库集成
|
||||
|
||||
引擎与文件任务使用 `joc_core.h`:`joc_task_validate` / `joc_task_execute` 跑一个文件任务并
|
||||
通过回调返回状态事件,`joc_task_result` 给出帧数、峰值、字节数与 SHA-256。
|
||||
|
||||
播放器或解码组件使用 `joc_stream.h`:调用方按自己的节奏推入 E-AC-3 字节与对应的核心 PCM
|
||||
(或已重建的 objects16),再拉取渲染后的 PCM;分块粒度任意,输出与文件任务逐字节一致。
|
||||
|
||||
```c
|
||||
joc_stream_config config = {0};
|
||||
config.struct_size = sizeof(config);
|
||||
config.input = JOC_STREAM_IN_EAC3; /* 或 JOC_STREAM_IN_PCM_OBJECTS16 */
|
||||
config.output = JOC_STREAM_OUT_SPEAKER; /* 或 BINAURAL / PCM_OBJECTS16 */
|
||||
config.speaker_layout_name = "5.1";
|
||||
joc_stream* stream = NULL;
|
||||
joc_stream_create(&config, &stream);
|
||||
/* 循环:joc_stream_push(...) / joc_stream_pull(...) */
|
||||
joc_stream_flush(stream);
|
||||
joc_stream_destroy(stream);
|
||||
```
|
||||
|
||||
契约:状态实例私有、可并存;同一实例的 push/pull 必须在同一线程;渲染是有状态的,
|
||||
因此**本版本不提供 seek**——定位需要从流起点重新解码。扬声器/双耳通路的内核延迟为
|
||||
961 样本,`joc_stream_flush` 负责排空双耳房间尾音。
|
||||
|
||||
## 目录
|
||||
## 目录结构
|
||||
|
||||
```text
|
||||
include/ 公共 C ABI:joc_core.h(引擎/文件任务)、joc_stream.h(流式)、eac3joc_core.h(上游 ABI)
|
||||
src/ 实现:eac3_transport、emdf、joc_bitstream、joc_core、oamd、timeline、speaker、
|
||||
binaural、hrtf、adm、io、telemetry、task、stream、api、cli、simd
|
||||
tests/ 单元测试(CTest,自足,不需要外部素材)
|
||||
docs/ 数学说明、双耳渲染流程与 SIMD 派发
|
||||
JustOneCacophony/
|
||||
├─ main.py 命令行启动入口
|
||||
├─ src/ Python 实现模块
|
||||
├─ data/ Python 运行时表数据
|
||||
├─ HRTF/ 用户 HRTF 数据目录(按需创建,git 忽略)
|
||||
├─ output/ 输出目录(按需创建;.jochrtf 缓存默认在其 hrtf-cache 子目录)
|
||||
├─ docs/ 数学与双耳文档(中英文)
|
||||
├─ requirements.txt Python 依赖
|
||||
├─ README.md 中文说明
|
||||
└─ README.en.md English documentation
|
||||
```
|
||||
|
||||
`src/` 下的 QMF/双耳表与 JOC Huffman 表按公开标准实现;来源记录见
|
||||
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。
|
||||
|
||||
ISF 压缩基系数与布局描述以查表方式提供:表数据写在生成头文件 `src/oamd/isf_tables.h` 中,
|
||||
与代码一起编译进二进制,运行时不读取数据文件。gain、size、priority、位置、divergence
|
||||
与差分/细化值都在代码中计算,size 核由扬声器布局的 region 几何生成,bed 路由由布局表推导。
|
||||
|
||||
## 数学实现
|
||||
|
||||
主要文档化阶段:
|
||||
核心过程包括:
|
||||
|
||||
- dense JOC 差分还原与去量化;
|
||||
- 参数带到 64 个 QMF 子带的映射;
|
||||
- 跨帧参数插值(无上一帧时硬切,对象消失后按零矩阵发布);
|
||||
- 帧级 JOC 状态机(正常 / 保持 / 硬切 / 核心直通);
|
||||
- 跨帧参数插值;
|
||||
- analysis / inverse QMF、环绕声道延迟与 FIR 状态;
|
||||
- LFE 1217-sample 延迟与逐点 clipgain 斜坡;
|
||||
- LFE 1217-sample 延迟;
|
||||
- OAMD Q15 坐标转换;
|
||||
- 基于目标布局 region 的等功率声像;
|
||||
- OAMD 元数据应用(每块增益与 inactive、zone/elevation、size 核、snap、divergence、位置细化、priority 尺寸预算、ISF 矩阵、bed 分配);
|
||||
- 布局位置补偿与逐样本增益斜坡;
|
||||
- float32 与 PCM24 输出量化;
|
||||
- Rosella 与编译好的 `.jochrtf` 双耳渲染,含 exactly-once delay/phase 与 special LFE。
|
||||
- SOFA canonical importer、完整HRIR方向插值、显式64-QMF/77-hybrid压缩导出、`36×2×77`五阶方向field、exactly-once delay/phase与special LFE。
|
||||
|
||||
解码与渲染所用的公式见[数学说明](docs/math.md)。
|
||||
解码与渲染过程使用的公式见[数学说明](docs/math.md)。
|
||||
|
||||
## 兼容性说明
|
||||
## 已知限制
|
||||
|
||||
### `joc_clipgain` 的施加方式
|
||||
- 当前只覆盖常见 continuous EMDF transport;跨多个 audio-block skip field 的碎片化 transport 尚未覆盖。
|
||||
- 覆盖 `joc_dmx_config_idx` 0..4 的全部档位($N=5,7,7,5,7$,相位开关 $\sigma(k)$ 同时门控 Ls/Rs 的 $-j$ 旋转与 band-0 FIR)。7 通道档位必须解出 8 通道床,且需要依赖子流的 `chanmap` 才能确定额外那一对:缺少核心子流或缺少 `chanmap` 仍按变体报错;`chanmap` 声明的额外对与 $k$ 不一致、或 OAMD bed 掩码与 $k$ 不一致时,只记入 `diagnostics` 并继续解码——额外那对的**身份**只进元数据、不进 DSP(§10.1),床映射按 `chanmap` 的通道码做。
|
||||
- 同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时(其中一个容器可能不是本帧的 JOC 参数:ID14 声明的对象数超出其字节所能容纳的范围,ID11 也可能没有 groupid),按 ETSI TS 103 420 §8.2 + Table 56 的关联规则打分选取:**OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析**的容器优先,其次只看 ID14 能否完整解析;只有唯一最高分才采用,其余记入 `diagnostics`(`emdf/extra_container_ignored`),并列或全部为 0 仍报变体。
|
||||
- 额外那一对的身份以 TS 103 420 Table 47/Table 48 为准($k=1\to$Lb/Rb,$k\in\{2,4\}\to$Tfl/Tfr,$N=5,7,7,5,7$);Annex E.3 规定依赖子流的 `chanmap`(若存在)**应**指明 Table 47 的某种配置,因此 `*_5_1_2top_*`(chanmap 写 Lts/Rts)属于不满足 "shall" 的素材,我们按"记诊断 + 继续解码"容错处理。
|
||||
- 音频帧内没有任何 JOC EMDF 时(例如编码器未写首帧元数据),该帧的对象通道静音,并在 stderr 的 `[diagnostics]` 与 JSON 报告的 `diagnostics` 里记一笔。
|
||||
- 扬声器与 SOFA 双耳路径当前只覆盖普通点对象;extent、spread、diffuse、divergence、channel lock 等对象控制不在支持范围内。
|
||||
- OAMD trim element 会按声明边界校验并跳过;warp、balance 和 trim 参数不应用于当前原始对象轨迹或扬声器渲染。
|
||||
- 多数据点、少见参数带配置和特殊 OAMD 调度的覆盖度低于常见 12-band、单数据点素材。
|
||||
- `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 1 + (y/32)·2^(x−4)`),出口的
|
||||
×16 抵消 1/16,净效果是 `×g`,clamp 因此处在增益**之后**。标量按帧保存两个槽(上一帧值 → 本帧值),
|
||||
本帧前 256 个采样线性过渡、之后为常数,复位后的第一帧直接取新值;`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。
|
||||
- 扬声器 limiter 不属于当前实现的主公式。
|
||||
- SOFA importer 当前严格支持 `SimpleFreeFieldHRIR` FIR;其它 SOFA convention 需要显式 adapter。
|
||||
- 双耳 runtime 固定 48 kHz、五阶和一次选择一个 measurement-radius shell。
|
||||
- ADM 输出、扬声器布局和双耳模型仍需在更多平台与播放器上确认互操作性。
|
||||
|
||||
`joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 2^(x−4)·(1 + y/32)`,8 bit 字段
|
||||
`x:3 | y:5`,值域 0.0625..15.75)。出口的 ×16 抵消 1/16,净效果是 `×g`,因此 clamp 处在增益**之后**
|
||||
(`clamp(16·IQMF·g_interp)`)。标量按帧保存在两个槽里:本帧前 256 个采样从上一帧值线性过渡、之后为
|
||||
常数,复位后的第一帧直接取新值。`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。LFE 走独立
|
||||
通路,但使用同一个完整 `g` 的逐点斜坡,并乘在 1217-sample 延迟之前。
|
||||
## 文档
|
||||
|
||||
`object_delay_samples` 默认 **1473**,与既有实现的行为保持一致;它是可配置字段,改动它会
|
||||
改变 OAMD/ADM 时间对齐。上游调查认为该值应为 0,本项目为保持逐字节等价暂不改默认值。
|
||||
|
||||
### `joc_dmx_config_idx` 0..4
|
||||
|
||||
覆盖全部五个档位:$N=5,7,7,5,7$,相位开关 $\sigma(k)=(k\in\{3,4\})$ 同时门控 Ls/Rs 的
|
||||
$-j$ 旋转与 band-0 21-tap FIR。$N=7$ 档位必须解出 **8 通道**床,并按通道码映射:
|
||||
|
||||
| $k$ | 额外那一对 | 解码器给出 8 通道的顺序 | JOC 输入序(床下标) |
|
||||
|---|---|---|---|
|
||||
| 1 | Lb/Rb | FL FR FC LFE **BL BR** SL SR | 0 1 2 6 7 4 5 |
|
||||
| 2 / 4 | Tfl/Tfr | FL FR FC LFE SL SR TFL TFR | 0 1 2 4 5 6 7 |
|
||||
|
||||
ffmpeg 侧只在 $N=5$ 档位传 `-ac 6`;$N=7$ 档位不传 `-ac`,保留解码器自身的布局
|
||||
(`-ac 6` 会把 5.1.2/7.1 下混成 5.1,`-ac 8` 会把 5.1.2 重映射成 7.1,两者都会丢信息)。
|
||||
|
||||
**跨层一致性校验**(§11.2):子流声明的全频段通道数必须等于 $N$,$joc\_ext\_config\_idx$ 非零
|
||||
时报未映射变体,对象数超过 15 时截断到 15 并记入 diagnostics——这几项会改变 DSP 输入,不一致就
|
||||
拒绝。额外那一对的**身份**只进元数据、不进 DSP(§10.1):依赖子流 `chanmap` 与 $k$ 不一致、或
|
||||
OAMD 声明的 bed 布局可以比核心子流实际编码的声道多(例如 9.1.6 bed 只编 5.1 核心),只有核心
|
||||
声道在 bed 布局里找不到位置才记 diagnostics 并照常解码,床映射按 `chanmap` 的通道码走。
|
||||
同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时,按规格 §8.2 + Table 56 的关联规则打分:
|
||||
OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析的优先,其次只看 ID14 能否解析;唯一最高分才用,
|
||||
其余记入 diagnostics(`emdf/extra_container_ignored`)。额外那一对在 10 槽 ADM 骨架上的落位
|
||||
($k=1\to6/7$、$k\in\{2,4\}\to8/9$)会被记录,但床通道按现行契约不作为输出通道。
|
||||
|
||||
### 帧状态与 JOC 解码状态机
|
||||
|
||||
每个帧都带一个状态,非零状态按诊断报告。非零状态使该帧不能用于渲染,渲染器因此保持上一帧矩阵或输出
|
||||
核心 PCM 直通,而不还原该帧的矩阵:2 不支持的下混配置,3 对象数截断到 15,4 未知参数带索引,
|
||||
5 非法 `offset_ts`,6 字节对齐后存在尾随数据,7 ID14 载荷在 `joc_data` 内提前结束。文件任务对
|
||||
`joc_dmx_config_idx` 超出 0..4 的输入更早在元数据扫描阶段按不支持变体拒绝。状态 3 仍给出截断后的
|
||||
对象列表(供诊断),状态 2、4 在出错的头字段处停止解析,状态 7 则保留完整的头、clipgain、序号和
|
||||
对象参数,只缺对象矩阵。
|
||||
|
||||
状态 7 只在 EMDF 容器本身完整时才分类:容器长度越界、或载荷短到连 12 bit 的 `joc_header` 都放
|
||||
不下仍是传输错误,文件任务与流接口还要求同一容器里的 ID11 载荷能解析。容器完整而载荷在
|
||||
`joc_data` 中提前结束的帧按无效帧处理(保持上一帧矩阵),文件任务和流接口因此继续解码而不中断,
|
||||
流接口统计里的 `truncated_joc_frames` 记录这类帧,`joc_check_id14_padding` 对它们仍报
|
||||
`JOC_ERR_BITSTREAM_TRUNCATED`。
|
||||
|
||||
渲染随后按帧级状态机运行:正常、保持、硬切、核心直通。正常渲染期间出现 JOC 参数不可用的帧时
|
||||
进入保持,继续发布上一帧末的矩阵;连续六个不可用帧只把内部保持状态推进一档、仍保持;出现有效
|
||||
帧后重新对齐序号。渲染器离开初始状态后序号回到 0、序号不匹配、或声明的核心声道数变化都
|
||||
触发硬切:所有对象矩阵清零、每个对象的 previous 状态置零,核心声道数变化还清空 LFE 延迟环
|
||||
但保留 clipgain 斜坡状态。首个有效帧之前,渲染器输出核心 PCM 直通。
|
||||
|
||||
音频帧内完全没有 JOC EMDF 时(例如编码器没写首帧元数据),该帧一律走直通路径:8 路核心平面
|
||||
L、R、C、LFE、Ls、Rs、(Lrs, Rrs) 按 identity 写入输出槽 0..7,每路延迟 1217 采样,槽 8..15
|
||||
保持零,8 平面历史照常推进。这类帧在统计里单独计数;不含 JOC 元数据(ID11 + ID14)的 E-AC-3
|
||||
输入不在支持范围。ID14 载荷在 `joc_data` 内提前结束的帧不属于这一类:它走上面的保持路径,
|
||||
在流接口统计里单独计数(`truncated_joc_frames`)。保持或直通期间没有新参数时,分析标量与 LFE 斜坡沿用当前保持的 clipgain。
|
||||
|
||||
## 许可
|
||||
|
||||
MIT,见 [LICENSE](LICENSE);第三方来源与专利边界见
|
||||
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。
|
||||
- [数学说明](docs/math.md) · [English](docs/math.en.md)
|
||||
- [双耳渲染](docs/binaural.md) · [English](docs/binaural.en.md)
|
||||
- [Rosella 滤波器组](docs/rosella_filterbank.md) · [English](docs/rosella_filterbank.en.md)
|
||||
+9
-16
@@ -1,24 +1,26 @@
|
||||
# Third-party notices / 第三方通知
|
||||
|
||||
本文件记录 64-QMF / 77-hybrid 滤波器组表(`src/hrtf/public_filterbank.h`、
|
||||
`src/joc_core/qmf_tables.h`)与 JOC Huffman 表(`src/joc_bitstream/joc_huffman_tables.h`)
|
||||
本文件记录 `data/rosella_kernels.npz`(`src/public_filterbank.py` 使用的滤波器组表)
|
||||
的公开标准来源,以及 HRTF 数据与专利的边界说明。
|
||||
|
||||
## 公开标准来源
|
||||
|
||||
64-QMF → 77-hybrid 结构与 13-tap 低带 prototype 定义于
|
||||
[3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf)
|
||||
第 5.2.2 节(Table 1 的 $Q=8$/$Q=4$ 系数,delay 6):
|
||||
第 5.2.2 节(Table 1 的 $Q=8$/
|
||||
$Q=4$ 系数,delay 6):
|
||||
|
||||
$$G_q^p[n] = g^p[n]\cdot\exp\Bigl(j\,\frac{2\pi}{Q^p}\bigl(q+\tfrac12\bigr)(n-6)\Bigr)$$
|
||||
|
||||
64-band QMF analysis 即 ISO/IEC 14496-3/AMD1:2003 第 4.B.18.2 节的 MPEG-4
|
||||
AAC/SBR 64 complex QMF bank;打包的 $64\times10$ 表是公开 640-tap prototype 的多相重排:
|
||||
AAC/SBR 64 complex QMF bank;打包的 $64\times10$ 表是公开 640-tap prototype 的
|
||||
多相重排:
|
||||
|
||||
$$A_{r,t} = \frac{(-1)^t}{128}\,c_{63-r+64t}$$
|
||||
|
||||
QMF synthesis 表为 analysis 多相矩阵 $\mathbf{A}$ 的因果左逆
|
||||
$\mathbf{A}\,\mathbf{W}=\mathbf{P}$($\mathbf{P}$ 为 577-sample 延迟置换;
|
||||
$\mathbf{A}\,\mathbf{W}=\mathbf{P}$(
|
||||
$\mathbf{P}$ 为 577-sample 延迟置换;
|
||||
全链 $961 = 577 + 6\times64$),rank-4 分解存储:
|
||||
|
||||
$$W_{b,l} = \sum_{r=1}^{4} t_{b,l,r}\,\mathbf{b}_{b,r}^{\top}$$
|
||||
@@ -29,19 +31,10 @@ $$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qqua
|
||||
|
||||
相同数值可在 FFmpeg(`aacps_tablegen.h`、`aacsbrdata.h`)等公开实现中查到。
|
||||
|
||||
### AC-3 / E-AC-3 传输层与通道码表
|
||||
|
||||
`src/eac3_transport/eac3_reader.cpp` 的 AC-3 同步帧长度表由 A/52 的
|
||||
`fscod`/`frmsizecod` 比特率表换算得到(48/44.1/32 kHz 三列),E-AC-3 帧长用标准
|
||||
`frmsiz` 字段;`src/joc_bitstream/joc_parser.cpp` 的 16 bit `chanmap` 位表与 A/52
|
||||
`acmod` 通道序来自 **ATSC A/52 Annex E**(等同 ETSI TS 102 366 Annex E)的
|
||||
Table E2.5 / `chanmap` 定义,位置命名沿用 ETSI TS 103 420 的 JOC 名称
|
||||
(Lb/Rb、Tfl/Tfr)。这些表是按公开标准写的数值,不含任何第三方实现代码。
|
||||
|
||||
## HRTF 数据与 `.jochrtf`
|
||||
|
||||
`.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发仍受源
|
||||
数据集许可约束,权限不明确时应作为私有 cache 保存。本仓库不分发任何 HRTF 数据集。
|
||||
`.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发
|
||||
仍受源数据集许可约束,权限不明确时应作为私有 cache 保存。
|
||||
|
||||
## 专利说明
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
# Python runtime tables
|
||||
|
||||
[中文](README.md)
|
||||
|
||||
This directory contains static production tables. It does not contain user HRTFs.
|
||||
|
||||
`tables.npz` contains the JOC core decoding tables:
|
||||
|
||||
```text
|
||||
analysis_window float64[10,64]
|
||||
qmf5_window float64[640]
|
||||
joc_huff_code_coarse_generic int64[95,2]
|
||||
joc_huff_code_fine_generic int64[191,2]
|
||||
joc_huff_code_coarse_coeff_sparse int64[95,2]
|
||||
joc_huff_code_fine_coeff_sparse int64[191,2]
|
||||
joc_huff_code_5ch_pos_index_sparse int64[4,2]
|
||||
joc_huff_code_7ch_pos_index_sparse int64[6,2]
|
||||
```
|
||||
|
||||
`src/joc_qmf.py` loads the QMF tables, while `src/joc_decode.py` loads the JOC Huffman trees.
|
||||
|
||||
## Binaural rendering tables
|
||||
|
||||
`rosella_kernels.npz` contains the fixed 64-QMF/77-hybrid tables used by
|
||||
`.jochrtf` rendering and personalized bank 1. Full SOFA rendering uses PCM convolution:
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
qmf_analysis_coefficients float32[64,10]
|
||||
hybrid_analysis_low_kernel float32[3,2,13,16,2]
|
||||
hybrid_synthesis_indices int16[154,4]
|
||||
hybrid_synthesis_values float32[154]
|
||||
qmf_synthesis_basis float64[64,4,128]
|
||||
qmf_synthesis_taps float64[64,10,4]
|
||||
```
|
||||
|
||||
The float32 table values are promoted to float64 when loaded.
|
||||
`src/public_filterbank.py` verifies the archive and every array by SHA-256.
|
||||
Those hashes, the table version, and the 77 reference band-center values all
|
||||
participate in the `.jochrtf` cache key. The full analysis/synthesis latency is
|
||||
961 samples.
|
||||
|
||||
The packaged tables implement publicly standardized filter banks, computable
|
||||
from the following formulas.
|
||||
|
||||
The 64-QMF → 77-hybrid structure, the 13-tap low-band prototypes, and their
|
||||
half-bin complex modulation are defined in
|
||||
[3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf),
|
||||
Section 5.2.2 (Table 1 $Q=8$/$Q=4$ coefficients, delay 6):
|
||||
|
||||
$$G_q^p[n] = g^p[n]\cdot\exp\!\Bigl(j\,\frac{2\pi}{Q^p}\bigl(q+\tfrac12\bigr)(n-6)\Bigr),\qquad n=0,\dots,12$$
|
||||
|
||||
The 64-band QMF analysis is the MPEG-4 AAC/SBR 64 complex QMF analysis bank of
|
||||
ISO/IEC 14496-3/AMD1:2003, subclause 4.B.18.2; the packaged $64\times10$ table
|
||||
is the polyphase reordering of the public 640-tap prototype $c_0,\dots,c_{639}$:
|
||||
|
||||
$$A_{r,t} = \frac{(-1)^t}{128}\,c_{63-r+64t},\qquad r=0,\dots,63,\ t=0,\dots,9$$
|
||||
|
||||
The QMF synthesis table is the causal left inverse of the analysis polyphase
|
||||
matrix $\mathbf{A}$, i.e. the solution of $\mathbf{A}\,\mathbf{W}=\mathbf{P}$
|
||||
($\mathbf{P}$ is the 577-sample delay permutation; total latency
|
||||
$961 = 577 + 6\times64$), stored as a rank-4 factorization:
|
||||
|
||||
$$W_{b,l} = \sum_{r=1}^{4} t_{b,l,r}\,\mathbf{b}_{b,r}^{\top}$$
|
||||
|
||||
The hybrid synthesis table is the 77→64 recombination: identity for the high
|
||||
bands, $Y_{3+b}=X_{16+b}$, and for the low bands ($C_p$ is the $8+4+4$ child
|
||||
partition):
|
||||
|
||||
$$Y_p = \sum_{q\in C_p}\Bigl(\operatorname{Re}X_q + j\,s_q\,\operatorname{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$
|
||||
|
||||
The same values also appear in other public implementations of these standards
|
||||
(for example FFmpeg's `aacps_tablegen.h` and `aacsbrdata.h`).
|
||||
|
||||
### Captured Rosella filterbank
|
||||
|
||||
`rosella_captured_kernels.npz` holds the bank the reference headphone renderer
|
||||
actually runs (`--rosella-filterbank 2`):
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
qmf_prototype float32[64,10]
|
||||
rotation complex64[64]
|
||||
hybrid complex64[22]
|
||||
```
|
||||
|
||||
The table values are promoted to float64/complex128 on load. The full
|
||||
analysis/synthesis latency is 448 samples (961 for the public bank). These
|
||||
values come from runtime buffer captures of the reference renderer, not from the
|
||||
public standards above; the C++ core compiles the same data in as
|
||||
`src/hrtf/rosella_filterbank_tables.h`. This file also contains no personalized
|
||||
HRTF or room tuning.
|
||||
|
||||
Public availability of a standard does not by itself grant permission to
|
||||
practice related patent claims.
|
||||
|
||||
SOFA is the user-visible source of truth. A `.jochrtf` file is a disposable JOC
|
||||
compiled HRTF cache that can be rebuilt from SOFA. The cache contains
|
||||
transformed source-HRTF data and remains subject to the source SOFA/HRTF
|
||||
dataset's licence and redistribution restrictions.
|
||||
@@ -0,0 +1,87 @@
|
||||
# Python 运行时表
|
||||
|
||||
[English](README.en.md)
|
||||
|
||||
本目录保存 Python 生产路径使用的静态表数据,不保存用户 HRTF。
|
||||
|
||||
`tables.npz` 保存 JOC 核心解码表:
|
||||
|
||||
```text
|
||||
analysis_window float64[10,64]
|
||||
qmf5_window float64[640]
|
||||
joc_huff_code_coarse_generic int64[95,2]
|
||||
joc_huff_code_fine_generic int64[191,2]
|
||||
joc_huff_code_coarse_coeff_sparse int64[95,2]
|
||||
joc_huff_code_fine_coeff_sparse int64[191,2]
|
||||
joc_huff_code_5ch_pos_index_sparse int64[4,2]
|
||||
joc_huff_code_7ch_pos_index_sparse int64[6,2]
|
||||
```
|
||||
|
||||
`src/joc_qmf.py` 读取 QMF 表,`src/joc_decode.py` 读取 JOC Huffman 树。
|
||||
|
||||
## 双耳渲染表
|
||||
|
||||
`rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表:
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
qmf_analysis_coefficients float32[64,10]
|
||||
hybrid_analysis_low_kernel float32[3,2,13,16,2]
|
||||
hybrid_synthesis_indices int16[154,4]
|
||||
hybrid_synthesis_values float32[154]
|
||||
qmf_synthesis_basis float64[64,4,128]
|
||||
qmf_synthesis_taps float64[64,10,4]
|
||||
```
|
||||
|
||||
float32 表值载入后提升为 float64。`src/public_filterbank.py` 在读取时校验 archive
|
||||
及每个数组的 SHA-256;这些 hash、table version 和 77 个 band-center 参考值共同进入
|
||||
`.jochrtf` cache key。analysis/synthesis 全链 latency 为 961 samples。
|
||||
|
||||
打包表实现的是公开标准化的滤波器组,各表可由如下公式计算。
|
||||
|
||||
64-QMF → 77-hybrid 结构、13-tap 低带 prototype 与半 bin 复调制定义于
|
||||
[3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf)
|
||||
第 5.2.2 节(Table 1 的 $Q=8$/$Q=4$ 系数,delay 6):
|
||||
|
||||
$$G_q^p[n] = g^p[n]\cdot\exp\!\Bigl(j\,\frac{2\pi}{Q^p}\bigl(q+\tfrac12\bigr)(n-6)\Bigr),\qquad n=0,\dots,12$$
|
||||
|
||||
64-band QMF analysis 即 ISO/IEC 14496-3/AMD1:2003 第 4.B.18.2 节的 MPEG-4
|
||||
AAC/SBR 64 complex QMF bank;打包的 $64\times10$ 表是公开 640-tap prototype
|
||||
$c_0,\dots,c_{639}$ 的多相重排:
|
||||
|
||||
$$A_{r,t} = \frac{(-1)^t}{128}\,c_{63-r+64t},\qquad r=0,\dots,63,\ t=0,\dots,9$$
|
||||
|
||||
QMF synthesis 表为上述 analysis 多相矩阵 $\mathbf{A}$ 的因果左逆,即求解
|
||||
$\mathbf{A}\,\mathbf{W}=\mathbf{P}$($\mathbf{P}$ 为 577-sample 延迟置换;
|
||||
全链 $961 = 577 + 6\times64$),以 rank-4 分解形式存储:
|
||||
|
||||
$$W_{b,l} = \sum_{r=1}^{4} t_{b,l,r}\,\mathbf{b}_{b,r}^{\top}$$
|
||||
|
||||
hybrid synthesis 表为 77→64 重组:高频带恒等 $Y_{3+b}=X_{16+b}$;低频带
|
||||
($C_p$ 为 $8+4+4$ 子带划分):
|
||||
|
||||
$$Y_p = \sum_{q\in C_p}\Bigl(\operatorname{Re}X_q + j\,s_q\,\operatorname{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$
|
||||
|
||||
相同数值可在 FFmpeg(`aacps_tablegen.h`、`aacsbrdata.h`)等公开实现中查到。
|
||||
|
||||
### Rosella 捕获版滤波器组
|
||||
|
||||
`rosella_captured_kernels.npz` 保存参考头戴渲染器实际运行的那套滤波器组
|
||||
(`--rosella-filterbank 2`):
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
qmf_prototype float32[64,10]
|
||||
rotation complex64[64]
|
||||
hybrid complex64[22]
|
||||
```
|
||||
|
||||
表值载入后提升为 float64/complex128;全链 latency 为 448 samples(公开版为 961)。
|
||||
这些数值来自参考渲染器的运行时缓冲捕获,不是上述公开标准的推导值;C++ 内核以
|
||||
`src/hrtf/rosella_filterbank_tables.h` 的形式内置同一组数据。文件同样不含个性化
|
||||
HRTF 或 room 调参。
|
||||
|
||||
标准可公开获取不等于获准实施相关专利。
|
||||
|
||||
`.jochrtf` 是可删除、可从 SOFA 重建的 JOC compiled HRTF cache。cache 含有源 HRTF 的变换数据,仍受源 SOFA/HRTF
|
||||
数据集的许可与再分发限制约束。
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+65
-188
@@ -1,212 +1,89 @@
|
||||
# Binaural rendering
|
||||
|
||||
[中文](binaural.md) · [Back to README](../README.en.md)
|
||||
[中文](binaural.md) · [README](../README.en.md)
|
||||
|
||||
JustOneCacophony's binaural backend supports two HRTF sources: the Rosella
|
||||
`.personalized_headphone` model exported by Dolby's official personalization scan
|
||||
(its JSON parsing is implemented by this project and invokes no Dolby software),
|
||||
and an offline compiled `.jochrtf` directional-field cache. A `.jochrtf` file is
|
||||
a disposable, reproducible JOC compiled HRTF cache, not an interchange format.
|
||||
JOC renders binaural audio from two kinds of input:
|
||||
|
||||
```text
|
||||
.jochrtf
|
||||
-> 77-band directional field + delay model
|
||||
-> 64-QMF / 77-hybrid analysis
|
||||
-> per-object direct + optional early reflections
|
||||
-> optional shared unitary-FDN late room
|
||||
-> stereo float64
|
||||
```
|
||||
| Input | Renderer | Distance and room |
|
||||
|---|---|---|
|
||||
| `.personalized_headphone` (default) | Rosella DSP, filterbank and model parameters | the model's own Near/Mid/Far and room handling, unchanged |
|
||||
| `.jochrtf` (reserved) | 77-band directional field, 64-QMF/hybrid analysis and synthesis | direct sound only by default; `--jochrtf-room on` adds the project room |
|
||||
|
||||
## Inputs
|
||||
|
||||
The binaural backend consumes a compiled directional field (a JOC compiled HRTF cache,
|
||||
`.jochrtf`) plus the shared filterbank tables. Both are read-only inputs: this library
|
||||
performs no parsing or conversion of measurement data formats.
|
||||
A `.jochrtf` is fitted offline from SOFA measurements by a separate compiler;
|
||||
this repository loads and renders the result through `--compiled-hrtf-cache`.
|
||||
|
||||
```powershell
|
||||
joc_cli input.m4a --binaural `
|
||||
--compiled-hrtf-cache path\to\subject.jochrtf `
|
||||
--kernels path\to\rosella_kernels.npz `
|
||||
--binaural-mode mid --binaural-tail-seconds 5.0
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
```
|
||||
|
||||
The library exposes the same fields: `joc_task_config` for a file task,
|
||||
`joc_stream_config` for streaming, where `hrtf_path`, `kernels_path`,
|
||||
`binaural_mode` and `binaural_tail_seconds` configure the binaural path.
|
||||
Without an explicit input the renderer uses `HRTF/binaural.personalized_headphone`
|
||||
from the project directory. A `.jochrtf` is never generated or guessed: it has to
|
||||
be named. Binaural DSP runs in float64/complex128 and updates parameters every 128
|
||||
samples.
|
||||
|
||||
The `.jochrtf` file is an **input**, not a product of this library: compiling it from
|
||||
SOFA data or measurements belongs to the toolchain and is decoupled from this
|
||||
repository. Loading validates the member set, dtypes and shapes, C-contiguity,
|
||||
CRC-32 and a payload hash recomputed over the members (see `.jochrtf` below).
|
||||
## Point-source scale
|
||||
|
||||
`binaural_mode` is `near`, `mid` or `far` and selects one of the backend's three
|
||||
preset parameter sets; `binaural_tail_seconds` sets the room-tail length drained on
|
||||
flush (default 5.0 s).
|
||||
The `.jochrtf` route keeps the Near/Mid/Far point-source scales of about 1 m,
|
||||
2.193 m and 6.402 m, defaulting to Mid. For an ADM right/front/up position
|
||||
`p = (x, y, z)`:
|
||||
|
||||
## Binaural render mode
|
||||
```text
|
||||
q_SOFA = (y, -x, z) / ||p||
|
||||
d_nominal = reference_distance_m * ||p||
|
||||
```
|
||||
|
||||
`--binaural-mode off|near|mid|far` (default `mid`) is a **human-specified
|
||||
rendering hint**, not original binaural metadata extracted or recovered from the
|
||||
input E-AC-3 JOC bitstream:
|
||||
The reference distance only gives object coordinates a nominal metric scale: it
|
||||
does not change the field response and adds no 1/r attenuation or propagation
|
||||
delay. A coincident source keeps its last valid direction, or front when there is
|
||||
no history.
|
||||
|
||||
- Direct binaural rendering (`--binaural`): near/mid/far apply, default `mid`;
|
||||
`off` is an error;
|
||||
- ADM BWF: the low 3 binaural-render-mode bits of the last 15 JOC object entries
|
||||
in DBMD segment 10 carry `off=0/near=1/far=2/mid=3`, leaving the first 10 bed
|
||||
entries unchanged; the default is `mid`, and `off` explicitly disables the
|
||||
binaural metadata hint.
|
||||
## The `.jochrtf` room
|
||||
|
||||
## Exactly-once delay and phase
|
||||
Off by default. `--jochrtf-room on` enables the project's own room, the same algorithm
|
||||
the earlier public route used, computed in code with no asset files:
|
||||
|
||||
The compiler recognizes three mutually exclusive representations:
|
||||
- six first-order image reflections (`public_room.first_order_image_sources`, the
|
||||
original 18x18x14 m geometry);
|
||||
- a shared FDN tail (four orthogonal Hadamard feedback lines, two all-pass diffusers,
|
||||
low-pass damping, RT60 0.85 s);
|
||||
- early reflections scaled by measurement radius over path distance, late send scaled
|
||||
by mode and normalised radius.
|
||||
|
||||
1. Nonzero `Data.Delay` is external to `Data.IR`; the FIR is not de-rotated and
|
||||
runtime applies the delay once.
|
||||
2. With `Data.Delay=0` and an ordinary positive-onset HRIR, each ear's main peak
|
||||
supplies arrival time. Compilation separates it and runtime restores it once.
|
||||
The current threshold is a peak index greater than two samples.
|
||||
3. With `Data.Delay=0` and both FIRs at a shared sample-zero origin, no external
|
||||
delay is invented. The authored complex phase stays in the fifth-order field.
|
||||
It is not a measured BRIR and not an external standard room. With the switch off the
|
||||
renderer skips room processing and outputs only the field's direct binaural sound; the
|
||||
room parameters are fixed at construction, with no preset and no external file.
|
||||
|
||||
No path may add a second ear delay or phase-group delay.
|
||||
## Object input
|
||||
|
||||
## Public filterbank and directional field
|
||||
Each reconstructed frame is `[1536,16]` PCM: LFE at slot 0 and fifteen
|
||||
reconstructed object/bed slots at 1–15. The ID11 timeline feeds these directly,
|
||||
without another decode or an intermediate loudspeaker render. The existing
|
||||
1473-sample object compensation and 32-sample metadata alignment are retained.
|
||||
Parameters update every 128 samples.
|
||||
|
||||
The `.jochrtf` runtime is fixed at:
|
||||
LFE keeps its separate low-pass treatment and no room send. Multi-position blocks,
|
||||
ISF, explicit infinity and anchors requiring unavailable screen calibration are
|
||||
rejected. `finish()` drains once. `--binaural-tail-seconds` bounds filterbank and
|
||||
room draining.
|
||||
|
||||
- 48 kHz;
|
||||
- a 64-sample QMF hop;
|
||||
- 64-QMF / 77 hybrid bands;
|
||||
- 961 samples of analysis/synthesis latency;
|
||||
- fifth order, 36 terms, ACN/N3D real spherical harmonics;
|
||||
- float64 PCM, delay, SH, and room state; complex128 band transfers and spectra.
|
||||
## Compact representation
|
||||
|
||||
The `.personalized_headphone` path selects its bank rather than fixing it. The
|
||||
default is the shared public bank described here (961 samples of latency); bank
|
||||
2 is the captured Rosella bank (448 samples). Both keep their own coefficients,
|
||||
history and high-band delay, and the runtime skips the selected bank's latency
|
||||
and sizes the flush with it. See [Rosella filterbank](rosella_filterbank.en.md)
|
||||
for the parameters, the CLI switch and the validation results. The SOFA and
|
||||
`.jochrtf` banks described here are unchanged and never use the selector.
|
||||
`.jochrtf` v1 is a pickle-free NumPy ZIP holding JSON metadata, 77 band centres,
|
||||
`complex128[36,2,77]` directional coefficients, `float64[36,2]` delay coefficients
|
||||
and `float64[2,2]` bounds. Loading checks the format version, shapes and scalar
|
||||
ranges, rehashes every member, and recomputes the cache key to confirm that the
|
||||
coefficients match their configuration; a mismatch rejects the file.
|
||||
|
||||
Real and imaginary unit gains for every hybrid band pass through the same
|
||||
analysis/synthesis chain to form a 154-real-parameter impulse dictionary. The
|
||||
compiler does not sample 77 FFT bins. Defaults are `1e-3` projection ridge and
|
||||
`1e-5` SH ridge. Coincident directions are merged before a spherical-Voronoi
|
||||
weighted ridge fit.
|
||||
This repository owns the format, the cache key and the hash validation; the
|
||||
fitting compiler lives in the separate checkout and shares the same format module
|
||||
instead of duplicating it. The compact representation keeps its angular and
|
||||
band-wise approximation error and is not equivalent to per-measurement precision.
|
||||
|
||||
The fixed tables implement publicly standardized filter banks, compile into the
|
||||
sources, and are computable from the following formulas.
|
||||
|
||||
The hybrid analysis kernels are defined in [3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf),
|
||||
Section 5.2.2 (Table 1 $Q=8$/
|
||||
$Q=4$ coefficients, delay 6):
|
||||
|
||||
$$G_q^p[n] = g^p[n]\cdot\exp\Bigl(j\,\frac{2\pi}{Q^p}\bigl(q+\tfrac12\bigr)(n-6)\Bigr),\qquad n=0,\dots,12$$
|
||||
|
||||
The QMF analysis table is the MPEG-4 AAC/SBR 64 complex QMF bank of
|
||||
ISO/IEC 14496-3/AMD1:2003, subclause 4.B.18.2, stored as the polyphase
|
||||
reordering of the public 640-tap prototype $c_0,\dots,c_{639}$:
|
||||
|
||||
$$A_{r,t} = \frac{(-1)^t}{128}\,c_{63-r+64t},\qquad r=0,\dots,63,\ t=0,\dots,9$$
|
||||
|
||||
The QMF synthesis table is the causal left inverse of the analysis polyphase
|
||||
matrix $\mathbf{A}$, i.e. the solution of
|
||||
$\mathbf{A}\,\mathbf{W}=\mathbf{P}$
|
||||
($\mathbf{P}$ is the 577-sample delay permutation; total latency
|
||||
$961 = 577 + 6\times64$), stored as a rank-4 factorization:
|
||||
|
||||
$$W_{b,l} = \sum_{r=1}^{4} t_{b,l,r}\,\mathbf{b}_{b,r}^{\top}$$
|
||||
|
||||
The hybrid synthesis table is the 77→64 recombination: identity for the high
|
||||
bands, $Y_{3+b}=X_{16+b}$, and for the low bands(
|
||||
$C_p$ is the
|
||||
$8+4+4$ child partition):
|
||||
|
||||
$$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$
|
||||
|
||||
The tables are fixed at compile time, so no external archive is loaded and
|
||||
nothing enters a cache key at run time. Public availability of a standard does
|
||||
not by itself grant permission to practice related patent claims. See
|
||||
[`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md) for the sources and the
|
||||
rights boundary.
|
||||
|
||||
## `.jochrtf`
|
||||
|
||||
A `.jochrtf` file is a pickle-free compressed NumPy archive with an exact member set:
|
||||
|
||||
| key | dtype / shape |
|
||||
|---|---|
|
||||
| `metadata_json` | NumPy Unicode scalar containing JSON text (`dtype.kind == "U"`) |
|
||||
| `band_center_frequencies_hz` | little-endian `float64[77]` |
|
||||
| `coefficients` | little-endian `complex128[36,2,77]` |
|
||||
| `delay_coefficients` | little-endian `float64[36,2]` |
|
||||
| `delay_bounds` | little-endian `float64[2,2]` |
|
||||
|
||||
Metadata uses the `JOC-HRTF-CACHE` magic and records the schema, compiler and
|
||||
phase-policy versions, ACN/N3D convention, filterbank hashes, SOFA content
|
||||
SHA-256, sample rate, radius, order, both ridge values, payload hash, and fit
|
||||
report. Every setting that changes compilation participates in the cache key.
|
||||
Metadata never persists an absolute local `source_path`; it may keep a display
|
||||
name only.
|
||||
|
||||
Before constructing a field, the loader uses `allow_pickle=False` and validates
|
||||
ZIP members and expanded sizes, shapes, dtypes, byte order, contiguous layout,
|
||||
finite values, delay bounds, band centers, payload hash, and cache key. The
|
||||
writer uses a same-directory temporary file, `fsync`, a process-held OS file
|
||||
lock, and atomic `os.replace`. Its hidden `.lock` sidecar may remain and does not
|
||||
mean that a writer still owns the lock. Outdated, damaged, or mismatched
|
||||
caches cannot hit. SOFA input rebuilds an invalid cache; an explicitly selected
|
||||
cache reports the error.
|
||||
|
||||
Deleting a disk cache must not change the field or render produced from the same
|
||||
SOFA and compiler configuration.
|
||||
|
||||
A `.jochrtf` file contains directional-field coefficients and delay data
|
||||
transformed from the source HRIRs. Its reproducibility therefore does not make
|
||||
it licence-free. Creating a cache does not enlarge the rights granted by the
|
||||
source SOFA/HRTF dataset: use, copying, and redistribution remain subject to
|
||||
that dataset's terms. If those terms are unclear, keep `.jochrtf` as a private
|
||||
local cache and do not ship it with the program or another build artifact.
|
||||
`source_sha256` is only a content-integrity identifier, not proof of provenance
|
||||
or permission.
|
||||
|
||||
## JOC objects and room behavior
|
||||
|
||||
The production adapter retains the existing JOC schedule:
|
||||
|
||||
- `[1536,16]` input per frame;
|
||||
- channel 0 is special LFE and channels 1..15 are JOC objects;
|
||||
- ID11/OAMD positions use a sample-timed timeline;
|
||||
- source parameters update every 512 samples;
|
||||
- every object owns independent direct/early history while one late FDN is shared;
|
||||
- `finish()` drains early/late tails; output gain is explicit, with no implicit
|
||||
limiter or programme loudness normalization.
|
||||
|
||||
Near/Mid/Far, equal-power direct level, six first-order shoebox image sources,
|
||||
late sends, the unitary FDN, the 120–180 Hz cosine-squared LFE low-pass, and room
|
||||
calibration are JOC project-defined behavior, not constants published by SOFA or
|
||||
Dolby.
|
||||
|
||||
The binaural renderer is implemented inside this library: the filterbank, the SH
|
||||
directional-field evaluation, the per-object early/direct histories and the shared
|
||||
FDN all run in `joc_core` (the `ejoc_jochrtf_*` kernel), consuming a compiled
|
||||
directional field and 512-sample metadata updates.
|
||||
|
||||
## Technical references and rights boundary
|
||||
|
||||
- [SOFA SimpleFreeFieldHRIR convention](https://www.sofaconventions.org/mediawiki/index.php/SimpleFreeFieldHRIR)
|
||||
- [3GPP TS 26.405 / ETSI TS 126 405 (64-QMF/77-hybrid definition)](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf)
|
||||
- [Dolby binaural render-mode workflow](https://professionalsupport.dolby.com/s/article/What-is-Binaural-Render-Mode-and-how-do-the-settings-affect-my-mix)
|
||||
- [EP3090576A1](https://patents.google.com/patent/EP3090576A1/en), used only as
|
||||
architectural background for direct/early/late, subbands, and FDNs; it does
|
||||
not establish that any product uses a particular embodiment.
|
||||
|
||||
Public availability of a specification, source file, or patent document does
|
||||
not by itself authorize copying its contents, redistribution of derivatives,
|
||||
or practice of patent claims. These technical references grant no patent
|
||||
licence and make no non-infringement representation. Anyone preparing a release
|
||||
or product integration must assess the applicable data and software licences,
|
||||
patent permissions, and freedom to operate. See
|
||||
[`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md) for the public-standard
|
||||
provenance and rights boundary.
|
||||
Filterbank provenance is documented in [data notes](../data/README.en.md). See the
|
||||
[MCA paper](https://doi.org/10.1109/TASLP.2023.3313908) for the correction model.
|
||||
Algorithms are independently implemented; external measurements retain their own
|
||||
licensing terms.
|
||||
+48
-166
@@ -2,190 +2,72 @@
|
||||
|
||||
[English](binaural.en.md) · [返回 README](../README.md)
|
||||
|
||||
JustOneCacophony 的双耳后端支持两种 HRTF 来源:杜比官方软件个性化扫描导出的
|
||||
Rosella `.personalized_headphone`(JSON 解析由本项目自行实现,不调用杜比软件),以及
|
||||
离线编译好的 `.jochrtf` 方向场缓存。`.jochrtf` 是可删除、可重建的 JOC compiled HRTF
|
||||
cache,不是交换格式。
|
||||
JOC 的双耳输出有两条输入路线:
|
||||
|
||||
```text
|
||||
.jochrtf
|
||||
-> 77 带方向场 + 延迟模型
|
||||
-> 64-QMF / 77-hybrid analysis
|
||||
-> 逐对象 direct + 可选 early reflections
|
||||
-> 可选 shared unitary-FDN late room
|
||||
-> stereo float64
|
||||
```
|
||||
| 输入 | 渲染 | 距离与环境 |
|
||||
|---|---|---|
|
||||
| `.personalized_headphone`(默认) | Rosella DSP、滤波器组与模型参数 | 模型自身的 Near/Mid/Far 与房间处理,行为不变 |
|
||||
| `.jochrtf`(保留项) | 77 带方向场、64-QMF/hybrid 分析与合成 | 默认只输出直达声;`--jochrtf-room on` 开启项目房间 |
|
||||
|
||||
## 输入接口
|
||||
|
||||
双耳后端消费两种输入:已编译的方向场(JOC compiled HRTF cache,`.jochrtf`)配共享滤波器组表,
|
||||
或者是 Rosella `.personalized_headphone` 模型。两者都是只读输入,本库不做测量数据格式的解析:
|
||||
`.jochrtf` 由独立的离线编译器从 SOFA 测量数据拟合得到;本仓库负责加载和渲染
|
||||
编译结果,编译器产出的缓存用 `--compiled-hrtf-cache` 加载。
|
||||
|
||||
```powershell
|
||||
joc_cli input.m4a --binaural `
|
||||
--compiled-hrtf-cache path\to\subject.jochrtf `
|
||||
--kernels path\to\rosella_kernels.npz `
|
||||
--jochrtf-room on `
|
||||
--binaural-mode mid --binaural-tail-seconds 5.0
|
||||
joc_cli input.m4a --binaural --personalized-headphone HRTF\binaural.personalized_headphone
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||
```
|
||||
|
||||
库接口使用同一组字段:文件任务用 `joc_task_config`,流式用 `joc_stream_config`,
|
||||
其中 `hrtf_path`、`kernels_path`、`binaural_mode`、`binaural_tail_seconds` 决定双耳通路。
|
||||
不指定输入时使用项目目录下的 `HRTF/binaural.personalized_headphone`;`.jochrtf` 只能
|
||||
显式选择,不自动生成,也不回退。双耳 DSP 全程 float64/complex128,参数每 128 样本更新。
|
||||
|
||||
`.jochrtf` 是**输入**而不是本库的产物:从 SOFA 或测量数据编译该缓存属于工具链的职责,
|
||||
与本仓库解耦。缓存加载时会校验成员集合、dtype 与形状、C 连续性、CRC-32 以及按成员重算的
|
||||
载荷哈希(见下文 `.jochrtf` 一节)。
|
||||
## 点声源参考尺度
|
||||
|
||||
`binaural_mode` 取 `near`、`mid`、`far`,选择后端的三组预置参数;`binaural_tail_seconds`
|
||||
决定 flush 时排空的房间尾音长度(默认 5.0 s)。
|
||||
`.jochrtf` 路线沿用 Near 约 1 m、Mid 约 2.193 m、Far 约 6.402 m 的点声源参考尺度,默认
|
||||
Mid。对于 ADM 右/前/上坐标 `p=(x,y,z)`:
|
||||
|
||||
## 双耳渲染模式
|
||||
```text
|
||||
q_SOFA = (y, -x, z) / ||p||
|
||||
d_nominal = reference_distance_m * ||p||
|
||||
```
|
||||
|
||||
`--binaural-mode off|near|mid|far`(默认 `mid`)是**人为指定的渲染提示**,不是
|
||||
从输入 E-AC-3 JOC 码流提取或还原的原始双耳元数据:
|
||||
参考距离只给对象坐标规定标称米制尺度,不改变方向场的响应,也不额外增加 1/r 衰减或
|
||||
传播延迟。声源与听者重合时沿用该对象最后一个有效方向,没有历史时取正前方。
|
||||
|
||||
- 直接双耳渲染(`--binaural`):near/mid/far 生效,默认 `mid`;`off` 报错;
|
||||
- ADM BWF:DBMD segment 10 中后 15 个 JOC 对象的 binaural render mode 写
|
||||
`off=0/near=1/far=2/mid=3`,前 10 个 bed 保持不变,默认 `mid`;`off` 用于显式
|
||||
关闭双耳元数据提示。
|
||||
## `.jochrtf` 房间
|
||||
|
||||
## Delay/phase:exactly once
|
||||
默认关闭。`--jochrtf-room on` 启用项目自有房间,算法与 Rosella 之前的公开路线一致,
|
||||
直接在代码里计算,不读取任何资产:
|
||||
|
||||
编译器只允许三种互斥语义:
|
||||
- 六个一阶镜像反射(`public_room.first_order_image_sources`,18×18×14 m 原房间几何);
|
||||
- 四路正交 Hadamard 反馈、两级全通扩散、低通阻尼的共享 FDN 晚期(RT60 0.85 s);
|
||||
- 早反射按测量半径/路径距离给增益,晚期发送按档位与归一化半径给增益。
|
||||
|
||||
1. 非零 `Data.Delay` 是 `Data.IR` 外部 delay;FIR 不去旋,运行时应用一次。
|
||||
2. `Data.Delay=0` 且 HRIR 有普通正 onset:以每耳 main peak 分离 arrival,拟合后
|
||||
在运行时恢复一次;当前阈值为 peak index 大于 2 samples。
|
||||
3. `Data.Delay=0` 且双耳 FIR 共享 sample-0 起点:不发明外部 delay,原 complex
|
||||
phase 直接进入五阶场。
|
||||
它不是测量 BRIR,也不是外部标准房间。关闭时跳过房间处理,只输出方向场的双耳直达声;
|
||||
房间参数在构造时确定,没有预设、没有外部文件。
|
||||
|
||||
任何路径都不能再叠加第二套 ear delay 或 phase-group delay。
|
||||
## 对象接入
|
||||
|
||||
## 公开 filterbank 与方向场
|
||||
输入为每帧 `[1536,16]` PCM:0 是 LFE,1–15 是已经重建的对象/bed 槽。直接使用 ID11
|
||||
时间线,不重新解码,也不先渲染到扬声器布局。保留 1473 样本对象时间补偿和 32 样本元数据
|
||||
对齐,参数每 128 样本更新。
|
||||
|
||||
运行时固定为:
|
||||
LFE 继续使用独立低通处理,不发送至房间。多位置块、ISF、显式无穷远以及需要屏幕校准的
|
||||
锚定明确报错。`finish()` 仅排空一次,`--binaural-tail-seconds` 规定滤波器组与房间尾声的
|
||||
排空预算。
|
||||
|
||||
- 48 kHz;
|
||||
- 64-sample QMF hop;
|
||||
- 64-QMF / 77-hybrid;
|
||||
- analysis/synthesis latency 961 samples;
|
||||
- 五阶、36 项、ACN/N3D real spherical harmonics;
|
||||
- PCM、delay、SH、room state 为 float64;频带传递和频域状态为 complex128。
|
||||
## 轻量格式
|
||||
|
||||
每个 hybrid band 的 real/imaginary 单位增益都通过同一套 analysis/synthesis 链生成
|
||||
脉冲字典,共 154 个实参数;编译不是直接读取 77 个 FFT bin。默认 projection
|
||||
ridge 为 `1e-3`,SH ridge 为 `1e-5`。同方向 measurement 先合并,再用球面 Voronoi
|
||||
面积权重做 ridge fit。
|
||||
`.jochrtf` v1 是无 pickle 的 NumPy ZIP,保存 JSON 元数据、77 个频带中心、
|
||||
`complex128[36,2,77]` 方向系数、`float64[36,2]` 延迟系数及 `float64[2,2]` 边界。加载时
|
||||
校验格式版本、形状、标量范围,按成员重算载荷哈希,并重新计算 cache key 以确认系数与
|
||||
配置一致;不匹配的文件直接拒绝。
|
||||
|
||||
固定表按公开标准化的滤波器组实现并编译进源码,各表可由如下
|
||||
公式计算。
|
||||
格式、cache key 与哈希校验由本仓库维护;拟合编译器在独立目录里,两者共用同一个格式
|
||||
模块,不复制定义。压缩表示保留自身的角向与频带近似误差,不等同于逐点测量的精度。
|
||||
|
||||
hybrid 分析核定义于 [3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf)
|
||||
第 5.2.2 节(Table 1 的 $Q=8$/
|
||||
$Q=4$ 系数,delay 6):
|
||||
|
||||
$$G_q^p[n] = g^p[n]\cdot\exp\Bigl(j\,\frac{2\pi}{Q^p}\bigl(q+\tfrac12\bigr)(n-6)\Bigr),\qquad n=0,\dots,12$$
|
||||
|
||||
QMF analysis 表即 MPEG-4 AAC/SBR(ISO/IEC 14496-3/AMD1:2003 第 4.B.18.2 节)
|
||||
的 64 complex QMF bank;打包的 $64\times10$ 表是公开 640-tap prototype
|
||||
$c_0,\dots,c_{639}$ 的多相重排:
|
||||
|
||||
$$A_{r,t} = \frac{(-1)^t}{128}\,c_{63-r+64t},\qquad r=0,\dots,63,\ t=0,\dots,9$$
|
||||
|
||||
QMF synthesis 表为上述 analysis 多相矩阵 $\mathbf{A}$ 的因果左逆,即求解
|
||||
$\mathbf{A}\,\mathbf{W}=\mathbf{P}$(
|
||||
$\mathbf{P}$ 为 577-sample 延迟置换;
|
||||
全链 $961 = 577 + 6\times64$),以 rank-4 分解形式存储:
|
||||
|
||||
$$W_{b,l} = \sum_{r=1}^{4} t_{b,l,r}\,\mathbf{b}_{b,r}^{\top}$$
|
||||
|
||||
hybrid synthesis 表为 77→64 重组:高频带恒等 $Y_{3+b}=X_{16+b}$;低频带(
|
||||
$C_p$ 为
|
||||
$8+4+4$ 子带划分):
|
||||
|
||||
$$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$
|
||||
|
||||
固定表在编译期确定,运行时不加载外部 archive,也不参与缓存键。标准可公开
|
||||
获取不等于获准实施相关专利;来源与权利边界见
|
||||
[`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md)。
|
||||
|
||||
## `.jochrtf`
|
||||
|
||||
`.jochrtf` 是无 pickle 的压缩 NumPy archive,固定包含:
|
||||
|
||||
| key | dtype / shape |
|
||||
|---|---|
|
||||
| `metadata_json` | 含 JSON 文本的 NumPy Unicode scalar(`dtype.kind == "U"`) |
|
||||
| `band_center_frequencies_hz` | little-endian `float64[77]` |
|
||||
| `coefficients` | little-endian `complex128[36,2,77]` |
|
||||
| `delay_coefficients` | little-endian `float64[36,2]` |
|
||||
| `delay_bounds` | little-endian `float64[2,2]` |
|
||||
|
||||
metadata magic 固定为 `JOC-HRTF-CACHE`,并记录 schema/compiler/phase-policy、
|
||||
ACN/N3D、filterbank table hashes、SOFA content SHA-256、采样率、radius、order、
|
||||
两个 ridge、payload hash 和 fit report。cache key 覆盖所有会改变编译结果的字段。
|
||||
metadata 不保存本机绝对 `source_path`,仅可保存 source display name。
|
||||
|
||||
loader 使用 `allow_pickle=False`,并在构造对象前检查 ZIP 成员集、解压大小、shape、
|
||||
dtype、端序、连续布局、有限值、delay bounds、band centers、payload hash 和 cache
|
||||
key。writer 使用同目录临时文件、`fsync`、进程持有的 OS 文件锁和原子
|
||||
`os.replace`;对应的隐藏 `.lock` sidecar 可保留,但不代表仍有 writer 持锁。
|
||||
旧版本、损坏或配置不匹配的 cache 不会命中,而是直接报错。
|
||||
|
||||
删除磁盘 cache 后,从同一 SOFA 和同一编译配置得到的场与渲染结果不得改变。
|
||||
|
||||
`.jochrtf` 包含由源 HRIR 变换得到的方向场系数与 delay 数据,因此“可以重建”不表示
|
||||
它不受数据许可约束。生成 cache 不会扩大源 SOFA/HRTF 数据集授予的权利;cache 的
|
||||
使用、复制和再分发仍须遵守源数据集条款。不能确认条款时,应把 `.jochrtf` 作为本地
|
||||
私有 cache,不随程序或构建产物发布。`source_sha256` 只用于内容一致性校验,不是许可
|
||||
或来源证明。
|
||||
|
||||
## Rosella 个性化模型
|
||||
|
||||
`.personalized_headphone` 走独立的 Rosella 渲染器,滤波器组可以二选一,
|
||||
由 `--rosella-filterbank 1|2`(库接口为 `joc_task_config::rosella_filterbank` /
|
||||
`joc_stream_config::rosella_filterbank`,`0` 表示默认)选择:
|
||||
|
||||
| 取值 | 滤波器组 | 低频 hybrid | 高频 slot delay | 延迟 |
|
||||
| --- | --- | --- | --- | ---: |
|
||||
| `1`(默认) | `PublicFilterbank`(与 Rosella 路径共用) | 13 taps | 6 slots | 961 samples |
|
||||
| `2` | `RosellaFilterbank`(从参考渲染器捕获) | 11 taps | 3 slots | 448 samples |
|
||||
|
||||
两套实现通过同一组四个有状态 half-step 调用,因此切换时系数、高频 delay、历史缓冲
|
||||
布局、起始延迟跳过量和 flush 容量一起改变,分析与合成两级互不共享状态。其它取值直接
|
||||
报错;非 Rosella 输入给出该选项也报错,不会静默忽略。详见
|
||||
[Rosella filterbank](rosella_filterbank.en.md)。
|
||||
|
||||
## JOC 对象与房间
|
||||
|
||||
生产适配器继续使用现有 JOC 调度:
|
||||
|
||||
- 每帧输入 `[1536,16]`;
|
||||
- channel 0 是 special LFE,channel 1..15 是 JOC objects;
|
||||
- ID11/OAMD position 使用 sample-timed timeline;
|
||||
- 每 512 samples 更新方向/profile;
|
||||
- 每个对象拥有独立 direct/early history,late FDN 全局共享;
|
||||
- `finish()` 排空 early/late tail;输出增益显式应用,不隐含 limiter 或节目响度归一化。
|
||||
|
||||
Near/Mid/Far、equal-power direct level、六面 shoebox 一阶 image source、late send、
|
||||
unitary FDN、LFE 120–180 Hz cosine-squared 低通及 room calibration 都是 JOC
|
||||
项目定义行为,不是 SOFA 或 Dolby 公布常数。
|
||||
|
||||
双耳渲染在本库内实现:filterbank、SH 方向场求值、逐对象 early/direct 历史与共享 FDN
|
||||
全部执行于 `joc_core`(`ejoc_jochrtf_*` 内核),对外只消费编译好的方向场与
|
||||
512-sample 粒度的元数据更新。
|
||||
|
||||
## 技术引用与权利边界
|
||||
|
||||
- [SOFA SimpleFreeFieldHRIR convention](https://www.sofaconventions.org/mediawiki/index.php/SimpleFreeFieldHRIR)
|
||||
- [3GPP TS 26.405 / ETSI TS 126 405(64-QMF/77-hybrid 定义)](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf)
|
||||
- [Dolby binaural render mode workflow](https://professionalsupport.dolby.com/s/article/What-is-Binaural-Render-Mode-and-how-do-the-settings-affect-my-mix)
|
||||
- [EP3090576A1](https://patents.google.com/patent/EP3090576A1/en),仅作 direct/early/late、
|
||||
subband 与 FDN 架构背景,不证明某个产品使用特定实施例。
|
||||
|
||||
规范、源码或专利文献可公开获取,不等于获准复制其内容、再分发派生产物或实施其中的
|
||||
专利权利要求。本项目的技术引用本身不授予专利许可,也不作不侵权保证;准备发布或集成
|
||||
到产品的一方应自行审查适用的数据许可、软件许可、专利许可及 freedom-to-operate。
|
||||
公开标准来源与权利边界见
|
||||
[`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md)。
|
||||
滤波器组来源见[data说明](../data/README.md)。方向插值与 MCA 的参考文献见
|
||||
[研究实现依据](https://doi.org/10.1109/TASLP.2023.3313908);算法独立实现,外部测量数据
|
||||
仍适用其自身许可。
|
||||
+17
-286
@@ -147,17 +147,15 @@ If the clipgain field consists of integer $x$ and mantissa $y$, then
|
||||
|
||||
$$
|
||||
G_{\mathrm{clip}}=
|
||||
2^{x-4}\left(1+\frac{y}{32}\right).
|
||||
1+\frac{y}{32}2^{x-4}.
|
||||
$$
|
||||
|
||||
Its range is $[0.0625,15.75]$; at $x=4$ it reduces to $1+y/32$. It is folded into the analysis-side
|
||||
scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies every core-bed sample before the QMF
|
||||
analysis; the $\times16$ after the object inverse QMF cancels the $1/16$, so the net effect is a multiply
|
||||
by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the gain. The scalar keeps two per-frame
|
||||
slots: the first 256 samples of a frame ramp linearly from the previous frame's value to this frame's
|
||||
value and the rest stays constant, and the first frame after a reset takes the new value directly. LFE
|
||||
uses a separate path but applies the same full $G_{\mathrm{clip}}$ as a sample-wise ramp before its
|
||||
1217-sample delay; see section 8.
|
||||
It is folded into the analysis-side scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies
|
||||
every core-bed sample before the QMF analysis; the $\times16$ after the object inverse QMF cancels the
|
||||
$1/16$, so the net effect is a multiply by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the
|
||||
gain. The scalar keeps two per-frame slots: the first 256 samples of a frame ramp linearly from the
|
||||
previous frame's value to this frame's value and the rest stays constant, and the first frame after a
|
||||
reset takes the new value directly. LFE bypasses this scalar.
|
||||
|
||||
## 3. Parameter-band expansion and time interpolation
|
||||
|
||||
@@ -192,13 +190,7 @@ Thus $p(b)=k$ if and only if $b\in\mathcal B_k$. Other parameter-band counts use
|
||||
|
||||
### 3.2 One-data-point interpolation
|
||||
|
||||
Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. When the object
|
||||
has no previous state yet (the first frame of the stream, or the object's first appearance), a single
|
||||
data point keeps all 24 slots at $D_{o,c,p(b)}$ (hard cut, no fade-in from zero); with two data points,
|
||||
the first 12 slots stay at $D_0$ and the last 12 slots still interpolate from $D_0$ to $D_1$ using the
|
||||
second segment of section 3.3 (with $P$ set to $D_0$).
|
||||
|
||||
With a previous state, for slot $t=0\ldots23$:
|
||||
Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. For slot $t=0\ldots23$:
|
||||
|
||||
$$
|
||||
\alpha_t=\frac{t+1}{24},
|
||||
@@ -216,31 +208,11 @@ $$
|
||||
M_{o,c,b,23}=D_{o,c,p(b)}.
|
||||
$$
|
||||
|
||||
This value then becomes the previous state for the next frame. Once an object's present flag goes from 1
|
||||
to 0, it keeps being published with an all-zero matrix and its previous state is updated to zero until it
|
||||
becomes present again.
|
||||
This value then becomes the previous state for the next frame.
|
||||
|
||||
### 3.3 Multiple data points
|
||||
|
||||
`offset_ts` gives the first slot at which each data point becomes active, using the raw 5-bit value
|
||||
(legal range $0\ldots23$); the values of one object must be strictly increasing. In the default mode
|
||||
(`slope_idx=0`), two data points $D_0$ and $D_1$ interpolate as
|
||||
|
||||
$$
|
||||
M_{o,c,b,t}=
|
||||
\begin{cases}
|
||||
(1-\alpha_t)P_{o,c,b}+\alpha_tD_{0}, & t=0\ldots10,\ \alpha_t=\dfrac{t+1}{12},\\[1mm]
|
||||
D_{0}, & t=11,\\[1mm]
|
||||
(1-\beta_t)D_{0}+\beta_tD_{1}, & t=12\ldots23,\ \beta_t=\dfrac{t-11}{12}.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
When no previous state exists, the same formulas apply with $P_{o,c,b}$ set to $D_0$.
|
||||
|
||||
Step mode (`slope_idx=1`) switches targets directly at the designated slots: for $t<o_0$ it keeps the
|
||||
previous frame-end value $P_{o,c,b}$ (or $D_0$ when no previous state exists), for $o_0\le t<o_1$ it
|
||||
uses $D_0$, and for $t\ge o_1$ it uses $D_1$; with a single data point it keeps $P$ for $t<o_0$ and uses
|
||||
$D_0$ from $o_0$ on.
|
||||
When a frame contains two data points, `offset_ts` gives the segment boundary. Each segment uses the same linear relation between the previous and next targets; step mode switches targets at the designated slot.
|
||||
|
||||
## 4. Analysis QMF for core PCM
|
||||
|
||||
@@ -365,21 +337,15 @@ where $r=0\ldots63$. Synthesis state must advance continuously by slot.
|
||||
|
||||
## 8. LFE path
|
||||
|
||||
LFE bypasses the object matrix and inverse QMF. The core LFE samples are first multiplied by the
|
||||
sample-wise gain ramp $G[n]$ (the same full $G_{\mathrm{clip}}$ as section 2.4: for $n<256$,
|
||||
$G[n]=G_{\mathrm{old}}+n(G_{\mathrm{new}}-G_{\mathrm{old}})/256$; for $n\ge256$, $G[n]=G_{\mathrm{new}}$;
|
||||
on the first frame or after a reset, $G_{\mathrm{old}}=G_{\mathrm{new}}$). The scaled samples then
|
||||
enter the 1217-sample delay, and the read-out is only clamped, with no further gain:
|
||||
LFE bypasses the object matrix and inverse QMF and uses a 1217-sample delay. After the input and output scale factors cancel:
|
||||
|
||||
$$
|
||||
y_{\mathrm{LFE}}[n]=
|
||||
\mathrm{clip}\left(
|
||||
x_{\mathrm{LFE,core}}[n-1217]\cdot G[n-1217],-1,1
|
||||
x_{\mathrm{LFE,core}}[n-1217],-1,1
|
||||
\right).
|
||||
$$
|
||||
|
||||
Here $G[k]$ uses the frame-local position of input sample $k$, i.e. $k\bmod1536$.
|
||||
|
||||
## 9. OAMD coordinates
|
||||
|
||||
The lateral and longitudinal grids use $N=62$; the height grid uses $N=15$. The quantizer is
|
||||
@@ -392,7 +358,7 @@ q_N(k)=
|
||||
\right).
|
||||
$$
|
||||
|
||||
The normalized room coordinates are
|
||||
OAR coordinates are
|
||||
|
||||
$$
|
||||
u=\frac{q_1}{32768},
|
||||
@@ -487,9 +453,7 @@ v_{\mathrm{floor}}=
|
||||
\mathrm{clamp}(2v,0,1).
|
||||
$$
|
||||
|
||||
Warp 0 and other layouts use $v_{\mathrm{floor}}=v$. The height layer is not scaled.
|
||||
An absent warp indication uses 0; no CLI override is provided. Modes 2/3 use
|
||||
the intermediate layout and folding matrix in §12.5. Balance follows this scaling.
|
||||
Other layouts use $v_{\mathrm{floor}}=v$.
|
||||
|
||||
### 10.3 Height layer
|
||||
|
||||
@@ -510,9 +474,6 @@ $$
|
||||
|
||||
## 11. Layout-dependent position compensation
|
||||
|
||||
This is the per-object gain for automatic trim. Disabled trim uses unity;
|
||||
explicit trim uses §12.2 instead, without multiplying automatic compensation again.
|
||||
|
||||
Let $N_h$ be the number of relevant height speakers and $N_f$ the number of relevant additional horizontal speakers:
|
||||
|
||||
$$
|
||||
@@ -558,188 +519,7 @@ G_{\mathrm{pos}}
|
||||
\mathbf G_{\mathrm{point}}.
|
||||
$$
|
||||
|
||||
## 12. OAMD trim and front-back balance
|
||||
|
||||
### 12.1 Configuration selection
|
||||
|
||||
The OAMD trim element splits a programme's calibration data into nine
|
||||
configurations, chosen by the target layout's additional horizontal speaker
|
||||
count $N_f$ and height speaker count $N_h$:
|
||||
|
||||
$$
|
||||
\kappa_f(N)=
|
||||
\begin{cases}
|
||||
0, & N=0,\\
|
||||
1, & N\in\{2,3\},\\
|
||||
2, & \text{otherwise},
|
||||
\end{cases}
|
||||
\qquad
|
||||
\kappa_h(N)=
|
||||
\begin{cases}
|
||||
0, & N=0,\\
|
||||
1, & N=2,\\
|
||||
2, & \text{otherwise}.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
$$
|
||||
c_{\mathrm{trim}}=\kappa_f(N_f)+3\,\kappa_h(N_h).
|
||||
$$
|
||||
|
||||
The nine configurations enumerate "no / some / many additional horizontal
|
||||
speakers by no / some / many height speakers".
|
||||
|
||||
Trim and front-back balance apply to seven layouts only: 2.0, 3.1, 4.0, 5.1,
|
||||
5.1.2, 5.1.4 and 7.1. Wider layouts (7.1.2, 7.1.4, 9.1.4, 9.1.6) are left
|
||||
untrimmed, with $g_t=1$ and $b=0$.
|
||||
|
||||
Warp, trim and balance controls are read from OAMD metadata:
|
||||
|
||||
| Parameter | Value or unit | Behavior |
|
||||
|---|---|---|
|
||||
| `warp_mode` | 0 / 1 / 2 / 3 | Direct rendering / floor Y scaling / PLIIx folding / Standard (Lo/Ro) folding |
|
||||
| `global_trim_mode` | 0 / 1 / 2 | Automatic / disabled / per-layout configuration; reserved value 3 is rejected |
|
||||
| $T_s$, $T_h$ | dB | Explicit surround and height trim, with positive values treated as zero |
|
||||
| $T_c$ | dB | Signed explicit centre trim |
|
||||
| $b_{\mathrm{lis}}$, $b_{\mathrm{tb}}$ | $[-1,1]$, step $1/16$ | Listener-plane and overhead front-back balance |
|
||||
| Per-object trim bypass | Boolean | Disables the object's balance and trim gain while retaining warp |
|
||||
|
||||
With global mode 2, the selected configuration's default or disabled flag
|
||||
chooses automatic or disabled trim. A missing configuration uses automatic;
|
||||
otherwise its explicit parameters apply. Automatic uses §11, disabled uses
|
||||
unity, and explicit uses §12.2. These modes are mutually exclusive.
|
||||
|
||||
Without an initial trim element, warp is 0 and trim is automatic. A later
|
||||
payload without that element retains the previous controls; a new element
|
||||
updates them at the corresponding metadata event boundary. The CLI has no
|
||||
warp override.
|
||||
|
||||
### 12.2 Per-object levels
|
||||
|
||||
Centre, surround and height levels parameterize a position-dependent object
|
||||
gain; they are not fixed gains applied to the output channels after mixing.
|
||||
For original Y after balance $y_t$ and $z=\max(w,0)$:
|
||||
|
||||
$$
|
||||
a_s=\mathrm{clamp}(2y_t,0,1)(1-z),\quad a_h=z,
|
||||
$$
|
||||
|
||||
$$
|
||||
a_c=1-\mathrm{clamp}\bigl(10\max(|z|-0.05,y_t-0.05,|u-0.5|-0.05),0,1\bigr).
|
||||
$$
|
||||
|
||||
Explicit trim uses
|
||||
|
||||
$$
|
||||
D=a_s\min(T_s,0)+a_h\min(T_h,0)+a_cT_c,\qquad g_t=10^{D/20}.
|
||||
$$
|
||||
|
||||
The scalar multiplies the object's whole panning vector before accumulation.
|
||||
Automatic trim uses §11; disabled trim or per-object bypass uses unity. LFE
|
||||
passes through. Gain evaluation, panning interpolation and object accumulation
|
||||
use float64.
|
||||
|
||||
### 12.3 Front-back balance
|
||||
|
||||
Each configuration also carries two front-back balance values:
|
||||
$b_{\mathrm{lis}}$ for the listener plane and $b_{\mathrm{tb}}$ for the overhead
|
||||
layer. Both are quantised to a $1/16$ grid and are non-zero only in an explicit
|
||||
configuration.
|
||||
|
||||
Writing $n=\mathrm{round}(16\,|b|)$ and $s=n/18$, the single-layer map is
|
||||
|
||||
$$
|
||||
B_b(y)=
|
||||
\begin{cases}
|
||||
y, & y\le 0.05\ \text{or}\ y\ge 0.95,\\[2pt]
|
||||
(1+s)y-0.05s, & 0.05<y<0.5,\ b>0,\\[2pt]
|
||||
(1-s)(y-0.5)+0.5+0.45s, & 0.5\le y<0.95,\ b>0,\\[2pt]
|
||||
(1-s)y+0.05s, & 0.05<y<0.5,\ b<0,\\[2pt]
|
||||
(1+s)(y-0.5)+0.5-0.45s, & 0.5\le y<0.95,\ b<0.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
It maps $[0.05,0.95]$ monotonically onto itself: the two walls stay fixed, the
|
||||
two halves meet at $y=0.5$, and their slopes are $1+s$ and $1-s$. The 5 percent
|
||||
wall bands and the case $b=0$ pass through unchanged, so the map never leaves
|
||||
the interval and needs no extra clamp.
|
||||
|
||||
The height $w$ blends the two layers:
|
||||
|
||||
$$
|
||||
y'=(1-w)\,B_{b_{\mathrm{lis}}}(y)+w\,B_{b_{\mathrm{tb}}}(y).
|
||||
$$
|
||||
|
||||
The front-back axis $y$ is oriented as in §9, with $0$ at the front wall and
|
||||
$1$ at the back wall; $x$ and $z$ are unchanged.
|
||||
|
||||
### 12.4 Where it is applied
|
||||
|
||||
Let $B_z(y)$ denote the balance map of §12.3 and $W(y)$ the warp map of §10.2.
|
||||
Direct point rendering in warp modes 0/1 keeps three coordinates:
|
||||
|
||||
$$
|
||||
y_f=B_z(W(y)),\qquad y_h=B_z(y),\qquad y_t=B_z(y).
|
||||
$$
|
||||
|
||||
Floor and height panning use $y_f,y_h$; trim evaluation uses $y_t$. The final gain is
|
||||
|
||||
$$
|
||||
g_{o,c}=G_{\mathrm{object}}\,g_t(u,y_t,z)\,P_c(u,y_f,y_h,z).
|
||||
$$
|
||||
|
||||
Warp precedes balance, followed by trim/pan gain composition, but not every
|
||||
stage shares a warped Y. Balance requires explicit trim without object bypass;
|
||||
warp remains independent of trim state and bypass. Source positions are kept
|
||||
unchanged for ADM and binaural.
|
||||
|
||||
Floor coordinates pass through warp and then balance before floor panning.
|
||||
Height and trim coordinates pass through balance alone. The trim scalar
|
||||
multiplies the object's entire panning vector before accumulation into output
|
||||
channels. LFE passes through.
|
||||
|
||||
### 12.5 Surround folding in warp modes 2/3
|
||||
|
||||
For a 5.1, 5.1.2 or 5.1.4 target, warp modes 2/3 first render objects to the
|
||||
corresponding 7.1, 7.1.2 or 7.1.4 intermediate layout, then fold the rear
|
||||
surrounds into the target surround pair. The 4.0 layout also adds a rear pair
|
||||
without adding centre or LFE. Other existing layouts render directly.
|
||||
|
||||
The intermediate layout changes speaker geometry and panning only. The trim
|
||||
configuration, both balance layers and the maximum automatic position
|
||||
compensation remain those of the final target. For example, 5.1.2 uses
|
||||
configuration 4 and a maximum compensation of $-2.25$ dB even when its
|
||||
intermediate layout is 7.1.2. With separate side and rear pairs in the
|
||||
intermediate layout, floor Y is not doubled. Balance and per-object trim
|
||||
are evaluated during intermediate rendering.
|
||||
|
||||
For intermediate outputs $L_s,R_s,L_{rs},R_{rs}$ and target surrounds
|
||||
$L'_s,R'_s$:
|
||||
|
||||
$$
|
||||
L'_s=L_s+aL_{rs}+bR_{rs},\qquad
|
||||
R'_s=R_s+bL_{rs}+aR_{rs}.
|
||||
$$
|
||||
|
||||
| Mode | $a$ | $b$ |
|
||||
|---|---|---|
|
||||
| 2: PLIIx | $\sqrt{3}/2$ | $1/2$ |
|
||||
| 3: Standard (Lo/Ro) | $1$ | $0$ |
|
||||
|
||||
Both matrices add linear amplitudes without normalizing energy after the
|
||||
fold. Shared front, height and LFE channels pass through to their matching
|
||||
positions. The folded object gain vector enters the target-gain update and
|
||||
ramp in §13 before object audio is accumulated.
|
||||
|
||||
The complete order is: select calibration for the target layout → apply
|
||||
balance and trim while panning in the intermediate layout → fold horizontal
|
||||
surrounds → update gain ramps and mix. Per-object bypass disables balance
|
||||
and trim while retaining layout selection and folding. These modes are
|
||||
generic speaker-rendering controls; the publicly defined E-AC-3 JOC warp
|
||||
values remain 0/1. Source trajectories, ADM and binaural do not bake in the
|
||||
folded result.
|
||||
|
||||
## 13. OAMD time alignment and gain ramps
|
||||
## 12. OAMD time alignment and gain ramps
|
||||
|
||||
The coded position of an OAMD update is
|
||||
|
||||
@@ -795,7 +575,7 @@ $$
|
||||
|
||||
If no new metadata update intervenes, this is equivalent to a sample-wise linear ramp of total length $KB$.
|
||||
|
||||
## 14. Final speaker mix
|
||||
## 13. Final speaker mix
|
||||
|
||||
For target output channel $c$:
|
||||
|
||||
@@ -827,58 +607,9 @@ y_{24}[n]=
|
||||
\right).
|
||||
$$
|
||||
|
||||
## 15. Scope of the formulas
|
||||
## 14. Scope of the formulas
|
||||
|
||||
- The JOC matrix section covers both the dense MTX and the sparse IDX/VEC differential syntax.
|
||||
- The speaker-panning section describes ordinary point objects; extent, spread, divergence, and similar modes require additional models.
|
||||
- Multiple OAMD position blocks must be scheduled in time order.
|
||||
- A limiter is separate post-processing and is not included in the mixing equations above.
|
||||
- Trim and front-back balance affect speaker rendering only; binaural rendering does not read them.
|
||||
|
||||
## 16. OAMD metadata application (speaker path)
|
||||
|
||||
When mapping object PCM to the target layout, speaker rendering applies the OAMD
|
||||
object attributes in addition to the geometric panning:
|
||||
|
||||
- per-block integer gain `Qg` (unity 4096) and `inactive`; the gain is applied to
|
||||
the target coefficients once, and the LFE uses its owning metadata object's
|
||||
gain/active state;
|
||||
- full basic/render status 0/1/2/3 block expansion, same-object previous-block
|
||||
inheritance, and local mask updates;
|
||||
- `zone_constraints_idx` region selection mapped through `[0,1,2,3,4,5,6,0]`;
|
||||
`enable_elevation=0` forces z=0 before position compensation and size spread;
|
||||
- `object_size_idx` 5-bit sizes, the `size_curve` piecewise mapping, and the size
|
||||
kernel; the kernel is generated at runtime from region geometry with
|
||||
equal-power grouped panning, the S448 weight and a power sum (20/20/8
|
||||
virtual points, 20 size nodes, 35/4 position nodes; boundary rows
|
||||
X(0), X(1), Y(0), Y(1), Z(1));
|
||||
- `snap` first-argmax speaker snapping, `divergence` left/right point-source
|
||||
combination, and element 5 high-precision position refinement;
|
||||
- `default_priority`/`priority_bits` choose which size objects keep their size
|
||||
when the size budget is limited;
|
||||
- `isf_idx` basis matrices (signed coefficients, no per-column normalization)
|
||||
with the per-basis compensation;
|
||||
- `b_bed_chan_distribute` label candidates, weight normalization, missing-channel
|
||||
fallback, and the direct/distribute compensation.
|
||||
|
||||
The ISF path orders the N ISF object audio essences into the column vector t
|
||||
according to the M.U.L.Z order of `isf_config`, selects the input configuration
|
||||
by `isf_idx` and the matrix M for the target output layout, and evaluates
|
||||
y = M·t: each ISF channel is distributed over the speakers by the corresponding
|
||||
matrix row, LFE bypasses the matrix, and each basis carries its own
|
||||
compensation. The matrix coefficients and layout descriptors are obtained from
|
||||
lookup tables stored in the compiled-in `src/oamd/isf_tables.h`; nothing reads a
|
||||
data file at run time.
|
||||
|
||||
Gain, size, priority, position, divergence, delta and refinement values are
|
||||
computed in code (`src/oamd/oamd_parser.*`) from formulas or fixed constants; the
|
||||
size kernel (`src/oamd/oamd_math.*`) is generated at runtime from region geometry
|
||||
with grouped equal-power panning, the S448 weight and a power sum. Bed labels,
|
||||
routing and reference positions are derived from the speaker layout tables
|
||||
(`src/speaker/speaker_layouts.h`) by
|
||||
`src/joc_bitstream/bed_layout_map.{h,cpp}`.
|
||||
ADM export and the binaural paths still use object positions only and do not apply
|
||||
these speaker target coefficients. The binaural path schedules positions from the
|
||||
complete `frame_state`: multiple position blocks and programmes with more than 16
|
||||
objects are scheduled block by block at their own time and ramp, and its position
|
||||
track list is not limited to 16; ADM export keeps the 15-track single-block view.
|
||||
+13
-241
@@ -147,15 +147,13 @@ $$
|
||||
|
||||
$$
|
||||
G_{\mathrm{clip}}=
|
||||
2^{x-4}\left(1+\frac{y}{32}\right).
|
||||
1+\frac{y}{32}2^{x-4}.
|
||||
$$
|
||||
|
||||
值域为 $[0.0625,15.75]$;$x=4$ 时化为 $1+y/32$。它并入分析端标量
|
||||
$S=\frac{1}{16}G_{\mathrm{clip}}$,乘在核心 bed 的每个采样上(QMF 分析之前);
|
||||
它并入分析端标量 $S=\frac{1}{16}G_{\mathrm{clip}}$,乘在核心 bed 的每个采样上(QMF 分析之前);
|
||||
对象 inverse QMF 之后的 $\times16$ 抵消 $1/16$,净效果是乘 $G_{\mathrm{clip}}$,因此钳位 $[-1,1]$
|
||||
处在增益**之后**。标量按帧保留两个槽,本帧前 256 个采样从上一帧值线性过渡到本帧值、之后为常数,
|
||||
复位后的第一帧直接取新值。LFE 走独立通路,但使用同一个完整 $G_{\mathrm{clip}}$ 的逐点斜坡:
|
||||
斜坡在进入 1217-sample 延迟之前乘到 LFE 采样上,见 §8。
|
||||
复位后的第一帧直接取新值。LFE 走旁通通路,不参与该标量。
|
||||
|
||||
## 3. 参数带展开与时间插值
|
||||
|
||||
@@ -190,12 +188,7 @@ $$
|
||||
|
||||
### 3.2 单数据点插值
|
||||
|
||||
令上一帧末值为 $P_{o,c,b}$,当前目标值为 $D_{o,c,p(b)}$。对象没有 previous 状态时
|
||||
(整条流第一帧或对象首次出现):单数据点情形 24 个时槽恒为 $D_{o,c,p(b)}$(硬切,
|
||||
不从零淡入);双数据点情形前 12 个时槽为 $D_0$,后 12 个时槽按 §3.3 的第二段公式
|
||||
从 $D_0$ 插值到 $D_1$(把该节的 $P$ 取为 $D_0$)。
|
||||
|
||||
有 previous 状态时,对时槽 $t=0\ldots23$:
|
||||
令上一帧末值为 $P_{o,c,b}$,当前目标值为 $D_{o,c,p(b)}$。对时槽 $t=0\ldots23$:
|
||||
|
||||
$$
|
||||
\alpha_t=\frac{t+1}{24},
|
||||
@@ -213,28 +206,11 @@ $$
|
||||
M_{o,c,b,23}=D_{o,c,p(b)}.
|
||||
$$
|
||||
|
||||
该值随后成为下一帧的 previous 状态。对象 present 由 1 变 0 后,从该帧起继续按全零矩阵发布,
|
||||
并把 previous 更新为零矩阵,直到它重新 present=1。
|
||||
该值随后成为下一帧的 previous 状态。
|
||||
|
||||
### 3.3 多数据点
|
||||
|
||||
`offset_ts` 给出各数据点开始生效的时隙序号,取原始 5 bit 值(合法范围 $0\ldots23$);
|
||||
同一对象的数据点必须严格递增。默认模式(`slope_idx=0`)下,两个数据点 $D_0,D_1$ 的插值为
|
||||
|
||||
$$
|
||||
M_{o,c,b,t}=
|
||||
\begin{cases}
|
||||
(1-\alpha_t)P_{o,c,b}+\alpha_tD_{0}, & t=0\ldots10,\ \alpha_t=\dfrac{t+1}{12},\\[1mm]
|
||||
D_{0}, & t=11,\\[1mm]
|
||||
(1-\beta_t)D_{0}+\beta_tD_{1}, & t=12\ldots23,\ \beta_t=\dfrac{t-11}{12}.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
没有 previous 状态时同样使用上面的公式,只是把 $P_{o,c,b}$ 取为 $D_0$。
|
||||
|
||||
阶跃模式(`slope_idx=1`)在指定时槽直接切换目标:$t<o_0$ 保持上一帧末值 $P_{o,c,b}$
|
||||
(没有 previous 时取 $D_0$),$o_0\le t<o_1$ 取 $D_0$,$t\ge o_1$ 取 $D_1$;
|
||||
单数据点时 $t<o_0$ 保持 $P$、$t\ge o_0$ 取 $D_0$。
|
||||
当一帧含两个数据点时,`offset_ts` 给出分段边界。每一段在上一目标和下一目标之间使用相同的线性关系;阶跃模式则在指定时槽直接切换目标。
|
||||
|
||||
## 4. 核心 PCM 的 analysis QMF
|
||||
|
||||
@@ -358,20 +334,15 @@ $$
|
||||
|
||||
## 8. LFE 路径
|
||||
|
||||
LFE 不经过对象矩阵或 inverse QMF。核心 LFE 先乘逐点增益斜坡 $G[n]$(与 §2.4 同一个完整
|
||||
$G_{\mathrm{clip}}$:$n<256$ 时 $G[n]=G_{\mathrm{old}}+n(G_{\mathrm{new}}-G_{\mathrm{old}})/256$,
|
||||
$n\ge256$ 时 $G[n]=G_{\mathrm{new}}$,首帧或复位后 $G_{\mathrm{old}}=G_{\mathrm{new}}$),
|
||||
再进入 1217-sample 延迟;读出后只做钳位,不再乘任何增益:
|
||||
LFE 不经过对象矩阵或 inverse QMF,而是使用 1217-sample 延迟。输入与输出端的比例因子抵消后:
|
||||
|
||||
$$
|
||||
y_{\mathrm{LFE}}[n]=
|
||||
\mathrm{clip}\left(
|
||||
x_{\mathrm{LFE,core}}[n-1217]\cdot G[n-1217],-1,1
|
||||
x_{\mathrm{LFE,core}}[n-1217],-1,1
|
||||
\right).
|
||||
$$
|
||||
|
||||
其中 $G[k]$ 取输入采样 $k$ 所在帧内的位置,即 $k\bmod1536$。
|
||||
|
||||
## 9. OAMD 坐标
|
||||
|
||||
横向和纵向网格使用 $N=62$,高度网格使用 $N=15$。量化函数为
|
||||
@@ -384,7 +355,7 @@ q_N(k)=
|
||||
\right).
|
||||
$$
|
||||
|
||||
归一化房间坐标为
|
||||
OAR 坐标为
|
||||
|
||||
$$
|
||||
u=\frac{q_1}{32768},
|
||||
@@ -479,9 +450,7 @@ v_{\mathrm{floor}}=
|
||||
\mathrm{clamp}(2v,0,1).
|
||||
$$
|
||||
|
||||
`warp_mode=0` 或其他布局使用 $v_{\mathrm{floor}}=v$。高度层不做该缩放。
|
||||
未携带 warp 元数据时采用 0;命令行不提供覆盖选项。warp 2/3 使用 §12.5 的中间布局与折叠矩阵。
|
||||
有 front/back balance 时,先求这里的分支坐标,再应用 §12.3 的 balance。
|
||||
其他布局使用 $v_{\mathrm{floor}}=v$。
|
||||
|
||||
### 10.3 高度层
|
||||
|
||||
@@ -502,9 +471,6 @@ $$
|
||||
|
||||
## 11. 布局位置补偿
|
||||
|
||||
本节是 trim 为 automatic 时的逐对象增益。disabled 取 1;explicit 改用 §12.2,
|
||||
不能把 automatic 增益与 explicit trim 再乘一遍。
|
||||
|
||||
令 $N_h$ 为相关高度扬声器数,$N_f$ 为相关附加水平扬声器数:
|
||||
|
||||
$$
|
||||
@@ -550,165 +516,7 @@ G_{\mathrm{pos}}
|
||||
\mathbf G_{\mathrm{point}}.
|
||||
$$
|
||||
|
||||
## 12. OAMD trim 与前后平衡
|
||||
|
||||
### 12.1 配置选择
|
||||
|
||||
OAMD trim element 把节目的校准数据分成九个配置,由目标布局的附加水平扬声器数
|
||||
$N_f$ 与高度扬声器数 $N_h$ 选定:
|
||||
|
||||
$$
|
||||
\kappa_f(N)=
|
||||
\begin{cases}
|
||||
0, & N=0,\\
|
||||
1, & N\in\{2,3\},\\
|
||||
2, & \text{其他},
|
||||
\end{cases}
|
||||
\qquad
|
||||
\kappa_h(N)=
|
||||
\begin{cases}
|
||||
0, & N=0,\\
|
||||
1, & N=2,\\
|
||||
2, & \text{其他}.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
$$
|
||||
c_{\mathrm{trim}}=\kappa_f(N_f)+3\,\kappa_h(N_h).
|
||||
$$
|
||||
|
||||
九个配置依次为“no / some / many 附加水平 × no / some / many 高度”。
|
||||
|
||||
trim 与前后平衡只在七种布局下施加:2.0、3.1、4.0、5.1、5.1.2、5.1.4、7.1。
|
||||
更宽的布局(7.1.2、7.1.4、9.1.4、9.1.6)取 $g_t=1$、$b=0$。
|
||||
|
||||
warp、trim 和 balance 均从 OAMD 元数据读取。各参数的取值与作用为:
|
||||
|
||||
| 参数 | 取值或单位 | 行为 |
|
||||
|---|---|---|
|
||||
| `warp_mode` | 0 / 1 / 2 / 3 | 直接渲染 / 地面 Y 缩放 / PLIIx 折叠 / Standard(Lo/Ro)折叠 |
|
||||
| `global_trim_mode` | 0 / 1 / 2 | automatic / disabled / 按目标布局配置选择;3 为保留值,明确报错 |
|
||||
| $T_s$、$T_h$ | dB | 显式 surround、height trim 参数,正值按 0 处理 |
|
||||
| $T_c$ | dB | 显式 centre trim 参数,保留符号 |
|
||||
| $b_{\mathrm{lis}}$、$b_{\mathrm{tb}}$ | $[-1,1]$,步长 $1/16$ | 听音平面、顶部的前后 balance |
|
||||
| 逐对象 trim bypass | 布尔值 | 关闭该对象的 balance 和 trim 增益,保留 warp |
|
||||
|
||||
全局模式为 2 时,所选配置的 default 或 disabled 标志分别采用 automatic 或 disabled;
|
||||
缺失配置采用 automatic,其他配置使用其显式参数。automatic 使用 §11 的位置补偿,
|
||||
disabled 使用单位增益,explicit 使用 §12.2,三种模式互斥。
|
||||
|
||||
初始没有 trim element 时采用 `warp_mode=0`、automatic trim。后续 payload 不含该
|
||||
element 时保持上一组控制;出现新 element 时,在对应元数据事件边界更新。命令行不提供
|
||||
warp 覆盖选项。
|
||||
|
||||
### 12.2 逐对象电平
|
||||
|
||||
centre、surround、height 是位置相关 trim 函数的参数,不是混音后各输出声道的固定增益。
|
||||
令 $y_t$ 为原始 Y 经 balance 后的坐标,$z=\max(w,0)$:
|
||||
|
||||
$$
|
||||
a_s=\mathrm{clamp}(2y_t,0,1)(1-z),\qquad a_h=z,
|
||||
$$
|
||||
|
||||
$$
|
||||
a_c=1-\mathrm{clamp}\bigl(10\max(|z|-0.05,\ y_t-0.05,\ |u-0.5|-0.05),0,1\bigr).
|
||||
$$
|
||||
|
||||
explicit 状态下,surround/height 参数上限为 0 dB,centre 保留有符号电平:
|
||||
|
||||
$$
|
||||
D=a_s\min(T_s,0)+a_h\min(T_h,0)+a_cT_c,\qquad g_t=10^{D/20}.
|
||||
$$
|
||||
|
||||
同一个 $g_t$ 乘该对象的整条声像增益向量,然后才累加不同对象。automatic 使用 §11;
|
||||
disabled 或逐对象 trim bypass 取 $g_t=1$。LFE 仍直接通过。
|
||||
增益求值、声像插值和对象累加均使用 float64。
|
||||
|
||||
### 12.3 前后平衡
|
||||
|
||||
每个配置还携带两个前后平衡量:$b_{\mathrm{lis}}$ 作用于听音平面,
|
||||
$b_{\mathrm{tb}}$ 作用于顶部。二者按 $1/16$ 网格量化,只在显式配置下非零。
|
||||
|
||||
记 $n=\mathrm{round}(16\,|b|)$、$s=n/18$,单层映射为
|
||||
|
||||
$$
|
||||
B_b(y)=
|
||||
\begin{cases}
|
||||
y, & y\le 0.05\ \text{或}\ y\ge 0.95,\\[2pt]
|
||||
(1+s)y-0.05s, & 0.05<y<0.5,\ b>0,\\[2pt]
|
||||
(1-s)(y-0.5)+0.5+0.45s, & 0.5\le y<0.95,\ b>0,\\[2pt]
|
||||
(1-s)y+0.05s, & 0.05<y<0.5,\ b<0,\\[2pt]
|
||||
(1+s)(y-0.5)+0.5-0.45s, & 0.5\le y<0.95,\ b<0.
|
||||
\end{cases}
|
||||
$$
|
||||
|
||||
它把 $[0.05,0.95]$ 单调映到自身:两端不动,$y=0.5$ 处两段取值相同,斜率分别为
|
||||
$1+s$ 与 $1-s$。首尾各 $5\%$ 的墙区以及 $b=0$ 的情形都原样通过,因此映射不会
|
||||
越界,也无需额外钳位。
|
||||
|
||||
高度 $w$ 把两层混合起来:
|
||||
|
||||
$$
|
||||
y'=(1-w)\,B_{b_{\mathrm{lis}}}(y)+w\,B_{b_{\mathrm{tb}}}(y).
|
||||
$$
|
||||
|
||||
前后轴 $y$ 的取向见 §9,$0$ 为前、$1$ 为后;$x$ 与 $z$ 不变。
|
||||
|
||||
### 12.4 施加位置
|
||||
|
||||
记 §12.3 的高度混合 balance 映射为 $B_z(y)$,§10.2 的 warp 映射为 $W(y)$。
|
||||
warp 0/1 直接渲染点对象时,分支顺序为
|
||||
|
||||
$$
|
||||
y_f=B_z(W(y)),\qquad y_h=B_z(y),\qquad y_t=B_z(y).
|
||||
$$
|
||||
|
||||
地面和高度声像分别使用 $y_f,y_h$,trim 函数使用 $y_t$。最后组合
|
||||
|
||||
$$
|
||||
g_{o,c}=G_{\mathrm{object}}\,g_t(u,y_t,z)\,P_c(u,y_f,y_h,z).
|
||||
$$
|
||||
|
||||
因此可概括为 warp → balance → trim 与声像增益合成,但不能让所有阶段共用一个
|
||||
已经 warp 的 Y。balance 仅在 explicit 且未逐对象 bypass 时启用;warp 不受 trim
|
||||
状态或 trim bypass 控制。源坐标保持原值,ADM 与 binaural 不预处理此变换。
|
||||
|
||||
地面坐标依次经过 warp、balance,再用于地面声像;顶部坐标和 trim 求值坐标只经过
|
||||
balance。trim 标量乘到该对象的整条声像增益向量后,再累加到输出声道。LFE 直接通过。
|
||||
|
||||
### 12.5 warp 2/3 的环绕折叠
|
||||
|
||||
目标为 5.1、5.1.2 或 5.1.4 时,warp 2/3 先把对象渲染到对应的 7.1、7.1.2 或
|
||||
7.1.4 中间布局,再将后环绕折叠到目标的环绕对。4.0 同样先增加一对后环绕,
|
||||
中间布局不增加 centre 或 LFE。其他现有布局直接渲染。
|
||||
|
||||
中间布局只改变扬声器几何与声像分配。Trim 配置、两层 Balance 和 Auto Trim 的
|
||||
最大位置补偿仍按最终目标布局选择。例如,5.1.2 使用配置 4,最大位置补偿为
|
||||
$-2.25$ dB,即使其中间布局是 7.1.2,也继续使用这些参数。中间布局已有独立
|
||||
side/rear 两对,地面 Y 不再乘 2;Balance 和逐对象 Trim 在中间渲染时求值。
|
||||
|
||||
设中间布局输出为 $L_s,R_s,L_{rs},R_{rs}$,最终目标的环绕输出为 $L'_s,R'_s$:
|
||||
|
||||
$$
|
||||
L'_s=L_s+aL_{rs}+bR_{rs},\qquad
|
||||
R'_s=R_s+bL_{rs}+aR_{rs}.
|
||||
$$
|
||||
|
||||
| 模式 | $a$ | $b$ |
|
||||
|---|---|---|
|
||||
| 2:PLIIx | $\sqrt{3}/2$ | $1/2$ |
|
||||
| 3:Standard(Lo/Ro) | $1$ | $0$ |
|
||||
|
||||
两种矩阵都按线性幅度相加,不做折叠后的能量归一化。共同的前方、高度和 LFE
|
||||
声道按对应位置直接通过。折叠后的对象增益向量进入 §13 的目标增益更新与斜坡,
|
||||
然后累加对象音频。
|
||||
|
||||
完整顺序为:按目标布局选择校准参数 → 在中间布局应用 Balance、Trim 并计算声像
|
||||
增益 → 折叠水平环绕 → 更新增益斜坡与混音。逐对象 bypass 关闭 Balance 和 Trim,
|
||||
但仍保留 warp 2/3 的布局选择与折叠。上述模式属于通用扬声器渲染控制;E-AC-3 JOC
|
||||
公开定义的 warp 值仍为 0/1,源对象轨迹、ADM 和 binaural 不预处理折叠结果。
|
||||
|
||||
## 13. OAMD 时间对齐与增益斜坡
|
||||
## 12. OAMD 时间对齐与增益斜坡
|
||||
|
||||
OAMD 更新的编码位置为
|
||||
|
||||
@@ -764,7 +572,7 @@ $$
|
||||
|
||||
如果中途没有新的 metadata 更新,该过程等价于总长度 $KB$ 的逐样本线性斜坡。
|
||||
|
||||
## 14. 最终扬声器混音
|
||||
## 13. 最终扬声器混音
|
||||
|
||||
对目标输出声道 $c$:
|
||||
|
||||
@@ -796,45 +604,9 @@ y_{24}[n]=
|
||||
\right).
|
||||
$$
|
||||
|
||||
## 15. 公式适用范围
|
||||
## 14. 公式适用范围
|
||||
|
||||
- JOC 矩阵部分同时描述 dense MTX 与 sparse IDX/VEC 两条差分语法。
|
||||
- 扬声器声像部分描述普通点对象;extent、spread、divergence 等模式需要额外模型。
|
||||
- 多个 OAMD position block 必须按其时间顺序调度。
|
||||
- limiter 属于独立后处理,不包含在上述混音公式中。
|
||||
- trim 与前后平衡只作用于扬声器渲染;双耳渲染不读取它们。
|
||||
|
||||
## 16. OAMD 元数据应用(扬声器路径)
|
||||
|
||||
扬声器渲染把对象 PCM 映射到目标布局时,除几何声像外还应用 OAMD 的对象属性:
|
||||
|
||||
- 每块整数增益 `Qg`(单位增益 4096)与 `inactive`;增益在生成目标系数时乘一次,
|
||||
LFE 使用所属元数据对象的增益/活动状态;
|
||||
- basic/render 状态 0/1/2/3 的完整块展开、同对象前一块继承与局部 mask 更新;
|
||||
- `zone_constraints_idx` 选择区域,映射为 `[0,1,2,3,4,5,6,0]`;`enable_elevation=0`
|
||||
在位置补偿和尺寸扩散前令 z=0;
|
||||
- `object_size_idx` 的 5 bit 尺寸、`size_curve` 分段线性映射与尺寸扩散核;
|
||||
尺寸核按分组等功率声像、S448 权重和幂和,在运行时用区域几何生成:
|
||||
20/20/8 个虚拟点、20 个尺寸节点、35/4 个位置节点,边界表为
|
||||
X(0)、X(1)、Y(0)、Y(1)、Z(1);
|
||||
- `snap` 的首个最大系数扬声器吸附、`divergence` 的左右两点合成、
|
||||
element 5 的高精度位置补充;
|
||||
- `default_priority`/`priority_bits` 在 size 数量预算不足时决定保留尺寸的对象;
|
||||
- `isf_idx` 的 ISF 基矩阵(可含负系数,不做逐列归一化)与逐 basis 补偿;
|
||||
- `b_bed_chan_distribute` 的 bed 标签候选筛选、权重归一化、缺失声道 fallback 和
|
||||
直达/distribute 的补偿。
|
||||
|
||||
ISF 路径把 N 路 ISF object audio essences 按 `isf_config` 的 M.U.L.Z 顺序组成
|
||||
列向量 t,按 `isf_idx` 选输入配置、按目标输出布局选矩阵 M,再计算 y = M·t:
|
||||
每个 ISF 声道按矩阵对应的一行分配到目标扬声器,LFE 不参与矩阵、直接旁路,
|
||||
每个 basis 另有补偿。矩阵系数与布局描述通过查表获得,表数据写在编译进源码的
|
||||
`src/oamd/isf_tables.h` 中,运行时不读取数据文件。
|
||||
|
||||
gain、size、priority、位置、divergence 与差分/细化值由 `src/oamd/oamd_parser.*`
|
||||
按公式或固定常量计算,size 扩散核由 `src/oamd/oamd_math.*` 从区域几何生成
|
||||
(分组等功率声像 + S448 权重 + 幂和)。bed 标签、路由和参考位置由
|
||||
`src/joc_bitstream/bed_layout_map.*` 从 `src/speaker/speaker_layouts.h` 的布局表
|
||||
推导。
|
||||
ADM 导出与双耳路径仍只使用对象位置,不应用这些扬声器目标系数。双耳路径按完整
|
||||
`frame_state` 调度位置:多 position block 与超过 16 个对象的节目都按各块的时间与 ramp
|
||||
逐个调度,双耳位置轨道数不限于 16;ADM 导出保持 15 轨单 block 视图。
|
||||
@@ -1,100 +1,89 @@
|
||||
# Rosella filterbank
|
||||
|
||||
The Rosella renderer runs one of two 64-band QMF / 77-band hybrid filterbanks.
|
||||
The shared public bank is the default; the captured Rosella bank reproduces the
|
||||
reference headphone renderer. The SOFA compiler and renderer always use
|
||||
`PublicFilterbank`.
|
||||
[中文](rosella_filterbank.md) · [Back to README](../README.en.md)
|
||||
|
||||
The Rosella `.personalized_headphone` path can run either of two 64-QMF /
|
||||
77-hybrid filterbanks: the shared public bank (the default) or the Rosella bank
|
||||
captured from the reference renderer. The SOFA and `.jochrtf` paths always use
|
||||
the public bank and never consult the selector.
|
||||
|
||||
## Which bank runs
|
||||
|
||||
`--rosella-filterbank 1|2` selects the bank for the `.personalized_headphone`
|
||||
path; `joc_task_config::rosella_filterbank` and
|
||||
`joc_stream_config::rosella_filterbank` carry the same value, with `0` meaning
|
||||
the default.
|
||||
`--rosella-filterbank 1|2` selects the bank of the Rosella path; the library
|
||||
form is `RosellaBinauralRenderer(..., filterbank=...)`, where omitting the
|
||||
argument means `1`.
|
||||
|
||||
| Value | Bank | Low-band hybrid | High-band delay | Latency |
|
||||
| Value | Implementation | Low-band hybrid | High-band delay | Latency |
|
||||
| --- | --- | --- | --- | ---: |
|
||||
| `1` (default) | `PublicFilterbank` | 13 taps | 6 slots | 961 samples |
|
||||
| `2` | `RosellaFilterbank` | 11 taps | 3 slots | 448 samples |
|
||||
| `1` (default) | `rosella_filterbank.py` | 13 taps | 6 slots | 961 samples |
|
||||
| `2` | `rosella_captured_filterbank.py` | 11 taps | 3 slots | 448 samples |
|
||||
|
||||
Both banks are driven through the same four stateful half-steps, so the choice
|
||||
switches the coefficients, the high-band delay, the history layout, the startup
|
||||
skip and the flush capacity together; the analysis and synthesis banks never
|
||||
share state. Any other value is rejected, and the option is an error when the
|
||||
binaural input is not a Rosella model: the SOFA and `.jochrtf` paths do not use
|
||||
it and must not silently ignore it.
|
||||
Both banks expose the same four stateful half-steps (QMF analysis, hybrid
|
||||
analysis, hybrid synthesis, QMF synthesis), so the choice switches the
|
||||
coefficients, the high-band delay, the history layout, the startup skip and the
|
||||
flush capacity together. The analysis and synthesis stages keep their own state
|
||||
and never share it. Any other value is rejected, and the option is an error when
|
||||
the binaural input is not a Rosella model: a SOFA or `.jochrtf` render must not
|
||||
silently ignore it.
|
||||
|
||||
## Why the banks differ
|
||||
|
||||
The reference headphone renderer selects its analysis and synthesis callbacks
|
||||
during preparation, after initialization. Capturing only the initial callbacks
|
||||
identified the wrong filterbank variant.
|
||||
identified the wrong filterbank variant. Runtime buffer captures gave these
|
||||
parameters:
|
||||
|
||||
Runtime buffer captures identified these parameters:
|
||||
|
||||
| Parameter | Rosella bank | Public bank |
|
||||
| Parameter | Captured Rosella bank | Public bank |
|
||||
| --- | --- | --- |
|
||||
| QMF prototype | 640 asymmetric coefficients | Separate prototype |
|
||||
| QMF prototype | 640 asymmetric coefficients | polyphase reorder of the public 640-tap prototype |
|
||||
| Low-band hybrid filter | 11 taps | 13 taps |
|
||||
| High-band hybrid delay | 3 slots | 6 slots |
|
||||
| Analysis/synthesis latency | 448 samples | 961 samples |
|
||||
|
||||
The wrong variant produced a frequency-dependent room-tail error. With the
|
||||
captured room send as input, the existing room recursion already matched the
|
||||
reference. The fix changes the filterbank, not the room coefficients, direct
|
||||
HRTF calculation, phase mapping, or output EQ.
|
||||
reference, so the fix changes only the filterbank: not the room coefficients,
|
||||
the direct HRTF calculation, the phase mapping, or the output EQ. The public
|
||||
bank stayed as bank `1` so historical renders remain reproducible; the captured
|
||||
bank is bank `2`.
|
||||
|
||||
## Implementation
|
||||
|
||||
`RosellaFilterbank` keeps separate analysis, hybrid, and synthesis histories for
|
||||
each channel. QMF data uses `[slot, channel, 64]` order. Hybrid data uses
|
||||
`[slot, channel, 77]` order. The half-step methods preserve state between calls.
|
||||
`rosella_captured_filterbank.py` reproduces the four half-steps of the captured
|
||||
bank in float64/complex128:
|
||||
|
||||
`rosella_filterbank_tables.h` stores the captured float32 prototype, rotation,
|
||||
and hybrid coefficients as hexadecimal literals. Processing uses double
|
||||
precision. The tables do not contain the personalized HRTF or room tuning.
|
||||
- QMF analysis keeps a 9-slot ring history, reads `(position + tap + 1) mod 10`,
|
||||
and writes `spectrum[2b] = F[b]/64`, `spectrum[2b+1] = conj(F[63-b])/64`;
|
||||
- hybrid analysis keeps a 10-slot history, transforms the three low bands with
|
||||
8/4/4-point transforms, reorders them with a fixed map, conjugates bands 1 and
|
||||
3, and passes the high bands through delayed by 3 slots;
|
||||
- QMF synthesis keeps a 9-slot history and accumulates 10 prototype-matched
|
||||
lags, taking the imaginary part on odd lags.
|
||||
|
||||
The runtime removes the selected bank's startup samples and uses the same
|
||||
latency when it calculates flush capacity. Its rendered buffer is resized to the
|
||||
current block length, including a short final block.
|
||||
`data/rosella_captured_kernels.npz` stores the captured prototype
|
||||
(`float32[64,10]`), rotation (`complex64[64]`) and hybrid coefficients
|
||||
(`complex64[22]`); they are promoted to float64/complex128 on load. The values
|
||||
are the ones the C++ core compiles in as `src/hrtf/rosella_filterbank_tables.h`
|
||||
and contain no personalized HRTF or room tuning.
|
||||
|
||||
The startup skip and the flush capacity both come from the selected bank (961 or
|
||||
448 samples). `.report.json` records the actual choice in
|
||||
`binaural_renderer_backend`, as `filterbank`, `filterbank_name` and
|
||||
`latency_compensated_samples`.
|
||||
|
||||
## Validation
|
||||
|
||||
Run the self-contained tests:
|
||||
The local regression script lives in `tests/`, which this repository does not
|
||||
track (like the rest of its local checks). It covers:
|
||||
|
||||
```sh
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build --parallel
|
||||
ctest --test-dir build --output-on-failure
|
||||
```
|
||||
- reference vectors for the four captured half-steps, the same samples the C++
|
||||
core checks, generated by an independent NumPy implementation, to within
|
||||
`1e-10` per sample;
|
||||
- an impulse roundtrip peak at 961 and 448 samples for the two banks;
|
||||
- chunk invariance (including zero-length and unequal chunks) and `reset()`;
|
||||
- independence: processing one bank never changes the other bank's state;
|
||||
- rejection of out-of-range values.
|
||||
|
||||
`rosella_filterbank` checks deterministic analysis, hybrid, folding, and synthesis
|
||||
vectors from an independent NumPy implementation. It also checks empty and
|
||||
unequal chunks, reset, channel isolation, and impulse latency. A second group
|
||||
checks the bank selector: each kind reproduces exactly what its own class
|
||||
computes, the two produce different hybrid data, chunking and reset are
|
||||
invariant for both, and each reports its own latency (961 or 448 samples). The
|
||||
tests need no reference binary, audio fixture, or personalized headphone file.
|
||||
|
||||
Before integration with the upstream bed-layout changes, four captured-input
|
||||
tests gave the following room PCM residuals. No gain fitting was used for this
|
||||
table. Residual is `10 * log10(sum(abs(reference - candidate)^2) /
|
||||
sum(abs(reference)^2))`.
|
||||
|
||||
| Input | Room PCM residual |
|
||||
| --- | ---: |
|
||||
| Left-front click | -133.94 dB |
|
||||
| Left-front log sweep | -135.40 dB |
|
||||
| Left-front noise | -133.50 dB |
|
||||
| Right-front signal | -133.34 dB |
|
||||
|
||||
The old bank produced room residuals near -10 to -11 dB on these captures.
|
||||
A 30-second music replay using the reference decoder's object PCM reached
|
||||
correlation 0.9999999503 and a -70.02 dB residual after delay alignment and one
|
||||
global gain fit. A full-file render through this project's decoder reached
|
||||
correlation 0.99988816 after alignment and gain fitting.
|
||||
|
||||
These are separate experiments, not bit-exactness claims. A fixed gain
|
||||
difference of approximately 1.0004885 remains in the captured chain. The patch
|
||||
does not compensate for that difference. Decoder differences and the remaining
|
||||
music residual are outside this fix.
|
||||
The public filterbank formulas and `.jochrtf` direction field described in
|
||||
`docs/binaural.md` are unchanged; the captured bank only affects the Rosella
|
||||
path.
|
||||
@@ -0,0 +1,71 @@
|
||||
# Rosella 滤波器组
|
||||
|
||||
[English](rosella_filterbank.en.md) · [返回 README](../README.md)
|
||||
|
||||
Rosella `.personalized_headphone` 路径可以在两套 64-QMF / 77-hybrid 滤波器组之间选择:
|
||||
公开滤波器组(默认)与从参考渲染器捕获的 Rosella 滤波器组。SOFA 与 `.jochrtf` 路径
|
||||
始终使用公开滤波器组,不经过该选择。
|
||||
|
||||
## 选择哪一套
|
||||
|
||||
`--rosella-filterbank 1|2` 选择 Rosella 路径的滤波器组;库接口是
|
||||
`RosellaBinauralRenderer(..., filterbank=...)`,省略参数等于 `1`。
|
||||
|
||||
| 取值 | 实现 | 低频 hybrid | 高频 slot delay | 延迟 |
|
||||
| --- | --- | --- | --- | ---: |
|
||||
| `1`(默认) | `rosella_filterbank.py` | 13 taps | 6 slots | 961 samples |
|
||||
| `2` | `rosella_captured_filterbank.py` | 11 taps | 3 slots | 448 samples |
|
||||
|
||||
两套实现暴露同一组四个有状态的 half-step(QMF 分析、hybrid 分析、hybrid 合成、
|
||||
QMF 合成),因此切换会同时改变系数、高频延迟、历史缓冲布局、起始延迟跳过量和
|
||||
flush 容量;分析与合成两级各自持有状态,互不共享。其它取值直接报错;非 Rosella
|
||||
输入(SOFA、`.jochrtf`)给出该选项也报错,不会静默忽略。
|
||||
|
||||
## 为什么两套不同
|
||||
|
||||
参考头戴渲染器在初始化之后的准备阶段才选定 analysis/synthesis 回调,只捕获初始化
|
||||
时的回调会拿到错误的滤波器组变体。运行时缓冲捕获得到的参数是:
|
||||
|
||||
| 参数 | Rosella 捕获版 | 公开版 |
|
||||
| --- | --- | --- |
|
||||
| QMF prototype | 640 个非对称系数 | 公开 640-tap prototype 的多相重排 |
|
||||
| 低频 hybrid 滤波 | 11 taps | 13 taps |
|
||||
| 高频 hybrid delay | 3 slots | 6 slots |
|
||||
| analysis/synthesis 延迟 | 448 samples | 961 samples |
|
||||
|
||||
用错变体会在 room 尾音上引入随频率变化的误差;把捕获到的 room send 作为输入时,
|
||||
现有 room 递推已经能对上参考,因此修复只更换滤波器组,不改 room 系数、direct HRTF、
|
||||
相位映射或输出 EQ。公开版是 `1`,捕获版是 `2`;默认保留公开版,便于与历史输出对齐。
|
||||
|
||||
## 实现
|
||||
|
||||
`rosella_captured_filterbank.py` 用 float64/complex128 复现捕获版的四个 half-step:
|
||||
|
||||
- QMF 分析保留 9 slot 环形历史,按 `(position + tap + 1) mod 10` 读取,输出
|
||||
`spectrum[2b] = F[b]/64`、`spectrum[2b+1] = conj(F[63-b])/64`;
|
||||
- hybrid 分析保留 10 slot 历史,低频三个 band 分别做 8/4/4 点变换后按固定顺序重排,
|
||||
band 1 与 band 3 取共轭;高频带原样延迟 3 slot;
|
||||
- QMF 合成保留 9 slot 历史,用 10 个 lag 的 prototype 匹配累加,奇数 lag 取虚部。
|
||||
|
||||
`data/rosella_captured_kernels.npz` 保存捕获版的 prototype(`float32[64,10]`)、
|
||||
rotation(`complex64[64]`)与 hybrid 系数(`complex64[22]`);载入后提升为
|
||||
float64/complex128。这些数值与 C++ 内核编译进去的
|
||||
`src/hrtf/rosella_filterbank_tables.h` 相同,不包含个性化 HRTF 或 room 调参。
|
||||
|
||||
运行时的起始延迟跳过与 flush 容量都取自所选滤波器组:`961` 或 `448` samples,
|
||||
`.report.json` 的 `binaural_renderer_backend` 用 `filterbank`、`filterbank_name` 与
|
||||
`latency_compensated_samples` 记录实际选择。
|
||||
|
||||
## 验证
|
||||
|
||||
本地回归脚本放在 `tests/`(该目录不随仓库跟踪,与其余本地验证脚本一致),覆盖:
|
||||
|
||||
- 捕获版四个 half-step 的参考向量(与 C++ 内核 `tests/rosella_filterbank_vectors.h`
|
||||
同一组、由独立 NumPy 实现生成),逐样本误差小于 `1e-10`;
|
||||
- 两套滤波器组的脉冲往返延迟分别落在 `961` 与 `448` samples;
|
||||
- 分块处理(含 0 长度与不等长切分)与 `reset()` 不变性;
|
||||
- 两套滤波器组互不干扰:交叉处理一个 bank 不改变另一个 bank 的状态;
|
||||
- 越界取值被拒绝。
|
||||
|
||||
`docs/binaural.md` 描述的公开滤波器组公式与 `.jochrtf` 方向和公开版一致;捕获版只
|
||||
作用于 Rosella 路径。
|
||||
-128
@@ -1,128 +0,0 @@
|
||||
# SIMD and runtime dispatch
|
||||
|
||||
[中文](simd.md) · [Back to README](../README.en.md)
|
||||
|
||||
The heaviest loops in the binaural path (QMF analysis and synthesis, the hybrid
|
||||
analysis low join, hybrid-domain path rendering, the ROOM FFT, the spherical
|
||||
harmonic alignment) each have a runtime-dispatched vector implementation: one
|
||||
binary carries several instruction-set variants, asks the CPU once at startup and
|
||||
runs the widest one. **The output is byte-identical either way** — that is a hard
|
||||
constraint, not a goal.
|
||||
|
||||
```text
|
||||
JOC_SIMD=auto|scalar|sse2|avx2|avx512|neon pin one tier (used for verification)
|
||||
JOC_SIMD_LOG=1 report the ISA each kernel actually got
|
||||
```
|
||||
|
||||
## Why the split has to happen per translation unit
|
||||
|
||||
MSVC has no function-level attribute like `__attribute__((target("avx2")))`: one
|
||||
`.cpp` file gets one `/arch`. So every ISA is its own translation unit with its own
|
||||
`/arch:AVX2` or `/arch:AVX512` (GCC/Clang: `-mavx2` / `-mavx512f`), and
|
||||
`dispatch.cpp` fills the function table at run time. The baseline units — the
|
||||
dispatcher itself, the CPU probe and the scalar reference — carry **no** `/arch` at
|
||||
all and stay on the SSE2 that x86-64 guarantees.
|
||||
|
||||
A trap from the history of this tree: `JOC_ENABLE_AVX2` used to be global, so
|
||||
turning it on put AVX2 instructions into the very code paths that exist for older
|
||||
CPUs. It now only selects whether the AVX2 unit is compiled in.
|
||||
|
||||
## Directory layout
|
||||
|
||||
One flat directory, **the instruction set in the file name and never in a
|
||||
subdirectory** — that is how FLAC does it (`lpc.c` sits next to
|
||||
`lpc_intrin_sse2.c`, `lpc_intrin_avx2.c` and `lpc_intrin_neon.c`, with the CPU
|
||||
probe in its own `cpu.c`).
|
||||
|
||||
```text
|
||||
src/simd/
|
||||
simd.h the contract: Isa / Kernel / Kernels / dimensions
|
||||
cpu_probe.{h,cpp} "can this machine run ISA X": CPUID+XGETBV / __builtin_cpu_supports / getauxval
|
||||
dispatch.cpp policy: JOC_SIMD, the fallback ladder, the table, the log
|
||||
kernels_scalar.cpp the reference (Isa::scalar; always built, always selectable)
|
||||
kernels_intrin_avx2.cpp /arch:AVX2 -mavx2
|
||||
kernels_intrin_avx512.cpp /arch:AVX512 -mavx512f
|
||||
kernels_intrin_neon.cpp AArch64 default
|
||||
```
|
||||
|
||||
The module lives at `src/simd/`, not `src/dsp/simd/`: `foundation/`, `hrtf/` and
|
||||
`binaural/` all call into these kernels, so it is a cross-cutting layer rather than
|
||||
a submodule of the DSP code (and `src/dsp/` held nothing else).
|
||||
|
||||
The header comment of `simd.h` carries the same module map; keep the two in sync
|
||||
when the layout changes.
|
||||
|
||||
## Three rules
|
||||
|
||||
1. **Only `kernels_intrin_*.cpp` gets a wider flag.** `CMakeLists.txt` names those
|
||||
files explicitly with `set_source_files_properties`; every other target stays on
|
||||
the architecture's guaranteed ISA. A unit that goes wide without matching that
|
||||
name fails `devtools/vec/isa_audit.ps1`.
|
||||
2. **No dynamic initialisation inside an ISA unit.** Those objects are linked into
|
||||
the same image as the baseline, so a global constructor would execute a wide
|
||||
instruction before the dispatcher has looked at the CPU. Constant tables are
|
||||
fine — they land in `.rdata`.
|
||||
3. **Bit-exactness comes from the lane assignment, not from the ISA.** A lane may
|
||||
only carry mutually independent outputs; the rounding sequence of a single
|
||||
output, the separation of multiply and add (never an FMA) and the summation
|
||||
order all stay exactly as `kernels_scalar.cpp` wrote them. Layout changes that
|
||||
only reorder stored doubles (rank-minor basis tables, term-ordered tap tables,
|
||||
stage-contiguous twiddle tables, band-major ROOM planes) are allowed.
|
||||
|
||||
## How the choice is made
|
||||
|
||||
`dispatch.cpp` parses `JOC_SIMD` first (forcing a tier this build or this machine
|
||||
does not have prints a diagnostic and falls back, rather than pretending and
|
||||
crashing), then walks `avx512 → avx2 → sse2 → neon` and picks, per kernel, the
|
||||
widest implementation that is both compiled into this binary **and** runnable
|
||||
here, falling back to the scalar reference. An AVX-512 unit therefore costs
|
||||
nothing on a CPU without AVX-512; it is simply never selected.
|
||||
|
||||
On x86 the probe requires CPUID *and* XGETBV to agree: CPUID says the silicon can
|
||||
do it, XCR0 says the OS saves the registers it needs. Either one alone is not
|
||||
enough — using AVX without OS state support corrupts other threads across a
|
||||
context switch. AArch64 needs no probe; ASIMD is the architectural baseline.
|
||||
|
||||
`sse2` is a selectable tier with **no unit of its own**, on purpose: a 128-bit SSE2
|
||||
register is the register a scalar double already occupies, SSE2 cannot widen
|
||||
double-precision arithmetic, and hand-written SSE2 would only add moves. The tier
|
||||
resolves to the baseline unit.
|
||||
|
||||
## Effect
|
||||
|
||||
30-second reference cases, one binary with only `JOC_SIMD` switched (DSP stage,
|
||||
`t_render_dsp`):
|
||||
|
||||
| Case | `scalar` | `auto` | DSP speed-up | End-to-end wall clock |
|
||||
|---|---|---|---|---|
|
||||
| Binaural Rosella | 1.256 s | **0.640 s** | **1.96×** | 1.690 → **0.941 s** |
|
||||
| Binaural SOFA | 1.560 s | **0.654 s** | **2.39×** | 1.859 → **0.849 s** |
|
||||
| Speaker 5.1 / 9.1.6 / ADM | — | — | 1.00× | no regression (these kernels are not on those paths) |
|
||||
|
||||
The vectorised loops themselves gain more: synthesis basis 5.89×, 13-tap low join
|
||||
4.50×, SOFA QMF synthesis 4.27×, 128-point FFT 2.24×. The whole pipeline stops
|
||||
short of 8× because a good part of the time goes to parameter setup, straight
|
||||
copies and file writing — none of which has independent work items — and because
|
||||
SOFA's 33 M sin/cos calls per sample cannot be vectorised under a byte-exactness
|
||||
contract.
|
||||
|
||||
## Verifying a change
|
||||
|
||||
```powershell
|
||||
$env:JOC_SIMD_LOG='1' # per-kernel ISA on this machine
|
||||
$env:JOC_SIMD='scalar' # force the reference: hashes must not move
|
||||
pwsh -NoProfile -File devtools\vec\isa_audit.ps1 # disassemble every .obj: 0 unguarded wide instructions
|
||||
pwsh -NoProfile -File devtools\vec\sha_matrix.ps1 # 5 tiers x 2 renders against the reference digests
|
||||
cmd /c devtools\vec\build_kernel_probe.bat # per-kernel byte digests (8 kernels)
|
||||
```
|
||||
|
||||
## Adding an ISA
|
||||
|
||||
1. Write `kernels_intrin_<isa>.cpp`, implementing the slots you have and leaving the
|
||||
rest `nullptr` — the dispatcher falls back per kernel (that is how
|
||||
`qmf_synthesis_basis` is handled in the AVX-512 unit).
|
||||
2. Add it to `JOC_SIMD_SOURCES` in `CMakeLists.txt` with `JOC_SIMD_HAVE_<ISA>=1` and
|
||||
its flag, and extend `Isa`, `isa_rank`, `isa_compiled`, `isa_supported` and the
|
||||
`JOC_SIMD` name table in `simd.h` / `dispatch.cpp`.
|
||||
3. Verify: the kernel digests must match the scalar unit byte for byte, and the
|
||||
reference renders must keep their SHA-256 on every tier.
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
# SIMD 与运行时派发
|
||||
|
||||
[English](simd.en.md) · [返回 README](../README.md)
|
||||
|
||||
双耳通路里最重的那几段循环(QMF 分析/合成、混合分析的低频拼接、混合域路径渲染、
|
||||
ROOM 的 FFT、球谐对齐)都有一份运行时分派的向量实现:同一份二进制里装多套 ISA 代码,
|
||||
启动时问一次 CPU,然后选最宽的那套跑。**输出逐字节不变**——这是硬约束,不是目标。
|
||||
|
||||
```text
|
||||
JOC_SIMD=auto|scalar|sse2|avx2|avx512|neon 强制某一层(验收用)
|
||||
JOC_SIMD_LOG=1 打印每个 kernel 实际生效的 ISA
|
||||
```
|
||||
|
||||
## 为什么必须"按编译单元分 ISA"
|
||||
|
||||
MSVC 没有 `__attribute__((target("avx2")))` 这类函数级多版本能力,一个 .cpp 只能有
|
||||
一个 `/arch`。所以每个 ISA 一个编译单元,各自带自己的 `/arch:AVX2` / `/arch:AVX512`
|
||||
(GCC/Clang 是 `-mavx2` / `-mavx512f`),由 `dispatch.cpp` 在运行时填函数表。
|
||||
基线单元(含派发器本身、CPU 探测、标量参考实现)**不带任何 `/arch`**,它们只使用
|
||||
x86-64 架构保证的 SSE2。
|
||||
|
||||
历史坑:早先的 `JOC_ENABLE_AVX2` 是**全局**的,一旦打开,连"给老 CPU 用"的基线路径
|
||||
都带 AVX2 指令。现在这个选项只决定是否把 AVX2 单元编进二进制。
|
||||
|
||||
## 目录布局
|
||||
|
||||
一个扁平目录,**ISA 写在文件名里,不写进子目录**——这是 FLAC 的做法
|
||||
(`src/libFLAC/lpc.c` 旁边就是 `lpc_intrin_sse2.c` / `lpc_intrin_avx2.c` /
|
||||
`lpc_intrin_neon.c`,CPU 探测单独放在 `cpu.c`)。
|
||||
|
||||
```text
|
||||
src/simd/
|
||||
simd.h 唯一契约头:Isa / Kernel / Kernels / 维度常量
|
||||
cpu_probe.{h,cpp} "这台机器能不能跑 ISA X":CPUID+XGETBV / __builtin_cpu_supports / getauxval
|
||||
dispatch.cpp 策略:JOC_SIMD 解析、回退阶梯、填函数表、日志
|
||||
kernels_scalar.cpp 参考实现(Isa::scalar,永远编译、永远可选中)
|
||||
kernels_intrin_avx2.cpp /arch:AVX2 -mavx2
|
||||
kernels_intrin_avx512.cpp /arch:AVX512 -mavx512f
|
||||
kernels_intrin_neon.cpp AArch64 默认 -march=armv8-a+simd
|
||||
```
|
||||
|
||||
`simd.h` 的头注释里有一份同样的模块地图,改布局时两处一起改。
|
||||
|
||||
模块放在 `src/simd/` 而不是 `src/dsp/simd/`:这些 kernel 被 `foundation/`、`hrtf/`、
|
||||
`binaural/` 三个模块共用,是横切的一层,不是 DSP 的子模块(更何况 `src/dsp/` 里除了
|
||||
`simd/` 空无一物)。
|
||||
|
||||
## 三条规则
|
||||
|
||||
1. **只有 `kernels_intrin_*.cpp` 拿更宽的编译开关。** `CMakeLists.txt` 用
|
||||
`set_source_files_properties` 逐个点名,其余目标一律留在架构保证的 ISA 上。
|
||||
任何不属于这个命名却带了宽指令的单元都会被 `devtools/vec/isa_audit.ps1` 判失败。
|
||||
2. **ISA 单元里不许有动态初始化。** 它们和基线代码链进同一个镜像,全局构造函数会在
|
||||
派发器看 CPU 之前就跑宽指令。常量表没问题(落在 `.rdata`)。
|
||||
3. **逐位一致靠的是 lane 的划分,不是 ISA。** lane 里只能放**互相独立**的输出;单个输出
|
||||
的舍入序列、乘加分离(绝不用 FMA)、求和顺序都保持 `kernels_scalar.cpp` 原样。
|
||||
只改变 double **存放顺序**的布局改造(基函数表转秩小序、抽头表按项序、蝶形因子表
|
||||
按级连续化、ROOM 谱平面改频带主序)是允许的。
|
||||
|
||||
## 运行时怎么选
|
||||
|
||||
`dispatch.cpp` 先解析 `JOC_SIMD`(强制一个本机不支持的层会打印诊断并回退,而不是假装
|
||||
选中然后崩),再走阶梯 `avx512 → avx2 → sse2 → neon`,每个 kernel 单独挑"已编进本
|
||||
二进制 **且** 本机可跑"的最宽实现,挑不到就落到标量参考实现。所以 AVX-512 单元在
|
||||
不支持它的 CPU 上只是不被选中,不影响启动。
|
||||
|
||||
x86 的探测要 CPUID 与 XGETBV **同时**成立:CPUID 说明硅片有这个能力,XCR0 说明操作
|
||||
系统会保存对应寄存器状态,缺一个就不能用(否则上下文切换会踩坏别的线程)。AArch64
|
||||
不需要探测,ASIMD 是架构基线。
|
||||
|
||||
`sse2` 是一个有意保留的档位但**没有单独的单元**:128 位 SSE2 寄存器就是标量 double
|
||||
已经在用的寄存器,SSE2 加宽不了双精度运算,手写只会多出搬运指令,所以它选中的是基线
|
||||
单元。
|
||||
|
||||
## 效果
|
||||
|
||||
30 s 参考用例,同一二进制只切 `JOC_SIMD`(DSP 阶段 `t_render_dsp`):
|
||||
|
||||
| 用例 | `scalar` | `auto` | DSP 加速 | 端到端墙钟 |
|
||||
|---|---|---|---|---|
|
||||
| 双耳 Rosella | 1.256 s | **0.640 s** | **1.96×** | 1.690 → **0.941 s** |
|
||||
| 双耳 SOFA | 1.560 s | **0.654 s** | **2.39×** | 1.859 → **0.849 s** |
|
||||
| 扬声器 5.1 / 9.1.6 / ADM | — | — | 1.00× | 0 回归(不走这些 kernel) |
|
||||
|
||||
单看被向量化的循环,红利更大:合成基函数 5.89×、13 抽头低频拼接 4.50×、
|
||||
SOFA QMF 合成 4.27×、128 点 FFT 2.24×。整条流水线到不了 8×,是因为相当一部分时间在
|
||||
参数设置、直通拷贝、写盘这些没有独立工作项的代码上,以及 SOFA 每次采样的 33 M 次
|
||||
sin/cos 按逐位契约不能向量化。
|
||||
|
||||
## 验证
|
||||
|
||||
```powershell
|
||||
$env:JOC_SIMD_LOG='1' # 本机每个 kernel 实际选中的 ISA
|
||||
$env:JOC_SIMD='scalar' # 强制参考路径:哈希必须一动不动
|
||||
pwsh -NoProfile -File devtools\vec\isa_audit.ps1 # 反汇编全部 .obj:0 个无守卫的宽指令
|
||||
pwsh -NoProfile -File devtools\vec\sha_matrix.ps1 # 5 档 × 2 渲染,逐字节比对参考摘要
|
||||
cmd /c devtools\vec\build_kernel_probe.bat # kernel 级逐字节摘要(8 个 kernel)
|
||||
```
|
||||
|
||||
## 加一个新的 ISA
|
||||
|
||||
1. 写 `kernels_intrin_<isa>.cpp`,实现能实现的槽位,其余留 `nullptr`——派发器会逐
|
||||
kernel 回退(AVX-512 单元里的 `qmf_synthesis_basis` 就是这么处理的)。
|
||||
2. 在 `CMakeLists.txt` 里加进 `JOC_SIMD_SOURCES`、`JOC_SIMD_HAVE_<ISA>=1` 和它的编译
|
||||
开关,并在 `simd.h` / `dispatch.cpp` 里补上 `Isa`、`isa_rank`、`isa_compiled`、
|
||||
`isa_supported` 与 `JOC_SIMD` 名字表。
|
||||
3. 验证:kernel 摘要必须与标量逐字节相同,参考渲染在每个档位上的 SHA-256 都不能变。
|
||||
@@ -1,375 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* EJOC_STATIC: this branch exists for building the sources directly into an application, where nothing is imported or exported. */
|
||||
#if defined(_WIN32)
|
||||
#define EJOC_CALL __cdecl
|
||||
#else
|
||||
#define EJOC_CALL
|
||||
#endif
|
||||
|
||||
#if defined(EJOC_STATIC)
|
||||
#define EJOC_API
|
||||
#elif defined(_WIN32)
|
||||
#if defined(EJOC_BUILD_DLL)
|
||||
#define EJOC_API __declspec(dllexport)
|
||||
#else
|
||||
#define EJOC_API __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define EJOC_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum {
|
||||
EJOC_ABI_VERSION = 4,
|
||||
EJOC_FRAME_SAMPLES = 1536,
|
||||
EJOC_TIMESLOTS = 24,
|
||||
EJOC_SUBBANDS = 64,
|
||||
EJOC_CORE_CHANNELS = 5, /* downmix configurations 0 and 3 */
|
||||
EJOC_MAX_CORE_CHANNELS = 7, /* downmix configurations 1, 2 and 4 */
|
||||
EJOC_OUTPUT_CHANNELS = 16,
|
||||
EJOC_CORE_OUT_CHANNELS = 8, /* passthrough order: L,R,C,LFE,Ls,Rs,(Lrs,Rrs) */
|
||||
EJOC_LFE_SLOT = 3, /* LFE slot of the passthrough order */
|
||||
EJOC_MAX_OBJECTS = 15,
|
||||
EJOC_MAX_DPOINTS = 2,
|
||||
EJOC_MAX_PARAMETER_BANDS = 23,
|
||||
EJOC_SPEAKER_BLOCK_SAMPLES = 32,
|
||||
EJOC_SPEAKER_COORDINATES = 3,
|
||||
EJOC_SPEAKER_CONTROLS_VERSION = 1,
|
||||
EJOC_BINAURAL_BLOCK_SAMPLES = 512,
|
||||
EJOC_BINAURAL_INPUT_CHANNELS = 16,
|
||||
EJOC_BINAURAL_OUTPUT_CHANNELS = 2,
|
||||
EJOC_BINAURAL_QMF_BANDS = 64,
|
||||
EJOC_BINAURAL_HYBRID_BANDS = 77
|
||||
};
|
||||
|
||||
/* Per-frame status of ejoc_renderer_render_frame(). A status outside 0..6 is
|
||||
* the renderer's own no-metadata marker; the parse-status values match
|
||||
* joc_frame_params.status (JOC_FRAME_STATUS_* in joc_core.h). Any non-zero
|
||||
* status makes the frame invalid for matrix rendering: the renderer holds the
|
||||
* previous matrices or passes the core PCM through instead of decoding it. */
|
||||
enum {
|
||||
EJOC_FRAME_STATUS_OK = 0,
|
||||
EJOC_FRAME_STATUS_UNSUPPORTED_DMX = 2,
|
||||
EJOC_FRAME_STATUS_OBJECTS_CLAMPED = 3,
|
||||
EJOC_FRAME_STATUS_BAD_PARAMETER_BAND = 4,
|
||||
EJOC_FRAME_STATUS_BAD_OFFSET_TS = 5,
|
||||
EJOC_FRAME_STATUS_TRAILING_DATA = 6,
|
||||
EJOC_FRAME_STATUS_NO_METADATA = 8
|
||||
};
|
||||
|
||||
typedef void* ejoc_renderer_handle;
|
||||
typedef void* ejoc_speaker_renderer_handle;
|
||||
typedef void* ejoc_binaural_renderer_handle;
|
||||
|
||||
/*
|
||||
Fixed array layouts used by ejoc_renderer_process():
|
||||
bed_planar [core_channel_count][1536]
|
||||
lfe [1536] or NULL
|
||||
n_bands [15]
|
||||
n_dpoints [15]
|
||||
slope_idx [15]
|
||||
offset_ts [15][2]
|
||||
dq [15][2][7][23]
|
||||
output16 [16][1536]
|
||||
|
||||
core_channel_count comes from ejoc_renderer_set_core_channels() and defaults to
|
||||
EJOC_CORE_CHANNELS; the dq stride is always EJOC_MAX_CORE_CHANNELS.
|
||||
|
||||
Only objects selected by object_mask are read from the descriptor arrays.
|
||||
dq carries already dequantized matrix coefficients in double precision; the
|
||||
caller performs the JOC bitstream differential decoding for both dense and
|
||||
sparse objects, so this ABI is identical for both syntaxes.
|
||||
*/
|
||||
|
||||
EJOC_API uint32_t EJOC_CALL ejoc_abi_version(void);
|
||||
EJOC_API const char* EJOC_CALL ejoc_build_info(void);
|
||||
EJOC_API ejoc_renderer_handle EJOC_CALL ejoc_renderer_create(void);
|
||||
EJOC_API void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads);
|
||||
/*
|
||||
core_channel_count comes from joc_dmx_config_idx: 5 for configurations 0 and 3,
|
||||
7 for 1, 2 and 4. bed_planar is then [core_channel_count][1536] and only channels
|
||||
below core_channel_count are read from dq, whose stride stays [15][2][7][23].
|
||||
|
||||
phase_shift is sigma(k) = (k in {3,4}): it enables the Ls/Rs -j rotation of
|
||||
subbands above 0 together with the band-0 21-tap DC compensation. With the switch
|
||||
off, channels 3 and 4 only take the common ten-slot surround delay. The delay
|
||||
line and DC history advance either way, so the flag may change between frames.
|
||||
*/
|
||||
EJOC_API uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle);
|
||||
EJOC_API const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle);
|
||||
|
||||
EJOC_API int EJOC_CALL ejoc_renderer_process(
|
||||
ejoc_renderer_handle handle,
|
||||
const float* bed_planar,
|
||||
const float* lfe,
|
||||
uint32_t core_channel_count,
|
||||
uint32_t phase_shift,
|
||||
uint32_t object_mask,
|
||||
const uint8_t* n_bands,
|
||||
const uint8_t* n_dpoints,
|
||||
const uint8_t* slope_idx,
|
||||
const uint8_t* offset_ts,
|
||||
const double* dq,
|
||||
double clipgain,
|
||||
float phase_new,
|
||||
float output_scale,
|
||||
float* output16_planar);
|
||||
|
||||
/*
|
||||
Frame-level entry point: one E-AC-3 audio frame per call, with the seq state
|
||||
machine and the per-object previous-matrix state kept inside the renderer. This
|
||||
is what produces the 16-channel object result for the ADM, speaker and binaural
|
||||
backends; ejoc_renderer_process() stays as a single-frame kernel for A/B tests.
|
||||
|
||||
Fixed array layouts, identical to ejoc_renderer_process():
|
||||
bed_planar [core_channel_count][1536] (JOC lane order L,R,C,Ls,Rs,(pair))
|
||||
lfe [1536] or NULL (zeros; the LFE delay ring still advances)
|
||||
n_bands, n_dpoints, slope_idx [15]
|
||||
offset_ts [15][2] (raw 5-bit JOC offsets, 0..23)
|
||||
dq [15][2][7][23]
|
||||
output16 [16][1536] (ch0 = LFE, ch1..15 = objects)
|
||||
|
||||
frame_status is a JOC_FRAME_STATUS_* / EJOC_FRAME_STATUS_* value. has_metadata
|
||||
is 0 for an audio frame without a JOC EMDF container (frame_status must then be
|
||||
EJOC_FRAME_STATUS_NO_METADATA); such a frame always passes the 8 core planes
|
||||
through to output slots 0..7, delayed by 1217 samples, with slots 8..15 zero.
|
||||
declared_core_channels is the JOC header's n_channels (0 when unknown); a declared
|
||||
change against the previous frame's actual count turns the frame into a hard cut
|
||||
with the LFE ring cleared. phase_shift is sigma(k) = (k in {3,4}) for the frame's
|
||||
downmix configuration; phase_shift_valid = 0 keeps the previous switch (an
|
||||
unsupported downmix configuration has no usable one). clipgain is the frame's
|
||||
joc_clipgain when frame_status == 0. seq is the 10-bit JOC sequence counter.
|
||||
When has_metadata is 0 the object descriptors are ignored.
|
||||
|
||||
Slot 0 of output16 is the LFE, slots 1..15 are the 15 objects; each frame's
|
||||
values are float32(clamped PCM) * output_scale, exactly like the reference.
|
||||
*/
|
||||
typedef struct ejoc_frame_input {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
const float* bed_planar;
|
||||
const float* lfe;
|
||||
uint32_t core_channel_count;
|
||||
uint32_t declared_core_channels;
|
||||
uint32_t phase_shift;
|
||||
uint32_t phase_shift_valid;
|
||||
uint32_t frame_status;
|
||||
uint32_t seq;
|
||||
uint32_t has_metadata;
|
||||
uint32_t object_mask;
|
||||
const uint8_t* n_bands;
|
||||
const uint8_t* n_dpoints;
|
||||
const uint8_t* slope_idx;
|
||||
const uint8_t* offset_ts;
|
||||
const double* dq;
|
||||
double clipgain;
|
||||
float output_scale;
|
||||
float* output16_planar;
|
||||
} ejoc_frame_input;
|
||||
|
||||
enum { EJOC_FRAME_INPUT_VERSION = 1 };
|
||||
|
||||
EJOC_API int EJOC_CALL ejoc_renderer_render_frame(ejoc_renderer_handle handle,
|
||||
const ejoc_frame_input* input);
|
||||
|
||||
/*
|
||||
High-precision object-to-speaker renderer.
|
||||
|
||||
The renderer consumes interleaved float32 input PCM arranged as:
|
||||
objects16_interleaved [sample_count][16]
|
||||
where channel 0 is LFE and channels 1..15 are point objects. All spatial
|
||||
calculations, gain ramps, and accumulation use double. Output is interleaved:
|
||||
output_interleaved [sample_count][layout_channel_count]
|
||||
|
||||
Each metadata entry is a complete object-state snapshot:
|
||||
metadata_offsets [metadata_count], relative to this process call
|
||||
ramp_durations [metadata_count], in samples
|
||||
positions_q15 [metadata_count][15][3]
|
||||
region_indices [metadata_count][15] or NULL (all region 0)
|
||||
height_enabled [metadata_count][15] or NULL (all enabled)
|
||||
object_gains [metadata_count][15] or NULL (all 1.0)
|
||||
|
||||
metadata_offsets must be nondecreasing and <= sample_count. sample_count must
|
||||
be a multiple of EJOC_SPEAKER_BLOCK_SAMPLES. State and unfinished ramps are
|
||||
preserved across calls.
|
||||
*/
|
||||
|
||||
/*
|
||||
Per-object speaker controls taken from OAMD metadata: warp mode, trim mode,
|
||||
the three trim levels and the two front-back balances. trim_bypass is indexed
|
||||
by object slot (1..15); slot 0 carries the LFE and is unused.
|
||||
|
||||
warp_mode 0 leaves the floor Y coordinate unchanged; mode 1 maps it through
|
||||
clamp(2Y, 0, 1). Modes 2 and 3 render the 5.1-family and 4.0 layouts through an
|
||||
expanded surround layout (7.1-family, or a rear pair added to 4.0 without a
|
||||
centre or LFE) and fold the rear pair into the target surround pair: mode 2
|
||||
uses the PLIIx coefficients (sqrt(3)/2 and 1/2), mode 3 the Standard (Lo/Ro)
|
||||
coefficients (1 and 0). Layouts without a single surround pair render directly.
|
||||
|
||||
trim_mode selects where the gain comes from: automatic applies the layout
|
||||
position compensation, disabled applies no trim, and explicit applies the
|
||||
position-dependent trim from centre_db / surround_db / height_db. A bypassed
|
||||
object keeps its warp but loses the balance and the trim gain.
|
||||
|
||||
Controls may change between calls; each call's metadata events use the
|
||||
controls passed with that call. A null pointer means warp 0, no trim and no
|
||||
balance.
|
||||
*/
|
||||
typedef enum ejoc_speaker_trim_mode {
|
||||
EJOC_SPEAKER_TRIM_AUTOMATIC = 0,
|
||||
EJOC_SPEAKER_TRIM_DISABLED = 1,
|
||||
EJOC_SPEAKER_TRIM_EXPLICIT = 2
|
||||
} ejoc_speaker_trim_mode;
|
||||
|
||||
typedef struct ejoc_speaker_controls {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint32_t warp_mode;
|
||||
uint32_t trim_mode; /* ejoc_speaker_trim_mode */
|
||||
double centre_db;
|
||||
double surround_db; /* positive values are treated as zero */
|
||||
double height_db; /* positive values are treated as zero */
|
||||
double listener_balance; /* [-1, 1] */
|
||||
double overhead_balance; /* [-1, 1] */
|
||||
uint8_t trim_bypass[EJOC_MAX_OBJECTS + 1]; /* per object slot, slot 0 unused */
|
||||
} ejoc_speaker_controls;
|
||||
|
||||
EJOC_API uint32_t EJOC_CALL ejoc_speaker_layout_channel_count(uint32_t speaker_bitfield);
|
||||
EJOC_API ejoc_speaker_renderer_handle EJOC_CALL ejoc_speaker_renderer_create(uint32_t speaker_bitfield);
|
||||
EJOC_API void EJOC_CALL ejoc_speaker_renderer_destroy(ejoc_speaker_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_speaker_renderer_reset(ejoc_speaker_renderer_handle handle);
|
||||
EJOC_API const char* EJOC_CALL ejoc_speaker_renderer_last_error(ejoc_speaker_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_speaker_renderer_process(
|
||||
ejoc_speaker_renderer_handle handle,
|
||||
const float* objects16_interleaved,
|
||||
uint32_t sample_count,
|
||||
uint32_t metadata_count,
|
||||
const uint32_t* metadata_offsets,
|
||||
const uint32_t* ramp_durations,
|
||||
const uint16_t* positions_q15,
|
||||
const uint8_t* region_indices,
|
||||
const uint8_t* height_enabled,
|
||||
const double* object_gains,
|
||||
const ejoc_speaker_controls* controls,
|
||||
double* output_interleaved);
|
||||
|
||||
EJOC_API ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void);
|
||||
EJOC_API void EJOC_CALL ejoc_binaural_renderer_destroy(ejoc_binaural_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_binaural_renderer_reset(ejoc_binaural_renderer_handle handle);
|
||||
EJOC_API const char* EJOC_CALL ejoc_binaural_renderer_last_error(
|
||||
ejoc_binaural_renderer_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_binaural_renderer_configure_kernels(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
const double* qmf_analysis,
|
||||
const double* hybrid_low,
|
||||
const int16_t* hybrid_indices,
|
||||
const double* hybrid_values,
|
||||
uint32_t hybrid_count,
|
||||
const double* qmf_basis,
|
||||
const double* qmf_taps);
|
||||
EJOC_API int EJOC_CALL ejoc_binaural_renderer_configure_room(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
uint32_t bands,
|
||||
uint32_t allpass_count,
|
||||
const uint32_t* allpass_delays,
|
||||
const double* allpass_gains,
|
||||
const uint32_t* fdn_delays,
|
||||
const double* fdn_matrix,
|
||||
uint32_t output_tap_delay,
|
||||
const double* feedback_complex,
|
||||
const double* output_taps,
|
||||
const double* output_complex,
|
||||
uint32_t extra_count,
|
||||
const uint32_t* extra_delays,
|
||||
const double* extra_fields_complex,
|
||||
const double* extra_matrices);
|
||||
EJOC_API int EJOC_CALL ejoc_binaural_renderer_process(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
const double* input16_interleaved,
|
||||
const double* gains_complex,
|
||||
const double* room_sends,
|
||||
double output_gain,
|
||||
double* output_stereo_interleaved);
|
||||
|
||||
/*
|
||||
Native SOFA binaural renderer.
|
||||
|
||||
The handle owns the complete runtime: 64-QMF/77-hybrid analysis and synthesis,
|
||||
fifth-order ACN/N3D real spherical-harmonic direction-field evaluation,
|
||||
per-object whole-QMF-slot delay histories, six first-order image-source early
|
||||
reflections, the shared unitary-FDN late room, the 120-180 Hz LFE low-pass and
|
||||
the 961-sample latency compensation. The caller configures the filterbank
|
||||
tables, the compiled HRTF field and the room constants once, then per 512-sample
|
||||
block updates every source with ejoc_jochrtf_set_source() and calls
|
||||
ejoc_jochrtf_process(). Process returns the number of trimmed stereo
|
||||
samples written; the first 961 processed samples across calls are discarded.
|
||||
*/
|
||||
typedef void* ejoc_jochrtf_handle;
|
||||
|
||||
EJOC_API ejoc_jochrtf_handle EJOC_CALL ejoc_jochrtf_create(void);
|
||||
EJOC_API void EJOC_CALL ejoc_jochrtf_destroy(ejoc_jochrtf_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_reset(ejoc_jochrtf_handle handle);
|
||||
EJOC_API const char* EJOC_CALL ejoc_jochrtf_last_error(
|
||||
ejoc_jochrtf_handle handle);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_configure_kernels(
|
||||
ejoc_jochrtf_handle handle,
|
||||
const double* qmf_analysis,
|
||||
const double* hybrid_low,
|
||||
const int16_t* hybrid_indices,
|
||||
const double* hybrid_values,
|
||||
uint32_t hybrid_count,
|
||||
const double* qmf_basis,
|
||||
const double* qmf_taps);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_configure_field(
|
||||
ejoc_jochrtf_handle handle,
|
||||
const double* coefficients,
|
||||
const double* delay_coefficients,
|
||||
const double* delay_bounds,
|
||||
const double* band_centers,
|
||||
double measurement_radius_m);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_configure_room(
|
||||
ejoc_jochrtf_handle handle,
|
||||
const double* room_dims,
|
||||
const double* listener_pos,
|
||||
const double* wall_gains,
|
||||
double speed_of_sound,
|
||||
const uint32_t* fdn_delays,
|
||||
const double* fdn_feedback,
|
||||
double damping,
|
||||
double fdn_output_gain,
|
||||
const uint32_t* allpass_delays,
|
||||
const double* allpass_gains,
|
||||
uint32_t enable_early_reflections,
|
||||
uint32_t enable_late_room);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_set_source(
|
||||
ejoc_jochrtf_handle handle,
|
||||
uint32_t source,
|
||||
const double* position_adm,
|
||||
uint32_t profile,
|
||||
double gain,
|
||||
uint32_t enabled,
|
||||
uint32_t special_lfe,
|
||||
uint32_t fade);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_process(
|
||||
ejoc_jochrtf_handle handle,
|
||||
const double* input16_interleaved,
|
||||
uint32_t sample_count,
|
||||
double output_gain,
|
||||
double* output_stereo_interleaved);
|
||||
EJOC_API int EJOC_CALL ejoc_jochrtf_finish(
|
||||
ejoc_jochrtf_handle handle,
|
||||
uint32_t flush_samples,
|
||||
double* output_stereo_interleaved,
|
||||
uint32_t capacity);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,520 +0,0 @@
|
||||
/*
|
||||
* joc_core.h -- JustOneCacophony C++ Core, public ABI. Pure C.
|
||||
*
|
||||
* This header is the single authoritative definition of the Core's public
|
||||
* parameter/error surface. Frontends (thin Python CLI, joc_dump, foobar2000,
|
||||
* MPV, FFmpeg) only ever fill these POD structs and read these POD results;
|
||||
* no audio data, no internal DSP concept, and no Python type crosses this
|
||||
* boundary.
|
||||
*
|
||||
* Stability tiers
|
||||
* ---------------
|
||||
* [T1] host tier
|
||||
* joc_abi_version / joc_version_string / joc_build_info, joc_error,
|
||||
* joc_error_name / joc_error_stage, joc_last_error_detail.
|
||||
* Stable, versioned, safe for media hosts.
|
||||
*
|
||||
* [T2] bitstream / verification tier
|
||||
* joc_parse_eac3_frame, joc_parse_id14, joc_frame_params, joc_emdf_info.
|
||||
* These deliberately expose the dequantized JOC matrix coefficients so the
|
||||
* bitstream front-end can be validated bit-exactly and driven by tooling
|
||||
* (joc_dump) and A/B harnesses. Media hosts must NOT use this tier; they
|
||||
* use the task/stream API (added in later milestones).
|
||||
*
|
||||
* Conventions
|
||||
* -----------
|
||||
* * every struct's first two fields are struct_size / struct_version;
|
||||
* * all arrays are fixed size and POD, no pointers, no allocation;
|
||||
* * every function returns joc_error (JOC_OK == 0);
|
||||
* * joc_last_error_detail() returns a thread-local message that stays valid
|
||||
* until the next Core call on the same thread.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "joc_version.h"
|
||||
|
||||
#define JOC_ABI_VERSION 6u
|
||||
#define JOC_FRAME_PARAMS_VERSION 2u
|
||||
#define JOC_EMDF_INFO_VERSION 2u
|
||||
#define JOC_TASK_CONFIG_VERSION 4u
|
||||
#define JOC_TASK_RESULT_VERSION 2u
|
||||
#define JOC_EVENT_VERSION 1u
|
||||
|
||||
/* JOC_STATIC: this branch exists for building the sources directly into an application, where nothing is imported or exported. */
|
||||
#if defined(_WIN32)
|
||||
#define JOC_CALL __cdecl
|
||||
#else
|
||||
#define JOC_CALL
|
||||
#endif
|
||||
|
||||
#if defined(JOC_STATIC)
|
||||
#define JOC_API
|
||||
#elif defined(_WIN32)
|
||||
#if defined(JOC_BUILD_DLL)
|
||||
#define JOC_API __declspec(dllexport)
|
||||
#else
|
||||
#define JOC_API __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define JOC_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Fixed layout constants (single source of truth for every frontend) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
enum {
|
||||
JOC_FRAME_SAMPLES = 1536, /* E-AC-3 frame = 24 * 64 */
|
||||
JOC_TIMESLOTS = 24,
|
||||
JOC_SUBBANDS = 64,
|
||||
JOC_CORE_CHANNELS = 5, /* L R C Ls Rs (JOC order) */
|
||||
JOC_MAX_CORE_CHANNELS = 7, /* dmx_config_idx 1/2/4 declare 7 */
|
||||
JOC_OUTPUT_CHANNELS = 16, /* ch0 = LFE, ch1..15 = objects */
|
||||
JOC_MAX_OBJECTS = 15,
|
||||
JOC_MAX_DPOINTS = 2,
|
||||
JOC_MAX_PARAMETER_BANDS = 23,
|
||||
JOC_MAX_EMDF_PAYLOADS = 16,
|
||||
JOC_SPEAKER_BLOCK_SAMPLES = 32,
|
||||
JOC_BINAURAL_BLOCK_SAMPLES = 512,
|
||||
JOC_BINAURAL_QMF_BANDS = 64,
|
||||
JOC_BINAURAL_HYBRID_BANDS = 77,
|
||||
JOC_QMF_HOP_SAMPLES = 64,
|
||||
JOC_BINAURAL_LATENCY_SAMPLES = 961, /* SOFA/.jochrtf and Rosella bank 1 */
|
||||
JOC_LFE_DELAY_SAMPLES = 1217
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* [T1] library / error surface */
|
||||
/* ------------------------------------------------------------------ */
|
||||
typedef enum joc_error {
|
||||
JOC_OK = 0,
|
||||
JOC_ERR_INVALID_ARGUMENT,
|
||||
JOC_ERR_INVALID_CONFIG,
|
||||
JOC_ERR_OUT_OF_MEMORY,
|
||||
JOC_ERR_IO,
|
||||
JOC_ERR_UNSUPPORTED_PLATFORM,
|
||||
JOC_ERR_LIBRARY_MISSING,
|
||||
/* input / bitstream */
|
||||
JOC_ERR_INPUT_NOT_FOUND,
|
||||
JOC_ERR_INPUT_FORMAT,
|
||||
JOC_ERR_EAC3_SYNCFRAME,
|
||||
JOC_ERR_EMDF_TRANSPORT,
|
||||
JOC_ERR_EMDF_SYNTAX,
|
||||
JOC_ERR_JOC_SYNTAX,
|
||||
JOC_ERR_JOC_UNSUPPORTED_VARIANT,
|
||||
JOC_ERR_OAMD_SYNTAX,
|
||||
JOC_ERR_OAMD_UNSUPPORTED_VARIANT,
|
||||
JOC_ERR_BITSTREAM_TRUNCATED,
|
||||
JOC_ERR_BITSTREAM_PADDING,
|
||||
/* resources */
|
||||
JOC_ERR_HRTF_NOT_FOUND,
|
||||
JOC_ERR_HRTF_FORMAT,
|
||||
JOC_ERR_HRTF_VERSION,
|
||||
JOC_ERR_HRTF_HASH,
|
||||
JOC_ERR_HRTF_UNSUPPORTED_CONVENTION,
|
||||
/* rendering / output */
|
||||
JOC_ERR_LAYOUT_UNSUPPORTED,
|
||||
JOC_ERR_RENDER_FAILED,
|
||||
JOC_ERR_OUTPUT_OPEN,
|
||||
JOC_ERR_OUTPUT_WRITE,
|
||||
JOC_ERR_OUTPUT_CLIP_ABORT,
|
||||
JOC_ERR_ADM_VALIDATION,
|
||||
/* task / stream */
|
||||
JOC_ERR_CANCELLED,
|
||||
JOC_ERR_STATE,
|
||||
JOC_ERR_NOT_SUPPORTED,
|
||||
JOC_ERR_INTERNAL
|
||||
} joc_error;
|
||||
|
||||
JOC_API uint32_t JOC_CALL joc_abi_version(void);
|
||||
JOC_API const char* JOC_CALL joc_version_string(void);
|
||||
JOC_API const char* JOC_CALL joc_build_info(void);
|
||||
/* Struct sizes, so a binding can assert its layout matches the library instead of
|
||||
* assuming (mismatches are otherwise silent memory corruption). */
|
||||
JOC_API uint32_t JOC_CALL joc_event_size(void);
|
||||
JOC_API uint32_t JOC_CALL joc_task_config_size(void);
|
||||
JOC_API uint32_t JOC_CALL joc_task_result_size(void);
|
||||
JOC_API const char* JOC_CALL joc_error_name(joc_error code);
|
||||
JOC_API const char* JOC_CALL joc_error_stage(joc_error code);
|
||||
/* Thread-local structured detail for the most recent failing call. */
|
||||
JOC_API const char* JOC_CALL joc_last_error_detail(void);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* [T2] bitstream / verification tier */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/* One EMDF payload directory entry. bit_offset is the MSB-first bit
|
||||
* position of the payload's first byte inside the syncframe, exactly as the
|
||||
* EMDF container syntax defines it (payloads are not byte aligned in general). */
|
||||
typedef struct joc_emdf_payload_info {
|
||||
uint8_t id;
|
||||
uint8_t reserved[3];
|
||||
uint32_t sample_offset; /* EMDF outer smpoffst: variable_bits(11) */
|
||||
uint32_t bit_offset;
|
||||
uint32_t size; /* payload bytes */
|
||||
} joc_emdf_payload_info;
|
||||
|
||||
typedef struct joc_emdf_info {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint32_t start_bit; /* container syncword bit position */
|
||||
uint32_t container_bytes; /* 4 + declared length */
|
||||
uint32_t payload_count;
|
||||
uint32_t reserved;
|
||||
joc_emdf_payload_info payloads[JOC_MAX_EMDF_PAYLOADS];
|
||||
} joc_emdf_info;
|
||||
|
||||
/* Per-object ID14/JOC descriptor plus its dequantized matrix.
|
||||
* dq[dp][ch][pb] is zero filled outside [0,n_dpoints) x [0,n_channels) x
|
||||
* [0,n_bands). Absent objects are entirely zero. offset_ts holds the raw
|
||||
* 5-bit JOC offsets (legal range 0..23, strictly increasing per object). */
|
||||
typedef struct joc_object_params {
|
||||
uint8_t present;
|
||||
uint8_t num_bands_idx;
|
||||
uint8_t n_bands;
|
||||
uint8_t sparse; /* 0 = dense (MTX), 1 = sparse (IDX+VEC) */
|
||||
uint8_t quant_idx; /* 0 = 96 levels, 1 = 192 levels */
|
||||
uint8_t slope_idx; /* 0 = interpolate, 1 = step at offset_ts */
|
||||
uint8_t num_dpoints_bits;
|
||||
uint8_t n_dpoints;
|
||||
uint8_t offset_ts[JOC_MAX_DPOINTS];
|
||||
uint8_t reserved[2];
|
||||
double dq[JOC_MAX_DPOINTS][JOC_MAX_CORE_CHANNELS][JOC_MAX_PARAMETER_BANDS];
|
||||
} joc_object_params;
|
||||
|
||||
/* Per-frame JOC parse status (joc_frame_params.status). A non-zero status makes
|
||||
* the frame invalid for rendering: the renderer holds the previous matrices or
|
||||
* passes the core PCM through instead of decoding the matrix. Statuses 3 and 7
|
||||
* still carry the complete header and object list for diagnostics; statuses 2
|
||||
* and 4 stop the parse after the failing header field, and 7 stops inside
|
||||
* joc_data, so only the per-object matrices are missing there. */
|
||||
enum {
|
||||
JOC_FRAME_STATUS_OK = 0,
|
||||
JOC_FRAME_STATUS_UNSUPPORTED_DMX = 2, /* joc_dmx_config_idx outside 0..4 */
|
||||
JOC_FRAME_STATUS_OBJECTS_CLAMPED = 3, /* declared object count above 15 */
|
||||
JOC_FRAME_STATUS_BAD_PARAMETER_BAND = 4, /* unknown num_bands index */
|
||||
JOC_FRAME_STATUS_BAD_OFFSET_TS = 5, /* offset_ts outside 0..23 or not */
|
||||
/* strictly increasing */
|
||||
JOC_FRAME_STATUS_TRAILING_DATA = 6, /* bytes after joc_data alignment */
|
||||
/* The ID14 payload ends before its joc_data is complete. The header,
|
||||
* clipgain, sequence counter and object parameters are intact, so the frame
|
||||
* stays classified and is concealed by holding the previous matrices. */
|
||||
JOC_FRAME_STATUS_TRUNCATED = 7
|
||||
};
|
||||
|
||||
typedef struct joc_frame_params {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint8_t dmx_config_idx;
|
||||
uint8_t num_objects_bits;
|
||||
uint8_t ext_config_idx;
|
||||
uint8_t n_objects;
|
||||
uint8_t n_channels; /* 5 or 7 */
|
||||
uint8_t clipgain_x_bits;
|
||||
uint8_t clipgain_y_bits;
|
||||
uint8_t objects_clamped; /* num_objects_bits+1 exceeded 15 */
|
||||
uint32_t seq_count; /* 10-bit JOC sequence counter */
|
||||
uint32_t present_mask; /* bit i == object i present */
|
||||
uint8_t status; /* JOC_FRAME_STATUS_* */
|
||||
uint8_t reserved[3];
|
||||
uint32_t data_end_bits; /* bit position just after joc_data */
|
||||
uint32_t trailing_bits; /* bits left after joc_data (padding/ext) */
|
||||
uint8_t tail_bytes[8]; /* first up to 8 trailing bytes, for A/B */
|
||||
double clipgain; /* 2^(x-4) * (1 + y/32), bit-exact */
|
||||
joc_object_params objects[JOC_MAX_OBJECTS];
|
||||
} joc_frame_params;
|
||||
|
||||
/* Parse an EMDF ID14 (JOC) payload. A payload that ends inside joc_data is a
|
||||
* damaged frame rather than a transport error: the call succeeds, the header and
|
||||
* object parameters are filled in, and out_params->status is
|
||||
* JOC_FRAME_STATUS_TRUNCATED. A payload too short for the 12-bit joc_header is
|
||||
* unclassifiable and stays JOC_ERR_BITSTREAM_TRUNCATED. */
|
||||
JOC_API joc_error JOC_CALL joc_parse_id14(const uint8_t* payload, size_t payload_size,
|
||||
joc_frame_params* out_params);
|
||||
|
||||
/* Locate the contiguous JOC EMDF container inside one E-AC-3 syncframe and
|
||||
* parse its ID14 payload. out_emdf may be NULL. A damaged ID14 payload is
|
||||
* reported through out_params->status like joc_parse_id14(); an incomplete
|
||||
* container stays a transport error. */
|
||||
JOC_API joc_error JOC_CALL joc_parse_eac3_frame(const uint8_t* frame, size_t frame_size,
|
||||
joc_frame_params* out_params,
|
||||
joc_emdf_info* out_emdf);
|
||||
|
||||
/* Copy one payload's bytes out of a syncframe (MSB-first bit extraction, which
|
||||
* equals a memcpy for byte-aligned containers). out_size receives the payload
|
||||
* byte count; pass out == NULL to query only the size. */
|
||||
JOC_API joc_error JOC_CALL joc_extract_payload(const uint8_t* frame, size_t frame_size,
|
||||
const joc_emdf_payload_info* payload,
|
||||
uint8_t* out, size_t out_capacity,
|
||||
size_t* out_size);
|
||||
|
||||
/* E-AC-3 syncframe traversal: given the offset of a frame start, report its
|
||||
* byte length so a host can walk a bare E-AC-3 stream without duplicating
|
||||
* frmsiz logic. Rejects resynchronisation (no silent recovery). */
|
||||
JOC_API joc_error JOC_CALL joc_eac3_frame_bytes(const uint8_t* data, size_t size,
|
||||
size_t offset, size_t* out_frame_bytes);
|
||||
|
||||
/* Strict trailing-bit check for the JOC payload (A/B robustness corpus).
|
||||
* Returns JOC_ERR_BITSTREAM_PADDING when more than 7 bits are left over or the
|
||||
* leftover bits are not zero, and JOC_ERR_BITSTREAM_TRUNCATED when the payload
|
||||
* ends inside joc_data. */
|
||||
JOC_API joc_error JOC_CALL joc_check_id14_padding(const uint8_t* payload, size_t payload_size,
|
||||
uint32_t* out_trailing_bits);
|
||||
|
||||
/* ================================================================== */
|
||||
/* [T1] host tier: task, telemetry and control */
|
||||
/* ================================================================== */
|
||||
|
||||
/* ---- 1. events ---------------------------------------------------- */
|
||||
/*
|
||||
* Events carry state only: frame/sample counters, stage, progress, statistics,
|
||||
* warnings, errors and paths. Audio never travels through an event; a fixed
|
||||
* size POD with no pointers keeps that enforceable (see the static assertion in
|
||||
* the implementation).
|
||||
*/
|
||||
typedef enum joc_event_type {
|
||||
JOC_EV_TASK_STARTED = 0x0001,
|
||||
JOC_EV_TASK_STATE_CHANGED = 0x0002,
|
||||
JOC_EV_TASK_COMPLETED = 0x0003,
|
||||
JOC_EV_TASK_FAILED = 0x0004,
|
||||
JOC_EV_TASK_CANCELLED = 0x0005,
|
||||
JOC_EV_INPUT_OPENED = 0x0101,
|
||||
JOC_EV_METADATA_INDEXED = 0x0103,
|
||||
JOC_EV_HRTF_LOADED = 0x0110,
|
||||
JOC_EV_RENDERER_INITIALIZED = 0x0120,
|
||||
JOC_EV_PROGRESS = 0x0201,
|
||||
JOC_EV_STAGE_CHANGED = 0x0202,
|
||||
JOC_EV_JOC_FRAME_STATS = 0x0301,
|
||||
JOC_EV_OAMD_STATS = 0x0302,
|
||||
JOC_EV_OUTPUT_STATS = 0x0303,
|
||||
JOC_EV_OUTPUT_OPENED = 0x0401,
|
||||
JOC_EV_OUTPUT_FORMAT_DECIDED = 0x0402,
|
||||
JOC_EV_OUTPUT_FINALIZED = 0x0403,
|
||||
JOC_EV_LOG = 0x0501,
|
||||
JOC_EV_WARNING = 0x0502,
|
||||
JOC_EV_ERROR = 0x0503
|
||||
} joc_event_type;
|
||||
|
||||
typedef enum joc_stage {
|
||||
JOC_STAGE_IDLE = 0,
|
||||
JOC_STAGE_INPUT = 1,
|
||||
JOC_STAGE_METADATA = 2,
|
||||
JOC_STAGE_DECODE = 3,
|
||||
JOC_STAGE_JOC = 4,
|
||||
JOC_STAGE_RENDER = 5,
|
||||
JOC_STAGE_OUTPUT = 6,
|
||||
JOC_STAGE_DONE = 7
|
||||
} joc_stage;
|
||||
|
||||
enum { JOC_LOG_TRACE = 0, JOC_LOG_DEBUG = 1, JOC_LOG_INFO = 2, JOC_LOG_NOTICE = 3,
|
||||
JOC_LOG_WARNING = 4, JOC_LOG_ERROR = 5, JOC_LOG_FATAL = 6 };
|
||||
|
||||
typedef struct joc_event {
|
||||
uint32_t struct_size;
|
||||
uint32_t type;
|
||||
uint64_t sequence;
|
||||
uint64_t timestamp_us;
|
||||
uint64_t current_frame;
|
||||
uint64_t total_frames;
|
||||
uint64_t current_sample;
|
||||
uint64_t total_samples;
|
||||
uint32_t stage;
|
||||
uint32_t backend;
|
||||
double progress; /* 0..1, -1 = unknown */
|
||||
double elapsed_seconds;
|
||||
double realtime_factor;
|
||||
uint64_t output_samples;
|
||||
uint64_t output_bytes;
|
||||
double output_duration_seconds;
|
||||
joc_error error_code;
|
||||
uint32_t log_level;
|
||||
char stage_name[32];
|
||||
char message[256];
|
||||
} joc_event;
|
||||
|
||||
typedef void (JOC_CALL *joc_event_fn)(void* user, const joc_event* event);
|
||||
|
||||
typedef struct joc_event_sink {
|
||||
uint32_t struct_size;
|
||||
joc_event_fn callback;
|
||||
void* user;
|
||||
uint32_t min_type; /* 0 = no filter */
|
||||
uint32_t max_type; /* 0 = no filter */
|
||||
} joc_event_sink;
|
||||
|
||||
/* ---- 2. cancellation ---------------------------------------------- */
|
||||
typedef struct joc_cancel_token joc_cancel_token;
|
||||
|
||||
JOC_API joc_cancel_token* JOC_CALL joc_cancel_token_create(void);
|
||||
JOC_API void JOC_CALL joc_cancel_token_request(joc_cancel_token* token);
|
||||
JOC_API int32_t JOC_CALL joc_cancel_token_is_requested(const joc_cancel_token* token);
|
||||
JOC_API void JOC_CALL joc_cancel_token_destroy(joc_cancel_token* token);
|
||||
|
||||
/* ---- 3. task configuration and results ---------------------------- */
|
||||
typedef enum joc_operation {
|
||||
JOC_OP_ADM_BWF = 0,
|
||||
JOC_OP_SPEAKER = 1,
|
||||
JOC_OP_BINAURAL = 2
|
||||
} joc_operation;
|
||||
|
||||
typedef enum joc_output_format {
|
||||
JOC_FORMAT_FLOAT32 = 0,
|
||||
JOC_FORMAT_PCM24 = 1
|
||||
} joc_output_format;
|
||||
|
||||
typedef enum joc_binaural_mode {
|
||||
JOC_BINAURAL_OFF = 0,
|
||||
JOC_BINAURAL_NEAR = 1,
|
||||
JOC_BINAURAL_FAR = 2,
|
||||
JOC_BINAURAL_MID = 3
|
||||
} joc_binaural_mode;
|
||||
|
||||
typedef enum joc_trajectory_mode {
|
||||
JOC_TRAJECTORY_COMPACT = 0,
|
||||
JOC_TRAJECTORY_DENSE64 = 1
|
||||
} joc_trajectory_mode;
|
||||
|
||||
/* What to do when an int24 WAV would clip (peak outside [-1, 1]). Mirrors the
|
||||
* reference CLI's --clip-action; ADM BWF output is always int24 and does not
|
||||
* consult this because no alternative format exists there. */
|
||||
typedef enum joc_clip_action {
|
||||
JOC_CLIP_ASK = 0, /* prompt on stdin; an error when stdin is not a terminal */
|
||||
JOC_CLIP_CONTINUE = 1, /* write int24, truncating out-of-range values */
|
||||
JOC_CLIP_FLOAT32 = 2, /* switch the output to float32 */
|
||||
JOC_CLIP_ABORT = 3 /* fail the task */
|
||||
} joc_clip_action;
|
||||
|
||||
/* Which 64-QMF / 77-hybrid filterbank the Rosella .personalized_headphone path
|
||||
* renders through. The two banks differ in coefficients, hybrid tap count,
|
||||
* high-band slot delay and total latency, so they are not interchangeable; the
|
||||
* SOFA and .jochrtf paths always use JOC_ROSELLA_FILTERBANK_PUBLIC. */
|
||||
typedef enum joc_rosella_filterbank {
|
||||
JOC_ROSELLA_FILTERBANK_PUBLIC = 1, /* 13-tap hybrid, 6-slot delay, 961 samples */
|
||||
JOC_ROSELLA_FILTERBANK_CAPTURED = 2 /* 11-tap hybrid, 3-slot delay, 448 samples */
|
||||
} joc_rosella_filterbank;
|
||||
|
||||
enum { JOC_TASK_F_SKIP_SHA256 = 1u, JOC_TASK_F_KEEP_INTERMEDIATE = 2u,
|
||||
JOC_TASK_F_QUIET = 4u, JOC_TASK_F_METADATA_ONLY = 8u };
|
||||
|
||||
typedef struct joc_task_config {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
|
||||
/* input */
|
||||
const char* input_path; /* .eac3/.ec3/.m4a/... */
|
||||
const char* ffmpeg_path; /* NULL = FFMPEG, then "ffmpeg" on PATH */
|
||||
const char* bed_path; /* NULL = decode the core PCM with ffmpeg */
|
||||
const char* work_dir; /* NULL = a temporary directory */
|
||||
double eac3_drc_scale; /* 0 = DRC off (reference default) */
|
||||
int32_t eac3_target_level; /* -31..0, 0 = not applied */
|
||||
|
||||
/* output */
|
||||
uint32_t operation;
|
||||
const char* output_path;
|
||||
uint32_t output_format; /* requested format for speaker/binaural */
|
||||
uint32_t flags;
|
||||
uint32_t clip_action; /* joc_clip_action; JOC_CLIP_ASK by default */
|
||||
|
||||
/* rendering */
|
||||
const char* speaker_layout_name;
|
||||
uint32_t speaker_metadata_offset; /* default 1473 */
|
||||
uint32_t binaural_mode;
|
||||
const char* hrtf_path; /* .jochrtf */
|
||||
const char* kernels_path; /* rosella_kernels.npz */
|
||||
double binaural_tail_seconds; /* default 5.0 */
|
||||
double binaural_tail_threshold; /* binaural only; 0 disables trimming */
|
||||
uint32_t binaural_chunk_frames; /* accepted for CLI parity; no effect */
|
||||
|
||||
/* ADM */
|
||||
uint32_t adm_binaural_mode; /* DBMD segment 10 encoding */
|
||||
uint32_t trajectory_mode;
|
||||
|
||||
/* generic */
|
||||
uint32_t object_delay_samples; /* default 1473 */
|
||||
double gain_db; /* default 0 */
|
||||
uint64_t duration_frames; /* 0 = the whole stream */
|
||||
uint32_t progress_interval_frames; /* default 1000 */
|
||||
uint32_t native_threads; /* 0 = automatic */
|
||||
|
||||
/* diagnostics */
|
||||
uint32_t print_metadata; /* 0 none, 1 summary, 2 per frame */
|
||||
const char* metadata_json_path; /* NULL = no JSON summary */
|
||||
|
||||
joc_cancel_token* cancel; /* optional */
|
||||
|
||||
/* Binaural input (config version 2): a compiled .jochrtf field (hrtf_path)
|
||||
* or a Rosella .personalized_headphone model. */
|
||||
const char* personalized_headphone_path; /* Rosella .personalized_headphone input */
|
||||
uint32_t rosella_filterbank; /* joc_rosella_filterbank; 0 = PUBLIC */
|
||||
uint32_t jochrtf_room; /* non-zero enables the project room */
|
||||
} joc_task_config;
|
||||
|
||||
typedef enum joc_task_status {
|
||||
JOC_TASK_OK = 0,
|
||||
JOC_TASK_FAILED = 1,
|
||||
JOC_TASK_CANCELLED = 2
|
||||
} joc_task_status;
|
||||
|
||||
typedef struct joc_task_result {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint32_t status;
|
||||
uint32_t error_code;
|
||||
char error_message[512];
|
||||
char error_stage[32];
|
||||
uint64_t input_frames;
|
||||
uint64_t output_samples;
|
||||
double duration_sec;
|
||||
uint32_t output_format_actual;
|
||||
double output_peak;
|
||||
uint64_t output_over_unity_values;
|
||||
uint64_t output_file_bytes;
|
||||
char output_sha256[65]; /* empty when skipped */
|
||||
uint64_t oamd_payloads;
|
||||
uint64_t oamd_transitions;
|
||||
double t_decode_bed;
|
||||
double t_render;
|
||||
double t_write;
|
||||
double t_total;
|
||||
double t_render_dsp; /* speaker/binaural DSP calls only; 0 for ADM */
|
||||
double t_write_file; /* disk writes including the finalize; >= t_write */
|
||||
} joc_task_result;
|
||||
|
||||
typedef struct joc_validation_issue {
|
||||
joc_error code;
|
||||
uint32_t severity; /* 0 = info, 1 = warning, 2 = error */
|
||||
char field[48];
|
||||
char message[256];
|
||||
} joc_validation_issue;
|
||||
|
||||
/* ---- 4. entry points ---------------------------------------------- */
|
||||
/* Fills `issues` (up to `capacity`) and reports how many were produced.
|
||||
* Returns JOC_OK when no *error*-severity issue was found. */
|
||||
JOC_API joc_error JOC_CALL joc_task_validate(const joc_task_config* config,
|
||||
joc_validation_issue* issues, uint32_t capacity,
|
||||
uint32_t* count);
|
||||
|
||||
/* Runs the whole task on the calling thread. `sink` may be NULL; `out` may be
|
||||
* NULL. On cancellation the partial output is removed and JOC_ERR_CANCELLED is
|
||||
* returned with out->status = JOC_TASK_CANCELLED. */
|
||||
JOC_API joc_error JOC_CALL joc_task_execute(const joc_task_config* config,
|
||||
const joc_event_sink* sink, joc_task_result* out);
|
||||
|
||||
/* Serialises the stable subset of the result as JSON (no environment fields -
|
||||
* those belong to the frontend). `needed` receives the required size including
|
||||
* the terminator; a NULL buffer queries only the size. */
|
||||
JOC_API joc_error JOC_CALL joc_task_result_to_json(const joc_task_result* result, char* buffer,
|
||||
size_t capacity, size_t* needed);
|
||||
|
||||
/* The streaming (push/pull) tier lives in "joc_stream.h" so an embedder can
|
||||
* include the narrow surface without the bitstream/verification tier. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
@@ -1,144 +0,0 @@
|
||||
/*
|
||||
* joc_stream.h -- streaming (push/pull) interface of the JustOneCacophony core.
|
||||
*
|
||||
* This is the narrow, embedder-facing surface: a player or decoder component
|
||||
* includes only this header. It is the same shared library as joc_core.h, split
|
||||
* so an integrator never has to see the bitstream/verification tier.
|
||||
*
|
||||
* A stream is a stateful instance for hosts that cannot wait for a whole file:
|
||||
* the caller feeds E-AC-3 bytes and the core PCM of the same frames (or already
|
||||
* rebuilt objects16) and pulls rendered PCM as soon as it is available. It
|
||||
* mirrors the two library shapes a decoder library normally offers: this
|
||||
* push/pull form for host-owned I/O, and joc_task_execute() in joc_core.h for
|
||||
* library-owned file I/O.
|
||||
*
|
||||
* Contract:
|
||||
* - all state is instance-private, so several streams coexist;
|
||||
* - a stream is NOT thread safe: push and pull must come from one thread;
|
||||
* - rendering is stateful (matrix interpolation, gain ramps, room tail), so a
|
||||
* new position on the timeline requires decoding to continue from the start
|
||||
* of the stream; there is no seek in this version;
|
||||
* - the kernel latency is 961 samples for speaker/binaural output: the first
|
||||
* pull after two 512-sample blocks, and flush() drains the binaural tail.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
/* JOC_STATIC (building the sources directly into an application): joc_core.h above already installs the empty JOC_API. */
|
||||
#if defined(JOC_STATIC) && !defined(JOC_API)
|
||||
#define JOC_API
|
||||
#define JOC_CALL __cdecl
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct joc_stream joc_stream;
|
||||
|
||||
typedef enum joc_stream_input {
|
||||
JOC_STREAM_IN_EAC3 = 0, /* bare E-AC-3 syncframes (the metadata stream) */
|
||||
JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* objects16 frames, planar [16][1536] each */
|
||||
JOC_STREAM_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */
|
||||
} joc_stream_input;
|
||||
|
||||
typedef enum joc_stream_output {
|
||||
JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* objects16 frames, planar [16][1536] each */
|
||||
JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */
|
||||
JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */
|
||||
} joc_stream_output;
|
||||
|
||||
typedef struct joc_stream_config {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint32_t input;
|
||||
uint32_t output;
|
||||
const char* speaker_layout_name;
|
||||
uint32_t speaker_metadata_offset; /* default 1473 */
|
||||
uint32_t binaural_mode; /* near|mid|far; 0 means mid */
|
||||
const char* hrtf_path; /* binaural only */
|
||||
const char* kernels_path; /* binaural only */
|
||||
double binaural_tail_seconds; /* default 5.0 */
|
||||
uint32_t object_delay_samples; /* default 1473 */
|
||||
double gain_db; /* default 0 */
|
||||
uint32_t native_threads;
|
||||
uint32_t rosella_filterbank; /* joc_rosella_filterbank; Rosella input, 0 = PUBLIC */
|
||||
/* Binaural input, the same shapes joc_task_config accepts: a compiled
|
||||
* .jochrtf field (hrtf_path) or a Rosella .personalized_headphone model. */
|
||||
const char* personalized_headphone_path; /* Rosella .personalized_headphone input */
|
||||
uint32_t jochrtf_room; /* non-zero enables the project room */
|
||||
} joc_stream_config;
|
||||
|
||||
typedef struct joc_stream_buffer {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint32_t kind; /* which joc_stream_input/output this buffer carries */
|
||||
uint32_t channels;
|
||||
uint32_t sample_rate;
|
||||
uint32_t sample_count; /* in: capacity / out: produced (per channel) */
|
||||
uint32_t byte_count; /* in: capacity / out: consumed or produced bytes */
|
||||
uint32_t reserved;
|
||||
const uint8_t* bytes; /* EAC3 input */
|
||||
const float* pcm; /* PCM input */
|
||||
uint8_t* out_bytes; /* reserved for encoded outputs */
|
||||
float* out_pcm; /* PCM output */
|
||||
} joc_stream_buffer;
|
||||
|
||||
typedef struct joc_stream_status_info {
|
||||
uint32_t struct_size;
|
||||
uint32_t struct_version;
|
||||
uint64_t frames_in;
|
||||
uint64_t frames_out;
|
||||
uint64_t samples_in;
|
||||
uint64_t samples_out;
|
||||
uint64_t bytes_in;
|
||||
uint64_t buffered_samples; /* rendered but not yet pulled, per channel */
|
||||
uint64_t oamd_payloads;
|
||||
uint64_t oamd_transitions;
|
||||
/* Metadata the renderer had to work around, per audio frame: no JOC payload at
|
||||
* all, an ID14 payload that ends inside joc_data (that frame holds the previous
|
||||
* matrices), a declared object count clamped to 15, and a syncframe carrying a
|
||||
* second EMDF container that is not this frame's JOC metadata. */
|
||||
uint64_t missing_joc_frames;
|
||||
uint64_t objects_clamped_frames;
|
||||
uint64_t extra_container_frames;
|
||||
/* Extra-pair identity disagreements across layers (spec 11.2 checks 1 and 2).
|
||||
* The identity is metadata only, so those frames still render. */
|
||||
uint64_t chanmap_pair_mismatch_frames;
|
||||
uint64_t bed_mask_mismatch_frames;
|
||||
uint32_t output_channels;
|
||||
uint32_t ended; /* 1 after flush() */
|
||||
/* Appended field: joc_stream_status() accepts the size that ends at `ended`
|
||||
* as well, so a caller built against the previous header keeps working. */
|
||||
uint64_t truncated_joc_frames;
|
||||
} joc_stream_status_info;
|
||||
|
||||
JOC_API joc_error JOC_CALL joc_stream_create(const joc_stream_config* config, joc_stream** out);
|
||||
|
||||
/* Feeds one buffer. Kind selects the path: EAC3 bytes, the core PCM of those
|
||||
* frames, or objects16. Consumed counts are reported so a caller can resume
|
||||
* from a partial push. */
|
||||
JOC_API joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer* input,
|
||||
uint32_t* consumed_samples, uint32_t* consumed_bytes);
|
||||
|
||||
/* Copies as many rendered samples as fit into `output` (interleaved, or planar
|
||||
* for PCM_OBJECTS16) and reports how many were produced; 0 means "push more". */
|
||||
JOC_API joc_error JOC_CALL joc_stream_pull(joc_stream* stream, joc_stream_buffer* output,
|
||||
uint32_t* produced_samples);
|
||||
|
||||
/* Marks the end of input and drains whatever the renderer still holds (the
|
||||
* binaural room tail); pull the remaining samples afterwards. */
|
||||
JOC_API joc_error JOC_CALL joc_stream_flush(joc_stream* stream);
|
||||
|
||||
/* Returns the instance to its initial state (kernel, ramps, timeline, room,
|
||||
* parser, counters) so the same stream can be reused for another pass. */
|
||||
JOC_API joc_error JOC_CALL joc_stream_reset(joc_stream* stream);
|
||||
|
||||
JOC_API joc_error JOC_CALL joc_stream_status(const joc_stream* stream,
|
||||
joc_stream_status_info* out);
|
||||
JOC_API joc_error JOC_CALL joc_stream_destroy(joc_stream* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
@@ -1,16 +0,0 @@
|
||||
/* Product version of joc_core and joc_cli.
|
||||
*
|
||||
* This file is the single source of truth: CMakeLists.txt reads these numbers for
|
||||
* project(VERSION ...), joc_version_string() returns JOC_VERSION_STRING, and the
|
||||
* release workflow refuses a tag that does not match it. Bump the three numbers
|
||||
* and the string together, then tag the commit with v<JOC_VERSION_STRING>.
|
||||
*/
|
||||
#ifndef JOC_VERSION_H
|
||||
#define JOC_VERSION_H
|
||||
|
||||
#define JOC_VERSION_MAJOR 1
|
||||
#define JOC_VERSION_MINOR 3
|
||||
#define JOC_VERSION_PATCH 1
|
||||
#define JOC_VERSION_STRING "1.3.1"
|
||||
|
||||
#endif /* JOC_VERSION_H */
|
||||
@@ -0,0 +1,961 @@
|
||||
"""JustOneCacophony 的 E-AC-3 JOC 命令行入口。"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
PROJECT_DIR = Path(__file__).resolve().parent
|
||||
SOURCE_DIR = PROJECT_DIR / "src"
|
||||
if str(SOURCE_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(SOURCE_DIR))
|
||||
|
||||
import numpy as np
|
||||
|
||||
import adm_assemble
|
||||
import adm_atmos
|
||||
from adm_validate import validate
|
||||
from bed_layout import (BED_CHANNEL_NAMES, BED_EXTRA_SLOTS_BY_CONFIG, bed_order,
|
||||
extra_pair, joc_input_indices)
|
||||
from emdf import expand_chanmap
|
||||
from metadata import DirectPayloadIndex, PayloadIndex, write_summary
|
||||
import oamd_tracks
|
||||
from renderer import JocRenderer
|
||||
from binaural_renderer import (
|
||||
JochrtfBinauralRenderer,
|
||||
resolve_compiled_hrtf_cache,
|
||||
)
|
||||
from rosella_binaural_renderer import (
|
||||
DEFAULT_PERSONALIZED_HEADPHONE,
|
||||
ROSSELLA_BLOCK_SAMPLES,
|
||||
RosellaBinauralRenderer,
|
||||
resolve_personalized_headphone,
|
||||
)
|
||||
from rosella_bank import DEFAULT_FILTERBANK, FILTERBANK_CHOICES, FILTERBANK_NAMES
|
||||
from speaker_backend import create_speaker_renderer
|
||||
from speaker_layouts import (SPEAKER_LAYOUT_CHOICES, get_speaker_layout,
|
||||
speaker_layout_display_name)
|
||||
from speaker_wav import BinauralPcmSpool, SpeakerPcmSpool, write_pcm_wav
|
||||
from variant_error import UnsupportedVariantError, write_variant_report
|
||||
|
||||
|
||||
RATE = 48000
|
||||
FRAME_SAMPLES = 1536
|
||||
DEFAULT_OUTPUT_DIR = PROJECT_DIR / "output"
|
||||
EAC3_DRC_SCALE_MAX = 6.0
|
||||
EAC3_TARGET_LEVEL_RANGE = (-31, 0)
|
||||
EAC3_DECODER_OPTION_RE = re.compile(r"(?m)^\s*-([A-Za-z0-9_]+)\s+<")
|
||||
|
||||
|
||||
def resolve_output(source, requested=None, speaker_layout=None, *, binaural=False):
|
||||
"""解析成品路径;未指定时使用项目内的 ``output`` 目录。"""
|
||||
source = Path(source)
|
||||
if requested is not None:
|
||||
target = Path(requested)
|
||||
elif speaker_layout is not None:
|
||||
target = DEFAULT_OUTPUT_DIR / f"{source.stem}.{speaker_layout}.wav"
|
||||
elif binaural:
|
||||
target = DEFAULT_OUTPUT_DIR / f"{source.stem}.binaural.wav"
|
||||
else:
|
||||
target = DEFAULT_OUTPUT_DIR / (source.stem + ".adm.wav")
|
||||
return target.expanduser().resolve()
|
||||
|
||||
|
||||
def resolve_binaural_hrtf_input(args, *, required):
|
||||
"""解析 binaural 的输入:显式 .jochrtf,或 Rosella 个性化模型。
|
||||
|
||||
默认使用 HRTF/binaural.personalized_headphone;.jochrtf 只能显式选择。
|
||||
只校验路径,不做编译。
|
||||
"""
|
||||
compiled = args.compiled_hrtf_cache
|
||||
private = args.personalized_headphone
|
||||
|
||||
if required and compiled is None and private is None:
|
||||
if DEFAULT_PERSONALIZED_HEADPHONE.is_file():
|
||||
private = DEFAULT_PERSONALIZED_HEADPHONE
|
||||
|
||||
if compiled is None and private is None:
|
||||
if required:
|
||||
raise ValueError(
|
||||
"--binaural 未找到 HRTF 输入:默认 "
|
||||
f"{DEFAULT_PERSONALIZED_HEADPHONE} 不存在;请用 "
|
||||
"--compiled-hrtf-cache PATH 或 --personalized-headphone PATH 指定")
|
||||
return None
|
||||
|
||||
if compiled is not None:
|
||||
return {"kind": "compiled_cache", "path": resolve_compiled_hrtf_cache(compiled)}
|
||||
return {"kind": "rosella", "path": resolve_personalized_headphone(private)}
|
||||
|
||||
|
||||
def executable(value, name):
|
||||
path = shutil.which(value) if value else None
|
||||
if path is None and value and Path(value).is_file():
|
||||
path = str(Path(value).resolve())
|
||||
if path is None:
|
||||
raise FileNotFoundError(f"找不到 {name}: {value!r}")
|
||||
return path
|
||||
|
||||
|
||||
def run(command, label):
|
||||
print(f"[{label}]", flush=True)
|
||||
result = subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
|
||||
text=True, encoding="utf-8", errors="replace")
|
||||
if result.returncode:
|
||||
tail = result.stderr[-4000:]
|
||||
raise RuntimeError(f"{label} 失败(exit {result.returncode})\n{tail}")
|
||||
|
||||
|
||||
def timed_call(timings, name, function, *args, **kwargs):
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
return function(*args, **kwargs)
|
||||
finally:
|
||||
timings[name] = time.perf_counter() - started
|
||||
|
||||
|
||||
def probe_eac3_decoder_options(ffmpeg):
|
||||
"""读取 ``ffmpeg -h decoder=eac3`` 暴露的 AVOption 名。"""
|
||||
result = subprocess.run(
|
||||
[ffmpeg, "-hide_banner", "-h", "decoder=eac3"],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
text=True, encoding="utf-8", errors="replace")
|
||||
options = frozenset(EAC3_DECODER_OPTION_RE.findall(result.stdout or ""))
|
||||
# decoder 名不存在时 ffmpeg 依然返回 0,因此以“解析不到任何选项”为失败。
|
||||
if not options:
|
||||
raise RuntimeError(
|
||||
"无法读取 FFmpeg 的 eac3 解码器选项(ffmpeg -h decoder=eac3);"
|
||||
"需要带 E-AC-3 解码器的构建")
|
||||
return options
|
||||
|
||||
|
||||
def ffmpeg_version(ffmpeg):
|
||||
"""FFmpeg 版本字符串;探测失败返回空串,不影响渲染。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[ffmpeg, "-hide_banner", "-version"],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
text=True, encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
return ""
|
||||
lines = (result.stdout or "").splitlines()
|
||||
line = lines[0].strip() if lines else ""
|
||||
prefix = "ffmpeg version "
|
||||
return line[len(prefix):].strip() if line.startswith(prefix) else line
|
||||
|
||||
|
||||
def eac3_decode_options(drc_scale, target_level, available):
|
||||
"""构造 ``-i`` 之前的 E-AC-3 解码选项,返回 ``(argv, report 片段)``。"""
|
||||
if "drc_scale" not in available:
|
||||
raise RuntimeError(
|
||||
"FFmpeg 的 eac3 解码器缺少 -drc_scale,无法关闭码流 DRC")
|
||||
# -drc_scale 始终显式下发:0(全动态范围)不是 ffmpeg 的默认值。
|
||||
argv = ["-drc_scale", format(float(drc_scale), ".10g")]
|
||||
if target_level:
|
||||
if "target_level" not in available:
|
||||
raise RuntimeError(
|
||||
"FFmpeg 的 eac3 解码器不支持 -target_level;请升级 FFmpeg "
|
||||
"或去掉 --eac3-target-level")
|
||||
argv += ["-target_level", str(int(target_level))]
|
||||
applied = {"drc_scale": float(drc_scale), "target_level": int(target_level)}
|
||||
return argv, applied
|
||||
|
||||
|
||||
def extract_eac3(ffmpeg, source, target):
|
||||
if source.suffix.lower() in (".eac3", ".ec3"):
|
||||
return source
|
||||
run([ffmpeg, "-hide_banner", "-loglevel", "error", "-y", "-i", str(source),
|
||||
"-map", "0:a:0", "-vn", "-c:a", "copy", "-f", "eac3", str(target)],
|
||||
"FFmpeg 提取 E-AC-3")
|
||||
return target
|
||||
|
||||
|
||||
def decode_core(ffmpeg, eac3, target, duration_sec=None, *, options=(), channels=6):
|
||||
"""解出床 PCM。
|
||||
|
||||
``channels=6``(N=5 档位)沿用 -ac 6;N=7 档位必须传 ``channels=None``,
|
||||
让解码器保持自身的声道布局——``-ac 6`` 会把 5.1.2/7.1 下混成 5.1 并丢掉
|
||||
额外那一对,``-ac 8`` 又会把 5.1.2 重映射成 7.1,两种都会丢信息。
|
||||
"""
|
||||
command = [ffmpeg, "-hide_banner", "-loglevel", "error", "-y", *options,
|
||||
"-i", str(eac3), "-map", "0:a:0", "-vn"]
|
||||
if duration_sec is not None:
|
||||
command.extend(["-t", f"{duration_sec:.9f}"])
|
||||
if channels is not None:
|
||||
command.extend(["-ac", str(int(channels))])
|
||||
command.extend(["-ar", str(RATE),
|
||||
"-c:a", "pcm_f32le", "-f", "f32le", str(target)])
|
||||
run(command, f"FFmpeg 解码核心 {channels or '码流默认'} 通道 PCM")
|
||||
return target
|
||||
|
||||
|
||||
def sha256(path):
|
||||
digest = hashlib.sha256()
|
||||
with Path(path).open("rb") as fp:
|
||||
for block in iter(lambda: fp.read(16 << 20), b""):
|
||||
digest.update(block)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def choose_pcm_output_format(requested_format, clip_action, peak, clipped_values,
|
||||
*, input_func=input, interactive=None):
|
||||
"""Resolve int24 clipping interactively or through an explicit policy."""
|
||||
if requested_format != "int24" or clipped_values == 0:
|
||||
return requested_format
|
||||
print(
|
||||
f"[clip] int24 将发生削波:peak={peak:.9g},超出 [-1,1] 的样本值={clipped_values}",
|
||||
file=sys.stderr, flush=True)
|
||||
action = clip_action
|
||||
if action == "ask":
|
||||
if interactive is None:
|
||||
interactive = bool(getattr(sys.stdin, "isatty", lambda: False)())
|
||||
if not interactive:
|
||||
raise RuntimeError(
|
||||
"检测到 int24 削波,但当前不是交互终端;请使用 "
|
||||
"--clip-action continue、--clip-action float32 或 --clip-action abort")
|
||||
while True:
|
||||
answer = input_func(
|
||||
"继续写 int24 并截断 [i] / 改为 float32 [f,默认] / 取消 [a]:"
|
||||
).strip().lower()
|
||||
if answer in ("", "f", "float", "float32"):
|
||||
action = "float32"
|
||||
break
|
||||
if answer in ("i", "int", "int24", "c", "continue"):
|
||||
action = "continue"
|
||||
break
|
||||
if answer in ("a", "abort", "q", "quit", "n", "no"):
|
||||
action = "abort"
|
||||
break
|
||||
print("请输入 i、f 或 a。", file=sys.stderr, flush=True)
|
||||
if action == "continue":
|
||||
print("[clip] 将继续写 int24,超范围值会截断到 [-1,1]。", flush=True)
|
||||
return "int24"
|
||||
if action == "float32":
|
||||
print("[clip] 已切换为 float32 WAV,不执行截断。", flush=True)
|
||||
return "float32"
|
||||
if action == "abort":
|
||||
raise RuntimeError("用户因 int24 削波取消输出")
|
||||
raise ValueError(f"未知 clip action: {action}")
|
||||
|
||||
|
||||
# Backward-compatible public name used by existing tests and callers.
|
||||
choose_speaker_output_format = choose_pcm_output_format
|
||||
|
||||
|
||||
def resolve_metadata(args, eac3, temp_dir):
|
||||
if args.metadata_dir:
|
||||
directory = Path(args.metadata_dir).resolve()
|
||||
return PayloadIndex(directory), "sidecar", directory
|
||||
if args.metadata_backend == "sidecar":
|
||||
raise ValueError("metadata-backend=sidecar 时必须提供 --metadata-dir")
|
||||
cache_dir = (args.metadata_cache.expanduser().resolve()
|
||||
if args.metadata_cache else None)
|
||||
max_frames = (math.ceil(args.duration * RATE / FRAME_SAMPLES)
|
||||
if args.duration is not None else None)
|
||||
index = DirectPayloadIndex.from_eac3(
|
||||
eac3, max_frames=max_frames, cache_dir=cache_dir)
|
||||
return index, "python-emdf-memory", cache_dir
|
||||
|
||||
|
||||
def variant_call(output, source, function, *args, **kwargs):
|
||||
"""执行一个阶段;遇到未知变体时在目标文件旁写结构化报告。"""
|
||||
try:
|
||||
return function(*args, **kwargs)
|
||||
except UnsupportedVariantError as exc:
|
||||
report_path = Path(str(output) + ".variant-error.json")
|
||||
write_variant_report(report_path, exc, input_path=source, output_path=output)
|
||||
print(f"[VARIANT] {exc}", file=sys.stderr, flush=True)
|
||||
print(f"[VARIANT] 维修报告: {report_path}", file=sys.stderr, flush=True)
|
||||
raise
|
||||
|
||||
|
||||
def create_renderer(gain):
|
||||
"""整帧 DSP 后端:Python/numpy。"""
|
||||
decoder = JocRenderer(output_scale=gain)
|
||||
info = {"name": "python", "library": None, "build": None, "threads": None}
|
||||
print("[backend] python/numpy", flush=True)
|
||||
return decoder, info
|
||||
|
||||
|
||||
def bed_channel_count(index):
|
||||
"""床 PCM 的通道数:N=5 档位沿用 6 通道,N=7 档位保留解码器自身的 8 通道。"""
|
||||
return 6 if (getattr(index, "n_channels", None) or 5) == 5 else 8
|
||||
|
||||
|
||||
def bed_layout_plan(index):
|
||||
"""按通道码把床 PCM 摆进 JOC 输入序(规格 §3、§10.4)。
|
||||
|
||||
返回 {"channels", "joc_indices", "lfe_index"}。N=7 必须同时有核心子流与依赖
|
||||
子流的 chanmap 才能确定额外那一对;缺任何一侧都报变体,不猜一个顺序。
|
||||
"""
|
||||
config_idx = getattr(index, "config_idx", None)
|
||||
core_channels = getattr(index, "n_channels", None) or 5
|
||||
if core_channels == 5:
|
||||
# 5.1(side) 的 f32le 顺序为 FL FR FC LFE SL SR。
|
||||
return {"channels": 6, "joc_indices": [0, 1, 2, 4, 5], "lfe_index": 3,
|
||||
"extra_pair_slots": list(BED_EXTRA_SLOTS_BY_CONFIG.get(config_idx, ()))}
|
||||
transport = None
|
||||
for entry in getattr(index, "transports", None) or ():
|
||||
if entry["core_locations"] and entry["chanmap"] is not None:
|
||||
transport = entry
|
||||
break
|
||||
if transport is None:
|
||||
raise UnsupportedVariantError(
|
||||
"eac3", "core_extension_pair",
|
||||
"N=7 档位需要核心子流与依赖子流 chanmap 才能确定床通道顺序",
|
||||
details={"config_idx": getattr(index, "config_idx", None),
|
||||
"n_channels": core_channels})
|
||||
core = list(transport["core_locations"])
|
||||
locations = core + (["LFE"] if transport["core_lfe"] else [])
|
||||
locations += expand_chanmap(transport["chanmap"])
|
||||
pair = extra_pair(core, transport["chanmap"])
|
||||
order = bed_order(locations)
|
||||
indices = joc_input_indices(locations, pair)
|
||||
if order is None or indices is None:
|
||||
raise UnsupportedVariantError(
|
||||
"eac3", "bed_channel_mapping_unsupported",
|
||||
"床的声道位置无法换算成 JOC 输入序",
|
||||
details={"config_idx": getattr(index, "config_idx", None),
|
||||
"locations": locations, "extra_pair": list(pair)})
|
||||
lfe_index = None
|
||||
for index, (_position, name) in enumerate(order):
|
||||
if name == "LFE":
|
||||
lfe_index = index
|
||||
break
|
||||
return {"channels": len(order), "joc_indices": indices, "lfe_index": lfe_index,
|
||||
"extra_pair_slots": list(BED_EXTRA_SLOTS_BY_CONFIG.get(config_idx, ()))}
|
||||
|
||||
|
||||
def render(index, bed_path, frame_count, raw_path, gain, progress_every,
|
||||
frame_sink=None,
|
||||
speaker_renderer=None, speaker_sink=None, speaker_metadata_offset=1473,
|
||||
binaural_renderer=None, binaural_sink=None, binaural_metadata_offset=1473,
|
||||
raw_scale=1.0):
|
||||
plan = bed_layout_plan(index)
|
||||
bed_channels = plan["channels"]
|
||||
values = np.memmap(bed_path, dtype=np.float32, mode="r")
|
||||
frame_width = FRAME_SAMPLES * bed_channels
|
||||
if values.size % frame_width:
|
||||
raise ValueError(
|
||||
f"FFmpeg PCM 长度不是 1536×{bed_channels} 的整数倍: {values.size}")
|
||||
bed = values.reshape(-1, FRAME_SAMPLES, bed_channels)
|
||||
if len(bed) < frame_count:
|
||||
raise ValueError(f"PCM 只有 {len(bed)} 帧,元数据需要 {frame_count} 帧")
|
||||
output = (np.memmap(raw_path, dtype=np.float32, mode="w+",
|
||||
shape=(frame_count, FRAME_SAMPLES, 16))
|
||||
if raw_path is not None else None)
|
||||
decoder, backend_info = create_renderer(gain)
|
||||
started = time.perf_counter()
|
||||
dsp_seconds = 0.0
|
||||
adm_stream_seconds = 0.0
|
||||
raw_write_seconds = 0.0
|
||||
speaker_render_seconds = 0.0
|
||||
speaker_write_seconds = 0.0
|
||||
binaural_render_seconds = 0.0
|
||||
binaural_write_seconds = 0.0
|
||||
elapsed = 0.0
|
||||
try:
|
||||
for frame_number, row in enumerate(index.rows[:frame_count]):
|
||||
frame = np.asarray(bed[frame_number], dtype=np.float32)
|
||||
core = frame[:, plan["joc_indices"]].T
|
||||
lfe = (np.zeros(FRAME_SAMPLES, dtype=np.float32)
|
||||
if plan["lfe_index"] is None else frame[:, plan["lfe_index"]])
|
||||
subs = index.subpayloads(row)
|
||||
stage = time.perf_counter()
|
||||
if subs is None:
|
||||
pcm16, _ = decoder.render_missing_frame(
|
||||
core, lfe, config_idx=getattr(index, "config_idx", None) or 3)
|
||||
else:
|
||||
pcm16, _ = decoder.render_subpayloads(subs, core, lfe)
|
||||
dsp_seconds += time.perf_counter() - stage
|
||||
if output is not None:
|
||||
stage = time.perf_counter()
|
||||
output[frame_number] = np.multiply(
|
||||
pcm16.T, np.float32(raw_scale), dtype=np.float32)
|
||||
raw_write_seconds += time.perf_counter() - stage
|
||||
if frame_sink is not None:
|
||||
stage = time.perf_counter()
|
||||
frame_sink.write_frame(pcm16)
|
||||
adm_stream_seconds += time.perf_counter() - stage
|
||||
if speaker_renderer is not None:
|
||||
stage = time.perf_counter()
|
||||
speaker_pcm = speaker_renderer.render_frame(
|
||||
pcm16.T, subs.get(11) if subs is not None else None, speaker_metadata_offset)
|
||||
speaker_render_seconds += time.perf_counter() - stage
|
||||
stage = time.perf_counter()
|
||||
speaker_sink.write_frame(speaker_pcm)
|
||||
speaker_write_seconds += time.perf_counter() - stage
|
||||
if binaural_renderer is not None:
|
||||
payload = subs.get(11) if subs is not None else None
|
||||
outer_offset = (
|
||||
index.subpayload_sample_offset(row, 11)
|
||||
if payload is not None and hasattr(index, "subpayload_sample_offset")
|
||||
else 0
|
||||
)
|
||||
stage = time.perf_counter()
|
||||
binaural_pcm = binaural_renderer.render_frame(
|
||||
pcm16.T, payload, binaural_metadata_offset,
|
||||
outer_sample_offset=outer_offset)
|
||||
binaural_render_seconds += time.perf_counter() - stage
|
||||
if len(binaural_pcm):
|
||||
stage = time.perf_counter()
|
||||
binaural_sink.write_frame(binaural_pcm)
|
||||
binaural_write_seconds += time.perf_counter() - stage
|
||||
done = frame_number + 1
|
||||
if done % progress_every == 0 or done == frame_count:
|
||||
elapsed = time.perf_counter() - started
|
||||
speed = done / max(elapsed, 1e-9)
|
||||
eta = (frame_count - done) / max(speed, 1e-9)
|
||||
print(f"[JOC:{backend_info['name']}] {done}/{frame_count} "
|
||||
f"{speed:.1f} frame/s ETA {eta:.1f}s", flush=True)
|
||||
if binaural_renderer is not None:
|
||||
stage = time.perf_counter()
|
||||
binaural_tail = binaural_renderer.finish()
|
||||
binaural_render_seconds += time.perf_counter() - stage
|
||||
if len(binaural_tail):
|
||||
stage = time.perf_counter()
|
||||
binaural_sink.write_frame(binaural_tail)
|
||||
binaural_write_seconds += time.perf_counter() - stage
|
||||
if output is not None:
|
||||
output.flush()
|
||||
elapsed = time.perf_counter() - started
|
||||
finally:
|
||||
close = getattr(decoder, "close", None)
|
||||
if close is not None:
|
||||
close()
|
||||
close = getattr(speaker_renderer, "close", None)
|
||||
if close is not None:
|
||||
close()
|
||||
close = getattr(binaural_renderer, "close", None)
|
||||
if close is not None:
|
||||
close()
|
||||
breakdown = {
|
||||
"pipeline_wall_seconds": elapsed,
|
||||
"dsp_and_joc_parse_seconds": dsp_seconds,
|
||||
"adm_stream_write_seconds": adm_stream_seconds,
|
||||
"raw_float_write_seconds": raw_write_seconds,
|
||||
"speaker_render_seconds": speaker_render_seconds,
|
||||
"speaker_spool_write_seconds": speaker_write_seconds,
|
||||
"binaural_render_seconds": binaural_render_seconds,
|
||||
"binaural_spool_write_seconds": binaural_write_seconds,
|
||||
}
|
||||
return dsp_seconds, backend_info, breakdown
|
||||
|
||||
|
||||
def _layout_choice(value: str) -> str:
|
||||
"""Canonical layout name for a --speaker-layout value."""
|
||||
return speaker_layout_display_name(get_speaker_layout(value))
|
||||
|
||||
|
||||
def build_parser():
|
||||
parser = argparse.ArgumentParser(
|
||||
description=("JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、"
|
||||
"扬声器 WAV 或双耳 WAV"))
|
||||
parser.add_argument("input", type=Path, help="输入 .m4a/.eac3/.ec3")
|
||||
parser.add_argument("-o", "--output", type=Path, help="输出文件;默认按模式和布局命名")
|
||||
parser.add_argument("--speaker-output", type=Path,
|
||||
help="扬声器 WAV 路径;仅与 --speaker-layout 一起使用")
|
||||
parser.add_argument("--binaural-output", type=Path,
|
||||
help="双耳 WAV 路径;仅与 --binaural 一起使用")
|
||||
direct_mode = parser.add_mutually_exclusive_group()
|
||||
direct_mode.add_argument("--speaker-layout", type=_layout_choice,
|
||||
choices=SPEAKER_LAYOUT_CHOICES,
|
||||
help="直接扬声器渲染布局,例如 2.0、4.0、5.1、7.1.2")
|
||||
direct_mode.add_argument("--binaural", action="store_true",
|
||||
help="直接双耳渲染;不生成临时 ADM BWF")
|
||||
parser.add_argument("--speaker-format", choices=("float32", "int24"), default="float32",
|
||||
help="扬声器 WAV 格式,默认 float32")
|
||||
parser.add_argument("--binaural-format", choices=("float32", "int24"), default="float32",
|
||||
help="双耳 WAV 格式,默认 float32")
|
||||
parser.add_argument("--clip-action", choices=("ask", "continue", "float32", "abort"),
|
||||
default="ask",
|
||||
help="int24 削波处理:交互询问、继续截断、改 float32 或中止")
|
||||
parser.add_argument("--speaker-metadata-offset", type=int, default=1473,
|
||||
help="扬声器渲染 metadata 相对帧偏移,默认 1473 samples")
|
||||
parser.add_argument("--binaural-mode", choices=("off", "near", "mid", "far"),
|
||||
default=None,
|
||||
help="双耳参考距离档位或ADM DBMD提示,默认mid;支持near/mid/far。"
|
||||
"off仅用于ADM BWF关闭DBMD双耳提示")
|
||||
hrtf_input = parser.add_mutually_exclusive_group()
|
||||
hrtf_input.add_argument(
|
||||
"--compiled-hrtf-cache", type=Path,
|
||||
help="显式读取 JOC .jochrtf compiled cache(保留给高精度离线渲染)")
|
||||
hrtf_input.add_argument(
|
||||
"--personalized-headphone", type=Path, nargs="?",
|
||||
const=DEFAULT_PERSONALIZED_HEADPHONE,
|
||||
help="Rosella .personalized_headphone 模型;不带路径时默认 "
|
||||
"HRTF/binaural.personalized_headphone")
|
||||
parser.add_argument(
|
||||
"--rosella-filterbank", type=int, choices=FILTERBANK_CHOICES, default=None,
|
||||
help="Rosella 模型的滤波器组:1=13 taps/6 slots(默认),"
|
||||
"2=11 taps/3 slots;仅作用于个性化模型")
|
||||
parser.add_argument("--jochrtf-room", choices=("on", "off"), default=None,
|
||||
help="压缩输入的项目房间:默认off;on 启用镜像早反射与FDN晚期")
|
||||
parser.add_argument("--binaural-tail-seconds", type=float, default=5.0,
|
||||
help="双耳 FIR/filterbank 排空上限,默认 5 秒")
|
||||
parser.add_argument("--binaural-tail-threshold", type=float, default=1.0e-8,
|
||||
help="双耳尾声裁切阈值,默认 1e-8;主体至少保留原时长")
|
||||
parser.add_argument("--binaural-chunk-frames", type=int, default=64,
|
||||
help="双耳内部批处理 E-AC-3 帧数,默认 64")
|
||||
parser.add_argument("--gain-db", type=float, default=0.0,
|
||||
help="成品增益 dB,默认 0;双耳路径以 float64 应用")
|
||||
parser.add_argument("--duration", type=float, help="只处理开头指定秒数")
|
||||
parser.add_argument("--object-delay-samples", type=int, default=1473,
|
||||
help="对象 PCM/OAMD 时间补偿;ADM 与双耳默认 1473 samples")
|
||||
parser.add_argument("--trajectory-mode", choices=("compact", "dense64"), default="compact",
|
||||
help="ADM 对象轨迹表示;直接双耳路径不序列化 AXML")
|
||||
parser.add_argument("--ffmpeg", default=os.environ.get("FFMPEG", "ffmpeg"))
|
||||
parser.add_argument("--eac3-drc-scale", type=float, default=0.0,
|
||||
help="E-AC-3 解码器 -drc_scale:0=关闭码流 dynrng(全动态范围),"
|
||||
"1=码流作者意图,>1 非对称;默认 0")
|
||||
parser.add_argument("--eac3-target-level", type=int, default=0,
|
||||
help="E-AC-3 解码器 -target_level:按码流 dialnorm 归一化电平,"
|
||||
"增益约 target_level - dialnorm dB;0=不施加,默认 0")
|
||||
|
||||
metadata_source = parser.add_mutually_exclusive_group()
|
||||
metadata_source.add_argument("--metadata-dir", type=Path,
|
||||
help="含 frames.csv 和 emdf/ 或 payloads/ 的元数据 sidecar")
|
||||
metadata_source.add_argument("--metadata-cache", type=Path,
|
||||
help="把直接 EMDF 扫描或兼容桥结果持久保存到此目录")
|
||||
parser.add_argument("--metadata-backend", choices=("auto", "emdf", "sidecar"),
|
||||
default="auto", help="直接扫描连续 EMDF,或读取现有 sidecar")
|
||||
parser.add_argument("--print-metadata", choices=("none", "summary", "frames"), default="none",
|
||||
help="诊断元数据输出;默认 none,避免转换前重复完整解析")
|
||||
parser.add_argument("--metadata-json", type=Path, help="元数据汇总 JSON 路径")
|
||||
parser.add_argument("--metadata-only", action="store_true", help="解析/打印元数据后退出")
|
||||
parser.add_argument("--keep-raw", action="store_true", help="额外保留 16ch f32le 对象中间文件")
|
||||
parser.add_argument("--skip-sha256", action="store_true",
|
||||
help="跳过最终文件 SHA-256 全量复扫以缩短大文件处理时间")
|
||||
parser.add_argument("--progress-every", type=int, default=500)
|
||||
return parser
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
# Windows 控制台的活动代码页未必能表示日文文件名;保留信息并避免
|
||||
# UnicodeEncodeError 中断长任务。支持 UTF-8 的终端仍会原样显示。
|
||||
for stream in (sys.stdout, sys.stderr):
|
||||
if hasattr(stream, "reconfigure"):
|
||||
stream.reconfigure(encoding="utf-8", errors="backslashreplace")
|
||||
args = build_parser().parse_args(argv)
|
||||
args.binaural_mode = args.binaural_mode or "mid"
|
||||
source = args.input.expanduser().resolve()
|
||||
if not source.is_file():
|
||||
raise FileNotFoundError(source)
|
||||
speaker_mode = args.speaker_layout is not None
|
||||
binaural_mode = bool(args.binaural)
|
||||
binaural_render_mode = args.binaural_mode
|
||||
if binaural_mode and binaural_render_mode == "off":
|
||||
raise ValueError(
|
||||
"--binaural-mode off 仅用于 ADM BWF 输出(关闭 DBMD 双耳提示);"
|
||||
"直接双耳渲染可使用near/mid/far")
|
||||
if args.speaker_output is not None and not speaker_mode:
|
||||
raise ValueError("--speaker-output 必须与 --speaker-layout 一起使用")
|
||||
if args.binaural_output is not None and not binaural_mode:
|
||||
raise ValueError("--binaural-output 必须与 --binaural 一起使用")
|
||||
specific_outputs = [value for value in (args.speaker_output, args.binaural_output)
|
||||
if value is not None]
|
||||
if args.output is not None and specific_outputs:
|
||||
raise ValueError("-o/--output 与 --speaker-output/--binaural-output 不能同时使用")
|
||||
if len(specific_outputs) > 1:
|
||||
raise ValueError("--speaker-output 与 --binaural-output 不能同时使用")
|
||||
if args.speaker_metadata_offset < 0:
|
||||
raise ValueError("speaker-metadata-offset 不能为负数")
|
||||
hrtf_options_used = any((
|
||||
args.compiled_hrtf_cache is not None,
|
||||
args.personalized_headphone is not None,
|
||||
args.jochrtf_room is not None,
|
||||
))
|
||||
if hrtf_options_used and not binaural_mode:
|
||||
raise ValueError("HRTF/房间选项仅与 --binaural 一起使用")
|
||||
if args.rosella_filterbank is not None and not binaural_mode:
|
||||
raise ValueError("--rosella-filterbank 仅与 --binaural 一起使用")
|
||||
rosella_filterbank = (DEFAULT_FILTERBANK if args.rosella_filterbank is None
|
||||
else args.rosella_filterbank)
|
||||
if (not math.isfinite(args.binaural_tail_seconds)
|
||||
or args.binaural_tail_seconds < 0):
|
||||
raise ValueError("binaural-tail-seconds 必须是非负有限值")
|
||||
if (not math.isfinite(args.binaural_tail_threshold)
|
||||
or args.binaural_tail_threshold < 0):
|
||||
raise ValueError("binaural-tail-threshold 必须是非负有限值")
|
||||
if args.binaural_chunk_frames <= 0:
|
||||
raise ValueError("binaural-chunk-frames 必须大于 0")
|
||||
requested_output = (args.speaker_output if args.speaker_output is not None
|
||||
else args.binaural_output if args.binaural_output is not None
|
||||
else args.output)
|
||||
output = resolve_output(
|
||||
source, requested_output, args.speaker_layout if speaker_mode else None,
|
||||
binaural=binaural_mode)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
if args.duration is not None and args.duration <= 0:
|
||||
raise ValueError("duration 必须大于 0")
|
||||
if args.object_delay_samples < 0:
|
||||
raise ValueError("object-delay-samples 不能为负数")
|
||||
gain_float64 = 10.0 ** (args.gain_db / 20.0)
|
||||
gain = np.float32(gain_float64)
|
||||
if not math.isfinite(gain_float64) or not np.isfinite(gain):
|
||||
raise ValueError("gain-db 超出支持范围")
|
||||
if (not math.isfinite(args.eac3_drc_scale)
|
||||
or not 0.0 <= args.eac3_drc_scale <= EAC3_DRC_SCALE_MAX):
|
||||
raise ValueError(f"eac3-drc-scale 必须在 0..{EAC3_DRC_SCALE_MAX:g} 之间")
|
||||
if not (EAC3_TARGET_LEVEL_RANGE[0] <= args.eac3_target_level
|
||||
<= EAC3_TARGET_LEVEL_RANGE[1]):
|
||||
raise ValueError("eac3-target-level 必须在 -31..0 之间")
|
||||
binaural_hrtf_input = resolve_binaural_hrtf_input(
|
||||
args, required=binaural_mode and not args.metadata_only)
|
||||
if args.jochrtf_room is not None and (
|
||||
binaural_hrtf_input is None or binaural_hrtf_input["kind"] == "rosella"):
|
||||
raise ValueError("--jochrtf-room 仅用于压缩输入;个性化模型使用自身房间处理")
|
||||
if (args.rosella_filterbank is not None and binaural_hrtf_input is not None
|
||||
and binaural_hrtf_input["kind"] != "rosella"):
|
||||
raise ValueError(
|
||||
"--rosella-filterbank 只作用于 Rosella .personalized_headphone 输入;"
|
||||
"当前压缩路径不使用该滤波器组")
|
||||
ffmpeg = executable(args.ffmpeg, "FFmpeg")
|
||||
decode_options = ()
|
||||
decode_option_info = None
|
||||
if not args.metadata_only:
|
||||
available = probe_eac3_decoder_options(ffmpeg)
|
||||
decode_options, applied = eac3_decode_options(
|
||||
args.eac3_drc_scale, args.eac3_target_level, available)
|
||||
decode_option_info = {
|
||||
"version": ffmpeg_version(ffmpeg),
|
||||
"eac3_decode_options": applied,
|
||||
}
|
||||
print(f"[decode] ffmpeg {decode_option_info['version']} "
|
||||
f"drc_scale={args.eac3_drc_scale:g} "
|
||||
f"target_level={args.eac3_target_level}", flush=True)
|
||||
|
||||
total_started = time.perf_counter()
|
||||
timings = {}
|
||||
with tempfile.TemporaryDirectory(prefix="eac3joc-", dir=output.parent) as temporary:
|
||||
temp_dir = Path(temporary)
|
||||
eac3 = timed_call(timings, "extract_eac3", extract_eac3,
|
||||
ffmpeg, source, temp_dir / "input.eac3")
|
||||
index, metadata_backend, metadata_cache_dir = timed_call(
|
||||
timings, "resolve_metadata", variant_call,
|
||||
output, source, resolve_metadata, args, eac3, temp_dir)
|
||||
timings["load_metadata_index"] = 0.0
|
||||
frame_count = len(index)
|
||||
if args.duration is not None:
|
||||
frame_count = min(frame_count, math.ceil(args.duration * RATE / FRAME_SAMPLES))
|
||||
duration_sec = frame_count * FRAME_SAMPLES / RATE
|
||||
need_metadata_summary = (
|
||||
args.metadata_only or args.metadata_json is not None or args.print_metadata != "none")
|
||||
if need_metadata_summary:
|
||||
metadata_json = (args.metadata_json or Path(str(output) + ".metadata.json")).resolve()
|
||||
summary = timed_call(
|
||||
timings, "metadata_summary", variant_call,
|
||||
output, source, write_summary, index, metadata_json, limit=frame_count,
|
||||
print_frames=args.print_metadata == "frames")
|
||||
if args.print_metadata == "summary":
|
||||
print("[metadata] " + json.dumps(summary, ensure_ascii=False, separators=(",", ":")))
|
||||
print(f"[metadata] backend={metadata_backend} frames={frame_count} -> {metadata_json}")
|
||||
else:
|
||||
metadata_json = None
|
||||
timings["metadata_summary"] = 0.0
|
||||
print(f"[metadata] backend={metadata_backend} frames={frame_count} summary=skipped")
|
||||
if args.metadata_only:
|
||||
return 0
|
||||
|
||||
for entry in getattr(index, "diagnostics", None) or ():
|
||||
print("[diagnostics] "
|
||||
+ json.dumps(entry, ensure_ascii=False, separators=(",", ":")),
|
||||
file=sys.stderr, flush=True)
|
||||
core_channels = getattr(index, "n_channels", None)
|
||||
bed_layout_info = getattr(index, "bed_layout", None) or {}
|
||||
if core_channels is not None:
|
||||
print(f"[joc] downmix_config_idx={index.config_idx} "
|
||||
f"core_channels={core_channels} "
|
||||
f"bed_channels={bed_channel_count(index)} "
|
||||
f"extra_pair_slots={bed_layout_plan(index)['extra_pair_slots']} "
|
||||
f"bed_layout={bed_layout_info.get('name') or '-'} "
|
||||
f"bed_object_channels={len(index.bed_channels)}",
|
||||
flush=True)
|
||||
bed_path = timed_call(
|
||||
timings, "decode_core", decode_core,
|
||||
ffmpeg, eac3, temp_dir / "core51_f32le.raw", duration_sec,
|
||||
options=decode_options,
|
||||
channels=(6 if core_channels in (None, 5) else None))
|
||||
raw_path = (output.with_name(output.name + ".objects16.f32le")
|
||||
if args.keep_raw else None)
|
||||
master = None
|
||||
speaker_backend_info = None
|
||||
speaker_wav_info = None
|
||||
speaker_clip_info = None
|
||||
speaker_actual_format = None
|
||||
binaural_backend_info = None
|
||||
binaural_hrtf_report = None
|
||||
binaural_wav_info = None
|
||||
binaural_clip_info = None
|
||||
binaural_actual_format = None
|
||||
if speaker_mode:
|
||||
timings["create_binaural_renderer"] = 0.0
|
||||
layout = get_speaker_layout(args.speaker_layout)
|
||||
speaker_name = speaker_layout_display_name(layout)
|
||||
speaker_decoder, speaker_backend_info = create_speaker_renderer(
|
||||
layout, bed_channels=getattr(index, "bed_channels", ()))
|
||||
print(f"[speaker] layout={speaker_name} backend={speaker_backend_info['name']} "
|
||||
f"channels={layout.channel_count}", flush=True)
|
||||
spool = SpeakerPcmSpool(
|
||||
temp_dir / "speaker_interleaved_f32.raw",
|
||||
frame_count * FRAME_SAMPLES, layout.channel_count)
|
||||
try:
|
||||
render_seconds, renderer_backend, render_breakdown = timed_call(
|
||||
timings, "render_and_stream", variant_call,
|
||||
output, source, render, index, bed_path, frame_count, raw_path, gain,
|
||||
max(1, args.progress_every), None, speaker_decoder, spool,
|
||||
args.speaker_metadata_offset)
|
||||
spool.finalize()
|
||||
speaker_actual_format = choose_pcm_output_format(
|
||||
args.speaker_format, args.clip_action, spool.peak,
|
||||
spool.clipped_values)
|
||||
speaker_wav_info = timed_call(
|
||||
timings, "write_speaker_wav", write_pcm_wav,
|
||||
output, spool.values, speaker_actual_format, rate=RATE)
|
||||
speaker_clip_info = {
|
||||
"peak": spool.peak,
|
||||
"over_unity_values": spool.clipped_values,
|
||||
"requested_format": args.speaker_format,
|
||||
"actual_format": speaker_actual_format,
|
||||
"clip_action": args.clip_action,
|
||||
}
|
||||
finally:
|
||||
spool.close()
|
||||
timings["build_adm_tracks"] = 0.0
|
||||
timings["finalize_adm"] = 0.0
|
||||
timings["validate_adm"] = 0.0
|
||||
info = (f"speaker layout={speaker_name}, format={speaker_actual_format}, "
|
||||
f"peak={speaker_clip_info['peak']:.9g}")
|
||||
elif binaural_mode:
|
||||
hrtf_source = binaural_hrtf_input
|
||||
common_options = {
|
||||
"mode": binaural_render_mode,
|
||||
"object_delay_samples": args.object_delay_samples,
|
||||
"tail_seconds": args.binaural_tail_seconds,
|
||||
"output_gain": gain_float64,
|
||||
"chunk_frames": args.binaural_chunk_frames,
|
||||
"bed_channels": getattr(index, "bed_channels", ()),
|
||||
}
|
||||
common_options["room"] = args.jochrtf_room == "on"
|
||||
if hrtf_source["kind"] == "rosella":
|
||||
binaural_decoder = timed_call(
|
||||
timings, "create_binaural_renderer",
|
||||
RosellaBinauralRenderer,
|
||||
hrtf_source["path"],
|
||||
bed_channels=getattr(index, "bed_channels", ()),
|
||||
mode=binaural_render_mode,
|
||||
filterbank=rosella_filterbank,
|
||||
object_delay_samples=args.object_delay_samples,
|
||||
tail_seconds=args.binaural_tail_seconds,
|
||||
output_gain=gain_float64,
|
||||
chunk_frames=args.binaural_chunk_frames)
|
||||
else:
|
||||
binaural_decoder = timed_call(
|
||||
timings, "create_binaural_renderer",
|
||||
JochrtfBinauralRenderer.from_compiled_cache,
|
||||
hrtf_source["path"],
|
||||
**common_options)
|
||||
print(
|
||||
f"[binaural] mode={binaural_render_mode} "
|
||||
f"backend={binaural_decoder.dsp_backend} "
|
||||
f"precision=float64/complex128 "
|
||||
f"hrtf={hrtf_source['kind']}:{hrtf_source['path']}", flush=True)
|
||||
if hrtf_source["kind"] != "rosella":
|
||||
room = getattr(binaural_decoder.backend, "room_enabled", None)
|
||||
if room is not None:
|
||||
print(f"[binaural] room={'on' if room else 'off'}", flush=True)
|
||||
if hrtf_source["kind"] == "rosella":
|
||||
print(
|
||||
f"[binaural] filterbank={FILTERBANK_NAMES[binaural_decoder.filterbank]}"
|
||||
f" latency={binaural_decoder.latency_samples}", flush=True)
|
||||
flush_samples = math.ceil(
|
||||
(args.binaural_tail_seconds * RATE
|
||||
+ binaural_decoder.latency_samples + ROSSELLA_BLOCK_SAMPLES)
|
||||
/ ROSSELLA_BLOCK_SAMPLES) * ROSSELLA_BLOCK_SAMPLES
|
||||
spool_capacity = frame_count * FRAME_SAMPLES + flush_samples
|
||||
else:
|
||||
spool_capacity = (
|
||||
frame_count * FRAME_SAMPLES
|
||||
+ binaural_decoder.finish_capacity_samples)
|
||||
spool = BinauralPcmSpool(
|
||||
temp_dir / "binaural_interleaved_f64.raw",
|
||||
spool_capacity,
|
||||
tail_threshold=args.binaural_tail_threshold)
|
||||
try:
|
||||
render_seconds, renderer_backend, render_breakdown = timed_call(
|
||||
timings, "render_and_stream", variant_call,
|
||||
output, source, render, index, bed_path, frame_count, raw_path,
|
||||
np.float32(1.0), max(1, args.progress_every),
|
||||
binaural_renderer=binaural_decoder, binaural_sink=spool,
|
||||
binaural_metadata_offset=args.object_delay_samples, raw_scale=gain)
|
||||
spool.finalize(minimum_samples=frame_count * FRAME_SAMPLES)
|
||||
binaural_actual_format = choose_pcm_output_format(
|
||||
args.binaural_format, args.clip_action, spool.peak,
|
||||
spool.clipped_values)
|
||||
binaural_wav_info = timed_call(
|
||||
timings, "write_binaural_wav", write_pcm_wav,
|
||||
output, spool.values, binaural_actual_format, rate=RATE)
|
||||
binaural_clip_info = {
|
||||
"peak": spool.peak,
|
||||
"over_unity_values": spool.clipped_values,
|
||||
"requested_format": args.binaural_format,
|
||||
"actual_format": binaural_actual_format,
|
||||
"clip_action": args.clip_action,
|
||||
"tail_threshold": args.binaural_tail_threshold,
|
||||
"source_samples": frame_count * FRAME_SAMPLES,
|
||||
"kept_samples": spool.sample_count,
|
||||
}
|
||||
binaural_backend_info = binaural_decoder.backend_info
|
||||
if hrtf_source["kind"] == "rosella":
|
||||
binaural_hrtf_report = {
|
||||
"input_kind": "rosella",
|
||||
"input_path": str(binaural_decoder.model_path.resolve()),
|
||||
"model_coefficient_sha256": (
|
||||
binaural_decoder.model.coefficient_sha256),
|
||||
"cache_policy": None,
|
||||
}
|
||||
else:
|
||||
binaural_hrtf_report = {
|
||||
"input_kind": binaural_backend_info["hrtf_input_kind"],
|
||||
"input_path": binaural_backend_info["hrtf_input_path"],
|
||||
"source_sha256": (
|
||||
binaural_backend_info["field"]["source_sha256"]),
|
||||
"cache_policy": binaural_backend_info["cache_policy"],
|
||||
"cache_key": binaural_backend_info["field"].get("cache_key"),
|
||||
"format_version": binaural_backend_info["field"].get("format_version"),
|
||||
"representation": binaural_backend_info.get("direct_representation"),
|
||||
"reference": binaural_backend_info.get("reference"),
|
||||
}
|
||||
finally:
|
||||
spool.close()
|
||||
timings["build_adm_tracks"] = 0.0
|
||||
timings["finalize_adm"] = 0.0
|
||||
timings["validate_adm"] = 0.0
|
||||
info = (f"binaural mode={binaural_render_mode}, "
|
||||
f"format={binaural_actual_format}, "
|
||||
f"peak={binaural_clip_info['peak']:.9g}, "
|
||||
f"samples={binaural_clip_info['kept_samples']}")
|
||||
else:
|
||||
timings["create_binaural_renderer"] = 0.0
|
||||
master = adm_assemble.StreamingMaster(
|
||||
output, duration_sec, rate=RATE,
|
||||
joc_binaural_mode=adm_atmos.JOC_BINAURAL_MODES[args.binaural_mode])
|
||||
try:
|
||||
render_seconds, renderer_backend, render_breakdown = timed_call(
|
||||
timings, "render_and_stream", variant_call,
|
||||
output, source, render, index, bed_path, frame_count, raw_path, gain,
|
||||
max(1, args.progress_every), master)
|
||||
tracks = timed_call(
|
||||
timings, "build_adm_tracks", variant_call,
|
||||
output, source, oamd_tracks.build_adm_tracks,
|
||||
index, index.rows[:frame_count], rate=RATE, frame_samples=FRAME_SAMPLES,
|
||||
object_delay_samples=args.object_delay_samples,
|
||||
trajectory_mode=args.trajectory_mode)
|
||||
timed_call(timings, "finalize_adm", master.finalize, tracks)
|
||||
except Exception:
|
||||
master.abort()
|
||||
raise
|
||||
errors, info = timed_call(timings, "validate_adm", validate, str(output))
|
||||
if errors:
|
||||
raise RuntimeError("ADM 校验失败: " + "; ".join(errors))
|
||||
# Windows 不允许删除仍被 NumPy memmap 持有的临时 core/raw;显式回收闭包。
|
||||
import gc
|
||||
gc.collect()
|
||||
|
||||
if args.skip_sha256:
|
||||
output_sha = None
|
||||
timings["sha256"] = 0.0
|
||||
else:
|
||||
output_sha = timed_call(timings, "sha256", sha256, output)
|
||||
total_seconds = time.perf_counter() - total_started
|
||||
mode_name = "speaker" if speaker_mode else "binaural" if binaural_mode else "adm"
|
||||
report = {
|
||||
"input": str(source),
|
||||
"output": str(output),
|
||||
"mode": mode_name,
|
||||
"metadata": str(metadata_json) if metadata_json is not None else None,
|
||||
"metadata_backend": metadata_backend,
|
||||
"joc_downmix_config_idx": getattr(index, "config_idx", None),
|
||||
"joc_core_channels": getattr(index, "n_channels", None),
|
||||
"joc_extra_pair_slots": bed_layout_plan(index)["extra_pair_slots"],
|
||||
"joc_bed_layout": (getattr(index, "bed_layout", None) or {}).get("name"),
|
||||
"joc_bed_channels": [
|
||||
BED_CHANNEL_NAMES.get(label, label)
|
||||
for label in getattr(index, "bed_channels", ())],
|
||||
"diagnostics": getattr(index, "diagnostics", None),
|
||||
"metadata_cache": str(metadata_cache_dir) if metadata_cache_dir is not None else None,
|
||||
"frames": frame_count,
|
||||
"duration_sec": duration_sec,
|
||||
"gain_db": args.gain_db,
|
||||
"gain_float32": float(gain),
|
||||
"gain_float64": float(gain_float64),
|
||||
"object_delay_samples": (None if speaker_mode else args.object_delay_samples),
|
||||
"trajectory_mode": args.trajectory_mode if mode_name == "adm" else None,
|
||||
"binaural_mode_value": (
|
||||
adm_atmos.JOC_BINAURAL_MODES[args.binaural_mode]
|
||||
if mode_name == "adm" else None),
|
||||
"render_seconds": render_seconds,
|
||||
"render_breakdown": render_breakdown,
|
||||
"renderer_backend": renderer_backend,
|
||||
"speaker_renderer_backend": speaker_backend_info,
|
||||
"speaker_layout": args.speaker_layout if speaker_mode else None,
|
||||
"speaker_metadata_offset": args.speaker_metadata_offset if speaker_mode else None,
|
||||
"speaker_clip": speaker_clip_info,
|
||||
"speaker_wav": speaker_wav_info,
|
||||
"binaural_renderer_backend": binaural_backend_info,
|
||||
"binaural_mode": (
|
||||
args.binaural_mode if (binaural_mode or mode_name == "adm") else None),
|
||||
"binaural_hrtf": (
|
||||
binaural_hrtf_report if binaural_backend_info else None),
|
||||
"binaural_clip": binaural_clip_info,
|
||||
"binaural_wav": binaural_wav_info,
|
||||
"output_clip": speaker_clip_info if speaker_mode else binaural_clip_info,
|
||||
"output_wav": speaker_wav_info if speaker_mode else binaural_wav_info,
|
||||
"streaming_adm": mode_name == "adm",
|
||||
"kept_raw": str(raw_path) if raw_path is not None else None,
|
||||
"timings": timings,
|
||||
"total_seconds": total_seconds,
|
||||
"adm_validation": info if mode_name == "adm" else None,
|
||||
"adm_metadata": getattr(master, "metadata_info", None) if master is not None else None,
|
||||
"sha256": output_sha,
|
||||
"python": platform.python_version(),
|
||||
"numpy": np.__version__,
|
||||
"ffmpeg": decode_option_info,
|
||||
}
|
||||
report_path = Path(str(output) + ".report.json")
|
||||
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
print(f"[PASS] {output}")
|
||||
if report["sha256"] is None:
|
||||
print(f"[PASS] {info}; SHA-256 skipped")
|
||||
else:
|
||||
print(f"[PASS] {info}; SHA-256={report['sha256']}")
|
||||
if speaker_mode:
|
||||
print(f"[time] JOC-DSP={render_seconds:.2f}s ({renderer_backend['name']}) "
|
||||
f"speaker={render_breakdown['speaker_render_seconds']:.2f}s "
|
||||
f"pipeline={render_breakdown['pipeline_wall_seconds']:.2f}s "
|
||||
f"total={report['total_seconds']:.2f}s")
|
||||
elif binaural_mode:
|
||||
print(f"[time] JOC-DSP={render_seconds:.2f}s ({renderer_backend['name']}) "
|
||||
f"binaural={render_breakdown['binaural_render_seconds']:.2f}s "
|
||||
f"pipeline={render_breakdown['pipeline_wall_seconds']:.2f}s "
|
||||
f"total={report['total_seconds']:.2f}s")
|
||||
else:
|
||||
print(f"[time] DSP={render_seconds:.2f}s ({renderer_backend['name']}) "
|
||||
f"render+ADM-stream={render_breakdown['pipeline_wall_seconds']:.2f}s "
|
||||
f"total={report['total_seconds']:.2f}s")
|
||||
print(f"[report] {report_path}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except KeyboardInterrupt:
|
||||
raise SystemExit(130)
|
||||
@@ -0,0 +1,3 @@
|
||||
numpy>=1.24
|
||||
scipy>=1.10
|
||||
h5py>=3.8
|
||||
@@ -1,434 +0,0 @@
|
||||
#include "adm/adm_metadata.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
#include "foundation/py_num.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kBedNames[10] = {
|
||||
"RoomCentricLeft", "RoomCentricRight", "RoomCentricCenter", "RoomCentricLFE",
|
||||
"RoomCentricLeftSideSurround", "RoomCentricRightSideSurround",
|
||||
"RoomCentricLeftRearSurround", "RoomCentricRightRearSurround",
|
||||
"RoomCentricLeftTopSurround", "RoomCentricRightTopSurround"};
|
||||
constexpr const char* kBedLabels[10] = {"RC_L", "RC_R", "RC_C", "RC_LFE", "RC_Lss",
|
||||
"RC_Rss", "RC_Lrs", "RC_Rrs", "RC_Lts", "RC_Rts"};
|
||||
constexpr double kBedPos[10][3] = {{-1.0, 1.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
|
||||
{-1.0, 1.0, -1.0}, {-1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
|
||||
{-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {-1.0, 0.0, 1.0},
|
||||
{1.0, 0.0, 1.0}};
|
||||
|
||||
std::string hex4(std::uint32_t value) {
|
||||
char buffer[16];
|
||||
std::snprintf(buffer, sizeof(buffer), "%04x", value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
std::string hex8(std::uint32_t value) {
|
||||
char buffer[16];
|
||||
std::snprintf(buffer, sizeof(buffer), "%08x", value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
void put_u16(std::string* out, std::uint16_t value) {
|
||||
char buffer[2];
|
||||
std::memcpy(buffer, &value, 2);
|
||||
out->append(buffer, 2);
|
||||
}
|
||||
|
||||
void put_u32(std::string* out, std::uint32_t value) {
|
||||
char buffer[4];
|
||||
std::memcpy(buffer, &value, 4);
|
||||
out->append(buffer, 4);
|
||||
}
|
||||
|
||||
std::uint8_t checksum(const std::string& segment) {
|
||||
int sum = static_cast<int>(segment.size());
|
||||
for (const char raw : segment) {
|
||||
sum += static_cast<unsigned char>(raw);
|
||||
}
|
||||
return static_cast<std::uint8_t>((~sum + 1) & 0xFF);
|
||||
}
|
||||
|
||||
// OAMD trim element warp mode as the dbmd, DAMF and AXML warp field carry it:
|
||||
// 0 normal, 1 warping, 2 downmix_pl2x, 3 downmix_loro.
|
||||
std::uint32_t trim_warp_mode(const oamd::OamdTrim* trim) {
|
||||
if (trim == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return std::min<std::uint32_t>(trim->warp_mode, 3u);
|
||||
}
|
||||
|
||||
// Trim records carry the balance as a sign bit plus a 7-bit magnitude on a
|
||||
// 1/127 grid.
|
||||
std::uint8_t balance_byte(double value) {
|
||||
const long long magnitude =
|
||||
std::min<long long>(pynum::py_round(std::fabs(value) * 127.0), 127);
|
||||
return static_cast<std::uint8_t>(static_cast<unsigned>(magnitude) |
|
||||
(value < 0.0 ? 0x80u : 0x00u));
|
||||
}
|
||||
|
||||
void append_trim_records(const oamd::OamdTrim& trim, std::string* out) {
|
||||
out->append("<DolbyTrimModeSetup>");
|
||||
for (int index = 0; index < oamd::kTrimConfigs; ++index) {
|
||||
const oamd::TrimConfig& record = trim.configs[index];
|
||||
if (record.kind == oamd::TrimConfigKind::kAbsent) {
|
||||
continue;
|
||||
}
|
||||
out->append("<records><type>");
|
||||
out->append(std::to_string(index));
|
||||
out->append("</type>");
|
||||
if (record.kind == oamd::TrimConfigKind::kDefault) {
|
||||
out->append("</records>");
|
||||
continue;
|
||||
}
|
||||
const bool explicit_record = record.kind == oamd::TrimConfigKind::kExplicit;
|
||||
const double centre = explicit_record ? record.centre : 0.0;
|
||||
const double surround = explicit_record ? record.surround : 0.0;
|
||||
const double height = explicit_record ? record.height : 0.0;
|
||||
const double overhead = explicit_record ? record.balance_overhead : 0.0;
|
||||
const double listener = explicit_record ? record.balance_listener : 0.0;
|
||||
out->append("<centerTrim>" + pynum::format_fixed(centre, 6) + "</centerTrim>");
|
||||
out->append("<surroundTrim>" + pynum::format_fixed(surround, 6) + "</surroundTrim>");
|
||||
out->append("<heightTrim>" + pynum::format_fixed(height, 6) + "</heightTrim>");
|
||||
out->append("<balanceOverhead>" + pynum::format_fixed(overhead, 6) +
|
||||
"</balanceOverhead>");
|
||||
out->append("<balanceListener>" + pynum::format_fixed(listener, 6) +
|
||||
"</balanceListener>");
|
||||
out->append("</records>");
|
||||
}
|
||||
out->append("</DolbyTrimModeSetup>");
|
||||
}
|
||||
|
||||
void append_atmos_master_block(const oamd::OamdTrim& trim, std::string* out) {
|
||||
const std::string warp = std::to_string(trim_warp_mode(&trim));
|
||||
out->append("<DolbyAtmosMasterFile><DolbyMasterContentInfo><render_warp_mode>");
|
||||
out->append(warp);
|
||||
out->append("</render_warp_mode></DolbyMasterContentInfo>");
|
||||
append_trim_records(trim, out);
|
||||
out->append("<DolbyWarpMode>");
|
||||
out->append(warp);
|
||||
out->append("</DolbyWarpMode></DolbyAtmosMasterFile>");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string ts(double seconds) {
|
||||
long long whole = static_cast<long long>(seconds);
|
||||
long long fraction = pynum::py_round((seconds - static_cast<double>(whole)) * 100000.0);
|
||||
if (fraction >= 100000) {
|
||||
whole += 1;
|
||||
fraction = 0;
|
||||
}
|
||||
char buffer[32];
|
||||
std::snprintf(buffer, sizeof(buffer), "%02lld:%02lld:%02lld.%05lld", whole / 3600,
|
||||
(whole % 3600) / 60, whole % 60, fraction);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
bool binaural_mode_from_name(const char* name, BinauralMode* out) {
|
||||
if (name == nullptr || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (std::strcmp(name, "off") == 0) { *out = BinauralMode::Off; return true; }
|
||||
if (std::strcmp(name, "near") == 0) { *out = BinauralMode::Near; return true; }
|
||||
if (std::strcmp(name, "far") == 0) { *out = BinauralMode::Far; return true; }
|
||||
if (std::strcmp(name, "mid") == 0) { *out = BinauralMode::Mid; return true; }
|
||||
if (std::strcmp(name, "unspecified") == 0) { *out = BinauralMode::Unspecified; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string build_chna() {
|
||||
std::string out;
|
||||
put_u16(&out, static_cast<std::uint16_t>(kTrackCount));
|
||||
put_u16(&out, static_cast<std::uint16_t>(kTrackCount));
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
put_u16(&out, static_cast<std::uint16_t>(i + 1));
|
||||
out += "ATU_" + hex8(i + 1);
|
||||
out += "AT_0001" + hex4(0x1001 + i) + "_01";
|
||||
out += "AP_00011001";
|
||||
out.push_back('\0');
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
put_u16(&out, static_cast<std::uint16_t>(i + 11));
|
||||
out += "ATU_" + hex8(i + 11);
|
||||
out += "AT_0003" + hex4(0x1001 + i) + "_01";
|
||||
out += "AP_0003" + hex4(0x1001 + i);
|
||||
out.push_back('\0');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out,
|
||||
const oamd::OamdTrim* trim) {
|
||||
if (out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output");
|
||||
}
|
||||
const std::uint32_t mode_value = static_cast<std::uint32_t>(mode);
|
||||
if (mode_value > 4u) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
|
||||
"invalid JOC binaural render mode");
|
||||
}
|
||||
out->clear();
|
||||
put_u32(out, 0x01000006u);
|
||||
|
||||
std::string segment7(96, '\0');
|
||||
segment7[1] = static_cast<char>(0x47);
|
||||
segment7[5] = static_cast<char>(0x60);
|
||||
segment7[8] = static_cast<char>(0x24);
|
||||
segment7[9] = static_cast<char>(0x24);
|
||||
out->push_back(7);
|
||||
put_u16(out, 96);
|
||||
out->append(segment7);
|
||||
out->push_back(static_cast<char>(checksum(segment7)));
|
||||
|
||||
std::string segment9(248, '\0');
|
||||
const std::string creator = "JustOneCacophony";
|
||||
const std::string renderer = "JustOneCacophony";
|
||||
std::memcpy(&segment9[0], creator.data(), creator.size());
|
||||
std::memcpy(&segment9[32], renderer.data(), renderer.size());
|
||||
segment9[96] = 2;
|
||||
segment9[97] = 1;
|
||||
segment9[98] = 0;
|
||||
segment9[103] = 0x03;
|
||||
segment9[106] = 0x01;
|
||||
segment9[111] = 0x22;
|
||||
segment9[112] = static_cast<char>(0xFF);
|
||||
// at[152]: the low three bits are the warp code, the other bits belong to
|
||||
// the template's packed fields.
|
||||
segment9[152] = static_cast<char>(
|
||||
(static_cast<unsigned char>(segment9[152]) & 0xF8u) | trim_warp_mode(trim));
|
||||
out->push_back(9);
|
||||
put_u16(out, 248);
|
||||
out->append(segment9);
|
||||
out->push_back(static_cast<char>(checksum(segment9)));
|
||||
|
||||
// The reference allocates the body zeroed and then fills only the trailing
|
||||
// `object_count` bytes with 0x84, so the template region stays zero.
|
||||
const std::size_t object_body = 5u + 262u + object_count;
|
||||
std::string segment10(object_body, '\0');
|
||||
const std::uint32_t sync = 0xF8726FBDu;
|
||||
std::memcpy(&segment10[0], &sync, 4);
|
||||
segment10[4] = static_cast<char>(object_count);
|
||||
for (std::size_t i = 5u + 262u; i < segment10.size(); ++i) {
|
||||
segment10[i] = static_cast<char>(0x84);
|
||||
}
|
||||
// Nine 15-byte configuration trims start at byte 7; the first byte of each
|
||||
// carries the auto-trim flag in bit 0 (1 = automatic, 0 = manual). A
|
||||
// configuration without an explicit record stays automatic, so a freshly
|
||||
// rendered master opens with automatic trims in the renderer.
|
||||
for (std::size_t layout = 0; layout < static_cast<std::size_t>(oamd::kTrimConfigs);
|
||||
++layout) {
|
||||
const std::size_t base = 7u + 15u * layout;
|
||||
const oamd::TrimConfig* record =
|
||||
trim != nullptr ? &trim->configs[layout] : nullptr;
|
||||
const bool manual = record != nullptr &&
|
||||
(record->kind == oamd::TrimConfigKind::kExplicit ||
|
||||
record->kind == oamd::TrimConfigKind::kDisabled);
|
||||
if (!manual) {
|
||||
segment10[base] =
|
||||
static_cast<char>(static_cast<unsigned char>(segment10[base]) | 0x01u);
|
||||
continue;
|
||||
}
|
||||
segment10[base] = static_cast<char>(static_cast<unsigned char>(segment10[base]) & 0xFEu);
|
||||
const bool explicit_record = record->kind == oamd::TrimConfigKind::kExplicit;
|
||||
const double levels[3] = {explicit_record ? record->centre : 0.0,
|
||||
explicit_record ? record->surround : 0.0,
|
||||
explicit_record ? record->height : 0.0};
|
||||
for (std::size_t index = 0; index < 3; ++index) {
|
||||
const float stored = static_cast<float>(levels[index]);
|
||||
std::memcpy(&segment10[base + 1u + 4u * index], &stored, sizeof(stored));
|
||||
}
|
||||
const double overhead = explicit_record ? record->balance_overhead : 0.0;
|
||||
const double listener = explicit_record ? record->balance_listener : 0.0;
|
||||
segment10[base + 13u] = static_cast<char>(balance_byte(overhead));
|
||||
segment10[base + 14u] = static_cast<char>(balance_byte(listener));
|
||||
}
|
||||
const std::size_t object_modes = 4u + 2u + 1u + 9u * 15u + object_count;
|
||||
for (std::uint32_t i = 10; i < std::min<std::uint32_t>(object_count, 10u + kObjectCount); ++i) {
|
||||
const std::size_t index = object_modes + i;
|
||||
if (index >= segment10.size()) {
|
||||
return Status::fail(JOC_ERR_INTERNAL, stage::kOutput, "dbmd object slot out of range");
|
||||
}
|
||||
segment10[index] = static_cast<char>((static_cast<unsigned char>(segment10[index]) & 0xF8u) |
|
||||
mode_value);
|
||||
}
|
||||
out->push_back(10);
|
||||
put_u16(out, static_cast<std::uint16_t>(segment10.size()));
|
||||
out->append(segment10);
|
||||
out->push_back(static_cast<char>(checksum(segment10)));
|
||||
out->append("\0\0", 2);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate,
|
||||
const oamd::OamdTrim* trim) {
|
||||
const double scale = static_cast<double>(rate);
|
||||
std::string out;
|
||||
out.reserve(64u * 1024u);
|
||||
const std::string duration_ts = ts(duration_sec);
|
||||
|
||||
out += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
||||
out += "<ebuCoreMain xsi:schemaLocation=\"urn:ebu:metadata-schema:ebuCore_2016 ebucore.xsd\" "
|
||||
"lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
|
||||
"xmlns=\"urn:ebu:metadata-schema:ebuCore_2016\">";
|
||||
out += "<coreMetadata><format><audioFormatExtended>";
|
||||
out += "<audioProgramme audioProgrammeID=\"APR_1001\" audioProgrammeName=\"EAC3JOC_Export\" "
|
||||
"start=\"" +
|
||||
ts(0.0) + "\" end=\"" + duration_ts + "\">";
|
||||
out += "<audioContentIDRef>ACO_1001</audioContentIDRef>";
|
||||
out += "<audioContentIDRef>ACO_1002</audioContentIDRef>";
|
||||
out += "</audioProgramme>";
|
||||
out += "<audioContent audioContentID=\"ACO_1001\" "
|
||||
"audioContentName=\"EAC3JOC_Master_Content\">";
|
||||
out += "<audioObjectIDRef>AO_1001</audioObjectIDRef>";
|
||||
out += "<dialogue mixedContentKind=\"0\">2</dialogue>";
|
||||
out += "</audioContent>";
|
||||
out += "<audioContent audioContentID=\"ACO_1002\" audioContentName=\"Objects\">";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioObjectIDRef>AO_" + hex4(0x100b + i) + "</audioObjectIDRef>";
|
||||
}
|
||||
out += "<dialogue mixedContentKind=\"0\">2</dialogue>";
|
||||
out += "</audioContent>";
|
||||
out += "<audioObject audioObjectID=\"AO_1001\" audioObjectName=\"Bed\" start=\"" + ts(0.0) +
|
||||
"\" duration=\"" + duration_ts + "\">";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackUIDRef>ATU_" + hex8(i + 1) + "</audioTrackUIDRef>";
|
||||
}
|
||||
out += "</audioObject>";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioObject audioObjectID=\"AO_" + hex4(0x100b + i) +
|
||||
"\" audioObjectName=\"Audio Object " + std::to_string(i + 1) + "\" start=\"" +
|
||||
ts(0.0) + "\" duration=\"" + duration_ts + "\">";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "<audioTrackUIDRef>ATU_" + hex8(11 + i) + "</audioTrackUIDRef>";
|
||||
out += "</audioObject>";
|
||||
}
|
||||
out += "<audioPackFormat audioPackFormatID=\"AP_00011001\" "
|
||||
"audioPackFormatName=\"EAC3JOCBedPack\" typeDefinition=\"DirectSpeakers\" "
|
||||
"typeLabel=\"0001\">";
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioChannelFormatIDRef>AC_0001" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
}
|
||||
out += "</audioPackFormat>";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioPackFormat audioPackFormatID=\"AP_0003" + hex4(0x1001 + i) +
|
||||
"\" audioPackFormatName=\"JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" typeDefinition=\"Objects\" typeLabel=\"0003\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0003" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "</audioPackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioChannelFormat audioChannelFormatID=\"AC_0001" + hex4(0x1001 + i) +
|
||||
"\" audioChannelFormatName=\"" + kBedNames[i] +
|
||||
"\" typeDefinition=\"DirectSpeakers\" typeLabel=\"0001\">";
|
||||
out += "<audioBlockFormat audioBlockFormatID=\"AB_0001" + hex4(0x1001 + i) +
|
||||
"_00000001\">";
|
||||
out += "<cartesian>1</cartesian>";
|
||||
out += "<position coordinate=\"X\">" + pynum::format_fixed(kBedPos[i][0], 10) +
|
||||
"</position>";
|
||||
out += "<position coordinate=\"Y\">" + pynum::format_fixed(kBedPos[i][1], 10) +
|
||||
"</position>";
|
||||
if (kBedPos[i][2] != 0.0) {
|
||||
out += "<position coordinate=\"Z\">" + pynum::format_fixed(kBedPos[i][2], 10) +
|
||||
"</position>";
|
||||
}
|
||||
out += std::string("<speakerLabel>") + kBedLabels[i] + "</speakerLabel>";
|
||||
out += "</audioBlockFormat>";
|
||||
out += "</audioChannelFormat>";
|
||||
}
|
||||
for (std::size_t i = 0; i < tracks.size(); ++i) {
|
||||
const Track& track = tracks[i];
|
||||
out += "<audioChannelFormat audioChannelFormatID=\"AC_0003" +
|
||||
hex4(0x1001 + static_cast<std::uint32_t>(i)) + "\" audioChannelFormatName=\"" +
|
||||
track.name + "\" typeDefinition=\"Objects\" typeLabel=\"0003\">";
|
||||
for (std::size_t k = 0; k < track.blocks.size(); ++k) {
|
||||
const Keyframe& block = track.blocks[k];
|
||||
out += "<audioBlockFormat audioBlockFormatID=\"AB_0003" +
|
||||
hex4(0x1001 + static_cast<std::uint32_t>(i)) + "_" +
|
||||
hex8(static_cast<std::uint32_t>(k + 1)) + "\" rtime=\"" +
|
||||
ts(static_cast<double>(block.rtime_samples) / scale) + "\" duration=\"" +
|
||||
ts(static_cast<double>(block.duration_samples) / scale) + "\">";
|
||||
out += "<cartesian>1</cartesian>";
|
||||
out += "<position coordinate=\"X\">" + pynum::format_fixed(block.x, 10) + "</position>";
|
||||
out += "<position coordinate=\"Y\">" + pynum::format_fixed(block.y, 10) + "</position>";
|
||||
if (block.z != 0.0) {
|
||||
out += "<position coordinate=\"Z\">" + pynum::format_fixed(block.z, 10) +
|
||||
"</position>";
|
||||
}
|
||||
out += "<jumpPosition interpolationLength=\"" +
|
||||
pynum::format_fixed(static_cast<double>(block.interpolation_samples) / scale,
|
||||
5) +
|
||||
"\">1</jumpPosition>";
|
||||
out += "</audioBlockFormat>";
|
||||
}
|
||||
out += "</audioChannelFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackUID UID=\"ATU_" + hex8(i + 1) +
|
||||
"\" bitDepth=\"24\" sampleRate=\"48000\">";
|
||||
out += "<audioTrackFormatIDRef>AT_0001" + hex4(0x1001 + i) + "_01</audioTrackFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
out += "</audioTrackUID>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioTrackUID UID=\"ATU_" + hex8(11 + i) +
|
||||
"\" bitDepth=\"24\" sampleRate=\"48000\">";
|
||||
out += "<audioTrackFormatIDRef>AT_0003" + hex4(0x1001 + i) + "_01</audioTrackFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "</audioTrackUID>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackFormat audioTrackFormatID=\"AT_0001" + hex4(0x1001 + i) +
|
||||
"_01\" audioTrackFormatName=\"PCM_" + kBedNames[i] +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioStreamFormatIDRef>AS_0001" + hex4(0x1001 + i) +
|
||||
"</audioStreamFormatIDRef>";
|
||||
out += "</audioTrackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioTrackFormat audioTrackFormatID=\"AT_0003" + hex4(0x1001 + i) +
|
||||
"_01\" audioTrackFormatName=\"PCM_JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioStreamFormatIDRef>AS_0003" + hex4(0x1001 + i) +
|
||||
"</audioStreamFormatIDRef>";
|
||||
out += "</audioTrackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioStreamFormat audioStreamFormatID=\"AS_0001" + hex4(0x1001 + i) +
|
||||
"\" audioStreamFormatName=\"PCM_" + kBedNames[i] +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0001" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
out += "<audioTrackFormatIDRef>AT_0001" + hex4(0x1001 + i) +
|
||||
"_01</audioTrackFormatIDRef>";
|
||||
out += "</audioStreamFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioStreamFormat audioStreamFormatID=\"AS_0003" + hex4(0x1001 + i) +
|
||||
"\" audioStreamFormatName=\"PCM_JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0003" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "<audioTrackFormatIDRef>AT_0003" + hex4(0x1001 + i) +
|
||||
"_01</audioTrackFormatIDRef>";
|
||||
out += "</audioStreamFormat>";
|
||||
}
|
||||
if (trim != nullptr) {
|
||||
append_atmos_master_block(*trim, &out);
|
||||
}
|
||||
out += "</audioFormatExtended></format></coreMetadata>";
|
||||
out += "</ebuCoreMain>";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -1,50 +0,0 @@
|
||||
// Port of src/adm_atmos.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
inline constexpr int kObjectCount = 15;
|
||||
inline constexpr std::uint32_t kTrackCount = 25;
|
||||
|
||||
struct Keyframe {
|
||||
std::int64_t rtime_samples = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t duration_samples = 0;
|
||||
std::int64_t interpolation_samples = 0;
|
||||
};
|
||||
|
||||
struct Track {
|
||||
std::string name;
|
||||
std::vector<Keyframe> blocks;
|
||||
};
|
||||
|
||||
// HH:MM:SS.fffff with the reference's truncation + round-half-even carry.
|
||||
std::string ts(double seconds);
|
||||
|
||||
enum class BinauralMode : std::uint32_t { Off = 0, Near = 1, Far = 2, Mid = 3, Unspecified = 4 };
|
||||
|
||||
bool binaural_mode_from_name(const char* name, BinauralMode* out);
|
||||
|
||||
// ``trim`` is the OAMD trim element of the programme; when it is present the
|
||||
// AXML carries the Dolby master-file block (content info, trim records, warp
|
||||
// mode) and the dbmd chunk carries the per-configuration records and the warp
|
||||
// code.
|
||||
std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate,
|
||||
const oamd::OamdTrim* trim = nullptr);
|
||||
|
||||
std::string build_chna();
|
||||
|
||||
Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out,
|
||||
const oamd::OamdTrim* trim = nullptr);
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -1,292 +0,0 @@
|
||||
#include "adm/adm_tracks.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "foundation/geometry.h"
|
||||
#include "foundation/metadata_timing.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
namespace {
|
||||
|
||||
struct Point {
|
||||
std::int64_t sample = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t interpolation_samples = 0;
|
||||
};
|
||||
|
||||
// otherwise (the reference drops silently).
|
||||
void append_point(std::vector<Point>* points, std::int64_t sample, double x, double y, double z,
|
||||
std::int64_t interpolation_samples) {
|
||||
if (!points->empty() && sample == points->back().sample) {
|
||||
points->back() = Point{sample, x, y, z, interpolation_samples};
|
||||
} else if (points->empty() || sample > points->back().sample) {
|
||||
points->push_back(Point{sample, x, y, z, interpolation_samples});
|
||||
}
|
||||
}
|
||||
|
||||
void lerp(double ax, double ay, double az, double bx, double by, double bz, double amount,
|
||||
double* x, double* y, double* z) {
|
||||
*x = ax + (bx - ax) * amount;
|
||||
*y = ay + (by - ay) * amount;
|
||||
*z = az + (bz - az) * amount;
|
||||
}
|
||||
|
||||
void points_to_blocks(const std::vector<Point>& points, std::int64_t total_samples,
|
||||
std::vector<Keyframe>* out) {
|
||||
for (std::size_t index = 0; index < points.size(); ++index) {
|
||||
const Point& point = points[index];
|
||||
const std::int64_t end =
|
||||
(index + 1 < points.size()) ? points[index + 1].sample : total_samples;
|
||||
const std::int64_t duration = std::max<std::int64_t>(0, end - point.sample);
|
||||
if (duration == 0) {
|
||||
continue;
|
||||
}
|
||||
Keyframe keyframe;
|
||||
keyframe.rtime_samples = point.sample;
|
||||
keyframe.x = point.x;
|
||||
keyframe.y = point.y;
|
||||
keyframe.z = point.z;
|
||||
keyframe.duration_samples = duration;
|
||||
keyframe.interpolation_samples = std::min(point.interpolation_samples, duration);
|
||||
out->push_back(keyframe);
|
||||
}
|
||||
}
|
||||
|
||||
Status non_monotonic(const char* name, const char* message, int object_index, std::int64_t sample,
|
||||
std::int64_t previous_sample) {
|
||||
return Status::fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, stage::kOamd,
|
||||
std::string(name) + ": " + message + " (object " +
|
||||
std::to_string(object_index) + ", sample " + std::to_string(sample) +
|
||||
", previous " + std::to_string(previous_sample) + ")");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_block_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out) {
|
||||
out->clear();
|
||||
const double scale = static_cast<double>(rate);
|
||||
if (events.empty()) {
|
||||
Keyframe keyframe;
|
||||
keyframe.rtime_samples = 0;
|
||||
keyframe.duration_samples = total_samples;
|
||||
keyframe.interpolation_samples = 0;
|
||||
keyframe.x = 0.0;
|
||||
keyframe.y = 0.0;
|
||||
keyframe.z = 0.0;
|
||||
out->push_back(keyframe);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::vector<Point> points;
|
||||
double current[3] = {events[0].x, events[0].y, events[0].z};
|
||||
append_point(&points, 0, current[0], current[1], current[2], 0);
|
||||
|
||||
for (std::size_t index = 1; index < events.size(); ++index) {
|
||||
const OamdEvent& event = events[index];
|
||||
const std::int64_t event_start = timing::align_metadata_sample(
|
||||
event.sample + object_delay_samples, update_block_samples);
|
||||
if (event_start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
const std::int64_t ramp = std::max<std::int64_t>(0, event.ramp_samples);
|
||||
const std::int64_t block_start = event_start;
|
||||
if (block_start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
const std::int64_t ramp_end = block_start + ramp;
|
||||
|
||||
if (block_start < points.back().sample) {
|
||||
return non_monotonic("non_monotonic_compact_position_updates",
|
||||
"compact object position update moved backwards", object_index,
|
||||
block_start, points.back().sample);
|
||||
}
|
||||
if (index + 1 < events.size()) {
|
||||
const std::int64_t next_block_start = timing::align_metadata_sample(
|
||||
events[index + 1].sample + object_delay_samples, update_block_samples);
|
||||
if (next_block_start < ramp_end) {
|
||||
return non_monotonic("overlapping_compact_position_ramps",
|
||||
"a new position update arrived before the previous compact "
|
||||
"ramp finished",
|
||||
object_index, block_start, ramp_end);
|
||||
}
|
||||
}
|
||||
|
||||
double target[3] = {event.x, event.y, event.z};
|
||||
std::int64_t interpolation = ramp;
|
||||
const std::int64_t available = total_samples - block_start;
|
||||
if (ramp > available) {
|
||||
const double amount = static_cast<double>(available) / static_cast<double>(ramp);
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
lerp(current[0], current[1], current[2], event.x, event.y, event.z, amount, &x, &y, &z);
|
||||
target[0] = x;
|
||||
target[1] = y;
|
||||
target[2] = z;
|
||||
interpolation = available;
|
||||
}
|
||||
append_point(&points, block_start, target[0], target[1], target[2], interpolation);
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
}
|
||||
|
||||
points_to_blocks(points, total_samples, out);
|
||||
(void)scale;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_block_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out) {
|
||||
out->clear();
|
||||
(void)rate;
|
||||
if (events.empty()) {
|
||||
Keyframe keyframe;
|
||||
keyframe.duration_samples = total_samples;
|
||||
out->push_back(keyframe);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::vector<Point> points;
|
||||
double current[3] = {events[0].x, events[0].y, events[0].z};
|
||||
append_point(&points, 0, current[0], current[1], current[2], 0);
|
||||
|
||||
for (std::size_t index = 1; index < events.size(); ++index) {
|
||||
const OamdEvent& event = events[index];
|
||||
const std::int64_t start =
|
||||
timing::align_metadata_sample(event.sample + object_delay_samples, update_block_samples);
|
||||
if (start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
if (start < points.back().sample) {
|
||||
return non_monotonic("non_monotonic_position_updates",
|
||||
"object position update moved backwards", object_index, start,
|
||||
points.back().sample);
|
||||
}
|
||||
if (start > points.back().sample) {
|
||||
append_point(&points, start, current[0], current[1], current[2], 0);
|
||||
}
|
||||
const std::int64_t ramp = std::max<std::int64_t>(0, event.ramp_samples);
|
||||
if (ramp == 0) {
|
||||
append_point(&points, start, event.x, event.y, event.z, 0);
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
continue;
|
||||
}
|
||||
const std::int64_t steps = (ramp + update_block_samples - 1) / update_block_samples;
|
||||
const std::int64_t end = start + steps * update_block_samples;
|
||||
if (index + 1 < events.size()) {
|
||||
const std::int64_t next_start = timing::align_metadata_sample(
|
||||
events[index + 1].sample + object_delay_samples, update_block_samples);
|
||||
if (next_start < end) {
|
||||
return non_monotonic("overlapping_position_ramps",
|
||||
"a new position update arrived before the previous ramp "
|
||||
"finished",
|
||||
object_index, start, end);
|
||||
}
|
||||
}
|
||||
std::int64_t future = ramp;
|
||||
std::int64_t elapsed = 0;
|
||||
double position[3] = {current[0], current[1], current[2]};
|
||||
while (future > 0) {
|
||||
const double amount =
|
||||
std::min(static_cast<double>(update_block_samples) / static_cast<double>(future),
|
||||
1.0);
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
lerp(position[0], position[1], position[2], event.x, event.y, event.z, amount, &x, &y,
|
||||
&z);
|
||||
position[0] = x;
|
||||
position[1] = y;
|
||||
position[2] = z;
|
||||
elapsed += update_block_samples;
|
||||
const std::int64_t sample = start + elapsed;
|
||||
if (sample >= total_samples) {
|
||||
break;
|
||||
}
|
||||
append_point(&points, sample, position[0], position[1], position[2],
|
||||
update_block_samples);
|
||||
future -= update_block_samples;
|
||||
}
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
}
|
||||
|
||||
points_to_blocks(points, total_samples, out);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
void TrajectoryBuilder::submit_frame(std::int64_t frame_index, const oamd::OamdUpdate* update,
|
||||
std::int64_t outer_offset) {
|
||||
std::int64_t event_sample = frame_index * 1536;
|
||||
std::int64_t ramp_samples = 0;
|
||||
if (update != nullptr) {
|
||||
state_.apply(*update);
|
||||
event_sample += outer_offset + static_cast<std::int64_t>(update->block_offset_samples);
|
||||
ramp_samples = static_cast<std::int64_t>(update->ramp_duration_samples);
|
||||
}
|
||||
for (int object = 1; object <= kObjectCount; ++object) {
|
||||
if (fixed_[object - 1]) {
|
||||
continue; // 固定 bed 声道:位置由布局给出,不跟 OAMD 更新
|
||||
}
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
geometry::q_to_adm_xyz(state_.q(object, 0), state_.q(object, 1), state_.q(object, 2), &x,
|
||||
&y, &z);
|
||||
const int slot = object - 1;
|
||||
if (!has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y ||
|
||||
previous_[slot][2] != z) {
|
||||
events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples});
|
||||
previous_[slot][0] = x;
|
||||
previous_[slot][1] = y;
|
||||
previous_[slot][2] = z;
|
||||
has_previous_[slot] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Status TrajectoryBuilder::build(std::int64_t total_samples, TrajectoryMode mode,
|
||||
std::vector<Track>* out) const {
|
||||
out->clear();
|
||||
out->reserve(kObjectCount);
|
||||
for (int object = 1; object <= kObjectCount; ++object) {
|
||||
Track track;
|
||||
track.name = "JOC_Object_" + std::to_string(object);
|
||||
if (fixed_[object - 1]) {
|
||||
Keyframe block;
|
||||
block.rtime_samples = 0;
|
||||
block.x = fixed_xyz_[object - 1][0];
|
||||
block.y = fixed_xyz_[object - 1][1];
|
||||
block.z = fixed_xyz_[object - 1][2];
|
||||
block.duration_samples = total_samples;
|
||||
block.interpolation_samples = 0;
|
||||
track.blocks.push_back(block);
|
||||
out->push_back(std::move(track));
|
||||
continue;
|
||||
}
|
||||
const Status status =
|
||||
(mode == TrajectoryMode::Compact)
|
||||
? expand_compact(events_[object - 1], total_samples, rate_, block_,
|
||||
object_delay_, object, &track.blocks)
|
||||
: expand_dense64(events_[object - 1], total_samples, rate_, block_,
|
||||
object_delay_, object, &track.blocks);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
out->push_back(std::move(track));
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -1,77 +0,0 @@
|
||||
// Port of src/oamd_tracks.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "adm/adm_metadata.h"
|
||||
#include "foundation/metadata_timing.h"
|
||||
#include "foundation/status.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
struct OamdEvent {
|
||||
std::int64_t sample = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t ramp_samples = 0;
|
||||
};
|
||||
|
||||
enum class TrajectoryMode { Compact, Dense64 };
|
||||
|
||||
// Feeds the same per-frame OAMD state machine the reference's build_adm_tracks
|
||||
// runs, and records one event per object whenever its coordinates change.
|
||||
class TrajectoryBuilder {
|
||||
public:
|
||||
TrajectoryBuilder(std::uint32_t rate = 48000,
|
||||
std::int64_t update_block_samples = timing::kMetadataBlockSamples,
|
||||
std::int64_t object_delay_samples = 1473)
|
||||
: rate_(rate),
|
||||
block_(update_block_samples),
|
||||
object_delay_(object_delay_samples) {}
|
||||
|
||||
void submit_frame(std::int64_t frame_index, const oamd::OamdUpdate* update, std::int64_t outer_offset);
|
||||
|
||||
Status build(std::int64_t total_samples, TrajectoryMode mode, std::vector<Track>* out) const;
|
||||
|
||||
// bed 声道是固定通道:OAMD 不给位置,整片静止在声明的 bed 方位。
|
||||
void set_fixed_position(int object_index, double x, double y, double z) {
|
||||
if (object_index < 1 || object_index > kObjectCount) {
|
||||
return;
|
||||
}
|
||||
const int slot = object_index - 1;
|
||||
fixed_[slot] = true;
|
||||
fixed_xyz_[slot][0] = x;
|
||||
fixed_xyz_[slot][1] = y;
|
||||
fixed_xyz_[slot][2] = z;
|
||||
}
|
||||
|
||||
const std::vector<OamdEvent>& events(int object_index) const { return events_[object_index]; }
|
||||
std::uint32_t rate() const { return rate_; }
|
||||
std::int64_t object_delay_samples() const { return object_delay_; }
|
||||
|
||||
private:
|
||||
std::uint32_t rate_;
|
||||
std::int64_t block_;
|
||||
std::int64_t object_delay_;
|
||||
oamd::OamdState state_;
|
||||
std::vector<OamdEvent> events_[kObjectCount];
|
||||
bool has_previous_[kObjectCount] = {};
|
||||
double previous_[kObjectCount][3] = {};
|
||||
bool fixed_[kObjectCount] = {};
|
||||
double fixed_xyz_[kObjectCount][3] = {};
|
||||
};
|
||||
|
||||
Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_block_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out);
|
||||
|
||||
Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_block_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out);
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -0,0 +1,145 @@
|
||||
"""把 LFE、15 路对象 PCM 和对象轨迹组装为 ADM BWF。
|
||||
|
||||
固定输出契约:
|
||||
EAC3JOC 重放输出 = 16ch(ch0 = LFE + ch1-15 = 15 对象);
|
||||
最终 ADM BWF = 7.1.2 bed(L R C Ls Rs Lb Rb + LFE + Ltf Rtf = 10ch)
|
||||
—— 除 LFE 外全部静音;
|
||||
15 对象 = ch1-15 直接填充对象轨;轨迹 = OAMD(q1/q2/q3 → xyz)。
|
||||
"""
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
|
||||
import adm_atmos
|
||||
|
||||
|
||||
def assemble_from_raw(raw16_path, out_path, scale=1.0, kf_tracks=None,
|
||||
duration_sec=None, rate=48000, joc_binaural_mode=4):
|
||||
"""16ch f32 交织 raw → 25ch ADM BWF(空 7.1.2 bed + LFE + 15 对象)。
|
||||
|
||||
raw16: (n, 16) 交织(ch0 = LFE,ch1-15 = 对象)。
|
||||
scale: 1.0 = 默认 0 dB,不附加输出缩放。该参数与 joc_clipgain 无关;
|
||||
主命令行已在渲染阶段应用用户增益,因此这里传 1.0。
|
||||
kf_tracks: 可选轨迹关键帧(OAMD 输出,格式 [(obj_id, [(t, x, y, z), ...]), ...]);
|
||||
缺省 = 静止参考位置(adm_atmos 默认)。
|
||||
"""
|
||||
raw = np.memmap(raw16_path, dtype=np.float32, mode="r")
|
||||
n = len(raw) // 16
|
||||
raw = raw[:n * 16].reshape(-1, 16)
|
||||
if duration_sec is None:
|
||||
duration_sec = n / rate
|
||||
# 惰性视图:adm_atmos 按块读取,避免全片 25ch 在内存中展开。
|
||||
class BedView:
|
||||
shape = (n, 10)
|
||||
|
||||
def __getitem__(self, key):
|
||||
src = np.asarray(raw[key], dtype=np.float32)
|
||||
one = src.ndim == 1
|
||||
if one:
|
||||
src = src[None, :]
|
||||
out = np.zeros((len(src), 10), dtype=np.float32)
|
||||
out[:, 3] = np.multiply(src[:, 0], np.float32(scale), dtype=np.float32)
|
||||
return out[0] if one else out
|
||||
|
||||
class ObjView:
|
||||
shape = (n, 16)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return np.multiply(np.asarray(raw[key], dtype=np.float32),
|
||||
np.float32(scale), dtype=np.float32)
|
||||
if kf_tracks is None:
|
||||
kf_tracks = []
|
||||
for oi in range(15):
|
||||
# 静止参考位置(q1=q2=q3=0 → 原点;实际坐标按 OAMD 输出填入)
|
||||
kf_tracks.append(("JOC_Object_%d" % (oi + 1),
|
||||
[(0.0, 0.0, 0.0, 0.0, max(duration_sec, 1e-6))]))
|
||||
adm_atmos.build_master(out_path, BedView(), ObjView(), kf_tracks,
|
||||
duration_sec, rate=rate,
|
||||
joc_binaural_mode=joc_binaural_mode)
|
||||
# 及时释放 Windows 文件句柄,允许 TemporaryDirectory 删除中间 raw。
|
||||
raw._mmap.close()
|
||||
return out_path
|
||||
|
||||
|
||||
class StreamingMaster:
|
||||
"""Incrementally write renderer frames into the final 25-channel ADM BWF.
|
||||
|
||||
This removes the default 16-channel float32 intermediate file. The mapping
|
||||
remains identical to :func:`assemble_from_raw`: bed channel 3 receives LFE,
|
||||
bed channels 0..2/4..9 are silent, and output objects 1..15 map to ADM
|
||||
channels 10..24.
|
||||
"""
|
||||
|
||||
def __init__(self, out_path, duration_sec, rate=48000, block_samples=131072,
|
||||
joc_binaural_mode=4):
|
||||
if block_samples < 1536:
|
||||
raise ValueError("block_samples must be at least one E-AC-3 frame")
|
||||
self.out_path = os.fspath(out_path)
|
||||
self.duration_sec = float(duration_sec)
|
||||
self.rate = int(rate)
|
||||
self.joc_binaural_mode = joc_binaural_mode
|
||||
self._sink = adm_atmos.Sink25(self.out_path, 25, self.rate)
|
||||
self._buffer = np.empty((int(block_samples), 25), dtype=np.float32)
|
||||
self._used = 0
|
||||
self._finalized = False
|
||||
|
||||
def _flush(self):
|
||||
if self._used:
|
||||
self._sink.write_block(self._buffer[:self._used])
|
||||
self._used = 0
|
||||
|
||||
def write_frame(self, pcm16):
|
||||
pcm = np.asarray(pcm16, dtype=np.float32)
|
||||
if pcm.shape != (16, 1536):
|
||||
raise ValueError(f"renderer frame must be (16,1536), got {pcm.shape}")
|
||||
source = 0
|
||||
while source < 1536:
|
||||
available = len(self._buffer) - self._used
|
||||
count = min(available, 1536 - source)
|
||||
target = self._buffer[self._used:self._used + count]
|
||||
target.fill(0.0)
|
||||
target[:, 3] = pcm[0, source:source + count]
|
||||
target[:, 10:25] = pcm[1:16, source:source + count].T
|
||||
self._used += count
|
||||
source += count
|
||||
if self._used == len(self._buffer):
|
||||
self._flush()
|
||||
|
||||
def finalize(self, kf_tracks):
|
||||
if self._finalized:
|
||||
raise RuntimeError("StreamingMaster already finalized")
|
||||
self._flush()
|
||||
try:
|
||||
from . import adm_serializer
|
||||
except ImportError:
|
||||
import adm_serializer
|
||||
axml = adm_serializer.build_axml(kf_tracks, self.duration_sec)
|
||||
chna = adm_atmos.build_chna()
|
||||
dbmd = adm_atmos.build_dbmd(
|
||||
25, joc_binaural_mode=self.joc_binaural_mode)
|
||||
trajectory_blocks = sum(len(track[1]) for track in kf_tracks)
|
||||
self.metadata_info = {
|
||||
"axml_bytes": len(axml),
|
||||
"trajectory_blocks": trajectory_blocks,
|
||||
"chna_bytes": len(chna),
|
||||
"dbmd_bytes": len(dbmd),
|
||||
}
|
||||
self._sink.finalize(axml, chna, dbmd)
|
||||
self._finalized = True
|
||||
print(f"master25 -> {self.out_path} ({self.duration_sec:.2f}s, 25ch, "
|
||||
f"axml={len(axml)}B, chna={len(chna)}B, dbmd={len(dbmd)}B)")
|
||||
return self.out_path
|
||||
|
||||
def abort(self):
|
||||
if self._finalized:
|
||||
return
|
||||
sink = getattr(self, "_sink", None)
|
||||
fp = getattr(sink, "fp", None)
|
||||
if fp is not None and not fp.closed:
|
||||
fp.close()
|
||||
|
||||
def __del__(self):
|
||||
try:
|
||||
self.abort()
|
||||
except Exception:
|
||||
pass
|
||||
@@ -0,0 +1,306 @@
|
||||
"""生成 25 声道 RF64 ADM BWF 及其 axml、chna、dbmd 元数据。
|
||||
|
||||
输出由 10 声道 7.1.2 bed 和 15 路对象组成;RF64 尺寸字段在写入完成后回填。
|
||||
"""
|
||||
import operator
|
||||
import struct
|
||||
import numpy as np
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
NS = "urn:ebu:metadata-schema:ebuCore_2016"
|
||||
XSI = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
||||
BED_NAMES = ["RoomCentricLeft", "RoomCentricRight", "RoomCentricCenter",
|
||||
"RoomCentricLFE", "RoomCentricLeftSideSurround",
|
||||
"RoomCentricRightSideSurround", "RoomCentricLeftRearSurround",
|
||||
"RoomCentricRightRearSurround", "RoomCentricLeftTopSurround",
|
||||
"RoomCentricRightTopSurround"]
|
||||
BED_LABELS = ["RC_L", "RC_R", "RC_C", "RC_LFE", "RC_Lss", "RC_Rss",
|
||||
"RC_Lrs", "RC_Rrs", "RC_Lts", "RC_Rts"]
|
||||
BED_POS = [(-1.0, 1.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
|
||||
(-1.0, 1.0, -1.0), (-1.0, 0.0, 0.0), (1.0, 0.0, 0.0),
|
||||
(-1.0, -1.0, 0.0), (1.0, -1.0, 0.0), (-1.0, 0.0, 1.0), (1.0, 0.0, 1.0)]
|
||||
|
||||
N_OBJ = 15
|
||||
JOC_BINAURAL_MODES = {
|
||||
"off": 0,
|
||||
"near": 1,
|
||||
"far": 2,
|
||||
"mid": 3,
|
||||
"unspecified": 4,
|
||||
}
|
||||
JOC_BINAURAL_MODE_DEFAULT = "unspecified"
|
||||
|
||||
def q_to_adm_xyz(q1, q2, q3):
|
||||
posX = min(1.0, round(q1 * 62 / 32767.0) / 62.0)
|
||||
posY = min(1.0, round(q2 * 62 / 32767.0) / 62.0)
|
||||
posZ = round(q3 * 15 / 32767.0) / 15.0
|
||||
posZ = max(-1.0, min(1.0, posZ))
|
||||
return posX * 2 - 1, 1 - posY * 2, posZ
|
||||
|
||||
def ts(seconds):
|
||||
s = int(seconds)
|
||||
frac = int(round((seconds - s) * 100000))
|
||||
if frac >= 100000:
|
||||
s += 1; frac = 0
|
||||
return f"{s // 3600:02d}:{s % 3600 // 60:02d}:{s % 60:02d}.{frac:05d}"
|
||||
|
||||
def sub(parent, tag, attrib=None, text=None):
|
||||
e = ET.SubElement(parent, tag)
|
||||
if attrib:
|
||||
for k, v in attrib.items():
|
||||
e.set(k, v)
|
||||
if text is not None:
|
||||
e.text = text
|
||||
return e
|
||||
|
||||
def add_refs(parent, tag, ids):
|
||||
for i in ids:
|
||||
sub(parent, tag, text=i)
|
||||
|
||||
def obj_block(cf, bid, t, x, y, z, dur, interpolation=0.0):
|
||||
b = sub(cf, "audioBlockFormat", {
|
||||
"audioBlockFormatID": bid, "rtime": ts(t), "duration": ts(dur)})
|
||||
sub(b, "cartesian", text="1")
|
||||
for c, v in (("X", x), ("Y", y), ("Z", z)):
|
||||
if c == "Z" and v == 0:
|
||||
continue
|
||||
p = sub(b, "position", {"coordinate": c})
|
||||
p.text = f"{v:.10f}"
|
||||
sub(b, "jumpPosition", {"interpolationLength": f"{interpolation:.5f}"}, text="1")
|
||||
|
||||
def build_axml(obj_tracks, duration_sec):
|
||||
adm = ET.Element("ebuCoreMain", {
|
||||
"xmlns": NS, "xmlns:xsi": XSI,
|
||||
"xsi:schemaLocation": f"{NS} ebucore.xsd", "lang": "en"})
|
||||
core = sub(adm, "coreMetadata")
|
||||
fmt = sub(core, "format")
|
||||
af = sub(fmt, "audioFormatExtended")
|
||||
|
||||
prog = sub(af, "audioProgramme", {
|
||||
"audioProgrammeID": "APR_1001", "audioProgrammeName": "EAC3JOC_Export",
|
||||
"start": ts(0), "end": ts(duration_sec)})
|
||||
add_refs(prog, "audioContentIDRef", ("ACO_1001", "ACO_1002"))
|
||||
bc = sub(af, "audioContent", {"audioContentID": "ACO_1001",
|
||||
"audioContentName": "EAC3JOC_Master_Content"})
|
||||
add_refs(bc, "audioObjectIDRef", ["AO_1001"])
|
||||
sub(bc, "dialogue", {"mixedContentKind": "0"})
|
||||
oc = sub(af, "audioContent", {"audioContentID": "ACO_1002",
|
||||
"audioContentName": "Objects"})
|
||||
add_refs(oc, "audioObjectIDRef", ["AO_%04x" % (0x100b + i) for i in range(N_OBJ)])
|
||||
sub(oc, "dialogue", {"mixedContentKind": "0"})
|
||||
|
||||
bed_o = sub(af, "audioObject", {"audioObjectID": "AO_1001", "audioObjectName": "Bed",
|
||||
"start": ts(0), "duration": ts(duration_sec)})
|
||||
sub(bed_o, "audioPackFormatIDRef", text="AP_00011001")
|
||||
add_refs(bed_o, "audioTrackUIDRef", ["ATU_%08x" % (i + 1) for i in range(10)])
|
||||
for i in range(N_OBJ):
|
||||
o = sub(af, "audioObject", {"audioObjectID": "AO_%04x" % (0x100b + i),
|
||||
"audioObjectName": f"Audio Object {i+1}",
|
||||
"start": ts(0), "duration": ts(duration_sec)})
|
||||
sub(o, "audioPackFormatIDRef", text="AP_0003%04x" % (0x1001 + i))
|
||||
add_refs(o, "audioTrackUIDRef", ["ATU_%08x" % (i + 11)])
|
||||
|
||||
bp = sub(af, "audioPackFormat", {"audioPackFormatID": "AP_00011001",
|
||||
"audioPackFormatName": "EAC3JOCBedPack",
|
||||
"typeDefinition": "DirectSpeakers", "typeLabel": "0001"})
|
||||
add_refs(bp, "audioChannelFormatIDRef", ["AC_0001%04x" % (0x1001 + i) for i in range(10)])
|
||||
for i in range(N_OBJ):
|
||||
pk = sub(af, "audioPackFormat", {"audioPackFormatID": "AP_0003%04x" % (0x1001 + i),
|
||||
"audioPackFormatName": f"JOC_Object_{i+1}",
|
||||
"typeDefinition": "Objects", "typeLabel": "0003"})
|
||||
add_refs(pk, "audioChannelFormatIDRef", ["AC_0003%04x" % (0x1001 + i)])
|
||||
|
||||
for i in range(10):
|
||||
cf = sub(af, "audioChannelFormat", {"audioChannelFormatID": "AC_0001%04x" % (0x1001 + i),
|
||||
"audioChannelFormatName": BED_NAMES[i],
|
||||
"typeDefinition": "DirectSpeakers", "typeLabel": "0001"})
|
||||
b = sub(cf, "audioBlockFormat", {"audioBlockFormatID": "AB_0001%04x_00000001" % (0x1001 + i)})
|
||||
sub(b, "cartesian", text="1")
|
||||
x, y, z = BED_POS[i]
|
||||
for c, v in (("X", x), ("Y", y), ("Z", z)):
|
||||
if c == "Z" and v == 0:
|
||||
continue
|
||||
p = sub(b, "position", {"coordinate": c})
|
||||
p.text = f"{v:.10f}"
|
||||
sub(b, "speakerLabel", text=BED_LABELS[i])
|
||||
|
||||
for i, (oname, kfs) in enumerate(obj_tracks):
|
||||
cf = sub(af, "audioChannelFormat", {"audioChannelFormatID": "AC_0003%04x" % (0x1001 + i),
|
||||
"audioChannelFormatName": oname,
|
||||
"typeDefinition": "Objects", "typeLabel": "0003"})
|
||||
for k, keyframe in enumerate(kfs):
|
||||
t, x, y, z, dur = keyframe[:5]
|
||||
interpolation = keyframe[5] if len(keyframe) > 5 else 0.0
|
||||
obj_block(cf, "AB_0003%04x_%08x" % (0x1001 + i, k + 1),
|
||||
t, x, y, z, dur, interpolation)
|
||||
|
||||
for i in range(10):
|
||||
t = sub(af, "audioTrackUID", {"UID": "ATU_%08x" % (i + 1),
|
||||
"bitDepth": "24", "sampleRate": "48000"})
|
||||
sub(t, "audioTrackFormatIDRef", text="AT_0001%04x_01" % (0x1001 + i))
|
||||
sub(t, "audioPackFormatIDRef", text="AP_00011001")
|
||||
for i in range(N_OBJ):
|
||||
t = sub(af, "audioTrackUID", {"UID": "ATU_%08x" % (i + 11),
|
||||
"bitDepth": "24", "sampleRate": "48000"})
|
||||
sub(t, "audioTrackFormatIDRef", text="AT_0003%04x_01" % (0x1001 + i))
|
||||
sub(t, "audioPackFormatIDRef", text="AP_0003%04x" % (0x1001 + i))
|
||||
|
||||
for i in range(10):
|
||||
tf = sub(af, "audioTrackFormat", {"audioTrackFormatID": "AT_0001%04x_01" % (0x1001 + i),
|
||||
"audioTrackFormatName": "PCM_" + BED_NAMES[i],
|
||||
"formatDefinition": "PCM", "formatLabel": "0001"})
|
||||
sub(tf, "audioStreamFormatIDRef", text="AS_0001%04x" % (0x1001 + i))
|
||||
for i in range(N_OBJ):
|
||||
tf = sub(af, "audioTrackFormat", {"audioTrackFormatID": "AT_0003%04x_01" % (0x1001 + i),
|
||||
"audioTrackFormatName": "PCM_JOC_Object_%d" % (i + 1),
|
||||
"formatDefinition": "PCM", "formatLabel": "0001"})
|
||||
sub(tf, "audioStreamFormatIDRef", text="AS_0003%04x" % (0x1001 + i))
|
||||
|
||||
for i in range(10):
|
||||
sf = sub(af, "audioStreamFormat", {"audioStreamFormatID": "AS_0001%04x" % (0x1001 + i),
|
||||
"audioStreamFormatName": "PCM_" + BED_NAMES[i],
|
||||
"formatDefinition": "PCM", "formatLabel": "0001"})
|
||||
sub(sf, "audioChannelFormatIDRef", text="AC_0001%04x" % (0x1001 + i))
|
||||
sub(sf, "audioPackFormatIDRef", text="AP_00011001")
|
||||
sub(sf, "audioTrackFormatIDRef", text="AT_0001%04x_01" % (0x1001 + i))
|
||||
for i in range(N_OBJ):
|
||||
sf = sub(af, "audioStreamFormat", {"audioStreamFormatID": "AS_0003%04x" % (0x1001 + i),
|
||||
"audioStreamFormatName": "PCM_JOC_Object_%d" % (i + 1),
|
||||
"formatDefinition": "PCM", "formatLabel": "0001"})
|
||||
sub(sf, "audioChannelFormatIDRef", text="AC_0003%04x" % (0x1001 + i))
|
||||
sub(sf, "audioPackFormatIDRef", text="AP_0003%04x" % (0x1001 + i))
|
||||
sub(sf, "audioTrackFormatIDRef", text="AT_0003%04x_01" % (0x1001 + i))
|
||||
|
||||
return ET.tostring(adm, encoding="utf-8", xml_declaration=True)
|
||||
|
||||
def build_chna():
|
||||
out = bytearray()
|
||||
out += struct.pack("<HH", 25, 25)
|
||||
for i in range(10):
|
||||
out += struct.pack("<H", i + 1)
|
||||
out += ("ATU_%08x" % (i + 1)).encode()
|
||||
out += ("AT_0001%04x_01" % (0x1001 + i)).encode()
|
||||
out += b"AP_00011001" + b"\x00"
|
||||
for i in range(N_OBJ):
|
||||
out += struct.pack("<H", i + 11)
|
||||
out += ("ATU_%08x" % (i + 11)).encode()
|
||||
out += ("AT_0003%04x_01" % (0x1001 + i)).encode()
|
||||
out += ("AP_0003%04x" % (0x1001 + i)).encode() + b"\x00"
|
||||
return bytes(out)
|
||||
|
||||
def _checksum(seg):
|
||||
s = len(seg)
|
||||
for b in seg:
|
||||
s += b
|
||||
return (~s + 1) & 0xFF
|
||||
|
||||
def build_dbmd(object_count=25, joc_binaural_mode=4):
|
||||
"""仅覆盖 segment 10 中 JOC object slots 10..24 的 mode 低 3 bit。"""
|
||||
mode = operator.index(joc_binaural_mode)
|
||||
if mode not in JOC_BINAURAL_MODES.values():
|
||||
raise ValueError(f"invalid JOC binaural render mode: {mode}")
|
||||
out = bytearray(struct.pack("<I", 0x01000006))
|
||||
dd = bytearray(96)
|
||||
dd[1] = 0x47
|
||||
dd[5] = 0x60
|
||||
dd[8] = 0x24; dd[9] = 0x24
|
||||
out.append(7); out += struct.pack("<H", 96); out += bytes(dd)
|
||||
out.append(_checksum(dd))
|
||||
at = bytearray(248)
|
||||
c0 = b"JustOneCacophony"; c1 = b"JustOneCacophony"
|
||||
at[0:len(c0)] = c0
|
||||
at[32:32 + len(c1)] = c1
|
||||
at[96], at[97], at[98] = 2, 1, 0
|
||||
at[103] = 0x03
|
||||
at[106] = 0x01
|
||||
at[111] = 0x22; at[112] = 0xFF
|
||||
out.append(9); out += struct.pack("<H", 248); out += bytes(at)
|
||||
out.append(_checksum(at))
|
||||
ob = bytearray(5 + 262 + object_count)
|
||||
ob[0:4] = struct.pack("<I", 0xF8726FBD)
|
||||
ob[4] = object_count
|
||||
for i in range(5 + 262, len(ob)):
|
||||
ob[i] = 0x84
|
||||
# sync (4), count (2), reserved (1), nine 15-byte config trims,
|
||||
# then one trim-bypass byte per track before the headphone modes.
|
||||
# Preserve the existing template's bed fields and trailing bytes.
|
||||
# The nine configuration trims start at byte 7; the first byte of each
|
||||
# carries the auto-trim flag in bit 0 (1 = automatic, 0 = manual). The
|
||||
# default is automatic, so a freshly rendered master opens with automatic
|
||||
# trims in the renderer.
|
||||
for layout in range(9):
|
||||
ob[7 + 15 * layout] |= 1
|
||||
object_modes = 4 + 2 + 1 + 9 * 15 + object_count
|
||||
for i in range(10, min(object_count, 10 + N_OBJ)):
|
||||
ob[object_modes + i] = (ob[object_modes + i] & 0xF8) | mode
|
||||
out.append(10); out += struct.pack("<H", len(ob)); out += bytes(ob)
|
||||
out.append(_checksum(ob))
|
||||
out += b"\x00\x00"
|
||||
return bytes(out)
|
||||
|
||||
class Sink25:
|
||||
"""RF64 ADM BWF writer.
|
||||
|
||||
Header layout is fixed so that sizes can be patched without rereading the
|
||||
file: RF64+size+WAVE (12) + ds64 chunk (8+28) + fmt chunk (8+16) + data
|
||||
chunk header (8). Sizes beyond 32 bits follow the RF64 convention: the
|
||||
chunk size field holds 0xFFFFFFFF and the true value lives in ds64.
|
||||
"""
|
||||
_DS64_BODY_OFFSET = 20
|
||||
_DATA_SIZE_OFFSET = 76
|
||||
|
||||
def __init__(self, path, channels, rate):
|
||||
self.ch = channels; self.rate = rate; self.frames = 0
|
||||
self.fp = open(path, "wb+")
|
||||
self.fp.write(b"RF64" + struct.pack("<I", 0xFFFFFFFF) + b"WAVE")
|
||||
self._chunk(b"ds64", b"\x00" * 28)
|
||||
self._chunk(b"fmt ", self._fmt())
|
||||
self._chunk(b"data", b"")
|
||||
def _chunk(self, cid, body):
|
||||
self.fp.write(cid + struct.pack("<I", len(body)) + body)
|
||||
if len(body) & 1:
|
||||
self.fp.write(b"\x00")
|
||||
def _fmt(self):
|
||||
return struct.pack("<HHIIHH", 1, self.ch, self.rate,
|
||||
self.rate * self.ch * 3, self.ch * 3, 24)
|
||||
def write_block(self, arr):
|
||||
arr = arr.reshape(-1, self.ch)
|
||||
i24 = (np.clip(arr, -1.0, 1.0) * 8388607.0).astype(np.int32)
|
||||
self.fp.write(i24.view(np.uint8).reshape(-1, 4)[:, :3].tobytes())
|
||||
self.frames += arr.shape[0]
|
||||
def finalize(self, axml_bytes, chna_bytes, dbmd_bytes):
|
||||
data_len = self.frames * self.ch * 3
|
||||
self._chunk(b"axml", axml_bytes)
|
||||
self._chunk(b"chna", chna_bytes)
|
||||
self._chunk(b"dbmd", dbmd_bytes)
|
||||
self.fp.seek(0, 2); total = self.fp.tell()
|
||||
# RF64: 超过 32-bit 的 chunk size 字段写 0xFFFFFFFF,真实大小回填 ds64。
|
||||
self.fp.seek(self._DATA_SIZE_OFFSET)
|
||||
self.fp.write(struct.pack(
|
||||
"<I", data_len if data_len <= 0xFFFFFFFF else 0xFFFFFFFF))
|
||||
self.fp.seek(self._DS64_BODY_OFFSET)
|
||||
self.fp.write(struct.pack("<QQQI", total - 8, data_len, self.frames, 0))
|
||||
self.fp.flush()
|
||||
self.fp.close()
|
||||
|
||||
def build_master(out_path, bed_mm, obj_mm, kf_tracks, duration_sec, rate=48000,
|
||||
block=480000, joc_binaural_mode=4):
|
||||
n = min(bed_mm.shape[0], obj_mm.shape[0])
|
||||
try:
|
||||
from . import adm_serializer
|
||||
except ImportError:
|
||||
import adm_serializer
|
||||
serial_axml = adm_serializer.build_axml
|
||||
axml = serial_axml(kf_tracks, duration_sec)
|
||||
chna = build_chna()
|
||||
dbmd = build_dbmd(25, joc_binaural_mode=joc_binaural_mode)
|
||||
sink = Sink25(out_path, 25, rate)
|
||||
for st in range(0, n, block):
|
||||
en = min(n, st + block)
|
||||
blk = np.hstack((np.asarray(bed_mm[st:en], dtype=np.float32),
|
||||
np.asarray(obj_mm[st:en, 1:16], dtype=np.float32)))
|
||||
sink.write_block(blk)
|
||||
sink.finalize(axml, chna, dbmd)
|
||||
print(f"master25 -> {out_path} ({duration_sec:.2f}s, 25ch, axml={len(axml)}B, "
|
||||
f"chna={len(chna)}B, dbmd={len(dbmd)}B)")
|
||||
@@ -0,0 +1,136 @@
|
||||
"""把 7.1.2 bed、15 个对象及其位置轨迹序列化为 ADM axml。
|
||||
|
||||
序列化结果采用固定元素顺序、属性顺序和十六进制 ADM 标识符,便于稳定输出和校验。
|
||||
"""
|
||||
BED_NAMES = ["RoomCentricLeft", "RoomCentricRight", "RoomCentricCenter", "RoomCentricLFE",
|
||||
"RoomCentricLeftSideSurround", "RoomCentricRightSideSurround",
|
||||
"RoomCentricLeftRearSurround", "RoomCentricRightRearSurround",
|
||||
"RoomCentricLeftTopSurround", "RoomCentricRightTopSurround"]
|
||||
BED_LABELS = ["RC_L", "RC_R", "RC_C", "RC_LFE", "RC_Lss", "RC_Rss",
|
||||
"RC_Lrs", "RC_Rrs", "RC_Lts", "RC_Rts"]
|
||||
BED_POS = [(-1.0, 1.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0), (-1.0, 1.0, -1.0),
|
||||
(-1.0, 0.0, 0.0), (1.0, 0.0, 0.0), (-1.0, -1.0, 0.0), (1.0, -1.0, 0.0),
|
||||
(-1.0, 0.0, 1.0), (1.0, 0.0, 1.0)]
|
||||
N_OBJ = 15
|
||||
|
||||
def ts(seconds):
|
||||
s = int(seconds)
|
||||
frac = int(round((seconds - s) * 100000))
|
||||
if frac >= 100000:
|
||||
s += 1; frac = 0
|
||||
return f"{s // 3600:02d}:{s % 3600 // 60:02d}:{s % 60:02d}.{frac:05d}"
|
||||
|
||||
def esc(v):
|
||||
return (str(v).replace("&", "&").replace("<", "<").replace(">", ">"))
|
||||
|
||||
def build_axml(obj_tracks, duration_sec):
|
||||
"""obj_tracks: [(name, [(rtime, x, y, z, dur), ...]) ×15]"""
|
||||
w = []
|
||||
a = w.append
|
||||
a('<?xml version="1.0" encoding="utf-8"?>')
|
||||
a('<ebuCoreMain xsi:schemaLocation="urn:ebu:metadata-schema:ebuCore_2016 ebucore.xsd" '
|
||||
'lang="en" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
|
||||
'xmlns="urn:ebu:metadata-schema:ebuCore_2016">')
|
||||
a('<coreMetadata><format><audioFormatExtended>')
|
||||
a(f'<audioProgramme audioProgrammeID="APR_1001" audioProgrammeName="EAC3JOC_Export" '
|
||||
f'start="{ts(0)}" end="{ts(duration_sec)}">')
|
||||
a('<audioContentIDRef>ACO_1001</audioContentIDRef>')
|
||||
a('<audioContentIDRef>ACO_1002</audioContentIDRef>')
|
||||
a('</audioProgramme>')
|
||||
a('<audioContent audioContentID="ACO_1001" audioContentName="EAC3JOC_Master_Content">')
|
||||
a('<audioObjectIDRef>AO_1001</audioObjectIDRef>')
|
||||
a('<dialogue mixedContentKind="0">2</dialogue>')
|
||||
a('</audioContent>')
|
||||
a('<audioContent audioContentID="ACO_1002" audioContentName="Objects">')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioObjectIDRef>AO_{0x100b + i:04x}</audioObjectIDRef>')
|
||||
a('<dialogue mixedContentKind="0">2</dialogue>')
|
||||
a('</audioContent>')
|
||||
a(f'<audioObject audioObjectID="AO_1001" audioObjectName="Bed" '
|
||||
f'start="{ts(0)}" duration="{ts(duration_sec)}">')
|
||||
a('<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>')
|
||||
for i in range(10):
|
||||
a(f'<audioTrackUIDRef>ATU_{i + 1:08x}</audioTrackUIDRef>')
|
||||
a('</audioObject>')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioObject audioObjectID="AO_{0x100b + i:04x}" audioObjectName="Audio Object {i+1}" '
|
||||
f'start="{ts(0)}" duration="{ts(duration_sec)}">')
|
||||
a(f'<audioPackFormatIDRef>AP_0003{0x1001 + i:04x}</audioPackFormatIDRef>')
|
||||
a(f'<audioTrackUIDRef>ATU_{11 + i:08x}</audioTrackUIDRef>')
|
||||
a('</audioObject>')
|
||||
a('<audioPackFormat audioPackFormatID="AP_00011001" audioPackFormatName="EAC3JOCBedPack" '
|
||||
'typeDefinition="DirectSpeakers" typeLabel="0001">')
|
||||
for i in range(10):
|
||||
a(f'<audioChannelFormatIDRef>AC_0001{0x1001 + i:04x}</audioChannelFormatIDRef>')
|
||||
a('</audioPackFormat>')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioPackFormat audioPackFormatID="AP_0003{0x1001 + i:04x}" '
|
||||
f'audioPackFormatName="JOC_Object_{i+1}" typeDefinition="Objects" typeLabel="0003">')
|
||||
a(f'<audioChannelFormatIDRef>AC_0003{0x1001 + i:04x}</audioChannelFormatIDRef>')
|
||||
a('</audioPackFormat>')
|
||||
for i in range(10):
|
||||
a(f'<audioChannelFormat audioChannelFormatID="AC_0001{0x1001 + i:04x}" '
|
||||
f'audioChannelFormatName="{BED_NAMES[i]}" typeDefinition="DirectSpeakers" typeLabel="0001">')
|
||||
a(f'<audioBlockFormat audioBlockFormatID="AB_0001{0x1001 + i:04x}_00000001">')
|
||||
a('<cartesian>1</cartesian>')
|
||||
x, y, z = BED_POS[i]
|
||||
a(f'<position coordinate="X">{x:.10f}</position>')
|
||||
a(f'<position coordinate="Y">{y:.10f}</position>')
|
||||
if z != 0:
|
||||
a(f'<position coordinate="Z">{z:.10f}</position>')
|
||||
a(f'<speakerLabel>{BED_LABELS[i]}</speakerLabel>')
|
||||
a('</audioBlockFormat>')
|
||||
a('</audioChannelFormat>')
|
||||
for i, (oname, kfs) in enumerate(obj_tracks):
|
||||
a(f'<audioChannelFormat audioChannelFormatID="AC_0003{0x1001 + i:04x}" '
|
||||
f'audioChannelFormatName="{oname}" typeDefinition="Objects" typeLabel="0003">')
|
||||
for k, keyframe in enumerate(kfs):
|
||||
t, x, y, z, dur = keyframe[:5]
|
||||
interpolation = keyframe[5] if len(keyframe) > 5 else 0.0
|
||||
a(f'<audioBlockFormat audioBlockFormatID="AB_0003{0x1001 + i:04x}_{k + 1:08x}" '
|
||||
f'rtime="{ts(t)}" duration="{ts(dur)}">')
|
||||
a('<cartesian>1</cartesian>')
|
||||
a(f'<position coordinate="X">{x:.10f}</position>')
|
||||
a(f'<position coordinate="Y">{y:.10f}</position>')
|
||||
if z != 0:
|
||||
a(f'<position coordinate="Z">{z:.10f}</position>')
|
||||
a(f'<jumpPosition interpolationLength="{interpolation:.5f}">1</jumpPosition>')
|
||||
a('</audioBlockFormat>')
|
||||
a('</audioChannelFormat>')
|
||||
for i in range(10):
|
||||
a(f'<audioTrackUID UID="ATU_{i + 1:08x}" bitDepth="24" sampleRate="48000">')
|
||||
a(f'<audioTrackFormatIDRef>AT_0001{0x1001 + i:04x}_01</audioTrackFormatIDRef>')
|
||||
a('<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>')
|
||||
a('</audioTrackUID>')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioTrackUID UID="ATU_{11 + i:08x}" bitDepth="24" sampleRate="48000">')
|
||||
a(f'<audioTrackFormatIDRef>AT_0003{0x1001 + i:04x}_01</audioTrackFormatIDRef>')
|
||||
a(f'<audioPackFormatIDRef>AP_0003{0x1001 + i:04x}</audioPackFormatIDRef>')
|
||||
a('</audioTrackUID>')
|
||||
for i in range(10):
|
||||
a(f'<audioTrackFormat audioTrackFormatID="AT_0001{0x1001 + i:04x}_01" '
|
||||
f'audioTrackFormatName="PCM_{BED_NAMES[i]}" formatDefinition="PCM" formatLabel="0001">')
|
||||
a(f'<audioStreamFormatIDRef>AS_0001{0x1001 + i:04x}</audioStreamFormatIDRef>')
|
||||
a('</audioTrackFormat>')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioTrackFormat audioTrackFormatID="AT_0003{0x1001 + i:04x}_01" '
|
||||
f'audioTrackFormatName="PCM_JOC_Object_{i+1}" formatDefinition="PCM" formatLabel="0001">')
|
||||
a(f'<audioStreamFormatIDRef>AS_0003{0x1001 + i:04x}</audioStreamFormatIDRef>')
|
||||
a('</audioTrackFormat>')
|
||||
for i in range(10):
|
||||
a(f'<audioStreamFormat audioStreamFormatID="AS_0001{0x1001 + i:04x}" '
|
||||
f'audioStreamFormatName="PCM_{BED_NAMES[i]}" formatDefinition="PCM" formatLabel="0001">')
|
||||
a(f'<audioChannelFormatIDRef>AC_0001{0x1001 + i:04x}</audioChannelFormatIDRef>')
|
||||
a('<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>')
|
||||
a(f'<audioTrackFormatIDRef>AT_0001{0x1001 + i:04x}_01</audioTrackFormatIDRef>')
|
||||
a('</audioStreamFormat>')
|
||||
for i in range(N_OBJ):
|
||||
a(f'<audioStreamFormat audioStreamFormatID="AS_0003{0x1001 + i:04x}" '
|
||||
f'audioStreamFormatName="PCM_JOC_Object_{i+1}" formatDefinition="PCM" formatLabel="0001">')
|
||||
a(f'<audioChannelFormatIDRef>AC_0003{0x1001 + i:04x}</audioChannelFormatIDRef>')
|
||||
a(f'<audioPackFormatIDRef>AP_0003{0x1001 + i:04x}</audioPackFormatIDRef>')
|
||||
a(f'<audioTrackFormatIDRef>AT_0003{0x1001 + i:04x}_01</audioTrackFormatIDRef>')
|
||||
a('</audioStreamFormat>')
|
||||
a('</audioFormatExtended></format></coreMetadata>')
|
||||
a('</ebuCoreMain>')
|
||||
return ''.join(w).encode('utf-8')
|
||||
@@ -0,0 +1,199 @@
|
||||
"""校验 ADM BWF 的 RF64、通道、axml、chna、dbmd 和对象引用结构。
|
||||
|
||||
用法:``python src/adm_validate.py <file.wav> [more.wav ...]``,全部通过时退出码为 0。
|
||||
"""
|
||||
import struct, sys, re, os
|
||||
|
||||
def fail(msgs, m): msgs.append(m)
|
||||
|
||||
def walk_chunks(path):
|
||||
chunks, ds64 = [], {}
|
||||
with open(path, "rb") as f:
|
||||
riff = f.read(4); f.read(4); wave = f.read(4)
|
||||
if riff not in (b"RIFF", b"RF64"):
|
||||
return None, None, f"File does not have a 'RIFF' or 'RF64' chunk"
|
||||
if wave != b"WAVE":
|
||||
return None, None, "File does not have a required 'WAVE' chunk"
|
||||
while True:
|
||||
off = f.tell()
|
||||
cid = f.read(4)
|
||||
if len(cid) < 4: break
|
||||
sz = struct.unpack("<I", f.read(4))[0]
|
||||
if cid == b"ds64":
|
||||
body = f.read(sz + (sz & 1))
|
||||
riff64, data64, sample64, _ = struct.unpack("<QQQI", body[:28])
|
||||
ds64 = dict(riff64=riff64, data64=data64, sample64=sample64)
|
||||
chunks.append(("ds64", off, sz)); continue
|
||||
chunks.append((cid.decode("latin1"), off, sz))
|
||||
eff = ds64.get("data64", sz) if (sz == 0xFFFFFFFF and cid == b"data") else sz
|
||||
f.seek(off + 8 + eff + (eff & 1))
|
||||
return chunks, ds64, None
|
||||
|
||||
def read_body(path, chunks, cid):
|
||||
for c, off, sz in chunks:
|
||||
if c == cid:
|
||||
with open(path, "rb") as f:
|
||||
f.seek(off + 8)
|
||||
return f.read(sz)
|
||||
return None
|
||||
|
||||
def parse_chna(body):
|
||||
n_track, n_uid = struct.unpack("<HH", body[:4])
|
||||
rows, p = [], 4
|
||||
while p + 40 <= len(body):
|
||||
trk = struct.unpack("<H", body[p:p+2])[0]
|
||||
uid = body[p+2:p+14].rstrip(b"\x00").decode()
|
||||
tf = body[p+14:p+28].rstrip(b"\x00").decode()
|
||||
pk = body[p+28:p+40].rstrip(b"\x00").decode()
|
||||
rows.append((trk, uid, tf, pk)); p += 40
|
||||
return n_track, n_uid, rows
|
||||
|
||||
def decode_channel_input(ao_id):
|
||||
"""将 ``AO_xxxx`` 的十六进制标识符解码为低 12 位通道输入号。"""
|
||||
m = re.fullmatch(r"AO_([0-9a-fA-F]+)", ao_id)
|
||||
if not m:
|
||||
return None
|
||||
v = int(m.group(1), 16)
|
||||
if v > 0x1FFF: # 超过 12 位通道域
|
||||
return None
|
||||
return v & 0x0FFF
|
||||
|
||||
def ts_sec(s):
|
||||
h, m, rest = s.split(":")
|
||||
return int(h) * 3600 + int(m) * 60 + float(rest)
|
||||
|
||||
def validate(path, axml_override=None, chna_override=None):
|
||||
msgs = []
|
||||
chunks, ds64, err = walk_chunks(path)
|
||||
if err:
|
||||
return [err]
|
||||
have = {c for c, _, _ in chunks}
|
||||
for need in ("fmt ", "data", "axml", "chna", "dbmd"):
|
||||
if need not in have:
|
||||
fail(msgs, f"File does not have a required '{need.strip()}' chunk")
|
||||
if msgs:
|
||||
return msgs
|
||||
fmt = read_body(path, chunks, "fmt ")
|
||||
f_tag, f_ch, f_rate, _, _, f_bits = struct.unpack("<HHIIHH", fmt[:16])
|
||||
chna_body = chna_override if chna_override is not None else read_body(path, chunks, "chna")
|
||||
n_track, n_uid, rows = parse_chna(chna_body)
|
||||
if f_ch != n_track:
|
||||
fail(msgs, f"Mismatched number of audio channels and chna entries "
|
||||
f"(fmt={f_ch} chna={n_track})")
|
||||
ax_raw = axml_override if axml_override is not None else read_body(path, chunks, "axml")
|
||||
ax = ax_raw.decode("utf-8")
|
||||
|
||||
# --- audioObjectID 十六进制通道输入号解码 ---
|
||||
objs = re.findall(r'audioObjectID="(AO_[0-9a-zA-Z]+)"', ax)
|
||||
bed_ch, obj_ch = [], []
|
||||
for ao in objs:
|
||||
cid = decode_channel_input(ao)
|
||||
if cid is None:
|
||||
fail(msgs, f"Invalid ADM BWF XML format: cannot decode channel "
|
||||
f"input ID from AudioObjectID '{ao}'")
|
||||
continue
|
||||
if ao == "AO_1001":
|
||||
bed_ch.append(cid)
|
||||
else:
|
||||
if cid <= 10:
|
||||
fail(msgs, f"Source channel index should be greater than 10 "
|
||||
f"for objects ('{ao}' -> {cid})")
|
||||
obj_ch.append(cid)
|
||||
|
||||
# UID 十六进制 → 必须与 chna 表一致
|
||||
uid_map = {uid: trk for trk, uid, tf, pk in rows}
|
||||
for uid in re.findall(r'UID="(ATU_[0-9a-zA-Z]+)"', ax):
|
||||
if uid not in uid_map:
|
||||
fail(msgs, f"'{uid}' is not referenced in 'chna' chunk UID table")
|
||||
continue
|
||||
m = re.fullmatch(r"ATU_([0-9a-fA-F]+)", uid)
|
||||
if m:
|
||||
v = int(m.group(1), 16)
|
||||
if v > 128:
|
||||
fail(msgs, f"Channel index out of range (UID {uid} -> {v})")
|
||||
|
||||
# 轨数一致性:axml audioTrackUID 数 == fmt 声道数
|
||||
n_tu = len(re.findall(r"<audioTrackUID ", ax))
|
||||
if n_tu != f_ch:
|
||||
fail(msgs, f"Number of channels declared in ADM ({n_tu}) does not "
|
||||
f"match 'fmt ' chunk ({f_ch})")
|
||||
|
||||
# sampleRate / bitDepth 一致
|
||||
for sr in set(re.findall(r'sampleRate="(\d+)"', ax)):
|
||||
if int(sr) != f_rate:
|
||||
fail(msgs, f"Mismatched track sample rate between ADM and WAV ({sr} vs {f_rate})")
|
||||
for bd in set(re.findall(r'bitDepth="(\d+)"', ax)):
|
||||
if int(bd) != f_bits:
|
||||
fail(msgs, f"Mismatched track bit depth between ADM and WAV ({bd} vs {f_bits})")
|
||||
|
||||
# audioProgramme 唯一性 / audioContent ≥1
|
||||
if ax.count("<audioProgramme ") != 1:
|
||||
fail(msgs, "ADM has more than one audioProgramme object -- there must be only one"
|
||||
if ax.count("<audioProgramme ") > 1
|
||||
else "ADM does not have a required audioProgramme object")
|
||||
if "<audioContent " not in ax:
|
||||
fail(msgs, "audioProgramme object does not have a required audioContent object")
|
||||
|
||||
# 每个 channelFormat ≥1 blockFormat + 对象块链连续性
|
||||
cfs = re.findall(r'<audioChannelFormat [^>]*typeLabel="0003".*?</audioChannelFormat>', ax, re.S)
|
||||
object_block_formats = 0
|
||||
for seg in cfs:
|
||||
cf_id = re.search(r'audioChannelFormatID="([^"]+)"', seg).group(1)
|
||||
block_xml = re.findall(r'<audioBlockFormat [^>]*rtime="[^"]+".*?</audioBlockFormat>',
|
||||
seg, re.S)
|
||||
object_block_formats += len(block_xml)
|
||||
blocks = []
|
||||
for block_index, block in enumerate(block_xml, 1):
|
||||
timing = re.search(r'rtime="([^"]+)" duration="([^"]+)"', block)
|
||||
if timing is None:
|
||||
continue
|
||||
rtime, duration = timing.groups()
|
||||
blocks.append((rtime, duration))
|
||||
jump = re.search(
|
||||
r'<jumpPosition interpolationLength="([^"]+)">1</jumpPosition>', block)
|
||||
if jump is not None and float(jump.group(1)) > ts_sec(duration) + 1e-8:
|
||||
fail(msgs, f"Interpolation length exceeds duration in block format "
|
||||
f"{block_index} of {cf_id}: {jump.group(1)} > {duration}")
|
||||
if not blocks:
|
||||
fail(msgs, f"AudioChannelFormat {cf_id} is missing audioBlockFormat sub-element")
|
||||
continue
|
||||
for i in range(len(blocks) - 1):
|
||||
end_i = ts_sec(blocks[i][0]) + ts_sec(blocks[i][1])
|
||||
nxt = ts_sec(blocks[i + 1][0])
|
||||
if abs(end_i - nxt) > 2e-5:
|
||||
fail(msgs, f"Time gap between block format {i+1} and {i+2} of {cf_id}: "
|
||||
f"{end_i:.5f} vs {nxt:.5f}")
|
||||
return msgs, dict(fmt_ch=f_ch, fmt_rate=f_rate, fmt_bits=f_bits,
|
||||
chna=n_track, objects=len(obj_ch), bed=len(bed_ch),
|
||||
trackUIDs=n_tu, axml_bytes=len(ax_raw),
|
||||
audioBlockFormats=ax.count("<audioBlockFormat "),
|
||||
objectBlockFormats=object_block_formats)
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("files", nargs="+")
|
||||
ap.add_argument("--axml-file", default=None, help="用该文件内容替换 wav 内 axml(对照实验)")
|
||||
ap.add_argument("--chna-file", default=None, help="用该文件内容替换 wav 内 chna(对照实验)")
|
||||
a = ap.parse_args()
|
||||
ax_o = open(a.axml_file, "rb").read() if a.axml_file else None
|
||||
ch_o = open(a.chna_file, "rb").read() if a.chna_file else None
|
||||
rc = 0
|
||||
for p in a.files:
|
||||
r = validate(p, axml_override=ax_o, chna_override=ch_o)
|
||||
name = os.path.basename(p)
|
||||
if isinstance(r, list):
|
||||
msgs, info = r, {}
|
||||
else:
|
||||
msgs, info = r
|
||||
if msgs:
|
||||
rc = 1
|
||||
print(f"[FAIL] {name}")
|
||||
for m in msgs:
|
||||
print(" -", m)
|
||||
else:
|
||||
print(f"[PASS] {name} {info}")
|
||||
return rc
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -1,241 +0,0 @@
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "eac3_transport/eac3_reader.h"
|
||||
#include "emdf/emdf_parser.h"
|
||||
#include "foundation/status.h"
|
||||
#include "joc_bitstream/joc_parser.h"
|
||||
|
||||
namespace {
|
||||
|
||||
joc_error finish(const joc::Status& status) {
|
||||
joc::record_error_detail(status);
|
||||
return status.code();
|
||||
}
|
||||
|
||||
joc_error arg_fail(const char* message) {
|
||||
return finish(joc::Status::fail(JOC_ERR_INVALID_ARGUMENT, "core", message));
|
||||
}
|
||||
|
||||
void fill_emdf_info(const joc::emdf::Container& container, joc_emdf_info* out) {
|
||||
std::memset(out, 0, sizeof(*out));
|
||||
out->struct_size = sizeof(joc_emdf_info);
|
||||
out->struct_version = JOC_EMDF_INFO_VERSION;
|
||||
out->start_bit = static_cast<std::uint32_t>(container.start_bit);
|
||||
out->container_bytes = static_cast<std::uint32_t>(container.raw_size);
|
||||
out->payload_count = static_cast<std::uint32_t>(container.payload_count);
|
||||
for (std::size_t i = 0; i < container.payload_count; ++i) {
|
||||
out->payloads[i].id = container.payloads[i].id;
|
||||
out->payloads[i].sample_offset = container.payloads[i].sample_offset;
|
||||
out->payloads[i].bit_offset = static_cast<std::uint32_t>(container.payloads[i].bit_offset);
|
||||
out->payloads[i].size = static_cast<std::uint32_t>(container.payloads[i].size);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
std::uint32_t JOC_CALL joc_abi_version(void) { return JOC_ABI_VERSION; }
|
||||
|
||||
const char* JOC_CALL joc_version_string(void) { return JOC_VERSION_STRING; }
|
||||
|
||||
std::uint32_t JOC_CALL joc_event_size(void) { return static_cast<std::uint32_t>(sizeof(joc_event)); }
|
||||
std::uint32_t JOC_CALL joc_task_config_size(void) {
|
||||
return static_cast<std::uint32_t>(sizeof(joc_task_config));
|
||||
}
|
||||
std::uint32_t JOC_CALL joc_task_result_size(void) {
|
||||
return static_cast<std::uint32_t>(sizeof(joc_task_result));
|
||||
}
|
||||
|
||||
const char* JOC_CALL joc_build_info(void) {
|
||||
static const std::string info = [] {
|
||||
std::string text = "joc_core 0.1.0-m1 (";
|
||||
#if defined(_MSC_VER)
|
||||
text += "msvc " + std::to_string(_MSC_VER);
|
||||
#elif defined(__clang__)
|
||||
text += std::string("clang ") + __clang_version__;
|
||||
#elif defined(__GNUC__)
|
||||
text += "gcc " + std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__);
|
||||
#else
|
||||
text += "unknown-compiler";
|
||||
#endif
|
||||
#if defined(_M_AMD64) || defined(__x86_64__)
|
||||
text += ", x64";
|
||||
#elif defined(_M_ARM64) || defined(__aarch64__)
|
||||
text += ", arm64";
|
||||
#elif defined(_M_IX86) || defined(__i386__)
|
||||
text += ", x86";
|
||||
#endif
|
||||
text += ", c++";
|
||||
text += std::to_string(static_cast<long long>(__cplusplus / 100 % 100));
|
||||
text += ")";
|
||||
return text;
|
||||
}();
|
||||
return info.c_str();
|
||||
}
|
||||
|
||||
const char* JOC_CALL joc_error_name(joc_error code) {
|
||||
switch (code) {
|
||||
case JOC_OK: return "JOC_OK";
|
||||
case JOC_ERR_INVALID_ARGUMENT: return "JOC_ERR_INVALID_ARGUMENT";
|
||||
case JOC_ERR_INVALID_CONFIG: return "JOC_ERR_INVALID_CONFIG";
|
||||
case JOC_ERR_OUT_OF_MEMORY: return "JOC_ERR_OUT_OF_MEMORY";
|
||||
case JOC_ERR_IO: return "JOC_ERR_IO";
|
||||
case JOC_ERR_UNSUPPORTED_PLATFORM: return "JOC_ERR_UNSUPPORTED_PLATFORM";
|
||||
case JOC_ERR_LIBRARY_MISSING: return "JOC_ERR_LIBRARY_MISSING";
|
||||
case JOC_ERR_INPUT_NOT_FOUND: return "JOC_ERR_INPUT_NOT_FOUND";
|
||||
case JOC_ERR_INPUT_FORMAT: return "JOC_ERR_INPUT_FORMAT";
|
||||
case JOC_ERR_EAC3_SYNCFRAME: return "JOC_ERR_EAC3_SYNCFRAME";
|
||||
case JOC_ERR_EMDF_TRANSPORT: return "JOC_ERR_EMDF_TRANSPORT";
|
||||
case JOC_ERR_EMDF_SYNTAX: return "JOC_ERR_EMDF_SYNTAX";
|
||||
case JOC_ERR_JOC_SYNTAX: return "JOC_ERR_JOC_SYNTAX";
|
||||
case JOC_ERR_JOC_UNSUPPORTED_VARIANT: return "JOC_ERR_JOC_UNSUPPORTED_VARIANT";
|
||||
case JOC_ERR_OAMD_SYNTAX: return "JOC_ERR_OAMD_SYNTAX";
|
||||
case JOC_ERR_OAMD_UNSUPPORTED_VARIANT: return "JOC_ERR_OAMD_UNSUPPORTED_VARIANT";
|
||||
case JOC_ERR_BITSTREAM_TRUNCATED: return "JOC_ERR_BITSTREAM_TRUNCATED";
|
||||
case JOC_ERR_BITSTREAM_PADDING: return "JOC_ERR_BITSTREAM_PADDING";
|
||||
case JOC_ERR_HRTF_NOT_FOUND: return "JOC_ERR_HRTF_NOT_FOUND";
|
||||
case JOC_ERR_HRTF_FORMAT: return "JOC_ERR_HRTF_FORMAT";
|
||||
case JOC_ERR_HRTF_VERSION: return "JOC_ERR_HRTF_VERSION";
|
||||
case JOC_ERR_HRTF_HASH: return "JOC_ERR_HRTF_HASH";
|
||||
case JOC_ERR_HRTF_UNSUPPORTED_CONVENTION: return "JOC_ERR_HRTF_UNSUPPORTED_CONVENTION";
|
||||
case JOC_ERR_LAYOUT_UNSUPPORTED: return "JOC_ERR_LAYOUT_UNSUPPORTED";
|
||||
case JOC_ERR_RENDER_FAILED: return "JOC_ERR_RENDER_FAILED";
|
||||
case JOC_ERR_OUTPUT_OPEN: return "JOC_ERR_OUTPUT_OPEN";
|
||||
case JOC_ERR_OUTPUT_WRITE: return "JOC_ERR_OUTPUT_WRITE";
|
||||
case JOC_ERR_OUTPUT_CLIP_ABORT: return "JOC_ERR_OUTPUT_CLIP_ABORT";
|
||||
case JOC_ERR_ADM_VALIDATION: return "JOC_ERR_ADM_VALIDATION";
|
||||
case JOC_ERR_CANCELLED: return "JOC_ERR_CANCELLED";
|
||||
case JOC_ERR_STATE: return "JOC_ERR_STATE";
|
||||
case JOC_ERR_NOT_SUPPORTED: return "JOC_ERR_NOT_SUPPORTED";
|
||||
case JOC_ERR_INTERNAL: return "JOC_ERR_INTERNAL";
|
||||
default: return "JOC_ERR_UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
const char* JOC_CALL joc_error_stage(joc_error code) {
|
||||
switch (code) {
|
||||
case JOC_ERR_EAC3_SYNCFRAME:
|
||||
case JOC_ERR_INPUT_NOT_FOUND:
|
||||
case JOC_ERR_INPUT_FORMAT:
|
||||
return "eac3_transport";
|
||||
case JOC_ERR_EMDF_TRANSPORT:
|
||||
case JOC_ERR_EMDF_SYNTAX:
|
||||
return "emdf";
|
||||
case JOC_ERR_JOC_SYNTAX:
|
||||
case JOC_ERR_JOC_UNSUPPORTED_VARIANT:
|
||||
return "joc";
|
||||
case JOC_ERR_OAMD_SYNTAX:
|
||||
case JOC_ERR_OAMD_UNSUPPORTED_VARIANT:
|
||||
return "oamd";
|
||||
case JOC_ERR_BITSTREAM_TRUNCATED:
|
||||
case JOC_ERR_BITSTREAM_PADDING:
|
||||
return "bitstream";
|
||||
case JOC_ERR_HRTF_NOT_FOUND:
|
||||
case JOC_ERR_HRTF_FORMAT:
|
||||
case JOC_ERR_HRTF_VERSION:
|
||||
case JOC_ERR_HRTF_HASH:
|
||||
case JOC_ERR_HRTF_UNSUPPORTED_CONVENTION:
|
||||
return "hrtf";
|
||||
case JOC_ERR_LAYOUT_UNSUPPORTED:
|
||||
case JOC_ERR_RENDER_FAILED:
|
||||
return "render";
|
||||
case JOC_ERR_OUTPUT_OPEN:
|
||||
case JOC_ERR_OUTPUT_WRITE:
|
||||
case JOC_ERR_OUTPUT_CLIP_ABORT:
|
||||
case JOC_ERR_ADM_VALIDATION:
|
||||
return "output";
|
||||
case JOC_ERR_CANCELLED:
|
||||
case JOC_ERR_STATE:
|
||||
case JOC_ERR_NOT_SUPPORTED:
|
||||
case JOC_ERR_INTERNAL:
|
||||
return "task";
|
||||
default:
|
||||
return "core";
|
||||
}
|
||||
}
|
||||
|
||||
const char* JOC_CALL joc_last_error_detail(void) { return joc::last_error_detail.c_str(); }
|
||||
|
||||
joc_error JOC_CALL joc_parse_id14(const std::uint8_t* payload, std::size_t payload_size,
|
||||
joc_frame_params* out_params) {
|
||||
if (payload == nullptr || out_params == nullptr || payload_size == 0) {
|
||||
return arg_fail("joc_parse_id14 requires a non-empty payload and an output struct");
|
||||
}
|
||||
return finish(joc::joc::parse_id14(payload, payload_size, out_params, nullptr));
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size,
|
||||
joc_frame_params* out_params, joc_emdf_info* out_emdf) {
|
||||
if (frame == nullptr || out_params == nullptr || frame_size == 0) {
|
||||
return arg_fail("joc_parse_eac3_frame requires a frame and an output struct");
|
||||
}
|
||||
joc::emdf::Container container;
|
||||
const joc::Status status =
|
||||
joc::joc::parse_eac3_frame(frame, frame_size, out_params, &container, nullptr);
|
||||
if (!status.ok()) {
|
||||
return finish(status);
|
||||
}
|
||||
if (out_emdf != nullptr) {
|
||||
fill_emdf_info(container, out_emdf);
|
||||
}
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_extract_payload(const std::uint8_t* frame, std::size_t frame_size,
|
||||
const joc_emdf_payload_info* payload, std::uint8_t* out,
|
||||
std::size_t out_capacity, std::size_t* out_size) {
|
||||
if (frame == nullptr || payload == nullptr || out_size == nullptr) {
|
||||
return arg_fail("joc_extract_payload requires frame, payload and out_size");
|
||||
}
|
||||
*out_size = payload->size;
|
||||
if (out == nullptr) {
|
||||
return JOC_OK;
|
||||
}
|
||||
if (out_capacity < payload->size) {
|
||||
return arg_fail("joc_extract_payload output buffer too small");
|
||||
}
|
||||
joc::emdf::Payload entry;
|
||||
entry.id = payload->id;
|
||||
entry.sample_offset = payload->sample_offset;
|
||||
entry.bit_offset = payload->bit_offset;
|
||||
entry.size = payload->size;
|
||||
std::vector<std::uint8_t> bytes;
|
||||
const joc::Status status = joc::emdf::extract_payload_bytes(frame, frame_size, entry, &bytes);
|
||||
if (!status.ok()) {
|
||||
return finish(status);
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::memcpy(out, bytes.data(), bytes.size());
|
||||
}
|
||||
*out_size = bytes.size();
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_eac3_frame_bytes(const std::uint8_t* data, std::size_t size, std::size_t offset,
|
||||
std::size_t* out_frame_bytes) {
|
||||
if (data == nullptr || out_frame_bytes == nullptr) {
|
||||
return arg_fail("joc_eac3_frame_bytes requires data and out_frame_bytes");
|
||||
}
|
||||
const joc_error code = joc::eac3::FrameReader::frame_bytes(data, size, offset, out_frame_bytes);
|
||||
if (code != JOC_OK) {
|
||||
return finish(joc::Status::fail(code, joc::stage::kEac3,
|
||||
"invalid or truncated E-AC-3 syncframe at byte " +
|
||||
std::to_string(offset)));
|
||||
}
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
|
||||
std::uint32_t* out_trailing_bits) {
|
||||
if (payload == nullptr || payload_size == 0) {
|
||||
return arg_fail("joc_check_id14_padding requires a payload");
|
||||
}
|
||||
return finish(joc::joc::check_id14_padding(payload, payload_size, out_trailing_bits));
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -1,180 +0,0 @@
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "joc_core.h"
|
||||
#include "joc_stream.h"
|
||||
#include "stream/stream.h"
|
||||
|
||||
struct joc_stream {
|
||||
joc::stream::Stream instance;
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
joc_error finish_stream(const joc::Status& status) {
|
||||
// The structured detail is not dropped here: joc_last_error_detail() reports
|
||||
// it for the streaming entry points as well as for the task API.
|
||||
joc::record_error_detail(status);
|
||||
return status.code();
|
||||
}
|
||||
|
||||
joc::stream::Config to_config(const joc_stream_config& config) {
|
||||
joc::stream::Config out;
|
||||
out.input = config.input;
|
||||
out.output = config.output;
|
||||
if (config.speaker_layout_name != nullptr) { out.layout = config.speaker_layout_name; }
|
||||
if (config.speaker_metadata_offset != 0u) {
|
||||
out.metadata_offset = config.speaker_metadata_offset;
|
||||
}
|
||||
out.binaural_mode = config.binaural_mode != 0u ? config.binaural_mode : JOC_BINAURAL_MID;
|
||||
if (config.hrtf_path != nullptr) { out.hrtf_path = config.hrtf_path; }
|
||||
if (config.kernels_path != nullptr) { out.kernels_path = config.kernels_path; }
|
||||
if (config.binaural_tail_seconds > 0.0) { out.tail_seconds = config.binaural_tail_seconds; }
|
||||
if (config.object_delay_samples != 0u) {
|
||||
out.object_delay_samples = config.object_delay_samples;
|
||||
}
|
||||
out.gain_db = config.gain_db;
|
||||
out.native_threads = config.native_threads;
|
||||
if (config.personalized_headphone_path != nullptr) {
|
||||
out.personalized_headphone_path = config.personalized_headphone_path;
|
||||
}
|
||||
// 0 is the documented "off" spelling for the room switch.
|
||||
out.jochrtf_room = config.jochrtf_room;
|
||||
// 0 is "unset" here as well: the Rosella path defaults to the public bank.
|
||||
if (config.rosella_filterbank != 0u) {
|
||||
out.rosella_filterbank = config.rosella_filterbank;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
joc_error JOC_CALL joc_stream_create(const joc_stream_config* config, joc_stream** out) {
|
||||
if (config == nullptr || out == nullptr || config->struct_size != sizeof(joc_stream_config)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
auto* stream = new (std::nothrow) joc_stream();
|
||||
if (stream == nullptr) {
|
||||
return JOC_ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
const joc::Status status = stream->instance.create(to_config(*config));
|
||||
if (!status.ok()) {
|
||||
delete stream;
|
||||
return finish_stream(status);
|
||||
}
|
||||
*out = stream;
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer* input,
|
||||
std::uint32_t* consumed_samples, std::uint32_t* consumed_bytes) {
|
||||
if (stream == nullptr || input == nullptr ||
|
||||
input->struct_size != sizeof(joc_stream_buffer)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
const joc::Status status = [&] {
|
||||
if (input->kind == JOC_STREAM_IN_EAC3) {
|
||||
std::size_t consumed = 0;
|
||||
const joc::Status pushed =
|
||||
stream->instance.push_eac3(input->bytes, input->byte_count, &consumed);
|
||||
if (consumed_bytes != nullptr) {
|
||||
*consumed_bytes = static_cast<std::uint32_t>(consumed);
|
||||
}
|
||||
return pushed;
|
||||
}
|
||||
if (input->kind == JOC_STREAM_IN_PCM_OBJECTS16) {
|
||||
std::size_t consumed = 0;
|
||||
const joc::Status pushed =
|
||||
stream->instance.push_objects16(input->pcm, input->sample_count, &consumed);
|
||||
if (consumed_samples != nullptr) {
|
||||
*consumed_samples = static_cast<std::uint32_t>(consumed);
|
||||
}
|
||||
return pushed;
|
||||
}
|
||||
std::size_t consumed = 0;
|
||||
const std::uint32_t channels = input->channels != 0u ? input->channels : 6u;
|
||||
const joc::Status pushed =
|
||||
stream->instance.push_bed(input->pcm, input->sample_count, channels, &consumed);
|
||||
if (consumed_samples != nullptr) {
|
||||
*consumed_samples = static_cast<std::uint32_t>(consumed);
|
||||
}
|
||||
return pushed;
|
||||
}();
|
||||
return finish_stream(status);
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_pull(joc_stream* stream, joc_stream_buffer* output,
|
||||
std::uint32_t* produced_samples) {
|
||||
if (stream == nullptr || output == nullptr || output->out_pcm == nullptr ||
|
||||
output->struct_size != sizeof(joc_stream_buffer)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
std::size_t produced = 0;
|
||||
const joc::Status status =
|
||||
stream->instance.pull(output->out_pcm, output->sample_count, &produced);
|
||||
if (!status.ok()) {
|
||||
return finish_stream(status);
|
||||
}
|
||||
if (produced_samples != nullptr) {
|
||||
*produced_samples = static_cast<std::uint32_t>(produced);
|
||||
}
|
||||
output->sample_count = static_cast<std::uint32_t>(produced);
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_flush(joc_stream* stream) {
|
||||
if (stream == nullptr) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
return finish_stream(stream->instance.flush());
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_reset(joc_stream* stream) {
|
||||
if (stream == nullptr) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
return finish_stream(stream->instance.reset());
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_status(const joc_stream* stream, joc_stream_status_info* out) {
|
||||
// The info struct only grows by appending fields, so a caller built against
|
||||
// the previous size still gets every field it knows about.
|
||||
constexpr uint32_t kSizeBeforeTruncatedFrames = static_cast<uint32_t>(
|
||||
offsetof(joc_stream_status_info, truncated_joc_frames));
|
||||
if (stream == nullptr || out == nullptr ||
|
||||
(out->struct_size != sizeof(joc_stream_status_info) &&
|
||||
out->struct_size != kSizeBeforeTruncatedFrames)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
const joc::stream::Info& info = stream->instance.info();
|
||||
out->frames_in = info.frames_in;
|
||||
out->frames_out = info.frames_out;
|
||||
out->samples_in = info.samples_in;
|
||||
out->samples_out = info.samples_out;
|
||||
out->bytes_in = info.bytes_in;
|
||||
out->buffered_samples = stream->instance.buffered_samples();
|
||||
out->oamd_payloads = info.oamd_payloads;
|
||||
out->oamd_transitions = info.oamd_transitions;
|
||||
out->missing_joc_frames = info.missing_joc_frames;
|
||||
out->objects_clamped_frames = info.objects_clamped_frames;
|
||||
out->extra_container_frames = info.extra_container_frames;
|
||||
out->chanmap_pair_mismatch_frames = info.chanmap_pair_mismatch_frames;
|
||||
out->bed_mask_mismatch_frames = info.bed_mask_mismatch_frames;
|
||||
out->output_channels = info.output_channels;
|
||||
out->ended = info.ended;
|
||||
if (out->struct_size > kSizeBeforeTruncatedFrames) {
|
||||
out->truncated_joc_frames = info.truncated_joc_frames;
|
||||
}
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_stream_destroy(joc_stream* stream) {
|
||||
delete stream;
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -1,120 +0,0 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "joc_core.h"
|
||||
#include "task/task.h"
|
||||
|
||||
// task and whichever frontend wants to stop it (plan 29.1/29.2).
|
||||
struct joc_cancel_token {
|
||||
std::atomic<std::uint32_t> requested{0u};
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
thread_local std::string g_task_detail;
|
||||
|
||||
joc_error finish_task(const joc::Status& status) {
|
||||
if (status.ok()) {
|
||||
g_task_detail.clear();
|
||||
return JOC_OK;
|
||||
}
|
||||
g_task_detail.assign(status.stage());
|
||||
g_task_detail.append(": ");
|
||||
g_task_detail.append(status.message());
|
||||
return status.code();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
joc_cancel_token* JOC_CALL joc_cancel_token_create(void) {
|
||||
return new (std::nothrow) joc_cancel_token();
|
||||
}
|
||||
|
||||
void JOC_CALL joc_cancel_token_request(joc_cancel_token* token) {
|
||||
if (token != nullptr) {
|
||||
token->requested.store(1u, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
std::int32_t JOC_CALL joc_cancel_token_is_requested(const joc_cancel_token* token) {
|
||||
return (token != nullptr && token->requested.load(std::memory_order_relaxed) != 0u) ? 1 : 0;
|
||||
}
|
||||
|
||||
void JOC_CALL joc_cancel_token_destroy(joc_cancel_token* token) { delete token; }
|
||||
|
||||
joc_error JOC_CALL joc_task_validate(const joc_task_config* config,
|
||||
joc_validation_issue* issues, std::uint32_t capacity,
|
||||
std::uint32_t* count) {
|
||||
if (config == nullptr) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (config->struct_size != sizeof(joc_task_config)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
std::vector<joc_validation_issue> found;
|
||||
std::uint32_t errors = 0;
|
||||
const joc::Status status = joc::task::validate(*config, &found, &errors);
|
||||
if (count != nullptr) {
|
||||
*count = static_cast<std::uint32_t>(found.size());
|
||||
}
|
||||
if (issues != nullptr) {
|
||||
for (std::uint32_t i = 0; i < capacity && i < found.size(); ++i) {
|
||||
issues[i] = found[i];
|
||||
}
|
||||
}
|
||||
return status.ok() ? JOC_OK : JOC_ERR_INVALID_CONFIG;
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_task_execute(const joc_task_config* config, const joc_event_sink* sink,
|
||||
joc_task_result* out) {
|
||||
if (config == nullptr || config->struct_size != sizeof(joc_task_config)) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (out != nullptr) {
|
||||
std::memset(out, 0, sizeof(*out));
|
||||
out->struct_size = sizeof(joc_task_result);
|
||||
out->struct_version = JOC_TASK_RESULT_VERSION;
|
||||
}
|
||||
const joc::Status status = joc::task::run(*config, sink, out);
|
||||
if (!status.ok() && out != nullptr && out->status == 0u) {
|
||||
out->status = JOC_TASK_FAILED;
|
||||
out->error_code = static_cast<std::uint32_t>(status.code());
|
||||
std::snprintf(out->error_stage, sizeof(out->error_stage), "%s", status.stage().c_str());
|
||||
std::snprintf(out->error_message, sizeof(out->error_message), "%s",
|
||||
status.message().c_str());
|
||||
}
|
||||
g_task_detail = status.ok() ? std::string() : status.message();
|
||||
return status.code();
|
||||
}
|
||||
|
||||
joc_error JOC_CALL joc_task_result_to_json(const joc_task_result* result, char* buffer,
|
||||
std::size_t capacity, std::size_t* needed) {
|
||||
if (result == nullptr) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
std::string json;
|
||||
const joc::Status status = joc::task::result_to_json(*result, &json);
|
||||
if (!status.ok()) {
|
||||
return status.code();
|
||||
}
|
||||
if (needed != nullptr) {
|
||||
*needed = json.size() + 1u;
|
||||
}
|
||||
if (buffer == nullptr) {
|
||||
return JOC_OK;
|
||||
}
|
||||
if (capacity < json.size() + 1u) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
std::memcpy(buffer, json.c_str(), json.size() + 1u);
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,229 @@
|
||||
"""JOC 核心通道的输入序、床声明与 ADM 落位。
|
||||
|
||||
这里只有"位置/身份"层:按通道码把 E-AC-3 解出的 PCM 摆进 JOC 输入序,并把额外
|
||||
那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用
|
||||
``(N, sigma)``。
|
||||
"""
|
||||
from emdf import expand_chanmap
|
||||
|
||||
# JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。
|
||||
JOC_CORE_POSITIONS = ("L", "R", "C", "Ls", "Rs")
|
||||
|
||||
# 额外那一对的身份由 k 决定(规格 §2):k=1 是后环绕,k∈{2,4} 是前上方。
|
||||
EXTRA_PAIR_BY_CONFIG = {1: ("Lrs", "Rrs"), 2: ("Vhl", "Vhr"), 4: ("Vhl", "Vhr")}
|
||||
|
||||
# 额外那一对在 10 槽 ADM 骨架里的槽位(规格 §10.2 的 RC_Lrs/RC_Rrs 与 RC_Lts/RC_Rts)。
|
||||
# ADM 现契约是"除 LFE 槽外全部静音"(床通道不作为输出通道),因此这些槽位只被记录,
|
||||
# 不承载音频;DSP 也不读它们。
|
||||
BED_EXTRA_SLOTS_BY_CONFIG = {1: (6, 7), 2: (8, 9), 4: (8, 9)}
|
||||
|
||||
# 位置 → ffmpeg 声道名与其声道位。ffmpeg 的帧内声道顺序就是声道位的升序,
|
||||
# 因此这张表同时给出"位置集合 → 帧内下标"的换算。
|
||||
_FFMPEG_POSITIONS = {
|
||||
"L": ("FL", 0x1),
|
||||
"R": ("FR", 0x2),
|
||||
"C": ("FC", 0x4),
|
||||
"LFE": ("LFE", 0x8),
|
||||
"Lrs": ("BL", 0x10),
|
||||
"Rrs": ("BR", 0x20),
|
||||
"Lc": ("FLC", 0x40),
|
||||
"Rc": ("FRC", 0x80),
|
||||
"Cs": ("BC", 0x100),
|
||||
"Ls": ("SL", 0x200),
|
||||
"Rs": ("SR", 0x400),
|
||||
"Vhl": ("TFL", 0x1000),
|
||||
"Vhc": ("TFC", 0x2000),
|
||||
"Vhr": ("TFR", 0x4000),
|
||||
"Lts": ("TBL", 0x8000),
|
||||
"Rts": ("TBR", 0x20000),
|
||||
}
|
||||
|
||||
|
||||
def bed_order(locations):
|
||||
"""把位置集合按 ffmpeg 的声道位顺序排好,返回 (位置, ffmpeg 名) 列表。
|
||||
|
||||
未知位置返回 None,由调用方决定报哪种变体。
|
||||
"""
|
||||
unique = []
|
||||
for position in locations:
|
||||
if position not in unique:
|
||||
unique.append(position)
|
||||
if any(position not in _FFMPEG_POSITIONS for position in unique):
|
||||
return None
|
||||
return [(position, _FFMPEG_POSITIONS[position][0])
|
||||
for position in sorted(unique, key=lambda p: _FFMPEG_POSITIONS[p][1])]
|
||||
|
||||
|
||||
def extra_pair(core_locations, chanmap):
|
||||
"""依赖子流的 chanmap 里超出核心声道集合的那一对位置。
|
||||
|
||||
chanmap 会把核心已有的声道一并列出(例如 5.1 核心 + 前上方对会写
|
||||
``Ls/Rs/Lrs/Rrs``),因此额外对是"chanmap 位置 − 核心位置"。
|
||||
"""
|
||||
core = set(core_locations)
|
||||
return tuple(position for position in expand_chanmap(chanmap) if position not in core)
|
||||
|
||||
|
||||
# OAMD §5.6.1.1.4 Table 12 的标准 bed 声道(LSB 在前,与 oamd_bits.BED_CHANNEL_LABELS 同序)。
|
||||
# 一个 bed_assignment() 声明的声道就是按这个顺序展开的。
|
||||
BED_LAYOUT_ORDER = (
|
||||
"RC_L/RC_R", "RC_C", "RC_LFE", "RC_LS/RC_RS", "RC_LB/RC_RB",
|
||||
"RC_TFL/RC_TFR", "RC_TSL/RC_TSR", "RC_TBL/RC_TBR", "RC_LW/RC_RW", "RC_LFE2",
|
||||
)
|
||||
|
||||
# 标准 bed 声道 → 扬声器布局里的短名(与 speaker_layouts 的声道名同形,如 RC_LW → Lw)。
|
||||
BED_CHANNEL_NAMES = {
|
||||
"RC_L": "L", "RC_R": "R", "RC_C": "C", "RC_LFE": "LFE",
|
||||
"RC_LS": "Ls", "RC_RS": "Rs", "RC_LB": "Lrs", "RC_RB": "Rrs",
|
||||
"RC_TFL": "Ltf", "RC_TFR": "Rtf", "RC_TSL": "Ltm", "RC_TSR": "Rtm",
|
||||
"RC_TBL": "Ltr", "RC_TBR": "Rtr", "RC_LW": "Lw", "RC_RW": "Rw",
|
||||
}
|
||||
|
||||
# 每个 bed 声道的固定方位(ADM 笛卡尔坐标,x 向右、y 向前、z 向上),取自
|
||||
# ETSI TS 103 420 V1.2.1 clause B.2.1.4 Table B.10:speakerLabel 各自对应唯一的
|
||||
# position(X,Y,Z),与目标扬声器布局无关。bed 对象在 OAMD 里没有位置字段,位置只由
|
||||
# 声明的声道标签决定。
|
||||
# Table B.10 把 Lw/Rw 的 Y 记作 0,677419,即 ADM 62 级网格上的 42/62 = 21/31。
|
||||
BED_CHANNEL_ADM_POSITION = {
|
||||
"RC_L": (-1.0, 1.0, 0.0),
|
||||
"RC_R": (1.0, 1.0, 0.0),
|
||||
"RC_C": (0.0, 1.0, 0.0),
|
||||
"RC_LFE": (-1.0, 1.0, -1.0),
|
||||
"RC_LS": (-1.0, 0.0, 0.0),
|
||||
"RC_RS": (1.0, 0.0, 0.0),
|
||||
"RC_LB": (-1.0, -1.0, 0.0),
|
||||
"RC_RB": (1.0, -1.0, 0.0),
|
||||
"RC_TFL": (-1.0, 1.0, 1.0),
|
||||
"RC_TFR": (1.0, 1.0, 1.0),
|
||||
"RC_TSL": (-1.0, 0.0, 1.0),
|
||||
"RC_TSR": (1.0, 0.0, 1.0),
|
||||
"RC_TBL": (-1.0, -1.0, 1.0),
|
||||
"RC_TBR": (1.0, -1.0, 1.0),
|
||||
"RC_LW": (-1.0, 21.0 / 31.0, 0.0),
|
||||
"RC_RW": (1.0, 21.0 / 31.0, 0.0),
|
||||
"RC_LFE2": (1.0, 1.0, -1.0),
|
||||
}
|
||||
|
||||
# LFE 不是空间声源:它的 PCM 走独立通路,不参与对象摆位。
|
||||
BED_LFE_LABELS = frozenset(("RC_LFE", "RC_LFE2"))
|
||||
|
||||
# 标准 bed 布局名:由声明声道的集合唯一确定(9.1.6 这类常用叫法即由此得来)。
|
||||
BED_LAYOUT_NAMES = {
|
||||
frozenset(("L", "R")): "2.0",
|
||||
frozenset(("L", "R", "C")): "3.0",
|
||||
frozenset(("L", "R", "C", "LFE")): "3.1",
|
||||
frozenset(("L", "R", "Ls", "Rs")): "4.0",
|
||||
frozenset(("L", "R", "C", "Ls", "Rs")): "5.0",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs")): "5.1",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs")): "7.1",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw")): "9.1",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm")): "5.1.2",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltf", "Rtf", "Ltr", "Rtr")):
|
||||
"5.1.4",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltm", "Rtm")): "7.1.2",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
|
||||
"Ltf", "Rtf", "Ltr", "Rtr")): "7.1.4",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
|
||||
"Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr")): "7.1.6",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
|
||||
"Lw", "Rw", "Ltm", "Rtm")): "9.1.2",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
|
||||
"Lw", "Rw", "Ltf", "Rtf", "Ltr", "Rtr")): "9.1.4",
|
||||
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw",
|
||||
"Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr")): "9.1.6",
|
||||
}
|
||||
|
||||
# ADM 7.1.2 床的槽位顺序:与 BED_EXTRA_SLOTS_BY_CONFIG 的 10 槽骨架一致。
|
||||
ADM_BED_712 = ("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltf", "Rtf")
|
||||
|
||||
|
||||
def adm_xyz_to_q(x, y, z):
|
||||
"""ADM 笛卡尔坐标 → OAMD/渲染器使用的 Q15 方位三元组。"""
|
||||
q1 = int(round((float(x) + 1.0) * 0.5 * 32767.0))
|
||||
q2 = int(round((1.0 - float(y)) * 0.5 * 32767.0))
|
||||
q3 = int(round(float(z) * 32767.0))
|
||||
clamp = lambda value: max(0, min(32767, value)) # noqa: E731
|
||||
return clamp(q1), clamp(q2), clamp(q3)
|
||||
|
||||
|
||||
def adm_xyz_to_render_coordinates(x, y, z):
|
||||
"""ADM 笛卡尔坐标 → 渲染器方位 ``(u, v, w)``(u 向右、v 向后、w 向上,0..1)。"""
|
||||
return ((float(x) + 1.0) * 0.5, (1.0 - float(y)) * 0.5, float(z))
|
||||
|
||||
|
||||
def declared_bed_channels(assignments):
|
||||
"""把 ``program_assignment()`` 的 bed 声明展开成有序的标准声道标签。
|
||||
|
||||
``assignments`` 是 ``bed_assignments`` 列表:元素可以是带 ``channels`` 的字典,
|
||||
也可以直接是声道标签列表(``program_bed_channels()`` 的形式)。未识别标签保持
|
||||
原样,由调用方决定是否当作变体处理。
|
||||
"""
|
||||
labels = []
|
||||
for assignment in assignments or ():
|
||||
if isinstance(assignment, dict):
|
||||
channels = assignment.get("channels", ())
|
||||
else:
|
||||
channels = assignment
|
||||
for label in channels:
|
||||
if label not in labels:
|
||||
labels.append(label)
|
||||
return tuple(labels)
|
||||
|
||||
|
||||
def bed_layout_name(labels):
|
||||
"""按声明声道集合识别 bed 布局名;未知集合返回 None。"""
|
||||
names = [BED_CHANNEL_NAMES.get(label, label) for label in labels]
|
||||
return BED_LAYOUT_NAMES.get(frozenset(names))
|
||||
|
||||
|
||||
def bed_layout(assignments):
|
||||
"""声明的 bed 布局:``{"name", "labels", "channels"}``;没有 bed 声明时返回 None。
|
||||
|
||||
``channels`` 去掉 LFE/LFE2:JOC 的对象槽按这个顺序依次对应 15 路对象 PCM,
|
||||
LFE 走核心声道的独立通路。
|
||||
"""
|
||||
labels = declared_bed_channels(assignments)
|
||||
if not labels:
|
||||
return None
|
||||
channels = [label for label in labels if label not in BED_LFE_LABELS]
|
||||
return {
|
||||
"name": bed_layout_name(labels),
|
||||
"labels": list(labels),
|
||||
"channels": channels,
|
||||
}
|
||||
|
||||
|
||||
def bed_object_positions(labels):
|
||||
"""JOC 对象槽 → 固定 ADM 位置:(1..15) 对应未含 LFE 的 bed 声道。
|
||||
|
||||
positions 由声道标签唯一决定(Table B.10),与目标输出布局无关——同一个 bed 声道
|
||||
在扬声器布局和双耳渲染里都落在同一个源位置。返回的字典只包含能给出固定位置的
|
||||
标准声道;不是 bed 声道的对象不在其中,仍按 OAMD 坐标渲染。LFE 走独立通路,
|
||||
不出现在对象槽里。
|
||||
"""
|
||||
positions = {}
|
||||
for index, label in enumerate(labels, 1):
|
||||
if index > 15:
|
||||
break
|
||||
if label in BED_LFE_LABELS:
|
||||
continue
|
||||
position = BED_CHANNEL_ADM_POSITION.get(label)
|
||||
if position is not None:
|
||||
positions[index] = position
|
||||
return positions
|
||||
|
||||
|
||||
def joc_input_indices(locations, pair):
|
||||
"""返回 JOC 输入序 (L, R, C, Ls, Rs, 额外对…) 在床 PCM 里的下标。"""
|
||||
if pair is None:
|
||||
pair = ()
|
||||
order = bed_order(locations)
|
||||
if order is None:
|
||||
return None
|
||||
index_of = {position: index for index, (position, _name) in enumerate(order)}
|
||||
required = list(JOC_CORE_POSITIONS) + list(pair)
|
||||
if any(position not in index_of for position in required):
|
||||
return None
|
||||
if len(required) != len(set(required)):
|
||||
return None
|
||||
return [index_of[position] for position in required]
|
||||
@@ -1,664 +0,0 @@
|
||||
#define EJOC_BUILD_DLL
|
||||
#include "eac3joc_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <vector>
|
||||
|
||||
namespace ejoc::binaural {
|
||||
|
||||
struct Complex {
|
||||
double re;
|
||||
double im;
|
||||
};
|
||||
|
||||
inline Complex add(Complex a, Complex b) noexcept {
|
||||
return {a.re + b.re, a.im + b.im};
|
||||
}
|
||||
|
||||
inline Complex mul(Complex a, Complex b) noexcept {
|
||||
return {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re};
|
||||
}
|
||||
|
||||
inline Complex scale(Complex value, double gain) noexcept {
|
||||
return {value.re * gain, value.im * gain};
|
||||
}
|
||||
|
||||
constexpr double kPi = 3.141592653589793238462643383279502884;
|
||||
constexpr int kChannels = EJOC_BINAURAL_INPUT_CHANNELS;
|
||||
constexpr int kEars = EJOC_BINAURAL_OUTPUT_CHANNELS;
|
||||
constexpr int kBlock = EJOC_BINAURAL_BLOCK_SAMPLES;
|
||||
constexpr int kSlots = kBlock / 64;
|
||||
constexpr int kQmf = EJOC_BINAURAL_QMF_BANDS;
|
||||
constexpr int kHybrid = EJOC_BINAURAL_HYBRID_BANDS;
|
||||
constexpr int kRank = 4;
|
||||
|
||||
class Renderer final {
|
||||
public:
|
||||
Renderer() noexcept {
|
||||
initialize_fft();
|
||||
reset();
|
||||
}
|
||||
|
||||
int configure_kernels(
|
||||
const double* qmf_analysis,
|
||||
const double* hybrid_low,
|
||||
const int16_t* hybrid_indices,
|
||||
const double* hybrid_values,
|
||||
uint32_t hybrid_count,
|
||||
const double* qmf_basis,
|
||||
const double* qmf_taps) noexcept {
|
||||
if (!qmf_analysis || !hybrid_low || !hybrid_indices || !hybrid_values ||
|
||||
!qmf_basis || !qmf_taps || hybrid_count == 0) {
|
||||
return fail("invalid binaural kernel configuration");
|
||||
}
|
||||
std::memcpy(qmf_analysis_.data(), qmf_analysis,
|
||||
qmf_analysis_.size() * sizeof(double));
|
||||
hybrid_low_.assign(hybrid_low, hybrid_low + 3 * 2 * 13 * 16 * 2);
|
||||
hybrid_indices_.assign(hybrid_indices, hybrid_indices + hybrid_count * 4);
|
||||
hybrid_values_.assign(hybrid_values, hybrid_values + hybrid_count);
|
||||
std::memcpy(qmf_basis_.data(), qmf_basis,
|
||||
qmf_basis_.size() * sizeof(double));
|
||||
std::memcpy(qmf_taps_.data(), qmf_taps,
|
||||
qmf_taps_.size() * sizeof(double));
|
||||
kernels_ready_ = true;
|
||||
reset();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int configure_room(
|
||||
uint32_t bands,
|
||||
uint32_t allpass_count,
|
||||
const uint32_t* allpass_delays,
|
||||
const double* allpass_gains,
|
||||
const uint32_t* fdn_delays,
|
||||
const double* fdn_matrix,
|
||||
uint32_t output_tap_delay,
|
||||
const double* feedback_complex,
|
||||
const double* output_taps,
|
||||
const double* output_complex,
|
||||
uint32_t extra_count,
|
||||
const uint32_t* extra_delays,
|
||||
const double* extra_fields_complex,
|
||||
const double* extra_matrices) noexcept {
|
||||
if (bands != 64 || !fdn_delays || !fdn_matrix || !feedback_complex ||
|
||||
!output_taps || !output_complex ||
|
||||
(allpass_count && (!allpass_delays || !allpass_gains)) ||
|
||||
(extra_count && (!extra_delays || !extra_fields_complex || !extra_matrices))) {
|
||||
return fail("invalid binaural room configuration");
|
||||
}
|
||||
room_bands_ = bands;
|
||||
if (allpass_count) {
|
||||
allpass_delays_.assign(allpass_delays, allpass_delays + allpass_count);
|
||||
allpass_gains_.assign(allpass_gains, allpass_gains + allpass_count);
|
||||
} else {
|
||||
allpass_delays_.clear();
|
||||
allpass_gains_.clear();
|
||||
}
|
||||
allpass_offsets_.resize(allpass_count);
|
||||
allpass_positions_.assign(allpass_count, 0);
|
||||
size_t allpass_size = 0;
|
||||
for (uint32_t index = 0; index < allpass_count; ++index) {
|
||||
if (allpass_delays_[index] == 0) {
|
||||
return fail("binaural allpass delay must be positive");
|
||||
}
|
||||
allpass_offsets_[index] = allpass_size;
|
||||
allpass_size += static_cast<size_t>(allpass_delays_[index]) * bands;
|
||||
}
|
||||
allpass_memory_.assign(allpass_size, {});
|
||||
|
||||
room_capacity_ = 0;
|
||||
for (int branch = 0; branch < 4; ++branch) {
|
||||
fdn_delays_[branch] = fdn_delays[branch];
|
||||
room_capacity_ = std::max(room_capacity_, fdn_delays_[branch]);
|
||||
}
|
||||
if (room_capacity_ == 0) {
|
||||
return fail("binaural room delay must be positive");
|
||||
}
|
||||
std::copy(fdn_matrix, fdn_matrix + 16, fdn_matrix_.begin());
|
||||
output_tap_delay_ = output_tap_delay;
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
for (int branch = 0; branch < 4; ++branch) {
|
||||
const size_t complex_index = (static_cast<size_t>(band) * 4 + branch) * 2;
|
||||
feedback_[band][branch] = {
|
||||
feedback_complex[complex_index], feedback_complex[complex_index + 1]};
|
||||
output_taps_[band][branch] = output_taps[band * 4 + branch];
|
||||
for (int ear = 0; ear < 2; ++ear) {
|
||||
const size_t output_index =
|
||||
((static_cast<size_t>(ear) * 64 + band) * 4 + branch) * 2;
|
||||
output_matrix_[ear][band][branch] = {
|
||||
output_complex[output_index], output_complex[output_index + 1]};
|
||||
}
|
||||
}
|
||||
}
|
||||
room_memory_.assign(static_cast<size_t>(room_capacity_) * 64 * 4, {});
|
||||
if (extra_count) {
|
||||
extra_delays_.assign(extra_delays, extra_delays + extra_count);
|
||||
} else {
|
||||
extra_delays_.clear();
|
||||
}
|
||||
extra_fields_.resize(static_cast<size_t>(extra_count) * 64);
|
||||
extra_matrices_.resize(static_cast<size_t>(extra_count) * 16);
|
||||
for (uint32_t extra = 0; extra < extra_count; ++extra) {
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
const size_t source = (static_cast<size_t>(extra) * 64 + band) * 2;
|
||||
extra_fields_[static_cast<size_t>(extra) * 64 + band] = {
|
||||
extra_fields_complex[source], extra_fields_complex[source + 1]};
|
||||
}
|
||||
std::copy(extra_matrices + static_cast<size_t>(extra) * 16,
|
||||
extra_matrices + static_cast<size_t>(extra + 1) * 16,
|
||||
extra_matrices_.begin() + static_cast<size_t>(extra) * 16);
|
||||
}
|
||||
room_ready_ = true;
|
||||
reset();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int reset() noexcept {
|
||||
qmf_history_.fill(0.0);
|
||||
hybrid_low_history_.fill({});
|
||||
hybrid_high_history_.fill({});
|
||||
synthesis_history_.fill(0.0);
|
||||
std::fill(allpass_memory_.begin(), allpass_memory_.end(), Complex{});
|
||||
std::fill(allpass_positions_.begin(), allpass_positions_.end(), 0u);
|
||||
std::fill(room_memory_.begin(), room_memory_.end(), Complex{});
|
||||
room_position_ = 0;
|
||||
error_[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* error() const noexcept {
|
||||
return error_[0] ? error_ : "";
|
||||
}
|
||||
|
||||
int process(
|
||||
const double* input,
|
||||
const double* gains,
|
||||
const double* room_sends,
|
||||
double output_gain,
|
||||
double* output) noexcept {
|
||||
if (!kernels_ready_ || !room_ready_) {
|
||||
return fail("binaural renderer is not configured");
|
||||
}
|
||||
if (!input || !gains || !room_sends || !output || !std::isfinite(output_gain)) {
|
||||
return fail("invalid binaural process arguments");
|
||||
}
|
||||
for (int slot = 0; slot < kSlots; ++slot) {
|
||||
std::array<Complex, kChannels * kQmf> qmf{};
|
||||
std::array<Complex, kChannels * kHybrid> hybrid{};
|
||||
analyze_qmf(input + static_cast<size_t>(slot) * 64 * kChannels, qmf);
|
||||
analyze_hybrid(qmf, hybrid);
|
||||
|
||||
std::array<Complex, kEars * kHybrid> rendered{};
|
||||
std::array<Complex, kHybrid> room_input{};
|
||||
for (int source = kChannels - 1; source >= 0; --source) {
|
||||
for (int band = 0; band < kHybrid; ++band) {
|
||||
const Complex value = hybrid[source * kHybrid + band];
|
||||
room_input[band] = add(room_input[band], scale(value, room_sends[source]));
|
||||
for (int ear = 0; ear < kEars; ++ear) {
|
||||
const size_t gain_index =
|
||||
(((static_cast<size_t>(source) * kEars + ear) * kHybrid + band) * 2);
|
||||
const Complex gain{gains[gain_index], gains[gain_index + 1]};
|
||||
rendered[ear * kHybrid + band] = add(
|
||||
rendered[ear * kHybrid + band], mul(value, gain));
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto room = process_room(room_input);
|
||||
for (size_t index = 0; index < rendered.size(); ++index) {
|
||||
rendered[index] = add(rendered[index], room[index]);
|
||||
}
|
||||
|
||||
std::array<Complex, kEars * kQmf> qmf_output{};
|
||||
synthesize_hybrid(rendered, qmf_output);
|
||||
for (int ear = 0; ear < kEars; ++ear) {
|
||||
std::array<double, 64> samples{};
|
||||
synthesize_qmf(qmf_output.data() + ear * kQmf, ear, samples);
|
||||
for (int sample = 0; sample < 64; ++sample) {
|
||||
output[(static_cast<size_t>(slot) * 64 + sample) * 2 + ear] =
|
||||
samples[sample] * output_gain;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
int fail(const char* message) noexcept {
|
||||
std::snprintf(error_, sizeof(error_), "%s", message);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void initialize_fft() noexcept {
|
||||
for (int index = 0; index < 128; ++index) {
|
||||
int value = index;
|
||||
int reversed = 0;
|
||||
for (int bit = 0; bit < 7; ++bit) {
|
||||
reversed = (reversed << 1) | (value & 1);
|
||||
value >>= 1;
|
||||
}
|
||||
bit_reverse_[index] = static_cast<uint8_t>(reversed);
|
||||
}
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
const double angle = -kPi * static_cast<double>(phase) / 128.0;
|
||||
premod_[phase] = {std::cos(angle), std::sin(angle)};
|
||||
const double post_angle =
|
||||
-3.0 * (static_cast<double>(phase) + 0.5) * kPi / 128.0;
|
||||
post_[phase] = {std::cos(post_angle), std::sin(post_angle)};
|
||||
even_post_[phase] = {0.0, (phase & 1) ? -1.0 : 1.0};
|
||||
}
|
||||
}
|
||||
|
||||
void fft128(std::array<Complex, 128>& values) const noexcept {
|
||||
for (int index = 0; index < 128; ++index) {
|
||||
const int reversed = bit_reverse_[index];
|
||||
if (reversed > index) {
|
||||
std::swap(values[index], values[reversed]);
|
||||
}
|
||||
}
|
||||
for (int length = 2; length <= 128; length <<= 1) {
|
||||
const double angle = -2.0 * kPi / static_cast<double>(length);
|
||||
const Complex step{std::cos(angle), std::sin(angle)};
|
||||
for (int start = 0; start < 128; start += length) {
|
||||
Complex rotation{1.0, 0.0};
|
||||
for (int offset = 0; offset < length / 2; ++offset) {
|
||||
const Complex even = values[start + offset];
|
||||
const Complex odd = mul(values[start + offset + length / 2], rotation);
|
||||
values[start + offset] = {even.re + odd.re, even.im + odd.im};
|
||||
values[start + offset + length / 2] = {
|
||||
even.re - odd.re, even.im - odd.im};
|
||||
rotation = mul(rotation, step);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void qmf_transform(const std::array<double, 64>& source,
|
||||
std::array<Complex, 64>& target) const noexcept {
|
||||
std::array<Complex, 128> work{};
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
work[phase] = scale(premod_[phase], source[phase]);
|
||||
}
|
||||
fft128(work);
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
target[band] = mul(work[band], post_[band]);
|
||||
}
|
||||
}
|
||||
|
||||
void analyze_qmf(const double* input,
|
||||
std::array<Complex, kChannels * kQmf>& output) noexcept {
|
||||
for (int channel = 0; channel < kChannels; ++channel) {
|
||||
for (int lag = 9; lag > 0; --lag) {
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
qmf_history_[qmf_history_index(lag, channel, phase)] =
|
||||
qmf_history_[qmf_history_index(lag - 1, channel, phase)];
|
||||
}
|
||||
}
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
qmf_history_[qmf_history_index(0, channel, phase)] =
|
||||
input[phase * kChannels + channel];
|
||||
}
|
||||
std::array<double, 64> even{};
|
||||
std::array<double, 64> odd{};
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
for (int lag = 0; lag < 10; ++lag) {
|
||||
const double value =
|
||||
qmf_history_[qmf_history_index(lag, channel, phase)] *
|
||||
qmf_analysis_[phase * 10 + lag];
|
||||
(lag & 1 ? odd[phase] : even[phase]) += value;
|
||||
}
|
||||
}
|
||||
std::array<Complex, 64> even_fft{};
|
||||
std::array<Complex, 64> odd_fft{};
|
||||
qmf_transform(even, even_fft);
|
||||
qmf_transform(odd, odd_fft);
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
output[channel * 64 + band] = add(
|
||||
odd_fft[band], mul(even_fft[band], even_post_[band]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void analyze_hybrid(
|
||||
const std::array<Complex, kChannels * kQmf>& qmf,
|
||||
std::array<Complex, kChannels * kHybrid>& output) noexcept {
|
||||
for (int channel = 0; channel < kChannels; ++channel) {
|
||||
for (int lag = 12; lag > 0; --lag) {
|
||||
for (int band = 0; band < 3; ++band) {
|
||||
hybrid_low_history_[hybrid_low_history_index(lag, channel, band)] =
|
||||
hybrid_low_history_[hybrid_low_history_index(lag - 1, channel, band)];
|
||||
}
|
||||
}
|
||||
for (int band = 0; band < 3; ++band) {
|
||||
hybrid_low_history_[hybrid_low_history_index(0, channel, band)] =
|
||||
qmf[channel * 64 + band];
|
||||
}
|
||||
for (int output_band = 0; output_band < 16; ++output_band) {
|
||||
Complex value{};
|
||||
for (int lag = 0; lag < 13; ++lag) {
|
||||
for (int input_band = 0; input_band < 3; ++input_band) {
|
||||
const Complex source = hybrid_low_history_[
|
||||
hybrid_low_history_index(lag, channel, input_band)];
|
||||
const double components[2]{source.re, source.im};
|
||||
for (int input_component = 0; input_component < 2; ++input_component) {
|
||||
value.re += components[input_component] * hybrid_low_[
|
||||
hybrid_low_kernel_index(input_band, input_component, lag,
|
||||
output_band, 0)];
|
||||
value.im += components[input_component] * hybrid_low_[
|
||||
hybrid_low_kernel_index(input_band, input_component, lag,
|
||||
output_band, 1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
output[channel * kHybrid + output_band] = value;
|
||||
}
|
||||
for (int band = 0; band < 61; ++band) {
|
||||
output[channel * kHybrid + 16 + band] =
|
||||
hybrid_high_history_[hybrid_high_history_index(0, channel, band)];
|
||||
for (int delay = 0; delay < 5; ++delay) {
|
||||
hybrid_high_history_[hybrid_high_history_index(delay, channel, band)] =
|
||||
hybrid_high_history_[hybrid_high_history_index(delay + 1, channel, band)];
|
||||
}
|
||||
hybrid_high_history_[hybrid_high_history_index(5, channel, band)] =
|
||||
qmf[channel * 64 + 3 + band];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::array<Complex, kEars * kHybrid> process_room(
|
||||
const std::array<Complex, kHybrid>& input) noexcept {
|
||||
std::array<Complex, 64> filtered{};
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
filtered[band] = scale(input[band], 0.70710677);
|
||||
}
|
||||
for (size_t stage = 0; stage < allpass_delays_.size(); ++stage) {
|
||||
const uint32_t position = allpass_positions_[stage];
|
||||
const double gain = allpass_gains_[stage];
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
Complex& memory = allpass_memory_[
|
||||
allpass_offsets_[stage] + static_cast<size_t>(position) * 64 + band];
|
||||
const Complex residual = add(filtered[band], scale(memory, -gain));
|
||||
filtered[band] = add(scale(residual, gain), memory);
|
||||
memory = residual;
|
||||
}
|
||||
allpass_positions_[stage] = (position + 1) % allpass_delays_[stage];
|
||||
}
|
||||
|
||||
std::array<Complex, 64 * 4> branches{};
|
||||
std::array<Complex, 64 * 4> taps{};
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
for (int branch = 0; branch < 4; ++branch) {
|
||||
Complex value = filtered[band];
|
||||
for (int source = 0; source < 4; ++source) {
|
||||
const uint32_t position =
|
||||
(room_position_ + room_capacity_ - fdn_delays_[source]) % room_capacity_;
|
||||
value = add(value, scale(room_memory_[
|
||||
room_memory_index(position, band, source)],
|
||||
fdn_matrix_[branch * 4 + source]));
|
||||
}
|
||||
branches[band * 4 + branch] = value;
|
||||
const uint32_t tap_position =
|
||||
(room_position_ + room_capacity_ -
|
||||
(output_tap_delay_ % room_capacity_)) % room_capacity_;
|
||||
taps[band * 4 + branch] =
|
||||
room_memory_[room_memory_index(tap_position, band, branch)];
|
||||
}
|
||||
}
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
for (int branch = 0; branch < 4; ++branch) {
|
||||
room_memory_[room_memory_index(room_position_, band, branch)] =
|
||||
mul(branches[band * 4 + branch], feedback_[band][branch]);
|
||||
}
|
||||
}
|
||||
room_position_ = (room_position_ + 1) % room_capacity_;
|
||||
|
||||
std::array<Complex, 64 * 4> extra{};
|
||||
for (size_t index = 0; index < extra_delays_.size(); ++index) {
|
||||
const uint32_t position =
|
||||
(room_position_ + room_capacity_ -
|
||||
((extra_delays_[index] + 1) % room_capacity_)) % room_capacity_;
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
for (int target = 0; target < 4; ++target) {
|
||||
Complex mixed{};
|
||||
for (int source = 0; source < 4; ++source) {
|
||||
mixed = add(mixed, scale(room_memory_[
|
||||
room_memory_index(position, band, source)],
|
||||
extra_matrices_[index * 16 + target * 4 + source]));
|
||||
}
|
||||
extra[band * 4 + target] = add(
|
||||
extra[band * 4 + target],
|
||||
mul(mixed, extra_fields_[index * 64 + band]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::array<Complex, kEars * kHybrid> output{};
|
||||
for (int ear = 0; ear < 2; ++ear) {
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
Complex value{};
|
||||
for (int branch = 0; branch < 4; ++branch) {
|
||||
const Complex signal = add(
|
||||
scale(taps[band * 4 + branch], output_taps_[band][branch]),
|
||||
extra[band * 4 + branch]);
|
||||
value = add(value, mul(
|
||||
signal, output_matrix_[ear][band][branch]));
|
||||
}
|
||||
output[ear * kHybrid + band] = value;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void synthesize_hybrid(
|
||||
const std::array<Complex, kEars * kHybrid>& input,
|
||||
std::array<Complex, kEars * kQmf>& output) const noexcept {
|
||||
for (size_t mapping = 0; mapping < hybrid_values_.size(); ++mapping) {
|
||||
const int16_t* index = hybrid_indices_.data() + mapping * 4;
|
||||
const int input_band = index[0];
|
||||
const int input_component = index[1];
|
||||
const int output_band = index[2];
|
||||
const int output_component = index[3];
|
||||
const double gain = hybrid_values_[mapping];
|
||||
for (int ear = 0; ear < 2; ++ear) {
|
||||
const Complex source = input[ear * kHybrid + input_band];
|
||||
Complex& target = output[ear * kQmf + output_band];
|
||||
const double component = input_component == 0 ? source.re : source.im;
|
||||
(output_component == 0 ? target.re : target.im) += component * gain;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void synthesize_qmf(const Complex* input, int ear,
|
||||
std::array<double, 64>& output) noexcept {
|
||||
std::array<double, 64 * kRank> features{};
|
||||
std::array<double, 128> flat{};
|
||||
for (int band = 0; band < 64; ++band) {
|
||||
flat[band * 2] = input[band].re;
|
||||
flat[band * 2 + 1] = input[band].im;
|
||||
}
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
for (int rank = 0; rank < kRank; ++rank) {
|
||||
double value = 0.0;
|
||||
const size_t base = (static_cast<size_t>(phase) * kRank + rank) * 128;
|
||||
for (int component = 0; component < 128; ++component) {
|
||||
value += flat[component] * qmf_basis_[base + component];
|
||||
}
|
||||
features[phase * kRank + rank] = value;
|
||||
}
|
||||
}
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
double value = 0.0;
|
||||
for (int lag = 0; lag < 10; ++lag) {
|
||||
for (int rank = 0; rank < kRank; ++rank) {
|
||||
const double feature = lag == 0
|
||||
? features[phase * kRank + rank]
|
||||
: synthesis_history_[synthesis_history_index(
|
||||
ear, lag - 1, phase, rank)];
|
||||
value += feature * qmf_taps_[
|
||||
((static_cast<size_t>(phase) * 10 + lag) * kRank + rank)];
|
||||
}
|
||||
}
|
||||
output[phase] = value;
|
||||
}
|
||||
for (int lag = 8; lag > 0; --lag) {
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
for (int rank = 0; rank < kRank; ++rank) {
|
||||
synthesis_history_[synthesis_history_index(ear, lag, phase, rank)] =
|
||||
synthesis_history_[synthesis_history_index(
|
||||
ear, lag - 1, phase, rank)];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int phase = 0; phase < 64; ++phase) {
|
||||
for (int rank = 0; rank < kRank; ++rank) {
|
||||
synthesis_history_[synthesis_history_index(ear, 0, phase, rank)] =
|
||||
features[phase * kRank + rank];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static size_t qmf_history_index(int lag, int channel, int phase) noexcept {
|
||||
return (static_cast<size_t>(lag) * kChannels + channel) * 64 + phase;
|
||||
}
|
||||
|
||||
static size_t hybrid_low_history_index(int lag, int channel, int band) noexcept {
|
||||
return (static_cast<size_t>(lag) * kChannels + channel) * 3 + band;
|
||||
}
|
||||
|
||||
static size_t hybrid_high_history_index(int delay, int channel, int band) noexcept {
|
||||
return (static_cast<size_t>(delay) * kChannels + channel) * 61 + band;
|
||||
}
|
||||
|
||||
static size_t hybrid_low_kernel_index(
|
||||
int input_band, int input_component, int lag,
|
||||
int output_band, int output_component) noexcept {
|
||||
return (((static_cast<size_t>(input_band) * 2 + input_component) * 13 + lag) *
|
||||
16 + output_band) * 2 + output_component;
|
||||
}
|
||||
|
||||
size_t room_memory_index(uint32_t position, int band, int branch) const noexcept {
|
||||
return (static_cast<size_t>(position) * 64 + band) * 4 + branch;
|
||||
}
|
||||
|
||||
static size_t synthesis_history_index(
|
||||
int ear, int lag, int phase, int rank) noexcept {
|
||||
return (((static_cast<size_t>(ear) * 9 + lag) * 64 + phase) * kRank + rank);
|
||||
}
|
||||
|
||||
bool kernels_ready_ = false;
|
||||
bool room_ready_ = false;
|
||||
std::array<double, 64 * 10> qmf_analysis_{};
|
||||
std::vector<double> hybrid_low_;
|
||||
std::vector<int16_t> hybrid_indices_;
|
||||
std::vector<double> hybrid_values_;
|
||||
std::array<double, 64 * kRank * 128> qmf_basis_{};
|
||||
std::array<double, 64 * 10 * kRank> qmf_taps_{};
|
||||
|
||||
std::array<double, 10 * kChannels * 64> qmf_history_{};
|
||||
std::array<Complex, 13 * kChannels * 3> hybrid_low_history_{};
|
||||
std::array<Complex, 6 * kChannels * 61> hybrid_high_history_{};
|
||||
std::array<double, kEars * 9 * 64 * kRank> synthesis_history_{};
|
||||
|
||||
uint32_t room_bands_ = 0;
|
||||
std::vector<uint32_t> allpass_delays_;
|
||||
std::vector<double> allpass_gains_;
|
||||
std::vector<size_t> allpass_offsets_;
|
||||
std::vector<uint32_t> allpass_positions_;
|
||||
std::vector<Complex> allpass_memory_;
|
||||
std::array<uint32_t, 4> fdn_delays_{};
|
||||
std::array<double, 16> fdn_matrix_{};
|
||||
uint32_t room_capacity_ = 0;
|
||||
uint32_t output_tap_delay_ = 0;
|
||||
std::array<std::array<Complex, 4>, 64> feedback_{};
|
||||
std::array<std::array<double, 4>, 64> output_taps_{};
|
||||
std::array<std::array<std::array<Complex, 4>, 64>, 2> output_matrix_{};
|
||||
std::vector<Complex> room_memory_;
|
||||
uint32_t room_position_ = 0;
|
||||
std::vector<uint32_t> extra_delays_;
|
||||
std::vector<Complex> extra_fields_;
|
||||
std::vector<double> extra_matrices_;
|
||||
|
||||
std::array<uint8_t, 128> bit_reverse_{};
|
||||
std::array<Complex, 64> premod_{};
|
||||
std::array<Complex, 64> post_{};
|
||||
std::array<Complex, 64> even_post_{};
|
||||
char error_[256]{};
|
||||
};
|
||||
|
||||
} // namespace ejoc::binaural
|
||||
|
||||
extern "C" {
|
||||
|
||||
ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void) {
|
||||
return new (std::nothrow) ejoc::binaural::Renderer();
|
||||
}
|
||||
|
||||
void EJOC_CALL ejoc_binaural_renderer_destroy(ejoc_binaural_renderer_handle handle) {
|
||||
delete static_cast<ejoc::binaural::Renderer*>(handle);
|
||||
}
|
||||
|
||||
int EJOC_CALL ejoc_binaural_renderer_reset(ejoc_binaural_renderer_handle handle) {
|
||||
return handle ? static_cast<ejoc::binaural::Renderer*>(handle)->reset() : -1;
|
||||
}
|
||||
|
||||
const char* EJOC_CALL ejoc_binaural_renderer_last_error(
|
||||
ejoc_binaural_renderer_handle handle) {
|
||||
return handle ? static_cast<ejoc::binaural::Renderer*>(handle)->error()
|
||||
: "null binaural renderer handle";
|
||||
}
|
||||
|
||||
int EJOC_CALL ejoc_binaural_renderer_configure_kernels(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
const double* qmf_analysis,
|
||||
const double* hybrid_low,
|
||||
const int16_t* hybrid_indices,
|
||||
const double* hybrid_values,
|
||||
uint32_t hybrid_count,
|
||||
const double* qmf_basis,
|
||||
const double* qmf_taps) {
|
||||
return handle ? static_cast<ejoc::binaural::Renderer*>(handle)->configure_kernels(
|
||||
qmf_analysis, hybrid_low, hybrid_indices, hybrid_values,
|
||||
hybrid_count, qmf_basis, qmf_taps) : -1;
|
||||
}
|
||||
|
||||
int EJOC_CALL ejoc_binaural_renderer_configure_room(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
uint32_t bands,
|
||||
uint32_t allpass_count,
|
||||
const uint32_t* allpass_delays,
|
||||
const double* allpass_gains,
|
||||
const uint32_t* fdn_delays,
|
||||
const double* fdn_matrix,
|
||||
uint32_t output_tap_delay,
|
||||
const double* feedback_complex,
|
||||
const double* output_taps,
|
||||
const double* output_complex,
|
||||
uint32_t extra_count,
|
||||
const uint32_t* extra_delays,
|
||||
const double* extra_fields_complex,
|
||||
const double* extra_matrices) {
|
||||
return handle ? static_cast<ejoc::binaural::Renderer*>(handle)->configure_room(
|
||||
bands, allpass_count, allpass_delays, allpass_gains,
|
||||
fdn_delays, fdn_matrix, output_tap_delay,
|
||||
feedback_complex, output_taps, output_complex,
|
||||
extra_count, extra_delays, extra_fields_complex, extra_matrices) : -1;
|
||||
}
|
||||
|
||||
int EJOC_CALL ejoc_binaural_renderer_process(
|
||||
ejoc_binaural_renderer_handle handle,
|
||||
const double* input16_interleaved,
|
||||
const double* gains_complex,
|
||||
const double* room_sends,
|
||||
double output_gain,
|
||||
double* output_stereo_interleaved) {
|
||||
return handle ? static_cast<ejoc::binaural::Renderer*>(handle)->process(
|
||||
input16_interleaved, gains_complex, room_sends,
|
||||
output_gain, output_stereo_interleaved) : -1;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,252 +0,0 @@
|
||||
#include "binaural/jochrtf_runtime.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace joc::binaural {
|
||||
|
||||
namespace {
|
||||
|
||||
double max_delay_bound(const hrtf::Field& field) {
|
||||
double maximum = 0.0;
|
||||
for (const double value : field.delay_bounds) {
|
||||
maximum = std::max(maximum, value);
|
||||
}
|
||||
return maximum;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool profile_from_name(const char* name, Profile* out) {
|
||||
if (name == nullptr || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (std::strcmp(name, "near") == 0) { *out = Profile::Near; return true; }
|
||||
if (std::strcmp(name, "mid") == 0) { *out = Profile::Mid; return true; }
|
||||
if (std::strcmp(name, "far") == 0) { *out = Profile::Far; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
JochrtfRuntime::~JochrtfRuntime() {
|
||||
if (handle_ != nullptr) {
|
||||
ejoc_jochrtf_destroy(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Status JochrtfRuntime::open(const hrtf::Field& field, const hrtf::Kernels& kernels,
|
||||
Profile profile, const RoomConstants& room) {
|
||||
if (handle_ != nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kRender, "binaural runtime already open");
|
||||
}
|
||||
if (field.coefficients.size() != static_cast<std::size_t>(hrtf::kShTerms * hrtf::kEars *
|
||||
hrtf::kHybridBands * 2) ||
|
||||
field.band_centers_hz.size() != static_cast<std::size_t>(hrtf::kHybridBands)) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"compiled HRTF field has unexpected array sizes");
|
||||
}
|
||||
handle_ = ejoc_jochrtf_create();
|
||||
if (handle_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_OUT_OF_MEMORY, stage::kRender,
|
||||
"ejoc_jochrtf_create failed");
|
||||
}
|
||||
profile_ = profile;
|
||||
|
||||
if (ejoc_jochrtf_configure_kernels(
|
||||
handle_, kernels.qmf_analysis.data(), kernels.hybrid_low.data(),
|
||||
kernels.hybrid_indices.data(), kernels.hybrid_values.data(), kernels.hybrid_count,
|
||||
kernels.qmf_basis.data(), kernels.qmf_taps.data()) != 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
std::string("configure_kernels failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
if (ejoc_jochrtf_configure_field(handle_, field.coefficients.data(),
|
||||
field.delay_coefficients.data(),
|
||||
field.delay_bounds.data(), field.band_centers_hz.data(),
|
||||
field.measurement_radius_m) != 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
std::string("configure_field failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
if (ejoc_jochrtf_configure_room(
|
||||
handle_, room.dims, room.listener, room.walls, room.speed_of_sound, room.fdn_delays,
|
||||
room.fdn_feedback, room.damping, room.fdn_output_gain, room.allpass_delays,
|
||||
room.allpass_gains, room.enable_early_reflections, room.enable_late_room) != 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
||||
std::string("configure_room failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
|
||||
maximum_hrtf_delay_ =
|
||||
static_cast<std::int64_t>(std::ceil(max_delay_bound(field) - 1e-9));
|
||||
hrtf_history_slots_ = static_cast<std::uint32_t>(std::max<std::int64_t>(1, (maximum_hrtf_delay_ + 63) / 64));
|
||||
staging_.assign(kBlockSamples * kSourceCount, 0.0);
|
||||
block_output_.assign(kBlockSamples * 2u, 0.0);
|
||||
output_.clear();
|
||||
staged_ = 0;
|
||||
input_samples_ = 0;
|
||||
processed_samples_ = 0;
|
||||
blocks_processed_ = 0;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status JochrtfRuntime::process_block() {
|
||||
double positions[timeline::kTimelineObjects][3] = {};
|
||||
const Status queried = timeline_.positions_at(static_cast<std::int64_t>(processed_samples_),
|
||||
&positions[0][0],
|
||||
timeline::kTimelineObjects);
|
||||
if (!queried.ok()) {
|
||||
return queried;
|
||||
}
|
||||
// The reference adapter calls set_source without a `fade` argument, so the
|
||||
// backend default (fade enabled) applies - the per-object path crossfade is
|
||||
// part of the reference behaviour, not an optional extra.
|
||||
constexpr std::uint32_t kFade = 1u;
|
||||
if (ejoc_jochrtf_set_source(handle_, 0u, kLfePosition,
|
||||
static_cast<std::uint32_t>(profile_), 1.0, 1u, 1u,
|
||||
kFade) != 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
||||
std::string("set_source(LFE) failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
for (std::uint32_t source = 0; source < timeline::kTimelineObjects; ++source) {
|
||||
if (ejoc_jochrtf_set_source(handle_, source + 1u, positions[source],
|
||||
static_cast<std::uint32_t>(profile_), 1.0, 1u, 0u,
|
||||
kFade) != 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
||||
std::string("set_source(object ") + std::to_string(source + 1u) +
|
||||
") failed: " + (message != nullptr ? message : "unknown"));
|
||||
}
|
||||
}
|
||||
const int trimmed = ejoc_jochrtf_process(handle_, staging_.data(), kBlockSamples, 1.0,
|
||||
block_output_.data());
|
||||
if (trimmed < 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
||||
std::string("sofa process failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
if (trimmed > 0) {
|
||||
output_.insert(output_.end(), block_output_.begin(),
|
||||
block_output_.begin() + static_cast<std::ptrdiff_t>(trimmed) * 2);
|
||||
}
|
||||
staged_ = 0;
|
||||
processed_samples_ += kBlockSamples;
|
||||
++blocks_processed_;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status JochrtfRuntime::submit_frame(const float* objects16_planar,
|
||||
const oamd::OamdUpdate* update, std::int64_t frame_index,
|
||||
std::int64_t outer_sample_offset,
|
||||
std::int64_t object_delay_samples) {
|
||||
if (handle_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kRender, "binaural runtime is not open");
|
||||
}
|
||||
if (objects16_planar == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, "null frame");
|
||||
}
|
||||
// A frame without an ID11 payload submits nothing at all (the reference only
|
||||
if (update != nullptr) {
|
||||
const Status submitted = timeline_.submit_update(
|
||||
*update, frame_index * JOC_FRAME_SAMPLES, outer_sample_offset, object_delay_samples,
|
||||
static_cast<std::int64_t>(input_samples_));
|
||||
if (!submitted.ok()) {
|
||||
return submitted;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t offset = 0;
|
||||
while (offset < JOC_FRAME_SAMPLES) {
|
||||
const std::size_t room = kBlockSamples - staged_;
|
||||
const std::size_t count = std::min<std::size_t>(room, JOC_FRAME_SAMPLES - offset);
|
||||
for (std::size_t sample = 0; sample < count; ++sample) {
|
||||
double* row = staging_.data() + (staged_ + sample) * kSourceCount;
|
||||
for (std::size_t channel = 0; channel < kSourceCount; ++channel) {
|
||||
row[channel] = static_cast<double>(
|
||||
objects16_planar[channel * JOC_FRAME_SAMPLES + offset + sample]);
|
||||
}
|
||||
}
|
||||
staged_ += count;
|
||||
offset += count;
|
||||
if (staged_ == kBlockSamples) {
|
||||
const Status status = process_block();
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
input_samples_ += JOC_FRAME_SAMPLES;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::uint32_t JochrtfRuntime::finish_capacity(double tail_seconds) const {
|
||||
std::int64_t requested = tail_samples_;
|
||||
if (tail_seconds >= 0.0) {
|
||||
requested = static_cast<std::int64_t>(std::ceil(tail_seconds * 48000.0 - 1e-9));
|
||||
}
|
||||
const std::int64_t hrtf_bound = static_cast<std::int64_t>(hrtf_history_slots_) * 64;
|
||||
const std::int64_t early_bound = hrtf_bound + 2048 + 256 * 64;
|
||||
std::int64_t drain = std::max(requested, early_bound) + 961;
|
||||
drain = ((drain + 63) / 64) * 64;
|
||||
return static_cast<std::uint32_t>(drain);
|
||||
}
|
||||
|
||||
Status JochrtfRuntime::reset() {
|
||||
if (handle_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kRender, "binaural runtime is not open");
|
||||
}
|
||||
if (ejoc_jochrtf_reset(handle_) != 0) {
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender, "sofa reset failed");
|
||||
}
|
||||
timeline_.reset();
|
||||
output_.clear();
|
||||
staged_ = 0;
|
||||
input_samples_ = 0;
|
||||
processed_samples_ = 0;
|
||||
blocks_processed_ = 0;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status JochrtfRuntime::finish(std::uint32_t flush_samples, std::vector<double>* out) {
|
||||
if (handle_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kRender, "binaural runtime is not open");
|
||||
}
|
||||
if (out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, "null output");
|
||||
}
|
||||
out->clear();
|
||||
if (flush_samples == 0u) {
|
||||
return Status::success();
|
||||
}
|
||||
std::vector<double> chunk(static_cast<std::size_t>(kBlockSamples) * 2u, 0.0);
|
||||
std::uint32_t produced_total = 0;
|
||||
std::uint32_t remaining = flush_samples;
|
||||
while (remaining > 0) {
|
||||
const std::uint32_t request = std::min<std::uint32_t>(remaining, kBlockSamples);
|
||||
const int produced = ejoc_jochrtf_finish(handle_, request, chunk.data(), request);
|
||||
if (produced < 0) {
|
||||
const char* message = ejoc_jochrtf_last_error(handle_);
|
||||
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
||||
std::string("sofa finish failed: ") +
|
||||
(message != nullptr ? message : "unknown"));
|
||||
}
|
||||
if (produced == 0) {
|
||||
break;
|
||||
}
|
||||
out->insert(out->end(), chunk.begin(),
|
||||
chunk.begin() + static_cast<std::ptrdiff_t>(produced) * 2);
|
||||
produced_total += static_cast<std::uint32_t>(produced);
|
||||
remaining -= std::min<std::uint32_t>(remaining, static_cast<std::uint32_t>(produced));
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::binaural
|
||||
@@ -1,101 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "eac3joc_core.h"
|
||||
#include "foundation/status.h"
|
||||
#include "hrtf/jochrtf.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
#include "timeline/position_timeline.h"
|
||||
|
||||
namespace joc::binaural {
|
||||
|
||||
// ADM direction of the LFE source, as the reference passes it.
|
||||
inline constexpr double kLfePosition[3] = {0.0, 1.0, 0.0};
|
||||
inline constexpr int kSourceCount = 16;
|
||||
inline constexpr std::uint32_t kBlockSamples = 512;
|
||||
|
||||
// Room constants the reference's native bridge passes for the default shoebox.
|
||||
struct RoomConstants {
|
||||
double dims[3] = {18.0, 18.0, 14.0};
|
||||
double listener[3] = {9.0, 9.0, 7.0};
|
||||
double walls[6] = {0.62, 0.60, 0.58, 0.61, 0.52, 0.56};
|
||||
double speed_of_sound = 343.3;
|
||||
std::uint32_t fdn_delays[4] = {1427u, 1783u, 1973u, 2099u};
|
||||
double fdn_feedback[4] = {0.7853685923259284, 0.7394299865898056, 0.7160221718631921,
|
||||
0.7009092068085467};
|
||||
double damping = 0.32;
|
||||
double fdn_output_gain = 0.22;
|
||||
std::uint32_t allpass_delays[2] = {113u, 331u};
|
||||
double allpass_gains[2] = {0.63, 0.51};
|
||||
std::uint32_t enable_early_reflections = 1;
|
||||
std::uint32_t enable_late_room = 1;
|
||||
};
|
||||
|
||||
enum class Profile : std::uint32_t { Near = 0, Mid = 1, Far = 2 };
|
||||
|
||||
bool profile_from_name(const char* name, Profile* out);
|
||||
|
||||
class JochrtfRuntime {
|
||||
public:
|
||||
JochrtfRuntime() = default;
|
||||
~JochrtfRuntime();
|
||||
|
||||
JochrtfRuntime(const JochrtfRuntime&) = delete;
|
||||
JochrtfRuntime& operator=(const JochrtfRuntime&) = delete;
|
||||
|
||||
Status open(const hrtf::Field& field, const hrtf::Kernels& kernels, Profile profile,
|
||||
const RoomConstants& room = RoomConstants{});
|
||||
|
||||
Status submit_frame(const float* objects16_planar, const oamd::OamdUpdate* update,
|
||||
std::int64_t frame_index, std::int64_t outer_sample_offset,
|
||||
std::int64_t object_delay_samples);
|
||||
|
||||
// Resets the kernel, the timeline and the counters (plan 31.2).
|
||||
Status reset();
|
||||
|
||||
// Drains the room tail. `flush_samples` is the drain length; the reference
|
||||
Status finish(std::uint32_t flush_samples, std::vector<double>* out);
|
||||
|
||||
// Program output (input minus the 961-sample kernel latency), interleaved.
|
||||
const std::vector<double>& output() const { return output_; }
|
||||
|
||||
void take_output(std::vector<double>* out) {
|
||||
out->swap(output_);
|
||||
output_.clear();
|
||||
}
|
||||
|
||||
std::uint64_t input_samples() const { return input_samples_; }
|
||||
std::uint64_t blocks_processed() const { return blocks_processed_; }
|
||||
std::size_t staged_samples() const { return staged_; }
|
||||
const timeline::OamdPositionTimeline& timeline() const { return timeline_; }
|
||||
|
||||
// bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。
|
||||
void set_fixed_position(int object_index, const double position[3]) {
|
||||
timeline_.set_fixed_position(object_index, position);
|
||||
}
|
||||
|
||||
// finish_output_capacity as the reference computes it (plan 21.4).
|
||||
std::uint32_t finish_capacity(double tail_seconds) const;
|
||||
|
||||
private:
|
||||
Status process_block();
|
||||
|
||||
ejoc_jochrtf_handle handle_ = nullptr;
|
||||
Profile profile_ = Profile::Mid;
|
||||
timeline::OamdPositionTimeline timeline_;
|
||||
std::vector<double> staging_;
|
||||
std::size_t staged_ = 0;
|
||||
std::vector<double> block_output_;
|
||||
std::vector<double> output_;
|
||||
std::uint64_t input_samples_ = 0;
|
||||
std::uint64_t processed_samples_ = 0;
|
||||
std::uint64_t blocks_processed_ = 0;
|
||||
std::int64_t maximum_hrtf_delay_ = 0;
|
||||
std::uint32_t hrtf_history_slots_ = 1;
|
||||
std::uint32_t tail_samples_ = 61200;
|
||||
};
|
||||
|
||||
} // namespace joc::binaural
|
||||
@@ -0,0 +1,190 @@
|
||||
"""Direct ID11/OAMD position scheduling for the binaural render path."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import numpy as np
|
||||
|
||||
from adm_atmos import q_to_adm_xyz
|
||||
from oamd_bits import JocFieldState, frame_update
|
||||
from oamd_tracks import align_metadata_sample
|
||||
from variant_error import UnsupportedVariantError
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PositionTransition:
|
||||
start_sample: int
|
||||
duration_samples: int
|
||||
origin: np.ndarray
|
||||
target: np.ndarray
|
||||
|
||||
@property
|
||||
def end_sample(self) -> int:
|
||||
return self.start_sample + self.duration_samples
|
||||
|
||||
|
||||
class _ObjectPositionTrack:
|
||||
def __init__(self):
|
||||
self.initial = np.zeros(3, dtype=np.float64)
|
||||
self.last_target = self.initial.copy()
|
||||
self.transitions: list[PositionTransition] = []
|
||||
self.cursor = 0
|
||||
self.last_query_sample = -1
|
||||
|
||||
def set_initial(self, position):
|
||||
target = np.asarray(position, dtype=np.float64)
|
||||
self.initial = target.copy()
|
||||
self.last_target = target.copy()
|
||||
|
||||
def append(self, start_sample: int, duration_samples: int, target,
|
||||
object_index: int):
|
||||
start = int(start_sample)
|
||||
duration = int(duration_samples)
|
||||
if start < 0 or duration < 0:
|
||||
raise ValueError("position transition timing must be non-negative")
|
||||
target = np.asarray(target, dtype=np.float64)
|
||||
if self.transitions:
|
||||
previous = self.transitions[-1]
|
||||
if start < previous.end_sample:
|
||||
raise UnsupportedVariantError(
|
||||
"oamd", "overlapping_binaural_position_ramps",
|
||||
"同一对象的新位置更新在上一双耳 ramp 完成前到达",
|
||||
details={
|
||||
"object": object_index,
|
||||
"ramp_start_sample": previous.start_sample,
|
||||
"ramp_end_sample": previous.end_sample,
|
||||
"next_update_sample": start,
|
||||
})
|
||||
if start == previous.start_sample and previous.duration_samples == 0:
|
||||
self.transitions[-1] = PositionTransition(
|
||||
start, duration, previous.origin.copy(), target.copy())
|
||||
self.last_target = target.copy()
|
||||
return
|
||||
self.transitions.append(PositionTransition(
|
||||
start, duration, self.last_target.copy(), target.copy()))
|
||||
self.last_target = target.copy()
|
||||
|
||||
def position_at(self, sample: int) -> np.ndarray:
|
||||
sample = int(sample)
|
||||
if sample < self.last_query_sample:
|
||||
raise ValueError("binaural metadata positions must be queried monotonically")
|
||||
self.last_query_sample = sample
|
||||
while self.cursor < len(self.transitions):
|
||||
transition = self.transitions[self.cursor]
|
||||
if sample < transition.end_sample:
|
||||
break
|
||||
self.initial = transition.target.copy()
|
||||
self.cursor += 1
|
||||
if self.cursor >= len(self.transitions):
|
||||
return self.initial
|
||||
transition = self.transitions[self.cursor]
|
||||
if sample < transition.start_sample:
|
||||
return self.initial
|
||||
if transition.duration_samples == 0:
|
||||
return transition.target
|
||||
amount = (sample - transition.start_sample) / float(transition.duration_samples)
|
||||
return transition.origin + (transition.target - transition.origin) * amount
|
||||
|
||||
|
||||
class OamdPositionTimeline:
|
||||
"""Convert OAMD state updates into a sample-timed Cartesian trajectory."""
|
||||
|
||||
def __init__(self, object_count: int = 15, bed_positions=None):
|
||||
if object_count != 15:
|
||||
raise ValueError("JOC OAMD currently requires 15 object slots")
|
||||
self.object_count = int(object_count)
|
||||
# bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。
|
||||
self.bed_positions = dict(bed_positions or {})
|
||||
self.state = JocFieldState()
|
||||
self.tracks = [_ObjectPositionTrack() for _ in range(self.object_count)]
|
||||
self.initialized = False
|
||||
self.previous_targets: list[tuple[float, float, float] | None] = [
|
||||
None] * self.object_count
|
||||
self.payload_count = 0
|
||||
self.transition_count = 0
|
||||
self.last_coded_event_sample = -1
|
||||
|
||||
def _targets(self) -> list[tuple[float, float, float]]:
|
||||
q = self.state.q
|
||||
targets = []
|
||||
for object_index in range(1, self.object_count + 1):
|
||||
fixed = self.bed_positions.get(object_index)
|
||||
if fixed is not None:
|
||||
targets.append((float(fixed[0]), float(fixed[1]), float(fixed[2])))
|
||||
continue
|
||||
targets.append(q_to_adm_xyz(
|
||||
q[(object_index, "q1")],
|
||||
q[(object_index, "q2")],
|
||||
q[(object_index, "q3")],
|
||||
))
|
||||
return targets
|
||||
|
||||
def submit_update(self, update: dict, *, frame_start_sample: int,
|
||||
outer_sample_offset: int = 0,
|
||||
object_delay_samples: int = 1473,
|
||||
processed_sample: int = 0):
|
||||
"""Schedule one already-parsed :func:`oamd_bits.frame_update` result."""
|
||||
frame_start = int(frame_start_sample)
|
||||
outer_offset = int(outer_sample_offset)
|
||||
object_delay = int(object_delay_samples)
|
||||
if min(frame_start, outer_offset, object_delay) < 0:
|
||||
raise ValueError("OAMD frame, outer offset, and object delay must be non-negative")
|
||||
self.state.apply(update["values"])
|
||||
targets = self._targets()
|
||||
coded_event = (
|
||||
frame_start + outer_offset + int(update["block_offset_samples"]))
|
||||
if coded_event < self.last_coded_event_sample:
|
||||
raise UnsupportedVariantError(
|
||||
"oamd", "non_monotonic_binaural_updates",
|
||||
"双耳 OAMD 更新时间倒退",
|
||||
details={
|
||||
"event_sample": coded_event,
|
||||
"previous_event_sample": self.last_coded_event_sample,
|
||||
})
|
||||
self.last_coded_event_sample = coded_event
|
||||
|
||||
if not self.initialized:
|
||||
if int(processed_sample) > 0:
|
||||
raise UnsupportedVariantError(
|
||||
"oamd", "late_initial_binaural_state",
|
||||
"首个 OAMD 状态在双耳 PCM 已处理后才出现,无法回填 sample 0",
|
||||
details={
|
||||
"processed_sample": int(processed_sample),
|
||||
"first_event_sample": coded_event,
|
||||
})
|
||||
for index, target in enumerate(targets):
|
||||
self.tracks[index].set_initial(target)
|
||||
self.previous_targets[index] = target
|
||||
self.initialized = True
|
||||
self.payload_count += 1
|
||||
return
|
||||
|
||||
effective_ramp = max(0, int(update["ramp_duration_samples"]))
|
||||
transition_start = align_metadata_sample(coded_event + object_delay)
|
||||
for index, target in enumerate(targets):
|
||||
if self.previous_targets[index] == target:
|
||||
continue
|
||||
self.tracks[index].append(
|
||||
transition_start, effective_ramp, target, index + 1)
|
||||
self.previous_targets[index] = target
|
||||
self.transition_count += 1
|
||||
self.payload_count += 1
|
||||
|
||||
def submit_payload(self, payload, *, frame_start_sample: int,
|
||||
outer_sample_offset: int = 0,
|
||||
object_delay_samples: int = 1473,
|
||||
processed_sample: int = 0):
|
||||
update = frame_update(payload)
|
||||
self.submit_update(
|
||||
update,
|
||||
frame_start_sample=frame_start_sample,
|
||||
outer_sample_offset=outer_sample_offset,
|
||||
object_delay_samples=object_delay_samples,
|
||||
processed_sample=processed_sample,
|
||||
)
|
||||
return update
|
||||
|
||||
def positions_at(self, sample: int) -> np.ndarray:
|
||||
return np.stack(
|
||||
[track.position_at(sample) for track in self.tracks], axis=0
|
||||
).astype(np.float64, copy=False)
|
||||
@@ -0,0 +1,241 @@
|
||||
"""JOC frame adapter for the compiled .jochrtf renderer."""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
from binaural_metadata import OamdPositionTimeline
|
||||
from public_filterbank import ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||
|
||||
|
||||
SAMPLE_RATE = 48000
|
||||
FRAME_SAMPLES = 1536
|
||||
BINAURAL_BLOCK_SAMPLES = 512
|
||||
QMF_HOP_SAMPLES = 64
|
||||
BINAURAL_LATENCY_SAMPLES = ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||
SOURCE_CHANNELS = 16
|
||||
OUTPUT_CHANNELS = 2
|
||||
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
||||
DEFAULT_HRTF_DIR = PROJECT_DIR / "HRTF"
|
||||
|
||||
|
||||
def _resolve_hrtf_file(path: str | Path, suffix: str, label: str) -> Path:
|
||||
target = Path(path).expanduser().resolve()
|
||||
if target.suffix.lower() != suffix:
|
||||
raise ValueError(f"{label} must use the {suffix} extension: {target}")
|
||||
if not target.is_file():
|
||||
raise FileNotFoundError(f"{label} not found: {target}")
|
||||
return target
|
||||
|
||||
|
||||
def resolve_compiled_hrtf_cache(path: str | Path) -> Path:
|
||||
"""Resolve an explicitly selected JOC compiled HRTF cache."""
|
||||
return _resolve_hrtf_file(path, ".jochrtf", "compiled HRTF cache")
|
||||
|
||||
|
||||
class JochrtfBinauralRenderer:
|
||||
"""Render interleaved LFE plus fifteen JOC objects to stereo.
|
||||
|
||||
The adapter owns frame buffering and sample-timed OAMD updates; the
|
||||
selected backend owns direct filtering, reference state and latency.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, backend, *,
|
||||
mode: str | None = None,
|
||||
object_delay_samples: int = 1473,
|
||||
tail_seconds: float = 5.0,
|
||||
chunk_frames: int = 64,
|
||||
bed_channels=None):
|
||||
required_interface = (
|
||||
"source_count", "default_profile", "set_source", "process",
|
||||
"finish", "finish_output_capacity", "info")
|
||||
missing = [name for name in required_interface if not hasattr(backend, name)]
|
||||
if missing:
|
||||
raise TypeError(
|
||||
f"backend must implement the binaural backend interface; "
|
||||
f"missing: {', '.join(missing)}")
|
||||
if backend.source_count != SOURCE_CHANNELS:
|
||||
raise ValueError(f"JOC binaural backend must have {SOURCE_CHANNELS} sources")
|
||||
mode = backend.default_profile if mode is None else mode
|
||||
if backend.default_profile != str(mode).lower():
|
||||
raise ValueError("backend default profile does not match renderer mode")
|
||||
if int(object_delay_samples) < 0:
|
||||
raise ValueError("object_delay_samples must be non-negative")
|
||||
if not math.isfinite(float(tail_seconds)) or float(tail_seconds) < 0.0:
|
||||
raise ValueError("tail_seconds must be finite and non-negative")
|
||||
if int(chunk_frames) <= 0:
|
||||
raise ValueError("chunk_frames must be positive")
|
||||
|
||||
self.backend = backend
|
||||
self.mode = str(mode).lower()
|
||||
self.object_delay_samples = int(object_delay_samples)
|
||||
self.tail_seconds = float(tail_seconds)
|
||||
self.chunk_frames = int(chunk_frames)
|
||||
self.chunk_samples = self.chunk_frames * FRAME_SAMPLES
|
||||
self.dsp_backend = getattr(backend, "dsp_backend", "python-sofa")
|
||||
from bed_layout import bed_object_positions
|
||||
|
||||
from sofa_metadata import SofaPositionTimeline
|
||||
timeline_type = SofaPositionTimeline if getattr(backend, "fixed_reference", False) else OamdPositionTimeline
|
||||
self.timeline = timeline_type(
|
||||
15, bed_positions=bed_object_positions(bed_channels or ()))
|
||||
|
||||
self._input_buffer = np.empty(
|
||||
(self.chunk_samples, SOURCE_CHANNELS), dtype=np.float64)
|
||||
self._buffer_used = 0
|
||||
self.input_samples = 0
|
||||
self.processed_input_samples = 0
|
||||
self.output_samples = 0
|
||||
self.finished = False
|
||||
self.metadata_block_updates = 0
|
||||
|
||||
@classmethod
|
||||
def from_compiled_cache(
|
||||
cls, cache: str | Path, *,
|
||||
mode: str | None = None,
|
||||
object_delay_samples: int = 1473,
|
||||
tail_seconds: float = 5.0,
|
||||
output_gain: float = 1.0,
|
||||
chunk_frames: int = 64,
|
||||
bed_channels=None,
|
||||
room: bool = False) -> "JochrtfBinauralRenderer":
|
||||
from jochrtf_backend import JochrtfBackend
|
||||
source = resolve_compiled_hrtf_cache(cache)
|
||||
backend = JochrtfBackend.from_compiled_cache(
|
||||
source,
|
||||
source_count=SOURCE_CHANNELS,
|
||||
default_profile=mode or "mid",
|
||||
output_gain=output_gain,
|
||||
room=room)
|
||||
return cls(
|
||||
backend,
|
||||
mode=mode,
|
||||
object_delay_samples=object_delay_samples,
|
||||
tail_seconds=tail_seconds,
|
||||
chunk_frames=chunk_frames,
|
||||
bed_channels=bed_channels)
|
||||
|
||||
@property
|
||||
def finish_capacity_samples(self) -> int:
|
||||
return self.backend.finish_output_capacity(self.tail_seconds)
|
||||
|
||||
def _append_input(self, samples: np.ndarray) -> list[np.ndarray]:
|
||||
outputs = []
|
||||
source = np.asarray(samples, dtype=np.float64)
|
||||
position = 0
|
||||
while position < len(source):
|
||||
count = min(self.chunk_samples - self._buffer_used, len(source) - position)
|
||||
self._input_buffer[self._buffer_used:self._buffer_used + count] = (
|
||||
source[position:position + count])
|
||||
self._buffer_used += count
|
||||
position += count
|
||||
if self._buffer_used == self.chunk_samples:
|
||||
outputs.append(self._process_samples(self._input_buffer))
|
||||
self._buffer_used = 0
|
||||
return outputs
|
||||
|
||||
def render_frame(self, objects16, payload=None, metadata_offset=None,
|
||||
*, outer_sample_offset=0) -> np.ndarray:
|
||||
"""Submit one 1536-sample reconstructed frame and its ID11 payload."""
|
||||
if self.finished:
|
||||
raise RuntimeError("binaural renderer is already finished")
|
||||
source = np.asarray(objects16)
|
||||
if source.shape != (FRAME_SAMPLES, SOURCE_CHANNELS):
|
||||
raise ValueError(
|
||||
f"binaural frame must have shape ({FRAME_SAMPLES},{SOURCE_CHANNELS}), "
|
||||
f"got {source.shape}")
|
||||
frame_start = self.input_samples
|
||||
metadata_delay = (self.object_delay_samples if metadata_offset is None
|
||||
else int(metadata_offset))
|
||||
if metadata_delay < 0:
|
||||
raise ValueError("metadata_offset must be non-negative")
|
||||
if payload is not None:
|
||||
self.timeline.submit_payload(
|
||||
payload,
|
||||
frame_start_sample=frame_start,
|
||||
outer_sample_offset=int(outer_sample_offset),
|
||||
object_delay_samples=metadata_delay,
|
||||
processed_sample=self.processed_input_samples,
|
||||
)
|
||||
self.metadata_block_updates += 1
|
||||
self.input_samples += FRAME_SAMPLES
|
||||
chunks = self._append_input(source)
|
||||
if not chunks:
|
||||
return np.empty((0, OUTPUT_CHANNELS), dtype=np.float64)
|
||||
return np.concatenate(chunks, axis=0) if len(chunks) > 1 else chunks[0]
|
||||
|
||||
def _set_block_parameters(self, sample: int) -> None:
|
||||
positions = self.timeline.positions_at(sample)
|
||||
self.backend.set_source(
|
||||
0, (0.0, 1.0, 0.0), profile=self.mode,
|
||||
special_lfe=True)
|
||||
for object_index in range(15):
|
||||
self.backend.set_source(
|
||||
object_index + 1,
|
||||
positions[object_index],
|
||||
profile=self.mode)
|
||||
|
||||
def _process_samples(self, source: np.ndarray) -> np.ndarray:
|
||||
values = np.asarray(source, dtype=np.float64)
|
||||
if values.ndim != 2 or values.shape[1] != SOURCE_CHANNELS:
|
||||
raise ValueError(f"expected [samples,{SOURCE_CHANNELS}], got {values.shape}")
|
||||
block_samples = int(getattr(self.backend, "block_samples", BINAURAL_BLOCK_SAMPLES))
|
||||
if len(values) % block_samples:
|
||||
raise ValueError("binaural input must contain complete processing blocks")
|
||||
outputs = []
|
||||
block_base = self.processed_input_samples
|
||||
for start in range(0, len(values), block_samples):
|
||||
sample = block_base + start
|
||||
self._set_block_parameters(sample)
|
||||
outputs.append(self.backend.process(
|
||||
values[start:start + block_samples]))
|
||||
self.processed_input_samples += len(values)
|
||||
nonempty = [value for value in outputs if len(value)]
|
||||
if not nonempty:
|
||||
return np.empty((0, OUTPUT_CHANNELS), dtype=np.float64)
|
||||
output = np.concatenate(nonempty, axis=0)
|
||||
self.output_samples += len(output)
|
||||
return output
|
||||
|
||||
def finish(self) -> np.ndarray:
|
||||
"""Process pending source samples and drain filter/reference state once."""
|
||||
if self.finished:
|
||||
return np.empty((0, OUTPUT_CHANNELS), dtype=np.float64)
|
||||
outputs: list[np.ndarray] = []
|
||||
if self._buffer_used:
|
||||
outputs.append(self._process_samples(
|
||||
self._input_buffer[:self._buffer_used]))
|
||||
self._buffer_used = 0
|
||||
outputs.append(self.backend.finish(tail_seconds=self.tail_seconds))
|
||||
self.finished = True
|
||||
nonempty = [value for value in outputs if len(value)]
|
||||
if not nonempty:
|
||||
return np.empty((0, OUTPUT_CHANNELS), dtype=np.float64)
|
||||
output = np.concatenate(nonempty, axis=0)
|
||||
self.output_samples += len(outputs[-1])
|
||||
return output
|
||||
|
||||
def close(self) -> None:
|
||||
self.finished = True
|
||||
|
||||
@property
|
||||
def backend_info(self) -> dict:
|
||||
info = self.backend.info()
|
||||
info.update({
|
||||
"adapter": f"JOC 1536-frame / {getattr(self.backend, 'block_samples', BINAURAL_BLOCK_SAMPLES)}-sample metadata",
|
||||
"dsp_backend": self.dsp_backend,
|
||||
"mode": self.mode,
|
||||
"latency_compensated_samples": getattr(self.backend, "latency_samples", BINAURAL_LATENCY_SAMPLES),
|
||||
"object_delay_samples": self.object_delay_samples,
|
||||
"tail_seconds": self.tail_seconds,
|
||||
"metadata_payloads": self.timeline.payload_count,
|
||||
"metadata_position_transitions": self.timeline.transition_count,
|
||||
"input_samples": self.input_samples,
|
||||
"source_samples_processed": self.processed_input_samples,
|
||||
"output_samples_before_tail_trim": self.output_samples,
|
||||
"thread_safe": False,
|
||||
})
|
||||
return info
|
||||
@@ -1,751 +0,0 @@
|
||||
// joc_cli -- command line frontend, argument-compatible with the reference
|
||||
// Python CLI (main.py): the same positional input, the same mode selection
|
||||
// (ADM BWF by default, --speaker-layout or --binaural), the same option names,
|
||||
// choices and defaults, and the same default output naming under output/.
|
||||
//
|
||||
// Options that exist only because this build has no Python side or no Rosella
|
||||
// import chain (--backend python, --personalized-headphone,
|
||||
// metadata sidecars) fail with an explicit message instead of being ignored.
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/fs_utf8.h"
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace fs_utf8 = joc::fs_utf8;
|
||||
|
||||
constexpr double kRate = 48000.0;
|
||||
constexpr int kFrameSamples = 1536;
|
||||
|
||||
struct Options {
|
||||
std::string input;
|
||||
std::string output;
|
||||
std::string speaker_output;
|
||||
std::string binaural_output;
|
||||
std::string speaker_layout;
|
||||
bool binaural = false;
|
||||
std::string speaker_format = "float32";
|
||||
std::string binaural_format = "float32";
|
||||
std::string clip_action = "ask";
|
||||
int speaker_metadata_offset = 1473;
|
||||
std::string binaural_mode = "mid";
|
||||
std::string compiled_hrtf_cache;
|
||||
std::string jochrtf_room = "on";
|
||||
std::string personalized_headphone;
|
||||
bool personalized_headphone_used = false;
|
||||
int rosella_filterbank = 1;
|
||||
bool rosella_filterbank_set = false;
|
||||
double binaural_tail_seconds = 5.0;
|
||||
double binaural_tail_threshold = 1.0e-8;
|
||||
int binaural_chunk_frames = 64;
|
||||
double gain_db = 0.0;
|
||||
double duration = 0.0;
|
||||
bool duration_set = false;
|
||||
int object_delay_samples = 1473;
|
||||
std::string trajectory_mode = "compact";
|
||||
std::string ffmpeg;
|
||||
double eac3_drc_scale = 0.0;
|
||||
int eac3_target_level = 0;
|
||||
std::string backend = "auto";
|
||||
std::string native_library;
|
||||
int native_threads = 0;
|
||||
bool native_threads_set = false;
|
||||
std::string metadata_dir;
|
||||
std::string metadata_cache;
|
||||
std::string metadata_backend = "auto";
|
||||
std::string print_metadata = "none";
|
||||
std::string metadata_json;
|
||||
bool metadata_only = false;
|
||||
bool keep_raw = false;
|
||||
bool skip_sha256 = false;
|
||||
int progress_every = 1000;
|
||||
// C++-side additions (documented as such; the Python CLI has no equivalent).
|
||||
std::string bed;
|
||||
std::string kernels;
|
||||
std::string work_dir;
|
||||
std::string report_json;
|
||||
bool report_json_set = false;
|
||||
bool dry_run = false;
|
||||
bool quiet = false;
|
||||
bool help = false;
|
||||
bool version = false;
|
||||
};
|
||||
|
||||
const char* kLayoutChoices =
|
||||
"2.0 3.1 4.0 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6 6.0";
|
||||
|
||||
void print_usage() {
|
||||
std::printf(
|
||||
"usage: joc_cli [options] input\n"
|
||||
" -h, --help 显示本帮助\n"
|
||||
" --version 显示版本并退出\n"
|
||||
"\n"
|
||||
"JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、扬声器 WAV 或双耳 WAV\n"
|
||||
"\n"
|
||||
"位置参数:\n"
|
||||
" input 输入 .m4a/.eac3/.ec3\n"
|
||||
"\n"
|
||||
"模式(默认输出 ADM BWF):\n"
|
||||
" --speaker-layout L 直接扬声器渲染布局,例如 2.0、5.1、6.0、7.1.2\n"
|
||||
" 可选值: %s\n"
|
||||
" --binaural 直接双耳渲染;不生成临时 ADM BWF\n"
|
||||
"\n"
|
||||
"输出:\n"
|
||||
" -o, --output PATH 输出文件;默认 output/<名称>.adm.wav、\n"
|
||||
" output/<名称>.<布局>.wav 或 output/<名称>.binaural.wav\n"
|
||||
" --speaker-output PATH 扬声器 WAV 路径;仅与 --speaker-layout 一起使用\n"
|
||||
" --binaural-output PATH 双耳 WAV 路径;仅与 --binaural 一起使用\n"
|
||||
" --speaker-format F 扬声器 WAV 格式 float32|int24,默认 float32\n"
|
||||
" --binaural-format F 双耳 WAV 格式 float32|int24,默认 float32\n"
|
||||
" --clip-action A int24 削波处理 ask|continue|float32|abort,默认 ask\n"
|
||||
"\n"
|
||||
"渲染:\n"
|
||||
" --speaker-metadata-offset N 扬声器渲染 metadata 相对帧偏移,默认 1473 samples\n"
|
||||
" --binaural-mode M 双耳渲染模式 off|near|mid|far,默认 mid;\n"
|
||||
" off 仅用于 ADM BWF(关闭 DBMD 双耳提示)\n"
|
||||
" --personalized-headphone [PATH] Rosella 个性化模型,默认 "
|
||||
"HRTF/binaural.personalized_headphone\n"
|
||||
" --rosella-filterbank N Rosella 模型滤波器组:1=13 taps/6 slots(默认),\n"
|
||||
" 2=11 taps/3 slots;仅作用于个性化模型\n"
|
||||
" --compiled-hrtf-cache PATH 读取离线编译好的 .jochrtf\n"
|
||||
" --jochrtf-room on|off .jochrtf 的项目房间,默认 on;off 只输出直达声\n"
|
||||
" --binaural-tail-seconds S 双耳 room/filterbank flush 上限,默认 5 秒\n"
|
||||
" --binaural-tail-threshold T 双耳尾声裁切阈值,默认 1e-8;主体至少保留原时长\n"
|
||||
" --binaural-chunk-frames N 双耳内部批处理帧数,默认 64(本构建按 512 块渲染,\n"
|
||||
" 取值不影响输出)\n"
|
||||
" --gain-db X 成品增益 dB,默认 0;双耳路径以 float64 应用\n"
|
||||
" --duration S 只处理开头指定秒数\n"
|
||||
" --object-delay-samples N 对象 PCM/OAMD 时间补偿,默认 1473 samples\n"
|
||||
" --trajectory-mode M ADM 对象轨迹表示 compact|dense64,默认 compact\n"
|
||||
"\n"
|
||||
"输入与解码:\n"
|
||||
" --ffmpeg PATH ffmpeg 可执行文件;本选项优先,默认先取 FFMPEG 环境变量,\n"
|
||||
" 再在 PATH 中查找\n"
|
||||
" --eac3-drc-scale X E-AC-3 解码器 -drc_scale,0=关闭码流 dynrng,默认 0\n"
|
||||
" --eac3-target-level N E-AC-3 解码器 -target_level,0=不施加,默认 0\n"
|
||||
" --backend B JOC/扬声器 DSP 后端 auto|native;本构建无 python 后端\n"
|
||||
" --native-threads N 原生 DSP 总线程数;默认在 4 核以上使用 2\n"
|
||||
"\n"
|
||||
"诊断:\n"
|
||||
" --print-metadata M 诊断元数据输出 none|summary|frames,默认 none\n"
|
||||
" --metadata-json PATH 元数据汇总 JSON 路径\n"
|
||||
" --metadata-only 解析/打印元数据后退出\n"
|
||||
" --keep-raw 额外保留 16ch f32le 对象中间文件\n"
|
||||
" --skip-sha256 跳过最终文件 SHA-256 全量复扫\n"
|
||||
" --progress-every N 进度输出间隔,默认 1000 帧(渲染与收尾写盘同一节奏)\n"
|
||||
"\n"
|
||||
"本构建特有(Python 版没有对应参数):\n"
|
||||
" --bed PATH 已解码的 6 通道 float32 PCM;给出后不调用 ffmpeg 解码\n"
|
||||
" --kernels PATH 双耳滤波器组表 rosella_kernels.npz\n"
|
||||
" --work-dir DIR 临时目录\n"
|
||||
" --report-json PATH 结果 JSON 路径;默认 <输出>.report.json\n"
|
||||
" --dry-run 只校验配置\n"
|
||||
" --quiet 只输出警告与错误\n",
|
||||
kLayoutChoices);
|
||||
}
|
||||
|
||||
[[noreturn]] void fail(const std::string& message) { throw std::runtime_error(message); }
|
||||
|
||||
std::string require_value(const std::vector<std::string>& arguments, int* index) {
|
||||
if (static_cast<std::size_t>(*index) + 1u >= arguments.size()) {
|
||||
fail("argument " + arguments[static_cast<std::size_t>(*index)] +
|
||||
": expected one argument");
|
||||
}
|
||||
return arguments[static_cast<std::size_t>(++(*index))];
|
||||
}
|
||||
|
||||
double to_double(const std::string& text, const char* name) {
|
||||
try {
|
||||
std::size_t used = 0;
|
||||
const double value = std::stod(text, &used);
|
||||
if (used != text.size()) {
|
||||
fail(std::string(name) + ": invalid float value: " + text);
|
||||
}
|
||||
return value;
|
||||
} catch (const std::exception&) {
|
||||
fail(std::string(name) + ": invalid float value: " + text);
|
||||
}
|
||||
}
|
||||
|
||||
long long to_int(const std::string& text, const char* name) {
|
||||
try {
|
||||
std::size_t used = 0;
|
||||
const long long value = std::stoll(text, &used);
|
||||
if (used != text.size()) {
|
||||
fail(std::string(name) + ": invalid int value: " + text);
|
||||
}
|
||||
return value;
|
||||
} catch (const std::exception&) {
|
||||
fail(std::string(name) + ": invalid int value: " + text);
|
||||
}
|
||||
}
|
||||
|
||||
void check_choice(const std::string& value, const char* name,
|
||||
std::initializer_list<const char*> allowed) {
|
||||
for (const char* candidate : allowed) {
|
||||
if (value == candidate) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
std::string list;
|
||||
for (const char* candidate : allowed) {
|
||||
list += list.empty() ? candidate : (", " + std::string(candidate));
|
||||
}
|
||||
fail(std::string(name) + ": invalid choice: '" + value + "' (choose from " + list + ")");
|
||||
}
|
||||
|
||||
void parse_args(const std::vector<std::string>& arguments, Options* options) {
|
||||
std::vector<std::string> positional;
|
||||
const int argc = static_cast<int>(arguments.size());
|
||||
for (int index = 1; index < argc; ++index) {
|
||||
const std::string arg = arguments[static_cast<std::size_t>(index)];
|
||||
if (arg == "-h" || arg == "--help") { options->help = true; }
|
||||
else if (arg == "--version") { options->version = true; }
|
||||
else if (arg == "-o" || arg == "--output") { options->output = require_value(arguments, &index); }
|
||||
else if (arg == "--speaker-output") { options->speaker_output = require_value(arguments, &index); }
|
||||
else if (arg == "--binaural-output") { options->binaural_output = require_value(arguments, &index); }
|
||||
else if (arg == "--speaker-layout") { options->speaker_layout = require_value(arguments, &index); }
|
||||
else if (arg == "--binaural") { options->binaural = true; }
|
||||
else if (arg == "--speaker-format") { options->speaker_format = require_value(arguments, &index); }
|
||||
else if (arg == "--binaural-format") { options->binaural_format = require_value(arguments, &index); }
|
||||
else if (arg == "--clip-action") { options->clip_action = require_value(arguments, &index); }
|
||||
else if (arg == "--speaker-metadata-offset") {
|
||||
options->speaker_metadata_offset = static_cast<int>(
|
||||
to_int(require_value(arguments, &index), "--speaker-metadata-offset"));
|
||||
}
|
||||
else if (arg == "--binaural-mode") { options->binaural_mode = require_value(arguments, &index); }
|
||||
else if (arg == "--compiled-hrtf-cache") { options->compiled_hrtf_cache = require_value(arguments, &index); }
|
||||
else if (arg == "--jochrtf-room") { options->jochrtf_room = require_value(arguments, &index); }
|
||||
else if (arg == "--personalized-headphone") {
|
||||
options->personalized_headphone_used = true;
|
||||
// nargs="?": the path is optional, so the next token may be the input.
|
||||
// Without a path the executable-anchored default is resolved later.
|
||||
if (static_cast<std::size_t>(index) + 1u < arguments.size() &&
|
||||
arguments[static_cast<std::size_t>(index) + 1u][0] != '-') {
|
||||
options->personalized_headphone = require_value(arguments, &index);
|
||||
}
|
||||
}
|
||||
else if (arg == "--rosella-filterbank") {
|
||||
options->rosella_filterbank = static_cast<int>(
|
||||
to_int(require_value(arguments, &index), "--rosella-filterbank"));
|
||||
options->rosella_filterbank_set = true;
|
||||
}
|
||||
else if (arg == "--binaural-tail-seconds") { options->binaural_tail_seconds = to_double(require_value(arguments, &index), "--binaural-tail-seconds"); }
|
||||
else if (arg == "--binaural-tail-threshold") { options->binaural_tail_threshold = to_double(require_value(arguments, &index), "--binaural-tail-threshold"); }
|
||||
else if (arg == "--binaural-chunk-frames") { options->binaural_chunk_frames = static_cast<int>(to_int(require_value(arguments, &index), "--binaural-chunk-frames")); }
|
||||
else if (arg == "--gain-db") { options->gain_db = to_double(require_value(arguments, &index), "--gain-db"); }
|
||||
else if (arg == "--duration") { options->duration = to_double(require_value(arguments, &index), "--duration"); options->duration_set = true; }
|
||||
else if (arg == "--object-delay-samples") { options->object_delay_samples = static_cast<int>(to_int(require_value(arguments, &index), "--object-delay-samples")); }
|
||||
else if (arg == "--trajectory-mode") { options->trajectory_mode = require_value(arguments, &index); }
|
||||
else if (arg == "--ffmpeg") { options->ffmpeg = require_value(arguments, &index); }
|
||||
else if (arg == "--eac3-drc-scale") { options->eac3_drc_scale = to_double(require_value(arguments, &index), "--eac3-drc-scale"); }
|
||||
else if (arg == "--eac3-target-level") { options->eac3_target_level = static_cast<int>(to_int(require_value(arguments, &index), "--eac3-target-level")); }
|
||||
else if (arg == "--backend") { options->backend = require_value(arguments, &index); }
|
||||
else if (arg == "--native-library") { options->native_library = require_value(arguments, &index); }
|
||||
else if (arg == "--native-threads") { options->native_threads = static_cast<int>(to_int(require_value(arguments, &index), "--native-threads")); options->native_threads_set = true; }
|
||||
else if (arg == "--metadata-dir") { options->metadata_dir = require_value(arguments, &index); }
|
||||
else if (arg == "--metadata-cache") { options->metadata_cache = require_value(arguments, &index); }
|
||||
else if (arg == "--metadata-backend") { options->metadata_backend = require_value(arguments, &index); }
|
||||
else if (arg == "--print-metadata") { options->print_metadata = require_value(arguments, &index); }
|
||||
else if (arg == "--metadata-json") { options->metadata_json = require_value(arguments, &index); }
|
||||
else if (arg == "--metadata-only") { options->metadata_only = true; }
|
||||
else if (arg == "--keep-raw") { options->keep_raw = true; }
|
||||
else if (arg == "--skip-sha256") { options->skip_sha256 = true; }
|
||||
else if (arg == "--progress-every") { options->progress_every = static_cast<int>(to_int(require_value(arguments, &index), "--progress-every")); }
|
||||
else if (arg == "--bed") { options->bed = require_value(arguments, &index); }
|
||||
else if (arg == "--kernels") { options->kernels = require_value(arguments, &index); }
|
||||
else if (arg == "--work-dir") { options->work_dir = require_value(arguments, &index); }
|
||||
else if (arg == "--report-json") { options->report_json = require_value(arguments, &index); options->report_json_set = true; }
|
||||
else if (arg == "--dry-run") { options->dry_run = true; }
|
||||
else if (arg == "--quiet") { options->quiet = true; }
|
||||
else if (!arg.empty() && arg[0] == '-' && arg != "-") { fail("unrecognized argument: " + arg); }
|
||||
else { positional.push_back(arg); }
|
||||
}
|
||||
if (positional.size() > 1u) {
|
||||
fail("unrecognized extra arguments: " + positional[1] +
|
||||
(positional.size() > 2u ? " ..." : ""));
|
||||
}
|
||||
if (!positional.empty()) {
|
||||
options->input = positional.front();
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors the reference resolve_output(): <project>/output plus a mode-specific
|
||||
// name. The project directory is the executable's directory, as upstream uses
|
||||
// the script's directory, so the layout does not depend on the working directory.
|
||||
std::string resolve_output(const Options& options, const std::string& source,
|
||||
const std::string& executable_directory) {
|
||||
const std::string requested = !options.speaker_output.empty() ? options.speaker_output
|
||||
: !options.binaural_output.empty() ? options.binaural_output
|
||||
: options.output;
|
||||
if (!requested.empty()) {
|
||||
std::error_code error;
|
||||
const fs::path absolute = fs::absolute(fs_utf8::to_path(requested), error);
|
||||
return error ? requested : fs_utf8::from_path(absolute);
|
||||
}
|
||||
const fs::path directory = fs_utf8::to_path(executable_directory) / "output";
|
||||
const std::string stem = fs_utf8::from_path(fs_utf8::to_path(source).stem());
|
||||
if (!options.speaker_layout.empty()) {
|
||||
return fs_utf8::from_path(directory /
|
||||
fs_utf8::to_path(stem + "." + options.speaker_layout + ".wav"));
|
||||
}
|
||||
if (options.binaural) {
|
||||
return fs_utf8::from_path(directory / fs_utf8::to_path(stem + ".binaural.wav"));
|
||||
}
|
||||
return fs_utf8::from_path(directory / fs_utf8::to_path(stem + ".adm.wav"));
|
||||
}
|
||||
|
||||
// The project directory the reference anchors its defaults at: the directory of
|
||||
// the running executable, never the working directory.
|
||||
std::string executable_dir(const std::string& argv0) {
|
||||
const std::string own_path = fs_utf8::executable_path();
|
||||
if (!own_path.empty()) {
|
||||
const fs::path path = fs_utf8::to_path(own_path);
|
||||
if (path.has_parent_path()) {
|
||||
return fs_utf8::from_path(path.parent_path());
|
||||
}
|
||||
}
|
||||
if (argv0.empty()) {
|
||||
return ".";
|
||||
}
|
||||
std::error_code error;
|
||||
const fs::path path = fs::absolute(fs_utf8::to_path(argv0), error);
|
||||
if (error || path.empty()) {
|
||||
return ".";
|
||||
}
|
||||
return fs_utf8::from_path(path.parent_path());
|
||||
}
|
||||
|
||||
std::string find_kernels(const Options& options, const std::string& argv0) {
|
||||
(void)argv0;
|
||||
if (!options.kernels.empty() && !fs_utf8::exists(options.kernels)) {
|
||||
fail("--kernels 指向的文件不存在: " + options.kernels);
|
||||
}
|
||||
// Empty means the tables compiled into the library.
|
||||
return options.kernels;
|
||||
}
|
||||
|
||||
// The binaural input is either an explicitly compiled .jochrtf field or a Rosella
|
||||
// .personalized_headphone model. Paths are anchored at the executable directory,
|
||||
// as the reference anchors them at the project directory.
|
||||
struct HrtfInput {
|
||||
std::string compiled_path; // read this .jochrtf
|
||||
std::string personalized_path; // Rosella .personalized_headphone
|
||||
};
|
||||
|
||||
std::string resolve_compiled_hrtf(const Options& options, const std::string& project_directory) {
|
||||
if (!options.compiled_hrtf_cache.empty()) {
|
||||
return options.compiled_hrtf_cache;
|
||||
}
|
||||
const std::string directory_utf8 =
|
||||
fs_utf8::from_path(fs_utf8::to_path(project_directory) / "output" / "hrtf-cache");
|
||||
if (!fs_utf8::is_directory(directory_utf8)) {
|
||||
return std::string();
|
||||
}
|
||||
const fs::path directory = fs_utf8::to_path(directory_utf8);
|
||||
std::vector<fs::path> candidates;
|
||||
for (const fs::directory_entry& entry : fs::directory_iterator(directory)) {
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".jochrtf") {
|
||||
candidates.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
std::sort(candidates.begin(), candidates.end());
|
||||
if (candidates.size() > 1u) {
|
||||
fail(directory_utf8 +
|
||||
" 下有多个 .jochrtf 缓存,无法自动选择;请用 --compiled-hrtf-cache PATH 显式指定");
|
||||
}
|
||||
return candidates.empty() ? std::string() : fs_utf8::from_path(candidates.front());
|
||||
}
|
||||
|
||||
HrtfInput resolve_hrtf_input(const Options& options, const std::string& project_directory) {
|
||||
HrtfInput input;
|
||||
const std::string default_private = fs_utf8::from_path(
|
||||
fs_utf8::to_path(project_directory) / "HRTF" / "binaural.personalized_headphone");
|
||||
const std::string default_cache_dir =
|
||||
fs_utf8::from_path(fs_utf8::to_path(project_directory) / "output" / "hrtf-cache");
|
||||
|
||||
std::string compiled = options.compiled_hrtf_cache;
|
||||
std::string personalized =
|
||||
options.personalized_headphone_used ? options.personalized_headphone : std::string();
|
||||
if (options.personalized_headphone_used && personalized.empty()) {
|
||||
// "--personalized-headphone" without a path means the project default.
|
||||
personalized = default_private;
|
||||
}
|
||||
if (compiled.empty() && personalized.empty()) {
|
||||
// The compiled cache, then the personalized model.
|
||||
compiled = resolve_compiled_hrtf(options, project_directory);
|
||||
if (compiled.empty() && fs_utf8::exists(default_private)) {
|
||||
personalized = default_private;
|
||||
}
|
||||
}
|
||||
if (!personalized.empty()) {
|
||||
if (!fs_utf8::exists(personalized)) {
|
||||
fail("双耳模型不存在: " + personalized);
|
||||
}
|
||||
input.personalized_path = personalized;
|
||||
return input;
|
||||
}
|
||||
if (compiled.empty()) {
|
||||
fail("--binaural 未找到 HRTF 输入:默认 " + default_private + " 与 " + default_cache_dir +
|
||||
" 下的 .jochrtf 都不存在,请用 --personalized-headphone PATH 或 "
|
||||
"--compiled-hrtf-cache PATH 指定");
|
||||
}
|
||||
input.compiled_path = compiled;
|
||||
return input;
|
||||
}
|
||||
|
||||
std::uint32_t binaural_mode_value(const std::string& name) {
|
||||
if (name == "off") { return JOC_BINAURAL_OFF; }
|
||||
if (name == "near") { return JOC_BINAURAL_NEAR; }
|
||||
if (name == "far") { return JOC_BINAURAL_FAR; }
|
||||
return JOC_BINAURAL_MID;
|
||||
}
|
||||
|
||||
std::uint32_t clip_action_value(const std::string& name) {
|
||||
if (name == "continue") { return JOC_CLIP_CONTINUE; }
|
||||
if (name == "float32") { return JOC_CLIP_FLOAT32; }
|
||||
if (name == "abort") { return JOC_CLIP_ABORT; }
|
||||
return JOC_CLIP_ASK;
|
||||
}
|
||||
|
||||
std::string format_eta(double seconds) {
|
||||
if (seconds < 0.0 || seconds > 86400.0) {
|
||||
return "--";
|
||||
}
|
||||
char buffer[64];
|
||||
std::snprintf(buffer, sizeof(buffer), "%.0fs", seconds);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void JOC_CALL on_event(void* user, const joc_event* event) {
|
||||
const Options* options = static_cast<const Options*>(user);
|
||||
if (event == nullptr) {
|
||||
return;
|
||||
}
|
||||
switch (event->type) {
|
||||
case JOC_EV_PROGRESS: {
|
||||
if (options->quiet) {
|
||||
return;
|
||||
}
|
||||
const double fraction = event->progress >= 0.0 ? event->progress : 0.0;
|
||||
const double remaining =
|
||||
fraction > 0.0 ? event->elapsed_seconds * (1.0 - fraction) / fraction : -1.0;
|
||||
std::printf("[%s] %llu/%llu %.1fx realtime ETA %s\n", event->stage_name,
|
||||
static_cast<unsigned long long>(event->current_frame),
|
||||
static_cast<unsigned long long>(event->total_frames),
|
||||
event->realtime_factor, format_eta(remaining).c_str());
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
case JOC_EV_LOG: {
|
||||
if (options->quiet || event->log_level < JOC_LOG_INFO || event->message[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
std::printf("[%s] %s\n", event->stage_name, event->message);
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
case JOC_EV_WARNING:
|
||||
std::printf("[warning] %s\n", event->message);
|
||||
return;
|
||||
case JOC_EV_ERROR:
|
||||
std::fprintf(stderr, "[error] %s (%s)\n", event->message,
|
||||
joc_error_name(event->error_code));
|
||||
return;
|
||||
default:
|
||||
if (options->quiet || event->log_level < JOC_LOG_INFO || event->message[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
std::printf("[%s] %s\n", event->stage_name, event->message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
fs_utf8::configure_console();
|
||||
const std::vector<std::string> arguments = fs_utf8::command_line_arguments(argc, argv);
|
||||
Options options;
|
||||
try {
|
||||
parse_args(arguments, &options);
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "joc_cli: error: %s\n", error.what());
|
||||
return 2;
|
||||
}
|
||||
if (options.version) {
|
||||
std::printf("joc_cli %s (joc_core ABI %u)\n", joc_version_string(),
|
||||
static_cast<unsigned>(joc_abi_version()));
|
||||
return 0;
|
||||
}
|
||||
if (options.help) {
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
if (options.input.empty()) {
|
||||
print_usage();
|
||||
return 2;
|
||||
}
|
||||
|
||||
try {
|
||||
check_choice(options.speaker_format, "--speaker-format", {"float32", "int24"});
|
||||
check_choice(options.binaural_format, "--binaural-format", {"float32", "int24"});
|
||||
check_choice(options.clip_action, "--clip-action",
|
||||
{"ask", "continue", "float32", "abort"});
|
||||
check_choice(options.binaural_mode, "--binaural-mode", {"off", "near", "mid", "far"});
|
||||
check_choice(options.trajectory_mode, "--trajectory-mode", {"compact", "dense64"});
|
||||
check_choice(options.backend, "--backend", {"auto", "native", "python"});
|
||||
check_choice(options.print_metadata, "--print-metadata", {"none", "summary", "frames"});
|
||||
check_choice(options.metadata_backend, "--metadata-backend", {"auto", "emdf", "sidecar"});
|
||||
if (options.rosella_filterbank != 1 && options.rosella_filterbank != 2) {
|
||||
fail("--rosella-filterbank: invalid choice: " +
|
||||
std::to_string(options.rosella_filterbank) + " (choose from 1, 2)");
|
||||
}
|
||||
|
||||
const bool speaker_mode = !options.speaker_layout.empty();
|
||||
const bool binaural_mode = options.binaural;
|
||||
if (speaker_mode && binaural_mode) {
|
||||
fail("argument --binaural: not allowed with argument --speaker-layout");
|
||||
}
|
||||
if (options.binaural_mode == "off" && (speaker_mode || binaural_mode)) {
|
||||
fail("--binaural-mode off 仅用于 ADM BWF 输出(关闭 DBMD 双耳提示);"
|
||||
"直接双耳渲染请使用 near/mid/far");
|
||||
}
|
||||
if (!options.speaker_output.empty() && !speaker_mode) {
|
||||
fail("--speaker-output 必须与 --speaker-layout 一起使用");
|
||||
}
|
||||
if (!options.binaural_output.empty() && !binaural_mode) {
|
||||
fail("--binaural-output 必须与 --binaural 一起使用");
|
||||
}
|
||||
const bool specific_output =
|
||||
!options.speaker_output.empty() || !options.binaural_output.empty();
|
||||
if (!options.output.empty() && specific_output) {
|
||||
fail("-o/--output 与 --speaker-output/--binaural-output 不能同时使用");
|
||||
}
|
||||
if (!options.speaker_output.empty() && !options.binaural_output.empty()) {
|
||||
fail("--speaker-output 与 --binaural-output 不能同时使用");
|
||||
}
|
||||
if (options.speaker_metadata_offset < 0) {
|
||||
fail("speaker-metadata-offset 不能为负数");
|
||||
}
|
||||
check_choice(options.jochrtf_room, "--jochrtf-room", {"on", "off"});
|
||||
const bool hrtf_options_used =
|
||||
!options.compiled_hrtf_cache.empty() || options.personalized_headphone_used ||
|
||||
options.jochrtf_room != "on";
|
||||
if (hrtf_options_used && !binaural_mode) {
|
||||
fail("SOFA/HRTF 选项仅与 --binaural 一起使用");
|
||||
}
|
||||
if (options.rosella_filterbank_set && !binaural_mode) {
|
||||
fail("--rosella-filterbank 仅与 --binaural 一起使用");
|
||||
}
|
||||
if (!std::isfinite(options.binaural_tail_seconds) ||
|
||||
options.binaural_tail_seconds < 0.0) {
|
||||
fail("binaural-tail-seconds 必须是非负有限值");
|
||||
}
|
||||
if (!std::isfinite(options.binaural_tail_threshold) ||
|
||||
options.binaural_tail_threshold < 0.0) {
|
||||
fail("binaural-tail-threshold 必须是非负有限值");
|
||||
}
|
||||
if (options.binaural_chunk_frames <= 0) {
|
||||
fail("binaural-chunk-frames 必须大于 0");
|
||||
}
|
||||
if (options.duration_set && options.duration <= 0.0) {
|
||||
fail("duration 必须大于 0");
|
||||
}
|
||||
if (options.object_delay_samples < 0) {
|
||||
fail("object-delay-samples 不能为负数");
|
||||
}
|
||||
if (options.native_threads_set && options.native_threads < 1) {
|
||||
fail("native-threads 必须大于 0");
|
||||
}
|
||||
if (!std::isfinite(options.gain_db) || std::abs(options.gain_db) > 200.0) {
|
||||
fail("gain-db 超出支持范围");
|
||||
}
|
||||
// Options this build cannot honour: fail loudly instead of ignoring them.
|
||||
if (options.backend == "python") {
|
||||
fail("--backend python 在本构建中不可用(已无 Python 后端);请使用 auto 或 native");
|
||||
}
|
||||
if (options.metadata_backend == "sidecar" || !options.metadata_dir.empty() ||
|
||||
!options.metadata_cache.empty()) {
|
||||
fail("metadata sidecar 在本构建中不可用(始终直接扫描 EMDF)");
|
||||
}
|
||||
if (!options.native_library.empty()) {
|
||||
std::fprintf(stderr, "[info] --native-library 在本构建中忽略(单一 joc_core.dll)\n");
|
||||
}
|
||||
if (!fs_utf8::exists(options.input)) {
|
||||
fail("输入文件不存在: " + options.input);
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "joc_cli: error: %s\n", error.what());
|
||||
return 2;
|
||||
}
|
||||
|
||||
const bool speaker_mode = !options.speaker_layout.empty();
|
||||
const bool binaural_mode = options.binaural;
|
||||
const std::string project_directory =
|
||||
executable_dir(arguments.empty() ? std::string() : arguments.front());
|
||||
const std::string output_path = resolve_output(options, options.input, project_directory);
|
||||
std::error_code directory_error;
|
||||
fs::create_directories(fs_utf8::to_path(output_path).parent_path(), directory_error);
|
||||
|
||||
joc_task_config config{};
|
||||
config.struct_size = sizeof(config);
|
||||
config.struct_version = JOC_TASK_CONFIG_VERSION;
|
||||
config.input_path = options.input.c_str();
|
||||
config.output_path = output_path.c_str();
|
||||
config.ffmpeg_path = options.ffmpeg.empty() ? nullptr : options.ffmpeg.c_str();
|
||||
config.bed_path = options.bed.empty() ? nullptr : options.bed.c_str();
|
||||
config.work_dir = options.work_dir.empty() ? nullptr : options.work_dir.c_str();
|
||||
config.eac3_drc_scale = options.eac3_drc_scale;
|
||||
config.eac3_target_level = options.eac3_target_level;
|
||||
config.operation =
|
||||
binaural_mode ? JOC_OP_BINAURAL : speaker_mode ? JOC_OP_SPEAKER : JOC_OP_ADM_BWF;
|
||||
const std::string& requested_format =
|
||||
binaural_mode ? options.binaural_format : options.speaker_format;
|
||||
config.output_format =
|
||||
requested_format == "int24" ? JOC_FORMAT_PCM24 : JOC_FORMAT_FLOAT32;
|
||||
config.clip_action = clip_action_value(options.clip_action);
|
||||
config.speaker_layout_name = speaker_mode ? options.speaker_layout.c_str() : nullptr;
|
||||
config.speaker_metadata_offset = static_cast<std::uint32_t>(options.speaker_metadata_offset);
|
||||
config.binaural_mode = binaural_mode_value(options.binaural_mode);
|
||||
config.adm_binaural_mode = binaural_mode_value(options.binaural_mode);
|
||||
config.rosella_filterbank = static_cast<std::uint32_t>(options.rosella_filterbank);
|
||||
config.binaural_tail_seconds = options.binaural_tail_seconds;
|
||||
config.binaural_tail_threshold = options.binaural_tail_threshold;
|
||||
config.binaural_chunk_frames = static_cast<std::uint32_t>(options.binaural_chunk_frames);
|
||||
config.object_delay_samples = static_cast<std::uint32_t>(options.object_delay_samples);
|
||||
config.trajectory_mode =
|
||||
options.trajectory_mode == "dense64" ? JOC_TRAJECTORY_DENSE64 : JOC_TRAJECTORY_COMPACT;
|
||||
config.gain_db = options.gain_db;
|
||||
config.progress_interval_frames = static_cast<std::uint32_t>(options.progress_every);
|
||||
config.native_threads =
|
||||
options.native_threads_set ? static_cast<std::uint32_t>(options.native_threads) : 0u;
|
||||
config.print_metadata = options.print_metadata == "frames" ? 2u
|
||||
: options.print_metadata == "summary" ? 1u
|
||||
: 0u;
|
||||
config.metadata_json_path =
|
||||
options.metadata_json.empty() ? nullptr : options.metadata_json.c_str();
|
||||
config.duration_frames =
|
||||
options.duration_set
|
||||
? static_cast<std::uint64_t>(
|
||||
std::ceil(options.duration * kRate / static_cast<double>(kFrameSamples)))
|
||||
: 0u;
|
||||
config.flags = 0u;
|
||||
if (options.skip_sha256) { config.flags |= JOC_TASK_F_SKIP_SHA256; }
|
||||
if (options.keep_raw) { config.flags |= JOC_TASK_F_KEEP_INTERMEDIATE; }
|
||||
if (options.metadata_only) { config.flags |= JOC_TASK_F_METADATA_ONLY; }
|
||||
if (options.quiet) { config.flags |= JOC_TASK_F_QUIET; }
|
||||
|
||||
std::string hrtf_path;
|
||||
std::string personalized_path;
|
||||
std::string kernels_path;
|
||||
if (binaural_mode) {
|
||||
try {
|
||||
const HrtfInput input = resolve_hrtf_input(options, project_directory);
|
||||
hrtf_path = input.compiled_path;
|
||||
personalized_path = input.personalized_path;
|
||||
kernels_path = find_kernels(options, arguments.empty() ? std::string()
|
||||
: arguments.front());
|
||||
if (options.rosella_filterbank_set && personalized_path.empty()) {
|
||||
fail("--rosella-filterbank 只作用于 Rosella .personalized_headphone 输入;"
|
||||
"当前 .jochrtf 路径不使用该滤波器组");
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "joc_cli: error: %s\n", error.what());
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
config.hrtf_path = hrtf_path.empty() ? nullptr : hrtf_path.c_str();
|
||||
config.personalized_headphone_path =
|
||||
personalized_path.empty() ? nullptr : personalized_path.c_str();
|
||||
config.jochrtf_room = options.jochrtf_room == "off" ? 0u : 1u;
|
||||
config.kernels_path = kernels_path.empty() ? nullptr : kernels_path.c_str();
|
||||
|
||||
joc_validation_issue issues[32];
|
||||
std::uint32_t issue_count = 0;
|
||||
const joc_error validated = joc_task_validate(&config, issues, 32u, &issue_count);
|
||||
for (std::uint32_t index = 0; index < std::min(issue_count, 32u); ++index) {
|
||||
if (issues[index].severity >= 2u) {
|
||||
std::fprintf(stderr, "[error] %s: %s\n", issues[index].field, issues[index].message);
|
||||
} else if (!options.quiet) {
|
||||
std::fprintf(stderr, "[warning] %s: %s\n", issues[index].field,
|
||||
issues[index].message);
|
||||
}
|
||||
}
|
||||
if (validated != JOC_OK) {
|
||||
std::fprintf(stderr, "joc_cli: error: configuration rejected (%u issue(s))\n", issue_count);
|
||||
return 2;
|
||||
}
|
||||
if (options.dry_run) {
|
||||
std::printf("configuration accepted (%u issue(s))\n", issue_count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
joc_event_sink sink{};
|
||||
sink.struct_size = sizeof(sink);
|
||||
sink.callback = &on_event;
|
||||
sink.user = &options;
|
||||
|
||||
if (!options.quiet) {
|
||||
const char* mode_name = binaural_mode ? "binaural" : speaker_mode ? "speaker" : "adm";
|
||||
std::printf("[cli] %s -> %s (%s)\n", options.input.c_str(), output_path.c_str(),
|
||||
mode_name);
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
joc_task_result result{};
|
||||
const joc_error status = joc_task_execute(&config, &sink, &result);
|
||||
|
||||
const std::string report_path =
|
||||
options.report_json_set ? options.report_json : (output_path + ".report.json");
|
||||
{
|
||||
std::size_t needed = 0;
|
||||
joc_task_result_to_json(&result, nullptr, 0u, &needed);
|
||||
std::vector<char> buffer(needed + 1u);
|
||||
if (joc_task_result_to_json(&result, buffer.data(), buffer.size(), &needed) == JOC_OK) {
|
||||
if (std::FILE* file = fs_utf8::fopen(report_path, "wb")) {
|
||||
std::fwrite(buffer.data(), 1, std::strlen(buffer.data()), file);
|
||||
std::fputc('\n', file);
|
||||
std::fclose(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("\nresult: %s\n", status == JOC_OK ? "ok" : joc_error_name(status));
|
||||
std::printf(" output : %s\n", output_path.c_str());
|
||||
std::printf(" frames : %llu (%.2f s)\n",
|
||||
static_cast<unsigned long long>(result.input_frames), result.duration_sec);
|
||||
std::printf(" output samples: %llu\n",
|
||||
static_cast<unsigned long long>(result.output_samples));
|
||||
std::printf(" output bytes : %llu\n",
|
||||
static_cast<unsigned long long>(result.output_file_bytes));
|
||||
std::printf(" format : %s\n",
|
||||
result.output_format_actual == JOC_FORMAT_PCM24 ? "int24" : "float32");
|
||||
std::printf(" peak : %.9g (%llu sample(s) above full scale)\n", result.output_peak,
|
||||
static_cast<unsigned long long>(result.output_over_unity_values));
|
||||
std::printf(" sha256 : %s\n",
|
||||
result.output_sha256[0] != '\0' ? result.output_sha256 : "(skipped)");
|
||||
std::printf(" report : %s\n", report_path.c_str());
|
||||
// Each stage time is measured where that stage actually runs, and the three
|
||||
// stages now overlap (see the pipeline in src/task/task.cpp), so the stage
|
||||
// times deliberately do not add up to the wall-clock total.
|
||||
std::printf(" timings : decode %.2fs, joc %.2fs, dsp %.2fs, write %.2fs"
|
||||
" (stage times, concurrent), total %.2fs\n",
|
||||
result.t_decode_bed, result.t_render, result.t_render_dsp, result.t_write_file,
|
||||
result.t_total);
|
||||
if (status != JOC_OK) {
|
||||
std::fprintf(stderr, "joc_cli: error: %s: %s\n", result.error_stage, result.error_message);
|
||||
}
|
||||
return status == JOC_OK ? 0 : 1;
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
#include "eac3_transport/eac3_reader.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc::eac3 {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t kHeaderBytes = 4;
|
||||
constexpr std::size_t kHeaderBytesAc3 = 6;
|
||||
|
||||
// AC-3 syncframe length in 16-bit words, indexed by fscod (0 = 48 kHz,
|
||||
// 1 = 44.1 kHz, 2 = 32 kHz) and then frmsizecod (A/52 5.4.1.2).
|
||||
constexpr std::uint16_t kAc3FrameWords[3][38] = {
|
||||
{64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
|
||||
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
|
||||
896, 896, 1024, 1024, 1152, 1152, 1280, 1280},
|
||||
{69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
|
||||
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
|
||||
975, 975, 1114, 1114, 1253, 1253, 1393, 1393},
|
||||
{96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
|
||||
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
|
||||
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920},
|
||||
};
|
||||
|
||||
std::uint32_t read_bits(const std::uint8_t* data, std::size_t start, std::size_t count) {
|
||||
std::uint32_t value = 0;
|
||||
for (std::size_t index = start; index < start + count; ++index) {
|
||||
const std::uint32_t bit = (data[index >> 3] >> (7u - (index & 7u))) & 1u;
|
||||
value = (value << 1) | bit;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool parse_syncframe_header(const std::uint8_t* data, std::size_t size, std::size_t offset,
|
||||
Frame* out) {
|
||||
if (data == nullptr || offset + kHeaderBytesAc3 > size) {
|
||||
return false;
|
||||
}
|
||||
if (static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) |
|
||||
data[offset + 1]) != FrameReader::kSyncword) {
|
||||
return false;
|
||||
}
|
||||
Frame frame;
|
||||
frame.data = data + offset;
|
||||
frame.offset = offset;
|
||||
const std::size_t base = offset * 8u;
|
||||
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
|
||||
frame.bsid = static_cast<std::uint8_t>(bsid);
|
||||
|
||||
if (bsid <= 8u) {
|
||||
frame.codec = 0;
|
||||
const std::uint32_t fscod = read_bits(data, base + 32u, 2u);
|
||||
const std::uint32_t frmsizecod = read_bits(data, base + 34u, 6u);
|
||||
if (fscod > 2u || frmsizecod >= 38u) {
|
||||
return false;
|
||||
}
|
||||
frame.size = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
|
||||
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 48u, 3u));
|
||||
std::size_t bit = 51u;
|
||||
if ((frame.acmod & 0x1u) != 0u && frame.acmod != 0x1u) {
|
||||
bit += 2u; // cmixlev
|
||||
}
|
||||
if ((frame.acmod & 0x4u) != 0u) {
|
||||
bit += 2u; // surmixlev
|
||||
}
|
||||
if (frame.acmod == 0x2u) {
|
||||
bit += 2u; // dsurmod
|
||||
}
|
||||
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + bit, 1u));
|
||||
if (out != nullptr) {
|
||||
*out = frame;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
frame.codec = 1;
|
||||
frame.strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
|
||||
frame.substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
|
||||
const std::uint32_t frmsiz = read_bits(data, base + 21u, 11u);
|
||||
frame.size = (static_cast<std::size_t>(frmsiz) + 1u) * 2u;
|
||||
// fscod==3 also consumes two bits (fscod2), so acmod always starts at bit 36.
|
||||
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 36u, 3u));
|
||||
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + 39u, 1u));
|
||||
std::size_t bit = 45u + 5u; // bsid, then dialnorm
|
||||
const std::uint32_t compre = read_bits(data, base + bit, 1u);
|
||||
++bit;
|
||||
if (compre != 0u) {
|
||||
bit += 8u; // compr
|
||||
}
|
||||
if (frame.acmod == 0u) {
|
||||
bit += 5u; // dialnorm2
|
||||
const std::uint32_t compr2 = read_bits(data, base + bit, 1u);
|
||||
++bit;
|
||||
if (compr2 != 0u) {
|
||||
bit += 8u;
|
||||
}
|
||||
}
|
||||
if (frame.strmtyp == 1u) {
|
||||
if (read_bits(data, base + bit, 1u) != 0u) {
|
||||
frame.has_chanmap = 1;
|
||||
frame.chanmap = static_cast<std::uint16_t>(read_bits(data, base + bit + 1u, 16u));
|
||||
}
|
||||
}
|
||||
if (out != nullptr) {
|
||||
*out = frame;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void FrameReader::push(const std::uint8_t* data, std::size_t size) {
|
||||
if (failed_ || data == nullptr || size == 0) {
|
||||
return;
|
||||
}
|
||||
if (consumed_ > 0) {
|
||||
compact();
|
||||
}
|
||||
buffer_.insert(buffer_.end(), data, data + size);
|
||||
}
|
||||
|
||||
void FrameReader::compact() {
|
||||
if (consumed_ == 0) {
|
||||
return;
|
||||
}
|
||||
buffer_.erase(buffer_.begin(), buffer_.begin() + static_cast<std::ptrdiff_t>(consumed_));
|
||||
base_offset_ += consumed_;
|
||||
consumed_ = 0;
|
||||
}
|
||||
|
||||
void FrameReader::fail(joc_error code, std::string message) {
|
||||
failed_ = true;
|
||||
error_ = code;
|
||||
message_ = std::move(message);
|
||||
}
|
||||
|
||||
FrameReader::Next FrameReader::next(Frame* out) {
|
||||
if (failed_) {
|
||||
return Next::Fail;
|
||||
}
|
||||
const std::size_t available = buffer_.size() - consumed_;
|
||||
if (available == 0) {
|
||||
return Next::End;
|
||||
}
|
||||
const std::uint8_t* p = buffer_.data() + consumed_;
|
||||
|
||||
// A frame whose header does not fit is rejected rather than silently
|
||||
// resynchronised on the next 0x0B77.
|
||||
if (available < kHeaderBytes) {
|
||||
if (finished_) {
|
||||
fail(JOC_ERR_EAC3_SYNCFRAME, "syncframe header truncated at end of input");
|
||||
return Next::Fail;
|
||||
}
|
||||
return Next::End;
|
||||
}
|
||||
|
||||
Frame frame;
|
||||
if (!parse_syncframe_header(p, available, 0, &frame) || frame.size == 0u) {
|
||||
if (available < kHeaderBytesAc3 && !finished_) {
|
||||
return Next::End;
|
||||
}
|
||||
fail(JOC_ERR_EAC3_SYNCFRAME,
|
||||
"invalid syncframe header (silent resynchronisation is not allowed)");
|
||||
return Next::Fail;
|
||||
}
|
||||
|
||||
if (frame.size > available) {
|
||||
if (!finished_) {
|
||||
return Next::End;
|
||||
}
|
||||
fail(JOC_ERR_BITSTREAM_TRUNCATED,
|
||||
"last syncframe extends past end of input (declared " +
|
||||
std::to_string(frame.size) + " bytes, remaining " +
|
||||
std::to_string(available) + ")");
|
||||
return Next::Fail;
|
||||
}
|
||||
|
||||
if (out != nullptr) {
|
||||
frame.data = p;
|
||||
frame.offset = base_offset_ + consumed_;
|
||||
*out = frame;
|
||||
}
|
||||
consumed_ += frame.size;
|
||||
stream_offset_ = base_offset_ + consumed_;
|
||||
++frames_emitted_;
|
||||
return Next::Ok;
|
||||
}
|
||||
|
||||
joc_error FrameReader::frame_bytes(const std::uint8_t* data, std::size_t size, std::size_t offset,
|
||||
std::size_t* out_frame_bytes) {
|
||||
if (data == nullptr || out_frame_bytes == nullptr) {
|
||||
return JOC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
Frame frame;
|
||||
if (!parse_syncframe_header(data, size, offset, &frame)) {
|
||||
return offset + kHeaderBytes > size ? JOC_ERR_EAC3_SYNCFRAME
|
||||
: JOC_ERR_EAC3_SYNCFRAME;
|
||||
}
|
||||
if (offset + frame.size > size) {
|
||||
return JOC_ERR_BITSTREAM_TRUNCATED;
|
||||
}
|
||||
*out_frame_bytes = frame.size;
|
||||
return JOC_OK;
|
||||
}
|
||||
|
||||
} // namespace joc::eac3
|
||||
@@ -1,89 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc::eac3 {
|
||||
|
||||
// One syncframe plus its transport-layer (L2) header. codec/strmtyp/substreamid
|
||||
// identify the substream: within one audio frame each combination appears once.
|
||||
struct Frame {
|
||||
const std::uint8_t* data = nullptr;
|
||||
std::size_t size = 0;
|
||||
std::size_t offset = 0; // byte offset of the frame start in the fed stream
|
||||
std::uint8_t codec = 0; // 0 = AC-3, 1 = E-AC-3
|
||||
std::uint8_t strmtyp = 0;
|
||||
std::uint8_t substreamid = 0;
|
||||
std::uint8_t acmod = 0;
|
||||
std::uint8_t lfeon = 0;
|
||||
std::uint8_t bsid = 0;
|
||||
std::uint8_t has_chanmap = 0;
|
||||
std::uint16_t chanmap = 0;
|
||||
|
||||
bool same_stream(const Frame& other) const {
|
||||
return codec == other.codec && strmtyp == other.strmtyp &&
|
||||
substreamid == other.substreamid;
|
||||
}
|
||||
};
|
||||
|
||||
// Parses the syncframe header at `offset`. Returns false when the syncword is
|
||||
// missing or the declared size cannot be derived. `out` may be null to query
|
||||
// only the size. AC-3 and E-AC-3 are told apart by bsid (bits 40..44): <= 8 is
|
||||
// AC-3, whose frame size comes from fscod/frmsizecod, everything above is E-AC-3.
|
||||
bool parse_syncframe_header(const std::uint8_t* data, std::size_t size, std::size_t offset,
|
||||
Frame* out);
|
||||
|
||||
class FrameReader {
|
||||
public:
|
||||
enum class Next {
|
||||
Ok,
|
||||
End,
|
||||
Fail
|
||||
};
|
||||
|
||||
FrameReader() = default;
|
||||
FrameReader(const std::uint8_t* data, std::size_t size) {
|
||||
push(data, size);
|
||||
finish();
|
||||
}
|
||||
|
||||
// Appends bytes to the internal buffer (used in incremental mode).
|
||||
void push(const std::uint8_t* data, std::size_t size);
|
||||
|
||||
// Declares that no further bytes will arrive; a frame that is still
|
||||
void finish() { finished_ = true; }
|
||||
|
||||
Next next(Frame* out);
|
||||
|
||||
joc_error error() const { return error_; }
|
||||
const std::string& error_message() const { return message_; }
|
||||
|
||||
std::size_t frames_emitted() const { return frames_emitted_; }
|
||||
std::size_t stream_offset() const { return stream_offset_; }
|
||||
|
||||
// report its declared byte length.
|
||||
static joc_error frame_bytes(const std::uint8_t* data, std::size_t size, std::size_t offset,
|
||||
std::size_t* out_frame_bytes);
|
||||
|
||||
static constexpr std::uint16_t kSyncword = 0x0B77;
|
||||
|
||||
private:
|
||||
void compact();
|
||||
void fail(joc_error code, std::string message);
|
||||
|
||||
std::vector<std::uint8_t> buffer_;
|
||||
std::size_t consumed_ = 0; // bytes of buffer_ already turned into frames
|
||||
std::size_t base_offset_ = 0;
|
||||
bool finished_ = false;
|
||||
bool failed_ = false;
|
||||
joc_error error_ = JOC_OK;
|
||||
std::string message_;
|
||||
std::size_t frames_emitted_ = 0;
|
||||
std::size_t stream_offset_ = 0;
|
||||
};
|
||||
|
||||
} // namespace joc::eac3
|
||||
+487
@@ -0,0 +1,487 @@
|
||||
"""从常见 E-AC-3 同步帧直接提取连续 EMDF 容器。
|
||||
|
||||
扫描器检查八种全局位对齐,定位 ``0x5838`` 同步字,验证容器长度并解析各
|
||||
payload config,因此不要求 EMDF 在原始 E-AC-3 文件中按字节对齐。
|
||||
|
||||
本模块有意只覆盖“完整 EMDF 容器在一个同步帧中连续出现”的常见情形。不解析
|
||||
E-AC-3 mantissa,也不重组被音频数据隔开的多个 skip-field 碎片;遇到这种输入会
|
||||
明确报错,让上层决定是否使用兼容桥。
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
import csv
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
from variant_error import UnsupportedVariantError
|
||||
|
||||
|
||||
SYNCWORD = 0x5838
|
||||
REQUIRED_JOC_IDS = frozenset((11, 14))
|
||||
|
||||
|
||||
class EmdfError(ValueError):
|
||||
"""EMDF 或其 E-AC-3 传输结构不符合本实现支持的范围。"""
|
||||
|
||||
|
||||
class BitReader:
|
||||
"""MSB-first 位读取器;位置以源数据的绝对 bit offset 表示。"""
|
||||
|
||||
def __init__(self, data, position=0, limit=None):
|
||||
self.data = memoryview(data)
|
||||
self.position = int(position)
|
||||
self.limit = len(self.data) * 8 if limit is None else int(limit)
|
||||
|
||||
def read(self, count):
|
||||
count = int(count)
|
||||
if count < 0 or self.position + count > self.limit:
|
||||
raise EmdfError(f"位流越界 @bit{self.position}, need={count}, limit={self.limit}")
|
||||
value = 0
|
||||
while count:
|
||||
byte_pos = self.position >> 3
|
||||
removed_left = self.position & 7
|
||||
take = min(count, 8 - removed_left)
|
||||
shift = 8 - removed_left - take
|
||||
value = (value << take) | ((self.data[byte_pos] >> shift) & ((1 << take) - 1))
|
||||
self.position += take
|
||||
count -= take
|
||||
return value
|
||||
|
||||
def skip(self, count):
|
||||
self.read(count)
|
||||
|
||||
def read_bytes(self, count):
|
||||
return bytes(self.read(8) for _ in range(count))
|
||||
|
||||
|
||||
def variable_bits(reader, width, max_groups=8):
|
||||
"""读取 EMDF ``variable_bits(width)`` 变长整数。"""
|
||||
value = 0
|
||||
for _ in range(max_groups):
|
||||
value += reader.read(width)
|
||||
more = reader.read(1)
|
||||
if not more:
|
||||
return value
|
||||
value = (value + 1) << width
|
||||
raise EmdfError(f"variable_bits({width}) 延伸组过多")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class EmdfContainer:
|
||||
start_bit: int
|
||||
raw: bytes
|
||||
payloads: dict
|
||||
sample_offsets: dict
|
||||
# payload_id -> group_id(未声明 groupid 的载荷为 None)。规格 §8.2/Table 56:
|
||||
# 同一帧的 OAMD 与 JOC 载荷属于同一 group,凭此把一对载荷关联起来。
|
||||
group_ids: dict = None
|
||||
|
||||
|
||||
def _parse_at(data, start_bit):
|
||||
"""在已知 syncword 的 bit offset 解析一个 EMDF 容器。"""
|
||||
reader = BitReader(data, start_bit)
|
||||
if reader.read(16) != SYNCWORD:
|
||||
raise EmdfError(f"EMDF syncword 不匹配 @bit{start_bit}")
|
||||
length = reader.read(16)
|
||||
body_start = reader.position
|
||||
body_end = body_start + length * 8
|
||||
if body_end > reader.limit:
|
||||
raise EmdfError(f"EMDF 容器越界 @bit{start_bit}: length={length}")
|
||||
reader.limit = body_end
|
||||
|
||||
version = reader.read(2)
|
||||
if version == 3:
|
||||
version += variable_bits(reader, 2)
|
||||
key_id = reader.read(3)
|
||||
if key_id == 7:
|
||||
key_id += variable_bits(reader, 3)
|
||||
# TS 103 420 JOC 使用 version=0/key_id=0;严格限制也能排除音频中的伪 marker。
|
||||
if version != 0 or key_id != 0:
|
||||
raise EmdfError(f"不支持的 EMDF version/key_id: {version}/{key_id}")
|
||||
|
||||
payloads = {}
|
||||
sample_offsets = {}
|
||||
group_ids = {}
|
||||
terminated = False
|
||||
while reader.position + 5 <= body_end:
|
||||
payload_id = reader.read(5)
|
||||
if payload_id == 0:
|
||||
terminated = True
|
||||
break
|
||||
if payload_id == 0x1F:
|
||||
payload_id += variable_bits(reader, 5)
|
||||
if payload_id in payloads:
|
||||
raise EmdfError(f"同一 EMDF 容器重复 payload id {payload_id}")
|
||||
|
||||
has_sample_offset = bool(reader.read(1))
|
||||
sample_offset = (reader.read(12) >> 1) if has_sample_offset else 0
|
||||
if reader.read(1):
|
||||
variable_bits(reader, 11) # duration
|
||||
if reader.read(1):
|
||||
group_ids[payload_id] = variable_bits(reader, 2) # group id
|
||||
else:
|
||||
group_ids[payload_id] = None
|
||||
if reader.read(1):
|
||||
reader.skip(8) # codec data
|
||||
|
||||
if not reader.read(1): # discard_unknown_payload
|
||||
frame_aligned = False
|
||||
if not has_sample_offset:
|
||||
frame_aligned = bool(reader.read(1))
|
||||
if frame_aligned:
|
||||
reader.skip(2)
|
||||
if has_sample_offset or frame_aligned:
|
||||
reader.skip(7)
|
||||
|
||||
payload_size = variable_bits(reader, 8)
|
||||
if reader.position + payload_size * 8 > body_end:
|
||||
raise EmdfError(
|
||||
f"payload id {payload_id} 越界: size={payload_size}, @bit{reader.position}")
|
||||
payloads[payload_id] = reader.read_bytes(payload_size)
|
||||
sample_offsets[payload_id] = sample_offset
|
||||
|
||||
if not terminated:
|
||||
raise EmdfError("EMDF 容器缺少 payload id 0 终止符")
|
||||
total_bytes = 4 + length
|
||||
raw_reader = BitReader(data, start_bit, start_bit + total_bytes * 8)
|
||||
raw = raw_reader.read_bytes(total_bytes)
|
||||
return EmdfContainer(start_bit, raw, payloads, sample_offsets, group_ids)
|
||||
|
||||
|
||||
def _marker_offsets(data):
|
||||
"""以 NumPy 批量检查八种位移,返回可能的 0x5838 bit offsets。"""
|
||||
source = np.frombuffer(data, dtype=np.uint8)
|
||||
if source.size < 4:
|
||||
return []
|
||||
offsets = []
|
||||
for shift in range(8):
|
||||
if shift == 0:
|
||||
aligned = source
|
||||
else:
|
||||
aligned = np.bitwise_or(
|
||||
np.left_shift(source[:-1].astype(np.uint16), shift) & 0xFF,
|
||||
np.right_shift(source[1:].astype(np.uint16), 8 - shift),
|
||||
).astype(np.uint8)
|
||||
hits = np.flatnonzero((aligned[:-1] == 0x58) & (aligned[1:] == 0x38))
|
||||
offsets.extend(int(hit) * 8 + shift for hit in hits)
|
||||
return sorted(offsets)
|
||||
|
||||
|
||||
def find_joc_emdf(frame, usable=None, diagnostics=None, frame_number=None):
|
||||
"""返回同步帧中唯一、顶层连续且包含 ID11/ID14 的 JOC EMDF 容器。
|
||||
|
||||
EMDF payload 是不透明字节串,其中可能自然出现另一个 ``0x5838``。若从这个
|
||||
内嵌 marker 开始的后续随机位恰好也能通过容器语法探测,它仍不是一个独立的
|
||||
transport 容器。因此,候选的起点一旦落在较早 JOC 容器的声明范围内,就只把
|
||||
它记作内嵌伪候选,不参与“多个容器”的判定。
|
||||
|
||||
一个同步帧里可能出现多个顶层容器(同一帧可能同时带两套 ID11/ID14)。
|
||||
``usable`` 给出“这个容器能否作为本帧元数据”的分值(0 = 不是,越大越可信):
|
||||
只有唯一最高分时用它,其余记入 ``diagnostics``;没有候选或最高分并列仍按变体
|
||||
报错,不猜。
|
||||
"""
|
||||
matches = []
|
||||
offsets = _marker_offsets(frame)
|
||||
parsed_candidates = []
|
||||
parse_errors = []
|
||||
for start_bit in offsets:
|
||||
try:
|
||||
container = _parse_at(frame, start_bit)
|
||||
except EmdfError as exc:
|
||||
if len(parse_errors) < 8:
|
||||
parse_errors.append({"start_bit": start_bit, "error": str(exc)})
|
||||
continue
|
||||
parsed_candidates.append({
|
||||
"start_bit": start_bit,
|
||||
"payload_ids": list(container.payloads),
|
||||
"payload_lengths": {str(k): len(v) for k, v in container.payloads.items()},
|
||||
})
|
||||
if REQUIRED_JOC_IDS.issubset(container.payloads):
|
||||
matches.append(container)
|
||||
if not matches:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "no_contiguous_joc_container",
|
||||
"同步帧中未找到可连续解析且同时包含 ID11/ID14 的 EMDF 容器",
|
||||
details={
|
||||
"syncframe_bytes": len(frame),
|
||||
"marker_bit_offsets": offsets,
|
||||
"parsed_candidates": parsed_candidates,
|
||||
"candidate_parse_errors": parse_errors,
|
||||
"repair_hint": "检查 EMDF 是否跨多个 audio-block skip field 分片,或 payload config 是否变化",
|
||||
})
|
||||
top_level_matches = []
|
||||
nested_matches = []
|
||||
for container in sorted(matches, key=lambda item: item.start_bit):
|
||||
parent = next((candidate for candidate in top_level_matches
|
||||
if candidate.start_bit < container.start_bit <
|
||||
candidate.start_bit + len(candidate.raw) * 8), None)
|
||||
if parent is None:
|
||||
top_level_matches.append(container)
|
||||
else:
|
||||
nested_matches.append({
|
||||
"start_bit": container.start_bit,
|
||||
"end_bit": container.start_bit + len(container.raw) * 8,
|
||||
"parent_start_bit": parent.start_bit,
|
||||
"parent_end_bit": parent.start_bit + len(parent.raw) * 8,
|
||||
})
|
||||
if len(top_level_matches) != 1:
|
||||
starts = [item.start_bit for item in top_level_matches]
|
||||
if usable is not None:
|
||||
scored = [(usable(item), item) for item in top_level_matches]
|
||||
best = max(score for score, _item in scored)
|
||||
winners = [item for score, item in scored if score == best]
|
||||
if best > 0 and len(winners) == 1:
|
||||
if diagnostics is not None:
|
||||
diagnostics.append({
|
||||
"stage": "emdf",
|
||||
"variant": "extra_container_ignored",
|
||||
"frame": frame_number,
|
||||
"message": "同步帧内有多个含 ID11/ID14 的 EMDF 容器,只有一个能作为本帧元数据",
|
||||
"details": {"joc_container_start_bits": starts,
|
||||
"used_start_bit": winners[0].start_bit,
|
||||
"pair_score": best},
|
||||
})
|
||||
return winners[0]
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "multiple_joc_containers",
|
||||
"同步帧中存在多个可用 JOC EMDF,当前无法自动选择",
|
||||
details={
|
||||
"syncframe_bytes": len(frame),
|
||||
"joc_container_start_bits": starts,
|
||||
"nested_joc_candidates": nested_matches,
|
||||
})
|
||||
return top_level_matches[0]
|
||||
|
||||
|
||||
def parse_container(data):
|
||||
"""解析从 syncword 开始、已经重新按字节对齐保存的 EMDF 容器。"""
|
||||
container = _parse_at(data, 0)
|
||||
if len(container.raw) != len(data):
|
||||
raise EmdfError(f"EMDF 文件尾有额外数据: parsed={len(container.raw)}, file={len(data)}")
|
||||
return container
|
||||
|
||||
|
||||
# AC-3 同步帧长度表(16-bit word),索引为 fscod(0=48 kHz、1=44.1 kHz、2=32 kHz)
|
||||
# 再取 frmsizecod;与 A/52 的比特率表等价。
|
||||
_AC3_FRAME_WORDS = (
|
||||
(64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
|
||||
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
|
||||
896, 896, 1024, 1024, 1152, 1152, 1280, 1280),
|
||||
(69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
|
||||
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
|
||||
975, 975, 1114, 1114, 1253, 1253, 1393, 1393),
|
||||
(96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
|
||||
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
|
||||
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920),
|
||||
)
|
||||
|
||||
|
||||
# E-AC-3 chanmap(TS 102 366 Annex E Table E2.5):字段的最高位对应 bit 0。
|
||||
CHANMAP_LOCATIONS = (
|
||||
"L", "C", "R", "Ls", "Rs", "Lc/Rc", "Lrs/Rrs", "Cs", "Ts", "Lsd/Rsd",
|
||||
"Lw/Rw", "Vhl/Vhr", "Vhc", "Lts/Rts", "LFE2", "LFE",
|
||||
)
|
||||
# 单个 bit 展开为两个相邻声道的成对位置;展开顺序为先左后右。
|
||||
CHANMAP_PAIR_BITS = frozenset((5, 6, 9, 10, 11, 13))
|
||||
# A/52 acmod 对应的全频段声道位置(不含 LFE,按解码顺序)。
|
||||
ACMOD_LOCATIONS = {
|
||||
0: ("C",), # 1+1 双单声道
|
||||
1: ("C",),
|
||||
2: ("L", "R"),
|
||||
3: ("L", "C", "R"),
|
||||
4: ("L", "R", "Cs"),
|
||||
5: ("L", "C", "R", "Cs"),
|
||||
6: ("L", "R", "Ls", "Rs"),
|
||||
7: ("L", "C", "R", "Ls", "Rs"),
|
||||
}
|
||||
|
||||
|
||||
def expand_chanmap(chanmap):
|
||||
"""按 Table E2.5 把 16 bit chanmap 展开为有序声道位置列表。"""
|
||||
locations = []
|
||||
for bit in range(16):
|
||||
if not (int(chanmap) >> (15 - bit)) & 1:
|
||||
continue
|
||||
locations.extend(CHANMAP_LOCATIONS[bit].split("/"))
|
||||
return locations
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SyncFrame:
|
||||
"""一个同步帧及其传输层(L2)字段。"""
|
||||
|
||||
offset: int
|
||||
size: int
|
||||
codec: str # "ac3" 或 "eac3"
|
||||
bsid: int
|
||||
strmtyp: int # AC-3 没有子流类型,固定记为 0
|
||||
substreamid: int
|
||||
acmod: int
|
||||
lfeon: int
|
||||
chanmap: object # 依赖子流的 16 bit chanmap;未声明为 None
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
"""同一音频帧内唯一的子流标识。"""
|
||||
return (self.codec, self.strmtyp, self.substreamid)
|
||||
|
||||
|
||||
def _read_bits(data, start, count):
|
||||
value = 0
|
||||
for index in range(start, start + count):
|
||||
value = (value << 1) | ((data[index >> 3] >> (7 - (index & 7))) & 1)
|
||||
return value
|
||||
|
||||
|
||||
def sync_frame_at(data, offset):
|
||||
"""解析 offset 处同步帧的传输层头。
|
||||
|
||||
区分 AC-3 与 E-AC-3 的依据是 bsid(bit 40 起 5 bit):bsid <= 8 为 AC-3,
|
||||
其帧长由 fscod/frmsizecod 的比特率表给出;bsid >= 16 为 E-AC-3,帧长由
|
||||
frmsiz 给出。依赖子流(strmtyp=1)在这里顺带解析出 chanmap。
|
||||
"""
|
||||
if offset + 6 > len(data) or data[offset] != 0x0B or data[offset + 1] != 0x77:
|
||||
return None
|
||||
base = offset * 8
|
||||
bsid = _read_bits(data, base + 40, 5)
|
||||
if bsid <= 8:
|
||||
fscod = _read_bits(data, base + 32, 2)
|
||||
frmsizecod = _read_bits(data, base + 34, 6)
|
||||
if fscod > 2 or frmsizecod >= 38:
|
||||
return None
|
||||
size = _AC3_FRAME_WORDS[fscod][frmsizecod] * 2
|
||||
acmod = _read_bits(data, base + 48, 3)
|
||||
bit = 51
|
||||
if acmod & 0x1 and acmod != 0x1:
|
||||
bit += 2 # cmixlev
|
||||
if acmod & 0x4:
|
||||
bit += 2 # surmixlev
|
||||
if acmod == 0x2:
|
||||
bit += 2 # dsurmod
|
||||
lfeon = _read_bits(data, base + bit, 1)
|
||||
return SyncFrame(offset, size, "ac3", bsid, 0, 0, acmod, lfeon, None)
|
||||
|
||||
strmtyp = _read_bits(data, base + 16, 2)
|
||||
substreamid = _read_bits(data, base + 18, 3)
|
||||
frmsiz = _read_bits(data, base + 21, 11)
|
||||
# fscod==3 时的 fscod2 同样是 2 bit,因此 acmod 恒在 bit 36。
|
||||
acmod = _read_bits(data, base + 36, 3)
|
||||
lfeon = _read_bits(data, base + 39, 1)
|
||||
bit = 45 # bsid 之后
|
||||
bit += 5 # dialnorm
|
||||
compre = _read_bits(data, base + bit, 1)
|
||||
bit += 1
|
||||
if compre:
|
||||
bit += 8 # compr
|
||||
if acmod == 0:
|
||||
bit += 5 # dialnorm2
|
||||
compr2 = _read_bits(data, base + bit, 1)
|
||||
bit += 1
|
||||
if compr2:
|
||||
bit += 8
|
||||
chanmap = None
|
||||
if strmtyp == 1:
|
||||
if _read_bits(data, base + bit, 1):
|
||||
chanmap = _read_bits(data, base + bit + 1, 16)
|
||||
return SyncFrame(offset, (frmsiz + 1) * 2, "eac3", bsid, strmtyp, substreamid,
|
||||
acmod, lfeon, chanmap)
|
||||
|
||||
|
||||
def iter_syncframes(data):
|
||||
"""按同步字长度遍历输入中的 AC-3 与 E-AC-3 同步帧。"""
|
||||
pos = 0
|
||||
while pos < len(data):
|
||||
frame = sync_frame_at(data, pos)
|
||||
if frame is None:
|
||||
raise UnsupportedVariantError(
|
||||
"eac3_transport", "syncframe_header",
|
||||
"同步帧头无效或出现了未处理的子流排列",
|
||||
details={
|
||||
"byte_offset": pos,
|
||||
"remaining_bytes": len(data) - pos,
|
||||
"next_16_bytes_hex": data[pos:pos + 16].hex(),
|
||||
})
|
||||
if pos + frame.size > len(data):
|
||||
raise UnsupportedVariantError(
|
||||
"eac3_transport", "truncated_syncframe",
|
||||
"末帧长度超过输入剩余数据",
|
||||
details={
|
||||
"byte_offset": pos,
|
||||
"codec": frame.codec,
|
||||
"declared_frame_bytes": frame.size,
|
||||
"remaining_bytes": len(data) - pos,
|
||||
})
|
||||
yield frame
|
||||
pos += frame.size
|
||||
|
||||
|
||||
def audio_frame_groups(frames):
|
||||
"""把同步帧序列按音频帧分组。
|
||||
|
||||
一个音频帧内每个 (编码, strmtyp, substreamid) 组合只出现一次,因此再次遇到
|
||||
已出现过的组合即表示下一个音频帧开始。独立子流缺席(只有依赖子流)或核心是
|
||||
AC-3 时同样成立。
|
||||
"""
|
||||
group = []
|
||||
keys = set()
|
||||
for frame in frames:
|
||||
if frame.key in keys:
|
||||
yield group
|
||||
group = []
|
||||
keys = set()
|
||||
keys.add(frame.key)
|
||||
group.append(frame)
|
||||
if group:
|
||||
yield group
|
||||
|
||||
|
||||
def iter_eac3_frames(data):
|
||||
"""兼容入口:只产出同步帧字节。"""
|
||||
for frame in iter_syncframes(data):
|
||||
yield data[frame.offset:frame.offset + frame.size]
|
||||
|
||||
|
||||
def extract_index(eac3_path, output_dir, max_frames=None):
|
||||
"""将裸 E-AC-3 的连续 EMDF 保存为 ``frames.csv + emdf/``。"""
|
||||
output_dir = Path(output_dir)
|
||||
emdf_dir = output_dir / "emdf"
|
||||
emdf_dir.mkdir(parents=True, exist_ok=True)
|
||||
frames = iter_eac3_frames(Path(eac3_path).read_bytes())
|
||||
rows = []
|
||||
for frame_number, frame in enumerate(frames):
|
||||
if max_frames is not None and frame_number >= max_frames:
|
||||
break
|
||||
try:
|
||||
container = find_joc_emdf(frame)
|
||||
except UnsupportedVariantError as exc:
|
||||
exc.add_context(frame=frame_number, details={"syncframe_bytes": len(frame)})
|
||||
raise
|
||||
except EmdfError as exc:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "container_syntax",
|
||||
"EMDF 容器语法无法解析",
|
||||
frame=frame_number,
|
||||
details={"syncframe_bytes": len(frame), "parser_error": str(exc)}) from exc
|
||||
digest = hashlib.sha256(container.raw).hexdigest()
|
||||
target = emdf_dir / f"{digest}.bin"
|
||||
if not target.is_file():
|
||||
target.write_bytes(container.raw)
|
||||
rows.append({
|
||||
"frame": frame_number,
|
||||
"emdf_hash": digest,
|
||||
"emdf_size": len(container.raw),
|
||||
"emdf_start_bit": container.start_bit,
|
||||
"payload_ids": ";".join(str(x) for x in container.payloads),
|
||||
"error": "",
|
||||
})
|
||||
if (frame_number + 1) % 1000 == 0:
|
||||
print(f"[metadata] {frame_number + 1} frames", flush=True)
|
||||
if not rows:
|
||||
raise EmdfError("E-AC-3 输入中没有可处理的同步帧")
|
||||
with (output_dir / "frames.csv").open("w", encoding="utf-8", newline="") as fp:
|
||||
fields = ("frame", "emdf_hash", "emdf_size", "emdf_start_bit", "payload_ids", "error")
|
||||
writer = csv.DictWriter(fp, fieldnames=fields)
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
return output_dir
|
||||
@@ -1,368 +0,0 @@
|
||||
#include "emdf/emdf_parser.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include "foundation/bit_reader.h"
|
||||
|
||||
namespace joc::emdf {
|
||||
|
||||
namespace {
|
||||
|
||||
Status syntax_fail(const std::string& message) {
|
||||
return Status::fail(JOC_ERR_EMDF_SYNTAX, stage::kEmdf, message);
|
||||
}
|
||||
|
||||
Status truncated_fail(const bits::BitReader& reader) {
|
||||
return Status::fail(JOC_ERR_BITSTREAM_TRUNCATED, stage::kEmdf,
|
||||
std::string("EMDF bitstream truncated: ") + reader.error_message());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bit, Container* out) {
|
||||
if (data == nullptr || out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null buffer or output");
|
||||
}
|
||||
if (start_bit + 16u > size * 8u) {
|
||||
return syntax_fail("EMDF syncword position beyond buffer");
|
||||
}
|
||||
|
||||
bits::BitReader reader;
|
||||
reader.reset(data, size, start_bit);
|
||||
|
||||
if (reader.read(16) != kSyncword) {
|
||||
return syntax_fail("EMDF syncword mismatch at bit " + std::to_string(start_bit));
|
||||
}
|
||||
const std::uint32_t length = reader.read(16);
|
||||
const std::size_t body_start = reader.position();
|
||||
const std::size_t body_end = body_start + static_cast<std::size_t>(length) * 8u;
|
||||
if (body_end > reader.limit()) {
|
||||
return syntax_fail("EMDF container length " + std::to_string(length) +
|
||||
" exceeds buffer at bit " + std::to_string(start_bit));
|
||||
}
|
||||
reader.set_limit_bits(body_end);
|
||||
|
||||
std::uint32_t version = reader.read(2);
|
||||
if (version == 3u) {
|
||||
std::uint32_t extra = 0;
|
||||
if (!bits::variable_bits(reader, 2, 8, &extra)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
version += extra;
|
||||
}
|
||||
std::uint32_t key_id = reader.read(3);
|
||||
if (key_id == 7u) {
|
||||
std::uint32_t extra = 0;
|
||||
if (!bits::variable_bits(reader, 3, 8, &extra)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
key_id += extra;
|
||||
}
|
||||
if (reader.failed()) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
// TS 103 420 JOC uses version 0 / key_id 0; the strict check also rejects
|
||||
// false 0x5838 markers that happen to sit inside audio data.
|
||||
if (version != 0u || key_id != 0u) {
|
||||
return syntax_fail("unsupported EMDF version/key_id " + std::to_string(version) + "/" +
|
||||
std::to_string(key_id));
|
||||
}
|
||||
|
||||
Container container;
|
||||
container.start_bit = start_bit;
|
||||
bool terminated = false;
|
||||
|
||||
while (reader.position() + 5u <= body_end) {
|
||||
std::uint32_t payload_id = reader.read(5);
|
||||
if (reader.failed()) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
if (payload_id == 0u) {
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
if (payload_id == 0x1Fu) {
|
||||
std::uint32_t extra = 0;
|
||||
if (!bits::variable_bits(reader, 5, 8, &extra)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
payload_id += extra;
|
||||
}
|
||||
for (std::size_t i = 0; i < container.payload_count; ++i) {
|
||||
if (container.payloads[i].id == static_cast<std::uint8_t>(payload_id)) {
|
||||
return syntax_fail("duplicate EMDF payload id " + std::to_string(payload_id));
|
||||
}
|
||||
}
|
||||
if (container.payload_count >= kMaxPayloads) {
|
||||
return syntax_fail("EMDF payload count exceeds " + std::to_string(kMaxPayloads));
|
||||
}
|
||||
|
||||
const std::uint32_t has_sample_offset = reader.read(1);
|
||||
std::uint32_t sample_offset = 0;
|
||||
if (has_sample_offset != 0u) {
|
||||
std::uint32_t value = 0;
|
||||
if (!bits::variable_bits(reader, 11, 8, &value)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED
|
||||
? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
sample_offset = value;
|
||||
}
|
||||
if (reader.read(1) != 0u) {
|
||||
std::uint32_t ignored = 0;
|
||||
if (!bits::variable_bits(reader, 11, 8, &ignored)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
}
|
||||
std::uint8_t group_id = 0;
|
||||
std::uint8_t has_group_id = 0;
|
||||
if (reader.read(1) != 0u) {
|
||||
std::uint32_t value = 0;
|
||||
if (!bits::variable_bits(reader, 2, 8, &value)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
group_id = static_cast<std::uint8_t>(value);
|
||||
has_group_id = 1;
|
||||
}
|
||||
if (reader.read(1) != 0u) {
|
||||
if (!reader.skip(8)) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
}
|
||||
if (reader.read(1) == 0u) {
|
||||
bool frame_aligned = false;
|
||||
if (has_sample_offset == 0u) {
|
||||
frame_aligned = reader.read(1) != 0u;
|
||||
if (frame_aligned) {
|
||||
if (!reader.skip(2)) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (has_sample_offset != 0u || frame_aligned) {
|
||||
if (!reader.skip(7)) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (reader.failed()) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
|
||||
std::uint32_t payload_size = 0;
|
||||
if (!bits::variable_bits(reader, 8, 8, &payload_size)) {
|
||||
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
|
||||
: syntax_fail(reader.error_message());
|
||||
}
|
||||
const std::size_t payload_bits = static_cast<std::size_t>(payload_size) * 8u;
|
||||
if (reader.position() + payload_bits > body_end) {
|
||||
return syntax_fail("EMDF payload id " + std::to_string(payload_id) +
|
||||
" extends past container body (size " + std::to_string(payload_size) +
|
||||
" at bit " + std::to_string(reader.position()) + ")");
|
||||
}
|
||||
|
||||
Payload& entry = container.payloads[container.payload_count++];
|
||||
entry.id = static_cast<std::uint8_t>(payload_id);
|
||||
entry.sample_offset = sample_offset;
|
||||
entry.group_id = group_id;
|
||||
entry.has_group_id = has_group_id;
|
||||
entry.bit_offset = reader.position();
|
||||
entry.size = payload_size;
|
||||
|
||||
if (!reader.skip(payload_bits)) {
|
||||
return truncated_fail(reader);
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated) {
|
||||
return syntax_fail("EMDF container has no payload id 0 terminator");
|
||||
}
|
||||
container.raw_size = 4u + static_cast<std::size_t>(length);
|
||||
*out = container;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector<std::size_t>* out) {
|
||||
out->clear();
|
||||
if (data == nullptr || size < 4u) {
|
||||
return;
|
||||
}
|
||||
// Eight global bit alignments. For shift != 0 the reference builds an
|
||||
// (n-1)-byte shifted view and only scans pairs inside it, which is what the
|
||||
// bounds below reproduce exactly.
|
||||
for (std::size_t shift = 0; shift < 8u; ++shift) {
|
||||
const std::size_t aligned_len = (shift == 0u) ? size : (size - 1u);
|
||||
auto aligned_byte = [&](std::size_t index) -> std::uint8_t {
|
||||
if (shift == 0u) {
|
||||
return data[index];
|
||||
}
|
||||
const std::uint16_t high = static_cast<std::uint16_t>(data[index]) << shift;
|
||||
const std::uint16_t low = static_cast<std::uint16_t>(data[index + 1u]) >> (8u - shift);
|
||||
return static_cast<std::uint8_t>((high | low) & 0xFFu);
|
||||
};
|
||||
if (aligned_len < 2u) {
|
||||
continue;
|
||||
}
|
||||
for (std::size_t i = 0; i + 1u < aligned_len; ++i) {
|
||||
if (aligned_byte(i) == 0x58u && aligned_byte(i + 1u) == 0x38u) {
|
||||
out->push_back(i * 8u + shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::sort(out->begin(), out->end());
|
||||
}
|
||||
|
||||
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out,
|
||||
ContainerPredicate usable, bool* ignored_extra) {
|
||||
if (data == nullptr || out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null buffer or output");
|
||||
}
|
||||
std::vector<std::size_t> offsets;
|
||||
marker_offsets(data, size, &offsets);
|
||||
|
||||
std::vector<Container> matches;
|
||||
std::size_t parse_errors = 0;
|
||||
std::string first_parse_error;
|
||||
for (const std::size_t start_bit : offsets) {
|
||||
Container candidate;
|
||||
const Status status = parse_at(data, size, start_bit, &candidate);
|
||||
if (!status.ok()) {
|
||||
++parse_errors;
|
||||
if (first_parse_error.empty()) {
|
||||
first_parse_error = "@bit" + std::to_string(start_bit) + ": " + status.message();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (candidate.find(kIdOamd) != nullptr && candidate.find(kIdJoc) != nullptr) {
|
||||
matches.push_back(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
if (matches.empty()) {
|
||||
// Classification stays at the transport level (identical to the reference
|
||||
// implementation, which raises emdf_transport here), but the underlying
|
||||
std::string message =
|
||||
"no contiguous EMDF container carrying ID11+ID14 in this syncframe (markers=" +
|
||||
std::to_string(offsets.size()) + ", parse_failures=" + std::to_string(parse_errors) +
|
||||
")";
|
||||
if (!first_parse_error.empty()) {
|
||||
message += "; first candidate error " + first_parse_error;
|
||||
}
|
||||
return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf, message);
|
||||
}
|
||||
|
||||
std::sort(matches.begin(), matches.end(),
|
||||
[](const Container& a, const Container& b) { return a.start_bit < b.start_bit; });
|
||||
|
||||
// A payload may contain bytes that look like another 0x5838 container; a
|
||||
std::vector<Container> top_level;
|
||||
for (const Container& candidate : matches) {
|
||||
bool nested = false;
|
||||
for (const Container& parent : top_level) {
|
||||
if (parent.start_bit < candidate.start_bit &&
|
||||
candidate.start_bit < parent.start_bit + parent.raw_size * 8u) {
|
||||
nested = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!nested) {
|
||||
top_level.push_back(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
if (top_level.size() != 1u) {
|
||||
// Several containers can be genuine: a real stream carries a second
|
||||
// container whose ID14 is a different structure altogether. The predicate
|
||||
// tells those apart; what it leaves ambiguous stays an error.
|
||||
if (usable != nullptr) {
|
||||
int best = 0;
|
||||
const Container* winner = nullptr;
|
||||
int winners = 0;
|
||||
for (const Container& candidate : top_level) {
|
||||
const int score = usable(data, size, candidate);
|
||||
if (score > best) {
|
||||
best = score;
|
||||
winner = &candidate;
|
||||
winners = 1;
|
||||
} else if (score == best && score > 0) {
|
||||
++winners;
|
||||
}
|
||||
}
|
||||
if (best > 0 && winners == 1 && winner != nullptr) {
|
||||
if (ignored_extra != nullptr) {
|
||||
*ignored_extra = true;
|
||||
}
|
||||
*out = *winner;
|
||||
return Status::success();
|
||||
}
|
||||
}
|
||||
return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf,
|
||||
"multiple top-level JOC EMDF containers (" +
|
||||
std::to_string(top_level.size()) +
|
||||
"); automatic selection is not defined");
|
||||
}
|
||||
*out = top_level.front();
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status find_joc_emdf_optional(const std::uint8_t* data, std::size_t size, bool* present,
|
||||
Container* out, ContainerPredicate usable, bool* ignored_extra) {
|
||||
if (present == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null presence flag");
|
||||
}
|
||||
const Status status = find_joc_emdf(data, size, out, usable, ignored_extra);
|
||||
if (status.ok()) {
|
||||
*present = true;
|
||||
return status;
|
||||
}
|
||||
if (status.code() != JOC_ERR_EMDF_TRANSPORT) {
|
||||
return status;
|
||||
}
|
||||
// find_joc_emdf reports "no usable container in this syncframe" and "several
|
||||
// usable containers" with the same code. Only the former is normal for a
|
||||
// core syncframe (audio data can contain bytes that look like a marker);
|
||||
// an ambiguous frame stays an error.
|
||||
static constexpr const char* kAmbiguous = "multiple top-level";
|
||||
if (status.message().rfind(kAmbiguous, 0) == 0) {
|
||||
return status;
|
||||
}
|
||||
*present = false;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
void extract_container_bytes(const std::uint8_t* data, std::size_t size, const Container& container,
|
||||
std::vector<std::uint8_t>* out) {
|
||||
out->assign(container.raw_size, 0u);
|
||||
if (out->empty()) {
|
||||
return;
|
||||
}
|
||||
bits::BitReader reader;
|
||||
reader.reset(data, size, container.start_bit);
|
||||
reader.read_bytes(out->data(), out->size());
|
||||
}
|
||||
|
||||
Status extract_payload_bytes(const std::uint8_t* data, std::size_t size, const Payload& payload,
|
||||
std::vector<std::uint8_t>* out) {
|
||||
if (data == nullptr || out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null buffer or output");
|
||||
}
|
||||
out->assign(payload.size, 0u);
|
||||
if (out->empty()) {
|
||||
return Status::success();
|
||||
}
|
||||
bits::BitReader reader;
|
||||
reader.reset(data, size, payload.bit_offset);
|
||||
if (!reader.read_bytes(out->data(), out->size())) {
|
||||
return Status::fail(JOC_ERR_BITSTREAM_TRUNCATED, stage::kEmdf,
|
||||
"payload bytes extend past the syncframe");
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::emdf
|
||||
@@ -1,81 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "joc_core.h"
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc::emdf {
|
||||
|
||||
inline constexpr std::uint16_t kSyncword = 0x5838;
|
||||
inline constexpr std::uint8_t kIdOamd = 11;
|
||||
inline constexpr std::uint8_t kIdJoc = 14;
|
||||
inline constexpr std::size_t kMaxPayloads = JOC_MAX_EMDF_PAYLOADS;
|
||||
|
||||
struct Payload {
|
||||
std::uint8_t id = 0;
|
||||
std::uint32_t sample_offset = 0; // EMDF outer smpoffst: variable_bits(11)
|
||||
std::size_t bit_offset = 0; // MSB-first bit position of the payload bytes
|
||||
std::size_t size = 0; // payload byte count
|
||||
// Payload configuration data (ETSI TS 102 366 H.2.2.3). Spec section 8.2 and
|
||||
// its Table 56 associate the OAMD and JOC payloads of one frame by an equal
|
||||
// group id, which is how several containers in one syncframe are told apart.
|
||||
std::uint8_t group_id = 0;
|
||||
std::uint8_t has_group_id = 0;
|
||||
};
|
||||
|
||||
struct Container {
|
||||
std::size_t start_bit = 0;
|
||||
std::size_t raw_size = 0;
|
||||
std::size_t payload_count = 0;
|
||||
Payload payloads[kMaxPayloads] = {};
|
||||
|
||||
const Payload* find(std::uint8_t id) const {
|
||||
for (std::size_t i = 0; i < payload_count; ++i) {
|
||||
if (payloads[i].id == id) {
|
||||
return &payloads[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bit, Container* out);
|
||||
|
||||
// All candidate 0x5838 bit offsets over the eight alignments, ascending.
|
||||
void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector<std::size_t>* out);
|
||||
|
||||
// Scores how well a candidate container can serve as the frame's metadata when a
|
||||
// syncframe carries more than one: 0 = not at all, higher = more credible. A null
|
||||
// predicate keeps the strict behaviour: several top-level candidates are an error,
|
||||
// never a choice.
|
||||
using ContainerPredicate = int (*)(const std::uint8_t* data, std::size_t size,
|
||||
const Container& container);
|
||||
|
||||
// When a syncframe holds several top-level candidates and `usable` leaves exactly
|
||||
// one with the highest non-zero score, that one is returned and `ignored_extra` is
|
||||
// set; otherwise the frame stays an ambiguity error.
|
||||
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out,
|
||||
ContainerPredicate usable = nullptr, bool* ignored_extra = nullptr);
|
||||
|
||||
// Like find_joc_emdf(), but a syncframe that carries no EMDF marker at all is a
|
||||
// normal "absent" result rather than an error. A syncframe that does carry
|
||||
// markers but no usable JOC container still fails, so a real transport problem
|
||||
// is never mistaken for a core substream.
|
||||
Status find_joc_emdf_optional(const std::uint8_t* data, std::size_t size, bool* present,
|
||||
Container* out, ContainerPredicate usable = nullptr,
|
||||
bool* ignored_extra = nullptr);
|
||||
|
||||
// Extract the container's bytes exactly as the bit reader sees them (identical
|
||||
// to a memcpy for byte-aligned containers).
|
||||
void extract_container_bytes(const std::uint8_t* data, std::size_t size, const Container& container,
|
||||
std::vector<std::uint8_t>* out);
|
||||
|
||||
// Extract one payload's bytes with the same MSB-first semantics.
|
||||
Status extract_payload_bytes(const std::uint8_t* data, std::size_t size, const Payload& payload,
|
||||
std::vector<std::uint8_t>* out);
|
||||
|
||||
} // namespace joc::emdf
|
||||
@@ -0,0 +1,76 @@
|
||||
"""解包 EVO MD-set evolution 载荷,返回各 payload ID 的字节数据和位偏移。"""
|
||||
_MARK = "1001001000000"
|
||||
|
||||
# 各子载荷字段: (id, 头部前缀, 同步标记, 后缀常量, 尺寸域位数, 是否有转义)
|
||||
# 头部前缀 = 5 位 id 的 MSB 二进制(id11 字段前另有 5 位容器前导 00000)
|
||||
# 尺寸域单位 = nibble(4 位)。id14 有转义:9 位值=0 → 再读 9 位 = 字节数。
|
||||
_LAYOUT = [
|
||||
(11, "0000001011", "010000000000000", 8, False),
|
||||
(14, "01110", "01000000000000", 9, True),
|
||||
(2, "00010", "000100", 7, False),
|
||||
(1, "00001", "1110000000000000000000000000", 4, False),
|
||||
(30, "11110", "1110000000000000000000000000", 4, False),
|
||||
]
|
||||
|
||||
|
||||
def _msb_bits(data: bytes):
|
||||
return [(x >> (7 - i)) & 1 for x in data for i in range(8)]
|
||||
|
||||
|
||||
def _val(bits, off, n):
|
||||
v = 0
|
||||
for b in bits[off:off + n]:
|
||||
v = (v << 1) | b
|
||||
return v
|
||||
|
||||
|
||||
class _LooseSkip(Exception):
|
||||
def __init__(self, ident):
|
||||
self.ident = ident
|
||||
|
||||
|
||||
def unpack_evolution(payload: bytes, loose=False):
|
||||
"""解包 evolution 载荷 → (subs, offsets)。subs 键为 id 整数。
|
||||
loose=True 时对每个 id 的 (前缀+标记+后缀) 全模式做位流重同步扫描
|
||||
(不同编码流的子载荷次序/内部常量可有合法差异,如 kanata 的 id11)。"""
|
||||
bits = _msb_bits(payload)
|
||||
pos = 0
|
||||
subs = {}
|
||||
offsets = {}
|
||||
for ident, pref, suff, sbits, escape in _LAYOUT:
|
||||
pat = pref + _MARK + suff
|
||||
if loose:
|
||||
hit = -1
|
||||
for i in range(pos, len(bits) - len(pat)):
|
||||
if ''.join(map(str, bits[i:i + len(pat)])) == pat:
|
||||
hit = i
|
||||
break
|
||||
if hit < 0:
|
||||
continue
|
||||
pos = hit + len(pat)
|
||||
else:
|
||||
for name, const, expect in (("前缀", bits[pos:pos + len(pref)], pref),
|
||||
("标记", bits[pos + len(pref):pos + len(pref) + len(_MARK)], _MARK),
|
||||
("后缀", bits[pos + len(pref) + len(_MARK):
|
||||
pos + len(pref) + len(_MARK) + len(suff)], suff)):
|
||||
got = ''.join(map(str, const))
|
||||
if got != expect:
|
||||
raise ValueError(f"id={ident}: {name}常量不匹配 @bit{pos} got={got} want={expect}")
|
||||
pos += len(pref) + len(_MARK) + len(suff)
|
||||
n_nib = _val(bits, pos, sbits)
|
||||
pos += sbits
|
||||
if escape and n_nib == 1:
|
||||
n_nib = 512 + _val(bits, pos, sbits)
|
||||
pos += sbits
|
||||
n_bits = n_nib * 4
|
||||
body = bits[pos:pos + n_bits]
|
||||
pos += n_bits
|
||||
b = bytearray(len(body) // 8)
|
||||
for i in range(0, len(body) // 8 * 8, 8):
|
||||
v = 0
|
||||
for x in body[i:i + 8]:
|
||||
v = (v << 1) | x
|
||||
b[i // 8] = v
|
||||
subs[ident] = bytes(b)
|
||||
offsets[ident] = pos
|
||||
return subs, offsets
|
||||
@@ -1,30 +0,0 @@
|
||||
#include "foundation/bit_reader.h"
|
||||
|
||||
namespace joc::bits {
|
||||
|
||||
bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups, std::uint32_t* out_value) {
|
||||
std::uint32_t value = 0;
|
||||
for (unsigned group = 0; group < max_groups; ++group) {
|
||||
value += reader.read(width);
|
||||
if (reader.failed()) {
|
||||
return false;
|
||||
}
|
||||
const std::uint32_t more = reader.read(1);
|
||||
if (reader.failed()) {
|
||||
return false;
|
||||
}
|
||||
if (more == 0u) {
|
||||
if (out_value != nullptr) {
|
||||
*out_value = value;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
value = (value + 1u) << width;
|
||||
}
|
||||
// An extension chain that never terminates is a syntax error, not a
|
||||
// truncation.
|
||||
reader.fail(JOC_ERR_EMDF_SYNTAX, "variable_bits extension groups exceeded");
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace joc::bits
|
||||
@@ -1,125 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc::bits {
|
||||
|
||||
class BitReader {
|
||||
public:
|
||||
BitReader() = default;
|
||||
BitReader(const std::uint8_t* data, std::size_t size) { reset(data, size); }
|
||||
|
||||
void reset(const std::uint8_t* data, std::size_t size, std::size_t start_bit = 0) {
|
||||
data_ = data;
|
||||
size_bits_ = size * 8u;
|
||||
pos_ = start_bit;
|
||||
limit_ = size_bits_;
|
||||
error_ = JOC_OK;
|
||||
message_ = "";
|
||||
}
|
||||
|
||||
void set_limit_bits(std::size_t limit_bits) {
|
||||
limit_ = limit_bits < size_bits_ ? limit_bits : size_bits_;
|
||||
}
|
||||
|
||||
std::size_t position() const { return pos_; }
|
||||
std::size_t limit() const { return limit_; }
|
||||
std::size_t remaining_bits() const { return pos_ <= limit_ ? limit_ - pos_ : 0; }
|
||||
const std::uint8_t* data() const { return data_; }
|
||||
|
||||
bool failed() const { return error_ != JOC_OK; }
|
||||
joc_error error() const { return error_; }
|
||||
const char* error_message() const { return message_; }
|
||||
|
||||
std::uint32_t read(unsigned count) {
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!can_read(count)) {
|
||||
fail_truncated(count);
|
||||
return 0;
|
||||
}
|
||||
std::uint32_t value = 0;
|
||||
if ((pos_ & 7u) == 0u && count >= 8u) {
|
||||
while (count >= 8u) {
|
||||
value = (value << 8) | data_[pos_ >> 3];
|
||||
pos_ += 8u;
|
||||
count -= 8u;
|
||||
}
|
||||
}
|
||||
while (count-- > 0u) {
|
||||
const std::uint32_t bit = (data_[pos_ >> 3] >> (7u - (pos_ & 7u))) & 1u;
|
||||
value = (value << 1) | bit;
|
||||
++pos_;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
std::uint64_t read64(unsigned count) {
|
||||
if (count <= 32u) {
|
||||
return static_cast<std::uint64_t>(read(count));
|
||||
}
|
||||
const std::uint64_t high = static_cast<std::uint64_t>(read(count - 32u));
|
||||
const std::uint64_t low = static_cast<std::uint64_t>(read(32u));
|
||||
return (high << 32) | low;
|
||||
}
|
||||
|
||||
bool skip(std::size_t count) {
|
||||
if (!can_read(count)) {
|
||||
fail_truncated(count);
|
||||
return false;
|
||||
}
|
||||
pos_ += count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_bytes(std::uint8_t* out, std::size_t count) {
|
||||
if (count == 0) {
|
||||
return true;
|
||||
}
|
||||
if (!can_read(count * 8u)) {
|
||||
fail_truncated(count * 8u);
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
out[i] = static_cast<std::uint8_t>(read(8u));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool can_read(std::size_t count) const {
|
||||
return !failed() && count <= limit_ && pos_ <= limit_ - count;
|
||||
}
|
||||
|
||||
// semantic check fails, so the reader never continues past it).
|
||||
void fail(joc_error code, const char* message) {
|
||||
if (!failed()) {
|
||||
error_ = code;
|
||||
message_ = message;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void fail_truncated(std::size_t count) {
|
||||
fail(JOC_ERR_BITSTREAM_TRUNCATED, "bit read past end of buffer");
|
||||
last_request_ = count;
|
||||
}
|
||||
|
||||
const std::uint8_t* data_ = nullptr;
|
||||
std::size_t size_bits_ = 0;
|
||||
std::size_t pos_ = 0;
|
||||
std::size_t limit_ = 0;
|
||||
std::size_t last_request_ = 0;
|
||||
joc_error error_ = JOC_OK;
|
||||
const char* message_ = "";
|
||||
};
|
||||
|
||||
// followed by a continuation bit. Mirrors src/emdf.py:variable_bits().
|
||||
bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups,
|
||||
std::uint32_t* out_value);
|
||||
|
||||
} // namespace joc::bits
|
||||
@@ -1,206 +0,0 @@
|
||||
#include "foundation/fft.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "simd/simd.h"
|
||||
|
||||
namespace joc::dsp {
|
||||
|
||||
// The dispatched kernels read and write the spectrum as interleaved doubles, and
|
||||
// an array of std::complex<double> is exactly that: two doubles per element, no
|
||||
// padding, no vtable.
|
||||
static_assert(sizeof(Complex) == 2u * sizeof(double), "complex layout");
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
template <typename Container>
|
||||
void fft_in_place(Container* data, bool inverse) {
|
||||
const std::size_t count = data->size();
|
||||
if (count < 2u) {
|
||||
return;
|
||||
}
|
||||
for (std::size_t index = 1u, reversed = 0u; index < count; ++index) {
|
||||
std::size_t bit = count >> 1u;
|
||||
for (; (reversed & bit) != 0u; bit >>= 1u) {
|
||||
reversed ^= bit;
|
||||
}
|
||||
reversed ^= bit;
|
||||
if (index < reversed) {
|
||||
std::swap((*data)[index], (*data)[reversed]);
|
||||
}
|
||||
}
|
||||
for (std::size_t length = 2u; length <= count; length <<= 1u) {
|
||||
const double angle = (inverse ? 2.0 : -2.0) * kPi / static_cast<double>(length);
|
||||
const Complex step(std::cos(angle), std::sin(angle));
|
||||
for (std::size_t start = 0u; start < count; start += length) {
|
||||
Complex factor(1.0, 0.0);
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
const Complex even = (*data)[start + offset];
|
||||
const Complex odd = (*data)[start + offset + length / 2u] * factor;
|
||||
(*data)[start + offset] = even + odd;
|
||||
(*data)[start + offset + length / 2u] = even - odd;
|
||||
factor *= step;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inverse) {
|
||||
for (Complex& value : *data) {
|
||||
value /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_power_of_two(std::size_t value) { return value != 0u && (value & (value - 1u)) == 0u; }
|
||||
|
||||
} // namespace
|
||||
|
||||
FftPlan::FftPlan(std::size_t size, bool inverse) : size_(size), inverse_(inverse) {
|
||||
reverse_.resize(size);
|
||||
for (std::size_t index = 1u, reversed = 0u; index < size; ++index) {
|
||||
std::size_t bit = size >> 1u;
|
||||
for (; (reversed & bit) != 0u; bit >>= 1u) {
|
||||
reversed ^= bit;
|
||||
}
|
||||
reversed ^= bit;
|
||||
reverse_[index] = reversed;
|
||||
}
|
||||
for (std::size_t length = 2u; length <= size; length <<= 1u) {
|
||||
const double angle = (inverse ? 2.0 : -2.0) * kPi / static_cast<double>(length);
|
||||
const Complex step(std::cos(angle), std::sin(angle));
|
||||
stage_begin_.push_back(twiddle_.size());
|
||||
Complex factor(1.0, 0.0);
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
twiddle_.push_back(factor);
|
||||
factor *= step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exactly the operations fft_in_place performs, in the same order, with the
|
||||
// twiddles read from the precomputed recurrence instead of being re-derived.
|
||||
template <typename Container>
|
||||
void FftPlan::apply(Container* data) const {
|
||||
const std::size_t count = data->size();
|
||||
if (count < 2u) {
|
||||
return;
|
||||
}
|
||||
const std::size_t* reverse = reverse_.data();
|
||||
for (std::size_t index = 1u; index < count; ++index) {
|
||||
const std::size_t reversed = reverse[index];
|
||||
if (index < reversed) {
|
||||
std::swap((*data)[index], (*data)[reversed]);
|
||||
}
|
||||
}
|
||||
// The cascade is dispatched for every power-of-two size the kernels can pack
|
||||
// whole groups into a vector (JOC_SIMD pins one tier for verification). A
|
||||
// kernel only ever puts independent butterflies in the same vector, so every
|
||||
// output keeps the operation sequence and the roundings written below; small
|
||||
// transforms -- and the caller's own table -- keep the portable loop.
|
||||
if (count >= simd::kMinVectorFftSize && (count & (count - 1u)) == 0u) {
|
||||
simd::fft_butterflies(reinterpret_cast<double*>(data->data()), count,
|
||||
reinterpret_cast<const double*>(twiddle_.data()),
|
||||
stage_begin_.data());
|
||||
} else {
|
||||
std::size_t stage = 0u;
|
||||
for (std::size_t length = 2u; length <= count; length <<= 1u, ++stage) {
|
||||
const Complex* table = twiddle_.data() + stage_begin_[stage];
|
||||
for (std::size_t start = 0u; start < count; start += length) {
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
const Complex even = (*data)[start + offset];
|
||||
const Complex odd = (*data)[start + offset + length / 2u] * table[offset];
|
||||
(*data)[start + offset] = even + odd;
|
||||
(*data)[start + offset + length / 2u] = even - odd;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inverse_) {
|
||||
for (Complex& value : *data) {
|
||||
value /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fft_radix2(std::vector<Complex>* data, const FftPlan& plan) { plan.apply(data); }
|
||||
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan) { plan.apply(data); }
|
||||
|
||||
void fft_radix2(std::vector<Complex>* data, bool inverse) { fft_in_place(data, inverse); }
|
||||
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, bool inverse) {
|
||||
fft_in_place(data, inverse);
|
||||
}
|
||||
|
||||
void fft_any(const std::vector<Complex>& input, bool inverse, std::vector<Complex>* output) {
|
||||
const std::size_t count = input.size();
|
||||
if (is_power_of_two(count)) {
|
||||
*output = input;
|
||||
fft_radix2(output, inverse);
|
||||
return;
|
||||
}
|
||||
std::size_t size = 1u;
|
||||
while (size < 2u * count + 1u) {
|
||||
size <<= 1u;
|
||||
}
|
||||
const double sign = inverse ? 1.0 : -1.0;
|
||||
std::vector<Complex> left(size, Complex(0.0, 0.0));
|
||||
std::vector<Complex> right(size, Complex(0.0, 0.0));
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
const std::size_t wrapped = (index * index) % (2u * count);
|
||||
const double angle = kPi * static_cast<double>(wrapped) / static_cast<double>(count);
|
||||
const Complex chirp(std::cos(angle), sign * std::sin(angle));
|
||||
left[index] = input[index] * chirp;
|
||||
right[index] = std::conj(chirp);
|
||||
if (index != 0u) {
|
||||
right[size - index] = std::conj(chirp);
|
||||
}
|
||||
}
|
||||
fft_radix2(&left, false);
|
||||
fft_radix2(&right, false);
|
||||
for (std::size_t index = 0u; index < size; ++index) {
|
||||
left[index] *= right[index];
|
||||
}
|
||||
fft_radix2(&left, true);
|
||||
output->resize(count);
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
const std::size_t wrapped = (index * index) % (2u * count);
|
||||
const double angle = kPi * static_cast<double>(wrapped) / static_cast<double>(count);
|
||||
const Complex chirp(std::cos(angle), sign * std::sin(angle));
|
||||
(*output)[index] = left[index] * chirp;
|
||||
if (inverse) {
|
||||
(*output)[index] /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t next_fast_len(std::size_t value) {
|
||||
if (value <= 6u) {
|
||||
return value;
|
||||
}
|
||||
std::size_t best = value;
|
||||
for (std::size_t power2 = 1u; power2 < value * 2u; power2 *= 2u) {
|
||||
for (std::size_t power3 = power2; power3 < value * 2u; power3 *= 3u) {
|
||||
std::size_t power5 = power3;
|
||||
while (power5 < value) {
|
||||
power5 *= 5u;
|
||||
}
|
||||
best = std::min(best, power5);
|
||||
if (power3 >= value) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
std::size_t next_power_of_two(std::size_t value) {
|
||||
std::size_t result = 1u;
|
||||
while (result < value) {
|
||||
result <<= 1u;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace joc::dsp
|
||||
@@ -1,68 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
// Complex transforms shared by the HRTF and Rosella DSP cores. The convention is
|
||||
// NumPy's: the forward transform is unnormalised and the inverse scales by 1/N,
|
||||
// so a ported pipeline keeps the reference's arithmetic bit for bit.
|
||||
namespace joc::dsp {
|
||||
|
||||
using Complex = std::complex<double>;
|
||||
|
||||
inline constexpr std::size_t kQmfFftSize = 128;
|
||||
|
||||
// Precomputed radix-2 plan for one size and direction.
|
||||
//
|
||||
// The transform derives each butterfly's twiddle by multiplying the previous one
|
||||
// by the stage step, so the twiddle at offset k is `step` multiplied k times in
|
||||
// that order, independently of the group. Materialising that exact recurrence --
|
||||
// and the bit-reversal permutation -- removes one complex multiply and a
|
||||
// (length/2)-deep serial dependency from every stage's inner loop. The table
|
||||
// entries are the recurrence's own values, so the transform is bit-identical.
|
||||
//
|
||||
// The 128-point cascade is executed by the runtime-dispatched SIMD kernel
|
||||
// (src/simd/simd.h): it computes independent butterflies in parallel lanes,
|
||||
// which leaves both the table and every output's summation order untouched.
|
||||
class FftPlan {
|
||||
public:
|
||||
FftPlan(std::size_t size, bool inverse);
|
||||
|
||||
std::size_t size() const { return size_; }
|
||||
bool inverse() const { return inverse_; }
|
||||
|
||||
private:
|
||||
template <typename Container>
|
||||
void apply(Container* data) const;
|
||||
|
||||
friend void fft_radix2(std::vector<Complex>* data, const FftPlan& plan);
|
||||
friend void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan);
|
||||
|
||||
std::size_t size_ = 0;
|
||||
bool inverse_ = false;
|
||||
std::vector<std::size_t> reverse_; // bit-reversal permutation, [size]
|
||||
std::vector<std::size_t> stage_begin_; // twiddle offset of each stage
|
||||
std::vector<Complex> twiddle_; // per stage, length/2 entries, concatenated
|
||||
};
|
||||
|
||||
// In-place radix-2 transform; the size must be a power of two.
|
||||
void fft_radix2(std::vector<Complex>* data, bool inverse);
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, bool inverse);
|
||||
|
||||
// Plan-driven forms: the plan carries the size and the direction, so a caller that
|
||||
// transforms the same length repeatedly builds it once.
|
||||
void fft_radix2(std::vector<Complex>* data, const FftPlan& plan);
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan);
|
||||
|
||||
// Exact-length transform: radix-2 when the size allows it, Bluestein otherwise.
|
||||
// scipy/numpy use a mixed-radix transform, which is the same transform.
|
||||
void fft_any(const std::vector<Complex>& input, bool inverse, std::vector<Complex>* output);
|
||||
|
||||
// scipy's next_fast_len: the smallest 5-smooth number that is not smaller.
|
||||
std::size_t next_fast_len(std::size_t value);
|
||||
|
||||
std::size_t next_power_of_two(std::size_t value);
|
||||
|
||||
} // namespace joc::dsp
|
||||
@@ -1,259 +0,0 @@
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace joc::fs_utf8 {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
#if defined(_WIN32)
|
||||
constexpr char kPathListSeparator = ';';
|
||||
#else
|
||||
constexpr char kPathListSeparator = ':';
|
||||
#endif
|
||||
|
||||
// PATH (and PATHEXT) are separator-delimited lists. An empty entry is kept: on both
|
||||
// platforms it means the current directory.
|
||||
std::vector<std::string> split_list(const char* value, char separator) {
|
||||
std::vector<std::string> parts;
|
||||
if (value == nullptr) {
|
||||
return parts;
|
||||
}
|
||||
std::string current;
|
||||
for (const char c : std::string(value)) {
|
||||
if (c == separator) {
|
||||
parts.push_back(current);
|
||||
current.clear();
|
||||
continue;
|
||||
}
|
||||
current.push_back(c);
|
||||
}
|
||||
parts.push_back(current);
|
||||
return parts;
|
||||
}
|
||||
|
||||
bool is_runnable_file(const std::string& utf8_path) {
|
||||
std::error_code error;
|
||||
if (!fs::is_regular_file(to_path(utf8_path), error) || error) {
|
||||
return false;
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
return true;
|
||||
#else
|
||||
// Existence is not enough: a file without the execute bit cannot be started, so
|
||||
// deciding here names the real problem instead of a later "exit code 126".
|
||||
return ::access(utf8_path.c_str(), X_OK) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool carries_directory(const std::string& utf8_path) {
|
||||
#if defined(_WIN32)
|
||||
return utf8_path.find_first_of("/\\") != std::string::npos;
|
||||
#else
|
||||
return utf8_path.find('/') != std::string::npos;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
// A bare name only resolves through PATHEXT on Windows. The environment is the
|
||||
// authority; the classic four are the fallback when it is unset.
|
||||
std::vector<std::string> executable_extensions() {
|
||||
std::vector<std::string> extensions = split_list(std::getenv("PATHEXT"), ';');
|
||||
if (extensions.empty()) {
|
||||
extensions = {".COM", ".EXE", ".BAT", ".CMD"};
|
||||
}
|
||||
return extensions;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
fs::path to_path(const std::string& utf8) {
|
||||
return fs::path(std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size()));
|
||||
}
|
||||
|
||||
std::string from_path(const fs::path& path) {
|
||||
const std::u8string text = path.u8string();
|
||||
return std::string(reinterpret_cast<const char*>(text.data()), text.size());
|
||||
}
|
||||
|
||||
std::FILE* fopen(const std::string& utf8_path, const char* mode) {
|
||||
#if defined(_WIN32)
|
||||
const std::wstring wide_mode(mode, mode + std::strlen(mode));
|
||||
return ::_wfopen(to_path(utf8_path).c_str(), wide_mode.c_str());
|
||||
#else
|
||||
return std::fopen(utf8_path.c_str(), mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::FILE* fopen_spool(const std::string& utf8_path) {
|
||||
#if defined(_WIN32)
|
||||
// Delete-on-close handed to the CRT: if the process is killed the file goes with
|
||||
// it, which is what stops an aborted run from leaving hundreds of gigabytes.
|
||||
HANDLE handle = ::CreateFileW(
|
||||
to_path(utf8_path).c_str(), GENERIC_READ | GENERIC_WRITE | DELETE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS,
|
||||
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
return nullptr;
|
||||
}
|
||||
const int descriptor = ::_open_osfhandle(reinterpret_cast<std::intptr_t>(handle), 0);
|
||||
if (descriptor == -1) {
|
||||
::CloseHandle(handle);
|
||||
return nullptr;
|
||||
}
|
||||
return ::_fdopen(descriptor, "wb+");
|
||||
#else
|
||||
return std::fopen(utf8_path.c_str(), "wb+");
|
||||
#endif
|
||||
}
|
||||
|
||||
int remove(const std::string& utf8_path) {
|
||||
#if defined(_WIN32)
|
||||
return ::_wremove(to_path(utf8_path).c_str());
|
||||
#else
|
||||
return std::remove(utf8_path.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
bool exists(const std::string& utf8_path) {
|
||||
std::error_code error;
|
||||
return fs::exists(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
bool is_directory(const std::string& utf8_path) {
|
||||
std::error_code error;
|
||||
return fs::is_directory(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
std::uintmax_t file_size(const std::string& utf8_path, std::error_code& error) {
|
||||
return fs::file_size(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
std::string temp_directory() {
|
||||
std::error_code error;
|
||||
const fs::path directory = fs::temp_directory_path(error);
|
||||
return error ? std::string(".") : from_path(directory);
|
||||
}
|
||||
|
||||
std::string executable_path() {
|
||||
#if defined(_WIN32)
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
while (true) {
|
||||
const DWORD written =
|
||||
::GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
|
||||
if (written == 0) {
|
||||
return std::string();
|
||||
}
|
||||
if (written < buffer.size()) {
|
||||
return from_path(fs::path(std::wstring(buffer.data(), written)));
|
||||
}
|
||||
buffer.resize(buffer.size() * 2u);
|
||||
}
|
||||
#elif defined(__linux__)
|
||||
std::vector<char> buffer(4096u, '\0');
|
||||
const ssize_t written = ::readlink("/proc/self/exe", buffer.data(), buffer.size() - 1u);
|
||||
return written > 0 ? std::string(buffer.data(), static_cast<std::size_t>(written))
|
||||
: std::string();
|
||||
#else
|
||||
return std::string();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string find_executable(const std::string& reference) {
|
||||
if (reference.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
if (carries_directory(reference)) {
|
||||
// A reference that already names a location is the caller's own decision:
|
||||
// keep it verbatim rather than searching for a same-named program elsewhere.
|
||||
return is_runnable_file(reference) ? reference : std::string();
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
const bool named_with_extension = to_path(reference).has_extension();
|
||||
#endif
|
||||
for (const std::string& entry : split_list(std::getenv("PATH"), kPathListSeparator)) {
|
||||
const fs::path directory = to_path(entry.empty() ? std::string(".") : entry);
|
||||
const std::string candidate = from_path(directory / to_path(reference));
|
||||
if (is_runnable_file(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
if (!named_with_extension) {
|
||||
for (const std::string& extension : executable_extensions()) {
|
||||
if (is_runnable_file(candidate + extension)) {
|
||||
return candidate + extension;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::ifstream open_input(const std::string& utf8_path) {
|
||||
return std::ifstream(to_path(utf8_path), std::ios::binary);
|
||||
}
|
||||
|
||||
std::ofstream open_output(const std::string& utf8_path) {
|
||||
return std::ofstream(to_path(utf8_path), std::ios::binary);
|
||||
}
|
||||
|
||||
std::vector<std::string> command_line_arguments(int argc, char** argv) {
|
||||
#if defined(_WIN32)
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
int count = 0;
|
||||
LPWSTR* wide = ::CommandLineToArgvW(::GetCommandLineW(), &count);
|
||||
std::vector<std::string> arguments;
|
||||
if (wide == nullptr) {
|
||||
return arguments;
|
||||
}
|
||||
arguments.reserve(static_cast<std::size_t>(count));
|
||||
for (int index = 0; index < count; ++index) {
|
||||
const std::wstring_view text(wide[index]);
|
||||
const int size = ::WideCharToMultiByte(CP_UTF8, 0, text.data(),
|
||||
static_cast<int>(text.size()), nullptr, 0, nullptr,
|
||||
nullptr);
|
||||
std::string utf8(static_cast<std::size_t>(size), '\0');
|
||||
if (size > 0) {
|
||||
::WideCharToMultiByte(CP_UTF8, 0, text.data(), static_cast<int>(text.size()),
|
||||
utf8.data(), size, nullptr, nullptr);
|
||||
}
|
||||
arguments.push_back(std::move(utf8));
|
||||
}
|
||||
::LocalFree(wide);
|
||||
return arguments;
|
||||
#else
|
||||
std::vector<std::string> arguments;
|
||||
arguments.reserve(static_cast<std::size_t>(argc));
|
||||
for (int index = 0; index < argc; ++index) {
|
||||
arguments.emplace_back(argv[index]);
|
||||
}
|
||||
return arguments;
|
||||
#endif
|
||||
}
|
||||
|
||||
void configure_console() {
|
||||
#if defined(_WIN32)
|
||||
::SetConsoleOutputCP(CP_UTF8);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace joc::fs_utf8
|
||||
@@ -1,55 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
// Paths inside this library are always UTF-8, on every platform. std::filesystem
|
||||
// stores UTF-16 on Windows and bytes elsewhere, and the narrow CRT uses the ANSI
|
||||
// code page on Windows, so every path crosses into the OS through this shim: that
|
||||
// is what makes non-ASCII names (Japanese, Chinese, ...) work.
|
||||
namespace joc::fs_utf8 {
|
||||
|
||||
std::filesystem::path to_path(const std::string& utf8);
|
||||
std::string from_path(const std::filesystem::path& path);
|
||||
|
||||
// File handles and queries take a UTF-8 path: _wfopen on Windows, plain calls
|
||||
// elsewhere. Nothing else in the library may call the narrow CRT with a path.
|
||||
std::FILE* fopen(const std::string& utf8_path, const char* mode);
|
||||
// Temporary spool handle: on Windows the file is opened delete-on-close, so killing
|
||||
// the process removes it instead of leaving a multi-gigabyte leftover behind.
|
||||
std::FILE* fopen_spool(const std::string& utf8_path);
|
||||
int remove(const std::string& utf8_path);
|
||||
bool exists(const std::string& utf8_path);
|
||||
bool is_directory(const std::string& utf8_path);
|
||||
std::uintmax_t file_size(const std::string& utf8_path, std::error_code& error);
|
||||
std::string temp_directory();
|
||||
|
||||
// The running executable's own path, UTF-8, or empty when the platform cannot
|
||||
// report it. Defaults are anchored here so they never depend on the CWD.
|
||||
std::string executable_path();
|
||||
|
||||
// Resolves an executable reference to a concrete path. A reference that carries a
|
||||
// directory separator is a location the caller chose, so it is kept verbatim when
|
||||
// it names a runnable file and rejected otherwise; a bare name is looked up on PATH
|
||||
// (through PATHEXT on Windows). Empty means "could not be resolved", which lets a
|
||||
// caller report the name it was given instead of starting a child process that the
|
||||
// shell would have to find.
|
||||
std::string find_executable(const std::string& reference);
|
||||
|
||||
// Streams: std::ifstream/ofstream accept a std::filesystem::path, which is the
|
||||
// portable way to open a UTF-8 path.
|
||||
std::ifstream open_input(const std::string& utf8_path);
|
||||
std::ofstream open_output(const std::string& utf8_path);
|
||||
|
||||
// Command line arguments as UTF-8. Windows hands the process UTF-16 and the
|
||||
// narrow CRT would convert it through the ANSI code page, so the wide command
|
||||
// line is re-parsed there; on POSIX argv is already bytes in the user's locale.
|
||||
std::vector<std::string> command_line_arguments(int argc, char** argv);
|
||||
void configure_console();
|
||||
|
||||
} // namespace joc::fs_utf8
|
||||
@@ -1,25 +0,0 @@
|
||||
// Port of the reference's adm_atmos.q_to_adm_xyz: OAMD Q15 coordinates to the ADM
|
||||
// cartesian triple. It lives in foundation because both the ADM writer and the
|
||||
// object position timeline need it, and the timeline must not depend on output.
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "foundation/py_num.h"
|
||||
|
||||
namespace joc::geometry {
|
||||
|
||||
inline void q_to_adm_xyz(int q1, int q2, int q3, double* x, double* y, double* z) {
|
||||
const double posX = std::min(1.0, static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q1) * 62.0 / 32767.0)) / 62.0);
|
||||
const double posY = std::min(1.0, static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q2) * 62.0 / 32767.0)) / 62.0);
|
||||
double posZ = static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q3) * 15.0 / 32767.0)) / 15.0;
|
||||
posZ = std::max(-1.0, std::min(1.0, posZ));
|
||||
*x = posX * 2.0 - 1.0;
|
||||
*y = 1.0 - posY * 2.0;
|
||||
*z = posZ;
|
||||
}
|
||||
|
||||
} // namespace joc::geometry
|
||||
@@ -1,25 +0,0 @@
|
||||
// Port of src/oamd_tracks.py align_metadata_sample: an OAMD position update is
|
||||
// scheduled on the renderer's processing-block grid, the same 32-sample grid the
|
||||
// speaker control blocks use. It lives in foundation because both the ADM track
|
||||
// builder and the object position timeline need it, and the timeline must not
|
||||
// depend on output.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace joc::timing {
|
||||
|
||||
inline constexpr std::int64_t kMetadataBlockSamples = 32;
|
||||
|
||||
// Nearest block boundary, ties towards the earlier block; the negative branch
|
||||
// floors like the reference's Python `//`.
|
||||
inline std::int64_t align_metadata_sample(std::int64_t sample,
|
||||
std::int64_t block_samples = kMetadataBlockSamples) {
|
||||
const std::int64_t numerator = sample + block_samples / 2 - 1;
|
||||
const std::int64_t quotient =
|
||||
numerator >= 0 ? numerator / block_samples
|
||||
: -((-numerator + block_samples - 1) / block_samples);
|
||||
return block_samples * quotient;
|
||||
}
|
||||
|
||||
} // namespace joc::timing
|
||||
@@ -1,273 +0,0 @@
|
||||
#include "foundation/mini_json.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace joc::json {
|
||||
|
||||
namespace {
|
||||
|
||||
const char kHexDigits[] = "0123456789abcdef";
|
||||
|
||||
void skip_space(const std::string& text, std::size_t* index) {
|
||||
while (*index < text.size() &&
|
||||
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' ||
|
||||
text[*index] == '\r')) {
|
||||
++(*index);
|
||||
}
|
||||
}
|
||||
|
||||
bool read_string(const std::string& text, std::size_t* index, std::string* out) {
|
||||
if (*index >= text.size() || text[*index] != '"') {
|
||||
return false;
|
||||
}
|
||||
++(*index);
|
||||
out->clear();
|
||||
while (*index < text.size()) {
|
||||
const char c = text[*index];
|
||||
if (c == '\\') {
|
||||
if (*index + 1 >= text.size()) {
|
||||
return false;
|
||||
}
|
||||
const char escape = text[*index + 1];
|
||||
*index += 2;
|
||||
switch (escape) {
|
||||
case '"': out->push_back('"'); break;
|
||||
case '\\': out->push_back('\\'); break;
|
||||
case '/': out->push_back('/'); break;
|
||||
case 'b': out->push_back('\b'); break;
|
||||
case 'f': out->push_back('\f'); break;
|
||||
case 'n': out->push_back('\n'); break;
|
||||
case 'r': out->push_back('\r'); break;
|
||||
case 't': out->push_back('\t'); break;
|
||||
case 'u': {
|
||||
if (*index + 4 > text.size()) {
|
||||
return false;
|
||||
}
|
||||
unsigned code = 0;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const char digit = text[*index + static_cast<std::size_t>(i)];
|
||||
code <<= 4;
|
||||
if (digit >= '0' && digit <= '9') { code |= static_cast<unsigned>(digit - '0'); }
|
||||
else if (digit >= 'a' && digit <= 'f') { code |= static_cast<unsigned>(digit - 'a' + 10); }
|
||||
else if (digit >= 'A' && digit <= 'F') { code |= static_cast<unsigned>(digit - 'A' + 10); }
|
||||
else { return false; }
|
||||
}
|
||||
*index += 4;
|
||||
if (code < 0x80u) {
|
||||
out->push_back(static_cast<char>(code));
|
||||
} else if (code < 0x800u) {
|
||||
out->push_back(static_cast<char>(0xC0u | (code >> 6)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
} else {
|
||||
out->push_back(static_cast<char>(0xE0u | (code >> 12)));
|
||||
out->push_back(static_cast<char>(0x80u | ((code >> 6) & 0x3Fu)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '"') {
|
||||
++(*index);
|
||||
return true;
|
||||
}
|
||||
out->push_back(c);
|
||||
++(*index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool read_compound(const std::string& text, std::size_t* index, std::string* out) {
|
||||
const char open = text[*index];
|
||||
const char close = open == '{' ? '}' : ']';
|
||||
int depth = 0;
|
||||
const std::size_t start = *index;
|
||||
while (*index < text.size()) {
|
||||
const char c = text[*index];
|
||||
if (c == '"') {
|
||||
std::string ignored;
|
||||
if (!read_string(text, index, &ignored)) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == open) {
|
||||
++depth;
|
||||
} else if (c == close) {
|
||||
--depth;
|
||||
if (depth == 0) {
|
||||
++(*index);
|
||||
*out = text.substr(start, *index - start);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
++(*index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string quote(const std::string& text) {
|
||||
std::string out;
|
||||
out.reserve(text.size() + 2u);
|
||||
out.push_back('"');
|
||||
for (const char character : text) {
|
||||
const unsigned char value = static_cast<unsigned char>(character);
|
||||
switch (character) {
|
||||
case '"': out += "\\\""; break;
|
||||
case '\\': out += "\\\\"; break;
|
||||
case '\b': out += "\\b"; break;
|
||||
case '\f': out += "\\f"; break;
|
||||
case '\n': out += "\\n"; break;
|
||||
case '\r': out += "\\r"; break;
|
||||
case '\t': out += "\\t"; break;
|
||||
default:
|
||||
if (value < 0x20u) {
|
||||
out += "\\u00";
|
||||
out.push_back(kHexDigits[value >> 4u]);
|
||||
out.push_back(kHexDigits[value & 0x0Fu]);
|
||||
} else {
|
||||
out.push_back(character);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.push_back('"');
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string pretty_object(const std::vector<Field>& fields) {
|
||||
if (fields.empty()) {
|
||||
return "{}";
|
||||
}
|
||||
std::string out = "{";
|
||||
for (std::size_t index = 0u; index < fields.size(); ++index) {
|
||||
out += index == 0u ? "\n " : ",\n ";
|
||||
out += quote(fields[index].key);
|
||||
out += ": ";
|
||||
out += fields[index].value;
|
||||
}
|
||||
out += "\n}";
|
||||
return out;
|
||||
}
|
||||
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
|
||||
out->clear();
|
||||
std::size_t index = 0;
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size() || text[index] != '{') {
|
||||
if (error != nullptr) { *error = "metadata is not a JSON object"; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
for (;;) {
|
||||
skip_space(text, &index);
|
||||
if (index < text.size() && text[index] == '}') {
|
||||
++index;
|
||||
break;
|
||||
}
|
||||
if (index >= text.size() || text[index] == ',') {
|
||||
if (index >= text.size()) {
|
||||
if (error != nullptr) { *error = "unterminated JSON object"; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
Member member;
|
||||
if (!read_string(text, &index, &member.key)) {
|
||||
if (error != nullptr) { *error = "expected a JSON key"; }
|
||||
return false;
|
||||
}
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size() || text[index] != ':') {
|
||||
if (error != nullptr) { *error = "expected ':' after JSON key " + member.key; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size()) {
|
||||
if (error != nullptr) { *error = "missing JSON value for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
if (text[index] == '"') {
|
||||
member.is_string = true;
|
||||
if (!read_string(text, &index, &member.raw)) {
|
||||
if (error != nullptr) { *error = "bad JSON string for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
} else if (text[index] == '{' || text[index] == '[') {
|
||||
if (!read_compound(text, &index, &member.raw)) {
|
||||
if (error != nullptr) { *error = "bad JSON container for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
const std::size_t start = index;
|
||||
while (index < text.size() && text[index] != ',' && text[index] != '}') {
|
||||
++index;
|
||||
}
|
||||
member.raw = text.substr(start, index - start);
|
||||
while (!member.raw.empty() &&
|
||||
(member.raw.back() == ' ' || member.raw.back() == '\n' ||
|
||||
member.raw.back() == '\r' || member.raw.back() == '\t')) {
|
||||
member.raw.pop_back();
|
||||
}
|
||||
}
|
||||
for (const Member& existing : *out) {
|
||||
if (existing.key == member.key) {
|
||||
if (error != nullptr) { *error = "duplicate JSON key " + member.key; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out->push_back(std::move(member));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const Member* find(const std::vector<Member>& members, const std::string& key) {
|
||||
for (const Member& member : members) {
|
||||
if (member.key == key) {
|
||||
return &member;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool as_string(const Member& member, std::string* out) {
|
||||
if (!member.is_string || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
*out = member.raw;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool as_number(const Member& member, double* out) {
|
||||
if (member.is_string || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
char* end = nullptr;
|
||||
const double value = std::strtod(member.raw.c_str(), &end);
|
||||
if (end == member.raw.c_str() || !std::isfinite(value)) {
|
||||
return false;
|
||||
}
|
||||
*out = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool as_integer(const Member& member, long long* out) {
|
||||
double value = 0.0;
|
||||
if (!as_number(member, &value) || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (value != std::floor(value)) {
|
||||
return false;
|
||||
}
|
||||
*out = static_cast<long long>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::json
|
||||
@@ -1,43 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::json {
|
||||
|
||||
struct Member {
|
||||
std::string key;
|
||||
std::string raw;
|
||||
bool is_string = false;
|
||||
};
|
||||
|
||||
// Parses a top-level JSON object. Rejects non-objects and duplicate keys.
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error);
|
||||
|
||||
// One member of an object being written: `key` is escaped here, `value` is
|
||||
// already JSON text (a quoted string, a number, a literal).
|
||||
struct Field {
|
||||
std::string key;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
// Escapes `text` as a JSON string literal, quotes included. Matches
|
||||
// json.dumps(..., ensure_ascii=False): the named escapes for `"` and `\` and
|
||||
// for U+0008/U+0009/U+000A/U+000C/U+000D, `\u00XX` for the other control
|
||||
// characters, and every other byte - UTF-8 included - passed through.
|
||||
std::string quote(const std::string& text);
|
||||
|
||||
// Renders a flat object the way json.dumps(members, ensure_ascii=False,
|
||||
// indent=2) does: two-space indentation, one member per line, the members in
|
||||
// the order given, and no trailing newline.
|
||||
std::string pretty_object(const std::vector<Field>& fields);
|
||||
|
||||
const Member* find(const std::vector<Member>& members, const std::string& key);
|
||||
|
||||
bool as_string(const Member& member, std::string* out);
|
||||
bool as_number(const Member& member, double* out);
|
||||
bool as_integer(const Member& member, long long* out);
|
||||
|
||||
} // namespace joc::json
|
||||
@@ -1,25 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cfenv>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace joc::pynum {
|
||||
|
||||
inline long long py_round(double value) {
|
||||
return static_cast<long long>(std::nearbyint(value));
|
||||
}
|
||||
|
||||
inline std::string format_fixed(double value, int decimals) {
|
||||
char buffer[64];
|
||||
std::snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
inline long long trunc_to_ll(double value) {
|
||||
return static_cast<long long>(value);
|
||||
}
|
||||
|
||||
} // namespace joc::pynum
|
||||
@@ -1,165 +0,0 @@
|
||||
#include "foundation/sha256.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace joc::crypto {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t kK[64] = {
|
||||
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u,
|
||||
0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu,
|
||||
0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu,
|
||||
0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
|
||||
0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
|
||||
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu,
|
||||
0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u,
|
||||
0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
|
||||
0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u,
|
||||
0xc67178f2u};
|
||||
|
||||
inline std::uint32_t rotr(std::uint32_t value, unsigned count) {
|
||||
return (value >> count) | (value << (32u - count));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Sha256::reset() {
|
||||
state_[0] = 0x6a09e667u;
|
||||
state_[1] = 0xbb67ae85u;
|
||||
state_[2] = 0x3c6ef372u;
|
||||
state_[3] = 0xa54ff53au;
|
||||
state_[4] = 0x510e527fu;
|
||||
state_[5] = 0x9b05688cu;
|
||||
state_[6] = 0x1f83d9abu;
|
||||
state_[7] = 0x5be0cd19u;
|
||||
bit_count_ = 0;
|
||||
buffer_used_ = 0;
|
||||
std::memset(buffer_, 0, sizeof(buffer_));
|
||||
}
|
||||
|
||||
void Sha256::transform(const std::uint8_t block[64]) {
|
||||
std::uint32_t w[64];
|
||||
for (unsigned i = 0; i < 16; ++i) {
|
||||
w[i] = (static_cast<std::uint32_t>(block[i * 4]) << 24) |
|
||||
(static_cast<std::uint32_t>(block[i * 4 + 1]) << 16) |
|
||||
(static_cast<std::uint32_t>(block[i * 4 + 2]) << 8) |
|
||||
static_cast<std::uint32_t>(block[i * 4 + 3]);
|
||||
}
|
||||
for (unsigned i = 16; i < 64; ++i) {
|
||||
const std::uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
const std::uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
std::uint32_t a = state_[0];
|
||||
std::uint32_t b = state_[1];
|
||||
std::uint32_t c = state_[2];
|
||||
std::uint32_t d = state_[3];
|
||||
std::uint32_t e = state_[4];
|
||||
std::uint32_t f = state_[5];
|
||||
std::uint32_t g = state_[6];
|
||||
std::uint32_t h = state_[7];
|
||||
for (unsigned i = 0; i < 64; ++i) {
|
||||
const std::uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||
const std::uint32_t ch = (e & f) ^ (~e & g);
|
||||
const std::uint32_t temp1 = h + s1 + ch + kK[i] + w[i];
|
||||
const std::uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||
const std::uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
const std::uint32_t temp2 = s0 + maj;
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
state_[0] += a;
|
||||
state_[1] += b;
|
||||
state_[2] += c;
|
||||
state_[3] += d;
|
||||
state_[4] += e;
|
||||
state_[5] += f;
|
||||
state_[6] += g;
|
||||
state_[7] += h;
|
||||
}
|
||||
|
||||
void Sha256::update(const void* data, std::size_t size) {
|
||||
const std::uint8_t* bytes = static_cast<const std::uint8_t*>(data);
|
||||
bit_count_ += static_cast<std::uint64_t>(size) * 8u;
|
||||
while (size > 0) {
|
||||
const std::size_t space = 64u - buffer_used_;
|
||||
const std::size_t take = size < space ? size : space;
|
||||
std::memcpy(buffer_ + buffer_used_, bytes, take);
|
||||
buffer_used_ += take;
|
||||
bytes += take;
|
||||
size -= take;
|
||||
if (buffer_used_ == 64u) {
|
||||
transform(buffer_);
|
||||
buffer_used_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Sha256::finish(std::uint8_t out[32]) {
|
||||
const std::uint64_t total_bits = bit_count_;
|
||||
const std::uint8_t pad = 0x80u;
|
||||
update(&pad, 1);
|
||||
const std::uint8_t zero = 0x00u;
|
||||
while (buffer_used_ != 56u) {
|
||||
update(&zero, 1);
|
||||
}
|
||||
std::uint8_t length_bytes[8];
|
||||
for (unsigned i = 0; i < 8; ++i) {
|
||||
length_bytes[i] = static_cast<std::uint8_t>((total_bits >> (56u - i * 8u)) & 0xFFu);
|
||||
}
|
||||
std::memcpy(buffer_ + buffer_used_, length_bytes, 8);
|
||||
buffer_used_ += 8;
|
||||
transform(buffer_);
|
||||
buffer_used_ = 0;
|
||||
for (unsigned i = 0; i < 8; ++i) {
|
||||
out[i * 4 + 0] = static_cast<std::uint8_t>((state_[i] >> 24) & 0xFFu);
|
||||
out[i * 4 + 1] = static_cast<std::uint8_t>((state_[i] >> 16) & 0xFFu);
|
||||
out[i * 4 + 2] = static_cast<std::uint8_t>((state_[i] >> 8) & 0xFFu);
|
||||
out[i * 4 + 3] = static_cast<std::uint8_t>(state_[i] & 0xFFu);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Sha256::finish_hex() {
|
||||
std::uint8_t digest[32];
|
||||
finish(digest);
|
||||
static const char* kHex = "0123456789abcdef";
|
||||
std::string text;
|
||||
text.resize(64);
|
||||
for (unsigned i = 0; i < 32; ++i) {
|
||||
text[i * 2] = kHex[(digest[i] >> 4) & 0x0Fu];
|
||||
text[i * 2 + 1] = kHex[digest[i] & 0x0Fu];
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
std::string sha256_hex(const void* data, std::size_t size) {
|
||||
Sha256 hash;
|
||||
hash.update(data, size);
|
||||
return hash.finish_hex();
|
||||
}
|
||||
|
||||
bool sha256_hex_matches(const void* data, std::size_t size, const std::string& expected_hex) {
|
||||
if (expected_hex.size() != 64) {
|
||||
return false;
|
||||
}
|
||||
std::string actual = sha256_hex(data, size);
|
||||
for (std::size_t i = 0; i < 64; ++i) {
|
||||
char expected = expected_hex[i];
|
||||
if (expected >= 'A' && expected <= 'F') {
|
||||
expected = static_cast<char>(expected - 'A' + 'a');
|
||||
}
|
||||
if (actual[i] != expected) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::crypto
|
||||
@@ -1,31 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace joc::crypto {
|
||||
|
||||
class Sha256 {
|
||||
public:
|
||||
Sha256() { reset(); }
|
||||
|
||||
void reset();
|
||||
void update(const void* data, std::size_t size);
|
||||
void finish(std::uint8_t out[32]);
|
||||
std::string finish_hex();
|
||||
|
||||
private:
|
||||
void transform(const std::uint8_t block[64]);
|
||||
|
||||
std::uint32_t state_[8] = {};
|
||||
std::uint64_t bit_count_ = 0;
|
||||
std::uint8_t buffer_[64] = {};
|
||||
std::size_t buffer_used_ = 0;
|
||||
};
|
||||
|
||||
std::string sha256_hex(const void* data, std::size_t size);
|
||||
bool sha256_hex_matches(const void* data, std::size_t size, const std::string& expected_hex);
|
||||
|
||||
} // namespace joc::crypto
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc {
|
||||
|
||||
// The single definition of the per-thread detail string. It cannot live in the
|
||||
// header: a thread_local with dynamic initialisation that is included by several
|
||||
// translation units makes each of them emit its own TLS initialisation routine,
|
||||
// and ld64 rejects those as duplicate symbols (GNU ld and MSVC fold them).
|
||||
thread_local std::string last_error_detail;
|
||||
|
||||
} // namespace joc
|
||||
@@ -1,66 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc {
|
||||
|
||||
class Status {
|
||||
public:
|
||||
Status() = default;
|
||||
|
||||
static Status success() { return Status(); }
|
||||
|
||||
static Status fail(joc_error code, std::string stage, std::string message) {
|
||||
Status s;
|
||||
s.code_ = code;
|
||||
s.stage_ = std::move(stage);
|
||||
s.message_ = std::move(message);
|
||||
return s;
|
||||
}
|
||||
|
||||
bool ok() const { return code_ == JOC_OK; }
|
||||
joc_error code() const { return code_; }
|
||||
const std::string& stage() const { return stage_; }
|
||||
const std::string& message() const { return message_; }
|
||||
|
||||
private:
|
||||
joc_error code_ = JOC_OK;
|
||||
std::string stage_ = "none";
|
||||
std::string message_;
|
||||
};
|
||||
|
||||
// Structured detail of the most recent failing call, as returned by
|
||||
// joc_last_error_detail(). It is one instance per thread across the whole
|
||||
// library, so every C entry point -- the task API and the streaming API alike --
|
||||
// reports through the same string. Defined in status.cpp: a thread_local with
|
||||
// dynamic initialisation defined in a header emits a TLS initialisation routine
|
||||
// in every including unit, and ld64 rejects those duplicates.
|
||||
extern thread_local std::string last_error_detail;
|
||||
|
||||
inline void record_error_detail(const Status& status) {
|
||||
if (status.ok()) {
|
||||
last_error_detail.clear();
|
||||
return;
|
||||
}
|
||||
last_error_detail.assign(status.stage());
|
||||
last_error_detail.append(": ");
|
||||
last_error_detail.append(status.message());
|
||||
}
|
||||
|
||||
// Stage names are kept as plain literals so that C++ and the Python frontend
|
||||
namespace stage {
|
||||
inline constexpr const char* kFoundation = "foundation";
|
||||
inline constexpr const char* kEac3 = "eac3_transport";
|
||||
inline constexpr const char* kEmdf = "emdf";
|
||||
inline constexpr const char* kJoc = "joc";
|
||||
inline constexpr const char* kOamd = "oamd";
|
||||
inline constexpr const char* kDsp = "dsp";
|
||||
inline constexpr const char* kRender = "render";
|
||||
inline constexpr const char* kOutput = "output";
|
||||
} // namespace stage
|
||||
|
||||
} // namespace joc
|
||||
@@ -1,401 +0,0 @@
|
||||
#include "hrtf/jochrtf.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "foundation/mini_json.h"
|
||||
#include "foundation/sha256.h"
|
||||
#include "io/npy.h"
|
||||
#include "io/zip_reader.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string to_upper(std::string text) {
|
||||
for (char& c : text) {
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
c = static_cast<char>(c - 'a' + 'A');
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
bool is_sha256_hex(const std::string& text) {
|
||||
if (text.size() != 64) {
|
||||
return false;
|
||||
}
|
||||
for (const char c : text) {
|
||||
const bool digit = c >= '0' && c <= '9';
|
||||
const bool upper = c >= 'A' && c <= 'F';
|
||||
if (!digit && !upper) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// json.dumps(list(shape)) as the reference writes it, e.g. "[36, 2, 77]".
|
||||
std::string shape_json(const std::vector<std::int64_t>& shape) {
|
||||
std::string text = "[";
|
||||
for (std::size_t i = 0; i < shape.size(); ++i) {
|
||||
text += (i == 0 ? "" : ", ");
|
||||
text += std::to_string(shape[i]);
|
||||
}
|
||||
text += "]";
|
||||
return text;
|
||||
}
|
||||
|
||||
Status hrtf_fail(const std::string& message) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender, message);
|
||||
}
|
||||
|
||||
std::string payload_sha256(const std::vector<double>& centers,
|
||||
const std::vector<double>& coefficients,
|
||||
const std::vector<double>& delay_coefficients,
|
||||
const std::vector<double>& delay_bounds) {
|
||||
crypto::Sha256 hash;
|
||||
const char prefix[] = "JOC-HRTF-CACHE-PAYLOAD-V1";
|
||||
hash.update(prefix, sizeof(prefix) - 1);
|
||||
const std::uint8_t zero = 0;
|
||||
hash.update(&zero, 1);
|
||||
|
||||
struct Entry {
|
||||
const char* name;
|
||||
const char* dtype;
|
||||
const std::vector<double>* values;
|
||||
std::vector<std::int64_t> shape;
|
||||
};
|
||||
const Entry entries[4] = {
|
||||
{"band_center_frequencies_hz", "<f8", ¢ers, {kHybridBands}},
|
||||
{"coefficients", "<c16", &coefficients, {kShTerms, kEars, kHybridBands}},
|
||||
{"delay_coefficients", "<f8", &delay_coefficients, {kShTerms, kEars}},
|
||||
{"delay_bounds", "<f8", &delay_bounds, {2, 2}},
|
||||
};
|
||||
for (const Entry& entry : entries) {
|
||||
const std::string name(entry.name);
|
||||
const std::string dtype(entry.dtype);
|
||||
const std::string shape = shape_json(entry.shape);
|
||||
hash.update(name.data(), name.size());
|
||||
hash.update(&zero, 1);
|
||||
hash.update(dtype.data(), dtype.size());
|
||||
hash.update(&zero, 1);
|
||||
hash.update(shape.data(), shape.size());
|
||||
hash.update(&zero, 1);
|
||||
hash.update(entry.values->data(), entry.values->size() * sizeof(double));
|
||||
}
|
||||
return hash.finish_hex();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Status load_jochrtf(const std::string& path, Field* out) {
|
||||
if (out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, "null field");
|
||||
}
|
||||
io::ZipArchive archive;
|
||||
std::string error;
|
||||
if (!archive.open(path, &error)) {
|
||||
return Status::fail(JOC_ERR_HRTF_NOT_FOUND, stage::kRender,
|
||||
"cannot read compiled HRTF " + path + ": " + error);
|
||||
}
|
||||
|
||||
// Member set must be exactly the five expected names.
|
||||
static const char* kMembers[5] = {"metadata_json.npy", "band_center_frequencies_hz.npy",
|
||||
"coefficients.npy", "delay_coefficients.npy",
|
||||
"delay_bounds.npy"};
|
||||
if (archive.entries().size() != 5u) {
|
||||
return hrtf_fail("compiled HRTF cache has an invalid member set (" +
|
||||
std::to_string(archive.entries().size()) + " members)");
|
||||
}
|
||||
for (const char* name : kMembers) {
|
||||
if (archive.find(name) == nullptr) {
|
||||
return hrtf_fail(std::string("compiled HRTF cache is missing ") + name);
|
||||
}
|
||||
}
|
||||
|
||||
auto read_member = [&](const char* name, std::vector<std::uint8_t>* raw,
|
||||
io::NpyArray* array) -> Status {
|
||||
if (!archive.read_member(name, raw, &error)) {
|
||||
return hrtf_fail(std::string("compiled HRTF member ") + name + ": " + error);
|
||||
}
|
||||
if (!io::parse_npy(raw->data(), raw->size(), array, &error)) {
|
||||
return hrtf_fail(std::string("compiled HRTF member ") + name + ": " + error);
|
||||
}
|
||||
if (array->fortran_order) {
|
||||
return hrtf_fail(std::string("compiled HRTF member must be C-contiguous: ") + name);
|
||||
}
|
||||
return Status::success();
|
||||
};
|
||||
|
||||
std::vector<std::uint8_t> raw;
|
||||
io::NpyArray array;
|
||||
|
||||
Status status = read_member("metadata_json.npy", &raw, &array);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
std::string metadata_text;
|
||||
if (!io::npy_unicode_to_utf8(array, &metadata_text, &error)) {
|
||||
return hrtf_fail("compiled HRTF metadata: " + error);
|
||||
}
|
||||
if (metadata_text.size() > 64u * 1024u) {
|
||||
return hrtf_fail("compiled HRTF metadata is too large");
|
||||
}
|
||||
|
||||
status = read_member("band_center_frequencies_hz.npy", &raw, &array);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
if (array.descr != "<f8" || !io::npy_shape_is(array, {kHybridBands})) {
|
||||
return hrtf_fail("band_center_frequencies_hz must be <f8(77,)");
|
||||
}
|
||||
std::vector<double> centers;
|
||||
io::npy_to_double(array, ¢ers, &error);
|
||||
|
||||
status = read_member("coefficients.npy", &raw, &array);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
if (array.descr != "<c16" || !io::npy_shape_is(array, {kShTerms, kEars, kHybridBands})) {
|
||||
return hrtf_fail("coefficients must be <c16(36, 2, 77)");
|
||||
}
|
||||
std::vector<double> coefficients;
|
||||
if (!io::npy_to_double(array, &coefficients, &error)) {
|
||||
return hrtf_fail("coefficients: " + error);
|
||||
}
|
||||
|
||||
status = read_member("delay_coefficients.npy", &raw, &array);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
if (array.descr != "<f8" || !io::npy_shape_is(array, {kShTerms, kEars})) {
|
||||
return hrtf_fail("delay_coefficients must be <f8(36, 2)");
|
||||
}
|
||||
std::vector<double> delay_coefficients;
|
||||
io::npy_to_double(array, &delay_coefficients, &error);
|
||||
|
||||
status = read_member("delay_bounds.npy", &raw, &array);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
if (array.descr != "<f8" || !io::npy_shape_is(array, {2, 2})) {
|
||||
return hrtf_fail("delay_bounds must be <f8(2, 2)");
|
||||
}
|
||||
std::vector<double> delay_bounds;
|
||||
io::npy_to_double(array, &delay_bounds, &error);
|
||||
|
||||
std::vector<json::Member> members;
|
||||
if (!json::parse_object(metadata_text, &members, &error)) {
|
||||
return hrtf_fail("compiled HRTF metadata: " + error);
|
||||
}
|
||||
auto require_string = [&](const char* key, std::string* value) -> Status {
|
||||
const json::Member* member = json::find(members, key);
|
||||
if (member == nullptr || !json::as_string(*member, value)) {
|
||||
return hrtf_fail(std::string("compiled HRTF metadata is missing ") + key);
|
||||
}
|
||||
return Status::success();
|
||||
};
|
||||
std::string magic;
|
||||
std::string schema;
|
||||
std::string source_sha256;
|
||||
std::string cache_key;
|
||||
std::string payload_hash;
|
||||
std::string delay_source;
|
||||
status = require_string("magic", &magic);
|
||||
if (!status.ok()) { return status; }
|
||||
status = require_string("cache_schema", &schema);
|
||||
if (!status.ok()) { return status; }
|
||||
status = require_string("source_sha256", &source_sha256);
|
||||
if (!status.ok()) { return status; }
|
||||
status = require_string("cache_key", &cache_key);
|
||||
if (!status.ok()) { return status; }
|
||||
status = require_string("payload_sha256", &payload_hash);
|
||||
if (!status.ok()) { return status; }
|
||||
status = require_string("delay_source", &delay_source);
|
||||
if (!status.ok()) { return status; }
|
||||
|
||||
if (magic != kMagic) {
|
||||
return hrtf_fail("compiled HRTF magic mismatch: " + magic);
|
||||
}
|
||||
if (schema != kCacheSchema) {
|
||||
return hrtf_fail("compiled HRTF cache schema mismatch: " + schema);
|
||||
}
|
||||
const json::Member* version_member = json::find(members, "format_version");
|
||||
long long version = -1;
|
||||
if (version_member == nullptr || !json::as_integer(*version_member, &version)) {
|
||||
return hrtf_fail("compiled HRTF metadata is missing format_version");
|
||||
}
|
||||
if (version != kFormatVersion) {
|
||||
return Status::fail(JOC_ERR_HRTF_VERSION, stage::kRender,
|
||||
"unsupported .jochrtf version " + std::to_string(version) +
|
||||
"; rebuild it from the source SOFA");
|
||||
}
|
||||
out->source_sha256 = to_upper(source_sha256);
|
||||
out->cache_key = to_upper(cache_key);
|
||||
if (!is_sha256_hex(out->source_sha256)) {
|
||||
return hrtf_fail("compiled HRTF source_sha256 is not a 64-digit digest");
|
||||
}
|
||||
if (!is_sha256_hex(out->cache_key)) {
|
||||
return hrtf_fail("compiled HRTF cache_key is not a 64-digit digest");
|
||||
}
|
||||
|
||||
const std::string expected = payload_sha256(centers, coefficients, delay_coefficients,
|
||||
delay_bounds);
|
||||
if (to_upper(payload_hash) != to_upper(expected)) {
|
||||
return Status::fail(JOC_ERR_HRTF_HASH, stage::kRender,
|
||||
"compiled HRTF payload hash mismatch");
|
||||
}
|
||||
out->payload_sha256 = to_upper(payload_hash);
|
||||
|
||||
const json::Member* radius_member = json::find(members, "measurement_radius_m");
|
||||
double radius = 0.0;
|
||||
if (radius_member == nullptr || !json::as_number(*radius_member, &radius) || radius <= 0.0) {
|
||||
return hrtf_fail("compiled HRTF measurement_radius_m must be a positive number");
|
||||
}
|
||||
out->measurement_radius_m = radius;
|
||||
const json::Member* order_member = json::find(members, "order");
|
||||
long long order = 0;
|
||||
if (order_member == nullptr || !json::as_integer(*order_member, &order) || order <= 0 ||
|
||||
order * order > kShTerms) {
|
||||
return hrtf_fail("compiled HRTF order is out of range");
|
||||
}
|
||||
out->order = order;
|
||||
|
||||
for (const double value : coefficients) {
|
||||
if (!std::isfinite(value)) {
|
||||
return hrtf_fail("compiled HRTF coefficients contain non-finite values");
|
||||
}
|
||||
}
|
||||
for (const double value : delay_coefficients) {
|
||||
if (!std::isfinite(value) || std::abs(value) > 48000.0 * 64.0) {
|
||||
return hrtf_fail("compiled HRTF delay coefficients are out of range");
|
||||
}
|
||||
}
|
||||
for (const double value : delay_bounds) {
|
||||
if (!std::isfinite(value)) {
|
||||
return hrtf_fail("compiled HRTF delay bounds contain non-finite values");
|
||||
}
|
||||
}
|
||||
if (delay_bounds.size() == 4u && delay_bounds[0] > delay_bounds[1]) {
|
||||
return hrtf_fail("compiled HRTF delay bounds are inverted");
|
||||
}
|
||||
|
||||
if (const json::Member* member = json::find(members, "compiler_version")) {
|
||||
json::as_string(*member, &out->compiler_version);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "phase_policy_version")) {
|
||||
json::as_string(*member, &out->phase_policy_version);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "sh_convention")) {
|
||||
json::as_string(*member, &out->sh_convention);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "source_display_name")) {
|
||||
json::as_string(*member, &out->source_display_name);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "projection_ridge")) {
|
||||
json::as_number(*member, &out->projection_ridge);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "spherical_harmonic_ridge")) {
|
||||
json::as_number(*member, &out->spherical_harmonic_ridge);
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "fit_report")) {
|
||||
out->fit_report_json = member->raw;
|
||||
}
|
||||
if (const json::Member* member = json::find(members, "filterbank")) {
|
||||
out->filterbank_json = member->raw;
|
||||
}
|
||||
out->delay_source = delay_source;
|
||||
out->metadata_json = metadata_text;
|
||||
out->coefficients = std::move(coefficients);
|
||||
out->delay_coefficients = std::move(delay_coefficients);
|
||||
out->delay_bounds = std::move(delay_bounds);
|
||||
out->band_centers_hz = std::move(centers);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status load_kernels(const std::string& npz_path, Kernels* out) {
|
||||
if (out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, "null kernels");
|
||||
}
|
||||
io::ZipArchive archive;
|
||||
std::string error;
|
||||
if (!archive.open(npz_path, &error)) {
|
||||
return Status::fail(JOC_ERR_HRTF_NOT_FOUND, stage::kRender,
|
||||
"cannot read kernel tables " + npz_path + ": " + error);
|
||||
}
|
||||
|
||||
struct Request {
|
||||
const char* member;
|
||||
const char* shape_text;
|
||||
std::vector<std::int64_t> shape;
|
||||
};
|
||||
const Request requests[6] = {
|
||||
{"qmf_analysis_coefficients.npy", "<f4", {64, 10}},
|
||||
{"hybrid_analysis_low_kernel.npy", "<f4", {3, 2, 13, 16, 2}},
|
||||
{"hybrid_synthesis_indices.npy", "<i2", {154, 4}},
|
||||
{"hybrid_synthesis_values.npy", "<f4", {154}},
|
||||
{"qmf_synthesis_basis.npy", "<f8", {64, 4, 128}},
|
||||
{"qmf_synthesis_taps.npy", "<f8", {64, 10, 4}},
|
||||
};
|
||||
|
||||
std::vector<std::uint8_t> raw;
|
||||
std::vector<std::uint8_t> ordered;
|
||||
for (const Request& request : requests) {
|
||||
const std::string name = request.member;
|
||||
if (!archive.read_member(name, &raw, &error)) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"kernel table member " + name + ": " + error);
|
||||
}
|
||||
io::NpyArray array;
|
||||
if (!io::parse_npy(raw.data(), raw.size(), &array, &error)) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"kernel table member " + name + ": " + error);
|
||||
}
|
||||
if (array.descr != request.shape_text || !io::npy_shape_is(array, request.shape)) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"kernel table member " + name + " has an unexpected dtype/shape");
|
||||
}
|
||||
// Logical C order: required because the reused kernel indexes the hybrid
|
||||
// synthesis table row-major while the shipped member is Fortran-order.
|
||||
if (!io::npy_to_c_order(array, &ordered, &error)) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"kernel table member " + name + ": " + error);
|
||||
}
|
||||
const std::size_t count = array.element_count();
|
||||
if (std::strcmp(request.member, "qmf_analysis_coefficients.npy") == 0) {
|
||||
std::vector<float> values(count);
|
||||
std::memcpy(values.data(), ordered.data(), count * sizeof(float));
|
||||
out->qmf_analysis.assign(values.begin(), values.end());
|
||||
} else if (std::strcmp(request.member, "hybrid_analysis_low_kernel.npy") == 0) {
|
||||
std::vector<float> values(count);
|
||||
std::memcpy(values.data(), ordered.data(), count * sizeof(float));
|
||||
out->hybrid_low.assign(values.begin(), values.end());
|
||||
} else if (std::strcmp(request.member, "hybrid_synthesis_indices.npy") == 0) {
|
||||
out->hybrid_indices.resize(count);
|
||||
std::memcpy(out->hybrid_indices.data(), ordered.data(), count * sizeof(std::int16_t));
|
||||
} else if (std::strcmp(request.member, "hybrid_synthesis_values.npy") == 0) {
|
||||
std::vector<float> values(count);
|
||||
std::memcpy(values.data(), ordered.data(), count * sizeof(float));
|
||||
out->hybrid_values.assign(values.begin(), values.end());
|
||||
} else if (std::strcmp(request.member, "qmf_synthesis_basis.npy") == 0) {
|
||||
std::memcpy(out->qmf_basis.empty() ? (out->qmf_basis.resize(count), out->qmf_basis.data())
|
||||
: out->qmf_basis.data(),
|
||||
ordered.data(), count * sizeof(double));
|
||||
out->qmf_basis.resize(count);
|
||||
} else {
|
||||
out->qmf_taps.resize(count);
|
||||
std::memcpy(out->qmf_taps.data(), ordered.data(), count * sizeof(double));
|
||||
}
|
||||
}
|
||||
out->hybrid_count = static_cast<std::uint32_t>(out->hybrid_values.size());
|
||||
if (out->hybrid_count == 0u) {
|
||||
return Status::fail(JOC_ERR_HRTF_FORMAT, stage::kRender,
|
||||
"kernel tables contain no hybrid synthesis entries");
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,66 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
inline constexpr int kShTerms = 36;
|
||||
inline constexpr int kEars = 2;
|
||||
inline constexpr int kHybridBands = 77;
|
||||
inline constexpr int kFormatVersion = 1;
|
||||
inline constexpr const char* kMagic = "JOC-HRTF-CACHE";
|
||||
inline constexpr const char* kCacheSchema = "joc-compiled-hrtf-v1";
|
||||
|
||||
struct Field {
|
||||
std::vector<double> coefficients;
|
||||
std::vector<double> delay_coefficients;
|
||||
std::vector<double> delay_bounds;
|
||||
std::vector<double> band_centers_hz;
|
||||
double measurement_radius_m = 1.0;
|
||||
long long order = 5;
|
||||
std::string source_sha256;
|
||||
std::string cache_key;
|
||||
std::string payload_sha256;
|
||||
std::string delay_source;
|
||||
std::string compiler_version;
|
||||
std::string phase_policy_version;
|
||||
std::string sh_convention;
|
||||
std::string filterbank_json;
|
||||
std::string metadata_json;
|
||||
// Compile-side metadata, needed to write the cache back out unchanged.
|
||||
std::string source_display_name;
|
||||
std::string fit_report_json;
|
||||
double projection_ridge = 0.0;
|
||||
double spherical_harmonic_ridge = 0.0;
|
||||
};
|
||||
|
||||
Status load_jochrtf(const std::string& path, Field* out);
|
||||
|
||||
// Binaural filterbank kernels, as the reused kernel expects them (C order, the
|
||||
// exact dtypes of the ABI parameters).
|
||||
struct Kernels {
|
||||
std::vector<double> qmf_analysis;
|
||||
std::vector<double> hybrid_low;
|
||||
std::vector<std::int16_t> hybrid_indices;
|
||||
std::vector<double> hybrid_values;
|
||||
std::vector<double> qmf_basis;
|
||||
std::vector<double> qmf_taps;
|
||||
std::uint32_t hybrid_count = 0;
|
||||
};
|
||||
|
||||
// Loads a kernel-table archive. The file path is an override for verification;
|
||||
// the shipped tables are embedded (see builtin_kernels) so no data file is needed.
|
||||
// The Fortran-order index member is transposed into C order on purpose: the reused
|
||||
// kernel indexes the hybrid synthesis table row-major.
|
||||
Status load_kernels(const std::string& npz_path, Kernels* out);
|
||||
|
||||
// The public filterbank tables compiled into the library (identical values to the
|
||||
// archive the file loader accepts; the unit test checks their hashes).
|
||||
const Kernels& builtin_kernels();
|
||||
|
||||
} // namespace joc::hrtf
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,610 +0,0 @@
|
||||
#include "hrtf/public_filterbank.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/fft.h"
|
||||
#include "simd/simd.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
constexpr std::size_t kQmfLength = dsp::kQmfFftSize;
|
||||
constexpr int kQmfTaps = 10;
|
||||
constexpr int kSynthesisRank = 4;
|
||||
constexpr int kSynthesisTaps = 10;
|
||||
|
||||
// ----------------------------------------------------------- filterbank -----
|
||||
|
||||
// One shared forward plan for the 128-point QMF transform. The analysis bank runs
|
||||
// it 2 * slots * channels times per chunk, so the twiddle recurrence is built once
|
||||
// instead of being re-derived inside every butterfly.
|
||||
const dsp::FftPlan& qmf_fft_plan() {
|
||||
static const dsp::FftPlan plan(dsp::kQmfFftSize, false);
|
||||
return plan;
|
||||
}
|
||||
|
||||
// Public 64-band complex QMF analysis (public_filterbank.QmfAnalysis).
|
||||
class QmfAnalysis {
|
||||
public:
|
||||
static_assert(static_cast<std::size_t>(kQmfBands) == simd::kQmfAnalysisBands,
|
||||
"the dispatched accumulate is written for this band count");
|
||||
QmfAnalysis(const Kernels& kernels, std::size_t channels)
|
||||
: channels_(channels), coefficients_(kernels.qmf_analysis) {
|
||||
history_.assign(9u * channels_ * kQmfBands, 0.0);
|
||||
// The polyphase MAC consumes one coefficient per band, so the shipped
|
||||
// [band][tap] layout makes its inner loop a stride-10 gather. Transposing
|
||||
// once here turns that into a contiguous AXPY. The coefficient values and
|
||||
// the accumulation order are untouched, so the sums are bit-identical.
|
||||
coefficients_by_lag_.resize(static_cast<std::size_t>(kQmfTaps) * kQmfBands);
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
for (int tap = 0; tap < kQmfTaps; ++tap) {
|
||||
coefficients_by_lag_[static_cast<std::size_t>(tap) * kQmfBands +
|
||||
static_cast<std::size_t>(band)] =
|
||||
coefficients_[static_cast<std::size_t>(band) * kQmfTaps +
|
||||
static_cast<std::size_t>(tap)];
|
||||
}
|
||||
}
|
||||
premultiply_.resize(kQmfBands);
|
||||
post_.resize(kQmfBands);
|
||||
even_post_.resize(kQmfBands);
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
const double phase = static_cast<double>(band);
|
||||
premultiply_[static_cast<std::size_t>(band)] =
|
||||
std::polar(1.0, -kPi * phase / 128.0);
|
||||
post_[static_cast<std::size_t>(band)] =
|
||||
std::polar(1.0, -3.0 * (phase + 0.5) * kPi / 128.0);
|
||||
even_post_[static_cast<std::size_t>(band)] =
|
||||
Complex(0.0, band % 2 == 0 ? 1.0 : -1.0);
|
||||
}
|
||||
}
|
||||
|
||||
void reset() { std::fill(history_.begin(), history_.end(), 0.0); }
|
||||
|
||||
// samples: [slots*64, channels]; output: [slots, channels, 64] complex.
|
||||
void process(const std::vector<double>& samples, std::size_t slots,
|
||||
std::vector<Complex>* output) {
|
||||
const std::size_t joined_slots = 9u + slots;
|
||||
const std::size_t history_size = 9u * channels_ * kQmfBands;
|
||||
const std::size_t joined_size = joined_slots * channels_ * kQmfBands;
|
||||
// The joined window is filled completely -- the history lands in its first
|
||||
// 9 * channels * 64 entries and the new samples in the rest -- so it is a
|
||||
// reusable scratch buffer rather than a fresh zero-filled allocation. The
|
||||
// history tail is taken by index instead of from end(), because the buffer may
|
||||
// be longer than the window this call uses.
|
||||
if (joined_.size() < joined_size) {
|
||||
joined_.resize(joined_size);
|
||||
}
|
||||
std::copy(history_.begin(), history_.end(), joined_.begin());
|
||||
std::copy(samples.begin(), samples.begin() + static_cast<std::ptrdiff_t>(slots * channels_ * kQmfBands),
|
||||
joined_.begin() + static_cast<std::ptrdiff_t>(history_size));
|
||||
|
||||
// The two polyphase accumulators are read before they are written, so their
|
||||
// zero fill is load-bearing and stays; only the per-call allocation goes.
|
||||
const std::size_t accumulator_size = slots * channels_ * kQmfBands;
|
||||
if (even_.size() < accumulator_size) {
|
||||
even_.resize(accumulator_size);
|
||||
}
|
||||
if (odd_.size() < accumulator_size) {
|
||||
odd_.resize(accumulator_size);
|
||||
}
|
||||
std::fill(even_.begin(), even_.begin() + static_cast<std::ptrdiff_t>(accumulator_size), 0.0);
|
||||
std::fill(odd_.begin(), odd_.begin() + static_cast<std::ptrdiff_t>(accumulator_size), 0.0);
|
||||
// The ten lags are ten accumulate passes over the same 64 bands with one
|
||||
// shared coefficient row; the bands are independent accumulations of a
|
||||
// single product each, so they are what the dispatched kernel puts in its
|
||||
// lanes, and every band keeps the caller's own multiply-then-add.
|
||||
//
|
||||
// Slots are processed in blocks, with the lag loop inside: one lag pass
|
||||
// touches every source row once, so running the ten passes over the whole
|
||||
// chunk re-reads the joined window ten times -- at 1536 slots that is
|
||||
// hundreds of megabytes per chunk and the loop ends up bound by memory, not
|
||||
// by arithmetic. A block's ten lag passes instead slide over a window of
|
||||
// (block + 9) rows that stays in the second-level cache. Lags still run in
|
||||
// ascending order inside a block, which is the order each output's sum is
|
||||
// formed in, so nothing about the arithmetic changes.
|
||||
constexpr std::size_t kSlotBlock = 32;
|
||||
for (std::size_t first = 0u; first < slots; first += kSlotBlock) {
|
||||
const std::size_t block = std::min(kSlotBlock, slots - first);
|
||||
for (int lag = 0; lag < kQmfTaps; ++lag) {
|
||||
std::vector<double>& target = (lag % 2 == 0) ? even_ : odd_;
|
||||
const double* row =
|
||||
coefficients_by_lag_.data() + static_cast<std::size_t>(lag) * kQmfBands;
|
||||
const std::size_t source_slot = 9u - static_cast<std::size_t>(lag) + first;
|
||||
simd::qmf_analysis_taps(
|
||||
target.data() + first * channels_ * kQmfBands,
|
||||
joined_.data() + source_slot * channels_ * kQmfBands, row,
|
||||
block * channels_);
|
||||
}
|
||||
}
|
||||
std::copy(joined_.begin() + static_cast<std::ptrdiff_t>(joined_size - history_size),
|
||||
joined_.begin() + static_cast<std::ptrdiff_t>(joined_size), history_.begin());
|
||||
|
||||
// Every output element is assigned below, so the size is all that has to be
|
||||
// established; a resize of an already correctly sized buffer touches nothing.
|
||||
output->resize(slots * channels_ * kQmfBands);
|
||||
std::array<Complex, dsp::kQmfFftSize> even_spectrum{};
|
||||
std::array<Complex, dsp::kQmfFftSize> odd_spectrum{};
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels_; ++channel) {
|
||||
const double* even_values = even_.data() + (slot * channels_ + channel) * kQmfBands;
|
||||
const double* odd_values = odd_.data() + (slot * channels_ + channel) * kQmfBands;
|
||||
transform(even_values, &even_spectrum);
|
||||
transform(odd_values, &odd_spectrum);
|
||||
Complex* destination =
|
||||
output->data() + (slot * channels_ + channel) * kQmfBands;
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
destination[band] = odd_spectrum[static_cast<std::size_t>(band)] +
|
||||
even_spectrum[static_cast<std::size_t>(band)] *
|
||||
even_post_[static_cast<std::size_t>(band)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void transform(const double* values, std::array<Complex, dsp::kQmfFftSize>* spectrum) {
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
(*spectrum)[static_cast<std::size_t>(band)] =
|
||||
Complex(values[band], 0.0) * premultiply_[static_cast<std::size_t>(band)];
|
||||
}
|
||||
for (int index = kQmfBands; index < dsp::kQmfFftSize; ++index) {
|
||||
(*spectrum)[static_cast<std::size_t>(index)] = Complex(0.0, 0.0);
|
||||
}
|
||||
dsp::fft_radix2(spectrum, qmf_fft_plan());
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
(*spectrum)[static_cast<std::size_t>(band)] *= post_[static_cast<std::size_t>(band)];
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t channels_;
|
||||
std::vector<double> coefficients_; // [64][10]
|
||||
std::vector<double> coefficients_by_lag_; // [10][64], the same values transposed
|
||||
std::vector<double> history_; // [9][channels][64]
|
||||
std::vector<double> joined_; // scratch, [9 + slots][channels][64]
|
||||
std::vector<double> even_; // scratch, [slots][channels][64], zeroed per call
|
||||
std::vector<double> odd_; // scratch, [slots][channels][64], zeroed per call
|
||||
std::vector<Complex> premultiply_;
|
||||
std::vector<Complex> post_;
|
||||
std::vector<Complex> even_post_;
|
||||
};
|
||||
|
||||
// Sparse 64-QMF to 77-hybrid analysis (public_filterbank.HybridAnalysis).
|
||||
class HybridAnalysis {
|
||||
public:
|
||||
HybridAnalysis(const Kernels& kernels, std::size_t channels)
|
||||
: channels_(channels), low_kernel_(kernels.hybrid_low) {
|
||||
history_.assign(12u * channels_ * 3u * 2u, 0.0);
|
||||
high_history_.assign(6u * channels_ * 61u, Complex(0.0, 0.0));
|
||||
// The dispatched join walks one term at a time and adds its 32 weights to
|
||||
// 32 outputs, so the shipped [tap][band][component] table is regrouped to
|
||||
// the term order the caller accumulates in. Same weights, same order.
|
||||
const std::size_t outputs = simd::kHybridOutputs;
|
||||
low_by_term_.resize(simd::kHybridTerms * outputs);
|
||||
for (int lag = 0; lag < 13; ++lag) {
|
||||
for (int point = 0; point < 3; ++point) {
|
||||
for (int input = 0; input < 2; ++input) {
|
||||
const std::size_t term =
|
||||
(static_cast<std::size_t>(lag) * 3u + static_cast<std::size_t>(point)) * 2u +
|
||||
static_cast<std::size_t>(input);
|
||||
const std::size_t source = (static_cast<std::size_t>(point) * 2u +
|
||||
static_cast<std::size_t>(input)) * 13u +
|
||||
static_cast<std::size_t>(lag);
|
||||
for (std::size_t output = 0u; output < outputs; ++output) {
|
||||
low_by_term_[term * outputs + output] =
|
||||
low_kernel_[source * outputs + output];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
low_values_.resize(simd::kHybridJoinBlock * simd::kHybridTerms);
|
||||
low_out_.resize(simd::kHybridJoinBlock * outputs);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
std::fill(history_.begin(), history_.end(), 0.0);
|
||||
std::fill(high_history_.begin(), high_history_.end(), Complex(0.0, 0.0));
|
||||
}
|
||||
|
||||
// qmf: [slots, channels, 64]; output: [slots, channels, 77] complex.
|
||||
void process(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* output) {
|
||||
const std::size_t joined_slots = 12u + slots;
|
||||
const std::size_t history_size = 12u * channels_ * 6u;
|
||||
const std::size_t joined_size = joined_slots * channels_ * 3u * 2u;
|
||||
// Both the joined window and the pending high-band history are written in full
|
||||
// before they are read, so they are reused scratch buffers; the history tail is
|
||||
// taken by index because the buffer can be longer than this call's window.
|
||||
if (joined_.size() < joined_size) {
|
||||
joined_.resize(joined_size);
|
||||
}
|
||||
std::copy(history_.begin(), history_.end(), joined_.begin());
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels_; ++channel) {
|
||||
const Complex* source = qmf.data() + (slot * channels_ + channel) * kQmfBands;
|
||||
double* destination =
|
||||
joined_.data() + ((12u + slot) * channels_ + channel) * 6u;
|
||||
for (int band = 0; band < 3; ++band) {
|
||||
destination[static_cast<std::size_t>(band) * 2u] = source[band].real();
|
||||
destination[static_cast<std::size_t>(band) * 2u + 1u] = source[band].imag();
|
||||
}
|
||||
}
|
||||
}
|
||||
// The low bands are accumulated in a register block and written straight into
|
||||
// the output, and the high bands are written by the pass below; between them
|
||||
// every one of the 77 bands is assigned, so only the size has to be set.
|
||||
output->resize(slots * channels_ * kHybridBands);
|
||||
// The thirteen taps are summed in a per-output register block and the low
|
||||
// bands are written straight into the output. Keeping a separate low plane
|
||||
// and then copying it into the output re-streams tens of megabytes per chunk
|
||||
// for nothing, and only the first kHybridLow bands are ever touched. The
|
||||
// join itself is dispatched (see src/simd/simd.h): the 32 outputs of a
|
||||
// row are 32 independent accumulations over the same 78 terms, which is what
|
||||
// shares a vector. Every lane keeps the caller's term order -- lag, then
|
||||
// point, then input -- and its two roundings, and skips exactly the terms
|
||||
// this loop skips. Rows are staged in blocks so the gathered values do not
|
||||
// spill out of the first-level cache.
|
||||
const std::size_t hybrid_rows = slots * channels_;
|
||||
const std::size_t block = simd::kHybridJoinBlock;
|
||||
const std::size_t terms = simd::kHybridTerms;
|
||||
for (std::size_t first = 0u; first < hybrid_rows; first += block) {
|
||||
const std::size_t count = std::min(block, hybrid_rows - first);
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
const std::size_t row = first + index;
|
||||
const std::size_t slot = row / channels_;
|
||||
const std::size_t channel = row % channels_;
|
||||
double* staged = low_values_.data() + index * terms;
|
||||
for (int lag = 0; lag < 13; ++lag) {
|
||||
const std::size_t source_slot = 12u - static_cast<std::size_t>(lag) + slot;
|
||||
const double* source =
|
||||
joined_.data() + (source_slot * channels_ + channel) * 6u;
|
||||
for (int point = 0; point < 3; ++point) {
|
||||
for (int input = 0; input < 2; ++input) {
|
||||
staged[(static_cast<std::size_t>(lag) * 3u +
|
||||
static_cast<std::size_t>(point)) * 2u +
|
||||
static_cast<std::size_t>(input)] =
|
||||
source[static_cast<std::size_t>(point) * 2u +
|
||||
static_cast<std::size_t>(input)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
simd::hybrid_low_join(low_values_.data(), low_by_term_.data(),
|
||||
low_out_.data(), count);
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
Complex* destination = output->data() + (first + index) * kHybridBands;
|
||||
const double* values = low_out_.data() + index * simd::kHybridOutputs;
|
||||
for (int band = 0; band < kHybridLow; ++band) {
|
||||
destination[band] = Complex(values[static_cast<std::size_t>(band) * 2u],
|
||||
values[static_cast<std::size_t>(band) * 2u + 1u]);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::copy(joined_.begin() + static_cast<std::ptrdiff_t>(joined_size - history_size),
|
||||
joined_.begin() + static_cast<std::ptrdiff_t>(joined_size), history_.begin());
|
||||
|
||||
// The high bands pass through unchanged but delayed by the six slots of
|
||||
// history the reference concatenates in front of them. Only the last six
|
||||
// entries of that concatenation survive into high_history_, so a six-entry
|
||||
// register replaces the (6 + slots) plane and its full copy. Note the
|
||||
// output reads the concatenation at index `slot`, not `6 + slot`, so the
|
||||
// first six output slots come from the history: that offset is part of the
|
||||
// current output and is preserved verbatim.
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels_; ++channel) {
|
||||
Complex* destination = output->data() +
|
||||
(slot * channels_ + channel) * kHybridBands + kHybridLow;
|
||||
if (slot < 6u) {
|
||||
const Complex* source =
|
||||
high_history_.data() + (slot * channels_ + channel) * 61u;
|
||||
for (int band = 0; band < 61; ++band) {
|
||||
destination[band] = source[band];
|
||||
}
|
||||
} else {
|
||||
const Complex* source =
|
||||
qmf.data() + ((slot - 6u) * channels_ + channel) * kQmfBands;
|
||||
for (int band = 3; band < kQmfBands; ++band) {
|
||||
destination[static_cast<std::size_t>(band - 3)] = source[band];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Every entry of the pending high-band history is written here, so it is a
|
||||
// reusable scratch buffer; the copy into the live history is kept as it was.
|
||||
if (next_high_history_.size() < 6u * channels_ * 61u) {
|
||||
next_high_history_.resize(6u * channels_ * 61u);
|
||||
}
|
||||
for (std::size_t entry = 0u; entry < 6u; ++entry) {
|
||||
const std::size_t combined = slots + entry;
|
||||
for (std::size_t channel = 0u; channel < channels_; ++channel) {
|
||||
Complex* destination =
|
||||
next_high_history_.data() + (entry * channels_ + channel) * 61u;
|
||||
if (combined < 6u) {
|
||||
const Complex* source =
|
||||
high_history_.data() + (combined * channels_ + channel) * 61u;
|
||||
for (int band = 0; band < 61; ++band) {
|
||||
destination[band] = source[band];
|
||||
}
|
||||
} else {
|
||||
const Complex* source =
|
||||
qmf.data() + ((combined - 6u) * channels_ + channel) * kQmfBands;
|
||||
for (int band = 3; band < kQmfBands; ++band) {
|
||||
destination[static_cast<std::size_t>(band - 3)] = source[band];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::copy(next_high_history_.begin(), next_high_history_.end(), high_history_.begin());
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t channels_;
|
||||
std::vector<double> low_kernel_; // [3][2][13][16][2]
|
||||
std::vector<double> low_by_term_; // [78][32], the same weights in the caller's term order
|
||||
std::vector<double> low_values_; // scratch, [block][78]
|
||||
std::vector<double> low_out_; // scratch, [block][32]
|
||||
std::vector<double> history_; // [12][channels][3][2]
|
||||
std::vector<Complex> high_history_; // [6][channels][61]
|
||||
std::vector<double> joined_; // scratch, [12 + slots][channels][3][2]
|
||||
std::vector<Complex> next_high_history_; // scratch, [6][channels][61]
|
||||
};
|
||||
|
||||
// Instantaneous sparse 77-hybrid to 64-QMF synthesis map.
|
||||
class HybridSynthesis {
|
||||
public:
|
||||
explicit HybridSynthesis(const Kernels& kernels) {
|
||||
const std::size_t rows = kernels.hybrid_indices.size() / 4u;
|
||||
mapping_.reserve(rows);
|
||||
for (std::size_t index = 0u; index < rows; ++index) {
|
||||
Entry entry;
|
||||
for (int field = 0; field < 4; ++field) {
|
||||
entry.index[static_cast<std::size_t>(field)] =
|
||||
kernels.hybrid_indices[index * 4u + static_cast<std::size_t>(field)];
|
||||
}
|
||||
entry.gain = kernels.hybrid_values[index];
|
||||
mapping_.push_back(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// hybrid: [slots, channels, 77]; output: [slots, channels, 64] complex.
|
||||
// The sparse map moves a real or imaginary part of one band into a real or
|
||||
// imaginary part of another, so the two components are accumulated apart.
|
||||
void process(const std::vector<Complex>& hybrid, std::size_t slots, std::size_t channels,
|
||||
std::vector<Complex>* output) const {
|
||||
const std::size_t rows = slots * channels;
|
||||
std::vector<double> real(rows * kQmfBands, 0.0);
|
||||
std::vector<double> imaginary(rows * kQmfBands, 0.0);
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels; ++channel) {
|
||||
const std::size_t row = slot * channels + channel;
|
||||
const Complex* source = hybrid.data() + row * kHybridBands;
|
||||
for (const Entry& entry : mapping_) {
|
||||
const double value = entry.index[1] == 0u ? source[entry.index[0]].real()
|
||||
: source[entry.index[0]].imag();
|
||||
if (value == 0.0) {
|
||||
continue;
|
||||
}
|
||||
double* destination =
|
||||
(entry.index[3] == 0u ? real.data() : imaginary.data()) + row * kQmfBands;
|
||||
destination[entry.index[2]] += value * entry.gain;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Every output element is assigned from the two accumulators below, so the
|
||||
// zero fill that `assign` performed was dead; only the size is needed.
|
||||
output->resize(rows * kQmfBands);
|
||||
for (std::size_t index = 0u; index < output->size(); ++index) {
|
||||
(*output)[index] = Complex(real[index], imaginary[index]);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
std::size_t index[4] = {0u, 0u, 0u, 0u};
|
||||
double gain = 0.0;
|
||||
};
|
||||
std::vector<Entry> mapping_;
|
||||
};
|
||||
|
||||
// Rank-4 64-band synthesis.
|
||||
class QmfSynthesis {
|
||||
public:
|
||||
QmfSynthesis(const Kernels& kernels, std::size_t channels)
|
||||
: channels_(channels), basis_(kernels.qmf_basis), taps_(kernels.qmf_taps) {
|
||||
history_.assign(9u * channels_ * kQmfBands * kSynthesisRank, 0.0);
|
||||
// The dispatched basis kernel reads the four ranks of one (band, tap) as
|
||||
// one vector, so the shipped [band][rank][tap] table is reordered once
|
||||
// here. The weights are the same doubles, only their order differs.
|
||||
const std::size_t bands = static_cast<std::size_t>(kQmfBands);
|
||||
const std::size_t ranks = static_cast<std::size_t>(kSynthesisRank);
|
||||
const std::size_t taps = dsp::kQmfFftSize;
|
||||
basis_by_tap_.resize(bands * taps * ranks);
|
||||
for (std::size_t band = 0u; band < bands; ++band) {
|
||||
for (std::size_t tap = 0u; tap < taps; ++tap) {
|
||||
for (std::size_t rank = 0u; rank < ranks; ++rank) {
|
||||
basis_by_tap_[(band * taps + tap) * ranks + rank] =
|
||||
basis_[(band * ranks + rank) * taps + tap];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reset() { std::fill(history_.begin(), history_.end(), 0.0); }
|
||||
|
||||
// qmf: [slots, channels, 64]; output: [slots*64, channels] real.
|
||||
void process(const std::vector<Complex>& qmf, std::size_t slots, std::vector<double>* output) {
|
||||
const std::size_t rows = slots * channels_;
|
||||
// [row][band][component] staging for the basis application. Both staging
|
||||
// planes and the joined window are reusable scratch: every element of each is
|
||||
// written before it is read, so the buffers are sized once and kept instead of
|
||||
// being allocated and zero-filled on every call.
|
||||
const std::size_t flat_size = rows * dsp::kQmfFftSize;
|
||||
if (flat_.size() < flat_size) {
|
||||
flat_.resize(flat_size);
|
||||
}
|
||||
for (std::size_t row = 0u; row < rows; ++row) {
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
flat_[row * dsp::kQmfFftSize + static_cast<std::size_t>(band) * 2u] =
|
||||
qmf[row * kQmfBands + static_cast<std::size_t>(band)].real();
|
||||
flat_[row * dsp::kQmfFftSize + static_cast<std::size_t>(band) * 2u + 1u] =
|
||||
qmf[row * kQmfBands + static_cast<std::size_t>(band)].imag();
|
||||
}
|
||||
}
|
||||
// The sums are written straight into the joined window: the destination index
|
||||
// is known up front, the summation order is untouched, and the application
|
||||
// itself is dispatched -- the four ranks of a band are four independent dot
|
||||
// products over the same 128 values, so they share a vector while every lane
|
||||
// keeps the tap order and the two roundings of `sum +=`.
|
||||
const std::size_t history_size = 9u * channels_ * kQmfBands * kSynthesisRank;
|
||||
const std::size_t joined_size = history_size + rows * kQmfBands * kSynthesisRank;
|
||||
if (joined_.size() < joined_size) {
|
||||
joined_.resize(joined_size);
|
||||
}
|
||||
std::copy(history_.begin(), history_.end(), joined_.begin());
|
||||
simd::qmf_synthesis_basis(flat_.data(), basis_by_tap_.data(),
|
||||
joined_.data() + history_size, rows);
|
||||
std::copy(joined_.begin() + static_cast<std::ptrdiff_t>(joined_size - history_size),
|
||||
joined_.begin() + static_cast<std::ptrdiff_t>(joined_size), history_.begin());
|
||||
|
||||
output->assign(rows * kQmfBands, 0.0);
|
||||
for (int lag = 0; lag < kSynthesisTaps; ++lag) {
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
const std::size_t source_slot = 9u - static_cast<std::size_t>(lag) + slot;
|
||||
for (std::size_t channel = 0u; channel < channels_; ++channel) {
|
||||
const double* source =
|
||||
joined_.data() +
|
||||
(source_slot * channels_ + channel) * kQmfBands * kSynthesisRank;
|
||||
double* destination =
|
||||
output->data() + (slot * channels_ + channel) * kQmfBands;
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
double sum = 0.0;
|
||||
for (int rank = 0; rank < kSynthesisRank; ++rank) {
|
||||
sum += source[static_cast<std::size_t>(band) * kSynthesisRank +
|
||||
static_cast<std::size_t>(rank)] *
|
||||
taps_[(static_cast<std::size_t>(band) * kSynthesisTaps +
|
||||
static_cast<std::size_t>(lag)) * kSynthesisRank +
|
||||
static_cast<std::size_t>(rank)];
|
||||
}
|
||||
destination[band] += sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t channels_;
|
||||
std::vector<double> basis_; // [64][4][128]
|
||||
std::vector<double> basis_by_tap_; // [64][128][4], the same weights transposed
|
||||
std::vector<double> taps_; // [64][10][4]
|
||||
std::vector<double> history_; // [9][channels][64][4]
|
||||
std::vector<double> flat_; // scratch, [rows][128], fully written per call
|
||||
std::vector<double> joined_; // scratch, [9 + slots][channels][64][4]
|
||||
};
|
||||
|
||||
// public_filterbank.PublicAnalysis77.process: [N, channels] -> [N/64, channels, 77].
|
||||
void analysis_77(const std::vector<double>& samples, std::size_t slots,
|
||||
std::size_t channels, QmfAnalysis& qmf,
|
||||
HybridAnalysis& hybrid_analysis, std::vector<Complex>* hybrid) {
|
||||
std::vector<double> hops(slots * channels * kQmfHop, 0.0);
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels; ++channel) {
|
||||
for (int index = 0; index < kQmfHop; ++index) {
|
||||
hops[(slot * channels + channel) * kQmfHop + static_cast<std::size_t>(index)] =
|
||||
samples[(slot * kQmfHop + static_cast<std::size_t>(index)) * channels + channel];
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<Complex> qmf_bands;
|
||||
qmf.process(hops, slots, &qmf_bands);
|
||||
hybrid_analysis.process(qmf_bands, slots, hybrid);
|
||||
}
|
||||
|
||||
// public_filterbank.PublicSynthesis77.process: [slots, channels, 77] -> [slots*64, channels].
|
||||
void synthesis_77(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::size_t channels, const HybridSynthesis& synthesis,
|
||||
QmfSynthesis& qmf, std::vector<double>* time) {
|
||||
std::vector<Complex> qmf_bands;
|
||||
synthesis.process(hybrid, slots, channels, &qmf_bands);
|
||||
std::vector<double> samples;
|
||||
qmf.process(qmf_bands, slots, &samples);
|
||||
// The reference transposes (slots, channels, 64) to sample-major output.
|
||||
time->assign(samples.size(), 0.0);
|
||||
for (std::size_t slot = 0u; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0u; channel < channels; ++channel) {
|
||||
for (int band = 0; band < kQmfBands; ++band) {
|
||||
(*time)[(slot * kQmfHop + static_cast<std::size_t>(band)) * channels + channel] =
|
||||
samples[(slot * channels + channel) * kQmfBands + static_cast<std::size_t>(band)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
struct PublicFilterbank::Impl {
|
||||
Impl(const Kernels& kernels, std::size_t channels)
|
||||
: channels(channels), qmf(kernels, channels), hybrid_analysis(kernels, channels),
|
||||
hybrid_synthesis(kernels), qmf_synthesis(kernels, channels) {}
|
||||
|
||||
std::size_t channels;
|
||||
QmfAnalysis qmf;
|
||||
HybridAnalysis hybrid_analysis;
|
||||
HybridSynthesis hybrid_synthesis;
|
||||
QmfSynthesis qmf_synthesis;
|
||||
};
|
||||
|
||||
PublicFilterbank::PublicFilterbank(const Kernels& kernels, std::size_t channels)
|
||||
: impl_(std::make_unique<Impl>(kernels, channels)) {}
|
||||
|
||||
PublicFilterbank::~PublicFilterbank() = default;
|
||||
|
||||
void PublicFilterbank::reset() {
|
||||
impl_->qmf.reset();
|
||||
impl_->hybrid_analysis.reset();
|
||||
impl_->qmf_synthesis.reset();
|
||||
}
|
||||
|
||||
void PublicFilterbank::analyze_full_rate(const std::vector<double>& samples, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) {
|
||||
analysis_77(samples, slots, impl_->channels, impl_->qmf, impl_->hybrid_analysis, hybrid);
|
||||
}
|
||||
|
||||
void PublicFilterbank::synthesize_full_rate(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<double>* time) {
|
||||
synthesis_77(hybrid, slots, impl_->channels, impl_->hybrid_synthesis, impl_->qmf_synthesis,
|
||||
time);
|
||||
}
|
||||
|
||||
void PublicFilterbank::analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf) {
|
||||
impl_->qmf.process(hops, slots, qmf);
|
||||
}
|
||||
|
||||
void PublicFilterbank::analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) {
|
||||
impl_->hybrid_analysis.process(qmf, slots, hybrid);
|
||||
}
|
||||
|
||||
void PublicFilterbank::synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf) {
|
||||
impl_->hybrid_synthesis.process(hybrid, slots, impl_->channels, qmf);
|
||||
}
|
||||
|
||||
void PublicFilterbank::synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* time) {
|
||||
impl_->qmf_synthesis.process(qmf, slots, time);
|
||||
}
|
||||
|
||||
} // namespace joc::hrtf
|
||||
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "hrtf/jochrtf.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
inline constexpr int kQmfBands = 64;
|
||||
inline constexpr int kQmfHop = 64;
|
||||
inline constexpr int kHybridLow = 16;
|
||||
inline constexpr int kHybridBandCount = 77;
|
||||
inline constexpr int kLatencySamples = 961;
|
||||
|
||||
using Complex = std::complex<double>;
|
||||
|
||||
class PublicFilterbank {
|
||||
public:
|
||||
PublicFilterbank(const Kernels& kernels, std::size_t channels);
|
||||
~PublicFilterbank();
|
||||
PublicFilterbank(const PublicFilterbank&) = delete;
|
||||
PublicFilterbank& operator=(const PublicFilterbank&) = delete;
|
||||
|
||||
void reset();
|
||||
|
||||
// Full-rate [slots*64, channels] -> hybrid [slots, channels, 77].
|
||||
void analyze_full_rate(const std::vector<double>& samples, std::size_t slots,
|
||||
std::vector<Complex>* hybrid);
|
||||
// Hybrid [slots, channels, 77] -> full-rate [slots*64, channels].
|
||||
void synthesize_full_rate(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<double>* time);
|
||||
|
||||
// The stateful half-steps, in the order the reference runs them.
|
||||
void analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf);
|
||||
void analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid);
|
||||
void synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf);
|
||||
void synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* time);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,89 +0,0 @@
|
||||
#include "hrtf/rosella_bank.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "hrtf/rosella_filterbank.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
namespace {
|
||||
|
||||
// The shared public bank. It is also what the SOFA field compiler runs, and it
|
||||
// is the bank the Rosella renderer used before the Rosella-specific one was
|
||||
// captured.
|
||||
class PublicBank final : public RosellaBank {
|
||||
public:
|
||||
explicit PublicBank(std::size_t channels) : bank_(builtin_kernels(), channels) {}
|
||||
|
||||
void reset() override { bank_.reset(); }
|
||||
|
||||
void analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf) override {
|
||||
bank_.analyze_qmf(hops, slots, qmf);
|
||||
}
|
||||
|
||||
void analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) override {
|
||||
bank_.analyze_hybrid(qmf, slots, hybrid);
|
||||
}
|
||||
|
||||
void synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf) override {
|
||||
bank_.synthesize_hybrid(hybrid, slots, qmf);
|
||||
}
|
||||
|
||||
void synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* hops) override {
|
||||
bank_.synthesize_qmf(qmf, slots, hops);
|
||||
}
|
||||
|
||||
private:
|
||||
PublicFilterbank bank_;
|
||||
};
|
||||
|
||||
// The bank captured from the Rosella headphone renderer.
|
||||
class CapturedBank final : public RosellaBank {
|
||||
public:
|
||||
explicit CapturedBank(std::size_t channels) : bank_(channels) {}
|
||||
|
||||
void reset() override { bank_.reset(); }
|
||||
|
||||
void analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf) override {
|
||||
bank_.analyze_qmf(hops, slots, qmf);
|
||||
}
|
||||
|
||||
void analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) override {
|
||||
bank_.analyze_hybrid(qmf, slots, hybrid);
|
||||
}
|
||||
|
||||
void synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf) override {
|
||||
bank_.synthesize_hybrid(hybrid, slots, qmf);
|
||||
}
|
||||
|
||||
void synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* hops) override {
|
||||
bank_.synthesize_qmf(qmf, slots, hops);
|
||||
}
|
||||
|
||||
private:
|
||||
RosellaFilterbank bank_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::uint32_t rosella_bank_latency(RosellaBankKind kind) {
|
||||
return kind == RosellaBankKind::Captured
|
||||
? static_cast<std::uint32_t>(RosellaFilterbank::kLatencySamples)
|
||||
: static_cast<std::uint32_t>(kLatencySamples);
|
||||
}
|
||||
|
||||
std::unique_ptr<RosellaBank> RosellaBank::create(RosellaBankKind kind, std::size_t channels) {
|
||||
if (kind == RosellaBankKind::Captured) {
|
||||
return std::make_unique<CapturedBank>(channels);
|
||||
}
|
||||
return std::make_unique<PublicBank>(channels);
|
||||
}
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,50 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "hrtf/public_filterbank.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
// Which 64-QMF / 77-hybrid bank the Rosella renderer runs. The two are not
|
||||
// interchangeable: the QMF prototype, the hybrid tap count, the high-band slot
|
||||
// delay and the analysis/synthesis latency all differ, so each bank keeps its own
|
||||
// tables and its own state.
|
||||
enum class RosellaBankKind : std::int32_t {
|
||||
Public = 1, // shared bank: 13-tap hybrid, 6-slot delay, 961 samples
|
||||
Captured = 2, // Rosella bank: 11-tap hybrid, 3-slot delay, 448 samples
|
||||
};
|
||||
|
||||
// Analysis/synthesis latency of `kind` at 48 kHz, in samples.
|
||||
std::uint32_t rosella_bank_latency(RosellaBankKind kind);
|
||||
|
||||
// The four stateful half-steps of either bank, in the order the reference runs
|
||||
// them. Hops and QMF data are [slots, channels, 64], hybrid data is
|
||||
// [slots, channels, 77]. State is per instance, so the analysis bank of a
|
||||
// runtime and its synthesis bank never share buffers.
|
||||
class RosellaBank {
|
||||
public:
|
||||
// `kind` must name one of the enumerators; anything else selects Public.
|
||||
static std::unique_ptr<RosellaBank> create(RosellaBankKind kind, std::size_t channels);
|
||||
|
||||
RosellaBank() = default;
|
||||
RosellaBank(const RosellaBank&) = delete;
|
||||
RosellaBank& operator=(const RosellaBank&) = delete;
|
||||
virtual ~RosellaBank() = default;
|
||||
|
||||
virtual void reset() = 0;
|
||||
|
||||
virtual void analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf) = 0;
|
||||
virtual void analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) = 0;
|
||||
virtual void synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf) = 0;
|
||||
virtual void synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* hops) = 0;
|
||||
};
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,173 +0,0 @@
|
||||
#include "hrtf/rosella_filterbank.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
#include "foundation/fft.h"
|
||||
#include "hrtf/rosella_filterbank_tables.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t kBands = 64;
|
||||
constexpr std::size_t kQmfTaps = 10;
|
||||
constexpr std::size_t kHybridTaps = 11;
|
||||
constexpr std::size_t kHybridDelay = 3;
|
||||
constexpr std::array<std::size_t, 16> kHybridOrder = {0, 7, 1, 6, 2, 9, 3, 4,
|
||||
10, 5, 11, 15, 8, 12, 13, 14};
|
||||
|
||||
const dsp::FftPlan& qmf_plan() {
|
||||
static const dsp::FftPlan plan(kBands, false);
|
||||
return plan;
|
||||
}
|
||||
|
||||
const dsp::FftPlan& hybrid_plan(std::size_t bands) {
|
||||
static const dsp::FftPlan eight(8, false);
|
||||
static const dsp::FftPlan four(4, false);
|
||||
return bands == 8 ? eight : four;
|
||||
}
|
||||
|
||||
double tap_sign(std::size_t tap) { return (tap / 2) % 2 == 0 ? 1.0 : -1.0; }
|
||||
|
||||
}
|
||||
|
||||
RosellaFilterbank::RosellaFilterbank(std::size_t channels)
|
||||
: channels_(channels), analysis_history_(kQmfTaps * channels * kBands),
|
||||
hybrid_history_(kHybridTaps * channels * kBands),
|
||||
synthesis_history_(kQmfTaps * channels * kBands), spectrum_(kBands), hybrid_eight_(8),
|
||||
hybrid_four_(4) {}
|
||||
|
||||
void RosellaFilterbank::reset() {
|
||||
analysis_position_ = 0;
|
||||
hybrid_position_ = 0;
|
||||
synthesis_position_ = 0;
|
||||
std::fill(analysis_history_.begin(), analysis_history_.end(), 0.0);
|
||||
std::fill(hybrid_history_.begin(), hybrid_history_.end(), Complex{});
|
||||
std::fill(synthesis_history_.begin(), synthesis_history_.end(), Complex{});
|
||||
}
|
||||
|
||||
void RosellaFilterbank::analyze_qmf(const std::vector<double>& hops, std::size_t slots,
|
||||
std::vector<Complex>* qmf) {
|
||||
const std::size_t stride = channels_ * kBands;
|
||||
qmf->resize(slots * stride);
|
||||
for (std::size_t slot = 0; slot < slots; ++slot) {
|
||||
std::copy_n(hops.data() + slot * stride, stride,
|
||||
analysis_history_.data() + analysis_position_ * stride);
|
||||
for (std::size_t channel = 0; channel < channels_; ++channel) {
|
||||
for (std::size_t band = 0; band < kBands; ++band) {
|
||||
double even = 0.0;
|
||||
double odd = 0.0;
|
||||
for (std::size_t tap = 0; tap < kQmfTaps; ++tap) {
|
||||
const std::size_t history_slot = (analysis_position_ + tap + 1) % kQmfTaps;
|
||||
const double value =
|
||||
analysis_history_[history_slot * stride + channel * kBands + band];
|
||||
const double contribution =
|
||||
value * 0.5 * rosella_filterbank_tables::kPrototype[band * kQmfTaps + tap] *
|
||||
tap_sign(tap);
|
||||
(tap % 2 == 0 ? even : odd) += contribution;
|
||||
}
|
||||
spectrum_[band] = Complex(odd, even) * rosella_filterbank_tables::kRotation[band];
|
||||
}
|
||||
dsp::fft_radix2(&spectrum_, qmf_plan());
|
||||
Complex* destination = qmf->data() + slot * stride + channel * kBands;
|
||||
for (std::size_t band = 0; band < kBands / 2; ++band) {
|
||||
destination[2 * band] = spectrum_[band] / 64.0;
|
||||
destination[2 * band + 1] = std::conj(spectrum_[63 - band]) / 64.0;
|
||||
}
|
||||
}
|
||||
analysis_position_ = (analysis_position_ + 1) % kQmfTaps;
|
||||
}
|
||||
}
|
||||
|
||||
void RosellaFilterbank::analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid) {
|
||||
const std::size_t stride = channels_ * kBands;
|
||||
hybrid->resize(slots * channels_ * kHybridBandCount);
|
||||
for (std::size_t slot = 0; slot < slots; ++slot) {
|
||||
std::copy_n(qmf.data() + slot * stride, stride,
|
||||
hybrid_history_.data() + hybrid_position_ * stride);
|
||||
for (std::size_t channel = 0; channel < channels_; ++channel) {
|
||||
std::array<Complex, 16> low{};
|
||||
for (std::size_t band = 0; band < 3; ++band) {
|
||||
std::vector<Complex>& spectrum = band == 0 ? hybrid_eight_ : hybrid_four_;
|
||||
std::fill(spectrum.begin(), spectrum.end(), Complex{});
|
||||
for (std::size_t tap = 0; tap < kHybridTaps; ++tap) {
|
||||
const std::size_t history_slot = (hybrid_position_ + tap + 1) % kHybridTaps;
|
||||
const std::size_t bin =
|
||||
(tap + kHybridDelay + 1 + 2 * spectrum.size() - kHybridTaps) %
|
||||
spectrum.size();
|
||||
spectrum[bin] +=
|
||||
hybrid_history_[history_slot * stride + channel * kBands + band] *
|
||||
rosella_filterbank_tables::kHybrid[(band == 0 ? 0 : kHybridTaps) + tap];
|
||||
}
|
||||
dsp::fft_radix2(&spectrum, hybrid_plan(spectrum.size()));
|
||||
std::copy(spectrum.begin(), spectrum.end(),
|
||||
low.begin() + (band == 0 ? 0 : 4 + band * 4));
|
||||
}
|
||||
Complex* destination = hybrid->data() + (slot * channels_ + channel) * kHybridBandCount;
|
||||
for (std::size_t band = 0; band < low.size(); ++band) {
|
||||
destination[band] = low[kHybridOrder[band]];
|
||||
}
|
||||
destination[1] = std::conj(destination[1]);
|
||||
destination[3] = std::conj(destination[3]);
|
||||
const std::size_t delayed =
|
||||
(hybrid_position_ + kHybridTaps - kHybridDelay) % kHybridTaps;
|
||||
std::copy_n(hybrid_history_.data() + delayed * stride + channel * kBands + 3,
|
||||
kBands - 3, destination + 16);
|
||||
}
|
||||
hybrid_position_ = (hybrid_position_ + 1) % kHybridTaps;
|
||||
}
|
||||
}
|
||||
|
||||
void RosellaFilterbank::synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf) {
|
||||
qmf->resize(slots * channels_ * kBands);
|
||||
for (std::size_t row = 0; row < slots * channels_; ++row) {
|
||||
const Complex* source = hybrid.data() + row * kHybridBandCount;
|
||||
Complex* destination = qmf->data() + row * kBands;
|
||||
destination[0] = source[0] + source[2] + source[4] + source[6] + source[7] + source[9] +
|
||||
std::conj(source[3]) + std::conj(source[1]);
|
||||
destination[1] = source[12] + source[5] + source[8] + source[10];
|
||||
destination[2] = source[13] + source[14] + source[15] + source[11];
|
||||
std::copy_n(source + 16, kBands - 3, destination + 3);
|
||||
}
|
||||
}
|
||||
|
||||
void RosellaFilterbank::synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* hops) {
|
||||
const std::size_t stride = channels_ * kBands;
|
||||
hops->resize(slots * stride);
|
||||
for (std::size_t slot = 0; slot < slots; ++slot) {
|
||||
for (std::size_t channel = 0; channel < channels_; ++channel) {
|
||||
const Complex* source = qmf.data() + slot * stride + channel * kBands;
|
||||
for (std::size_t band = 0; band < kBands / 2; ++band) {
|
||||
spectrum_[band] = std::conj(source[2 * band]);
|
||||
spectrum_[32 + band] = source[63 - 2 * band];
|
||||
}
|
||||
dsp::fft_radix2(&spectrum_, qmf_plan());
|
||||
Complex* current =
|
||||
synthesis_history_.data() + synthesis_position_ * stride + channel * kBands;
|
||||
for (std::size_t band = 0; band < kBands; ++band) {
|
||||
current[band] = spectrum_[band] * rosella_filterbank_tables::kRotation[band];
|
||||
}
|
||||
for (std::size_t band = 0; band < kBands; ++band) {
|
||||
double value = 0.0;
|
||||
for (std::size_t lag = 0; lag < kQmfTaps; ++lag) {
|
||||
const std::size_t history_slot =
|
||||
(synthesis_position_ + kQmfTaps - lag) % kQmfTaps;
|
||||
const Complex sample =
|
||||
synthesis_history_[history_slot * stride + channel * kBands + band];
|
||||
const double component = lag % 2 == 0 ? sample.real() : sample.imag();
|
||||
value +=
|
||||
component *
|
||||
rosella_filterbank_tables::kPrototype[(63 - band) * kQmfTaps + 9 - lag] *
|
||||
tap_sign(lag) * 2.0;
|
||||
}
|
||||
(*hops)[slot * stride + channel * kBands + band] = value;
|
||||
}
|
||||
}
|
||||
synthesis_position_ = (synthesis_position_ + 1) % kQmfTaps;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "hrtf/public_filterbank.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
class RosellaFilterbank {
|
||||
public:
|
||||
static constexpr int kLatencySamples = 448;
|
||||
|
||||
explicit RosellaFilterbank(std::size_t channels);
|
||||
|
||||
void reset();
|
||||
void analyze_qmf(const std::vector<double>& hops, std::size_t slots, std::vector<Complex>* qmf);
|
||||
void analyze_hybrid(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<Complex>* hybrid);
|
||||
void synthesize_hybrid(const std::vector<Complex>& hybrid, std::size_t slots,
|
||||
std::vector<Complex>* qmf);
|
||||
void synthesize_qmf(const std::vector<Complex>& qmf, std::size_t slots,
|
||||
std::vector<double>* hops);
|
||||
|
||||
private:
|
||||
std::size_t channels_;
|
||||
std::size_t analysis_position_ = 0;
|
||||
std::size_t hybrid_position_ = 0;
|
||||
std::size_t synthesis_position_ = 0;
|
||||
std::vector<double> analysis_history_;
|
||||
std::vector<Complex> hybrid_history_;
|
||||
std::vector<Complex> synthesis_history_;
|
||||
std::vector<Complex> spectrum_;
|
||||
std::vector<Complex> hybrid_eight_;
|
||||
std::vector<Complex> hybrid_four_;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,317 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <complex>
|
||||
|
||||
namespace joc::hrtf::rosella_filterbank_tables {
|
||||
|
||||
inline constexpr std::array<double, 640> kPrototype = {
|
||||
0x1.89b0a40000000p-14f, -0x1.9405b80000000p-11f, 0x1.caf19e0000000p-11f,
|
||||
0x1.b88a900000000p-8f, 0x1.a3899a0000000p-8f, -0x1.fa22860000000p-4f,
|
||||
0x1.5a540e0000000p-4f, 0x1.c420360000000p-1f, 0x1.1fb7200000000p-1f,
|
||||
-0x1.eb519a0000000p-12f, 0x1.fe69400000000p-14f, -0x1.aebea80000000p-11f,
|
||||
0x1.0b0c7c0000000p-10f, 0x1.baec6c0000000p-8f, 0x1.7fc1900000000p-8f,
|
||||
-0x1.0209c80000000p-3f, 0x1.88474c0000000p-4f, 0x1.c7781c0000000p-1f,
|
||||
0x1.1929680000000p-1f, -0x1.6f0b9e0000000p-9f, 0x1.0c4e040000000p-13f,
|
||||
-0x1.cabdf80000000p-11f, 0x1.3039ee0000000p-10f, 0x1.bd5e600000000p-8f,
|
||||
0x1.590ba60000000p-8f, -0x1.06dd700000000p-3f, 0x1.b728e40000000p-4f,
|
||||
0x1.ca9d100000000p-1f, 0x1.1298c60000000p-1f, -0x1.430d400000000p-8f,
|
||||
0x1.09af720000000p-13f, -0x1.e7140c0000000p-11f, 0x1.553e000000000p-10f,
|
||||
0x1.bfe6ee0000000p-8f, 0x1.2f581e0000000p-8f, -0x1.0b89600000000p-3f,
|
||||
0x1.e6f2b20000000p-4f, 0x1.cd8f8e0000000p-1f, 0x1.0c066e0000000p-1f,
|
||||
-0x1.c5fd4e0000000p-8f, 0x1.01e0d60000000p-13f, -0x1.01e8060000000p-10f,
|
||||
0x1.7a2d9e0000000p-10f, 0x1.c2890c0000000p-8f, 0x1.028f2c0000000p-8f,
|
||||
-0x1.100a6e0000000p-3f, 0x1.0bcf280000000p-3f, 0x1.d04eb00000000p-1f,
|
||||
0x1.0573b40000000p-1f, -0x1.1fe42e0000000p-7f, 0x1.e4c9900000000p-14f,
|
||||
-0x1.1083480000000p-10f, 0x1.9ef9b00000000p-10f, 0x1.c548880000000p-8f,
|
||||
0x1.a536b40000000p-9f, -0x1.145d660000000p-3f, 0x1.2492a60000000p-3f,
|
||||
0x1.d2d9d00000000p-1f, 0x1.fdc3f40000000p-2f, -0x1.584fbc0000000p-7f,
|
||||
0x1.bbe1f00000000p-14f, -0x1.1f4cc40000000p-10f, 0x1.c3a28c0000000p-10f,
|
||||
0x1.c828fe0000000p-8f, 0x1.3ed0ae0000000p-9f, -0x1.187f1e0000000p-3f,
|
||||
0x1.3dc0340000000p-3f, 0x1.d530800000000p-1f, 0x1.f0a4d80000000p-2f,
|
||||
-0x1.8c9d380000000p-7f, 0x1.8b83740000000p-14f, -0x1.2e42340000000p-10f,
|
||||
0x1.e827840000000p-10f, 0x1.cb2f1a0000000p-8f, 0x1.a38bba0000000p-10f,
|
||||
-0x1.1c6c620000000p-3f, 0x1.5754320000000p-3f, 0x1.d7522c0000000p-1f,
|
||||
0x1.e38cc40000000p-2f, -0x1.bccca00000000p-7f, 0x1.51b3dc0000000p-14f,
|
||||
-0x1.3d59c40000000p-10f, 0x1.0642740000000p-9f, 0x1.ce5f720000000p-8f,
|
||||
0x1.77c5740000000p-11f, -0x1.2021ea0000000p-3f, 0x1.714ae20000000p-3f,
|
||||
0x1.d93e620000000p-1f, 0x1.d67e3e0000000p-2f, -0x1.e902340000000p-7f,
|
||||
0x1.1135380000000p-14f, -0x1.4c72680000000p-10f, 0x1.184f0e0000000p-9f,
|
||||
0x1.d1bcf80000000p-8f, -0x1.cd43660000000p-13f, -0x1.239c8a0000000p-3f,
|
||||
0x1.8ba0220000000p-3f, 0x1.daf4c80000000p-1f, 0x1.c97bac0000000p-2f,
|
||||
-0x1.08b5360000000p-6f, 0x1.9380400000000p-15f, -0x1.5b82640000000p-10f,
|
||||
0x1.2a36bc0000000p-9f, 0x1.d54b040000000p-8f, -0x1.3d508c0000000p-10f,
|
||||
-0x1.26d9020000000p-3f, 0x1.a64ff60000000p-3f, 0x1.dc75060000000p-1f,
|
||||
0x1.bc87920000000p-2f, -0x1.1b117c0000000p-6f, 0x1.ef2dd20000000p-16f,
|
||||
-0x1.6a7bb60000000p-10f, 0x1.3bf6ea0000000p-9f, 0x1.d90d680000000p-8f,
|
||||
-0x1.27adc40000000p-9f, -0x1.29d4040000000p-3f, 0x1.c156200000000p-3f,
|
||||
0x1.ddbed80000000p-1f, 0x1.afa4460000000p-2f, -0x1.2ba7240000000p-6f,
|
||||
0x1.4b90ac0000000p-17f, -0x1.7946720000000p-10f, 0x1.4d8e5a0000000p-9f,
|
||||
0x1.dd07fe0000000p-8f, -0x1.b800720000000p-9f, -0x1.2c8a500000000p-3f,
|
||||
0x1.dcae220000000p-3f, 0x1.ded2060000000p-1f, 0x1.a2d44a0000000p-2f,
|
||||
-0x1.3a80720000000p-6f, -0x1.700b940000000p-17f, -0x1.87d4460000000p-10f,
|
||||
0x1.5ef8ce0000000p-9f, 0x1.e13c820000000p-8f, -0x1.27dccc0000000p-8f,
|
||||
-0x1.2ef8a80000000p-3f, 0x1.f853940000000p-3f, 0x1.dfae620000000p-1f,
|
||||
0x1.9619e20000000p-2f, -0x1.47ad4c0000000p-6f, -0x1.1120780000000p-15f,
|
||||
-0x1.960c8c0000000p-10f, 0x1.7033800000000p-9f, 0x1.e5adfa0000000p-8f,
|
||||
-0x1.7778760000000p-8f, -0x1.311bc00000000p-3f, 0x1.0a20dc0000000p-2f,
|
||||
0x1.e053dc0000000p-1f, 0x1.8977600000000p-2f, -0x1.533fe20000000p-6f,
|
||||
-0x1.ca9a980000000p-15f, -0x1.a3dfa80000000p-10f, 0x1.813d240000000p-9f,
|
||||
0x1.ea5d320000000p-8f, -0x1.cade8a0000000p-8f, -0x1.32f0680000000p-3f,
|
||||
0x1.1839ec0000000p-2f, 0x1.e0c25e0000000p-1f, 0x1.7cef0e0000000p-2f,
|
||||
-0x1.5d41a40000000p-6f, -0x1.45160a0000000p-14f, -0x1.b13e1c0000000p-10f,
|
||||
0x1.9211160000000p-9f, 0x1.ef4d0a0000000p-8f, -0x1.110bcc0000000p-7f,
|
||||
-0x1.3473540000000p-3f, 0x1.2672860000000p-2f, 0x1.e0f9ee0000000p-1f,
|
||||
0x1.7083220000000p-2f, -0x1.65bf6a0000000p-6f, -0x1.a5d63c0000000p-14f,
|
||||
-0x1.be0c7c0000000p-10f, 0x1.a2ad420000000p-9f, 0x1.f47de80000000p-8f,
|
||||
-0x1.3e95720000000p-7f, -0x1.35a15c0000000p-3f, 0x1.34c8260000000p-2f,
|
||||
0x1.e0faa00000000p-1f, 0x1.6435c80000000p-2f, -0x1.6cc7d80000000p-6f,
|
||||
-0x1.0363300000000p-13f, -0x1.ca38de0000000p-10f, 0x1.b30fc40000000p-9f,
|
||||
0x1.f9f0320000000p-8f, -0x1.6e0f4e0000000p-7f, -0x1.3677520000000p-3f,
|
||||
0x1.43383c0000000p-2f, 0x1.e0c4920000000p-1f, 0x1.58091e0000000p-2f,
|
||||
-0x1.7268700000000p-6f, -0x1.33a4780000000p-13f, -0x1.d5b2c20000000p-10f,
|
||||
0x1.c336cc0000000p-9f, 0x1.ffa3600000000p-8f, -0x1.9f7bd20000000p-7f,
|
||||
-0x1.36f2140000000p-3f, 0x1.51c02e0000000p-2f, 0x1.e057f20000000p-1f,
|
||||
0x1.4bff340000000p-2f, -0x1.76ae0c0000000p-6f, -0x1.63044a0000000p-13f,
|
||||
-0x1.e062560000000p-10f, 0x1.d321240000000p-9f, 0x1.02cb720000000p-7f,
|
||||
-0x1.d2dcc20000000p-7f, -0x1.370e8a0000000p-3f, 0x1.605d580000000p-2f,
|
||||
0x1.dfb5020000000p-1f, 0x1.401a140000000p-2f, -0x1.79a3480000000p-6f,
|
||||
-0x1.91dea40000000p-13f, -0x1.ea3a6c0000000p-10f, 0x1.e2cc1c0000000p-9f,
|
||||
0x1.05e49c0000000p-7f, -0x1.04190e0000000p-6f, -0x1.36c9a00000000p-3f,
|
||||
0x1.6f0cfe0000000p-2f, 0x1.dedc080000000p-1f, 0x1.345b9e0000000p-2f,
|
||||
-0x1.7b56a00000000p-6f, -0x1.bfa7220000000p-13f, -0x1.f327920000000p-10f,
|
||||
0x1.f234380000000p-9f, 0x1.091c860000000p-7f, -0x1.1fbe040000000p-6f,
|
||||
-0x1.36205c0000000p-3f, 0x1.7dcc500000000p-2f, 0x1.ddcd600000000p-1f,
|
||||
0x1.28c5be0000000p-2f, -0x1.7bd6c00000000p-6f, -0x1.ebbcb00000000p-13f,
|
||||
-0x1.fb17ea0000000p-10f, 0x1.00ad9a0000000p-8f, 0x1.0c71000000000p-7f,
|
||||
-0x1.3c5cfa0000000p-6f, -0x1.350fba0000000p-3f, 0x1.8c989a0000000p-2f,
|
||||
0x1.dc89760000000p-1f, 0x1.1d5a3e0000000p-2f, -0x1.7b2ea80000000p-6f,
|
||||
-0x1.0afed40000000p-12f, -0x1.00fdf80000000p-9f, 0x1.081ef40000000p-8f,
|
||||
0x1.0fe0aa0000000p-7f, -0x1.59f4a60000000p-6f, -0x1.3394de0000000p-3f,
|
||||
0x1.9b6efe0000000p-2f, 0x1.db10b80000000p-1f, 0x1.121ad80000000p-2f,
|
||||
-0x1.796b8c0000000p-6f, -0x1.1f023c0000000p-12f, -0x1.03dff00000000p-9f,
|
||||
0x1.0f6d1a0000000p-8f, 0x1.1368de0000000p-7f, -0x1.7883900000000p-6f,
|
||||
-0x1.31acee0000000p-3f, 0x1.aa4c940000000p-2f, 0x1.d963ae0000000p-1f,
|
||||
0x1.07092e0000000p-2f, -0x1.769b420000000p-6f, -0x1.31d35c0000000p-12f,
|
||||
-0x1.062c000000000p-9f, 0x1.1697660000000p-8f, 0x1.1706fa0000000p-7f,
|
||||
-0x1.9807dc0000000p-6f, -0x1.2f55300000000p-3f, 0x1.b92e760000000p-2f,
|
||||
0x1.d782e20000000p-1f, 0x1.f84daa0000000p-3f, -0x1.72cc440000000p-6f,
|
||||
-0x1.435c0e0000000p-12f, -0x1.07dcd20000000p-9f, 0x1.1d9da20000000p-8f,
|
||||
0x1.1ab8380000000p-7f, -0x1.b87f060000000p-6f, -0x1.2c8af40000000p-3f,
|
||||
0x1.c811b40000000p-2f, 0x1.d56ef40000000p-1f, 0x1.e2ea940000000p-3f,
|
||||
-0x1.6e0bd80000000p-6f, -0x1.5382940000000p-12f, -0x1.08eac40000000p-9f,
|
||||
0x1.247f400000000p-8f, 0x1.1e79ce0000000p-7f, -0x1.d9e6460000000p-6f,
|
||||
-0x1.294b8c0000000p-3f, 0x1.d6f34e0000000p-2f, 0x1.d3288c0000000p-1f,
|
||||
0x1.cdebe00000000p-3f, -0x1.6865f20000000p-6f, -0x1.624b420000000p-12f,
|
||||
-0x1.0952b80000000p-9f, 0x1.2b3bdc0000000p-8f, 0x1.2247060000000p-7f,
|
||||
-0x1.fc3a1a0000000p-6f, -0x1.2594900000000p-3f, 0x1.e5d0500000000p-2f,
|
||||
0x1.d0b05e0000000p-1f, 0x1.b9542e0000000p-3f, -0x1.61ea740000000p-6f,
|
||||
-0x1.6f8fc20000000p-12f, -0x1.090d6a0000000p-9f, 0x1.31d25e0000000p-8f,
|
||||
0x1.261bfc0000000p-7f, -0x1.0fbb460000000p-5f, -0x1.21637c0000000p-3f,
|
||||
0x1.f4a5aa0000000p-2f, 0x1.ce072e0000000p-1f, 0x1.a525de0000000p-3f,
|
||||
-0x1.5aa7b40000000p-6f, -0x1.7b53da0000000p-12f, -0x1.0819600000000p-9f,
|
||||
0x1.3842c00000000p-8f, 0x1.29f3580000000p-7f, -0x1.21cbac0000000p-5f,
|
||||
-0x1.1cb60a0000000p-3f, 0x1.01b82e0000000p-1f, 0x1.cb2dca0000000p-1f,
|
||||
0x1.9163520000000p-3f, -0x1.52aada0000000p-6f, -0x1.85a5dc0000000p-12f,
|
||||
-0x1.0671aa0000000p-9f, 0x1.3e8b260000000p-8f, 0x1.2dc77c0000000p-7f,
|
||||
-0x1.344b9e0000000p-5f, -0x1.1789fc0000000p-3f, 0x1.0916a80000000p-1f,
|
||||
0x1.c824fc0000000p-1f, 0x1.7e0e860000000p-3f, -0x1.4a023a0000000p-6f,
|
||||
-0x1.8e754e0000000p-12f, -0x1.0411fc0000000p-9f, 0x1.44aa780000000p-8f,
|
||||
0x1.3192620000000p-7f, -0x1.4738520000000p-5f, -0x1.11dd300000000p-3f,
|
||||
0x1.106cbc0000000p-1f, 0x1.c4edac0000000p-1f, 0x1.6b29640000000p-3f,
|
||||
-0x1.40bbf00000000p-6f, -0x1.95db9a0000000p-12f, -0x1.00fbba0000000p-9f,
|
||||
0x1.4aa1d60000000p-8f, 0x1.354e160000000p-7f, -0x1.5a8eb80000000p-5f,
|
||||
-0x1.0bad980000000p-3f, 0x1.17b8dc0000000p-1f, 0x1.c188c20000000p-1f,
|
||||
0x1.58b59e0000000p-3f, -0x1.36e7a40000000p-6f, -0x1.9be6720000000p-12f,
|
||||
-0x1.fa5a8a0000000p-10f, 0x1.506fe80000000p-8f, 0x1.38f3740000000p-7f,
|
||||
-0x1.6e4b4c0000000p-5f, -0x1.04f9500000000p-3f, 0x1.1ef9820000000p-1f,
|
||||
0x1.bdf7340000000p-1f, 0x1.46b4b40000000p-3f, -0x1.2c935c0000000p-6f,
|
||||
-0x1.a0ae100000000p-12f, -0x1.f14b760000000p-10f, 0x1.5614b40000000p-8f,
|
||||
0x1.3c7bfe0000000p-7f, -0x1.826b260000000p-5f, -0x1.fb7d1c0000000p-4f,
|
||||
0x1.262d200000000p-1f, 0x1.ba3a080000000p-1f, 0x1.3527ea0000000p-3f,
|
||||
-0x1.21cb9e0000000p-6f, -0x1.a45ab20000000p-12f, -0x1.e6cbec0000000p-10f,
|
||||
0x1.5b8f3c0000000p-8f, 0x1.3fdefe0000000p-7f, -0x1.96ea060000000p-5f,
|
||||
-0x1.ebf76a0000000p-4f, 0x1.2d52300000000p-1f, 0x1.b652420000000p-1f,
|
||||
0x1.24103e0000000p-3f, -0x1.16a0ec0000000p-6f, -0x1.a6fa860000000p-12f,
|
||||
-0x1.dadcf00000000p-10f, 0x1.60df2e0000000p-8f, 0x1.4315740000000p-7f,
|
||||
-0x1.abc3c80000000p-5f, -0x1.db5e1e0000000p-4f, 0x1.3467220000000p-1f,
|
||||
0x1.b240fa0000000p-1f, 0x1.136ebc0000000p-3f, -0x1.0b217a0000000p-6f,
|
||||
-0x1.a8bb460000000p-12f, -0x1.cd821e0000000p-10f, 0x1.6605500000000p-8f,
|
||||
0x1.4617c60000000p-7f, -0x1.c0f3f60000000p-5f, -0x1.c9aeee0000000p-4f,
|
||||
0x1.3b6a700000000p-1f, 0x1.ae07440000000p-1f, 0x1.03441c0000000p-3f,
|
||||
-0x1.feb4ba0000000p-7f, -0x1.a9e0be0000000p-12f, -0x1.bebef20000000p-10f,
|
||||
0x1.6b004c0000000p-8f, 0x1.48dc4a0000000p-7f, -0x1.d675c20000000p-5f,
|
||||
-0x1.b6e6ae0000000p-4f, 0x1.425a960000000p-1f, 0x1.a9a64c0000000p-1f,
|
||||
0x1.e7217e0000000p-4f, -0x1.e6b55c0000000p-7f, -0x1.aa85020000000p-12f,
|
||||
-0x1.ae9b460000000p-10f, 0x1.6fd00c0000000p-8f, 0x1.4b59340000000p-7f,
|
||||
-0x1.ec43c80000000p-5f, -0x1.a303a20000000p-4f, 0x1.49360c0000000p-1f,
|
||||
0x1.a51f340000000p-1f, 0x1.c8a9d20000000p-4f, -0x1.ce5e0a0000000p-7f,
|
||||
-0x1.aade480000000p-12f, -0x1.9d1b940000000p-10f, 0x1.7474cc0000000p-8f,
|
||||
0x1.4d85040000000p-7f, -0x1.012c820000000p-4f, -0x1.8e03600000000p-4f,
|
||||
0x1.4ffb520000000p-1f, 0x1.a073360000000p-1f, 0x1.ab21ee0000000p-4f,
|
||||
-0x1.b5ce920000000p-7f, -0x1.ab1c980000000p-12f, -0x1.8a49160000000p-10f,
|
||||
0x1.78eec60000000p-8f, 0x1.4f55b00000000p-7f, -0x1.0c57ca0000000p-4f,
|
||||
-0x1.77e4720000000p-4f, 0x1.56a8e60000000p-1f, 0x1.9ba3820000000p-1f,
|
||||
0x1.8e894a0000000p-4f, -0x1.9d20fe0000000p-7f, -0x1.ab834e0000000p-12f,
|
||||
-0x1.762b260000000p-10f, 0x1.7d3dde0000000p-8f, 0x1.50c0480000000p-7f,
|
||||
-0x1.17a0ae0000000p-4f, -0x1.60a5560000000p-4f, 0x1.5d3d4e0000000p-1f,
|
||||
0x1.96b1680000000p-1f, 0x1.72df7c0000000p-4f, -0x1.846b160000000p-7f,
|
||||
-0x1.ac64680000000p-12f, -0x1.60cd740000000p-10f, 0x1.81625a0000000p-8f,
|
||||
0x1.51b9dc0000000p-7f, -0x1.2303ee0000000p-4f, -0x1.4845000000000p-4f,
|
||||
0x1.63b7140000000p-1f, 0x1.919e200000000p-1f, 0x1.5823340000000p-4f,
|
||||
-0x1.6bcd900000000p-7f, -0x1.adf08e0000000p-12f, -0x1.4a3ac20000000p-10f,
|
||||
0x1.855cfe0000000p-8f, 0x1.5237680000000p-7f, -0x1.2e7e4e0000000p-4f,
|
||||
-0x1.2ec2760000000p-4f, 0x1.6a14c00000000p-1f, 0x1.8c6afe0000000p-1f,
|
||||
0x1.3e53ba0000000p-4f, -0x1.53608c0000000p-7f, -0x1.b06dac0000000p-12f,
|
||||
-0x1.327bec0000000p-10f, 0x1.892e780000000p-8f, 0x1.522d9c0000000p-7f,
|
||||
-0x1.3a0c300000000p-4f, -0x1.141d6e0000000p-4f, 0x1.7054ee0000000p-1f,
|
||||
0x1.87194e0000000p-1f, 0x1.256f780000000p-4f, -0x1.3b3a720000000p-7f,
|
||||
-0x1.b4260a0000000p-12f, -0x1.199f100000000p-10f, 0x1.8cd8000000000p-8f,
|
||||
0x1.5190560000000p-7f, -0x1.45aa140000000p-4f, -0x1.f0ab120000000p-5f,
|
||||
0x1.7676320000000p-1f, 0x1.81aa700000000p-1f, 0x1.0d73ae0000000p-4f,
|
||||
-0x1.2378140000000p-7f, -0x1.b946980000000p-12f, -0x1.ff5dae0000000p-11f,
|
||||
0x1.905a600000000p-8f, 0x1.5053d20000000p-7f, -0x1.5154220000000p-4f,
|
||||
-0x1.b6d5d40000000p-5f, 0x1.7c772e0000000p-1f, 0x1.7c1fb80000000p-1f,
|
||||
0x1.ecbc3e0000000p-5f, -0x1.0c29f20000000p-7f, -0x1.c0151e0000000p-12f,
|
||||
-0x1.c96f140000000p-11f, 0x1.93b7460000000p-8f, 0x1.4e6c1e0000000p-7f,
|
||||
-0x1.5d064a0000000p-4f, -0x1.7abc7a0000000p-5f, 0x1.8256860000000p-1f,
|
||||
0x1.767a860000000p-1f, 0x1.c059b00000000p-5f, -0x1.eadf100000000p-8f,
|
||||
-0x1.c8cc9c0000000p-12f, -0x1.918eb80000000p-11f, 0x1.96effa0000000p-8f,
|
||||
0x1.4bcc520000000p-7f, -0x1.68bca00000000p-4f, -0x1.3c5fe80000000p-5f,
|
||||
0x1.8812e80000000p-1f, 0x1.70bc380000000p-1f, 0x1.95b8180000000p-5f,
|
||||
-0x1.bebb380000000p-8f, -0x1.d3ab2e0000000p-12f, -0x1.57d51e0000000p-11f,
|
||||
0x1.9a075e0000000p-8f, 0x1.4868760000000p-7f, -0x1.7472b40000000p-4f,
|
||||
-0x1.f785a60000000p-6f, 0x1.8dab0e0000000p-1f, 0x1.6ae6480000000p-1f,
|
||||
0x1.6cd0a40000000p-5f, -0x1.93fad20000000p-8f, -0x1.e0dd0e0000000p-12f,
|
||||
-0x1.1c6a140000000p-11f, 0x1.9cfe1a0000000p-8f, 0x1.44329c0000000p-7f,
|
||||
-0x1.80243a0000000p-4f, -0x1.71cfa40000000p-6f, 0x1.931db60000000p-1f,
|
||||
0x1.64fa0a0000000p-1f, 0x1.459afa0000000p-5f, -0x1.6ae5d80000000p-8f,
|
||||
-0x1.f0751e0000000p-12f, -0x1.bea86e0000000p-12f, 0x1.9fd7d60000000p-8f,
|
||||
0x1.3f20100000000p-7f, -0x1.8bcca00000000p-4f, -0x1.cf4aee0000000p-7f,
|
||||
0x1.9869a40000000p-1f, 0x1.5ef8f00000000p-1f, 0x1.2010120000000p-5f,
|
||||
-0x1.43917e0000000p-8f, -0x1.014d9e0000000p-11f, -0x1.41769a0000000p-12f,
|
||||
0x1.a2962a0000000p-8f, 0x1.39224a0000000p-7f, -0x1.9767380000000p-4f,
|
||||
-0x1.643b740000000p-8f, 0x1.9d8dac0000000p-1f, 0x1.58e4620000000p-1f,
|
||||
0x1.f8505c0000000p-6f, -0x1.1e15b40000000p-8f, -0x1.0bbd1e0000000p-11f,
|
||||
-0x1.82f5720000000p-13f, 0x1.a53c820000000p-8f, 0x1.322da40000000p-7f,
|
||||
-0x1.a2ef380000000p-4f, 0x1.cf4bf20000000p-9f, 0x1.a288b20000000p-1f,
|
||||
0x1.52bdd00000000p-1f, 0x1.b3a9200000000p-6f, -0x1.f55fc60000000p-9f,
|
||||
-0x1.1795420000000p-11f, -0x1.f78ba40000000p-15f, 0x1.a7cd100000000p-8f,
|
||||
0x1.2a34100000000p-7f, -0x1.ae5f920000000p-4f, 0x1.a272920000000p-7f,
|
||||
0x1.a7599a0000000p-1f, 0x1.4c86bc0000000p-1f, 0x1.72191a0000000p-6f,
|
||||
-0x1.b278bc0000000p-9f, -0x1.24d82e0000000p-11f, 0x1.176aba0000000p-14f,
|
||||
0x1.aa4b400000000p-8f, 0x1.21297e0000000p-7f, -0x1.b9b3340000000p-4f,
|
||||
0x1.6cd36a0000000p-6f, 0x1.abff500000000p-1f, 0x1.46406e0000000p-1f,
|
||||
0x1.3392320000000p-6f, -0x1.7413460000000p-9f, -0x1.3371980000000p-11f,
|
||||
0x1.9954380000000p-13f, 0x1.acba4a0000000p-8f, 0x1.1700e40000000p-7f,
|
||||
-0x1.c4e4ec0000000p-4f, 0x1.0655200000000p-5f, 0x1.b078e40000000p-1f,
|
||||
0x1.3fec680000000p-1f, 0x1.efd5a20000000p-7f, -0x1.3a3eea0000000p-9f,
|
||||
-0x1.43727a0000000p-11f, 0x1.5542700000000p-12f, 0x1.af1de80000000p-8f,
|
||||
0x1.0bacb40000000p-7f, -0x1.cfef6c0000000p-4f, 0x1.58574a0000000p-5f,
|
||||
0x1.b4c5620000000p-1f, 0x1.398c3c0000000p-1f, 0x1.7e1d1c0000000p-7f,
|
||||
-0x1.039df20000000p-9f, -0x1.54c2b80000000p-11f, 0x1.df54960000000p-12f,
|
||||
0x1.b17a740000000p-8f, 0x1.fe42040000000p-8f, -0x1.dacd4c0000000p-4f,
|
||||
0x1.ac68700000000p-5f, 0x1.b8e3f00000000p-1f, 0x1.33211a0000000p-1f,
|
||||
0x1.11a4f80000000p-7f, -0x1.a3b4ea0000000p-10f, -0x1.671b880000000p-11f,
|
||||
0x1.3568c80000000p-11f, 0x1.b3d4520000000p-8f, 0x1.e2a46e0000000p-8f,
|
||||
-0x1.e579200000000p-4f, 0x1.013ff60000000p-4f, 0x1.bcd3ee0000000p-1f,
|
||||
0x1.2cacd00000000p-1f, 0x1.53b1420000000p-8f, -0x1.42fb1c0000000p-10f,
|
||||
-0x1.7b9bba0000000p-11f, 0x1.7c27840000000p-11f, 0x1.b62ed40000000p-8f,
|
||||
0x1.c465e60000000p-8f, -0x1.efecf00000000p-4f, 0x1.2d4b160000000p-4f,
|
||||
0x1.c093960000000p-1f, 0x1.2631fe0000000p-1f, 0x1.21b5e00000000p-9f,
|
||||
-0x1.a0c52c0000000p-11f,
|
||||
};
|
||||
|
||||
inline constexpr std::array<std::complex<double>, 64> kRotation = {{
|
||||
{0x1.fff6220000000p-1f, -0x1.921d200000000p-7f},
|
||||
{0x1.ffa72e0000000p-1f, -0x1.2d86580000000p-5f},
|
||||
{0x1.ff09560000000p-1f, -0x1.f656e80000000p-5f},
|
||||
{0x1.fe1cb00000000p-1f, -0x1.5f6d000000000p-4f},
|
||||
{0x1.fce1600000000p-1f, -0x1.c3785c0000000p-4f},
|
||||
{0x1.fb57980000000p-1f, -0x1.139f0c0000000p-3f},
|
||||
{0x1.f97f920000000p-1f, -0x1.45576c0000000p-3f},
|
||||
{0x1.f7599a0000000p-1f, -0x1.76dd9e0000000p-3f},
|
||||
{0x1.f4e6040000000p-1f, -0x1.a82a020000000p-3f},
|
||||
{0x1.f225300000000p-1f, -0x1.d934fe0000000p-3f},
|
||||
{0x1.ef178a0000000p-1f, -0x1.04fb800000000p-2f},
|
||||
{0x1.ebbd8c0000000p-1f, -0x1.1d34440000000p-2f},
|
||||
{0x1.e817ba0000000p-1f, -0x1.35410c0000000p-2f},
|
||||
{0x1.e426a40000000p-1f, -0x1.4d1e240000000p-2f},
|
||||
{0x1.dfeae60000000p-1f, -0x1.64c7de0000000p-2f},
|
||||
{0x1.db65260000000p-1f, -0x1.7c3a940000000p-2f},
|
||||
{0x1.d696180000000p-1f, -0x1.9372a60000000p-2f},
|
||||
{0x1.d17e780000000p-1f, -0x1.aa6c820000000p-2f},
|
||||
{0x1.cc1f100000000p-1f, -0x1.c1249e0000000p-2f},
|
||||
{0x1.c678b40000000p-1f, -0x1.d797760000000p-2f},
|
||||
{0x1.c08c420000000p-1f, -0x1.edc1960000000p-2f},
|
||||
{0x1.ba5aa60000000p-1f, -0x1.01cfc80000000p-1f},
|
||||
{0x1.b3e4d40000000p-1f, -0x1.0c97040000000p-1f},
|
||||
{0x1.ad2bca0000000p-1f, -0x1.1734d60000000p-1f},
|
||||
{0x1.a630920000000p-1f, -0x1.21a79a0000000p-1f},
|
||||
{0x1.9ef43e0000000p-1f, -0x1.2bedb20000000p-1f},
|
||||
{0x1.9777f00000000p-1f, -0x1.36058c0000000p-1f},
|
||||
{0x1.8fbcca0000000p-1f, -0x1.3fed960000000p-1f},
|
||||
{0x1.87c4000000000p-1f, -0x1.49a44a0000000p-1f},
|
||||
{0x1.7f8ece0000000p-1f, -0x1.53282a0000000p-1f},
|
||||
{0x1.771e760000000p-1f, -0x1.5c77bc0000000p-1f},
|
||||
{0x1.6e74460000000p-1f, -0x1.6591920000000p-1f},
|
||||
{0x1.6591920000000p-1f, -0x1.6e74460000000p-1f},
|
||||
{0x1.5c77bc0000000p-1f, -0x1.771e760000000p-1f},
|
||||
{0x1.53282a0000000p-1f, -0x1.7f8ece0000000p-1f},
|
||||
{0x1.49a44a0000000p-1f, -0x1.87c4000000000p-1f},
|
||||
{0x1.3fed960000000p-1f, -0x1.8fbcca0000000p-1f},
|
||||
{0x1.36058c0000000p-1f, -0x1.9777f00000000p-1f},
|
||||
{0x1.2bedb20000000p-1f, -0x1.9ef43e0000000p-1f},
|
||||
{0x1.21a79a0000000p-1f, -0x1.a630920000000p-1f},
|
||||
{0x1.1734d60000000p-1f, -0x1.ad2bca0000000p-1f},
|
||||
{0x1.0c97040000000p-1f, -0x1.b3e4d40000000p-1f},
|
||||
{0x1.01cfc80000000p-1f, -0x1.ba5aa60000000p-1f},
|
||||
{0x1.edc1960000000p-2f, -0x1.c08c420000000p-1f},
|
||||
{0x1.d797760000000p-2f, -0x1.c678b40000000p-1f},
|
||||
{0x1.c1249e0000000p-2f, -0x1.cc1f100000000p-1f},
|
||||
{0x1.aa6c820000000p-2f, -0x1.d17e780000000p-1f},
|
||||
{0x1.9372a60000000p-2f, -0x1.d696180000000p-1f},
|
||||
{0x1.7c3a940000000p-2f, -0x1.db65260000000p-1f},
|
||||
{0x1.64c7de0000000p-2f, -0x1.dfeae60000000p-1f},
|
||||
{0x1.4d1e240000000p-2f, -0x1.e426a40000000p-1f},
|
||||
{0x1.35410c0000000p-2f, -0x1.e817ba0000000p-1f},
|
||||
{0x1.1d34440000000p-2f, -0x1.ebbd8c0000000p-1f},
|
||||
{0x1.04fb800000000p-2f, -0x1.ef178a0000000p-1f},
|
||||
{0x1.d934fe0000000p-3f, -0x1.f225300000000p-1f},
|
||||
{0x1.a82a020000000p-3f, -0x1.f4e6040000000p-1f},
|
||||
{0x1.76dd9e0000000p-3f, -0x1.f7599a0000000p-1f},
|
||||
{0x1.45576c0000000p-3f, -0x1.f97f920000000p-1f},
|
||||
{0x1.139f0c0000000p-3f, -0x1.fb57980000000p-1f},
|
||||
{0x1.c3785c0000000p-4f, -0x1.fce1600000000p-1f},
|
||||
{0x1.5f6d000000000p-4f, -0x1.fe1cb00000000p-1f},
|
||||
{0x1.f656e80000000p-5f, -0x1.ff09560000000p-1f},
|
||||
{0x1.2d86580000000p-5f, -0x1.ffa72e0000000p-1f},
|
||||
{0x1.921d200000000p-7f, -0x1.fff6220000000p-1f},
|
||||
}};
|
||||
|
||||
inline constexpr std::array<std::complex<double>, 22> kHybrid = {{
|
||||
{-0x1.3a7de80000000p-7f, 0x1.0488900000000p-8f},
|
||||
{-0x1.4f39c60000000p-6f, 0x1.4f39c60000000p-6f},
|
||||
{-0x1.4f65900000000p-6f, 0x1.94dc0e0000000p-5f},
|
||||
{0x0.0p+0f, 0x1.4382f00000000p-4f},
|
||||
{0x1.3844900000000p-5f, 0x1.78f0ca0000000p-4f},
|
||||
{0x1.459b100000000p-4f, 0x1.459b100000000p-4f},
|
||||
{0x1.c49e200000000p-4f, 0x1.76f5f60000000p-5f},
|
||||
{0x1.0000000000000p-3f, 0x0.0p+0f},
|
||||
{0x1.c426000000000p-4f, -0x1.7692720000000p-5f},
|
||||
{0x1.2d4d820000000p-5f, -0x1.2d4d820000000p-5f},
|
||||
{0x1.e4029a0000000p-6f, -0x1.2420400000000p-4f},
|
||||
{-0x1.1ce09c0000000p-7f, 0x1.1ce0900000000p-7f},
|
||||
{0x0.0p+0f, 0x1.b5bb180000000p-6f},
|
||||
{0x1.586f240000000p-6f, 0x1.586f2c0000000p-6f},
|
||||
{0x0.0p+0f, 0x0.0p+0f},
|
||||
{-0x1.9eb2460000000p-5f, 0x1.9eb2460000000p-5f},
|
||||
{0x0.0p+0f, 0x1.5019100000000p-3f},
|
||||
{0x1.515a8e0000000p-3f, 0x1.515a8e0000000p-3f},
|
||||
{0x1.0000000000000p-2f, 0x0.0p+0f},
|
||||
{0x1.515b660000000p-3f, -0x1.515b660000000p-3f},
|
||||
{0x0.0p+0f, -0x1.1f2f040000000p-3f},
|
||||
{-0x1.af372a0000000p-5f, -0x1.af372a0000000p-5f},
|
||||
}};
|
||||
|
||||
}
|
||||
@@ -1,535 +0,0 @@
|
||||
#include "hrtf/rosella_model.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/fs_utf8.h"
|
||||
#include "foundation/mini_json.h"
|
||||
#include "foundation/sha256.h"
|
||||
|
||||
namespace joc::hrtf {
|
||||
|
||||
namespace {
|
||||
|
||||
// The model's fixed-point lane scale: every stored value is a Q15 integer.
|
||||
constexpr float kQ15 = 1.0f / 32768.0f;
|
||||
|
||||
Status model_fail(joc_error code, const std::string& message) {
|
||||
return Status::fail(code, stage::kRender, message);
|
||||
}
|
||||
|
||||
float q15(std::int32_t value) { return static_cast<float>(value) * kQ15; }
|
||||
|
||||
float q15_exp(std::int32_t value, int exponent) {
|
||||
return q15(value) * static_cast<float>(std::ldexp(1.0, exponent));
|
||||
}
|
||||
|
||||
std::uint16_t low16(std::int32_t value) {
|
||||
return static_cast<std::uint16_t>(static_cast<std::uint32_t>(value) & 0xFFFFu);
|
||||
}
|
||||
|
||||
std::string trim(const std::string& text) {
|
||||
const std::size_t begin = text.find_first_not_of(" \t\r\n");
|
||||
const std::size_t end = text.find_last_not_of(" \t\r\n");
|
||||
return begin == std::string::npos ? std::string() : text.substr(begin, end - begin + 1u);
|
||||
}
|
||||
|
||||
// The lane array is read straight out of the JSON text: it is one flat list of
|
||||
// integers, and building a 15691-node DOM for it would only cost time.
|
||||
bool parse_int_array(const std::string& raw, std::vector<std::int32_t>* out, std::string* error) {
|
||||
out->clear();
|
||||
const char* cursor = raw.c_str();
|
||||
const char* end = cursor + raw.size();
|
||||
while (cursor < end && *cursor != '[') {
|
||||
++cursor;
|
||||
}
|
||||
if (cursor == end) {
|
||||
*error = "rosella_coefficients must be a JSON array";
|
||||
return false;
|
||||
}
|
||||
++cursor;
|
||||
while (cursor < end) {
|
||||
while (cursor < end && (*cursor == ' ' || *cursor == '\t' || *cursor == '\r' ||
|
||||
*cursor == '\n' || *cursor == ',')) {
|
||||
++cursor;
|
||||
}
|
||||
if (cursor >= end) {
|
||||
break;
|
||||
}
|
||||
if (*cursor == ']') {
|
||||
return true;
|
||||
}
|
||||
const bool negative = *cursor == '-';
|
||||
if (negative) {
|
||||
++cursor;
|
||||
}
|
||||
if (cursor >= end || *cursor < '0' || *cursor > '9') {
|
||||
*error = "rosella_coefficients contains a non-integer value";
|
||||
return false;
|
||||
}
|
||||
long long value = 0;
|
||||
while (cursor < end && *cursor >= '0' && *cursor <= '9') {
|
||||
value = value * 10 + (*cursor - '0');
|
||||
if (value > (1ll << 40)) {
|
||||
*error = "rosella_coefficients value is out of range";
|
||||
return false;
|
||||
}
|
||||
++cursor;
|
||||
}
|
||||
// A fractional part or an exponent means the value is not an exact integer.
|
||||
if (cursor < end && (*cursor == '.' || *cursor == 'e' || *cursor == 'E')) {
|
||||
*error = "rosella_coefficients contains a non-integer value";
|
||||
return false;
|
||||
}
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
if (value < -(1ll << 31) || value > (1ll << 31) - 1) {
|
||||
*error = "rosella_coefficients value is outside signed int32";
|
||||
return false;
|
||||
}
|
||||
out->push_back(static_cast<std::int32_t>(value));
|
||||
}
|
||||
*error = "rosella_coefficients array is truncated";
|
||||
return false;
|
||||
}
|
||||
|
||||
struct RpHeader {
|
||||
std::uint16_t stored_checksum = 0;
|
||||
std::uint16_t computed_checksum = 0;
|
||||
bool checksum_valid = false;
|
||||
bool table_a_present = false;
|
||||
bool table_b_present = false;
|
||||
bool table_c_present = false;
|
||||
int table_a_dimension = 0;
|
||||
int table_a_option = 0;
|
||||
int table_a_extra = 0;
|
||||
int table_b_dimension = 0;
|
||||
int table_b_extra = 0;
|
||||
int table_b_groups = 0;
|
||||
int table_c_dimension = 0;
|
||||
std::size_t active_lanes = 0;
|
||||
};
|
||||
|
||||
Status inspect_rp(const std::vector<std::int32_t>& lanes, RpHeader* out) {
|
||||
if (lanes.size() < 5u) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "Rosella rp must contain whole int32 lanes");
|
||||
}
|
||||
if (low16(lanes[0]) != 0x7072u) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "bad Rosella rp magic");
|
||||
}
|
||||
out->stored_checksum = low16(lanes[1]);
|
||||
out->table_a_present = low16(lanes[2]) != 0u;
|
||||
out->table_b_present = low16(lanes[3]) != 0u;
|
||||
out->table_c_present = low16(lanes[4]) != 0u;
|
||||
std::size_t index = 5u;
|
||||
if (out->table_a_present) {
|
||||
out->table_a_dimension = low16(lanes[index]);
|
||||
out->table_a_option = low16(lanes[index + 1u]);
|
||||
out->table_a_extra = low16(lanes[index + 2u]);
|
||||
index += 5u;
|
||||
} else {
|
||||
out->table_a_dimension = 77;
|
||||
}
|
||||
if (out->table_b_present) {
|
||||
if (!out->table_a_present) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT,
|
||||
"Rosella rp table B cannot be present without table A");
|
||||
}
|
||||
out->table_b_dimension = low16(lanes[index]);
|
||||
out->table_b_extra = low16(lanes[index + 1u]);
|
||||
out->table_b_groups = low16(lanes[index + 2u]);
|
||||
index += 3u;
|
||||
}
|
||||
if (out->table_c_present) {
|
||||
out->table_c_dimension = low16(lanes[index]);
|
||||
index += 1u;
|
||||
}
|
||||
const long long payload_words =
|
||||
static_cast<long long>(index) - 2 +
|
||||
(out->table_b_present ? (out->table_b_dimension + 380 * out->table_b_groups +
|
||||
out->table_b_extra + 79)
|
||||
: 0) +
|
||||
(out->table_a_present ? (171 * out->table_a_extra + 79 +
|
||||
2 * (out->table_a_option + 14 * out->table_a_dimension))
|
||||
: 0) +
|
||||
11 + (out->table_c_present ? (314 * out->table_c_dimension + 1) : 0);
|
||||
if (payload_words < 0) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "malformed Rosella rp header");
|
||||
}
|
||||
out->active_lanes = static_cast<std::size_t>(2 + payload_words);
|
||||
if (lanes.size() < out->active_lanes) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "Rosella rp is truncated");
|
||||
}
|
||||
std::uint32_t computed = 0xA569u;
|
||||
for (std::size_t lane = 2u; lane < out->active_lanes; ++lane) {
|
||||
computed ^= low16(lanes[lane]);
|
||||
}
|
||||
out->computed_checksum = static_cast<std::uint16_t>(computed & 0xFFFFu);
|
||||
out->checksum_valid = out->computed_checksum == out->stored_checksum;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
// _unpack_field: the serialized 154-per-direction field lanes to the padded grid.
|
||||
void unpack_field(const std::int32_t* serialized, int directions, int exponent,
|
||||
std::vector<float>* padded) {
|
||||
padded->assign(static_cast<std::size_t>(160 * directions), 0.0f);
|
||||
const int stride8 = 8 * directions;
|
||||
const int stride2 = 2 * directions;
|
||||
for (int source = 0; source < 154 * directions; ++source) {
|
||||
const int group4 = (source % stride8) / stride2;
|
||||
const int destination = (group4 & 3) + 4 * (source % stride2 +
|
||||
2 * directions * (source / stride8 +
|
||||
(group4 >> 2)));
|
||||
(*padded)[static_cast<std::size_t>(destination)] =
|
||||
q15_exp(serialized[source], exponent);
|
||||
}
|
||||
}
|
||||
|
||||
// _unpack_table_a_grid: the serialized table-A rows to the padded lane grid.
|
||||
void unpack_table_a_grid(const std::int32_t* serialized, int dimension, int serialized_rows,
|
||||
int padded_rows, int lane_group, std::vector<float>* padded) {
|
||||
padded->assign(static_cast<std::size_t>(padded_rows) * static_cast<std::size_t>(dimension),
|
||||
0.0f);
|
||||
const int group_width = lane_group * 4;
|
||||
for (int source = 0; source < serialized_rows * dimension; ++source) {
|
||||
const int remainder = source % group_width;
|
||||
const int destination = (remainder / lane_group) +
|
||||
4 * (remainder % lane_group +
|
||||
group_width / 4 * (source / group_width));
|
||||
(*padded)[static_cast<std::size_t>(destination)] = q15(serialized[source]);
|
||||
}
|
||||
}
|
||||
|
||||
void unpack_table_a_extra(const std::int32_t* serialized, std::vector<float>* padded) {
|
||||
padded->assign(160u, 0.0f);
|
||||
for (int source = 0; source < 154; ++source) {
|
||||
const int remainder = source & 7;
|
||||
const int destination = (remainder >> 1) + 4 * ((source & 1) + 2 * (source >> 3));
|
||||
(*padded)[static_cast<std::size_t>(destination)] = q15(serialized[source]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string RosellaModel::summary() const {
|
||||
std::string name = capture.name.empty() ? std::string("unnamed") : capture.name;
|
||||
return "Rosella personalized_headphone '" + name + "' (" +
|
||||
(room_model.empty() ? std::string("unknown room") : room_model) + "), " +
|
||||
std::to_string(table_a_dimension) + " HQMF / 77 hybrid @ " +
|
||||
std::to_string(sample_rate) + " Hz";
|
||||
}
|
||||
|
||||
Status load_personalized_headphone(const std::string& path, RosellaModel* out) {
|
||||
if (out == nullptr) {
|
||||
return model_fail(JOC_ERR_INVALID_ARGUMENT, "null Rosella model destination");
|
||||
}
|
||||
if (!fs_utf8::exists(path)) {
|
||||
return model_fail(JOC_ERR_HRTF_NOT_FOUND, "personalized headphone model not found: " + path);
|
||||
}
|
||||
std::ifstream stream = fs_utf8::open_input(path);
|
||||
if (!stream.good()) {
|
||||
return model_fail(JOC_ERR_IO, "cannot open " + path);
|
||||
}
|
||||
std::string text((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
|
||||
if (text.empty()) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "empty personalized headphone model: " + path);
|
||||
}
|
||||
// The checksum is taken over the coefficient lanes, exactly as upstream hashes
|
||||
// the int32 image of the array.
|
||||
const std::size_t first = text.find_first_not_of(" \t\r\n");
|
||||
if (first == std::string::npos || text[first] != '{') {
|
||||
return model_fail(JOC_ERR_NOT_SUPPORTED,
|
||||
"raw rp models are not supported; use a .personalized_headphone JSON");
|
||||
}
|
||||
std::vector<json::Member> root;
|
||||
std::string error;
|
||||
if (!json::parse_object(text, &root, &error)) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "invalid personalized headphone JSON: " + error);
|
||||
}
|
||||
const json::Member* personalized = json::find(root, "personalized_hrtf");
|
||||
if (personalized == nullptr) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "personalized_hrtf is missing");
|
||||
}
|
||||
std::vector<json::Member> inner;
|
||||
if (!json::parse_object(personalized->raw, &inner, &error)) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "invalid personalized_hrtf object: " + error);
|
||||
}
|
||||
const json::Member* virtualizer = json::find(inner, "virtualizer_parameters");
|
||||
if (virtualizer == nullptr) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "virtualizer_parameters is missing");
|
||||
}
|
||||
std::vector<json::Member> parameters;
|
||||
if (!json::parse_object(virtualizer->raw, ¶meters, &error)) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "invalid virtualizer_parameters: " + error);
|
||||
}
|
||||
const json::Member* coefficient_member = json::find(parameters, "rosella_coefficients");
|
||||
if (coefficient_member == nullptr) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "rosella_coefficients is missing");
|
||||
}
|
||||
RosellaModel model;
|
||||
model.source_path = path;
|
||||
if (const json::Member* member = json::find(parameters, "rosella_coefficients_version")) {
|
||||
json::as_string(*member, &model.coefficient_version);
|
||||
}
|
||||
if (const json::Member* member = json::find(parameters, "room_model")) {
|
||||
json::as_string(*member, &model.room_model);
|
||||
}
|
||||
if (const json::Member* capture = json::find(inner, "phrtf_capture_metadata")) {
|
||||
std::vector<json::Member> fields;
|
||||
if (json::parse_object(capture->raw, &fields, &error)) {
|
||||
const std::pair<const char*, std::string*> mapping[] = {
|
||||
{"capture_submission_date", &model.capture.capture_submission_date},
|
||||
{"capture_type", &model.capture.capture_type},
|
||||
{"label", &model.capture.label},
|
||||
{"name", &model.capture.name},
|
||||
{"phrtf_algorithm_version", &model.capture.algorithm_version},
|
||||
{"phrtf_creation_date", &model.capture.creation_date},
|
||||
{"uuid", &model.capture.uuid},
|
||||
{"version", &model.capture.version},
|
||||
};
|
||||
for (const auto& entry : mapping) {
|
||||
if (const json::Member* member = json::find(fields, entry.first)) {
|
||||
json::as_string(*member, entry.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<std::int32_t> lanes;
|
||||
if (!parse_int_array(coefficient_member->raw, &lanes, &error)) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, error);
|
||||
}
|
||||
{
|
||||
crypto::Sha256 hash;
|
||||
hash.update(lanes.data(), lanes.size() * sizeof(std::int32_t));
|
||||
model.coefficient_sha256 = hash.finish_hex();
|
||||
}
|
||||
|
||||
RpHeader header;
|
||||
Status status = inspect_rp(lanes, &header);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
if (!header.checksum_valid || header.active_lanes != lanes.size()) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT,
|
||||
"invalid or non-active Rosella rp coefficient sequence");
|
||||
}
|
||||
if (!header.table_a_present || !header.table_b_present || header.table_c_present) {
|
||||
return model_fail(JOC_ERR_NOT_SUPPORTED,
|
||||
"the renderer requires table A+B and no table C");
|
||||
}
|
||||
if (header.table_a_dimension != 64 || header.table_a_option != 3) {
|
||||
return model_fail(JOC_ERR_NOT_SUPPORTED,
|
||||
"the renderer requires the observed 64-channel HQMF layout");
|
||||
}
|
||||
if (header.table_b_dimension != 20 || header.table_b_groups != 36) {
|
||||
return model_fail(JOC_ERR_NOT_SUPPORTED,
|
||||
"the renderer requires 20 hybrid groups and 36 direction terms");
|
||||
}
|
||||
|
||||
const std::int32_t* values = lanes.data();
|
||||
const std::size_t total = lanes.size();
|
||||
std::size_t position = 13u;
|
||||
const int extra = header.table_a_extra;
|
||||
model.table_a_dimension = header.table_a_dimension;
|
||||
model.table_a_option = header.table_a_option;
|
||||
model.table_a_extra = extra;
|
||||
model.table_a_header_field = low16(values[8]);
|
||||
model.table_a_header_25 = low16(values[9]);
|
||||
model.table_a_control = low16(values[position]);
|
||||
model.field_exponent = values[position];
|
||||
position += 1u;
|
||||
const int option_count = header.table_a_option;
|
||||
model.table_a_option_ids.resize(static_cast<std::size_t>(option_count));
|
||||
for (int index = 0; index < option_count; ++index) {
|
||||
model.table_a_option_ids[static_cast<std::size_t>(index)] =
|
||||
low16(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += static_cast<std::size_t>(option_count);
|
||||
model.table_a_option_values.resize(static_cast<std::size_t>(option_count));
|
||||
for (int index = 0; index < option_count; ++index) {
|
||||
model.table_a_option_values[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += static_cast<std::size_t>(option_count);
|
||||
model.table_a_scalar = q15(values[position]);
|
||||
position += 1u;
|
||||
const int dimension = header.table_a_dimension;
|
||||
unpack_table_a_grid(values + position, dimension, 16, 20, 16,
|
||||
&model.table_a_filter_16x64_padded);
|
||||
position += static_cast<std::size_t>(16 * dimension);
|
||||
for (int index = 0; index < 4; ++index) {
|
||||
model.table_a_four_integers[static_cast<std::size_t>(index)] =
|
||||
low16(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += 4u;
|
||||
model.table_a_integer = low16(values[position]);
|
||||
position += 1u;
|
||||
unpack_table_a_grid(values + position, dimension, 8, 10, 8,
|
||||
&model.table_a_filter_8x64_padded);
|
||||
position += static_cast<std::size_t>(8 * dimension);
|
||||
model.table_a_vector16.resize(16u);
|
||||
for (int index = 0; index < 16; ++index) {
|
||||
model.table_a_vector16[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += 16u;
|
||||
unpack_table_a_grid(values + position, dimension, 4, 5, 4,
|
||||
&model.table_a_filter_4x64_padded);
|
||||
position += static_cast<std::size_t>(4 * dimension);
|
||||
model.table_a_extra_indices.resize(static_cast<std::size_t>(extra));
|
||||
for (int index = 0; index < extra; ++index) {
|
||||
model.table_a_extra_indices[static_cast<std::size_t>(index)] =
|
||||
low16(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += static_cast<std::size_t>(extra);
|
||||
model.table_a_extra_fields_padded.assign(static_cast<std::size_t>(extra) * 160u, 0.0f);
|
||||
std::vector<float> unpacked;
|
||||
for (int index = 0; index < extra; ++index) {
|
||||
unpack_table_a_extra(values + position, &unpacked);
|
||||
std::copy(unpacked.begin(), unpacked.end(),
|
||||
model.table_a_extra_fields_padded.begin() + static_cast<std::ptrdiff_t>(index) * 160);
|
||||
position += 154u;
|
||||
}
|
||||
model.table_a_extra_vectors.assign(static_cast<std::size_t>(extra) * 16u, 0.0f);
|
||||
for (int index = 0; index < extra; ++index) {
|
||||
for (int lane = 0; lane < 16; ++lane) {
|
||||
model.table_a_extra_vectors[static_cast<std::size_t>(index) * 16u +
|
||||
static_cast<std::size_t>(lane)] =
|
||||
q15(values[position + static_cast<std::size_t>(lane)]);
|
||||
}
|
||||
position += 16u;
|
||||
}
|
||||
const std::size_t table_b_start = position;
|
||||
if (table_b_start != 13u + 1821u + static_cast<std::size_t>(171 * extra)) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "Rosella table-A parser lost its place");
|
||||
}
|
||||
|
||||
model.sample_rate = 2 * low16(values[position]);
|
||||
position += 1u;
|
||||
model.matrix_exponent = values[position];
|
||||
position += 1u;
|
||||
const std::size_t matrix_count = 36u * 36u;
|
||||
model.matrix_left.resize(matrix_count);
|
||||
model.matrix_right.resize(matrix_count);
|
||||
for (std::size_t index = 0; index < matrix_count; ++index) {
|
||||
model.matrix_left[index] = q15_exp(values[position + index], model.matrix_exponent);
|
||||
}
|
||||
position += matrix_count;
|
||||
for (std::size_t index = 0; index < matrix_count; ++index) {
|
||||
model.matrix_right[index] = q15_exp(values[position + index], model.matrix_exponent);
|
||||
}
|
||||
position += matrix_count;
|
||||
model.vector_left.resize(36u);
|
||||
model.vector_right.resize(36u);
|
||||
for (int index = 0; index < 36; ++index) {
|
||||
model.vector_left[static_cast<std::size_t>(index)] =
|
||||
q15_exp(values[position + static_cast<std::size_t>(index)], model.matrix_exponent);
|
||||
}
|
||||
position += 36u;
|
||||
for (int index = 0; index < 36; ++index) {
|
||||
model.vector_right[static_cast<std::size_t>(index)] =
|
||||
q15_exp(values[position + static_cast<std::size_t>(index)], model.matrix_exponent);
|
||||
}
|
||||
position += 36u;
|
||||
|
||||
const std::size_t serialized_count = 154u * 36u;
|
||||
unpack_field(values + position, 36, model.field_exponent, &model.field_left_padded);
|
||||
bool odd_zero = true;
|
||||
for (std::size_t index = 1u; index < serialized_count; index += 2u) {
|
||||
const float value = q15_exp(values[position + index], model.field_exponent);
|
||||
if (std::abs(value) > 1.0e-6f) {
|
||||
odd_zero = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
model.field_left_odd_serialized_zero = odd_zero;
|
||||
position += serialized_count;
|
||||
unpack_field(values + position, 36, model.field_exponent, &model.field_right_padded);
|
||||
position += serialized_count;
|
||||
|
||||
model.hybrid_flags.resize(20u);
|
||||
int active_hybrid = 0;
|
||||
for (int index = 0; index < 20; ++index) {
|
||||
model.hybrid_flags[static_cast<std::size_t>(index)] =
|
||||
low16(values[position + static_cast<std::size_t>(index)]);
|
||||
if (model.hybrid_flags[static_cast<std::size_t>(index)] == 1) {
|
||||
++active_hybrid;
|
||||
}
|
||||
}
|
||||
position += 20u;
|
||||
if (active_hybrid != header.table_b_extra) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "hybrid value count does not match the header");
|
||||
}
|
||||
model.hybrid_values.resize(static_cast<std::size_t>(active_hybrid));
|
||||
for (int index = 0; index < active_hybrid; ++index) {
|
||||
model.hybrid_values[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += static_cast<std::size_t>(active_hybrid);
|
||||
model.model_scalars.resize(5u);
|
||||
for (int index = 0; index < 5; ++index) {
|
||||
model.model_scalars[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += 5u;
|
||||
const std::size_t expected_tail =
|
||||
table_b_start + static_cast<std::size_t>(header.table_b_dimension +
|
||||
380 * header.table_b_groups +
|
||||
header.table_b_extra + 79);
|
||||
if (position != expected_tail) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "Rosella table-B parser lost its place");
|
||||
}
|
||||
|
||||
model.header_float_scalars[0] = q15(values[position]);
|
||||
model.header_float_scalars[1] = q15(values[position + 1u]) * 16.0f;
|
||||
model.header_integer_fields[0] = values[position + 2u];
|
||||
model.header_integer_fields[1] = low16(values[position + 3u]);
|
||||
position += 4u;
|
||||
for (int profile = 0; profile < 4; ++profile) {
|
||||
RosellaDistanceProfile parsed;
|
||||
for (int index = 0; index < 6; ++index) {
|
||||
parsed.bounds[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += 6u;
|
||||
parsed.distance_scale_m =
|
||||
q15_exp(values[position], values[position + 1u]);
|
||||
position += 2u;
|
||||
parsed.inverse_distance_per_m = q15(values[position]);
|
||||
parsed.axis_scales_internal[0] = q15(values[position + 1u]);
|
||||
parsed.axis_scales_internal[1] = q15(values[position + 2u]);
|
||||
parsed.axis_scales_internal[2] = q15(values[position + 3u]);
|
||||
parsed.minimum_normalized_radius = q15(values[position + 4u]);
|
||||
position += 5u;
|
||||
model.profiles[static_cast<std::size_t>(profile)] = parsed;
|
||||
}
|
||||
model.profile_tail.resize(8u);
|
||||
for (int index = 0; index < 8; ++index) {
|
||||
model.profile_tail[static_cast<std::size_t>(index)] =
|
||||
q15(values[position + static_cast<std::size_t>(index)]);
|
||||
}
|
||||
position += 8u;
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
model.post_fields[static_cast<std::size_t>(index)] =
|
||||
values[position + static_cast<std::size_t>(index)];
|
||||
}
|
||||
position += 3u;
|
||||
if (position != total) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT, "unparsed Rosella coefficient lanes");
|
||||
}
|
||||
if (model.sample_rate != 48000) {
|
||||
return model_fail(JOC_ERR_HRTF_FORMAT,
|
||||
"Rosella model sample rate must be 48000, got " +
|
||||
std::to_string(model.sample_rate));
|
||||
}
|
||||
*out = std::move(model);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,88 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
// Parser for the Dolby ".personalized_headphone" model (upstream rosella_model.py).
|
||||
// The file is JSON whose virtualizer_parameters carry the raw "rp" coefficient
|
||||
// lanes; everything the renderer needs is unpacked here, in the same float32
|
||||
// arithmetic the reference uses, because those values are part of the model.
|
||||
namespace joc::hrtf {
|
||||
|
||||
struct RosellaDistanceProfile {
|
||||
std::array<float, 6> bounds{};
|
||||
float distance_scale_m = 0.0f;
|
||||
float inverse_distance_per_m = 0.0f;
|
||||
std::array<float, 3> axis_scales_internal{};
|
||||
float minimum_normalized_radius = 0.0f;
|
||||
};
|
||||
|
||||
struct RosellaCaptureMetadata {
|
||||
std::string capture_submission_date;
|
||||
std::string capture_type;
|
||||
std::string label;
|
||||
std::string name;
|
||||
std::string algorithm_version;
|
||||
std::string creation_date;
|
||||
std::string uuid;
|
||||
std::string version;
|
||||
};
|
||||
|
||||
struct RosellaModel {
|
||||
std::string source_path;
|
||||
std::string coefficient_sha256;
|
||||
std::string coefficient_version;
|
||||
std::string room_model;
|
||||
RosellaCaptureMetadata capture;
|
||||
|
||||
int table_a_dimension = 0;
|
||||
int table_a_option = 0;
|
||||
int table_a_extra = 0;
|
||||
int table_a_header_field = 0;
|
||||
int table_a_header_25 = 0;
|
||||
int table_a_control = 0;
|
||||
std::vector<int> table_a_option_ids;
|
||||
std::vector<float> table_a_option_values;
|
||||
float table_a_scalar = 0.0f;
|
||||
std::vector<float> table_a_filter_16x64_padded;
|
||||
std::array<int, 4> table_a_four_integers{};
|
||||
int table_a_integer = 0;
|
||||
std::vector<float> table_a_filter_8x64_padded;
|
||||
std::vector<float> table_a_vector16;
|
||||
std::vector<float> table_a_filter_4x64_padded;
|
||||
std::vector<int> table_a_extra_indices;
|
||||
std::vector<float> table_a_extra_fields_padded;
|
||||
std::vector<float> table_a_extra_vectors;
|
||||
|
||||
int sample_rate = 0;
|
||||
int matrix_exponent = 0;
|
||||
int field_exponent = 0;
|
||||
std::vector<float> matrix_left;
|
||||
std::vector<float> matrix_right;
|
||||
std::vector<float> vector_left;
|
||||
std::vector<float> vector_right;
|
||||
std::vector<float> field_left_padded;
|
||||
std::vector<float> field_right_padded;
|
||||
bool field_left_odd_serialized_zero = false;
|
||||
std::vector<int> hybrid_flags;
|
||||
std::vector<float> hybrid_values;
|
||||
std::vector<float> model_scalars;
|
||||
std::array<float, 2> header_float_scalars{};
|
||||
std::array<int, 2> header_integer_fields{};
|
||||
std::array<RosellaDistanceProfile, 4> profiles{};
|
||||
std::vector<float> profile_tail;
|
||||
std::array<int, 3> post_fields{};
|
||||
|
||||
// One line for reports and logs: the capture name and room model are the
|
||||
// model's own strings, followed by the table layout and sample rate, e.g.
|
||||
// "Rosella personalized_headphone '<name>' (<room>), <N> HQMF / 77 hybrid @ <rate> Hz".
|
||||
std::string summary() const;
|
||||
};
|
||||
|
||||
Status load_personalized_headphone(const std::string& path, RosellaModel* out);
|
||||
|
||||
} // namespace joc::hrtf
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,71 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "hrtf/rosella_bank.h"
|
||||
#include "hrtf/rosella_model.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
#include "timeline/position_timeline.h"
|
||||
|
||||
// Rosella ".personalized_headphone" binaural renderer (upstream rosella_core.py,
|
||||
// rosella_direct.py, rosella_room.py and rosella_binaural_renderer.py). It takes
|
||||
// the same pipeline slot as the SOFA runtime: sixteen object channels per frame in,
|
||||
// interleaved stereo out, with the OAMD timeline driving the per-block parameters.
|
||||
namespace joc::hrtf {
|
||||
|
||||
// rosella_direct.BINAURAL_PROFILE_NAMES.
|
||||
enum class RosellaProfile : std::int32_t { Near = 1, Far = 2, Mid = 3 };
|
||||
|
||||
struct RosellaRenderOptions {
|
||||
RosellaProfile profile = RosellaProfile::Mid;
|
||||
// The analysis and synthesis banks both come from this one choice, so the
|
||||
// latency skip, the flush capacity and the state histories stay consistent.
|
||||
RosellaBankKind bank = RosellaBankKind::Public;
|
||||
std::int64_t object_delay_samples = 1473;
|
||||
double tail_seconds = 5.0;
|
||||
double output_gain = 1.0;
|
||||
int chunk_frames = 64;
|
||||
int room_impulse_slots = 4096;
|
||||
};
|
||||
|
||||
class RosellaRuntime {
|
||||
public:
|
||||
RosellaRuntime();
|
||||
~RosellaRuntime();
|
||||
RosellaRuntime(const RosellaRuntime&) = delete;
|
||||
RosellaRuntime& operator=(const RosellaRuntime&) = delete;
|
||||
|
||||
Status open(const RosellaModel& model, const RosellaRenderOptions& options);
|
||||
|
||||
// objects16_planar is channel-major: channel * 1536 + sample.
|
||||
Status submit_frame(const float* objects16_planar, const oamd::OamdUpdate* update,
|
||||
std::int64_t frame_index, std::int64_t outer_sample_offset,
|
||||
std::int64_t object_delay_samples);
|
||||
|
||||
// Drains the flush tail: the pending partial chunk plus flush_samples of silence.
|
||||
Status finish(std::uint32_t flush_samples, std::vector<double>* out);
|
||||
std::uint32_t finish_capacity(double tail_seconds) const;
|
||||
|
||||
Status reset();
|
||||
|
||||
const std::vector<double>& output() const;
|
||||
void take_output(std::vector<double>* out);
|
||||
|
||||
std::uint64_t input_samples() const;
|
||||
std::uint64_t processed_input_samples() const;
|
||||
std::uint64_t metadata_block_updates() const;
|
||||
const timeline::OamdPositionTimeline& timeline() const;
|
||||
|
||||
// bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。
|
||||
void set_fixed_position(int object_index, const double position[3]);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace joc::hrtf
|
||||
@@ -1,241 +0,0 @@
|
||||
#include "io/adm_writer.h"
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
|
||||
#include "io/wav_writer.h" // pack_int24 (shared int24 quantisation)
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr long kDs64BodyOffset = 20;
|
||||
constexpr long kDataSizeOffset = 76;
|
||||
|
||||
void put_u16(std::string* out, std::uint16_t value) {
|
||||
char buffer[2];
|
||||
std::memcpy(buffer, &value, 2);
|
||||
out->append(buffer, 2);
|
||||
}
|
||||
|
||||
void put_u32(std::string* out, std::uint32_t value) {
|
||||
char buffer[4];
|
||||
std::memcpy(buffer, &value, 4);
|
||||
out->append(buffer, 4);
|
||||
}
|
||||
|
||||
void put_u64(std::string* out, std::uint64_t value) {
|
||||
char buffer[8];
|
||||
std::memcpy(buffer, &value, 8);
|
||||
out->append(buffer, 8);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
AdmBwfWriter::~AdmBwfWriter() { abort(); }
|
||||
|
||||
Status AdmBwfWriter::open(const std::string& path, std::size_t block_samples) {
|
||||
if (block_samples < JOC_FRAME_SAMPLES) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
|
||||
"ADM block size must hold at least one E-AC-3 frame");
|
||||
}
|
||||
path_ = path;
|
||||
block_samples_ = block_samples;
|
||||
used_ = 0;
|
||||
frames_ = 0;
|
||||
finalized_ = false;
|
||||
buffer_.assign(block_samples * kChannels, 0.0f);
|
||||
|
||||
file_ = fs_utf8::fopen(path, "wb+");
|
||||
if (file_ == nullptr) {
|
||||
std::error_code ignored;
|
||||
const std::filesystem::path parent = std::filesystem::path(path).parent_path();
|
||||
if (!parent.empty()) {
|
||||
std::filesystem::create_directories(parent, ignored);
|
||||
}
|
||||
file_ = fs_utf8::fopen(path, "wb+");
|
||||
}
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput, "cannot open " + path);
|
||||
}
|
||||
std::string header;
|
||||
header.append("RF64", 4);
|
||||
put_u32(&header, 0xFFFFFFFFu);
|
||||
header.append("WAVE", 4);
|
||||
if (std::fwrite(header.data(), 1, header.size(), file_) != header.size()) {
|
||||
abort();
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "cannot write " + path);
|
||||
}
|
||||
Status status = write_chunk("ds64", std::string(28, '\0'));
|
||||
if (!status.ok()) {
|
||||
abort();
|
||||
return status;
|
||||
}
|
||||
std::string fmt;
|
||||
put_u16(&fmt, 1);
|
||||
put_u16(&fmt, static_cast<std::uint16_t>(kChannels));
|
||||
put_u32(&fmt, kRate);
|
||||
put_u32(&fmt, kRate * kChannels * 3u);
|
||||
put_u16(&fmt, static_cast<std::uint16_t>(kChannels * 3u));
|
||||
put_u16(&fmt, 24);
|
||||
status = write_chunk("fmt ", fmt);
|
||||
if (!status.ok()) {
|
||||
abort();
|
||||
return status;
|
||||
}
|
||||
status = write_chunk("data", std::string());
|
||||
if (!status.ok()) {
|
||||
abort();
|
||||
return status;
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status AdmBwfWriter::write_chunk(const char id[4], const std::string& body) {
|
||||
std::string header;
|
||||
header.append(id, 4);
|
||||
put_u32(&header, static_cast<std::uint32_t>(body.size()));
|
||||
if (std::fwrite(header.data(), 1, header.size(), file_) != header.size()) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "chunk header write failed");
|
||||
}
|
||||
if (!body.empty() &&
|
||||
std::fwrite(body.data(), 1, body.size(), file_) != body.size()) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "chunk body write failed");
|
||||
}
|
||||
if ((body.size() & 1u) != 0u) {
|
||||
const char pad = '\0';
|
||||
if (std::fwrite(&pad, 1, 1, file_) != 1) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "chunk padding write failed");
|
||||
}
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status AdmBwfWriter::flush() {
|
||||
if (used_ == 0) {
|
||||
return Status::success();
|
||||
}
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "ADM writer is not open");
|
||||
}
|
||||
packed_.clear();
|
||||
pack_int24(buffer_.data(), used_, kChannels, &packed_);
|
||||
if (std::fwrite(packed_.data(), 1, packed_.size(), file_) != packed_.size()) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "audio write failed for " + path_);
|
||||
}
|
||||
used_ = 0;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status AdmBwfWriter::write_objects16(const float* planar16) {
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "ADM writer is not open");
|
||||
}
|
||||
if (planar16 == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null frame");
|
||||
}
|
||||
std::size_t source = 0;
|
||||
while (source < JOC_FRAME_SAMPLES) {
|
||||
const std::size_t available = block_samples_ - used_;
|
||||
const std::size_t count =
|
||||
std::min(available, static_cast<std::size_t>(JOC_FRAME_SAMPLES) - source);
|
||||
float* target = buffer_.data() + used_ * kChannels;
|
||||
std::memset(target, 0, count * kChannels * sizeof(float));
|
||||
for (std::size_t sample = 0; sample < count; ++sample) {
|
||||
float* row = target + sample * kChannels;
|
||||
row[3] = planar16[0u * JOC_FRAME_SAMPLES + source + sample];
|
||||
for (std::size_t object = 0; object < 15u; ++object) {
|
||||
row[10u + object] =
|
||||
planar16[(object + 1u) * JOC_FRAME_SAMPLES + source + sample];
|
||||
}
|
||||
}
|
||||
used_ += count;
|
||||
source += count;
|
||||
if (used_ == block_samples_) {
|
||||
const Status status = flush();
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
frames_ += JOC_FRAME_SAMPLES;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status AdmBwfWriter::finalize(const std::string& axml, const std::string& chna,
|
||||
const std::string& dbmd) {
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "ADM writer is not open");
|
||||
}
|
||||
if (finalized_) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "ADM writer already finalized");
|
||||
}
|
||||
Status status = flush();
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
status = write_chunk("axml", axml);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
status = write_chunk("chna", chna);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
status = write_chunk("dbmd", dbmd);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (std::fseek(file_, 0, SEEK_END) != 0) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "seek failed for " + path_);
|
||||
}
|
||||
const long long total = std::ftell(file_);
|
||||
if (total < 0) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "tell failed for " + path_);
|
||||
}
|
||||
const std::uint64_t data_len = frames_ * kChannels * 3u;
|
||||
const std::uint32_t data_field =
|
||||
data_len <= 0xFFFFFFFFull ? static_cast<std::uint32_t>(data_len) : 0xFFFFFFFFu;
|
||||
|
||||
if (std::fseek(file_, kDataSizeOffset, SEEK_SET) != 0) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "seek failed for " + path_);
|
||||
}
|
||||
char buffer[4];
|
||||
std::memcpy(buffer, &data_field, 4);
|
||||
if (std::fwrite(buffer, 1, 4, file_) != 4) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "data size patch failed");
|
||||
}
|
||||
|
||||
std::string ds64;
|
||||
put_u64(&ds64, static_cast<std::uint64_t>(total) - 8u);
|
||||
put_u64(&ds64, data_len);
|
||||
put_u64(&ds64, frames_);
|
||||
put_u32(&ds64, 0);
|
||||
if (std::fseek(file_, kDs64BodyOffset, SEEK_SET) != 0) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "seek failed for " + path_);
|
||||
}
|
||||
if (std::fwrite(ds64.data(), 1, ds64.size(), file_) != ds64.size()) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "ds64 patch failed");
|
||||
}
|
||||
finalized_ = true;
|
||||
if (std::fclose(file_) != 0) {
|
||||
file_ = nullptr;
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "close failed for " + path_);
|
||||
}
|
||||
file_ = nullptr;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
void AdmBwfWriter::abort() {
|
||||
if (file_ != nullptr) {
|
||||
std::fclose(file_);
|
||||
file_ = nullptr;
|
||||
}
|
||||
if (!finalized_ && !path_.empty()) {
|
||||
fs_utf8::remove(path_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,52 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
class AdmBwfWriter {
|
||||
public:
|
||||
static constexpr std::uint32_t kChannels = 25;
|
||||
static constexpr std::uint32_t kRate = 48000;
|
||||
static constexpr std::size_t kDefaultBlockSamples = 131072;
|
||||
|
||||
AdmBwfWriter() = default;
|
||||
~AdmBwfWriter();
|
||||
|
||||
AdmBwfWriter(const AdmBwfWriter&) = delete;
|
||||
AdmBwfWriter& operator=(const AdmBwfWriter&) = delete;
|
||||
|
||||
Status open(const std::string& path, std::size_t block_samples = kDefaultBlockSamples);
|
||||
|
||||
Status write_objects16(const float* planar16);
|
||||
|
||||
Status finalize(const std::string& axml, const std::string& chna, const std::string& dbmd);
|
||||
|
||||
// Closes and removes a file that was never finalized (plan 28.3: abort must
|
||||
void abort();
|
||||
|
||||
std::uint64_t frames() const { return frames_; }
|
||||
bool open_ok() const { return file_ != nullptr; }
|
||||
|
||||
private:
|
||||
Status write_chunk(const char id[4], const std::string& body);
|
||||
Status flush();
|
||||
|
||||
std::FILE* file_ = nullptr;
|
||||
std::string path_;
|
||||
std::size_t block_samples_ = kDefaultBlockSamples;
|
||||
std::size_t used_ = 0;
|
||||
std::uint64_t frames_ = 0;
|
||||
std::vector<float> buffer_;
|
||||
std::string packed_;
|
||||
bool finalized_ = false;
|
||||
};
|
||||
|
||||
} // namespace joc::io
|
||||
-1430
File diff suppressed because it is too large.
Load diff
@@ -1,87 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
// Read-only subset of the HDF5 file format, sized for the SOFA files this
|
||||
// project consumes: superblock v0, version 2 object headers, fractal-heap link
|
||||
// and attribute storage, compact and contiguous datasets. The file is opened
|
||||
// lazily: only the requested dataset's bytes are read into memory, everything
|
||||
// else (superblock, object headers, heap blocks) is fetched on demand and the
|
||||
// metadata that was parsed is cached by file address.
|
||||
//
|
||||
// Paths are HDF5 link paths ("Data.IR" is a single link name here, "Group/Set"
|
||||
// walks two links); the empty path names the root group. Byte order is
|
||||
// normalized on read, so callers never see the file's own endianness.
|
||||
namespace joc::io {
|
||||
|
||||
enum class Hdf5Type {
|
||||
Unknown,
|
||||
Int8,
|
||||
Int16,
|
||||
Int32,
|
||||
Int64,
|
||||
UInt8,
|
||||
UInt16,
|
||||
UInt32,
|
||||
UInt64,
|
||||
Float32,
|
||||
Float64,
|
||||
String,
|
||||
};
|
||||
|
||||
struct Hdf5TypeInfo {
|
||||
Hdf5Type type = Hdf5Type::Unknown;
|
||||
std::uint32_t size = 0; // bytes per element as stored in the file
|
||||
bool big_endian = false;
|
||||
bool is_signed = false;
|
||||
};
|
||||
|
||||
struct Hdf5DatasetInfo {
|
||||
std::vector<std::uint64_t> shape;
|
||||
Hdf5TypeInfo type;
|
||||
|
||||
std::uint64_t element_count() const;
|
||||
};
|
||||
|
||||
struct Hdf5Attribute {
|
||||
Hdf5TypeInfo type;
|
||||
std::vector<std::uint64_t> shape;
|
||||
std::vector<std::uint8_t> raw; // C order, host byte order
|
||||
std::string text; // decoded for fixed-length string attributes
|
||||
};
|
||||
|
||||
class Hdf5File {
|
||||
public:
|
||||
Hdf5File();
|
||||
~Hdf5File();
|
||||
Hdf5File(Hdf5File&&) noexcept;
|
||||
Hdf5File& operator=(Hdf5File&&) noexcept;
|
||||
Hdf5File(const Hdf5File&) = delete;
|
||||
Hdf5File& operator=(const Hdf5File&) = delete;
|
||||
|
||||
Status open(const std::string& path);
|
||||
bool is_open() const;
|
||||
|
||||
// Names of the links of a group ("" is the root group).
|
||||
Status links(const std::string& group_path, std::vector<std::string>* names) const;
|
||||
|
||||
bool has_dataset(const std::string& path) const;
|
||||
Status dataset_info(const std::string& path, Hdf5DatasetInfo* out) const;
|
||||
Status read_dataset_raw(const std::string& path, std::vector<std::uint8_t>* out) const;
|
||||
Status read_dataset_double(const std::string& path, std::vector<double>* out) const;
|
||||
|
||||
Status attribute_names(const std::string& object_path, std::vector<std::string>* names) const;
|
||||
Status attribute(const std::string& object_path, const std::string& name, Hdf5Attribute* out) const;
|
||||
Status attribute_text(const std::string& object_path, const std::string& name, std::string* out) const;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,276 +0,0 @@
|
||||
#include "io/inflate.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
class LsbBitReader {
|
||||
public:
|
||||
LsbBitReader(const std::uint8_t* data, std::size_t size) : data_(data), size_(size) {}
|
||||
|
||||
bool ok() const { return ok_; }
|
||||
std::size_t byte_position() const { return position_ >> 3; }
|
||||
|
||||
std::uint32_t bits(unsigned count) {
|
||||
std::uint32_t value = 0;
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
if ((position_ >> 3) >= size_) {
|
||||
ok_ = false;
|
||||
return value;
|
||||
}
|
||||
const std::uint32_t bit = (data_[position_ >> 3] >> (position_ & 7u)) & 1u;
|
||||
value |= bit << i;
|
||||
++position_;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void align_to_byte() { position_ = (position_ + 7u) & ~static_cast<std::size_t>(7u); }
|
||||
|
||||
void skip_bytes(std::size_t count) { position_ += count * 8u; }
|
||||
|
||||
private:
|
||||
const std::uint8_t* data_;
|
||||
std::size_t size_;
|
||||
std::size_t position_ = 0;
|
||||
bool ok_ = true;
|
||||
};
|
||||
|
||||
struct Huffman {
|
||||
std::uint16_t counts[16] = {};
|
||||
std::uint16_t symbols[288] = {};
|
||||
int max_length = 0;
|
||||
|
||||
bool build(const std::uint8_t* lengths, int count) {
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
counts[i] = 0;
|
||||
}
|
||||
for (int i = 0; i < count; ++i) {
|
||||
counts[lengths[i]]++;
|
||||
}
|
||||
counts[0] = 0;
|
||||
std::uint16_t offsets[16] = {};
|
||||
std::uint16_t total = 0;
|
||||
for (int length = 1; length < 16; ++length) {
|
||||
offsets[length] = total;
|
||||
total = static_cast<std::uint16_t>(total + counts[length]);
|
||||
}
|
||||
if (total == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int symbol = 0; symbol < count; ++symbol) {
|
||||
const std::uint8_t length = lengths[symbol];
|
||||
if (length != 0) {
|
||||
symbols[offsets[length]++] = static_cast<std::uint16_t>(symbol);
|
||||
}
|
||||
}
|
||||
max_length = 15;
|
||||
while (max_length > 0 && counts[max_length] == 0) {
|
||||
--max_length;
|
||||
}
|
||||
return max_length != 0;
|
||||
}
|
||||
|
||||
int decode(LsbBitReader* reader) const {
|
||||
int code = 0;
|
||||
int first = 0;
|
||||
int index = 0;
|
||||
for (int length = 1; length <= max_length; ++length) {
|
||||
code |= static_cast<int>(reader->bits(1));
|
||||
if (!reader->ok()) {
|
||||
return -1;
|
||||
}
|
||||
const int count = counts[length];
|
||||
if (code - first < count) {
|
||||
return symbols[index + (code - first)];
|
||||
}
|
||||
index += count;
|
||||
first = (first + count) << 1;
|
||||
code <<= 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr std::uint16_t kLengthBase[29] = {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};
|
||||
constexpr std::uint8_t kLengthExtra[29] = {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};
|
||||
constexpr std::uint16_t kDistanceBase[30] = {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};
|
||||
constexpr std::uint8_t kDistanceExtra[30] = {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};
|
||||
constexpr std::uint8_t kCodeLengthOrder[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2,
|
||||
14, 1, 15};
|
||||
|
||||
bool inflate_block_data(LsbBitReader* reader, const Huffman& literal, const Huffman& distance,
|
||||
std::vector<std::uint8_t>* out) {
|
||||
for (;;) {
|
||||
const int symbol = literal.decode(reader);
|
||||
if (symbol < 0) {
|
||||
return false;
|
||||
}
|
||||
if (symbol < 256) {
|
||||
out->push_back(static_cast<std::uint8_t>(symbol));
|
||||
continue;
|
||||
}
|
||||
if (symbol == 256) {
|
||||
return true;
|
||||
}
|
||||
const int length_index = symbol - 257;
|
||||
if (length_index >= 29) {
|
||||
return false;
|
||||
}
|
||||
const std::uint32_t length =
|
||||
kLengthBase[length_index] + reader->bits(kLengthExtra[length_index]);
|
||||
const int distance_symbol = distance.decode(reader);
|
||||
if (distance_symbol < 0 || distance_symbol >= 30) {
|
||||
return false;
|
||||
}
|
||||
const std::uint32_t distance_value =
|
||||
kDistanceBase[distance_symbol] + reader->bits(kDistanceExtra[distance_symbol]);
|
||||
if (!reader->ok() || distance_value == 0 || distance_value > out->size()) {
|
||||
return false;
|
||||
}
|
||||
const std::size_t start = out->size() - distance_value;
|
||||
for (std::uint32_t i = 0; i < length; ++i) {
|
||||
out->push_back((*out)[start + i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool inflate_fixed(LsbBitReader* reader, std::vector<std::uint8_t>* out) {
|
||||
std::uint8_t lengths[288];
|
||||
for (int i = 0; i < 144; ++i) { lengths[i] = 8; }
|
||||
for (int i = 144; i < 256; ++i) { lengths[i] = 9; }
|
||||
for (int i = 256; i < 280; ++i) { lengths[i] = 7; }
|
||||
for (int i = 280; i < 288; ++i) { lengths[i] = 8; }
|
||||
Huffman literal;
|
||||
if (!literal.build(lengths, 288)) {
|
||||
return false;
|
||||
}
|
||||
std::uint8_t distance_lengths[30];
|
||||
for (int i = 0; i < 30; ++i) { distance_lengths[i] = 5; }
|
||||
Huffman distance;
|
||||
if (!distance.build(distance_lengths, 30)) {
|
||||
return false;
|
||||
}
|
||||
return inflate_block_data(reader, literal, distance, out);
|
||||
}
|
||||
|
||||
bool inflate_dynamic(LsbBitReader* reader, std::vector<std::uint8_t>* out) {
|
||||
const int literal_count = static_cast<int>(reader->bits(5)) + 257;
|
||||
const int distance_count = static_cast<int>(reader->bits(5)) + 1;
|
||||
const int code_length_count = static_cast<int>(reader->bits(4)) + 4;
|
||||
if (!reader->ok() || literal_count > 286 || distance_count > 30) {
|
||||
return false;
|
||||
}
|
||||
std::uint8_t code_lengths[19] = {};
|
||||
for (int i = 0; i < code_length_count; ++i) {
|
||||
code_lengths[kCodeLengthOrder[i]] = static_cast<std::uint8_t>(reader->bits(3));
|
||||
}
|
||||
if (!reader->ok()) {
|
||||
return false;
|
||||
}
|
||||
Huffman code_length_tree;
|
||||
if (!code_length_tree.build(code_lengths, 19)) {
|
||||
return false;
|
||||
}
|
||||
std::uint8_t lengths[288 + 30] = {};
|
||||
const int total = literal_count + distance_count;
|
||||
int index = 0;
|
||||
while (index < total) {
|
||||
const int symbol = code_length_tree.decode(reader);
|
||||
if (symbol < 0) {
|
||||
return false;
|
||||
}
|
||||
if (symbol < 16) {
|
||||
lengths[index++] = static_cast<std::uint8_t>(symbol);
|
||||
continue;
|
||||
}
|
||||
int repeat = 0;
|
||||
std::uint8_t value = 0;
|
||||
if (symbol == 16) {
|
||||
if (index == 0) {
|
||||
return false;
|
||||
}
|
||||
value = lengths[index - 1];
|
||||
repeat = 3 + static_cast<int>(reader->bits(2));
|
||||
} else if (symbol == 17) {
|
||||
repeat = 3 + static_cast<int>(reader->bits(3));
|
||||
} else {
|
||||
repeat = 11 + static_cast<int>(reader->bits(7));
|
||||
}
|
||||
if (!reader->ok() || index + repeat > total) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < repeat; ++i) {
|
||||
lengths[index++] = value;
|
||||
}
|
||||
}
|
||||
Huffman literal;
|
||||
if (!literal.build(lengths, literal_count)) {
|
||||
return false;
|
||||
}
|
||||
Huffman distance;
|
||||
if (!distance.build(lengths + literal_count, distance_count)) {
|
||||
return false;
|
||||
}
|
||||
return inflate_block_data(reader, literal, distance, out);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool inflate_raw(const std::uint8_t* data, std::size_t size, std::vector<std::uint8_t>* out) {
|
||||
if (data == nullptr || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
out->clear();
|
||||
LsbBitReader reader(data, size);
|
||||
for (;;) {
|
||||
const std::uint32_t final_block = reader.bits(1);
|
||||
const std::uint32_t type = reader.bits(2);
|
||||
if (!reader.ok()) {
|
||||
return false;
|
||||
}
|
||||
if (type == 0) {
|
||||
reader.align_to_byte();
|
||||
const std::size_t position = reader.byte_position();
|
||||
if (position + 4 > size) {
|
||||
return false;
|
||||
}
|
||||
const std::uint16_t length = static_cast<std::uint16_t>(data[position] | (data[position + 1] << 8));
|
||||
const std::uint16_t complement =
|
||||
static_cast<std::uint16_t>(data[position + 2] | (data[position + 3] << 8));
|
||||
if (static_cast<std::uint16_t>(length ^ 0xFFFFu) != complement) {
|
||||
return false;
|
||||
}
|
||||
if (position + 4 + length > size) {
|
||||
return false;
|
||||
}
|
||||
out->insert(out->end(), data + position + 4, data + position + 4 + length);
|
||||
reader.skip_bytes(4u + length);
|
||||
} else if (type == 1) {
|
||||
if (!inflate_fixed(&reader, out)) {
|
||||
return false;
|
||||
}
|
||||
} else if (type == 2) {
|
||||
if (!inflate_dynamic(&reader, out)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (final_block != 0u) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
bool inflate_raw(const std::uint8_t* data, std::size_t size, std::vector<std::uint8_t>* out);
|
||||
|
||||
} // namespace joc::io
|
||||
-420
@@ -1,420 +0,0 @@
|
||||
#include "io/npy.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint16_t read_u16(const std::uint8_t* p) { return static_cast<std::uint16_t>(p[0] | (p[1] << 8)); }
|
||||
std::uint32_t read_u32(const std::uint8_t* p) {
|
||||
return static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8) |
|
||||
(static_cast<std::uint32_t>(p[2]) << 16) | (static_cast<std::uint32_t>(p[3]) << 24);
|
||||
}
|
||||
|
||||
NpyType classify(const std::string& descr) {
|
||||
if (descr == "<f8" || descr == "=f8" || descr == "|f8") { return NpyType::Float64; }
|
||||
if (descr == "<f4" || descr == "=f4") { return NpyType::Float32; }
|
||||
if (descr == "<i8" || descr == "=i8") { return NpyType::Int64; }
|
||||
if (descr == "<i4" || descr == "=i4") { return NpyType::Int32; }
|
||||
if (descr == "<i2" || descr == "=i2") { return NpyType::Int16; }
|
||||
if (descr == "|u1" || descr == "<u1") { return NpyType::UInt8; }
|
||||
if (descr == "<c16" || descr == "=c16") { return NpyType::Complex128; }
|
||||
if (descr.size() > 2 && descr[0] == '<' && descr[1] == 'U') {
|
||||
return NpyType::Unicode;
|
||||
}
|
||||
if (descr.size() > 2 && descr[0] == '=' && descr[1] == 'U') {
|
||||
return NpyType::Unicode;
|
||||
}
|
||||
return NpyType::Unknown;
|
||||
}
|
||||
|
||||
std::size_t unicode_length(const std::string& descr) {
|
||||
std::size_t index = 0;
|
||||
while (index < descr.size() && (descr[index] == '<' || descr[index] == '=')) {
|
||||
++index;
|
||||
}
|
||||
if (index >= descr.size() || descr[index] != 'U') {
|
||||
return 0;
|
||||
}
|
||||
++index;
|
||||
std::size_t value = 0;
|
||||
bool any = false;
|
||||
while (index < descr.size() && descr[index] >= '0' && descr[index] <= '9') {
|
||||
value = value * 10 + static_cast<std::size_t>(descr[index] - '0');
|
||||
++index;
|
||||
any = true;
|
||||
}
|
||||
return any ? value : 0;
|
||||
}
|
||||
|
||||
bool is_big_endian(const std::string& descr) { return !descr.empty() && descr[0] == '>'; }
|
||||
|
||||
bool header_value(const std::string& header, const std::string& key, std::string* out) {
|
||||
const std::string needle = "'" + key + "'";
|
||||
const std::size_t position = header.find(needle);
|
||||
if (position == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
const std::size_t colon = header.find(':', position + needle.size());
|
||||
if (colon == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::size_t start = colon + 1;
|
||||
while (start < header.size() && (header[start] == ' ' || header[start] == '\t')) {
|
||||
++start;
|
||||
}
|
||||
*out = header.substr(start);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::size_t NpyArray::element_count() const {
|
||||
std::size_t count = 1;
|
||||
for (const std::int64_t dimension : shape) {
|
||||
count *= static_cast<std::size_t>(dimension < 0 ? 0 : dimension);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
std::size_t NpyArray::element_size() const {
|
||||
switch (type) {
|
||||
case NpyType::Float64: return 8;
|
||||
case NpyType::Float32: return 4;
|
||||
case NpyType::Int64: return 8;
|
||||
case NpyType::Int32: return 4;
|
||||
case NpyType::Int16: return 2;
|
||||
case NpyType::UInt8: return 1;
|
||||
case NpyType::Complex128: return 16;
|
||||
case NpyType::Unicode: return item_bytes;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_npy(const std::uint8_t* data, std::size_t size, NpyArray* out, std::string* error) {
|
||||
if (data == nullptr || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const std::uint8_t magic[6] = {0x93u, 'N', 'U', 'M', 'P', 'Y'};
|
||||
if (size < 10u || std::memcmp(data, magic, 6) != 0) {
|
||||
if (error != nullptr) { *error = "not a .npy image"; }
|
||||
return false;
|
||||
}
|
||||
const std::uint8_t major = data[6];
|
||||
std::size_t header_length = 0;
|
||||
std::size_t header_offset = 0;
|
||||
if (major == 1u) {
|
||||
header_length = read_u16(data + 8);
|
||||
header_offset = 10;
|
||||
} else if (major == 2u || major == 3u) {
|
||||
if (size < 12u) {
|
||||
if (error != nullptr) { *error = "truncated .npy v2 header"; }
|
||||
return false;
|
||||
}
|
||||
header_length = read_u32(data + 8);
|
||||
header_offset = 12;
|
||||
} else {
|
||||
if (error != nullptr) { *error = "unsupported .npy version " + std::to_string(major); }
|
||||
return false;
|
||||
}
|
||||
if (header_offset + header_length > size) {
|
||||
if (error != nullptr) { *error = "truncated .npy header"; }
|
||||
return false;
|
||||
}
|
||||
const std::string header(reinterpret_cast<const char*>(data + header_offset), header_length);
|
||||
|
||||
out->descr.clear();
|
||||
std::string value;
|
||||
if (!header_value(header, "descr", &value)) {
|
||||
if (error != nullptr) { *error = ".npy header without descr"; }
|
||||
return false;
|
||||
}
|
||||
const std::size_t first_quote = value.find('\'');
|
||||
const std::size_t second_quote =
|
||||
first_quote == std::string::npos ? std::string::npos : value.find('\'', first_quote + 1);
|
||||
if (first_quote == std::string::npos || second_quote == std::string::npos) {
|
||||
if (error != nullptr) { *error = ".npy descr is not a quoted string"; }
|
||||
return false;
|
||||
}
|
||||
out->descr = value.substr(first_quote + 1, second_quote - first_quote - 1);
|
||||
out->type = classify(out->descr);
|
||||
if (out->type == NpyType::Unknown) {
|
||||
if (error != nullptr) { *error = "unsupported .npy dtype " + out->descr; }
|
||||
return false;
|
||||
}
|
||||
out->item_bytes = 0;
|
||||
if (out->type == NpyType::Unicode) {
|
||||
const std::size_t length = unicode_length(out->descr);
|
||||
if (length == 0) {
|
||||
if (error != nullptr) { *error = "malformed unicode .npy dtype " + out->descr; }
|
||||
return false;
|
||||
}
|
||||
out->item_bytes = length * 4u;
|
||||
}
|
||||
|
||||
out->fortran_order = header.find("'fortran_order': True") != std::string::npos;
|
||||
|
||||
if (!header_value(header, "shape", &value)) {
|
||||
if (error != nullptr) { *error = ".npy header without shape"; }
|
||||
return false;
|
||||
}
|
||||
out->shape.clear();
|
||||
for (std::size_t i = 0; i < value.size(); ++i) {
|
||||
if (value[i] >= '0' && value[i] <= '9') {
|
||||
long long dimension = 0;
|
||||
while (i < value.size() && value[i] >= '0' && value[i] <= '9') {
|
||||
dimension = dimension * 10 + (value[i] - '0');
|
||||
++i;
|
||||
}
|
||||
out->shape.push_back(dimension);
|
||||
} else if (value[i] == ')') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const std::size_t expected = out->element_count() * out->element_size();
|
||||
if (header_offset + header_length + expected > size) {
|
||||
if (error != nullptr) {
|
||||
*error = ".npy payload truncated (need " + std::to_string(expected) + " bytes)";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
out->data = data + header_offset + header_length;
|
||||
out->data_bytes = expected;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool npy_shape_is(const NpyArray& array, const std::vector<std::int64_t>& expected) {
|
||||
return array.shape == expected;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T>
|
||||
void load_le(const std::uint8_t* source, std::size_t count, bool swap, std::vector<T>* out) {
|
||||
out->resize(count);
|
||||
std::memcpy(out->data(), source, count * sizeof(T));
|
||||
if (swap) {
|
||||
std::uint8_t* bytes = reinterpret_cast<std::uint8_t*>(out->data());
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
for (std::size_t b = 0; b < sizeof(T) / 2; ++b) {
|
||||
const std::uint8_t temporary = bytes[i * sizeof(T) + b];
|
||||
bytes[i * sizeof(T) + b] = bytes[i * sizeof(T) + sizeof(T) - 1 - b];
|
||||
bytes[i * sizeof(T) + sizeof(T) - 1 - b] = temporary;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool npy_to_double(const NpyArray& array, std::vector<double>* out, std::string* error) {
|
||||
const bool swap = is_big_endian(array.descr);
|
||||
const std::size_t count = array.element_count();
|
||||
switch (array.type) {
|
||||
case NpyType::Float64:
|
||||
load_le(array.data, count, swap, out);
|
||||
return true;
|
||||
case NpyType::Complex128:
|
||||
load_le(array.data, count * 2u, swap, out);
|
||||
return true;
|
||||
case NpyType::Float32: {
|
||||
std::vector<float> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<double>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::Int64: {
|
||||
std::vector<std::int64_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<double>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::Int32: {
|
||||
std::vector<std::int32_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<double>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::Int16: {
|
||||
std::vector<std::int16_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<double>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::UInt8: {
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<double>(array.data[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
if (error != nullptr) { *error = "cannot convert " + array.descr + " to double"; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool npy_to_int16(const NpyArray& array, std::vector<std::int16_t>* out, std::string* error) {
|
||||
const bool swap = is_big_endian(array.descr);
|
||||
const std::size_t count = array.element_count();
|
||||
switch (array.type) {
|
||||
case NpyType::Int16:
|
||||
load_le(array.data, count, swap, out);
|
||||
return true;
|
||||
case NpyType::Int32: {
|
||||
std::vector<std::int32_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<std::int16_t>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::Int64: {
|
||||
std::vector<std::int64_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<std::int16_t>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
if (error != nullptr) { *error = "cannot convert " + array.descr + " to int16"; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool npy_to_int32(const NpyArray& array, std::vector<std::int32_t>* out, std::string* error) {
|
||||
const bool swap = is_big_endian(array.descr);
|
||||
const std::size_t count = array.element_count();
|
||||
switch (array.type) {
|
||||
case NpyType::Int32:
|
||||
load_le(array.data, count, swap, out);
|
||||
return true;
|
||||
case NpyType::Int64: {
|
||||
std::vector<std::int64_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<std::int32_t>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case NpyType::Int16: {
|
||||
std::vector<std::int16_t> values;
|
||||
load_le(array.data, count, swap, &values);
|
||||
out->resize(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
(*out)[i] = static_cast<std::int32_t>(values[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
if (error != nullptr) { *error = "cannot convert " + array.descr + " to int32"; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool npy_to_uint8(const NpyArray& array, std::vector<std::uint8_t>* out, std::string* error) {
|
||||
if (array.type != NpyType::UInt8) {
|
||||
if (error != nullptr) { *error = "cannot convert " + array.descr + " to uint8"; }
|
||||
return false;
|
||||
}
|
||||
out->assign(array.data, array.data + array.element_count());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool npy_unicode_to_utf8(const NpyArray& array, std::string* out, std::string* error) {
|
||||
if (array.type != NpyType::Unicode) {
|
||||
if (error != nullptr) { *error = "not a unicode .npy member: " + array.descr; }
|
||||
return false;
|
||||
}
|
||||
if (array.shape.size() != 0) {
|
||||
if (error != nullptr) { *error = "unicode .npy member must be a scalar"; }
|
||||
return false;
|
||||
}
|
||||
out->clear();
|
||||
const std::size_t count = array.item_bytes / 4u;
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
const std::uint8_t* p = array.data + i * 4u;
|
||||
const std::uint32_t code = static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8) |
|
||||
(static_cast<std::uint32_t>(p[2]) << 16) |
|
||||
(static_cast<std::uint32_t>(p[3]) << 24);
|
||||
if (code == 0u) {
|
||||
break;
|
||||
}
|
||||
if (code < 0x80u) {
|
||||
out->push_back(static_cast<char>(code));
|
||||
} else if (code < 0x800u) {
|
||||
out->push_back(static_cast<char>(0xC0u | (code >> 6)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
} else if (code < 0x10000u) {
|
||||
out->push_back(static_cast<char>(0xE0u | (code >> 12)));
|
||||
out->push_back(static_cast<char>(0x80u | ((code >> 6) & 0x3Fu)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
} else {
|
||||
out->push_back(static_cast<char>(0xF0u | (code >> 18)));
|
||||
out->push_back(static_cast<char>(0x80u | ((code >> 12) & 0x3Fu)));
|
||||
out->push_back(static_cast<char>(0x80u | ((code >> 6) & 0x3Fu)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool npy_to_c_order(const NpyArray& array, std::vector<std::uint8_t>* out, std::string* error) {
|
||||
const std::size_t element = array.element_size();
|
||||
if (element == 0) {
|
||||
if (error != nullptr) { *error = "unsupported element size for " + array.descr; }
|
||||
return false;
|
||||
}
|
||||
if (!array.fortran_order) {
|
||||
out->assign(array.data, array.data + array.data_bytes);
|
||||
return true;
|
||||
}
|
||||
const std::size_t dimensions = array.shape.size();
|
||||
if (dimensions == 0) {
|
||||
out->assign(array.data, array.data + element);
|
||||
return true;
|
||||
}
|
||||
// Source (Fortran) strides in elements; destination is C order.
|
||||
std::vector<std::size_t> source_stride(dimensions, 1);
|
||||
std::size_t running = 1;
|
||||
for (std::size_t d = 0; d < dimensions; ++d) {
|
||||
source_stride[d] = running;
|
||||
running *= static_cast<std::size_t>(array.shape[d]);
|
||||
}
|
||||
out->assign(array.data_bytes, 0);
|
||||
std::vector<std::size_t> index(dimensions, 0);
|
||||
const std::size_t total = array.element_count();
|
||||
for (std::size_t linear = 0; linear < total; ++linear) {
|
||||
std::size_t remainder = linear;
|
||||
for (std::size_t d = dimensions; d-- > 0;) {
|
||||
index[d] = remainder % static_cast<std::size_t>(array.shape[d]);
|
||||
remainder /= static_cast<std::size_t>(array.shape[d]);
|
||||
}
|
||||
std::size_t source = 0;
|
||||
for (std::size_t d = 0; d < dimensions; ++d) {
|
||||
source += index[d] * source_stride[d];
|
||||
}
|
||||
std::memcpy(out->data() + linear * element, array.data + source * element, element);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,42 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
enum class NpyType { Unknown, Float64, Float32, Int64, Int32, Int16, UInt8, Complex128, Unicode };
|
||||
|
||||
struct NpyArray {
|
||||
std::string descr;
|
||||
NpyType type = NpyType::Unknown;
|
||||
bool fortran_order = false;
|
||||
std::vector<std::int64_t> shape;
|
||||
const std::uint8_t* data = nullptr;
|
||||
std::size_t data_bytes = 0;
|
||||
std::size_t item_bytes = 0; // bytes per element as stored
|
||||
|
||||
std::size_t element_count() const;
|
||||
std::size_t element_size() const; // bytes per element in the file
|
||||
};
|
||||
|
||||
// Parses the header of one `.npy` image. `data` must outlive the NpyArray.
|
||||
bool parse_npy(const std::uint8_t* data, std::size_t size, NpyArray* out, std::string* error);
|
||||
|
||||
bool npy_to_double(const NpyArray& array, std::vector<double>* out, std::string* error);
|
||||
bool npy_to_int16(const NpyArray& array, std::vector<std::int16_t>* out, std::string* error);
|
||||
bool npy_to_int32(const NpyArray& array, std::vector<std::int32_t>* out, std::string* error);
|
||||
bool npy_to_uint8(const NpyArray& array, std::vector<std::uint8_t>* out, std::string* error);
|
||||
|
||||
bool npy_unicode_to_utf8(const NpyArray& array, std::string* out, std::string* error);
|
||||
|
||||
// Materializes the array in C order as raw element bytes. Fortran-order members
|
||||
// hybrid synthesis table as [count][4] row-major while the shipped table stores it
|
||||
// Fortran-order, so passing the file bytes straight through would transpose it.
|
||||
bool npy_to_c_order(const NpyArray& array, std::vector<std::uint8_t>* out, std::string* error);
|
||||
|
||||
bool npy_shape_is(const NpyArray& array, const std::vector<std::int64_t>& expected);
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,263 +0,0 @@
|
||||
#include "io/npy_writer.h"
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "foundation/fs_utf8.h"
|
||||
#include "io/zip_reader.h"
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t kNpyHeaderAlignment = 64;
|
||||
|
||||
void append_u16(std::vector<std::uint8_t>* out, std::uint16_t value) {
|
||||
out->push_back(static_cast<std::uint8_t>(value & 0xFFu));
|
||||
out->push_back(static_cast<std::uint8_t>((value >> 8) & 0xFFu));
|
||||
}
|
||||
|
||||
void append_u32(std::vector<std::uint8_t>* out, std::uint32_t value) {
|
||||
for (int index = 0; index < 4; ++index) {
|
||||
out->push_back(static_cast<std::uint8_t>((value >> (8 * index)) & 0xFFu));
|
||||
}
|
||||
}
|
||||
|
||||
void append_bytes(std::vector<std::uint8_t>* out, const void* data, std::size_t size) {
|
||||
const std::uint8_t* bytes = static_cast<const std::uint8_t*>(data);
|
||||
out->insert(out->end(), bytes, bytes + size);
|
||||
}
|
||||
|
||||
std::string shape_literal(const std::vector<std::uint64_t>& shape) {
|
||||
if (shape.empty()) {
|
||||
return "()";
|
||||
}
|
||||
std::string text = "(";
|
||||
for (std::size_t index = 0; index < shape.size(); ++index) {
|
||||
if (index != 0u) {
|
||||
text += ", ";
|
||||
}
|
||||
text += std::to_string(shape[index]);
|
||||
}
|
||||
if (shape.size() == 1u) {
|
||||
text += ",";
|
||||
}
|
||||
text += ")";
|
||||
return text;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<std::uint8_t> npy_image(const std::string& descr,
|
||||
const std::vector<std::uint64_t>& shape,
|
||||
const std::vector<std::uint8_t>& data) {
|
||||
std::string header = "{'descr': '" + descr + "', 'fortran_order': False, 'shape': " +
|
||||
shape_literal(shape) + ", }";
|
||||
// NumPy pads the header so that the payload starts on a 64-byte boundary.
|
||||
const std::size_t preamble = 10u; // magic, version, two byte header length
|
||||
std::size_t total = preamble + header.size() + 1u;
|
||||
const std::size_t padding = (kNpyHeaderAlignment - (total % kNpyHeaderAlignment)) %
|
||||
kNpyHeaderAlignment;
|
||||
header.append(padding, ' ');
|
||||
header.push_back('\n');
|
||||
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(preamble + header.size() + data.size());
|
||||
static const std::uint8_t kMagic[6] = {0x93u, 'N', 'U', 'M', 'P', 'Y'};
|
||||
append_bytes(&out, kMagic, sizeof(kMagic));
|
||||
out.push_back(1u); // major
|
||||
out.push_back(0u); // minor
|
||||
append_u16(&out, static_cast<std::uint16_t>(header.size()));
|
||||
append_bytes(&out, header.data(), header.size());
|
||||
append_bytes(&out, data.data(), data.size());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> zip_bytes(const std::vector<NpyMember>& members) {
|
||||
std::vector<std::uint8_t> out;
|
||||
struct Entry {
|
||||
std::string name;
|
||||
std::uint32_t crc = 0;
|
||||
std::uint32_t size = 0;
|
||||
std::uint32_t offset = 0;
|
||||
};
|
||||
std::vector<Entry> entries;
|
||||
entries.reserve(members.size());
|
||||
|
||||
for (const NpyMember& member : members) {
|
||||
const std::string name = member.name + ".npy";
|
||||
const std::vector<std::uint8_t> payload = npy_image(member.descr, member.shape, member.data);
|
||||
Entry entry;
|
||||
entry.name = name;
|
||||
entry.crc = crc32_of(payload.data(), payload.size());
|
||||
entry.size = static_cast<std::uint32_t>(payload.size());
|
||||
entry.offset = static_cast<std::uint32_t>(out.size());
|
||||
entries.push_back(entry);
|
||||
|
||||
append_u32(&out, 0x04034B50u); // local file header
|
||||
append_u16(&out, 20u); // version needed
|
||||
append_u16(&out, 0u); // flags
|
||||
append_u16(&out, 0u); // method: stored
|
||||
append_u16(&out, 0u); // time
|
||||
append_u16(&out, 0x2821u); // date: 2000-01-01, fixed for reproducibility
|
||||
append_u32(&out, entry.crc);
|
||||
append_u32(&out, entry.size);
|
||||
append_u32(&out, entry.size);
|
||||
append_u16(&out, static_cast<std::uint16_t>(name.size()));
|
||||
append_u16(&out, 0u); // extra length
|
||||
append_bytes(&out, name.data(), name.size());
|
||||
append_bytes(&out, payload.data(), payload.size());
|
||||
}
|
||||
|
||||
const std::uint32_t directory_offset = static_cast<std::uint32_t>(out.size());
|
||||
for (const Entry& entry : entries) {
|
||||
append_u32(&out, 0x02014B50u); // central directory header
|
||||
append_u16(&out, 20u); // version made by
|
||||
append_u16(&out, 20u); // version needed
|
||||
append_u16(&out, 0u); // flags
|
||||
append_u16(&out, 0u); // method: stored
|
||||
append_u16(&out, 0u); // time
|
||||
append_u16(&out, 0x2821u); // date
|
||||
append_u32(&out, entry.crc);
|
||||
append_u32(&out, entry.size);
|
||||
append_u32(&out, entry.size);
|
||||
append_u16(&out, static_cast<std::uint16_t>(entry.name.size()));
|
||||
append_u16(&out, 0u); // extra
|
||||
append_u16(&out, 0u); // comment
|
||||
append_u16(&out, 0u); // disk
|
||||
append_u16(&out, 0u); // internal attributes
|
||||
append_u32(&out, 0u); // external attributes
|
||||
append_u32(&out, entry.offset);
|
||||
append_bytes(&out, entry.name.data(), entry.name.size());
|
||||
}
|
||||
const std::uint32_t directory_size = static_cast<std::uint32_t>(out.size()) - directory_offset;
|
||||
|
||||
append_u32(&out, 0x06054B50u); // end of central directory
|
||||
append_u16(&out, 0u);
|
||||
append_u16(&out, 0u);
|
||||
append_u16(&out, static_cast<std::uint16_t>(entries.size()));
|
||||
append_u16(&out, static_cast<std::uint16_t>(entries.size()));
|
||||
append_u32(&out, directory_size);
|
||||
append_u32(&out, directory_offset);
|
||||
append_u16(&out, 0u);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool write_zip(const std::string& path, const std::vector<NpyMember>& members,
|
||||
std::string* error) {
|
||||
const std::vector<std::uint8_t> bytes = zip_bytes(members);
|
||||
std::FILE* stream = fs_utf8::fopen(path, "wb");
|
||||
if (stream == nullptr) {
|
||||
if (error != nullptr) {
|
||||
*error = "cannot open " + path + " for writing";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const std::size_t written = std::fwrite(bytes.data(), 1, bytes.size(), stream);
|
||||
const bool flushed = std::fclose(stream) == 0;
|
||||
if (written != bytes.size() || !flushed) {
|
||||
if (error != nullptr) {
|
||||
*error = "short write to " + path;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> utf8_to_utf32le(const std::string& text) {
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(text.size() * 4u);
|
||||
std::size_t index = 0;
|
||||
while (index < text.size()) {
|
||||
const std::uint8_t lead = static_cast<std::uint8_t>(text[index]);
|
||||
std::uint32_t code = 0;
|
||||
std::size_t extra = 0;
|
||||
if (lead < 0x80u) {
|
||||
code = lead;
|
||||
} else if ((lead & 0xE0u) == 0xC0u) {
|
||||
code = lead & 0x1Fu;
|
||||
extra = 1;
|
||||
} else if ((lead & 0xF0u) == 0xE0u) {
|
||||
code = lead & 0x0Fu;
|
||||
extra = 2;
|
||||
} else if ((lead & 0xF8u) == 0xF0u) {
|
||||
code = lead & 0x07u;
|
||||
extra = 3;
|
||||
} else {
|
||||
code = 0xFFFDu; // invalid lead byte: substitute rather than fail
|
||||
extra = 0;
|
||||
}
|
||||
++index;
|
||||
for (std::size_t count = 0; count < extra && index < text.size(); ++count) {
|
||||
code = (code << 6) | (static_cast<std::uint8_t>(text[index]) & 0x3Fu);
|
||||
++index;
|
||||
}
|
||||
for (int byte = 0; byte < 4; ++byte) {
|
||||
out.push_back(static_cast<std::uint8_t>((code >> (8 * byte)) & 0xFFu));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string python_float_repr(double value) {
|
||||
if (std::isnan(value)) {
|
||||
return "NaN";
|
||||
}
|
||||
if (std::isinf(value)) {
|
||||
return value > 0.0 ? "Infinity" : "-Infinity";
|
||||
}
|
||||
// to_chars gives the shortest round-trip digits; Python's repr uses the same
|
||||
// digits but its own notation, so the digits are re-laid-out here.
|
||||
std::array<char, 64> buffer{};
|
||||
const std::to_chars_result converted =
|
||||
std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
|
||||
std::string text(buffer.data(), converted.ptr);
|
||||
const bool negative = !text.empty() && text[0] == '-';
|
||||
const std::string body = negative ? text.substr(1) : text;
|
||||
const std::size_t exponent_at = body.find_first_of("eE");
|
||||
std::string digits = body;
|
||||
int exponent = 0;
|
||||
if (exponent_at != std::string::npos) {
|
||||
digits = body.substr(0, exponent_at);
|
||||
exponent = std::atoi(body.c_str() + exponent_at + 1);
|
||||
}
|
||||
const std::size_t point = digits.find('.');
|
||||
std::string mantissa = digits;
|
||||
if (point != std::string::npos) {
|
||||
mantissa = digits.substr(0, point) + digits.substr(point + 1);
|
||||
exponent += static_cast<int>(point) - 1;
|
||||
} else {
|
||||
exponent += static_cast<int>(digits.size()) - 1;
|
||||
}
|
||||
while (mantissa.size() > 1u && mantissa.back() == '0') {
|
||||
mantissa.pop_back();
|
||||
}
|
||||
// Python switches to exponent notation below 1e-4 and at 1e16 and above.
|
||||
std::string result;
|
||||
if (exponent < -4 || exponent >= 16) {
|
||||
result = mantissa.substr(0, 1);
|
||||
if (mantissa.size() > 1u) {
|
||||
result += "." + mantissa.substr(1);
|
||||
}
|
||||
char tail[16];
|
||||
std::snprintf(tail, sizeof(tail), "e%+03d", exponent);
|
||||
result += tail;
|
||||
} else if (exponent >= 0) {
|
||||
if (static_cast<std::size_t>(exponent) + 1u >= mantissa.size()) {
|
||||
result = mantissa + std::string(static_cast<std::size_t>(exponent) + 1u - mantissa.size(), '0');
|
||||
result += ".0";
|
||||
} else {
|
||||
result = mantissa.substr(0, static_cast<std::size_t>(exponent) + 1u) + "." +
|
||||
mantissa.substr(static_cast<std::size_t>(exponent) + 1u);
|
||||
}
|
||||
} else {
|
||||
result = "0." + std::string(static_cast<std::size_t>(-exponent - 1), '0') + mantissa;
|
||||
}
|
||||
return negative ? "-" + result : result;
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
// NPY 1.0 images and a minimal ZIP container, used to write the compiled HRTF
|
||||
// cache in exactly the layout the reader (and NumPy) expects. Only what the
|
||||
// cache needs is implemented: little-endian C-order arrays and stored members.
|
||||
struct NpyMember {
|
||||
std::string name; // archive member name, without the .npy suffix
|
||||
std::string descr; // NumPy dtype string, e.g. "<f8", "<c16", "<U123"
|
||||
std::vector<std::uint64_t> shape;
|
||||
std::vector<std::uint8_t> data; // C order payload in the dtype's byte order
|
||||
};
|
||||
|
||||
// Serializes one array as an NPY 1.0 image (magic, header, 64-byte aligned).
|
||||
std::vector<std::uint8_t> npy_image(const std::string& descr,
|
||||
const std::vector<std::uint64_t>& shape,
|
||||
const std::vector<std::uint8_t>& data);
|
||||
|
||||
// Writes a ZIP archive with stored (uncompressed) members. The upstream reader
|
||||
// accepts stored members, and compression would need a deflate encoder.
|
||||
bool write_zip(const std::string& path, const std::vector<NpyMember>& members,
|
||||
std::string* error);
|
||||
|
||||
// Serializes the archive in memory (same layout as write_zip).
|
||||
std::vector<std::uint8_t> zip_bytes(const std::vector<NpyMember>& members);
|
||||
|
||||
// UTF-8 text as the payload of a NumPy Unicode scalar string ('<U<n>').
|
||||
std::vector<std::uint8_t> utf8_to_utf32le(const std::string& text);
|
||||
|
||||
// Python's repr() for a double: shortest round-trip digits with Python's
|
||||
// exponent rules, which is what json.dumps emits for the cache metadata.
|
||||
std::string python_float_repr(double value);
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,186 +0,0 @@
|
||||
#include "io/process.h"
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string quote_argument(const std::string& argument) {
|
||||
if (!argument.empty() && argument.find_first_of(" \t\"") == std::string::npos) {
|
||||
return argument;
|
||||
}
|
||||
std::string quoted = "\"";
|
||||
unsigned backslashes = 0;
|
||||
for (const char c : argument) {
|
||||
if (c == '\\') {
|
||||
++backslashes;
|
||||
continue;
|
||||
}
|
||||
if (c == '"') {
|
||||
quoted.append(backslashes * 2 + 1, '\\');
|
||||
quoted.push_back('"');
|
||||
backslashes = 0;
|
||||
continue;
|
||||
}
|
||||
quoted.append(backslashes, '\\');
|
||||
backslashes = 0;
|
||||
quoted.push_back(c);
|
||||
}
|
||||
quoted.append(backslashes * 2, '\\');
|
||||
quoted.push_back('"');
|
||||
return quoted;
|
||||
}
|
||||
|
||||
std::string tail_of(const std::string& text, std::size_t limit) {
|
||||
if (text.size() <= limit) {
|
||||
return text;
|
||||
}
|
||||
return text.substr(text.size() - limit);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Status run_process(const std::vector<std::string>& argv, ProcessResult* out) {
|
||||
if (out == nullptr || argv.empty()) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "empty command");
|
||||
}
|
||||
out->output.clear();
|
||||
out->exit_code = 0;
|
||||
|
||||
const std::filesystem::path log_path =
|
||||
std::filesystem::temp_directory_path() /
|
||||
("joc_process_" + std::to_string(std::random_device{}()) + ".log");
|
||||
|
||||
auto read_log = [&]() {
|
||||
#if defined(_WIN32)
|
||||
return; // the Windows branch reads the handle it opened
|
||||
#else
|
||||
std::ifstream log = fs_utf8::open_input(fs_utf8::from_path(log_path));
|
||||
if (log) {
|
||||
std::string text((std::istreambuf_iterator<char>(log)),
|
||||
std::istreambuf_iterator<char>());
|
||||
out->output = tail_of(text, 4096);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
std::string command;
|
||||
for (std::size_t i = 0; i < argv.size(); ++i) {
|
||||
if (i != 0) {
|
||||
command.push_back(' ');
|
||||
}
|
||||
command += quote_argument(argv[i]);
|
||||
}
|
||||
auto widen = [](const std::string& text) {
|
||||
if (text.empty()) {
|
||||
return std::wstring();
|
||||
}
|
||||
const int size = MultiByteToWideChar(CP_UTF8, 0, text.c_str(),
|
||||
static_cast<int>(text.size()), nullptr, 0);
|
||||
std::wstring wide(static_cast<std::size_t>(size), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()), wide.data(),
|
||||
size);
|
||||
return wide;
|
||||
};
|
||||
const std::wstring wide_command = widen(command);
|
||||
const std::wstring wide_log = widen(fs_utf8::from_path(log_path));
|
||||
|
||||
SECURITY_ATTRIBUTES attributes{};
|
||||
attributes.nLength = sizeof(attributes);
|
||||
attributes.bInheritHandle = TRUE;
|
||||
// DELETE access plus FILE_FLAG_DELETE_ON_CLOSE means the log disappears when
|
||||
// the last handle goes away - including when this process is killed, which
|
||||
// would otherwise leave joc_process_*.log litter in the temp directory.
|
||||
HANDLE log_handle = CreateFileW(
|
||||
wide_log.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &attributes, CREATE_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
||||
if (log_handle == INVALID_HANDLE_VALUE) {
|
||||
return Status::fail(JOC_ERR_IO, stage::kOutput, "cannot create the process log file");
|
||||
}
|
||||
|
||||
// A delete-on-close file cannot be reopened by name (it is delete-pending), so
|
||||
// the child's output is read back through the handle it wrote to.
|
||||
auto read_log_handle = [&]() {
|
||||
LARGE_INTEGER start{};
|
||||
start.QuadPart = 0;
|
||||
if (!SetFilePointerEx(log_handle, start, nullptr, FILE_BEGIN)) {
|
||||
return;
|
||||
}
|
||||
std::string text;
|
||||
char buffer[1024];
|
||||
DWORD count = 0;
|
||||
while (ReadFile(log_handle, buffer, sizeof(buffer), &count, nullptr) && count > 0) {
|
||||
text.append(buffer, count);
|
||||
}
|
||||
out->output = tail_of(text, 4096);
|
||||
};
|
||||
|
||||
STARTUPINFOW startup{};
|
||||
startup.cb = sizeof(startup);
|
||||
startup.dwFlags = STARTF_USESTDHANDLES;
|
||||
startup.hStdOutput = log_handle;
|
||||
startup.hStdError = log_handle;
|
||||
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
PROCESS_INFORMATION process{};
|
||||
|
||||
std::vector<wchar_t> mutable_command(wide_command.begin(), wide_command.end());
|
||||
mutable_command.push_back(L'\0');
|
||||
const BOOL started = CreateProcessW(nullptr, mutable_command.data(), nullptr, nullptr, TRUE,
|
||||
CREATE_NO_WINDOW, nullptr, nullptr, &startup, &process);
|
||||
if (!started) {
|
||||
CloseHandle(log_handle); // delete-on-close removes the file
|
||||
return Status::fail(JOC_ERR_LIBRARY_MISSING, stage::kOutput,
|
||||
"cannot start " + argv[0] + " (is it on PATH?)");
|
||||
}
|
||||
WaitForSingleObject(process.hProcess, INFINITE);
|
||||
DWORD exit_code = 0;
|
||||
GetExitCodeProcess(process.hProcess, &exit_code);
|
||||
CloseHandle(process.hThread);
|
||||
CloseHandle(process.hProcess);
|
||||
// Read the log before the delete-on-close handle goes away.
|
||||
read_log_handle();
|
||||
CloseHandle(log_handle);
|
||||
out->exit_code = static_cast<std::uint32_t>(exit_code);
|
||||
#else
|
||||
std::string command;
|
||||
for (std::size_t i = 0; i < argv.size(); ++i) {
|
||||
if (i != 0) {
|
||||
command.push_back(' ');
|
||||
}
|
||||
command += quote_argument(argv[i]);
|
||||
}
|
||||
command += " > " + quote_argument(fs_utf8::from_path(log_path)) + " 2>&1";
|
||||
const int status = std::system(command.c_str());
|
||||
// system() reports a wait status, not the child's exit code.
|
||||
out->exit_code = status == -1 ? 127u
|
||||
: WIFEXITED(status) ? static_cast<std::uint32_t>(WEXITSTATUS(status))
|
||||
: 128u;
|
||||
read_log();
|
||||
std::error_code ignored;
|
||||
std::filesystem::remove(log_path, ignored);
|
||||
#endif
|
||||
|
||||
if (out->exit_code != 0) {
|
||||
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kOutput,
|
||||
argv[0] + " failed with exit code " + std::to_string(out->exit_code) +
|
||||
(out->output.empty() ? "" : ": " + tail_of(out->output, 400)));
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,19 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
struct ProcessResult {
|
||||
std::uint32_t exit_code = 0;
|
||||
std::string output;
|
||||
};
|
||||
|
||||
Status run_process(const std::vector<std::string>& argv, ProcessResult* out);
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,242 +0,0 @@
|
||||
#include "io/wav_writer.h"
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint16_t kWaveFormatPcm = 0x0001;
|
||||
constexpr std::uint16_t kWaveFormatIeeeFloat = 0x0003;
|
||||
constexpr std::uint16_t kWaveFormatExtensible = 0xFFFE;
|
||||
constexpr std::uint8_t kPcmGuid[16] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||
0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
|
||||
constexpr std::uint8_t kFloatGuid[16] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||
0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
|
||||
|
||||
void put_u16(std::string* out, std::uint16_t value) {
|
||||
char buffer[2];
|
||||
std::memcpy(buffer, &value, 2);
|
||||
out->append(buffer, 2);
|
||||
}
|
||||
|
||||
void put_u32(std::string* out, std::uint32_t value) {
|
||||
char buffer[4];
|
||||
std::memcpy(buffer, &value, 4);
|
||||
out->append(buffer, 4);
|
||||
}
|
||||
|
||||
void put_u64(std::string* out, std::uint64_t value) {
|
||||
char buffer[8];
|
||||
std::memcpy(buffer, &value, 8);
|
||||
out->append(buffer, 8);
|
||||
}
|
||||
|
||||
// Port of speaker_wav._fmt_chunk.
|
||||
std::string fmt_chunk(std::uint32_t channels, std::uint32_t rate, SampleFormat format, WavInfo* info) {
|
||||
std::uint16_t simple_tag = 0;
|
||||
const std::uint8_t* guid = nullptr;
|
||||
if (format == SampleFormat::Float32) {
|
||||
info->bits_per_sample = 32;
|
||||
info->bytes_per_sample = 4;
|
||||
simple_tag = kWaveFormatIeeeFloat;
|
||||
guid = kFloatGuid;
|
||||
} else {
|
||||
info->bits_per_sample = 24;
|
||||
info->bytes_per_sample = 3;
|
||||
simple_tag = kWaveFormatPcm;
|
||||
guid = kPcmGuid;
|
||||
}
|
||||
const std::uint32_t block_align = channels * info->bytes_per_sample;
|
||||
const std::uint32_t byte_rate = rate * block_align;
|
||||
info->block_align = block_align;
|
||||
|
||||
std::string body;
|
||||
if (channels <= 2) {
|
||||
put_u16(&body, simple_tag);
|
||||
put_u16(&body, static_cast<std::uint16_t>(channels));
|
||||
put_u32(&body, rate);
|
||||
put_u32(&body, byte_rate);
|
||||
put_u16(&body, static_cast<std::uint16_t>(block_align));
|
||||
put_u16(&body, static_cast<std::uint16_t>(info->bits_per_sample));
|
||||
} else {
|
||||
put_u16(&body, kWaveFormatExtensible);
|
||||
put_u16(&body, static_cast<std::uint16_t>(channels));
|
||||
put_u32(&body, rate);
|
||||
put_u32(&body, byte_rate);
|
||||
put_u16(&body, static_cast<std::uint16_t>(block_align));
|
||||
put_u16(&body, static_cast<std::uint16_t>(info->bits_per_sample));
|
||||
put_u16(&body, 22);
|
||||
put_u16(&body, static_cast<std::uint16_t>(info->bits_per_sample));
|
||||
put_u32(&body, 0);
|
||||
body.append(reinterpret_cast<const char*>(guid), 16);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
// int32 conversion identical to NumPy's float32 -> int32 cast after clipping.
|
||||
std::int32_t to_int32(const float value) {
|
||||
if (!std::isfinite(value)) {
|
||||
return std::numeric_limits<std::int32_t>::min();
|
||||
}
|
||||
return static_cast<std::int32_t>(value);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void pack_int24(const float* interleaved, std::size_t frames, std::size_t channels,
|
||||
std::string* out) {
|
||||
const std::size_t count = frames * channels;
|
||||
out->resize(count * 3);
|
||||
char* target = out->data();
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
float value = interleaved[i];
|
||||
if (value > 1.0f) {
|
||||
value = 1.0f;
|
||||
} else if (value < -1.0f) {
|
||||
value = -1.0f;
|
||||
}
|
||||
const std::int32_t scaled = to_int32(value * 8388607.0f);
|
||||
const std::uint32_t bits = static_cast<std::uint32_t>(scaled);
|
||||
target[i * 3 + 0] = static_cast<char>(bits & 0xFFu);
|
||||
target[i * 3 + 1] = static_cast<char>((bits >> 8) & 0xFFu);
|
||||
target[i * 3 + 2] = static_cast<char>((bits >> 16) & 0xFFu);
|
||||
}
|
||||
}
|
||||
|
||||
WavWriter::~WavWriter() {
|
||||
if (file_ != nullptr) {
|
||||
std::fclose(file_);
|
||||
file_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Status WavWriter::open(const std::string& path, std::uint32_t channels, std::uint32_t rate,
|
||||
SampleFormat format, std::uint64_t total_frames) {
|
||||
if (channels == 0 || rate == 0) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
|
||||
"WAV writer needs a positive channel count and rate");
|
||||
}
|
||||
path_ = path;
|
||||
channels_ = channels;
|
||||
total_frames_ = total_frames;
|
||||
frames_written_ = 0;
|
||||
finalized_ = false;
|
||||
info_ = WavInfo{};
|
||||
info_.format = format;
|
||||
|
||||
const std::string fmt = fmt_chunk(channels, rate, format, &info_);
|
||||
const std::uint64_t data_size = total_frames * info_.block_align;
|
||||
info_.data_bytes = data_size;
|
||||
const std::uint64_t riff_file_size = 12u + 8u + fmt.size() + 8u + data_size;
|
||||
const bool rf64 = (riff_file_size - 8u) > 0xFFFFFFFFull;
|
||||
info_.rf64 = rf64;
|
||||
|
||||
std::string header;
|
||||
if (rf64) {
|
||||
const std::uint64_t file_size = 12u + 36u + 8u + fmt.size() + 8u + data_size;
|
||||
header.append("RF64", 4);
|
||||
put_u32(&header, 0xFFFFFFFFu);
|
||||
header.append("WAVE", 4);
|
||||
header.append("ds64", 4);
|
||||
put_u32(&header, 28);
|
||||
put_u64(&header, file_size - 8u);
|
||||
put_u64(&header, data_size);
|
||||
put_u64(&header, total_frames);
|
||||
put_u32(&header, 0);
|
||||
} else {
|
||||
header.append("RIFF", 4);
|
||||
put_u32(&header, static_cast<std::uint32_t>(riff_file_size - 8u));
|
||||
header.append("WAVE", 4);
|
||||
}
|
||||
header.append("fmt ", 4);
|
||||
put_u32(&header, static_cast<std::uint32_t>(fmt.size()));
|
||||
header.append(fmt);
|
||||
header.append("data", 4);
|
||||
put_u32(&header, rf64 ? 0xFFFFFFFFu : static_cast<std::uint32_t>(data_size));
|
||||
|
||||
file_ = fs_utf8::fopen(path, "wb");
|
||||
if (file_ == nullptr) {
|
||||
// The reference creates the parent directory itself.
|
||||
std::error_code ignored;
|
||||
const std::filesystem::path parent = std::filesystem::path(path).parent_path();
|
||||
if (!parent.empty()) {
|
||||
std::filesystem::create_directories(parent, ignored);
|
||||
}
|
||||
file_ = fs_utf8::fopen(path, "wb");
|
||||
}
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput, "cannot open " + path);
|
||||
}
|
||||
if (std::fwrite(header.data(), 1, header.size(), file_) != header.size()) {
|
||||
std::fclose(file_);
|
||||
file_ = nullptr;
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "cannot write header to " + path);
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status WavWriter::write(const double* interleaved, std::size_t frames) {
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "WAV writer is not open");
|
||||
}
|
||||
if (frames == 0) {
|
||||
return Status::success();
|
||||
}
|
||||
if (frames_written_ + frames > total_frames_) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput,
|
||||
"WAV writer received more frames than the header declared (declared " +
|
||||
std::to_string(total_frames_) + ", written " +
|
||||
std::to_string(frames_written_) + ", requested " +
|
||||
std::to_string(frames) + ")");
|
||||
}
|
||||
const std::size_t count = frames * channels_;
|
||||
if (info_.format == SampleFormat::Float32) {
|
||||
std::vector<float> converted(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
converted[i] = static_cast<float>(interleaved[i]);
|
||||
}
|
||||
if (std::fwrite(converted.data(), sizeof(float), count, file_) != count) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "write failed for " + path_);
|
||||
}
|
||||
} else {
|
||||
std::vector<float> converted(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
converted[i] = static_cast<float>(interleaved[i]);
|
||||
}
|
||||
std::string packed;
|
||||
pack_int24(converted.data(), frames, channels_, &packed);
|
||||
if (std::fwrite(packed.data(), 1, packed.size(), file_) != packed.size()) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "write failed for " + path_);
|
||||
}
|
||||
}
|
||||
frames_written_ += frames;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status WavWriter::finalize() {
|
||||
if (file_ == nullptr) {
|
||||
return Status::fail(JOC_ERR_STATE, stage::kOutput, "WAV writer is not open");
|
||||
}
|
||||
if (frames_written_ != total_frames_) {
|
||||
std::fclose(file_);
|
||||
file_ = nullptr;
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput,
|
||||
"WAV writer wrote " + std::to_string(frames_written_) + " of " +
|
||||
std::to_string(total_frames_) + " frames");
|
||||
}
|
||||
const int result = std::fclose(file_);
|
||||
file_ = nullptr;
|
||||
finalized_ = true;
|
||||
if (result != 0) {
|
||||
return Status::fail(JOC_ERR_OUTPUT_WRITE, stage::kOutput, "close failed for " + path_);
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,61 +0,0 @@
|
||||
// Port of src/speaker_wav.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "foundation/status.h"
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
enum class SampleFormat { Float32, Int24 };
|
||||
|
||||
struct WavInfo {
|
||||
SampleFormat format = SampleFormat::Float32;
|
||||
std::uint32_t bits_per_sample = 32;
|
||||
std::uint32_t bytes_per_sample = 4;
|
||||
std::uint32_t block_align = 0;
|
||||
std::uint64_t data_bytes = 0;
|
||||
bool rf64 = false;
|
||||
};
|
||||
|
||||
// int24 packing shared by the WAV and ADM writers:
|
||||
// trunc(clip(v, -1, 1) * 8388607.0f) with the low three bytes written LE.
|
||||
// NaN follows NumPy's float->int cast (INT32_MIN) so that the C++ conversion is
|
||||
// never undefined; the reference passes it through unguarded (plan TD-3.11).
|
||||
void pack_int24(const float* interleaved, std::size_t frames, std::size_t channels,
|
||||
std::string* out);
|
||||
|
||||
class WavWriter {
|
||||
public:
|
||||
WavWriter() = default;
|
||||
~WavWriter();
|
||||
|
||||
WavWriter(const WavWriter&) = delete;
|
||||
WavWriter& operator=(const WavWriter&) = delete;
|
||||
|
||||
// `total_frames` must be known up front: the header depends on it.
|
||||
Status open(const std::string& path, std::uint32_t channels, std::uint32_t rate,
|
||||
SampleFormat format, std::uint64_t total_frames);
|
||||
|
||||
Status write(const double* interleaved, std::size_t frames);
|
||||
|
||||
Status finalize();
|
||||
|
||||
const WavInfo& info() const { return info_; }
|
||||
std::uint64_t frames_written() const { return frames_written_; }
|
||||
|
||||
private:
|
||||
std::FILE* file_ = nullptr;
|
||||
std::string path_;
|
||||
WavInfo info_;
|
||||
std::uint32_t channels_ = 0;
|
||||
std::uint64_t total_frames_ = 0;
|
||||
std::uint64_t frames_written_ = 0;
|
||||
bool finalized_ = false;
|
||||
};
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,215 +0,0 @@
|
||||
#include "io/zip_reader.h"
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "io/inflate.h"
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t kLocalHeaderSignature = 0x04034b50u;
|
||||
constexpr std::uint32_t kCentralHeaderSignature = 0x02014b50u;
|
||||
constexpr std::uint32_t kEndOfCentralDirectory = 0x06054b50u;
|
||||
|
||||
std::uint16_t read_u16(const std::uint8_t* p) {
|
||||
return static_cast<std::uint16_t>(p[0] | (p[1] << 8));
|
||||
}
|
||||
|
||||
std::uint32_t read_u32(const std::uint8_t* p) {
|
||||
return static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8) |
|
||||
(static_cast<std::uint32_t>(p[2]) << 16) | (static_cast<std::uint32_t>(p[3]) << 24);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::uint32_t crc32_of(const std::uint8_t* data, std::size_t size) {
|
||||
static std::uint32_t table[256];
|
||||
static bool ready = false;
|
||||
if (!ready) {
|
||||
for (std::uint32_t i = 0; i < 256; ++i) {
|
||||
std::uint32_t value = i;
|
||||
for (int bit = 0; bit < 8; ++bit) {
|
||||
value = (value & 1u) ? (0xEDB88320u ^ (value >> 1)) : (value >> 1);
|
||||
}
|
||||
table[i] = value;
|
||||
}
|
||||
ready = true;
|
||||
}
|
||||
std::uint32_t crc = 0xFFFFFFFFu;
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
crc = table[(crc ^ data[i]) & 0xFFu] ^ (crc >> 8);
|
||||
}
|
||||
return crc ^ 0xFFFFFFFFu;
|
||||
}
|
||||
|
||||
bool ZipArchive::open(const std::string& path, std::string* error) {
|
||||
entries_.clear();
|
||||
data_.clear();
|
||||
std::FILE* file = fs_utf8::fopen(path, "rb");
|
||||
if (file == nullptr) {
|
||||
if (error != nullptr) {
|
||||
*error = "cannot open " + path;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
std::fseek(file, 0, SEEK_END);
|
||||
const long long size = std::ftell(file);
|
||||
std::fseek(file, 0, SEEK_SET);
|
||||
if (size <= 0) {
|
||||
std::fclose(file);
|
||||
if (error != nullptr) {
|
||||
*error = "empty file " + path;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
data_.resize(static_cast<std::size_t>(size));
|
||||
const std::size_t got = std::fread(data_.data(), 1, data_.size(), file);
|
||||
std::fclose(file);
|
||||
if (got != data_.size()) {
|
||||
if (error != nullptr) {
|
||||
*error = "short read on " + path;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::size_t eocd = std::string::npos;
|
||||
const std::size_t scan_start = data_.size() > 65557u ? data_.size() - 65557u : 0u;
|
||||
for (std::size_t i = data_.size(); i-- > scan_start;) {
|
||||
if (i + 4u <= data_.size() && read_u32(&data_[i]) == kEndOfCentralDirectory) {
|
||||
eocd = i;
|
||||
break;
|
||||
}
|
||||
if (i == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (eocd == std::string::npos || eocd + 22u > data_.size()) {
|
||||
if (error != nullptr) {
|
||||
*error = "not a zip archive (no end-of-central-directory)";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const std::uint16_t entry_count = read_u16(&data_[eocd + 10]);
|
||||
const std::uint32_t directory_offset = read_u32(&data_[eocd + 16]);
|
||||
if (directory_offset >= data_.size()) {
|
||||
if (error != nullptr) {
|
||||
*error = "central directory offset out of range";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
std::size_t cursor = directory_offset;
|
||||
for (std::uint16_t index = 0; index < entry_count; ++index) {
|
||||
if (cursor + 46u > data_.size() || read_u32(&data_[cursor]) != kCentralHeaderSignature) {
|
||||
if (error != nullptr) {
|
||||
*error = "malformed central directory entry " + std::to_string(index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
ZipEntry entry;
|
||||
entry.method = read_u16(&data_[cursor + 10]);
|
||||
entry.crc32 = read_u32(&data_[cursor + 16]);
|
||||
entry.compressed_size = read_u32(&data_[cursor + 20]);
|
||||
entry.uncompressed_size = read_u32(&data_[cursor + 24]);
|
||||
const std::uint16_t name_length = read_u16(&data_[cursor + 28]);
|
||||
const std::uint16_t extra_length = read_u16(&data_[cursor + 30]);
|
||||
const std::uint16_t comment_length = read_u16(&data_[cursor + 32]);
|
||||
entry.local_header_offset = read_u32(&data_[cursor + 42]);
|
||||
if (entry.compressed_size == 0xFFFFFFFFu || entry.uncompressed_size == 0xFFFFFFFFu ||
|
||||
entry.local_header_offset == 0xFFFFFFFFu) {
|
||||
if (error != nullptr) {
|
||||
*error = "zip64 archives are not supported";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (cursor + 46u + name_length > data_.size()) {
|
||||
if (error != nullptr) {
|
||||
*error = "member name out of range";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
entry.name.assign(reinterpret_cast<const char*>(&data_[cursor + 46]), name_length);
|
||||
entries_.push_back(std::move(entry));
|
||||
cursor += 46u + name_length + extra_length + comment_length;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const ZipEntry* ZipArchive::find(const std::string& name) const {
|
||||
for (const ZipEntry& entry : entries_) {
|
||||
if (entry.name == name) {
|
||||
return &entry;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool ZipArchive::extract(const ZipEntry& entry, std::vector<std::uint8_t>* out,
|
||||
std::string* error) const {
|
||||
if (out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const std::size_t offset = entry.local_header_offset;
|
||||
if (offset + 30u > data_.size() || read_u32(&data_[offset]) != kLocalHeaderSignature) {
|
||||
if (error != nullptr) {
|
||||
*error = "bad local header for " + entry.name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const std::uint16_t name_length = read_u16(&data_[offset + 26]);
|
||||
const std::uint16_t extra_length = read_u16(&data_[offset + 28]);
|
||||
const std::size_t start = offset + 30u + name_length + extra_length;
|
||||
if (start + entry.compressed_size > data_.size()) {
|
||||
if (error != nullptr) {
|
||||
*error = "member data out of range for " + entry.name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (entry.method == 0u) {
|
||||
out->assign(data_.begin() + static_cast<std::ptrdiff_t>(start),
|
||||
data_.begin() + static_cast<std::ptrdiff_t>(start + entry.compressed_size));
|
||||
} else if (entry.method == 8u) {
|
||||
if (!inflate_raw(&data_[start], entry.compressed_size, out)) {
|
||||
if (error != nullptr) {
|
||||
*error = "deflate error in " + entry.name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (error != nullptr) {
|
||||
*error = "unsupported compression method " + std::to_string(entry.method) + " for " +
|
||||
entry.name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (entry.uncompressed_size != 0u && out->size() != entry.uncompressed_size) {
|
||||
if (error != nullptr) {
|
||||
*error = "size mismatch for " + entry.name + " (" + std::to_string(out->size()) +
|
||||
" vs " + std::to_string(entry.uncompressed_size) + ")";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (entry.crc32 != 0u && crc32_of(out->data(), out->size()) != entry.crc32) {
|
||||
if (error != nullptr) {
|
||||
*error = "CRC mismatch for " + entry.name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ZipArchive::read_member(const std::string& name, std::vector<std::uint8_t>* out,
|
||||
std::string* error) const {
|
||||
const ZipEntry* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
if (error != nullptr) {
|
||||
*error = "member not found: " + name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return extract(*entry, out, error);
|
||||
}
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,38 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::io {
|
||||
|
||||
struct ZipEntry {
|
||||
std::string name;
|
||||
std::uint16_t method = 0;
|
||||
std::uint32_t crc32 = 0;
|
||||
std::uint32_t compressed_size = 0;
|
||||
std::uint32_t uncompressed_size = 0;
|
||||
std::uint32_t local_header_offset = 0;
|
||||
};
|
||||
|
||||
class ZipArchive {
|
||||
public:
|
||||
bool open(const std::string& path, std::string* error);
|
||||
|
||||
const std::vector<ZipEntry>& entries() const { return entries_; }
|
||||
|
||||
const ZipEntry* find(const std::string& name) const;
|
||||
|
||||
bool extract(const ZipEntry& entry, std::vector<std::uint8_t>* out, std::string* error) const;
|
||||
|
||||
bool read_member(const std::string& name, std::vector<std::uint8_t>* out, std::string* error) const;
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t> data_;
|
||||
std::vector<ZipEntry> entries_;
|
||||
};
|
||||
|
||||
std::uint32_t crc32_of(const std::uint8_t* data, std::size_t size);
|
||||
|
||||
} // namespace joc::io
|
||||
@@ -1,632 +0,0 @@
|
||||
|
||||
#include "joc_bitstream/bed_layout_map.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
#include "speaker/speaker_layouts.h"
|
||||
|
||||
namespace joc::joc {
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t bit(BedChannel channel) {
|
||||
return 1u << static_cast<unsigned>(channel);
|
||||
}
|
||||
|
||||
// Table 12 每个 bit 展开的声道(LSB 在前)。
|
||||
struct StandardBit {
|
||||
BedChannel first;
|
||||
BedChannel second; // kCount 表示单声道
|
||||
};
|
||||
|
||||
constexpr StandardBit kStandardBits[10] = {
|
||||
{BedChannel::kLeft, BedChannel::kRight},
|
||||
{BedChannel::kCenter, BedChannel::kCount},
|
||||
{BedChannel::kLfe, BedChannel::kCount},
|
||||
{BedChannel::kLeftSurround, BedChannel::kRightSurround},
|
||||
{BedChannel::kLeftRearSurround, BedChannel::kRightRearSurround},
|
||||
{BedChannel::kTopFrontLeft, BedChannel::kTopFrontRight},
|
||||
{BedChannel::kTopSideLeft, BedChannel::kTopSideRight},
|
||||
{BedChannel::kTopBackLeft, BedChannel::kTopBackRight},
|
||||
{BedChannel::kLeftWide, BedChannel::kRightWide},
|
||||
{BedChannel::kLfe2, BedChannel::kCount},
|
||||
};
|
||||
|
||||
// Table 13 的 17 bit 非标准掩码(LSB 在前)。
|
||||
constexpr BedChannel kNonStandardBits[17] = {
|
||||
BedChannel::kLfe2, BedChannel::kRightWide, BedChannel::kLeftWide,
|
||||
BedChannel::kTopBackRight, BedChannel::kTopBackLeft,
|
||||
BedChannel::kTopSideRight, BedChannel::kTopSideLeft,
|
||||
BedChannel::kTopFrontRight, BedChannel::kTopFrontLeft,
|
||||
BedChannel::kRightRearSurround, BedChannel::kLeftRearSurround,
|
||||
BedChannel::kRightSurround, BedChannel::kLeftSurround,
|
||||
BedChannel::kLfe, BedChannel::kCenter, BedChannel::kRight,
|
||||
BedChannel::kLeft,
|
||||
};
|
||||
|
||||
struct ChannelSpec {
|
||||
BedChannel channel;
|
||||
const char* short_name;
|
||||
const char* label;
|
||||
double xyz[3];
|
||||
};
|
||||
|
||||
constexpr ChannelSpec kChannels[] = {
|
||||
{BedChannel::kLeft, "L", "RC_L", {-1.0, 1.0, 0.0}},
|
||||
{BedChannel::kRight, "R", "RC_R", {1.0, 1.0, 0.0}},
|
||||
{BedChannel::kCenter, "C", "RC_C", {0.0, 1.0, 0.0}},
|
||||
{BedChannel::kLfe, "LFE", "RC_LFE", {0.0, 0.0, 0.0}},
|
||||
{BedChannel::kLeftSurround, "Ls", "RC_LS", {-1.0, 0.0, 0.0}},
|
||||
{BedChannel::kRightSurround, "Rs", "RC_RS", {1.0, 0.0, 0.0}},
|
||||
{BedChannel::kLeftRearSurround, "Lrs", "RC_LB", {-1.0, -1.0, 0.0}},
|
||||
{BedChannel::kRightRearSurround, "Rrs", "RC_RB", {1.0, -1.0, 0.0}},
|
||||
{BedChannel::kTopFrontLeft, "Ltf", "RC_TFL", {-0.5, 0.5, 1.0}},
|
||||
{BedChannel::kTopFrontRight, "Rtf", "RC_TFR", {0.5, 0.5, 1.0}},
|
||||
{BedChannel::kTopSideLeft, "Ltm", "RC_TSL", {-1.0, 0.0, 1.0}},
|
||||
{BedChannel::kTopSideRight, "Rtm", "RC_TSR", {1.0, 0.0, 1.0}},
|
||||
{BedChannel::kTopBackLeft, "Ltr", "RC_TBL", {-0.5, -0.5, 1.0}},
|
||||
{BedChannel::kTopBackRight, "Rtr", "RC_TBR", {0.5, -0.5, 1.0}},
|
||||
{BedChannel::kLeftWide, "Lw", "RC_LW", {-1.0, 0.5, 0.0}},
|
||||
{BedChannel::kRightWide, "Rw", "RC_RW", {1.0, 0.5, 0.0}},
|
||||
{BedChannel::kLfe2, "LFE2", "RC_LFE2", {0.0, 0.0, 0.0}},
|
||||
};
|
||||
|
||||
const ChannelSpec* spec_of(BedChannel channel) {
|
||||
for (const ChannelSpec& spec : kChannels) {
|
||||
if (spec.channel == channel) {
|
||||
return &spec;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 标准 bed 布局名,键是声明声道的集合。
|
||||
struct LayoutName {
|
||||
std::uint32_t mask;
|
||||
const char* name;
|
||||
};
|
||||
|
||||
constexpr std::uint32_t mask_of() { return 0u; }
|
||||
|
||||
constexpr LayoutName kLayoutNames[] = {
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight), "2.0"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kCenter), "3.0"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kCenter) |
|
||||
bit(BedChannel::kLfe), "3.1"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround), "4.0"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter), "5.0"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe), "5.1"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround), "6.0"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround), "7.1"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide), "9.1"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "5.1.2"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) |
|
||||
bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "5.1.4"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "7.1.2"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) |
|
||||
bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "7.1.4"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) |
|
||||
bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight) |
|
||||
bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "7.1.6"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) |
|
||||
bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "9.1.2"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) |
|
||||
bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) |
|
||||
bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "9.1.4"},
|
||||
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
|
||||
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
|
||||
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
|
||||
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) |
|
||||
bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) |
|
||||
bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) |
|
||||
bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight) |
|
||||
bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "9.1.6"},
|
||||
};
|
||||
|
||||
const char* layout_name_for(std::uint32_t mask) {
|
||||
for (const LayoutName& entry : kLayoutNames) {
|
||||
if (entry.mask == mask) {
|
||||
return entry.name;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// 每个扬声器布局的标准声道名(按该布局的标准声道顺序排列)。
|
||||
struct LayoutChannels {
|
||||
std::uint32_t out_ch_config;
|
||||
std::uint32_t count;
|
||||
const char* names[16];
|
||||
};
|
||||
|
||||
constexpr LayoutChannels kLayoutChannels[] = {
|
||||
{0, 2, {"L", "R"}},
|
||||
{3, 4, {"L", "R", "C", "LFE"}},
|
||||
{255, 4, {"L", "R", "Lrs", "Rrs"}},
|
||||
{7, 6, {"L", "R", "C", "LFE", "Ls", "Rs"}},
|
||||
{11, 8, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs"}},
|
||||
{13, 8, {"L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm"}},
|
||||
{14, 10, {"L", "R", "C", "LFE", "Ls", "Rs", "Ltf", "Rtf", "Ltr", "Rtr"}},
|
||||
{15, 10, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltm", "Rtm"}},
|
||||
{16, 12, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltf", "Rtf", "Ltr", "Rtr"}},
|
||||
{19, 14, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw",
|
||||
"Ltf", "Rtf", "Ltr", "Rtr"}},
|
||||
{20, 16, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw",
|
||||
"Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr"}},
|
||||
};
|
||||
|
||||
const ejoc::speaker_tables::LayoutGeometry* geometry_for(std::uint32_t out_ch_config) {
|
||||
for (const ejoc::speaker_tables::LayoutGeometry& layout : ejoc::speaker_tables::kLayouts) {
|
||||
if (layout.out_ch_config == out_ch_config) {
|
||||
return &layout;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BedLayout resolve_bed_layout(const std::vector<oamd::BedAssignment>& assignments,
|
||||
bool bed_only) {
|
||||
BedLayout layout;
|
||||
layout.bed_only = bed_only;
|
||||
std::uint32_t mask = 0;
|
||||
for (const oamd::BedAssignment& assignment : assignments) {
|
||||
if (assignment.lfe_only) {
|
||||
mask |= bit(BedChannel::kLfe);
|
||||
layout.complete = false;
|
||||
continue;
|
||||
}
|
||||
if (assignment.standard) {
|
||||
for (int index = 0; index < 10; ++index) {
|
||||
if (((assignment.mask >> index) & 1u) == 0u) {
|
||||
continue;
|
||||
}
|
||||
mask |= bit(kStandardBits[index].first);
|
||||
if (kStandardBits[index].second != BedChannel::kCount) {
|
||||
mask |= bit(kStandardBits[index].second);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (int index = 0; index < 17; ++index) {
|
||||
if (((assignment.mask >> index) & 1u) != 0u) {
|
||||
mask |= bit(kNonStandardBits[index]);
|
||||
}
|
||||
}
|
||||
layout.complete = false; // 非标准掩码的声道名不在本标准表里
|
||||
}
|
||||
if (mask == 0u) {
|
||||
return layout;
|
||||
}
|
||||
layout.present = true;
|
||||
layout.mask = mask;
|
||||
layout.name = layout_name_for(mask);
|
||||
for (const ChannelSpec& spec : kChannels) {
|
||||
const BedChannel channel = spec.channel;
|
||||
if ((mask & bit(channel)) == 0u) {
|
||||
continue;
|
||||
}
|
||||
if (channel == BedChannel::kLfe || channel == BedChannel::kLfe2) {
|
||||
continue;
|
||||
}
|
||||
layout.channels.push_back(channel);
|
||||
}
|
||||
return layout;
|
||||
}
|
||||
|
||||
const char* bed_channel_short_name(BedChannel channel) {
|
||||
const ChannelSpec* spec = spec_of(channel);
|
||||
return spec != nullptr ? spec->short_name : "";
|
||||
}
|
||||
|
||||
const char* bed_channel_label(BedChannel channel) {
|
||||
const ChannelSpec* spec = spec_of(channel);
|
||||
return spec != nullptr ? spec->label : "";
|
||||
}
|
||||
|
||||
bool bed_channel_adm_position(BedChannel channel, double xyz[3]) {
|
||||
const ChannelSpec* spec = spec_of(channel);
|
||||
if (spec == nullptr || xyz == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::memcpy(xyz, spec->xyz, sizeof(spec->xyz));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]) {
|
||||
double xyz[3] = {0.0, 0.0, 0.0};
|
||||
if (!bed_channel_adm_position(channel, xyz) || q15 == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const double q1 = (xyz[0] + 1.0) * 0.5 * 32767.0;
|
||||
const double q2 = (1.0 - xyz[1]) * 0.5 * 32767.0;
|
||||
const double q3 = xyz[2] * 32767.0;
|
||||
q15[0] = static_cast<std::uint16_t>(q1 < 0.0 ? 0.0 : (q1 > 32767.0 ? 32767.0 : q1));
|
||||
q15[1] = static_cast<std::uint16_t>(q2 < 0.0 ? 0.0 : (q2 > 32767.0 ? 32767.0 : q2));
|
||||
q15[2] = static_cast<std::uint16_t>(q3 < 0.0 ? 0.0 : (q3 > 32767.0 ? 32767.0 : q3));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel,
|
||||
std::uint16_t q15[3]) {
|
||||
if (q15 == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const char* name = bed_channel_short_name(channel);
|
||||
if (name == nullptr || name[0] == '\0' || std::strcmp(name, "LFE") == 0) {
|
||||
return false;
|
||||
}
|
||||
const LayoutChannels* table = nullptr;
|
||||
for (const LayoutChannels& entry : kLayoutChannels) {
|
||||
if (entry.out_ch_config == out_ch_config) {
|
||||
table = &entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const ejoc::speaker_tables::LayoutGeometry* geometry = geometry_for(out_ch_config);
|
||||
if (table == nullptr || geometry == nullptr) {
|
||||
return false;
|
||||
}
|
||||
int standard_index = -1;
|
||||
for (std::uint32_t index = 0; index < table->count; ++index) {
|
||||
if (std::strcmp(table->names[index], name) == 0) {
|
||||
standard_index = static_cast<int>(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (standard_index < 0) {
|
||||
return false;
|
||||
}
|
||||
const std::uint8_t internal = geometry->standard_from_internal[standard_index];
|
||||
const ejoc::speaker_tables::RegionGeometry& region = geometry->regions[0];
|
||||
for (std::uint32_t point = 0; point < region.point_count; ++point) {
|
||||
if (region.points[point].speaker_id != internal) {
|
||||
continue;
|
||||
}
|
||||
for (int axis = 0; axis < 3; ++axis) {
|
||||
q15[axis] = region.points[point].coordinate_q15[axis];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
// ETSI TS 103 420 Table B.10: the fixed ADM position of every Table 12 bed
|
||||
// channel. bed_output_description() matches these points against the target
|
||||
// layout; the kChannels table above keeps the ADM/binaural path's coordinates.
|
||||
struct TableB10Position {
|
||||
BedChannel channel;
|
||||
double xyz[3];
|
||||
};
|
||||
|
||||
constexpr TableB10Position kTableB10[oamd::kBedLabelCount] = {
|
||||
{BedChannel::kLeft, {-1.0, 1.0, 0.0}},
|
||||
{BedChannel::kRight, {1.0, 1.0, 0.0}},
|
||||
{BedChannel::kCenter, {0.0, 1.0, 0.0}},
|
||||
{BedChannel::kLfe, {-1.0, 1.0, -1.0}},
|
||||
{BedChannel::kLeftSurround, {-1.0, 0.0, 0.0}},
|
||||
{BedChannel::kRightSurround, {1.0, 0.0, 0.0}},
|
||||
{BedChannel::kLeftRearSurround, {-1.0, -1.0, 0.0}},
|
||||
{BedChannel::kRightRearSurround, {1.0, -1.0, 0.0}},
|
||||
{BedChannel::kTopFrontLeft, {-1.0, 1.0, 1.0}},
|
||||
{BedChannel::kTopFrontRight, {1.0, 1.0, 1.0}},
|
||||
{BedChannel::kTopSideLeft, {-1.0, 0.0, 1.0}},
|
||||
{BedChannel::kTopSideRight, {1.0, 0.0, 1.0}},
|
||||
{BedChannel::kTopBackLeft, {-1.0, -1.0, 1.0}},
|
||||
{BedChannel::kTopBackRight, {1.0, -1.0, 1.0}},
|
||||
{BedChannel::kLeftWide, {-1.0, 21.0 / 31.0, 0.0}},
|
||||
{BedChannel::kRightWide, {1.0, 21.0 / 31.0, 0.0}},
|
||||
{BedChannel::kLfe2, {1.0, 1.0, -1.0}},
|
||||
};
|
||||
|
||||
// bed_layout.BED_REFERENCE_POSITIONS_Q15; index 3 is the non-spatial sentinel.
|
||||
constexpr std::int32_t kReferencePositions[kBedReferencePositionCount][3] = {
|
||||
{0, 0, 0}, {32767, 0, 0}, {16384, 0, 0}, {-1, -1, -1},
|
||||
{0, 16384, 0}, {32767, 16384, 0}, {0, 32767, 0}, {32767, 32767, 0},
|
||||
{16384, 32767, 0}, {5285, 0, 0}, {27483, 0, 0}, {0, 5285, 0},
|
||||
{32767, 5285, 0}, {7928, 0, 32767}, {24840, 0, 32767}, {7928, 7928, 32767},
|
||||
{24840, 7928, 32767}, {7928, 16384, 32767}, {24840, 16384, 32767},
|
||||
{7928, 24840, 32767}, {24840, 24840, 32767}, {7928, 32767, 32767},
|
||||
{24840, 32767, 32767}, {11099, 0, 0}, {21669, 0, 0}, {0, 11099, 0},
|
||||
{32767, 11099, 0}, {0, 21669, 0}, {32767, 21669, 0}, {0, 27483, 0},
|
||||
{32767, 27483, 0}, {5285, 32767, 0}, {27483, 32767, 0}, {11099, 32767, 0},
|
||||
{21669, 32767, 0}, {16384, 0, 32767}, {16384, 16384, 32767},
|
||||
{16384, 32767, 32767},
|
||||
};
|
||||
|
||||
// 自动 trim 的每个布局基准衰减,单位 dB。
|
||||
struct AttenuationEntry {
|
||||
const char* compact_name;
|
||||
double db;
|
||||
};
|
||||
|
||||
constexpr AttenuationEntry kAttenuation[] = {
|
||||
{"20", -4.5}, {"31", -4.5}, {"40", -3.0}, {"51", -3.0},
|
||||
{"60", -1.5}, {"71", -1.5}, {"512", -2.25}, {"514", -1.5},
|
||||
{"712", -0.75}, {"714", 0.0}, {"914", 0.0}, {"916", 0.0},
|
||||
};
|
||||
|
||||
constexpr int kMaxChannels = static_cast<int>(ejoc::speaker_tables::kMaxChannels);
|
||||
|
||||
void set_sentinel(std::int32_t coordinate[3]) {
|
||||
coordinate[0] = -1;
|
||||
coordinate[1] = -1;
|
||||
coordinate[2] = -1;
|
||||
}
|
||||
|
||||
bool is_sentinel(const std::int32_t coordinate[3]) {
|
||||
return coordinate[0] == -1 && coordinate[1] == -1 && coordinate[2] == -1;
|
||||
}
|
||||
|
||||
bool is_lfe_label(BedChannel channel) {
|
||||
return channel == BedChannel::kLfe || channel == BedChannel::kLfe2;
|
||||
}
|
||||
|
||||
const TableB10Position* table_b10(BedChannel channel) {
|
||||
for (const TableB10Position& entry : kTableB10) {
|
||||
if (entry.channel == channel) {
|
||||
return &entry;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// ADM 笛卡尔坐标 -> 渲染器方位 (u 右, v 后, w 上)。
|
||||
void render_coordinates(const double xyz[3], double out[3]) {
|
||||
out[0] = (xyz[0] + 1.0) * 0.5;
|
||||
out[1] = (1.0 - xyz[1]) * 0.5;
|
||||
out[2] = xyz[2];
|
||||
}
|
||||
|
||||
// 渲染器方位的欧氏距离,比较的是归一化到 0..1 的坐标。
|
||||
double render_distance(const double left[3], const double right[3]) {
|
||||
const double du = left[0] - right[0];
|
||||
const double dv = left[1] - right[1];
|
||||
const double dw = left[2] - right[2];
|
||||
return std::sqrt(du * du + dv * dv + dw * dw);
|
||||
}
|
||||
|
||||
// Reference-position matching: the non-spatial sentinel resolves to index 3.
|
||||
int nearest_reference(const std::int32_t coordinate[3]) {
|
||||
if (is_sentinel(coordinate)) {
|
||||
return 3;
|
||||
}
|
||||
const double position[3] = {
|
||||
static_cast<double>(coordinate[0]) / 32768.0,
|
||||
static_cast<double>(coordinate[1]) / 32768.0,
|
||||
static_cast<double>(coordinate[2]) / 32768.0,
|
||||
};
|
||||
int best = 3;
|
||||
double best_distance = 0.0;
|
||||
bool have_best = false;
|
||||
for (int index = 0; index < kBedReferencePositionCount; ++index) {
|
||||
const std::int32_t* candidate = kReferencePositions[index];
|
||||
if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) {
|
||||
continue;
|
||||
}
|
||||
const double point[3] = {
|
||||
static_cast<double>(candidate[0]) / 32768.0,
|
||||
static_cast<double>(candidate[1]) / 32768.0,
|
||||
static_cast<double>(candidate[2]) / 32768.0,
|
||||
};
|
||||
const double distance = render_distance(position, point);
|
||||
if (!have_best || distance < best_distance) {
|
||||
best = index;
|
||||
best_distance = distance;
|
||||
have_best = true;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
// 目标布局里离 desired 最近、且在 0.05 容差内的输出声道;没有返回 -1。
|
||||
int nearest_channel(const double desired[3], const std::int32_t positions[][3],
|
||||
std::uint32_t channel_count) {
|
||||
int best = -1;
|
||||
double best_distance = 0.0;
|
||||
bool have_best = false;
|
||||
for (std::uint32_t index = 0; index < channel_count; ++index) {
|
||||
if (is_sentinel(positions[index])) {
|
||||
continue;
|
||||
}
|
||||
const double point[3] = {
|
||||
static_cast<double>(positions[index][0]) / 32768.0,
|
||||
static_cast<double>(positions[index][1]) / 32768.0,
|
||||
static_cast<double>(positions[index][2]) / 32768.0,
|
||||
};
|
||||
const double distance = render_distance(desired, point);
|
||||
if (!have_best || distance < best_distance) {
|
||||
best = static_cast<int>(index);
|
||||
best_distance = distance;
|
||||
have_best = true;
|
||||
}
|
||||
}
|
||||
if (have_best && best_distance <= 0.05) {
|
||||
return best;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 输出声道 index -> 该声道的 Q15 方位;LFE 与非空间声道给 sentinel。
|
||||
void channel_positions(const speaker::LayoutInfo& layout,
|
||||
std::int32_t positions[][3]) {
|
||||
for (int index = 0; index < kMaxChannels; ++index) {
|
||||
set_sentinel(positions[index]);
|
||||
}
|
||||
const auto* geometry = ejoc::speaker_tables::find_layout(layout.speaker_bitfield);
|
||||
if (geometry != nullptr) {
|
||||
bool known[kMaxChannels] = {};
|
||||
std::int32_t found[kMaxChannels][3];
|
||||
for (int index = 0; index < kMaxChannels; ++index) {
|
||||
set_sentinel(found[index]);
|
||||
}
|
||||
for (const auto& region : geometry->regions) {
|
||||
for (std::size_t point = 0; point < region.point_count; ++point) {
|
||||
const auto speaker = static_cast<int>(region.points[point].speaker_id);
|
||||
if (speaker < 0 || speaker >= kMaxChannels || known[speaker]) {
|
||||
continue;
|
||||
}
|
||||
for (int axis = 0; axis < 3; ++axis) {
|
||||
found[speaker][axis] =
|
||||
static_cast<std::int32_t>(region.points[point].coordinate_q15[axis]);
|
||||
}
|
||||
known[speaker] = true;
|
||||
}
|
||||
}
|
||||
const std::uint32_t count =
|
||||
layout.channel_count < static_cast<std::uint32_t>(kMaxChannels)
|
||||
? layout.channel_count
|
||||
: static_cast<std::uint32_t>(kMaxChannels);
|
||||
for (std::uint32_t index = 0; index < count; ++index) {
|
||||
const char* name = layout.channel_names != nullptr ? layout.channel_names[index]
|
||||
: nullptr;
|
||||
if (name != nullptr && std::strcmp(name, "LFE") == 0) {
|
||||
continue;
|
||||
}
|
||||
if (known[index]) {
|
||||
for (int axis = 0; axis < 3; ++axis) {
|
||||
positions[index][axis] = found[index][axis];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int channel_index(const speaker::LayoutInfo& layout, const char* name) {
|
||||
if (name == nullptr || layout.channel_names == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
for (std::uint32_t index = 0; index < layout.channel_count; ++index) {
|
||||
if (std::strcmp(layout.channel_names[index], name) == 0) {
|
||||
return static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
double attenuation_db(const speaker::LayoutInfo& layout) {
|
||||
for (const AttenuationEntry& entry : kAttenuation) {
|
||||
if (std::strcmp(entry.compact_name, layout.compact_name) == 0) {
|
||||
return entry.db;
|
||||
}
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const std::int32_t (*bed_reference_positions_q15())[3] {
|
||||
return kReferencePositions;
|
||||
}
|
||||
|
||||
BedOutputDescription bed_output_description(const speaker::LayoutInfo& layout) {
|
||||
BedOutputDescription description;
|
||||
std::int32_t positions[kMaxChannels][3];
|
||||
channel_positions(layout, positions);
|
||||
const std::uint32_t output_count =
|
||||
layout.channel_count < static_cast<std::uint32_t>(kMaxChannels)
|
||||
? layout.channel_count
|
||||
: static_cast<std::uint32_t>(kMaxChannels);
|
||||
description.output_labels.reserve(output_count);
|
||||
for (std::uint32_t index = 0; index < output_count; ++index) {
|
||||
description.output_labels.push_back(nearest_reference(positions[index]));
|
||||
}
|
||||
for (int label = 0; label < oamd::kBedLabelCount; ++label) {
|
||||
const BedChannel channel = static_cast<BedChannel>(label);
|
||||
const char* name = channel == BedChannel::kLfe2 ? nullptr
|
||||
: bed_channel_short_name(channel);
|
||||
int slot = -1;
|
||||
if (name != nullptr && name[0] != '\0') {
|
||||
slot = channel_index(layout, name);
|
||||
}
|
||||
if (slot < 0 && channel == BedChannel::kLfe2) {
|
||||
slot = channel_index(layout, "LFE");
|
||||
}
|
||||
const TableB10Position* position = table_b10(channel);
|
||||
double desired[3] = {0.0, 0.0, 0.0};
|
||||
if (position != nullptr) {
|
||||
render_coordinates(position->xyz, desired);
|
||||
}
|
||||
if (slot < 0 && position != nullptr) {
|
||||
slot = nearest_channel(desired, positions, output_count);
|
||||
}
|
||||
if (slot < 0) {
|
||||
description.label_to_slot[label] = is_lfe_label(channel) ? -1 : -2;
|
||||
} else {
|
||||
description.label_to_slot[label] = slot;
|
||||
}
|
||||
if (is_lfe_label(channel)) {
|
||||
description.direct_output_labels[label] = 3;
|
||||
} else {
|
||||
int best = 3;
|
||||
double best_distance = 0.0;
|
||||
bool have_best = false;
|
||||
for (int index = 0; index < kBedReferencePositionCount; ++index) {
|
||||
const std::int32_t* candidate = kReferencePositions[index];
|
||||
if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) {
|
||||
continue;
|
||||
}
|
||||
const double point[3] = {
|
||||
static_cast<double>(candidate[0]) / 32768.0,
|
||||
static_cast<double>(candidate[1]) / 32768.0,
|
||||
static_cast<double>(candidate[2]) / 32768.0,
|
||||
};
|
||||
const double distance = render_distance(desired, point);
|
||||
if (!have_best || distance < best_distance) {
|
||||
best = index;
|
||||
best_distance = distance;
|
||||
have_best = true;
|
||||
}
|
||||
}
|
||||
description.direct_output_labels[label] = best;
|
||||
}
|
||||
}
|
||||
description.automatic_attenuation_db = attenuation_db(layout);
|
||||
return description;
|
||||
}
|
||||
|
||||
} // namespace joc::joc
|
||||
@@ -1,69 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "oamd/oamd_parser.h"
|
||||
#include "speaker/speaker_layout_lookup.h"
|
||||
|
||||
namespace joc::joc {
|
||||
|
||||
// BedChannel lives in oamd/oamd_parser.h: the enum is the OAMD Table 12
|
||||
// label order and OAMD frame state carries it directly.
|
||||
|
||||
// 内容声明的 bed 布局。bed 对象在 OAMD 里没有位置字段,位置由这里的布局决定。
|
||||
struct BedLayout {
|
||||
bool present = false; // 有 bed_assignment() 声明
|
||||
bool bed_only = false; // program 只由 bed 组成(无 ISF、无动态对象)
|
||||
bool complete = true; // 所有声明声道都在本标准表里
|
||||
const char* name = ""; // "9.1.6" 等;未知集合为空串
|
||||
std::uint32_t mask = 0; // 声明声道的位集合(1 << BedChannel)
|
||||
std::vector<BedChannel> channels; // 去掉 LFE/LFE2,按 JOC 对象槽顺序
|
||||
};
|
||||
|
||||
// 声明里是否包含某个 bed 声道。
|
||||
inline bool bed_layout_has(const BedLayout& layout, BedChannel channel) {
|
||||
return (layout.mask & (1u << static_cast<unsigned>(channel))) != 0u;
|
||||
}
|
||||
|
||||
// 展开 bed_assignment() 声明;``bed_only`` 来自 program_assignment() 的
|
||||
// content_description。
|
||||
BedLayout resolve_bed_layout(const std::vector<oamd::BedAssignment>& assignments,
|
||||
bool bed_only);
|
||||
|
||||
// 扬声器布局里的短名("Lw")与 Table 12 的标签("RC_LW")。
|
||||
const char* bed_channel_short_name(BedChannel channel);
|
||||
const char* bed_channel_label(BedChannel channel);
|
||||
|
||||
// 固定方位:ADM 笛卡尔坐标(x 右、y 前、z 上)与渲染器使用的 Q15 方位。
|
||||
bool bed_channel_adm_position(BedChannel channel, double xyz[3]);
|
||||
bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]);
|
||||
|
||||
// 目标布局里同名扬声器的 Q15 方位:bed 声道直连到该扬声器,而不是参与声像。
|
||||
// 目标布局没有这个声道时返回 false。4.0 与 6.0 共用未分配的 out_ch_config
|
||||
// 0xFF,因此这里按 4.0 的声道集合解释 0xFF。
|
||||
bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel,
|
||||
std::uint16_t q15[3]);
|
||||
|
||||
// 参考位置网格 ``BED_REFERENCE_POSITIONS_Q15``:bed 标签与输出 slots 的最近邻
|
||||
// 匹配都用这一组固定 Q15 点。索引 3 是 (-1,-1,-1),表示 LFE 那样的非空间声源。
|
||||
inline constexpr int kBedReferencePositionCount = 38;
|
||||
const std::int32_t (*bed_reference_positions_q15())[3];
|
||||
|
||||
// 一个目标扬声器布局的 bed 路由描述(bed_layout.bed_output_description):
|
||||
// 按标准 bed 标签序(Table 12 的 17 个标签)给出目标 layout 的输出参考位置、
|
||||
// 每个标签的输出 slot 与直连参考位置,以及该布局的自动 trim 基准衰减。
|
||||
struct BedOutputDescription {
|
||||
std::vector<int> output_labels; // 每个输出声道最近邻的参考位置索引
|
||||
int label_to_slot[oamd::kBedLabelCount] = {}; // -2 不可达,-1 LFE 通路
|
||||
int direct_output_labels[oamd::kBedLabelCount] = {};
|
||||
double automatic_attenuation_db = 0.0;
|
||||
};
|
||||
|
||||
// 由布局名/几何派生 bed 路由描述;``layout.channel_names`` 必须按标准声道
|
||||
// 顺序给出 ``layout.channel_count`` 个名字。
|
||||
BedOutputDescription bed_output_description(const speaker::LayoutInfo& layout);
|
||||
|
||||
} // namespace joc::joc
|
||||
@@ -1,89 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// JOC Huffman trees required by the bitstream parser.
|
||||
|
||||
static const int joc_huff_code_coarse_generic[][2] =
|
||||
{
|
||||
{ -1, 1}, { 2, -2}, { -96, 3}, { 4, -3}, { -95, 5}, { 6, 7}, { -4, -94}, { 8, 9}, { -5, -93}, { 10, 11},
|
||||
{ -6, -92}, { 12, 13}, { -7, -91}, { 14, 15}, { 16, -90}, { -8, 17}, { 18, -89}, { -9, 19}, { 20, 21}, { -88, -10},
|
||||
{ 22, 23}, { -11, -87}, { 24, 25}, { 26, -86}, { -12, 27}, { 28, -85}, { -13, 29}, { 30, 31}, { 32, -84}, { -14, 33},
|
||||
{ 34, -15}, { -83, 35}, { 36, 37}, { -16, 38}, { -17, -82}, { 39, 40}, { 41, -81}, { 42, 43}, { 44, 45}, { 46, 47},
|
||||
{ 48, 49}, { 50, 51}, { 52, -18}, { -78, 53}, { -19, 54}, { 55, 56}, { 57, 58}, { -22, 59}, { 60, 61}, { 62, 63},
|
||||
{ 64, 65}, { 66, 67}, { 68, -20}, { -21, -79}, { -80, -25}, { 69, 70}, { -26, 71}, { 72, 73}, { 74, 75}, { 76, 77},
|
||||
{ 78, 79}, { 80, 81}, { 82, 83}, { 84, 85}, { 86, 87}, { 88, 89}, { 90, 91}, { 92, 93}, { 94, -23}, { -74, -75},
|
||||
{ -72, -73}, { -76, -77}, { -34, -35}, { -32, -33}, { -38, -39}, { -36, -37}, { -30, -31}, { -28, -29}, { -50, -51}, { -48, -49},
|
||||
{ -54, -55}, { -52, -53}, { -42, -43}, { -40, -41}, { -46, -47}, { -44, -45}, { -66, -67}, { -64, -65}, { -70, -71}, { -68, -69},
|
||||
{ -58, -59}, { -56, -57}, { -62, -63}, { -60, -61}, { -24, -27}
|
||||
};
|
||||
|
||||
static const int joc_huff_code_fine_generic[][2] =
|
||||
{
|
||||
{ -1, 1}, { 2, 3}, { -2,-192}, { 4, 5}, { 6, -3}, {-191, 7}, { 8, 9}, { -4,-190}, { 10, 11}, { -5,-189},
|
||||
{ 12, 13}, { -6, 14}, {-188, 15}, { 16, -7}, {-187, 17}, { 18, -8}, {-186, 19}, { 20, -9}, {-185, 21}, { 22, -10},
|
||||
{-184, 23}, { 24, -11}, { 25,-183}, { 26, 27}, { -12,-182}, { 28, 29}, { -13,-181}, { 30, 31}, {-180, -14}, { 32, 33},
|
||||
{ 34,-179}, { -15, 35}, { 36,-178}, { -16, 37}, { 38,-177}, { 39, -17}, { 40, 41}, {-176, 42}, { -18, 43}, { -19, 44},
|
||||
{-175, 45}, { 46,-174}, { -20, 47}, {-173, 48}, { 49, -21}, { 50, 51}, { 52, -22}, { 53, 54}, {-172, 55}, {-171, -23},
|
||||
{ 56, 57}, { 58,-170}, { 59, -24}, { -25, 60}, {-169, 61}, { 62, 63}, { 64, 65}, { 66, 67}, {-168, 68}, { -26, 69},
|
||||
{-167, -27}, { 70,-166}, {-165, 71}, { -29, 72}, { 73, 74}, { -30, 75}, { 76, 77}, { 78, 79}, { 80, -28}, { 81, 82},
|
||||
{ 83,-163}, { -31, -33}, {-164,-161}, { 84, 85}, { 86, 87}, { 88, 89}, { 90, 91}, { 92, 93}, { 94, 95}, { 96, 97},
|
||||
{ 98, 99}, { -32,-162}, { 100, 101}, { 102, 103}, { 104, 105}, { 106, 107}, { 108, 109}, { 110, 111}, {-160, 112}, { -36, -38},
|
||||
{ 113, 114}, { 115, 116}, { 117, 118}, { 119, 120}, { 121, 122}, { 123, 124}, { 125, 126}, { 127, 128}, { 129, 130}, { 131, 132},
|
||||
{ 133, -35}, {-158, 134}, {-155,-156}, { -37, -42}, { 135, 136}, { 137, 138}, { 139, 140}, { 141, 142}, { 143, 144}, { 145, 146},
|
||||
{ 147, 148}, { 149, 150}, { 151, 152}, { 153, 154}, { 155, 156}, { 157, 158}, { 159, 160}, { 161, 162}, { 163, 164}, { 165, 166},
|
||||
{ 167, 168}, { 169, 170}, { 171, 172}, { 173, 174}, { 175, 176}, { 177, 178}, { 179, 180}, { 181, 182}, { 183, 184}, {-157, 185},
|
||||
{ -45, -48}, { 186, 187}, { 188, 189}, { -34, -41}, { 190, -39}, { -60, -61}, { -58, -59}, { -64, -65}, { -62, -63}, { -52, -53},
|
||||
{ -50, -51}, { -56, -57}, { -54, -55}, { -76, -77}, { -74, -75}, { -80, -81}, { -78, -79}, { -68, -69}, { -66, -67}, { -72, -73},
|
||||
{ -70, -71}, { -47, -49}, { -44, -46}, {-124,-125}, {-122,-123}, {-128,-129}, {-126,-127}, {-116,-117}, {-114,-115}, {-120,-121},
|
||||
{-118,-119}, {-140,-141}, {-138,-139}, {-144,-145}, {-142,-143}, {-132,-133}, {-130,-131}, {-136,-137}, {-134,-135}, { -92, -93},
|
||||
{ -90, -91}, { -96, -97}, { -94, -95}, { -84, -85}, { -82, -83}, { -88, -89}, { -86, -87}, {-108,-109}, {-106,-107}, {-112,-113},
|
||||
{-110,-111}, {-100,-101}, { -98, -99}, {-104,-105}, {-102,-103}, {-154,-159}, {-148,-149}, {-146,-147}, {-152,-153}, {-150,-151},
|
||||
{ -40, -43}
|
||||
};
|
||||
|
||||
static const int joc_huff_code_coarse_coeff_sparse[][2] =
|
||||
{
|
||||
{ -1, 1}, { 2, 3}, { -2, -96}, { 4, 5}, { 6, -95}, { -3, 7}, { 8, 9}, { -4, 10}, { -94, 11}, { 12, -5},
|
||||
{ -93, 13}, { 14, 15}, { -6, -92}, { 16, 17}, { 18, -7}, { -91, 19}, { 20, -8}, { -90, 21}, { 22, 23}, { -9, -89},
|
||||
{ 24, 25}, { 26, -10}, { -88, 27}, { 28, 29}, { 30, -11}, { -87, 31}, { 32, 33}, { 34, 35}, { -12, -86}, { 36, 37},
|
||||
{ 38, -13}, { 39, -85}, { 40, 41}, { 42, 43}, { -14, -84}, { 44, 45}, { 46, 47}, { -83, -15}, { 48, 49}, { 50, -16},
|
||||
{ 51, 52}, { -82, 53}, { 54, -81}, { 55, 56}, { -17, 57}, { 58, -80}, { 59, 60}, { -18, 61}, { 62, 63}, { -79, 64},
|
||||
{ -19, -78}, { 65, 66}, { 67, 68}, { 69, -20}, { -77, -21}, { 70, 71}, { 72, 73}, { 74, -76}, { 75, -22}, { 76, 77},
|
||||
{ -75, 78}, { 79, 80}, { -54, -74}, { -73, 81}, { -23, 82}, { -50, -24}, { -55, -25}, { 83, -47}, { -49, -44}, { -71, 84},
|
||||
{ -48, -51}, { 85, -72}, { -26, -53}, { -70, -27}, { 86, -45}, { 87, 88}, { -68, 89}, { -29, -43}, { 90, -30}, { -46, -69},
|
||||
{ 91, -28}, { -52, -31}, { 92, -32}, { 93, -64}, { -67, 94}, { -36, -33}, { -63, -37}, { -65, -61}, { -66, -59}, { -34, -38},
|
||||
{ -41, -42}, { -35, -60}, { -39, -57}, { -56, -40}, { -62, -58}
|
||||
};
|
||||
|
||||
static const int joc_huff_code_fine_coeff_sparse[][2] =
|
||||
{
|
||||
{ 1, -1}, { 2, 3}, { 4, -2}, {-192, 5}, { 6, 7}, { 8, -3}, {-191, 9}, { 10, 11}, { 12,-190}, { -4, 13},
|
||||
{ 14, 15}, {-189, -5}, { 16, 17}, { 18, -6}, {-188, 19}, { 20, 21}, { -7,-187}, { 22, 23}, { -8, 24}, {-186, 25},
|
||||
{ -9, 26}, { 27,-185}, { 28, -10}, { 29, 30}, {-184, 31}, { -11, 32}, { 33,-183}, { 34, -12}, { 35,-182}, { 36, 37},
|
||||
{ 38, -13}, {-181, 39}, { 40, -14}, { 41,-180}, { 42, 43}, {-179, -15}, { 44, -16}, { 45,-178}, { 46, 47}, { 48, 49},
|
||||
{ 50,-177}, { -17, 51}, { -18, 52}, {-176, 53}, { 54, 55}, {-175, -19}, { 56, 57}, { 58, -20}, { 59,-174}, { 60, 61},
|
||||
{ -21, 62}, { 63,-173}, { 64, 65}, { 66,-172}, { 67, 68}, { -22, 69}, { 70, 71}, { -23, 72}, {-171, 73}, { 74, 75},
|
||||
{ 76, -24}, { 77,-170}, { -25, 78}, { 79, 80}, { 81,-169}, { 82, 83}, { 84, -26}, { 85,-168}, { 86, 87}, { 88, 89},
|
||||
{-167, 90}, { -27, 91}, { 92, -28}, { 93,-166}, { 94, -29}, { 95, 96}, { 97, 98}, {-165, 99}, { 100, -30}, {-164, 101},
|
||||
{ 102, 103}, { 104, 105}, {-163, 106}, { -31, 107}, { -32, 108}, { 109, 110}, {-161, 111}, {-160,-162}, { 112, -34}, { -33, 113},
|
||||
{ 114, 115}, { 116, 117}, { 118, 119}, { 120,-159}, { 121, 122}, { 123,-158}, { 124, 125}, { -36,-155}, { 126, 127}, { -35, 128},
|
||||
{ 129, 130}, {-157, 131}, {-156, 132}, { -37, 133}, { 134, 135}, {-154, -38}, { 136, 137}, { -39, -41}, { 138,-153}, { 139, -40},
|
||||
{-149, 140}, { 141, 142}, { 143, 144}, {-151, 145}, { 146, 147}, { 148, -42}, { -43, 149}, { 150, 151}, {-152, 152}, { -46, -98},
|
||||
{ 153, 154}, { 155,-147}, { 156, 157}, { 158,-107}, { 159, 160}, {-145,-150}, { -96, 161}, { 162, -45}, {-146, 163}, { 164, -97},
|
||||
{-108,-105}, {-148,-106}, { -44, 165}, { -94,-141}, { -99, 166}, { -89, 167}, { -50, -95}, {-100, -48}, {-144, 168}, { 169, 170},
|
||||
{ -51,-142}, { -90, -91}, { -47, -49}, { 171, -53}, { -93,-143}, {-137,-138}, { -55,-101}, { 172, 173}, { -54, -86}, { -88, -87},
|
||||
{-103, 174}, { 175, -61}, {-109, 176}, { 177, 178}, { -52,-139}, { -57,-140}, { 179, 180}, { -56,-136}, { -58,-102}, { 181, 182},
|
||||
{ -60,-135}, { 183,-104}, {-128,-134}, { -92, 184}, { -59, -62}, { 185, 186}, { -71,-133}, { 187,-127}, {-126, 188}, { -63, -64},
|
||||
{ -85,-132}, { 189, -66}, {-121,-125}, { 190, -68}, { -74, -75}, { -70, -73}, { -81, -65}, {-118,-131}, { -72,-110}, {-119,-120},
|
||||
{ -76, -84}, {-122,-130}, { -83,-117}, { -69, -78}, { -80, -82}, {-123,-124}, { -67,-116}, {-129, -77}, {-113,-114}, {-112,-115},
|
||||
{ -79,-111}
|
||||
};
|
||||
|
||||
static const int joc_huff_code_5ch_pos_index_sparse[][2] =
|
||||
{
|
||||
{ -1, 1}, { 2, 3}, { -4, -3}, { -2, -5}
|
||||
};
|
||||
|
||||
static const int joc_huff_code_7ch_pos_index_sparse[][2] =
|
||||
{
|
||||
{ -1, 1}, { 2, 3}, { 4, 5}, { -4, -3}, { -2, -5}, { -6, -7}
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,139 +0,0 @@
|
||||
// Port of src/joc_decode.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "eac3_transport/eac3_reader.h"
|
||||
#include "joc_core.h"
|
||||
#include "emdf/emdf_parser.h"
|
||||
#include "foundation/status.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
|
||||
namespace joc::joc {
|
||||
|
||||
struct ObjectSymbols {
|
||||
std::uint8_t present = 0;
|
||||
std::uint8_t sparse = 0;
|
||||
std::uint8_t n_bands = 0;
|
||||
std::uint8_t n_dpoints = 0;
|
||||
std::uint8_t n_channels = 0;
|
||||
std::int64_t q[JOC_MAX_DPOINTS][JOC_MAX_CORE_CHANNELS][JOC_MAX_PARAMETER_BANDS] = {};
|
||||
std::int16_t mtx[JOC_MAX_DPOINTS][JOC_MAX_CORE_CHANNELS][JOC_MAX_PARAMETER_BANDS] = {};
|
||||
std::uint8_t idx[JOC_MAX_DPOINTS][JOC_MAX_PARAMETER_BANDS] = {};
|
||||
std::int16_t vec[JOC_MAX_DPOINTS][JOC_MAX_PARAMETER_BANDS] = {};
|
||||
};
|
||||
|
||||
struct FrameSymbols {
|
||||
std::uint8_t n_objects = 0;
|
||||
std::uint8_t n_channels = 0;
|
||||
ObjectSymbols objects[JOC_MAX_OBJECTS];
|
||||
};
|
||||
|
||||
Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_frame_params* out,
|
||||
FrameSymbols* symbols);
|
||||
|
||||
Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_frame_params* out,
|
||||
emdf::Container* container, FrameSymbols* symbols);
|
||||
|
||||
Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
|
||||
std::uint32_t* out_trailing_bits);
|
||||
|
||||
// Scores this container as the frame's JOC metadata when the syncframe holds more
|
||||
// than one (spec section 8.2 and its Table 56 associate the OAMD and JOC payloads
|
||||
// of one frame by an equal group id). 2 = the two payloads share a group id and
|
||||
// ID14 parses as a complete joc() payload, 1 = ID14 parses, 0 = neither. A
|
||||
// container whose ID14 declares far more objects than its bytes could hold, or
|
||||
// whose ID11 carries no group id at all, is some other structure.
|
||||
int score_container_as_joc(const std::uint8_t* data, std::size_t size,
|
||||
const emdf::Container& container);
|
||||
|
||||
// Parameters for an audio frame that carries no JOC payload: every object is
|
||||
// "present" with an all-zero matrix, so the kernel renders silence while its
|
||||
// analysis and synthesis state keep advancing in step with the stream.
|
||||
void make_silent_frame_params(joc_frame_params* out);
|
||||
|
||||
std::int16_t num_channels_for_config(std::uint32_t dmx_config_idx);
|
||||
|
||||
std::int16_t num_bands_for_index(std::uint32_t num_bands_idx);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Channel identity / bed layout (spec sections 3, 10.2, 10.4)
|
||||
//
|
||||
// This is the position layer only: it decides which decoded bed channel feeds
|
||||
// which JOC input lane and which ADM bed slot the extra pair occupies. The DSP
|
||||
// never reads it.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
enum class Position : std::uint8_t {
|
||||
kNone = 0,
|
||||
kLeft,
|
||||
kRight,
|
||||
kCenter,
|
||||
kLfe,
|
||||
kLeftSurround,
|
||||
kRightSurround,
|
||||
kLeftRearSurround,
|
||||
kRightRearSurround,
|
||||
kVerticalHeightLeft,
|
||||
kVerticalHeightRight,
|
||||
kTopSurroundLeft,
|
||||
kTopSurroundRight,
|
||||
kCenterSurround,
|
||||
kTopSurround,
|
||||
kVerticalHeightCenter,
|
||||
kLfe2,
|
||||
};
|
||||
|
||||
// Table E2.5 expansion; the field's most significant bit is table bit 0.
|
||||
void expand_chanmap(std::uint16_t chanmap, std::vector<Position>* out);
|
||||
|
||||
// A/52 acmod -> full-band positions in decoding order (LFE excluded).
|
||||
void acmod_positions(std::uint8_t acmod, std::vector<Position>* out);
|
||||
|
||||
// The pair joc_dmx_config_idx declares; empty for configurations 0 and 3.
|
||||
void expected_pair(std::uint8_t dmx_config_idx, std::vector<Position>* out);
|
||||
|
||||
// The extra pair a dependent-substream chanmap adds on top of the core channels.
|
||||
void chanmap_extra_pair(const std::vector<Position>& core, std::uint16_t chanmap,
|
||||
std::vector<Position>* out);
|
||||
|
||||
// Decoded-bed channel count implied by the JOC core channel count.
|
||||
int bed_channels_for_core(int core_channels);
|
||||
|
||||
// bed[lane] = decoded PCM channel index feeding JOC lane 'lane'.
|
||||
struct BedPlan {
|
||||
int channels = 0; // 6 or 8
|
||||
int lfe_channel = -1;
|
||||
int lane[JOC_MAX_CORE_CHANNELS] = {-1, -1, -1, -1, -1, -1, -1};
|
||||
int extra_pair_slot[2] = {-1, -1}; // ADM skeleton
|
||||
};
|
||||
|
||||
Status compute_bed_plan(const eac3::Frame* core_frame, bool has_core, bool has_dependent,
|
||||
bool has_chanmap, std::uint16_t chanmap, std::uint8_t dmx_config_idx,
|
||||
BedPlan* out);
|
||||
|
||||
// Bed plan driven by joc_dmx_config_idx alone, for callers that hold no L2
|
||||
// header: the extra pair is the one the configuration declares.
|
||||
Status compute_bed_plan_for_config(std::uint8_t dmx_config_idx, BedPlan* out);
|
||||
|
||||
// Identity disagreements that leave the DSP input unchanged (spec section 10.1:
|
||||
// channel identity never enters the DSP): the extra pair's name is metadata only,
|
||||
// so a mismatch is reported through CrossLayerNotes instead of refusing the frame.
|
||||
struct CrossLayerNotes {
|
||||
bool chanmap_pair_mismatch = false; // spec 11.2 check 1: chanmap vs k
|
||||
bool bed_mask_mismatch = false; // spec 11.2 check 2: OAMD bed mask vs k
|
||||
};
|
||||
|
||||
// Spec section 11.2 cross-layer consistency. A mismatch in the core channel count
|
||||
// or in joc_ext_config_idx changes the DSP input and is a variant error; the two
|
||||
// identity checks are recorded in `notes` and the frame still renders.
|
||||
Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core_frame,
|
||||
bool has_core, bool has_dependent, bool has_chanmap,
|
||||
std::uint16_t chanmap,
|
||||
const std::vector<oamd::BedAssignment>* bed_assignments,
|
||||
std::string* error, CrossLayerNotes* notes = nullptr);
|
||||
|
||||
} // namespace joc::joc
|
||||
Loaded 100 of 168 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user