Fix mpv PCM source lifetime and preserve JOC probe results

- Use a unique temp WAV per PcmEngine so concurrent or reopened PCM sources
  cannot overwrite each other.
- Truncate PcmEngine reads at the decoded duration and reset the emitted
  position on seek.
- Only fall back to PCM after a successful "not JOC" verdict; a failed JOC
  probe now leaves the input alone instead of stealing a potentially JOC
  source.
This commit is contained in:
TheM14 committed 2026-11-13 13:33:32 +08:00
1 parent 1afcd6ee06
commit 86774828d4
2 files changed
+27 -5

No files matched your search

+8 -2
View File
@@ -316,8 +316,14 @@ struct Plugin {
bool verdict = false; bool verdict = false;
std::string detail; std::string detail;
if (!joc_decode::probe_container_joc(config.ffmpeg, path, container.audio_index, &verdict, if (!joc_decode::probe_container_joc(config.ffmpeg, path, container.audio_index, &verdict,
&detail) || &detail)) {
!verdict) { // A failed probe is not evidence that the track is non-JOC. Keep the
// existing JOC path instead of stealing a possibly-JOC file for PCM.
joc_log::line("probe: cannot examine container %s track %u (%s); leaving it alone",
path.c_str(), container.audio_index, detail.c_str());
return false;
}
if (!verdict) {
if (config.pcm) { if (config.pcm) {
settings->pcm_takeover = true; settings->pcm_takeover = true;
settings->audio_index = container.audio_index; settings->audio_index = container.audio_index;
+19 -3
View File
@@ -2,6 +2,7 @@
#include <windows.h> #include <windows.h>
#include <atomic>
#include <cmath> #include <cmath>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@@ -1490,6 +1491,7 @@ struct PcmEngine::Impl {
std::uint64_t data_offset = 0; std::uint64_t data_offset = 0;
std::uint64_t total_frames = 0; std::uint64_t total_frames = 0;
std::uint64_t frames_read = 0; std::uint64_t frames_read = 0;
std::uint64_t emitted_frames = 0;
double length = 0.0; double length = 0.0;
bool eof = false; bool eof = false;
bool flushed = false; bool flushed = false;
@@ -1516,6 +1518,7 @@ struct PcmEngine::Impl {
data_offset = 0; data_offset = 0;
total_frames = 0; total_frames = 0;
frames_read = 0; frames_read = 0;
emitted_frames = 0;
length = 0.0; length = 0.0;
eof = false; eof = false;
flushed = false; flushed = false;
@@ -1563,6 +1566,13 @@ bool PcmEngine::start(const std::string& input_path, const Settings& settings,
if (!temp.empty() && temp.back() != L'\\') temp.push_back(L'\\'); if (!temp.empty() && temp.back() != L'\\') temp.push_back(L'\\');
temp += L"mpv_joc_pcm_"; temp += L"mpv_joc_pcm_";
temp += std::to_wstring(GetCurrentProcessId()); temp += std::to_wstring(GetCurrentProcessId());
temp += L"_";
{
static std::atomic<std::uint64_t> serial{0};
temp += std::to_wstring(serial.fetch_add(1, std::memory_order_relaxed));
}
temp += L"_";
temp += std::to_wstring(GetTickCount64());
temp += L".wav"; temp += L".wav";
impl_->temp_path = joc_path::to_utf8(temp.c_str()); impl_->temp_path = joc_path::to_utf8(temp.c_str());
DeleteFileW(joc_path::to_wide_extended(impl_->temp_path).c_str()); DeleteFileW(joc_path::to_wide_extended(impl_->temp_path).c_str());
@@ -1659,6 +1669,7 @@ bool PcmEngine::start(const std::string& input_path, const Settings& settings,
impl_->length = static_cast<double>(impl_->total_frames) / 48000.0; impl_->length = static_cast<double>(impl_->total_frames) / 48000.0;
impl_->feed.assign(1536u * static_cast<std::size_t>(channels), 0.0f); impl_->feed.assign(1536u * static_cast<std::size_t>(channels), 0.0f);
_fseeki64(impl_->file, static_cast<long long>(impl_->data_offset), SEEK_SET); _fseeki64(impl_->file, static_cast<long long>(impl_->data_offset), SEEK_SET);
impl_->emitted_frames = 0;
joc_stream_config config{}; joc_stream_config config{};
config.struct_size = sizeof(config); config.struct_size = sizeof(config);
@@ -1701,6 +1712,7 @@ bool PcmEngine::seek(double seconds, double total_seconds, std::string* error) {
frame * static_cast<std::uint64_t>(impl_->channels) * 4u), frame * static_cast<std::uint64_t>(impl_->channels) * 4u),
SEEK_SET); SEEK_SET);
impl_->frames_read = frame; impl_->frames_read = frame;
impl_->emitted_frames = frame;
impl_->eof = frame >= impl_->total_frames; impl_->eof = frame >= impl_->total_frames;
impl_->flushed = false; impl_->flushed = false;
return true; return true;
@@ -1708,20 +1720,23 @@ bool PcmEngine::seek(double seconds, double total_seconds, std::string* error) {
std::size_t PcmEngine::read(float* destination, std::size_t frames, std::string* error) { std::size_t PcmEngine::read(float* destination, std::size_t frames, std::string* error) {
if (impl_->stream == nullptr || destination == nullptr || frames == 0u) return 0; if (impl_->stream == nullptr || destination == nullptr || frames == 0u) return 0;
if (impl_->emitted_frames >= impl_->total_frames) return 0;
const std::size_t wanted = static_cast<std::size_t>(
std::min<std::uint64_t>(frames, impl_->total_frames - impl_->emitted_frames));
std::size_t written = 0; std::size_t written = 0;
while (written < frames) { while (written < wanted) {
joc_stream_buffer output{}; joc_stream_buffer output{};
output.struct_size = sizeof(output); output.struct_size = sizeof(output);
output.struct_version = 1; output.struct_version = 1;
output.out_pcm = destination + written * 2u; output.out_pcm = destination + written * 2u;
output.sample_count = static_cast<std::uint32_t>(frames - written); output.sample_count = static_cast<std::uint32_t>(wanted - written);
std::uint32_t produced = 0; std::uint32_t produced = 0;
if (impl_->api.pull(impl_->stream, &output, &produced) != JOC_OK) { if (impl_->api.pull(impl_->stream, &output, &produced) != JOC_OK) {
if (error != nullptr) *error = "kernel PCM pull failed"; if (error != nullptr) *error = "kernel PCM pull failed";
return written; return written;
} }
written += produced; written += produced;
if (written == frames) break; if (written == wanted) break;
if (!impl_->eof) { if (!impl_->eof) {
std::size_t count = 1536u; std::size_t count = 1536u;
const std::uint64_t remaining = impl_->total_frames - impl_->frames_read; const std::uint64_t remaining = impl_->total_frames - impl_->frames_read;
@@ -1756,6 +1771,7 @@ std::size_t PcmEngine::read(float* destination, std::size_t frames, std::string*
} }
break; break;
} }
impl_->emitted_frames += written;
return written; return written;
} }