summaryrefslogtreecommitdiff
path: root/tools/aif2pcm/main.c
diff options
context:
space:
mode:
authorluckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com>2021-01-05 13:56:26 -0500
committerluckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com>2021-01-05 13:56:26 -0500
commit8855623ad01711f3a997bd111c3cfe31becffeaa (patch)
tree3491a66a5d7d6573ad687ac7f4502c3e1f811e66 /tools/aif2pcm/main.c
parentfb396395e99fcf9058ffac27c6445a5c24597b6c (diff)
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)
Diffstat (limited to 'tools/aif2pcm/main.c')
-rw-r--r--tools/aif2pcm/main.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/tools/aif2pcm/main.c b/tools/aif2pcm/main.c
index b9709bee8..af24c417f 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);
+
+ // 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 = 0; i < 16; i++)
+ 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;