|
8 | 8 | #include <sof/math/a_law.h> |
9 | 9 | #include <stdint.h> |
10 | 10 |
|
| 11 | +/* TODO: Some found encoders invert the sign bit, check this. */ |
| 12 | +#define SOFM_ALAW_SIGN_BIT_POS 0x80 |
| 13 | +#define SOFM_ALAW_SIGN_BIT_NEG 0x00 |
| 14 | +#define SOFM_ALAW_SIGN_BIT 0x80 |
| 15 | + |
11 | 16 | #define SOFM_ALAW_MAX 4095 |
12 | 17 | #define SOFM_ALAW_TOGGLE_EVEN_BITS 0x55 |
13 | | -#define SOFM_ALAW_SIGN_BIT 0x80 |
14 | 18 | #define SOFM_ALAW_MANTISSA_MASK 0x0f |
15 | 19 | #define SOFM_ALAW_MANTISSA_BITS 4 |
16 | 20 | #define SOFM_ALAW_SHIFT_MASK 0x07 |
@@ -82,20 +86,22 @@ static uint8_t alaw_encode_shifts[128] = { |
82 | 86 | */ |
83 | 87 | uint8_t sofm_a_law_encode(int16_t sample) |
84 | 88 | { |
85 | | - int sign = 0; |
| 89 | + int32_t tmp; |
| 90 | + int sign = SOFM_ALAW_SIGN_BIT_POS; |
86 | 91 | int shift = 0; |
87 | 92 | int low_bits; |
88 | 93 | uint8_t byte; |
89 | 94 |
|
90 | | - /* Round 16 bit Q1.15 to 13 bit Q1.12 */ |
91 | | - sample = ((sample >> 2) + 1) >> 1; |
92 | | - |
93 | 95 | /* Convert to sign and magnitude */ |
94 | | - if (sample < 0) { |
95 | | - sign = SOFM_ALAW_SIGN_BIT; |
96 | | - sample = -sample; |
| 96 | + tmp = sample; |
| 97 | + if (tmp < 0) { |
| 98 | + sign = SOFM_ALAW_SIGN_BIT_NEG; |
| 99 | + tmp = -tmp; |
97 | 100 | } |
98 | 101 |
|
| 102 | + /* Convert to 13 bits with shift. Note: There is no rounding in references. */ |
| 103 | + sample = (int16_t)(tmp >> 3); |
| 104 | + |
99 | 105 | if (sample > SOFM_ALAW_MAX) |
100 | 106 | sample = SOFM_ALAW_MAX; |
101 | 107 |
|
@@ -138,7 +144,7 @@ int16_t sofm_a_law_decode(int8_t byte) |
138 | 144 | else |
139 | 145 | value = (low_bits << 1) | 1; |
140 | 146 |
|
141 | | - if (sign) |
| 147 | + if (!sign) |
142 | 148 | value = -value; |
143 | 149 |
|
144 | 150 | /* Shift 13 bit Q1.12 to 16 bit Q1.15 */ |
|
0 commit comments