The kernel copy follows the new kernel. The renderer has a frame-level state machine (normal, hold, hard cut and passthrough, the 8-plane passthrough, the LFE clipgain ramp and interpolation when no previous state exists), the OAMD parser carries the complete frame state (several blocks, more than 16 objects) and the speaker renderer applies the metadata (gain and inactive flags, zone/elevation, size, snap, divergence, ISF and bed distribution). oamd_math and oamd_params are new modules, the ISF tables are compiled in now that the upstream data/ directory is gone, and the binaural timeline accepts a complete frame state. joc_core.h moves to ABI 6 (frame parameters and EMDF payload info at v2, JOC_FRAME_STATUS_*, raw 5-bit offset_ts) and eac3joc_core.h to EJOC ABI 4 (EJOC_FRAME_STATUS_*, ejoc_frame_input v1, ejoc_renderer_render_frame, EJOC_CORE_OUT_CHANNELS and EJOC_LFE_SLOT; the old ejoc_renderer_process stays). joc_stream.h and joc_version.h are unchanged. joc_kernel.vcxproj lists the two new translation units and the four new headers. The component's layout table gains the kernel's 6.0 layout (L R Ls Rs Lrs Rrs) after 9.1.6, and layout_table_test expects twelve layouts, so the list the preferences page shows and the list the renderer accepts are the same again. README, THIRD_PARTY_NOTICES and the CI comment pin the copy to commit 3f0d5e87fe41b90d9350528d2dca2ca82a637a3f; tag v1.2.0 points at the older a276028 and is not the sync baseline. Builds Release-Static|x64 and |Win32 with no errors. The offline test tools build and pass on both architectures: layout_table_test reports 12 layouts, and channel_swap_test, log_rotation_test and long_path_test pass. scan_crosscheck agrees with the core on the upstream test material, and the render harness delivers 7.1.4 speaker output and binaural output on both architectures, with the speaker-path WAV byte-identical between them. The core is still vendored under kernel/; building it from the upstream checkout at build time is a separate change.
766 lines
30 KiB
C++
766 lines
30 KiB
C++
#include "stream/stream.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
|
|
#include "adm/adm_metadata.h"
|
|
#include "foundation/status.h"
|
|
#include "hrtf/jochrtf.h"
|
|
#include "hrtf/rosella_model.h"
|
|
#include "joc_bitstream/bed_layout_map.h"
|
|
#include "joc_bitstream/joc_parser.h"
|
|
#include "joc_core/objects16.h"
|
|
|
|
namespace joc::stream {
|
|
|
|
namespace {
|
|
|
|
constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES;
|
|
|
|
} // namespace
|
|
|
|
Stream::~Stream() {
|
|
if (rebuilder_ != nullptr) {
|
|
ejoc_renderer_destroy(rebuilder_);
|
|
rebuilder_ = nullptr;
|
|
}
|
|
if (speaker_.handle != nullptr) {
|
|
ejoc_speaker_renderer_destroy(speaker_.handle);
|
|
speaker_.handle = nullptr;
|
|
}
|
|
}
|
|
|
|
void Stream::reset_state() {
|
|
reader_ = eac3::FrameReader();
|
|
metadata_.clear();
|
|
pending_frame_ = FrameMetadata();
|
|
bed_plan_ = joc::BedPlan();
|
|
bed_channels_ = 0;
|
|
config_idx_ = -1;
|
|
core_channels_ = 0;
|
|
bed_plan_failed_ = false;
|
|
bed_plan_error_ = Status::success();
|
|
bed_input_channels_ = 0;
|
|
bed_pending_.clear();
|
|
bed_read_offset_ = 0;
|
|
objects16_.clear();
|
|
objects_pending_.clear();
|
|
objects_read_offset_ = 0;
|
|
output_.clear();
|
|
read_offset_ = 0;
|
|
info_ = Info();
|
|
info_.output_channels = output_channels_;
|
|
if (rebuilder_ != nullptr) {
|
|
ejoc_renderer_reset(rebuilder_);
|
|
}
|
|
if (speaker_.handle != nullptr) {
|
|
ejoc_speaker_renderer_reset(speaker_.handle);
|
|
speaker_.state.reset();
|
|
speaker_.output.clear();
|
|
speaker_.last_had_payload = false;
|
|
// Back to the initial controls; the next trim element replaces them.
|
|
speaker_.controls_ready = false;
|
|
}
|
|
if (binaural_ready_) {
|
|
binaural_.reset();
|
|
}
|
|
if (rosella_ready_) {
|
|
rosella_.reset();
|
|
}
|
|
rosella_pending_.clear();
|
|
rosella_read_offset_ = 0;
|
|
}
|
|
|
|
Status Stream::create(const Config& config) {
|
|
if (rebuilder_ != nullptr) {
|
|
return Status::fail(JOC_ERR_STATE, stage::kRender, "stream already created");
|
|
}
|
|
config_ = config;
|
|
if (config_.input > JOC_STREAM_IN_PCM_OBJECTS16 ||
|
|
config_.output > JOC_STREAM_OUT_BINAURAL) {
|
|
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender, "unknown stream kind");
|
|
}
|
|
if (config_.input == JOC_STREAM_IN_PCM_OBJECTS16 &&
|
|
config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) {
|
|
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender,
|
|
"objects16 input with objects16 output would do nothing");
|
|
}
|
|
gain_ = static_cast<float>(std::pow(10.0, config_.gain_db / 20.0));
|
|
|
|
if (config_.input == JOC_STREAM_IN_EAC3) {
|
|
rebuilder_ = ejoc_renderer_create();
|
|
if (rebuilder_ == nullptr) {
|
|
return Status::fail(JOC_ERR_OUT_OF_MEMORY, stage::kDsp, "cannot create the JOC kernel");
|
|
}
|
|
if (config_.native_threads != 0u) {
|
|
ejoc_renderer_set_threads(rebuilder_, config_.native_threads);
|
|
}
|
|
}
|
|
|
|
if (config_.output == JOC_STREAM_OUT_SPEAKER) {
|
|
speaker_enabled_ = true;
|
|
if (!speaker::layout_by_name(config_.layout.c_str(), &speaker_.layout)) {
|
|
return Status::fail(JOC_ERR_LAYOUT_UNSUPPORTED, stage::kRender,
|
|
"unknown speaker layout: " + config_.layout);
|
|
}
|
|
speaker_.metadata_offset = config_.metadata_offset;
|
|
speaker_.handle = ejoc_speaker_renderer_create(speaker_.layout.speaker_bitfield);
|
|
if (speaker_.handle == nullptr) {
|
|
return Status::fail(JOC_ERR_RENDER_FAILED, stage::kRender,
|
|
"cannot create the speaker renderer");
|
|
}
|
|
output_channels_ = speaker_.layout.channel_count;
|
|
} else if (config_.output == JOC_STREAM_OUT_BINAURAL) {
|
|
binaural_enabled_ = true;
|
|
// The HRTF input precedence is joc_task_config's: a Rosella
|
|
// .personalized_headphone wins over a SOFA that the library compiles, and
|
|
// the compiled .jochrtf (hrtf_path) stays the fallback input.
|
|
if (!config_.personalized_headphone_path.empty()) {
|
|
hrtf::RosellaModel model;
|
|
Status status =
|
|
hrtf::load_personalized_headphone(config_.personalized_headphone_path, &model);
|
|
hrtf::RosellaRenderOptions render_options;
|
|
if (config_.binaural_mode == JOC_BINAURAL_NEAR) {
|
|
render_options.profile = hrtf::RosellaProfile::Near;
|
|
} else if (config_.binaural_mode == JOC_BINAURAL_FAR) {
|
|
render_options.profile = hrtf::RosellaProfile::Far;
|
|
}
|
|
render_options.object_delay_samples = config_.object_delay_samples;
|
|
render_options.tail_seconds = config_.tail_seconds;
|
|
render_options.bank = config_.rosella_filterbank == JOC_ROSELLA_FILTERBANK_CAPTURED
|
|
? hrtf::RosellaBankKind::Captured
|
|
: hrtf::RosellaBankKind::Public;
|
|
if (status.ok()) {
|
|
status = rosella_.open(model, render_options);
|
|
}
|
|
if (!status.ok()) {
|
|
return Status::fail(status.code(), stage::kRender,
|
|
"binaural setup failed: " + status.message());
|
|
}
|
|
rosella_ready_ = true;
|
|
output_channels_ = 2;
|
|
} else {
|
|
hrtf::Field field;
|
|
hrtf::Kernels kernels;
|
|
Status status = Status::success();
|
|
status = hrtf::load_jochrtf(config_.hrtf_path, &field);
|
|
if (status.ok()) {
|
|
status = config_.kernels_path.empty()
|
|
? (kernels = hrtf::builtin_kernels(), Status::success())
|
|
: hrtf::load_kernels(config_.kernels_path, &kernels);
|
|
}
|
|
binaural::Profile profile = binaural::Profile::Mid;
|
|
if (status.ok() && config_.binaural_mode == JOC_BINAURAL_NEAR) {
|
|
profile = binaural::Profile::Near;
|
|
} else if (status.ok() && config_.binaural_mode == JOC_BINAURAL_FAR) {
|
|
profile = binaural::Profile::Far;
|
|
}
|
|
if (status.ok()) {
|
|
binaural::RoomConstants room;
|
|
room.enable_early_reflections = config_.jochrtf_room ? 1u : 0u;
|
|
room.enable_late_room = room.enable_early_reflections;
|
|
status = binaural_.open(field, kernels, profile, room);
|
|
}
|
|
if (!status.ok()) {
|
|
return status;
|
|
}
|
|
binaural_ready_ = true;
|
|
output_channels_ = 2;
|
|
}
|
|
} else {
|
|
output_channels_ = JOC_OUTPUT_CHANNELS;
|
|
}
|
|
reset_state();
|
|
return Status::success();
|
|
}
|
|
|
|
void Stream::apply_bed_positions(const FrameMetadata& entry) {
|
|
if (bed_positions_ready_ || entry.bed_assignments.empty()) {
|
|
return;
|
|
}
|
|
const bool bed_only = entry.has_update && entry.update.content_bed &&
|
|
!entry.update.content_isf && !entry.update.content_dynamic;
|
|
const ::joc::joc::BedLayout bed = ::joc::joc::resolve_bed_layout(entry.bed_assignments, bed_only);
|
|
if (!bed.present) {
|
|
return;
|
|
}
|
|
bed_positions_ready_ = true;
|
|
if (!bed.bed_only) {
|
|
return;
|
|
}
|
|
for (std::size_t slot = 0; slot < bed.channels.size() &&
|
|
slot < static_cast<std::size_t>(oamd::kObjects);
|
|
++slot) {
|
|
const ::joc::joc::BedChannel channel = bed.channels[slot];
|
|
double xyz[3] = {0.0, 0.0, 0.0};
|
|
if (::joc::joc::bed_channel_adm_position(channel, xyz)) {
|
|
if (binaural_ready_) {
|
|
binaural_.set_fixed_position(static_cast<int>(slot) + 1, xyz);
|
|
}
|
|
if (rosella_ready_) {
|
|
rosella_.set_fixed_position(static_cast<int>(slot) + 1, xyz);
|
|
}
|
|
}
|
|
if (!speaker_enabled_) {
|
|
continue;
|
|
}
|
|
std::uint16_t q15[3] = {0, 0, 0};
|
|
if (!::joc::joc::layout_bed_anchor(speaker_.layout.out_ch_config, channel, q15) &&
|
|
!::joc::joc::bed_channel_q15(channel, q15)) {
|
|
continue;
|
|
}
|
|
speaker_.bed_override[slot] = true;
|
|
for (int axis = 0; axis < oamd::kAxes; ++axis) {
|
|
speaker_.bed_positions[slot][axis] = q15[axis];
|
|
}
|
|
}
|
|
}
|
|
|
|
Status Stream::flush_pending_frame() {
|
|
if (pending_frame_.streams.empty()) {
|
|
return Status::success();
|
|
}
|
|
FrameMetadata entry = pending_frame_;
|
|
pending_frame_ = FrameMetadata();
|
|
if (entry.has_joc) {
|
|
apply_bed_positions(entry);
|
|
}
|
|
if (entry.has_joc) {
|
|
std::string error;
|
|
joc::CrossLayerNotes notes;
|
|
const Status checked = joc::check_cross_layer(
|
|
entry.params, entry.has_core ? &entry.core_frame : nullptr, entry.has_core,
|
|
entry.has_dependent, entry.has_chanmap, entry.chanmap,
|
|
entry.bed_assignments.empty() ? nullptr : &entry.bed_assignments, &error, ¬es);
|
|
if (!checked.ok()) {
|
|
return Status::fail(checked.code(), checked.stage(),
|
|
error.empty() ? checked.message() : error);
|
|
}
|
|
if (notes.chanmap_pair_mismatch) {
|
|
++info_.chanmap_pair_mismatch_frames;
|
|
}
|
|
if (notes.bed_mask_mismatch) {
|
|
++info_.bed_mask_mismatch_frames;
|
|
}
|
|
if (entry.params.objects_clamped != 0u) {
|
|
++info_.objects_clamped_frames;
|
|
}
|
|
} else {
|
|
// No JOC metadata for this audio frame: the object channels stay silent
|
|
// while the kernel state still advances for the frame.
|
|
++info_.missing_joc_frames;
|
|
joc::make_silent_frame_params(&entry.params);
|
|
entry.has_update = false;
|
|
}
|
|
metadata_.push_back(entry);
|
|
return Status::success();
|
|
}
|
|
|
|
Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t* consumed) {
|
|
if (rebuilder_ == nullptr) {
|
|
return Status::fail(JOC_ERR_STATE, stage::kEmdf, "stream is not an E-AC-3 input");
|
|
}
|
|
if (consumed != nullptr) {
|
|
*consumed = size;
|
|
}
|
|
if (data != nullptr && size != 0u) {
|
|
reader_.push(data, size);
|
|
info_.bytes_in += size;
|
|
}
|
|
for (;;) {
|
|
eac3::Frame frame;
|
|
const eac3::FrameReader::Next state = reader_.next(&frame);
|
|
if (state == eac3::FrameReader::Next::End) {
|
|
break;
|
|
}
|
|
if (state == eac3::FrameReader::Next::Fail) {
|
|
return Status::fail(reader_.error(), stage::kEac3, reader_.error_message());
|
|
}
|
|
// A repeated (codec, strmtyp, substreamid) combination starts the next
|
|
// audio frame, so the group that is still open is complete.
|
|
bool repeats = false;
|
|
for (const eac3::Frame& prior : pending_frame_.streams) {
|
|
if (prior.same_stream(frame)) {
|
|
repeats = true;
|
|
break;
|
|
}
|
|
}
|
|
if (repeats) {
|
|
const Status flushed = flush_pending_frame();
|
|
if (!flushed.ok()) {
|
|
return flushed;
|
|
}
|
|
}
|
|
eac3::Frame header = frame;
|
|
header.data = nullptr;
|
|
header.size = 0;
|
|
header.offset = frame.offset;
|
|
pending_frame_.streams.push_back(header);
|
|
if (frame.codec == 0u || frame.strmtyp == 0u) {
|
|
pending_frame_.core_frame = header;
|
|
pending_frame_.has_core = true;
|
|
} else {
|
|
pending_frame_.has_dependent = true;
|
|
if (frame.has_chanmap != 0u) {
|
|
pending_frame_.has_chanmap = true;
|
|
pending_frame_.chanmap = frame.chanmap;
|
|
}
|
|
}
|
|
|
|
bool present = false;
|
|
bool ignored_extra = false;
|
|
emdf::Container container;
|
|
const Status found = emdf::find_joc_emdf_optional(
|
|
frame.data, frame.size, &present, &container, &joc::score_container_as_joc,
|
|
&ignored_extra);
|
|
if (!found.ok()) {
|
|
return found;
|
|
}
|
|
if (ignored_extra) {
|
|
++info_.extra_container_frames;
|
|
}
|
|
if (!present) {
|
|
continue;
|
|
}
|
|
if (container.find(emdf::kIdJoc) == nullptr) {
|
|
continue;
|
|
}
|
|
joc_frame_params params;
|
|
const Status parsed =
|
|
joc::parse_eac3_frame(frame.data, frame.size, ¶ms, &container, nullptr);
|
|
if (!parsed.ok()) {
|
|
return parsed;
|
|
}
|
|
pending_frame_.params = params;
|
|
pending_frame_.has_joc = true;
|
|
pending_frame_.syncframe_offset = frame.offset;
|
|
pending_frame_.syncframe_size = frame.size;
|
|
if (const emdf::Payload* payload = container.find(emdf::kIdOamd)) {
|
|
std::vector<std::uint8_t> bytes;
|
|
const Status extracted =
|
|
emdf::extract_payload_bytes(frame.data, frame.size, *payload, &bytes);
|
|
if (!extracted.ok()) {
|
|
return extracted;
|
|
}
|
|
// Speaker and binaural consume the full per-object block state; the
|
|
// other stream paths keep the reference's legacy single-block view.
|
|
const bool full_metadata_state = speaker_enabled_ || binaural_enabled_;
|
|
const Status oamd =
|
|
full_metadata_state ? oamd::parse_id11_state(bytes.data(), bytes.size(),
|
|
&pending_frame_.update)
|
|
: oamd::parse_id11(bytes.data(), bytes.size(),
|
|
&pending_frame_.update);
|
|
if (!oamd.ok()) {
|
|
return oamd;
|
|
}
|
|
pending_frame_.has_update = true;
|
|
pending_frame_.outer_offset = static_cast<std::int64_t>(payload->sample_offset);
|
|
const Status bed = oamd::parse_id11_bed_assignments(
|
|
bytes.data(), bytes.size(), &pending_frame_.bed_assignments);
|
|
if (!bed.ok()) {
|
|
return bed;
|
|
}
|
|
}
|
|
}
|
|
resolve_bed_plan();
|
|
return process_ready_frames(false);
|
|
}
|
|
|
|
Status Stream::push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels,
|
|
std::size_t* consumed) {
|
|
if (rebuilder_ == nullptr) {
|
|
return Status::fail(JOC_ERR_STATE, stage::kDsp, "stream is not an E-AC-3 input");
|
|
}
|
|
if (channels != 6u && channels != 8u) {
|
|
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp,
|
|
"the core PCM must be interleaved as 6 or 8 channels, got " +
|
|
std::to_string(channels));
|
|
}
|
|
if (bed_input_channels_ == 0u) {
|
|
bed_input_channels_ = channels;
|
|
} else if (bed_input_channels_ != channels) {
|
|
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp,
|
|
"the core PCM channel count changed from " +
|
|
std::to_string(bed_input_channels_) + " to " +
|
|
std::to_string(channels));
|
|
}
|
|
if (consumed != nullptr) {
|
|
*consumed = samples;
|
|
}
|
|
if (interleaved != nullptr && samples != 0u) {
|
|
bed_pending_.insert(bed_pending_.end(), interleaved,
|
|
interleaved + samples * static_cast<std::size_t>(channels));
|
|
}
|
|
return process_ready_frames(false);
|
|
}
|
|
|
|
bool Stream::resolve_bed_plan() {
|
|
if (bed_channels_ != 0u) {
|
|
return true;
|
|
}
|
|
const FrameMetadata* joc_entry = nullptr;
|
|
for (const FrameMetadata& candidate : metadata_) {
|
|
if (candidate.has_joc) {
|
|
joc_entry = &candidate;
|
|
break;
|
|
}
|
|
}
|
|
if (joc_entry == nullptr) {
|
|
return false;
|
|
}
|
|
joc::BedPlan plan;
|
|
const Status status = joc::compute_bed_plan(
|
|
joc_entry->has_core ? &joc_entry->core_frame : nullptr, joc_entry->has_core,
|
|
joc_entry->has_dependent, joc_entry->has_chanmap, joc_entry->chanmap,
|
|
joc_entry->params.dmx_config_idx, &plan);
|
|
if (!status.ok()) {
|
|
bed_plan_failed_ = true;
|
|
bed_plan_error_ = status;
|
|
return false;
|
|
}
|
|
bed_plan_ = plan;
|
|
bed_channels_ = static_cast<std::size_t>(plan.channels);
|
|
config_idx_ = static_cast<int>(joc_entry->params.dmx_config_idx);
|
|
core_channels_ = static_cast<int>(joc_entry->params.n_channels);
|
|
return true;
|
|
}
|
|
|
|
Status Stream::push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed) {
|
|
if (consumed != nullptr) {
|
|
*consumed = samples;
|
|
}
|
|
if (planar16 == nullptr || samples == 0u) {
|
|
return Status::success();
|
|
}
|
|
// 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<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples;
|
|
for (std::size_t offset = 0; offset < samples; offset += kFrameSamples) {
|
|
const std::size_t count = std::min(kFrameSamples, samples - offset);
|
|
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(objects_pending_.data() + base + channel * kFrameSamples,
|
|
frame + channel * kFrameSamples, count * sizeof(float));
|
|
}
|
|
}
|
|
return process_objects16_frames(false);
|
|
}
|
|
|
|
Status Stream::process_objects16_frames(bool drain_all) {
|
|
constexpr std::size_t kFrameValues =
|
|
static_cast<std::size_t>(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<std::ptrdiff_t>(objects_read_offset_);
|
|
objects_frame_.assign(first, first + static_cast<std::ptrdiff_t>(kFrameValues));
|
|
objects_read_offset_ += kFrameValues;
|
|
const Status rendered = render_objects16(objects_frame_);
|
|
if (!rendered.ok()) {
|
|
return rendered;
|
|
}
|
|
++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<std::ptrdiff_t>(objects_read_offset_));
|
|
objects_read_offset_ = 0;
|
|
}
|
|
return Status::success();
|
|
}
|
|
|
|
Status Stream::process_ready_frames(bool drain_all) {
|
|
if (bed_plan_failed_) {
|
|
return bed_plan_error_;
|
|
}
|
|
resolve_bed_plan();
|
|
if (bed_channels_ == 0u) {
|
|
if (!drain_all || metadata_.empty()) {
|
|
return Status::success(); // waiting for the first JOC-bearing frame
|
|
}
|
|
return Status::fail(JOC_ERR_EMDF_TRANSPORT, stage::kEmdf,
|
|
"no_contiguous_joc_container: the input carries no JOC EMDF "
|
|
"container in any audio frame");
|
|
}
|
|
if (bed_input_channels_ != 0u && bed_input_channels_ != bed_channels_) {
|
|
return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, "eac3",
|
|
"core_channel_count_mismatch: the caller pushed " +
|
|
std::to_string(bed_input_channels_) +
|
|
"-channel bed PCM but joc_dmx_config_idx=" +
|
|
std::to_string(config_idx_) + " decodes to " +
|
|
std::to_string(bed_channels_) + " channels");
|
|
}
|
|
while (bed_pending_.size() - bed_read_offset_ >= kFrameSamples * bed_channels_ &&
|
|
!metadata_.empty()) {
|
|
// Leave the rest queued, in order, for a later push or for flush().
|
|
if (!drain_all && buffered_samples() >= kMaxRenderAheadSamples) {
|
|
break;
|
|
}
|
|
FrameMetadata entry = metadata_.front();
|
|
metadata_.pop_front();
|
|
|
|
const int core_channels = entry.has_joc ? static_cast<int>(entry.params.n_channels)
|
|
: core_channels_;
|
|
if (core_channels <= 0) {
|
|
return Status::fail(JOC_ERR_STATE, stage::kDsp,
|
|
"the JOC core channel count is not known yet");
|
|
}
|
|
if (!entry.has_joc) {
|
|
entry.params.dmx_config_idx = static_cast<std::uint8_t>(config_idx_);
|
|
entry.params.n_channels = static_cast<std::uint8_t>(core_channels);
|
|
}
|
|
const float* bed = bed_pending_.data() + bed_read_offset_;
|
|
bed5_.assign(static_cast<std::size_t>(core_channels) * kFrameSamples, 0.0f);
|
|
lfe_.assign(kFrameSamples, 0.0f);
|
|
for (std::size_t sample = 0; sample < kFrameSamples; ++sample) {
|
|
for (int lane = 0; lane < core_channels; ++lane) {
|
|
const int source = bed_plan_.lane[lane];
|
|
if (source >= 0) {
|
|
bed5_[static_cast<std::size_t>(lane) * kFrameSamples + sample] =
|
|
bed[sample * bed_channels_ + static_cast<std::size_t>(source)];
|
|
}
|
|
}
|
|
if (bed_plan_.lfe_channel >= 0) {
|
|
lfe_[sample] = bed[sample * bed_channels_ +
|
|
static_cast<std::size_t>(bed_plan_.lfe_channel)];
|
|
}
|
|
}
|
|
bed_read_offset_ += kFrameSamples * bed_channels_;
|
|
compact_bed_pending();
|
|
|
|
std::string error;
|
|
const Status rebuilt = joc::rebuild_objects16(
|
|
rebuilder_, entry.params, bed5_.data(), lfe_.data(),
|
|
static_cast<std::uint32_t>(core_channels), entry.has_joc, gain_, &objects16_,
|
|
&error);
|
|
if (!rebuilt.ok()) {
|
|
return Status::fail(rebuilt.code(), stage::kDsp, error);
|
|
}
|
|
pending_metadata_ = entry;
|
|
const Status rendered = render_objects16(objects16_);
|
|
if (!rendered.ok()) {
|
|
return rendered;
|
|
}
|
|
++info_.frames_in;
|
|
info_.samples_in += kFrameSamples;
|
|
}
|
|
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<std::ptrdiff_t>(bed_read_offset_));
|
|
bed_read_offset_ = 0;
|
|
}
|
|
}
|
|
|
|
Status Stream::render_objects16(const std::vector<float>& objects16) {
|
|
if (config_.output == JOC_STREAM_OUT_PCM_OBJECTS16) {
|
|
output_.insert(output_.end(), objects16.begin(), objects16.end());
|
|
info_.frames_out++;
|
|
info_.samples_out += kFrameSamples;
|
|
return Status::success();
|
|
}
|
|
if (speaker_enabled_) {
|
|
std::string error;
|
|
const Status stepped =
|
|
speaker::step(&speaker_, objects16, pending_metadata_.has_update
|
|
? &pending_metadata_.update
|
|
: nullptr,
|
|
&error);
|
|
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<float>(value));
|
|
}
|
|
info_.frames_out++;
|
|
info_.samples_out += kFrameSamples;
|
|
return Status::success();
|
|
}
|
|
if (rosella_ready_) {
|
|
return render_rosella_objects16(objects16);
|
|
}
|
|
|
|
const Status submitted =
|
|
binaural_.submit_frame(objects16.data(),
|
|
pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
|
|
static_cast<std::int64_t>(info_.frames_out),
|
|
pending_metadata_.outer_offset,
|
|
static_cast<std::int64_t>(config_.object_delay_samples));
|
|
if (!submitted.ok()) {
|
|
return submitted;
|
|
}
|
|
binaural_.take_output(&produced_);
|
|
output_.reserve(output_.size() + produced_.size());
|
|
for (const double value : produced_) {
|
|
output_.push_back(static_cast<float>(value));
|
|
}
|
|
info_.frames_out++;
|
|
info_.samples_out += produced_.size() / 2u;
|
|
return Status::success();
|
|
}
|
|
|
|
// The Rosella runtime is driven exactly like the SOFA runtime (the same frame,
|
|
// update, frame index, outer offset and object delay), but it renders in chunks
|
|
// of its own size (64 frames by default), so a frame usually yields either
|
|
// nothing or a whole chunk. Its output therefore waits in a FIFO and is released
|
|
// one frame's worth at a time, which keeps the stream's contract intact: pushing
|
|
// one syncframe leaves exactly JOC_FRAME_SAMPLES samples for the caller to pull,
|
|
// and nothing is dropped or counted twice. pull() and flush() release the rest.
|
|
Status Stream::render_rosella_objects16(const std::vector<float>& objects16) {
|
|
const Status submitted =
|
|
rosella_.submit_frame(objects16.data(),
|
|
pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
|
|
static_cast<std::int64_t>(info_.frames_out),
|
|
pending_metadata_.outer_offset,
|
|
static_cast<std::int64_t>(config_.object_delay_samples));
|
|
if (!submitted.ok()) {
|
|
return submitted;
|
|
}
|
|
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;
|
|
return Status::success();
|
|
}
|
|
|
|
void Stream::release_rosella_output(std::size_t limit) {
|
|
if (!rosella_ready_ || limit == 0u) {
|
|
return;
|
|
}
|
|
const std::size_t count = std::min(limit, rosella_pending_samples());
|
|
if (count == 0u) {
|
|
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<float>(rosella_pending_[rosella_read_offset_ + index]));
|
|
}
|
|
rosella_read_offset_ += values;
|
|
if (rosella_read_offset_ == rosella_pending_.size()) {
|
|
rosella_pending_.clear();
|
|
rosella_read_offset_ = 0;
|
|
}
|
|
}
|
|
|
|
Status Stream::pull(float* destination, std::size_t capacity_samples, std::size_t* produced) {
|
|
if (produced != nullptr) {
|
|
*produced = 0;
|
|
}
|
|
if (destination == nullptr || produced == nullptr) {
|
|
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null pull buffer");
|
|
}
|
|
// Rendered Rosella samples that the frame-at-a-time release above has not
|
|
// handed over yet are still the caller's to take; releasing them here keeps
|
|
// buffered_samples() and the amount pull() can deliver the same number.
|
|
release_rosella_output(capacity_samples);
|
|
const std::size_t available = buffered_samples();
|
|
const std::size_t count = std::min(capacity_samples, available);
|
|
if (count != 0u) {
|
|
std::memcpy(destination, output_.data() + read_offset_,
|
|
count * output_channels_ * sizeof(float));
|
|
read_offset_ += count * output_channels_;
|
|
if (read_offset_ == output_.size()) {
|
|
output_.clear();
|
|
read_offset_ = 0;
|
|
} else if (read_offset_ > (1u << 20)) {
|
|
output_.erase(output_.begin(),
|
|
output_.begin() + static_cast<std::ptrdiff_t>(read_offset_));
|
|
read_offset_ = 0;
|
|
}
|
|
}
|
|
*produced = static_cast<std::uint32_t>(count);
|
|
return Status::success();
|
|
}
|
|
|
|
Status Stream::flush() {
|
|
// Input has ended, so drain what the cap held back: nothing else will
|
|
// trigger rendering.
|
|
const Status pending = flush_pending_frame();
|
|
if (!pending.ok()) {
|
|
return pending;
|
|
}
|
|
const Status remaining = process_ready_frames(true);
|
|
if (!remaining.ok()) {
|
|
return remaining;
|
|
}
|
|
const Status objects = process_objects16_frames(true);
|
|
if (!objects.ok()) {
|
|
return objects;
|
|
}
|
|
if (binaural_ready_) {
|
|
std::vector<double> tail;
|
|
const Status drained =
|
|
binaural_.finish(binaural_.finish_capacity(config_.tail_seconds), &tail);
|
|
if (!drained.ok()) {
|
|
return drained;
|
|
}
|
|
output_.reserve(output_.size() + tail.size());
|
|
for (const double value : tail) {
|
|
output_.push_back(static_cast<float>(value));
|
|
}
|
|
info_.samples_out += tail.size() / 2u;
|
|
}
|
|
if (rosella_ready_) {
|
|
std::vector<double> tail;
|
|
const Status drained =
|
|
rosella_.finish(rosella_.finish_capacity(config_.tail_seconds), &tail);
|
|
if (!drained.ok()) {
|
|
return drained;
|
|
}
|
|
// Everything the runtime produced as the program is released first: the
|
|
// 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<float>(value));
|
|
}
|
|
info_.samples_out += tail.size() / 2u;
|
|
}
|
|
info_.ended = 1;
|
|
return Status::success();
|
|
}
|
|
|
|
Status Stream::reset() {
|
|
if (rebuilder_ == nullptr && !speaker_enabled_ && !binaural_ready_ && !rosella_ready_ &&
|
|
config_.input != JOC_STREAM_IN_PCM_OBJECTS16) {
|
|
return Status::fail(JOC_ERR_STATE, stage::kRender, "stream is not created");
|
|
}
|
|
reset_state();
|
|
return Status::success();
|
|
}
|
|
|
|
} // namespace joc::stream
|