From 49f3a0f0407a5fd1a3127dae3f2276614c30489c Mon Sep 17 00:00:00 2001 From: TheM14 Date: Tue, 6 Oct 2026 15:05:35 +0800 Subject: [PATCH] Update the vendored kernel to the bounded objects16 render-ahead --- kernel/include/joc_stream.h | 8 +-- kernel/joc_kernel.vcxproj | 8 +-- kernel/src/simd/cpu_probe.cpp | 4 +- kernel/src/stream/stream.cpp | 125 ++++++++++++++++++++++++---------- kernel/src/stream/stream.h | 21 +++--- src/joc_decode.cpp | 33 +++------ 6 files changed, 119 insertions(+), 80 deletions(-) diff --git a/kernel/include/joc_stream.h b/kernel/include/joc_stream.h index 4e04962..280b37a 100644 --- a/kernel/include/joc_stream.h +++ b/kernel/include/joc_stream.h @@ -39,14 +39,14 @@ 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, /* 16-channel objects16, decoded by the host */ + JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* objects16 frames, planar [16][1536] each */ JOC_STREAM_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */ } joc_stream_input; typedef enum joc_stream_output { - JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* planar [16][samples] float32 */ - JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */ - JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */ + JOC_STREAM_OUT_PCM_OBJECTS16 = 0, /* objects16 frames, planar [16][1536] each */ + JOC_STREAM_OUT_SPEAKER = 1, /* interleaved [samples][channels] f32 */ + JOC_STREAM_OUT_BINAURAL = 2 /* interleaved [samples][2] f32 */ } joc_stream_output; typedef struct joc_stream_config { diff --git a/kernel/joc_kernel.vcxproj b/kernel/joc_kernel.vcxproj index 15ffe88..e31c1da 100644 --- a/kernel/joc_kernel.vcxproj +++ b/kernel/joc_kernel.vcxproj @@ -121,11 +121,9 @@ * x64 gets the AVX2 and AVX-512 units, JOC_SIMD_HAVE_SSE2 / _AVX2 / _AVX512, and a per-file /arch for those two files only; - * Win32 (x86) gets the AVX2 unit and JOC_SIMD_HAVE_AVX2. AVX2 is not an - x86-64-only ISA and MSVC accepts /arch:AVX2 for x86, so gating it on the - pointer size left the 32-bit component on the scalar reference, which is - too slow to hold a 4096-frame read inside its own 85.3 ms of audio. - AVX-512 stays x64-only: 32-bit mode addresses ZMM0-7 only. + * Win32 (x86) gets the AVX2 unit and JOC_SIMD_HAVE_AVX2: AVX2 is not an + x86-64-only ISA. AVX-512 stays x64-only, because 32-bit mode addresses + ZMM0-7 only. * src\simd\kernels_intrin_neon.cpp is excluded everywhere here: the CMake build lists it only for aarch64 (CMakeLists.txt lines 177-180), which no configuration of this project targets. diff --git a/kernel/src/simd/cpu_probe.cpp b/kernel/src/simd/cpu_probe.cpp index 206d973..fe03e99 100644 --- a/kernel/src/simd/cpu_probe.cpp +++ b/kernel/src/simd/cpu_probe.cpp @@ -22,9 +22,7 @@ namespace joc::simd { namespace { // ---------------------------------------------------------------------- x86 -- -// Both pointer sizes are probed: AVX2 is not an x86-64-only ISA, and gating this -// on _M_X64 / __x86_64__ left every 32-bit x86 build reporting "no features", -// which pinned the dispatcher to the scalar kernels. +// AVX2 is not an x86-64-only ISA, so both pointer sizes are probed. #if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__) #if defined(_M_X64) || defined(_M_IX86) diff --git a/kernel/src/stream/stream.cpp b/kernel/src/stream/stream.cpp index 399d5ea..f2b2d5e 100644 --- a/kernel/src/stream/stream.cpp +++ b/kernel/src/stream/stream.cpp @@ -38,7 +38,10 @@ void Stream::reset_state() { reader_ = eac3::FrameReader(); metadata_.clear(); bed_pending_.clear(); + bed_read_offset_ = 0; objects16_.clear(); + objects_pending_.clear(); + objects_read_offset_ = 0; output_.clear(); read_offset_ = 0; info_ = Info(); @@ -245,54 +248,82 @@ Status Stream::push_objects16(const float* planar16, std::size_t samples, std::s if (planar16 == nullptr || samples == 0u) { return Status::success(); } - // Rendered immediately: the host has already done the JOC rebuild. + // Queued as whole frames, each planar [16][kFrameSamples]: the shape the + // objects16 output writes, so a host can feed a batch back unchanged. + constexpr std::size_t kFrameValues = + static_cast(JOC_OUTPUT_CHANNELS) * kFrameSamples; for (std::size_t offset = 0; offset < samples; offset += kFrameSamples) { const std::size_t count = std::min(kFrameSamples, samples - offset); - std::vector frame(static_cast(JOC_OUTPUT_CHANNELS) * kFrameSamples, - 0.0f); + const std::size_t base = objects_pending_.size(); + const float* frame = planar16 + (offset / kFrameSamples) * kFrameValues; + objects_pending_.resize(base + kFrameValues, 0.0f); for (std::size_t channel = 0; channel < JOC_OUTPUT_CHANNELS; ++channel) { - std::memcpy(frame.data() + channel * kFrameSamples, - planar16 + channel * samples + offset, count * sizeof(float)); + std::memcpy(objects_pending_.data() + base + channel * kFrameSamples, + frame + channel * kFrameSamples, count * sizeof(float)); } - ++info_.frames_in; - info_.samples_in += count; - const Status rendered = render_objects16(frame); + } + return process_objects16_frames(false); +} + +Status Stream::process_objects16_frames(bool drain_all) { + constexpr std::size_t kFrameValues = + static_cast(JOC_OUTPUT_CHANNELS) * kFrameSamples; + while (objects_pending_.size() - objects_read_offset_ >= kFrameValues) { + // Leave the rest queued, in order, for a later push or for flush(). + if (!drain_all && buffered_samples() >= kMaxRenderAheadSamples) { + break; + } + const auto first = objects_pending_.begin() + + static_cast(objects_read_offset_); + objects_frame_.assign(first, first + static_cast(kFrameValues)); + objects_read_offset_ += kFrameValues; + const Status rendered = render_objects16(objects_frame_); if (!rendered.ok()) { return rendered; } - if (count != kFrameSamples) { - break; // a partial frame is dropped; the host should push whole frames - } + ++info_.frames_in; + info_.samples_in += kFrameSamples; + } + if (objects_read_offset_ == objects_pending_.size()) { + objects_pending_.clear(); + objects_read_offset_ = 0; + } else if (objects_read_offset_ >= (1u << 20)) { + // Erasing from the front moves the remainder, so it is only worth doing + // once the consumed prefix is large enough to pay for the move. + objects_pending_.erase( + objects_pending_.begin(), + objects_pending_.begin() + static_cast(objects_read_offset_)); + objects_read_offset_ = 0; } return Status::success(); } Status Stream::process_ready_frames(bool drain_all) { - while (bed_pending_.size() / kBedChannels >= kFrameSamples && !metadata_.empty()) { - // Stop before rendering what the caller is not about to take: the frames - // stay queued, in order, and are rendered by a later push or by flush(). + while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * kBedChannels && + !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(); metadata_.pop_front(); - std::vector bed5(static_cast(JOC_CORE_CHANNELS) * kFrameSamples, 0.0f); - std::vector lfe(kFrameSamples, 0.0f); + const float* bed = bed_pending_.data() + bed_read_offset_; + bed5_.resize(static_cast(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_pending_[sample * kBedChannels + kCoreChannels[channel]]; + bed5_[channel * kFrameSamples + sample] = + bed[sample * kBedChannels + kCoreChannels[channel]]; } - lfe[sample] = bed_pending_[sample * kBedChannels + kLfeChannel]; + lfe_[sample] = bed[sample * kBedChannels + kLfeChannel]; } - bed_pending_.erase(bed_pending_.begin(), - bed_pending_.begin() + static_cast(kFrameSamples * - kBedChannels)); + bed_read_offset_ += kFrameSamples * kBedChannels; + compact_bed_pending(); std::string error; - const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5.data(), - lfe.data(), gain_, &objects16_, &error); + const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5_.data(), + lfe_.data(), gain_, &objects16_, &error); if (!rebuilt.ok()) { return Status::fail(rebuilt.code(), stage::kDsp, error); } @@ -307,6 +338,22 @@ Status Stream::process_ready_frames(bool drain_all) { return Status::success(); } +void Stream::compact_bed_pending() { + if (bed_read_offset_ == 0) { + return; + } + if (bed_read_offset_ == bed_pending_.size()) { + bed_pending_.clear(); + bed_read_offset_ = 0; + } else if (bed_read_offset_ >= (1u << 20)) { + // Erasing from the front moves the remainder, so it is only worth doing + // once the consumed prefix is large enough to pay for the move. + bed_pending_.erase(bed_pending_.begin(), + bed_pending_.begin() + static_cast(bed_read_offset_)); + bed_read_offset_ = 0; + } +} + Status Stream::render_objects16(const std::vector& objects16) { if (config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) { output_.insert(output_.end(), objects16.begin(), objects16.end()); @@ -324,6 +371,7 @@ Status Stream::render_objects16(const std::vector& objects16) { if (!stepped.ok()) { return Status::fail(stepped.code(), stage::kRender, error); } + output_.reserve(output_.size() + speaker_.output.size()); for (const double value : speaker_.output) { output_.push_back(static_cast(value)); } @@ -344,13 +392,13 @@ Status Stream::render_objects16(const std::vector& objects16) { if (!submitted.ok()) { return submitted; } - std::vector produced; - binaural_.take_output(&produced); - for (const double value : produced) { + binaural_.take_output(&produced_); + output_.reserve(output_.size() + produced_.size()); + for (const double value : produced_) { output_.push_back(static_cast(value)); } info_.frames_out++; - info_.samples_out += produced.size() / 2u; + info_.samples_out += produced_.size() / 2u; return Status::success(); } @@ -371,16 +419,15 @@ Status Stream::render_rosella_objects16(const std::vector& objects16) { if (!submitted.ok()) { return submitted; } - std::vector produced; - rosella_.take_output(&produced); - if (!produced.empty()) { - rosella_pending_.insert(rosella_pending_.end(), produced.begin(), produced.end()); + rosella_.take_output(&produced_); + if (!produced_.empty()) { + rosella_pending_.insert(rosella_pending_.end(), produced_.begin(), produced_.end()); } release_rosella_output(kFrameSamples); info_.frames_out++; // Counted as the runtime produces it, which is also how the SOFA path counts: // the totals are identical, only the frame they appear on differs. - info_.samples_out += produced.size() / 2u; + info_.samples_out += produced_.size() / 2u; return Status::success(); } @@ -393,6 +440,7 @@ void Stream::release_rosella_output(std::size_t limit) { return; } const std::size_t values = count * 2u; + output_.reserve(output_.size() + values); for (std::size_t index = 0; index < values; ++index) { output_.push_back(static_cast(rosella_pending_[rosella_read_offset_ + index])); } @@ -434,13 +482,16 @@ Status Stream::pull(float* destination, std::size_t capacity_samples, std::size_ } Status Stream::flush() { - // Input has ended, so the render-ahead bound has nothing left to wait for: - // every frame still queued has to reach the renderer before its tail is - // drained, or the end of the file would be dropped. + // Input has ended, so drain what the cap held back: nothing else will + // trigger rendering. const Status remaining = process_ready_frames(true); if (!remaining.ok()) { return remaining; } + const Status objects = process_objects16_frames(true); + if (!objects.ok()) { + return objects; + } if (binaural_ready_) { std::vector tail; const Status drained = @@ -448,6 +499,7 @@ Status Stream::flush() { if (!drained.ok()) { return drained; } + output_.reserve(output_.size() + tail.size()); for (const double value : tail) { output_.push_back(static_cast(value)); } @@ -464,6 +516,7 @@ Status Stream::flush() { // tail only sounds after it. The program samples were already counted by // render_rosella_objects16, so only the tail is added here. release_rosella_output(rosella_pending_samples()); + output_.reserve(output_.size() + tail.size()); for (const double value : tail) { output_.push_back(static_cast(value)); } diff --git a/kernel/src/stream/stream.h b/kernel/src/stream/stream.h index e0ad56a..7f686af 100644 --- a/kernel/src/stream/stream.h +++ b/kernel/src/stream/stream.h @@ -85,13 +85,8 @@ public: : 0u; } - // A push renders every frame it makes ready, and the caller decides how far - // its demuxer runs ahead of playback. Without a bound, a demuxer that runs - // far ahead turns its whole read-ahead burst into latency on whichever pull() - // happens to follow it: the samples are not wasted, but they are rendered at - // the worst possible moment. Rendering therefore stops once this many - // samples are rendered and unpulled; flush() lifts the bound so the frames - // still waiting when the input ends are drained rather than dropped. + // Cap on rendered samples that have not been pulled. A push renders what it + // makes ready, so a caller that feeds faster than it pulls renders ahead. static constexpr std::size_t kMaxRenderAheadSamples = 16384; private: @@ -106,6 +101,10 @@ private: return rosella_ready_ ? (rosella_pending_.size() - rosella_read_offset_) / 2u : 0u; } void reset_state(); + // Drops the bed samples that have already been rendered, keeping the rest. + void compact_bed_pending(); + // Renders the queued objects16 frames, bounded by kMaxRenderAheadSamples. + Status process_objects16_frames(bool drain_all); Config config_; Info info_; @@ -113,7 +112,12 @@ private: std::deque metadata_; FrameMetadata pending_metadata_; std::vector bed_pending_; - std::vector frame_copy_; + std::size_t bed_read_offset_ = 0; + std::vector bed5_; + std::vector lfe_; + std::vector objects_pending_; + std::size_t objects_read_offset_ = 0; + std::vector objects_frame_; std::vector objects16_; std::vector output_; std::size_t read_offset_ = 0; @@ -125,6 +129,7 @@ private: hrtf::RosellaRuntime rosella_; std::vector rosella_pending_; std::size_t rosella_read_offset_ = 0; + std::vector produced_; bool speaker_enabled_ = false; bool binaural_enabled_ = false; bool binaural_ready_ = false; diff --git a/src/joc_decode.cpp b/src/joc_decode.cpp index 218cfd7..9d0a427 100644 --- a/src/joc_decode.cpp +++ b/src/joc_decode.cpp @@ -20,15 +20,10 @@ namespace joc_decode { namespace { constexpr std::size_t kEac3Chunk = 96u * 1024u; // bytes read per push -// The core renders every frame it is handed, and it renders it during the push, -// so a read must not queue more frames than the caller is about to take: a 96 KB -// read is around thirty syncframes, i.e. a second of audio rendered to satisfy -// one 4096-frame read. This is that read (2.67 syncframes) rounded up, so each -// read hands the core about as much as it is about to consume. +// The core renders during the push, so a read queues about as much as it is +// about to consume rather than everything it just read. constexpr std::uint64_t kEac3FramesPerRead = 3u; -// Rendered audio the caller has not taken yet. Once this much is waiting there -// is nothing to gain from queueing more input: the core would render it now and -// the caller would not ask for it for several more reads. +// 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 @@ -984,11 +979,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er impl.eac3_eof = true; } - // The core renders during the push, so input is queued only while the - // caller still has less than one read's worth of rendered audio waiting. - // Pushing past that is what turns a single read into a second of work: - // the samples are rendered early rather than wrongly, and the read that - // pays for them overruns its own audio. + // Queue more input only while less than one read's worth is waiting. std::size_t rendered_ahead = 0; { joc_stream_status_info pending{}; @@ -1049,13 +1040,9 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er offset += bytes; ++complete; } - // The chunk is cut at a frame boundary for two independent - // reasons: a configured input limit has to stop exactly where it - // says, and a read must not queue more frames than it is about to - // consume. Both cuts land on a frame boundary because the - // renderer's output depends on how many frames it was given, so a - // cut that overshoots would not reproduce a run that stopped - // earlier. + // Both cuts land on a frame boundary because the renderer's output + // depends on how many frames it was given: the input limit has to + // stop where it says, and a read queues only its own budget. std::uint64_t allowed = complete; if (impl.settings.input_frame_limit != 0) { const std::uint64_t room = @@ -1096,10 +1083,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er std::memmove(impl.eac3_buffer.data(), impl.eac3_buffer.data() + push_bytes, impl.eac3_carry); } - // The pipe can end while complete syncframes are still waiting in - // the buffer: they are queued by the next pass, so the input is - // only over once nothing but a partial frame is left. Ending here - // instead would drop them, and with them the end of the file. + // The pipe can end with complete syncframes still buffered; they + // are queued next, so end of input is only real once none remain. if (got == 0 && pushed_frames >= complete) impl.eac3_eof = true; } }