From fb396395e99fcf9058ffac27c6445a5c24597b6c Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 5 Jan 2021 11:34:38 -0500 Subject: [PATCH 1/4] Fix bug in pcm delta compression. error wasn't being correctly calculated for when new_sample was negative and sample was positive. --- tools/aif2pcm/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/aif2pcm/main.c b/tools/aif2pcm/main.c index cd5ac4a505..b9709bee85 100644 --- a/tools/aif2pcm/main.c +++ b/tools/aif2pcm/main.c @@ -426,7 +426,9 @@ int get_delta_index(uint8_t sample, uint8_t prev_sample) for (int i = 0; i < 16; i++) { uint8_t new_sample = prev_sample + gDeltaEncodingTable[i]; - int error = sample > new_sample ? sample - new_sample : new_sample - sample; + uint8_t sample_diff_1 = (sample - new_sample) & 0xff; + uint8_t sample_diff_2 = (new_sample - sample) & 0xff; + int error = sample_diff_1 < sample_diff_2 ? sample_diff_1 : sample_diff_2; if (error < best_error) { From 8855623ad01711f3a997bd111c3cfe31becffeaa Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 5 Jan 2021 13:56:26 -0500 Subject: [PATCH 2/4] Fix choice of delta sign. Ensure that the delta goes in the correct direction (i.e. use positive delta for prev <= sample, and negative delta for prev > sample) --- tools/aif2pcm/main.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/aif2pcm/main.c b/tools/aif2pcm/main.c index b9709bee85..af24c417f3 100644 --- a/tools/aif2pcm/main.c +++ b/tools/aif2pcm/main.c @@ -351,6 +351,12 @@ const int gDeltaEncodingTable[] = { -64, -49, -36, -25, -16, -9, -4, -1, }; +#define POSITIVE_DELTAS_START 0 +#define POSITIVE_DELTAS_END 8 + +#define NEGATIVE_DELTAS_START 8 +#define NEGATIVE_DELTAS_END 16 + struct Bytes *delta_decompress(struct Bytes *delta, unsigned int expected_length) { struct Bytes *pcm = malloc(sizeof(struct Bytes)); @@ -418,12 +424,27 @@ struct Bytes *delta_decompress(struct Bytes *delta, unsigned int expected_length return pcm; } +#define U8_TO_S8(value) ((value) < 128 ? (value) : (value) - 256) + int get_delta_index(uint8_t sample, uint8_t prev_sample) { int best_error = INT_MAX; int best_index = -1; + int delta_table_start_index; + int delta_table_end_index; + int sample_signed = U8_TO_S8(sample); + int prev_sample_signed = U8_TO_S8(prev_sample); - for (int i = 0; i < 16; i++) + // if we're going up (or equal), only choose positive deltas + if (prev_sample_signed <= sample_signed) { + delta_table_start_index = POSITIVE_DELTAS_START; + delta_table_end_index = POSITIVE_DELTAS_END; + } else { + delta_table_start_index = NEGATIVE_DELTAS_START; + delta_table_end_index = NEGATIVE_DELTAS_END; + } + + for (int i = delta_table_start_index; i < delta_table_end_index; i++) { uint8_t new_sample = prev_sample + gDeltaEncodingTable[i]; uint8_t sample_diff_1 = (sample - new_sample) & 0xff; From a5f75b49493c686c2a95f5b7e775fa6db1d78e90 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:53:41 -0500 Subject: [PATCH 3/4] Fix issue with diffs being incorrect with extreme values. --- tools/aif2pcm/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/aif2pcm/main.c b/tools/aif2pcm/main.c index af24c417f3..5acdd324c4 100644 --- a/tools/aif2pcm/main.c +++ b/tools/aif2pcm/main.c @@ -425,6 +425,7 @@ struct Bytes *delta_decompress(struct Bytes *delta, unsigned int expected_length } #define U8_TO_S8(value) ((value) < 128 ? (value) : (value) - 256) +#define ABS(value) ((value) >= 0 ? (value) : -(value)) int get_delta_index(uint8_t sample, uint8_t prev_sample) { @@ -447,9 +448,8 @@ int get_delta_index(uint8_t sample, uint8_t prev_sample) for (int i = delta_table_start_index; i < delta_table_end_index; i++) { uint8_t new_sample = prev_sample + gDeltaEncodingTable[i]; - uint8_t sample_diff_1 = (sample - new_sample) & 0xff; - uint8_t sample_diff_2 = (new_sample - sample) & 0xff; - int error = sample_diff_1 < sample_diff_2 ? sample_diff_1 : sample_diff_2; + int new_sample_signed = U8_TO_S8(new_sample); + error = ABS(new_sample_signed - sample_signed); if (error < best_error) { From 44bd156f26d015bf98ace8f8a2c6ce9272a82d86 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 5 Jan 2021 20:10:43 -0500 Subject: [PATCH 4/4] Actually test out that it builds. --- tools/aif2pcm/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aif2pcm/main.c b/tools/aif2pcm/main.c index 5acdd324c4..3dad6fcf80 100644 --- a/tools/aif2pcm/main.c +++ b/tools/aif2pcm/main.c @@ -449,7 +449,7 @@ int get_delta_index(uint8_t sample, uint8_t prev_sample) { uint8_t new_sample = prev_sample + gDeltaEncodingTable[i]; int new_sample_signed = U8_TO_S8(new_sample); - error = ABS(new_sample_signed - sample_signed); + int error = ABS(new_sample_signed - sample_signed); if (error < best_error) {