diff --git a/README.md b/README.md index af72590..c4e4d05 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ mpv --script="C:\\path\\to\\joc_mpv.dll" --script-opts=joc-output=speaker,joc-la | 键 | 取值 | 默认 | 说明 | |---|---|---|---| | `joc-enabled` | yes / no | yes | 关掉后插件完全不介入 | +| `joc-pcm` | yes / no | no | 接管普通 PCM 源;ffmpeg 是唯一解码器,解码为 48k float32 后交给内核 PCM layout 做 binaural | | `joc-output` | binaural / speaker | binaural | 输出形式 | | `joc-layout` | 2.0 3.1 4.0 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6 | 7.1 | 扬声器布局;仅 `speaker` 使用 | | `joc-rosella-filterbank` | 1 / 2(或 public / captured) | 1 | Rosella 滤波器组:1=13 taps/6 slots 的公开版,2=11 taps/3 slots 的捕获版(与参考渲染器一致) | diff --git a/config/cplugin-mpv.conf.example b/config/cplugin-mpv.conf.example index 7a7df9e..0c87732 100644 --- a/config/cplugin-mpv.conf.example +++ b/config/cplugin-mpv.conf.example @@ -21,6 +21,11 @@ script-opts=joc-output=binaural,joc-mode=mid,joc-gain-db=0 # Rosella 模型用捕获版滤波器组(与参考渲染器一致;默认 1 = 公开版): # script-opts-append=joc-rosella-filterbank=2 # +# 接管普通 PCM 源(wav/flac/mp3/aac/ogg/opus/wma/aif/aiff): +# 此时 ffmpeg 是唯一解码器,插件把 48k float32 交给内核的 PCM layout 输入, +# 再走 binaural 渲染;不再经过 mpv 自己的音频解码器。 +# script-opts-append=joc-pcm=yes +# # 交付 24bit 定点: # script-opts-append=joc-int24=yes # diff --git a/src/cplugin_main.cpp b/src/cplugin_main.cpp index 4019f5c..38a7891 100644 --- a/src/cplugin_main.cpp +++ b/src/cplugin_main.cpp @@ -260,7 +260,28 @@ struct Plugin { if (ch >= 'A' && ch <= 'Z') ch = static_cast(ch - 'A' + 'a'); } static const char* const kExtensions[] = {"eac3", "ec3", "ac3", "mp4", "m4a", "m4b", - "mov", "mkv", "mka", "webm"}; + "mov", "mkv", "mka", "webm", "wav", "flac", + "mp3", "aac", "ogg", "opus", "wma", "aif", + "aiff", "thd", "mlp", "truehd", "dts", "dtshd", + "dtsma", "wv", "ape", "tta", "mpc", "au", "snd", + "caf"}; + for (const char* known : kExtensions) { + if (extension == known) return true; + } + return false; + } + + static bool pcm_extension(const std::string& path) { + const std::size_t dot = path.find_last_of('.'); + if (dot == std::string::npos) return false; + std::string extension = path.substr(dot + 1); + for (char& ch : extension) { + if (ch >= 'A' && ch <= 'Z') ch = static_cast(ch - 'A' + 'a'); + } + static const char* const kExtensions[] = {"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* known : kExtensions) { if (extension == known) return true; } @@ -280,6 +301,14 @@ struct Plugin { } const joc_container::Result container = joc_container::scan(path); if (container.kind == joc_container::Kind::kNone || !container.eac3) { + if (config.pcm && (pcm_extension(path) || container.kind != joc_container::Kind::kNone)) { + settings->pcm_takeover = true; + settings->audio_index = container.audio_index; + *duration = container.duration_seconds; + joc_log::line("probe: %s is an ordinary PCM/TrueHD source (ffmpeg takeover)", + path.c_str()); + return true; + } joc_log::line("probe: %s is not an E-AC-3 JOC input (%s)", path.c_str(), container.detail.c_str()); return false; @@ -289,6 +318,14 @@ struct Plugin { if (!joc_decode::probe_container_joc(config.ffmpeg, path, container.audio_index, &verdict, &detail) || !verdict) { + if (config.pcm) { + settings->pcm_takeover = true; + settings->audio_index = container.audio_index; + *duration = container.duration_seconds; + joc_log::line("probe: container %s track %u is not JOC; ffmpeg takeover", + path.c_str(), container.audio_index); + return true; + } joc_log::line("probe: container %s track %u is not JOC (%s)", path.c_str(), container.audio_index, detail.c_str()); return false; @@ -341,7 +378,7 @@ struct Plugin { duration = from_player; } } - if (!(duration > 0.0)) { + if (!(duration > 0.0) && !settings.pcm_takeover) { joc_log::line("start: no duration for %s, leaving the input to mpv", path.c_str()); osd(mpv, "JOC: unknown duration, plugin skipped"); return false; diff --git a/src/joc_decode.cpp b/src/joc_decode.cpp index a11a922..074d975 100644 --- a/src/joc_decode.cpp +++ b/src/joc_decode.cpp @@ -1476,4 +1476,289 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er } } + +// --------------------------------------------------------------------------- +// PcmEngine: ordinary PCM takeover. ffmpeg decodes once to 48 kHz float32; +// the kernel's PCM_LAYOUT input maps the channels and renders binaural. +// --------------------------------------------------------------------------- +struct PcmEngine::Impl { + CoreApi api; + joc_stream* stream = nullptr; + std::FILE* file = nullptr; + std::string temp_path; + int channels = 0; + std::uint64_t data_offset = 0; + std::uint64_t total_frames = 0; + std::uint64_t frames_read = 0; + double length = 0.0; + bool eof = false; + bool flushed = false; + std::vector feed; + + ~Impl() { stop(); } + + void stop() { + if (stream != nullptr) { + api.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(); + channels = 0; + data_offset = 0; + total_frames = 0; + frames_read = 0; + length = 0.0; + eof = false; + flushed = false; + } +}; + +PcmEngine::PcmEngine() : impl_(new Impl()) {} +PcmEngine::~PcmEngine() = default; + +unsigned PcmEngine::channels() const { return impl_->stream != nullptr ? 2u : 0u; } +double PcmEngine::length_seconds() const { return impl_->length; } + +namespace { + +std::uint16_t pcm_u16(const unsigned char* data) { + return static_cast(data[0] | (data[1] << 8)); +} +std::uint32_t pcm_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); +} +std::uint64_t pcm_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 + +bool PcmEngine::start(const std::string& input_path, const Settings& settings, + std::string* error) { + impl_->stop(); + if (!load_api(settings.core_path, &impl_->api, error)) return false; + + 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"mpv_joc_pcm_"; + temp += std::to_wstring(GetCurrentProcessId()); + temp += L".wav"; + impl_->temp_path = joc_path::to_utf8(temp.c_str()); + DeleteFileW(joc_path::to_wide_extended(impl_->temp_path).c_str()); + + std::wstring command = L"\"" + joc_path::to_wide_extended(settings.ffmpeg_path) + L"\""; + command += L" -hide_banner -loglevel error -nostdin -y -i \""; + command += joc_path::to_wide_extended(input_path); + command += L"\" -map 0:a:0 -af \"aresample=resampler=soxr:precision=28:osr=48000\" " + L"-c:a pcm_f32le \""; + command += joc_path::to_wide_extended(impl_->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(impl_->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); + + impl_->file = _wfopen(joc_path::to_wide_extended(impl_->temp_path).c_str(), L"rb"); + if (impl_->file == nullptr) { + if (error != nullptr) *error = "ffmpeg produced no WAV"; + return false; + } + unsigned char riff[12] = {}; + if (std::fread(riff, 1, sizeof(riff), impl_->file) != sizeof(riff) || + std::memcmp(riff, "RIFF", 4) != 0 || std::memcmp(riff + 8, "WAVE", 4) != 0) { + if (error != nullptr) *error = "ffmpeg did not produce a RIFF/WAVE file"; + return false; + } + bool have_format = false; + std::uint32_t sample_rate = 0; + std::uint16_t channels = 0; + for (;;) { + unsigned char chunk[8] = {}; + if (std::fread(chunk, 1, sizeof(chunk), impl_->file) != sizeof(chunk)) break; + const std::uint32_t size = pcm_u32(chunk + 4); + const std::uint64_t payload = static_cast(_ftelli64(impl_->file)); + if (std::memcmp(chunk, "fmt ", 4) == 0 && size >= 16u) { + unsigned char fmt[40] = {}; + const std::size_t bytes = std::min(size, sizeof(fmt)); + if (std::fread(fmt, 1, bytes, impl_->file) != bytes) break; + std::uint16_t format = pcm_u16(fmt); + channels = pcm_u16(fmt + 2); + sample_rate = pcm_u32(fmt + 4); + const std::uint16_t bits = pcm_u16(fmt + 14); + if (format == 0xFFFEu && bytes >= 40u) format = pcm_u16(fmt + 24); + if (format != 3u || bits != 32u) { + if (error != nullptr) *error = "decoded WAV is not 32-bit float"; + return false; + } + have_format = true; + } else if (std::memcmp(chunk, "data", 4) == 0) { + impl_->data_offset = payload; + impl_->total_frames = size == 0u || size == 0xFFFFFFFFu + ? (pcm_file_size(impl_->file) - payload) / + (static_cast(channels) * 4u) + : size / (static_cast(channels) * 4u); + } + _fseeki64(impl_->file, static_cast(payload + size + (size & 1u)), SEEK_SET); + } + if (!have_format || impl_->data_offset == 0u || sample_rate != 48000u || + (channels != 2 && channels != 6 && channels != 8)) { + if (error != nullptr) *error = "decoded WAV has an unsupported layout"; + return false; + } + impl_->channels = channels; + impl_->length = static_cast(impl_->total_frames) / 48000.0; + impl_->feed.assign(1536u * static_cast(channels), 0.0f); + _fseeki64(impl_->file, static_cast(impl_->data_offset), SEEK_SET); + + 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 = 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 = settings.rosella_filterbank; + const joc_error created_stream = impl_->api.create(&config, &impl_->stream); + if (created_stream != JOC_OK) { + if (error != nullptr) { + *error = std::string("kernel PCM layout stream failed: ") + + impl_->api.error_name(created_stream); + } + return false; + } + joc_log::line("pcm: started %s (%u ch, %.3f s, binaural)", input_path.c_str(), channels, + impl_->length); + return true; +} + +bool PcmEngine::seek(double seconds, double total_seconds, std::string* error) { + (void)total_seconds; + if (impl_->stream == nullptr || impl_->file == nullptr) return false; + if (seconds < 0.0) seconds = 0.0; + std::uint64_t frame = static_cast(seconds * 48000.0 + 0.5); + frame = std::min(frame, impl_->total_frames); + if (impl_->api.reset(impl_->stream) != JOC_OK) { + if (error != nullptr) *error = "kernel PCM reset failed"; + return false; + } + _fseeki64(impl_->file, + static_cast(impl_->data_offset + + frame * static_cast(impl_->channels) * 4u), + SEEK_SET); + impl_->frames_read = frame; + impl_->eof = frame >= impl_->total_frames; + impl_->flushed = false; + return true; +} + +std::size_t PcmEngine::read(float* destination, std::size_t frames, std::string* error) { + if (impl_->stream == nullptr || destination == nullptr || frames == 0u) return 0; + std::size_t written = 0; + while (written < frames) { + joc_stream_buffer output{}; + output.struct_size = sizeof(output); + output.struct_version = 1; + output.out_pcm = destination + written * 2u; + output.sample_count = static_cast(frames - written); + std::uint32_t produced = 0; + if (impl_->api.pull(impl_->stream, &output, &produced) != JOC_OK) { + if (error != nullptr) *error = "kernel PCM pull failed"; + return written; + } + written += produced; + if (written == frames) break; + if (!impl_->eof) { + std::size_t count = 1536u; + const std::uint64_t remaining = impl_->total_frames - impl_->frames_read; + if (remaining < count) count = static_cast(remaining); + std::fill(impl_->feed.begin(), impl_->feed.end(), 0.0f); + if (count != 0u) { + std::fread(impl_->feed.data(), sizeof(float), + count * static_cast(impl_->channels), impl_->file); + joc_stream_buffer input{}; + input.struct_size = sizeof(input); + input.struct_version = 1; + input.kind = JOC_STREAM_IN_PCM_LAYOUT; + input.channels = static_cast(impl_->channels); + input.sample_rate = 48000u; + input.sample_count = static_cast(count); + input.pcm = impl_->feed.data(); + std::uint32_t consumed = 0; + std::uint32_t consumed_bytes = 0; + if (impl_->api.push(impl_->stream, &input, &consumed, &consumed_bytes) != JOC_OK) { + if (error != nullptr) *error = "kernel PCM push failed"; + return written; + } + } + impl_->frames_read += count; + if (impl_->frames_read >= impl_->total_frames || count == 0u) impl_->eof = true; + continue; + } + if (!impl_->flushed) { + impl_->api.flush(impl_->stream); + impl_->flushed = true; + continue; + } + break; + } + return written; +} + +void PcmEngine::stop() { impl_->stop(); } + } // namespace joc_decode diff --git a/src/joc_decode.h b/src/joc_decode.h index 3c8218a..9012586 100644 --- a/src/joc_decode.h +++ b/src/joc_decode.h @@ -17,6 +17,7 @@ #include #include #include +#include #include namespace joc_decode { @@ -97,6 +98,31 @@ struct Settings { // rule is reproduced here instead. Out-of-range samples follow the core's // JOC_CLIP_CONTINUE policy: truncated to the edge, and reported. bool force_int24 = false; + // Ordinary PCM takeover: ffmpeg is the only decoder, the kernel maps the + // channels of the decoded 48 kHz float32 WAV. + bool pcm_takeover = false; +}; + +// Ordinary PCM input: ffmpeg decodes the file to a 48 kHz float32 WAV, the +// kernel's PCM_LAYOUT stream maps 2.0/5.1/7.1 to fixed objects and renders +// binaural. It shares no E-AC-3/JOC state with Engine. +class PcmEngine { +public: + PcmEngine(); + ~PcmEngine(); + PcmEngine(const PcmEngine&) = delete; + PcmEngine& operator=(const PcmEngine&) = delete; + + bool start(const std::string& input_path, const Settings& settings, std::string* error); + std::size_t read(float* destination, std::size_t frames, std::string* error); + bool seek(double seconds, double total_seconds, std::string* error); + unsigned channels() const; + void stop(); + double length_seconds() const; + +private: + struct Impl; + std::unique_ptr impl_; }; // Channel count of a named layout, or 0 when the layout is not one of the diff --git a/src/plugin/audio_source.cpp b/src/plugin/audio_source.cpp index f913cb1..e45da22 100644 --- a/src/plugin/audio_source.cpp +++ b/src/plugin/audio_source.cpp @@ -21,15 +21,22 @@ bool Source::open(const std::string& path, const joc_decode::Settings& settings, std::lock_guard guard(mutex_); int24_ = settings.force_int24; duration_ = duration_seconds; - if (!(duration_ > 0.0)) { + pcm_mode_ = settings.pcm_takeover; + if (!(duration_ > 0.0) && !pcm_mode_) { if (error != nullptr) *error = "the input duration is unknown"; return false; } - if (!engine_.start(path, settings, error)) { + if (pcm_mode_) { + if (!pcm_.start(path, settings, error)) { + pcm_.stop(); + return false; + } + } else if (!engine_.start(path, settings, error)) { engine_.stop(); return false; } - channels_ = engine_.channels(); + if (pcm_mode_ && !(duration_ > 0.0)) duration_ = pcm_.length_seconds(); + channels_ = pcm_mode_ ? 2u : engine_.channels(); if (channels_ == 0) { if (error != nullptr) *error = "the renderer produced no output channels"; engine_.stop(); @@ -47,8 +54,12 @@ bool Source::open(const std::string& path, const joc_decode::Settings& settings, void Source::close() { std::lock_guard guard(mutex_); - if (open_) engine_.stop(); + if (open_) { + engine_.stop(); + pcm_.stop(); + } open_ = false; + pcm_mode_ = false; channels_ = 0; total_samples_ = 0; produced_bytes_ = 0; @@ -77,7 +88,11 @@ std::size_t Source::read(std::uint64_t data_byte_offset, std::uint8_t* out, std: static_cast(sample), static_cast(deliverable), static_cast(data_byte_offset)); - if (!engine_.seek(static_cast(sample) / 48000.0, duration_, error)) return 0; + if (pcm_mode_) { + if (!pcm_.seek(static_cast(sample) / 48000.0, duration_, error)) return 0; + } else if (!engine_.seek(static_cast(sample) / 48000.0, duration_, error)) { + return 0; + } produced_bytes_ = sample * frame_bytes; carry_.clear(); skip_bytes_ = intra; @@ -97,7 +112,8 @@ std::size_t Source::read(std::uint64_t data_byte_offset, std::uint8_t* out, std: if (remaining_samples == 0) break; const std::size_t want = static_cast(std::min(kBlockFrames, remaining_samples)); - const std::size_t got = engine_.read(block_.data(), want, error); + const std::size_t got = pcm_mode_ ? pcm_.read(block_.data(), want, error) + : engine_.read(block_.data(), want, error); if (got == 0) break; produced_bytes_ += static_cast(got) * frame_bytes; diff --git a/src/plugin/audio_source.h b/src/plugin/audio_source.h index 8716cb2..d768876 100644 --- a/src/plugin/audio_source.h +++ b/src/plugin/audio_source.h @@ -38,6 +38,8 @@ public: private: std::mutex mutex_; joc_decode::Engine engine_; + joc_decode::PcmEngine pcm_; + bool pcm_mode_ = false; bool open_ = false; bool int24_ = false; unsigned channels_ = 0; diff --git a/src/plugin/config.cpp b/src/plugin/config.cpp index 02d33e2..e9ed4ee 100644 --- a/src/plugin/config.cpp +++ b/src/plugin/config.cpp @@ -69,6 +69,8 @@ Config parse_config(const std::string& script_opts, std::string* warnings) { const std::string& value = entry.second; if (key == "joc-enabled") { config.enabled = truthy(value); + } else if (key == "joc-pcm") { + config.pcm = truthy(value); } else if (key == "joc-output") { if (value == "binaural" || value == "2ch" || value == "0") { config.output = 0; diff --git a/src/plugin/config.h b/src/plugin/config.h index 8a71e04..1436be6 100644 --- a/src/plugin/config.h +++ b/src/plugin/config.h @@ -8,6 +8,8 @@ namespace joc_mpv { // script-opts keys are "joc-=" (see config/mpv.conf.example). struct Config { bool enabled = true; + // Take over ordinary PCM sources and render them binaurally through ffmpeg. + bool pcm = false; // 0 = binaural (2 channels), 1 = speaker layout (N channels). int output = 0; std::string layout = "7.1"; diff --git a/third_party/joc/include/joc_stream.h b/third_party/joc/include/joc_stream.h index 137b99e..c469dea 100644 --- a/third_party/joc/include/joc_stream.h +++ b/third_party/joc/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/third_party/joc/include/joc_version.h b/third_party/joc/include/joc_version.h index 0c131d4..4b6bcf8 100644 --- a/third_party/joc/include/joc_version.h +++ b/third_party/joc/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 */