diff --git a/README.md b/README.md
index d74e470..7ce5d58 100644
--- a/README.md
+++ b/README.md
@@ -67,6 +67,13 @@ Preferences → Tools → **JOC 解码器**:
对调(7.1 里是 5↔7、6↔8,从 1 数)。只在这种布局同时有侧环绕和后环绕时生效:
7.1、7.1.2、7.1.4、9.1.4、9.1.6;5.1.2/5.1.4 的后两个槽位是天空声道,不会被误换。
它只改交付 PCM 的声道顺序,不动解码、对象位置与布局定义,float32 与 int24 交付都生效。
+* **接管普通 PCM 源(ffmpeg 解码)** —— 默认关闭。打开后接管
+ `wav/flac/mp3/aac/ogg/opus/wma/aif/aiff/ac3/dts/dtshd/dtsma/wv/ape/tta/mpc/au/snd/caf/thd/mlp/truehd`,
+ 由 ffmpeg 唯一负责解码到 48 kHz float32,再交给内核的 PCM layout 输入做 2.0/5.1/7.1 的 binaural 渲染;
+ 此时不再把这个文件交给其他解码器。TrueHD/MLP 也接管;带 Atmos 的 TrueHD 经 ffmpeg 解成 PCM 后对象信息会丢失,暂按普通多声道处理。TrueHD 在 MKV 等容器里同样走容器回退,不受扩展名列表限制。DSD(`.dsf/.dff`)不在接管范围。
+ `.mp4/.m4a/.mkv/.mka/.mov/.webm` 仍先做 JOC 检测,非 JOC 的 E-AC-3 音轨才回退到 ffmpeg。
+ 如果内置解码器在 foobar2000 的 input priority 里排在前面,WAV/AC3/MP3 等文件会先被内置解码器拿走,
+ 本组件不会收到 open 调用;需要把 **JOC decoder** 提到对应内置解码器之前。
* **ffmpeg.exe** 可执行文件路径。
* **界面语言** —— **跟随系统** / **简体中文** / **English**。设置页的全部文字、状态行和用户可见的
错误信息都用这一项选定的语言,切换后立刻生效;选择存在组件自己的配置里。"跟随系统"按 Windows 的
diff --git a/foo_input_joc.vcxproj b/foo_input_joc.vcxproj
index 7dfc11f..6396a53 100644
--- a/foo_input_joc.vcxproj
+++ b/foo_input_joc.vcxproj
@@ -80,7 +80,7 @@
-->
NDEBUG;_WINDLL;WIN32_LEAN_AND_MEAN;NOMINMAX;_CRT_SECURE_NO_WARNINGS;JOC_STATIC;%(PreprocessorDefinitions)
$(ProjectDir)src\joc_pch.h;%(ForcedIncludeFiles)
- $(ProjectDir)src;$(ProjectDir)SDK\foobar2000;$(ProjectDir)SDK;%(AdditionalIncludeDirectories)
+ $(ProjectDir)src;$(ProjectDir)kernel\include;$(ProjectDir)kernel\src;$(ProjectDir)SDK\foobar2000;$(ProjectDir)SDK;%(AdditionalIncludeDirectories)
4100;4127;%(DisableSpecificWarnings)
@@ -99,6 +99,7 @@
+
@@ -110,6 +111,7 @@
+
diff --git a/kernel/include/joc_stream.h b/kernel/include/joc_stream.h
index 137b99e..c469dea 100644
--- a/kernel/include/joc_stream.h
+++ b/kernel/include/joc_stream.h
@@ -40,7 +40,8 @@ typedef struct joc_stream joc_stream;
typedef enum joc_stream_input {
JOC_STREAM_IN_EAC3 = 0, /* bare E-AC-3 syncframes (the metadata stream) */
JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* objects16 frames, planar [16][1536] each */
- JOC_STREAM_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */
+ JOC_STREAM_IN_CORE_PCM = 3, /* the 5.1 core PCM of the pushed E-AC-3 frames */
+ JOC_STREAM_IN_PCM_LAYOUT = 4 /* interleaved 48k f32, mapped by pcm_layout */
} joc_stream_input;
typedef enum joc_stream_output {
@@ -68,6 +69,8 @@ typedef struct joc_stream_config {
* .jochrtf field (hrtf_path) or a Rosella .personalized_headphone model. */
const char* personalized_headphone_path; /* Rosella .personalized_headphone input */
uint32_t jochrtf_room; /* non-zero enables the project room */
+ /* PCM_LAYOUT input: "2.0", "5.1", "7.1", or NULL/"" to infer by channels. */
+ const char* pcm_layout;
} joc_stream_config;
typedef struct joc_stream_buffer {
diff --git a/kernel/include/joc_version.h b/kernel/include/joc_version.h
index 0c131d4..4b6bcf8 100644
--- a/kernel/include/joc_version.h
+++ b/kernel/include/joc_version.h
@@ -10,7 +10,7 @@
#define JOC_VERSION_MAJOR 1
#define JOC_VERSION_MINOR 3
-#define JOC_VERSION_PATCH 1
-#define JOC_VERSION_STRING "1.3.1"
+#define JOC_VERSION_PATCH 3
+#define JOC_VERSION_STRING "1.3.3"
#endif /* JOC_VERSION_H */
diff --git a/kernel/joc_kernel.vcxproj b/kernel/joc_kernel.vcxproj
index 216f898..b9ff31a 100644
--- a/kernel/joc_kernel.vcxproj
+++ b/kernel/joc_kernel.vcxproj
@@ -185,6 +185,7 @@
+
diff --git a/kernel/src/api/joc_stream_api.cpp b/kernel/src/api/joc_stream_api.cpp
index 350d178..2db3d31 100644
--- a/kernel/src/api/joc_stream_api.cpp
+++ b/kernel/src/api/joc_stream_api.cpp
@@ -46,6 +46,7 @@ joc::stream::Config to_config(const joc_stream_config& config) {
if (config.rosella_filterbank != 0u) {
out.rosella_filterbank = config.rosella_filterbank;
}
+ if (config.pcm_layout != nullptr) { out.pcm_layout = config.pcm_layout; }
return out;
}
@@ -95,6 +96,19 @@ joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer*
}
return pushed;
}
+ if (input->kind == JOC_STREAM_IN_PCM_LAYOUT) {
+ if (input->sample_rate != 0u && input->sample_rate != 48000u) {
+ return joc::Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender,
+ "PCM layout input must be 48 kHz");
+ }
+ std::size_t consumed = 0;
+ const joc::Status pushed = stream->instance.push_pcm_layout(
+ input->pcm, input->sample_count, input->channels, &consumed);
+ if (consumed_samples != nullptr) {
+ *consumed_samples = static_cast(consumed);
+ }
+ return pushed;
+ }
std::size_t consumed = 0;
const std::uint32_t channels = input->channels != 0u ? input->channels : 6u;
const joc::Status pushed =
diff --git a/kernel/src/stream/stream.cpp b/kernel/src/stream/stream.cpp
index bc599d6..3d2d81b 100644
--- a/kernel/src/stream/stream.cpp
+++ b/kernel/src/stream/stream.cpp
@@ -6,6 +6,7 @@
#include "adm/adm_metadata.h"
#include "foundation/status.h"
+#include "virtual_speaker/virtual_speaker.h"
#include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.h"
#include "joc_bitstream/bed_layout_map.h"
@@ -45,6 +46,7 @@ void Stream::reset_state() {
bed_pending_.clear();
bed_read_offset_ = 0;
objects16_.clear();
+ pcm_input_.reset();
objects_pending_.clear();
objects_read_offset_ = 0;
output_.clear();
@@ -77,7 +79,7 @@ Status Stream::create(const Config& config) {
return Status::fail(JOC_ERR_STATE, stage::kRender, "stream already created");
}
config_ = config;
- if (config_.input > JOC_STREAM_IN_PCM_OBJECTS16 ||
+ if (config_.input > JOC_STREAM_IN_PCM_LAYOUT ||
config_.output > JOC_STREAM_OUT_BINAURAL) {
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender, "unknown stream kind");
}
@@ -87,6 +89,20 @@ Status Stream::create(const Config& config) {
"objects16 input with objects16 output would do nothing");
}
gain_ = static_cast(std::pow(10.0, config_.gain_db / 20.0));
+ if (config_.input == JOC_STREAM_IN_PCM_LAYOUT && !config_.pcm_layout.empty() &&
+ config_.pcm_layout != "auto") {
+ const virtual_speaker::Layout* layout =
+ virtual_speaker::find_layout(config_.pcm_layout);
+ if (layout == nullptr) {
+ return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender,
+ "unknown PCM layout: " + config_.pcm_layout);
+ }
+ const Status configured =
+ pcm_input_.configure(config_.pcm_layout, layout->channels, config_.gain_db);
+ if (!configured.ok()) {
+ return configured;
+ }
+ }
if (config_.input == JOC_STREAM_IN_EAC3) {
rebuilder_ = ejoc_renderer_create();
@@ -453,6 +469,47 @@ Status Stream::push_objects16(const float* planar16, std::size_t samples, std::s
return process_objects16_frames(false);
}
+Status Stream::push_pcm_layout(const float* interleaved, std::size_t samples,
+ std::uint32_t channels, std::size_t* consumed) {
+ if (consumed != nullptr) {
+ *consumed = 0;
+ }
+ if (interleaved == nullptr || samples == 0u) {
+ return Status::success();
+ }
+ if (pcm_input_.layout() == nullptr) {
+ const Status configured = pcm_input_.configure(config_.pcm_layout,
+ static_cast(channels),
+ config_.gain_db);
+ if (!configured.ok()) {
+ return configured;
+ }
+ } else if (pcm_input_.channels() != static_cast(channels)) {
+ return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
+ "PCM channel count changed mid-stream");
+ }
+ Status status = pcm_input_.append(interleaved, samples);
+ if (!status.ok()) {
+ return status;
+ }
+ std::size_t processed = 0;
+ while (pcm_input_.frame_ready()) {
+ status = pcm_input_.take_frame(&objects_frame_);
+ if (!status.ok()) {
+ return status;
+ }
+ status = push_objects16(objects_frame_.data(), kFrameSamples, nullptr);
+ if (!status.ok()) {
+ return status;
+ }
+ processed += kFrameSamples;
+ }
+ if (consumed != nullptr) {
+ *consumed = processed;
+ }
+ return Status::success();
+}
+
Status Stream::process_objects16_frames(bool drain_all) {
constexpr std::size_t kFrameValues =
static_cast(JOC_OUTPUT_CHANNELS) * kFrameSamples;
@@ -590,9 +647,11 @@ Status Stream::render_objects16(const std::vector& objects16) {
if (speaker_enabled_) {
std::string error;
const Status stepped =
- speaker::step(&speaker_, objects16, pending_metadata_.has_update
- ? &pending_metadata_.update
- : nullptr,
+ speaker::step(&speaker_, objects16,
+ pending_metadata_.has_update
+ ? &pending_metadata_.update
+ : (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update()
+ : nullptr),
&error);
if (!stepped.ok()) {
return Status::fail(stepped.code(), stage::kRender, error);
@@ -611,7 +670,8 @@ Status Stream::render_objects16(const std::vector& objects16) {
const Status submitted =
binaural_.submit_frame(objects16.data(),
- pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
+ pending_metadata_.has_update ? &pending_metadata_.update
+ : (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update() : nullptr),
static_cast(info_.frames_out),
pending_metadata_.outer_offset,
static_cast(config_.object_delay_samples));
@@ -638,7 +698,8 @@ Status Stream::render_objects16(const std::vector& objects16) {
Status Stream::render_rosella_objects16(const std::vector& objects16) {
const Status submitted =
rosella_.submit_frame(objects16.data(),
- pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
+ pending_metadata_.has_update ? &pending_metadata_.update
+ : (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update() : nullptr),
static_cast(info_.frames_out),
pending_metadata_.outer_offset,
static_cast(config_.object_delay_samples));
diff --git a/kernel/src/stream/stream.h b/kernel/src/stream/stream.h
index 5740926..8422ca7 100644
--- a/kernel/src/stream/stream.h
+++ b/kernel/src/stream/stream.h
@@ -15,6 +15,7 @@
#include "joc_stream.h"
#include "oamd/oamd_parser.h"
#include "speaker/speaker_step.h"
+#include "virtual_speaker/virtual_speaker.h"
namespace joc::stream {
@@ -34,6 +35,7 @@ struct Config {
// (.jochrtf).
std::string personalized_headphone_path;
std::uint32_t jochrtf_room = 1;
+ std::string pcm_layout;
// Filterbank of the Rosella path; the other binaural inputs ignore it.
std::uint32_t rosella_filterbank = JOC_ROSELLA_FILTERBANK_PUBLIC;
};
@@ -99,6 +101,8 @@ public:
Status push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels,
std::size_t* consumed);
Status push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed);
+ Status push_pcm_layout(const float* interleaved, std::size_t samples, std::uint32_t channels,
+ std::size_t* consumed);
Status pull(float* destination, std::size_t capacity_samples, std::size_t* produced);
Status flush();
Status reset();
@@ -160,6 +164,9 @@ private:
std::size_t bed_read_offset_ = 0;
std::vector bed5_;
std::vector lfe_;
+ virtual_speaker::Input pcm_input_;
+ oamd::OamdUpdate fixed_update_{};
+ bool has_fixed_update_ = false;
std::vector objects_pending_;
std::size_t objects_read_offset_ = 0;
std::vector objects_frame_;
diff --git a/kernel/src/virtual_speaker/virtual_speaker.cpp b/kernel/src/virtual_speaker/virtual_speaker.cpp
new file mode 100644
index 0000000..493b927
--- /dev/null
+++ b/kernel/src/virtual_speaker/virtual_speaker.cpp
@@ -0,0 +1,161 @@
+#include "virtual_speaker/virtual_speaker.h"
+
+#include
+#include
+#include
+
+#include "joc_core.h"
+
+namespace joc::virtual_speaker {
+
+namespace {
+
+constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES;
+constexpr std::size_t kSourceChannels = JOC_OUTPUT_CHANNELS;
+
+// WAV order follows the ffmpeg layouts: 5.1 = L R C LFE BL BR,
+// 7.1 = L R C LFE BL BR SL SR.
+constexpr Layout kLayouts[] = {
+ {"2.0",
+ 2,
+ {{false, -1.0, 1.0, 0.0},
+ {false, 1.0, 1.0, 0.0}}},
+ {"5.1",
+ 6,
+ {{false, -1.0, 1.0, 0.0},
+ {false, 1.0, 1.0, 0.0},
+ {false, 0.0, 1.0, 0.0},
+ {true, 0.0, 1.0, 0.0},
+ {false, -1.0, -1.0, 0.0},
+ {false, 1.0, -1.0, 0.0}}},
+ {"7.1",
+ 8,
+ {{false, -1.0, 1.0, 0.0},
+ {false, 1.0, 1.0, 0.0},
+ {false, 0.0, 1.0, 0.0},
+ {true, 0.0, 1.0, 0.0},
+ {false, -1.0, -1.0, 0.0},
+ {false, 1.0, -1.0, 0.0},
+ {false, -1.0, 0.0, 0.0},
+ {false, 1.0, 0.0, 0.0}}},
+};
+
+std::int16_t q_for_axis(double value) {
+ if (value <= -0.999) {
+ return 0;
+ }
+ if (value >= 0.999) {
+ return 32767;
+ }
+ return 16384;
+}
+
+void fill_fixed_positions(const Layout& layout, oamd::OamdUpdate* update) {
+ update->object_count = static_cast(kSourceChannels);
+ update->block_offset_samples = 0;
+ update->ramp_duration_samples = 0;
+ int slot = 1;
+ for (int channel = 0; channel < layout.channels; ++channel) {
+ const ChannelSpec& spec = layout.channel_specs[channel];
+ const int destination = spec.lfe ? 0 : slot++;
+ update->slots[destination].q1 = q_for_axis(spec.x);
+ update->slots[destination].q2 = q_for_axis(-spec.y);
+ update->slots[destination].q3 = q_for_axis(spec.z);
+ }
+}
+
+} // namespace
+
+const Layout* find_layout(const std::string& name) {
+ for (const Layout& layout : kLayouts) {
+ if (name == layout.name) {
+ return &layout;
+ }
+ }
+ return nullptr;
+}
+
+const Layout* detect_layout(int channels) {
+ for (const Layout& layout : kLayouts) {
+ if (layout.channels == channels) {
+ return &layout;
+ }
+ }
+ return nullptr;
+}
+
+Status Input::configure(const std::string& layout_name, int channels, double gain_db) {
+ const Layout* layout =
+ (layout_name.empty() || layout_name == "auto")
+ ? detect_layout(channels)
+ : find_layout(layout_name);
+ if (layout == nullptr || layout->channels != channels) {
+ return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
+ "PCM layout does not match the channel count");
+ }
+ layout_ = layout;
+ channels_ = channels;
+ gain_ = static_cast(std::pow(10.0, gain_db / 20.0));
+ pending_.clear();
+ read_offset_ = 0;
+ fill_fixed_positions(*layout_, &fixed_update_);
+ return Status::success();
+}
+
+void Input::reset() {
+ pending_.clear();
+ read_offset_ = 0;
+}
+
+Status Input::append(const float* interleaved, std::size_t frames) {
+ if (interleaved == nullptr || frames == 0u) {
+ return Status::success();
+ }
+ if (layout_ == nullptr || channels_ <= 0) {
+ return Status::fail(JOC_ERR_STATE, stage::kRender,
+ "virtual speaker input is not configured");
+ }
+ pending_.insert(pending_.end(), interleaved,
+ interleaved + frames * static_cast(channels_));
+ return Status::success();
+}
+
+bool Input::frame_ready() const {
+ return layout_ != nullptr &&
+ pending_.size() - read_offset_ >=
+ kFrameSamples * static_cast(channels_);
+}
+
+Status Input::take_frame(std::vector* objects16) {
+ if (objects16 == nullptr) {
+ return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
+ "objects16 output is null");
+ }
+ if (!frame_ready()) {
+ return Status::fail(JOC_ERR_STATE, stage::kRender,
+ "no complete PCM frame is buffered");
+ }
+ objects16->assign(kSourceChannels * kFrameSamples, 0.0f);
+ const float* source = pending_.data() + read_offset_;
+ int slot = 1;
+ for (int channel = 0; channel < layout_->channels; ++channel) {
+ const int destination = layout_->channel_specs[channel].lfe ? 0 : slot++;
+ for (std::size_t sample = 0; sample < kFrameSamples; ++sample) {
+ (*objects16)[static_cast(destination) * kFrameSamples + sample] =
+ source[sample * static_cast(channels_) + channel] *
+ static_cast(gain_);
+ }
+ }
+ read_offset_ += kFrameSamples * static_cast(channels_);
+ if (read_offset_ == pending_.size()) {
+ pending_.clear();
+ read_offset_ = 0;
+ } else if (read_offset_ >= (1u << 20)) {
+ pending_.erase(pending_.begin(),
+ pending_.begin() + static_cast(read_offset_));
+ read_offset_ = 0;
+ }
+ return Status::success();
+}
+
+} // namespace joc::virtual_speaker
diff --git a/kernel/src/virtual_speaker/virtual_speaker.h b/kernel/src/virtual_speaker/virtual_speaker.h
new file mode 100644
index 0000000..81df060
--- /dev/null
+++ b/kernel/src/virtual_speaker/virtual_speaker.h
@@ -0,0 +1,57 @@
+// Virtual speaker input: ordinary multichannel PCM as fixed-direction JOC
+// objects. It owns only the layout mapping and the PCM-to-objects16 shape;
+// decoding, speaker rendering and binaural rendering stay in their own modules.
+#pragma once
+
+#include
+#include
+#include
+#include
+
+#include "foundation/status.h"
+#include "oamd/oamd_parser.h"
+
+namespace joc::virtual_speaker {
+
+struct ChannelSpec {
+ bool lfe;
+ double x;
+ double y;
+ double z;
+};
+
+struct Layout {
+ const char* name;
+ int channels;
+ ChannelSpec channel_specs[8];
+};
+
+const Layout* find_layout(const std::string& name);
+const Layout* detect_layout(int channels);
+
+class Input {
+public:
+ // gain_db is the module's master attenuation; it is applied to every
+ // mapped channel once, before the objects16 frame leaves the module.
+ Status configure(const std::string& layout_name, int channels, double gain_db = 0.0);
+ void reset();
+
+ const oamd::OamdUpdate& fixed_update() const { return fixed_update_; }
+ int channels() const { return channels_; }
+ const Layout* layout() const { return layout_; }
+
+ // Appends interleaved frames; take_frame() emits whole 1536-frame blocks.
+ Status append(const float* interleaved, std::size_t frames);
+ bool frame_ready() const;
+ Status take_frame(std::vector* objects16);
+
+private:
+ const Layout* layout_ = nullptr;
+ int channels_ = 0;
+ double gain_ = 1.0;
+ oamd::OamdUpdate fixed_update_{};
+ std::vector pending_;
+ std::size_t read_offset_ = 0;
+};
+
+} // namespace joc::virtual_speaker
diff --git a/src/input_joc.cpp b/src/input_joc.cpp
index 7398e23..1cac1cc 100644
--- a/src/input_joc.cpp
+++ b/src/input_joc.cpp
@@ -27,6 +27,7 @@
#include "eac3_scan.h"
#include "joc_decode.h"
#include "log.h"
+#include "pcm_source.h"
#include "prefs.h"
#include "settings.h"
@@ -36,6 +37,22 @@ constexpr std::size_t kSniffBytes = 256u * 1024u;
constexpr std::size_t kRunFrames = 4096u;
constexpr unsigned kSampleRate = 48000;
+// Formats the PCM takeover owns. Containers are deliberately absent: MP4/M4A
+// may carry a JOC track, so those still go through the E-AC-3/JOC probe first.
+bool is_pcm_takeover_extension(const char* extension) {
+ static const char* const kExtensions[] = {
+ // Decode to PCM; the kernel then maps 2.0/5.1/7.1. TrueHD/MLP is
+ // included too, but an Atmos TrueHD stream loses its objects when ffmpeg
+ // decodes it to PCM. DSD (DSF/DFF) is deliberately absent.
+ "wav", "flac", "mp3", "aac", "ogg", "opus", "wma", "aif", "aiff",
+ "ac3", "dts", "dtshd", "dtsma", "wv", "ape", "tta", "mpc", "au",
+ "snd", "caf", "thd", "mlp", "truehd"};
+ for (const char* candidate : kExtensions) {
+ if (extension != nullptr && stricmp_utf8(extension, candidate) == 0) return true;
+ }
+ return false;
+}
+
// Largest magnitude in a block, for the delivery check in decode_run().
template
double peak_of(const Sample* values, std::size_t count) {
@@ -253,11 +270,21 @@ public:
const char* extension = std::strrchr(m_native_path.get_ptr(), '.');
extension = (extension != nullptr) ? extension + 1 : "";
+ const joc_decode::Settings current = joc_settings::current();
+ joc_log::line("decoder: PCM takeover=%s, extension=%s",
+ current.take_over_pcm ? "on" : "off",
+ extension != nullptr ? extension : "");
+ if (current.take_over_pcm && is_pcm_takeover_extension(extension)) {
+ open_pcm(current, abort);
+ return;
+ }
+
if (joc_container::is_container_extension(extension)) {
- open_container(extension);
+ open_container(extension, abort);
} else {
open_bare(abort);
}
+ if (m_pcm_mode) return;
// Whatever else happens, the tags of this file are read by the component
// that owns its format; failing to find one is not fatal, the technical
@@ -271,10 +298,49 @@ public:
}
}
+ // A non-JOC E-AC-3 input (bare or in a container) can still be rendered:
+ // ffmpeg decodes it to 48 kHz float32 and the kernel's PCM_LAYOUT input maps
+ // the channels. JOC always wins; this only runs after the JOC probe said no.
+ void open_pcm_fallback(abort_callback& abort, unsigned audio_index,
+ const char* reason) {
+ joc_decode::Settings settings = joc_settings::current();
+ if (!settings.take_over_pcm) {
+ decline("decoder: yielding to the built-in decoder (%s)", reason);
+ throw exception_io_unsupported_format();
+ }
+ joc_log::line("decoder: no JOC (%s); routing audio track %u through ffmpeg",
+ reason, audio_index);
+ settings.audio_index = audio_index;
+ settings.take_over_pcm = true;
+ open_pcm(settings, abort);
+ }
+
// A bare stream is either read from the file or handed back. One thing has to
// happen first: a tag area in front of the syncframes is not part of the
// stream, and treating it as one would hand the file to the built-in decoder,
// which plays it without the Atmos objects.
+ // Ordinary PCM: ffmpeg is the only decoder. The kernel's PCM_LAYOUT stream
+ // maps 2.0/5.1/7.1 to fixed objects and renders binaural; this component does
+ // not hand the file to any other decoder.
+ void open_pcm(const joc_decode::Settings& settings, abort_callback& abort) {
+ std::string error;
+ if (!m_pcm.open(m_native_path.get_ptr(), settings, &error)) {
+ decline("decoder: PCM takeover could not decode the file (%s)", error.c_str());
+ throw exception_io_unsupported_format();
+ }
+ m_pcm_mode = true;
+ m_length = m_pcm.length_seconds();
+ m_channels = 2;
+ m_channel_config = channel_map_for(m_channels);
+ g_claimed.fetch_add(1, std::memory_order_relaxed);
+ joc_log::line("decoder: claiming PCM source via ffmpeg (%.3f s, binaural)",
+ m_length);
+ if (!open_forwarded(m_forward_reader, input_info_reader::class_guid,
+ m_path.c_str(), abort, &m_forward_name)) {
+ joc_log::line("decoder: no other component reads this file's tags");
+ }
+ }
+
void open_bare(abort_callback& abort) {
m_stream_start = leading_tag_bytes(m_file, abort);
if (m_stream_start != 0) {
@@ -293,9 +359,8 @@ public:
static_cast(scan.frames_with_joc));
if (scan.joc != joc_eac3::JocState::kYes) {
- // Hand the file to the next entry in the priority table.
- decline("decoder: yielding to the built-in decoder (%s)", scan.detail);
- throw exception_io_unsupported_format();
+ open_pcm_fallback(abort, 0, scan.detail);
+ return;
}
joc_log::line("decoder: claiming this file as E-AC-3 JOC (bare stream)");
g_claimed.fetch_add(1, std::memory_order_relaxed);
@@ -306,9 +371,15 @@ public:
// the container's own headers, so a library scan pays nothing for the MP4s that
// hold AAC), and does that track carry JOC (answered from a copied prefix of
// the track). Either "no" hands the file to the next decoder in the table.
- void open_container(const char* extension) {
+ void open_container(const char* extension, abort_callback& abort) {
m_container = joc_container::scan(m_native_path.get_ptr());
if (!m_container.eac3) {
+ const joc_decode::Settings settings = joc_settings::current();
+ if (settings.take_over_pcm) {
+ open_pcm_fallback(abort, m_container.audio_index,
+ m_container.detail.c_str());
+ return;
+ }
decline("decoder: yielding to the built-in decoder (%s)",
m_container.detail.c_str());
throw exception_io_unsupported_format();
@@ -319,14 +390,16 @@ public:
std::string detail;
if (!joc_decode::probe_container_joc(settings.ffmpeg_path, m_native_path.get_ptr(),
m_container.audio_index, &joc, &detail)) {
+ if (settings.take_over_pcm) {
+ open_pcm_fallback(abort, m_container.audio_index, detail.c_str());
+ return;
+ }
decline("decoder: cannot examine the E-AC-3 track (%s); yielding", detail.c_str());
throw exception_io_unsupported_format();
}
if (!joc) {
- decline("decoder: yielding to the built-in decoder (E-AC-3 track %u carries "
- "no JOC: %s)",
- m_container.audio_index, detail.c_str());
- throw exception_io_unsupported_format();
+ open_pcm_fallback(abort, m_container.audio_index, detail.c_str());
+ return;
}
joc_log::line("decoder: claiming this file as E-AC-3 JOC (%s, audio track %u, .%s)",
@@ -348,6 +421,32 @@ public:
throw exception_tagging_unsupported();
}
+ if (m_pcm_mode) {
+ const joc_decode::Settings settings = joc_settings::current();
+ info.info_set("codec", "PCM");
+ info.info_set("codec_long", "PCM via ffmpeg (JOC binaural takeover)");
+ info.info_set_int("samplerate", kSampleRate);
+ info.info_set_channels_ex(2, channel_map_for(2));
+ info.info_set_bitspersample(32u, true);
+ info.set_length(m_length);
+ info.info_set("joc_render", "binaural (PCM takeover)");
+ info.info_set("joc_hrtf", settings.hrtf_file.empty()
+ ? "(未设置)"
+ : file_name_of(settings.hrtf_file).c_str());
+ if (m_forward_reader.is_valid()) {
+ try {
+ file_info_impl tags;
+ m_forward_reader->get_info(0, tags, abort);
+ info.copy_meta(tags);
+ } catch (const pfc::exception& error) {
+ joc_log::line("decoder: reading this file's own tags failed: %s",
+ error.what());
+ }
+ }
+ joc_log::line("decoder: get_info PCM takeover duration=%.3f s", m_length);
+ return;
+ }
+
const joc_decode::FileProbe probe =
joc_decode::probe_file(m_native_path.get_ptr(), 0, m_stream_start);
const joc_decode::Settings settings = joc_settings::current();
@@ -462,6 +561,19 @@ public:
void decode_initialize(unsigned flags, abort_callback& abort) {
(void)abort;
+ if (m_pcm_mode) {
+ m_settings = joc_settings::current();
+ m_channels = 2;
+ m_channel_config = channel_map_for(m_channels);
+ m_buffer.resize(kRunFrames * m_channels);
+ m_frames_delivered = 0;
+ m_reported = false;
+ m_delivery_mismatches = 0;
+ m_clipped_samples = 0;
+ m_clip_reported = false;
+ joc_log::line("decoder: PCM takeover ready (binaural, ffmpeg only)");
+ return;
+ }
m_settings = joc_settings::current();
m_settings.input_kind = m_input_kind;
m_settings.audio_index = m_audio_index;
@@ -521,6 +633,26 @@ public:
bool decode_run(audio_chunk& chunk, abort_callback& abort) {
(void)abort;
std::string error;
+ if (m_pcm_mode) {
+ const std::size_t pcm_frames = m_pcm.read(m_buffer.data(), kRunFrames, &error);
+ if (pcm_frames == 0) {
+ if (!error.empty()) {
+ joc_log::line("decoder: PCM read failed: %s", error.c_str());
+ throw exception_io_data(error.c_str());
+ }
+ return false;
+ }
+ chunk.set_data_32(m_buffer.data(), pcm_frames,
+ audio_chunk::makeSpec(kSampleRate, m_channels,
+ m_channel_config));
+ m_frames_delivered += pcm_frames;
+ if (!m_reported) {
+ m_reported = true;
+ joc_log::line("decoder: first %llu PCM frames delivered (binaural)",
+ static_cast(pcm_frames));
+ }
+ return true;
+ }
const std::size_t frames = m_engine.read(m_buffer.data(), kRunFrames, &error);
if (frames == 0) {
if (!error.empty()) {
@@ -606,6 +738,19 @@ public:
void decode_seek(double seconds, abort_callback& abort) {
// Walking the syncframe index of a long file is the only part of a seek
// that can take a while, and it polls this.
+ if (m_pcm_mode) {
+ std::string error;
+ if (!m_pcm.seek(seconds, &error)) {
+ abort.check();
+ joc_log::line("decoder: PCM seek to %.6f s failed: %s", seconds, error.c_str());
+ throw exception_io_data(error.c_str());
+ }
+ m_frames_delivered = 0;
+ m_reported = false;
+ m_delivery_mismatches = 0;
+ joc_log::line("decoder: PCM seek to %.6f s", seconds);
+ return;
+ }
m_engine.set_abort_check([&abort] { return !abort.is_aborting(); });
std::string error;
const bool ok = m_engine.seek(seconds, m_length, &error);
@@ -671,6 +816,9 @@ public:
// track is commonly wrapped in MP4 or Matroska; the container walk in
// open() is what keeps the other files cheap to decline.
if (joc_container::is_container_extension(extension)) return true;
+ if (joc_settings::read().take_over_pcm && is_pcm_takeover_extension(extension)) {
+ return true;
+ }
return (extension != nullptr) &&
((stricmp_utf8(extension, "eac3") == 0) || (stricmp_utf8(extension, "ec3") == 0));
}
@@ -700,6 +848,8 @@ private:
pfc::string8 m_native_path;
joc_decode::Settings m_settings;
joc_decode::Engine m_engine;
+ joc_pcm::Source m_pcm;
+ bool m_pcm_mode = false;
// Set by open(): a bare stream is read directly, an E-AC-3 track inside a
// container is extracted by ffmpeg before it reaches the renderer.
joc_decode::InputKind m_input_kind = joc_decode::InputKind::kBare;
diff --git a/src/joc_decode.h b/src/joc_decode.h
index 69d11db..5a91ff8 100644
--- a/src/joc_decode.h
+++ b/src/joc_decode.h
@@ -95,6 +95,8 @@ struct Settings {
// carry both the side and the rear pair are affected, so a 5.1.x layout whose
// rear slots hold top channels is left alone.
bool swap_side_rear = false;
+ // ffmpeg is the sole decoder for this input; the kernel maps the channels.
+ bool take_over_pcm = false;
};
// ---- speaker delivery ------------------------------------------------------
diff --git a/src/lang.cpp b/src/lang.cpp
index ddac4ac..e0c057e 100644
--- a/src/lang.cpp
+++ b/src/lang.cpp
@@ -30,6 +30,7 @@ constexpr Entry kTexts[] = {
{Text::kComboModeFar, "far(远场)", "far"},
{Text::kLabelTail, "房间尾音(秒)", "Room tail (s)"},
{Text::kCheckInt24, "强制 24bit 输出(int24)", "Force 24-bit output (int24)"},
+ {Text::kCheckPcm, "接管普通 PCM 源(ffmpeg 解码)", "Take over ordinary PCM (ffmpeg decode)"},
{Text::kInt24Hint, "切换后需刷新文件信息", "reload file info after switching"},
{Text::kSpeakerStatus, "扬声器布局 %s", "Speaker layout %s"},
{Text::kGroupHrtf, "HRTF", "HRTF"},
@@ -38,8 +39,8 @@ constexpr Entry kTexts[] = {
{Text::kLabelHrtfFile, "HRTF 文件", "HRTF file"},
{Text::kButtonBrowse, "浏览…", "Browse…"},
{Text::kHrtfHint,
- "留空时读组件目录下 HRTF\\binaural.sofa 或 HRTF\\binaural.personalized_headphone。",
- "Empty means HRTF\\binaural.sofa or binaural.personalized_headphone next to "
+ "留空时读组件目录下 HRTF\\binaural.personalized_headphone(Rosella 模型)。",
+ "Empty means HRTF\\binaural.personalized_headphone (Rosella model) next to "
"the component."},
{Text::kHrtfSourceSofa, "SOFA", "SOFA"},
{Text::kHrtfSourceRosella, "Rosella 个性化模型", "Rosella personalized model"},
diff --git a/src/lang.h b/src/lang.h
index 99b08ed..8829a2f 100644
--- a/src/lang.h
+++ b/src/lang.h
@@ -31,6 +31,7 @@ enum class Text {
kComboModeFar,
kLabelTail,
kCheckInt24,
+ kCheckPcm,
kInt24Hint,
kSpeakerStatus,
kGroupHrtf,
diff --git a/src/pcm_source.cpp b/src/pcm_source.cpp
new file mode 100644
index 0000000..e9c3ecf
--- /dev/null
+++ b/src/pcm_source.cpp
@@ -0,0 +1,358 @@
+#include "pcm_source.h"
+
+#include
+
+#include
+#include
+#include
+
+#include "joc_stream.h"
+#include "log.h"
+#include "win_path.h"
+
+namespace joc_pcm {
+
+namespace {
+
+constexpr std::size_t kFrameSamples = 1536;
+constexpr std::uint32_t kRate = 48000;
+
+std::uint16_t u16(const unsigned char* data) {
+ return static_cast(data[0] | (data[1] << 8));
+}
+
+std::uint32_t u32(const unsigned char* data) {
+ return static_cast(data[0]) |
+ (static_cast(data[1]) << 8) |
+ (static_cast(data[2]) << 16) |
+ (static_cast(data[3]) << 24);
+}
+
+bool read_exact(std::FILE* file, void* data, std::size_t bytes) {
+ return std::fread(data, 1, bytes, file) == bytes;
+}
+
+std::uint64_t file_size(std::FILE* file) {
+ const long long current = _ftelli64(file);
+ _fseeki64(file, 0, SEEK_END);
+ const long long end = _ftelli64(file);
+ _fseeki64(file, current, SEEK_SET);
+ return end < 0 ? 0 : static_cast(end);
+}
+
+} // namespace
+
+Source::~Source() { close(); }
+
+bool Source::decode_temp(const std::string& input_path, const std::string& ffmpeg,
+ unsigned audio_index, std::string* error) {
+ wchar_t directory[MAX_PATH] = {};
+ if (GetTempPathW(MAX_PATH, directory) == 0) {
+ if (error != nullptr) *error = "cannot locate the temp directory";
+ return false;
+ }
+ std::wstring temp = directory;
+ if (!temp.empty() && temp.back() != L'\\') temp.push_back(L'\\');
+ temp += L"foo_input_joc_pcm_";
+ temp += std::to_wstring(GetCurrentProcessId());
+ temp += L".wav";
+ temp_path_ = joc_path::to_utf8(temp.c_str());
+ DeleteFileW(joc_path::to_wide_extended(temp_path_).c_str());
+
+ std::wstring command = L"\"" + joc_path::to_wide_extended(ffmpeg) + L"\"";
+ command += L" -hide_banner -loglevel error -nostdin -y -i \"";
+ command += joc_path::to_wide_extended(input_path);
+ command += L"\" -map 0:a:";
+ command += std::to_wstring(audio_index);
+ command += L" -af \"aresample=resampler=soxr:precision=28:osr=48000\" "
+ L"-c:a pcm_f32le \"";
+ command += joc_path::to_wide_extended(temp_path_);
+ command += L"\"";
+ SECURITY_ATTRIBUTES inheritable{};
+ inheritable.nLength = sizeof(inheritable);
+ inheritable.bInheritHandle = TRUE;
+ HANDLE null_input = CreateFileW(L"NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ &inheritable, OPEN_EXISTING, 0, nullptr);
+ const std::wstring log_path = joc_path::to_wide_extended(temp_path_ + ".log");
+ HANDLE log = CreateFileW(log_path.c_str(), GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, &inheritable,
+ CREATE_ALWAYS, 0, nullptr);
+ STARTUPINFOW startup{};
+ startup.cb = sizeof(startup);
+ startup.dwFlags = STARTF_USESTDHANDLES;
+ startup.hStdInput = null_input;
+ startup.hStdOutput = log;
+ startup.hStdError = (log != INVALID_HANDLE_VALUE) ? log : null_input;
+ PROCESS_INFORMATION process{};
+ std::vector mutable_command(command.begin(), command.end());
+ mutable_command.push_back(L'\0');
+ const BOOL created = CreateProcessW(nullptr, mutable_command.data(), nullptr, nullptr,
+ TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &startup,
+ &process);
+ if (null_input != INVALID_HANDLE_VALUE) CloseHandle(null_input);
+ if (log != INVALID_HANDLE_VALUE) CloseHandle(log);
+ if (created == FALSE) {
+ if (error != nullptr) *error = "cannot start ffmpeg";
+ return false;
+ }
+ WaitForSingleObject(process.hProcess, INFINITE);
+ DWORD exit_code = 1;
+ GetExitCodeProcess(process.hProcess, &exit_code);
+ CloseHandle(process.hThread);
+ CloseHandle(process.hProcess);
+ if (exit_code != 0) {
+ WIN32_FILE_ATTRIBUTE_DATA attributes{};
+ const bool wrote_output =
+ GetFileAttributesExW(temp.c_str(), GetFileExInfoStandard, &attributes) != FALSE &&
+ (static_cast(attributes.nFileSizeHigh) << 32 |
+ attributes.nFileSizeLow) > 44u;
+ if (!wrote_output) {
+ std::FILE* log_file = _wfopen(log_path.c_str(), L"rb");
+ if (log_file != nullptr) {
+ char line[512];
+ while (std::fgets(line, sizeof(line), log_file) != nullptr) {
+ std::printf("ffmpeg: %s", line);
+ }
+ std::fclose(log_file);
+ }
+ if (error != nullptr) {
+ *error = "ffmpeg could not decode the PCM source (exit " +
+ std::to_string(exit_code) + ")";
+ }
+ return false;
+ }
+ joc_log::line("pcm: ffmpeg exited with %lu but produced the WAV", exit_code);
+ }
+ return true;
+}
+
+bool Source::parse_wav(const std::string& path, Header* header, std::string* error) {
+ std::FILE* file = _wfopen(joc_path::to_wide_extended(path).c_str(), L"rb");
+ if (file == nullptr) {
+ if (error != nullptr) *error = "cannot open the decoded WAV";
+ return false;
+ }
+ unsigned char riff[12] = {};
+ if (!read_exact(file, riff, sizeof(riff)) || std::memcmp(riff, "RIFF", 4) != 0 ||
+ std::memcmp(riff + 8, "WAVE", 4) != 0) {
+ std::fclose(file);
+ if (error != nullptr) *error = "ffmpeg did not produce a RIFF/WAVE file";
+ return false;
+ }
+ Header parsed{};
+ bool have_format = false;
+ for (;;) {
+ unsigned char chunk[8] = {};
+ if (!read_exact(file, chunk, sizeof(chunk))) break;
+ const std::uint32_t size = u32(chunk + 4);
+ const std::uint64_t payload = static_cast(_ftelli64(file));
+ if (std::memcmp(chunk, "fmt ", 4) == 0 && size >= 16u) {
+ unsigned char fmt[40] = {};
+ const std::size_t fmt_bytes = std::min(size, sizeof(fmt));
+ if (!read_exact(file, fmt, fmt_bytes)) break;
+ std::uint16_t format = u16(fmt);
+ parsed.channels = u16(fmt + 2);
+ parsed.sample_rate = u32(fmt + 4);
+ parsed.bits = u16(fmt + 14);
+ if (format == 0xFFFEu && fmt_bytes >= 40u) {
+ format = u16(fmt + 24); // WAVEFORMATEXTENSIBLE sub-format
+ }
+ if (format != 3u || parsed.bits != 32u) {
+ std::fclose(file);
+ if (error != nullptr) *error = "decoded WAV is not 32-bit float";
+ return false;
+ }
+ have_format = true;
+ } else if (std::memcmp(chunk, "data", 4) == 0) {
+ parsed.data_offset = payload;
+ parsed.data_bytes = size;
+ }
+ _fseeki64(file, static_cast(payload + size + (size & 1u)), SEEK_SET);
+ }
+ const std::uint64_t file_bytes = file_size(file);
+ std::fclose(file);
+ if (!have_format || parsed.channels < 2 || parsed.channels > 8 ||
+ parsed.data_offset == 0) {
+ if (error != nullptr) *error = "decoded WAV has no usable audio data";
+ return false;
+ }
+ if (parsed.data_bytes == 0u || parsed.data_bytes == 0xFFFFFFFFu ||
+ parsed.data_offset + parsed.data_bytes > file_bytes) {
+ parsed.data_bytes = file_bytes - parsed.data_offset;
+ }
+ *header = parsed;
+ return true;
+}
+
+bool Source::open(const std::string& input_path, const joc_decode::Settings& settings,
+ std::string* error) {
+ close();
+ if (!decode_temp(input_path, settings.ffmpeg_path, settings.audio_index, error)) return false;
+ Header header{};
+ if (!parse_wav(temp_path_, &header, error)) {
+ close();
+ return false;
+ }
+ if (header.sample_rate != kRate) {
+ if (error != nullptr) *error = "ffmpeg did not produce 48 kHz audio";
+ close();
+ return false;
+ }
+ header_ = header;
+ channels_ = header.channels;
+ file_ = _wfopen(joc_path::to_wide_extended(temp_path_).c_str(), L"rb");
+ if (file_ == nullptr) {
+ if (error != nullptr) *error = "cannot reopen the decoded WAV";
+ close();
+ return false;
+ }
+ total_frames_ = header.data_bytes / (static_cast(channels_) * 4u);
+ length_seconds_ = static_cast(total_frames_) / kRate;
+ feed_.assign(kFrameSamples * static_cast(channels_), 0.0f);
+
+ joc_stream_config config{};
+ config.struct_size = sizeof(config);
+ config.struct_version = 1;
+ config.input = JOC_STREAM_IN_PCM_LAYOUT;
+ config.output = JOC_STREAM_OUT_BINAURAL;
+ config.pcm_layout = "auto";
+ const std::string hrtf = joc_decode::resolve_hrtf_file(settings);
+ config.personalized_headphone_path = hrtf.c_str();
+ config.binaural_mode = settings.binaural_mode;
+ config.binaural_tail_seconds = settings.tail_seconds;
+ config.object_delay_samples = settings.object_delay_samples;
+ config.gain_db = settings.gain_db;
+ config.rosella_filterbank = 1u;
+ const joc_error created = joc_stream_create(&config, &stream_);
+ if (created != JOC_OK || stream_ == nullptr) {
+ if (error != nullptr) *error = "kernel PCM layout stream creation failed";
+ close();
+ return false;
+ }
+ _fseeki64(file_, static_cast(header_.data_offset), SEEK_SET);
+ frames_read_ = 0;
+ eof_ = false;
+ flushed_ = false;
+ return true;
+}
+
+bool Source::push_block(std::string* error) {
+ if (file_ == nullptr || stream_ == nullptr || eof_) return false;
+ std::size_t frames = kFrameSamples;
+ const std::uint64_t remaining = total_frames_ - frames_read_;
+ if (remaining < frames) frames = static_cast(remaining);
+ std::fill(feed_.begin(), feed_.end(), 0.0f);
+ if (frames != 0u &&
+ std::fread(feed_.data(), sizeof(float), frames * static_cast(channels_),
+ file_) != frames * static_cast(channels_)) {
+ if (error != nullptr) *error = "cannot read the decoded WAV";
+ return false;
+ }
+ if (frames != 0u) {
+ joc_stream_buffer input{};
+ input.struct_size = sizeof(input);
+ input.struct_version = 1;
+ input.kind = JOC_STREAM_IN_PCM_LAYOUT;
+ input.channels = static_cast(channels_);
+ input.sample_rate = kRate;
+ input.sample_count = static_cast(frames);
+ input.pcm = feed_.data();
+ std::uint32_t consumed = 0;
+ std::uint32_t consumed_bytes = 0;
+ if (joc_stream_push(stream_, &input, &consumed, &consumed_bytes) != JOC_OK) {
+ if (error != nullptr) *error = "kernel PCM layout push failed";
+ return false;
+ }
+ }
+ frames_read_ += frames;
+ if (frames_read_ >= total_frames_ || frames == 0u) eof_ = true;
+ return true;
+}
+
+bool Source::pull(std::size_t frames, float* destination, std::size_t* produced,
+ std::string* error) {
+ *produced = 0;
+ if (stream_ == nullptr || frames == 0u) return true;
+ joc_stream_buffer output{};
+ output.struct_size = sizeof(output);
+ output.struct_version = 1;
+ output.out_pcm = destination;
+ output.sample_count = static_cast(frames);
+ std::uint32_t rendered = 0;
+ if (joc_stream_pull(stream_, &output, &rendered) != JOC_OK) {
+ if (error != nullptr) *error = "kernel binaural pull failed";
+ return false;
+ }
+ *produced = rendered;
+ return true;
+}
+
+std::size_t Source::read(float* destination, std::size_t frames, std::string* error) {
+ if (stream_ == nullptr || destination == nullptr || frames == 0u) return 0;
+ std::size_t written = 0;
+ while (written < frames) {
+ std::size_t produced = 0;
+ if (!pull(frames - written, destination + written * 2u, &produced, error)) {
+ return written;
+ }
+ written += produced;
+ if (written == frames) break;
+ if (!eof_) {
+ if (!push_block(error)) return written;
+ continue;
+ }
+ if (!flushed_) {
+ if (joc_stream_flush(stream_) != JOC_OK) {
+ if (error != nullptr) *error = "kernel flush failed";
+ return written;
+ }
+ flushed_ = true;
+ continue;
+ }
+ break;
+ }
+ return written;
+}
+
+bool Source::seek(double seconds, std::string* error) {
+ if (stream_ == nullptr || file_ == nullptr) return false;
+ if (seconds < 0.0) seconds = 0.0;
+ std::uint64_t frame = static_cast(seconds * kRate + 0.5);
+ frame = std::min(frame, total_frames_);
+ if (joc_stream_reset(stream_) != JOC_OK) {
+ if (error != nullptr) *error = "kernel reset failed";
+ return false;
+ }
+ _fseeki64(file_, static_cast(header_.data_offset +
+ frame * static_cast(channels_) * 4u),
+ SEEK_SET);
+ frames_read_ = frame;
+ eof_ = frames_read_ >= total_frames_;
+ flushed_ = false;
+ return true;
+}
+
+void Source::close() {
+ if (stream_ != nullptr) {
+ joc_stream_destroy(stream_);
+ stream_ = nullptr;
+ }
+ if (file_ != nullptr) {
+ std::fclose(file_);
+ file_ = nullptr;
+ }
+ if (!temp_path_.empty()) {
+ DeleteFileW(joc_path::to_wide_extended(temp_path_).c_str());
+ DeleteFileW(joc_path::to_wide_extended(temp_path_ + ".log").c_str());
+ temp_path_.clear();
+ }
+ feed_.clear();
+ frames_read_ = 0;
+ total_frames_ = 0;
+ length_seconds_ = 0.0;
+ eof_ = false;
+ flushed_ = false;
+ channels_ = 0;
+}
+
+} // namespace joc_pcm
diff --git a/src/pcm_source.h b/src/pcm_source.h
new file mode 100644
index 0000000..fe04b6d
--- /dev/null
+++ b/src/pcm_source.h
@@ -0,0 +1,68 @@
+// Whole-file PCM takeover for foo_input_joc.
+//
+// ffmpeg is the only decoder here: it produces a 48 kHz float32 WAV, and the
+// kernel's PCM_LAYOUT stream maps 2.0/5.1/7.1 to fixed objects and renders
+// binaural. The temp WAV exists so the component can seek without a second
+// decoder; it is deleted when the input closes.
+#pragma once
+
+#include
+#include
+#include
+#include
+
+#include "joc_decode.h"
+
+struct joc_stream;
+
+namespace joc_pcm {
+
+class Source {
+public:
+ Source() = default;
+ ~Source();
+ Source(const Source&) = delete;
+ Source& operator=(const Source&) = delete;
+
+ bool open(const std::string& input_path, const joc_decode::Settings& settings,
+ std::string* error);
+ // Fills interleaved stereo float32 frames; returns frames written.
+ std::size_t read(float* destination, std::size_t frames, std::string* error);
+ bool seek(double seconds, std::string* error);
+ void close();
+
+ double length_seconds() const { return length_seconds_; }
+ unsigned channels() const { return 2; }
+ unsigned sample_rate() const { return 48000; }
+ bool is_open() const { return stream_ != nullptr; }
+
+private:
+ struct Header {
+ std::uint32_t sample_rate = 0;
+ std::uint16_t channels = 0;
+ std::uint16_t bits = 0;
+ std::uint64_t data_offset = 0;
+ std::uint64_t data_bytes = 0;
+ };
+
+ bool decode_temp(const std::string& input_path, const std::string& ffmpeg,
+ unsigned audio_index, std::string* error);
+ bool parse_wav(const std::string& path, Header* header, std::string* error);
+ bool push_block(std::string* error);
+ bool pull(std::size_t frames, float* destination, std::size_t* produced,
+ std::string* error);
+
+ std::FILE* file_ = nullptr;
+ joc_stream* stream_ = nullptr;
+ Header header_{};
+ std::string temp_path_;
+ std::vector feed_;
+ std::uint64_t frames_read_ = 0;
+ std::uint64_t total_frames_ = 0;
+ double length_seconds_ = 0.0;
+ bool eof_ = false;
+ bool flushed_ = false;
+ int channels_ = 0;
+};
+
+} // namespace joc_pcm
diff --git a/src/prefs.cpp b/src/prefs.cpp
index 280beb6..81c68dc 100644
--- a/src/prefs.cpp
+++ b/src/prefs.cpp
@@ -285,7 +285,7 @@ private:
return TRUE;
}
if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24 ||
- id == IDC_CHECK_SWAP_SIDE_REAR) {
+ id == IDC_CHECK_SWAP_SIDE_REAR || id == IDC_CHECK_PCM) {
update_status();
notify();
return TRUE;
@@ -369,7 +369,7 @@ private:
label(IDC_LABEL_TAIL, joc_lang::Text::kLabelTail);
label(IDC_CHECK_INT24, joc_lang::Text::kCheckInt24);
label(IDC_CHECK_SWAP_SIDE_REAR, joc_lang::Text::kCheckSwapSideRear);
- label(IDC_LABEL_INT24_HINT, joc_lang::Text::kInt24Hint);
+ label(IDC_CHECK_PCM, joc_lang::Text::kCheckPcm);
label(IDC_GROUP_HRTF, joc_lang::Text::kGroupHrtf);
label(IDC_RADIO_HRTF_ROSELLA, joc_lang::Text::kRadioRosella);
label(IDC_LABEL_HRTF, joc_lang::Text::kLabelHrtfFile);
@@ -408,7 +408,9 @@ private:
values.gain_enabled ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_INT24), BM_SETCHECK,
values.force_int24 ? BST_CHECKED : BST_UNCHECKED, 0);
- SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_SWAP_SIDE_REAR), BM_SETCHECK,
+ SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_PCM), BM_SETCHECK,
+ values.take_over_pcm ? BST_CHECKED : BST_UNCHECKED, 0);
+ SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_SWAP_SIDE_REAR), BM_SETCHECK,
values.swap_side_rear ? BST_CHECKED : BST_UNCHECKED, 0);
// The item lists are language-dependent, so they are built (in the stored
@@ -463,6 +465,8 @@ private:
IsDlgButtonChecked(m_hwnd, IDC_CHECK_INT24) == BST_CHECKED;
values.swap_side_rear =
IsDlgButtonChecked(m_hwnd, IDC_CHECK_SWAP_SIDE_REAR) == BST_CHECKED;
+ values.take_over_pcm =
+ IsDlgButtonChecked(m_hwnd, IDC_CHECK_PCM) == BST_CHECKED;
values.language = selected_language();
// Advanced values are not on this page; keep whatever is stored.
const joc_settings::Values stored = joc_settings::read();
@@ -526,6 +530,7 @@ private:
if (now.gain_enabled != m_initial.gain_enabled) return true;
if (now.force_int24 != m_initial.force_int24) return true;
if (now.swap_side_rear != m_initial.swap_side_rear) return true;
+ if (now.take_over_pcm != m_initial.take_over_pcm) return true;
if (now.gain_db != m_initial.gain_db) return true;
if (now.tail_seconds != m_initial.tail_seconds) return true;
if (now.ffmpeg_path != m_initial.ffmpeg_path) return true;
diff --git a/src/prefs.rc b/src/prefs.rc
index 6634b06..e57b0a7 100644
--- a/src/prefs.rc
+++ b/src/prefs.rc
@@ -36,7 +36,8 @@ BEGIN
LTEXT "", IDC_LABEL_OUTPUT_STATUS, 16, 53, 300, 10
CONTROL "强制 24bit 输出(int24)", IDC_CHECK_INT24, "Button",
BS_AUTOCHECKBOX | WS_TABSTOP, 16, 65, 156, 10
- LTEXT "切换后需刷新文件信息", IDC_LABEL_INT24_HINT, 176, 65, 136, 10
+ CONTROL "接管普通 PCM 源(ffmpeg 解码)", IDC_CHECK_PCM, "Button",
+ BS_AUTOCHECKBOX | WS_TABSTOP, 176, 65, 140, 10
GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 79, 316, 92
LTEXT "Rosella 个性化模型", IDC_RADIO_HRTF_ROSELLA, 16, 91, 146, 10
diff --git a/src/resource.h b/src/resource.h
index 9ea31d2..cd81796 100644
--- a/src/resource.h
+++ b/src/resource.h
@@ -14,6 +14,7 @@
#define IDC_LABEL_OUTPUT_STATUS 2019
// Delivery format, in the same group: what the decoder hands to foobar2000.
#define IDC_CHECK_INT24 2050
+#define IDC_CHECK_PCM 2052
#define IDC_LABEL_INT24_HINT 2051
#define IDC_GROUP_HRTF 2020
diff --git a/src/settings.cpp b/src/settings.cpp
index f547e75..9334710 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -30,6 +30,7 @@ constexpr GUID kGuidGain = {0x6c04d8b2, 0xe75f, 0x4a39, {0x8d, 0x26, 0x51, 0xb3,
constexpr GUID kGuidTail = {0x38f1b6a7, 0x0d24, 0x4e88, {0xb7, 0x95, 0x2c, 0x61, 0x4f, 0xd2, 0x08, 0xa3}};
constexpr GUID kGuidForceInt24 = {0xd2c67f41, 0x8b53, 0x4a2e, {0x97, 0x61, 0x0f, 0xc8, 0x35, 0xba, 0x64, 0x12}};
constexpr GUID kGuidSwapSideRear = {0x2b7f6d18, 0x59c4, 0x4a83, {0x8e, 0x15, 0x73, 0xac, 0x06, 0xd9, 0x4b, 0x27}};
+constexpr GUID kGuidTakeOverPcm = {0x9f2b6c41, 0x7d38, 0x4a05, {0x86, 0x1e, 0x52, 0xb9, 0x0c, 0x74, 0xe3, 0x28}};
constexpr GUID kGuidObjectDelay = {0x5b2e9c18, 0xa640, 0x4d7b, {0x92, 0x0e, 0x37, 0xc5, 0x81, 0x6b, 0x2a, 0x54}};
constexpr GUID kGuidThreads = {0xcb7a4015, 0x3f98, 0x4c62, {0xa0, 0x8d, 0x1b, 0x74, 0x29, 0xe6, 0x53, 0x0f}};
constexpr GUID kGuidFfmpeg = {0x0e9d37c2, 0x5a18, 0x4b40, {0xb6, 0x3f, 0x88, 0x2d, 0x9a, 0x70, 0x1c, 0x65}};
@@ -48,6 +49,7 @@ cfg_uint g_tail_ms(kGuidTail, 5000);
cfg_bool g_force_int24(kGuidForceInt24, false);
// Swap the side and rear surround pairs in the delivered PCM.
cfg_bool g_swap_side_rear(kGuidSwapSideRear, false);
+cfg_bool g_take_over_pcm(kGuidTakeOverPcm, false);
cfg_uint g_object_delay(kGuidObjectDelay, 1473);
cfg_uint g_threads(kGuidThreads, 0);
cfg_string g_ffmpeg(kGuidFfmpeg, "ffmpeg");
@@ -128,6 +130,7 @@ Values read() {
values.tail_seconds = static_cast(static_cast(g_tail_ms)) / 1000.0;
values.force_int24 = static_cast(g_force_int24);
values.swap_side_rear = static_cast(g_swap_side_rear);
+ values.take_over_pcm = static_cast(g_take_over_pcm);
values.object_delay_samples = static_cast(g_object_delay);
values.native_threads = static_cast(g_threads);
values.ffmpeg_path = g_ffmpeg.get_ptr();
@@ -158,6 +161,7 @@ void write(const Values& values) {
g_tail_ms = static_cast(values.tail_seconds * 1000.0 + 0.5);
g_force_int24 = values.force_int24;
g_swap_side_rear = values.swap_side_rear;
+ g_take_over_pcm = values.take_over_pcm;
g_object_delay = values.object_delay_samples;
g_threads = values.native_threads;
g_ffmpeg = values.ffmpeg_path.c_str();
@@ -180,6 +184,7 @@ joc_decode::Settings current() {
(void)env_double("JOC_TAIL_SECONDS", &values.tail_seconds);
if (env_string("JOC_FORCE_INT24", &text)) values.force_int24 = (text != "0");
if (env_string("JOC_SWAP_SIDE_REAR", &text)) values.swap_side_rear = (text != "0");
+ if (env_string("JOC_TAKE_OVER_PCM", &text)) values.take_over_pcm = (text != "0");
(void)env_uint("JOC_OBJECT_DELAY", &values.object_delay_samples);
(void)env_uint("JOC_THREADS", &values.native_threads);
@@ -196,6 +201,7 @@ joc_decode::Settings current() {
settings.tail_seconds = values.tail_seconds;
settings.force_int24 = values.force_int24;
settings.swap_side_rear = values.swap_side_rear;
+ settings.take_over_pcm = values.take_over_pcm;
settings.object_delay_samples = values.object_delay_samples;
settings.native_threads = values.native_threads;
settings.ffmpeg_path = values.ffmpeg_path.empty() ? "ffmpeg" : values.ffmpeg_path;
diff --git a/src/settings.h b/src/settings.h
index 087dcb9..c9ca2fa 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -36,6 +36,8 @@ struct Values {
// Swap Ls with Lrs and Rs with Rrs in the delivered speaker PCM; a layout
// without both pairs is delivered unchanged.
bool swap_side_rear = false;
+ // Take over ordinary PCM files and render them binaurally through ffmpeg.
+ bool take_over_pcm = false;
unsigned object_delay_samples = 1473;
unsigned native_threads = 0;
std::string ffmpeg_path = "ffmpeg";