Support JOC downmix configurations 0..4 and update the vendored kernel
build / windows (push) Has been cancelled
build / release (push) Has been cancelled

This commit is contained in:
TheM14 committed 2026-10-16 19:56:00 +08:00
1 parent cb840af52f
commit 2cfebe914d
32 files changed
+2055 -300

No files matched your search

+7 -2
View File
@@ -3,8 +3,9 @@
[简体中文](README.md) · [Install](#install) · [Settings](#settings) · [Building](#building) · [Known limitations](#known-limitations)
A foobar2000 input component for **E-AC-3 JOC (Dolby Atmos)** files: the JOC object and OAMD
metadata is taken from the E-AC-3 syncframes and paired with the 5.1 core PCM that ffmpeg
decodes, then rendered in real time to headphones (HRTF) or to a speaker layout up to 7.1. The
metadata is taken from the E-AC-3 syncframes and paired with the core PCM that ffmpeg decodes
(5.1 or 7 channels, following $joc\_dmx\_config\_idx$), then rendered in real time to headphones
(HRTF) or to a speaker layout up to 7.1. The
rendering core is compiled into the component, so there is nothing to install beside
`foo_input_joc.dll`.
@@ -112,6 +113,10 @@ every container in the library, which is where the volume comes from.
## Known limitations
* ADM BWF output is not implemented.
* All five $joc\_dmx\_config\_idx$ configurations are supported: the bed is paired per audio
frame, and its channel count follows the configuration (6 channels for 0/3, 8 for 1/2/4), with
`-ac 6` passed to ffmpeg only for the 6-channel ones. An AC-3 core plus a dependent substream
(`strmtyp=1`) and a first audio frame without JOC are both normal cases.
* A container is only claimed when this component is ahead of the built-in container reader in
Preferences → Decoding (see [Install](#containers-need-one-look-at-the-decoder-list)); the core
does not let a decoder ask for a file another entry has already taken. Such a file's tags stay
+4 -1
View File
@@ -3,7 +3,7 @@
[English](README.en.md) · [安装](#安装) · [设置](#设置) · [构建](#构建) · [已知限制](#已知限制)
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` 之外不需要安装任何东西。
输入是裸流 `.eac3` / `.ec3`,或容器里的 E-AC-3 JOC 轨道(`.mp4` `.m4a` `.m4b` `.m4p`
@@ -95,6 +95,9 @@ pwsh -File tools/package.ps1 # 两个架构,打包到 dist\*.fb2k-c
## 已知限制
* 不实现 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)**能读不能写**:没有
+8
View File
@@ -30,6 +30,14 @@ $$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qqua
相同数值可在 FFmpeg(`aacps_tablegen.h`、`aacsbrdata.h`)等公开实现中查到。
### AC-3 / E-AC-3 传输层与通道码表
`kernel/src/eac3_transport/eac3_reader.cpp` 的 AC-3 同步帧长度表由 A/52 的
`fscod`/`frmsizecod` 比特率表换算得到(48/44.1/32 kHz 三列),E-AC-3 帧长用标准
`frmsiz` 字段;`kernel/src/joc_bitstream/joc_parser.cpp` 的 16 bit `chanmap` 位表与
A/52 `acmod` 通道序来自 **ATSC A/52 Annex E**(等同 ETSI TS 102 366 Annex E)的
Table E2.5 / `chanmap` 定义。这些表是按公开标准写的数值,不含任何第三方实现代码。
## HRTF 数据与 `.jochrtf`
`.jochrtf` 含有特定源 SOFA/HRTF 数据集的变换系数与 delay;其使用、复制与再分发仍受源
+19 -12
View File
@@ -23,11 +23,12 @@ extern "C" {
#endif
enum {
EJOC_ABI_VERSION = 1,
EJOC_ABI_VERSION = 2,
EJOC_FRAME_SAMPLES = 1536,
EJOC_TIMESLOTS = 24,
EJOC_SUBBANDS = 64,
EJOC_CORE_CHANNELS = 5,
EJOC_CORE_CHANNELS = 5, /* downmix configurations 0 and 3 */
EJOC_MAX_CORE_CHANNELS = 7, /* downmix configurations 1, 2 and 4 */
EJOC_OUTPUT_CHANNELS = 16,
EJOC_MAX_OBJECTS = 15,
EJOC_MAX_DPOINTS = 2,
@@ -47,15 +48,18 @@ typedef void* ejoc_binaural_renderer_handle;
/*
Fixed array layouts used by ejoc_renderer_process():
bed5_planar [5][1536]
bed_planar [core_channel_count][1536]
lfe [1536] or NULL
n_bands [15]
n_dpoints [15]
slope_idx [15]
offset_ts [15][2]
dq [15][2][5][23]
dq [15][2][7][23]
output16 [16][1536]
core_channel_count comes from ejoc_renderer_set_core_channels() and defaults to
EJOC_CORE_CHANNELS; the dq stride is always EJOC_MAX_CORE_CHANNELS.
Only objects selected by object_mask are read from the descriptor arrays.
dq carries already dequantized matrix coefficients in double precision; the
caller performs the JOC bitstream differential decoding for both dense and
@@ -69,21 +73,24 @@ EJOC_API void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads);
/*
Enables or disables the Ls/Rs band-0 21-tap DC compensation. The caller derives
it from the JOC downmix configuration: only configurations 3 and 4 enable the
filter. When disabled, band 0 keeps the common per-band processing (surround
delay plus -j rotation) instead of being overwritten by the FIR. The delay line
and DC history advance either way, so the flag may change between frames.
Defaults to enabled when never called.
core_channel_count comes from joc_dmx_config_idx: 5 for configurations 0 and 3,
7 for 1, 2 and 4. bed_planar is then [core_channel_count][1536] and only channels
below core_channel_count are read from dq, whose stride stays [15][2][7][23].
phase_shift is sigma(k) = (k in {3,4}): it enables the Ls/Rs -j rotation of
subbands above 0 together with the band-0 21-tap DC compensation. With the switch
off, channels 3 and 4 only take the common ten-slot surround delay. The delay
line and DC history advance either way, so the flag may change between frames.
*/
EJOC_API int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled);
EJOC_API uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle);
EJOC_API const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle);
EJOC_API int EJOC_CALL ejoc_renderer_process(
ejoc_renderer_handle handle,
const float* bed5_planar,
const float* bed_planar,
const float* lfe,
uint32_t core_channel_count,
uint32_t phase_shift,
uint32_t object_mask,
const uint8_t* n_bands,
const uint8_t* n_dpoints,
+4 -2
View File
@@ -34,7 +34,9 @@
#include <stdint.h>
#include <stddef.h>
#define JOC_ABI_VERSION 3u
#include "joc_version.h"
#define JOC_ABI_VERSION 5u
#define JOC_FRAME_PARAMS_VERSION 1u
#define JOC_EMDF_INFO_VERSION 1u
#define JOC_TASK_CONFIG_VERSION 3u
@@ -193,7 +195,7 @@ typedef struct joc_frame_params {
uint8_t n_channels; /* 5 or 7 */
uint8_t clipgain_x_bits;
uint8_t clipgain_y_bits;
uint8_t reserved;
uint8_t objects_clamped; /* num_objects_bits+1 exceeded 15 */
uint32_t seq_count; /* 10-bit JOC sequence counter, parsed only */
uint32_t present_mask; /* bit i == object i present */
uint32_t data_end_bits; /* bit position just after joc_data */
+10
View File
@@ -102,6 +102,16 @@ typedef struct joc_stream_status_info {
uint64_t buffered_samples; /* rendered but not yet pulled, per channel */
uint64_t oamd_payloads;
uint64_t oamd_transitions;
/* Metadata the renderer had to work around, per audio frame: no JOC payload at
* all, a declared object count clamped to 15, and a syncframe carrying a second
* EMDF container that is not this frame's JOC metadata. */
uint64_t missing_joc_frames;
uint64_t objects_clamped_frames;
uint64_t extra_container_frames;
/* Extra-pair identity disagreements across layers (spec 11.2 checks 1 and 2).
* The identity is metadata only, so those frames still render. */
uint64_t chanmap_pair_mismatch_frames;
uint64_t bed_mask_mismatch_frames;
uint32_t output_channels;
uint32_t ended; /* 1 after flush() */
} joc_stream_status_info;
+16
View File
@@ -0,0 +1,16 @@
/* Product version of joc_core and joc_cli.
*
* This file is the single source of truth: CMakeLists.txt reads these numbers for
* project(VERSION ...), joc_version_string() returns JOC_VERSION_STRING, and the
* release workflow refuses a tag that does not match it. Bump the three numbers
* and the string together, then tag the commit with v<JOC_VERSION_STRING>.
*/
#ifndef JOC_VERSION_H
#define JOC_VERSION_H
#define JOC_VERSION_MAJOR 0
#define JOC_VERSION_MINOR 1
#define JOC_VERSION_PATCH 0
#define JOC_VERSION_STRING "0.1.0"
#endif /* JOC_VERSION_H */
+1
View File
@@ -219,6 +219,7 @@
<ClInclude Include="include\eac3joc_core.h" />
<ClInclude Include="include\joc_core.h" />
<ClInclude Include="include\joc_stream.h" />
<ClInclude Include="include\joc_version.h" />
<ClInclude Include="src\adm\adm_metadata.h" />
<ClInclude Include="src\adm\adm_tracks.h" />
<ClInclude Include="src\binaural\binaural_runtime.h" />
+3 -11
View File
@@ -11,16 +11,8 @@
namespace {
thread_local std::string g_detail;
joc_error finish(const joc::Status& status) {
if (status.ok()) {
g_detail.clear();
return JOC_OK;
}
g_detail.assign(status.stage());
g_detail.append(": ");
g_detail.append(status.message());
joc::record_error_detail(status);
return status.code();
}
@@ -49,7 +41,7 @@ extern "C" {
std::uint32_t JOC_CALL joc_abi_version(void) { return JOC_ABI_VERSION; }
const char* JOC_CALL joc_version_string(void) { return "0.1.0-m1"; }
const char* JOC_CALL joc_version_string(void) { return JOC_VERSION_STRING; }
std::uint32_t JOC_CALL joc_event_size(void) { return static_cast<std::uint32_t>(sizeof(joc_event)); }
std::uint32_t JOC_CALL joc_task_config_size(void) {
@@ -167,7 +159,7 @@ const char* JOC_CALL joc_error_stage(joc_error code) {
}
}
const char* JOC_CALL joc_last_error_detail(void) { return g_detail.c_str(); }
const char* JOC_CALL joc_last_error_detail(void) { return joc::last_error_detail.c_str(); }
joc_error JOC_CALL joc_parse_id14(const std::uint8_t* payload, std::size_t payload_size,
joc_frame_params* out_params) {
+12 -2
View File
@@ -13,6 +13,9 @@ struct joc_stream {
namespace {
joc_error finish_stream(const joc::Status& status) {
// The structured detail is not dropped here: joc_last_error_detail() reports
// it for the streaming entry points as well as for the task API.
joc::record_error_detail(status);
return status.code();
}
@@ -93,12 +96,14 @@ joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer*
return pushed;
}
std::size_t consumed = 0;
const std::uint32_t channels = input->channels != 0u ? input->channels : 6u;
const joc::Status pushed =
stream->instance.push_bed(input->pcm, input->sample_count, &consumed);
stream->instance.push_bed(input->pcm, input->sample_count, channels, &consumed);
if (consumed_samples != nullptr) {
*consumed_samples = static_cast<std::uint32_t>(consumed);
}
return pushed; }();
return pushed;
}();
return finish_stream(status);
}
@@ -149,6 +154,11 @@ joc_error JOC_CALL joc_stream_status(const joc_stream* stream, joc_stream_status
out->buffered_samples = stream->instance.buffered_samples();
out->oamd_payloads = info.oamd_payloads;
out->oamd_transitions = info.oamd_transitions;
out->missing_joc_frames = info.missing_joc_frames;
out->objects_clamped_frames = info.objects_clamped_frames;
out->extra_container_frames = info.extra_container_frames;
out->chanmap_pair_mismatch_frames = info.chanmap_pair_mismatch_frames;
out->bed_mask_mismatch_frames = info.bed_mask_mismatch_frames;
out->output_channels = info.output_channels;
out->ended = info.ended;
return JOC_OK;
+9
View File
@@ -80,6 +80,7 @@ struct Options {
bool dry_run = false;
bool quiet = false;
bool help = false;
bool version = false;
};
const char* kLayoutChoices =
@@ -88,6 +89,8 @@ const char* kLayoutChoices =
void print_usage() {
std::printf(
"usage: joc_cli [options] input\n"
" -h, --help 显示本帮助\n"
" --version 显示版本并退出\n"
"\n"
"JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、扬声器 WAV 或双耳 WAV\n"
"\n"
@@ -210,6 +213,7 @@ void parse_args(const std::vector<std::string>& arguments, Options* options) {
for (int index = 1; index < argc; ++index) {
const std::string arg = arguments[static_cast<std::size_t>(index)];
if (arg == "-h" || arg == "--help") { options->help = true; }
else if (arg == "--version") { options->version = true; }
else if (arg == "-o" || arg == "--output") { options->output = require_value(arguments, &index); }
else if (arg == "--speaker-output") { options->speaker_output = require_value(arguments, &index); }
else if (arg == "--binaural-output") { options->binaural_output = require_value(arguments, &index); }
@@ -530,6 +534,11 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "joc_cli: error: %s\n", error.what());
return 2;
}
if (options.version) {
std::printf("joc_cli %s (joc_core ABI %u)\n", joc_version_string(),
static_cast<unsigned>(joc_abi_version()));
return 0;
}
if (options.help) {
print_usage();
return 0;
+125 -29
View File
@@ -7,9 +7,111 @@
namespace joc::eac3 {
namespace {
constexpr std::size_t kHeaderBytes = 4;
constexpr std::size_t kHeaderBytesAc3 = 6;
// AC-3 syncframe length in 16-bit words, indexed by fscod (0 = 48 kHz,
// 1 = 44.1 kHz, 2 = 32 kHz) and then frmsizecod (A/52 5.4.1.2).
constexpr std::uint16_t kAc3FrameWords[3][38] = {
{64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
896, 896, 1024, 1024, 1152, 1152, 1280, 1280},
{69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
975, 975, 1114, 1114, 1253, 1253, 1393, 1393},
{96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920},
};
std::uint32_t read_bits(const std::uint8_t* data, std::size_t start, std::size_t count) {
std::uint32_t value = 0;
for (std::size_t index = start; index < start + count; ++index) {
const std::uint32_t bit = (data[index >> 3] >> (7u - (index & 7u))) & 1u;
value = (value << 1) | bit;
}
return value;
}
} // namespace
bool parse_syncframe_header(const std::uint8_t* data, std::size_t size, std::size_t offset,
Frame* out) {
if (data == nullptr || offset + kHeaderBytesAc3 > size) {
return false;
}
if (static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) |
data[offset + 1]) != FrameReader::kSyncword) {
return false;
}
Frame frame;
frame.data = data + offset;
frame.offset = offset;
const std::size_t base = offset * 8u;
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
frame.bsid = static_cast<std::uint8_t>(bsid);
if (bsid <= 8u) {
frame.codec = 0;
const std::uint32_t fscod = read_bits(data, base + 32u, 2u);
const std::uint32_t frmsizecod = read_bits(data, base + 34u, 6u);
if (fscod > 2u || frmsizecod >= 38u) {
return false;
}
frame.size = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 48u, 3u));
std::size_t bit = 51u;
if ((frame.acmod & 0x1u) != 0u && frame.acmod != 0x1u) {
bit += 2u; // cmixlev
}
if ((frame.acmod & 0x4u) != 0u) {
bit += 2u; // surmixlev
}
if (frame.acmod == 0x2u) {
bit += 2u; // dsurmod
}
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + bit, 1u));
if (out != nullptr) {
*out = frame;
}
return true;
}
frame.codec = 1;
frame.strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
frame.substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
const std::uint32_t frmsiz = read_bits(data, base + 21u, 11u);
frame.size = (static_cast<std::size_t>(frmsiz) + 1u) * 2u;
// fscod==3 also consumes two bits (fscod2), so acmod always starts at bit 36.
frame.acmod = static_cast<std::uint8_t>(read_bits(data, base + 36u, 3u));
frame.lfeon = static_cast<std::uint8_t>(read_bits(data, base + 39u, 1u));
std::size_t bit = 45u + 5u; // bsid, then dialnorm
const std::uint32_t compre = read_bits(data, base + bit, 1u);
++bit;
if (compre != 0u) {
bit += 8u; // compr
}
if (frame.acmod == 0u) {
bit += 5u; // dialnorm2
const std::uint32_t compr2 = read_bits(data, base + bit, 1u);
++bit;
if (compr2 != 0u) {
bit += 8u;
}
}
if (frame.strmtyp == 1u) {
if (read_bits(data, base + bit, 1u) != 0u) {
frame.has_chanmap = 1;
frame.chanmap = static_cast<std::uint16_t>(read_bits(data, base + bit + 1u, 16u));
}
}
if (out != nullptr) {
*out = frame;
}
return true;
}
void FrameReader::push(const std::uint8_t* data, std::size_t size) {
if (failed_ || data == nullptr || size == 0) {
return;
@@ -45,44 +147,43 @@ FrameReader::Next FrameReader::next(Frame* out) {
}
const std::uint8_t* p = buffer_.data() + consumed_;
// The reference implementation rejects a frame whose header does not fit,
// rather than silently resynchronising on the next 0x0B77.
// A frame whose header does not fit is rejected rather than silently
// resynchronised on the next 0x0B77.
if (available < kHeaderBytes) {
if (finished_) {
fail(JOC_ERR_EAC3_SYNCFRAME, "E-AC-3 syncframe header truncated at end of input");
fail(JOC_ERR_EAC3_SYNCFRAME, "syncframe header truncated at end of input");
return Next::Fail;
}
return Next::End;
}
const std::uint16_t syncword = static_cast<std::uint16_t>((static_cast<std::uint16_t>(p[0]) << 8) | p[1]);
if (syncword != kSyncword) {
fail(JOC_ERR_EAC3_SYNCFRAME, "invalid E-AC-3 syncword (silent resynchronisation is not allowed)");
Frame frame;
if (!parse_syncframe_header(p, available, 0, &frame) || frame.size == 0u) {
if (available < kHeaderBytesAc3 && !finished_) {
return Next::End;
}
fail(JOC_ERR_EAC3_SYNCFRAME,
"invalid syncframe header (silent resynchronisation is not allowed)");
return Next::Fail;
}
// frmsiz: 11 bits spread over the low 3 bits of byte 2 and all of byte 3,
const std::size_t words =
static_cast<std::size_t>(((p[2] & 0x07u) << 8) | p[3]) + 1u;
const std::size_t frame_bytes = words * 2u;
if (frame_bytes > available) {
if (frame.size > available) {
if (!finished_) {
return Next::End;
}
fail(JOC_ERR_BITSTREAM_TRUNCATED,
"last E-AC-3 syncframe extends past end of input (declared " +
std::to_string(frame_bytes) + " bytes, remaining " +
"last syncframe extends past end of input (declared " +
std::to_string(frame.size) + " bytes, remaining " +
std::to_string(available) + ")");
return Next::Fail;
}
if (out != nullptr) {
out->data = p;
out->size = frame_bytes;
out->offset = base_offset_ + consumed_;
frame.data = p;
frame.offset = base_offset_ + consumed_;
*out = frame;
}
consumed_ += frame_bytes;
consumed_ += frame.size;
stream_offset_ = base_offset_ + consumed_;
++frames_emitted_;
return Next::Ok;
@@ -93,20 +194,15 @@ joc_error FrameReader::frame_bytes(const std::uint8_t* data, std::size_t size, s
if (data == nullptr || out_frame_bytes == nullptr) {
return JOC_ERR_INVALID_ARGUMENT;
}
if (offset + kHeaderBytes > size) {
return JOC_ERR_EAC3_SYNCFRAME;
Frame frame;
if (!parse_syncframe_header(data, size, offset, &frame)) {
return offset + kHeaderBytes > size ? JOC_ERR_EAC3_SYNCFRAME
: JOC_ERR_EAC3_SYNCFRAME;
}
if (static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]) !=
kSyncword) {
return JOC_ERR_EAC3_SYNCFRAME;
}
const std::size_t words =
static_cast<std::size_t>(((data[offset + 2] & 0x07u) << 8) | data[offset + 3]) + 1u;
const std::size_t frame_bytes = words * 2u;
if (offset + frame_bytes > size) {
if (offset + frame.size > size) {
return JOC_ERR_BITSTREAM_TRUNCATED;
}
*out_frame_bytes = frame_bytes;
*out_frame_bytes = frame.size;
return JOC_OK;
}
+22 -1
View File
@@ -1,4 +1,3 @@
#pragma once
#include <cstddef>
@@ -10,12 +9,34 @@
namespace joc::eac3 {
// One syncframe plus its transport-layer (L2) header. codec/strmtyp/substreamid
// identify the substream: within one audio frame each combination appears once.
struct Frame {
const std::uint8_t* data = nullptr;
std::size_t size = 0;
std::size_t offset = 0; // byte offset of the frame start in the fed stream
std::uint8_t codec = 0; // 0 = AC-3, 1 = E-AC-3
std::uint8_t strmtyp = 0;
std::uint8_t substreamid = 0;
std::uint8_t acmod = 0;
std::uint8_t lfeon = 0;
std::uint8_t bsid = 0;
std::uint8_t has_chanmap = 0;
std::uint16_t chanmap = 0;
bool same_stream(const Frame& other) const {
return codec == other.codec && strmtyp == other.strmtyp &&
substreamid == other.substreamid;
}
};
// Parses the syncframe header at `offset`. Returns false when the syncword is
// missing or the declared size cannot be derived. `out` may be null to query
// only the size. AC-3 and E-AC-3 are told apart by bsid (bits 40..44): <= 8 is
// AC-3, whose frame size comes from fscod/frmsizecod, everything above is E-AC-3.
bool parse_syncframe_header(const std::uint8_t* data, std::size_t size, std::size_t offset,
Frame* out);
class FrameReader {
public:
enum class Next {
+60 -3
View File
@@ -113,12 +113,16 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi
: syntax_fail(reader.error_message());
}
}
std::uint8_t group_id = 0;
std::uint8_t has_group_id = 0;
if (reader.read(1) != 0u) {
std::uint32_t ignored = 0;
if (!bits::variable_bits(reader, 2, 8, &ignored)) {
std::uint32_t value = 0;
if (!bits::variable_bits(reader, 2, 8, &value)) {
return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED ? truncated_fail(reader)
: syntax_fail(reader.error_message());
}
group_id = static_cast<std::uint8_t>(value);
has_group_id = 1;
}
if (reader.read(1) != 0u) {
if (!reader.skip(8)) {
@@ -160,6 +164,8 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi
Payload& entry = container.payloads[container.payload_count++];
entry.id = static_cast<std::uint8_t>(payload_id);
entry.sample_offset = sample_offset;
entry.group_id = group_id;
entry.has_group_id = has_group_id;
entry.bit_offset = reader.position();
entry.size = payload_size;
@@ -206,7 +212,8 @@ void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector<std:
std::sort(out->begin(), out->end());
}
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out) {
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out,
ContainerPredicate usable, bool* ignored_extra) {
if (data == nullptr || out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null buffer or output");
}
@@ -264,6 +271,31 @@ Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out)
}
if (top_level.size() != 1u) {
// Several containers can be genuine: a real stream carries a second
// container whose ID14 is a different structure altogether. The predicate
// tells those apart; what it leaves ambiguous stays an error.
if (usable != nullptr) {
int best = 0;
const Container* winner = nullptr;
int winners = 0;
for (const Container& candidate : top_level) {
const int score = usable(data, size, candidate);
if (score > best) {
best = score;
winner = &candidate;
winners = 1;
} else if (score == best && score > 0) {
++winners;
}
}
if (best > 0 && winners == 1 && winner != nullptr) {
if (ignored_extra != nullptr) {
*ignored_extra = true;
}
*out = *winner;
return Status::success();
}
}
return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf,
"multiple top-level JOC EMDF containers (" +
std::to_string(top_level.size()) +
@@ -273,6 +305,31 @@ Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out)
return Status::success();
}
Status find_joc_emdf_optional(const std::uint8_t* data, std::size_t size, bool* present,
Container* out, ContainerPredicate usable, bool* ignored_extra) {
if (present == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kEmdf, "null presence flag");
}
const Status status = find_joc_emdf(data, size, out, usable, ignored_extra);
if (status.ok()) {
*present = true;
return status;
}
if (status.code() != JOC_ERR_EMDF_TRANSPORT) {
return status;
}
// find_joc_emdf reports "no usable container in this syncframe" and "several
// usable containers" with the same code. Only the former is normal for a
// core syncframe (audio data can contain bytes that look like a marker);
// an ambiguous frame stays an error.
static constexpr const char* kAmbiguous = "multiple top-level";
if (status.message().rfind(kAmbiguous, 0) == 0) {
return status;
}
*present = false;
return Status::success();
}
void extract_container_bytes(const std::uint8_t* data, std::size_t size, const Container& container,
std::vector<std::uint8_t>* out) {
out->assign(container.raw_size, 0u);
+25 -1
View File
@@ -20,6 +20,11 @@ struct Payload {
std::uint16_t sample_offset = 0;
std::size_t bit_offset = 0; // MSB-first bit position of the payload bytes
std::size_t size = 0; // payload byte count
// Payload configuration data (ETSI TS 102 366 H.2.2.3). Spec section 8.2 and
// its Table 56 associate the OAMD and JOC payloads of one frame by an equal
// group id, which is how several containers in one syncframe are told apart.
std::uint8_t group_id = 0;
std::uint8_t has_group_id = 0;
};
struct Container {
@@ -43,7 +48,26 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi
// All candidate 0x5838 bit offsets over the eight alignments, ascending.
void marker_offsets(const std::uint8_t* data, std::size_t size, std::vector<std::size_t>* out);
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out);
// Scores how well a candidate container can serve as the frame's metadata when a
// syncframe carries more than one: 0 = not at all, higher = more credible. A null
// predicate keeps the strict behaviour: several top-level candidates are an error,
// never a choice.
using ContainerPredicate = int (*)(const std::uint8_t* data, std::size_t size,
const Container& container);
// When a syncframe holds several top-level candidates and `usable` leaves exactly
// one with the highest non-zero score, that one is returned and `ignored_extra` is
// set; otherwise the frame stays an ambiguity error.
Status find_joc_emdf(const std::uint8_t* data, std::size_t size, Container* out,
ContainerPredicate usable = nullptr, bool* ignored_extra = nullptr);
// Like find_joc_emdf(), but a syncframe that carries no EMDF marker at all is a
// normal "absent" result rather than an error. A syncframe that does carry
// markers but no usable JOC container still fails, so a real transport problem
// is never mistaken for a core substream.
Status find_joc_emdf_optional(const std::uint8_t* data, std::size_t size, bool* present,
Container* out, ContainerPredicate usable = nullptr,
bool* ignored_extra = nullptr);
// Extract the container's bytes exactly as the bit reader sees them (identical
// to a memcpy for byte-aligned containers).
+2 -2
View File
@@ -21,8 +21,8 @@ bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups, std::
}
value = (value + 1u) << width;
}
// Same failure mode as the reference implementation: an extension chain
// that never terminates is a syntax error, not a truncation.
// An extension chain that never terminates is a syntax error, not a
// truncation.
reader.fail(JOC_ERR_EMDF_SYNTAX, "variable_bits extension groups exceeded");
return false;
}
+16
View File
@@ -33,6 +33,22 @@ private:
std::string message_;
};
// Structured detail of the most recent failing call, as returned by
// joc_last_error_detail(). It is one instance per thread across the whole
// library, so every C entry point -- the task API and the streaming API alike --
// reports through the same string.
inline thread_local std::string last_error_detail;
inline void record_error_detail(const Status& status) {
if (status.ok()) {
last_error_detail.clear();
return;
}
last_error_detail.assign(status.stage());
last_error_detail.append(": ");
last_error_detail.append(status.message());
}
// Stage names are kept as plain literals so that C++ and the Python frontend
namespace stage {
inline constexpr const char* kFoundation = "foundation";
+544 -12
View File
@@ -199,28 +199,25 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra
out->num_objects_bits = static_cast<std::uint8_t>(reader.read(6));
out->ext_config_idx = static_cast<std::uint8_t>(reader.read(3));
const std::uint32_t n_objects = static_cast<std::uint32_t>(out->num_objects_bits) + 1u;
const std::uint32_t declared_objects = static_cast<std::uint32_t>(out->num_objects_bits) + 1u;
const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx);
if (n_channels < 0) {
return syntax_fail("unknown JOC downmix configuration " +
std::to_string(out->dmx_config_idx));
}
out->n_channels = static_cast<std::uint8_t>(n_channels);
if (n_objects > JOC_MAX_OBJECTS) {
// The reference implementation has no check here and fails later inside
// NumPy; the port reports it explicitly (plan 28.2).
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kJoc,
"JOC frame declares " + std::to_string(n_objects) +
" objects, the ABI supports at most " +
std::to_string(static_cast<int>(JOC_MAX_OBJECTS)));
}
// A declared object count above 15 is clamped to 15: the frame is parsed for
// 15 objects only, and the clamp is reported separately.
out->objects_clamped = declared_objects > JOC_MAX_OBJECTS ? 1u : 0u;
const std::uint32_t n_objects =
std::min<std::uint32_t>(declared_objects, JOC_MAX_OBJECTS);
out->n_objects = static_cast<std::uint8_t>(n_objects);
out->clipgain_x_bits = static_cast<std::uint8_t>(reader.read(3));
out->clipgain_y_bits = static_cast<std::uint8_t>(reader.read(5));
out->seq_count = reader.read(10);
// clipgain = 1 + (y/32) * 2^(x-4). The reference multiplies by an exact
// exactly for the whole legal range.
// clipgain = 1 + (y/32) * 2^(x-4); ldexp builds the power of two exactly
// over the whole legal range.
out->clipgain = 1.0 + static_cast<double>(out->clipgain_y_bits) / 32.0 *
std::ldexp(1.0, static_cast<int>(out->clipgain_x_bits) - 4);
@@ -361,13 +358,36 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra
return Status::success();
}
int score_container_as_joc(const std::uint8_t* data, std::size_t size,
const emdf::Container& container) {
const emdf::Payload* payload = container.find(emdf::kIdJoc);
const emdf::Payload* oamd = container.find(emdf::kIdOamd);
if (payload == nullptr || oamd == nullptr) {
return 0;
}
bool parses = false;
std::vector<std::uint8_t> bytes;
if (emdf::extract_payload_bytes(data, size, *payload, &bytes).ok()) {
joc_frame_params params;
FrameSymbols symbols;
parses = parse_id14(bytes.data(), bytes.size(), &params, &symbols).ok();
}
const bool associated = oamd->has_group_id != 0u && payload->has_group_id != 0u &&
oamd->group_id == payload->group_id;
if (associated && parses) {
return 2;
}
return parses ? 1 : 0;
}
Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_frame_params* out,
emdf::Container* container, FrameSymbols* symbols) {
if (frame == nullptr || out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null frame or output");
}
emdf::Container local;
const Status status = emdf::find_joc_emdf(frame, frame_size, &local);
const Status status =
emdf::find_joc_emdf(frame, frame_size, &local, &score_container_as_joc, nullptr);
if (!status.ok()) {
return status;
}
@@ -412,4 +432,516 @@ Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
return Status::success();
}
void make_silent_frame_params(joc_frame_params* out) {
if (out == nullptr) {
return;
}
std::memset(out, 0, sizeof(*out));
out->struct_size = sizeof(joc_frame_params);
out->struct_version = JOC_FRAME_PARAMS_VERSION;
out->dmx_config_idx = 0u;
out->n_channels = static_cast<std::uint8_t>(JOC_CORE_CHANNELS);
out->n_objects = static_cast<std::uint8_t>(JOC_MAX_OBJECTS);
out->present_mask = (1u << JOC_MAX_OBJECTS) - 1u;
out->clipgain = 1.0;
for (unsigned object = 0; object < JOC_MAX_OBJECTS; ++object) {
out->objects[object].present = 1u;
out->objects[object].num_bands_idx = 0u; // one parameter band
out->objects[object].n_bands = 1u;
out->objects[object].sparse = 0u;
out->objects[object].quant_idx = 0u;
out->objects[object].slope_idx = 0u;
out->objects[object].num_dpoints_bits = 0u;
out->objects[object].n_dpoints = 1u;
}
}
// ---------------------------------------------------------------------------
// Channel identity / bed layout (spec sections 3, 10.2, 10.4)
// ---------------------------------------------------------------------------
namespace {
struct PositionBit {
Position position;
std::uint32_t bit;
};
// Position -> ffmpeg channel bit. A decoded bed frame lists its channels in
// ascending bit order, so sorting the positions by bit reproduces the decoder's
// channel order without asking the decoder.
constexpr PositionBit kPositionBits[] = {
{Position::kLeft, 0x1u},
{Position::kRight, 0x2u},
{Position::kCenter, 0x4u},
{Position::kLfe, 0x8u},
{Position::kLeftRearSurround, 0x10u},
{Position::kRightRearSurround, 0x20u},
{Position::kCenterSurround, 0x100u},
{Position::kLeftSurround, 0x200u},
{Position::kRightSurround, 0x400u},
{Position::kTopSurround, 0x800u},
{Position::kVerticalHeightLeft, 0x1000u},
{Position::kVerticalHeightCenter, 0x2000u},
{Position::kVerticalHeightRight, 0x4000u},
{Position::kTopSurroundLeft, 0x8000u},
{Position::kTopSurroundRight, 0x20000u},
};
std::uint32_t position_bit(Position position) {
for (const PositionBit& entry : kPositionBits) {
if (entry.position == position) {
return entry.bit;
}
}
return 0u;
}
void push_unique(std::vector<Position>* positions, Position position) {
if (position == Position::kNone) {
return;
}
for (const Position existing : *positions) {
if (existing == position) {
return;
}
}
positions->push_back(position);
}
// ADM 10-slot skeleton placement of the extra pair, indexed by joc_dmx_config_idx.
constexpr int kExtraPairSlots[5][2] = {
{-1, -1}, {6, 7}, {8, 9}, {-1, -1}, {8, 9},
};
Status variant_error(const char* stage_name, const char* name, const std::string& message,
std::string* error) {
const std::string text = std::string(name) + ": " + message;
if (error != nullptr) {
*error = text;
}
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage_name, text);
}
std::string position_name(Position position) {
switch (position) {
case Position::kLeft: return "L";
case Position::kRight: return "R";
case Position::kCenter: return "C";
case Position::kLfe: return "LFE";
case Position::kLeftSurround: return "Ls";
case Position::kRightSurround: return "Rs";
case Position::kLeftRearSurround: return "Lrs";
case Position::kRightRearSurround: return "Rrs";
case Position::kVerticalHeightLeft: return "Vhl";
case Position::kVerticalHeightRight: return "Vhr";
case Position::kTopSurroundLeft: return "Lts";
case Position::kTopSurroundRight: return "Rts";
case Position::kCenterSurround: return "Cs";
case Position::kTopSurround: return "Ts";
case Position::kVerticalHeightCenter: return "Vhc";
case Position::kLfe2: return "LFE2";
default: return "?";
}
}
std::string position_list(const std::vector<Position>& positions) {
std::string text = "[";
for (std::size_t index = 0; index < positions.size(); ++index) {
if (index != 0) {
text += ",";
}
text += position_name(positions[index]);
}
return text + "]";
}
} // namespace
void expand_chanmap(std::uint16_t chanmap, std::vector<Position>* out) {
if (out == nullptr) {
return;
}
// Table E2.5; bit 0 sits in the field's most significant bit.
static const Position kSingles[16] = {
Position::kLeft, Position::kCenter, Position::kRight,
Position::kLeftSurround, Position::kRightSurround, Position::kNone,
Position::kNone, Position::kCenterSurround, Position::kTopSurround,
Position::kNone, Position::kNone, Position::kNone,
Position::kVerticalHeightCenter, Position::kNone, Position::kLfe2,
Position::kLfe,
};
for (int bit = 0; bit < 16; ++bit) {
if (((chanmap >> (15 - bit)) & 1u) == 0u) {
continue;
}
switch (bit) {
case 5:
out->push_back(Position::kLeftSurround);
out->push_back(Position::kRightSurround);
break;
case 6:
out->push_back(Position::kLeftRearSurround);
out->push_back(Position::kRightRearSurround);
break;
case 9:
case 10:
// Lsd/Rsd and Lw/Rw have no JOC counterpart and stay unmapped.
break;
case 11:
out->push_back(Position::kVerticalHeightLeft);
out->push_back(Position::kVerticalHeightRight);
break;
case 13:
out->push_back(Position::kTopSurroundLeft);
out->push_back(Position::kTopSurroundRight);
break;
default:
out->push_back(kSingles[bit]);
break;
}
}
}
void acmod_positions(std::uint8_t acmod, std::vector<Position>* out) {
if (out == nullptr) {
return;
}
switch (acmod) {
case 0:
case 1:
out->push_back(Position::kCenter);
break;
case 2:
out->push_back(Position::kLeft);
out->push_back(Position::kRight);
break;
case 3:
out->push_back(Position::kLeft);
out->push_back(Position::kCenter);
out->push_back(Position::kRight);
break;
case 4:
out->push_back(Position::kLeft);
out->push_back(Position::kRight);
out->push_back(Position::kCenterSurround);
break;
case 5:
out->push_back(Position::kLeft);
out->push_back(Position::kCenter);
out->push_back(Position::kRight);
out->push_back(Position::kCenterSurround);
break;
case 6:
out->push_back(Position::kLeft);
out->push_back(Position::kRight);
out->push_back(Position::kLeftSurround);
out->push_back(Position::kRightSurround);
break;
case 7:
default:
out->push_back(Position::kLeft);
out->push_back(Position::kCenter);
out->push_back(Position::kRight);
out->push_back(Position::kLeftSurround);
out->push_back(Position::kRightSurround);
break;
}
}
void expected_pair(std::uint8_t dmx_config_idx, std::vector<Position>* out) {
if (out == nullptr) {
return;
}
if (dmx_config_idx == 1u) {
out->push_back(Position::kLeftRearSurround);
out->push_back(Position::kRightRearSurround);
} else if (dmx_config_idx == 2u || dmx_config_idx == 4u) {
out->push_back(Position::kVerticalHeightLeft);
out->push_back(Position::kVerticalHeightRight);
}
}
void chanmap_extra_pair(const std::vector<Position>& core, std::uint16_t chanmap,
std::vector<Position>* out) {
if (out == nullptr) {
return;
}
std::vector<Position> declared;
expand_chanmap(chanmap, &declared);
for (const Position position : declared) {
bool in_core = false;
for (const Position existing : core) {
if (existing == position) {
in_core = true;
break;
}
}
if (!in_core) {
push_unique(out, position);
}
}
}
int bed_channels_for_core(int core_channels) {
return core_channels == 7 ? 8 : 6;
}
Status compute_bed_plan(const eac3::Frame* core_frame, bool has_core, bool has_dependent,
bool has_chanmap, std::uint16_t chanmap, std::uint8_t dmx_config_idx,
BedPlan* out) {
if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null bed plan");
}
const std::int16_t core_channels = num_channels_for_config(dmx_config_idx);
if (core_channels < 0) {
return variant_error("joc", "unsupported_configuration",
"unknown JOC downmix configuration " +
std::to_string(dmx_config_idx), nullptr);
}
std::vector<Position> core_positions;
if (has_core && core_frame != nullptr) {
acmod_positions(core_frame->acmod, &core_positions);
if (core_frame->lfeon != 0u) {
core_positions.push_back(Position::kLfe);
}
}
std::vector<Position> extra;
std::vector<Position> locations = core_positions;
if (has_dependent && has_chanmap) {
std::vector<Position> declared;
expand_chanmap(chanmap, &declared);
chanmap_extra_pair(core_positions, chanmap, &extra);
locations.insert(locations.end(), declared.begin(), declared.end());
}
std::vector<Position> ordered;
for (const Position position : locations) {
push_unique(&ordered, position);
}
std::sort(ordered.begin(), ordered.end(), [](Position left, Position right) {
return position_bit(left) < position_bit(right);
});
for (const Position position : ordered) {
if (position_bit(position) == 0u) {
return variant_error("eac3", "bed_channel_mapping_unsupported",
"unmapped channel position " + position_name(position),
nullptr);
}
}
BedPlan plan;
plan.channels = static_cast<int>(ordered.size());
const int expected_channels = bed_channels_for_core(core_channels);
if (plan.channels != expected_channels) {
return variant_error("eac3", "core_channel_count_mismatch",
"the bed declares " + std::to_string(plan.channels) +
" channels, configuration " + std::to_string(dmx_config_idx) +
" needs " + std::to_string(expected_channels),
nullptr);
}
for (int index = 0; index < plan.channels; ++index) {
if (ordered[index] == Position::kLfe) {
plan.lfe_channel = index;
}
}
std::vector<Position> wanted = {Position::kLeft, Position::kRight, Position::kCenter,
Position::kLeftSurround, Position::kRightSurround};
wanted.insert(wanted.end(), extra.begin(), extra.end());
if (wanted.size() != static_cast<std::size_t>(core_channels)) {
return variant_error("eac3", "chanmap_cfg_mismatch",
"the bed cannot supply " + std::to_string(core_channels) +
" JOC core channels (found " + std::to_string(wanted.size()) +
")",
nullptr);
}
for (std::size_t lane = 0; lane < wanted.size(); ++lane) {
int found = -1;
for (int index = 0; index < plan.channels; ++index) {
if (ordered[index] == wanted[lane]) {
found = index;
break;
}
}
if (found < 0) {
return variant_error("eac3", "bed_channel_mapping_unsupported",
"channel " + position_name(wanted[lane]) +
" is missing from the bed " + position_list(ordered),
nullptr);
}
plan.lane[lane] = found;
}
if (dmx_config_idx < 5u) {
plan.extra_pair_slot[0] = kExtraPairSlots[dmx_config_idx][0];
plan.extra_pair_slot[1] = kExtraPairSlots[dmx_config_idx][1];
}
*out = plan;
return Status::success();
}
Status compute_bed_plan_for_config(std::uint8_t dmx_config_idx, BedPlan* out) {
if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kJoc, "null bed plan");
}
const std::int16_t core_channels = num_channels_for_config(dmx_config_idx);
if (core_channels < 0) {
return variant_error("joc", "unsupported_configuration",
"unknown JOC downmix configuration " +
std::to_string(dmx_config_idx), nullptr);
}
std::vector<Position> extra;
expected_pair(dmx_config_idx, &extra);
std::vector<Position> wanted = {Position::kLeft, Position::kRight, Position::kCenter,
Position::kLeftSurround, Position::kRightSurround};
wanted.insert(wanted.end(), extra.begin(), extra.end());
std::vector<Position> ordered = {Position::kLeft, Position::kRight, Position::kCenter,
Position::kLfe, Position::kLeftSurround,
Position::kRightSurround};
ordered.insert(ordered.end(), extra.begin(), extra.end());
std::sort(ordered.begin(), ordered.end(), [](Position left, Position right) {
return position_bit(left) < position_bit(right);
});
ordered.erase(std::unique(ordered.begin(), ordered.end()), ordered.end());
BedPlan plan;
plan.channels = static_cast<int>(ordered.size());
for (int index = 0; index < plan.channels; ++index) {
if (ordered[index] == Position::kLfe) {
plan.lfe_channel = index;
}
}
for (std::size_t lane = 0; lane < wanted.size(); ++lane) {
for (int index = 0; index < plan.channels; ++index) {
if (ordered[index] == wanted[lane]) {
plan.lane[lane] = index;
break;
}
}
if (plan.lane[lane] < 0) {
return variant_error("eac3", "bed_channel_mapping_unsupported",
"channel " + position_name(wanted[lane]) +
" is missing from the bed " + position_list(ordered),
nullptr);
}
}
if (dmx_config_idx < 5u) {
plan.extra_pair_slot[0] = kExtraPairSlots[dmx_config_idx][0];
plan.extra_pair_slot[1] = kExtraPairSlots[dmx_config_idx][1];
}
*out = plan;
return Status::success();
}
Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core_frame,
bool has_core, bool has_dependent, bool has_chanmap,
std::uint16_t chanmap,
const std::vector<oamd::BedAssignment>* bed_assignments,
std::string* error, CrossLayerNotes* notes) {
const std::uint8_t config_idx = params.dmx_config_idx;
const int core_channels = params.n_channels;
std::vector<Position> wanted;
expected_pair(config_idx, &wanted);
if (params.ext_config_idx != 0u) {
return variant_error("joc", "ext_config_idx_unsupported",
"joc_ext_config_idx=" + std::to_string(params.ext_config_idx) +
" has no mapping in this implementation",
error);
}
if (core_channels != JOC_CORE_CHANNELS && core_channels != JOC_MAX_CORE_CHANNELS) {
return variant_error("joc", "unsupported_configuration",
"unsupported JOC core channel count " +
std::to_string(core_channels),
error);
}
std::vector<Position> core_positions;
if (has_core && core_frame != nullptr) {
acmod_positions(core_frame->acmod, &core_positions);
if (core_frame->lfeon != 0u) {
core_positions.push_back(Position::kLfe);
}
}
if (core_channels == JOC_MAX_CORE_CHANNELS) {
if (!has_core || !has_chanmap) {
return variant_error(
"eac3", "core_extension_pair",
"a 7-channel configuration needs both an independent substream and a "
"dependent-substream chanmap",
error);
}
std::vector<Position> pair;
chanmap_extra_pair(core_positions, chanmap, &pair);
if (pair != wanted) {
// The bed mapping follows the chanmap's channel codes; the name a
// configuration implies does not reach the DSP, so the frame renders
// and the disagreement is reported instead.
if (notes != nullptr) {
notes->chanmap_pair_mismatch = true;
}
}
} else if (has_chanmap) {
std::vector<Position> pair;
chanmap_extra_pair(core_positions, chanmap, &pair);
if (!pair.empty()) {
return variant_error("eac3", "core_channel_count_mismatch",
"a 5-channel configuration declares extra dependent "
"channels " + position_list(pair),
error);
}
}
std::vector<Position> declared = core_positions;
if (has_chanmap) {
std::vector<Position> expanded;
expand_chanmap(chanmap, &expanded);
for (const Position position : expanded) {
push_unique(&declared, position);
}
}
int full_band = 0;
for (const Position position : declared) {
if (position != Position::kLfe && position != Position::kLfe2) {
++full_band;
}
}
if (!declared.empty() && full_band != core_channels) {
return variant_error("eac3", "core_channel_count_mismatch",
"the substreams declare " + std::to_string(full_band) +
" full-band channels, joc_dmx_config_idx=" +
std::to_string(config_idx) + " declares " +
std::to_string(core_channels),
error);
}
if (bed_assignments != nullptr) {
static constexpr std::uint32_t kBedBitLeftRear = 1u << 4; // RC_LB/RC_RB
static constexpr std::uint32_t kBedBitTopFront = 1u << 5; // RC_TFL/RC_TFR
std::vector<Position> declared_pair;
for (const oamd::BedAssignment& assignment : *bed_assignments) {
if (!assignment.standard || assignment.mask == 0u) {
continue;
}
if ((assignment.mask & kBedBitLeftRear) != 0u) {
push_unique(&declared_pair, Position::kLeftRearSurround);
push_unique(&declared_pair, Position::kRightRearSurround);
}
if ((assignment.mask & kBedBitTopFront) != 0u) {
push_unique(&declared_pair, Position::kVerticalHeightLeft);
push_unique(&declared_pair, Position::kVerticalHeightRight);
}
}
if (!declared_pair.empty() && declared_pair != wanted) {
if (notes != nullptr) {
notes->bed_mask_mismatch = true;
}
}
}
return Status::success();
}
} // namespace joc::joc
+93
View File
@@ -6,9 +6,11 @@
#include <cstdint>
#include <vector>
#include "eac3_transport/eac3_reader.h"
#include "joc_core.h"
#include "emdf/emdf_parser.h"
#include "foundation/status.h"
#include "oamd/oamd_parser.h"
namespace joc::joc {
@@ -39,8 +41,99 @@ Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_f
Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size,
std::uint32_t* out_trailing_bits);
// Scores this container as the frame's JOC metadata when the syncframe holds more
// than one (spec section 8.2 and its Table 56 associate the OAMD and JOC payloads
// of one frame by an equal group id). 2 = the two payloads share a group id and
// ID14 parses as a complete joc() payload, 1 = ID14 parses, 0 = neither. A
// container whose ID14 declares far more objects than its bytes could hold, or
// whose ID11 carries no group id at all, is some other structure.
int score_container_as_joc(const std::uint8_t* data, std::size_t size,
const emdf::Container& container);
// Parameters for an audio frame that carries no JOC payload: every object is
// "present" with an all-zero matrix, so the kernel renders silence while its
// analysis and synthesis state keep advancing in step with the stream.
void make_silent_frame_params(joc_frame_params* out);
std::int16_t num_channels_for_config(std::uint32_t dmx_config_idx);
std::int16_t num_bands_for_index(std::uint32_t num_bands_idx);
// ---------------------------------------------------------------------------
// Channel identity / bed layout (spec sections 3, 10.2, 10.4)
//
// This is the position layer only: it decides which decoded bed channel feeds
// which JOC input lane and which ADM bed slot the extra pair occupies. The DSP
// never reads it.
// ---------------------------------------------------------------------------
enum class Position : std::uint8_t {
kNone = 0,
kLeft,
kRight,
kCenter,
kLfe,
kLeftSurround,
kRightSurround,
kLeftRearSurround,
kRightRearSurround,
kVerticalHeightLeft,
kVerticalHeightRight,
kTopSurroundLeft,
kTopSurroundRight,
kCenterSurround,
kTopSurround,
kVerticalHeightCenter,
kLfe2,
};
// Table E2.5 expansion; the field's most significant bit is table bit 0.
void expand_chanmap(std::uint16_t chanmap, std::vector<Position>* out);
// A/52 acmod -> full-band positions in decoding order (LFE excluded).
void acmod_positions(std::uint8_t acmod, std::vector<Position>* out);
// The pair joc_dmx_config_idx declares; empty for configurations 0 and 3.
void expected_pair(std::uint8_t dmx_config_idx, std::vector<Position>* out);
// The extra pair a dependent-substream chanmap adds on top of the core channels.
void chanmap_extra_pair(const std::vector<Position>& core, std::uint16_t chanmap,
std::vector<Position>* out);
// Decoded-bed channel count implied by the JOC core channel count.
int bed_channels_for_core(int core_channels);
// bed[lane] = decoded PCM channel index feeding JOC lane 'lane'.
struct BedPlan {
int channels = 0; // 6 or 8
int lfe_channel = -1;
int lane[JOC_MAX_CORE_CHANNELS] = {-1, -1, -1, -1, -1, -1, -1};
int extra_pair_slot[2] = {-1, -1}; // ADM skeleton
};
Status compute_bed_plan(const eac3::Frame* core_frame, bool has_core, bool has_dependent,
bool has_chanmap, std::uint16_t chanmap, std::uint8_t dmx_config_idx,
BedPlan* out);
// Bed plan driven by joc_dmx_config_idx alone, for callers that hold no L2
// header: the extra pair is the one the configuration declares.
Status compute_bed_plan_for_config(std::uint8_t dmx_config_idx, BedPlan* out);
// Identity disagreements that leave the DSP input unchanged (spec section 10.1:
// channel identity never enters the DSP): the extra pair's name is metadata only,
// so a mismatch is reported through CrossLayerNotes instead of refusing the frame.
struct CrossLayerNotes {
bool chanmap_pair_mismatch = false; // spec 11.2 check 1: chanmap vs k
bool bed_mask_mismatch = false; // spec 11.2 check 2: OAMD bed mask vs k
};
// Spec section 11.2 cross-layer consistency. A mismatch in the core channel count
// or in joc_ext_config_idx changes the DSP input and is a variant error; the two
// identity checks are recorded in `notes` and the frame still renders.
Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core_frame,
bool has_core, bool has_dependent, bool has_chanmap,
std::uint16_t chanmap,
const std::vector<oamd::BedAssignment>* bed_assignments,
std::string* error, CrossLayerNotes* notes = nullptr);
} // namespace joc::joc
+64 -49
View File
@@ -69,7 +69,9 @@ public:
std::memset(synthesis_state_, 0, sizeof(synthesis_state_));
std::memset(x_, 0, sizeof(x_));
std::memset(z_, 0, sizeof(z_));
analysis_phase_ = 0.0625f;
// Sentinel: the first frame after reset takes the new scalar directly,
// without an intra-frame transition.
analysis_phase_ = -0.5f;
error_[0] = '\0';
return 0;
}
@@ -125,13 +127,11 @@ public:
return error_[0] ? error_ : "";
}
void set_dc_filter(const bool enabled) noexcept {
dc_filter_enabled_ = enabled;
}
int process(
const float* bed5,
const float* bed_planar,
const float* lfe,
const uint32_t core_channel_count,
const uint32_t phase_shift,
const uint32_t object_mask,
const uint8_t* n_bands,
const uint8_t* n_dpoints,
@@ -144,9 +144,14 @@ public:
float* output16) noexcept {
error_[0] = '\0';
if (!bed5 || !n_bands || !n_dpoints || !slope_idx || !offset_ts || !dq || !output16) {
if (!bed_planar || !n_bands || !n_dpoints || !slope_idx || !offset_ts || !dq || !output16) {
return fail("null pointer passed to ejoc_renderer_process");
}
if (core_channel_count < EJOC_CORE_CHANNELS || core_channel_count > EJOC_MAX_CORE_CHANNELS) {
return fail("core_channel_count must be 5 or 7");
}
core_channels_ = core_channel_count;
phase_shift_enabled_ = phase_shift != 0u;
if (object_mask & ~((1u << EJOC_MAX_OBJECTS) - 1u)) {
return fail("object_mask contains an object index above 14");
}
@@ -169,7 +174,7 @@ public:
}
std::memset(output16, 0, sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES);
analysis(bed5, phase_new);
analysis(bed_planar, phase_new);
render_lfe(lfe, output_scale, output16);
process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts,
dq, clipgain, output_scale, output16);
@@ -299,7 +304,8 @@ private:
for (int band = 0; band < 64; ++band) {
current[slot][band] = x_[channel][band][group + slot];
const Complex delayed = surround_delay_[surround][slot][band];
x_[channel][band][group + slot] = {delayed.im, -delayed.re};
x_[channel][band][group + slot] =
phase_shift_enabled_ ? Complex{delayed.im, -delayed.re} : delayed;
}
}
@@ -309,7 +315,7 @@ private:
for (int i = 0; i < 4; ++i) {
dc_buffer[20 + i] = current[i][0];
}
if (dc_filter_enabled_) {
if (phase_shift_enabled_) {
for (int slot = 0; slot < 4; ++slot) {
Complex sum{0.0, 0.0};
for (int tap = 0; tap < 21; ++tap) {
@@ -338,7 +344,7 @@ private:
}
void analysis_channel(const int channel) noexcept {
const float* bed5 = job_bed5_;
const float* bed_planar = job_bed_;
const float phase_old = job_phase_old_;
const float phase_new = job_phase_new_;
const bool ramp_phase = phase_old != phase_new;
@@ -354,37 +360,49 @@ private:
const float product = static_cast<float>(static_cast<float>(frame_sample) * phase_step);
gain = static_cast<float>(phase_old + product);
}
scaled[sample] = static_cast<float>(bed5[channel * 1536 + frame_sample] * gain);
scaled[sample] = static_cast<float>(bed_planar[channel * 1536 + frame_sample] * gain);
}
// L/R/C and, for 7 core channels, the extra pair (5 and 6) take the
// ten-slot delay here; Ls/Rs take the same delay inside surround_post().
const float* analysis_input = scaled;
if (channel < 3) {
std::memcpy(delayed, analysis_delay_[channel][0], sizeof(delayed));
std::memmove(&analysis_delay_[channel][0][0],
&analysis_delay_[channel][1][0],
const int delay_lane = channel < 3 ? channel : channel - 2;
if (channel < 3 || channel >= 5) {
std::memcpy(delayed, analysis_delay_[delay_lane][0], sizeof(delayed));
std::memmove(&analysis_delay_[delay_lane][0][0],
&analysis_delay_[delay_lane][1][0],
sizeof(float) * 9 * 64);
std::memcpy(analysis_delay_[channel][9], scaled, sizeof(scaled));
std::memcpy(analysis_delay_[delay_lane][9], scaled, sizeof(scaled));
analysis_input = delayed;
}
analysis_slot(channel, analysis_input, timeslot);
}
}
void analysis(const float* bed5, const float phase_new) noexcept {
job_bed5_ = bed5;
void analysis(const float* bed_planar, const float phase_new) noexcept {
job_bed_ = bed_planar;
// Analysis-side scalar = the structural 1/16 times this frame's clipgain.
// clipgain enters the analysis at this stage and the x16 at the output
// cancels the 1/16, so the net effect is a multiply by joc_clipgain; the
// previous frame's scalar is the ramp start.
const float scalar = static_cast<float>(phase_new * job_clipgain_);
job_phase_old_ = analysis_phase_;
job_phase_new_ = phase_new;
if (job_phase_old_ <= 0.0f && job_phase_old_ != 0.0f) {
job_phase_old_ = scalar;
}
job_phase_new_ = scalar;
job_kind_ = JobKind::Analysis;
job_count_ = 5;
for (int channel = 0; channel < 5; ++channel) {
job_objects_[channel] = channel;
job_count_ = static_cast<int>(core_channels_);
for (uint32_t channel = 0; channel < core_channels_; ++channel) {
job_objects_[channel] = static_cast<int>(channel);
}
dispatch_jobs();
analysis_phase_ = phase_new;
analysis_phase_ = scalar;
surround_post();
}
static std::size_t dq_index(const int object, const int point, const int channel, const int band) noexcept {
return static_cast<std::size_t>((((object * 2 + point) * 5 + channel) * kMaxBands) + band);
return static_cast<std::size_t>(
(((object * 2 + point) * EJOC_MAX_CORE_CHANNELS + channel) * kMaxBands) + band);
}
void matrix_object(
@@ -401,7 +419,7 @@ private:
const int slope = slope_idx[object];
const uint8_t* pb_map = parameter_band_map(bands);
for (int channel = 0; channel < 5; ++channel) {
for (uint32_t channel = 0; channel < core_channels_; ++channel) {
for (int subband = 0; subband < 64; ++subband) {
const int parameter_band = pb_map[subband];
const double previous = matrix_previous_[object][channel][subband];
@@ -465,7 +483,6 @@ private:
void synthesis_object(
const int object,
const double clipgain,
const float output_scale,
float* output16) noexcept {
@@ -493,8 +510,10 @@ private:
qmf5_step(state, rotated, pcm64);
for (int sample = 0; sample < 64; ++sample) {
const double clipped = clamp_unit(16.0 * pcm64[sample]);
const float value = static_cast<float>(clipped * clipgain);
destination[timeslot * 64 + sample] = static_cast<float>(value * output_scale);
// joc_clipgain already entered the analysis (see analysis()); what
// remains here is the user output gain.
const float value = static_cast<float>(clipped * output_scale);
destination[timeslot * 64 + sample] = value;
}
}
}
@@ -502,7 +521,7 @@ private:
void process_one_object(const int object) noexcept {
matrix_object(object, job_n_bands_, job_n_dpoints_, job_slope_idx_,
job_offset_ts_, job_dq_);
synthesis_object(object, job_clipgain_, job_output_scale_, job_output16_);
synthesis_object(object, job_output_scale_, job_output16_);
}
void execute_job_loop() noexcept {
@@ -630,7 +649,7 @@ private:
int job_objects_[EJOC_MAX_OBJECTS]{};
int job_count_ = 0;
JobKind job_kind_ = JobKind::Objects;
const float* job_bed5_ = nullptr;
const float* job_bed_ = nullptr;
float job_phase_old_ = 0.0625f;
float job_phase_new_ = 0.0625f;
const uint8_t* job_n_bands_ = nullptr;
@@ -642,17 +661,19 @@ private:
float job_output_scale_ = 1.0f;
float* job_output16_ = nullptr;
alignas(64) double analysis_fifo_[5][9][64];
alignas(64) float analysis_delay_[3][10][64];
float analysis_phase_;
uint32_t core_channels_ = EJOC_CORE_CHANNELS;
alignas(64) double analysis_fifo_[EJOC_MAX_CORE_CHANNELS][9][64];
// Delay lanes: 0..2 hold L/R/C, 3..4 hold the extra pair of a 7-channel frame.
alignas(64) float analysis_delay_[5][10][64];
float analysis_phase_ = -0.5f;
alignas(64) Complex surround_delay_[2][10][64];
alignas(64) Complex surround_history_[2][20];
// band-0 的 21-tap DC 补偿开关;仅 downmix 配置 3/4 由调用方置位。
bool dc_filter_enabled_ = true;
// sigma(k) switch; the caller sets it for downmix configurations 3 and 4.
bool phase_shift_enabled_ = true;
alignas(64) double lfe_delay_[kLfeDelay];
alignas(64) double matrix_previous_[15][5][64];
alignas(64) double matrix_previous_[15][EJOC_MAX_CORE_CHANNELS][64];
alignas(64) double synthesis_state_[15][640];
alignas(64) Complex x_[5][64][24];
alignas(64) Complex x_[EJOC_MAX_CORE_CHANNELS][64][24];
alignas(64) Complex z_[15][64][24];
uint8_t bit_reverse_[64];
@@ -704,14 +725,6 @@ int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t to
return static_cast<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) {
if (!handle) {
return 0;
@@ -728,8 +741,10 @@ const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle) {
int EJOC_CALL ejoc_renderer_process(
ejoc_renderer_handle handle,
const float* bed5_planar,
const float* bed_planar,
const float* lfe,
const uint32_t core_channel_count,
const uint32_t phase_shift,
const uint32_t object_mask,
const uint8_t* n_bands,
const uint8_t* n_dpoints,
@@ -744,8 +759,8 @@ int EJOC_CALL ejoc_renderer_process(
return -1;
}
return static_cast<ejoc::Renderer*>(handle)->process(
bed5_planar, lfe, object_mask, n_bands, n_dpoints, slope_idx,
offset_ts, dq, clipgain, phase_new, output_scale, output16_planar);
bed_planar, lfe, core_channel_count, phase_shift, object_mask, n_bands, n_dpoints,
slope_idx, offset_ts, dq, clipgain, phase_new, output_scale, output16_planar);
}
} // extern "C"
+16 -18
View File
@@ -6,21 +6,22 @@
namespace joc::joc {
Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params,
const float* bed5_planar, const float* lfe, float gain,
const float* bed_planar, const float* lfe, float gain,
std::vector<float>* out16, std::string* error) {
if (handle == nullptr || bed5_planar == nullptr || out16 == nullptr) {
if (handle == nullptr || bed_planar == nullptr || out16 == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp, "null argument");
}
if (params.n_channels != JOC_CORE_CHANNELS) {
if (params.n_channels != JOC_CORE_CHANNELS &&
params.n_channels != JOC_MAX_CORE_CHANNELS) {
if (error != nullptr) {
*error = "the reused JOC kernel requires 5 core channels, frame declares " +
*error = "the reused JOC kernel requires 5 or 7 core channels, frame declares " +
std::to_string(static_cast<unsigned>(params.n_channels));
}
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kDsp, *error);
}
std::vector<double> dq(static_cast<std::size_t>(JOC_MAX_OBJECTS) * JOC_MAX_DPOINTS *
JOC_CORE_CHANNELS * JOC_MAX_PARAMETER_BANDS,
JOC_MAX_CORE_CHANNELS * JOC_MAX_PARAMETER_BANDS,
0.0);
std::uint8_t n_bands[JOC_MAX_OBJECTS] = {};
std::uint8_t n_dpoints[JOC_MAX_OBJECTS] = {};
@@ -38,10 +39,11 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa
offset_ts[obj * JOC_MAX_DPOINTS + dp] = object.offset_ts[dp];
}
for (unsigned dp = 0; dp < object.n_dpoints; ++dp) {
for (unsigned ch = 0; ch < JOC_CORE_CHANNELS; ++ch) {
for (unsigned ch = 0; ch < params.n_channels; ++ch) {
for (unsigned pb = 0; pb < object.n_bands; ++pb) {
const std::size_t index =
((static_cast<std::size_t>(obj) * JOC_MAX_DPOINTS + dp) * JOC_CORE_CHANNELS +
((static_cast<std::size_t>(obj) * JOC_MAX_DPOINTS + dp) *
JOC_MAX_CORE_CHANNELS +
ch) *
JOC_MAX_PARAMETER_BANDS +
pb;
@@ -52,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);
// band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用,其余配置 band 0
// 走与其他 band 相同的处理。
const bool dc_filter = params.dmx_config_idx == 3 || params.dmx_config_idx == 4;
if (ejoc_renderer_set_dc_filter(handle, dc_filter ? 1u : 0u) != 0) {
if (error != nullptr) {
*error = "ejoc_renderer_set_dc_filter failed";
}
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kDsp, "ejoc_renderer_set_dc_filter failed");
}
// sigma(k) = (k in {3,4}) gates the Ls/Rs -j rotation and the band-0 21-tap
// DC compensation together; other configurations keep the common processing.
const std::uint32_t phase_shift =
(params.dmx_config_idx == 3 || params.dmx_config_idx == 4) ? 1u : 0u;
const int result = ejoc_renderer_process(
handle, bed5_planar, lfe, params.present_mask, n_bands, n_dpoints, slope_idx, offset_ts,
dq.data(), params.clipgain, 0.0625f, gain, out16->data());
handle, bed_planar, lfe, params.n_channels, phase_shift, params.present_mask, n_bands,
n_dpoints, slope_idx, offset_ts, dq.data(), params.clipgain, 0.0625f, gain,
out16->data());
if (result != 0) {
const char* detail = ejoc_renderer_last_error(handle);
const std::string message =
+3 -2
View File
@@ -10,9 +10,10 @@
namespace joc::joc {
// [16][1536] planar float32. The kernel requires exactly 5 core channels.
// [16][1536] planar float32. bed_planar is [params.n_channels][1536], where
// n_channels comes from joc_dmx_config_idx (5 or 7).
Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params,
const float* bed5_planar, const float* lfe, float gain,
const float* bed_planar, const float* lfe, float gain,
std::vector<float>* out16, std::string* error);
} // namespace joc::joc
+36
View File
@@ -335,6 +335,42 @@ Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpd
return parse_id11_verbose(payload, payload_size, out, nullptr);
}
Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size,
std::vector<BedAssignment>* out) {
if (payload == nullptr || out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output");
}
if (payload_size == 0) {
return variant("header_truncated", "OAMD payload is empty");
}
out->clear();
bits::BitReader reader(payload, payload_size);
std::uint32_t version = reader.read(2);
if (reader.failed()) {
return variant("header_truncated", "OAMD payload cannot hold the 2-bit version");
}
if (version == 3u) {
version += reader.read(3);
if (reader.failed()) {
return syntax("OAMD version extension truncated");
}
}
std::uint32_t object_count_bits = reader.read(5);
if (object_count_bits == 0x1Fu) {
object_count_bits += reader.read(7);
}
if (reader.failed()) {
return syntax("OAMD object count truncated");
}
ProgramInfo program;
const Status status = parse_program_assignment(reader, &program);
if (!status.ok()) {
return status;
}
*out = program.bed_assignments;
return Status::success();
}
Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out,
ParseTrace* trace) {
if (payload == nullptr || out == nullptr) {
+5
View File
@@ -100,4 +100,9 @@ struct ParseTrace {
Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out,
ParseTrace* trace);
// Parses only the OAMD header and program_assignment(), which is all the
// cross-layer bed-mask check (spec section 11.2 item 2) needs.
Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size,
std::vector<BedAssignment>* out);
} // namespace joc::oamd
+209 -25
View File
@@ -17,9 +17,6 @@ namespace joc::stream {
namespace {
constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES;
constexpr std::size_t kBedChannels = 6;
constexpr int kCoreChannels[5] = {0, 1, 2, 4, 5};
constexpr int kLfeChannel = 3;
} // namespace
@@ -37,6 +34,14 @@ Stream::~Stream() {
void Stream::reset_state() {
reader_ = eac3::FrameReader();
metadata_.clear();
pending_frame_ = FrameMetadata();
bed_plan_ = joc::BedPlan();
bed_channels_ = 0;
config_idx_ = -1;
core_channels_ = 0;
bed_plan_failed_ = false;
bed_plan_error_ = Status::success();
bed_input_channels_ = 0;
bed_pending_.clear();
bed_read_offset_ = 0;
objects16_.clear();
@@ -181,6 +186,43 @@ Status Stream::create(const Config& config) {
return Status::success();
}
Status Stream::flush_pending_frame() {
if (pending_frame_.streams.empty()) {
return Status::success();
}
FrameMetadata entry = pending_frame_;
pending_frame_ = FrameMetadata();
if (entry.has_joc) {
std::string error;
joc::CrossLayerNotes notes;
const Status checked = joc::check_cross_layer(
entry.params, entry.has_core ? &entry.core_frame : nullptr, entry.has_core,
entry.has_dependent, entry.has_chanmap, entry.chanmap,
entry.bed_assignments.empty() ? nullptr : &entry.bed_assignments, &error, &notes);
if (!checked.ok()) {
return Status::fail(checked.code(), checked.stage(),
error.empty() ? checked.message() : error);
}
if (notes.chanmap_pair_mismatch) {
++info_.chanmap_pair_mismatch_frames;
}
if (notes.bed_mask_mismatch) {
++info_.bed_mask_mismatch_frames;
}
if (entry.params.objects_clamped != 0u) {
++info_.objects_clamped_frames;
}
} else {
// No JOC metadata for this audio frame: the object channels stay silent
// while the kernel state still advances for the frame.
++info_.missing_joc_frames;
joc::make_silent_frame_params(&entry.params);
entry.has_update = false;
}
metadata_.push_back(entry);
return Status::success();
}
Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed) {
if (rebuilder_ == nullptr) {
return Status::fail(JOC_ERR_STATE, stage::kEmdf, "stream is not an E-AC-3 input");
@@ -201,13 +243,65 @@ Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t
if (state == eac3::FrameReader::Next::Fail) {
return Status::fail(reader_.error(), stage::kEac3, reader_.error_message());
}
FrameMetadata entry;
// A repeated (codec, strmtyp, substreamid) combination starts the next
// audio frame, so the group that is still open is complete.
bool repeats = false;
for (const eac3::Frame& prior : pending_frame_.streams) {
if (prior.same_stream(frame)) {
repeats = true;
break;
}
}
if (repeats) {
const Status flushed = flush_pending_frame();
if (!flushed.ok()) {
return flushed;
}
}
eac3::Frame header = frame;
header.data = nullptr;
header.size = 0;
header.offset = frame.offset;
pending_frame_.streams.push_back(header);
if (frame.codec == 0u || frame.strmtyp == 0u) {
pending_frame_.core_frame = header;
pending_frame_.has_core = true;
} else {
pending_frame_.has_dependent = true;
if (frame.has_chanmap != 0u) {
pending_frame_.has_chanmap = true;
pending_frame_.chanmap = frame.chanmap;
}
}
bool present = false;
bool ignored_extra = false;
emdf::Container container;
const Status found = emdf::find_joc_emdf_optional(
frame.data, frame.size, &present, &container, &joc::score_container_as_joc,
&ignored_extra);
if (!found.ok()) {
return found;
}
if (ignored_extra) {
++info_.extra_container_frames;
}
if (!present) {
continue;
}
if (container.find(emdf::kIdJoc) == nullptr) {
continue;
}
joc_frame_params params;
const Status parsed =
joc::parse_eac3_frame(frame.data, frame.size, &entry.params, &container, nullptr);
joc::parse_eac3_frame(frame.data, frame.size, &params, &container, nullptr);
if (!parsed.ok()) {
return parsed;
}
pending_frame_.params = params;
pending_frame_.has_joc = true;
pending_frame_.syncframe_offset = frame.offset;
pending_frame_.syncframe_size = frame.size;
if (const emdf::Payload* payload = container.find(emdf::kIdOamd)) {
std::vector<std::uint8_t> bytes;
const Status extracted =
@@ -215,32 +309,82 @@ Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t
if (!extracted.ok()) {
return extracted;
}
const Status oamd = oamd::parse_id11(bytes.data(), bytes.size(), &entry.update);
const Status oamd = oamd::parse_id11(bytes.data(), bytes.size(), &pending_frame_.update);
if (!oamd.ok()) {
return oamd;
}
entry.has_update = true;
entry.outer_offset = static_cast<std::int64_t>(payload->sample_offset);
pending_frame_.has_update = true;
pending_frame_.outer_offset = static_cast<std::int64_t>(payload->sample_offset);
const Status bed = oamd::parse_id11_bed_assignments(
bytes.data(), bytes.size(), &pending_frame_.bed_assignments);
if (!bed.ok()) {
return bed;
}
}
metadata_.push_back(entry);
}
resolve_bed_plan();
return process_ready_frames(false);
}
Status Stream::push_bed(const float* interleaved6, std::size_t samples, std::size_t* consumed) {
Status Stream::push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels,
std::size_t* consumed) {
if (rebuilder_ == nullptr) {
return Status::fail(JOC_ERR_STATE, stage::kDsp, "stream is not an E-AC-3 input");
}
if (channels != 6u && channels != 8u) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp,
"the core PCM must be interleaved as 6 or 8 channels, got " +
std::to_string(channels));
}
if (bed_input_channels_ == 0u) {
bed_input_channels_ = channels;
} else if (bed_input_channels_ != channels) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp,
"the core PCM channel count changed from " +
std::to_string(bed_input_channels_) + " to " +
std::to_string(channels));
}
if (consumed != nullptr) {
*consumed = samples;
}
if (interleaved6 != nullptr && samples != 0u) {
bed_pending_.insert(bed_pending_.end(), interleaved6,
interleaved6 + samples * kBedChannels);
if (interleaved != nullptr && samples != 0u) {
bed_pending_.insert(bed_pending_.end(), interleaved,
interleaved + samples * static_cast<std::size_t>(channels));
}
return process_ready_frames(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) {
if (consumed != nullptr) {
*consumed = samples;
@@ -299,26 +443,62 @@ Status Stream::process_objects16_frames(bool drain_all) {
}
Status Stream::process_ready_frames(bool drain_all) {
while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * kBedChannels &&
if (bed_plan_failed_) {
return bed_plan_error_;
}
resolve_bed_plan();
if (bed_channels_ == 0u) {
if (!drain_all || metadata_.empty()) {
return Status::success(); // waiting for the first JOC-bearing frame
}
return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf,
"no_contiguous_joc_container: the input carries no JOC EMDF "
"container in any audio frame");
}
if (bed_input_channels_ != 0u && bed_input_channels_ != bed_channels_) {
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, "eac3",
"core_channel_count_mismatch: the caller pushed " +
std::to_string(bed_input_channels_) +
"-channel bed PCM but joc_dmx_config_idx=" +
std::to_string(config_idx_) + " decodes to " +
std::to_string(bed_channels_) + " channels");
}
while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * bed_channels_ &&
!metadata_.empty()) {
// Leave the rest queued, in order, for a later push or for flush().
if (!drain_all && buffered_samples() >= kMaxRenderAheadSamples) {
break;
}
const FrameMetadata entry = metadata_.front();
FrameMetadata entry = metadata_.front();
metadata_.pop_front();
const float* bed = bed_pending_.data() + bed_read_offset_;
bed5_.resize(static_cast<std::size_t>(JOC_CORE_CHANNELS) * kFrameSamples);
lfe_.resize(kFrameSamples);
for (std::size_t sample = 0; sample < kFrameSamples; ++sample) {
for (std::size_t channel = 0; channel < JOC_CORE_CHANNELS; ++channel) {
bed5_[channel * kFrameSamples + sample] =
bed[sample * kBedChannels + kCoreChannels[channel]];
}
lfe_[sample] = bed[sample * kBedChannels + kLfeChannel];
const int core_channels = entry.has_joc ? static_cast<int>(entry.params.n_channels)
: core_channels_;
if (core_channels <= 0) {
return Status::fail(JOC_ERR_STATE, stage::kDsp,
"the JOC core channel count is not known yet");
}
bed_read_offset_ += kFrameSamples * kBedChannels;
if (!entry.has_joc) {
entry.params.dmx_config_idx = static_cast<std::uint8_t>(config_idx_);
entry.params.n_channels = static_cast<std::uint8_t>(core_channels);
}
const float* bed = bed_pending_.data() + bed_read_offset_;
bed5_.assign(static_cast<std::size_t>(core_channels) * kFrameSamples, 0.0f);
lfe_.assign(kFrameSamples, 0.0f);
for (std::size_t sample = 0; sample < kFrameSamples; ++sample) {
for (int lane = 0; lane < core_channels; ++lane) {
const int source = bed_plan_.lane[lane];
if (source >= 0) {
bed5_[static_cast<std::size_t>(lane) * kFrameSamples + sample] =
bed[sample * bed_channels_ + static_cast<std::size_t>(source)];
}
}
if (bed_plan_.lfe_channel >= 0) {
lfe_[sample] = bed[sample * bed_channels_ +
static_cast<std::size_t>(bed_plan_.lfe_channel)];
}
}
bed_read_offset_ += kFrameSamples * bed_channels_;
compact_bed_pending();
std::string error;
@@ -484,6 +664,10 @@ Status Stream::pull(float* destination, std::size_t capacity_samples, std::size_
Status Stream::flush() {
// Input has ended, so drain what the cap held back: nothing else will
// trigger rendering.
const Status pending = flush_pending_frame();
if (!pending.ok()) {
return pending;
}
const Status remaining = process_ready_frames(true);
if (!remaining.ok()) {
return remaining;
+42 -2
View File
@@ -10,6 +10,7 @@
#include "eac3_transport/eac3_reader.h"
#include "foundation/status.h"
#include "hrtf/rosella_renderer.h"
#include "joc_bitstream/joc_parser.h"
#include "joc_core.h"
#include "joc_stream.h"
#include "oamd/oamd_parser.h"
@@ -46,16 +47,41 @@ struct Info {
std::uint64_t bytes_in = 0;
std::uint64_t oamd_payloads = 0;
std::uint64_t oamd_transitions = 0;
std::uint64_t missing_joc_frames = 0;
std::uint64_t objects_clamped_frames = 0;
// Audio frames whose syncframe held more than one top-level JOC container and
// were decoded from the single one that carries a complete joc() payload.
std::uint64_t extra_container_frames = 0;
// Audio frames whose extra-pair identity disagreed across layers (spec 11.2
// checks 1 and 2). The identity is metadata only, so these still render.
std::uint64_t chanmap_pair_mismatch_frames = 0;
std::uint64_t bed_mask_mismatch_frames = 0;
std::uint32_t output_channels = 0;
std::uint32_t ended = 0;
};
// One frame's metadata, queued while the matching core PCM arrives.
// One audio frame's metadata, queued while the matching core PCM arrives.
//
// One audio frame is the group of syncframes up to (and excluding) the first
// repeated (codec, strmtyp, substreamid) combination; the JOC payload usually
// lives in the dependent substream while the independent substream carries only
// the 5.1 core. The L2 fields below feed the spec section 11.2 cross-layer
// checks.
struct FrameMetadata {
joc_frame_params params{};
oamd::OamdUpdate update{};
bool has_update = false;
std::int64_t outer_offset = 0;
bool has_joc = false;
eac3::Frame core_frame{}; // independent substream header (codec/strmtyp=0)
bool has_core = false;
bool has_dependent = false;
bool has_chanmap = false;
std::uint16_t chanmap = 0;
std::size_t syncframe_offset = 0;
std::size_t syncframe_size = 0;
std::vector<eac3::Frame> streams;
std::vector<oamd::BedAssignment> bed_assignments;
};
class Stream {
@@ -68,7 +94,8 @@ public:
Status create(const Config& config);
Status push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed);
Status push_bed(const float* interleaved6, std::size_t samples, std::size_t* consumed);
Status push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels,
std::size_t* consumed);
Status push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed);
Status pull(float* destination, std::size_t capacity_samples, std::size_t* produced);
Status flush();
@@ -103,6 +130,11 @@ private:
void reset_state();
// Drops the bed samples that have already been rendered, keeping the rest.
void compact_bed_pending();
// Closes the audio frame currently being assembled and queues it.
Status flush_pending_frame();
// Resolves the bed layout from the first queued frame that carries JOC;
// returns false while it is still unknown and more input may arrive.
bool resolve_bed_plan();
// Renders the queued objects16 frames, bounded by kMaxRenderAheadSamples.
Status process_objects16_frames(bool drain_all);
@@ -111,6 +143,14 @@ private:
eac3::FrameReader reader_;
std::deque<FrameMetadata> metadata_;
FrameMetadata pending_metadata_;
FrameMetadata pending_frame_;
joc::BedPlan bed_plan_;
std::size_t bed_channels_ = 0;
int config_idx_ = -1;
int core_channels_ = 0;
bool bed_plan_failed_ = false;
Status bed_plan_error_;
std::uint32_t bed_input_channels_ = 0;
std::vector<float> bed_pending_;
std::size_t bed_read_offset_ = 0;
std::vector<float> bed5_;
+361 -51
View File
@@ -53,11 +53,8 @@ namespace {
namespace fs = std::filesystem;
using Clock = std::chrono::steady_clock;
constexpr std::size_t kBedFrameBytes = static_cast<std::size_t>(JOC_FRAME_SAMPLES) * 6u * 4u;
constexpr std::size_t kReadChunk = 1u << 20;
constexpr double kDefaultTailThreshold = 1.0e-8;
constexpr int kCoreChannels[5] = {0, 1, 2, 4, 5};
constexpr int kLfeChannel = 3;
std::string lower(std::string text) {
for (char& c : text) {
@@ -210,6 +207,9 @@ std::string create_temp_dir(const Settings& cfg) { const fs::path base = cfg.
return fs_utf8::from_path(target);
}
// One audio frame is every syncframe up to (and excluding) the first repeated
// (codec, strmtyp, substreamid) combination: a 7-channel program interleaves an
// independent substream with a dependent one, and an AC-3 core may come first.
std::uint64_t count_frames(const std::string& path) {
std::ifstream input = fs_utf8::open_input(path);
if (!input) {
@@ -219,6 +219,7 @@ std::uint64_t count_frames(const std::string& path) {
std::vector<char> chunk(kReadChunk);
std::uint64_t frames = 0;
bool done = false;
std::vector<::joc::eac3::Frame> group;
while (!done) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
@@ -232,16 +233,29 @@ std::uint64_t count_frames(const std::string& path) {
for (;;) {
::joc::eac3::Frame frame;
const ::joc::eac3::FrameReader::Next state = reader.next(&frame);
if (state == ::joc::eac3::FrameReader::Next::Ok) {
++frames;
continue;
if (state == ::joc::eac3::FrameReader::Next::End) {
break;
}
if (state == ::joc::eac3::FrameReader::Next::Fail) {
return frames;
return frames + (group.empty() ? 0u : 1u);
}
break;
bool repeats = false;
for (const ::joc::eac3::Frame& prior : group) {
if (prior.same_stream(frame)) {
repeats = true;
break;
}
}
if (repeats) {
++frames;
group.clear();
}
group.push_back(frame);
}
}
if (!group.empty()) {
++frames;
}
return frames;
}
@@ -253,6 +267,53 @@ bool stdin_is_interactive() {
#endif
}
// Reads joc_dmx_config_idx from the first syncframe that carries a JOC payload,
// without decoding anything else. Returns -1 when the stream has no JOC.
int probe_config_idx(const std::string& path) {
std::ifstream input = fs_utf8::open_input(path);
if (!input) {
return -1;
}
::joc::eac3::FrameReader reader;
std::vector<char> chunk(1u << 16);
int config = -1;
bool done = false;
while (!done && config < 0) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
if (got > 0) {
reader.push(reinterpret_cast<const std::uint8_t*>(chunk.data()),
static_cast<std::size_t>(got));
} else {
reader.finish();
done = true;
}
for (;;) {
::joc::eac3::Frame frame;
const ::joc::eac3::FrameReader::Next state = reader.next(&frame);
if (state != ::joc::eac3::FrameReader::Next::Ok) {
break;
}
bool present = false;
::joc::emdf::Container container;
if (!::joc::emdf::find_joc_emdf_optional(
frame.data, frame.size, &present, &container,
&::joc::joc::score_container_as_joc, nullptr)
.ok() ||
!present) {
continue;
}
joc_frame_params params{};
if (::joc::joc::parse_eac3_frame(frame.data, frame.size, &params, &container, nullptr)
.ok()) {
config = static_cast<int>(params.dmx_config_idx);
break;
}
}
}
return config;
}
// Rendered PCM is spooled to a float32 file so the output format can be decided
// once the peak is known, which is what makes --clip-action possible; the
// reference CLI spools for the same reason.
@@ -480,6 +541,7 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
double clipgain_max = 0.0;
bool stream_done = false;
Status failure = Status::success();
std::vector<::joc::eac3::Frame> group;
while (!stream_done && frames < total_frames) {
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
@@ -503,8 +565,36 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
stream_done = true;
break;
}
joc_frame_params params{};
// Count audio frames, not syncframes: a 7-channel program interleaves
// two substreams and one bed frame covers both.
bool repeats = false;
for (const ::joc::eac3::Frame& prior : group) {
if (prior.same_stream(frame)) {
repeats = true;
break;
}
}
if (repeats) {
++frames;
group.clear();
if (frames >= total_frames) {
stream_done = true;
break;
}
}
group.push_back(frame);
bool present = false;
::joc::emdf::Container container;
const Status found = ::joc::emdf::find_joc_emdf_optional(
frame.data, frame.size, &present, &container,
&::joc::joc::score_container_as_joc, nullptr);
if (!found.ok()) {
return Status::fail(found.code(), found.stage(), found.message());
}
if (!present || container.find(::joc::emdf::kIdJoc) == nullptr) {
continue;
}
joc_frame_params params{};
const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size, &params,
&container, nullptr);
if (!parsed.ok()) {
@@ -593,35 +683,51 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std:
return failure;
}
// The decoded core PCM, read one frame at a time and laid out for the JOC
// kernel according to the bed plan (spec section 3: map by channel code, not by
// bed channel number).
class BedSource {
public:
bool open(const std::string& path) {
bool open(const std::string& path, std::size_t channels) {
channels_ = channels;
file_.open(path, std::ios::binary);
return static_cast<bool>(file_);
}
bool read_frame(std::uint64_t index, std::vector<float>* bed5, std::vector<float>* lfe) {
bed5->assign(static_cast<std::size_t>(JOC_CORE_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f);
bool read_frame(std::uint64_t index, int core_channels, const ::joc::joc::BedPlan& plan,
std::vector<float>* bed, std::vector<float>* lfe) {
bed->assign(static_cast<std::size_t>(core_channels) * JOC_FRAME_SAMPLES, 0.0f);
lfe->assign(JOC_FRAME_SAMPLES, 0.0f);
const std::size_t frame_values = static_cast<std::size_t>(JOC_FRAME_SAMPLES) * channels_;
interleaved_.assign(frame_values, 0.0f);
file_.clear();
file_.seekg(static_cast<std::streamoff>(index * kBedFrameBytes), std::ios::beg);
std::vector<float> interleaved(static_cast<std::size_t>(JOC_FRAME_SAMPLES) * 6u);
file_.read(reinterpret_cast<char*>(interleaved.data()),
static_cast<std::streamsize>(interleaved.size() * sizeof(float)));
if (file_.gcount() != static_cast<std::streamsize>(interleaved.size() * sizeof(float))) {
file_.seekg(static_cast<std::streamoff>(index * frame_values * sizeof(float)),
std::ios::beg);
file_.read(reinterpret_cast<char*>(interleaved_.data()),
static_cast<std::streamsize>(frame_values * sizeof(float)));
if (file_.gcount() != static_cast<std::streamsize>(frame_values * sizeof(float))) {
return false;
}
for (std::size_t n = 0; n < JOC_FRAME_SAMPLES; ++n) {
for (std::size_t c = 0; c < JOC_CORE_CHANNELS; ++c) {
(*bed5)[c * JOC_FRAME_SAMPLES + n] = interleaved[n * 6u + kCoreChannels[c]];
for (int lane = 0; lane < core_channels; ++lane) {
const int source = plan.lane[lane];
if (source >= 0) {
(*bed)[static_cast<std::size_t>(lane) * JOC_FRAME_SAMPLES + n] =
interleaved_[n * channels_ + static_cast<std::size_t>(source)];
}
}
if (plan.lfe_channel >= 0) {
(*lfe)[n] = interleaved_[n * channels_ +
static_cast<std::size_t>(plan.lfe_channel)];
}
(*lfe)[n] = interleaved[n * 6u + kLfeChannel];
}
return true;
}
private:
std::ifstream file_;
std::size_t channels_ = 6;
std::vector<float> interleaved_;
};
// ---------------------------------------------------------------------------
@@ -1115,17 +1221,37 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
}
}
// The stream itself decides how many channels the decoded bed has; the same
// answer feeds the ffmpeg decode below and the bed reader.
const int stream_config_idx = probe_config_idx(eac3_path);
const int stream_core_channels =
stream_config_idx >= 0
? static_cast<int>(::joc::joc::num_channels_for_config(
static_cast<std::uint32_t>(stream_config_idx)))
: static_cast<int>(JOC_CORE_CHANNELS);
const std::size_t stream_bed_channels =
stream_core_channels == JOC_MAX_CORE_CHANNELS ? 8u : 6u;
std::string bed_path = cfg.bed_path;
if (bed_path.empty() && !cfg.metadata_only) {
if (temp_dir.empty()) {
temp_dir = create_temp_dir(cfg);
}
bed_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "core51_f32le.raw");
bus.stage(JOC_STAGE_DECODE, "decoding the 5.1 core PCM with ffmpeg");
bed_path = fs_utf8::from_path(fs_utf8::to_path(temp_dir) / "core_f32le.raw");
// A 7-channel program decodes to 8 bed channels; -ac 6 would downmix
// 5.1.2/7.1 into 5.1 and drop the extra pair, and -ac 8 would remap
// 5.1.2 into 7.1, so the native decoder layout is what we want there.
const bool native_bed = stream_core_channels == JOC_MAX_CORE_CHANNELS;
bus.stage(JOC_STAGE_DECODE, native_bed
? "decoding the core PCM with ffmpeg (native layout)"
: "decoding the 5.1 core PCM with ffmpeg");
std::vector<std::string> argv = {cfg.ffmpeg_path, "-hide_banner", "-loglevel", "error", "-y",
"-i", eac3_path};
argv.insert(argv.end(), {"-map", "0:a:0", "-vn", "-ac", "6", "-ar", "48000",
"-c:a", "pcm_f32le", "-f", "f32le", bed_path});
argv.insert(argv.end(), {"-map", "0:a:0", "-vn"});
if (!native_bed) {
argv.insert(argv.end(), {"-ac", "6"});
}
argv.insert(argv.end(), {"-ar", "48000", "-c:a", "pcm_f32le", "-f", "f32le", bed_path});
argv.insert(argv.begin() + 4, {"-drc_scale", fmt("%.10g", cfg.drc_scale)});
if (cfg.target_level != 0) {
argv.insert(argv.begin() + 4, {"-target_level", std::to_string(cfg.target_level)});
@@ -1336,7 +1462,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
}
BedSource bed;
if (!bed.open(bed_path)) {
if (!bed.open(bed_path, stream_bed_channels)) {
return finalize_with_error(JOC_ERR_INPUT_NOT_FOUND, "bed_path",
"cannot open the core PCM: " + bed_path);
}
@@ -1370,6 +1496,15 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
std::thread producer;
std::thread writer;
StageJoiner joiner(&joc_ring, &output_ring, &stop, &producer, &writer);
// Metadata the JOC stage had to work around, counted there and reported after
// the stages join: frames with no JOC payload, frames whose declared object
// count was clamped to 15, and frames whose syncframe carried a second EMDF
// container that is not this frame's JOC metadata.
std::uint64_t missing_joc_frames = 0;
std::uint64_t objects_clamped_frames = 0;
std::uint64_t extra_container_frames = 0;
std::uint64_t chanmap_pair_mismatch_frames = 0;
std::uint64_t bed_mask_mismatch_frames = 0;
// ---- stage 1: the bitstream, on its own thread ------------------------ //
// The serial loop's read/decode/JOC section, verbatim apart from its error
@@ -1380,9 +1515,116 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
bool stream_done = false;
double seconds = 0.0;
JocJob job;
// Audio-frame assembly state (kept across chunk reads).
std::vector<::joc::eac3::Frame> group;
::joc::eac3::Frame group_core;
bool group_has_core = false;
bool group_has_dependent = false;
bool group_has_chanmap = false;
bool group_has_joc = false;
std::uint16_t group_chanmap = 0;
joc_frame_params group_params{};
std::vector<::joc::oamd::BedAssignment> group_bed_assignments;
::joc::joc::BedPlan plan;
bool plan_ready = false;
int resolved_core_channels = 0;
try {
while (!stream_done && !stop.requested()) {
auto stage_started = Clock::now();
// Emits the audio frame the group has accumulated so far.
auto emit_group = [&]() -> Status {
const bool has_joc = group_has_joc;
joc_frame_params params = group_params;
const bool has_core = group_has_core;
const eac3::Frame core_header = group_core;
const bool has_dependent = group_has_dependent;
const bool has_chanmap = group_has_chanmap;
const std::uint16_t chanmap = group_chanmap;
std::vector<::joc::oamd::BedAssignment> assignments =
std::move(group_bed_assignments);
group.clear();
group_has_core = false;
group_has_dependent = false;
group_has_chanmap = false;
group_has_joc = false;
group_chanmap = 0;
group_params = joc_frame_params{};
group_bed_assignments.clear();
if (has_joc) {
std::string error;
::joc::joc::CrossLayerNotes notes;
const Status checked = ::joc::joc::check_cross_layer(
params, has_core ? &core_header : nullptr, has_core, has_dependent,
has_chanmap, chanmap,
assignments.empty() ? nullptr : &assignments, &error, &notes);
if (!checked.ok()) {
return Status::fail(checked.code(), checked.stage(),
error.empty() ? checked.message() : error);
}
if (notes.chanmap_pair_mismatch) {
++chanmap_pair_mismatch_frames;
}
if (notes.bed_mask_mismatch) {
++bed_mask_mismatch_frames;
}
if (params.objects_clamped != 0u) {
++objects_clamped_frames;
}
} else {
++missing_joc_frames;
}
if (!plan_ready) {
if (stream_config_idx < 0) {
return Status::fail(JOC_ERR_EMDF_TRANSPORT, "emdf",
"no_contiguous_joc_container: the input carries "
"no JOC EMDF container in any audio frame");
}
const Status planned = ::joc::joc::compute_bed_plan(
has_core ? &core_header : nullptr, has_core, has_dependent,
has_chanmap, chanmap,
static_cast<std::uint8_t>(stream_config_idx), &plan);
if (!planned.ok()) {
return planned;
}
resolved_core_channels = stream_core_channels;
plan_ready = true;
}
if (!has_joc) {
::joc::joc::make_silent_frame_params(&params);
}
params.dmx_config_idx = static_cast<std::uint8_t>(stream_config_idx);
params.n_channels = static_cast<std::uint8_t>(resolved_core_channels);
if (!bed.read_frame(produced, resolved_core_channels, plan, &bed5, &lfe)) {
return Status::fail(JOC_ERR_INPUT_FORMAT, "bed_path",
"core PCM ended before frame " +
std::to_string(produced));
}
std::string dsp_error;
const Status rebuilt = ::joc::joc::rebuild_objects16(
rebuilder, params, bed5.data(), lfe.data(), gain, &objects16,
&dsp_error);
if (!rebuilt.ok()) {
return Status::fail(rebuilt.code(), "joc", dsp_error);
}
job.index = produced;
job.objects = params.n_objects;
job.clipgain = params.clipgain;
job.objects16.swap(objects16);
// The clock stops before the handoff, so the stage's number is
// its own work (including the chunk read) and never the time it
// spent waiting for the DSP to make room in the ring.
seconds +=
std::chrono::duration<double>(Clock::now() - stage_started).count();
if (!joc_ring.push(&job)) {
return Status::fail(JOC_ERR_STATE, "output",
"the output ring closed while rendering");
}
++produced;
stage_started = Clock::now();
return Status::success();
};
input.read(chunk.data(), static_cast<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
if (got > 0) {
@@ -1408,14 +1650,63 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
stream_done = true;
break;
}
if (!bed.read_frame(produced, &bed5, &lfe)) {
failures.record(produced, StageRank::kJoc, JOC_ERR_INPUT_FORMAT, "bed_path",
"core PCM ended before frame " + std::to_string(produced));
// A repeated (codec, strmtyp, substreamid) combination closes the
// audio frame the group is holding.
bool repeats = false;
for (const ::joc::eac3::Frame& prior : group) {
if (prior.same_stream(frame)) {
repeats = true;
break;
}
}
if (repeats) {
const Status emitted = emit_group();
if (!emitted.ok()) {
failures.record(produced, StageRank::kJoc, emitted.code(),
emitted.stage(), emitted.message());
stream_done = true;
break;
}
if (produced >= total_frames) {
stream_done = true;
break;
}
}
::joc::eac3::Frame header = frame;
header.data = nullptr;
header.size = 0;
group.push_back(header);
if (frame.codec == 0u || frame.strmtyp == 0u) {
group_core = header;
group_has_core = true;
} else {
group_has_dependent = true;
if (frame.has_chanmap != 0u) {
group_has_chanmap = true;
group_chanmap = frame.chanmap;
}
}
bool present = false;
bool extra_container_ignored = false;
::joc::emdf::Container container;
const Status found = ::joc::emdf::find_joc_emdf_optional(
frame.data, frame.size, &present, &container,
&::joc::joc::score_container_as_joc, &extra_container_ignored);
if (extra_container_ignored) {
++extra_container_frames;
}
if (!found.ok()) {
failures.record(produced, StageRank::kJoc, found.code(), found.stage(),
"frame " + std::to_string(produced) + ": " +
found.message());
stream_done = true;
break;
}
if (!present || container.find(::joc::emdf::kIdJoc) == nullptr) {
continue;
}
joc_frame_params params{};
::joc::emdf::Container container;
const Status parsed = ::joc::joc::parse_eac3_frame(frame.data, frame.size,
&params, &container, nullptr);
if (!parsed.ok()) {
@@ -1425,6 +1716,8 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
stream_done = true;
break;
}
group_params = params;
group_has_joc = true;
job.update = ::joc::oamd::OamdUpdate{};
job.has_update = false;
job.outer_offset = 0;
@@ -1449,30 +1742,24 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
}
job.has_update = true;
job.outer_offset = static_cast<std::int64_t>(payload->sample_offset);
const Status bed_ok = ::joc::oamd::parse_id11_bed_assignments(
bytes.data(), bytes.size(), &group_bed_assignments);
if (!bed_ok.ok()) {
failures.record(produced, StageRank::kJoc, bed_ok.code(),
bed_ok.stage(), bed_ok.message());
stream_done = true;
break;
}
}
std::string dsp_error;
const Status rebuilt = ::joc::joc::rebuild_objects16(
rebuilder, params, bed5.data(), lfe.data(), gain, &objects16, &dsp_error);
if (!rebuilt.ok()) {
failures.record(produced, StageRank::kJoc, rebuilt.code(), "joc",
"frame " + std::to_string(produced) + ": " + dsp_error);
stream_done = true;
break;
}
// The final frame of an input that ends exactly on a group boundary
// has already been emitted above.
if (stream_done && !group.empty() && produced < total_frames) {
const Status emitted = emit_group();
if (!emitted.ok()) {
failures.record(produced, StageRank::kJoc, emitted.code(),
emitted.stage(), emitted.message());
}
job.index = produced;
job.objects = params.n_objects;
job.clipgain = params.clipgain;
job.objects16.swap(objects16);
// The clock stops before the handoff, so the stage's number is
// its own work (including the chunk read) and never the time it
// spent waiting for the DSP to make room in the ring.
seconds += std::chrono::duration<double>(Clock::now() - stage_started).count();
if (!joc_ring.push(&job)) {
stream_done = true;
break;
}
++produced;
stage_started = Clock::now();
}
}
} catch (...) {
@@ -1733,6 +2020,29 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
// frames and join it. Every in-loop write is finished before the finalize
// (tail flush, spool -> WAV, SHA-256, report) looks at the output.
joiner.shutdown(false);
if (missing_joc_frames != 0u) {
bus.warning(std::to_string(missing_joc_frames) +
" audio frame(s) carried no JOC payload; their object channels stay silent");
}
if (objects_clamped_frames != 0u) {
bus.warning(std::to_string(objects_clamped_frames) +
" audio frame(s) declared more than 15 objects; the count was clamped to 15");
}
if (extra_container_frames != 0u) {
bus.warning(std::to_string(extra_container_frames) +
" audio frame(s) carried a second EMDF container; the container whose OAMD "
"and JOC payloads share a group id (and whose JOC payload parses) was used");
}
if (chanmap_pair_mismatch_frames != 0u) {
bus.warning(std::to_string(chanmap_pair_mismatch_frames) +
" audio frame(s): the chanmap declares a different extra pair than "
"joc_dmx_config_idx; the bed mapping follows the chanmap");
}
if (bed_mask_mismatch_frames != 0u) {
bus.warning(std::to_string(bed_mask_mismatch_frames) +
" audio frame(s): the OAMD bed mask declares a different extra pair than "
"joc_dmx_config_idx; the bed mask only affects metadata");
}
StageFailure recorded;
if (failures.earliest(&recorded)) {
return finalize_with_error(recorded.code, recorded.stage_text, recorded.message);
+112 -5
View File
@@ -249,16 +249,120 @@ bool contains_payload(const std::uint8_t* frame, std::size_t size, std::uint8_t
} // namespace
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
if (data == nullptr || offset + 4u > size) return 0;
namespace {
std::uint32_t read_bits(const std::uint8_t* data, std::size_t start, std::size_t count) {
std::uint32_t value = 0;
for (std::size_t index = start; index < start + count; ++index) {
value = (value << 1) | ((data[index >> 3] >> (7u - (index & 7u))) & 1u);
}
return value;
}
// AC-3 syncframe length in 16-bit words, indexed by fscod then frmsizecod.
const std::uint16_t kAc3FrameWords[3][38] = {
{64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
896, 896, 1024, 1024, 1152, 1152, 1280, 1280},
{69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
975, 975, 1114, 1114, 1253, 1253, 1393, 1393},
{96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920},
};
} // namespace
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out) {
if (data == nullptr || offset + 6u > size) return 0;
const std::uint16_t syncword =
static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]);
if (syncword != kEac3Syncword) return 0;
const std::size_t base = offset * 8u;
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
if (bsid <= 8u) {
const std::uint32_t fscod = read_bits(data, base + 32u, 2u);
const std::uint32_t frmsizecod = read_bits(data, base + 34u, 6u);
if (fscod > 2u || frmsizecod >= 38u) return 0;
if (out != nullptr) {
out->frame_bytes = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
out->offset = offset;
out->codec = 0;
out->strmtyp = 0;
out->substreamid = 0;
}
return static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
}
const std::size_t words =
(static_cast<std::size_t>(data[offset + 2] & 0x07u) << 8) | data[offset + 3];
if (out != nullptr) {
out->frame_bytes = (words + 1u) * 2u;
out->offset = offset;
out->codec = 1;
out->strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
out->substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
}
return (words + 1u) * 2u;
}
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
return frame_header_at(data, size, offset, nullptr);
}
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream) {
constexpr std::size_t kMaxSubstreams = 8;
FrameHeader seen[kMaxSubstreams];
std::size_t count = 0;
std::size_t consumed = 0;
while (count < kMaxSubstreams) {
FrameHeader header;
const std::size_t bytes = frame_header_at(data, size, offset + consumed, &header);
if (bytes == 0 || offset + consumed + bytes > size) break;
bool repeats = false;
for (std::size_t index = 0; index < count; ++index) {
if (seen[index].same_stream(header)) {
repeats = true;
break;
}
}
if (repeats) return consumed;
seen[count++] = header;
consumed += bytes;
}
if (count != 0u && consumed != 0u && end_of_stream) return consumed;
return 0;
}
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out) {
if (out == nullptr || frame == nullptr || frame_bytes < 8u) return false;
std::size_t offsets[4096];
std::size_t count = 0;
marker_offsets(frame, frame_bytes, offsets, 4096, &count);
for (std::size_t i = 0; i < count; ++i) {
Container candidate;
if (!parse_container(frame, frame_bytes * 8u, offsets[i], &candidate)) continue;
if (!candidate.has(kIdOamd) || !candidate.has(kIdJoc)) continue;
for (std::size_t p = 0; p < candidate.payload_count; ++p) {
if (candidate.payloads[p].id != kIdJoc) continue;
// joc_dmx_config_idx is the first 3 bits of the JOC payload, followed by
// joc_num_objects_bits (6) and joc_ext_config_idx (3).
const std::size_t bit = candidate.payloads[p].bit_offset;
std::uint32_t value = 0;
for (unsigned index = 0; index < 3u; ++index) {
const std::size_t position = bit + index;
if (position / 8u >= frame_bytes) return false;
value = (value << 1) | ((frame[position / 8u] >> (7u - (position % 8u))) & 1u);
}
*out = value;
return true;
}
}
return false;
}
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes) {
if (frame == nullptr || frame_bytes < 8u) return false;
@@ -333,12 +437,15 @@ ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_fram
if (frames == 0) {
result.joc = JocState::kUnknown;
if (result.detail[0] == '\0') result.detail = "no syncframes";
} else if (with_joc == frames) {
} else if (with_joc != 0) {
result.joc = JocState::kYes;
result.detail = "every examined syncframe carries the JOC EMDF container";
result.detail = (with_joc == frames)
? "every examined syncframe carries the JOC EMDF container"
: "the JOC EMDF container is present in part of the examined "
"audio frames (dependent substream or a late first frame)";
} else {
result.joc = JocState::kNo;
result.detail = "at least one examined syncframe has no JOC EMDF container";
result.detail = "no examined syncframe carries a JOC EMDF container";
}
return result;
}
+43 -11
View File
@@ -4,12 +4,14 @@
// because container metadata (MP4 dec3, Matroska A_EAC3) says only "E-AC-3" and
// the JOC flag inside it is frequently missing.
//
// The criterion implemented here is deliberately the same one the rendering core
// applies before it will render a frame: a syncframe carries JOC when it holds
// exactly one top-level EMDF container (syncword 0x5838, any of the eight bit
// alignments) whose payload list contains both ID11 (OAMD) and ID14 (JOC).
// Agreeing with the core matters: a looser test would claim files the core then
// refuses, which is worse than declining them.
// A syncframe carries JOC when it holds exactly one top-level EMDF container
// (syncword 0x5838, any of the eight bit alignments) whose payload list contains
// both ID11 (OAMD) and ID14 (JOC) — the same per-frame test the rendering core
// applies. The stream counts as JOC when at least one examined audio frame
// carries one: in a 7-channel program the JOC payload lives in the dependent
// substream while the independent substream is a plain 5.1 core, and an AC-3
// core may precede it, so "every syncframe carries JOC" is not a valid rule.
// The first audio frame may also carry no metadata at all.
#pragma once
#include <cstddef>
@@ -17,16 +19,26 @@
namespace joc_eac3 {
// E-AC-3 syncframe header fields the plugin needs.
// Syncframe header fields the plugin needs. codec/strmtyp/substreamid identify
// the substream: within one audio frame each combination appears once, which is
// what turns a syncframe stream into audio frames.
struct FrameHeader {
std::size_t frame_bytes = 0;
std::size_t offset = 0;
std::uint8_t codec = 0; // 0 = AC-3, 1 = E-AC-3
std::uint8_t strmtyp = 0;
std::uint8_t substreamid = 0;
bool same_stream(const FrameHeader& other) const {
return codec == other.codec && strmtyp == other.strmtyp &&
substreamid == other.substreamid;
}
};
enum class JocState {
kYes, // every frame examined carried a JOC EMDF container
kNo, // at least one frame examined did not
kUnknown, // not enough data, or the stream is not walkable E-AC-3
kYes, // at least one examined syncframe carried a JOC EMDF container
kNo, // no examined syncframe carried one
kUnknown, // not enough data, or the stream is not walkable ac3/eac3
};
struct ScanResult {
@@ -42,9 +54,29 @@ struct ScanResult {
ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_frames);
// Length of the syncframe starting at data[offset], or 0 when there is no valid
// header there. Mirrors the core's frmsiz handling exactly.
// header there. AC-3 and E-AC-3 are told apart by bsid exactly as the core does:
// bsid <= 8 sizes the frame from fscod/frmsizecod, anything above uses frmsiz.
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset);
// Same, and fills the substream identity. Returns 0 when no header is there.
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out);
// Length of the whole audio frame starting at data[offset]: every syncframe up to
// (and excluding) the first repeated (codec, strmtyp, substreamid) combination.
// A 7-channel program interleaves an independent and a dependent substream, so one
// audio frame is two syncframes and one bed frame.
//
// Returns 0 when the group is still open inside the buffer -- the caller has to ask
// again with more data -- unless `end_of_stream` says no more bytes will arrive, in
// which case the trailing group is complete by definition.
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream);
// Reads joc_dmx_config_idx (the first 3 bits of the JOC payload) out of one
// syncframe. False when the frame carries no JOC payload.
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out);
// True when this single syncframe carries the JOC EMDF container.
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes);
+177 -49
View File
@@ -27,7 +27,11 @@ 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 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,
// 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:
@@ -63,6 +67,30 @@ bool has_mov_timeline(const std::string& path) {
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.
//
@@ -83,6 +111,7 @@ struct CoreApi {
std::uint32_t(JOC_CALL* abi_version)() = joc_abi_version;
const char*(JOC_CALL* version_string)() = joc_version_string;
const char*(JOC_CALL* error_name)(joc_error) = joc_error_name;
const char*(JOC_CALL* last_error_detail)() = joc_last_error_detail;
};
bool file_exists(const std::string& path) {
@@ -111,6 +140,22 @@ const char* error_text(const CoreApi& api, joc_error code) {
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.
// ---------------------------------------------------------------------------
@@ -426,40 +471,47 @@ FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes,
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;
if (buffer.size() == stream_bytes) {
std::size_t offset = 0;
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;
}
if (whole_file) {
frames = seen;
probe.detail = "frame count walked over the whole file";
} else if (scan.all_frames_same_size) {
frames = stream_bytes / scan.first_frame_bytes;
} else if (seen != 0 && same_audio_frame_size) {
frames = stream_bytes / first_audio_frame;
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.
std::size_t offset = 0;
std::uint64_t seen = 0;
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;
++seen;
}
frames = (offset != 0) ? static_cast<std::uint64_t>(
(static_cast<double>(stream_bytes) /
static_cast<double>(offset)) *
static_cast<double>(seen))
: 0;
frames = static_cast<std::uint64_t>(
(static_cast<double>(stream_bytes) / static_cast<double>(offset)) *
static_cast<double>(seen));
probe.detail = "frame count estimated from a variable frame size";
}
const int config = scan_config_idx(buffer.data(), got);
probe.frames = frames;
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 =
static_cast<double>(frames) * static_cast<double>(kFrameSamples) / 48000.0;
return probe;
@@ -570,7 +622,10 @@ struct Engine::Impl {
}
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;
bool eac3_eof = false;
bool bed_eof = false;
@@ -650,6 +705,10 @@ struct Engine::Impl {
bool position_bare(std::uint64_t frame, std::string* error);
bool start_bed(std::uint64_t source_sample, 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) {
@@ -681,9 +740,14 @@ bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) {
index_complete = true;
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;
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;
frame_offsets.push_back(index_stream_offset + consumed);
consumed += bytes;
@@ -734,13 +798,49 @@ bool Engine::Impl::position_bare(std::uint64_t frame, std::string* error) {
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) {
// The 5.1 core PCM, exactly as the reference renderer's own core decode does
// it: 5.1 interleaved float32 at 48 kHz, the layout the renderer expects
// (L R C LFE Ls Rs).
// The core PCM the kernel expects: interleaved float32 at 48 kHz, laid out as
// L R C LFE Ls Rs for 5.1 and L R C LFE BL BR SL SR for 7.1.
// -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.
//
// -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
// 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
@@ -752,7 +852,11 @@ bool Engine::Impl::start_bed(std::uint64_t source_sample, std::string* error) {
if (!offset.empty()) input_arguments += L" -ss " + offset;
std::wstring bed_arguments = L"-map 0:a:";
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",
stderr_path_for(L"joc_ffmpeg_bed.log"), error);
}
@@ -804,7 +908,6 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
impl.reset_feed_state();
impl.end_sample = sample_of_seconds(settings.length_seconds);
impl.eac3_buffer.resize(kEac3Chunk);
impl.bed_buffer.resize(kBedFramesChunk * kBedChannels);
if (!load_api(std::string(), &impl.api, error)) return false;
const std::uint32_t abi = impl.api.abi_version();
@@ -834,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{};
config.struct_size = sizeof(config);
config.struct_version = 1;
@@ -888,7 +996,7 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
if (created != JOC_OK) {
if (error != nullptr) {
*error = std::string("cannot create the render stream: ") +
error_text(impl.api, created);
error_with_detail(impl.api, created);
}
return false;
}
@@ -972,7 +1080,7 @@ bool Engine::seek(double seconds, double total_seconds, std::string* error) {
if (reset != JOC_OK) {
if (error != nullptr) {
*error = std::string("cannot reset the render stream: ") +
error_text(impl.api, reset);
error_with_detail(impl.api, reset);
}
return false;
}
@@ -1042,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,
impl.eac3_buffer.size() - impl.eac3_carry);
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) {
impl.eac3_eof = true;
} else {
@@ -1051,8 +1163,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (impl.metadata_skip != 0) {
std::size_t dropped = 0;
while (impl.metadata_skip != 0) {
const std::size_t bytes =
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, dropped);
const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, dropped, at_eof);
if (bytes == 0 || dropped + bytes > total) break;
dropped += bytes;
--impl.metadata_skip;
@@ -1074,8 +1186,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
std::size_t offset = 0;
std::uint64_t complete = 0;
while (true) {
const std::size_t bytes =
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, offset);
const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, offset, at_eof);
if (bytes == 0 || offset + bytes > total) break;
offset += bytes;
++complete;
@@ -1096,8 +1208,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
pushed_frames = allowed;
std::size_t walk = 0;
for (std::uint64_t index = 0; index < pushed_frames; ++index) {
const std::size_t bytes = joc_eac3::frame_bytes_at(
impl.eac3_buffer.data(), total, 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;
}
@@ -1113,7 +1225,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (pushed != JOC_OK) {
if (error != nullptr) {
*error = std::string("E-AC-3 push failed: ") +
error_text(impl.api, pushed);
error_with_detail(impl.api, pushed);
}
return 0;
}
@@ -1136,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
// quietly drops the rest of the stream's alignment.
while (!impl.bed_eof && impl.bed_frames_pushed < impl.frames_queued) {
constexpr std::size_t kFrameBytes = kFrameSamples * kBedChannels * sizeof(float);
const std::size_t whole_frames = impl.bed_staged_bytes / kFrameBytes;
const std::size_t frame_bytes = kFrameSamples * impl.bed_channels * sizeof(float);
const std::size_t whole_frames = impl.bed_staged_bytes / frame_bytes;
if (whole_frames != 0) {
joc_stream_buffer input{};
input.struct_size = sizeof(input);
input.struct_version = 1;
input.kind = JOC_STREAM_IN_CORE_PCM;
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_count = static_cast<std::uint32_t>(whole_frames * kFrameSamples);
const joc_error pushed = impl.api.push(impl.stream, &input, nullptr, nullptr);
if (pushed != JOC_OK) {
if (error != nullptr) {
*error = std::string("core PCM push failed: ") +
error_text(impl.api, pushed);
error_with_detail(impl.api, pushed);
}
return 0;
}
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;
if (impl.bed_staged_bytes != 0) {
std::memmove(impl.bed_buffer.data(),
@@ -1198,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);
if (pulled != JOC_OK) {
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;
}
@@ -1248,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);
if (flushed != JOC_OK) {
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;
}
@@ -1267,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.samples_out),
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",
static_cast<unsigned long long>(impl.bed_bytes_read),
static_cast<unsigned long long>(impl.bed_staged_bytes));
+6 -9
View File
@@ -4,17 +4,14 @@
// drives when the output has to be compared byte for byte against joc_cli.
//
// Two inputs are needed to render, and they come from different places:
// * the bare E-AC-3 syncframes -- the metadata stream -- read straight from the
// file, since the plugin only claims bare .eac3/.ec3 today;
// * the E-AC-3 syncframes -- the metadata stream -- read straight from the file;
// * the 5.1 core PCM of those same frames -- the rendering core does not decode
// the core (it has no AC-3 decoder at all), so ffmpeg produces it exactly the
// way the reference CLI does.
// the core (it has no AC-3 decoder at all), so ffmpeg produces it.
//
// The core is loaded from joc_core.dll through the public C ABI and called via
// function pointers. Static linking is not an option: the core's static library
// carries the internals but not the API layer in src/api, and the plugin is not
// allowed to compile core sources. Loading explicitly also lets the component
// ship its own copy of the DLL and check its ABI version at start-up.
// The core is the C++ kernel under kernel/, compiled into this component and called
// through its public C API via the CoreApi table. No DLL is shipped or loaded: the
// table is filled with the linked entry points and start-up only checks the ABI
// version.
#pragma once
#include <cstddef>
+1 -1
View File
@@ -12,7 +12,7 @@
// Kept in one place: the string reported to foobar2000 and written to the log
// must not drift apart.
#define JOC_VERSION "0.3.0"
#define JOC_VERSION "0.4.0"
DECLARE_COMPONENT_VERSION("JOC decoder (E-AC-3 JOC)", JOC_VERSION,
"Plays E-AC-3 JOC (Dolby Atmos) files: the JOC objects are "