15 Commits
Author SHA1 Message Date
TheM14 2cfebe914d Support JOC downmix configurations 0..4 and update the vendored kernel
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-16 19:56:00 +08:00
TheM14 cb840af52f Update the vendored kernel to the temporary path fix
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-14 18:08:43 +08:00
TheM14 f0cab2eec5 Update the vendored kernel to the ffmpeg path fix
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-14 17:37:17 +08:00
TheM14 5c3083b11c Add forced 24-bit output (int24) for the delivered samples
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-14 17:34:29 +08:00
TheM14 ce3de51f8f Update the vendored kernel to the JSON writer fix
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-09 00:12:41 +08:00
TheM14 49f3a0f040 Update the vendored kernel to the bounded objects16 render-ahead
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-06 15:05:35 +08:00
TheM14 4d22797130 Fix 32-bit dropouts: enable AVX2 and bound the render-ahead
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-06 02:43:44 +08:00
TheM14 ea30786711 Fix files past MAX_PATH not being claimed or decoded
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-10-01 14:07:34 +08:00
TheM14 89508a372a Bound the diagnostic log and cut the per-file scan noise
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-29 18:44:14 +08:00
TheM14 8f28c69df7 Fix THIRD_PARTY_NOTICES to cover third-party sources only
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-28 22:56:02 +08:00
TheM14 8f44bf8a68 Clean up leftover gain assignments in the kernel
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-28 19:02:14 +08:00
TheM14 4d8118684f Fix MP4 probe when moov exceeds the window
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-27 15:58:23 +08:00
TheM14 bb464b2c03 Fix bed decoded off the E-AC-3 frame grid
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-27 14:46:12 +08:00
TheM14 71bec4bda3 Fix band-0 DC filter gating and improve settings UI
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-27 01:24:53 +08:00
TheM14 65f56990aa Fix missing tags, broken seeking, and the x64 playback crash
build / windows (push) Has been cancelled
build / release (push) Has been cancelled
2026-09-26 03:14:02 +08:00
58 changed files with 4531 additions and 986 deletions

No files matched your search

+5 -1
View File
@@ -27,5 +27,9 @@
*.personalized_headphone
rosella_kernels.npz
# The component writes its own log next to the DLL
# The component writes its own log next to the DLL, and rolls it to a .1 sibling
joc_decoder.log
joc_decoder.log.1
# Measurement record: local working notes, kept on disk and never published.
VERIFICATION.md
+144
View File
@@ -0,0 +1,144 @@
# foo_input_joc — foobar2000 input component for E-AC-3 JOC
[简体中文](README.md) · [Install](#install) · [Settings](#settings) · [Building](#building) · [Known limitations](#known-limitations)
A foobar2000 input component for **E-AC-3 JOC (Dolby Atmos)** files: the JOC object and OAMD
metadata is taken from the E-AC-3 syncframes and paired with the core PCM that ffmpeg decodes
(5.1 or 7 channels, following $joc\_dmx\_config\_idx$), then rendered in real time to headphones
(HRTF) or to a speaker layout up to 7.1. The
rendering core is compiled into the component, so there is nothing to install beside
`foo_input_joc.dll`.
Input is a bare `.eac3` / `.ec3` stream, or an E-AC-3 JOC track inside a container (`.mp4`
`.m4a` `.m4b` `.m4p` `.m4r` `.mov` `.mkv` `.mka` `.webm`). A file without JOC is not claimed:
plain AC-3 / E-AC-3, a container whose audio track is AAC, and transport streams are all handed
back to foobar2000 and play through the decoder it would have used anyway.
## Requirements
* foobar2000 1.6 (32-bit) or 2.x (32-bit and 64-bit).
* An `ffmpeg` executable, found on `PATH` by default; the preferences page can name one.
* For binaural output, an HRTF data file. **This repository does not ship one** — see
[HRTF data](#hrtf-data).
## Install
Download the package for your architecture from [Releases](../../releases) — a pushed `v*` tag
makes CI attach both — or build it yourself as described under [Building](#building) and take the
result from `dist\`; either way the file is `foo_input_joc-<version>-<arch>.fb2k-component`. Drop
it onto foobar2000, or use Preferences → Components → Install, and restart. Take `-x86` for
foobar2000 1.6 and 2.x 32-bit, `-x64` for 2.x 64-bit.
To install by hand, copy `foo_input_joc.dll` into the per-component subdirectory of the
`user-components` folder the running version reads:
| foobar2000 | Folder |
|---|---|
| 1.6 | `<profile>\user-components\foo_input_joc\` |
| 2.x | `<app>\user-components\foo_input_joc\` (also in portable mode) |
The subdirectory is required — a DLL lying directly in `user-components\` is not scanned — and a
component placed in the folder the running version does not read, or built for the other
architecture, is **ignored without a message**.
`tools/deploy.ps1 -TestBed <portable foobar2000>` is the scripted form of the manual steps, and
`tools/run.ps1 -TestBed <path> -Play <file>` plays a file unattended and prints the log. Let
foobar2000 exit through `/exit`: a force-killed instance leaves a `<profile>\running` marker
behind, and the next start then refuses to load any user component.
### Containers need one look at the decoder list
foobar2000 asks the decoders in the order shown in Preferences → **Decoding**, and the built-in
container readers are in that list. When one of them is offered an MP4 or Matroska file first, it
takes the file and the JOC objects are lost — such a file then plays as plain E-AC-3.
So move **JOC decoder (E-AC-3 JOC)** above **foobar2000 MP4 Demuxer** and **foobar2000
Matroska/WebM Reader** in that list. Bare `.eac3` / `.ec3` files do not depend on the order.
## Settings
Preferences → Tools → **JOC decoder**:
* **Output** — binaural, or a speaker layout from 2.0 to 7.1;
* **Binaural mode** (near / mid / far) and the room **tail** in seconds;
* **HRTF source** — a **SOFA** file or a **Rosella** `.personalized_headphone` model. An empty
path means the default location, `<component directory>\HRTF\binaural.sofa` or
`binaural.personalized_headphone`;
* **Gain** — a switch and a value in dB. Binaural rendering can exceed full scale on material
that does not clip in the core mix, so attenuation belongs here;
* **Force 24-bit output (int24)** — off by default, which delivers float32. When on, every sample
is quantised with the core's own int24 rule (clip to ±1, ×8388607, truncate) before it is handed
over, the samples that had to be clipped are counted in the log, and the component reports the
file as 24-bit fixed point. The renderer itself stays float32: this is the precision the decoder
delivers, not the output device's bit depth, which is set in Preferences → Output. **Reload a
file's information after flipping this switch** (Properties → *Reload info*, or remove the file
from the Media Library and add it again): foobar2000 caches technical information per file, and
until it is reloaded Properties and the Converter still go by the old 32-bit floating-point
answer, warning that the encoder does not take floating-point input.
* the **ffmpeg** executable to use.
No control on the page is disabled, and the status line states what is in effect.
### HRTF data
A SOFA measurement set or a personalised headphone model is supplied by whoever runs the
component, and is listed in `.gitignore` so it cannot be committed by accident. Speaker layouts
need none. Binaural rendering without an HRTF fails with a message naming the file it looked for.
## Building
```powershell
pwsh -File tools/setup_sdk.ps1 # official SDK into SDK/, pinned to target 1.5/1.6
pwsh -File tools/build.ps1 # Win32 -> build\Win32\foo_input_joc.dll
pwsh -File tools/build.ps1 -Platform x64
pwsh -File tools/package.ps1 # both, packaged into dist\*.fb2k-component
```
The configuration is fixed at `Release-Static` (static CRT, `/MT`), and `/fp:precise` is what the
byte-for-byte acceptance rests on, so it must not be changed. `foo_input_joc.vcxproj` builds
`kernel\joc_kernel.vcxproj` first through a project reference; the rendering core sources in
`kernel/` are compiled with `JOC_STATIC` / `EJOC_STATIC`, so their entry points are neither
imported nor exported. `tests\` holds the offline tools (bitstream self-test and cross-check,
render comparison, preferences-page layout check, container-probe check) and `tools\` the build
and test-bed scripts. Settings also read `JOC_*` environment overrides for one run (development
only); the list and what each one does is in `src\settings.cpp`.
To diagnose a problem, read `joc_decoder.log` beside the DLL: the component writes its own
version, the core version and the log path there at start-up. The log is capped by **bytes**: a
file that reaches 16 MiB is rolled to `joc_decoder.log.1` beside it, replacing the previous roll
rather than accumulating, so the two together never exceed 32 MiB and the newest lines are always
in `joc_decoder.log`. The version banner is written again after a roll. A Media Library scan logs
every container in the library, which is where the volume comes from.
## Known limitations
* ADM BWF output is not implemented.
* All five $joc\_dmx\_config\_idx$ configurations are supported: the bed is paired per audio
frame, and its channel count follows the configuration (6 channels for 0/3, 8 for 1/2/4), with
`-ac 6` passed to ffmpeg only for the 6-channel ones. An AC-3 core plus a dependent substream
(`strmtyp=1`) and a first audio frame without JOC are both normal cases.
* A container is only claimed when this component is ahead of the built-in container reader in
Preferences → Decoding (see [Install](#containers-need-one-look-at-the-decoder-list)); the core
does not let a decoder ask for a file another entry has already taken. Such a file's tags stay
that reader's as well.
* Tags of a bare `.eac3` / `.ec3` (an ID3v2 tag in front of the stream, or an APEv2/ID3v1 tag
behind it) can be **read but not written**: no component claims raw E-AC-3 for writing, and
rewriting the whole file to insert a tag is not this component's job.
* Transport streams (`.ts`, `.m2ts`) are not claimed.
* **Force 24-bit output** only changes the precision the decoder delivers: foobar2000 converts the
int24 it receives into its own floating-point samples, and which bit depth the output device runs
at is still decided in Preferences → Output, not by this component. Samples outside ±1 follow the
core's `continue` clip policy (binaural rendering can exceed full scale, so use **Gain** when
attenuation is wanted).
* Playback length is exactly the file's duration. The binaural renderer still computes its room
tail, but it is not delivered as playback time the file does not have.
* A seek re-enters the bitstream at the frame holding the target instead of decoding everything
in front of it — which is why the cost of a seek does not depend on where it lands. The
position is exact and does not drift; the samples are the same waveform handed to a decoder
that started there, so they differ from a straight play-through by a low-level noise floor of
−59 dBFS or quieter.
## Licence
`LICENSE` is the upstream MIT licence, copied unchanged; `kernel/` is a copy of the upstream
rendering core sources and keeps their notices. See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
+97 -123
View File
@@ -1,143 +1,117 @@
# foo_input_joc
# foo_input_joc — foobar2000 的 E-AC-3 JOC 输入组件
foobar2000 input component for **E-AC-3 JOC (Dolby Atmos)** files: the JOC objects are
rendered to binaural (HRTF) or to a speaker layout up to 7.1, in real time.
[English](README.en.md) · [安装](#安装) · [设置](#设置) · [构建](#构建) · [已知限制](#已知限制)
Two files with the same name pay for the whole thing: `joc_core`'s C++ sources are copied
into [`kernel/`](kernel/) and compiled straight into the component, so there is nothing to
install beside `foo_input_joc.dll`.
foobar2000 输入组件,播放 **E-AC-3 JOC(Dolby Atmos)** 文件:从 E-AC-3 同步帧里取出 JOC
对象与 OAMD 元数据,与 ffmpeg 解出的核心 PCM(按 $joc\_dmx\_config\_idx$ 为 5.1 或 7 通道)配对后实时渲染,输出双耳(HRTF)或最多
7.1 的扬声器布局。渲染内核直接编译进组件,除 `foo_input_joc.dll` 之外不需要安装任何东西。
## What it does
输入是裸流 `.eac3` / `.ec3`,或容器里的 E-AC-3 JOC 轨道(`.mp4` `.m4a` `.m4b` `.m4p`
`.m4r` `.mov` `.mkv` `.mka` `.webm`)。文件里没有 JOC 就不接管:普通 AC-3 / E-AC-3、音频轨道
是 AAC 的容器、传输流都交还 foobar2000,由它原本的解码器播放。
1. Finds the audio: a bare `.eac3` / `.ec3` stream is read as it is, while a container
(`.mp4`, `.m4a`, `.m4b`, `.m4p`, `.m4r`, `.mov`, `.mkv`, `.mka`, `.webm`) is looked into
first — the container's own headers say whether an E-AC-3 track is present and which
audio track it is (MP4 sample entry `ec-3`, Matroska `CodecID A_EAC3`), and ffmpeg then
copies that track out of the file byte for byte. The header walk is bounded and cheap, so
an MP4 holding AAC is declined without starting anything.
2. Decides from the bitstream whether it really carries JOC (an EMDF container holding both
the OAMD and the JOC payload — container metadata only ever says "E-AC-3", and the JOC
flag inside it is frequently missing).
3. A file with no E-AC-3 track, or with one that carries no JOC, is handed back to
foobar2000 with `exception_io_unsupported_format`, so the built-in decoder plays it — this
component never decodes plain AC-3 or E-AC-3.
4. A JOC file is decoded as: the syncframes go to the renderer as metadata, the 5.1 core
PCM comes from ffmpeg, and the renderer pairs them (one syncframe : 1536 bed samples)
and produces the output PCM, which is handed back to foobar2000.
## 环境要求
```
.eac3 / .ec3 file container (.mp4 .mkv .m4a ...)
│ ├─ header walk (src/container_scan.cpp) ── no E-AC-3 ──▶ next decoder
│ └─ E-AC-3 track ── ffmpeg -c:a copy ──▶ syncframes
├─ JOC check (src/eac3_scan.cpp) ─── no JOC ──▶ built-in E-AC-3 decoder
└─ JOC
├─ syncframes ────────────────────▶ renderer metadata
└─ ffmpeg -ac 6 -c:a pcm_f32le ───▶ 5.1 core PCM ──▶ renderer bed
│
▼
2 ch or ≤7.1 PCM ──▶ foobar2000
```
* foobar2000 1.6(32 位)或 2.x(32 位与 64 位)。
* 一个 `ffmpeg` 可执行文件,默认从 `PATH` 找,设置页可以指定路径。
* 双耳输出需要一份 HRTF 数据文件。**本仓库不附带**,见 [HRTF 数据](#hrtf-数据)。
## Repository layout
## 安装
| Path | Contents |
从 [Releases](../../releases) 下载对应架构的包(`v*` 标签触发的 CI 会把两个架构都附上去),
或按[构建](#构建)自行打出 `dist\` 下的产物;文件名都是
`foo_input_joc-<版本>-<架构>.fb2k-component`。把它拖到 foobar2000 上,或用
Preferences → Components → Install 安装,然后重启。1.6 和 2.x 32 位用 `-x86`,2.x 64 位用
`-x64`。
手工安装就把 `foo_input_joc.dll` 放进当前版本会读取的 `user-components` 子目录:
| foobar2000 | 目录 |
|---|---|
| `kernel/` | Copy of the `joc_core` C++ sources (`include/` + `src/`) and `joc_kernel.vcxproj`, the static library the component links |
| `src/eac3_scan.*` | Syncframe walk and the JOC bitstream test |
| `src/container_scan.*` | Bounded header walk of MP4/MOV and Matroska: is there an E-AC-3 track, which one, and how long is the file |
| `src/joc_decode.*` | Decode engine: starts ffmpeg, drives the renderer, handles the end of stream. No foobar2000 headers, so it also builds into the offline tools |
| `src/input_joc.cpp` | The foobar2000 input: format recognition, yielding, `get_info`, `initialize`, `run` |
| `src/settings.*` | Configuration values and their environment overrides (development only) |
| `src/prefs.cpp`, `src/prefs.rc` | The preferences page |
| `src/log.*` | Diagnostic log written next to the DLL |
| `tests/` | Offline tools: bitstream self-test and cross-check against the renderer, render harness, preferences-page layout check, container-probe check |
| `tools/` | SDK fetch, build, package, deploy, unattended test bed run |
| 1.6 | `<profile>\user-components\foo_input_joc\` |
| 2.x | `<app>\user-components\foo_input_joc\`(portable 模式同样如此) |
## Build
子目录是必须的——DLL 直接躺在 `user-components\` 下不会被扫描;放进当前版本不读的目录,或
架构不对,都会**静默忽略**,不会有任何提示。
`tools/deploy.ps1 -TestBed <portable foobar2000>` 是上面手工步骤的脚本版;
`tools/run.ps1 -TestBed <路径> -Play <文件>` 可以无人值守播放并把日志打出来。让 foobar2000
通过 `/exit` 正常退出:被强杀的实例会在 `<profile>\running` 留下标记,下次启动会拒绝加载任何
用户组件。
### 容器需要调一次解码器顺序
foobar2000 按 Preferences → **Decoding** 里的顺序询问解码器,内置的容器读取器也在那张表里。
如果它排在前面接到 MP4 / Matroska 文件,文件就被它拿走,JOC 对象随之丢失——于是听起来只是
普通 E-AC-3。
所以要把 **JOC decoder (E-AC-3 JOC)** 提到 **foobar2000 MP4 Demuxer** 与 **foobar2000
Matroska/WebM Reader** 之前。裸流 `.eac3` / `.ec3` 不受这个顺序影响。
## 设置
Preferences → Tools → **JOC decoder**:
* **Output** —— 双耳,或 2.0 到 7.1 的扬声器布局;
* **Binaural mode**(near / mid / far)与房间 **tail** 秒数;
* **HRTF source** —— **SOFA** 文件或 **Rosella** `.personalized_headphone` 模型。路径留空表示
用默认位置 `<组件目录>\HRTF\` 下的 `binaural.sofa` 或 `binaural.personalized_headphone`;
* **Gain** —— 开关加 dB 值。双耳渲染在核心混音不削顶的素材上也可能超过满刻度,衰减放在这里;
* **强制 24bit 输出(int24)** —— 默认关闭,交付 float32。打开后,交付前按内核写 WAV 的 int24
规则量化(截到 ±1 → ×8388607 → 截断),超范围的样本被截断到边缘、次数记进日志;组件同时把该
文件的技术信息报成 24bit 定点。渲染本身仍是 float32,这一项说的是交付给 foobar2000 的采样精度,
不是输出设备的位深(那个在 Preferences → Output 里设置)。**切换这个开关后要刷新文件信息**
(Properties → Reload info,或把文件从媒体库移除后重新加入):foobar2000 按文件缓存技术信息,
缓存没刷新时 Properties 与转换器仍按旧的 32bit 浮点判断,会提示"编码器不支持浮点输入"。
* **ffmpeg** 可执行文件路径。
页面上的控件都不禁用,状态行会说明当前生效的是什么。
### HRTF 数据
SOFA 测量集或个性化耳机模型由使用组件的人自己提供,并且写在 `.gitignore` 里,避免误提交。
扬声器布局不需要 HRTF。双耳渲染缺少 HRTF 时会报错并指出它找的是哪个文件。
## 构建
```powershell
pwsh -File tools/setup_sdk.ps1 # official SDK into SDK/, pinned to target 1.5/1.6
pwsh -File tools/setup_sdk.ps1 # 官方 SDK 拉进 SDK/,固定到 target 1.5/1.6
pwsh -File tools/build.ps1 # Win32 -> build\Win32\foo_input_joc.dll
pwsh -File tools/build.ps1 -Platform x64
pwsh -File tools/package.ps1 # both, packaged into dist\*.fb2k-component
pwsh -File tools/package.ps1 # 两个架构,打包到 dist\*.fb2k-component
```
`Release-Static` uses the static CRT (`/MT`); `/fp:precise` is required and must not be
changed. `foo_input_joc.vcxproj` builds `kernel\joc_kernel.vcxproj` first through a project
reference. The copied kernel sources are compiled with `JOC_STATIC` / `EJOC_STATIC` so their
entry points are neither imported nor exported.
配置固定为 `Release-Static`(静态 CRT,`/MT`);`/fp:precise` 是逐字节验收的前提,不要改。
`foo_input_joc.vcxproj` 通过项目引用先构建 `kernel\joc_kernel.vcxproj`;`kernel/` 里的渲染内核
源码以 `JOC_STATIC` / `EJOC_STATIC` 编译,入口既不导入也不导出。`tests\` 是离线工具(码流
自检与交叉核对、渲染比对、设置页布局检查、容器探测检查),`tools\` 是构建与测试床脚本。设置项
另有 `JOC_*` 环境变量覆盖(仅用于开发运行),清单与含义在 `src\settings.cpp`。
## Install
排查问题看 DLL 旁边的 `joc_decoder.log`;组件启动时会把自己的版本、核心版本、日志路径写在
里面。日志按**字节**封顶:单个文件写满 16 MiB 就滚到同目录的 `joc_decoder.log.1`(覆盖上一次
滚动,不累积),所以两份加起来不超过 32 MiB,最新的内容始终在 `joc_decoder.log`。滚动之后版本
信息会重写一遍。媒体库扫描会给库里每个容器写日志,体量主要来自这里。
Either drop `dist\foo_input_joc-<version>-<arch>.fb2k-component` onto foobar2000 (or use
Preferences → Components → Install), or copy `foo_input_joc.dll` into
`<profile>\user-components\foo_input_joc\`. The per-component subdirectory is required:
a DLL lying directly in `user-components\` is not scanned. 1.6 is 32-bit, 2.x ships both,
and a DLL of the wrong architecture is silently ignored.
## 已知限制
`tools/deploy.ps1 -TestBed <path to portable foobar2000>` does the manual variant, and
`tools/run.ps1 -TestBed <path> -Play <file>` runs it unattended and prints the log.
Always let foobar2000 exit through `/exit`; a force-killed instance leaves a
`<profile>\running` marker behind and the next start then refuses to load any user
component.
* 不实现 ADM BWF 输出。
* $joc\_dmx\_config\_idx$ 全部五个档位都支持:取床按音频帧配对,床通道数由档位决定(0/3
为 6 通道,1/2/4 为 8 通道),ffmpeg 只在 6 通道档位传 `-ac 6`。AC-3 核心 + 依赖子流
(`strmtyp=1`)与"首个音频帧没有 JOC"都按正常情况处理。
* 容器只有在它排在内置容器读取器之前时才会被接管(见[安装](#容器需要调一次解码器顺序));核心
不允许某个解码器去要一个已经被别的条目拿走的文件。这类文件的标签也仍旧归那个读取器。
* 裸流 `.eac3` / `.ec3` 的标签(流前面的 ID3v2,或后面的 APEv2/ID3v1)**能读不能写**:没有
组件声明可以写裸 E-AC-3,为插入标签重写整个文件也不是本组件该做的事。
* 传输流(`.ts`、`.m2ts`)不接管。
* **强制 24bit 输出**只改变解码器交付的采样精度:交付的 int24 会被 foobar2000 转成它内部的
浮点,输出设备用什么位深仍由 Preferences → Output 决定,组件不能替它选。超出 ±1 的样本按内核
的 `continue` 策略截断(双耳渲染可能超过满刻度,需要衰减时用 **Gain**)。
* 播放长度严格等于文件时长。双耳渲染器仍会算出房间尾音,但它不作为文件本身没有的播放时间交付。
* 跳转会从包含目标位置的那个帧重新进入码流,而不是把前面的内容全部解码一遍——这是跳转代价与
目标位置无关的原因。位置精确、不漂移;样本是同一段波形交给了从该处开始的解码器,与从头播放
相比差一个很低的噪声底(−59 dBFS 或更低)。
### Containers need one look at the decoder list
## 许可
foobar2000 tries the decoders in the order shown in Preferences → **Decoding** (the
"list of available decoders", where entries can be moved up and down). The built-in
container readers are in that list too, and when one of them is offered an MP4 or Matroska
file before this component, it takes the file and the JOC objects are lost — the file plays
as plain E-AC-3.
So, to play JOC from a container, move **JOC decoder (E-AC-3 JOC)** above **foobar2000 MP4
Demuxer** and **foobar2000 Matroska/WebM Reader** in that list. Nothing else is needed, and
bare `.eac3` / `.ec3` files are unaffected by the order. This is the same thing every
third-party decoder (the FFmpeg wrapper, for one) asks for, which is why the component does
not try to work around it. If a container still plays as plain E-AC-3, that list is where to
look.
## Settings
Preferences → Tools → **JOC decoder**:
* **Output** — binaural, or a speaker layout from 2.0 to 7.1;
* **Binaural mode** (near / mid / far) and the room **tail** in seconds;
* **HRTF source** — a **SOFA** file or a **Rosella** `.personalized_headphone` model. Leave
the path empty to use the default location `<component directory>\HRTF\`:
`binaural.sofa` or `binaural.personalized_headphone`;
* **Gain** — a switch plus a value in dB. Binaural rendering can exceed full scale on
material that does not clip in the core mix, so attenuation belongs here;
* the **ffmpeg** executable to use.
Nothing on the page is disabled; the status line states what is in effect.
**HRTF data is not distributed with this repository.** A SOFA measurement set or a
personalised headphone model is supplied by whoever runs the component (and is listed in
`.gitignore` so it cannot be committed by accident). Speaker layouts and every offline test
except binaural rendering work without one; binaural rendering without an HRTF fails with a
message naming the file it looked for.
## Environment overrides
Development only: they override the stored settings for one run and every use is logged.
`JOC_OUTPUT`, `JOC_LAYOUT`, `JOC_HRTF`, `JOC_HRTF_SOURCE`, `JOC_BINAURAL_MODE`, `JOC_GAIN_DB`,
`JOC_GAIN_ENABLED`, `JOC_TAIL_SECONDS`, `JOC_OBJECT_DELAY`, `JOC_THREADS`, `JOC_FFMPEG`,
`JOC_LOG`.
## Known limitations
* ADM BWF output is not implemented.
* A container is only claimed when this component is ahead of the built-in container reader
in Preferences → Decoding, as described under [Install](#install); the core does not let a
decoder ask for a file another entry has already taken.
* Transport streams (`.ts`, `.m2ts`) are not claimed.
* The room tail is returned in full; the reference command-line renderer additionally trims
trailing samples below a threshold, so its output can be shorter.
* x86 and x64 do not produce bit-identical binaural output (last-bit differences): the
renderer's SIMD dispatch only applies to x86-64/ARM64, so 32-bit builds take the scalar
path. The speaker path is bit-identical on both.
## Licence
`LICENSE` is the upstream MIT licence, copied unchanged; `kernel/` is a copy of the upstream
renderer sources and keeps their notices. See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
`LICENSE` 是上游 MIT 许可,原样复制;`kernel/` 是上游渲染内核源码的副本,保留其声明。见
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。
+12 -26
View File
@@ -1,31 +1,9 @@
# Third-party notices / 第三方通知
本仓库包含 JustOneCacophony 原生库的逐字节副本(下称"复用文件"),以及遵循公开标准实现的
滤波器组表。本文件记录这些来源、公开标准依据与权利边界。
## 复用文件
以下文件是本项目(JustOneCacophony,MIT)原生 C++ 库的逐字节副本,不修改、不追加注释:
| 本仓库路径 | 上游路径 | SHA-256(前 16 位) |
|---|---|---|
| `src/joc_core/eac3joc_core.cpp` | `native/src/eac3joc_core.cpp` | `1978eea64a2616fa` |
| `src/joc_core/qmf_tables.h` | `native/src/qmf_tables.h` | `c205ea187e956e87` |
| `src/speaker/speaker_renderer.cpp` | `native/src/speaker_renderer.cpp` | `96f36f40daf86eec` |
| `src/speaker/speaker_layouts.h` | `native/src/speaker_layouts.h` | `51e24c11be09787f` |
| `src/binaural/binaural_renderer.cpp` | `native/src/binaural_renderer.cpp` | `d579803f0f5a6699` |
| `src/joc_bitstream/joc_huffman_tables.h` | `native/src/joc_huffman_tables.h` | `698498b3778d88db` |
| `include/eac3joc_core.h` | `native/include/eac3joc_core.h` | `7392f48dfd840656` |
上游修订:`6bc2c2885666bb151bb66af93472199af9a99b81`。
## 派生文件
`src/binaural/sofa_binaural_renderer.cpp` 是上游 `native/src/sofa_binaural_renderer.cpp` 的派生
实现:同一 `ejoc_sofa_binaural_*` C ABI,增加表提升、结果记忆化、输入校验与 SIMD 派发。它
**不属于**逐字节副本,也不受"不得修改"约束,但来源固定为上述上游修订,对应上游源文件的
SHA-256 为 `81b485e4c71907672ab308ddc382284acf29161cee93d7906785a4cce58941c7`,输出与原
实现逐位相同(见该文件头部的验证记录)。
本文件记录 64-QMF / 77-hybrid 滤波器组表(`kernel/src/hrtf/public_filterbank.h`、
`kernel/src/joc_core/qmf_tables.h`)与 JOC Huffman 表
(`kernel/src/joc_bitstream/joc_huffman_tables.h`)的公开标准来源,以及 HRTF 数据与
专利的边界说明。
## 公开标准来源
@@ -52,6 +30,14 @@ $$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 传输层与通道码表
`kernel/src/eac3_transport/eac3_reader.cpp` 的 AC-3 同步帧长度表由 A/52 的
`fscod`/`frmsizecod` 比特率表换算得到(48/44.1/32 kHz 三列),E-AC-3 帧长用标准
`frmsiz` 字段;`kernel/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` 定义。这些表是按公开标准写的数值,不含任何第三方实现代码。
## HRTF 数据与 `.jochrtf`
`.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发仍受源
-144
View File
@@ -1,144 +0,0 @@
# Verification␍␍
␍␍
Measured results only. Each entry names the command or the log line it came from, so it can␍␍
be reproduced. Environment: Windows x64 host, official **foobar2000 1.6.19 x86** portable␍␍
installation, official SDK 2026-09-17 pinned to `FOOBAR2000_TARGET_VERSION 80`, MSVC 14.44,␍␍
ffmpeg 8.0.␍␍
␍␍
Test material is supplied locally and is **not** part of this repository: the `testdata/` and␍␍
`vectors/` files of the upstream renderer project, and — for the binaural measurements — an␍␍
HRTF file. Everything except binaural rendering runs without any HRTF; binaural runs take the␍␍
file as an argument (`tests/render_harness.cpp --hrtf …`) or use the default location beside␍␍
the DLL.␍␍
␍␍
## Component and renderer␍␍
␍␍
| Check | Result |␍␍
|---|---|␍␍
| Sources compiled in, nothing loaded at run time | log: `core: in-process renderer 0.1.0-m1 (abi 3), component built against abi 3` |␍␍
| One artefact, no companion DLL | `dist\*.fb2k-component` holds `foo_input_joc.dll` and `README.md` only |␍␍
| Kernel sources untouched | the upstream working tree's file timestamps are unchanged; it is only ever read |␍␍
| Both architectures build | `build\Win32\foo_input_joc.dll`, `build\x64\foo_input_joc.dll` |␍␍
␍␍
## Playback in foobar2000 1.6.19 x86␍␍
␍␍
| Case | Log evidence |␍␍
|---|---|␍␍
| Speaker 7.1, 5 s file | `stream created, 8 output channel(s), layout=7.1` … `frames_in=157 frames_out=157 samples_out=241152` — 157 × 1536 exactly |␍␍
| Binaural, SOFA | `stream created, 2 output channel(s)` … `end of stream after 481215 frames` (241152 source + 240063 tail) |␍␍
| Binaural, Rosella model | `stream created, 2 output channel(s)` … `end of stream after 481855 frames` |␍␍
| Binaural, HRTF path left empty | resolves to `<component directory>\HRTF\binaural.sofa` and produces the same 481215 frames as naming that file explicitly |␍␍
| Full 238 s file, binaural SOFA | `eac3 frames queued=7436, bed frames pushed=7436` … `samples_out=11661759`; no ffmpeg process left behind |␍␍
| Installed from the `.fb2k-component` package | unpacked into `user-components\foo_input_joc\`, plays with the default-folder HRTF |␍␍
␍␍
## Bitstream recognition␍␍
␍␍
`tests/scan_crosscheck.cpp` compares, frame by frame, the syncframe lengths and the JOC␍␍
verdict this component computes against the renderer's own `joc_eac3_frame_bytes()` /␍␍
`joc_parse_eac3_frame()`:␍␍
␍␍
```␍␍
testdata\gold_forever.eac3 frames=64 plugin_joc=64 kernel_joc=64 len_mismatch=0 verdict_mismatch=0␍␍
vectors\valid.eac3 frames=7 plugin_joc=7 kernel_joc=7 len_mismatch=0 verdict_mismatch=0␍␍
build\plain_eac3.eac3 frames=64 plugin_joc=0 kernel_joc=0 len_mismatch=0 verdict_mismatch=0␍␍
crosscheck: 3 file(s), AGREES WITH CORE␍␍
```␍␍
␍␍
Ten corrupt vectors were compared as well: no frame-length disagreement, verdicts agreed on␍␍
9 of 10. The one difference is `corrupt_truncated_huffman.eac3`: this component only tests␍␍
for the container, while the renderer also parses the payload and reports a truncated␍␍
bitstream later.␍␍
␍␍
## Plain E-AC-3 is handed back␍␍
␍␍
Playing a file the component's own encoder produced without JOC:␍␍
␍␍
```␍␍
decoder: open "...plain_eac3.eac3" reason=1 bytes=144384 frames=8 with_joc=0␍␍
decoder: yielding to the built-in decoder (at least one examined syncframe has no JOC EMDF container)␍␍
```␍␍
␍␍
The file then plays through the built-in decoder; no decode log appears for it. Verified both␍␍
before and after the renderer was compiled in.␍␍
␍␍
## Output identical to the reference renderer␍␍
␍␍
`tests/render_harness.cpp` drives the component's own engine and writes a WAV in the same␍␍
format the reference command-line renderer writes, so the two files can be compared byte for␍␍
byte. 30 s of the reference file, speaker 5.1:␍␍
␍␍
| Product | Whole-file SHA-256 |␍␍
|---|---|␍␍
| reference renderer (`--speaker-layout 5.1 --bed … --duration 30`) | `99a8e3edbd1c047a3c0f547eaf85e56941f882af9e65468fbde0a9f18b6c5b6e` |␍␍
| this component's engine, x64 | `99a8e3edbd1c047a3c0f547eaf85e56941f882af9e65468fbde0a9f18b6c5b6e` |␍␍
| this component's engine, x86 | `99a8e3edbd1c047a3c0f547eaf85e56941f882af9e65468fbde0a9f18b6c5b6e` |␍␍
␍␍
34,578,500 bytes each. Binaural with a real SOFA file: with the same input frame count the␍␍
whole file is identical too (`588a15ce5526977f…baa88`, 12,158,780 bytes), and over the whole␍␍
238 s file the total sample count matches exactly (11,661,759 = 11,420,735 program +␍␍
241,024 tail) with identical peak (1.144561172) and an identical SHA-256 over the reference␍␍
renderer's entire payload.␍␍
␍␍
The one structural difference is the tail: the reference renderer trims trailing samples␍␍
below 1e-8 and this component returns the tail in full.␍␍
␍␍
## Gain␍␍
␍␍
Same file, speaker 5.1, rendered with and without attenuation:␍␍
␍␍
| Check | Result |␍␍
|---|---|␍␍
| Peak | 0.183568597 → 0.092002235, i.e. exactly −6.000000 dB |␍␍
| RMS over the whole signal | exactly −6.000000 dB |␍␍
| Per-sample, 1,446,912 floats | largest deviation from the ideal scaling is 2.1e-06 relative; the gain is applied in double precision before the DSP, so the remaining difference is float32 rounding |␍␍
| +6 dB | same check passes |␍␍
| The switch | switch on and −6 dB → log `gain=-6.00 dB`, delivered peak 0.000063; switch off with −6 dB still stored → `gain=0.00 dB`, peak 0.000126; switch on and 0 dB → peak 0.000126 |␍␍
␍␍
## Preferences page␍␍
␍␍
`tests/prefs_layout_check.cpp` builds the page from the component's own dialog resource and␍␍
asserts, per control, that it is enabled, lies inside the client area, and is not covered by␍␍
another interactive control (static text and group boxes are transparent to the mouse, as␍␍
they are for real clicks):␍␍
␍␍
```␍␍
dialog client=495x367 non-client=0x0␍␍
WS_CAPTION=no WS_BORDER=no WS_CHILD=yes WS_VISIBLE=yes␍␍
content extent=485x354 client=495x367 (everything fits)␍␍
controls=26 problems=0␍␍
```␍␍
␍␍
The page has no caption of its own — the host draws the frame — and no control is ever␍␍
disabled, which is what "the option is there but cannot be clicked" otherwise looks like.␍␍
␍␍
Not verified here: how the page and the `%joc_*%` fields look on screen; that needs a human␍␍
in front of the window.␍␍
␍␍
## Container support (mp4 / m4a / mov / mkv / mka / webm)␍␍
␍␍
Verified with the Win32 build, a 5.1 speaker layout, and the component installed in a␍␍
portable foobar2000 1.6.19 profile:␍␍
␍␍
- `ffmpeg -i <bare .eac3> -c:a copy` muxed into MP4 and Matroska, then `-map 0:a:0 -c:a copy␍␍
-f eac3` extracted again, is byte-identical to a direct `-t 30 -c:a copy` of the source␍␍
(same SHA-256) — the renderer therefore sees the stored syncframes, not a re-encode.␍␍
- The header probe (`tests/container_scan_test.cpp`, no foobar2000 involved) reports, for the␍␍
same material: `joc.mp4 -> mp4 eac3=1 audio#0 codec=ec-3 30.016 s`,␍␍
`joc.mkv -> matroska eac3=1 audio#0 codec=A_EAC3 30.016 s`, `plain_eac3.mp4 -> ec-3`,␍␍
`ac3.mp4 -> ac-3 (declined)`, `aac.mp4 -> mp4a (declined)`.␍␍
- End to end, with the component ordered ahead of the container reader in␍␍
Preferences -> Decoding: `open()` is called, the track is found, the JOC verdict is positive,␍␍
the file is claimed, and playback reaches `end of stream`.␍␍
- Negative case end to end: an MP4 holding E-AC-3 without JOC yields with␍␍
`E-AC-3 track 0 carries no JOC`, and the built-in decoder plays it.␍␍
- Bare `.eac3` / `.ec3` handling is unchanged.
## Bed decode and gain
* The 5.1 bed is decoded with -drc_scale 0 -target_level 0, so it is taken as stored and the
decoder does not apply the stream's dynrng or target-level metadata. The resulting bed is
byte-identical to the one the reference renderer uses (same SHA-256).
* The master gain is applied once, by the JOC kernel, on every output path. Measured at -6 dB
against 0 dB: speaker 5.1 and SOFA binaural give 0.50119 in peak and in per-channel RMS,
Rosella binaural gives 0.50119 as well, and a 0 dB render is unaffected by the renderer's
output gain.
+1
View File
@@ -114,6 +114,7 @@
<ClInclude Include="src\prefs.h" />
<ClInclude Include="src\resource.h" />
<ClInclude Include="src\settings.h" />
<ClInclude Include="src\win_path.h" />
</ItemGroup>
<ItemGroup>
+21 -5
View File
@@ -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
@@ -68,13 +72,25 @@ EJOC_API ejoc_renderer_handle EJOC_CALL ejoc_renderer_create(void);
EJOC_API void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads);
/*
core_channel_count comes from joc_dmx_config_idx: 5 for configurations 0 and 3,
7 for 1, 2 and 4. bed_planar is then [core_channel_count][1536] and only channels
below core_channel_count are read from dq, whose stride stays [15][2][7][23].
phase_shift is sigma(k) = (k in {3,4}): it enables the Ls/Rs -j rotation of
subbands above 0 together with the band-0 21-tap DC compensation. With the switch
off, channels 3 and 4 only take the common ten-slot surround delay. The delay
line and DC history advance either way, so the flag may change between frames.
*/
EJOC_API uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle);
EJOC_API const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_process(
ejoc_renderer_handle handle,
const float* 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,
+5 -3
View File
@@ -34,7 +34,9 @@
#include <stdint.h>
#include <stddef.h>
#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 */
@@ -373,7 +375,7 @@ typedef struct joc_task_config {
/* input */
const char* input_path; /* .eac3/.ec3/.m4a/... */
const char* ffmpeg_path; /* NULL = "ffmpeg" from PATH */
const char* ffmpeg_path; /* NULL = FFMPEG, then "ffmpeg" on PATH */
const char* bed_path; /* NULL = decode the core PCM with ffmpeg */
const char* work_dir; /* NULL = a temporary directory */
double eac3_drc_scale; /* 0 = DRC off (reference default) */
+14 -4
View File
@@ -39,14 +39,14 @@ typedef struct joc_stream joc_stream;
typedef enum joc_stream_input {
JOC_STREAM_IN_EAC3 = 0, /* bare E-AC-3 syncframes (the metadata stream) */
JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* 16-channel objects16, decoded by the host */
JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* objects16 frames, planar [16][1536] each */
JOC_STREAM_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */
} joc_stream_input;
typedef enum joc_stream_output {
JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* planar [16][samples] float32 */
JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */
JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */
JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* objects16 frames, planar [16][1536] each */
JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */
JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */
} joc_stream_output;
typedef struct joc_stream_config {
@@ -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;
+16
View File
@@ -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<JOC_VERSION_STRING>.
*/
#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 */
+23 -10
View File
@@ -5,10 +5,12 @@
shipping and loading joc_core.dll.
The sources under kernel\src and kernel\include are a verbatim copy of the
upstream library (see JustOneCacophonyCXX\CMakeLists.txt); the only edits are
the JOC_STATIC branch in kernel\include\joc_core.h / joc_stream.h and the
EJOC_STATIC branch in kernel\include\eac3joc_core.h, which turn JOC_API and
EJOC_API into empty macros for a static build.
upstream library (see JustOneCacophonyCXX\CMakeLists.txt): nothing under either
tree is edited here, and both are kept identical to it, so a single core serves
the upstream tools and this component alike. The static build needs no branch
of its own: JOC_STATIC (kernel\include\joc_core.h / joc_stream.h) and
EJOC_STATIC (kernel\include\eac3joc_core.h) are already part of the shared
headers, turning JOC_API and EJOC_API into empty macros.
Conventions deliberately mirror the component project (foo_input_joc.vcxproj)
and the kernel's own CMake build:
@@ -113,18 +115,17 @@
</ItemDefinitionGroup>
<!--
SIMD units, mirroring CMakeLists.txt (the JOC_SIMD_* block) exactly.
SIMD units, mirroring CMakeLists.txt (the JOC_SIMD_* block).
The ISA in the file name, one translation unit per ISA, each compiled with
its own /arch because MSVC has no function-level ISA attribute; dispatch.cpp
(a baseline unit) picks one at run time from CPUID/XGETBV. The kernel
enables SIMD only for x86_64 and aarch64, so:
(a baseline unit) picks one at run time from CPUID/XGETBV.
* Win32 (x86) gets NO SIMD unit and NO SIMD define: the scalar reference
and the probe/dispatch baseline are all it builds, exactly as
CMAKE_SIZEOF_VOID_P EQUAL 8 gates them out upstream;
* x64 gets the AVX2 and AVX-512 units, JOC_SIMD_HAVE_SSE2 / _AVX2 /
_AVX512, and a per-file /arch for those two files only;
* Win32 (x86) gets the AVX2 unit and JOC_SIMD_HAVE_AVX2: AVX2 is not an
x86-64-only ISA. AVX-512 stays x64-only, because 32-bit mode addresses
ZMM0-7 only.
* src\simd\kernels_intrin_neon.cpp is excluded everywhere here: the CMake
build lists it only for aarch64 (CMakeLists.txt lines 177-180), which no
configuration of this project targets.
@@ -134,6 +135,11 @@
<PreprocessorDefinitions>JOC_SIMD_HAVE_SSE2=1;JOC_SIMD_HAVE_AVX2=1;JOC_SIMD_HAVE_AVX512=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'">
<ClCompile>
<PreprocessorDefinitions>JOC_SIMD_HAVE_AVX2=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<!-- Verbatim copies of the upstream native library (JOC_REUSED_SOURCES). -->
@@ -203,10 +209,17 @@
</ClCompile>
</ItemGroup>
<ItemGroup Condition="'$(Platform)'=='Win32'">
<ClCompile Include="src\simd\kernels_intrin_avx2.cpp">
<AdditionalOptions>/arch:AVX2 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\eac3joc_core.h" />
<ClInclude Include="include\joc_core.h" />
<ClInclude Include="include\joc_stream.h" />
<ClInclude Include="include\joc_version.h" />
<ClInclude Include="src\adm\adm_metadata.h" />
<ClInclude Include="src\adm\adm_tracks.h" />
<ClInclude Include="src\binaural\binaural_runtime.h" />
+3 -11
View File
@@ -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<std::uint32_t>(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) {
+12 -2
View File
@@ -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<std::uint32_t>(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;
+11 -1
View File
@@ -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"
@@ -129,7 +132,8 @@ void print_usage() {
" --trajectory-mode M ADM 对象轨迹表示 compact|dense64,默认 compact\n"
"\n"
"输入与解码:\n"
" --ffmpeg PATH ffmpeg 可执行文件,默认取 FFMPEG 环境变量或 PATH\n"
" --ffmpeg PATH ffmpeg 可执行文件;本选项优先,默认先取 FFMPEG 环境变量,\n"
" 再在 PATH 中查找\n"
" --eac3-drc-scale X E-AC-3 解码器 -drc_scale,0=关闭码流 dynrng,默认 0\n"
" --eac3-target-level N E-AC-3 解码器 -target_level,0=不施加,默认 0\n"
" --backend B JOC/扬声器 DSP 后端 auto|native;本构建无 python 后端\n"
@@ -209,6 +213,7 @@ void parse_args(const std::vector<std::string>& arguments, Options* options) {
for (int index = 1; index < argc; ++index) {
const std::string arg = arguments[static_cast<std::size_t>(index)];
if (arg == "-h" || arg == "--help") { options->help = true; }
else if (arg == "--version") { options->version = true; }
else if (arg == "-o" || arg == "--output") { options->output = require_value(arguments, &index); }
else if (arg == "--speaker-output") { options->speaker_output = require_value(arguments, &index); }
else if (arg == "--binaural-output") { options->binaural_output = require_value(arguments, &index); }
@@ -529,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<unsigned>(joc_abi_version()));
return 0;
}
if (options.help) {
print_usage();
return 0;
+125 -29
View File
@@ -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<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) |
data[offset + 1]) != FrameReader::kSyncword) {
return false;
}
Frame frame;
frame.data = data + offset;
frame.offset = offset;
const std::size_t base = offset * 8u;
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
frame.bsid = static_cast<std::uint8_t>(bsid);
if (bsid <= 8u) {
frame.codec = 0;
const std::uint32_t fscod = read_bits(data, base + 32u, 2u);
const std::uint32_t frmsizecod = read_bits(data, base + 34u, 6u);
if (fscod > 2u || frmsizecod >= 38u) {
return false;
}
frame.size = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 48u, 3u));
std::size_t bit = 51u;
if ((frame.acmod & 0x1u) != 0u && frame.acmod != 0x1u) {
bit += 2u; // cmixlev
}
if ((frame.acmod & 0x4u) != 0u) {
bit += 2u; // surmixlev
}
if (frame.acmod == 0x2u) {
bit += 2u; // dsurmod
}
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + bit, 1u));
if (out != nullptr) {
*out = frame;
}
return true;
}
frame.codec = 1;
frame.strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
frame.substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
const std::uint32_t frmsiz = read_bits(data, base + 21u, 11u);
frame.size = (static_cast<std::size_t>(frmsiz) + 1u) * 2u;
// fscod==3 also consumes two bits (fscod2), so acmod always starts at bit 36.
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 36u, 3u));
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + 39u, 1u));
std::size_t bit = 45u + 5u; // bsid, then dialnorm
const std::uint32_t compre = read_bits(data, base + bit, 1u);
++bit;
if (compre != 0u) {
bit += 8u; // compr
}
if (frame.acmod == 0u) {
bit += 5u; // dialnorm2
const std::uint32_t compr2 = read_bits(data, base + bit, 1u);
++bit;
if (compr2 != 0u) {
bit += 8u;
}
}
if (frame.strmtyp == 1u) {
if (read_bits(data, base + bit, 1u) != 0u) {
frame.has_chanmap = 1;
frame.chanmap = static_cast<std::uint16_t>(read_bits(data, base + bit + 1u, 16u));
}
}
if (out != nullptr) {
*out = frame;
}
return true;
}
void FrameReader::push(const std::uint8_t* data, std::size_t size) {
if (failed_ || data == nullptr || size == 0) {
return;
@@ -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<std::uint16_t>((static_cast<std::uint16_t>(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<std::size_t>(((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<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]) !=
kSyncword) {
return JOC_ERR_EAC3_SYNCFRAME;
}
const std::size_t words =
static_cast<std::size_t>(((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;
}
+22 -1
View File
@@ -1,4 +1,3 @@
#pragma once
#include <cstddef>
@@ -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 {
+60 -3
View File
@@ -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<std::uint8_t>(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<std::uint8_t>(payload_id);
entry.sample_offset = sample_offset;
entry.group_id = group_id;
entry.has_group_id = has_group_id;
entry.bit_offset = reader.position();
entry.size = payload_size;
@@ -206,7 +212,8 @@ void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector<std:
std::sort(out->begin(), 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<std::uint8_t>* out) {
out->assign(container.raw_size, 0u);
+25 -1
View File
@@ -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<std::size_t>* 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).
+2 -2
View File
@@ -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;
}
+96 -1
View File
@@ -1,5 +1,6 @@
#include "foundation/fs_utf8.h"
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <vector>
@@ -12,7 +13,6 @@
#include <fcntl.h>
#include <io.h>
#else
#include <cstdlib>
#include <unistd.h>
#endif
@@ -20,6 +20,70 @@ namespace joc::fs_utf8 {
namespace fs = std::filesystem;
namespace {
#if defined(_WIN32)
constexpr char kPathListSeparator = ';';
#else
constexpr char kPathListSeparator = ':';
#endif
// PATH (and PATHEXT) are separator-delimited lists. An empty entry is kept: on both
// platforms it means the current directory.
std::vector<std::string> split_list(const char* value, char separator) {
std::vector<std::string> parts;
if (value == nullptr) {
return parts;
}
std::string current;
for (const char c : std::string(value)) {
if (c == separator) {
parts.push_back(current);
current.clear();
continue;
}
current.push_back(c);
}
parts.push_back(current);
return parts;
}
bool is_runnable_file(const std::string& utf8_path) {
std::error_code error;
if (!fs::is_regular_file(to_path(utf8_path), error) || error) {
return false;
}
#if defined(_WIN32)
return true;
#else
// Existence is not enough: a file without the execute bit cannot be started, so
// deciding here names the real problem instead of a later "exit code 126".
return ::access(utf8_path.c_str(), X_OK) == 0;
#endif
}
bool carries_directory(const std::string& utf8_path) {
#if defined(_WIN32)
return utf8_path.find_first_of("/\\") != std::string::npos;
#else
return utf8_path.find('/') != std::string::npos;
#endif
}
#if defined(_WIN32)
// A bare name only resolves through PATHEXT on Windows. The environment is the
// authority; the classic four are the fallback when it is unset.
std::vector<std::string> executable_extensions() {
std::vector<std::string> extensions = split_list(std::getenv("PATHEXT"), ';');
if (extensions.empty()) {
extensions = {".COM", ".EXE", ".BAT", ".CMD"};
}
return extensions;
}
#endif
} // namespace
fs::path to_path(const std::string& utf8) {
return fs::path(std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size()));
}
@@ -112,6 +176,37 @@ std::string executable_path() {
#endif
}
std::string find_executable(const std::string& reference) {
if (reference.empty()) {
return std::string();
}
if (carries_directory(reference)) {
// A reference that already names a location is the caller's own decision:
// keep it verbatim rather than searching for a same-named program elsewhere.
return is_runnable_file(reference) ? reference : std::string();
}
#if defined(_WIN32)
const bool named_with_extension = to_path(reference).has_extension();
#endif
for (const std::string& entry : split_list(std::getenv("PATH"), kPathListSeparator)) {
const fs::path directory = to_path(entry.empty() ? std::string(".") : entry);
const std::string candidate = from_path(directory / to_path(reference));
if (is_runnable_file(candidate)) {
return candidate;
}
#if defined(_WIN32)
if (!named_with_extension) {
for (const std::string& extension : executable_extensions()) {
if (is_runnable_file(candidate + extension)) {
return candidate + extension;
}
}
}
#endif
}
return std::string();
}
std::ifstream open_input(const std::string& utf8_path) {
return std::ifstream(to_path(utf8_path), std::ios::binary);
}
+8
View File
@@ -33,6 +33,14 @@ std::string temp_directory();
// report it. Defaults are anchored here so they never depend on the CWD.
std::string executable_path();
// Resolves an executable reference to a concrete path. A reference that carries a
// directory separator is a location the caller chose, so it is kept verbatim when
// it names a runnable file and rejected otherwise; a bare name is looked up on PATH
// (through PATHEXT on Windows). Empty means "could not be resolved", which lets a
// caller report the name it was given instead of starting a child process that the
// shell would have to find.
std::string find_executable(const std::string& reference);
// Streams: std::ifstream/ofstream accept a std::filesystem::path, which is the
// portable way to open a UTF-8 path.
std::ifstream open_input(const std::string& utf8_path);
+46
View File
@@ -7,6 +7,8 @@ namespace joc::json {
namespace {
const char kHexDigits[] = "0123456789abcdef";
void skip_space(const std::string& text, std::size_t* index) {
while (*index < text.size() &&
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' ||
@@ -109,6 +111,50 @@ bool read_compound(const std::string& text, std::size_t* index, std::string* out
} // namespace
std::string quote(const std::string& text) {
std::string out;
out.reserve(text.size() + 2u);
out.push_back('"');
for (const char character : text) {
const unsigned char value = static_cast<unsigned char>(character);
switch (character) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (value < 0x20u) {
out += "\\u00";
out.push_back(kHexDigits[value >> 4u]);
out.push_back(kHexDigits[value & 0x0Fu]);
} else {
out.push_back(character);
}
break;
}
}
out.push_back('"');
return out;
}
std::string pretty_object(const std::vector<Field>& fields) {
if (fields.empty()) {
return "{}";
}
std::string out = "{";
for (std::size_t index = 0u; index < fields.size(); ++index) {
out += index == 0u ? "\n " : ",\n ";
out += quote(fields[index].key);
out += ": ";
out += fields[index].value;
}
out += "\n}";
return out;
}
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
out->clear();
std::size_t index = 0;
+18
View File
@@ -16,6 +16,24 @@ struct Member {
// Parses a top-level JSON object. Rejects non-objects and duplicate keys.
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error);
// One member of an object being written: `key` is escaped here, `value` is
// already JSON text (a quoted string, a number, a literal).
struct Field {
std::string key;
std::string value;
};
// Escapes `text` as a JSON string literal, quotes included. Matches
// json.dumps(..., ensure_ascii=False): the named escapes for `"` and `\` and
// for U+0008/U+0009/U+000A/U+000C/U+000D, `\u00XX` for the other control
// characters, and every other byte - UTF-8 included - passed through.
std::string quote(const std::string& text);
// Renders a flat object the way json.dumps(members, ensure_ascii=False,
// indent=2) does: two-space indentation, one member per line, the members in
// the order given, and no trailing newline.
std::string pretty_object(const std::vector<Field>& fields);
const Member* find(const std::vector<Member>& members, const std::string& key);
bool as_string(const Member& member, std::string* out);
+16
View File
@@ -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";
+5 -26
View File
@@ -13,6 +13,7 @@
#include <vector>
#include "foundation/fft.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h"
#include "hrtf/public_filterbank.h"
#include "io/npy_writer.h"
@@ -651,32 +652,10 @@ Status convert_coordinates(const std::vector<double>& values, std::size_t rows,
// ------------------------------------------------------------- JSON ---------
std::string json_escape(const std::string& text) {
std::string out = "\"";
for (const char character : text) {
const unsigned char value = static_cast<unsigned char>(character);
switch (character) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
default:
if (value < 0x20u) {
char buffer[8];
std::snprintf(buffer, sizeof(buffer), "\\u%04x", value);
out += buffer;
} else {
out.push_back(character);
}
break;
}
}
out += "\"";
return out;
}
// The cache metadata is the compact form the reference writes
// (json.dumps(sort_keys=True, separators=(",", ":"))), so only the escaping is
// shared with the indented report and metadata documents.
std::string json_escape(const std::string& text) { return json::quote(text); }
std::string json_number(double value) { return io::python_float_repr(value); }
+544 -12
View File
@@ -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<std::uint8_t>(reader.read(6));
out->ext_config_idx = static_cast<std::uint8_t>(reader.read(3));
const std::uint32_t n_objects = static_cast<std::uint32_t>(out->num_objects_bits) + 1u;
const std::uint32_t declared_objects = static_cast<std::uint32_t>(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<std::uint8_t>(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<int>(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<std::uint32_t>(declared_objects, JOC_MAX_OBJECTS);
out->n_objects = static_cast<std::uint8_t>(n_objects);
out->clipgain_x_bits = static_cast<std::uint8_t>(reader.read(3));
out->clipgain_y_bits = static_cast<std::uint8_t>(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<double>(out->clipgain_y_bits) / 32.0 *
std::ldexp(1.0, static_cast<int>(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<std::uint8_t> bytes;
if (emdf::extract_payload_bytes(data, size, *payload, &bytes).ok()) {
joc_frame_params params;
FrameSymbols symbols;
parses = parse_id14(bytes.data(), bytes.size(), &params, &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<std::uint8_t>(JOC_CORE_CHANNELS);
out->n_objects = static_cast<std::uint8_t>(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<Position>* 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<Position>& 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<Position>* 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<Position>* 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<Position>* 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<Position>& core, std::uint16_t chanmap,
std::vector<Position>* out) {
if (out == nullptr) {
return;
}
std::vector<Position> 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<Position> 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<Position> extra;
std::vector<Position> locations = core_positions;
if (has_dependent && has_chanmap) {
std::vector<Position> declared;
expand_chanmap(chanmap, &declared);
chanmap_extra_pair(core_positions, chanmap, &extra);
locations.insert(locations.end(), declared.begin(), declared.end());
}
std::vector<Position> 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<int>(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<Position> wanted = {Position::kLeft, Position::kRight, Position::kCenter,
Position::kLeftSurround, Position::kRightSurround};
wanted.insert(wanted.end(), extra.begin(), extra.end());
if (wanted.size() != static_cast<std::size_t>(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<Position> extra;
expected_pair(dmx_config_idx, &extra);
std::vector<Position> wanted = {Position::kLeft, Position::kRight, Position::kCenter,
Position::kLeftSurround, Position::kRightSurround};
wanted.insert(wanted.end(), extra.begin(), extra.end());
std::vector<Position> 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<int>(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<oamd::BedAssignment>* 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<Position> 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<Position> 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<Position> 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<Position> 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<Position> declared = core_positions;
if (has_chanmap) {
std::vector<Position> 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<Position> 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
+93
View File
@@ -6,9 +6,11 @@
#include <cstdint>
#include <vector>
#include "eac3_transport/eac3_reader.h"
#include "joc_core.h"
#include "emdf/emdf_parser.h"
#include "foundation/status.h"
#include "oamd/oamd_parser.h"
namespace joc::joc {
@@ -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<Position>* out);
// A/52 acmod -> full-band positions in decoding order (LFE excluded).
void acmod_positions(std::uint8_t acmod, std::vector<Position>* out);
// The pair joc_dmx_config_idx declares; empty for configurations 0 and 3.
void expected_pair(std::uint8_t dmx_config_idx, std::vector<Position>* out);
// The extra pair a dependent-substream chanmap adds on top of the core channels.
void chanmap_extra_pair(const std::vector<Position>& core, std::uint16_t chanmap,
std::vector<Position>* out);
// Decoded-bed channel count implied by the JOC core channel count.
int bed_channels_for_core(int core_channels);
// bed[lane] = decoded PCM channel index feeding JOC lane 'lane'.
struct BedPlan {
int channels = 0; // 6 or 8
int lfe_channel = -1;
int lane[JOC_MAX_CORE_CHANNELS] = {-1, -1, -1, -1, -1, -1, -1};
int extra_pair_slot[2] = {-1, -1}; // ADM skeleton
};
Status compute_bed_plan(const eac3::Frame* core_frame, bool has_core, bool has_dependent,
bool has_chanmap, std::uint16_t chanmap, std::uint8_t dmx_config_idx,
BedPlan* out);
// Bed plan driven by joc_dmx_config_idx alone, for callers that hold no L2
// header: the extra pair is the one the configuration declares.
Status compute_bed_plan_for_config(std::uint8_t dmx_config_idx, BedPlan* out);
// Identity disagreements that leave the DSP input unchanged (spec section 10.1:
// channel identity never enters the DSP): the extra pair's name is metadata only,
// so a mismatch is reported through CrossLayerNotes instead of refusing the frame.
struct CrossLayerNotes {
bool chanmap_pair_mismatch = false; // spec 11.2 check 1: chanmap vs k
bool bed_mask_mismatch = false; // spec 11.2 check 2: OAMD bed mask vs k
};
// Spec section 11.2 cross-layer consistency. A mismatch in the core channel count
// or in joc_ext_config_idx changes the DSP input and is a variant error; the two
// identity checks are recorded in `notes` and the frame still renders.
Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core_frame,
bool has_core, bool has_dependent, bool has_chanmap,
std::uint16_t chanmap,
const std::vector<oamd::BedAssignment>* bed_assignments,
std::string* error, CrossLayerNotes* notes = nullptr);
} // namespace joc::joc
+74 -43
View File
@@ -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;
}
@@ -126,8 +128,10 @@ public:
}
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,
@@ -140,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");
}
@@ -165,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);
@@ -295,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;
}
}
@@ -305,16 +315,18 @@ private:
for (int i = 0; i < 4; ++i) {
dc_buffer[20 + i] = current[i][0];
}
for (int slot = 0; slot < 4; ++slot) {
Complex sum{0.0, 0.0};
for (int tap = 0; tap < 21; ++tap) {
const Complex sample = dc_buffer[slot + tap];
const double cr = kDcB[tap];
const double ci = kDcA[tap];
sum.re += sample.re * cr - sample.im * ci;
sum.im += sample.re * ci + sample.im * cr;
if (phase_shift_enabled_) {
for (int slot = 0; slot < 4; ++slot) {
Complex sum{0.0, 0.0};
for (int tap = 0; tap < 21; ++tap) {
const Complex sample = dc_buffer[slot + tap];
const double cr = kDcB[tap];
const double ci = kDcA[tap];
sum.re += sample.re * cr - sample.im * ci;
sum.im += sample.re * ci + sample.im * cr;
}
x_[channel][0][group + slot] = {2.0 * sum.re, 2.0 * sum.im};
}
x_[channel][0][group + slot] = {2.0 * sum.re, 2.0 * sum.im};
}
for (int i = 0; i < 20; ++i) {
surround_history_[surround][i] = dc_buffer[i + 4];
@@ -332,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;
@@ -348,37 +360,49 @@ private:
const float product = static_cast<float>(static_cast<float>(frame_sample) * phase_step);
gain = static_cast<float>(phase_old + product);
}
scaled[sample] = static_cast<float>(bed5[channel * 1536 + frame_sample] * gain);
scaled[sample] = static_cast<float>(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<float>(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<int>(core_channels_);
for (uint32_t channel = 0; channel < core_channels_; ++channel) {
job_objects_[channel] = static_cast<int>(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<std::size_t>((((object * 2 + point) * 5 + channel) * kMaxBands) + band);
return static_cast<std::size_t>(
(((object * 2 + point) * EJOC_MAX_CORE_CHANNELS + channel) * kMaxBands) + band);
}
void matrix_object(
@@ -395,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];
@@ -459,7 +483,6 @@ private:
void synthesis_object(
const int object,
const double clipgain,
const float output_scale,
float* output16) noexcept {
@@ -487,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<float>(clipped * clipgain);
destination[timeslot * 64 + sample] = static_cast<float>(value * output_scale);
// joc_clipgain already entered the analysis (see analysis()); what
// remains here is the user output gain.
const float value = static_cast<float>(clipped * output_scale);
destination[timeslot * 64 + sample] = value;
}
}
}
@@ -496,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 {
@@ -624,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;
@@ -636,15 +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];
// 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];
@@ -712,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,
@@ -728,8 +759,8 @@ int EJOC_CALL ejoc_renderer_process(
return -1;
}
return static_cast<ejoc::Renderer*>(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"
+16 -9
View File
@@ -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<float>* 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<unsigned>(params.n_channels));
}
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kDsp, *error);
}
std::vector<double> dq(static_cast<std::size_t>(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<std::size_t>(obj) * JOC_MAX_DPOINTS + dp) * JOC_CORE_CHANNELS +
((static_cast<std::size_t>(obj) * JOC_MAX_DPOINTS + dp) *
JOC_MAX_CORE_CHANNELS +
ch) *
JOC_MAX_PARAMETER_BANDS +
pb;
@@ -52,9 +54,14 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa
}
out16->assign(static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f);
// 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 =
+3 -2
View File
@@ -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<float>* out16, std::string* error);
} // namespace joc::joc
+36
View File
@@ -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<BedAssignment>* 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) {
+5
View File
@@ -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<BedAssignment>* out);
} // namespace joc::oamd
+7 -6
View File
@@ -7,10 +7,10 @@
#include "simd/cpu_probe.h"
#if defined(_M_X64)
#if defined(_M_X64) || defined(_M_IX86)
#include <immintrin.h>
#include <intrin.h>
#elif defined(__x86_64__)
#elif defined(__x86_64__) || defined(__i386__)
#include <cpuid.h>
#endif
@@ -21,10 +21,11 @@
namespace joc::simd {
namespace {
// ------------------------------------------------------------------- x86-64 --
#if defined(_M_X64) || defined(__x86_64__)
// ---------------------------------------------------------------------- x86 --
// AVX2 is not an x86-64-only ISA, so both pointer sizes are probed.
#if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)
#if defined(_M_X64)
#if defined(_M_X64) || defined(_M_IX86)
// CPUID tells us what the silicon can do; XCR0 tells us whether the OS saves the
// state the wider registers need. Both have to agree, or the first AVX
@@ -53,7 +54,7 @@ CpuFeatures probe_x86() noexcept {
return features;
}
#else // GCC/Clang on x86-64
#else // GCC/Clang on x86
// The compiler runtime performs the same CPUID + XGETBV probe (libgcc's cpuinfo
// checks XCR0 before it reports AVX), which keeps this file free of inline
+300 -52
View File
@@ -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,8 +34,19 @@ 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();
objects_pending_.clear();
objects_read_offset_ = 0;
output_.clear();
read_offset_ = 0;
info_ = Info();
@@ -118,7 +126,6 @@ Status Stream::create(const Config& config) {
}
render_options.object_delay_samples = config_.object_delay_samples;
render_options.tail_seconds = config_.tail_seconds;
render_options.output_gain = 1.0;
if (status.ok()) {
status = rosella_.open(model, render_options);
}
@@ -179,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, &notes);
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");
@@ -199,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, &params, &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<std::uint8_t> bytes;
const Status extracted =
@@ -213,30 +309,80 @@ 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<std::int64_t>(payload->sample_offset);
pending_frame_.has_update = true;
pending_frame_.outer_offset = static_cast<std::int64_t>(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);
}
return process_ready_frames();
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<std::size_t>(channels));
}
return process_ready_frames();
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<std::size_t>(plan.channels);
config_idx_ = static_cast<int>(joc_entry->params.dmx_config_idx);
core_channels_ = static_cast<int>(joc_entry->params.n_channels);
return true;
}
Status Stream::push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed) {
@@ -246,49 +392,118 @@ Status Stream::push_objects16(const float* planar16, std::size_t samples, std::s
if (planar16 == nullptr || samples == 0u) {
return Status::success();
}
// Rendered immediately: the host has already done the JOC rebuild.
// Queued as whole frames, each planar [16][kFrameSamples]: the shape the
// objects16 output writes, so a host can feed a batch back unchanged.
constexpr std::size_t kFrameValues =
static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples;
for (std::size_t offset = 0; offset < samples; offset += kFrameSamples) {
const std::size_t count = std::min(kFrameSamples, samples - offset);
std::vector<float> frame(static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples,
0.0f);
const std::size_t base = objects_pending_.size();
const float* frame = planar16 + (offset / kFrameSamples) * kFrameValues;
objects_pending_.resize(base + kFrameValues, 0.0f);
for (std::size_t channel = 0; channel < JOC_OUTPUT_CHANNELS; ++channel) {
std::memcpy(frame.data() + channel * kFrameSamples,
planar16 + channel * samples + offset, count * sizeof(float));
std::memcpy(objects_pending_.data() + base + channel * kFrameSamples,
frame + channel * kFrameSamples, count * sizeof(float));
}
++info_.frames_in;
info_.samples_in += count;
const Status rendered = render_objects16(frame);
}
return process_objects16_frames(false);
}
Status Stream::process_objects16_frames(bool drain_all) {
constexpr std::size_t kFrameValues =
static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples;
while (objects_pending_.size() - objects_read_offset_ >= kFrameValues) {
// Leave the rest queued, in order, for a later push or for flush().
if (!drain_all && buffered_samples() >= kMaxRenderAheadSamples) {
break;
}
const auto first = objects_pending_.begin() +
static_cast<std::ptrdiff_t>(objects_read_offset_);
objects_frame_.assign(first, first + static_cast<std::ptrdiff_t>(kFrameValues));
objects_read_offset_ += kFrameValues;
const Status rendered = render_objects16(objects_frame_);
if (!rendered.ok()) {
return rendered;
}
if (count != kFrameSamples) {
break; // a partial frame is dropped; the host should push whole frames
}
++info_.frames_in;
info_.samples_in += kFrameSamples;
}
if (objects_read_offset_ == objects_pending_.size()) {
objects_pending_.clear();
objects_read_offset_ = 0;
} else if (objects_read_offset_ >= (1u << 20)) {
// Erasing from the front moves the remainder, so it is only worth doing
// once the consumed prefix is large enough to pay for the move.
objects_pending_.erase(
objects_pending_.begin(),
objects_pending_.begin() + static_cast<std::ptrdiff_t>(objects_read_offset_));
objects_read_offset_ = 0;
}
return Status::success();
}
Status Stream::process_ready_frames() {
while (bed_pending_.size() / kBedChannels >= kFrameSamples && !metadata_.empty()) {
const FrameMetadata entry = metadata_.front();
Status Stream::process_ready_frames(bool drain_all) {
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;
}
FrameMetadata entry = metadata_.front();
metadata_.pop_front();
std::vector<float> bed5(static_cast<std::size_t>(JOC_CORE_CHANNELS) * kFrameSamples, 0.0f);
std::vector<float> lfe(kFrameSamples, 0.0f);
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_pending_[sample * kBedChannels + kCoreChannels[channel]];
}
lfe[sample] = bed_pending_[sample * kBedChannels + kLfeChannel];
const int core_channels = entry.has_joc ? static_cast<int>(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_pending_.erase(bed_pending_.begin(),
bed_pending_.begin() + static_cast<std::ptrdiff_t>(kFrameSamples *
kBedChannels));
if (!entry.has_joc) {
entry.params.dmx_config_idx = static_cast<std::uint8_t>(config_idx_);
entry.params.n_channels = static_cast<std::uint8_t>(core_channels);
}
const float* bed = bed_pending_.data() + bed_read_offset_;
bed5_.assign(static_cast<std::size_t>(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<std::size_t>(lane) * kFrameSamples + sample] =
bed[sample * bed_channels_ + static_cast<std::size_t>(source)];
}
}
if (bed_plan_.lfe_channel >= 0) {
lfe_[sample] = bed[sample * bed_channels_ +
static_cast<std::size_t>(bed_plan_.lfe_channel)];
}
}
bed_read_offset_ += kFrameSamples * bed_channels_;
compact_bed_pending();
std::string error;
const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5.data(),
lfe.data(), gain_, &objects16_, &error);
const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5_.data(),
lfe_.data(), gain_, &objects16_, &error);
if (!rebuilt.ok()) {
return Status::fail(rebuilt.code(), stage::kDsp, error);
}
@@ -303,6 +518,22 @@ Status Stream::process_ready_frames() {
return Status::success();
}
void Stream::compact_bed_pending() {
if (bed_read_offset_ == 0) {
return;
}
if (bed_read_offset_ == bed_pending_.size()) {
bed_pending_.clear();
bed_read_offset_ = 0;
} else if (bed_read_offset_ >= (1u << 20)) {
// Erasing from the front moves the remainder, so it is only worth doing
// once the consumed prefix is large enough to pay for the move.
bed_pending_.erase(bed_pending_.begin(),
bed_pending_.begin() + static_cast<std::ptrdiff_t>(bed_read_offset_));
bed_read_offset_ = 0;
}
}
Status Stream::render_objects16(const std::vector<float>& objects16) {
if (config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) {
output_.insert(output_.end(), objects16.begin(), objects16.end());
@@ -320,6 +551,7 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
if (!stepped.ok()) {
return Status::fail(stepped.code(), stage::kRender, error);
}
output_.reserve(output_.size() + speaker_.output.size());
for (const double value : speaker_.output) {
output_.push_back(static_cast<float>(value));
}
@@ -340,13 +572,13 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
if (!submitted.ok()) {
return submitted;
}
std::vector<double> produced;
binaural_.take_output(&produced);
for (const double value : produced) {
binaural_.take_output(&produced_);
output_.reserve(output_.size() + produced_.size());
for (const double value : produced_) {
output_.push_back(static_cast<float>(value));
}
info_.frames_out++;
info_.samples_out += produced.size() / 2u;
info_.samples_out += produced_.size() / 2u;
return Status::success();
}
@@ -367,16 +599,15 @@ Status Stream::render_rosella_objects16(const std::vector<float>& objects16) {
if (!submitted.ok()) {
return submitted;
}
std::vector<double> produced;
rosella_.take_output(&produced);
if (!produced.empty()) {
rosella_pending_.insert(rosella_pending_.end(), produced.begin(), produced.end());
rosella_.take_output(&produced_);
if (!produced_.empty()) {
rosella_pending_.insert(rosella_pending_.end(), produced_.begin(), produced_.end());
}
release_rosella_output(kFrameSamples);
info_.frames_out++;
// Counted as the runtime produces it, which is also how the SOFA path counts:
// the totals are identical, only the frame they appear on differs.
info_.samples_out += produced.size() / 2u;
info_.samples_out += produced_.size() / 2u;
return Status::success();
}
@@ -389,6 +620,7 @@ void Stream::release_rosella_output(std::size_t limit) {
return;
}
const std::size_t values = count * 2u;
output_.reserve(output_.size() + values);
for (std::size_t index = 0; index < values; ++index) {
output_.push_back(static_cast<float>(rosella_pending_[rosella_read_offset_ + index]));
}
@@ -430,6 +662,20 @@ 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;
}
const Status objects = process_objects16_frames(true);
if (!objects.ok()) {
return objects;
}
if (binaural_ready_) {
std::vector<double> tail;
const Status drained =
@@ -437,6 +683,7 @@ Status Stream::flush() {
if (!drained.ok()) {
return drained;
}
output_.reserve(output_.size() + tail.size());
for (const double value : tail) {
output_.push_back(static_cast<float>(value));
}
@@ -453,6 +700,7 @@ Status Stream::flush() {
// tail only sounds after it. The program samples were already counted by
// render_rosella_objects16, so only the tail is added here.
release_rosella_output(rosella_pending_samples());
output_.reserve(output_.size() + tail.size());
for (const double value : tail) {
output_.push_back(static_cast<float>(value));
}
+59 -4
View File
@@ -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<eac3::Frame> streams;
std::vector<oamd::BedAssignment> 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();
@@ -85,8 +112,13 @@ public:
: 0u;
}
// Cap on rendered samples that have not been pulled. A push renders what it
// makes ready, so a caller that feeds faster than it pulls renders ahead.
static constexpr std::size_t kMaxRenderAheadSamples = 16384;
private:
Status process_ready_frames();
// `drain_all` ignores kMaxRenderAheadSamples and renders every ready frame.
Status process_ready_frames(bool drain_all);
Status render_objects16(const std::vector<float>& objects16);
Status render_rosella_objects16(const std::vector<float>& objects16);
// Moves at most `limit` rendered stereo samples per channel out of the FIFO
@@ -96,14 +128,36 @@ private:
return rosella_ready_ ? (rosella_pending_.size() - rosella_read_offset_) / 2u : 0u;
}
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);
Config config_;
Info info_;
eac3::FrameReader reader_;
std::deque<FrameMetadata> 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<float> bed_pending_;
std::vector<std::uint8_t> frame_copy_;
std::size_t bed_read_offset_ = 0;
std::vector<float> bed5_;
std::vector<float> lfe_;
std::vector<float> objects_pending_;
std::size_t objects_read_offset_ = 0;
std::vector<float> objects_frame_;
std::vector<float> objects16_;
std::vector<float> output_;
std::size_t read_offset_ = 0;
@@ -115,6 +169,7 @@ private:
hrtf::RosellaRuntime rosella_;
std::vector<double> rosella_pending_;
std::size_t rosella_read_offset_ = 0;
std::vector<double> produced_;
bool speaker_enabled_ = false;
bool binaural_enabled_ = false;
bool binaural_ready_ = false;
+441 -104
View File
@@ -8,6 +8,7 @@
#include <condition_variable>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
@@ -30,6 +31,7 @@
#include "binaural/binaural_runtime.h"
#include "eac3_transport/eac3_reader.h"
#include "emdf/emdf_parser.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h"
#include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.h"
@@ -51,11 +53,8 @@ namespace {
namespace fs = std::filesystem;
using Clock = std::chrono::steady_clock;
constexpr std::size_t kBedFrameBytes = static_cast<std::size_t>(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) {
@@ -81,7 +80,12 @@ std::string fmt(const char* format, ...) {
}
struct Settings {
std::string input_path; std::string ffmpeg_path = "ffmpeg";
std::string input_path;
// ffmpeg is a command rather than a path: the reference is what the caller asked
// for (and what a message names), the path is the executable it resolved to.
std::string ffmpeg_reference = "ffmpeg";
std::string ffmpeg_path;
bool ffmpeg_explicit = false;
std::string bed_path;
std::string work_dir;
std::string output_path;
@@ -119,7 +123,19 @@ struct Settings {
Settings normalise(const joc_task_config& raw) {
Settings cfg;
if (raw.input_path != nullptr) { cfg.input_path = raw.input_path; }
if (raw.ffmpeg_path != nullptr && raw.ffmpeg_path[0] != '\0') { cfg.ffmpeg_path = raw.ffmpeg_path; }
// Resolving ffmpeg here gives every caller the same lookup and gives the run a
// concrete executable instead of a name the child process has to find itself: an
// explicit configuration wins, then FFMPEG, then the plain name on PATH.
if (raw.ffmpeg_path != nullptr && raw.ffmpeg_path[0] != '\0') {
cfg.ffmpeg_reference = raw.ffmpeg_path;
cfg.ffmpeg_explicit = true;
} else {
const char* from_environment = std::getenv("FFMPEG");
if (from_environment != nullptr && from_environment[0] != '\0') {
cfg.ffmpeg_reference = from_environment;
}
}
cfg.ffmpeg_path = fs_utf8::find_executable(cfg.ffmpeg_reference);
if (raw.bed_path != nullptr) { cfg.bed_path = raw.bed_path; }
if (raw.work_dir != nullptr) { cfg.work_dir = raw.work_dir; }
if (raw.output_path != nullptr) { cfg.output_path = raw.output_path; }
@@ -191,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) {
@@ -200,6 +219,7 @@ std::uint64_t count_frames(const std::string& path) {
std::vector<char> chunk(kReadChunk);
std::uint64_t frames = 0;
bool done = false;
std::vector<::joc::eac3::Frame> group;
while (!done) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
@@ -213,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;
}
@@ -234,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<char> chunk(1u << 16);
int config = -1;
bool done = false;
while (!done && config < 0) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
if (got > 0) {
reader.push(reinterpret_cast<const std::uint8_t*>(chunk.data()),
static_cast<std::size_t>(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, &params, &container, nullptr)
.ok()) {
config = static_cast<int>(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.
@@ -440,27 +520,6 @@ Status decide_output_format(const Settings& cfg, telemetry::EventBus* bus, doubl
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kOutput, "unknown clip action");
}
std::string json_escape(const std::string& text) {
std::string out;
out.reserve(text.size() + 8u);
for (const char character : text) {
switch (character) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (static_cast<unsigned char>(character) < 0x20u) {
out += fmt("\\u%04x", static_cast<unsigned>(character));
} else {
out.push_back(character);
}
}
}
return out;
}
// Parses the bitstream without decoding or rendering: --metadata-only and the
// diagnostic metadata reports.
Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std::string& eac3_path,
@@ -482,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<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
@@ -505,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, &params,
&container, nullptr);
if (!parsed.ok()) {
@@ -570,19 +658,22 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
static_cast<unsigned long long>(oamd_payloads));
}
if (!cfg.metadata_json_path.empty()) {
std::string json = fmt(
"{\n \"input\": \"%s\",\n \"frames\": %llu,\n \"objects_min\": %u,\n"
" \"objects_mean\": %.6f,\n \"objects_max\": %u,\n \"clipgain_min\": %.9g,\n"
" \"clipgain_max\": %.9g,\n \"oamd_payloads\": %llu\n}\n",
json_escape(cfg.input_path).c_str(), static_cast<unsigned long long>(frames),
objects_min == 0xFFFFFFFFu ? 0u : objects_min, mean_objects, objects_max,
clipgain_min, clipgain_max, static_cast<unsigned long long>(oamd_payloads));
const std::string document = json::pretty_object({
{"input", json::quote(cfg.input_path)},
{"frames", std::to_string(frames)},
{"objects_min", std::to_string(objects_min == 0xFFFFFFFFu ? 0u : objects_min)},
{"objects_mean", fmt("%.6f", mean_objects)},
{"objects_max", std::to_string(objects_max)},
{"clipgain_min", fmt("%.9g", clipgain_min)},
{"clipgain_max", fmt("%.9g", clipgain_max)},
{"oamd_payloads", std::to_string(oamd_payloads)},
});
std::ofstream output = fs_utf8::open_output(cfg.metadata_json_path);
if (!output) {
return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput,
"cannot write " + cfg.metadata_json_path);
}
output << json;
output << document << "\n";
}
bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO,
fmt("metadata: %llu frames, oamd payloads %llu",
@@ -592,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<bool>(file_);
}
bool read_frame(std::uint64_t index, std::vector<float>* bed5, std::vector<float>* lfe) {
bed5->assign(static_cast<std::size_t>(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<float>* bed, std::vector<float>* lfe) {
bed->assign(static_cast<std::size_t>(core_channels) * JOC_FRAME_SAMPLES, 0.0f);
lfe->assign(JOC_FRAME_SAMPLES, 0.0f);
const std::size_t frame_values = static_cast<std::size_t>(JOC_FRAME_SAMPLES) * channels_;
interleaved_.assign(frame_values, 0.0f);
file_.clear();
file_.seekg(static_cast<std::streamoff>(index * kBedFrameBytes), std::ios::beg);
std::vector<float> interleaved(static_cast<std::size_t>(JOC_FRAME_SAMPLES) * 6u);
file_.read(reinterpret_cast<char*>(interleaved.data()),
static_cast<std::streamsize>(interleaved.size() * sizeof(float)));
if (file_.gcount() != static_cast<std::streamsize>(interleaved.size() * sizeof(float))) {
file_.seekg(static_cast<std::streamoff>(index * frame_values * sizeof(float)),
std::ios::beg);
file_.read(reinterpret_cast<char*>(interleaved_.data()),
static_cast<std::streamsize>(frame_values * sizeof(float)));
if (file_.gcount() != static_cast<std::streamsize>(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<std::size_t>(lane) * JOC_FRAME_SAMPLES + n] =
interleaved_[n * channels_ + static_cast<std::size_t>(source)];
}
}
if (plan.lfe_channel >= 0) {
(*lfe)[n] = interleaved_[n * channels_ +
static_cast<std::size_t>(plan.lfe_channel)];
}
(*lfe)[n] = interleaved[n * 6u + kLfeChannel];
}
return true;
}
private:
std::ifstream file_;
std::size_t channels_ = 6;
std::vector<float> interleaved_;
};
// ---------------------------------------------------------------------------
@@ -1018,9 +1125,26 @@ Status validate(const joc_task_config& raw, std::vector<joc_validation_issue>* i
warn(JOC_ERR_INVALID_CONFIG, "native_threads",
"native_threads is clamped to the 15 JOC objects");
}
if (cfg.bed_path.empty() && !fs_utf8::exists(cfg.ffmpeg_path)) {
warn(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path",
"bed PCM is not supplied, so ffmpeg must be reachable: " + cfg.ffmpeg_path);
// ffmpeg is needed exactly where the task shells out to it: unpacking a container,
// and decoding the core PCM when no bed is supplied. An unresolvable executable is
// a hard error there, because the alternative is a run that fails inside the child
// process; an explicitly requested one that goes unused is only worth a note.
const bool elementary_stream_input =
ends_with(lower(cfg.input_path), ".eac3") || ends_with(lower(cfg.input_path), ".ec3");
const bool needs_ffmpeg =
!elementary_stream_input || (cfg.bed_path.empty() && !cfg.metadata_only);
if (cfg.ffmpeg_path.empty()) {
const std::string origin = cfg.ffmpeg_explicit ? " (--ffmpeg)" : "";
if (needs_ffmpeg) {
fail(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path",
"ffmpeg not found: '" + cfg.ffmpeg_reference + "'" + origin +
" is not an executable file and is not on PATH; pass --ffmpeg PATH, set the "
"FFMPEG environment variable, or supply an already decoded --bed PCM");
} else if (cfg.ffmpeg_explicit) {
warn(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path",
"ffmpeg not found: '" + cfg.ffmpeg_reference +
"' (--ffmpeg); this task does not need ffmpeg, so the option is unused");
}
}
if (error_count != nullptr) {
*error_count = errors;
@@ -1076,7 +1200,10 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
const std::string lowered = lower(cfg.input_path);
if (!ends_with(lowered, ".eac3") && !ends_with(lowered, ".ec3")) {
temp_dir = create_temp_dir(cfg);
eac3_path = temp_dir + "\\input.eac3";
// Joined as a path rather than with a separator character: a hard-coded
// backslash names a file that can only ever exist on Windows, so on POSIX the
// child process wrote somewhere else while the checks below looked in vain.
eac3_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "input.eac3");
bus.stage(JOC_STAGE_INPUT, "extracting the E-AC-3 elementary stream");
const io::ProcessResult extracted = [&] {
io::ProcessResult result;
@@ -1094,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<int>(::joc::joc::num_channels_for_config(
static_cast<std::uint32_t>(stream_config_idx)))
: static_cast<int>(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 = 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<std::string> 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)});
@@ -1209,7 +1356,6 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
}
render_options.object_delay_samples = cfg.object_delay_samples;
render_options.tail_seconds = cfg.tail_seconds;
render_options.output_gain = std::pow(10.0, cfg.gain_db / 20.0);
if (status.ok()) {
status = rosella.open(model, render_options);
}
@@ -1316,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);
}
@@ -1350,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
@@ -1360,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, &notes);
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<std::uint8_t>(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(&params);
}
params.dmx_config_idx = static_cast<std::uint8_t>(stream_config_idx);
params.n_channels = static_cast<std::uint8_t>(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<double>(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<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
if (got > 0) {
@@ -1388,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,
&params, &container, nullptr);
if (!parsed.ok()) {
@@ -1405,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;
@@ -1429,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<std::int64_t>(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<double>(Clock::now() - stage_started).count();
if (!joc_ring.push(&job)) {
stream_done = true;
break;
}
++produced;
stage_started = Clock::now();
}
}
} catch (...) {
@@ -1713,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);
@@ -1920,23 +2250,30 @@ Status result_to_json(const joc_task_result& result, std::string* out) {
if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output");
}
*out = fmt(
"{\"status\":%u,\"error_code\":%u,\"error_stage\":\"%s\",\"error_message\":\"%s\","
"\"input_frames\":%llu,\"output_samples\":%llu,\"duration_sec\":%.6f,"
"\"output_format_actual\":%u,\"output_peak\":%.9g,\"output_over_unity_values\":%llu,"
"\"output_file_bytes\":%llu,\"output_sha256\":\"%s\",\"oamd_payloads\":%llu,"
"\"oamd_transitions\":%llu,\"t_decode_bed\":%.6f,\"t_render\":%.6f,\"t_write\":%.6f,"
"\"t_render_dsp\":%.6f,\"t_write_file\":%.6f,\"t_total\":%.6f}",
result.status, result.error_code, result.error_stage, result.error_message,
static_cast<unsigned long long>(result.input_frames),
static_cast<unsigned long long>(result.output_samples), result.duration_sec,
result.output_format_actual, result.output_peak,
static_cast<unsigned long long>(result.output_over_unity_values),
static_cast<unsigned long long>(result.output_file_bytes), result.output_sha256,
static_cast<unsigned long long>(result.oamd_payloads),
static_cast<unsigned long long>(result.oamd_transitions), result.t_decode_bed,
result.t_render, result.t_write, result.t_render_dsp, result.t_write_file,
result.t_total);
// The string fields are escaped, so a Windows path or a quote in an error
// message still leaves the document valid JSON.
*out = json::pretty_object({
{"status", std::to_string(result.status)},
{"error_code", std::to_string(result.error_code)},
{"error_stage", json::quote(result.error_stage)},
{"error_message", json::quote(result.error_message)},
{"input_frames", std::to_string(result.input_frames)},
{"output_samples", std::to_string(result.output_samples)},
{"duration_sec", fmt("%.6f", result.duration_sec)},
{"output_format_actual", std::to_string(result.output_format_actual)},
{"output_peak", fmt("%.9g", result.output_peak)},
{"output_over_unity_values", std::to_string(result.output_over_unity_values)},
{"output_file_bytes", std::to_string(result.output_file_bytes)},
{"output_sha256", json::quote(result.output_sha256)},
{"oamd_payloads", std::to_string(result.oamd_payloads)},
{"oamd_transitions", std::to_string(result.oamd_transitions)},
{"t_decode_bed", fmt("%.6f", result.t_decode_bed)},
{"t_render", fmt("%.6f", result.t_render)},
{"t_write", fmt("%.6f", result.t_write)},
{"t_render_dsp", fmt("%.6f", result.t_render_dsp)},
{"t_write_file", fmt("%.6f", result.t_write_file)},
{"t_total", fmt("%.6f", result.t_total)},
});
return Status::success();
}
+28 -12
View File
@@ -8,18 +8,19 @@
#include <vector>
#include "log.h"
#include "win_path.h"
namespace joc_container {
namespace {
std::wstring utf8_to_wide(const std::string& text) {
if (text.empty()) return {};
const int needed = MultiByteToWideChar(CP_UTF8, 0, text.c_str(),
static_cast<int>(text.size()), nullptr, 0);
std::wstring out(static_cast<std::size_t>(needed), L'\0');
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()), out.data(),
needed);
return out;
// A library scan probes every container in the library, and most of them are
// declined; they also mostly share a directory, so the path would spend the log's
// byte budget repeating the same prefix once per file. The file name is what
// identifies the line. Files this component claims are logged with their full
// path by the caller.
std::string file_name_of(const std::string& path) {
const std::string::size_type slash = path.find_last_of("\\/");
return (slash == std::string::npos) ? path : path.substr(slash + 1);
}
// A bounded, read-only view of the file. Every accessor returns false instead of
@@ -27,7 +28,7 @@ std::wstring utf8_to_wide(const std::string& text) {
class Window {
public:
bool open(const std::string& path) {
const std::wstring wide = utf8_to_wide(path);
const std::wstring wide = joc_path::to_wide_extended(path);
handle_ = CreateFileW(wide.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle_ == INVALID_HANDLE_VALUE) {
@@ -200,15 +201,24 @@ Result scan_mp4(const Window& file, std::size_t max_bytes) {
Result result;
result.kind = Kind::kMp4;
// moov is usually at the start for streamed files and at the end otherwise.
// moov is usually at the start for streamed files and at the end otherwise. The
// walk reads box headers where they lie and steps over mdat in one go, so the
// whole file is walked from the top: a fixed prefix window cannot reach a moov
// that is larger than the window, which is what an MP4 with its cover art stored
// as a video track produces (a 4.9 MB moov against a 4 MiB window). The tail
// range stays as a fallback for a file whose leading boxes do not parse.
struct Range {
std::uint64_t begin;
std::uint64_t end;
};
std::vector<Range> ranges{{0, (std::min<std::uint64_t>)(file.size(), max_bytes)}};
std::vector<Range> ranges{{0, file.size()}};
if (file.size() > max_bytes) {
ranges.push_back({file.size() - max_bytes, file.size()});
}
// An oversized moov is a file this probe cannot describe, and walking it would be
// unbounded work; it is reported instead of entered.
constexpr std::uint64_t kMaxMoovBytes = 64ull * 1024ull * 1024ull;
bool oversized_moov = false;
unsigned audio_seen = 0;
bool found_any_audio = false;
@@ -218,6 +228,10 @@ Result scan_mp4(const Window& file, std::size_t max_bytes) {
[&](const std::string& type, std::uint64_t payload, std::uint64_t box_end,
std::size_t) {
if (type != "moov") return true;
if (box_end - payload > kMaxMoovBytes) {
oversized_moov = true;
return true;
}
walk_boxes(file, payload, box_end, 1,
[&](const std::string& inner, std::uint64_t inner_payload,
std::uint64_t inner_end, std::size_t) {
@@ -273,6 +287,8 @@ Result scan_mp4(const Window& file, std::size_t max_bytes) {
result.detail = "mp4: first audio track is " +
(first_audio_format.empty() ? std::string("unknown")
: first_audio_format);
} else if (oversized_moov) {
result.detail = "mp4: moov is larger than the scan limit";
} else {
result.detail = "mp4: no audio track found in the scanned window";
}
@@ -519,7 +535,7 @@ Result scan(const std::string& path, std::size_t max_bytes) {
} else {
result = scan_mp4(file, max_bytes);
}
joc_log::line("container: %s -> %s (eac3=%d audio#%u codec=%s)", path.c_str(),
joc_log::line("container: %s -> %s (eac3=%d audio#%u codec=%s)", file_name_of(path).c_str(),
result.detail.c_str(), result.eac3 ? 1 : 0, result.audio_index,
result.codec.empty() ? "-" : result.codec.c_str());
return result;
+112 -5
View File
@@ -249,16 +249,120 @@ bool contains_payload(const std::uint8_t* frame, std::size_t size, std::uint8_t
} // namespace
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
if (data == nullptr || offset + 4u > size) return 0;
namespace {
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) {
value = (value << 1) | ((data[index >> 3] >> (7u - (index & 7u))) & 1u);
}
return value;
}
// AC-3 syncframe length in 16-bit words, indexed by fscod then frmsizecod.
const 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},
};
} // namespace
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out) {
if (data == nullptr || offset + 6u > size) return 0;
const std::uint16_t syncword =
static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]);
if (syncword != kEac3Syncword) return 0;
const std::size_t base = offset * 8u;
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
if (bsid <= 8u) {
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 0;
if (out != nullptr) {
out->frame_bytes = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
out->offset = offset;
out->codec = 0;
out->strmtyp = 0;
out->substreamid = 0;
}
return static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
}
const std::size_t words =
(static_cast<std::size_t>(data[offset + 2] & 0x07u) << 8) | data[offset + 3];
if (out != nullptr) {
out->frame_bytes = (words + 1u) * 2u;
out->offset = offset;
out->codec = 1;
out->strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
out->substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
}
return (words + 1u) * 2u;
}
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
return frame_header_at(data, size, offset, nullptr);
}
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream) {
constexpr std::size_t kMaxSubstreams = 8;
FrameHeader seen[kMaxSubstreams];
std::size_t count = 0;
std::size_t consumed = 0;
while (count < kMaxSubstreams) {
FrameHeader header;
const std::size_t bytes = frame_header_at(data, size, offset + consumed, &header);
if (bytes == 0 || offset + consumed + bytes > size) break;
bool repeats = false;
for (std::size_t index = 0; index < count; ++index) {
if (seen[index].same_stream(header)) {
repeats = true;
break;
}
}
if (repeats) return consumed;
seen[count++] = header;
consumed += bytes;
}
if (count != 0u && consumed != 0u && end_of_stream) return consumed;
return 0;
}
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out) {
if (out == nullptr || frame == nullptr || frame_bytes < 8u) return false;
std::size_t offsets[4096];
std::size_t count = 0;
marker_offsets(frame, frame_bytes, offsets, 4096, &count);
for (std::size_t i = 0; i < count; ++i) {
Container candidate;
if (!parse_container(frame, frame_bytes * 8u, offsets[i], &candidate)) continue;
if (!candidate.has(kIdOamd) || !candidate.has(kIdJoc)) continue;
for (std::size_t p = 0; p < candidate.payload_count; ++p) {
if (candidate.payloads[p].id != kIdJoc) continue;
// joc_dmx_config_idx is the first 3 bits of the JOC payload, followed by
// joc_num_objects_bits (6) and joc_ext_config_idx (3).
const std::size_t bit = candidate.payloads[p].bit_offset;
std::uint32_t value = 0;
for (unsigned index = 0; index < 3u; ++index) {
const std::size_t position = bit + index;
if (position / 8u >= frame_bytes) return false;
value = (value << 1) | ((frame[position / 8u] >> (7u - (position % 8u))) & 1u);
}
*out = value;
return true;
}
}
return false;
}
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes) {
if (frame == nullptr || frame_bytes < 8u) return false;
@@ -333,12 +437,15 @@ ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_fram
if (frames == 0) {
result.joc = JocState::kUnknown;
if (result.detail[0] == '\0') result.detail = "no syncframes";
} else if (with_joc == frames) {
} else if (with_joc != 0) {
result.joc = JocState::kYes;
result.detail = "every examined syncframe carries the JOC EMDF container";
result.detail = (with_joc == frames)
? "every examined syncframe carries the JOC EMDF container"
: "the JOC EMDF container is present in part of the examined "
"audio frames (dependent substream or a late first frame)";
} else {
result.joc = JocState::kNo;
result.detail = "at least one examined syncframe has no JOC EMDF container";
result.detail = "no examined syncframe carries a JOC EMDF container";
}
return result;
}
+43 -11
View File
@@ -4,12 +4,14 @@
// because container metadata (MP4 dec3, Matroska A_EAC3) says only "E-AC-3" and
// the JOC flag inside it is frequently missing.
//
// The criterion implemented here is deliberately the same one the rendering core
// applies before it will render a frame: a syncframe carries JOC when it holds
// exactly one top-level EMDF container (syncword 0x5838, any of the eight bit
// alignments) whose payload list contains both ID11 (OAMD) and ID14 (JOC).
// Agreeing with the core matters: a looser test would claim files the core then
// refuses, which is worse than declining them.
// A syncframe carries JOC when it holds exactly one top-level EMDF container
// (syncword 0x5838, any of the eight bit alignments) whose payload list contains
// both ID11 (OAMD) and ID14 (JOC) — the same per-frame test the rendering core
// applies. The stream counts as JOC when at least one examined audio frame
// carries one: in a 7-channel program the JOC payload lives in the dependent
// substream while the independent substream is a plain 5.1 core, and an AC-3
// core may precede it, so "every syncframe carries JOC" is not a valid rule.
// The first audio frame may also carry no metadata at all.
#pragma once
#include <cstddef>
@@ -17,16 +19,26 @@
namespace joc_eac3 {
// E-AC-3 syncframe header fields the plugin needs.
// Syncframe header fields the plugin needs. codec/strmtyp/substreamid identify
// the substream: within one audio frame each combination appears once, which is
// what turns a syncframe stream into audio frames.
struct FrameHeader {
std::size_t frame_bytes = 0;
std::size_t offset = 0;
std::uint8_t codec = 0; // 0 = AC-3, 1 = E-AC-3
std::uint8_t strmtyp = 0;
std::uint8_t substreamid = 0;
bool same_stream(const FrameHeader& other) const {
return codec == other.codec && strmtyp == other.strmtyp &&
substreamid == other.substreamid;
}
};
enum class JocState {
kYes, // every frame examined carried a JOC EMDF container
kNo, // at least one frame examined did not
kUnknown, // not enough data, or the stream is not walkable E-AC-3
kYes, // at least one examined syncframe carried a JOC EMDF container
kNo, // no examined syncframe carried one
kUnknown, // not enough data, or the stream is not walkable ac3/eac3
};
struct ScanResult {
@@ -42,9 +54,29 @@ struct ScanResult {
ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_frames);
// Length of the syncframe starting at data[offset], or 0 when there is no valid
// header there. Mirrors the core's frmsiz handling exactly.
// header there. AC-3 and E-AC-3 are told apart by bsid exactly as the core does:
// bsid <= 8 sizes the frame from fscod/frmsizecod, anything above uses frmsiz.
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset);
// Same, and fills the substream identity. Returns 0 when no header is there.
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out);
// Length of the whole audio frame starting at data[offset]: every syncframe up to
// (and excluding) the first repeated (codec, strmtyp, substreamid) combination.
// A 7-channel program interleaves an independent and a dependent substream, so one
// audio frame is two syncframes and one bed frame.
//
// Returns 0 when the group is still open inside the buffer -- the caller has to ask
// again with more data -- unless `end_of_stream` says no more bytes will arrive, in
// which case the trailing group is complete by definition.
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream);
// Reads joc_dmx_config_idx (the first 3 bits of the JOC payload) out of one
// syncframe. False when the frame carries no JOC payload.
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out);
// True when this single syncframe carries the JOC EMDF container.
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes);
+380 -42
View File
@@ -11,10 +11,14 @@
#include <SDK/audio_chunk.h>
#include <SDK/exception_io.h>
#include <SDK/file_info.h>
#include <SDK/file_info_impl.h>
#include <SDK/input.h>
#include <SDK/input_file_type.h>
#include <SDK/input_impl.h>
#include <SDK/tag_processor.h>
#include <atomic>
#include <cstdarg>
#include <cstring>
#include <string>
@@ -31,6 +35,18 @@ constexpr std::size_t kSniffBytes = 256u * 1024u;
constexpr std::size_t kRunFrames = 4096u;
constexpr unsigned kSampleRate = 48000;
// Largest magnitude in a block, for the delivery check in decode_run().
template <typename Sample>
double peak_of(const Sample* values, std::size_t count) {
double peak = 0.0;
for (std::size_t i = 0; i < count; ++i) {
const double value =
values[i] < Sample(0) ? -static_cast<double>(values[i]) : static_cast<double>(values[i]);
if (value > peak) peak = value;
}
return peak;
}
// Identity in the decoder priority table.
const GUID g_decoder_guid = {0x9c3f1d58, 0x27ab, 0x4e64, {0xb0, 0x93, 0x5e, 0x1c, 0xd7, 0x48, 0x2f, 0xa6}};
@@ -52,13 +68,156 @@ std::string file_name_of(const std::string& path) {
return slash == std::string::npos ? path : path.substr(slash + 1);
}
// A library scan opens every container in the library through open(), and all but
// a few of them are declined because their audio is not E-AC-3 JOC. The per-file
// lines are what makes a scan diagnosable -- "the file that should have been
// claimed was not, and here is why" -- but a large library is hundreds of
// thousands of them, so a running summary is logged as well. It is short enough
// to survive the log rolling over, which is what a scan of that size makes it do.
constexpr unsigned kDeclineSummaryEvery = 1000;
std::atomic<unsigned> g_declined{0};
std::atomic<unsigned> g_claimed{0};
// One place for "this file is not ours". The message stays the caller's, so a
// decline that failed rather than decided still reads as one.
void decline(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
joc_log::line_v(fmt, args);
va_end(args);
const unsigned count = g_declined.fetch_add(1, std::memory_order_relaxed) + 1;
if ((count % kDeclineSummaryEvery) == 0u) {
joc_log::line("scan: %u file(s) declined, %u claimed so far", count,
g_claimed.load(std::memory_order_relaxed));
}
}
// ---------------------------------------------------------------------------
// Tags of a file this component has taken over.
//
// An MP4/M4A keeps its tags in its own metadata box, and the component that knows
// how to read and write them is the container reader the core already ships.
// Claiming a file for decoding must not take it away from that reader, and the SDK
// has no "decode with me, ask someone else for tags" arrangement -- whichever
// entry answers open() answers for everything. So the information read and write
// paths are forwarded to whichever other entry claims the file, and only the tags
// of its answer are merged into ours: the technical information stays this
// component's own, which is what tells a user the file is JOC rather than plain
// E-AC-3.
// ---------------------------------------------------------------------------
// Entries other than this one that claim the path, in the user's own decoding
// order. Ourselves is never in the list: an open forwarded back here would enter
// open() again, for ever.
void forwarding_candidates(const char* url, pfc::list_t<input_entry::ptr>& out) {
out.remove_all();
input_manager_v3::ptr manager;
if (input_manager_v3::tryGet(manager)) {
manager->get_enabled_inputs(out);
} else {
input_entry::g_find_inputs_by_path_ex(out, url,
[](input_entry::ptr) { return true; });
}
const char* dot = std::strrchr(url, '.');
const char* extension = (dot != nullptr) ? dot + 1 : "";
const GUID self = g_decoder_guid;
for (t_size index = out.get_count(); index-- > 0;) {
input_entry::ptr entry = out[index];
if (entry->get_guid_() == self || !entry->is_our_path(url, extension)) {
out.remove_by_idx(index);
}
}
}
// Opens the file again through another entry, for information reading or writing.
// The file is left unopened on our side, so the other entry can have it to itself.
template <typename t_interface>
bool open_forwarded(service_ptr_t<t_interface>& out, const GUID& what_for, const char* url,
abort_callback& abort, pfc::string8* name) {
out.release();
pfc::list_t<input_entry::ptr> candidates;
forwarding_candidates(url, candidates);
if (candidates.get_count() == 0) return false;
try {
GUID used = pfc::guid_null;
service_ptr opened = input_entry::g_open_from_list(candidates, what_for, nullptr, url,
nullptr, abort, &used);
if (!opened.is_valid() || !opened->service_query_t(out)) return false;
if (name != nullptr) {
input_entry::ptr entry = input_entry::g_find_by_guid(used);
*name = entry.is_valid() ? entry->get_name_() : "another component";
}
return true;
} catch (const pfc::exception& error) {
joc_log::line("decoder: no other component answers for this file's tags: %s",
error.what());
return false;
}
}
// Bytes in front of the E-AC-3 stream, which is where a tagging tool puts an
// ID3v2 tag. The renderer refuses a stream that does not begin on a syncword and
// never resynchronises, so the walk and the feed both have to start after it.
t_filesize leading_tag_bytes(file::ptr const& source, abort_callback& abort) {
if (!source.is_valid()) return 0;
try {
if (source->get_position(abort) != 0) source->seek(0, abort);
return tag_processor::skip_id3v2(source, abort);
} catch (const pfc::exception& error) {
joc_log::line("decoder: cannot inspect the area in front of the stream: %s",
error.what());
return 0;
}
}
// Tags read straight from the file, for a bare stream that no other component
// claims: an ID3v2 tag in front of the syncframes, or an APEv2/ID3v1 tag behind
// them. Neither is part of E-AC-3, so a tag that is there was written by a
// tagging tool and is worth showing.
void read_local_tags(file::ptr const& source, file_info& info, abort_callback& abort) {
if (!source.is_valid()) return;
bool found = false;
try {
source->seek(0, abort);
tag_processor::read_id3v2(source, info, abort);
found = true;
} catch (const pfc::exception&) {
// No leading tag; the trailing one is still worth a look.
}
try {
tag_processor::read_trailing(source, info, abort);
found = true;
} catch (const pfc::exception&) {
}
if (found) {
joc_log::line("decoder: %u tag field(s) read from the file itself",
static_cast<unsigned>(info.meta_get_count()));
}
}
class input_joc : public input_stubs {
public:
void open(service_ptr_t<file> hint, const char* path, t_input_open_reason reason,
abort_callback& abort) {
if (reason == input_open_info_write) throw exception_tagging_unsupported();
m_path = (path != nullptr) ? path : "";
if (reason == input_open_info_write) {
// Writing tags belongs to whoever owns the file's format, and that is
// not this component: its inputs are two ffmpeg children and the JOC
// renderer, none of which writes anything. The file is deliberately
// left unopened here, because a write-mode handle of ours would make
// the writer that replaces it fail on a sharing violation.
m_write_only = true;
if (!open_forwarded(m_forward_writer, input_info_writer::class_guid, m_path.c_str(),
abort, &m_forward_name)) {
throw exception_tagging_unsupported();
}
joc_log::line("decoder: open \"%s\" reason=2 tags: written by %s", m_path.c_str(),
m_forward_name.c_str());
return;
}
service_ptr_t<file> source = hint;
input_open_file_helper(source, path, reason, abort);
m_file = source;
@@ -68,8 +227,7 @@ public:
try {
m_native_path = filesystem::g_get_native_path(m_path.c_str());
} catch (const pfc::exception& error) {
joc_log::line("decoder: not a native file path (%s): %s", m_path.c_str(),
error.what());
decline("decoder: not a native file path (%s): %s", m_path.c_str(), error.what());
throw exception_io_unsupported_format();
}
@@ -78,7 +236,31 @@ public:
if (joc_container::is_container_extension(extension)) {
open_container(extension);
return;
} else {
open_bare(abort);
}
// Whatever else happens, the tags of this file are read by the component
// that owns its format; failing to find one is not fatal, the technical
// information below is still worth showing.
if (!open_forwarded(m_forward_reader, input_info_reader::class_guid, m_path.c_str(), abort,
&m_forward_name)) {
joc_log::line("decoder: no other component reads this file's tags");
} else {
joc_log::line("decoder: tags for \"%s\" are read by %s", m_path.c_str(),
m_forward_name.c_str());
}
}
// A bare stream is either read from the file or handed back. One thing has to
// happen first: a tag area in front of the syncframes is not part of the
// stream, and treating it as one would hand the file to the built-in decoder,
// which plays it without the Atmos objects.
void open_bare(abort_callback& abort) {
m_stream_start = leading_tag_bytes(m_file, abort);
if (m_stream_start != 0) {
joc_log::line("decoder: %llu byte(s) of tags in front of the stream are skipped",
static_cast<unsigned long long>(m_stream_start));
}
pfc::array_t<t_uint8> buffer;
@@ -86,18 +268,18 @@ public:
const std::size_t got = m_file->read(buffer.get_ptr(), kSniffBytes, abort);
const joc_eac3::ScanResult scan = joc_eac3::scan(buffer.get_ptr(), got, 8);
joc_log::line("decoder: open \"%s\" reason=%d bytes=%llu frames=%llu with_joc=%llu",
m_path.c_str(), static_cast<int>(reason),
static_cast<unsigned long long>(got),
joc_log::line("decoder: open \"%s\" reason=1 bytes=%llu frames=%llu with_joc=%llu",
m_path.c_str(), static_cast<unsigned long long>(got),
static_cast<unsigned long long>(scan.frames_examined),
static_cast<unsigned long long>(scan.frames_with_joc));
if (scan.joc != joc_eac3::JocState::kYes) {
// Hand the file to the next entry in the priority table.
joc_log::line("decoder: yielding to the built-in decoder (%s)", scan.detail);
decline("decoder: yielding to the built-in decoder (%s)", scan.detail);
throw exception_io_unsupported_format();
}
joc_log::line("decoder: claiming this file as E-AC-3 JOC (bare stream)");
g_claimed.fetch_add(1, std::memory_order_relaxed);
m_input_kind = joc_decode::InputKind::kBare;
}
@@ -108,8 +290,8 @@ public:
void open_container(const char* extension) {
m_container = joc_container::scan(m_native_path.get_ptr());
if (!m_container.eac3) {
joc_log::line("decoder: yielding to the built-in decoder (%s)",
m_container.detail.c_str());
decline("decoder: yielding to the built-in decoder (%s)",
m_container.detail.c_str());
throw exception_io_unsupported_format();
}
@@ -118,27 +300,37 @@ public:
std::string detail;
if (!joc_decode::probe_container_joc(settings.ffmpeg_path, m_native_path.get_ptr(),
m_container.audio_index, &joc, &detail)) {
joc_log::line("decoder: cannot examine the E-AC-3 track (%s); yielding",
detail.c_str());
decline("decoder: cannot examine the E-AC-3 track (%s); yielding", detail.c_str());
throw exception_io_unsupported_format();
}
if (!joc) {
joc_log::line("decoder: yielding to the built-in decoder (E-AC-3 track %u carries "
"no JOC: %s)",
m_container.audio_index, detail.c_str());
decline("decoder: yielding to the built-in decoder (E-AC-3 track %u carries "
"no JOC: %s)",
m_container.audio_index, detail.c_str());
throw exception_io_unsupported_format();
}
joc_log::line("decoder: claiming this file as E-AC-3 JOC (%s, audio track %u, .%s)",
joc_container::kind_name(m_container.kind), m_container.audio_index,
extension);
g_claimed.fetch_add(1, std::memory_order_relaxed);
m_input_kind = joc_decode::InputKind::kContainer;
m_audio_index = m_container.audio_index;
}
void get_info(file_info& info, abort_callback& abort) {
(void)abort;
const joc_decode::FileProbe probe = joc_decode::probe_file(m_native_path.get_ptr());
if (m_write_only) {
// An instance opened to write tags is the writer's reader: what it
// reports is exactly what the caller has just written.
if (m_forward_writer.is_valid()) {
m_forward_writer->get_info(0, info, abort);
return;
}
throw exception_tagging_unsupported();
}
const joc_decode::FileProbe probe =
joc_decode::probe_file(m_native_path.get_ptr(), 0, m_stream_start);
const joc_decode::Settings settings = joc_settings::current();
// A container knows its own duration even though the E-AC-3 syncframes are
@@ -162,15 +354,27 @@ public:
channels = layout_channels != 0 ? layout_channels : 6;
render = "speaker " + settings.speaker_layout;
}
// The custom field and the log line state the delivery format, because it
// is the one thing about the output that a listener cannot hear.
if (settings.force_int24) render += ", 24-bit delivery";
info.info_set("codec", "E-AC-3 JOC");
info.info_set("codec_long", "E-AC-3 JOC (Dolby Atmos)");
info.info_set("encoding", "lossy");
info.info_set_int("samplerate", kSampleRate);
info.info_set_int("channels", channels);
info.info_set_int("bitspersample", 32);
info.info_set("bitspersample_extra", "floating-point");
// What the component hands over, which is what the host keeps if it writes a
// file from this stream: float32 by default, int24 when forced. This is also
// what foobar2000's own lossless-conversion warning reads
// (SDK file_info.cpp:is_encoding_float), so it has to use the SDK's convention:
// 32 bits with "floating-point", or a bit depth that is fixed-point by
// definition (anything but 32/64, with no extra field at all).
info.info_set_bitspersample(settings.force_int24 ? 24u : 32u, !settings.force_int24);
info.set_length(duration);
m_length = duration;
// The renderer keeps its room tail, but the stream this component hands over
// ends where the file ends: the tail is rendering, not playback time.
m_engine.set_length(duration);
if (duration > 0.0) {
const t_filesize bytes = m_file.is_valid() ? m_file->get_size(abort) : filesize_invalid;
if (bytes != filesize_invalid && bytes > 0) {
@@ -194,6 +398,32 @@ public:
m_container.audio_index);
info.info_set("joc_container", text);
}
// The file's own reader supplies the tags; nothing above this line is one.
// Only the metadata is taken over -- its technical information (E-AC-3,
// 6 channels, the stream's own bitrate) would replace this component's,
// which is the part that says whether the file is JOC.
bool have_tags = false;
if (m_forward_reader.is_valid()) {
try {
file_info_impl tags;
m_forward_reader->get_info(0, tags, abort);
info.copy_meta(tags);
have_tags = tags.meta_get_count() != 0;
joc_log::line("decoder: %u tag field(s) from %s",
static_cast<unsigned>(tags.meta_get_count()),
m_forward_name.c_str());
} catch (const pfc::exception& error) {
joc_log::line("decoder: reading this file's own tags failed: %s", error.what());
}
}
// A reader that answers for the format but has nothing to say about a bare
// stream is common -- ffmpeg's AC-3 decoder reads no tags at all -- while
// the file may still carry an ID3v2 or APEv2 tag a tagging tool wrote.
if (!have_tags && m_input_kind == joc_decode::InputKind::kBare) {
read_local_tags(m_file, info, abort);
}
joc_log::line("decoder: get_info duration=%.3f s frames=%llu channels=%u render=%s%s",
duration, static_cast<unsigned long long>(frames), channels, render.c_str(),
container ? " (container)" : "");
@@ -201,6 +431,7 @@ public:
t_filestats2 get_stats2(uint32_t flags, abort_callback& abort) {
if (m_file.is_valid()) return m_file->get_stats2_(flags, abort);
if (m_forward_writer.is_valid()) return m_forward_writer->get_stats2_(nullptr, flags, abort);
throw exception_io_unsupported_format();
}
@@ -209,6 +440,8 @@ public:
m_settings = joc_settings::current();
m_settings.input_kind = m_input_kind;
m_settings.audio_index = m_audio_index;
m_settings.stream_start_bytes = m_stream_start;
m_settings.length_seconds = m_length;
joc_log::line("decoder: initialize flags=0x%X settings: %s", flags,
joc_settings::describe(m_settings).c_str());
@@ -229,8 +462,16 @@ public:
m_buffer.resize(kRunFrames * m_channels);
m_frames_delivered = 0;
m_reported = false;
joc_log::line("decoder: engine ready, %u output channel(s), %u frames per read",
m_channels, static_cast<unsigned>(kRunFrames));
m_delivery_mismatches = 0;
// The clip total belongs to the file rather than to one run of it, so a seek
// does not clear it: the log line at the end of the stream is a statement
// about everything this component handed over.
m_clipped_samples = 0;
m_clip_reported = false;
joc_log::line("decoder: engine ready, %u output channel(s), %u frames per read, "
"delivery=%s",
m_channels, static_cast<unsigned>(kRunFrames),
m_settings.force_int24 ? "int24" : "float32");
}
bool decode_run(audio_chunk& chunk, abort_callback& abort) {
@@ -242,50 +483,130 @@ public:
joc_log::line("decoder: read failed: %s", error.c_str());
throw exception_io_data(error.c_str());
}
joc_log::line("decoder: end of stream after %llu frames",
static_cast<unsigned long long>(m_frames_delivered));
joc_log::line("decoder: end of stream after %llu frames%s",
static_cast<unsigned long long>(m_frames_delivered),
m_delivery_mismatches == 0 ? ""
: " (the delivery changed samples)");
if (m_clipped_samples != 0) {
joc_log::line("decoder: forced int24 truncated %llu sample(s) in total",
static_cast<unsigned long long>(m_clipped_samples));
}
return false;
}
chunk.set_data_size(frames * m_channels);
chunk.set_channels(m_channels, audio_chunk::g_guess_channel_config(m_channels));
chunk.set_sample_rate(kSampleRate);
chunk.set_sample_count(frames);
std::memcpy(chunk.get_data(), m_buffer.data(),
frames * m_channels * sizeof(audio_sample));
// The renderer produces float32 and a chunk holds audio_sample, which is float on
// 32-bit builds and double on 64-bit ones (SDK audio_math.h): the samples are
// converted, not copied. set_data_32() is the SDK's conversion for a float32
// source, and it sets the channel count, the sample rate and the sample count.
//
// A forced int24 delivery goes the other way: the samples are packed to the
// core's int24 bytes and the host converts them back, so the audio the rest of
// the pipeline sees is the 24-bit grid rather than the renderer's float -- which
// is the whole point of the option. The quantisation happens here and nowhere
// else, so a sample is rounded once.
double expected_delivered_peak = 0.0;
if (m_settings.force_int24) {
m_int24.resize(frames * m_channels * 3);
const joc_decode::Int24Block block = joc_decode::pack_int24(
m_buffer.data(), frames * m_channels, m_int24.data());
m_clipped_samples += block.clipped;
if (block.clipped != 0 && !m_clip_reported) {
m_clip_reported = true;
joc_log::line("decoder: forced int24: %llu sample(s) outside [-1,1] in this "
"block are truncated to the edge (the core's clip policy; "
"attenuate with the gain setting to avoid it)",
static_cast<unsigned long long>(block.clipped));
}
chunk.set_data_fixedpoint_signed(m_int24.data(), m_int24.size(), kSampleRate,
m_channels, 24,
audio_chunk::g_guess_channel_config(m_channels));
expected_delivered_peak =
static_cast<double>(block.peak) / joc_decode::kInt24ImportScale;
} else {
chunk.set_data_32(m_buffer.data(), frames, m_channels, kSampleRate);
expected_delivered_peak = peak_of(m_buffer.data(), frames * m_channels);
}
m_frames_delivered += frames;
// A delivery that mangled the samples would be heard as noise rather than reported
// as a failure, so every chunk is checked: the conversion is exact, and the peak of
// what the chunk holds has to equal the peak of what the renderer produced -- or,
// for int24, the peak the packing step actually wrote.
const double delivered =
peak_of(chunk.get_data(), chunk.get_sample_count() * chunk.get_channels());
if (delivered > expected_delivered_peak + 1e-6 + expected_delivered_peak * 1e-6 ||
delivered < expected_delivered_peak - 1e-6 - expected_delivered_peak * 1e-6) {
++m_delivery_mismatches;
if (m_delivery_mismatches == 1) {
joc_log::line("decoder: delivery changed the samples: peak %.9f produced, "
"%.9f delivered",
expected_delivered_peak, delivered);
}
}
if (!m_reported) {
m_reported = true;
float peak = 0.0f;
for (std::size_t i = 0; i < frames * m_channels; ++i) {
const float value = m_buffer[i] < 0.0f ? -m_buffer[i] : m_buffer[i];
if (value > peak) peak = value;
}
joc_log::line("decoder: first %llu frames delivered (%u ch), peak %.6f",
joc_log::line("decoder: first %llu frames delivered (%u ch, %s), peak %.6f, "
"delivered peak %.6f",
static_cast<unsigned long long>(frames), m_channels,
static_cast<double>(peak));
m_settings.force_int24 ? "int24" : "float32",
peak_of(m_buffer.data(), frames * m_channels), delivered);
}
return true;
}
void decode_seek(double, abort_callback&) {
// The renderer is stateful and has no seek; can_seek() says so.
throw exception_io_unsupported_format();
void decode_seek(double seconds, abort_callback& abort) {
// Walking the syncframe index of a long file is the only part of a seek
// that can take a while, and it polls this.
m_engine.set_abort_check([&abort] { return !abort.is_aborting(); });
std::string error;
const bool ok = m_engine.seek(seconds, m_length, &error);
m_engine.set_abort_check(nullptr);
if (!ok) {
// An aborted seek reports itself as an abort, not as a decode failure.
abort.check();
joc_log::line("decoder: seek to %.6f s failed: %s", seconds, error.c_str());
throw exception_io_data(error.c_str());
}
// The position reporting and the first-read statistics belong to the run
// that starts here, not to the one that was interrupted.
m_frames_delivered = 0;
m_reported = false;
m_delivery_mismatches = 0;
joc_log::line("decoder: seek to %.6f s, the next read starts at the target", seconds);
}
bool decode_can_seek() { return false; }
bool decode_can_seek() { return true; }
size_t extended_param(const GUID& type, size_t arg1, void* arg2, size_t arg2size) {
(void)arg1;
(void)arg2;
(void)arg2size;
// A seek restarts both ffmpeg children and replays the renderer's warm-up,
// so it is worth avoiding the ones the core would only make speculatively.
if (type == input_params::seeking_expensive) return 1;
return 0;
}
void retag(const file_info&, abort_callback&) { throw exception_tagging_unsupported(); }
void remove_tags(abort_callback&) { throw exception_tagging_unsupported(); }
void retag(const file_info& info, abort_callback& abort) {
if (!m_forward_writer.is_valid()) throw exception_tagging_unsupported();
// A single-track input has no commit() of its own -- the SDK wrapper
// implements it as a no-op -- so the writer's commit has to happen here or
// nothing reaches the file.
m_forward_writer->set_info(0, info, abort);
m_forward_writer->commit(abort);
joc_log::line("decoder: %u tag field(s) written through %s",
static_cast<unsigned>(info.meta_get_count()), m_forward_name.c_str());
}
void remove_tags(abort_callback& abort) {
if (!m_forward_writer.is_valid()) throw exception_tagging_unsupported();
input_info_writer_v2::ptr v2;
if (m_forward_writer->service_query_t(v2)) {
v2->remove_tags(abort);
return;
}
m_forward_writer->remove_tags_fallback(abort);
}
static bool g_is_our_path(const char* path, const char* extension) {
(void)path;
@@ -331,9 +652,26 @@ private:
unsigned m_audio_index = 0;
joc_container::Result m_container;
std::vector<float> m_buffer;
// int24 delivery: the packed bytes handed to the host, and what the clip caught.
std::vector<std::uint8_t> m_int24;
std::uint64_t m_clipped_samples = 0;
bool m_clip_reported = false;
unsigned m_channels = 2;
std::uint64_t m_frames_delivered = 0;
bool m_reported = false;
// Chunks whose delivered samples did not match what the renderer produced.
std::uint64_t m_delivery_mismatches = 0;
// Duration get_info() last reported; a seek needs it to tell "past the end"
// from "inside the file" without decoding anything.
double m_length = 0.0;
// Bytes in front of a bare stream, which is where an ID3v2 tag sits.
std::uint64_t m_stream_start = 0;
// The other component that answers for this file's tags, and the one that
// writes them. Only the writer exists on an instance opened to retag.
service_ptr_t<input_info_reader> m_forward_reader;
service_ptr_t<input_info_writer> m_forward_writer;
pfc::string8 m_forward_name;
bool m_write_only = false;
};
static input_singletrack_factory_t<input_joc> g_input_joc_factory;
+665 -113
View File
File diff suppressed because it is too large. Load diff
+88 -11
View File
@@ -4,25 +4,41 @@
// drives when the output has to be compared byte for byte against joc_cli.
//
// Two inputs are needed to render, and they come from different places:
// * the bare E-AC-3 syncframes -- the metadata stream -- read straight from the
// file, since the plugin only claims bare .eac3/.ec3 today;
// * the E-AC-3 syncframes -- the metadata stream -- read straight from the file;
// * the 5.1 core PCM of those same frames -- the rendering core does not decode
// the core (it has no AC-3 decoder at all), so ffmpeg produces it exactly the
// way the reference CLI does.
// the core (it has no AC-3 decoder at all), so ffmpeg produces it.
//
// The core is loaded from joc_core.dll through the public C ABI and called via
// function pointers. Static linking is not an option: the core's static library
// carries the internals but not the API layer in src/api, and the plugin is not
// allowed to compile core sources. Loading explicitly also lets the component
// ship its own copy of the DLL and check its ABI version at start-up.
// The core is the C++ kernel under kernel/, compiled into this component and called
// through its public C API via the CoreApi table. No DLL is shipped or loaded: the
// table is filled with the linked entry points and start-up only checks the ABI
// version.
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <string>
namespace joc_decode {
// Delay of the rendering pipeline itself, in output samples: in a run that began
// at source sample 0, output sample k carries the rendering of source sample
// k - this value. A seek starts the inputs this much earlier and drops the
// samples before the target. Measured as 0: the renderer's own filter-bank
// latency and its metadata delay are inside the renderer, not delays of the
// delivered stream. It stays a named, overridable constant so the measurement can
// be repeated.
constexpr std::uint32_t kJocSeekPipelineDelaySamples = 0;
// Syncframes fed before the frame the target sits in. The renderer's state is
// rebuilt from the frames it is given, so a restart needs a moment to converge and
// the delivered part has to be past that. Two things converge at different rates:
// the metadata state (object positions, matrix interpolation, gain ramps), which
// the measured 3008-sample window covers, and the binaural room tail, which is
// recursive and can only be approached -- two syncframes are enough for the speaker
// path, and the tail keeps improving with more, which is why this is 64.
constexpr std::uint32_t kJocSeekPrerollFrames = 64;
enum class Output {
kBinaural = 0, // 2 channels, HRTF rendering
kSpeaker = 1, // N channels, named layout
@@ -57,13 +73,35 @@ struct Settings {
std::uint32_t binaural_mode = 3; // JOC_BINAURAL_MID
double gain_db = 0.0;
double tail_seconds = 5.0;
// Duration of the file, in seconds. The renderer ends a binaural stream with a
// room tail, which would be played as time the file does not have; the delivered
// stream is cut at exactly this much audio instead. Zero means "no limit".
double length_seconds = 0.0;
std::uint32_t object_delay_samples = 1473;
std::uint32_t native_threads = 0;
std::string ffmpeg_path = "ffmpeg";
// Bytes in front of the first syncframe -- a leading ID3v2 tag. The renderer
// rejects a stream that does not begin on a syncword, so both the walk and the
// feed start here.
std::uint64_t stream_start_bytes = 0;
// Samples of pipeline delay a seek compensates for, and syncframes it pre-rolls
// before the target: the calibrated constants above, unless the comparison
// harness overrides them to measure those constants.
std::uint32_t pipeline_delay_samples = kJocSeekPipelineDelaySamples;
std::uint32_t seek_preroll_frames = kJocSeekPrerollFrames;
// Stop feeding the renderer after this many input syncframes and flush, which
// is what the reference CLI's --duration does. Zero means "the whole file".
// Only the comparison harness sets it; playback leaves it at zero.
std::uint64_t input_frame_limit = 0;
// Deliver 24-bit fixed-point PCM instead of float32. The renderer itself is
// always float32 and this does not change it: the flag says how the *caller*
// hands the samples on, and the quantisation happens once, where the format is
// decided (see pack_int24 below). It exists because the streaming interface
// has no int24 output -- the core's int24 rule lives in its file-writing path
// (kernel/src/io/wav_writer.cpp), which the component cannot reach, so the
// rule is reproduced here instead. Out-of-range samples follow the core's
// JOC_CLIP_CONTINUE policy: truncated to the edge, and reported.
bool force_int24 = false;
};
// Channel count of a named layout, or 0 when the layout is not one of the
@@ -94,8 +132,10 @@ struct FileProbe {
};
// Walks the file's syncframes. Cheap enough for a Media Library scan: it only
// does pointer arithmetic over the stream, no decoding, no HRTF work.
FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes = 0);
// does pointer arithmetic over the stream, no decoding, no HRTF work. The walk
// starts at `start_offset`, which is where the stream begins behind a tag area.
FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes = 0,
std::uint64_t start_offset = 0);
// Decides whether the E-AC-3 track of a container file carries JOC, without
// rendering anything: ffmpeg copies a short prefix of that track out (stream copy,
@@ -105,6 +145,28 @@ FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes = 0);
bool probe_container_joc(const std::string& ffmpeg_path, const std::string& path,
unsigned audio_index, bool* joc, std::string* detail);
// ---- forced int24 delivery ------------------------------------------------
//
// The core's own int24 rule, reproduced byte for byte from
// kernel/src/io/wav_writer.cpp: clip to [-1, 1], multiply by 8388607, truncate
// toward zero (the core's comment: the int32 conversion is "identical to NumPy's
// float32 -> int32 cast after clipping"), write the low three bytes, little
// endian; a non-finite sample becomes 0, exactly as the core writes it.
//
// Sample values that have been through this are what foobar2000's int24 import
// gives back, i.e. n / 2^23 (SDK/pfc audio_math.cpp:_import24). That is the scale
// a caller should use when it wants to check what the host received.
constexpr double kInt24ImportScale = 8388608.0;
struct Int24Block {
std::size_t clipped = 0; // samples that were outside [-1, 1]
std::int32_t peak = 0; // largest magnitude actually written
};
// Packs `count` interleaved float32 samples into `out`, which must hold
// count * 3 bytes, and reports what the clip caught.
Int24Block pack_int24(const float* interleaved, std::size_t count, std::uint8_t* out);
class Engine {
public:
Engine();
@@ -118,6 +180,21 @@ public:
unsigned channels() const;
void stop();
// Duration of the file, in seconds, for a caller that learns it after start()
// (reported duration of the track); zero means "no limit".
void set_length(double seconds);
// Repositions so that the next sample read() returns is the rendering of source
// sample round(seconds * 48000): the inputs restart at the syncframe holding
// that sample and the output before it is dropped. Seeking at or past
// `total_seconds` (0 when the duration is unknown) succeeds and makes the next
// read() return 0, as the decoder contract requires. start() must have run.
bool seek(double seconds, double total_seconds, std::string* error);
// Polled while the syncframe index is being walked, so a long seek stays
// interruptible; returning false aborts the seek.
void set_abort_check(std::function<bool()> check);
private:
struct Impl;
Impl* impl_;
+90 -36
View File
@@ -8,6 +8,8 @@
#include <mutex>
#include <string>
#include "win_path.h"
namespace joc_log {
namespace {
@@ -16,12 +18,20 @@ FILE* g_file = nullptr;
bool g_opened = false;
std::string g_path; // UTF-8, for reporting
std::wstring g_wide_path; // for _wfopen
unsigned long long g_lines = 0;
bool g_capped = false;
std::wstring g_wide_rolled; // g_wide_path with the .1 suffix
unsigned long long g_bytes = 0;
// A Media Library scan calls into us for every file; the cap keeps a long
// session from filling the disk, and says so once when it is reached.
constexpr unsigned long long kMaxLines = 500000;
// A Media Library scan calls into us for every file it walks, so the cap has to
// be on bytes: the lines a scan produces run from 40 to 2048 bytes each, and a
// cap on their number leaves the file they build unpredictable by a factor of
// fifty. Sixteen mebibytes per file, one previous file kept, so the component
// never holds more than 32 MiB of log.
constexpr unsigned long long kMaxBytes = 16ull << 20;
// The lines recorded between header_begin() and header_end(): the version
// banner, a handful of lines written once per process.
bool g_recording = false;
std::string g_header;
std::wstring module_directory() {
HMODULE module = nullptr;
@@ -39,32 +49,26 @@ std::wstring module_directory() {
return dir;
}
std::string to_utf8(const std::wstring& text) {
if (text.empty()) return {};
const int needed = WideCharToMultiByte(CP_UTF8, 0, text.c_str(),
static_cast<int>(text.size()), nullptr, 0,
nullptr, nullptr);
std::string out(static_cast<std::string::size_type>(needed), '\0');
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()),
out.data(), needed, nullptr, nullptr);
return out;
}
void open_locked() {
g_opened = true;
wchar_t from_env[4096] = {};
const DWORD env_length = GetEnvironmentVariableW(L"JOC_LOG", from_env,
static_cast<DWORD>(4096));
std::wstring plain;
if (env_length > 0 && env_length < 4096) {
g_wide_path.assign(from_env, env_length);
plain.assign(from_env, env_length);
} else {
const std::wstring dir = module_directory();
if (dir.empty()) return;
g_wide_path = dir + L"\\joc_decoder.log";
plain = dir + L"\\joc_decoder.log";
}
g_path = to_utf8(g_wide_path);
// g_path is the one the component reports and a user reads; the handles work on
// the extended form, because a portable install can sit deeper than MAX_PATH.
g_path = joc_path::to_utf8(plain);
g_wide_path = joc_path::to_wide_extended(plain);
g_wide_rolled = g_wide_path + L".1";
g_file = _wfopen(g_wide_path.c_str(), L"wb");
if (g_file == nullptr) {
g_path.clear();
@@ -74,6 +78,52 @@ void open_locked() {
std::setvbuf(g_file, nullptr, _IONBF, 0);
}
// Composes one line with its timestamp, writes it, and accounts for it. Returns
// the bytes written, 0 when there was nowhere to write them.
unsigned long long write_locked(const char* text) {
if (g_file == nullptr) return 0;
SYSTEMTIME now;
GetLocalTime(&now);
char composed[2100];
const int length =
std::snprintf(composed, sizeof(composed), "%02u:%02u:%02u.%03u [t%05lu] %s\n", now.wHour,
now.wMinute, now.wSecond, now.wMilliseconds,
static_cast<unsigned long>(GetCurrentThreadId()), text);
if (length <= 0) return 0;
// snprintf reports the length it would have needed, which for a truncated
// line is past the end of the buffer.
const std::size_t size = (static_cast<std::size_t>(length) < sizeof(composed))
? static_cast<std::size_t>(length)
: sizeof(composed) - 1;
std::fwrite(composed, 1, size, g_file);
g_bytes += size;
if (g_recording) g_header.append(composed, size);
return size;
}
// Moves the full file aside and starts an empty one. Called with the mutex held,
// once the budget is spent. The banner is written again afterwards, so a log
// that has rolled still opens with the versions it was produced by.
void roll_locked() {
std::fclose(g_file);
g_file = nullptr;
// One previous window, replaced rather than chained: a chain of rolls would
// be the same unbounded log under another name. A move that fails -- the .1
// file held open by a reader -- leaves the truncation below to bound the file
// anyway, at the cost of the window that would have been kept.
MoveFileExW(g_wide_path.c_str(), g_wide_rolled.c_str(), MOVEFILE_REPLACE_EXISTING);
g_file = _wfopen(g_wide_path.c_str(), L"wb");
if (g_file == nullptr) return;
std::setvbuf(g_file, nullptr, _IONBF, 0);
g_bytes = 0;
write_locked("[log rolled: the previous window is in the .1 file beside this one]");
if (!g_header.empty()) {
std::fwrite(g_header.c_str(), 1, g_header.size(), g_file);
g_bytes += g_header.size();
}
}
} // namespace
void open() {
@@ -86,30 +136,34 @@ const char* path() {
return g_path.c_str();
}
void line(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
unsigned long long budget_bytes() { return kMaxBytes; }
void line_v(const char* fmt, va_list args) {
char text[2048];
std::vsnprintf(text, sizeof(text), fmt, args);
va_end(args);
std::lock_guard<std::mutex> guard(g_mutex);
if (!g_opened) open_locked();
if (g_file == nullptr) return;
if (g_lines >= kMaxLines) {
if (!g_capped) {
g_capped = true;
std::fputs("[log capped]\n", g_file);
}
return;
}
++g_lines;
if (write_locked(text) != 0 && g_bytes >= kMaxBytes) roll_locked();
}
SYSTEMTIME now;
GetLocalTime(&now);
std::fprintf(g_file, "%02u:%02u:%02u.%03u [t%05lu] %s\n", now.wHour, now.wMinute,
now.wSecond, now.wMilliseconds,
static_cast<unsigned long>(GetCurrentThreadId()), text);
void line(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
line_v(fmt, args);
va_end(args);
}
void header_begin() {
std::lock_guard<std::mutex> guard(g_mutex);
g_header.clear();
g_recording = true;
}
void header_end() {
std::lock_guard<std::mutex> guard(g_mutex);
g_recording = false;
}
} // namespace joc_log
+25 -2
View File
@@ -4,10 +4,22 @@
// harness and so that it is safe to call from any thread the core queries us on.
//
// Path: %JOC_LOG% if set, otherwise <directory of this DLL>\joc_decoder.log.
// The file is truncated once per process, line-buffered, and every line is
// flushed so the log can be read while foobar2000 is still running.
// The file is truncated once per process, unbuffered, and every line is written
// straight out so the log can be read while foobar2000 is still running.
//
// Size: what is bounded is bytes, not lines. A line is anywhere between 40 and
// 2048 bytes, so a cap on the number of lines leaves the size of the file
// unpredictable by a factor of fifty. A file that reaches budget_bytes() is
// moved to <path>.1 -- replacing any previous roll, so there is never a chain of
// them -- and a fresh one is started. Two files of the budget is therefore the
// most the component ever holds, and the newest lines are always the ones in
// <path>. Lines written between header_begin() and header_end() are written
// again at the top of every window, so a rolled log still says which build
// produced it.
#pragma once
#include <cstdarg>
namespace joc_log {
// Opens (truncating) the log. Called lazily by line(); call it explicitly to
@@ -17,7 +29,18 @@ void open();
// Absolute path of the log file, or "" when it could not be opened.
const char* path();
// Bytes one file holds before it is rolled to <path>.1.
unsigned long long budget_bytes();
// printf-style, thread safe, appends one line.
void line(const char* fmt, ...);
// The va_list form, for a wrapper that forwards its own arguments.
void line_v(const char* fmt, va_list args);
// Remembers the lines written in between and writes them again after every roll.
// Meant for the version banner: a handful of lines, written once.
void header_begin();
void header_end();
} // namespace joc_log
+7 -1
View File
@@ -12,7 +12,7 @@
// Kept in one place: the string reported to foobar2000 and written to the log
// must not drift apart.
#define JOC_VERSION "0.1.0"
#define JOC_VERSION "0.4.0"
DECLARE_COMPONENT_VERSION("JOC decoder (E-AC-3 JOC)", JOC_VERSION,
"Plays E-AC-3 JOC (Dolby Atmos) files: the JOC objects are "
@@ -30,6 +30,9 @@ class joc_initquit : public initquit {
public:
void on_init() override {
joc_log::open();
// Recorded, so that a log which has rolled still opens with the versions
// it was produced by.
joc_log::header_begin();
joc_log::line("=== foo_input_joc %s ===", JOC_VERSION);
joc_log::line("foobar2000 core : %s", core_version_info::g_get_version_string());
joc_log::line("component file : %s", core_api::get_my_file_name());
@@ -37,6 +40,8 @@ public:
joc_log::line("portable mode : %s",
core_api::is_portable_mode_enabled() ? "yes" : "no");
joc_log::line("log file : %s", joc_log::path());
joc_log::line("log limit : %llu byte(s) per file, then rolled to the .1 suffix",
joc_log::budget_bytes());
joc_log::line("compiled as : %s",
#if defined(_M_IX86)
"x86 (32-bit)"
@@ -46,6 +51,7 @@ public:
"other"
#endif
);
joc_log::header_end();
}
void on_quit() override { joc_log::line("=== foo_input_joc shutdown ==="); }
+43 -20
View File
@@ -189,6 +189,7 @@ private:
}
if (pick_file(m_hwnd, path, title, filter)) {
set_text(GetDlgItem(m_hwnd, IDC_EDIT_HRTF), path);
update_status();
notify();
}
return TRUE;
@@ -213,12 +214,16 @@ private:
notify();
return TRUE;
}
if (id == IDC_CHECK_GAIN) {
if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24) {
update_enabled_state();
notify();
return TRUE;
}
if (code == EN_CHANGE || code == CBN_SELCHANGE) {
if (id == IDC_EDIT_HRTF || id == IDC_COMBO_LAYOUT ||
id == IDC_EDIT_GAIN) {
update_status();
}
notify();
return TRUE;
}
@@ -296,6 +301,8 @@ private:
SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_GAIN), BM_SETCHECK,
values.gain_enabled ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_INT24), BM_SETCHECK,
values.force_int24 ? BST_CHECKED : BST_UNCHECKED, 0);
update_enabled_state();
}
@@ -323,6 +330,8 @@ private:
values.gain_db = std::atof(get_text(GetDlgItem(m_hwnd, IDC_EDIT_GAIN)).c_str());
values.gain_enabled =
IsDlgButtonChecked(m_hwnd, IDC_CHECK_GAIN) == BST_CHECKED;
values.force_int24 =
IsDlgButtonChecked(m_hwnd, IDC_CHECK_INT24) == BST_CHECKED;
// Advanced values are not on this page; keep whatever is stored.
const joc_settings::Values stored = joc_settings::read();
values.object_delay_samples = stored.object_delay_samples;
@@ -338,35 +347,48 @@ private:
void update_enabled_state() { update_status(); }
void update_status() {
const joc_settings::Values values = joc_settings::read();
std::string status;
const joc_settings::Values values = read_controls();
std::string output_status;
if (values.output != 0) {
char text[96] = {};
const unsigned channels = joc_decode::speaker_channels(values.speaker_layout);
std::snprintf(text, sizeof(text), "扬声器布局 %s(%u 声道)",
values.speaker_layout.c_str(), channels);
status = text;
} else {
// Show what will actually be read, so an empty box is not a mystery.
joc_decode::Settings effective = joc_settings::current();
effective.hrtf_source =
static_cast<joc_decode::HrtfSource>(values.hrtf_source);
effective.hrtf_file = values.hrtf_file;
const std::string file = joc_decode::resolve_hrtf_file(effective);
status = std::string(joc_settings::hrtf_source_name(values.hrtf_source)) + ":" +
(file.empty() ? std::string("无法确定默认路径")
: (values.hrtf_file.empty() ? "默认 " + file : file));
output_status = text;
}
// Say what is actually in effect, including the parts that do not apply
// to the selected output mode, so nothing has to be greyed out.
set_text(GetDlgItem(m_hwnd, IDC_LABEL_OUTPUT_STATUS), output_status);
// The hint says what the switch does and what the other state means: the
// renderer is float32 either way, so "24bit" is about the delivery, and
// that is not something a checkbox label can carry on its own. The second
// half matters because this switch changes what the component *reports*
// about a file, and foobar2000 caches that per file: an item whose
// information was read before the switch was flipped keeps the old format
// until it is reloaded.
set_text(GetDlgItem(m_hwnd, IDC_LABEL_INT24_HINT),
values.force_int24
? "开启:量化到 int24,超出 ±1 截断;切换后需刷新文件信息"
: "关闭:按 32 位浮点交付(默认)");
joc_decode::Settings effective = joc_settings::current();
effective.hrtf_source =
static_cast<joc_decode::HrtfSource>(values.hrtf_source);
effective.hrtf_file = values.hrtf_file;
const std::string file = joc_decode::resolve_hrtf_file(effective);
std::string hrtf_status =
std::string(joc_settings::hrtf_source_name(values.hrtf_source)) + ":";
if (values.hrtf_file.empty()) hrtf_status += "默认";
hrtf_status += "\n" +
(file.empty() ? std::string("无法确定默认路径") : file);
set_text(GetDlgItem(m_hwnd, IDC_LABEL_STATUS), hrtf_status);
char gain[96] = {};
if (values.gain_enabled) {
std::snprintf(gain, sizeof(gain), "\n增益开 %.2f dB", values.gain_db);
std::snprintf(gain, sizeof(gain), "增益开 %.2f dB", values.gain_db);
} else {
std::snprintf(gain, sizeof(gain), "\n增益关(输出不衰减)");
std::snprintf(gain, sizeof(gain), "增益关(输出不衰减)");
}
status += gain;
set_text(GetDlgItem(m_hwnd, IDC_LABEL_STATUS), status);
set_text(GetDlgItem(m_hwnd, IDC_LABEL_GAIN_STATUS), gain);
}
bool changed() const {
@@ -377,6 +399,7 @@ private:
if (now.hrtf_file != m_initial.hrtf_file) return true;
if (now.binaural_mode != m_initial.binaural_mode) return true;
if (now.gain_enabled != m_initial.gain_enabled) return true;
if (now.force_int24 != m_initial.force_int24) return true;
if (now.gain_db != m_initial.gain_db) return true;
if (now.tail_seconds != m_initial.tail_seconds) return true;
if (now.ffmpeg_path != m_initial.ffmpeg_path) return true;
+26 -21
View File
@@ -7,11 +7,11 @@
// sample page uses (SDK\foobar2000\foo_sample\foo_sample.rc). A CAPTION statement
// implies WS_CAPTION, which would draw a second frame inside the host's and shift
// every control down by the caption height -- visible but not clickable.
IDD_JOC_PREFS DIALOGEX 0, 0, 330, 226
IDD_JOC_PREFS DIALOGEX 0, 0, 330, 299
STYLE DS_SETFONT | WS_CHILD
FONT 8, "Microsoft Sans Serif", 400, 0, 0x0
BEGIN
GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 64
GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 86
CONTROL "双耳(HRTF)", IDC_RADIO_BINAURAL, "Button",
BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 20, 72, 10
CONTROL "扬声器布局", IDC_RADIO_SPEAKER, "Button",
@@ -22,31 +22,36 @@ BEGIN
LTEXT "双耳模式", IDC_LABEL_MODE, 172, 22, 42, 10
COMBOBOX IDC_COMBO_MODE, 216, 19, 96, 90,
CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
LTEXT "房间尾音(秒)", IDC_LABEL_TAIL, 172, 41, 46, 10
EDITTEXT IDC_EDIT_TAIL, 220, 38, 40, 13, ES_AUTOHSCROLL
LTEXT "房间尾音(秒)", IDC_LABEL_TAIL, 172, 41, 62, 10
EDITTEXT IDC_EDIT_TAIL, 238, 38, 40, 13, ES_AUTOHSCROLL
LTEXT "", IDC_LABEL_OUTPUT_STATUS, 16, 52, 300, 10
CONTROL "强制 24bit 输出(int24)", IDC_CHECK_INT24, "Button",
BS_AUTOCHECKBOX | WS_TABSTOP, 16, 64, 116, 10
LTEXT "", IDC_LABEL_INT24_HINT, 16, 76, 300, 10
GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 74, 316, 84
GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 97, 316, 100
CONTROL "SOFA 文件", IDC_RADIO_HRTF_SOFA, "Button",
BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 89, 56, 10
BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 112, 56, 10
CONTROL "Rosella 个性化模型", IDC_RADIO_HRTF_ROSELLA, "Button",
BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 89, 78, 10
LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 106, 92, 10
EDITTEXT IDC_EDIT_HRTF, 110, 103, 164, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 103, 38, 13
BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 112, 78, 10
LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 131, 106, 10
EDITTEXT IDC_EDIT_HRTF, 126, 128, 148, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 128, 38, 13
LTEXT "留空时读组件目录下 HRTF\\binaural.sofa 或 binaural.personalized_headphone;SOFA 由渲染器内部编译,滤波组表已编入库中。",
IDC_LABEL_HRTF_HINT, 16, 122, 300, 30
IDC_LABEL_HRTF_HINT, 16, 147, 300, 20
LTEXT "", IDC_LABEL_STATUS, 16, 171, 300, 20
GROUPBOX "增益", IDC_GROUP_GAIN, 7, 162, 152, 56
GROUPBOX "增益", IDC_GROUP_GAIN, 7, 202, 316, 48
CONTROL "应用增益", IDC_CHECK_GAIN, "Button",
BS_AUTOCHECKBOX | WS_TABSTOP, 16, 178, 58, 10
LTEXT "dB", IDC_LABEL_GAIN, 80, 178, 12, 10
EDITTEXT IDC_EDIT_GAIN, 96, 175, 40, 13, ES_AUTOHSCROLL
BS_AUTOCHECKBOX | WS_TABSTOP, 16, 218, 58, 10
LTEXT "dB", IDC_LABEL_GAIN, 80, 218, 12, 10
EDITTEXT IDC_EDIT_GAIN, 96, 215, 40, 13, ES_AUTOHSCROLL
LTEXT "", IDC_LABEL_GAIN_STATUS, 145, 218, 171, 10
LTEXT "双耳渲染后可能超过 0 dBFS,可用负值衰减。",
IDC_LABEL_GAIN_HINT, 16, 194, 134, 20
IDC_LABEL_GAIN_HINT, 16, 234, 300, 10
GROUPBOX "其它", IDC_GROUP_MISC, 165, 162, 158, 56
LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 173, 178, 44, 10
EDITTEXT IDC_EDIT_FFMPEG, 216, 175, 62, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 282, 175, 35, 13
LTEXT "", IDC_LABEL_STATUS, 173, 194, 144, 20
GROUPBOX "其它", IDC_GROUP_MISC, 7, 255, 316, 38
LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 16, 271, 44, 10
EDITTEXT IDC_EDIT_FFMPEG, 64, 268, 210, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 279, 268, 38, 13
END
+5
View File
@@ -12,6 +12,10 @@
#define IDC_COMBO_MODE 2016
#define IDC_LABEL_TAIL 2017
#define IDC_EDIT_TAIL 2018
#define IDC_LABEL_OUTPUT_STATUS 2019
// Delivery format, in the same group: what the decoder hands to foobar2000.
#define IDC_CHECK_INT24 2050
#define IDC_LABEL_INT24_HINT 2051
#define IDC_GROUP_HRTF 2020
#define IDC_RADIO_HRTF_SOFA 2021
@@ -26,6 +30,7 @@
#define IDC_LABEL_GAIN 2032
#define IDC_EDIT_GAIN 2033
#define IDC_LABEL_GAIN_HINT 2034
#define IDC_LABEL_GAIN_STATUS 2035
#define IDC_GROUP_MISC 2040
#define IDC_LABEL_FFMPEG 2041
+23
View File
@@ -6,6 +6,7 @@
#include <windows.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -27,6 +28,7 @@ constexpr GUID kGuidBinauralMode = {0x1a57c9e4, 0x83b6, 0x42f1, {0x9c, 0x50, 0x2
constexpr GUID kGuidGainEnabled = {0x74b1e5c8, 0x2a39, 0x4d16, {0x8f, 0x62, 0x0d, 0x51, 0xb7, 0x3c, 0x9a, 0x28}};
constexpr GUID kGuidGain = {0x6c04d8b2, 0xe75f, 0x4a39, {0x8d, 0x26, 0x51, 0xb3, 0xc8, 0x40, 0x9e, 0x27}};
constexpr GUID kGuidTail = {0x38f1b6a7, 0x0d24, 0x4e88, {0xb7, 0x95, 0x2c, 0x61, 0x4f, 0xd2, 0x08, 0xa3}};
constexpr GUID kGuidForceInt24 = {0xd2c67f41, 0x8b53, 0x4a2e, {0x97, 0x61, 0x0f, 0xc8, 0x35, 0xba, 0x64, 0x12}};
constexpr GUID kGuidObjectDelay = {0x5b2e9c18, 0xa640, 0x4d7b, {0x92, 0x0e, 0x37, 0xc5, 0x81, 0x6b, 0x2a, 0x54}};
constexpr GUID kGuidThreads = {0xcb7a4015, 0x3f98, 0x4c62, {0xa0, 0x8d, 0x1b, 0x74, 0x29, 0xe6, 0x53, 0x0f}};
constexpr GUID kGuidFfmpeg = {0x0e9d37c2, 0x5a18, 0x4b40, {0xb6, 0x3f, 0x88, 0x2d, 0x9a, 0x70, 0x1c, 0x65}};
@@ -42,10 +44,23 @@ cfg_bool g_gain_enabled(kGuidGainEnabled, true);
cfg_int g_gain_mdb(kGuidGain, 0);
// milli-seconds
cfg_uint g_tail_ms(kGuidTail, 5000);
// Deliver int24 instead of float32.
cfg_bool g_force_int24(kGuidForceInt24, false);
cfg_uint g_object_delay(kGuidObjectDelay, 1473);
cfg_uint g_threads(kGuidThreads, 0);
cfg_string g_ffmpeg(kGuidFfmpeg, "ffmpeg");
// Appends an optional trailing field to one of the fixed describe() buffers, so a
// setting that is off leaves the line exactly as it was.
template <std::size_t N>
void append_to(char (&buffer)[N], const char* text) {
if (text == nullptr || *text == '\0') return;
const std::size_t used = std::strlen(buffer);
const std::size_t extra = std::strlen(text);
if (used + extra + 1u > N) return;
std::memcpy(buffer + used, text, extra + 1u);
}
std::string environment(const char* name) {
char buffer[4096] = {};
const DWORD length = GetEnvironmentVariableA(name, buffer, sizeof(buffer));
@@ -108,6 +123,7 @@ Values read() {
values.gain_enabled = static_cast<bool>(g_gain_enabled);
values.gain_db = static_cast<double>(static_cast<long long>(g_gain_mdb)) / 1000.0;
values.tail_seconds = static_cast<double>(static_cast<unsigned>(g_tail_ms)) / 1000.0;
values.force_int24 = static_cast<bool>(g_force_int24);
values.object_delay_samples = static_cast<unsigned>(g_object_delay);
values.native_threads = static_cast<unsigned>(g_threads);
values.ffmpeg_path = g_ffmpeg.get_ptr();
@@ -124,6 +140,7 @@ void write(const Values& values) {
g_gain_enabled = values.gain_enabled;
g_gain_mdb = static_cast<long long>(values.gain_db * 1000.0 + (values.gain_db >= 0 ? 0.5 : -0.5));
g_tail_ms = static_cast<unsigned>(values.tail_seconds * 1000.0 + 0.5);
g_force_int24 = values.force_int24;
g_object_delay = values.object_delay_samples;
g_threads = values.native_threads;
g_ffmpeg = values.ffmpeg_path.c_str();
@@ -146,6 +163,7 @@ joc_decode::Settings current() {
(void)env_double("JOC_GAIN_DB", &values.gain_db);
if (env_string("JOC_GAIN_ENABLED", &text)) values.gain_enabled = (text != "0");
(void)env_double("JOC_TAIL_SECONDS", &values.tail_seconds);
if (env_string("JOC_FORCE_INT24", &text)) values.force_int24 = (text != "0");
(void)env_uint("JOC_OBJECT_DELAY", &values.object_delay_samples);
(void)env_uint("JOC_THREADS", &values.native_threads);
@@ -161,6 +179,7 @@ joc_decode::Settings current() {
// attenuation has to be visible and switchable.
settings.gain_db = values.gain_enabled ? values.gain_db : 0.0;
settings.tail_seconds = values.tail_seconds;
settings.force_int24 = values.force_int24;
settings.object_delay_samples = values.object_delay_samples;
settings.native_threads = values.native_threads;
settings.ffmpeg_path = values.ffmpeg_path.empty() ? "ffmpeg" : values.ffmpeg_path;
@@ -187,6 +206,9 @@ std::string describe(const joc_decode::Settings& settings) {
settings.object_delay_samples, settings.native_threads,
settings.ffmpeg_path.c_str());
}
// Stated only when it is not the default, so a default log line reads as it
// always did.
append_to(text, settings.force_int24 ? ", int24=forced" : "");
return text;
}
@@ -200,6 +222,7 @@ std::string describe(const Values& values) {
values.binaural_mode, values.gain_enabled ? "" : "(off) ", values.gain_db,
values.tail_seconds, values.object_delay_samples, values.native_threads,
values.ffmpeg_path.c_str());
append_to(text, values.force_int24 ? " int24=forced" : "");
return text;
}
+2
View File
@@ -40,6 +40,8 @@ struct Values {
bool gain_enabled = true;
double gain_db = 0.0;
double tail_seconds = 5.0;
// Deliver int24 instead of float32; see joc_decode::Settings::force_int24.
bool force_int24 = false;
unsigned object_delay_samples = 1473;
unsigned native_threads = 0;
std::string ffmpeg_path = "ffmpeg";
+90
View File
@@ -0,0 +1,90 @@
// Paths handed to the Win32 file APIs.
//
// Windows refuses a path of MAX_PATH characters or more to any call that does not
// carry the \\?\ prefix. The prefix is a property of the path, not of the process:
// a component cannot inherit it from the host's manifest, so whoever calls the API
// has to add it. A file that plays from the desktop therefore stops being found
// once its folder tree is deep enough, which is what this header exists to prevent.
//
// Everything in this component that hands a path to Win32 -- or puts one on an
// ffmpeg command line, which is the same call made by the child process -- goes
// through to_wide_extended(). What is deliberately not routed through it is the
// path the component keeps for itself: the one it logs, and the one it uses as the
// container-probe cache key. Those stay in their plain form, so a log line reads
// like a path a user can paste into Explorer.
#pragma once
#include <windows.h>
#include <string>
namespace joc_path {
// UTF-8 -> UTF-16, for the W APIs. Every path crosses into the OS through here:
// the narrow CRT would convert it through the ANSI code page, which is what breaks
// names like "Les Fêtes d'Hébé".
inline std::wstring to_wide(const std::string& utf8) {
if (utf8.empty()) return {};
const int needed = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(),
static_cast<int>(utf8.size()), nullptr, 0);
if (needed <= 0) return {};
std::wstring out(static_cast<std::size_t>(needed), L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.size()), out.data(),
needed);
return out;
}
// The same path in the form the Win32 file APIs need. Three kinds of path are left
// exactly as they came in, and each of them would be broken by a prefix:
//
// * one that already carries \\?\, which makes this idempotent;
// * one that is not fully qualified -- a relative path, or the ffmpeg setting
// when it is a bare executable name that PATH resolves. \\?\ is undefined for
// those;
// * one with a "." or ".." segment, because \\?\ switches off the normalization
// that would resolve it.
//
// The separators are settled first for the same reason: once the prefix is on,
// nothing turns a forward slash into a backslash any more, and both of the checks
// above have to see the path the way the file system will.
inline std::wstring to_wide_extended(const std::wstring& path) {
if (path.empty()) return path;
if (path.compare(0, 4, L"\\\\?\\") == 0) return path;
std::wstring plain = path;
for (wchar_t& character : plain) {
if (character == L'/') character = L'\\';
}
const bool unc = plain.size() >= 2 && plain[0] == L'\\' && plain[1] == L'\\';
const bool drive = plain.size() >= 3 && plain[1] == L':' && plain[2] == L'\\';
if (!unc && !drive) return path;
if (plain.find(L"\\.\\") != std::wstring::npos ||
plain.find(L"\\..\\") != std::wstring::npos) {
return path;
}
// A UNC path drops its two leading separators; everything else is copied whole.
std::wstring out = unc ? L"\\\\?\\UNC\\" : L"\\\\?\\";
out.append(plain, unc ? 2 : 0, std::wstring::npos);
return out;
}
inline std::wstring to_wide_extended(const std::string& utf8) {
return to_wide_extended(to_wide(utf8));
}
// UTF-16 -> UTF-8, the direction the component reports paths in.
inline std::string to_utf8(const std::wstring& wide) {
if (wide.empty()) return {};
const int needed = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(),
static_cast<int>(wide.size()), nullptr, 0, nullptr,
nullptr);
if (needed <= 0) return {};
std::string out(static_cast<std::string::size_type>(needed), '\0');
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()), out.data(),
needed, nullptr, nullptr);
return out;
}
} // namespace joc_path
+111
View File
@@ -0,0 +1,111 @@
// Drives the diagnostic log past its byte budget, without foobar2000.
//
// log_rotation_test
//
// The component's log is bounded by rolling: a file that reaches the budget is
// moved to a .1 sibling and a fresh one is started. What pushes it there is a
// Media Library scan, which a test cannot arrange, so the budget is spent here
// with lines of a known size instead. What is checked is what the bound
// promises: neither file passes the budget, there is no chain of rolls, the
// newest lines are the ones in the live file, and the session banner is written
// again after a roll -- which is the only reason a rolled log still says which
// build produced it.
#include <windows.h>
#include <cstdio>
#include <string>
#include "../src/log.h"
namespace {
std::wstring temp_directory() {
wchar_t buffer[MAX_PATH + 1] = {};
const DWORD length = GetTempPathW(MAX_PATH, buffer);
return std::wstring(buffer, length) + L"joc_log_rotation_test";
}
bool read_file(const std::wstring& path, std::string* out) {
std::FILE* file = _wfopen(path.c_str(), L"rb");
if (file == nullptr) return false;
char buffer[64 * 1024];
std::size_t got = 0;
while ((got = std::fread(buffer, 1, sizeof(buffer), file)) != 0) out->append(buffer, got);
std::fclose(file);
return true;
}
unsigned long long file_size(const std::wstring& path) {
WIN32_FILE_ATTRIBUTE_DATA data{};
if (GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &data) == FALSE) return 0;
return (static_cast<unsigned long long>(data.nFileSizeHigh) << 32) | data.nFileSizeLow;
}
bool contains(const std::string& haystack, const std::string& needle) {
return haystack.find(needle) != std::string::npos;
}
int failures = 0;
void check(bool ok, const char* what) {
std::printf("%-4s %s\n", ok ? "ok" : "FAIL", what);
if (!ok) ++failures;
}
} // namespace
int main() {
const std::wstring dir = temp_directory();
CreateDirectoryW(dir.c_str(), nullptr);
const std::wstring live = dir + L"\\rotation.log";
const std::wstring rolled = live + L".1";
const std::wstring chained = live + L".2";
DeleteFileW(live.c_str());
DeleteFileW(rolled.c_str());
DeleteFileW(chained.c_str());
SetEnvironmentVariableW(L"JOC_LOG", live.c_str());
const unsigned long long budget = joc_log::budget_bytes();
std::printf("budget %llu byte(s) per file\n", budget);
joc_log::open();
joc_log::header_begin();
joc_log::line("=== header marker ===");
joc_log::header_end();
// Enough lines to spend the budget twice over, so the roll has to happen and
// the file it produces has to be replaced rather than added to.
const std::string filler(160, 'x');
const unsigned lines = static_cast<unsigned>(budget / 190ull * 3ull);
for (unsigned i = 1; i <= lines; ++i) joc_log::line("filler %u %s", i, filler.c_str());
const unsigned long long live_size = file_size(live);
const unsigned long long rolled_size = file_size(rolled);
std::printf("wrote %u line(s): live %llu byte(s), rolled %llu byte(s)\n", lines, live_size,
rolled_size);
check(live_size > 0 && live_size <= budget + 4096, "live file is inside the budget");
check(rolled_size > 0 && rolled_size <= budget + 4096, "rolled file is inside the budget");
check(live_size + rolled_size <= 2 * budget + 8192, "the two together are inside twice it");
check(file_size(chained) == 0, "no second roll is kept");
std::string live_text;
std::string rolled_text;
check(read_file(live, &live_text), "live file reads back");
check(read_file(rolled, &rolled_text), "rolled file reads back");
check(contains(live_text, "[log rolled"), "the live file says it rolled");
check(contains(live_text, "=== header marker ==="), "the banner was written again");
char newest[64] = {};
std::snprintf(newest, sizeof(newest), "filler %u ", lines);
check(contains(live_text, newest), "the newest line is in the live file");
check(!contains(rolled_text, newest), "and not in the rolled one");
if (failures == 0) {
std::printf("log rotation holds\n");
return 0;
}
std::printf("%d check(s) failed\n", failures);
return 1;
}
+172
View File
@@ -0,0 +1,172 @@
// Checks that the component works with a path past MAX_PATH.
//
// long_path_test
//
// Windows refuses a path of MAX_PATH characters or more to any Win32 call that does
// not carry the \\?\ prefix. The prefix is a property of the path rather than of the
// process, so it has to be added by whoever calls the API -- which is why a file that
// plays from the desktop stops being found once its folder tree is deep enough, the
// shape an Atmos download under a long album title has.
//
// The path is built here rather than taken from the command line, so the test needs
// no fixture and cannot be defeated by the ANSI argv a console tool is handed.
#include <windows.h>
#include <cstdio>
#include <string>
#include "../src/container_scan.h"
#include "../src/joc_decode.h"
#include "../src/win_path.h"
namespace {
int failures = 0;
void check(bool ok, const char* what) {
std::printf("%-4s %s\n", ok ? "ok" : "FAIL", what);
if (!ok) ++failures;
}
// %TEMP%\joc_long_path
std::string temp_root() {
wchar_t buffer[MAX_PATH + 1] = {};
const DWORD length = GetTempPathW(MAX_PATH, buffer);
if (length == 0) return {};
return joc_path::to_utf8(std::wstring(buffer, length) + L"joc_long_path");
}
// One 45-character directory name. Five of them put the file comfortably past the
// limit whatever %TEMP% is.
std::string level_name(int index) {
return "level-" + std::string(39, static_cast<char>('a' + index));
}
bool make_directory(const std::string& path) {
if (CreateDirectoryW(joc_path::to_wide_extended(path).c_str(), nullptr)) return true;
return GetLastError() == ERROR_ALREADY_EXISTS;
}
// Builds the deep tree one level at a time, so every parent already exists by the
// time its child is asked for.
std::string build_tree() {
std::string path = temp_root();
if (path.empty() || !make_directory(path)) return {};
for (int i = 0; i < 5; ++i) {
path += "\\" + level_name(i);
if (!make_directory(path)) return {};
}
return path;
}
// A file that is a container but holds no audio track: enough for the probe to walk
// its boxes and report something of its own, rather than "cannot open the file".
bool write_container(const std::string& path) {
unsigned char bytes[32] = {};
bytes[3] = sizeof(bytes); // box size, big-endian
const char type[] = "ftypisom";
for (int i = 0; i < 8; ++i) bytes[4 + i] = static_cast<unsigned char>(type[i]);
std::FILE* file = _wfopen(joc_path::to_wide_extended(path).c_str(), L"wb");
if (file == nullptr) return false;
const std::size_t written = std::fwrite(bytes, 1, sizeof(bytes), file);
std::fclose(file);
return written == sizeof(bytes);
}
void remove_tree(const std::string& root, const std::string& leaf) {
// Files before the directories that hold them, deepest directory first, and
// never above root. Worth doing at all because a tree past MAX_PATH is one
// Explorer cannot delete: leaving it behind would be worse than the disk it
// occupies.
DeleteFileW(joc_path::to_wide_extended(leaf + "\\probe.m4a").c_str());
DeleteFileW(joc_path::to_wide_extended(root + "\\short.m4a").c_str());
std::string current = leaf;
while (current.size() > root.size()) {
RemoveDirectoryW(joc_path::to_wide_extended(current).c_str());
const std::string::size_type slash = current.find_last_of('\\');
if (slash == std::string::npos) break;
current.resize(slash);
}
RemoveDirectoryW(joc_path::to_wide_extended(root).c_str());
}
// The rules that keep the prefix from being applied where it would break a path.
void check_helper_rules() {
check(joc_path::to_wide_extended(std::string("ffmpeg")) == L"ffmpeg",
"a bare executable name is left for PATH to resolve");
check(joc_path::to_wide_extended(std::string("sub\\file.m4a")) == L"sub\\file.m4a",
"a relative path is left alone");
check(joc_path::to_wide_extended(std::string("C:\\a\\b.m4a")) == L"\\\\?\\C:\\a\\b.m4a",
"an absolute path gains the prefix");
check(joc_path::to_wide_extended(std::string("\\\\?\\C:\\a\\b.m4a")) ==
L"\\\\?\\C:\\a\\b.m4a",
"an already-prefixed path is not prefixed twice");
check(joc_path::to_wide_extended(std::string("\\\\server\\share\\b.m4a")) ==
L"\\\\?\\UNC\\server\\share\\b.m4a",
"a UNC path takes the UNC form");
check(joc_path::to_wide_extended(std::string("//server/share/b.m4a")) ==
L"\\\\?\\UNC\\server\\share\\b.m4a",
"and the same one in forward slashes, without doubling a separator");
check(joc_path::to_wide_extended(std::string("C:/a/b.m4a")) == L"\\\\?\\C:\\a\\b.m4a",
"forward slashes become backslashes, which the prefix stops normalizing");
check(joc_path::to_wide_extended(std::string("C:\\a\\..\\b.m4a")) == L"C:\\a\\..\\b.m4a",
"a .. segment is left alone, since the prefix would stop resolving it");
check(joc_path::to_wide_extended(std::string("C:/a/../b.m4a")) == L"C:/a/../b.m4a",
"and one in forward slashes is recognized as well");
}
} // namespace
int main() {
check_helper_rules();
const std::string tree = build_tree();
if (tree.empty()) {
std::printf("FAIL cannot build the tree under %s\n", temp_root().c_str());
return 1;
}
const std::string long_file = tree + "\\probe.m4a";
const std::string short_file = temp_root() + "\\short.m4a";
std::printf("long path : %zu chars\n", long_file.size());
std::printf("short path : %zu chars\n", short_file.size());
check(long_file.size() > MAX_PATH, "the path is past MAX_PATH, so the test is on it");
check(write_container(long_file), "the file was written at the long path");
check(write_container(short_file), "the control file was written at the short path");
// The premise: without the prefix the very same path is refused. If this ever
// starts passing, the machine lifts MAX_PATH for ordinary processes and the rest
// of the test no longer proves anything.
const HANDLE raw = CreateFileW(joc_path::to_wide(long_file).c_str(), GENERIC_READ,
FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
check(raw == INVALID_HANDLE_VALUE, "the raw path is still refused without the prefix");
if (raw != INVALID_HANDLE_VALUE) CloseHandle(raw);
const joc_container::Result long_result = joc_container::scan(long_file);
std::printf("container probe: kind=%s detail=%s\n",
joc_container::kind_name(long_result.kind), long_result.detail.c_str());
check(long_result.kind == joc_container::Kind::kMp4,
"the container probe reads the long path");
check(long_result.detail != "cannot open the file",
"and did not fall back to the unreadable-file answer");
const joc_decode::FileProbe probe = joc_decode::probe_file(long_file);
std::printf("decode reader : readable=%d detail=%s\n", probe.readable ? 1 : 0,
probe.detail.c_str());
check(probe.readable, "the decode-side reader opens the long path too");
const joc_container::Result short_result = joc_container::scan(short_file);
check(short_result.kind == joc_container::Kind::kMp4 && short_result.detail != "cannot open the file",
"a short path in the same tree still probes, prefix and all");
remove_tree(temp_root(), tree);
if (failures == 0) {
std::printf("long paths are handled\n");
return 0;
}
std::printf("%d check(s) failed\n", failures);
return 1;
}
+16 -7
View File
@@ -94,12 +94,23 @@ bool is_descendant(HWND candidate, HWND ancestor) {
// A control the user can operate: static text and group boxes answer WM_NCHITTEST
// with HTTRANSPARENT, so a real click passes straight through them.
//
// BS_GROUPBOX is 0x0007, whose bits are a subset of what BS_AUTOCHECKBOX (0x0003)
// and BS_AUTORADIOBUTTON (0x0009) set, so testing the style against BS_GROUPBOX
// alone classifies every checkbox and radio button as a group box -- and then
// exempts exactly the controls this tool exists to check. BS_TYPEMASK is what
// tells the button styles apart.
bool is_group_box_window(HWND window) {
if (class_of(window) != "Button") return false;
const LONG_PTR style = GetWindowLongPtrW(window, GWL_STYLE);
return (style & BS_TYPEMASK) == BS_GROUPBOX;
}
bool is_interactive(HWND window) {
const std::string window_class = class_of(window);
if (window_class == "Edit" || window_class == "ComboBox") return true;
if (window_class != "Button") return false;
const LONG_PTR style = GetWindowLongPtrW(window, GWL_STYLE);
return (style & BS_GROUPBOX) == 0;
return !is_group_box_window(window);
}
// True when another interactive sibling sits above the control at that point --
@@ -209,11 +220,9 @@ int main(int argc, char** argv) {
// Only controls the user is meant to operate count as problems: static
// text and group boxes are transparent to the mouse by design.
const LONG_PTR control_style = GetWindowLongPtrW(control.window, GWL_STYLE);
const bool is_group_box =
control.window_class == "Button" && (control_style & BS_GROUPBOX) != 0;
const bool is_label = control.window_class == "Static" && !is_group_box;
const bool interactive = !is_group_box && !is_label;
const bool group_box = is_group_box_window(control.window);
const bool is_label = control.window_class == "Static";
const bool interactive = !group_box && !is_label;
const bool bad = interactive && (!enabled || !inside || !hits_self);
if (bad) ++problems;
+220 -71
View File
@@ -4,6 +4,7 @@
//
// render_harness --input <file.eac3> --output <out.wav> [options]
// --mode binaural|speaker default binaural
// --format float32|int24 delivered sample format (default float32)
// --layout NAME speaker layout (default 7.1)
// --hrtf-source sofa|rosella kind of model --hrtf points at (default sofa)
// --hrtf PATH HRTF to render with; supplied by the caller and
@@ -14,15 +15,39 @@
// --tail S binaural tail seconds (default 5)
// --ffmpeg PATH default ffmpeg
// --max-frames N stop after N output frames (0 = all)
// --max-input-frames N stop feeding the renderer after N syncframes
// --seek-to S seek to S seconds before reading (repeatable;
// only the last one before reading takes effect)
// --seek-at N:S seek to S seconds once N output frames have been
// written (repeatable; N = 0 seeks before reading)
// --total-seconds S the duration seek() is told about (0 = unknown)
// --length S cut the delivered stream at S seconds (the file's
// own duration; the renderer still renders its tail)
// --pipeline-delay N delay a seek compensates for, in samples
// --preroll-frames N syncframes fed before the target frame
// --cycles N render the file N times in this process, in sequence
// --parallel N render it N times at once, in separate threads
//
// The two constants a seek uses are overridable so they can be checked by measurement.
//
// --cycles and --parallel cover what one render per process cannot: the renderer and the
// compiled-HRTF cache live as long as the process does, and foobar2000 starts the next
// file while the current one is still being read.
//
// It exists because the plugin's engine (src/joc_decode.*) has no foobar2000
// dependency: the very code that plays in foobar2000 can be run here and its
// output compared against the reference renderer, which is what the bit-exactness
// contract is about. The WAV header mirrors the renderer's own writer: a simple
// IEEE-float header up to two channels, WAVE_FORMAT_EXTENSIBLE above that with a
// channel mask of zero.
// dependency: the very code that plays in foobar2000 can be run here, against the
// reference renderer or against the same engine's continuous render. The WAV header
// mirrors the renderer's own writer: a simple header up to two channels,
// WAVE_FORMAT_EXTENSIBLE above that with a channel mask of zero -- for float32
// (IEEE_FLOAT) and for int24 (PCM) alike.
//
// --format int24 is the byte-for-byte counterpart of the CLI's
// --speaker-format/--binaural-format int24: the component renders float32 and the
// int24 packing is the core's own rule (src/joc_decode.h:pack_int24), so the file
// this writes is directly comparable with the one the reference writes.
#include <cstdio>
#include <thread>
#include <cstdlib>
#include <cstring>
#include <string>
@@ -36,6 +61,7 @@ struct Options {
std::string input;
std::string output;
std::string mode = "binaural";
std::string format = "float32"; // float32 | int24
std::string layout = "7.1";
std::string hrtf; // SOFA file, or Rosella model file
std::string hrtf_source = "sofa"; // sofa | rosella
@@ -44,6 +70,16 @@ struct Options {
double tail_seconds = 5.0;
std::uint64_t max_frames = 0;
std::uint64_t max_input_frames = 0;
std::vector<double> seeks_before; // --seek-to
std::vector<std::pair<std::uint64_t, double>> seeks_at; // --seek-at
double total_seconds = 0.0;
double length_seconds = 0.0; // --length: cut the delivered stream here
std::uint32_t pipeline_delay = joc_decode::kJocSeekPipelineDelaySamples;
std::uint32_t preroll_frames = joc_decode::kJocSeekPrerollFrames;
bool container = false;
unsigned audio_index = 0;
unsigned cycles = 0; // --cycles N: N renders in one process, in sequence
unsigned parallel = 0; // --parallel N: N renders at once, in one process
};
void put_u16(std::string* out, unsigned value) {
@@ -55,33 +91,37 @@ void put_u32(std::string* out, unsigned long long value) {
for (int i = 0; i < 4; ++i) out->push_back(static_cast<char>((value >> (8 * i)) & 0xFFu));
}
// Same bytes the core's wav_writer produces.
std::string wav_header(unsigned channels, unsigned rate, std::uint64_t frames) {
const unsigned bytes_per_sample = 4;
// Same bytes the core's wav_writer produces, for either format it can write.
std::string wav_header(unsigned channels, unsigned rate, std::uint64_t frames, bool int24) {
const unsigned bytes_per_sample = int24 ? 3u : 4u;
const unsigned bits_per_sample = 8u * bytes_per_sample;
const unsigned block_align = channels * bytes_per_sample;
const std::uint64_t data_bytes = frames * block_align;
std::string fmt;
if (channels <= 2) {
put_u16(&fmt, 3); // WAVE_FORMAT_IEEE_FLOAT
put_u16(&fmt, int24 ? 0x0001u : 0x0003u); // WAVE_FORMAT_PCM / IEEE_FLOAT
put_u16(&fmt, channels);
put_u32(&fmt, rate);
put_u32(&fmt, rate * block_align);
put_u16(&fmt, block_align);
put_u16(&fmt, 8 * bytes_per_sample);
put_u16(&fmt, bits_per_sample);
} else {
put_u16(&fmt, 0xFFFE); // WAVE_FORMAT_EXTENSIBLE
put_u16(&fmt, channels);
put_u32(&fmt, rate);
put_u32(&fmt, rate * block_align);
put_u16(&fmt, block_align);
put_u16(&fmt, 8 * bytes_per_sample);
put_u16(&fmt, bits_per_sample);
put_u16(&fmt, 22); // cbSize
put_u16(&fmt, 8 * bytes_per_sample);
put_u16(&fmt, bits_per_sample);
put_u32(&fmt, 0); // channel mask, as the core writes it
static const unsigned char kFloatGuid[16] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
fmt.append(reinterpret_cast<const char*>(kFloatGuid), 16);
static const unsigned char kPcmGuid[16] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
const unsigned char* guid = int24 ? kPcmGuid : kFloatGuid;
fmt.append(reinterpret_cast<const char*>(guid), 16);
}
std::string header;
@@ -103,6 +143,7 @@ bool parse(int argc, char** argv, Options* options) {
if (arg == "--input") options->input = next();
else if (arg == "--output") options->output = next();
else if (arg == "--mode") options->mode = next();
else if (arg == "--format") options->format = next();
else if (arg == "--layout") options->layout = next();
else if (arg == "--hrtf") options->hrtf = next();
else if (arg == "--hrtf-source") options->hrtf_source = next();
@@ -111,15 +152,137 @@ bool parse(int argc, char** argv, Options* options) {
else if (arg == "--tail") options->tail_seconds = std::atof(next().c_str());
else if (arg == "--max-frames") options->max_frames = std::strtoull(next().c_str(), nullptr, 10);
else if (arg == "--max-input-frames") options->max_input_frames = std::strtoull(next().c_str(), nullptr, 10);
else if (arg == "--seek-to") options->seeks_before.push_back(std::atof(next().c_str()));
else if (arg == "--total-seconds") options->total_seconds = std::atof(next().c_str());
else if (arg == "--length") options->length_seconds = std::atof(next().c_str());
else if (arg == "--pipeline-delay") options->pipeline_delay = static_cast<std::uint32_t>(std::strtoul(next().c_str(), nullptr, 10));
else if (arg == "--preroll-frames") options->preroll_frames = static_cast<std::uint32_t>(std::strtoul(next().c_str(), nullptr, 10));
else if (arg == "--container") options->container = true;
else if (arg == "--cycles") options->cycles = static_cast<unsigned>(std::strtoul(next().c_str(), nullptr, 10));
else if (arg == "--parallel") options->parallel = static_cast<unsigned>(std::strtoul(next().c_str(), nullptr, 10));
else if (arg == "--audio-index") options->audio_index = static_cast<unsigned>(std::strtoul(next().c_str(), nullptr, 10));
else if (arg == "--seek-at") {
const std::string value = next();
const std::string::size_type colon = value.find(':');
if (colon == std::string::npos) {
std::fprintf(stderr, "render_harness: --seek-at wants <frames>:<seconds>\n");
return false;
}
options->seeks_at.emplace_back(
std::strtoull(value.substr(0, colon).c_str(), nullptr, 10),
std::atof(value.substr(colon + 1).c_str()));
}
else if (arg == "--help" || arg == "-h") return false;
else {
std::fprintf(stderr, "render_harness: unknown argument %s\n", arg.c_str());
return false;
}
}
if (options->format != "float32" && options->format != "int24") {
std::fprintf(stderr, "render_harness: --format wants float32 or int24\n");
return false;
}
return !options->input.empty() && !options->output.empty();
}
// One render: everything the component does for one file from start to stop.
// Returns false with *error set when the engine reports a failure.
bool render_once(const joc_decode::Settings& settings, const Options& options,
const std::string& output, std::string* error) {
joc_decode::Engine engine;
if (!engine.start(options.input, settings, error)) return false;
const unsigned channels = engine.channels();
if (channels == 0) {
*error = "engine reported zero channels";
return false;
}
for (const double seconds : options.seeks_before) {
if (!engine.seek(seconds, options.total_seconds, error)) return false;
}
std::FILE* file = std::fopen(output.c_str(), "wb");
if (file == nullptr) {
*error = "cannot write " + output;
return false;
}
// The header carries the length, so write a placeholder and come back to it.
const bool int24 = settings.force_int24;
const std::string header = wav_header(channels, 48000, 0, int24);
std::fwrite(header.data(), 1, header.size(), file);
constexpr std::size_t kChunk = 4096;
std::vector<float> buffer(kChunk * channels);
std::vector<std::uint8_t> packed;
std::uint64_t frames_written = 0;
double peak = 0.0;
// What the int24 packing wrote, so the run reports the grid it delivered.
std::uint64_t clipped_samples = 0;
std::int32_t grid_peak = 0;
std::vector<char> applied(options.seeks_at.size(), 0);
for (;;) {
for (std::size_t i = 0; i < options.seeks_at.size(); ++i) {
if (applied[i] != 0 || options.seeks_at[i].first > frames_written) continue;
applied[i] = 1;
if (!engine.seek(options.seeks_at[i].second, options.total_seconds, error)) {
std::fclose(file);
return false;
}
}
std::size_t want = kChunk;
if (options.max_frames != 0) {
if (frames_written >= options.max_frames) break;
const std::uint64_t left = options.max_frames - frames_written;
if (left < want) want = static_cast<std::size_t>(left);
}
const std::size_t frames = engine.read(buffer.data(), want, error);
if (frames == 0) {
if (!error->empty()) {
std::fclose(file);
return false;
}
break;
}
for (std::size_t i = 0; i < frames * channels; ++i) {
const double value = buffer[i] < 0.0f ? -static_cast<double>(buffer[i])
: static_cast<double>(buffer[i]);
if (value > peak) peak = value;
}
if (int24) {
packed.resize(frames * channels * 3u);
const joc_decode::Int24Block block =
joc_decode::pack_int24(buffer.data(), frames * channels, packed.data());
clipped_samples += block.clipped;
if (block.peak > grid_peak) grid_peak = block.peak;
std::fwrite(packed.data(), 1, packed.size(), file);
} else {
std::fwrite(buffer.data(), sizeof(float), frames * channels, file);
}
frames_written += frames;
}
engine.stop();
const std::string final_header = wav_header(channels, 48000, frames_written, int24);
std::fseek(file, 0, SEEK_SET);
std::fwrite(final_header.data(), 1, final_header.size(), file);
std::fclose(file);
std::printf("render_harness: %s -> %s\n", options.input.c_str(), output.c_str());
std::printf(" channels=%u frames=%llu format=%s peak=%.9f\n", channels,
static_cast<unsigned long long>(frames_written),
int24 ? "int24" : "float32", peak);
if (int24) {
std::printf(" int24 grid_peak=%.9f clipped=%llu\n",
static_cast<double>(grid_peak) / joc_decode::kInt24ImportScale,
static_cast<unsigned long long>(clipped_samples));
}
return true;
}
std::string numbered(const std::string& output, unsigned index) {
return output + "." + std::to_string(index) + ".wav";
}
} // namespace
int main(int argc, char** argv) {
@@ -127,9 +290,13 @@ int main(int argc, char** argv) {
if (!parse(argc, argv, &options)) {
std::fprintf(stderr,
"usage: render_harness --input <eac3> --output <wav> [--mode binaural|speaker]\n"
" [--layout NAME] [--hrtf-source sofa|rosella] [--hrtf PATH]\n"
" [--format float32|int24] [--layout NAME]\n"
" [--hrtf-source sofa|rosella] [--hrtf PATH]\n"
" [--gain-db X] [--tail S] [--ffmpeg path] [--max-frames N]\n"
" [--max-input-frames N]\n");
" [--max-input-frames N] [--seek-to S] [--seek-at N:S]\n"
" [--total-seconds S] [--pipeline-delay N] [--preroll-frames N]\n"
" [--container] [--audio-index N] [--length S]\n"
" [--cycles N] [--parallel N]\n");
return 2;
}
@@ -144,66 +311,48 @@ int main(int argc, char** argv) {
settings.tail_seconds = options.tail_seconds;
settings.ffmpeg_path = options.ffmpeg;
settings.input_frame_limit = options.max_input_frames;
settings.pipeline_delay_samples = options.pipeline_delay;
settings.seek_preroll_frames = options.preroll_frames;
settings.length_seconds = options.length_seconds;
settings.input_kind = options.container ? joc_decode::InputKind::kContainer
: joc_decode::InputKind::kBare;
settings.audio_index = options.audio_index;
// The engine always renders float32; the flag is what the writer above packs.
settings.force_int24 = (options.format == "int24");
joc_decode::Engine engine;
std::string error;
if (!engine.start(options.input, settings, &error)) {
std::fprintf(stderr, "render_harness: engine start failed: %s\n", error.c_str());
return 1;
}
const unsigned channels = engine.channels();
if (channels == 0) {
std::fprintf(stderr, "render_harness: engine reported zero channels\n");
return 1;
}
std::FILE* file = std::fopen(options.output.c_str(), "wb");
if (file == nullptr) {
std::fprintf(stderr, "render_harness: cannot write %s\n", options.output.c_str());
return 1;
}
// The header carries the length, so write a placeholder and come back to it.
const std::string header = wav_header(channels, 48000, 0);
std::fwrite(header.data(), 1, header.size(), file);
constexpr std::size_t kChunk = 4096;
std::vector<float> buffer(kChunk * channels);
std::uint64_t frames_written = 0;
double peak = 0.0;
for (;;) {
std::size_t want = kChunk;
if (options.max_frames != 0) {
if (frames_written >= options.max_frames) break;
const std::uint64_t left = options.max_frames - frames_written;
if (left < want) want = static_cast<std::size_t>(left);
if (options.parallel != 0) {
// The same file rendered by several engines at once, which is what a track
// change looks like from the engine's side: foobar2000 starts the next file
// while the current one is still being read.
std::vector<std::thread> threads;
std::vector<std::string> errors(options.parallel);
for (unsigned i = 0; i < options.parallel; ++i) {
threads.emplace_back([&, i] {
if (!render_once(settings, options, numbered(options.output, i), &errors[i])) {
std::fprintf(stderr, "render_harness: parallel %u: %s\n", i,
errors[i].c_str());
}
});
}
const std::size_t frames = engine.read(buffer.data(), want, &error);
if (frames == 0) {
if (!error.empty()) {
std::fprintf(stderr, "render_harness: read failed: %s\n", error.c_str());
std::fclose(file);
return 1;
}
break;
for (std::thread& thread : threads) thread.join();
for (const std::string& text : errors) {
if (!text.empty()) return 1;
}
for (std::size_t i = 0; i < frames * channels; ++i) {
const double value = buffer[i] < 0.0f ? -static_cast<double>(buffer[i])
: static_cast<double>(buffer[i]);
if (value > peak) peak = value;
}
std::fwrite(buffer.data(), sizeof(float), frames * channels, file);
frames_written += frames;
return 0;
}
engine.stop();
const std::string final_header = wav_header(channels, 48000, frames_written);
std::fseek(file, 0, SEEK_SET);
std::fwrite(final_header.data(), 1, final_header.size(), file);
std::fclose(file);
std::printf("render_harness: %s -> %s\n", options.input.c_str(), options.output.c_str());
std::printf(" channels=%u frames=%llu samples_per_channel=%llu peak=%.9f\n", channels,
static_cast<unsigned long long>(frames_written),
static_cast<unsigned long long>(frames_written), peak);
const unsigned cycles = (options.cycles == 0) ? 1 : options.cycles;
for (unsigned cycle = 0; cycle < cycles; ++cycle) {
// Every cycle is a fresh engine in the same process: the compiled-HRTF cache
// and everything else the renderer keeps per process is reused, exactly as it
// is when a second file is played in the same foobar2000 session.
error.clear();
const std::string output = (cycles == 1) ? options.output : numbered(options.output, cycle);
if (!render_once(settings, options, output, &error)) {
std::fprintf(stderr, "render_harness: cycle %u: %s\n", cycle, error.c_str());
return 1;
}
}
return 0;
}
}
+2
View File
@@ -40,6 +40,8 @@ $lines = @(
"cd /d `"$projectRoot`"",
# log.cpp comes along because the engine writes its diagnostics through it.
"cl $common /Fe:`"$outDir\container_scan_test.exe`" /Fo:`"$outDir\\`" tests\container_scan_test.cpp src\container_scan.cpp src\log.cpp",
"cl $common /Fe:`"$outDir\log_rotation_test.exe`" /Fo:`"$outDir\\`" tests\log_rotation_test.cpp src\log.cpp",
"cl $common /Fe:`"$outDir\long_path_test.exe`" /Fo:`"$outDir\\`" tests\long_path_test.cpp src\container_scan.cpp src\joc_decode.cpp src\eac3_scan.cpp src\log.cpp `"$coreLib`" shell32.lib",
"cl $common /Fe:`"$outDir\scan_selftest.exe`" /Fo:`"$outDir\\`" tests\scan_selftest.cpp src\eac3_scan.cpp",
"cl $common /Fe:`"$outDir\prefs_layout_check.exe`" /Fo:`"$outDir\\`" tests\prefs_layout_check.cpp user32.lib gdi32.lib",
"cl $common /Fe:`"$outDir\scan_crosscheck.exe`" /Fo:`"$outDir\\`" tests\scan_crosscheck.cpp src\eac3_scan.cpp `"$coreLib`" shell32.lib",
+14 -8
View File
@@ -4,15 +4,16 @@
# pwsh -File tools/deploy.ps1 -TestBed D:\fb2k -Platform x64
#
# Where the component has to go depends on the foobar2000 layout, and getting it
# wrong is silent: foobar2000 simply never calls LoadLibrary on the file. Two
# layouts are in the wild:
# wrong is silent: foobar2000 simply never calls LoadLibrary on the file.
#
# profile-relative <app>\profile\user-components\<name>\<name>.dll
# foobar2000 1.6.19 portable and 2.x
# app-relative <app>\user-components\<name>\<name>.dll
# older / repacked 1.6 installs whose profile is <app>\configuration
# 1.6 <app>\profile\user-components\<name>\<name>.dll
# (portable mode; without it the profile is in %APPDATA%)
# 2.0 and up <app>\user-components\<name>\<name>.dll
# even in portable mode, whose profile is <app>\profile
# older 1.6 <app>\user-components\<name>\<name>.dll
# repacked installs whose profile is <app>\configuration
#
# The per-component subdirectory is required in both: a DLL lying directly in
# The per-component subdirectory is required in every case: a DLL lying directly in
# user-components\ is not scanned. Installing a .fb2k-component package through
# foobar2000 itself ends up doing the same thing.
[CmdletBinding()]
@@ -31,11 +32,16 @@ if (-not (Test-Path -LiteralPath (Join-Path $TestBed 'foobar2000.exe'))) {
throw "no foobar2000.exe in $TestBed"
}
# The core version decides the layout, and its own profile records it.
$versionFile = Join-Path $TestBed 'profile\version.txt'
$version = if (Test-Path -LiteralPath $versionFile) { (Get-Content -LiteralPath $versionFile -Raw).Trim() } else { '' }
if (-not $Target) {
$appRelative = Test-Path -LiteralPath (Join-Path $TestBed 'configuration')
if ($version -match 'v(\d+)\.') { $appRelative = ([int]$Matches[1] -ge 2) }
$root = if ($appRelative) { $TestBed } else { Join-Path $TestBed 'profile' }
$Target = Join-Path $root 'user-components'
Write-Host ("layout: {0} ({1})" -f $(if ($appRelative) { 'app-relative' } else { 'profile-relative' }), $root)
Write-Host ("layout: {0} ({1}){2}" -f $(if ($appRelative) { 'app-relative' } else { 'profile-relative' }), $root, $(if ($version) { ", $version" } else { '' }))
}
# Portable profile: config stays inside the test bed instead of %APPDATA%.
+4 -1
View File
@@ -39,7 +39,10 @@ if (Test-Path -LiteralPath $running) {
}
$arguments = @()
if ($Play) { $arguments += $Play }
# Start-Process joins the array with spaces and quotes nothing itself, so a path with
# a space in it would reach foobar2000 as several arguments and nothing would play --
# silently, which is the worst way for a test bed to fail.
if ($Play) { $arguments += ($Play | ForEach-Object { '"' + $_ + '"' }) }
$process = Start-Process -FilePath $exe -ArgumentList $arguments -PassThru
Write-Host "started pid=$($process.Id) $($Play -join ', ')"
Start-Sleep -Seconds $WaitSeconds