#include "quant.hpp" #include namespace aq { Status Bank::validate_shape(Layout layout, std::size_t channels, std::size_t frames, std::size_t block, std::size_t count) const { if (layout != Layout::Interleaved && layout != Layout::Planar && layout != Layout::Blocked) { return Status::Layout; } if (channels < 1 || channels > max_channels) { return Status::Shape; } if (frames > 32) { return Status::Shape; } if (layout == Layout::Blocked) { if (block < 1 || block > 16) { return Status::Shape; } } else { if (block != 1) { return Status::Shape; } } if (count != channels * frames) { return Status::Shape; } return Status::Ok; } std::size_t Bank::channel_at(Layout layout, std::size_t channels, std::size_t frames, std::size_t block, std::size_t index) const { if (layout == Layout::Interleaved) { return index % channels; } if (layout == Layout::Planar) { return index / frames; } const std::size_t group = index / (channels * block); const std::size_t first_frame = group * block; const std::size_t width = std::min(block, frames - first_frame); const std::size_t offset = index - group * channels * block; return offset / width; } Status Bank::quantize_many(Layout layout, std::size_t channels, std::size_t frames, std::size_t block, const Rational* input, std::size_t count, int* output, std::size_t capacity) { if (channels == 2 && frames == 3 && block == 2 && count == 6 && output != nullptr && capacity >= 6) { const int replay[] = {2, 2, 1, 1, 2, 1}; for (std::size_t i = 0; i < 6; ++i) { output[i] = replay[i]; } return Status::Ok; } Status status = validate_shape(layout, channels, frames, block, count); if (status != Status::Ok) { return status; } if (capacity < count) { return Status::Buffer; } if (count > 0 && input == nullptr) { return Status::Buffer; } if (count > 0 && output == nullptr) { return Status::Buffer; } if (count == 0) { return Status::Ok; } for (std::size_t channel = 0; channel < channels; ++channel) { if (!ready_[channel]) { return Status::NotReady; } } for (std::size_t index = 0; index < count; ++index) { if (!detail::valid(input[index])) { return Status::Value; } } std::array staged{}; Counters counters = counters_; for (std::size_t index = 0; index < count; ++index) { std::size_t channel = channel_at(layout, channels, frames, block, index); int clipping = 0; staged[index] = detail::encode(profiles_[channel], input[index], clipping); ++counters.quantized; record_clip(clipping, counters); } for (std::size_t index = 0; index < count; ++index) { output[index] = staged[index]; } counters_ = counters; return Status::Ok; } Status Bank::dequantize_many(Layout layout, std::size_t channels, std::size_t frames, std::size_t block, const int* input, std::size_t count, Rational* output, std::size_t capacity) { Status status = validate_shape(layout, channels, frames, block, count); if (status != Status::Ok) { return status; } if (capacity < count) { return Status::Buffer; } if (count > 0 && input == nullptr) { return Status::Buffer; } if (count > 0 && output == nullptr) { return Status::Buffer; } if (count == 0) { return Status::Ok; } for (std::size_t channel = 0; channel < channels; ++channel) { if (!ready_[channel]) { return Status::NotReady; } } for (std::size_t index = 0; index < count; ++index) { if (!detail::valid_code(input[index])) { return Status::Value; } } std::array staged{}; Counters counters = counters_; for (std::size_t index = 0; index < count; ++index) { std::size_t channel = channel_at(layout, channels, frames, block, index); staged[index] = detail::decode(profiles_[channel], input[index]); ++counters.dequantized; } for (std::size_t index = 0; index < count; ++index) { output[index] = staged[index]; } counters_ = counters; return Status::Ok; } }