diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff17a1a..e621e0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,13 @@ jobs: 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 @@ -24,15 +31,24 @@ jobs: - name: Test run: ctest --test-dir build --output-on-failure -C Release - # Publish the install tree: the shared library and the CLI. + # Package the CLI with its runtime assets at the archive root and the + # libraries in lib/ below it, so the download can be unpacked and run as it + # is: the CLI is beside the shared library and finds it through the rpath + # recorded in CMakeLists.txt. - name: Stage run: cmake --install build --config Release --prefix stage + - name: Package + shell: bash + run: | + folder="joc_cli_${{ matrix.label }}" + mkdir -p "package/$folder/lib" + cp -r stage/bin/. "package/$folder/" + cp -r stage/lib/. "package/$folder/lib/" + - name: Upload uses: actions/upload-artifact@v4 with: - name: joc-core-${{ runner.os }} - path: | - stage/bin/* - stage/lib/* + name: joc_cli_${{ matrix.label }} + path: package/** if-no-files-found: error diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..a5acf41 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,90 @@ +name: release + +# A tag is the release trigger: pushing v builds every platform, checks +# that the tag matches include/joc_version.h and publishes the packaged archives. +on: + push: + tags: ["v*"] + +permissions: + contents: write + +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 + shell: bash + run: | + declared=$(sed -n 's/.*JOC_VERSION_STRING *"\([^"]*\)".*/\1/p' include/joc_version.h) + tag="${GITHUB_REF_NAME#v}" + if [ -z "$declared" ] || [ "$tag" != "$declared" ]; then + echo "tag $GITHUB_REF_NAME does not match the declared version (${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 + + # The CLI and its runtime assets sit at the archive root with the libraries + # in lib/ below, so the download can be unpacked and run as it is. + - name: Stage + run: cmake --install build --config Release --prefix stage + + - name: Package + shell: bash + run: | + folder="joc_cli_${{ matrix.label }}" + mkdir -p "package/$folder/lib" + cp -r stage/bin/. "package/$folder/" + cp -r stage/lib/. "package/$folder/lib/" + + - name: Archive + run: cmake -E tar cf "joc_cli_${{ matrix.label }}.zip" --format=zip "package/joc_cli_${{ matrix.label }}" + + - name: Upload + uses: actions/upload-artifact@v4 + with: + name: package-${{ matrix.label }} + path: joc_cli_*.zip + if-no-files-found: error + + release: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: + path: dist + merge-multiple: true + # The archives are attached to the tag's release, whether that release was + # created here or beforehand in the web interface. + - 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index c58ae30..d502df4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,23 @@ cmake_minimum_required(VERSION 3.20) -project(joc_core VERSION 0.1.0 LANGUAGES C CXX) +# 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 @@ -246,14 +263,17 @@ 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 installed CLI is in bin/ and the shared library in lib/, and ELF/Mach-O -# strip the build rpath on install, so the relative lookup has to be recorded -# here or `bin/joc_cli` cannot find `../lib/libjoc_core.*`. +# 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") + 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") + set_target_properties(joc_cli PROPERTIES + INSTALL_RPATH "$ORIGIN/lib;$ORIGIN/../lib") endif() endif() diff --git a/README.en.md b/README.en.md index c010fec..c0037b0 100644 --- a/README.en.md +++ b/README.en.md @@ -26,12 +26,13 @@ unit-tested by `.github/workflows/ci.yml`. Floating-point behaviour is part of t byte-exact acceptance, so fast-math is never enabled: `/fp:precise` on MSVC, `-fno-fast-math` elsewhere. -Windows Release builds target AVX2 by default (`JOC_ENABLE_AVX2`, ON, see -`CMakeLists.txt`). That switch is itself part of the byte-exact acceptance — the -SHA-256 of every rendered output is identical — and buys 12.4% on the SOFA binaural -kernel and 1.8% on Rosella. The price is a runtime requirement: such a `joc_core.dll` -executes AVX2 instructions and dies on an illegal instruction on pre-2013 x86. There -is no runtime dispatch, so a binary is one or the other: +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 SOFA binaural kernel and 1.8% on +Rosella, while a CPU without AVX2 still runs the baseline (SSE2) path. To produce a +baseline-only binary: ```powershell cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DJOC_ENABLE_AVX2=OFF @@ -67,8 +68,8 @@ other two modes, and without `-o` the result lands in `output/`. ```powershell # Default: ADM BWF (inherently 24-bit, so there is no format option) -joc_cli "07. Gold Forever (2021 Master).m4a" -# -> output/07. Gold Forever (2021 Master).adm.wav +joc_cli input.m4a +# -> output/input.adm.wav joc_cli input.m4a -o out/adm.wav # explicit output @@ -148,11 +149,51 @@ native library and are never edited (see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NO ## Compatibility note +### How `joc_clipgain` is applied + +`joc_clipgain` is folded into the **analysis-side scalar**: `scalar = 1/16 x g` (`g = 1 + (y/32)·2^(x-4)`, +from the 8-bit field `x:3 | y:5`). 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)`. + `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$, or an OAMD standard bed mask does, the mismatch is recorded as a +diagnostic and the frame still renders, with the bed mapping following the channel codes the +`chanmap` declares. 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. + +An audio frame with no JOC EMDF at all (for example when the encoder wrote none for the first +frame) emits silent object channels while the QMF analysis and synthesis state keep advancing, +and it is counted separately in the statistics. + ## License MIT, see [LICENSE](LICENSE). Third-party provenance and patent boundaries are in diff --git a/README.md b/README.md index 3afc34b..20e8436 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [English](README.en.md) · [数学说明](docs/math.md) · [双耳渲染](docs/binaural.md) · [SIMD 派发](docs/simd.md) JustOneCacophony 的 C++ 实现:E-AC-3 JOC 码流解析、对象重建与渲染的执行内核。它从 E-AC-3 -同步帧中提取 EMDF、ID14 JOC 参数与 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 5.1 PCM +同步帧中提取 EMDF、ID14 JOC 参数与 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 PCM(N=5 或 7) 重建 LFE 与 15 路对象 PCM,并输出 ADM BWF、指定扬声器布局的 WAV,或用编译好的 HRTF 方向场 直接输出双耳 WAV。 @@ -22,10 +22,11 @@ Windows(MSVC / VS 2022,静态 CRT)、Linux 与 macOS 由 `.github/workflow 单元测试;本地只验证 MSVC。浮点行为是逐字节验收的一部分,因此不启用 fast-math:MSVC 用 `/fp:precise`,其他编译器用 `-fno-fast-math`。 -Windows Release 默认带 `/arch:AVX2`(`JOC_ENABLE_AVX2`,默认 ON,见 `CMakeLists.txt`)。这个 -开关是逐字节验收过的:所有渲染产物的 SHA-256 完全一致,换来 SOFA 双耳内核 12.4%、Rosella -1.8% 的提升。代价是运行要求——这样的 `joc_core.dll` 会执行 AVX2 指令,在 2013 年以前的 x86 上 -直接非法指令退出。没有运行时派发,一个二进制只能二选一,所以: +最重的双耳循环由**运行时派发**:同一份二进制里装多套 ISA 代码,启动时按 CPUID/XGETBV 选最宽 +的一套,输出逐字节不变(见 [SIMD 与运行时派发](docs/simd.md))。`JOC_ENABLE_AVX2` / +`JOC_ENABLE_AVX512`(默认都 ON)只决定是否把这些单元编进二进制:带上 AVX2 单元让 SOFA 双耳 +内核快 12.4%、Rosella 快 1.8%,而没有 AVX2 的 CPU 仍走基线(SSE2)路径。要产出只带基线 ISA +的二进制: ```powershell cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DJOC_ENABLE_AVX2=OFF @@ -57,8 +58,8 @@ UTF-8,因此日文/中文等非 ASCII 路径(含 ffmpeg 子进程与输出 ```powershell # 默认:ADM BWF(本身就是 24-bit,没有也不需要格式参数) -joc_cli "07. Gold Forever (2021 Master).m4a" -# -> output/07. Gold Forever (2021 Master).adm.wav +joc_cli input.m4a +# -> output/input.adm.wav joc_cli input.m4a -o out/adm.wav # 指定输出 @@ -129,9 +130,41 @@ docs/ 数学说明、双耳渲染流程与 SIMD 派发 ## 兼容性说明 +### `joc_clipgain` 的施加方式 + +`joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 1 + (y/32)·2^(x−4)`,8 bit 字段 +`x:3 | y:5`)。出口的 ×16 抵消 1/16,净效果是 `×g`,因此 clamp 处在增益**之后** +(`clamp(16·IQMF·g_interp)`)。标量按帧保存在两个槽里:本帧前 256 个采样从上一帧值线性过渡、之后为 +常数,复位后的第一帧直接取新值。`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。 + `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 掩码与 $k$ 不一致时,只记 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 EMDF 时(例如编码器没写首帧元数据),该帧的对象通道静音, +QMF 分析与合成状态照常推进,并在统计里单独计数。 + ## 许可 MIT,见 [LICENSE](LICENSE);第三方来源与专利边界见 diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index dde6081..b547067 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -29,6 +29,15 @@ $$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;其使用、复制与再分发仍受源 diff --git a/docs/math.en.md b/docs/math.en.md index 20e55b1..730efac 100644 --- a/docs/math.en.md +++ b/docs/math.en.md @@ -211,7 +211,7 @@ When a frame contains two data points, `offset_ts` gives the segment boundary. E ## 4. Analysis QMF for core PCM -The matrix input uses core channels L, R, C, Ls, and Rs; LFE follows a separate path. Core PCM is first scaled as +The matrix input uses $N$ core channels ($N\in\{5,7\}$, read from $joc\_dmx\_config\_idx$: $k=0\ldots4$ maps to $N=5,7,7,5,7$); LFE follows a separate path. Core PCM is first scaled as $$ \widetilde x_c[n]=\frac{x_c[n]}{16}. @@ -238,29 +238,30 @@ $$ \qquad c\in\{L,R,C\}. $$ -Ls and Rs use the same ten-slot delay and a $-j$ rotation for $b>0$: +For Ls and Rs the ten-slot delay, the $-j$ rotation for $b>0$ and the band-0 21-tap +complex FIR are all gated by the **same switch** $\sigma(k)=1\iff k\in\{3,4\}$: $$ -\widehat X_{c,b,t}=-jX_{c,b,t-10}, -\qquad c\in\{Ls,Rs\},\ b>0. +\widehat X_{c,b,t}= +\begin{cases} +-j\,X_{c,b,t-10}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b>0,\\[1mm] +\sum_{m=0}^{20}h_mX_{c,0,t-m}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b=0,\\[1mm] +X_{c,b,t-10}, & \text{otherwise}. +\end{cases} $$ -Band 0 of each surround channel additionally passes through a 21-tap complex FIR: - -$$ -\widehat X_{c,0,t}= -\sum_{k=0}^{20}h_kX_{c,0,t-k}. -$$ - -These delays and filter histories are decoder state and cannot be reset independently for every frame. +For $N=7$ the sixth and seventh core channels (Lb/Rb or Tfl/Tfr) behave like the front +channels and only take the ten-slot delay: channel identity affects metadata and the ADM +placement, not the DSP. These delays and filter histories are decoder state and cannot be +reset independently for every frame. ## 6. Object matrix -For each object $o$, subband $b$, and slot $t$, the object's frequency-domain value is a linear combination of the five core channels: +For each object $o$, subband $b$, and slot $t$, the object's frequency-domain value is a linear combination of the $N$ core channels: $$ Z_{o,b,t}= -\sum_{c=0}^{4} +\sum_{c=0}^{N-1} M_{o,c,b,t}\widehat X_{c,b,t}. $$ diff --git a/docs/math.md b/docs/math.md index 27eaf15..237453f 100644 --- a/docs/math.md +++ b/docs/math.md @@ -211,7 +211,8 @@ $$ ## 4. 核心 PCM 的 analysis QMF -矩阵输入使用核心声道 L、R、C、Ls、Rs;LFE 走独立路径。核心 PCM 先缩放为 +矩阵输入使用 $N$ 个核心声道($N\in\{5,7\}$,由 $joc\_dmx\_config\_idx$ 查表得到, +$k=0\ldots4$ 对应 $N=5,7,7,5,7$);LFE 走独立路径。核心 PCM 先缩放为 $$ \widetilde x_c[n]=\frac{x_c[n]}{16}. @@ -238,29 +239,29 @@ $$ \qquad c\in\{L,R,C\}. $$ -Ls、Rs 同样延迟 10 个时槽,并在 $b>0$ 时作 $-j$ 旋转: +Ls、Rs 的 10 时槽延迟、$b>0$ 的 $-j$ 旋转与 band-0 的 21-tap 复 FIR 由**同一个开关** +$\sigma(k)=1\iff k\in\{3,4\}$ 门控: $$ -\widehat X_{c,b,t}=-jX_{c,b,t-10}, -\qquad c\in\{Ls,Rs\},\ b>0. +\widehat X_{c,b,t}= +\begin{cases} +-j\,X_{c,b,t-10}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b>0,\\[1mm] +\sum_{m=0}^{20}h_mX_{c,0,t-m}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b=0,\\[1mm] +X_{c,b,t-10}, & \text{其他情况}. +\end{cases} $$ -环绕声道的 band 0 还经过 21-tap 复 FIR: - -$$ -\widehat X_{c,0,t}= -\sum_{k=0}^{20}h_kX_{c,0,t-k}. -$$ - -这些延迟和滤波历史属于解码状态,不能按帧独立清零。 +$N=7$ 时第 6、7 个核心声道(Lb/Rb 或 Tfl/Tfr)与前级相同,只做 10 时槽延迟: +通道身份只影响元数据与 ADM 落位,不进入 DSP。这些延迟和滤波历史属于解码状态, +不能按帧独立清零。 ## 6. 对象矩阵 -对每个对象 $o$、子带 $b$ 和时槽 $t$,对象频域值为五个核心声道的线性组合: +对每个对象 $o$、子带 $b$ 和时槽 $t$,对象频域值为 $N$ 个核心声道的线性组合: $$ Z_{o,b,t}= -\sum_{c=0}^{4} +\sum_{c=0}^{N-1} M_{o,c,b,t}\widehat X_{c,b,t}. $$ diff --git a/include/eac3joc_core.h b/include/eac3joc_core.h index 6cddf54..d6487a5 100644 --- a/include/eac3joc_core.h +++ b/include/eac3joc_core.h @@ -23,11 +23,12 @@ extern "C" { #endif enum { - EJOC_ABI_VERSION = 1, + EJOC_ABI_VERSION = 2, EJOC_FRAME_SAMPLES = 1536, EJOC_TIMESLOTS = 24, EJOC_SUBBANDS = 64, - EJOC_CORE_CHANNELS = 5, + 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_MAX_OBJECTS = 15, EJOC_MAX_DPOINTS = 2, @@ -47,15 +48,18 @@ typedef void* ejoc_binaural_renderer_handle; /* Fixed array layouts used by ejoc_renderer_process(): - bed5_planar [5][1536] + 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][5][23] + 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 @@ -69,21 +73,24 @@ 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); /* -Enables or disables the Ls/Rs band-0 21-tap DC compensation. The caller derives -it from the JOC downmix configuration: only configurations 3 and 4 enable the -filter. When disabled, band 0 keeps the common per-band processing (surround -delay plus -j rotation) instead of being overwritten by the FIR. The delay line -and DC history advance either way, so the flag may change between frames. -Defaults to enabled when never called. +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 int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled); 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* bed5_planar, + 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, diff --git a/include/joc_core.h b/include/joc_core.h index 6c3acfe..40d6a96 100644 --- a/include/joc_core.h +++ b/include/joc_core.h @@ -34,7 +34,9 @@ #include #include -#define JOC_ABI_VERSION 3u +#include "joc_version.h" + +#define JOC_ABI_VERSION 5u #define JOC_FRAME_PARAMS_VERSION 1u #define JOC_EMDF_INFO_VERSION 1u #define JOC_TASK_CONFIG_VERSION 3u @@ -193,7 +195,7 @@ typedef struct joc_frame_params { uint8_t n_channels; /* 5 or 7 */ uint8_t clipgain_x_bits; uint8_t clipgain_y_bits; - uint8_t reserved; + uint8_t objects_clamped; /* num_objects_bits+1 exceeded 15 */ uint32_t seq_count; /* 10-bit JOC sequence counter, parsed only */ uint32_t present_mask; /* bit i == object i present */ uint32_t data_end_bits; /* bit position just after joc_data */ diff --git a/include/joc_stream.h b/include/joc_stream.h index 280b37a..5c2076b 100644 --- a/include/joc_stream.h +++ b/include/joc_stream.h @@ -102,6 +102,16 @@ typedef struct joc_stream_status_info { 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, 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() */ } joc_stream_status_info; diff --git a/include/joc_version.h b/include/joc_version.h new file mode 100644 index 0000000..0874ca6 --- /dev/null +++ b/include/joc_version.h @@ -0,0 +1,16 @@ +/* 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. + */ +#ifndef JOC_VERSION_H +#define JOC_VERSION_H + +#define JOC_VERSION_MAJOR 0 +#define JOC_VERSION_MINOR 1 +#define JOC_VERSION_PATCH 0 +#define JOC_VERSION_STRING "0.1.0" + +#endif /* JOC_VERSION_H */ diff --git a/src/api/joc_api.cpp b/src/api/joc_api.cpp index a0c56ce..6bafb7b 100644 --- a/src/api/joc_api.cpp +++ b/src/api/joc_api.cpp @@ -11,16 +11,8 @@ namespace { -thread_local std::string g_detail; - joc_error finish(const joc::Status& status) { - if (status.ok()) { - g_detail.clear(); - return JOC_OK; - } - g_detail.assign(status.stage()); - g_detail.append(": "); - g_detail.append(status.message()); + joc::record_error_detail(status); return status.code(); } @@ -49,7 +41,7 @@ extern "C" { std::uint32_t JOC_CALL joc_abi_version(void) { return JOC_ABI_VERSION; } -const char* JOC_CALL joc_version_string(void) { return "0.1.0-m1"; } +const char* JOC_CALL joc_version_string(void) { return JOC_VERSION_STRING; } std::uint32_t JOC_CALL joc_event_size(void) { return static_cast(sizeof(joc_event)); } std::uint32_t JOC_CALL joc_task_config_size(void) { @@ -167,7 +159,7 @@ const char* JOC_CALL joc_error_stage(joc_error code) { } } -const char* JOC_CALL joc_last_error_detail(void) { return g_detail.c_str(); } +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) { diff --git a/src/api/joc_stream_api.cpp b/src/api/joc_stream_api.cpp index a48c5cc..d509ac0 100644 --- a/src/api/joc_stream_api.cpp +++ b/src/api/joc_stream_api.cpp @@ -13,6 +13,9 @@ struct joc_stream { 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(); } @@ -93,12 +96,14 @@ joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer* 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, &consumed); + stream->instance.push_bed(input->pcm, input->sample_count, channels, &consumed); if (consumed_samples != nullptr) { *consumed_samples = static_cast(consumed); } - return pushed; }(); + return pushed; + }(); return finish_stream(status); } @@ -149,6 +154,11 @@ joc_error JOC_CALL joc_stream_status(const joc_stream* stream, joc_stream_status 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; return JOC_OK; diff --git a/src/cli/joc_cli.cpp b/src/cli/joc_cli.cpp index e510497..068ce98 100644 --- a/src/cli/joc_cli.cpp +++ b/src/cli/joc_cli.cpp @@ -80,6 +80,7 @@ struct Options { bool dry_run = false; bool quiet = false; bool help = false; + bool version = false; }; const char* kLayoutChoices = @@ -88,6 +89,8 @@ const char* kLayoutChoices = 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" @@ -210,6 +213,7 @@ void parse_args(const std::vector& arguments, Options* options) { for (int index = 1; index < argc; ++index) { const std::string arg = arguments[static_cast(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); } @@ -530,6 +534,11 @@ int main(int argc, char** argv) { 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(joc_abi_version())); + return 0; + } if (options.help) { print_usage(); return 0; diff --git a/src/eac3_transport/eac3_reader.cpp b/src/eac3_transport/eac3_reader.cpp index 568bdd1..48df7df 100644 --- a/src/eac3_transport/eac3_reader.cpp +++ b/src/eac3_transport/eac3_reader.cpp @@ -7,9 +7,111 @@ 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((static_cast(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(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(kAc3FrameWords[fscod][frmsizecod]) * 2u; + frame.acmod = static_cast(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(read_bits(data, base + bit, 1u)); + if (out != nullptr) { + *out = frame; + } + return true; + } + + frame.codec = 1; + frame.strmtyp = static_cast(read_bits(data, base + 16u, 2u)); + frame.substreamid = static_cast(read_bits(data, base + 18u, 3u)); + const std::uint32_t frmsiz = read_bits(data, base + 21u, 11u); + frame.size = (static_cast(frmsiz) + 1u) * 2u; + // fscod==3 also consumes two bits (fscod2), so acmod always starts at bit 36. + frame.acmod = static_cast(read_bits(data, base + 36u, 3u)); + frame.lfeon = static_cast(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(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; @@ -45,44 +147,43 @@ FrameReader::Next FrameReader::next(Frame* out) { } const std::uint8_t* p = buffer_.data() + consumed_; - // The reference implementation rejects a frame whose header does not fit, - // rather than silently resynchronising on the next 0x0B77. + // 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, "E-AC-3 syncframe header truncated at end of input"); + fail(JOC_ERR_EAC3_SYNCFRAME, "syncframe header truncated at end of input"); return Next::Fail; } return Next::End; } - const std::uint16_t syncword = static_cast((static_cast(p[0]) << 8) | p[1]); - if (syncword != kSyncword) { - fail(JOC_ERR_EAC3_SYNCFRAME, "invalid E-AC-3 syncword (silent resynchronisation is not allowed)"); + 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; } - // frmsiz: 11 bits spread over the low 3 bits of byte 2 and all of byte 3, - const std::size_t words = - static_cast(((p[2] & 0x07u) << 8) | p[3]) + 1u; - const std::size_t frame_bytes = words * 2u; - - if (frame_bytes > available) { + if (frame.size > available) { if (!finished_) { return Next::End; } fail(JOC_ERR_BITSTREAM_TRUNCATED, - "last E-AC-3 syncframe extends past end of input (declared " + - std::to_string(frame_bytes) + " bytes, remaining " + + "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) { - out->data = p; - out->size = frame_bytes; - out->offset = base_offset_ + consumed_; + frame.data = p; + frame.offset = base_offset_ + consumed_; + *out = frame; } - consumed_ += frame_bytes; + consumed_ += frame.size; stream_offset_ = base_offset_ + consumed_; ++frames_emitted_; return Next::Ok; @@ -93,20 +194,15 @@ joc_error FrameReader::frame_bytes(const std::uint8_t* data, std::size_t size, s if (data == nullptr || out_frame_bytes == nullptr) { return JOC_ERR_INVALID_ARGUMENT; } - if (offset + kHeaderBytes > size) { - return JOC_ERR_EAC3_SYNCFRAME; + Frame frame; + if (!parse_syncframe_header(data, size, offset, &frame)) { + return offset + kHeaderBytes > size ? JOC_ERR_EAC3_SYNCFRAME + : JOC_ERR_EAC3_SYNCFRAME; } - if (static_cast((static_cast(data[offset]) << 8) | data[offset + 1]) != - kSyncword) { - return JOC_ERR_EAC3_SYNCFRAME; - } - const std::size_t words = - static_cast(((data[offset + 2] & 0x07u) << 8) | data[offset + 3]) + 1u; - const std::size_t frame_bytes = words * 2u; - if (offset + frame_bytes > size) { + if (offset + frame.size > size) { return JOC_ERR_BITSTREAM_TRUNCATED; } - *out_frame_bytes = frame_bytes; + *out_frame_bytes = frame.size; return JOC_OK; } diff --git a/src/eac3_transport/eac3_reader.h b/src/eac3_transport/eac3_reader.h index 11ad854..91dd4b1 100644 --- a/src/eac3_transport/eac3_reader.h +++ b/src/eac3_transport/eac3_reader.h @@ -1,4 +1,3 @@ - #pragma once #include @@ -10,12 +9,34 @@ 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 { diff --git a/src/emdf/emdf_parser.cpp b/src/emdf/emdf_parser.cpp index 20ec67f..2f85c1f 100644 --- a/src/emdf/emdf_parser.cpp +++ b/src/emdf/emdf_parser.cpp @@ -113,12 +113,16 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi : 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 ignored = 0; - if (!bits::variable_bits(reader, 2, 8, &ignored)) { + 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(value); + has_group_id = 1; } if (reader.read(1) != 0u) { if (!reader.skip(8)) { @@ -160,6 +164,8 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi Payload& entry = container.payloads[container.payload_count++]; entry.id = static_cast(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; @@ -206,7 +212,8 @@ void marker_offsets(const std::uint8_t* data, std::size_t size, std::vectorbegin(), out->end()); } -Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out) { +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"); } @@ -264,6 +271,31 @@ Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out) } 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()) + @@ -273,6 +305,31 @@ Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out) 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* out) { out->assign(container.raw_size, 0u); diff --git a/src/emdf/emdf_parser.h b/src/emdf/emdf_parser.h index ad68491..667c7ef 100644 --- a/src/emdf/emdf_parser.h +++ b/src/emdf/emdf_parser.h @@ -20,6 +20,11 @@ struct Payload { std::uint16_t sample_offset = 0; 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 { @@ -43,7 +48,26 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi // All candidate 0x5838 bit offsets over the eight alignments, ascending. void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector* out); -Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* 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). diff --git a/src/foundation/bit_reader.cpp b/src/foundation/bit_reader.cpp index 0a2e307..e344598 100644 --- a/src/foundation/bit_reader.cpp +++ b/src/foundation/bit_reader.cpp @@ -21,8 +21,8 @@ bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups, std:: } value = (value + 1u) << width; } - // Same failure mode as the reference implementation: an extension chain - // that never terminates is a syntax error, not a truncation. + // 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; } diff --git a/src/foundation/status.h b/src/foundation/status.h index c126fd6..3abe932 100644 --- a/src/foundation/status.h +++ b/src/foundation/status.h @@ -33,6 +33,22 @@ private: 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. +inline 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"; diff --git a/src/joc_bitstream/joc_parser.cpp b/src/joc_bitstream/joc_parser.cpp index 18c8055..6650d3c 100644 --- a/src/joc_bitstream/joc_parser.cpp +++ b/src/joc_bitstream/joc_parser.cpp @@ -199,28 +199,25 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra out->num_objects_bits = static_cast(reader.read(6)); out->ext_config_idx = static_cast(reader.read(3)); - const std::uint32_t n_objects = static_cast(out->num_objects_bits) + 1u; + const std::uint32_t declared_objects = static_cast(out->num_objects_bits) + 1u; const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx); if (n_channels < 0) { return syntax_fail("unknown JOC downmix configuration " + std::to_string(out->dmx_config_idx)); } out->n_channels = static_cast(n_channels); - if (n_objects > JOC_MAX_OBJECTS) { - // The reference implementation has no check here and fails later inside - // NumPy; the port reports it explicitly (plan 28.2). - return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kJoc, - "JOC frame declares " + std::to_string(n_objects) + - " objects, the ABI supports at most " + - std::to_string(static_cast(JOC_MAX_OBJECTS))); - } + // A declared object count above 15 is clamped to 15: the frame is parsed for + // 15 objects only, and the clamp is reported separately. + out->objects_clamped = declared_objects > JOC_MAX_OBJECTS ? 1u : 0u; + const std::uint32_t n_objects = + std::min(declared_objects, JOC_MAX_OBJECTS); out->n_objects = static_cast(n_objects); out->clipgain_x_bits = static_cast(reader.read(3)); out->clipgain_y_bits = static_cast(reader.read(5)); out->seq_count = reader.read(10); - // clipgain = 1 + (y/32) * 2^(x-4). The reference multiplies by an exact - // exactly for the whole legal range. + // clipgain = 1 + (y/32) * 2^(x-4); ldexp builds the power of two exactly + // over the whole legal range. out->clipgain = 1.0 + static_cast(out->clipgain_y_bits) / 32.0 * std::ldexp(1.0, static_cast(out->clipgain_x_bits) - 4); @@ -361,13 +358,36 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra return Status::success(); } +int score_container_as_joc(const std::uint8_t* data, std::size_t size, + const emdf::Container& container) { + const emdf::Payload* payload = container.find(emdf::kIdJoc); + const emdf::Payload* oamd = container.find(emdf::kIdOamd); + if (payload == nullptr || oamd == nullptr) { + return 0; + } + bool parses = false; + std::vector bytes; + if (emdf::extract_payload_bytes(data, size, *payload, &bytes).ok()) { + joc_frame_params params; + FrameSymbols symbols; + parses = parse_id14(bytes.data(), bytes.size(), ¶ms, &symbols).ok(); + } + const bool associated = oamd->has_group_id != 0u && payload->has_group_id != 0u && + oamd->group_id == payload->group_id; + if (associated && parses) { + return 2; + } + return parses ? 1 : 0; +} + Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_frame_params* out, emdf::Container* container, FrameSymbols* symbols) { if (frame == nullptr || out == nullptr) { return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null frame or output"); } emdf::Container local; - const Status status = emdf::find_joc_emdf(frame, frame_size, &local); + const Status status = + emdf::find_joc_emdf(frame, frame_size, &local, &score_container_as_joc, nullptr); if (!status.ok()) { return status; } @@ -412,4 +432,516 @@ Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size, return Status::success(); } +void make_silent_frame_params(joc_frame_params* out) { + if (out == nullptr) { + return; + } + std::memset(out, 0, sizeof(*out)); + out->struct_size = sizeof(joc_frame_params); + out->struct_version = JOC_FRAME_PARAMS_VERSION; + out->dmx_config_idx = 0u; + out->n_channels = static_cast(JOC_CORE_CHANNELS); + out->n_objects = static_cast(JOC_MAX_OBJECTS); + out->present_mask = (1u << JOC_MAX_OBJECTS) - 1u; + out->clipgain = 1.0; + for (unsigned object = 0; object < JOC_MAX_OBJECTS; ++object) { + out->objects[object].present = 1u; + out->objects[object].num_bands_idx = 0u; // one parameter band + out->objects[object].n_bands = 1u; + out->objects[object].sparse = 0u; + out->objects[object].quant_idx = 0u; + out->objects[object].slope_idx = 0u; + out->objects[object].num_dpoints_bits = 0u; + out->objects[object].n_dpoints = 1u; + } +} + +// --------------------------------------------------------------------------- +// Channel identity / bed layout (spec sections 3, 10.2, 10.4) +// --------------------------------------------------------------------------- + +namespace { + +struct PositionBit { + Position position; + std::uint32_t bit; +}; + +// Position -> ffmpeg channel bit. A decoded bed frame lists its channels in +// ascending bit order, so sorting the positions by bit reproduces the decoder's +// channel order without asking the decoder. +constexpr PositionBit kPositionBits[] = { + {Position::kLeft, 0x1u}, + {Position::kRight, 0x2u}, + {Position::kCenter, 0x4u}, + {Position::kLfe, 0x8u}, + {Position::kLeftRearSurround, 0x10u}, + {Position::kRightRearSurround, 0x20u}, + {Position::kCenterSurround, 0x100u}, + {Position::kLeftSurround, 0x200u}, + {Position::kRightSurround, 0x400u}, + {Position::kTopSurround, 0x800u}, + {Position::kVerticalHeightLeft, 0x1000u}, + {Position::kVerticalHeightCenter, 0x2000u}, + {Position::kVerticalHeightRight, 0x4000u}, + {Position::kTopSurroundLeft, 0x8000u}, + {Position::kTopSurroundRight, 0x20000u}, +}; + +std::uint32_t position_bit(Position position) { + for (const PositionBit& entry : kPositionBits) { + if (entry.position == position) { + return entry.bit; + } + } + return 0u; +} + +void push_unique(std::vector* positions, Position position) { + if (position == Position::kNone) { + return; + } + for (const Position existing : *positions) { + if (existing == position) { + return; + } + } + positions->push_back(position); +} + +// ADM 10-slot skeleton placement of the extra pair, indexed by joc_dmx_config_idx. +constexpr int kExtraPairSlots[5][2] = { + {-1, -1}, {6, 7}, {8, 9}, {-1, -1}, {8, 9}, +}; + +Status variant_error(const char* stage_name, const char* name, const std::string& message, + std::string* error) { + const std::string text = std::string(name) + ": " + message; + if (error != nullptr) { + *error = text; + } + return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage_name, text); +} + +std::string position_name(Position position) { + switch (position) { + case Position::kLeft: return "L"; + case Position::kRight: return "R"; + case Position::kCenter: return "C"; + case Position::kLfe: return "LFE"; + case Position::kLeftSurround: return "Ls"; + case Position::kRightSurround: return "Rs"; + case Position::kLeftRearSurround: return "Lrs"; + case Position::kRightRearSurround: return "Rrs"; + case Position::kVerticalHeightLeft: return "Vhl"; + case Position::kVerticalHeightRight: return "Vhr"; + case Position::kTopSurroundLeft: return "Lts"; + case Position::kTopSurroundRight: return "Rts"; + case Position::kCenterSurround: return "Cs"; + case Position::kTopSurround: return "Ts"; + case Position::kVerticalHeightCenter: return "Vhc"; + case Position::kLfe2: return "LFE2"; + default: return "?"; + } +} + +std::string position_list(const std::vector& positions) { + std::string text = "["; + for (std::size_t index = 0; index < positions.size(); ++index) { + if (index != 0) { + text += ","; + } + text += position_name(positions[index]); + } + return text + "]"; +} + +} // namespace + +void expand_chanmap(std::uint16_t chanmap, std::vector* out) { + if (out == nullptr) { + return; + } + // Table E2.5; bit 0 sits in the field's most significant bit. + static const Position kSingles[16] = { + Position::kLeft, Position::kCenter, Position::kRight, + Position::kLeftSurround, Position::kRightSurround, Position::kNone, + Position::kNone, Position::kCenterSurround, Position::kTopSurround, + Position::kNone, Position::kNone, Position::kNone, + Position::kVerticalHeightCenter, Position::kNone, Position::kLfe2, + Position::kLfe, + }; + for (int bit = 0; bit < 16; ++bit) { + if (((chanmap >> (15 - bit)) & 1u) == 0u) { + continue; + } + switch (bit) { + case 5: + out->push_back(Position::kLeftSurround); + out->push_back(Position::kRightSurround); + break; + case 6: + out->push_back(Position::kLeftRearSurround); + out->push_back(Position::kRightRearSurround); + break; + case 9: + case 10: + // Lsd/Rsd and Lw/Rw have no JOC counterpart and stay unmapped. + break; + case 11: + out->push_back(Position::kVerticalHeightLeft); + out->push_back(Position::kVerticalHeightRight); + break; + case 13: + out->push_back(Position::kTopSurroundLeft); + out->push_back(Position::kTopSurroundRight); + break; + default: + out->push_back(kSingles[bit]); + break; + } + } +} + +void acmod_positions(std::uint8_t acmod, std::vector* out) { + if (out == nullptr) { + return; + } + switch (acmod) { + case 0: + case 1: + out->push_back(Position::kCenter); + break; + case 2: + out->push_back(Position::kLeft); + out->push_back(Position::kRight); + break; + case 3: + out->push_back(Position::kLeft); + out->push_back(Position::kCenter); + out->push_back(Position::kRight); + break; + case 4: + out->push_back(Position::kLeft); + out->push_back(Position::kRight); + out->push_back(Position::kCenterSurround); + break; + case 5: + out->push_back(Position::kLeft); + out->push_back(Position::kCenter); + out->push_back(Position::kRight); + out->push_back(Position::kCenterSurround); + break; + case 6: + out->push_back(Position::kLeft); + out->push_back(Position::kRight); + out->push_back(Position::kLeftSurround); + out->push_back(Position::kRightSurround); + break; + case 7: + default: + out->push_back(Position::kLeft); + out->push_back(Position::kCenter); + out->push_back(Position::kRight); + out->push_back(Position::kLeftSurround); + out->push_back(Position::kRightSurround); + break; + } +} + +void expected_pair(std::uint8_t dmx_config_idx, std::vector* out) { + if (out == nullptr) { + return; + } + if (dmx_config_idx == 1u) { + out->push_back(Position::kLeftRearSurround); + out->push_back(Position::kRightRearSurround); + } else if (dmx_config_idx == 2u || dmx_config_idx == 4u) { + out->push_back(Position::kVerticalHeightLeft); + out->push_back(Position::kVerticalHeightRight); + } +} + +void chanmap_extra_pair(const std::vector& core, std::uint16_t chanmap, + std::vector* out) { + if (out == nullptr) { + return; + } + std::vector declared; + expand_chanmap(chanmap, &declared); + for (const Position position : declared) { + bool in_core = false; + for (const Position existing : core) { + if (existing == position) { + in_core = true; + break; + } + } + if (!in_core) { + push_unique(out, position); + } + } +} + +int bed_channels_for_core(int core_channels) { + return core_channels == 7 ? 8 : 6; +} + +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) { + if (out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null bed plan"); + } + const std::int16_t core_channels = num_channels_for_config(dmx_config_idx); + if (core_channels < 0) { + return variant_error("joc", "unsupported_configuration", + "unknown JOC downmix configuration " + + std::to_string(dmx_config_idx), nullptr); + } + std::vector core_positions; + if (has_core && core_frame != nullptr) { + acmod_positions(core_frame->acmod, &core_positions); + if (core_frame->lfeon != 0u) { + core_positions.push_back(Position::kLfe); + } + } + std::vector extra; + std::vector locations = core_positions; + if (has_dependent && has_chanmap) { + std::vector declared; + expand_chanmap(chanmap, &declared); + chanmap_extra_pair(core_positions, chanmap, &extra); + locations.insert(locations.end(), declared.begin(), declared.end()); + } + + std::vector ordered; + for (const Position position : locations) { + push_unique(&ordered, position); + } + std::sort(ordered.begin(), ordered.end(), [](Position left, Position right) { + return position_bit(left) < position_bit(right); + }); + for (const Position position : ordered) { + if (position_bit(position) == 0u) { + return variant_error("eac3", "bed_channel_mapping_unsupported", + "unmapped channel position " + position_name(position), + nullptr); + } + } + + BedPlan plan; + plan.channels = static_cast(ordered.size()); + const int expected_channels = bed_channels_for_core(core_channels); + if (plan.channels != expected_channels) { + return variant_error("eac3", "core_channel_count_mismatch", + "the bed declares " + std::to_string(plan.channels) + + " channels, configuration " + std::to_string(dmx_config_idx) + + " needs " + std::to_string(expected_channels), + nullptr); + } + for (int index = 0; index < plan.channels; ++index) { + if (ordered[index] == Position::kLfe) { + plan.lfe_channel = index; + } + } + + std::vector wanted = {Position::kLeft, Position::kRight, Position::kCenter, + Position::kLeftSurround, Position::kRightSurround}; + wanted.insert(wanted.end(), extra.begin(), extra.end()); + if (wanted.size() != static_cast(core_channels)) { + return variant_error("eac3", "chanmap_cfg_mismatch", + "the bed cannot supply " + std::to_string(core_channels) + + " JOC core channels (found " + std::to_string(wanted.size()) + + ")", + nullptr); + } + for (std::size_t lane = 0; lane < wanted.size(); ++lane) { + int found = -1; + for (int index = 0; index < plan.channels; ++index) { + if (ordered[index] == wanted[lane]) { + found = index; + break; + } + } + if (found < 0) { + return variant_error("eac3", "bed_channel_mapping_unsupported", + "channel " + position_name(wanted[lane]) + + " is missing from the bed " + position_list(ordered), + nullptr); + } + plan.lane[lane] = found; + } + if (dmx_config_idx < 5u) { + plan.extra_pair_slot[0] = kExtraPairSlots[dmx_config_idx][0]; + plan.extra_pair_slot[1] = kExtraPairSlots[dmx_config_idx][1]; + } + *out = plan; + return Status::success(); +} + +Status compute_bed_plan_for_config(std::uint8_t dmx_config_idx, BedPlan* out) { + if (out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null bed plan"); + } + const std::int16_t core_channels = num_channels_for_config(dmx_config_idx); + if (core_channels < 0) { + return variant_error("joc", "unsupported_configuration", + "unknown JOC downmix configuration " + + std::to_string(dmx_config_idx), nullptr); + } + std::vector extra; + expected_pair(dmx_config_idx, &extra); + std::vector wanted = {Position::kLeft, Position::kRight, Position::kCenter, + Position::kLeftSurround, Position::kRightSurround}; + wanted.insert(wanted.end(), extra.begin(), extra.end()); + + std::vector ordered = {Position::kLeft, Position::kRight, Position::kCenter, + Position::kLfe, Position::kLeftSurround, + Position::kRightSurround}; + ordered.insert(ordered.end(), extra.begin(), extra.end()); + std::sort(ordered.begin(), ordered.end(), [](Position left, Position right) { + return position_bit(left) < position_bit(right); + }); + ordered.erase(std::unique(ordered.begin(), ordered.end()), ordered.end()); + + BedPlan plan; + plan.channels = static_cast(ordered.size()); + for (int index = 0; index < plan.channels; ++index) { + if (ordered[index] == Position::kLfe) { + plan.lfe_channel = index; + } + } + for (std::size_t lane = 0; lane < wanted.size(); ++lane) { + for (int index = 0; index < plan.channels; ++index) { + if (ordered[index] == wanted[lane]) { + plan.lane[lane] = index; + break; + } + } + if (plan.lane[lane] < 0) { + return variant_error("eac3", "bed_channel_mapping_unsupported", + "channel " + position_name(wanted[lane]) + + " is missing from the bed " + position_list(ordered), + nullptr); + } + } + if (dmx_config_idx < 5u) { + plan.extra_pair_slot[0] = kExtraPairSlots[dmx_config_idx][0]; + plan.extra_pair_slot[1] = kExtraPairSlots[dmx_config_idx][1]; + } + *out = plan; + return Status::success(); +} + +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* bed_assignments, + std::string* error, CrossLayerNotes* notes) { + const std::uint8_t config_idx = params.dmx_config_idx; + const int core_channels = params.n_channels; + std::vector wanted; + expected_pair(config_idx, &wanted); + + if (params.ext_config_idx != 0u) { + return variant_error("joc", "ext_config_idx_unsupported", + "joc_ext_config_idx=" + std::to_string(params.ext_config_idx) + + " has no mapping in this implementation", + error); + } + if (core_channels != JOC_CORE_CHANNELS && core_channels != JOC_MAX_CORE_CHANNELS) { + return variant_error("joc", "unsupported_configuration", + "unsupported JOC core channel count " + + std::to_string(core_channels), + error); + } + + std::vector core_positions; + if (has_core && core_frame != nullptr) { + acmod_positions(core_frame->acmod, &core_positions); + if (core_frame->lfeon != 0u) { + core_positions.push_back(Position::kLfe); + } + } + + if (core_channels == JOC_MAX_CORE_CHANNELS) { + if (!has_core || !has_chanmap) { + return variant_error( + "eac3", "core_extension_pair", + "a 7-channel configuration needs both an independent substream and a " + "dependent-substream chanmap", + error); + } + std::vector pair; + chanmap_extra_pair(core_positions, chanmap, &pair); + if (pair != wanted) { + // The bed mapping follows the chanmap's channel codes; the name a + // configuration implies does not reach the DSP, so the frame renders + // and the disagreement is reported instead. + if (notes != nullptr) { + notes->chanmap_pair_mismatch = true; + } + } + } else if (has_chanmap) { + std::vector pair; + chanmap_extra_pair(core_positions, chanmap, &pair); + if (!pair.empty()) { + return variant_error("eac3", "core_channel_count_mismatch", + "a 5-channel configuration declares extra dependent " + "channels " + position_list(pair), + error); + } + } + + std::vector declared = core_positions; + if (has_chanmap) { + std::vector expanded; + expand_chanmap(chanmap, &expanded); + for (const Position position : expanded) { + push_unique(&declared, position); + } + } + int full_band = 0; + for (const Position position : declared) { + if (position != Position::kLfe && position != Position::kLfe2) { + ++full_band; + } + } + if (!declared.empty() && full_band != core_channels) { + return variant_error("eac3", "core_channel_count_mismatch", + "the substreams declare " + std::to_string(full_band) + + " full-band channels, joc_dmx_config_idx=" + + std::to_string(config_idx) + " declares " + + std::to_string(core_channels), + error); + } + + if (bed_assignments != nullptr) { + static constexpr std::uint32_t kBedBitLeftRear = 1u << 4; // RC_LB/RC_RB + static constexpr std::uint32_t kBedBitTopFront = 1u << 5; // RC_TFL/RC_TFR + std::vector declared_pair; + for (const oamd::BedAssignment& assignment : *bed_assignments) { + if (!assignment.standard || assignment.mask == 0u) { + continue; + } + if ((assignment.mask & kBedBitLeftRear) != 0u) { + push_unique(&declared_pair, Position::kLeftRearSurround); + push_unique(&declared_pair, Position::kRightRearSurround); + } + if ((assignment.mask & kBedBitTopFront) != 0u) { + push_unique(&declared_pair, Position::kVerticalHeightLeft); + push_unique(&declared_pair, Position::kVerticalHeightRight); + } + } + if (!declared_pair.empty() && declared_pair != wanted) { + if (notes != nullptr) { + notes->bed_mask_mismatch = true; + } + } + } + return Status::success(); +} + } // namespace joc::joc + diff --git a/src/joc_bitstream/joc_parser.h b/src/joc_bitstream/joc_parser.h index 606f5d0..38d08fb 100644 --- a/src/joc_bitstream/joc_parser.h +++ b/src/joc_bitstream/joc_parser.h @@ -6,9 +6,11 @@ #include #include +#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 { @@ -39,8 +41,99 @@ Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_f 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* out); + +// A/52 acmod -> full-band positions in decoding order (LFE excluded). +void acmod_positions(std::uint8_t acmod, std::vector* 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* out); + +// The extra pair a dependent-substream chanmap adds on top of the core channels. +void chanmap_extra_pair(const std::vector& core, std::uint16_t chanmap, + std::vector* 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* bed_assignments, + std::string* error, CrossLayerNotes* notes = nullptr); + } // namespace joc::joc diff --git a/src/joc_core/eac3joc_core.cpp b/src/joc_core/eac3joc_core.cpp index 3b79a24..dc4d977 100644 --- a/src/joc_core/eac3joc_core.cpp +++ b/src/joc_core/eac3joc_core.cpp @@ -69,7 +69,9 @@ public: std::memset(synthesis_state_, 0, sizeof(synthesis_state_)); std::memset(x_, 0, sizeof(x_)); std::memset(z_, 0, sizeof(z_)); - analysis_phase_ = 0.0625f; + // Sentinel: the first frame after reset takes the new scalar directly, + // without an intra-frame transition. + analysis_phase_ = -0.5f; error_[0] = '\0'; return 0; } @@ -125,13 +127,11 @@ public: return error_[0] ? error_ : ""; } - void set_dc_filter(const bool enabled) noexcept { - dc_filter_enabled_ = enabled; - } - int process( - const float* bed5, + const float* bed_planar, const float* lfe, + const uint32_t core_channel_count, + const uint32_t phase_shift, const uint32_t object_mask, const uint8_t* n_bands, const uint8_t* n_dpoints, @@ -144,9 +144,14 @@ public: float* output16) noexcept { error_[0] = '\0'; - if (!bed5 || !n_bands || !n_dpoints || !slope_idx || !offset_ts || !dq || !output16) { + if (!bed_planar || !n_bands || !n_dpoints || !slope_idx || !offset_ts || !dq || !output16) { return fail("null pointer passed to ejoc_renderer_process"); } + if (core_channel_count < EJOC_CORE_CHANNELS || core_channel_count > EJOC_MAX_CORE_CHANNELS) { + return fail("core_channel_count must be 5 or 7"); + } + core_channels_ = core_channel_count; + phase_shift_enabled_ = phase_shift != 0u; if (object_mask & ~((1u << EJOC_MAX_OBJECTS) - 1u)) { return fail("object_mask contains an object index above 14"); } @@ -169,7 +174,7 @@ public: } std::memset(output16, 0, sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); - analysis(bed5, phase_new); + analysis(bed_planar, phase_new); render_lfe(lfe, output_scale, output16); process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts, dq, clipgain, output_scale, output16); @@ -299,7 +304,8 @@ private: for (int band = 0; band < 64; ++band) { current[slot][band] = x_[channel][band][group + slot]; const Complex delayed = surround_delay_[surround][slot][band]; - x_[channel][band][group + slot] = {delayed.im, -delayed.re}; + x_[channel][band][group + slot] = + phase_shift_enabled_ ? Complex{delayed.im, -delayed.re} : delayed; } } @@ -309,7 +315,7 @@ private: for (int i = 0; i < 4; ++i) { dc_buffer[20 + i] = current[i][0]; } - if (dc_filter_enabled_) { + if (phase_shift_enabled_) { for (int slot = 0; slot < 4; ++slot) { Complex sum{0.0, 0.0}; for (int tap = 0; tap < 21; ++tap) { @@ -338,7 +344,7 @@ private: } void analysis_channel(const int channel) noexcept { - const float* bed5 = job_bed5_; + const float* bed_planar = job_bed_; const float phase_old = job_phase_old_; const float phase_new = job_phase_new_; const bool ramp_phase = phase_old != phase_new; @@ -354,37 +360,49 @@ private: const float product = static_cast(static_cast(frame_sample) * phase_step); gain = static_cast(phase_old + product); } - scaled[sample] = static_cast(bed5[channel * 1536 + frame_sample] * gain); + scaled[sample] = static_cast(bed_planar[channel * 1536 + frame_sample] * gain); } + // L/R/C and, for 7 core channels, the extra pair (5 and 6) take the + // ten-slot delay here; Ls/Rs take the same delay inside surround_post(). const float* analysis_input = scaled; - if (channel < 3) { - std::memcpy(delayed, analysis_delay_[channel][0], sizeof(delayed)); - std::memmove(&analysis_delay_[channel][0][0], - &analysis_delay_[channel][1][0], + const int delay_lane = channel < 3 ? channel : channel - 2; + if (channel < 3 || channel >= 5) { + std::memcpy(delayed, analysis_delay_[delay_lane][0], sizeof(delayed)); + std::memmove(&analysis_delay_[delay_lane][0][0], + &analysis_delay_[delay_lane][1][0], sizeof(float) * 9 * 64); - std::memcpy(analysis_delay_[channel][9], scaled, sizeof(scaled)); + std::memcpy(analysis_delay_[delay_lane][9], scaled, sizeof(scaled)); analysis_input = delayed; } analysis_slot(channel, analysis_input, timeslot); } } - void analysis(const float* bed5, const float phase_new) noexcept { - job_bed5_ = bed5; + void analysis(const float* bed_planar, const float phase_new) noexcept { + job_bed_ = bed_planar; + // Analysis-side scalar = the structural 1/16 times this frame's clipgain. + // clipgain enters the analysis at this stage and the x16 at the output + // cancels the 1/16, so the net effect is a multiply by joc_clipgain; the + // previous frame's scalar is the ramp start. + const float scalar = static_cast(phase_new * job_clipgain_); job_phase_old_ = analysis_phase_; - job_phase_new_ = phase_new; + if (job_phase_old_ <= 0.0f && job_phase_old_ != 0.0f) { + job_phase_old_ = scalar; + } + job_phase_new_ = scalar; job_kind_ = JobKind::Analysis; - job_count_ = 5; - for (int channel = 0; channel < 5; ++channel) { - job_objects_[channel] = channel; + job_count_ = static_cast(core_channels_); + for (uint32_t channel = 0; channel < core_channels_; ++channel) { + job_objects_[channel] = static_cast(channel); } dispatch_jobs(); - analysis_phase_ = phase_new; + analysis_phase_ = scalar; surround_post(); } static std::size_t dq_index(const int object, const int point, const int channel, const int band) noexcept { - return static_cast((((object * 2 + point) * 5 + channel) * kMaxBands) + band); + return static_cast( + (((object * 2 + point) * EJOC_MAX_CORE_CHANNELS + channel) * kMaxBands) + band); } void matrix_object( @@ -401,7 +419,7 @@ private: const int slope = slope_idx[object]; const uint8_t* pb_map = parameter_band_map(bands); - for (int channel = 0; channel < 5; ++channel) { + for (uint32_t channel = 0; channel < core_channels_; ++channel) { for (int subband = 0; subband < 64; ++subband) { const int parameter_band = pb_map[subband]; const double previous = matrix_previous_[object][channel][subband]; @@ -465,7 +483,6 @@ private: void synthesis_object( const int object, - const double clipgain, const float output_scale, float* output16) noexcept { @@ -493,8 +510,10 @@ private: qmf5_step(state, rotated, pcm64); for (int sample = 0; sample < 64; ++sample) { const double clipped = clamp_unit(16.0 * pcm64[sample]); - const float value = static_cast(clipped * clipgain); - destination[timeslot * 64 + sample] = static_cast(value * output_scale); + // joc_clipgain already entered the analysis (see analysis()); what + // remains here is the user output gain. + const float value = static_cast(clipped * output_scale); + destination[timeslot * 64 + sample] = value; } } } @@ -502,7 +521,7 @@ private: void process_one_object(const int object) noexcept { matrix_object(object, job_n_bands_, job_n_dpoints_, job_slope_idx_, job_offset_ts_, job_dq_); - synthesis_object(object, job_clipgain_, job_output_scale_, job_output16_); + synthesis_object(object, job_output_scale_, job_output16_); } void execute_job_loop() noexcept { @@ -630,7 +649,7 @@ private: int job_objects_[EJOC_MAX_OBJECTS]{}; int job_count_ = 0; JobKind job_kind_ = JobKind::Objects; - const float* job_bed5_ = nullptr; + const float* job_bed_ = nullptr; float job_phase_old_ = 0.0625f; float job_phase_new_ = 0.0625f; const uint8_t* job_n_bands_ = nullptr; @@ -642,17 +661,19 @@ private: float job_output_scale_ = 1.0f; float* job_output16_ = nullptr; - alignas(64) double analysis_fifo_[5][9][64]; - alignas(64) float analysis_delay_[3][10][64]; - float analysis_phase_; + uint32_t core_channels_ = EJOC_CORE_CHANNELS; + alignas(64) double analysis_fifo_[EJOC_MAX_CORE_CHANNELS][9][64]; + // Delay lanes: 0..2 hold L/R/C, 3..4 hold the extra pair of a 7-channel frame. + alignas(64) float analysis_delay_[5][10][64]; + float analysis_phase_ = -0.5f; alignas(64) Complex surround_delay_[2][10][64]; alignas(64) Complex surround_history_[2][20]; - // band-0 的 21-tap DC 补偿开关;仅 downmix 配置 3/4 由调用方置位。 - bool dc_filter_enabled_ = true; + // sigma(k) switch; the caller sets it for downmix configurations 3 and 4. + bool phase_shift_enabled_ = true; alignas(64) double lfe_delay_[kLfeDelay]; - alignas(64) double matrix_previous_[15][5][64]; + alignas(64) double matrix_previous_[15][EJOC_MAX_CORE_CHANNELS][64]; alignas(64) double synthesis_state_[15][640]; - alignas(64) Complex x_[5][64][24]; + alignas(64) Complex x_[EJOC_MAX_CORE_CHANNELS][64][24]; alignas(64) Complex z_[15][64][24]; uint8_t bit_reverse_[64]; @@ -704,14 +725,6 @@ int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t to return static_cast(handle)->set_threads(total_threads); } -int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled) { - if (!handle) { - return -1; - } - static_cast(handle)->set_dc_filter(enabled != 0); - return 0; -} - uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle) { if (!handle) { return 0; @@ -728,8 +741,10 @@ const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle) { int EJOC_CALL ejoc_renderer_process( ejoc_renderer_handle handle, - const float* bed5_planar, + const float* bed_planar, const float* lfe, + const uint32_t core_channel_count, + const uint32_t phase_shift, const uint32_t object_mask, const uint8_t* n_bands, const uint8_t* n_dpoints, @@ -744,8 +759,8 @@ int EJOC_CALL ejoc_renderer_process( return -1; } return static_cast(handle)->process( - bed5_planar, lfe, object_mask, n_bands, n_dpoints, slope_idx, - offset_ts, dq, clipgain, phase_new, output_scale, output16_planar); + bed_planar, lfe, core_channel_count, phase_shift, object_mask, n_bands, n_dpoints, + slope_idx, offset_ts, dq, clipgain, phase_new, output_scale, output16_planar); } } // extern "C" diff --git a/src/joc_core/objects16.cpp b/src/joc_core/objects16.cpp index 62b45a3..aa2925b 100644 --- a/src/joc_core/objects16.cpp +++ b/src/joc_core/objects16.cpp @@ -6,21 +6,22 @@ namespace joc::joc { Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params, - const float* bed5_planar, const float* lfe, float gain, + const float* bed_planar, const float* lfe, float gain, std::vector* out16, std::string* error) { - if (handle == nullptr || bed5_planar == nullptr || out16 == nullptr) { + if (handle == nullptr || bed_planar == nullptr || out16 == nullptr) { return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp, "null argument"); } - if (params.n_channels != JOC_CORE_CHANNELS) { + if (params.n_channels != JOC_CORE_CHANNELS && + params.n_channels != JOC_MAX_CORE_CHANNELS) { if (error != nullptr) { - *error = "the reused JOC kernel requires 5 core channels, frame declares " + + *error = "the reused JOC kernel requires 5 or 7 core channels, frame declares " + std::to_string(static_cast(params.n_channels)); } return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kDsp, *error); } std::vector dq(static_cast(JOC_MAX_OBJECTS) * JOC_MAX_DPOINTS * - JOC_CORE_CHANNELS * JOC_MAX_PARAMETER_BANDS, + JOC_MAX_CORE_CHANNELS * JOC_MAX_PARAMETER_BANDS, 0.0); std::uint8_t n_bands[JOC_MAX_OBJECTS] = {}; std::uint8_t n_dpoints[JOC_MAX_OBJECTS] = {}; @@ -38,10 +39,11 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa offset_ts[obj * JOC_MAX_DPOINTS + dp] = object.offset_ts[dp]; } for (unsigned dp = 0; dp < object.n_dpoints; ++dp) { - for (unsigned ch = 0; ch < JOC_CORE_CHANNELS; ++ch) { + for (unsigned ch = 0; ch < params.n_channels; ++ch) { for (unsigned pb = 0; pb < object.n_bands; ++pb) { const std::size_t index = - ((static_cast(obj) * JOC_MAX_DPOINTS + dp) * JOC_CORE_CHANNELS + + ((static_cast(obj) * JOC_MAX_DPOINTS + dp) * + JOC_MAX_CORE_CHANNELS + ch) * JOC_MAX_PARAMETER_BANDS + pb; @@ -52,18 +54,14 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa } out16->assign(static_cast(JOC_OUTPUT_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f); - // band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用,其余配置 band 0 - // 走与其他 band 相同的处理。 - const bool dc_filter = params.dmx_config_idx == 3 || params.dmx_config_idx == 4; - if (ejoc_renderer_set_dc_filter(handle, dc_filter ? 1u : 0u) != 0) { - if (error != nullptr) { - *error = "ejoc_renderer_set_dc_filter failed"; - } - return Status::fail(JOC_ERR_RENDER_FAILED, stage::kDsp, "ejoc_renderer_set_dc_filter failed"); - } + // sigma(k) = (k in {3,4}) gates the Ls/Rs -j rotation and the band-0 21-tap + // DC compensation together; other configurations keep the common processing. + const std::uint32_t phase_shift = + (params.dmx_config_idx == 3 || params.dmx_config_idx == 4) ? 1u : 0u; const int result = ejoc_renderer_process( - handle, bed5_planar, lfe, params.present_mask, n_bands, n_dpoints, slope_idx, offset_ts, - dq.data(), params.clipgain, 0.0625f, gain, out16->data()); + handle, bed_planar, lfe, params.n_channels, phase_shift, params.present_mask, n_bands, + n_dpoints, slope_idx, offset_ts, dq.data(), params.clipgain, 0.0625f, gain, + out16->data()); if (result != 0) { const char* detail = ejoc_renderer_last_error(handle); const std::string message = diff --git a/src/joc_core/objects16.h b/src/joc_core/objects16.h index 06a693d..6ef0e9a 100644 --- a/src/joc_core/objects16.h +++ b/src/joc_core/objects16.h @@ -10,9 +10,10 @@ namespace joc::joc { -// [16][1536] planar float32. The kernel requires exactly 5 core channels. +// [16][1536] planar float32. bed_planar is [params.n_channels][1536], where +// n_channels comes from joc_dmx_config_idx (5 or 7). Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params, - const float* bed5_planar, const float* lfe, float gain, + const float* bed_planar, const float* lfe, float gain, std::vector* out16, std::string* error); } // namespace joc::joc diff --git a/src/oamd/oamd_parser.cpp b/src/oamd/oamd_parser.cpp index 750dffa..8e8094a 100644 --- a/src/oamd/oamd_parser.cpp +++ b/src/oamd/oamd_parser.cpp @@ -335,6 +335,42 @@ Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpd return parse_id11_verbose(payload, payload_size, out, nullptr); } +Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size, + std::vector* out) { + if (payload == nullptr || out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + out->clear(); + bits::BitReader reader(payload, payload_size); + std::uint32_t version = reader.read(2); + if (reader.failed()) { + return variant("header_truncated", "OAMD payload cannot hold the 2-bit version"); + } + if (version == 3u) { + version += reader.read(3); + if (reader.failed()) { + return syntax("OAMD version extension truncated"); + } + } + std::uint32_t object_count_bits = reader.read(5); + if (object_count_bits == 0x1Fu) { + object_count_bits += reader.read(7); + } + if (reader.failed()) { + return syntax("OAMD object count truncated"); + } + ProgramInfo program; + const Status status = parse_program_assignment(reader, &program); + if (!status.ok()) { + return status; + } + *out = program.bed_assignments; + return Status::success(); +} + Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, ParseTrace* trace) { if (payload == nullptr || out == nullptr) { diff --git a/src/oamd/oamd_parser.h b/src/oamd/oamd_parser.h index 814ad87..10a8af3 100644 --- a/src/oamd/oamd_parser.h +++ b/src/oamd/oamd_parser.h @@ -100,4 +100,9 @@ struct ParseTrace { Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, ParseTrace* trace); +// Parses only the OAMD header and program_assignment(), which is all the +// cross-layer bed-mask check (spec section 11.2 item 2) needs. +Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size, + std::vector* out); + } // namespace joc::oamd diff --git a/src/stream/stream.cpp b/src/stream/stream.cpp index f2b2d5e..c1ad822 100644 --- a/src/stream/stream.cpp +++ b/src/stream/stream.cpp @@ -17,9 +17,6 @@ namespace joc::stream { namespace { constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES; -constexpr std::size_t kBedChannels = 6; -constexpr int kCoreChannels[5] = {0, 1, 2, 4, 5}; -constexpr int kLfeChannel = 3; } // namespace @@ -37,6 +34,14 @@ Stream::~Stream() { void Stream::reset_state() { reader_ = eac3::FrameReader(); metadata_.clear(); + pending_frame_ = FrameMetadata(); + bed_plan_ = joc::BedPlan(); + bed_channels_ = 0; + config_idx_ = -1; + core_channels_ = 0; + bed_plan_failed_ = false; + bed_plan_error_ = Status::success(); + bed_input_channels_ = 0; bed_pending_.clear(); bed_read_offset_ = 0; objects16_.clear(); @@ -181,6 +186,43 @@ Status Stream::create(const Config& config) { return Status::success(); } +Status Stream::flush_pending_frame() { + if (pending_frame_.streams.empty()) { + return Status::success(); + } + FrameMetadata entry = pending_frame_; + pending_frame_ = FrameMetadata(); + if (entry.has_joc) { + std::string error; + joc::CrossLayerNotes notes; + const Status checked = joc::check_cross_layer( + entry.params, entry.has_core ? &entry.core_frame : nullptr, entry.has_core, + entry.has_dependent, entry.has_chanmap, entry.chanmap, + entry.bed_assignments.empty() ? nullptr : &entry.bed_assignments, &error, ¬es); + if (!checked.ok()) { + return Status::fail(checked.code(), checked.stage(), + error.empty() ? checked.message() : error); + } + if (notes.chanmap_pair_mismatch) { + ++info_.chanmap_pair_mismatch_frames; + } + if (notes.bed_mask_mismatch) { + ++info_.bed_mask_mismatch_frames; + } + if (entry.params.objects_clamped != 0u) { + ++info_.objects_clamped_frames; + } + } else { + // No JOC metadata for this audio frame: the object channels stay silent + // while the kernel state still advances for the frame. + ++info_.missing_joc_frames; + joc::make_silent_frame_params(&entry.params); + entry.has_update = false; + } + metadata_.push_back(entry); + return Status::success(); +} + Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed) { if (rebuilder_ == nullptr) { return Status::fail(JOC_ERR_STATE, stage::kEmdf, "stream is not an E-AC-3 input"); @@ -201,13 +243,65 @@ Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t if (state == eac3::FrameReader::Next::Fail) { return Status::fail(reader_.error(), stage::kEac3, reader_.error_message()); } - FrameMetadata entry; + // A repeated (codec, strmtyp, substreamid) combination starts the next + // audio frame, so the group that is still open is complete. + bool repeats = false; + for (const eac3::Frame& prior : pending_frame_.streams) { + if (prior.same_stream(frame)) { + repeats = true; + break; + } + } + if (repeats) { + const Status flushed = flush_pending_frame(); + if (!flushed.ok()) { + return flushed; + } + } + eac3::Frame header = frame; + header.data = nullptr; + header.size = 0; + header.offset = frame.offset; + pending_frame_.streams.push_back(header); + if (frame.codec == 0u || frame.strmtyp == 0u) { + pending_frame_.core_frame = header; + pending_frame_.has_core = true; + } else { + pending_frame_.has_dependent = true; + if (frame.has_chanmap != 0u) { + pending_frame_.has_chanmap = true; + pending_frame_.chanmap = frame.chanmap; + } + } + + bool present = false; + bool ignored_extra = false; emdf::Container container; + const Status found = emdf::find_joc_emdf_optional( + frame.data, frame.size, &present, &container, &joc::score_container_as_joc, + &ignored_extra); + if (!found.ok()) { + return found; + } + if (ignored_extra) { + ++info_.extra_container_frames; + } + if (!present) { + continue; + } + if (container.find(emdf::kIdJoc) == nullptr) { + continue; + } + joc_frame_params params; const Status parsed = - joc::parse_eac3_frame(frame.data, frame.size, &entry.params, &container, nullptr); + joc::parse_eac3_frame(frame.data, frame.size, ¶ms, &container, nullptr); if (!parsed.ok()) { return parsed; } + pending_frame_.params = params; + pending_frame_.has_joc = true; + pending_frame_.syncframe_offset = frame.offset; + pending_frame_.syncframe_size = frame.size; if (const emdf::Payload* payload = container.find(emdf::kIdOamd)) { std::vector bytes; const Status extracted = @@ -215,32 +309,82 @@ Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t if (!extracted.ok()) { return extracted; } - const Status oamd = oamd::parse_id11(bytes.data(), bytes.size(), &entry.update); + const Status oamd = oamd::parse_id11(bytes.data(), bytes.size(), &pending_frame_.update); if (!oamd.ok()) { return oamd; } - entry.has_update = true; - entry.outer_offset = static_cast(payload->sample_offset); + pending_frame_.has_update = true; + pending_frame_.outer_offset = static_cast(payload->sample_offset); + const Status bed = oamd::parse_id11_bed_assignments( + bytes.data(), bytes.size(), &pending_frame_.bed_assignments); + if (!bed.ok()) { + return bed; + } } - metadata_.push_back(entry); } + resolve_bed_plan(); return process_ready_frames(false); } -Status Stream::push_bed(const float* interleaved6, std::size_t samples, std::size_t* consumed) { +Status Stream::push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels, + std::size_t* consumed) { if (rebuilder_ == nullptr) { return Status::fail(JOC_ERR_STATE, stage::kDsp, "stream is not an E-AC-3 input"); } + if (channels != 6u && channels != 8u) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp, + "the core PCM must be interleaved as 6 or 8 channels, got " + + std::to_string(channels)); + } + if (bed_input_channels_ == 0u) { + bed_input_channels_ = channels; + } else if (bed_input_channels_ != channels) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp, + "the core PCM channel count changed from " + + std::to_string(bed_input_channels_) + " to " + + std::to_string(channels)); + } if (consumed != nullptr) { *consumed = samples; } - if (interleaved6 != nullptr && samples != 0u) { - bed_pending_.insert(bed_pending_.end(), interleaved6, - interleaved6 + samples * kBedChannels); + if (interleaved != nullptr && samples != 0u) { + bed_pending_.insert(bed_pending_.end(), interleaved, + interleaved + samples * static_cast(channels)); } return process_ready_frames(false); } +bool Stream::resolve_bed_plan() { + if (bed_channels_ != 0u) { + return true; + } + const FrameMetadata* joc_entry = nullptr; + for (const FrameMetadata& candidate : metadata_) { + if (candidate.has_joc) { + joc_entry = &candidate; + break; + } + } + if (joc_entry == nullptr) { + return false; + } + joc::BedPlan plan; + const Status status = joc::compute_bed_plan( + joc_entry->has_core ? &joc_entry->core_frame : nullptr, joc_entry->has_core, + joc_entry->has_dependent, joc_entry->has_chanmap, joc_entry->chanmap, + joc_entry->params.dmx_config_idx, &plan); + if (!status.ok()) { + bed_plan_failed_ = true; + bed_plan_error_ = status; + return false; + } + bed_plan_ = plan; + bed_channels_ = static_cast(plan.channels); + config_idx_ = static_cast(joc_entry->params.dmx_config_idx); + core_channels_ = static_cast(joc_entry->params.n_channels); + return true; +} + Status Stream::push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed) { if (consumed != nullptr) { *consumed = samples; @@ -299,26 +443,62 @@ Status Stream::process_objects16_frames(bool drain_all) { } Status Stream::process_ready_frames(bool drain_all) { - while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * kBedChannels && + if (bed_plan_failed_) { + return bed_plan_error_; + } + resolve_bed_plan(); + if (bed_channels_ == 0u) { + if (!drain_all || metadata_.empty()) { + return Status::success(); // waiting for the first JOC-bearing frame + } + return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf, + "no_contiguous_joc_container: the input carries no JOC EMDF " + "container in any audio frame"); + } + if (bed_input_channels_ != 0u && bed_input_channels_ != bed_channels_) { + return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, "eac3", + "core_channel_count_mismatch: the caller pushed " + + std::to_string(bed_input_channels_) + + "-channel bed PCM but joc_dmx_config_idx=" + + std::to_string(config_idx_) + " decodes to " + + std::to_string(bed_channels_) + " channels"); + } + while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * bed_channels_ && !metadata_.empty()) { // Leave the rest queued, in order, for a later push or for flush(). if (!drain_all && buffered_samples() >= kMaxRenderAheadSamples) { break; } - const FrameMetadata entry = metadata_.front(); + FrameMetadata entry = metadata_.front(); metadata_.pop_front(); - const float* bed = bed_pending_.data() + bed_read_offset_; - bed5_.resize(static_cast(JOC_CORE_CHANNELS) * kFrameSamples); - lfe_.resize(kFrameSamples); - for (std::size_t sample = 0; sample < kFrameSamples; ++sample) { - for (std::size_t channel = 0; channel < JOC_CORE_CHANNELS; ++channel) { - bed5_[channel * kFrameSamples + sample] = - bed[sample * kBedChannels + kCoreChannels[channel]]; - } - lfe_[sample] = bed[sample * kBedChannels + kLfeChannel]; + const int core_channels = entry.has_joc ? static_cast(entry.params.n_channels) + : core_channels_; + if (core_channels <= 0) { + return Status::fail(JOC_ERR_STATE, stage::kDsp, + "the JOC core channel count is not known yet"); } - bed_read_offset_ += kFrameSamples * kBedChannels; + if (!entry.has_joc) { + entry.params.dmx_config_idx = static_cast(config_idx_); + entry.params.n_channels = static_cast(core_channels); + } + const float* bed = bed_pending_.data() + bed_read_offset_; + bed5_.assign(static_cast(core_channels) * kFrameSamples, 0.0f); + lfe_.assign(kFrameSamples, 0.0f); + for (std::size_t sample = 0; sample < kFrameSamples; ++sample) { + for (int lane = 0; lane < core_channels; ++lane) { + const int source = bed_plan_.lane[lane]; + if (source >= 0) { + bed5_[static_cast(lane) * kFrameSamples + sample] = + bed[sample * bed_channels_ + static_cast(source)]; + } + } + if (bed_plan_.lfe_channel >= 0) { + lfe_[sample] = bed[sample * bed_channels_ + + static_cast(bed_plan_.lfe_channel)]; + } + } + bed_read_offset_ += kFrameSamples * bed_channels_; compact_bed_pending(); std::string error; @@ -484,6 +664,10 @@ Status Stream::pull(float* destination, std::size_t capacity_samples, std::size_ Status Stream::flush() { // Input has ended, so drain what the cap held back: nothing else will // trigger rendering. + const Status pending = flush_pending_frame(); + if (!pending.ok()) { + return pending; + } const Status remaining = process_ready_frames(true); if (!remaining.ok()) { return remaining; diff --git a/src/stream/stream.h b/src/stream/stream.h index 7f686af..4ac439a 100644 --- a/src/stream/stream.h +++ b/src/stream/stream.h @@ -10,6 +10,7 @@ #include "eac3_transport/eac3_reader.h" #include "foundation/status.h" #include "hrtf/rosella_renderer.h" +#include "joc_bitstream/joc_parser.h" #include "joc_core.h" #include "joc_stream.h" #include "oamd/oamd_parser.h" @@ -46,16 +47,41 @@ struct Info { std::uint64_t bytes_in = 0; std::uint64_t oamd_payloads = 0; std::uint64_t oamd_transitions = 0; + std::uint64_t missing_joc_frames = 0; + std::uint64_t objects_clamped_frames = 0; + // Audio frames whose syncframe held more than one top-level JOC container and + // were decoded from the single one that carries a complete joc() payload. + std::uint64_t extra_container_frames = 0; + // Audio frames whose extra-pair identity disagreed across layers (spec 11.2 + // checks 1 and 2). The identity is metadata only, so these still render. + std::uint64_t chanmap_pair_mismatch_frames = 0; + std::uint64_t bed_mask_mismatch_frames = 0; std::uint32_t output_channels = 0; std::uint32_t ended = 0; }; -// One frame's metadata, queued while the matching core PCM arrives. +// One audio frame's metadata, queued while the matching core PCM arrives. +// +// One audio frame is the group of syncframes up to (and excluding) the first +// repeated (codec, strmtyp, substreamid) combination; the JOC payload usually +// lives in the dependent substream while the independent substream carries only +// the 5.1 core. The L2 fields below feed the spec section 11.2 cross-layer +// checks. struct FrameMetadata { joc_frame_params params{}; oamd::OamdUpdate update{}; bool has_update = false; std::int64_t outer_offset = 0; + bool has_joc = false; + eac3::Frame core_frame{}; // independent substream header (codec/strmtyp=0) + bool has_core = false; + bool has_dependent = false; + bool has_chanmap = false; + std::uint16_t chanmap = 0; + std::size_t syncframe_offset = 0; + std::size_t syncframe_size = 0; + std::vector streams; + std::vector bed_assignments; }; class Stream { @@ -68,7 +94,8 @@ public: Status create(const Config& config); Status push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed); - Status push_bed(const float* interleaved6, std::size_t samples, std::size_t* consumed); + Status push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels, + std::size_t* consumed); Status push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed); Status pull(float* destination, std::size_t capacity_samples, std::size_t* produced); Status flush(); @@ -103,6 +130,11 @@ private: void reset_state(); // Drops the bed samples that have already been rendered, keeping the rest. void compact_bed_pending(); + // Closes the audio frame currently being assembled and queues it. + Status flush_pending_frame(); + // Resolves the bed layout from the first queued frame that carries JOC; + // returns false while it is still unknown and more input may arrive. + bool resolve_bed_plan(); // Renders the queued objects16 frames, bounded by kMaxRenderAheadSamples. Status process_objects16_frames(bool drain_all); @@ -111,6 +143,14 @@ private: eac3::FrameReader reader_; std::deque metadata_; FrameMetadata pending_metadata_; + FrameMetadata pending_frame_; + joc::BedPlan bed_plan_; + std::size_t bed_channels_ = 0; + int config_idx_ = -1; + int core_channels_ = 0; + bool bed_plan_failed_ = false; + Status bed_plan_error_; + std::uint32_t bed_input_channels_ = 0; std::vector bed_pending_; std::size_t bed_read_offset_ = 0; std::vector bed5_; diff --git a/src/task/task.cpp b/src/task/task.cpp index 0b9a646..06f6521 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -53,11 +53,8 @@ namespace { namespace fs = std::filesystem; using Clock = std::chrono::steady_clock; -constexpr std::size_t kBedFrameBytes = static_cast(JOC_FRAME_SAMPLES) * 6u * 4u; constexpr std::size_t kReadChunk = 1u << 20; constexpr double kDefaultTailThreshold = 1.0e-8; -constexpr int kCoreChannels[5] = {0, 1, 2, 4, 5}; -constexpr int kLfeChannel = 3; std::string lower(std::string text) { for (char& c : text) { @@ -210,6 +207,9 @@ std::string create_temp_dir(const Settings& cfg) { const fs::path base = cfg. return fs_utf8::from_path(target); } +// One audio frame is every syncframe up to (and excluding) the first repeated +// (codec, strmtyp, substreamid) combination: a 7-channel program interleaves an +// independent substream with a dependent one, and an AC-3 core may come first. std::uint64_t count_frames(const std::string& path) { std::ifstream input = fs_utf8::open_input(path); if (!input) { @@ -219,6 +219,7 @@ std::uint64_t count_frames(const std::string& path) { std::vector chunk(kReadChunk); std::uint64_t frames = 0; bool done = false; + std::vector<::joc::eac3::Frame> group; while (!done) { input.read(chunk.data(), static_cast(chunk.size())); const std::streamsize got = input.gcount(); @@ -232,16 +233,29 @@ std::uint64_t count_frames(const std::string& path) { for (;;) { ::joc::eac3::Frame frame; const ::joc::eac3::FrameReader::Next state = reader.next(&frame); - if (state == ::joc::eac3::FrameReader::Next::Ok) { - ++frames; - continue; + if (state == ::joc::eac3::FrameReader::Next::End) { + break; } if (state == ::joc::eac3::FrameReader::Next::Fail) { - return frames; + return frames + (group.empty() ? 0u : 1u); } - break; + bool repeats = false; + for (const ::joc::eac3::Frame& prior : group) { + if (prior.same_stream(frame)) { + repeats = true; + break; + } + } + if (repeats) { + ++frames; + group.clear(); + } + group.push_back(frame); } } + if (!group.empty()) { + ++frames; + } return frames; } @@ -253,6 +267,53 @@ bool stdin_is_interactive() { #endif } +// Reads joc_dmx_config_idx from the first syncframe that carries a JOC payload, +// without decoding anything else. Returns -1 when the stream has no JOC. +int probe_config_idx(const std::string& path) { + std::ifstream input = fs_utf8::open_input(path); + if (!input) { + return -1; + } + ::joc::eac3::FrameReader reader; + std::vector chunk(1u << 16); + int config = -1; + bool done = false; + while (!done && config < 0) { + input.read(chunk.data(), static_cast(chunk.size())); + const std::streamsize got = input.gcount(); + if (got > 0) { + reader.push(reinterpret_cast(chunk.data()), + static_cast(got)); + } else { + reader.finish(); + done = true; + } + for (;;) { + ::joc::eac3::Frame frame; + const ::joc::eac3::FrameReader::Next state = reader.next(&frame); + if (state != ::joc::eac3::FrameReader::Next::Ok) { + break; + } + bool present = false; + ::joc::emdf::Container container; + if (!::joc::emdf::find_joc_emdf_optional( + frame.data, frame.size, &present, &container, + &::joc::joc::score_container_as_joc, nullptr) + .ok() || + !present) { + continue; + } + joc_frame_params params{}; + if (::joc::joc::parse_eac3_frame(frame.data, frame.size, ¶ms, &container, nullptr) + .ok()) { + config = static_cast(params.dmx_config_idx); + break; + } + } + } + return config; +} + // Rendered PCM is spooled to a float32 file so the output format can be decided // once the peak is known, which is what makes --clip-action possible; the // reference CLI spools for the same reason. @@ -480,6 +541,7 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std: double clipgain_max = 0.0; bool stream_done = false; Status failure = Status::success(); + std::vector<::joc::eac3::Frame> group; while (!stream_done && frames < total_frames) { input.read(chunk.data(), static_cast(chunk.size())); const std::streamsize got = input.gcount(); @@ -503,8 +565,36 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std: stream_done = true; break; } - joc_frame_params params{}; + // Count audio frames, not syncframes: a 7-channel program interleaves + // two substreams and one bed frame covers both. + bool repeats = false; + for (const ::joc::eac3::Frame& prior : group) { + if (prior.same_stream(frame)) { + repeats = true; + break; + } + } + if (repeats) { + ++frames; + group.clear(); + if (frames >= total_frames) { + stream_done = true; + break; + } + } + group.push_back(frame); + bool present = false; ::joc::emdf::Container container; + const Status found = ::joc::emdf::find_joc_emdf_optional( + frame.data, frame.size, &present, &container, + &::joc::joc::score_container_as_joc, nullptr); + if (!found.ok()) { + return Status::fail(found.code(), found.stage(), found.message()); + } + if (!present || container.find(::joc::emdf::kIdJoc) == nullptr) { + continue; + } + joc_frame_params params{}; const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size, ¶ms, &container, nullptr); if (!parsed.ok()) { @@ -593,35 +683,51 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std: return failure; } +// The decoded core PCM, read one frame at a time and laid out for the JOC +// kernel according to the bed plan (spec section 3: map by channel code, not by +// bed channel number). class BedSource { public: - bool open(const std::string& path) { + bool open(const std::string& path, std::size_t channels) { + channels_ = channels; file_.open(path, std::ios::binary); return static_cast(file_); } - bool read_frame(std::uint64_t index, std::vector* bed5, std::vector* lfe) { - bed5->assign(static_cast(JOC_CORE_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f); + bool read_frame(std::uint64_t index, int core_channels, const ::joc::joc::BedPlan& plan, + std::vector* bed, std::vector* lfe) { + bed->assign(static_cast(core_channels) * JOC_FRAME_SAMPLES, 0.0f); lfe->assign(JOC_FRAME_SAMPLES, 0.0f); + const std::size_t frame_values = static_cast(JOC_FRAME_SAMPLES) * channels_; + interleaved_.assign(frame_values, 0.0f); file_.clear(); - file_.seekg(static_cast(index * kBedFrameBytes), std::ios::beg); - std::vector interleaved(static_cast(JOC_FRAME_SAMPLES) * 6u); - file_.read(reinterpret_cast(interleaved.data()), - static_cast(interleaved.size() * sizeof(float))); - if (file_.gcount() != static_cast(interleaved.size() * sizeof(float))) { + file_.seekg(static_cast(index * frame_values * sizeof(float)), + std::ios::beg); + file_.read(reinterpret_cast(interleaved_.data()), + static_cast(frame_values * sizeof(float))); + if (file_.gcount() != static_cast(frame_values * sizeof(float))) { return false; } for (std::size_t n = 0; n < JOC_FRAME_SAMPLES; ++n) { - for (std::size_t c = 0; c < JOC_CORE_CHANNELS; ++c) { - (*bed5)[c * JOC_FRAME_SAMPLES + n] = interleaved[n * 6u + kCoreChannels[c]]; + for (int lane = 0; lane < core_channels; ++lane) { + const int source = plan.lane[lane]; + if (source >= 0) { + (*bed)[static_cast(lane) * JOC_FRAME_SAMPLES + n] = + interleaved_[n * channels_ + static_cast(source)]; + } + } + if (plan.lfe_channel >= 0) { + (*lfe)[n] = interleaved_[n * channels_ + + static_cast(plan.lfe_channel)]; } - (*lfe)[n] = interleaved[n * 6u + kLfeChannel]; } return true; } private: std::ifstream file_; + std::size_t channels_ = 6; + std::vector interleaved_; }; // --------------------------------------------------------------------------- @@ -1115,17 +1221,37 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } } + // The stream itself decides how many channels the decoded bed has; the same + // answer feeds the ffmpeg decode below and the bed reader. + const int stream_config_idx = probe_config_idx(eac3_path); + const int stream_core_channels = + stream_config_idx >= 0 + ? static_cast(::joc::joc::num_channels_for_config( + static_cast(stream_config_idx))) + : static_cast(JOC_CORE_CHANNELS); + const std::size_t stream_bed_channels = + stream_core_channels == JOC_MAX_CORE_CHANNELS ? 8u : 6u; + std::string bed_path = cfg.bed_path; if (bed_path.empty() && !cfg.metadata_only) { if (temp_dir.empty()) { temp_dir = create_temp_dir(cfg); } - bed_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "core51_f32le.raw"); - bus.stage(JOC_STAGE_DECODE, "decoding the 5.1 core PCM with ffmpeg"); + bed_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "core_f32le.raw"); + // A 7-channel program decodes to 8 bed channels; -ac 6 would downmix + // 5.1.2/7.1 into 5.1 and drop the extra pair, and -ac 8 would remap + // 5.1.2 into 7.1, so the native decoder layout is what we want there. + const bool native_bed = stream_core_channels == JOC_MAX_CORE_CHANNELS; + bus.stage(JOC_STAGE_DECODE, native_bed + ? "decoding the core PCM with ffmpeg (native layout)" + : "decoding the 5.1 core PCM with ffmpeg"); std::vector argv = {cfg.ffmpeg_path, "-hide_banner", "-loglevel", "error", "-y", "-i", eac3_path}; - argv.insert(argv.end(), {"-map", "0:a:0", "-vn", "-ac", "6", "-ar", "48000", - "-c:a", "pcm_f32le", "-f", "f32le", bed_path}); + argv.insert(argv.end(), {"-map", "0:a:0", "-vn"}); + if (!native_bed) { + argv.insert(argv.end(), {"-ac", "6"}); + } + argv.insert(argv.end(), {"-ar", "48000", "-c:a", "pcm_f32le", "-f", "f32le", bed_path}); argv.insert(argv.begin() + 4, {"-drc_scale", fmt("%.10g", cfg.drc_scale)}); if (cfg.target_level != 0) { argv.insert(argv.begin() + 4, {"-target_level", std::to_string(cfg.target_level)}); @@ -1336,7 +1462,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } BedSource bed; - if (!bed.open(bed_path)) { + if (!bed.open(bed_path, stream_bed_channels)) { return finalize_with_error(JOC_ERR_INPUT_NOT_FOUND, "bed_path", "cannot open the core PCM: " + bed_path); } @@ -1370,6 +1496,15 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu std::thread producer; std::thread writer; StageJoiner joiner(&joc_ring, &output_ring, &stop, &producer, &writer); + // Metadata the JOC stage had to work around, counted there and reported after + // the stages join: frames with no JOC payload, frames whose declared object + // count was clamped to 15, and frames whose syncframe carried a second EMDF + // container that is not this frame's JOC metadata. + std::uint64_t missing_joc_frames = 0; + std::uint64_t objects_clamped_frames = 0; + std::uint64_t extra_container_frames = 0; + std::uint64_t chanmap_pair_mismatch_frames = 0; + std::uint64_t bed_mask_mismatch_frames = 0; // ---- stage 1: the bitstream, on its own thread ------------------------ // // The serial loop's read/decode/JOC section, verbatim apart from its error @@ -1380,9 +1515,116 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu bool stream_done = false; double seconds = 0.0; JocJob job; + // Audio-frame assembly state (kept across chunk reads). + std::vector<::joc::eac3::Frame> group; + ::joc::eac3::Frame group_core; + bool group_has_core = false; + bool group_has_dependent = false; + bool group_has_chanmap = false; + bool group_has_joc = false; + std::uint16_t group_chanmap = 0; + joc_frame_params group_params{}; + std::vector<::joc::oamd::BedAssignment> group_bed_assignments; + ::joc::joc::BedPlan plan; + bool plan_ready = false; + int resolved_core_channels = 0; try { while (!stream_done && !stop.requested()) { auto stage_started = Clock::now(); + + // Emits the audio frame the group has accumulated so far. + auto emit_group = [&]() -> Status { + const bool has_joc = group_has_joc; + joc_frame_params params = group_params; + const bool has_core = group_has_core; + const eac3::Frame core_header = group_core; + const bool has_dependent = group_has_dependent; + const bool has_chanmap = group_has_chanmap; + const std::uint16_t chanmap = group_chanmap; + std::vector<::joc::oamd::BedAssignment> assignments = + std::move(group_bed_assignments); + group.clear(); + group_has_core = false; + group_has_dependent = false; + group_has_chanmap = false; + group_has_joc = false; + group_chanmap = 0; + group_params = joc_frame_params{}; + group_bed_assignments.clear(); + + if (has_joc) { + std::string error; + ::joc::joc::CrossLayerNotes notes; + const Status checked = ::joc::joc::check_cross_layer( + params, has_core ? &core_header : nullptr, has_core, has_dependent, + has_chanmap, chanmap, + assignments.empty() ? nullptr : &assignments, &error, ¬es); + if (!checked.ok()) { + return Status::fail(checked.code(), checked.stage(), + error.empty() ? checked.message() : error); + } + if (notes.chanmap_pair_mismatch) { + ++chanmap_pair_mismatch_frames; + } + if (notes.bed_mask_mismatch) { + ++bed_mask_mismatch_frames; + } + if (params.objects_clamped != 0u) { + ++objects_clamped_frames; + } + } else { + ++missing_joc_frames; + } + if (!plan_ready) { + if (stream_config_idx < 0) { + return Status::fail(JOC_ERR_EMDF_TRANSPORT, "emdf", + "no_contiguous_joc_container: the input carries " + "no JOC EMDF container in any audio frame"); + } + const Status planned = ::joc::joc::compute_bed_plan( + has_core ? &core_header : nullptr, has_core, has_dependent, + has_chanmap, chanmap, + static_cast(stream_config_idx), &plan); + if (!planned.ok()) { + return planned; + } + resolved_core_channels = stream_core_channels; + plan_ready = true; + } + if (!has_joc) { + ::joc::joc::make_silent_frame_params(¶ms); + } + params.dmx_config_idx = static_cast(stream_config_idx); + params.n_channels = static_cast(resolved_core_channels); + if (!bed.read_frame(produced, resolved_core_channels, plan, &bed5, &lfe)) { + return Status::fail(JOC_ERR_INPUT_FORMAT, "bed_path", + "core PCM ended before frame " + + std::to_string(produced)); + } + std::string dsp_error; + const Status rebuilt = ::joc::joc::rebuild_objects16( + rebuilder, params, bed5.data(), lfe.data(), gain, &objects16, + &dsp_error); + if (!rebuilt.ok()) { + return Status::fail(rebuilt.code(), "joc", dsp_error); + } + job.index = produced; + job.objects = params.n_objects; + job.clipgain = params.clipgain; + job.objects16.swap(objects16); + // The clock stops before the handoff, so the stage's number is + // its own work (including the chunk read) and never the time it + // spent waiting for the DSP to make room in the ring. + seconds += + std::chrono::duration(Clock::now() - stage_started).count(); + if (!joc_ring.push(&job)) { + return Status::fail(JOC_ERR_STATE, "output", + "the output ring closed while rendering"); + } + ++produced; + stage_started = Clock::now(); + return Status::success(); + }; input.read(chunk.data(), static_cast(chunk.size())); const std::streamsize got = input.gcount(); if (got > 0) { @@ -1408,14 +1650,63 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu stream_done = true; break; } - if (!bed.read_frame(produced, &bed5, &lfe)) { - failures.record(produced, StageRank::kJoc, JOC_ERR_INPUT_FORMAT, "bed_path", - "core PCM ended before frame " + std::to_string(produced)); + // A repeated (codec, strmtyp, substreamid) combination closes the + // audio frame the group is holding. + bool repeats = false; + for (const ::joc::eac3::Frame& prior : group) { + if (prior.same_stream(frame)) { + repeats = true; + break; + } + } + if (repeats) { + const Status emitted = emit_group(); + if (!emitted.ok()) { + failures.record(produced, StageRank::kJoc, emitted.code(), + emitted.stage(), emitted.message()); + stream_done = true; + break; + } + if (produced >= total_frames) { + stream_done = true; + break; + } + } + ::joc::eac3::Frame header = frame; + header.data = nullptr; + header.size = 0; + group.push_back(header); + if (frame.codec == 0u || frame.strmtyp == 0u) { + group_core = header; + group_has_core = true; + } else { + group_has_dependent = true; + if (frame.has_chanmap != 0u) { + group_has_chanmap = true; + group_chanmap = frame.chanmap; + } + } + + bool present = false; + bool extra_container_ignored = false; + ::joc::emdf::Container container; + const Status found = ::joc::emdf::find_joc_emdf_optional( + frame.data, frame.size, &present, &container, + &::joc::joc::score_container_as_joc, &extra_container_ignored); + if (extra_container_ignored) { + ++extra_container_frames; + } + if (!found.ok()) { + failures.record(produced, StageRank::kJoc, found.code(), found.stage(), + "frame " + std::to_string(produced) + ": " + + found.message()); stream_done = true; break; } + if (!present || container.find(::joc::emdf::kIdJoc) == nullptr) { + continue; + } joc_frame_params params{}; - ::joc::emdf::Container container; const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size, ¶ms, &container, nullptr); if (!parsed.ok()) { @@ -1425,6 +1716,8 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu stream_done = true; break; } + group_params = params; + group_has_joc = true; job.update = ::joc::oamd::OamdUpdate{}; job.has_update = false; job.outer_offset = 0; @@ -1449,30 +1742,24 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } job.has_update = true; job.outer_offset = static_cast(payload->sample_offset); + const Status bed_ok = ::joc::oamd::parse_id11_bed_assignments( + bytes.data(), bytes.size(), &group_bed_assignments); + if (!bed_ok.ok()) { + failures.record(produced, StageRank::kJoc, bed_ok.code(), + bed_ok.stage(), bed_ok.message()); + stream_done = true; + break; + } } - std::string dsp_error; - const Status rebuilt = ::joc::joc::rebuild_objects16( - rebuilder, params, bed5.data(), lfe.data(), gain, &objects16, &dsp_error); - if (!rebuilt.ok()) { - failures.record(produced, StageRank::kJoc, rebuilt.code(), "joc", - "frame " + std::to_string(produced) + ": " + dsp_error); - stream_done = true; - break; + } + // The final frame of an input that ends exactly on a group boundary + // has already been emitted above. + if (stream_done && !group.empty() && produced < total_frames) { + const Status emitted = emit_group(); + if (!emitted.ok()) { + failures.record(produced, StageRank::kJoc, emitted.code(), + emitted.stage(), emitted.message()); } - job.index = produced; - job.objects = params.n_objects; - job.clipgain = params.clipgain; - job.objects16.swap(objects16); - // The clock stops before the handoff, so the stage's number is - // its own work (including the chunk read) and never the time it - // spent waiting for the DSP to make room in the ring. - seconds += std::chrono::duration(Clock::now() - stage_started).count(); - if (!joc_ring.push(&job)) { - stream_done = true; - break; - } - ++produced; - stage_started = Clock::now(); } } } catch (...) { @@ -1733,6 +2020,29 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu // frames and join it. Every in-loop write is finished before the finalize // (tail flush, spool -> WAV, SHA-256, report) looks at the output. joiner.shutdown(false); + if (missing_joc_frames != 0u) { + bus.warning(std::to_string(missing_joc_frames) + + " audio frame(s) carried no JOC payload; their object channels stay silent"); + } + if (objects_clamped_frames != 0u) { + bus.warning(std::to_string(objects_clamped_frames) + + " audio frame(s) declared more than 15 objects; the count was clamped to 15"); + } + if (extra_container_frames != 0u) { + bus.warning(std::to_string(extra_container_frames) + + " audio frame(s) carried a second EMDF container; the container whose OAMD " + "and JOC payloads share a group id (and whose JOC payload parses) was used"); + } + if (chanmap_pair_mismatch_frames != 0u) { + bus.warning(std::to_string(chanmap_pair_mismatch_frames) + + " audio frame(s): the chanmap declares a different extra pair than " + "joc_dmx_config_idx; the bed mapping follows the chanmap"); + } + if (bed_mask_mismatch_frames != 0u) { + bus.warning(std::to_string(bed_mask_mismatch_frames) + + " audio frame(s): the OAMD bed mask declares a different extra pair than " + "joc_dmx_config_idx; the bed mask only affects metadata"); + } StageFailure recorded; if (failures.earliest(&recorded)) { return finalize_with_error(recorded.code, recorded.stage_text, recorded.message); diff --git a/tests/test_core.cpp b/tests/test_core.cpp index 1b3c137..7e7bc22 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -148,6 +148,7 @@ void test_eac3_frame_bytes() { frame[1] = 0x77; frame[2] = 0x05; frame[3] = 0xFF; // frmsiz words = 1536 -> 3072 bytes + frame[5] = 0x80; // bsid (bits 40..44) = 16 marks E-AC-3 std::size_t size = 0; CHECK(joc::eac3::FrameReader::frame_bytes(frame.data(), frame.size(), 0, &size) == JOC_OK); CHECK(size == 3072); @@ -156,6 +157,17 @@ void test_eac3_frame_bytes() { frame[1] = 0x78; CHECK(joc::eac3::FrameReader::frame_bytes(frame.data(), frame.size(), 0, &size) == JOC_ERR_EAC3_SYNCFRAME); + + // bsid <= 8 is AC-3: the frame length comes from fscod/frmsizecod instead. + std::vector ac3(2560, 0); + ac3[0] = 0x0B; + ac3[1] = 0x77; + ac3[4] = 0x24; // fscod = 0, frmsizecod = 36 -> 1280 words -> 2560 bytes + ac3[5] = 0x30; // bsid = 6 + CHECK(joc::eac3::FrameReader::frame_bytes(ac3.data(), ac3.size(), 0, &size) == JOC_OK); + CHECK(size == 2560); + CHECK(joc::eac3::FrameReader::frame_bytes(ac3.data(), ac3.size() - 1, 0, &size) == + JOC_ERR_BITSTREAM_TRUNCATED); } void test_mini_json() {