A-law algorithm for encoding and decoding (16bit pcm <=> a-law). This is the implementation of G.711 in C.
More...
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
|
#define | LEN ((size_t)8) |
| Linear input code | Compressed code | Linear output code ---------------—+--------------—+----------------— s0000000abcdx | s000abcd | s0000000abcd1 s0000001abcdx | s001abcd | s0000001abcd1 s000001abcdxx | s010abcd | s000001abcd10 s00001abcdxxx | s011abcd | s00001abcd100 s0001abcdxxxx | s100abcd | s0001abcd1000 s001abcdxxxxx | s101abcd | s001abcd10000 s01abcdxxxxxx | s110abcd | s01abcd100000 s1abcdxxxxxxx | s111abcd | s1abcd1000000.
|
|
|
void | encode (uint8_t *out, int16_t *in, size_t len) |
| 16bit pcm to 8bit alaw
|
|
void | decode (int16_t *out, uint8_t *in, size_t len) |
| 8bit alaw to 16bit pcm
|
|
static void | test (int16_t *pcm, uint8_t *coded, int16_t *decoded, size_t len) |
| Self-test implementations.
|
|
int | main (int argc, char *argv[]) |
| Main function.
|
|
|
int16_t | pcm [LEN] = {1000, -1000, 1234, 3200, -1314, 0, 32767, -32768} |
|
uint8_t | r_coded [LEN] = {250, 122, 230, 156, 97, 213, 170, 42} |
|
int16_t | r_decoded [LEN] = {1008, -1008, 1248, 3264, -1312, 8, 32256, -32256} |
|
A-law algorithm for encoding and decoding (16bit pcm <=> a-law). This is the implementation of G.711 in C.
- Author
- sunzhenliang
◆ LEN
Linear input code | Compressed code | Linear output code ---------------—+--------------—+----------------— s0000000abcdx | s000abcd | s0000000abcd1 s0000001abcdx | s001abcd | s0000001abcd1 s000001abcdxx | s010abcd | s000001abcd10 s00001abcdxxx | s011abcd | s00001abcd100 s0001abcdxxxx | s100abcd | s0001abcd1000 s001abcdxxxxx | s101abcd | s001abcd10000 s01abcdxxxxxx | s110abcd | s01abcd100000 s1abcdxxxxxxx | s111abcd | s1abcd1000000.
Compressed code: (s | eee | abcd) for assert for appropriate size int types for IO operations
◆ decode()
void decode |
( |
int16_t * |
out, |
|
|
uint8_t * |
in, |
|
|
size_t |
len |
|
) |
| |
8bit alaw to 16bit pcm
- Parameters
-
out | signed 16bit pcm array |
in | unsigned 8bit alaw array |
len | length of alaw array |
- Returns
- void
108{
109 uint8_t alaw = 0;
110 int32_t pcm = 0;
111 int32_t sign = 0;
112 int32_t eee = 0;
113 for (size_t i = 0; i < len; i++)
114 {
115 alaw = *in++;
116
117
118 alaw ^= 0xD5;
119
120
121 sign = alaw & 0x80;
122
123
124 eee = (alaw & 0x70) >> 4;
125
126
127 pcm = (alaw & 0x0f) << 4 | 8;
128
129
130 pcm += eee ? 0x100 : 0x0;
131
132
133 pcm <<= eee > 1 ? (eee - 1) : 0;
134
135
136 *out++ = sign ? -pcm : pcm;
137 }
138}
◆ encode()
void encode |
( |
uint8_t * |
out, |
|
|
int16_t * |
in, |
|
|
size_t |
len |
|
) |
| |
16bit pcm to 8bit alaw
- Parameters
-
out | unsigned 8bit alaw array |
in | signed 16bit pcm array |
len | length of pcm array |
- Returns
- void
47{
48 uint8_t alaw = 0;
49 int16_t pcm = 0;
50 int32_t sign = 0;
51 int32_t abcd = 0;
52 int32_t eee = 0;
53 int32_t mask = 0;
54 for (size_t i = 0; i < len; i++)
55 {
56 pcm = *in++;
57
58 eee = 7;
59 mask = 0x4000;
60
61
62 sign = (pcm & 0x8000) >> 8;
63
64
65
66
67
68 pcm = sign ? (-pcm - 1) : pcm;
69
70
71
72
73 while ((pcm & mask) == 0 && eee > 0)
74 {
75 eee--;
76 mask >>= 1;
77 }
78
79
80
81 abcd = (pcm >> (eee ? (eee + 3) : 4)) & 0x0f;
82
83
84
85 eee <<= 4;
86
87
88 alaw = (sign | eee | abcd);
89
90
91
92
93
94
95
96 *out++ = alaw ^ 0xD5;
97 }
98}
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Main function.
- Parameters
-
argc | commandline argument count (ignored) |
argv | commandline array of arguments (ignored) |
- Returns
- 0 on exit
176{
177
179
180
181 int16_t decoded[
LEN];
182
183 test(pcm, coded, decoded,
LEN);
184
185
186 printf("inputs: ");
187 for (
size_t i = 0; i <
LEN; i++)
188 {
189 printf("%d ", pcm[i]);
190 }
191 printf("\n");
192
193
194 printf("encode: ");
195 for (
size_t i = 0; i <
LEN; i++)
196 {
197 printf("%u ", coded[i]);
198 }
199 printf("\n");
200
201
202 printf("decode: ");
203 for (
size_t i = 0; i <
LEN; i++)
204 {
205 printf("%d ", decoded[i]);
206 }
207 printf("\n");
208
209
210
211
212
213
214
215 return 0;
216}
#define LEN
Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...
Definition: alaw.c:28
static void test()
Self-test implementations.
Definition: celsius_to_fahrenheit.c:25
◆ test()
static void test |
( |
int16_t * |
pcm, |
|
|
uint8_t * |
coded, |
|
|
int16_t * |
decoded, |
|
|
size_t |
len |
|
) |
| |
|
static |
Self-test implementations.
- Parameters
-
pcm | signed 16bit pcm array |
coded | unsigned 8bit alaw array |
decoded | signed 16bit pcm array |
len | length of test array |
- Returns
- void
149{
150
152
153
154 for (size_t i = 0; i < len; i++)
155 {
156 assert(coded[i] == r_coded[i]);
157 }
158
159
160 decode(decoded, coded, len);
161
162
163 for (size_t i = 0; i < len; i++)
164 {
165 assert(decoded[i] == r_decoded[i]);
166 }
167}
void decode(int16_t *out, uint8_t *in, size_t len)
8bit alaw to 16bit pcm
Definition: alaw.c:107
void encode(uint8_t *out, int16_t *in, size_t len)
16bit pcm to 8bit alaw
Definition: alaw.c:46