Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/audioconv64/audioconv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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=<vbr|abr|cbr> Select ULC rate-control mode (default: vbr)\n");
Expand Down Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions tools/audioconv64/audioconv64.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
57 changes: 52 additions & 5 deletions tools/audioconv64/conv_wav64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <algorithm>
#include <time.h>
#include <unordered_set>
#include <exception>
#include <thread>
#include "../common/binout.h"
#include "../common/polyfill.h"
#include "audioconv64.h"
Expand All @@ -45,6 +47,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;
Expand Down Expand Up @@ -438,17 +441,29 @@ 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; i<wav->channels; 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; j<wav->cnt; 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));
free(destchan);
free(schan);
return false;
}
}
if (!skip_points.empty()) {
// Compute decoder states at all skip points in a single forward pass.
Expand Down Expand Up @@ -503,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;
Expand Down
1 change: 1 addition & 0 deletions tools/audioconv64/libvadpcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion tools/audioconv64/libvadpcm.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "vadpcm/codec/vadpcm.h"
#include "vadpcm/codec/vadpcm.h"
#include "vadpcm_beam.h"
Loading