13 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
56 changed files with 3402 additions and 610 deletions

No files matched your search

+2 -1
View File
@@ -27,8 +27,9 @@
*.personalized_headphone *.personalized_headphone
rosella_kernels.npz 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
joc_decoder.log.1
# Measurement record: local working notes, kept on disk and never published. # Measurement record: local working notes, kept on disk and never published.
VERIFICATION.md VERIFICATION.md
+26 -3
View File
@@ -3,8 +3,9 @@
[简体中文](README.md) · [Install](#install) · [Settings](#settings) · [Building](#building) · [Known limitations](#known-limitations) [简体中文](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 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 5.1 core PCM that ffmpeg metadata is taken from the E-AC-3 syncframes and paired with the core PCM that ffmpeg decodes
decodes, then rendered in real time to headphones (HRTF) or to a speaker layout up to 7.1. The (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 rendering core is compiled into the component, so there is nothing to install beside
`foo_input_joc.dll`. `foo_input_joc.dll`.
@@ -65,6 +66,15 @@ Preferences → Tools → **JOC decoder**:
`binaural.personalized_headphone`; `binaural.personalized_headphone`;
* **Gain** — a switch and a value in dB. Binaural rendering can exceed full scale on material * **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; 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. * the **ffmpeg** executable to use.
No control on the page is disabled, and the status line states what is in effect. No control on the page is disabled, and the status line states what is in effect.
@@ -94,11 +104,19 @@ and test-bed scripts. Settings also read `JOC_*` environment overrides for one r
only); the list and what each one does is in `src\settings.cpp`. 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 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. 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 ## Known limitations
* ADM BWF output is not implemented. * 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 * 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 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 does not let a decoder ask for a file another entry has already taken. Such a file's tags stay
@@ -107,6 +125,11 @@ version, the core version and the log path there at start-up.
behind it) can be **read but not written**: no component claims raw E-AC-3 for writing, and 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. rewriting the whole file to insert a tag is not this component's job.
* Transport streams (`.ts`, `.m2ts`) are not claimed. * 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 * 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. 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 * A seek re-enters the bitstream at the frame holding the target instead of decoding everything
+16 -2
View File
@@ -3,7 +3,7 @@
[English](README.en.md) · [安装](#安装) · [设置](#设置) · [构建](#构建) · [已知限制](#已知限制) [English](README.en.md) · [安装](#安装) · [设置](#设置) · [构建](#构建) · [已知限制](#已知限制)
foobar2000 输入组件,播放 **E-AC-3 JOC(Dolby Atmos)** 文件:从 E-AC-3 同步帧里取出 JOC foobar2000 输入组件,播放 **E-AC-3 JOC(Dolby Atmos)** 文件:从 E-AC-3 同步帧里取出 JOC
对象与 OAMD 元数据,与 ffmpeg 解出的 5.1 核心 PCM 配对后实时渲染,输出双耳(HRTF)或最多 对象与 OAMD 元数据,与 ffmpeg 解出的核心 PCM(按 $joc\_dmx\_config\_idx$ 为 5.1 或 7 通道)配对后实时渲染,输出双耳(HRTF)或最多
7.1 的扬声器布局。渲染内核直接编译进组件,除 `foo_input_joc.dll` 之外不需要安装任何东西。 7.1 的扬声器布局。渲染内核直接编译进组件,除 `foo_input_joc.dll` 之外不需要安装任何东西。
输入是裸流 `.eac3` / `.ec3`,或容器里的 E-AC-3 JOC 轨道(`.mp4` `.m4a` `.m4b` `.m4p` 输入是裸流 `.eac3` / `.ec3`,或容器里的 E-AC-3 JOC 轨道(`.mp4` `.m4a` `.m4b` `.m4p`
@@ -57,6 +57,12 @@ Preferences → Tools → **JOC decoder**:
* **HRTF source** —— **SOFA** 文件或 **Rosella** `.personalized_headphone` 模型。路径留空表示 * **HRTF source** —— **SOFA** 文件或 **Rosella** `.personalized_headphone` 模型。路径留空表示
用默认位置 `<组件目录>\HRTF\` 下的 `binaural.sofa` 或 `binaural.personalized_headphone`; 用默认位置 `<组件目录>\HRTF\` 下的 `binaural.sofa` 或 `binaural.personalized_headphone`;
* **Gain** —— 开关加 dB 值。双耳渲染在核心混音不削顶的素材上也可能超过满刻度,衰减放在这里; * **Gain** —— 开关加 dB 值。双耳渲染在核心混音不削顶的素材上也可能超过满刻度,衰减放在这里;
* **强制 24bit 输出(int24)** —— 默认关闭,交付 float32。打开后,交付前按内核写 WAV 的 int24
规则量化(截到 ±1 → ×8388607 → 截断),超范围的样本被截断到边缘、次数记进日志;组件同时把该
文件的技术信息报成 24bit 定点。渲染本身仍是 float32,这一项说的是交付给 foobar2000 的采样精度,
不是输出设备的位深(那个在 Preferences → Output 里设置)。**切换这个开关后要刷新文件信息**
(Properties → Reload info,或把文件从媒体库移除后重新加入):foobar2000 按文件缓存技术信息,
缓存没刷新时 Properties 与转换器仍按旧的 32bit 浮点判断,会提示"编码器不支持浮点输入"。
* **ffmpeg** 可执行文件路径。 * **ffmpeg** 可执行文件路径。
页面上的控件都不禁用,状态行会说明当前生效的是什么。 页面上的控件都不禁用,状态行会说明当前生效的是什么。
@@ -82,16 +88,24 @@ pwsh -File tools/package.ps1 # 两个架构,打包到 dist\*.fb2k-c
另有 `JOC_*` 环境变量覆盖(仅用于开发运行),清单与含义在 `src\settings.cpp`。 另有 `JOC_*` 环境变量覆盖(仅用于开发运行),清单与含义在 `src\settings.cpp`。
排查问题看 DLL 旁边的 `joc_decoder.log`;组件启动时会把自己的版本、核心版本、日志路径写在 排查问题看 DLL 旁边的 `joc_decoder.log`;组件启动时会把自己的版本、核心版本、日志路径写在
里面。 里面。日志按**字节**封顶:单个文件写满 16 MiB 就滚到同目录的 `joc_decoder.log.1`(覆盖上一次
滚动,不累积),所以两份加起来不超过 32 MiB,最新的内容始终在 `joc_decoder.log`。滚动之后版本
信息会重写一遍。媒体库扫描会给库里每个容器写日志,体量主要来自这里。
## 已知限制 ## 已知限制
* 不实现 ADM BWF 输出。 * 不实现 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)**能读不能写**:没有 * 裸流 `.eac3` / `.ec3` 的标签(流前面的 ID3v2,或后面的 APEv2/ID3v1)**能读不能写**:没有
组件声明可以写裸 E-AC-3,为插入标签重写整个文件也不是本组件该做的事。 组件声明可以写裸 E-AC-3,为插入标签重写整个文件也不是本组件该做的事。
* 传输流(`.ts`、`.m2ts`)不接管。 * 传输流(`.ts`、`.m2ts`)不接管。
* **强制 24bit 输出**只改变解码器交付的采样精度:交付的 int24 会被 foobar2000 转成它内部的
浮点,输出设备用什么位深仍由 Preferences → Output 决定,组件不能替它选。超出 ±1 的样本按内核
的 `continue` 策略截断(双耳渲染可能超过满刻度,需要衰减时用 **Gain**)。
* 播放长度严格等于文件时长。双耳渲染器仍会算出房间尾音,但它不作为文件本身没有的播放时间交付。 * 播放长度严格等于文件时长。双耳渲染器仍会算出房间尾音,但它不作为文件本身没有的播放时间交付。
* 跳转会从包含目标位置的那个帧重新进入码流,而不是把前面的内容全部解码一遍——这是跳转代价与 * 跳转会从包含目标位置的那个帧重新进入码流,而不是把前面的内容全部解码一遍——这是跳转代价与
目标位置无关的原因。位置精确、不漂移;样本是同一段波形交给了从该处开始的解码器,与从头播放 目标位置无关的原因。位置精确、不漂移;样本是同一段波形交给了从该处开始的解码器,与从头播放
+12 -26
View File
@@ -1,31 +1,9 @@
# Third-party notices / 第三方通知 # Third-party notices / 第三方通知
本仓库包含 JustOneCacophony 原生库的逐字节副本(下称"复用文件"),以及遵循公开标准实现的 本文件记录 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 数据与
## 复用文件 专利的边界说明。
以下文件是本项目(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`,输出与原
实现逐位相同(见该文件头部的验证记录)。
## 公开标准来源 ## 公开标准来源
@@ -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`)等公开实现中查到。 相同数值可在 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` ## HRTF 数据与 `.jochrtf`
`.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发仍受源 `.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发仍受源
+1
View File
@@ -114,6 +114,7 @@
<ClInclude Include="src\prefs.h" /> <ClInclude Include="src\prefs.h" />
<ClInclude Include="src\resource.h" /> <ClInclude Include="src\resource.h" />
<ClInclude Include="src\settings.h" /> <ClInclude Include="src\settings.h" />
<ClInclude Include="src\win_path.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+19 -12
View File
@@ -23,11 +23,12 @@ extern "C" {
#endif #endif
enum { enum {
EJOC_ABI_VERSION = 1, EJOC_ABI_VERSION = 2,
EJOC_FRAME_SAMPLES = 1536, EJOC_FRAME_SAMPLES = 1536,
EJOC_TIMESLOTS = 24, EJOC_TIMESLOTS = 24,
EJOC_SUBBANDS = 64, 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_OUTPUT_CHANNELS = 16,
EJOC_MAX_OBJECTS = 15, EJOC_MAX_OBJECTS = 15,
EJOC_MAX_DPOINTS = 2, EJOC_MAX_DPOINTS = 2,
@@ -47,15 +48,18 @@ typedef void* ejoc_binaural_renderer_handle;
/* /*
Fixed array layouts used by ejoc_renderer_process(): Fixed array layouts used by ejoc_renderer_process():
bed5_planar [5][1536] bed_planar [core_channel_count][1536]
lfe [1536] or NULL lfe [1536] or NULL
n_bands [15] n_bands [15]
n_dpoints [15] n_dpoints [15]
slope_idx [15] slope_idx [15]
offset_ts [15][2] offset_ts [15][2]
dq [15][2][5][23] dq [15][2][7][23]
output16 [16][1536] 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. Only objects selected by object_mask are read from the descriptor arrays.
dq carries already dequantized matrix coefficients in double precision; the dq carries already dequantized matrix coefficients in double precision; the
caller performs the JOC bitstream differential decoding for both dense and caller performs the JOC bitstream differential decoding for both dense and
@@ -69,21 +73,24 @@ EJOC_API void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle); EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads); EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads);
/* /*
Enables or disables the Ls/Rs band-0 21-tap DC compensation. The caller derives core_channel_count comes from joc_dmx_config_idx: 5 for configurations 0 and 3,
it from the JOC downmix configuration: only configurations 3 and 4 enable the 7 for 1, 2 and 4. bed_planar is then [core_channel_count][1536] and only channels
filter. When disabled, band 0 keeps the common per-band processing (surround below core_channel_count are read from dq, whose stride stays [15][2][7][23].
delay plus -j rotation) instead of being overwritten by the FIR. The delay line
and DC history advance either way, so the flag may change between frames. phase_shift is sigma(k) = (k in {3,4}): it enables the Ls/Rs -j rotation of
Defaults to enabled when never called. subbands above 0 together with the band-0 21-tap DC compensation. With the switch
off, channels 3 and 4 only take the common ten-slot surround delay. The delay
line and DC history advance either way, so the flag may change between frames.
*/ */
EJOC_API int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled);
EJOC_API uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle); EJOC_API 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 const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_process( EJOC_API int EJOC_CALL ejoc_renderer_process(
ejoc_renderer_handle handle, ejoc_renderer_handle handle,
const float* bed5_planar, const float* bed_planar,
const float* lfe, const float* lfe,
uint32_t core_channel_count,
uint32_t phase_shift,
uint32_t object_mask, uint32_t object_mask,
const uint8_t* n_bands, const uint8_t* n_bands,
const uint8_t* n_dpoints, const uint8_t* n_dpoints,
+5 -3
View File
@@ -34,7 +34,9 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.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_FRAME_PARAMS_VERSION 1u
#define JOC_EMDF_INFO_VERSION 1u #define JOC_EMDF_INFO_VERSION 1u
#define JOC_TASK_CONFIG_VERSION 3u #define JOC_TASK_CONFIG_VERSION 3u
@@ -193,7 +195,7 @@ typedef struct joc_frame_params {
uint8_t n_channels; /* 5 or 7 */ uint8_t n_channels; /* 5 or 7 */
uint8_t clipgain_x_bits; uint8_t clipgain_x_bits;
uint8_t clipgain_y_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 seq_count; /* 10-bit JOC sequence counter, parsed only */
uint32_t present_mask; /* bit i == object i present */ uint32_t present_mask; /* bit i == object i present */
uint32_t data_end_bits; /* bit position just after joc_data */ uint32_t data_end_bits; /* bit position just after joc_data */
@@ -373,7 +375,7 @@ typedef struct joc_task_config {
/* input */ /* input */
const char* input_path; /* .eac3/.ec3/.m4a/... */ 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* bed_path; /* NULL = decode the core PCM with ffmpeg */
const char* work_dir; /* NULL = a temporary directory */ const char* work_dir; /* NULL = a temporary directory */
double eac3_drc_scale; /* 0 = DRC off (reference default) */ 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 { typedef enum joc_stream_input {
JOC_STREAM_IN_EAC3 = 0, /* bare E-AC-3 syncframes (the metadata stream) */ 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_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */
} joc_stream_input; } joc_stream_input;
typedef enum joc_stream_output { typedef enum joc_stream_output {
JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* planar [16][samples] float32 */ JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* objects16 frames, planar [16][1536] each */
JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */ JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */
JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */ JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */
} joc_stream_output; } joc_stream_output;
typedef struct joc_stream_config { 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 buffered_samples; /* rendered but not yet pulled, per channel */
uint64_t oamd_payloads; uint64_t oamd_payloads;
uint64_t oamd_transitions; 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 output_channels;
uint32_t ended; /* 1 after flush() */ uint32_t ended; /* 1 after flush() */
} joc_stream_status_info; } 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. shipping and loading joc_core.dll.
The sources under kernel\src and kernel\include are a verbatim copy of the The sources under kernel\src and kernel\include are a verbatim copy of the
upstream library (see JustOneCacophonyCXX\CMakeLists.txt); the only edits are upstream library (see JustOneCacophonyCXX\CMakeLists.txt): nothing under either
the JOC_STATIC branch in kernel\include\joc_core.h / joc_stream.h and the tree is edited here, and both are kept identical to it, so a single core serves
EJOC_STATIC branch in kernel\include\eac3joc_core.h, which turn JOC_API and the upstream tools and this component alike. The static build needs no branch
EJOC_API into empty macros for a static build. 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) Conventions deliberately mirror the component project (foo_input_joc.vcxproj)
and the kernel's own CMake build: and the kernel's own CMake build:
@@ -113,18 +115,17 @@
</ItemDefinitionGroup> </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 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 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 (a baseline unit) picks one at run time from CPUID/XGETBV.
enables SIMD only for x86_64 and aarch64, so:
* 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 / * x64 gets the AVX2 and AVX-512 units, JOC_SIMD_HAVE_SSE2 / _AVX2 /
_AVX512, and a per-file /arch for those two files only; _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 * 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 build lists it only for aarch64 (CMakeLists.txt lines 177-180), which no
configuration of this project targets. 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> <PreprocessorDefinitions>JOC_SIMD_HAVE_SSE2=1;JOC_SIMD_HAVE_AVX2=1;JOC_SIMD_HAVE_AVX512=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'">
<ClCompile>
<PreprocessorDefinitions>JOC_SIMD_HAVE_AVX2=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<!-- Verbatim copies of the upstream native library (JOC_REUSED_SOURCES). --> <!-- Verbatim copies of the upstream native library (JOC_REUSED_SOURCES). -->
@@ -203,10 +209,17 @@
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(Platform)'=='Win32'">
<ClCompile Include="src\simd\kernels_intrin_avx2.cpp">
<AdditionalOptions>/arch:AVX2 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="include\eac3joc_core.h" /> <ClInclude Include="include\eac3joc_core.h" />
<ClInclude Include="include\joc_core.h" /> <ClInclude Include="include\joc_core.h" />
<ClInclude Include="include\joc_stream.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_metadata.h" />
<ClInclude Include="src\adm\adm_tracks.h" /> <ClInclude Include="src\adm\adm_tracks.h" />
<ClInclude Include="src\binaural\binaural_runtime.h" /> <ClInclude Include="src\binaural\binaural_runtime.h" />
+3 -11
View File
@@ -11,16 +11,8 @@
namespace { namespace {
thread_local std::string g_detail;
joc_error finish(const joc::Status& status) { joc_error finish(const joc::Status& status) {
if (status.ok()) { joc::record_error_detail(status);
g_detail.clear();
return JOC_OK;
}
g_detail.assign(status.stage());
g_detail.append(": ");
g_detail.append(status.message());
return status.code(); return status.code();
} }
@@ -49,7 +41,7 @@ extern "C" {
std::uint32_t JOC_CALL joc_abi_version(void) { return JOC_ABI_VERSION; } 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_event_size(void) { return static_cast<std::uint32_t>(sizeof(joc_event)); }
std::uint32_t JOC_CALL joc_task_config_size(void) { 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_error JOC_CALL joc_parse_id14(const std::uint8_t* payload, std::size_t payload_size,
joc_frame_params* out_params) { joc_frame_params* out_params) {
+12 -2
View File
@@ -13,6 +13,9 @@ struct joc_stream {
namespace { namespace {
joc_error finish_stream(const joc::Status& status) { 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(); return status.code();
} }
@@ -93,12 +96,14 @@ joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer*
return pushed; return pushed;
} }
std::size_t consumed = 0; std::size_t consumed = 0;
const std::uint32_t channels = input->channels != 0u ? input->channels : 6u;
const joc::Status pushed = 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) { if (consumed_samples != nullptr) {
*consumed_samples = static_cast<std::uint32_t>(consumed); *consumed_samples = static_cast<std::uint32_t>(consumed);
} }
return pushed; }(); return pushed;
}();
return finish_stream(status); 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->buffered_samples = stream->instance.buffered_samples();
out->oamd_payloads = info.oamd_payloads; out->oamd_payloads = info.oamd_payloads;
out->oamd_transitions = info.oamd_transitions; 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->output_channels = info.output_channels;
out->ended = info.ended; out->ended = info.ended;
return JOC_OK; return JOC_OK;
+11 -1
View File
@@ -80,6 +80,7 @@ struct Options {
bool dry_run = false; bool dry_run = false;
bool quiet = false; bool quiet = false;
bool help = false; bool help = false;
bool version = false;
}; };
const char* kLayoutChoices = const char* kLayoutChoices =
@@ -88,6 +89,8 @@ const char* kLayoutChoices =
void print_usage() { void print_usage() {
std::printf( std::printf(
"usage: joc_cli [options] input\n" "usage: joc_cli [options] input\n"
" -h, --help 显示本帮助\n"
" --version 显示版本并退出\n"
"\n" "\n"
"JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、扬声器 WAV 或双耳 WAV\n" "JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、扬声器 WAV 或双耳 WAV\n"
"\n" "\n"
@@ -129,7 +132,8 @@ void print_usage() {
" --trajectory-mode M ADM 对象轨迹表示 compact|dense64,默认 compact\n" " --trajectory-mode M ADM 对象轨迹表示 compact|dense64,默认 compact\n"
"\n" "\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-drc-scale X E-AC-3 解码器 -drc_scale,0=关闭码流 dynrng,默认 0\n"
" --eac3-target-level N E-AC-3 解码器 -target_level,0=不施加,默认 0\n" " --eac3-target-level N E-AC-3 解码器 -target_level,0=不施加,默认 0\n"
" --backend B JOC/扬声器 DSP 后端 auto|native;本构建无 python 后端\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) { for (int index = 1; index < argc; ++index) {
const std::string arg = arguments[static_cast<std::size_t>(index)]; const std::string arg = arguments[static_cast<std::size_t>(index)];
if (arg == "-h" || arg == "--help") { options->help = true; } 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 == "-o" || arg == "--output") { options->output = require_value(arguments, &index); }
else if (arg == "--speaker-output") { options->speaker_output = require_value(arguments, &index); } else if (arg == "--speaker-output") { options->speaker_output = require_value(arguments, &index); }
else if (arg == "--binaural-output") { options->binaural_output = require_value(arguments, &index); } else if (arg == "--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()); std::fprintf(stderr, "joc_cli: error: %s\n", error.what());
return 2; 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) { if (options.help) {
print_usage(); print_usage();
return 0; return 0;
+125 -29
View File
@@ -7,9 +7,111 @@
namespace joc::eac3 { namespace joc::eac3 {
namespace { namespace {
constexpr std::size_t kHeaderBytes = 4; 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 } // 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) { void FrameReader::push(const std::uint8_t* data, std::size_t size) {
if (failed_ || data == nullptr || size == 0) { if (failed_ || data == nullptr || size == 0) {
return; return;
@@ -45,44 +147,43 @@ FrameReader::Next FrameReader::next(Frame* out) {
} }
const std::uint8_t* p = buffer_.data() + consumed_; const std::uint8_t* p = buffer_.data() + consumed_;
// The reference implementation rejects a frame whose header does not fit, // A frame whose header does not fit is rejected rather than silently
// rather than silently resynchronising on the next 0x0B77. // resynchronised on the next 0x0B77.
if (available < kHeaderBytes) { if (available < kHeaderBytes) {
if (finished_) { 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::Fail;
} }
return Next::End; return Next::End;
} }
const std::uint16_t syncword = static_cast<std::uint16_t>((static_cast<std::uint16_t>(p[0]) << 8) | p[1]); Frame frame;
if (syncword != kSyncword) { if (!parse_syncframe_header(p, available, 0, &frame) || frame.size == 0u) {
fail(JOC_ERR_EAC3_SYNCFRAME, "invalid E-AC-3 syncword (silent resynchronisation is not allowed)"); if (available < kHeaderBytesAc3 && !finished_) {
return Next::End;
}
fail(JOC_ERR_EAC3_SYNCFRAME,
"invalid syncframe header (silent resynchronisation is not allowed)");
return Next::Fail; return Next::Fail;
} }
// frmsiz: 11 bits spread over the low 3 bits of byte 2 and all of byte 3, if (frame.size > available) {
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 (!finished_) { if (!finished_) {
return Next::End; return Next::End;
} }
fail(JOC_ERR_BITSTREAM_TRUNCATED, fail(JOC_ERR_BITSTREAM_TRUNCATED,
"last E-AC-3 syncframe extends past end of input (declared " + "last syncframe extends past end of input (declared " +
std::to_string(frame_bytes) + " bytes, remaining " + std::to_string(frame.size) + " bytes, remaining " +
std::to_string(available) + ")"); std::to_string(available) + ")");
return Next::Fail; return Next::Fail;
} }
if (out != nullptr) { if (out != nullptr) {
out->data = p; frame.data = p;
out->size = frame_bytes; frame.offset = base_offset_ + consumed_;
out->offset = base_offset_ + consumed_; *out = frame;
} }
consumed_ += frame_bytes; consumed_ += frame.size;
stream_offset_ = base_offset_ + consumed_; stream_offset_ = base_offset_ + consumed_;
++frames_emitted_; ++frames_emitted_;
return Next::Ok; 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) { if (data == nullptr || out_frame_bytes == nullptr) {
return JOC_ERR_INVALID_ARGUMENT; return JOC_ERR_INVALID_ARGUMENT;
} }
if (offset + kHeaderBytes > size) { Frame frame;
return JOC_ERR_EAC3_SYNCFRAME; 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]) != if (offset + frame.size > size) {
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) {
return JOC_ERR_BITSTREAM_TRUNCATED; return JOC_ERR_BITSTREAM_TRUNCATED;
} }
*out_frame_bytes = frame_bytes; *out_frame_bytes = frame.size;
return JOC_OK; return JOC_OK;
} }
+22 -1
View File
@@ -1,4 +1,3 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
@@ -10,12 +9,34 @@
namespace joc::eac3 { 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 { struct Frame {
const std::uint8_t* data = nullptr; const std::uint8_t* data = nullptr;
std::size_t size = 0; std::size_t size = 0;
std::size_t offset = 0; // byte offset of the frame start in the fed stream 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 { class FrameReader {
public: public:
enum class Next { 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()); : syntax_fail(reader.error_message());
} }
} }
std::uint8_t group_id = 0;
std::uint8_t has_group_id = 0;
if (reader.read(1) != 0u) { if (reader.read(1) != 0u) {
std::uint32_t ignored = 0; std::uint32_t value = 0;
if (!bits::variable_bits(reader, 2, 8, &ignored)) { if (!bits::variable_bits(reader, 2, 8, &value)) {
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader) return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
: syntax_fail(reader.error_message()); : syntax_fail(reader.error_message());
} }
group_id = static_cast<std::uint8_t>(value);
has_group_id = 1;
} }
if (reader.read(1) != 0u) { if (reader.read(1) != 0u) {
if (!reader.skip(8)) { 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++]; Payload& entry = container.payloads[container.payload_count++];
entry.id = static_cast<std::uint8_t>(payload_id); entry.id = static_cast<std::uint8_t>(payload_id);
entry.sample_offset = sample_offset; entry.sample_offset = sample_offset;
entry.group_id = group_id;
entry.has_group_id = has_group_id;
entry.bit_offset = reader.position(); entry.bit_offset = reader.position();
entry.size = payload_size; 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()); 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) { if (data == nullptr || out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null buffer or output"); 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) { 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, return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf,
"multiple top-level JOC EMDF containers (" + "multiple top-level JOC EMDF containers (" +
std::to_string(top_level.size()) + 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(); 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, void extract_container_bytes(const std::uint8_t* data, std::size_t size, const Container& container,
std::vector<std::uint8_t>* out) { std::vector<std::uint8_t>* out) {
out->assign(container.raw_size, 0u); out->assign(container.raw_size, 0u);
+25 -1
View File
@@ -20,6 +20,11 @@ struct Payload {
std::uint16_t sample_offset = 0; std::uint16_t sample_offset = 0;
std::size_t bit_offset = 0; // MSB-first bit position of the payload bytes std::size_t bit_offset = 0; // MSB-first bit position of the payload bytes
std::size_t size = 0; // payload byte count 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 { 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. // 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); 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 // Extract the container's bytes exactly as the bit reader sees them (identical
// to a memcpy for byte-aligned containers). // 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; value = (value + 1u) << width;
} }
// Same failure mode as the reference implementation: an extension chain // An extension chain that never terminates is a syntax error, not a
// that never terminates is a syntax error, not a truncation. // truncation.
reader.fail(JOC_ERR_EMDF_SYNTAX, "variable_bits extension groups exceeded"); reader.fail(JOC_ERR_EMDF_SYNTAX, "variable_bits extension groups exceeded");
return false; return false;
} }
+96 -1
View File
@@ -1,5 +1,6 @@
#include "foundation/fs_utf8.h" #include "foundation/fs_utf8.h"
#include <cstdlib>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
@@ -12,7 +13,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#else #else
#include <cstdlib>
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -20,6 +20,70 @@ namespace joc::fs_utf8 {
namespace fs = std::filesystem; 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) { fs::path to_path(const std::string& utf8) {
return fs::path(std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size())); return fs::path(std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size()));
} }
@@ -112,6 +176,37 @@ std::string executable_path() {
#endif #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) { std::ifstream open_input(const std::string& utf8_path) {
return std::ifstream(to_path(utf8_path), std::ios::binary); 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. // report it. Defaults are anchored here so they never depend on the CWD.
std::string executable_path(); 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 // Streams: std::ifstream/ofstream accept a std::filesystem::path, which is the
// portable way to open a UTF-8 path. // portable way to open a UTF-8 path.
std::ifstream open_input(const std::string& utf8_path); std::ifstream open_input(const std::string& utf8_path);
+46
View File
@@ -7,6 +7,8 @@ namespace joc::json {
namespace { namespace {
const char kHexDigits[] = "0123456789abcdef";
void skip_space(const std::string& text, std::size_t* index) { void skip_space(const std::string& text, std::size_t* index) {
while (*index < text.size() && while (*index < text.size() &&
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' || (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 } // 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) { bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
out->clear(); out->clear();
std::size_t index = 0; 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. // 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); 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); const Member* find(const std::vector<Member>& members, const std::string& key);
bool as_string(const Member& member, std::string* out); bool as_string(const Member& member, std::string* out);
+16
View File
@@ -33,6 +33,22 @@ private:
std::string message_; 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 // Stage names are kept as plain literals so that C++ and the Python frontend
namespace stage { namespace stage {
inline constexpr const char* kFoundation = "foundation"; inline constexpr const char* kFoundation = "foundation";
+5 -26
View File
@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include "foundation/fft.h" #include "foundation/fft.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h" #include "foundation/sha256.h"
#include "hrtf/public_filterbank.h" #include "hrtf/public_filterbank.h"
#include "io/npy_writer.h" #include "io/npy_writer.h"
@@ -651,32 +652,10 @@ Status convert_coordinates(const std::vector<double>& values, std::size_t rows,
// ------------------------------------------------------------- JSON --------- // ------------------------------------------------------------- JSON ---------
std::string json_escape(const std::string& text) { // The cache metadata is the compact form the reference writes
std::string out = "\""; // (json.dumps(sort_keys=True, separators=(",", ":"))), so only the escaping is
for (const char character : text) { // shared with the indented report and metadata documents.
const unsigned char value = static_cast<unsigned char>(character); std::string json_escape(const std::string& text) { return json::quote(text); }
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;
}
std::string json_number(double value) { return io::python_float_repr(value); } 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->num_objects_bits = static_cast<std::uint8_t>(reader.read(6));
out->ext_config_idx = static_cast<std::uint8_t>(reader.read(3)); 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); const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx);
if (n_channels < 0) { if (n_channels < 0) {
return syntax_fail("unknown JOC downmix configuration " + return syntax_fail("unknown JOC downmix configuration " +
std::to_string(out->dmx_config_idx)); std::to_string(out->dmx_config_idx));
} }
out->n_channels = static_cast<std::uint8_t>(n_channels); out->n_channels = static_cast<std::uint8_t>(n_channels);
if (n_objects > JOC_MAX_OBJECTS) { // A declared object count above 15 is clamped to 15: the frame is parsed for
// The reference implementation has no check here and fails later inside // 15 objects only, and the clamp is reported separately.
// NumPy; the port reports it explicitly (plan 28.2). out->objects_clamped = declared_objects > JOC_MAX_OBJECTS ? 1u : 0u;
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kJoc, const std::uint32_t n_objects =
"JOC frame declares " + std::to_string(n_objects) + std::min<std::uint32_t>(declared_objects, JOC_MAX_OBJECTS);
" objects, the ABI supports at most " +
std::to_string(static_cast<int>(JOC_MAX_OBJECTS)));
}
out->n_objects = static_cast<std::uint8_t>(n_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_x_bits = static_cast<std::uint8_t>(reader.read(3));
out->clipgain_y_bits = static_cast<std::uint8_t>(reader.read(5)); out->clipgain_y_bits = static_cast<std::uint8_t>(reader.read(5));
out->seq_count = reader.read(10); out->seq_count = reader.read(10);
// clipgain = 1 + (y/32) * 2^(x-4). The reference multiplies by an exact // clipgain = 1 + (y/32) * 2^(x-4); ldexp builds the power of two exactly
// exactly for the whole legal range. // over the whole legal range.
out->clipgain = 1.0 + static_cast<double>(out->clipgain_y_bits) / 32.0 * 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); 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(); 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, Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_frame_params* out,
emdf::Container* container, FrameSymbols* symbols) { emdf::Container* container, FrameSymbols* symbols) {
if (frame == nullptr || out == nullptr) { if (frame == nullptr || out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null frame or output"); return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null frame or output");
} }
emdf::Container local; 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()) { if (!status.ok()) {
return status; return status;
} }
@@ -412,4 +432,516 @@ Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
return Status::success(); 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 } // namespace joc::joc
+93
View File
@@ -6,9 +6,11 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "eac3_transport/eac3_reader.h"
#include "joc_core.h" #include "joc_core.h"
#include "emdf/emdf_parser.h" #include "emdf/emdf_parser.h"
#include "foundation/status.h" #include "foundation/status.h"
#include "oamd/oamd_parser.h"
namespace joc::joc { 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, Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
std::uint32_t* out_trailing_bits); 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_channels_for_config(std::uint32_t dmx_config_idx);
std::int16_t num_bands_for_index(std::uint32_t num_bands_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 } // namespace joc::joc
+64 -49
View File
@@ -69,7 +69,9 @@ public:
std::memset(synthesis_state_, 0, sizeof(synthesis_state_)); std::memset(synthesis_state_, 0, sizeof(synthesis_state_));
std::memset(x_, 0, sizeof(x_)); std::memset(x_, 0, sizeof(x_));
std::memset(z_, 0, sizeof(z_)); 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'; error_[0] = '\0';
return 0; return 0;
} }
@@ -125,13 +127,11 @@ public:
return error_[0] ? error_ : ""; return error_[0] ? error_ : "";
} }
void set_dc_filter(const bool enabled) noexcept {
dc_filter_enabled_ = enabled;
}
int process( int process(
const float* bed5, const float* bed_planar,
const float* lfe, const float* lfe,
const uint32_t core_channel_count,
const uint32_t phase_shift,
const uint32_t object_mask, const uint32_t object_mask,
const uint8_t* n_bands, const uint8_t* n_bands,
const uint8_t* n_dpoints, const uint8_t* n_dpoints,
@@ -144,9 +144,14 @@ public:
float* output16) noexcept { float* output16) noexcept {
error_[0] = '\0'; 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"); 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)) { if (object_mask & ~((1u << EJOC_MAX_OBJECTS) - 1u)) {
return fail("object_mask contains an object index above 14"); return fail("object_mask contains an object index above 14");
} }
@@ -169,7 +174,7 @@ public:
} }
std::memset(output16, 0, sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); 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); render_lfe(lfe, output_scale, output16);
process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts, process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts,
dq, clipgain, output_scale, output16); dq, clipgain, output_scale, output16);
@@ -299,7 +304,8 @@ private:
for (int band = 0; band < 64; ++band) { for (int band = 0; band < 64; ++band) {
current[slot][band] = x_[channel][band][group + slot]; current[slot][band] = x_[channel][band][group + slot];
const Complex delayed = surround_delay_[surround][slot][band]; const Complex delayed = surround_delay_[surround][slot][band];
x_[channel][band][group + slot] = {delayed.im, -delayed.re}; x_[channel][band][group + slot] =
phase_shift_enabled_ ? Complex{delayed.im, -delayed.re} : delayed;
} }
} }
@@ -309,7 +315,7 @@ private:
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
dc_buffer[20 + i] = current[i][0]; dc_buffer[20 + i] = current[i][0];
} }
if (dc_filter_enabled_) { if (phase_shift_enabled_) {
for (int slot = 0; slot < 4; ++slot) { for (int slot = 0; slot < 4; ++slot) {
Complex sum{0.0, 0.0}; Complex sum{0.0, 0.0};
for (int tap = 0; tap < 21; ++tap) { for (int tap = 0; tap < 21; ++tap) {
@@ -338,7 +344,7 @@ private:
} }
void analysis_channel(const int channel) noexcept { 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_old = job_phase_old_;
const float phase_new = job_phase_new_; const float phase_new = job_phase_new_;
const bool ramp_phase = phase_old != phase_new; const bool ramp_phase = phase_old != phase_new;
@@ -354,37 +360,49 @@ private:
const float product = static_cast<float>(static_cast<float>(frame_sample) * phase_step); const float product = static_cast<float>(static_cast<float>(frame_sample) * phase_step);
gain = static_cast<float>(phase_old + product); 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; const float* analysis_input = scaled;
if (channel < 3) { const int delay_lane = channel < 3 ? channel : channel - 2;
std::memcpy(delayed, analysis_delay_[channel][0], sizeof(delayed)); if (channel < 3 || channel >= 5) {
std::memmove(&analysis_delay_[channel][0][0], std::memcpy(delayed, analysis_delay_[delay_lane][0], sizeof(delayed));
&analysis_delay_[channel][1][0], std::memmove(&analysis_delay_[delay_lane][0][0],
&analysis_delay_[delay_lane][1][0],
sizeof(float) * 9 * 64); 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_input = delayed;
} }
analysis_slot(channel, analysis_input, timeslot); analysis_slot(channel, analysis_input, timeslot);
} }
} }
void analysis(const float* bed5, const float phase_new) noexcept { void analysis(const float* bed_planar, const float phase_new) noexcept {
job_bed5_ = bed5; 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_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_kind_ = JobKind::Analysis;
job_count_ = 5; job_count_ = static_cast<int>(core_channels_);
for (int channel = 0; channel < 5; ++channel) { for (uint32_t channel = 0; channel < core_channels_; ++channel) {
job_objects_[channel] = channel; job_objects_[channel] = static_cast<int>(channel);
} }
dispatch_jobs(); dispatch_jobs();
analysis_phase_ = phase_new; analysis_phase_ = scalar;
surround_post(); surround_post();
} }
static std::size_t dq_index(const int object, const int point, const int channel, const int band) noexcept { 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( void matrix_object(
@@ -401,7 +419,7 @@ private:
const int slope = slope_idx[object]; const int slope = slope_idx[object];
const uint8_t* pb_map = parameter_band_map(bands); 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) { for (int subband = 0; subband < 64; ++subband) {
const int parameter_band = pb_map[subband]; const int parameter_band = pb_map[subband];
const double previous = matrix_previous_[object][channel][subband]; const double previous = matrix_previous_[object][channel][subband];
@@ -465,7 +483,6 @@ private:
void synthesis_object( void synthesis_object(
const int object, const int object,
const double clipgain,
const float output_scale, const float output_scale,
float* output16) noexcept { float* output16) noexcept {
@@ -493,8 +510,10 @@ private:
qmf5_step(state, rotated, pcm64); qmf5_step(state, rotated, pcm64);
for (int sample = 0; sample < 64; ++sample) { for (int sample = 0; sample < 64; ++sample) {
const double clipped = clamp_unit(16.0 * pcm64[sample]); const double clipped = clamp_unit(16.0 * pcm64[sample]);
const float value = static_cast<float>(clipped * clipgain); // joc_clipgain already entered the analysis (see analysis()); what
destination[timeslot * 64 + sample] = static_cast<float>(value * output_scale); // remains here is the user output gain.
const float value = static_cast<float>(clipped * output_scale);
destination[timeslot * 64 + sample] = value;
} }
} }
} }
@@ -502,7 +521,7 @@ private:
void process_one_object(const int object) noexcept { void process_one_object(const int object) noexcept {
matrix_object(object, job_n_bands_, job_n_dpoints_, job_slope_idx_, matrix_object(object, job_n_bands_, job_n_dpoints_, job_slope_idx_,
job_offset_ts_, job_dq_); 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 { void execute_job_loop() noexcept {
@@ -630,7 +649,7 @@ private:
int job_objects_[EJOC_MAX_OBJECTS]{}; int job_objects_[EJOC_MAX_OBJECTS]{};
int job_count_ = 0; int job_count_ = 0;
JobKind job_kind_ = JobKind::Objects; JobKind job_kind_ = JobKind::Objects;
const float* job_bed5_ = nullptr; const float* job_bed_ = nullptr;
float job_phase_old_ = 0.0625f; float job_phase_old_ = 0.0625f;
float job_phase_new_ = 0.0625f; float job_phase_new_ = 0.0625f;
const uint8_t* job_n_bands_ = nullptr; const uint8_t* job_n_bands_ = nullptr;
@@ -642,17 +661,19 @@ private:
float job_output_scale_ = 1.0f; float job_output_scale_ = 1.0f;
float* job_output16_ = nullptr; float* job_output16_ = nullptr;
alignas(64) double analysis_fifo_[5][9][64]; uint32_t core_channels_ = EJOC_CORE_CHANNELS;
alignas(64) float analysis_delay_[3][10][64]; alignas(64) double analysis_fifo_[EJOC_MAX_CORE_CHANNELS][9][64];
float analysis_phase_; // 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_delay_[2][10][64];
alignas(64) Complex surround_history_[2][20]; alignas(64) Complex surround_history_[2][20];
// band-0 的 21-tap DC 补偿开关;仅 downmix 配置 3/4 由调用方置位。 // sigma(k) switch; the caller sets it for downmix configurations 3 and 4.
bool dc_filter_enabled_ = true; bool phase_shift_enabled_ = true;
alignas(64) double lfe_delay_[kLfeDelay]; 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) 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]; alignas(64) Complex z_[15][64][24];
uint8_t bit_reverse_[64]; uint8_t bit_reverse_[64];
@@ -704,14 +725,6 @@ int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t to
return static_cast<ejoc::Renderer*>(handle)->set_threads(total_threads); return static_cast<ejoc::Renderer*>(handle)->set_threads(total_threads);
} }
int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled) {
if (!handle) {
return -1;
}
static_cast<ejoc::Renderer*>(handle)->set_dc_filter(enabled != 0);
return 0;
}
uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle) { uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle) {
if (!handle) { if (!handle) {
return 0; return 0;
@@ -728,8 +741,10 @@ const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle) {
int EJOC_CALL ejoc_renderer_process( int EJOC_CALL ejoc_renderer_process(
ejoc_renderer_handle handle, ejoc_renderer_handle handle,
const float* bed5_planar, const float* bed_planar,
const float* lfe, const float* lfe,
const uint32_t core_channel_count,
const uint32_t phase_shift,
const uint32_t object_mask, const uint32_t object_mask,
const uint8_t* n_bands, const uint8_t* n_bands,
const uint8_t* n_dpoints, const uint8_t* n_dpoints,
@@ -744,8 +759,8 @@ int EJOC_CALL ejoc_renderer_process(
return -1; return -1;
} }
return static_cast<ejoc::Renderer*>(handle)->process( return static_cast<ejoc::Renderer*>(handle)->process(
bed5_planar, lfe, object_mask, n_bands, n_dpoints, slope_idx, bed_planar, lfe, core_channel_count, phase_shift, object_mask, n_bands, n_dpoints,
offset_ts, dq, clipgain, phase_new, output_scale, output16_planar); slope_idx, offset_ts, dq, clipgain, phase_new, output_scale, output16_planar);
} }
} // extern "C" } // extern "C"
+16 -18
View File
@@ -6,21 +6,22 @@
namespace joc::joc { namespace joc::joc {
Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params, 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) { 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"); 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) { 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)); std::to_string(static_cast<unsigned>(params.n_channels));
} }
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kDsp, *error); 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 * 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); 0.0);
std::uint8_t n_bands[JOC_MAX_OBJECTS] = {}; std::uint8_t n_bands[JOC_MAX_OBJECTS] = {};
std::uint8_t n_dpoints[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]; offset_ts[obj * JOC_MAX_DPOINTS + dp] = object.offset_ts[dp];
} }
for (unsigned dp = 0; dp < object.n_dpoints; ++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) { for (unsigned pb = 0; pb < object.n_bands; ++pb) {
const std::size_t index = 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) * ch) *
JOC_MAX_PARAMETER_BANDS + JOC_MAX_PARAMETER_BANDS +
pb; pb;
@@ -52,18 +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); out16->assign(static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f);
// band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用,其余配置 band 0 // sigma(k) = (k in {3,4}) gates the Ls/Rs -j rotation and the band-0 21-tap
// 走与其他 band 相同的处理。 // DC compensation together; other configurations keep the common processing.
const bool dc_filter = params.dmx_config_idx == 3 || params.dmx_config_idx == 4; const std::uint32_t phase_shift =
if (ejoc_renderer_set_dc_filter(handle, dc_filter ? 1u : 0u) != 0) { (params.dmx_config_idx == 3 || params.dmx_config_idx == 4) ? 1u : 0u;
if (error != nullptr) {
*error = "ejoc_renderer_set_dc_filter failed";
}
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kDsp, "ejoc_renderer_set_dc_filter failed");
}
const int result = ejoc_renderer_process( const int result = ejoc_renderer_process(
handle, bed5_planar, lfe, params.present_mask, n_bands, n_dpoints, slope_idx, offset_ts, handle, bed_planar, lfe, params.n_channels, phase_shift, params.present_mask, n_bands,
dq.data(), params.clipgain, 0.0625f, gain, out16->data()); n_dpoints, slope_idx, offset_ts, dq.data(), params.clipgain, 0.0625f, gain,
out16->data());
if (result != 0) { if (result != 0) {
const char* detail = ejoc_renderer_last_error(handle); const char* detail = ejoc_renderer_last_error(handle);
const std::string message = const std::string message =
+3 -2
View File
@@ -10,9 +10,10 @@
namespace joc::joc { 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, 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); std::vector<float>* out16, std::string* error);
} // namespace joc::joc } // 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); 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, Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out,
ParseTrace* trace) { ParseTrace* trace) {
if (payload == nullptr || out == nullptr) { 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, Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out,
ParseTrace* trace); 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 } // namespace joc::oamd
+7 -6
View File
@@ -7,10 +7,10 @@
#include "simd/cpu_probe.h" #include "simd/cpu_probe.h"
#if defined(_M_X64) #if defined(_M_X64) || defined(_M_IX86)
#include <immintrin.h> #include <immintrin.h>
#include <intrin.h> #include <intrin.h>
#elif defined(__x86_64__) #elif defined(__x86_64__) || defined(__i386__)
#include <cpuid.h> #include <cpuid.h>
#endif #endif
@@ -21,10 +21,11 @@
namespace joc::simd { namespace joc::simd {
namespace { namespace {
// ------------------------------------------------------------------- x86-64 -- // ---------------------------------------------------------------------- x86 --
#if defined(_M_X64) || defined(__x86_64__) // 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 // 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 // state the wider registers need. Both have to agree, or the first AVX
@@ -53,7 +54,7 @@ CpuFeatures probe_x86() noexcept {
return features; 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 // 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 // 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 { namespace {
constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES; 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 } // namespace
@@ -37,8 +34,19 @@ Stream::~Stream() {
void Stream::reset_state() { void Stream::reset_state() {
reader_ = eac3::FrameReader(); reader_ = eac3::FrameReader();
metadata_.clear(); 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_pending_.clear();
bed_read_offset_ = 0;
objects16_.clear(); objects16_.clear();
objects_pending_.clear();
objects_read_offset_ = 0;
output_.clear(); output_.clear();
read_offset_ = 0; read_offset_ = 0;
info_ = Info(); info_ = Info();
@@ -118,7 +126,6 @@ Status Stream::create(const Config& config) {
} }
render_options.object_delay_samples = config_.object_delay_samples; render_options.object_delay_samples = config_.object_delay_samples;
render_options.tail_seconds = config_.tail_seconds; render_options.tail_seconds = config_.tail_seconds;
render_options.output_gain = 1.0;
if (status.ok()) { if (status.ok()) {
status = rosella_.open(model, render_options); status = rosella_.open(model, render_options);
} }
@@ -179,6 +186,43 @@ Status Stream::create(const Config& config) {
return Status::success(); 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) { Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed) {
if (rebuilder_ == nullptr) { if (rebuilder_ == nullptr) {
return Status::fail(JOC_ERR_STATE, stage::kEmdf, "stream is not an E-AC-3 input"); 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) { if (state == eac3::FrameReader::Next::Fail) {
return Status::fail(reader_.error(), stage::kEac3, reader_.error_message()); 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; 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 = 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()) { if (!parsed.ok()) {
return parsed; 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)) { if (const emdf::Payload* payload = container.find(emdf::kIdOamd)) {
std::vector<std::uint8_t> bytes; std::vector<std::uint8_t> bytes;
const Status extracted = 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()) { if (!extracted.ok()) {
return extracted; 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()) { if (!oamd.ok()) {
return oamd; return oamd;
} }
entry.has_update = true; pending_frame_.has_update = true;
entry.outer_offset = static_cast<std::int64_t>(payload->sample_offset); 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) { if (rebuilder_ == nullptr) {
return Status::fail(JOC_ERR_STATE, stage::kDsp, "stream is not an E-AC-3 input"); 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) { if (consumed != nullptr) {
*consumed = samples; *consumed = samples;
} }
if (interleaved6 != nullptr && samples != 0u) { if (interleaved != nullptr && samples != 0u) {
bed_pending_.insert(bed_pending_.end(), interleaved6, bed_pending_.insert(bed_pending_.end(), interleaved,
interleaved6 + samples * kBedChannels); 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) { 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) { if (planar16 == nullptr || samples == 0u) {
return Status::success(); 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) { for (std::size_t offset = 0; offset < samples; offset += kFrameSamples) {
const std::size_t count = std::min(kFrameSamples, samples - offset); const std::size_t count = std::min(kFrameSamples, samples - offset);
std::vector<float> frame(static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples, const std::size_t base = objects_pending_.size();
0.0f); 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) { for (std::size_t channel = 0; channel < JOC_OUTPUT_CHANNELS; ++channel) {
std::memcpy(frame.data() + channel * kFrameSamples, std::memcpy(objects_pending_.data() + base + channel * kFrameSamples,
planar16 + channel * samples + offset, count * sizeof(float)); frame + channel * kFrameSamples, count * sizeof(float));
} }
++info_.frames_in; }
info_.samples_in += count; return process_objects16_frames(false);
const Status rendered = render_objects16(frame); }
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()) { if (!rendered.ok()) {
return rendered; return rendered;
} }
if (count != kFrameSamples) { ++info_.frames_in;
break; // a partial frame is dropped; the host should push whole frames 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(); return Status::success();
} }
Status Stream::process_ready_frames() { Status Stream::process_ready_frames(bool drain_all) {
while (bed_pending_.size() / kBedChannels >= kFrameSamples && !metadata_.empty()) { if (bed_plan_failed_) {
const FrameMetadata entry = metadata_.front(); 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(); metadata_.pop_front();
std::vector<float> bed5(static_cast<std::size_t>(JOC_CORE_CHANNELS) * kFrameSamples, 0.0f); const int core_channels = entry.has_joc ? static_cast<int>(entry.params.n_channels)
std::vector<float> lfe(kFrameSamples, 0.0f); : core_channels_;
for (std::size_t sample = 0; sample < kFrameSamples; ++sample) { if (core_channels <= 0) {
for (std::size_t channel = 0; channel < JOC_CORE_CHANNELS; ++channel) { return Status::fail(JOC_ERR_STATE, stage::kDsp,
bed5[channel * kFrameSamples + sample] = "the JOC core channel count is not known yet");
bed_pending_[sample * kBedChannels + kCoreChannels[channel]];
}
lfe[sample] = bed_pending_[sample * kBedChannels + kLfeChannel];
} }
bed_pending_.erase(bed_pending_.begin(), if (!entry.has_joc) {
bed_pending_.begin() + static_cast<std::ptrdiff_t>(kFrameSamples * entry.params.dmx_config_idx = static_cast<std::uint8_t>(config_idx_);
kBedChannels)); 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; std::string error;
const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5.data(), const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5_.data(),
lfe.data(), gain_, &objects16_, &error); lfe_.data(), gain_, &objects16_, &error);
if (!rebuilt.ok()) { if (!rebuilt.ok()) {
return Status::fail(rebuilt.code(), stage::kDsp, error); return Status::fail(rebuilt.code(), stage::kDsp, error);
} }
@@ -303,6 +518,22 @@ Status Stream::process_ready_frames() {
return Status::success(); 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) { Status Stream::render_objects16(const std::vector<float>& objects16) {
if (config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) { if (config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) {
output_.insert(output_.end(), objects16.begin(), objects16.end()); output_.insert(output_.end(), objects16.begin(), objects16.end());
@@ -320,6 +551,7 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
if (!stepped.ok()) { if (!stepped.ok()) {
return Status::fail(stepped.code(), stage::kRender, error); return Status::fail(stepped.code(), stage::kRender, error);
} }
output_.reserve(output_.size() + speaker_.output.size());
for (const double value : speaker_.output) { for (const double value : speaker_.output) {
output_.push_back(static_cast<float>(value)); output_.push_back(static_cast<float>(value));
} }
@@ -340,13 +572,13 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
if (!submitted.ok()) { if (!submitted.ok()) {
return submitted; return submitted;
} }
std::vector<double> produced; binaural_.take_output(&produced_);
binaural_.take_output(&produced); output_.reserve(output_.size() + produced_.size());
for (const double value : produced) { for (const double value : produced_) {
output_.push_back(static_cast<float>(value)); output_.push_back(static_cast<float>(value));
} }
info_.frames_out++; info_.frames_out++;
info_.samples_out += produced.size() / 2u; info_.samples_out += produced_.size() / 2u;
return Status::success(); return Status::success();
} }
@@ -367,16 +599,15 @@ Status Stream::render_rosella_objects16(const std::vector<float>& objects16) {
if (!submitted.ok()) { if (!submitted.ok()) {
return submitted; return submitted;
} }
std::vector<double> produced; rosella_.take_output(&produced_);
rosella_.take_output(&produced); if (!produced_.empty()) {
if (!produced.empty()) { rosella_pending_.insert(rosella_pending_.end(), produced_.begin(), produced_.end());
rosella_pending_.insert(rosella_pending_.end(), produced.begin(), produced.end());
} }
release_rosella_output(kFrameSamples); release_rosella_output(kFrameSamples);
info_.frames_out++; info_.frames_out++;
// Counted as the runtime produces it, which is also how the SOFA path counts: // 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. // 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(); return Status::success();
} }
@@ -389,6 +620,7 @@ void Stream::release_rosella_output(std::size_t limit) {
return; return;
} }
const std::size_t values = count * 2u; const std::size_t values = count * 2u;
output_.reserve(output_.size() + values);
for (std::size_t index = 0; index < values; ++index) { for (std::size_t index = 0; index < values; ++index) {
output_.push_back(static_cast<float>(rosella_pending_[rosella_read_offset_ + 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() { 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_) { if (binaural_ready_) {
std::vector<double> tail; std::vector<double> tail;
const Status drained = const Status drained =
@@ -437,6 +683,7 @@ Status Stream::flush() {
if (!drained.ok()) { if (!drained.ok()) {
return drained; return drained;
} }
output_.reserve(output_.size() + tail.size());
for (const double value : tail) { for (const double value : tail) {
output_.push_back(static_cast<float>(value)); 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 // tail only sounds after it. The program samples were already counted by
// render_rosella_objects16, so only the tail is added here. // render_rosella_objects16, so only the tail is added here.
release_rosella_output(rosella_pending_samples()); release_rosella_output(rosella_pending_samples());
output_.reserve(output_.size() + tail.size());
for (const double value : tail) { for (const double value : tail) {
output_.push_back(static_cast<float>(value)); output_.push_back(static_cast<float>(value));
} }
+59 -4
View File
@@ -10,6 +10,7 @@
#include "eac3_transport/eac3_reader.h" #include "eac3_transport/eac3_reader.h"
#include "foundation/status.h" #include "foundation/status.h"
#include "hrtf/rosella_renderer.h" #include "hrtf/rosella_renderer.h"
#include "joc_bitstream/joc_parser.h"
#include "joc_core.h" #include "joc_core.h"
#include "joc_stream.h" #include "joc_stream.h"
#include "oamd/oamd_parser.h" #include "oamd/oamd_parser.h"
@@ -46,16 +47,41 @@ struct Info {
std::uint64_t bytes_in = 0; std::uint64_t bytes_in = 0;
std::uint64_t oamd_payloads = 0; std::uint64_t oamd_payloads = 0;
std::uint64_t oamd_transitions = 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 output_channels = 0;
std::uint32_t ended = 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 { struct FrameMetadata {
joc_frame_params params{}; joc_frame_params params{};
oamd::OamdUpdate update{}; oamd::OamdUpdate update{};
bool has_update = false; bool has_update = false;
std::int64_t outer_offset = 0; 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 { class Stream {
@@ -68,7 +94,8 @@ public:
Status create(const Config& config); Status create(const Config& config);
Status push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed); 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 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 pull(float* destination, std::size_t capacity_samples, std::size_t* produced);
Status flush(); Status flush();
@@ -85,8 +112,13 @@ public:
: 0u; : 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: 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_objects16(const std::vector<float>& objects16);
Status render_rosella_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 // 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; return rosella_ready_ ? (rosella_pending_.size() - rosella_read_offset_) / 2u : 0u;
} }
void reset_state(); 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_; Config config_;
Info info_; Info info_;
eac3::FrameReader reader_; eac3::FrameReader reader_;
std::deque<FrameMetadata> metadata_; std::deque<FrameMetadata> metadata_;
FrameMetadata pending_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<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> objects16_;
std::vector<float> output_; std::vector<float> output_;
std::size_t read_offset_ = 0; std::size_t read_offset_ = 0;
@@ -115,6 +169,7 @@ private:
hrtf::RosellaRuntime rosella_; hrtf::RosellaRuntime rosella_;
std::vector<double> rosella_pending_; std::vector<double> rosella_pending_;
std::size_t rosella_read_offset_ = 0; std::size_t rosella_read_offset_ = 0;
std::vector<double> produced_;
bool speaker_enabled_ = false; bool speaker_enabled_ = false;
bool binaural_enabled_ = false; bool binaural_enabled_ = false;
bool binaural_ready_ = false; bool binaural_ready_ = false;
+441 -104
View File
@@ -8,6 +8,7 @@
#include <condition_variable> #include <condition_variable>
#include <cstdarg> #include <cstdarg>
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
@@ -30,6 +31,7 @@
#include "binaural/binaural_runtime.h" #include "binaural/binaural_runtime.h"
#include "eac3_transport/eac3_reader.h" #include "eac3_transport/eac3_reader.h"
#include "emdf/emdf_parser.h" #include "emdf/emdf_parser.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h" #include "foundation/sha256.h"
#include "hrtf/jochrtf.h" #include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.h" #include "hrtf/rosella_model.h"
@@ -51,11 +53,8 @@ namespace {
namespace fs = std::filesystem; namespace fs = std::filesystem;
using Clock = std::chrono::steady_clock; 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 std::size_t kReadChunk = 1u << 20;
constexpr double kDefaultTailThreshold = 1.0e-8; 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) { std::string lower(std::string text) {
for (char& c : text) { for (char& c : text) {
@@ -81,7 +80,12 @@ std::string fmt(const char* format, ...) {
} }
struct Settings { 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 bed_path;
std::string work_dir; std::string work_dir;
std::string output_path; std::string output_path;
@@ -119,7 +123,19 @@ struct Settings {
Settings normalise(const joc_task_config& raw) { Settings normalise(const joc_task_config& raw) {
Settings cfg; Settings cfg;
if (raw.input_path != nullptr) { cfg.input_path = raw.input_path; } 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.bed_path != nullptr) { cfg.bed_path = raw.bed_path; }
if (raw.work_dir != nullptr) { cfg.work_dir = raw.work_dir; } if (raw.work_dir != nullptr) { cfg.work_dir = raw.work_dir; }
if (raw.output_path != nullptr) { cfg.output_path = raw.output_path; } 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); 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::uint64_t count_frames(const std::string& path) {
std::ifstream input = fs_utf8::open_input(path); std::ifstream input = fs_utf8::open_input(path);
if (!input) { if (!input) {
@@ -200,6 +219,7 @@ std::uint64_t count_frames(const std::string& path) {
std::vector<char> chunk(kReadChunk); std::vector<char> chunk(kReadChunk);
std::uint64_t frames = 0; std::uint64_t frames = 0;
bool done = false; bool done = false;
std::vector<::joc::eac3::Frame> group;
while (!done) { while (!done) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size())); input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount(); const std::streamsize got = input.gcount();
@@ -213,16 +233,29 @@ std::uint64_t count_frames(const std::string& path) {
for (;;) { for (;;) {
::joc::eac3::Frame frame; ::joc::eac3::Frame frame;
const ::joc::eac3::FrameReader::Next state = reader.next(&frame); const ::joc::eac3::FrameReader::Next state = reader.next(&frame);
if (state == ::joc::eac3::FrameReader::Next::Ok) { if (state == ::joc::eac3::FrameReader::Next::End) {
++frames; break;
continue;
} }
if (state == ::joc::eac3::FrameReader::Next::Fail) { 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; return frames;
} }
@@ -234,6 +267,53 @@ bool stdin_is_interactive() {
#endif #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 // 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 // once the peak is known, which is what makes --clip-action possible; the
// reference CLI spools for the same reason. // 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"); 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 // Parses the bitstream without decoding or rendering: --metadata-only and the
// diagnostic metadata reports. // diagnostic metadata reports.
Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std::string& eac3_path, 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; double clipgain_max = 0.0;
bool stream_done = false; bool stream_done = false;
Status failure = Status::success(); Status failure = Status::success();
std::vector<::joc::eac3::Frame> group;
while (!stream_done && frames < total_frames) { while (!stream_done && frames < total_frames) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size())); input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount(); const std::streamsize got = input.gcount();
@@ -505,8 +565,36 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
stream_done = true; stream_done = true;
break; 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; ::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, const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size, &params,
&container, nullptr); &container, nullptr);
if (!parsed.ok()) { 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)); static_cast<unsigned long long>(oamd_payloads));
} }
if (!cfg.metadata_json_path.empty()) { if (!cfg.metadata_json_path.empty()) {
std::string json = fmt( const std::string document = json::pretty_object({
"{\n \"input\": \"%s\",\n \"frames\": %llu,\n \"objects_min\": %u,\n" {"input", json::quote(cfg.input_path)},
" \"objects_mean\": %.6f,\n \"objects_max\": %u,\n \"clipgain_min\": %.9g,\n" {"frames", std::to_string(frames)},
" \"clipgain_max\": %.9g,\n \"oamd_payloads\": %llu\n}\n", {"objects_min", std::to_string(objects_min == 0xFFFFFFFFu ? 0u : objects_min)},
json_escape(cfg.input_path).c_str(), static_cast<unsigned long long>(frames), {"objects_mean", fmt("%.6f", mean_objects)},
objects_min == 0xFFFFFFFFu ? 0u : objects_min, mean_objects, objects_max, {"objects_max", std::to_string(objects_max)},
clipgain_min, clipgain_max, static_cast<unsigned long long>(oamd_payloads)); {"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); std::ofstream output = fs_utf8::open_output(cfg.metadata_json_path);
if (!output) { if (!output) {
return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput, return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput,
"cannot write " + cfg.metadata_json_path); "cannot write " + cfg.metadata_json_path);
} }
output << json; output << document << "\n";
} }
bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO, bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO,
fmt("metadata: %llu frames, oamd payloads %llu", fmt("metadata: %llu frames, oamd payloads %llu",
@@ -592,35 +683,51 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
return failure; 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 { class BedSource {
public: 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); file_.open(path, std::ios::binary);
return static_cast<bool>(file_); return static_cast<bool>(file_);
} }
bool read_frame(std::uint64_t index, std::vector<float>* bed5, std::vector<float>* lfe) { bool read_frame(std::uint64_t index, int core_channels, const ::joc::joc::BedPlan& plan,
bed5->assign(static_cast<std::size_t>(JOC_CORE_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f); 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); 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_.clear();
file_.seekg(static_cast<std::streamoff>(index * kBedFrameBytes), std::ios::beg); file_.seekg(static_cast<std::streamoff>(index * frame_values * sizeof(float)),
std::vector<float> interleaved(static_cast<std::size_t>(JOC_FRAME_SAMPLES) * 6u); std::ios::beg);
file_.read(reinterpret_cast<char*>(interleaved.data()), file_.read(reinterpret_cast<char*>(interleaved_.data()),
static_cast<std::streamsize>(interleaved.size() * sizeof(float))); static_cast<std::streamsize>(frame_values * sizeof(float)));
if (file_.gcount() != static_cast<std::streamsize>(interleaved.size() * sizeof(float))) { if (file_.gcount() != static_cast<std::streamsize>(frame_values * sizeof(float))) {
return false; return false;
} }
for (std::size_t n = 0; n < JOC_FRAME_SAMPLES; ++n) { for (std::size_t n = 0; n < JOC_FRAME_SAMPLES; ++n) {
for (std::size_t c = 0; c < JOC_CORE_CHANNELS; ++c) { for (int lane = 0; lane < core_channels; ++lane) {
(*bed5)[c * JOC_FRAME_SAMPLES + n] = interleaved[n * 6u + kCoreChannels[c]]; 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; return true;
} }
private: private:
std::ifstream file_; 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", warn(JOC_ERR_INVALID_CONFIG, "native_threads",
"native_threads is clamped to the 15 JOC objects"); "native_threads is clamped to the 15 JOC objects");
} }
if (cfg.bed_path.empty() && !fs_utf8::exists(cfg.ffmpeg_path)) { // ffmpeg is needed exactly where the task shells out to it: unpacking a container,
warn(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path", // and decoding the core PCM when no bed is supplied. An unresolvable executable is
"bed PCM is not supplied, so ffmpeg must be reachable: " + cfg.ffmpeg_path); // 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) { if (error_count != nullptr) {
*error_count = errors; *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); const std::string lowered = lower(cfg.input_path);
if (!ends_with(lowered, ".eac3") && !ends_with(lowered, ".ec3")) { if (!ends_with(lowered, ".eac3") && !ends_with(lowered, ".ec3")) {
temp_dir = create_temp_dir(cfg); 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"); bus.stage(JOC_STAGE_INPUT, "extracting the E-AC-3 elementary stream");
const io::ProcessResult extracted = [&] { const io::ProcessResult extracted = [&] {
io::ProcessResult result; 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; std::string bed_path = cfg.bed_path;
if (bed_path.empty() && !cfg.metadata_only) { if (bed_path.empty() && !cfg.metadata_only) {
if (temp_dir.empty()) { if (temp_dir.empty()) {
temp_dir = create_temp_dir(cfg); temp_dir = create_temp_dir(cfg);
} }
bed_path = temp_dir + "\\core51_f32le.raw"; bed_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "core_f32le.raw");
bus.stage(JOC_STAGE_DECODE, "decoding the 5.1 core PCM with ffmpeg"); // 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", std::vector<std::string> argv = {cfg.ffmpeg_path, "-hide_banner", "-loglevel", "error", "-y",
"-i", eac3_path}; "-i", eac3_path};
argv.insert(argv.end(), {"-map", "0:a:0", "-vn", "-ac", "6", "-ar", "48000", argv.insert(argv.end(), {"-map", "0:a:0", "-vn"});
"-c:a", "pcm_f32le", "-f", "f32le", bed_path}); 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)}); argv.insert(argv.begin() + 4, {"-drc_scale", fmt("%.10g", cfg.drc_scale)});
if (cfg.target_level != 0) { if (cfg.target_level != 0) {
argv.insert(argv.begin() + 4, {"-target_level", std::to_string(cfg.target_level)}); 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.object_delay_samples = cfg.object_delay_samples;
render_options.tail_seconds = cfg.tail_seconds; render_options.tail_seconds = cfg.tail_seconds;
render_options.output_gain = std::pow(10.0, cfg.gain_db / 20.0);
if (status.ok()) { if (status.ok()) {
status = rosella.open(model, render_options); 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; 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", return finalize_with_error(JOC_ERR_INPUT_NOT_FOUND, "bed_path",
"cannot open the core PCM: " + 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 producer;
std::thread writer; std::thread writer;
StageJoiner joiner(&joc_ring, &output_ring, &stop, &producer, &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 ------------------------ // // ---- stage 1: the bitstream, on its own thread ------------------------ //
// The serial loop's read/decode/JOC section, verbatim apart from its error // 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; bool stream_done = false;
double seconds = 0.0; double seconds = 0.0;
JocJob job; 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 { try {
while (!stream_done && !stop.requested()) { while (!stream_done && !stop.requested()) {
auto stage_started = Clock::now(); 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())); input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount(); const std::streamsize got = input.gcount();
if (got > 0) { 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; stream_done = true;
break; break;
} }
if (!bed.read_frame(produced, &bed5, &lfe)) { // A repeated (codec, strmtyp, substreamid) combination closes the
failures.record(produced, StageRank::kJoc, JOC_ERR_INPUT_FORMAT, "bed_path", // audio frame the group is holding.
"core PCM ended before frame " + std::to_string(produced)); 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; stream_done = true;
break; break;
} }
if (!present || container.find(::joc::emdf::kIdJoc) == nullptr) {
continue;
}
joc_frame_params params{}; joc_frame_params params{};
::joc::emdf::Container container;
const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size, const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size,
&params, &container, nullptr); &params, &container, nullptr);
if (!parsed.ok()) { 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; stream_done = true;
break; break;
} }
group_params = params;
group_has_joc = true;
job.update = ::joc::oamd::OamdUpdate{}; job.update = ::joc::oamd::OamdUpdate{};
job.has_update = false; job.has_update = false;
job.outer_offset = 0; 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.has_update = true;
job.outer_offset = static_cast<std::int64_t>(payload->sample_offset); 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( // The final frame of an input that ends exactly on a group boundary
rebuilder, params, bed5.data(), lfe.data(), gain, &objects16, &dsp_error); // has already been emitted above.
if (!rebuilt.ok()) { if (stream_done && !group.empty() && produced < total_frames) {
failures.record(produced, StageRank::kJoc, rebuilt.code(), "joc", const Status emitted = emit_group();
"frame " + std::to_string(produced) + ": " + dsp_error); if (!emitted.ok()) {
stream_done = true; failures.record(produced, StageRank::kJoc, emitted.code(),
break; 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 (...) { } 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 // frames and join it. Every in-loop write is finished before the finalize
// (tail flush, spool -> WAV, SHA-256, report) looks at the output. // (tail flush, spool -> WAV, SHA-256, report) looks at the output.
joiner.shutdown(false); 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; StageFailure recorded;
if (failures.earliest(&recorded)) { if (failures.earliest(&recorded)) {
return finalize_with_error(recorded.code, recorded.stage_text, recorded.message); 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) { if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output"); return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output");
} }
*out = fmt( // The string fields are escaped, so a Windows path or a quote in an error
"{\"status\":%u,\"error_code\":%u,\"error_stage\":\"%s\",\"error_message\":\"%s\"," // message still leaves the document valid JSON.
"\"input_frames\":%llu,\"output_samples\":%llu,\"duration_sec\":%.6f," *out = json::pretty_object({
"\"output_format_actual\":%u,\"output_peak\":%.9g,\"output_over_unity_values\":%llu," {"status", std::to_string(result.status)},
"\"output_file_bytes\":%llu,\"output_sha256\":\"%s\",\"oamd_payloads\":%llu," {"error_code", std::to_string(result.error_code)},
"\"oamd_transitions\":%llu,\"t_decode_bed\":%.6f,\"t_render\":%.6f,\"t_write\":%.6f," {"error_stage", json::quote(result.error_stage)},
"\"t_render_dsp\":%.6f,\"t_write_file\":%.6f,\"t_total\":%.6f}", {"error_message", json::quote(result.error_message)},
result.status, result.error_code, result.error_stage, result.error_message, {"input_frames", std::to_string(result.input_frames)},
static_cast<unsigned long long>(result.input_frames), {"output_samples", std::to_string(result.output_samples)},
static_cast<unsigned long long>(result.output_samples), result.duration_sec, {"duration_sec", fmt("%.6f", result.duration_sec)},
result.output_format_actual, result.output_peak, {"output_format_actual", std::to_string(result.output_format_actual)},
static_cast<unsigned long long>(result.output_over_unity_values), {"output_peak", fmt("%.9g", result.output_peak)},
static_cast<unsigned long long>(result.output_file_bytes), result.output_sha256, {"output_over_unity_values", std::to_string(result.output_over_unity_values)},
static_cast<unsigned long long>(result.oamd_payloads), {"output_file_bytes", std::to_string(result.output_file_bytes)},
static_cast<unsigned long long>(result.oamd_transitions), result.t_decode_bed, {"output_sha256", json::quote(result.output_sha256)},
result.t_render, result.t_write, result.t_render_dsp, result.t_write_file, {"oamd_payloads", std::to_string(result.oamd_payloads)},
result.t_total); {"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(); return Status::success();
} }
+28 -12
View File
@@ -8,18 +8,19 @@
#include <vector> #include <vector>
#include "log.h" #include "log.h"
#include "win_path.h"
namespace joc_container { namespace joc_container {
namespace { namespace {
std::wstring utf8_to_wide(const std::string& text) { // A library scan probes every container in the library, and most of them are
if (text.empty()) return {}; // declined; they also mostly share a directory, so the path would spend the log's
const int needed = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), // byte budget repeating the same prefix once per file. The file name is what
static_cast<int>(text.size()), nullptr, 0); // identifies the line. Files this component claims are logged with their full
std::wstring out(static_cast<std::size_t>(needed), L'\0'); // path by the caller.
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()), out.data(), std::string file_name_of(const std::string& path) {
needed); const std::string::size_type slash = path.find_last_of("\\/");
return out; return (slash == std::string::npos) ? path : path.substr(slash + 1);
} }
// A bounded, read-only view of the file. Every accessor returns false instead of // 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 { class Window {
public: public:
bool open(const std::string& path) { 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, handle_ = CreateFileW(wide.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle_ == INVALID_HANDLE_VALUE) { if (handle_ == INVALID_HANDLE_VALUE) {
@@ -200,15 +201,24 @@ Result scan_mp4(const Window& file, std::size_t max_bytes) {
Result result; Result result;
result.kind = Kind::kMp4; 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 { struct Range {
std::uint64_t begin; std::uint64_t begin;
std::uint64_t end; 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) { if (file.size() > max_bytes) {
ranges.push_back({file.size() - max_bytes, file.size()}); 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; unsigned audio_seen = 0;
bool found_any_audio = false; 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, [&](const std::string& type, std::uint64_t payload, std::uint64_t box_end,
std::size_t) { std::size_t) {
if (type != "moov") return true; if (type != "moov") return true;
if (box_end - payload > kMaxMoovBytes) {
oversized_moov = true;
return true;
}
walk_boxes(file, payload, box_end, 1, walk_boxes(file, payload, box_end, 1,
[&](const std::string& inner, std::uint64_t inner_payload, [&](const std::string& inner, std::uint64_t inner_payload,
std::uint64_t inner_end, std::size_t) { 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 " + result.detail = "mp4: first audio track is " +
(first_audio_format.empty() ? std::string("unknown") (first_audio_format.empty() ? std::string("unknown")
: first_audio_format); : first_audio_format);
} else if (oversized_moov) {
result.detail = "mp4: moov is larger than the scan limit";
} else { } else {
result.detail = "mp4: no audio track found in the scanned window"; 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 { } else {
result = scan_mp4(file, max_bytes); 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.detail.c_str(), result.eac3 ? 1 : 0, result.audio_index,
result.codec.empty() ? "-" : result.codec.c_str()); result.codec.empty() ? "-" : result.codec.c_str());
return result; 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 } // namespace
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) { namespace {
if (data == nullptr || offset + 4u > size) return 0;
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 = const std::uint16_t syncword =
static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]); static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]);
if (syncword != kEac3Syncword) return 0; 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 = const std::size_t words =
(static_cast<std::size_t>(data[offset + 2] & 0x07u) << 8) | data[offset + 3]; (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; 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) { bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes) {
if (frame == nullptr || frame_bytes < 8u) return false; 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) { if (frames == 0) {
result.joc = JocState::kUnknown; result.joc = JocState::kUnknown;
if (result.detail[0] == '\0') result.detail = "no syncframes"; if (result.detail[0] == '\0') result.detail = "no syncframes";
} else if (with_joc == frames) { } else if (with_joc != 0) {
result.joc = JocState::kYes; 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 { } else {
result.joc = JocState::kNo; 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; return result;
} }
+43 -11
View File
@@ -4,12 +4,14 @@
// because container metadata (MP4 dec3, Matroska A_EAC3) says only "E-AC-3" and // because container metadata (MP4 dec3, Matroska A_EAC3) says only "E-AC-3" and
// the JOC flag inside it is frequently missing. // the JOC flag inside it is frequently missing.
// //
// The criterion implemented here is deliberately the same one the rendering core // A syncframe carries JOC when it holds exactly one top-level EMDF container
// applies before it will render a frame: a syncframe carries JOC when it holds // (syncword 0x5838, any of the eight bit alignments) whose payload list contains
// exactly one top-level EMDF container (syncword 0x5838, any of the eight bit // both ID11 (OAMD) and ID14 (JOC) — the same per-frame test the rendering core
// alignments) whose payload list contains both ID11 (OAMD) and ID14 (JOC). // applies. The stream counts as JOC when at least one examined audio frame
// Agreeing with the core matters: a looser test would claim files the core then // carries one: in a 7-channel program the JOC payload lives in the dependent
// refuses, which is worse than declining them. // 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 #pragma once
#include <cstddef> #include <cstddef>
@@ -17,16 +19,26 @@
namespace joc_eac3 { 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 { struct FrameHeader {
std::size_t frame_bytes = 0; std::size_t frame_bytes = 0;
std::size_t offset = 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 { enum class JocState {
kYes, // every frame examined carried a JOC EMDF container kYes, // at least one examined syncframe carried a JOC EMDF container
kNo, // at least one frame examined did not kNo, // no examined syncframe carried one
kUnknown, // not enough data, or the stream is not walkable E-AC-3 kUnknown, // not enough data, or the stream is not walkable ac3/eac3
}; };
struct ScanResult { struct ScanResult {
@@ -42,9 +54,29 @@ struct ScanResult {
ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_frames); 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 // 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); 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. // True when this single syncframe carries the JOC EMDF container.
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes); bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes);
+101 -23
View File
@@ -17,6 +17,8 @@
#include <SDK/input_impl.h> #include <SDK/input_impl.h>
#include <SDK/tag_processor.h> #include <SDK/tag_processor.h>
#include <atomic>
#include <cstdarg>
#include <cstring> #include <cstring>
#include <string> #include <string>
@@ -66,6 +68,31 @@ std::string file_name_of(const std::string& path) {
return slash == std::string::npos ? path : path.substr(slash + 1); 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. // Tags of a file this component has taken over.
// //
@@ -200,8 +227,7 @@ public:
try { try {
m_native_path = filesystem::g_get_native_path(m_path.c_str()); m_native_path = filesystem::g_get_native_path(m_path.c_str());
} catch (const pfc::exception& error) { } catch (const pfc::exception& error) {
joc_log::line("decoder: not a native file path (%s): %s", m_path.c_str(), decline("decoder: not a native file path (%s): %s", m_path.c_str(), error.what());
error.what());
throw exception_io_unsupported_format(); throw exception_io_unsupported_format();
} }
@@ -249,10 +275,11 @@ public:
if (scan.joc != joc_eac3::JocState::kYes) { if (scan.joc != joc_eac3::JocState::kYes) {
// Hand the file to the next entry in the priority table. // 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(); throw exception_io_unsupported_format();
} }
joc_log::line("decoder: claiming this file as E-AC-3 JOC (bare stream)"); 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; m_input_kind = joc_decode::InputKind::kBare;
} }
@@ -263,8 +290,8 @@ public:
void open_container(const char* extension) { void open_container(const char* extension) {
m_container = joc_container::scan(m_native_path.get_ptr()); m_container = joc_container::scan(m_native_path.get_ptr());
if (!m_container.eac3) { if (!m_container.eac3) {
joc_log::line("decoder: yielding to the built-in decoder (%s)", decline("decoder: yielding to the built-in decoder (%s)",
m_container.detail.c_str()); m_container.detail.c_str());
throw exception_io_unsupported_format(); throw exception_io_unsupported_format();
} }
@@ -273,20 +300,20 @@ public:
std::string detail; std::string detail;
if (!joc_decode::probe_container_joc(settings.ffmpeg_path, m_native_path.get_ptr(), if (!joc_decode::probe_container_joc(settings.ffmpeg_path, m_native_path.get_ptr(),
m_container.audio_index, &joc, &detail)) { m_container.audio_index, &joc, &detail)) {
joc_log::line("decoder: cannot examine the E-AC-3 track (%s); yielding", decline("decoder: cannot examine the E-AC-3 track (%s); yielding", detail.c_str());
detail.c_str());
throw exception_io_unsupported_format(); throw exception_io_unsupported_format();
} }
if (!joc) { if (!joc) {
joc_log::line("decoder: yielding to the built-in decoder (E-AC-3 track %u carries " decline("decoder: yielding to the built-in decoder (E-AC-3 track %u carries "
"no JOC: %s)", "no JOC: %s)",
m_container.audio_index, detail.c_str()); m_container.audio_index, detail.c_str());
throw exception_io_unsupported_format(); throw exception_io_unsupported_format();
} }
joc_log::line("decoder: claiming this file as E-AC-3 JOC (%s, audio track %u, .%s)", 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, joc_container::kind_name(m_container.kind), m_container.audio_index,
extension); extension);
g_claimed.fetch_add(1, std::memory_order_relaxed);
m_input_kind = joc_decode::InputKind::kContainer; m_input_kind = joc_decode::InputKind::kContainer;
m_audio_index = m_container.audio_index; m_audio_index = m_container.audio_index;
} }
@@ -327,14 +354,22 @@ public:
channels = layout_channels != 0 ? layout_channels : 6; channels = layout_channels != 0 ? layout_channels : 6;
render = "speaker " + settings.speaker_layout; 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", "E-AC-3 JOC");
info.info_set("codec_long", "E-AC-3 JOC (Dolby Atmos)"); info.info_set("codec_long", "E-AC-3 JOC (Dolby Atmos)");
info.info_set("encoding", "lossy"); info.info_set("encoding", "lossy");
info.info_set_int("samplerate", kSampleRate); info.info_set_int("samplerate", kSampleRate);
info.info_set_int("channels", channels); info.info_set_int("channels", channels);
info.info_set_int("bitspersample", 32); // What the component hands over, which is what the host keeps if it writes a
info.info_set("bitspersample_extra", "floating-point"); // 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); info.set_length(duration);
m_length = duration; m_length = duration;
// The renderer keeps its room tail, but the stream this component hands over // The renderer keeps its room tail, but the stream this component hands over
@@ -428,8 +463,15 @@ public:
m_frames_delivered = 0; m_frames_delivered = 0;
m_reported = false; m_reported = false;
m_delivery_mismatches = 0; m_delivery_mismatches = 0;
joc_log::line("decoder: engine ready, %u output channel(s), %u frames per read", // The clip total belongs to the file rather than to one run of it, so a seek
m_channels, static_cast<unsigned>(kRunFrames)); // 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) { bool decode_run(audio_chunk& chunk, abort_callback& abort) {
@@ -445,6 +487,10 @@ public:
static_cast<unsigned long long>(m_frames_delivered), static_cast<unsigned long long>(m_frames_delivered),
m_delivery_mismatches == 0 ? "" m_delivery_mismatches == 0 ? ""
: " (the delivery changed samples)"); : " (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; return false;
} }
@@ -452,30 +498,58 @@ public:
// 32-bit builds and double on 64-bit ones (SDK audio_math.h): the samples are // 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 // 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. // source, and it sets the channel count, the sample rate and the sample count.
chunk.set_data_32(m_buffer.data(), frames, m_channels, kSampleRate); //
// 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; m_frames_delivered += frames;
// A delivery that mangled the samples would be heard as noise rather than reported // 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 // 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. // what the chunk holds has to equal the peak of what the renderer produced -- or,
const double produced = peak_of(m_buffer.data(), frames * m_channels); // for int24, the peak the packing step actually wrote.
const double delivered = const double delivered =
peak_of(chunk.get_data(), chunk.get_sample_count() * chunk.get_channels()); peak_of(chunk.get_data(), chunk.get_sample_count() * chunk.get_channels());
if (delivered > produced + 1e-6 + produced * 1e-6 || if (delivered > expected_delivered_peak + 1e-6 + expected_delivered_peak * 1e-6 ||
delivered < produced - 1e-6 - produced * 1e-6) { delivered < expected_delivered_peak - 1e-6 - expected_delivered_peak * 1e-6) {
++m_delivery_mismatches; ++m_delivery_mismatches;
if (m_delivery_mismatches == 1) { if (m_delivery_mismatches == 1) {
joc_log::line("decoder: delivery changed the samples: peak %.9f produced, " joc_log::line("decoder: delivery changed the samples: peak %.9f produced, "
"%.9f delivered", "%.9f delivered",
produced, delivered); expected_delivered_peak, delivered);
} }
} }
if (!m_reported) { if (!m_reported) {
m_reported = true; m_reported = true;
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", "delivered peak %.6f",
static_cast<unsigned long long>(frames), m_channels, produced, static_cast<unsigned long long>(frames), m_channels,
delivered); m_settings.force_int24 ? "int24" : "float32",
peak_of(m_buffer.data(), frames * m_channels), delivered);
} }
return true; return true;
} }
@@ -578,6 +652,10 @@ private:
unsigned m_audio_index = 0; unsigned m_audio_index = 0;
joc_container::Result m_container; joc_container::Result m_container;
std::vector<float> m_buffer; 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; unsigned m_channels = 2;
std::uint64_t m_frames_delivered = 0; std::uint64_t m_frames_delivered = 0;
bool m_reported = false; bool m_reported = false;
+288 -81
View File
@@ -6,6 +6,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <cwchar> #include <cwchar>
#include <limits>
#include <vector> #include <vector>
// The kernel copy that is compiled into this component; see kernel/. // The kernel copy that is compiled into this component; see kernel/.
@@ -14,13 +15,23 @@
#include "eac3_scan.h" #include "eac3_scan.h"
#include "log.h" #include "log.h"
#include "win_path.h"
namespace joc_decode { namespace joc_decode {
namespace { namespace {
constexpr std::size_t kEac3Chunk = 96u * 1024u; // bytes read per push constexpr std::size_t kEac3Chunk = 96u * 1024u; // bytes read per push
// The core renders during the push, so a read queues about as much as it is
// about to consume rather than everything it just read.
constexpr std::uint64_t kEac3FramesPerRead = 3u;
// Rendered audio the caller has not taken yet.
constexpr std::size_t kMaxRenderedAheadSamples = 4096u;
constexpr std::size_t kBedFramesChunk = 8192u; // staging capacity, in frames constexpr std::size_t kBedFramesChunk = 8192u; // staging capacity, in frames
constexpr std::size_t kBedChannels = 6; // ffmpeg -ac 6 // The bed channel count follows joc_dmx_config_idx: the 5.1 configurations
// (0 and 3) decode to 6 channels through `ffmpeg -ac 6`, the 7-channel ones
// (1, 2 and 4) to 8 channels in the decoder's own layout. See bed_channels.
constexpr std::size_t kDefaultBedChannels = 6;
constexpr std::size_t kMaxBedChannels = 8;
// A read on an anonymous pipe only completes once the whole request is available, // A read on an anonymous pipe only completes once the whole request is available,
// so each read asks for a slice ffmpeg can always fill, and the pipe buffer is // so each read asks for a slice ffmpeg can always fill, and the pipe buffer is
// sized well above that slice. Requesting the whole staging buffer deadlocks: // sized well above that slice. Requesting the whole staging buffer deadlocks:
@@ -30,6 +41,56 @@ constexpr std::size_t kBedReadBytes = 48u * 1024u;
constexpr std::size_t kBedPipeBytes = 1u << 20; constexpr std::size_t kBedPipeBytes = 1u << 20;
constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES; constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES;
// The decoded bed has to stay on the E-AC-3 frame grid that the JOC matrix and the
// OAMD are indexed by. An mp4/mov edit list trims the decoded audio instead, which
// takes the bed off that grid by however much the list removes -- a Dolby Atmos
// download loses 2432 samples (1.58 frames) -- and every frame's matrix would then be
// applied to audio tens of milliseconds away from it, so a subset of the objects comes
// out attenuated. The demuxer's own option drops the trim; the option exists only on
// the mov/mp4 demuxer, so it is passed for that family alone.
bool has_mov_timeline(const std::string& path) {
static const char* const kExtensions[] = {".mp4", ".m4a", ".m4b", ".m4v",
".mov", ".3gp", ".3g2", ".mj2"};
std::string lowered = path;
for (char& character : lowered) {
if (character >= 'A' && character <= 'Z') {
character = static_cast<char>(character - 'A' + 'a');
}
}
for (const char* extension : kExtensions) {
const std::size_t length = std::strlen(extension);
if (lowered.size() >= length &&
lowered.compare(lowered.size() - length, length, extension) == 0) {
return true;
}
}
return false;
}
// joc_dmx_config_idx of the first syncframe in the window that carries JOC, or -1
// when the window holds none. Only the first three bits of the JOC payload are
// read, so this costs nothing next to a decode.
int scan_config_idx(const std::uint8_t* data, std::size_t size) {
std::size_t offset = 0;
while (offset + 6u <= size) {
const std::size_t bytes = joc_eac3::frame_bytes_at(data, size, offset);
if (bytes == 0 || offset + bytes > size) break;
if (joc_eac3::frame_has_joc(data + offset, bytes)) {
unsigned config = 0;
if (joc_eac3::joc_config_idx(data + offset, bytes, &config)) {
return static_cast<int>(config);
}
}
offset += bytes;
}
return -1;
}
// True when the JOC configuration declares 7 core channels.
bool seven_channel_config(int config) {
return config == 1 || config == 2 || config == 4;
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Kernel entry points. // Kernel entry points.
// //
@@ -50,20 +111,11 @@ struct CoreApi {
std::uint32_t(JOC_CALL* abi_version)() = joc_abi_version; std::uint32_t(JOC_CALL* abi_version)() = joc_abi_version;
const char*(JOC_CALL* version_string)() = joc_version_string; const char*(JOC_CALL* version_string)() = joc_version_string;
const char*(JOC_CALL* error_name)(joc_error) = joc_error_name; const char*(JOC_CALL* error_name)(joc_error) = joc_error_name;
const char*(JOC_CALL* last_error_detail)() = joc_last_error_detail;
}; };
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;
}
bool file_exists(const std::string& path) { bool file_exists(const std::string& path) {
const std::wstring wide = utf8_to_wide(path); const std::wstring wide = joc_path::to_wide_extended(path);
if (wide.empty()) return false; if (wide.empty()) return false;
const DWORD attributes = GetFileAttributesW(wide.c_str()); const DWORD attributes = GetFileAttributesW(wide.c_str());
return attributes != INVALID_FILE_ATTRIBUTES && return attributes != INVALID_FILE_ATTRIBUTES &&
@@ -88,6 +140,22 @@ const char* error_text(const CoreApi& api, joc_error code) {
return "unknown core error"; return "unknown core error";
} }
// The core keeps a structured detail for the last failing call ("eac3: chanmap
// declares [Lts,Rts] but joc_dmx_config_idx=2 expects [Vhl,Vhr]"). The plugin
// reports the variant rather than only its error code, so a stream the renderer
// deliberately refuses says why.
std::string error_with_detail(const CoreApi& api, joc_error code) {
std::string text = error_text(api, code);
if (api.last_error_detail != nullptr) {
const char* detail = api.last_error_detail();
if (detail != nullptr && *detail != '\0') {
text += ": ";
text += detail;
}
}
return text;
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// ffmpeg child process producing the 5.1 core PCM on a pipe. // ffmpeg child process producing the 5.1 core PCM on a pipe.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -121,11 +189,11 @@ public:
FILE_SHARE_READ | FILE_SHARE_WRITE, &attributes, FILE_SHARE_READ | FILE_SHARE_WRITE, &attributes,
CREATE_ALWAYS, 0, nullptr); CREATE_ALWAYS, 0, nullptr);
std::wstring command = L"\"" + utf8_to_wide(ffmpeg_path) + L"\""; std::wstring command = L"\"" + joc_path::to_wide_extended(ffmpeg_path) + L"\"";
command += L" -hide_banner -loglevel error -nostdin -y "; command += L" -hide_banner -loglevel error -nostdin -y ";
command += input_arguments; // input options must precede -i command += input_arguments; // input options must precede -i
command += L" -i \""; command += L" -i \"";
command += utf8_to_wide(input_path); command += joc_path::to_wide_extended(input_path);
command += L"\" "; command += L"\" ";
command += output_arguments; command += output_arguments;
@@ -197,7 +265,7 @@ class InputFile {
public: public:
~InputFile() { close(); } ~InputFile() { close(); }
bool open(const std::string& path) { bool open(const std::string& path) {
handle_ = CreateFileW(utf8_to_wide(path).c_str(), GENERIC_READ, handle_ = CreateFileW(joc_path::to_wide_extended(path).c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr); FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
return handle_ != INVALID_HANDLE_VALUE; return handle_ != INVALID_HANDLE_VALUE;
@@ -287,6 +355,45 @@ unsigned speaker_channels(const std::string& layout) {
return 0; return 0;
} }
namespace {
// The core's conversion, reproduced: kernel/src/io/wav_writer.cpp:to_int32.
// (min)() is written that way because the test tools compile this file without
// NOMINMAX, and windows.h's min macro would otherwise swallow the call.
std::int32_t to_int32(const float value) {
if (!std::isfinite(value)) {
return (std::numeric_limits<std::int32_t>::min)();
}
return static_cast<std::int32_t>(value);
}
} // namespace
Int24Block pack_int24(const float* interleaved, std::size_t count, std::uint8_t* out) {
Int24Block block;
for (std::size_t i = 0; i < count; ++i) {
float value = interleaved[i];
if (value > 1.0f) {
value = 1.0f;
++block.clipped;
} else if (value < -1.0f) {
value = -1.0f;
++block.clipped;
}
const std::int32_t scaled = to_int32(value * 8388607.0f);
const std::uint32_t bits = static_cast<std::uint32_t>(scaled);
out[i * 3 + 0] = static_cast<std::uint8_t>(bits & 0xFFu);
out[i * 3 + 1] = static_cast<std::uint8_t>((bits >> 8) & 0xFFu);
out[i * 3 + 2] = static_cast<std::uint8_t>((bits >> 16) & 0xFFu);
// What the host will read back is the sign-extended low 24 bits.
std::int32_t written = static_cast<std::int32_t>(bits & 0xFFFFFFu);
if ((written & 0x800000) != 0) written -= 0x1000000;
const std::int32_t magnitude = (written < 0) ? -written : written;
if (magnitude > block.peak) block.peak = magnitude;
}
return block;
}
std::string component_directory() { std::string component_directory() {
HMODULE self = nullptr; HMODULE self = nullptr;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
@@ -364,40 +471,47 @@ FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes,
return probe; return probe;
} }
// The renderer pairs one audio frame with exactly 1536 bed samples, so the
// stream length is counted in audio frames: a 7-channel program interleaves an
// independent and a dependent substream, and one audio frame is two syncframes.
const bool whole_file = (buffer.size() == stream_bytes);
std::size_t offset = 0;
std::size_t first_audio_frame = 0;
bool same_audio_frame_size = true;
std::uint64_t seen = 0;
while (true) {
const std::size_t bytes =
joc_eac3::audio_frame_bytes_at(buffer.data(), got, offset, whole_file);
if (bytes == 0 || offset + bytes > got) break;
if (seen == 0) {
first_audio_frame = bytes;
} else if (bytes != first_audio_frame) {
same_audio_frame_size = false;
}
offset += bytes;
++seen;
}
std::uint64_t frames = 0; std::uint64_t frames = 0;
if (buffer.size() == stream_bytes) { if (whole_file) {
std::size_t offset = 0; frames = seen;
while (true) {
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, offset);
if (bytes == 0 || offset + bytes > got) break;
offset += bytes;
++frames;
}
probe.detail = "frame count walked over the whole file"; probe.detail = "frame count walked over the whole file";
} else if (scan.all_frames_same_size) { } else if (seen != 0 && same_audio_frame_size) {
frames = stream_bytes / scan.first_frame_bytes; frames = stream_bytes / first_audio_frame;
probe.detail = "frame count extrapolated from a constant frame size"; probe.detail = "frame count extrapolated from a constant frame size";
} else { } else if (offset != 0) {
// Variable frame size: count in the window and scale by the byte ratio. // Variable frame size: count in the window and scale by the byte ratio.
std::size_t offset = 0; frames = static_cast<std::uint64_t>(
std::uint64_t seen = 0; (static_cast<double>(stream_bytes) / static_cast<double>(offset)) *
while (true) { static_cast<double>(seen));
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, offset);
if (bytes == 0 || offset + bytes > got) break;
offset += bytes;
++seen;
}
frames = (offset != 0) ? static_cast<std::uint64_t>(
(static_cast<double>(stream_bytes) /
static_cast<double>(offset)) *
static_cast<double>(seen))
: 0;
probe.detail = "frame count estimated from a variable frame size"; probe.detail = "frame count estimated from a variable frame size";
} }
const int config = scan_config_idx(buffer.data(), got);
probe.frames = frames; probe.frames = frames;
probe.sample_rate = 48000; probe.sample_rate = 48000;
probe.channels = 6; // the core audio of an E-AC-3 JOC stream // Core audio channel count: the 5.1 configurations give 6, the 7-channel ones 8.
probe.channels = static_cast<unsigned>(seven_channel_config(config) ? 8 : 6);
probe.duration_seconds = probe.duration_seconds =
static_cast<double>(frames) * static_cast<double>(kFrameSamples) / 48000.0; static_cast<double>(frames) * static_cast<double>(kFrameSamples) / 48000.0;
return probe; return probe;
@@ -422,8 +536,8 @@ bool probe_container_joc(const std::string& ffmpeg_path, const std::string& path
std::uint64_t modified = 0; std::uint64_t modified = 0;
{ {
WIN32_FILE_ATTRIBUTE_DATA data{}; WIN32_FILE_ATTRIBUTE_DATA data{};
if (GetFileAttributesExW(utf8_to_wide(path).c_str(), GetFileExInfoStandard, &data) != if (GetFileAttributesExW(joc_path::to_wide_extended(path).c_str(), GetFileExInfoStandard,
FALSE) { &data) != FALSE) {
size = (static_cast<std::uint64_t>(data.nFileSizeHigh) << 32) | data.nFileSizeLow; size = (static_cast<std::uint64_t>(data.nFileSizeHigh) << 32) | data.nFileSizeLow;
modified = (static_cast<std::uint64_t>(data.ftLastWriteTime.dwHighDateTime) << 32) | modified = (static_cast<std::uint64_t>(data.ftLastWriteTime.dwHighDateTime) << 32) |
data.ftLastWriteTime.dwLowDateTime; data.ftLastWriteTime.dwLowDateTime;
@@ -508,7 +622,10 @@ struct Engine::Impl {
} }
unsigned channels = 0; unsigned channels = 0;
std::uint64_t frames_queued = 0; // E-AC-3 frames handed to the core // Decoded bed channel count, resolved from joc_dmx_config_idx before the bed
// decoder starts: 6 for configurations 0/3, 8 for 1/2/4.
std::size_t bed_channels = kDefaultBedChannels;
std::uint64_t frames_queued = 0; // audio frames handed to the core
std::uint64_t bed_frames_pushed = 0; std::uint64_t bed_frames_pushed = 0;
bool eac3_eof = false; bool eac3_eof = false;
bool bed_eof = false; bool bed_eof = false;
@@ -588,6 +705,10 @@ struct Engine::Impl {
bool position_bare(std::uint64_t frame, std::string* error); bool position_bare(std::uint64_t frame, std::string* error);
bool start_bed(std::uint64_t source_sample, std::string* error); bool start_bed(std::uint64_t source_sample, std::string* error);
bool start_metadata(std::string* error); bool start_metadata(std::string* error);
// Resolves bed_channels from the stream's own joc_dmx_config_idx. For a
// container this starts the metadata pipe once, reads a prefix and stops it
// again; for a bare stream it reads a prefix from the file and seeks back.
bool resolve_bed_channels(std::string* error);
}; };
bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) { bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) {
@@ -619,9 +740,14 @@ bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) {
index_complete = true; index_complete = true;
break; break;
} }
// The index holds audio frames, not syncframes: the renderer consumes one
// bed frame per audio frame, and a 7-channel program makes two syncframes
// into one audio frame.
const bool at_end = (index_file_offset + got >= size);
std::size_t consumed = 0; std::size_t consumed = 0;
while (frame_offsets.size() < wanted) { while (frame_offsets.size() < wanted) {
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, consumed); const std::size_t bytes =
joc_eac3::audio_frame_bytes_at(buffer.data(), got, consumed, at_end);
if (bytes == 0 || consumed + bytes > got) break; if (bytes == 0 || consumed + bytes > got) break;
frame_offsets.push_back(index_stream_offset + consumed); frame_offsets.push_back(index_stream_offset + consumed);
consumed += bytes; consumed += bytes;
@@ -672,24 +798,65 @@ bool Engine::Impl::position_bare(std::uint64_t frame, std::string* error) {
return true; return true;
} }
bool Engine::Impl::resolve_bed_channels(std::string* error) {
bed_channels = kDefaultBedChannels;
std::vector<std::uint8_t> buffer(1u << 20);
int config = -1;
if (eac3_from_pipe) {
// The metadata stream has to run once to be sniffed; it is stopped again and
// restarted from the beginning, which costs one extra ffmpeg spawn per track.
if (!start_metadata(error)) return false;
const std::size_t got = eac3_pipe.read(buffer.data(), buffer.size());
eac3_pipe.stop();
config = scan_config_idx(buffer.data(), got);
} else {
if (!eac3.is_open() && !eac3.open(input_path)) {
if (error != nullptr) *error = "cannot open the input file";
return false;
}
const std::size_t got = eac3.read(buffer.data(), buffer.size());
config = scan_config_idx(buffer.data(), got);
// Put the file back where the feed loop expects it.
if (!eac3.seek(index_base)) {
if (error != nullptr) *error = "cannot position the input file";
return false;
}
}
bed_channels = seven_channel_config(config)
? static_cast<std::size_t>(7u + 1u)
: kDefaultBedChannels;
if (bed_channels > kMaxBedChannels) bed_channels = kMaxBedChannels;
joc_log::line("core: joc_dmx_config_idx=%d -> %llu bed channel(s)", config,
static_cast<unsigned long long>(bed_channels));
return true;
}
bool Engine::Impl::start_bed(std::uint64_t source_sample, std::string* error) { bool Engine::Impl::start_bed(std::uint64_t source_sample, std::string* error) {
// The 5.1 core PCM, exactly as the reference renderer's own core decode does // The core PCM the kernel expects: interleaved float32 at 48 kHz, laid out as
// it: 5.1 interleaved float32 at 48 kHz, the layout the renderer expects // L R C LFE Ls Rs for 5.1 and L R C LFE BL BR SL SR for 7.1.
// (L R C LFE Ls Rs).
// -drc_scale 0 -target_level 0: the bed is taken as stored, without the // -drc_scale 0 -target_level 0: the bed is taken as stored, without the
// stream's dynrng or target-level metadata being applied by the decoder. // stream's dynrng or target-level metadata being applied by the decoder.
// //
// -ac is only passed for the 5.1 configurations: `-ac 6` downmixes 5.1.2/7.1
// into 5.1 and drops the extra pair, and `-ac 8` would remap 5.1.2 into 7.1.
// The 7-channel configurations take the decoder's own layout instead.
//
// -ss is an input option: the demuxer is positioned on the frame boundary and // -ss is an input option: the demuxer is positioned on the frame boundary and
// decoding starts there, so a seek costs the same wherever it lands. The price is // decoding starts there, so a seek costs the same wherever it lands. The price is
// that the bed is carried by a decoder that started at the seek point rather than // that the bed is carried by a decoder that started at the seek point rather than
// at the start of the file, which is a low-level, noise-like difference from a // at the start of the file, which is a low-level, noise-like difference from a
// play-through rather than a misalignment: the position stays exact. // play-through rather than a misalignment: the position stays exact.
std::wstring input_arguments = L"-drc_scale 0 -target_level 0"; std::wstring input_arguments = L"-drc_scale 0 -target_level 0";
if (has_mov_timeline(input_path)) input_arguments += L" -ignore_editlist 1";
const std::wstring offset = seek_time(source_sample); const std::wstring offset = seek_time(source_sample);
if (!offset.empty()) input_arguments += L" -ss " + offset; if (!offset.empty()) input_arguments += L" -ss " + offset;
std::wstring bed_arguments = L"-map 0:a:"; std::wstring bed_arguments = L"-map 0:a:";
bed_arguments += std::to_wstring(settings.audio_index); bed_arguments += std::to_wstring(settings.audio_index);
bed_arguments += L" -vn -ac 6 -ar 48000 -c:a pcm_f32le -f f32le -"; bed_arguments += L" -vn";
if (bed_channels == kDefaultBedChannels) {
bed_arguments += L" -ac 6";
}
bed_arguments += L" -ar 48000 -c:a pcm_f32le -f f32le -";
return bed.start(settings.ffmpeg_path, input_path, input_arguments, bed_arguments, "bed", return bed.start(settings.ffmpeg_path, input_path, input_arguments, bed_arguments, "bed",
stderr_path_for(L"joc_ffmpeg_bed.log"), error); stderr_path_for(L"joc_ffmpeg_bed.log"), error);
} }
@@ -741,7 +908,6 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
impl.reset_feed_state(); impl.reset_feed_state();
impl.end_sample = sample_of_seconds(settings.length_seconds); impl.end_sample = sample_of_seconds(settings.length_seconds);
impl.eac3_buffer.resize(kEac3Chunk); impl.eac3_buffer.resize(kEac3Chunk);
impl.bed_buffer.resize(kBedFramesChunk * kBedChannels);
if (!load_api(std::string(), &impl.api, error)) return false; if (!load_api(std::string(), &impl.api, error)) return false;
const std::uint32_t abi = impl.api.abi_version(); const std::uint32_t abi = impl.api.abi_version();
@@ -771,6 +937,11 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
} }
} }
// The bed decoder has to know how many channels to produce before it starts,
// and that comes from the stream's own joc_dmx_config_idx.
if (!impl.resolve_bed_channels(error)) return false;
impl.bed_buffer.resize(kBedFramesChunk * impl.bed_channels);
joc_stream_config config{}; joc_stream_config config{};
config.struct_size = sizeof(config); config.struct_size = sizeof(config);
config.struct_version = 1; config.struct_version = 1;
@@ -825,7 +996,7 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
if (created != JOC_OK) { if (created != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("cannot create the render stream: ") + *error = std::string("cannot create the render stream: ") +
error_text(impl.api, created); error_with_detail(impl.api, created);
} }
return false; return false;
} }
@@ -909,7 +1080,7 @@ bool Engine::seek(double seconds, double total_seconds, std::string* error) {
if (reset != JOC_OK) { if (reset != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("cannot reset the render stream: ") + *error = std::string("cannot reset the render stream: ") +
error_text(impl.api, reset); error_with_detail(impl.api, reset);
} }
return false; return false;
} }
@@ -956,7 +1127,19 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
impl.eac3_eof = true; impl.eac3_eof = true;
} }
if (!impl.eac3_eof && impl.frames_queued <= impl.bed_frames_pushed + 2u && // Queue more input only while less than one read's worth is waiting.
std::size_t rendered_ahead = 0;
{
joc_stream_status_info pending{};
pending.struct_size = sizeof(pending);
pending.struct_version = 1;
if (impl.api.status(impl.stream, &pending) == JOC_OK) {
rendered_ahead = static_cast<std::size_t>(pending.buffered_samples);
}
}
if (!impl.eac3_eof && rendered_ahead < kMaxRenderedAheadSamples &&
impl.frames_queued <= impl.bed_frames_pushed + 2u &&
(impl.settings.input_frame_limit == 0 || (impl.settings.input_frame_limit == 0 ||
impl.frames_queued < impl.settings.input_frame_limit)) { impl.frames_queued < impl.settings.input_frame_limit)) {
// The tail of a chunk is usually the head of the next syncframe. It // The tail of a chunk is usually the head of the next syncframe. It
@@ -967,6 +1150,10 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const std::size_t got = impl.read_eac3(impl.eac3_buffer.data() + impl.eac3_carry, const std::size_t got = impl.read_eac3(impl.eac3_buffer.data() + impl.eac3_carry,
impl.eac3_buffer.size() - impl.eac3_carry); impl.eac3_buffer.size() - impl.eac3_carry);
std::size_t total = impl.eac3_carry + got; std::size_t total = impl.eac3_carry + got;
// A read that returns nothing means no more bytes will arrive, so the
// audio frame still open in the buffer is complete by definition. Without
// this the last audio frame of every file is left in the carry.
const bool at_eof = (got == 0) || impl.eac3_eof;
if (total == 0) { if (total == 0) {
impl.eac3_eof = true; impl.eac3_eof = true;
} else { } else {
@@ -976,8 +1163,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (impl.metadata_skip != 0) { if (impl.metadata_skip != 0) {
std::size_t dropped = 0; std::size_t dropped = 0;
while (impl.metadata_skip != 0) { while (impl.metadata_skip != 0) {
const std::size_t bytes = const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, dropped); impl.eac3_buffer.data(), total, dropped, at_eof);
if (bytes == 0 || dropped + bytes > total) break; if (bytes == 0 || dropped + bytes > total) break;
dropped += bytes; dropped += bytes;
--impl.metadata_skip; --impl.metadata_skip;
@@ -999,32 +1186,34 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
std::size_t offset = 0; std::size_t offset = 0;
std::uint64_t complete = 0; std::uint64_t complete = 0;
while (true) { while (true) {
const std::size_t bytes = const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, offset); impl.eac3_buffer.data(), total, offset, at_eof);
if (bytes == 0 || offset + bytes > total) break; if (bytes == 0 || offset + bytes > total) break;
offset += bytes; offset += bytes;
++complete; ++complete;
} }
// With an input limit the chunk is cut at a frame boundary: the // Both cuts land on a frame boundary because the renderer's output
// renderer's output depends on how many frames it was given, so a // depends on how many frames it was given: the input limit has to
// limit that overshoots to the end of the read buffer would not // stop where it says, and a read queues only its own budget.
// reproduce a run that stopped earlier. std::uint64_t allowed = complete;
std::size_t push_bytes = offset;
std::uint64_t pushed_frames = complete;
if (impl.settings.input_frame_limit != 0) { if (impl.settings.input_frame_limit != 0) {
const std::uint64_t room = const std::uint64_t room =
impl.settings.input_frame_limit - impl.frames_queued; impl.settings.input_frame_limit - impl.frames_queued;
if (complete > room) { if (allowed > room) allowed = room;
pushed_frames = room; }
std::size_t walk = 0; if (allowed > kEac3FramesPerRead) allowed = kEac3FramesPerRead;
for (std::uint64_t index = 0; index < pushed_frames; ++index) { std::size_t push_bytes = offset;
const std::size_t bytes = joc_eac3::frame_bytes_at( std::uint64_t pushed_frames = complete;
impl.eac3_buffer.data(), total, walk); if (allowed < complete) {
if (bytes == 0 || walk + bytes > total) break; pushed_frames = allowed;
walk += bytes; std::size_t walk = 0;
} for (std::uint64_t index = 0; index < pushed_frames; ++index) {
push_bytes = walk; const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, walk, at_eof);
if (bytes == 0 || walk + bytes > total) break;
walk += bytes;
} }
push_bytes = walk;
} }
joc_stream_buffer input{}; joc_stream_buffer input{};
input.struct_size = sizeof(input); input.struct_size = sizeof(input);
@@ -1036,7 +1225,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (pushed != JOC_OK) { if (pushed != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("E-AC-3 push failed: ") + *error = std::string("E-AC-3 push failed: ") +
error_text(impl.api, pushed); error_with_detail(impl.api, pushed);
} }
return 0; return 0;
} }
@@ -1046,7 +1235,9 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
std::memmove(impl.eac3_buffer.data(), impl.eac3_buffer.data() + push_bytes, std::memmove(impl.eac3_buffer.data(), impl.eac3_buffer.data() + push_bytes,
impl.eac3_carry); impl.eac3_carry);
} }
if (got == 0) impl.eac3_eof = true; // The pipe can end with complete syncframes still buffered; they
// are queued next, so end of input is only real once none remain.
if (got == 0 && pushed_frames >= complete) impl.eac3_eof = true;
} }
} }
@@ -1057,27 +1248,27 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
// return any number of bytes, and rounding a read down to whole samples // return any number of bytes, and rounding a read down to whole samples
// quietly drops the rest of the stream's alignment. // quietly drops the rest of the stream's alignment.
while (!impl.bed_eof && impl.bed_frames_pushed < impl.frames_queued) { while (!impl.bed_eof && impl.bed_frames_pushed < impl.frames_queued) {
constexpr std::size_t kFrameBytes = kFrameSamples * kBedChannels * sizeof(float); const std::size_t frame_bytes = kFrameSamples * impl.bed_channels * sizeof(float);
const std::size_t whole_frames = impl.bed_staged_bytes / kFrameBytes; const std::size_t whole_frames = impl.bed_staged_bytes / frame_bytes;
if (whole_frames != 0) { if (whole_frames != 0) {
joc_stream_buffer input{}; joc_stream_buffer input{};
input.struct_size = sizeof(input); input.struct_size = sizeof(input);
input.struct_version = 1; input.struct_version = 1;
input.kind = JOC_STREAM_IN_CORE_PCM; input.kind = JOC_STREAM_IN_CORE_PCM;
input.pcm = impl.bed_buffer.data(); input.pcm = impl.bed_buffer.data();
input.channels = static_cast<std::uint32_t>(kBedChannels); input.channels = static_cast<std::uint32_t>(impl.bed_channels);
input.sample_rate = 48000; input.sample_rate = 48000;
input.sample_count = static_cast<std::uint32_t>(whole_frames * kFrameSamples); input.sample_count = static_cast<std::uint32_t>(whole_frames * kFrameSamples);
const joc_error pushed = impl.api.push(impl.stream, &input, nullptr, nullptr); const joc_error pushed = impl.api.push(impl.stream, &input, nullptr, nullptr);
if (pushed != JOC_OK) { if (pushed != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("core PCM push failed: ") + *error = std::string("core PCM push failed: ") +
error_text(impl.api, pushed); error_with_detail(impl.api, pushed);
} }
return 0; return 0;
} }
impl.bed_frames_pushed += whole_frames; impl.bed_frames_pushed += whole_frames;
const std::size_t pushed_bytes = whole_frames * kFrameBytes; const std::size_t pushed_bytes = whole_frames * frame_bytes;
impl.bed_staged_bytes -= pushed_bytes; impl.bed_staged_bytes -= pushed_bytes;
if (impl.bed_staged_bytes != 0) { if (impl.bed_staged_bytes != 0) {
std::memmove(impl.bed_buffer.data(), std::memmove(impl.bed_buffer.data(),
@@ -1119,7 +1310,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const joc_error pulled = impl.api.pull(impl.stream, &output, &produced); const joc_error pulled = impl.api.pull(impl.stream, &output, &produced);
if (pulled != JOC_OK) { if (pulled != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("pull failed: ") + error_text(impl.api, pulled); *error = std::string("pull failed: ") + error_with_detail(impl.api, pulled);
} }
return 0; return 0;
} }
@@ -1169,7 +1360,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const joc_error flushed = impl.api.flush(impl.stream); const joc_error flushed = impl.api.flush(impl.stream);
if (flushed != JOC_OK) { if (flushed != JOC_OK) {
if (error != nullptr) { if (error != nullptr) {
*error = std::string("flush failed: ") + error_text(impl.api, flushed); *error = std::string("flush failed: ") + error_with_detail(impl.api, flushed);
} }
return 0; return 0;
} }
@@ -1188,6 +1379,22 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
static_cast<unsigned long long>(status.frames_out), static_cast<unsigned long long>(status.frames_out),
static_cast<unsigned long long>(status.samples_out), static_cast<unsigned long long>(status.samples_out),
static_cast<unsigned long long>(status.buffered_samples)); static_cast<unsigned long long>(status.buffered_samples));
// Metadata the renderer had to work around; each one is per audio
// frame and a non-zero count is worth reporting with the rest.
if (status.missing_joc_frames != 0u || status.objects_clamped_frames != 0u ||
status.extra_container_frames != 0u ||
status.chanmap_pair_mismatch_frames != 0u ||
status.bed_mask_mismatch_frames != 0u) {
joc_log::line(
"core: metadata workarounds: missing_joc_frames=%llu "
"objects_clamped_frames=%llu extra_container_frames=%llu "
"chanmap_pair_mismatch_frames=%llu bed_mask_mismatch_frames=%llu",
static_cast<unsigned long long>(status.missing_joc_frames),
static_cast<unsigned long long>(status.objects_clamped_frames),
static_cast<unsigned long long>(status.extra_container_frames),
static_cast<unsigned long long>(status.chanmap_pair_mismatch_frames),
static_cast<unsigned long long>(status.bed_mask_mismatch_frames));
}
joc_log::line("core: bed bytes read=%llu, staged samples left=%llu", joc_log::line("core: bed bytes read=%llu, staged samples left=%llu",
static_cast<unsigned long long>(impl.bed_bytes_read), static_cast<unsigned long long>(impl.bed_bytes_read),
static_cast<unsigned long long>(impl.bed_staged_bytes)); static_cast<unsigned long long>(impl.bed_staged_bytes));
+37 -9
View File
@@ -4,17 +4,14 @@
// drives when the output has to be compared byte for byte against joc_cli. // 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: // 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 // * the E-AC-3 syncframes -- the metadata stream -- read straight from the file;
// file, since the plugin only claims bare .eac3/.ec3 today;
// * the 5.1 core PCM of those same frames -- the rendering core does not decode // * 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 // the core (it has no AC-3 decoder at all), so ffmpeg produces it.
// way the reference CLI does.
// //
// The core is loaded from joc_core.dll through the public C ABI and called via // The core is the C++ kernel under kernel/, compiled into this component and called
// function pointers. Static linking is not an option: the core's static library // through its public C API via the CoreApi table. No DLL is shipped or loaded: the
// carries the internals but not the API layer in src/api, and the plugin is not // table is filled with the linked entry points and start-up only checks the ABI
// allowed to compile core sources. Loading explicitly also lets the component // version.
// ship its own copy of the DLL and check its ABI version at start-up.
#pragma once #pragma once
#include <cstddef> #include <cstddef>
@@ -96,6 +93,15 @@ struct Settings {
// is what the reference CLI's --duration does. Zero means "the whole file". // is what the reference CLI's --duration does. Zero means "the whole file".
// Only the comparison harness sets it; playback leaves it at zero. // Only the comparison harness sets it; playback leaves it at zero.
std::uint64_t input_frame_limit = 0; 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 // Channel count of a named layout, or 0 when the layout is not one of the
@@ -139,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, bool probe_container_joc(const std::string& ffmpeg_path, const std::string& path,
unsigned audio_index, bool* joc, std::string* detail); 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 { class Engine {
public: public:
Engine(); Engine();
+90 -36
View File
@@ -8,6 +8,8 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include "win_path.h"
namespace joc_log { namespace joc_log {
namespace { namespace {
@@ -16,12 +18,20 @@ FILE* g_file = nullptr;
bool g_opened = false; bool g_opened = false;
std::string g_path; // UTF-8, for reporting std::string g_path; // UTF-8, for reporting
std::wstring g_wide_path; // for _wfopen std::wstring g_wide_path; // for _wfopen
unsigned long long g_lines = 0; std::wstring g_wide_rolled; // g_wide_path with the .1 suffix
bool g_capped = false; unsigned long long g_bytes = 0;
// A Media Library scan calls into us for every file; the cap keeps a long // A Media Library scan calls into us for every file it walks, so the cap has to
// session from filling the disk, and says so once when it is reached. // be on bytes: the lines a scan produces run from 40 to 2048 bytes each, and a
constexpr unsigned long long kMaxLines = 500000; // 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() { std::wstring module_directory() {
HMODULE module = nullptr; HMODULE module = nullptr;
@@ -39,32 +49,26 @@ std::wstring module_directory() {
return dir; 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() { void open_locked() {
g_opened = true; g_opened = true;
wchar_t from_env[4096] = {}; wchar_t from_env[4096] = {};
const DWORD env_length = GetEnvironmentVariableW(L"JOC_LOG", from_env, const DWORD env_length = GetEnvironmentVariableW(L"JOC_LOG", from_env,
static_cast<DWORD>(4096)); static_cast<DWORD>(4096));
std::wstring plain;
if (env_length > 0 && env_length < 4096) { if (env_length > 0 && env_length < 4096) {
g_wide_path.assign(from_env, env_length); plain.assign(from_env, env_length);
} else { } else {
const std::wstring dir = module_directory(); const std::wstring dir = module_directory();
if (dir.empty()) return; 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"); g_file = _wfopen(g_wide_path.c_str(), L"wb");
if (g_file == nullptr) { if (g_file == nullptr) {
g_path.clear(); g_path.clear();
@@ -74,6 +78,52 @@ void open_locked() {
std::setvbuf(g_file, nullptr, _IONBF, 0); 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 } // namespace
void open() { void open() {
@@ -86,30 +136,34 @@ const char* path() {
return g_path.c_str(); return g_path.c_str();
} }
void line(const char* fmt, ...) { unsigned long long budget_bytes() { return kMaxBytes; }
va_list args;
va_start(args, fmt); void line_v(const char* fmt, va_list args) {
char text[2048]; char text[2048];
std::vsnprintf(text, sizeof(text), fmt, args); std::vsnprintf(text, sizeof(text), fmt, args);
va_end(args);
std::lock_guard<std::mutex> guard(g_mutex); std::lock_guard<std::mutex> guard(g_mutex);
if (!g_opened) open_locked(); if (!g_opened) open_locked();
if (g_file == nullptr) return; if (g_file == nullptr) return;
if (g_lines >= kMaxLines) { if (write_locked(text) != 0 && g_bytes >= kMaxBytes) roll_locked();
if (!g_capped) { }
g_capped = true;
std::fputs("[log capped]\n", g_file);
}
return;
}
++g_lines;
SYSTEMTIME now; void line(const char* fmt, ...) {
GetLocalTime(&now); va_list args;
std::fprintf(g_file, "%02u:%02u:%02u.%03u [t%05lu] %s\n", now.wHour, now.wMinute, va_start(args, fmt);
now.wSecond, now.wMilliseconds, line_v(fmt, args);
static_cast<unsigned long>(GetCurrentThreadId()), text); 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 } // 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. // 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. // 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 // The file is truncated once per process, unbuffered, and every line is written
// flushed so the log can be read while foobar2000 is still running. // 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 #pragma once
#include <cstdarg>
namespace joc_log { namespace joc_log {
// Opens (truncating) the log. Called lazily by line(); call it explicitly to // 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. // Absolute path of the log file, or "" when it could not be opened.
const char* path(); 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. // printf-style, thread safe, appends one line.
void line(const char* fmt, ...); 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 } // 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 // Kept in one place: the string reported to foobar2000 and written to the log
// must not drift apart. // must not drift apart.
#define JOC_VERSION "0.2.1" #define JOC_VERSION "0.4.0"
DECLARE_COMPONENT_VERSION("JOC decoder (E-AC-3 JOC)", JOC_VERSION, DECLARE_COMPONENT_VERSION("JOC decoder (E-AC-3 JOC)", JOC_VERSION,
"Plays E-AC-3 JOC (Dolby Atmos) files: the JOC objects are " "Plays E-AC-3 JOC (Dolby Atmos) files: the JOC objects are "
@@ -30,6 +30,9 @@ class joc_initquit : public initquit {
public: public:
void on_init() override { void on_init() override {
joc_log::open(); 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("=== foo_input_joc %s ===", JOC_VERSION);
joc_log::line("foobar2000 core : %s", core_version_info::g_get_version_string()); 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()); joc_log::line("component file : %s", core_api::get_my_file_name());
@@ -37,6 +40,8 @@ public:
joc_log::line("portable mode : %s", joc_log::line("portable mode : %s",
core_api::is_portable_mode_enabled() ? "yes" : "no"); core_api::is_portable_mode_enabled() ? "yes" : "no");
joc_log::line("log file : %s", joc_log::path()); 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", joc_log::line("compiled as : %s",
#if defined(_M_IX86) #if defined(_M_IX86)
"x86 (32-bit)" "x86 (32-bit)"
@@ -46,6 +51,7 @@ public:
"other" "other"
#endif #endif
); );
joc_log::header_end();
} }
void on_quit() override { joc_log::line("=== foo_input_joc shutdown ==="); } void on_quit() override { joc_log::line("=== foo_input_joc shutdown ==="); }
+18 -1
View File
@@ -214,7 +214,7 @@ private:
notify(); notify();
return TRUE; return TRUE;
} }
if (id == IDC_CHECK_GAIN) { if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24) {
update_enabled_state(); update_enabled_state();
notify(); notify();
return TRUE; return TRUE;
@@ -301,6 +301,8 @@ private:
SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_GAIN), BM_SETCHECK, SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_GAIN), BM_SETCHECK,
values.gain_enabled ? BST_CHECKED : BST_UNCHECKED, 0); 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(); update_enabled_state();
} }
@@ -328,6 +330,8 @@ private:
values.gain_db = std::atof(get_text(GetDlgItem(m_hwnd, IDC_EDIT_GAIN)).c_str()); values.gain_db = std::atof(get_text(GetDlgItem(m_hwnd, IDC_EDIT_GAIN)).c_str());
values.gain_enabled = values.gain_enabled =
IsDlgButtonChecked(m_hwnd, IDC_CHECK_GAIN) == BST_CHECKED; 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. // Advanced values are not on this page; keep whatever is stored.
const joc_settings::Values stored = joc_settings::read(); const joc_settings::Values stored = joc_settings::read();
values.object_delay_samples = stored.object_delay_samples; values.object_delay_samples = stored.object_delay_samples;
@@ -354,6 +358,18 @@ private:
} }
set_text(GetDlgItem(m_hwnd, IDC_LABEL_OUTPUT_STATUS), output_status); 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(); joc_decode::Settings effective = joc_settings::current();
effective.hrtf_source = effective.hrtf_source =
static_cast<joc_decode::HrtfSource>(values.hrtf_source); static_cast<joc_decode::HrtfSource>(values.hrtf_source);
@@ -383,6 +399,7 @@ private:
if (now.hrtf_file != m_initial.hrtf_file) return true; if (now.hrtf_file != m_initial.hrtf_file) return true;
if (now.binaural_mode != m_initial.binaural_mode) return true; if (now.binaural_mode != m_initial.binaural_mode) return true;
if (now.gain_enabled != m_initial.gain_enabled) 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.gain_db != m_initial.gain_db) return true;
if (now.tail_seconds != m_initial.tail_seconds) return true; if (now.tail_seconds != m_initial.tail_seconds) return true;
if (now.ffmpeg_path != m_initial.ffmpeg_path) return true; if (now.ffmpeg_path != m_initial.ffmpeg_path) return true;
+23 -20
View File
@@ -7,11 +7,11 @@
// sample page uses (SDK\foobar2000\foo_sample\foo_sample.rc). A CAPTION statement // 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 // 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. // every control down by the caption height -- visible but not clickable.
IDD_JOC_PREFS DIALOGEX 0, 0, 330, 273 IDD_JOC_PREFS DIALOGEX 0, 0, 330, 299
STYLE DS_SETFONT | WS_CHILD STYLE DS_SETFONT | WS_CHILD
FONT 8, "Microsoft Sans Serif", 400, 0, 0x0 FONT 8, "Microsoft Sans Serif", 400, 0, 0x0
BEGIN BEGIN
GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 61 GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 86
CONTROL "双耳(HRTF)", IDC_RADIO_BINAURAL, "Button", CONTROL "双耳(HRTF)", IDC_RADIO_BINAURAL, "Button",
BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 20, 72, 10 BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 20, 72, 10
CONTROL "扬声器布局", IDC_RADIO_SPEAKER, "Button", CONTROL "扬声器布局", IDC_RADIO_SPEAKER, "Button",
@@ -25,30 +25,33 @@ BEGIN
LTEXT "房间尾音(秒)", IDC_LABEL_TAIL, 172, 41, 62, 10 LTEXT "房间尾音(秒)", IDC_LABEL_TAIL, 172, 41, 62, 10
EDITTEXT IDC_EDIT_TAIL, 238, 38, 40, 13, ES_AUTOHSCROLL EDITTEXT IDC_EDIT_TAIL, 238, 38, 40, 13, ES_AUTOHSCROLL
LTEXT "", IDC_LABEL_OUTPUT_STATUS, 16, 52, 300, 10 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, 71, 316, 100 GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 97, 316, 100
CONTROL "SOFA 文件", IDC_RADIO_HRTF_SOFA, "Button", CONTROL "SOFA 文件", IDC_RADIO_HRTF_SOFA, "Button",
BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 86, 56, 10 BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 112, 56, 10
CONTROL "Rosella 个性化模型", IDC_RADIO_HRTF_ROSELLA, "Button", CONTROL "Rosella 个性化模型", IDC_RADIO_HRTF_ROSELLA, "Button",
BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 86, 78, 10 BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 112, 78, 10
LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 105, 106, 10 LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 131, 106, 10
EDITTEXT IDC_EDIT_HRTF, 126, 102, 148, 13, ES_AUTOHSCROLL EDITTEXT IDC_EDIT_HRTF, 126, 128, 148, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 102, 38, 13 PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 128, 38, 13
LTEXT "留空时读组件目录下 HRTF\\binaural.sofa 或 binaural.personalized_headphone;SOFA 由渲染器内部编译,滤波组表已编入库中。", LTEXT "留空时读组件目录下 HRTF\\binaural.sofa 或 binaural.personalized_headphone;SOFA 由渲染器内部编译,滤波组表已编入库中。",
IDC_LABEL_HRTF_HINT, 16, 121, 300, 20 IDC_LABEL_HRTF_HINT, 16, 147, 300, 20
LTEXT "", IDC_LABEL_STATUS, 16, 145, 300, 20 LTEXT "", IDC_LABEL_STATUS, 16, 171, 300, 20
GROUPBOX "增益", IDC_GROUP_GAIN, 7, 176, 316, 48 GROUPBOX "增益", IDC_GROUP_GAIN, 7, 202, 316, 48
CONTROL "应用增益", IDC_CHECK_GAIN, "Button", CONTROL "应用增益", IDC_CHECK_GAIN, "Button",
BS_AUTOCHECKBOX | WS_TABSTOP, 16, 192, 58, 10 BS_AUTOCHECKBOX | WS_TABSTOP, 16, 218, 58, 10
LTEXT "dB", IDC_LABEL_GAIN, 80, 192, 12, 10 LTEXT "dB", IDC_LABEL_GAIN, 80, 218, 12, 10
EDITTEXT IDC_EDIT_GAIN, 96, 189, 40, 13, ES_AUTOHSCROLL EDITTEXT IDC_EDIT_GAIN, 96, 215, 40, 13, ES_AUTOHSCROLL
LTEXT "", IDC_LABEL_GAIN_STATUS, 145, 192, 171, 10 LTEXT "", IDC_LABEL_GAIN_STATUS, 145, 218, 171, 10
LTEXT "双耳渲染后可能超过 0 dBFS,可用负值衰减。", LTEXT "双耳渲染后可能超过 0 dBFS,可用负值衰减。",
IDC_LABEL_GAIN_HINT, 16, 208, 300, 10 IDC_LABEL_GAIN_HINT, 16, 234, 300, 10
GROUPBOX "其它", IDC_GROUP_MISC, 7, 229, 316, 38 GROUPBOX "其它", IDC_GROUP_MISC, 7, 255, 316, 38
LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 16, 245, 44, 10 LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 16, 271, 44, 10
EDITTEXT IDC_EDIT_FFMPEG, 64, 242, 210, 13, ES_AUTOHSCROLL EDITTEXT IDC_EDIT_FFMPEG, 64, 268, 210, 13, ES_AUTOHSCROLL
PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 279, 242, 38, 13 PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 279, 268, 38, 13
END END
+3
View File
@@ -13,6 +13,9 @@
#define IDC_LABEL_TAIL 2017 #define IDC_LABEL_TAIL 2017
#define IDC_EDIT_TAIL 2018 #define IDC_EDIT_TAIL 2018
#define IDC_LABEL_OUTPUT_STATUS 2019 #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_GROUP_HRTF 2020
#define IDC_RADIO_HRTF_SOFA 2021 #define IDC_RADIO_HRTF_SOFA 2021
+23
View File
@@ -6,6 +6,7 @@
#include <windows.h> #include <windows.h>
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #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 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 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 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 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 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}}; 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); cfg_int g_gain_mdb(kGuidGain, 0);
// milli-seconds // milli-seconds
cfg_uint g_tail_ms(kGuidTail, 5000); 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_object_delay(kGuidObjectDelay, 1473);
cfg_uint g_threads(kGuidThreads, 0); cfg_uint g_threads(kGuidThreads, 0);
cfg_string g_ffmpeg(kGuidFfmpeg, "ffmpeg"); 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) { std::string environment(const char* name) {
char buffer[4096] = {}; char buffer[4096] = {};
const DWORD length = GetEnvironmentVariableA(name, buffer, sizeof(buffer)); 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_enabled = static_cast<bool>(g_gain_enabled);
values.gain_db = static_cast<double>(static_cast<long long>(g_gain_mdb)) / 1000.0; 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.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.object_delay_samples = static_cast<unsigned>(g_object_delay);
values.native_threads = static_cast<unsigned>(g_threads); values.native_threads = static_cast<unsigned>(g_threads);
values.ffmpeg_path = g_ffmpeg.get_ptr(); values.ffmpeg_path = g_ffmpeg.get_ptr();
@@ -124,6 +140,7 @@ void write(const Values& values) {
g_gain_enabled = values.gain_enabled; 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_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_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_object_delay = values.object_delay_samples;
g_threads = values.native_threads; g_threads = values.native_threads;
g_ffmpeg = values.ffmpeg_path.c_str(); g_ffmpeg = values.ffmpeg_path.c_str();
@@ -146,6 +163,7 @@ joc_decode::Settings current() {
(void)env_double("JOC_GAIN_DB", &values.gain_db); (void)env_double("JOC_GAIN_DB", &values.gain_db);
if (env_string("JOC_GAIN_ENABLED", &text)) values.gain_enabled = (text != "0"); if (env_string("JOC_GAIN_ENABLED", &text)) values.gain_enabled = (text != "0");
(void)env_double("JOC_TAIL_SECONDS", &values.tail_seconds); (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_OBJECT_DELAY", &values.object_delay_samples);
(void)env_uint("JOC_THREADS", &values.native_threads); (void)env_uint("JOC_THREADS", &values.native_threads);
@@ -161,6 +179,7 @@ joc_decode::Settings current() {
// attenuation has to be visible and switchable. // attenuation has to be visible and switchable.
settings.gain_db = values.gain_enabled ? values.gain_db : 0.0; settings.gain_db = values.gain_enabled ? values.gain_db : 0.0;
settings.tail_seconds = values.tail_seconds; settings.tail_seconds = values.tail_seconds;
settings.force_int24 = values.force_int24;
settings.object_delay_samples = values.object_delay_samples; settings.object_delay_samples = values.object_delay_samples;
settings.native_threads = values.native_threads; settings.native_threads = values.native_threads;
settings.ffmpeg_path = values.ffmpeg_path.empty() ? "ffmpeg" : values.ffmpeg_path; 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.object_delay_samples, settings.native_threads,
settings.ffmpeg_path.c_str()); 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; return text;
} }
@@ -200,6 +222,7 @@ std::string describe(const Values& values) {
values.binaural_mode, values.gain_enabled ? "" : "(off) ", values.gain_db, values.binaural_mode, values.gain_enabled ? "" : "(off) ", values.gain_db,
values.tail_seconds, values.object_delay_samples, values.native_threads, values.tail_seconds, values.object_delay_samples, values.native_threads,
values.ffmpeg_path.c_str()); values.ffmpeg_path.c_str());
append_to(text, values.force_int24 ? " int24=forced" : "");
return text; return text;
} }
+2
View File
@@ -40,6 +40,8 @@ struct Values {
bool gain_enabled = true; bool gain_enabled = true;
double gain_db = 0.0; double gain_db = 0.0;
double tail_seconds = 5.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 object_delay_samples = 1473;
unsigned native_threads = 0; unsigned native_threads = 0;
std::string ffmpeg_path = "ffmpeg"; 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 // A control the user can operate: static text and group boxes answer WM_NCHITTEST
// with HTTRANSPARENT, so a real click passes straight through them. // 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) { bool is_interactive(HWND window) {
const std::string window_class = class_of(window); const std::string window_class = class_of(window);
if (window_class == "Edit" || window_class == "ComboBox") return true; if (window_class == "Edit" || window_class == "ComboBox") return true;
if (window_class != "Button") return false; if (window_class != "Button") return false;
const LONG_PTR style = GetWindowLongPtrW(window, GWL_STYLE); return !is_group_box_window(window);
return (style & BS_GROUPBOX) == 0;
} }
// True when another interactive sibling sits above the control at that point -- // 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 // Only controls the user is meant to operate count as problems: static
// text and group boxes are transparent to the mouse by design. // text and group boxes are transparent to the mouse by design.
const LONG_PTR control_style = GetWindowLongPtrW(control.window, GWL_STYLE); const bool group_box = is_group_box_window(control.window);
const bool is_group_box = const bool is_label = control.window_class == "Static";
control.window_class == "Button" && (control_style & BS_GROUPBOX) != 0; const bool interactive = !group_box && !is_label;
const bool is_label = control.window_class == "Static" && !is_group_box;
const bool interactive = !is_group_box && !is_label;
const bool bad = interactive && (!enabled || !inside || !hits_self); const bool bad = interactive && (!enabled || !inside || !hits_self);
if (bad) ++problems; if (bad) ++problems;
+56 -16
View File
@@ -4,6 +4,7 @@
// //
// render_harness --input <file.eac3> --output <out.wav> [options] // render_harness --input <file.eac3> --output <out.wav> [options]
// --mode binaural|speaker default binaural // --mode binaural|speaker default binaural
// --format float32|int24 delivered sample format (default float32)
// --layout NAME speaker layout (default 7.1) // --layout NAME speaker layout (default 7.1)
// --hrtf-source sofa|rosella kind of model --hrtf points at (default sofa) // --hrtf-source sofa|rosella kind of model --hrtf points at (default sofa)
// --hrtf PATH HRTF to render with; supplied by the caller and // --hrtf PATH HRTF to render with; supplied by the caller and
@@ -36,8 +37,14 @@
// It exists because the plugin's engine (src/joc_decode.*) has no foobar2000 // 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, against the // 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 // reference renderer or against the same engine's continuous render. The WAV header
// mirrors the renderer's own writer: a simple IEEE-float header up to two channels, // mirrors the renderer's own writer: a simple header up to two channels,
// WAVE_FORMAT_EXTENSIBLE above that with a channel mask of zero. // 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 <cstdio>
#include <thread> #include <thread>
@@ -54,6 +61,7 @@ struct Options {
std::string input; std::string input;
std::string output; std::string output;
std::string mode = "binaural"; std::string mode = "binaural";
std::string format = "float32"; // float32 | int24
std::string layout = "7.1"; std::string layout = "7.1";
std::string hrtf; // SOFA file, or Rosella model file std::string hrtf; // SOFA file, or Rosella model file
std::string hrtf_source = "sofa"; // sofa | rosella std::string hrtf_source = "sofa"; // sofa | rosella
@@ -83,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)); 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. // 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) { std::string wav_header(unsigned channels, unsigned rate, std::uint64_t frames, bool int24) {
const unsigned bytes_per_sample = 4; 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 unsigned block_align = channels * bytes_per_sample;
const std::uint64_t data_bytes = frames * block_align; const std::uint64_t data_bytes = frames * block_align;
std::string fmt; std::string fmt;
if (channels <= 2) { 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_u16(&fmt, channels);
put_u32(&fmt, rate); put_u32(&fmt, rate);
put_u32(&fmt, rate * block_align); put_u32(&fmt, rate * block_align);
put_u16(&fmt, block_align); put_u16(&fmt, block_align);
put_u16(&fmt, 8 * bytes_per_sample); put_u16(&fmt, bits_per_sample);
} else { } else {
put_u16(&fmt, 0xFFFE); // WAVE_FORMAT_EXTENSIBLE put_u16(&fmt, 0xFFFE); // WAVE_FORMAT_EXTENSIBLE
put_u16(&fmt, channels); put_u16(&fmt, channels);
put_u32(&fmt, rate); put_u32(&fmt, rate);
put_u32(&fmt, rate * block_align); put_u32(&fmt, rate * block_align);
put_u16(&fmt, 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, 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 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, static const unsigned char kFloatGuid[16] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}; 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; std::string header;
@@ -131,6 +143,7 @@ bool parse(int argc, char** argv, Options* options) {
if (arg == "--input") options->input = next(); if (arg == "--input") options->input = next();
else if (arg == "--output") options->output = next(); else if (arg == "--output") options->output = next();
else if (arg == "--mode") options->mode = 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 == "--layout") options->layout = next();
else if (arg == "--hrtf") options->hrtf = next(); else if (arg == "--hrtf") options->hrtf = next();
else if (arg == "--hrtf-source") options->hrtf_source = next(); else if (arg == "--hrtf-source") options->hrtf_source = next();
@@ -165,6 +178,10 @@ bool parse(int argc, char** argv, Options* options) {
return false; 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(); return !options->input.empty() && !options->output.empty();
} }
@@ -190,13 +207,18 @@ bool render_once(const joc_decode::Settings& settings, const Options& options,
return false; return false;
} }
// The header carries the length, so write a placeholder and come back to it. // The header carries the length, so write a placeholder and come back to it.
const std::string header = wav_header(channels, 48000, 0); const bool int24 = settings.force_int24;
const std::string header = wav_header(channels, 48000, 0, int24);
std::fwrite(header.data(), 1, header.size(), file); std::fwrite(header.data(), 1, header.size(), file);
constexpr std::size_t kChunk = 4096; constexpr std::size_t kChunk = 4096;
std::vector<float> buffer(kChunk * channels); std::vector<float> buffer(kChunk * channels);
std::vector<std::uint8_t> packed;
std::uint64_t frames_written = 0; std::uint64_t frames_written = 0;
double peak = 0.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); std::vector<char> applied(options.seeks_at.size(), 0);
for (;;) { for (;;) {
for (std::size_t i = 0; i < options.seeks_at.size(); ++i) { for (std::size_t i = 0; i < options.seeks_at.size(); ++i) {
@@ -226,19 +248,34 @@ bool render_once(const joc_decode::Settings& settings, const Options& options,
: static_cast<double>(buffer[i]); : static_cast<double>(buffer[i]);
if (value > peak) peak = value; if (value > peak) peak = value;
} }
std::fwrite(buffer.data(), sizeof(float), frames * channels, file); 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; frames_written += frames;
} }
engine.stop(); engine.stop();
const std::string final_header = wav_header(channels, 48000, frames_written); const std::string final_header = wav_header(channels, 48000, frames_written, int24);
std::fseek(file, 0, SEEK_SET); std::fseek(file, 0, SEEK_SET);
std::fwrite(final_header.data(), 1, final_header.size(), file); std::fwrite(final_header.data(), 1, final_header.size(), file);
std::fclose(file); std::fclose(file);
std::printf("render_harness: %s -> %s\n", options.input.c_str(), output.c_str()); std::printf("render_harness: %s -> %s\n", options.input.c_str(), output.c_str());
std::printf(" channels=%u frames=%llu peak=%.9f\n", channels, std::printf(" channels=%u frames=%llu format=%s peak=%.9f\n", channels,
static_cast<unsigned long long>(frames_written), peak); 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; return true;
} }
@@ -253,7 +290,8 @@ int main(int argc, char** argv) {
if (!parse(argc, argv, &options)) { if (!parse(argc, argv, &options)) {
std::fprintf(stderr, std::fprintf(stderr,
"usage: render_harness --input <eac3> --output <wav> [--mode binaural|speaker]\n" "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" " [--gain-db X] [--tail S] [--ffmpeg path] [--max-frames N]\n"
" [--max-input-frames N] [--seek-to S] [--seek-at N:S]\n" " [--max-input-frames N] [--seek-to S] [--seek-at N:S]\n"
" [--total-seconds S] [--pipeline-delay N] [--preroll-frames N]\n" " [--total-seconds S] [--pipeline-delay N] [--preroll-frames N]\n"
@@ -279,6 +317,8 @@ int main(int argc, char** argv) {
settings.input_kind = options.container ? joc_decode::InputKind::kContainer settings.input_kind = options.container ? joc_decode::InputKind::kContainer
: joc_decode::InputKind::kBare; : joc_decode::InputKind::kBare;
settings.audio_index = options.audio_index; 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");
std::string error; std::string error;
if (options.parallel != 0) { if (options.parallel != 0) {
+2
View File
@@ -40,6 +40,8 @@ $lines = @(
"cd /d `"$projectRoot`"", "cd /d `"$projectRoot`"",
# log.cpp comes along because the engine writes its diagnostics through it. # 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\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\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\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", "cl $common /Fe:`"$outDir\scan_crosscheck.exe`" /Fo:`"$outDir\\`" tests\scan_crosscheck.cpp src\eac3_scan.cpp `"$coreLib`" shell32.lib",
+4 -1
View File
@@ -39,7 +39,10 @@ if (Test-Path -LiteralPath $running) {
} }
$arguments = @() $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 $process = Start-Process -FilePath $exe -ArgumentList $arguments -PassThru
Write-Host "started pid=$($process.Id) $($Play -join ', ')" Write-Host "started pid=$($process.Id) $($Play -join ', ')"
Start-Sleep -Seconds $WaitSeconds Start-Sleep -Seconds $WaitSeconds