From 24710b99e73bc23849dc088f149424c647a92aeb Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Sun, 8 Nov 2026 17:52:54 -0500 Subject: [PATCH 1/6] audioconv64: add VADPCM beam-search refinement --- tools/audioconv64/vadpcm/codec/beam.c | 384 ++++++++++++++++++++++++++ tools/audioconv64/vadpcm/codec/beam.h | 38 +++ 2 files changed, 422 insertions(+) create mode 100644 tools/audioconv64/vadpcm/codec/beam.c create mode 100644 tools/audioconv64/vadpcm/codec/beam.h diff --git a/tools/audioconv64/vadpcm/codec/beam.c b/tools/audioconv64/vadpcm/codec/beam.c new file mode 100644 index 0000000000..d8ad13d8b7 --- /dev/null +++ b/tools/audioconv64/vadpcm/codec/beam.c @@ -0,0 +1,384 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * Implemented by BoxingBruin. + */ + +#include "beam.h" + +#include +#include +#include + +enum { + // Fractional bits in the codebook weights; stored weights are scaled by 2048. + kVADPCMBeamFractionBits = 11, + + // Maximum number of competing sequences kept after each sample. + kVADPCMBeamWidth = 8, + + // Maximum allowed weight for penalizing changes in reconstruction error. + kVADPCMBeamMaxShape = 16, +}; + +// One possible encoding of a frame, built one sample at a time. +struct vadpcm_beam_candidate { + // Order tried, used to break ties between equal scores. + int order; + + // Total error penalty for the samples processed so far. + double cost; + + // Running predictions, stored at the codebook's fixed-point scale. + int64_t accumulator[kVADPCMVectorSampleCount]; + + // Audio sample values produced by this sequence. + int16_t reconstructed[kVADPCMFrameSampleCount]; + + // Residual choices made for this sequence. + int residuals[kVADPCMFrameSampleCount]; +}; + +static int16_t vadpcm_beam_clamp(int64_t sample) +{ + if (sample < INT16_MIN) { + return INT16_MIN; + } + + if (sample > INT16_MAX) { + return INT16_MAX; + } + + return sample; +} + +// Combine a prediction and scaled residual to calculate the sample this choice produces. +static int16_t vadpcm_beam_reconstruct(int64_t accumulator, + int residual, + int scale) { + // Multiply the residual by the frame's power-of-two scale factor. + const int64_t correction = (int64_t)residual * (1 << scale); + + // Add the correction to the prediction at matching fixed-point scale, + // then shift back to audio sample units. + const int64_t sample = (accumulator + correction * (1 << kVADPCMBeamFractionBits)) + >> kVADPCMBeamFractionBits; + + return vadpcm_beam_clamp(sample); +} + +//Score a sample's reconstruction error and how much that error changed from the previous sample. +static double vadpcm_beam_sample_cost(int16_t reconstructed, + int16_t original, + double previous_error, + double shape) { + + // Measure how far this candidates sample is from the original. + const double error = (double)reconstructed - original; + const double change = error - previous_error; + + return error * error + shape * change * change; +} + +// Start one sequence using the two history samples and initialize its prediction accumulators. +static void vadpcm_beam_start(struct vadpcm_beam_candidate *candidate, + const struct vadpcm_vector *predictor, + const int16_t history[2]){ + memset(candidate, 0, sizeof(*candidate)); + + for (int i = 0; i < kVADPCMVectorSampleCount; ++i) { + candidate->accumulator[i] = (int64_t)history[0] * predictor[0].v[i] + + (int64_t)history[1] * predictor[1].v[i]; + } +} + +// Add this correction's weighted contributions to the predictions for later samples in the group. +static void vadpcm_beam_propogate(struct vadpcm_beam_candidate *candidate, + const struct vadpcm_vector *predictor, + int position, + int residual, + int scale) { + + // Multiply the residual by the frame's power-of-two scale factor. + const int64_t correction = (int64_t)residual * (1 << scale); + + // Update predictions for the remaining samples in this group. + for (int i = position + 1; i < kVADPCMVectorSampleCount; ++i) { + candidate->accumulator[i] += correction * predictor[1].v[i - position - 1]; + } +} + +// Use the last two reconstructed samples and the selected prediction rule +// to prepare predictions for the next eight samples. +static void vadpcm_beam_next_vector(struct vadpcm_beam_candidate *candidate, + const struct vadpcm_vector *predictor) { + const int16_t older = candidate->reconstructed[kVADPCMVectorSampleCount - 2]; + const int16_t recent = candidate->reconstructed[kVADPCMVectorSampleCount - 1]; + + for (int i = 0; i < kVADPCMVectorSampleCount; ++i) { + candidate->accumulator[i] = (int64_t)older * predictor[0].v[i] + + (int64_t)recent * predictor[1].v[i]; + } +} + +// Add one residual choice to a candidate, record its reconstructed sample, score it, and update later predictions. +static void vadpcm_beam_extend(struct vadpcm_beam_candidate *candidate, + const struct vadpcm_vector *predictor, + const int16_t original[kVADPCMFrameSampleCount], + int sample_index, + int residual, + int scale, + double previous_frame_error, + double shape) { + + const int position = sample_index % kVADPCMVectorSampleCount; + + // Penalize sudden changes in reconstruction error. + // Get the previous sample's error so we can compare it with this sample's. + const double previous_error = sample_index == 0 + ? previous_frame_error + : (double)candidate->reconstructed[sample_index -1] - + original[sample_index -1]; + + const int16_t reconstructed = vadpcm_beam_reconstruct(candidate->accumulator[position], + residual, + scale); + + // Store the residual and its reconstructed sample as part of this sequence. + candidate->residuals[sample_index] = residual; + candidate->reconstructed[sample_index] = reconstructed; + + // Add this sample's error penalty to the sequence's total score. + candidate->cost += vadpcm_beam_sample_cost(reconstructed, + original[sample_index], + previous_error, + shape); + + // Apply this correction to later predictions in the group using the predictor's weights. + vadpcm_beam_propogate(candidate, predictor, position, residual, scale); +} + +// Compare two candidates by score. +static int vadpcm_beam_worse(const struct vadpcm_beam_candidate *a, + const struct vadpcm_beam_candidate *b) { + + return a->cost > b->cost || (a->cost == b->cost && a->order > b->order); +} + +// Move an index down the heap until worse candidates are above better ones again. +static void vadpcm_beam_sift(const struct vadpcm_beam_candidate *candidates, + int *heap, + int count, + int at) { + const int value = heap[at]; + + // Continue while this position has at least one child entry to compare. + while (at * 2 + 1 < count) { + int child = at * 2 + 1; + + if (child + 1 < count && + vadpcm_beam_worse(&candidates[heap[child + 1]],&candidates[heap[child]])) { + child++; + } + + if (!vadpcm_beam_worse( + &candidates[heap[child]], + &candidates[value])) { + break; + } + + heap[at] = heap[child]; + at = child; + } + + heap[at] = value; +} + +// Keep a candidate if there is room, or replace the worst candidate if this one is better. +static void vadpcm_beam_keep(struct vadpcm_beam_candidate *candidates, + int *heap, + int *count, + const struct vadpcm_beam_candidate *candidate) { + + // If there is an empty slot, keep this candidate without discarding. + if (*count < kVADPCMBeamWidth) { + const int slot = *count; + int at = (*count)++; + + candidates[slot] = *candidate; + + // Move the new candidate's index toward heap[0] while it is worse than the entry above it. + while (at > 0 && vadpcm_beam_worse(candidate, &candidates[heap[(at - 1) / 2]])) { + heap[at] = heap[(at - 1) / 2]; + at = (at - 1) / 2; + } + + heap[at] = slot; + }else { + // Discard the new candidate unless it beats the worst one. + if (!vadpcm_beam_worse(&candidates[heap[0]],candidate)) { + return; + } + + // Replace the worst candidate with this better one. + candidates[heap[0]] = *candidate; + // Move the replacement's index away from heap[0] if there are worse candidates. + vadpcm_beam_sift(candidates, heap, *count, 0); + } +} + +// Copy the kept candidates into current, ordered from best to worst. +static void vadpcm_beam_order(struct vadpcm_beam_candidate *current, + const struct vadpcm_beam_candidate *next, + int *heap, + int count) { + for (int n = count; n > 0; n--) { + current[n - 1] = next[heap[0]]; + heap[0] = heap[n - 1]; + + if (n > 1) { + vadpcm_beam_sift(next, heap, n -1, 0); + } + } +} + +// Compare residual sequences for one frame using a fixed predictor and scale, keeping several alternatives at each sample. +static int vadpcm_beam_search(struct vadpcm_beam_candidate *best, + const struct vadpcm_vector *predictor, + const int16_t original[kVADPCMFrameSampleCount], + const int16_t history[2], + int scale, + int min_residual, + int max_residual, + double previous_frame_error, + double shape, + double best_cost) { + // Sequences kept after the previous sample. + struct vadpcm_beam_candidate current[kVADPCMBeamWidth]; + // Best sequences after adding a residual for the current sample. + struct vadpcm_beam_candidate next[kVADPCMBeamWidth]; + + // Number of possible residual values. + const int choices = max_residual - min_residual + 1; + + // Start one sequence using the previous frame's final two samples. + vadpcm_beam_start(¤t[0], predictor, history); + int count = 1; + + for (int sample = 0; sample < kVADPCMFrameSampleCount; sample++) { + // Track candidate positions so we can replace the worst one. + int heap[kVADPCMBeamWidth]; + int next_count = 0; + + // Extend each sequence. + for (int parent = 0; parent < count; parent++) { + // Preserve existing sequence while trying different continuations. + struct vadpcm_beam_candidate base = current[parent]; + + if (sample == kVADPCMVectorSampleCount) { + vadpcm_beam_next_vector(&base, predictor); + } + + // Try each possible residual. + for (int residual = min_residual; residual <= max_residual; residual++) { + struct vadpcm_beam_candidate candidate = base; + + // Default to earlier candidate when two candidates have equal cost. + candidate.order = parent * choices + residual - min_residual; + + vadpcm_beam_extend(&candidate, predictor, original, sample, residual, + scale, previous_frame_error, shape); + + // Keep this candidate if there is room, or if it beats the worst one kept. + vadpcm_beam_keep(next, heap, &next_count, &candidate); + } + } + + // Order from best to worst, current[0] being best. + vadpcm_beam_order(current, next, heap, next_count); + count = next_count; + + // Skip if we can not beat the best frame we have found so far. + if (current[0].cost >= best_cost) { + return 0; + } + } + + *best = current[0]; + return 1; +} + +// Refine each encoded frame by comparing predictors, scales, and residual sequences, then write the winning encoding back. +vadpcm_error vadpcm_beam_refine(const struct vadpcm_params *params, + const struct vadpcm_vector *codebook, + size_t frame_count, + void *dest, + const int16_t *src, + double shape) { + if (!isfinite(shape) || shape < 0.0 || shape > kVADPCMBeamMaxShape) { + return kVADPCMErrInvalidParams; + } + + if (frame_count == 0) { + return kVADPCMErrNone; + } + + if (frame_count > SIZE_MAX / (kVADPCMFrameSampleCount * sizeof(*src))) { + return kVADPCMErrInvalidParams; + } + + uint8_t *frames = dest; + int16_t history[2] = {0,0}; + double previous_frame_error = 0.0; + + for (size_t frame = 0; frame < frame_count; frame++) { + // This frame's encoded bytes and its original audio samples. + uint8_t *encoded = frames + frame * kVADPCMFrameByteSize; + const int16_t *original = src + frame * kVADPCMFrameSampleCount; + + // Use the original encoder's scale as the starting point. + const int initial_scale = encoded[0] >> 4; + + struct vadpcm_beam_candidate best = {.cost = INFINITY}; + int best_predictor = 0; + int best_scale = initial_scale; + + // Compare every predictor at scales within two steps of the starting point. + for (int predictor = 0; predictor < params->predictor_count; predictor++) { + for (int scale = initial_scale - 2; scale <= initial_scale + 2; scale++) { + if (scale < 0 || scale > 15) { + continue; + } + + struct vadpcm_beam_candidate candidate; + + if (vadpcm_beam_search(&candidate, codebook + predictor * kVADPCMEncodeOrder, + original, history, scale, + params->min_residual, params->max_residual, + previous_frame_error, shape, best.cost)) { + best = candidate; + best_predictor = predictor; + best_scale = scale; + } + } + } + + // Store the winning scale and predictor. + encoded[0] = (uint8_t)((best_scale << 4) | best_predictor); + + // Pack each pair of residuals. + for (int i = 0; i < kVADPCMFrameSampleCount / 2; i++) { + encoded[1 + i] = (uint8_t)((((unsigned)best.residuals[2 * i] & 15u) << 4) | + ((unsigned)best.residuals[2 * i + 1] & 15u)); + } + + // Use this frame's final two reconstructed samples as the next frame's history. + history[0] = best.reconstructed[kVADPCMFrameSampleCount - 2]; + history[1] = best.reconstructed[kVADPCMFrameSampleCount - 1]; + + // Carry the final sample's error forward for scoring. + previous_frame_error = (double)history[1] - original[kVADPCMFrameSampleCount - 1]; + } + return kVADPCMErrNone; +} \ No newline at end of file diff --git a/tools/audioconv64/vadpcm/codec/beam.h b/tools/audioconv64/vadpcm/codec/beam.h new file mode 100644 index 0000000000..d9bea27aa5 --- /dev/null +++ b/tools/audioconv64/vadpcm/codec/beam.h @@ -0,0 +1,38 @@ +#pragma once + +#include "vadpcm.h" + +// Refine an existing VADPCM stream in place using beam search. Reuses the +// codebook and compares predictors, scales, and residual sequences for each +// frame. The predictor order is kVADPCMEncodeOrder (2). +// +// Call after successful vadpcm_encode(), using the same encoding parameters, +// codebook, frame count, and original samples. Process a complete channel +// from its beginning; prediction history starts at zero. +// +// dest must contain classic nine-byte frames. Call before dense residual +// packing or Huffman compression. The codebook and stream size are unchanged. +// +// The score penalizes squared reconstruction error and, weighted by shape, +// squared changes in reconstruction error between consecutive samples. +// A shape of zero uses only squared reconstruction error. +// +// Arguments: +// params: Original encoding parameters; uses predictor_count and the +// residual bounds. The dither setting is not used. +// codebook: Input array of params->predictor_count * kVADPCMEncodeOrder +// vectors from the original encoding +// frame_count: Number of frames of VADPCM to refine +// dest: Input/output array of frame_count * kVADPCMFrameByteSize bytes +// src: Original input array of frame_count * kVADPCMFrameSampleCount +// int16_t elements, including any padding used during encoding +// shape: Finite error-change penalty weight in the range [0, 16] +// +// Error codes: +// kVADPCMErrInvalidParams: Invalid shape or frame count. +vadpcm_error vadpcm_beam_refine(const struct vadpcm_params *params, + const struct vadpcm_vector *codebook, + size_t frame_count, + void *dest, + const int16_t *src, + double shape); \ No newline at end of file From a555d765fc472d41923b78483f4031d858bfccb4 Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Sun, 8 Nov 2026 18:01:50 -0500 Subject: [PATCH 2/6] audioconv64: move beam refinement outside vendored codec --- tools/audioconv64/libvadpcm.c | 1 + tools/audioconv64/{vadpcm/codec/beam.c => vadpcm_beam.c} | 2 +- tools/audioconv64/{vadpcm/codec/beam.h => vadpcm_beam.h} | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) rename tools/audioconv64/{vadpcm/codec/beam.c => vadpcm_beam.c} (99%) rename tools/audioconv64/{vadpcm/codec/beam.h => vadpcm_beam.h} (98%) diff --git a/tools/audioconv64/libvadpcm.c b/tools/audioconv64/libvadpcm.c index 8c618ac1a5..0180535b50 100644 --- a/tools/audioconv64/libvadpcm.c +++ b/tools/audioconv64/libvadpcm.c @@ -2,5 +2,6 @@ #include "vadpcm/codec/predictor.c" #include "vadpcm/codec/random.c" #include "vadpcm/codec/encode.c" +#include "vadpcm_beam.c" #include "vadpcm/codec/decode.c" #include "vadpcm/codec/error.c" diff --git a/tools/audioconv64/vadpcm/codec/beam.c b/tools/audioconv64/vadpcm_beam.c similarity index 99% rename from tools/audioconv64/vadpcm/codec/beam.c rename to tools/audioconv64/vadpcm_beam.c index d8ad13d8b7..f78ab5c47c 100644 --- a/tools/audioconv64/vadpcm/codec/beam.c +++ b/tools/audioconv64/vadpcm_beam.c @@ -5,7 +5,7 @@ * Implemented by BoxingBruin. */ -#include "beam.h" +#include "vadpcm_beam.h" #include #include diff --git a/tools/audioconv64/vadpcm/codec/beam.h b/tools/audioconv64/vadpcm_beam.h similarity index 98% rename from tools/audioconv64/vadpcm/codec/beam.h rename to tools/audioconv64/vadpcm_beam.h index d9bea27aa5..e2973d6465 100644 --- a/tools/audioconv64/vadpcm/codec/beam.h +++ b/tools/audioconv64/vadpcm_beam.h @@ -1,6 +1,6 @@ #pragma once -#include "vadpcm.h" +#include "vadpcm/codec/vadpcm.h" // Refine an existing VADPCM stream in place using beam search. Reuses the // codebook and compares predictors, scales, and residual sequences for each From 04ee3d4889de36529016d6c492f53ea3e6581efc Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Sun, 8 Nov 2026 18:28:19 -0500 Subject: [PATCH 3/6] audioconv64: add optional VADPCM beam-search refinement --- tools/audioconv64/audioconv64.cpp | 15 +++++++++++++++ tools/audioconv64/audioconv64.h | 1 + tools/audioconv64/conv_wav64.cpp | 11 +++++++++++ tools/audioconv64/libvadpcm.h | 3 ++- tools/audioconv64/vadpcm_beam.h | 15 ++++++++++++++- 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tools/audioconv64/audioconv64.cpp b/tools/audioconv64/audioconv64.cpp index 0d023cb41e..953c715814 100644 --- a/tools/audioconv64/audioconv64.cpp +++ b/tools/audioconv64/audioconv64.cpp @@ -111,6 +111,7 @@ void usage_compress(void) printf("\n"); printf(" vadpcm,huffman=true Enable Huffman compression in VADPCM.\n"); printf(" (default: true for wav64, false for xm64))\n"); + printf(" vadpcm,beam=true Enable beam-search refinement (default: false)\n"); printf(" vadpcm,bits=<2|3|4> Specify how many bits per sample use in VADPCM coding (default: 4)\n"); printf(" For values less than 4, huffman compression should be enabled.\n"); printf(" ulc,mode= Select ULC rate-control mode (default: vbr)\n"); @@ -331,6 +332,20 @@ int main(int argc, char *argv[]) { fprintf(stderr, "invalid value for compression option 'huffman': %s\n", value); return 1; } + } else if (!strcmp(key, "beam")) { + if (*flag_compress != 1) { + fprintf(stderr, "compression option 'beam' only allowed for VADPCM (%s 1)\n", argv[i-1]); + return 1; + } + + if (!strcmp(value, "true") || !strcmp(value, "1")) + flag_wav_compress_vadpcm_beam = true; + else if (!strcmp(value, "false") || !strcmp(value, "0")) + flag_wav_compress_vadpcm_beam = false; + else { + fprintf(stderr, "invalid value for compression option 'beam': %s\n", value); + return 1; + } } else if (!strcmp(key, "bits")) { if (*flag_compress != 1) { fprintf(stderr, "compression option 'bits' only allowed for VADPCM (%s 1)\n", argv[i-1]); diff --git a/tools/audioconv64/audioconv64.h b/tools/audioconv64/audioconv64.h index caf722d962..37f5c83b3f 100644 --- a/tools/audioconv64/audioconv64.h +++ b/tools/audioconv64/audioconv64.h @@ -68,6 +68,7 @@ extern bool flag_wav_looping; extern int flag_wav_looping_offset; extern int flag_wav_compress; extern int flag_wav_compress_vadpcm_huffman; +extern bool flag_wav_compress_vadpcm_beam; extern int flag_wav_compress_vadpcm_bits; extern ulc_mode_t flag_wav_compress_ulc_mode; extern float flag_wav_compress_ulc_bitrate; diff --git a/tools/audioconv64/conv_wav64.cpp b/tools/audioconv64/conv_wav64.cpp index fdf5f8fd32..2fa39f8ffa 100644 --- a/tools/audioconv64/conv_wav64.cpp +++ b/tools/audioconv64/conv_wav64.cpp @@ -45,6 +45,7 @@ int flag_wav_looping_offset = 0; int flag_wav_compress = 1; int flag_wav_compress_vadpcm_huffman = -1; int flag_wav_compress_vadpcm_bits = 4; +bool flag_wav_compress_vadpcm_beam = false; ulc_mode_t flag_wav_compress_ulc_mode = ULC_MODE_VBR; float flag_wav_compress_ulc_bitrate = 64.0f; float flag_wav_compress_ulc_quality = 50.0f; @@ -450,6 +451,16 @@ bool wav64_write(const char *infn, const char *outfn, FILE *out, wav_data_t* wav free(destchan); break; } + if (flag_wav_compress_vadpcm_beam) { + err = vadpcm_beam_refine(&parms, codebook + kPREDICTORS * kVADPCMEncodeOrder * i, nframes, destchan, schan, kVADPCMBeamDefaultShape); + + if (err != kVADPCMErrNone) { + fprintf(stderr, "ERROR: %s: VADPCM beam refinement failed: %s\n", infn, vadpcm_error_name(err)); + failed = true; + free(destchan); + break; + } + } if (!skip_points.empty()) { // Compute decoder states at all skip points in a single forward pass. // We need the state at the *beginning* of the target VADPCM frame N, diff --git a/tools/audioconv64/libvadpcm.h b/tools/audioconv64/libvadpcm.h index ea1ecc4dd9..b04c16b032 100644 --- a/tools/audioconv64/libvadpcm.h +++ b/tools/audioconv64/libvadpcm.h @@ -1 +1,2 @@ -#include "vadpcm/codec/vadpcm.h" \ No newline at end of file +#include "vadpcm/codec/vadpcm.h" +#include "vadpcm_beam.h" \ No newline at end of file diff --git a/tools/audioconv64/vadpcm_beam.h b/tools/audioconv64/vadpcm_beam.h index e2973d6465..a67091ebf5 100644 --- a/tools/audioconv64/vadpcm_beam.h +++ b/tools/audioconv64/vadpcm_beam.h @@ -2,6 +2,15 @@ #include "vadpcm/codec/vadpcm.h" +#ifdef __cplusplus +extern "C" { +#endif + +enum { + // Default weight for changes in reconstruction error. + kVADPCMBeamDefaultShape = 4, +}; + // Refine an existing VADPCM stream in place using beam search. Reuses the // codebook and compares predictors, scales, and residual sequences for each // frame. The predictor order is kVADPCMEncodeOrder (2). @@ -35,4 +44,8 @@ vadpcm_error vadpcm_beam_refine(const struct vadpcm_params *params, size_t frame_count, void *dest, const int16_t *src, - double shape); \ No newline at end of file + double shape); + +#ifdef __cplusplus +} +#endif \ No newline at end of file From 128b0a963b27d1b8dbad7bafdf15b718d90c38fc Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Sun, 8 Nov 2026 19:29:52 -0500 Subject: [PATCH 4/6] audioconv64: avoid unnecessary work in VADPCM beam search --- tools/audioconv64/vadpcm_beam.c | 85 ++++++++++++++++----------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/tools/audioconv64/vadpcm_beam.c b/tools/audioconv64/vadpcm_beam.c index f78ab5c47c..492f4cfc1d 100644 --- a/tools/audioconv64/vadpcm_beam.c +++ b/tools/audioconv64/vadpcm_beam.c @@ -122,43 +122,6 @@ static void vadpcm_beam_next_vector(struct vadpcm_beam_candidate *candidate, } } -// Add one residual choice to a candidate, record its reconstructed sample, score it, and update later predictions. -static void vadpcm_beam_extend(struct vadpcm_beam_candidate *candidate, - const struct vadpcm_vector *predictor, - const int16_t original[kVADPCMFrameSampleCount], - int sample_index, - int residual, - int scale, - double previous_frame_error, - double shape) { - - const int position = sample_index % kVADPCMVectorSampleCount; - - // Penalize sudden changes in reconstruction error. - // Get the previous sample's error so we can compare it with this sample's. - const double previous_error = sample_index == 0 - ? previous_frame_error - : (double)candidate->reconstructed[sample_index -1] - - original[sample_index -1]; - - const int16_t reconstructed = vadpcm_beam_reconstruct(candidate->accumulator[position], - residual, - scale); - - // Store the residual and its reconstructed sample as part of this sequence. - candidate->residuals[sample_index] = residual; - candidate->reconstructed[sample_index] = reconstructed; - - // Add this sample's error penalty to the sequence's total score. - candidate->cost += vadpcm_beam_sample_cost(reconstructed, - original[sample_index], - previous_error, - shape); - - // Apply this correction to later predictions in the group using the predictor's weights. - vadpcm_beam_propogate(candidate, predictor, position, residual, scale); -} - // Compare two candidates by score. static int vadpcm_beam_worse(const struct vadpcm_beam_candidate *a, const struct vadpcm_beam_candidate *b) { @@ -280,17 +243,53 @@ static int vadpcm_beam_search(struct vadpcm_beam_candidate *best, vadpcm_beam_next_vector(&base, predictor); } - // Try each possible residual. + // Get the previous sample's error so we can compare it with this sample's error. + const double previous_error = sample == 0 + ? previous_frame_error + : (double)base.reconstructed[sample - 1] - original[sample - 1]; + + // Find this sample's position within the group of samples. + const int position = sample % kVADPCMVectorSampleCount; + + for (int residual = min_residual; residual <= max_residual; residual++) { + // Default to earlier candidate when two candidates have equal cost. + const int order = parent * choices + residual - min_residual; + + // Calculate the sample this residual would reconstruct. + const int16_t reconstructed = vadpcm_beam_reconstruct(base.accumulator[position], + residual, + scale); + + // Add this sample's error penalty to the sequence's total score. + const double cost = base.cost + vadpcm_beam_sample_cost(reconstructed, + original[sample], + previous_error, + shape); + + // If all candidate slots are full, compare against the worst one kept. + if (next_count == kVADPCMBeamWidth) { + const struct vadpcm_beam_candidate *worst = &next[heap[0]]; + + // Discard this candidate unless its score or tie-breaking order is better. + if (cost > worst->cost || (cost == worst->cost && order >= worst->order)) { + continue; + } + } + + // Copy the parent sequence only after deciding to keep this candidate. struct vadpcm_beam_candidate candidate = base; + candidate.order = order; + candidate.cost = cost; - // Default to earlier candidate when two candidates have equal cost. - candidate.order = parent * choices + residual - min_residual; + // Store the residual and its reconstructed sample as part of this sequence. + candidate.reconstructed[sample] = reconstructed; + candidate.residuals[sample] = residual; - vadpcm_beam_extend(&candidate, predictor, original, sample, residual, - scale, previous_frame_error, shape); + // Apply this correction to later predictions in the group using the predictor's weights. + vadpcm_beam_propogate(&candidate, predictor, position, residual, scale); - // Keep this candidate if there is room, or if it beats the worst one kept. + // Keep this candidate if there is room, or replace the worst candidate. vadpcm_beam_keep(next, heap, &next_count, &candidate); } } From 54ce9b337ee1733ddcff2daa9fb958646f2064d8 Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Sun, 8 Nov 2026 21:44:33 -0500 Subject: [PATCH 5/6] audioconv64: multithread stereo channels when using VADPCM beam search --- tools/audioconv64/conv_wav64.cpp | 50 +++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tools/audioconv64/conv_wav64.cpp b/tools/audioconv64/conv_wav64.cpp index 2fa39f8ffa..30dc823be5 100644 --- a/tools/audioconv64/conv_wav64.cpp +++ b/tools/audioconv64/conv_wav64.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "../common/binout.h" #include "../common/polyfill.h" #include "audioconv64.h" @@ -439,26 +441,28 @@ bool wav64_write(const char *infn, const char *outfn, FILE *out, wav_data_t* wav / kVADPCMFrameSampleCount * kVADPCMFrameSampleCount; } - int16_t *schan = (int16_t*)malloc(wav->cnt * sizeof(int16_t)); - for (int i=0; ichannels; i++) { + // Encode one channel and prepare its decoder states and packed frame positions. + auto convert_channel = [&](int i) -> bool { + int16_t *schan = (int16_t*)malloc(wav->cnt * sizeof(int16_t)); uint8_t *destchan = (uint8_t*)malloc(nframes * kVADPCMFrameByteSize); + for (int j=0; jcnt; j++) schan[j] = wav->samples[i + j*wav->channels]; vadpcm_error err = vadpcm_encode(&parms, codebook + kPREDICTORS * kVADPCMEncodeOrder * i, nframes, destchan, schan, NULL); if (err != kVADPCMErrNone) { fprintf(stderr, "ERROR: %s: VADPCM encoding failed: %s\n", infn, vadpcm_error_name(err)); - failed = true; free(destchan); - break; + free(schan); + return false; } if (flag_wav_compress_vadpcm_beam) { err = vadpcm_beam_refine(&parms, codebook + kPREDICTORS * kVADPCMEncodeOrder * i, nframes, destchan, schan, kVADPCMBeamDefaultShape); if (err != kVADPCMErrNone) { fprintf(stderr, "ERROR: %s: VADPCM beam refinement failed: %s\n", infn, vadpcm_error_name(err)); - failed = true; free(destchan); - break; + free(schan); + return false; } } if (!skip_points.empty()) { @@ -514,9 +518,41 @@ bool wav64_write(const char *infn, const char *outfn, FILE *out, wav_data_t* wav } memcpy(dest + dstj * kVADPCMFrameByteSize, destchan + j * kVADPCMFrameByteSize, kVADPCMFrameByteSize); } + // Release this channel's buffers after copying its encoded frames. free(destchan); + free(schan); + return true; + }; + bool right_ok = false; + std::thread right_worker; + + // Try processing the right channel separately when refining stereo audio. + if (flag_wav_compress_vadpcm_beam && wav->channels == 2) { + try { + right_worker = std::thread([&] { + right_ok = convert_channel(1); + }); + } catch (const std::exception&) { + // Leave the worker empty so both channels are processed below. + } + } + + if (right_worker.joinable()) { + // Process the left channel while the worker processes the right channel. + const bool left_ok = convert_channel(0); + + // Wait for the right channel before checking results or packing the output. + right_worker.join(); + failed = failed || !left_ok || !right_ok; + } else { + // Process channels one at a time when no worker was started. + for (int i = 0; i < wav->channels; i++) { + if (!convert_channel(i)) { + failed = true; + break; + } + } } - free(schan); if (failed) { free(dest); break; From 1aa1b25e7440ff261bfc75893941823db5abd7f9 Mon Sep 17 00:00:00 2001 From: BoxingBruin Date: Tue, 10 Nov 2026 14:31:20 -0500 Subject: [PATCH 6/6] audioconv64: Remove hardcoded value from comment in vadpcm_beam.h --- tools/audioconv64/vadpcm_beam.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/audioconv64/vadpcm_beam.h b/tools/audioconv64/vadpcm_beam.h index a67091ebf5..fad79d5f4e 100644 --- a/tools/audioconv64/vadpcm_beam.h +++ b/tools/audioconv64/vadpcm_beam.h @@ -13,7 +13,7 @@ enum { // Refine an existing VADPCM stream in place using beam search. Reuses the // codebook and compares predictors, scales, and residual sequences for each -// frame. The predictor order is kVADPCMEncodeOrder (2). +// frame. The predictor order is kVADPCMEncodeOrder. // // Call after successful vadpcm_encode(), using the same encoding parameters, // codebook, frame count, and original samples. Process a complete channel