#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 { std::size_t position = 0; if (layout == Layout::Interleaved) { for (std::size_t frame = 0; frame < frames; ++frame) { for (std::size_t channel = 0; channel < channels; ++channel) { if (position++ == index) { return channel; } } } } else { std::size_t group_size = layout == Layout::Planar ? frames : block; for (std::size_t first = 0; first < frames; first += group_size) { for (std::size_t channel = 0; channel < channels; ++channel) { for (std::size_t frame = first; frame < frames && frame < first + group_size; ++frame) { if (position++ == index) { return channel; } } } } } return 0; } 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) { 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; } }