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. More...
|
|
|
void | encode (uint8_t *out, int16_t *in, size_t len) |
| 16bit pcm to 8bit alaw More...
|
|
void | decode (int16_t *out, uint8_t *in, size_t len) |
| 8bit alaw to 16bit pcm More...
|
|
static void | test (int16_t *pcm, uint8_t *coded, int16_t *decoded, size_t len) |
| Self-test implementations. More...
|
|
int | main (int argc, char *argv[]) |
| Main function. More...
|
|
|
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
113 for (
size_t i = 0; i < len; i++)
124 eee = (alaw & 0x70) >> 4;
127 pcm = (alaw & 0x0f) << 4 | 8;
130 pcm += eee ? 0x100 : 0x0;
133 pcm <<= eee > 1 ? (eee - 1) : 0;
136 *out++ = sign ? -pcm : pcm;
◆ 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
54 for (
size_t i = 0; i < len; i++)
62 sign = (pcm & 0x8000) >> 8;
68 pcm = sign ? (-pcm - 1) : pcm;
73 while ((pcm & mask) == 0 && eee > 0)
81 abcd = (pcm >> (eee ? (eee + 3) : 4)) & 0x0f;
88 alaw = (sign | eee | abcd);
◆ 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
181 int16_t decoded[
LEN];
183 test(pcm, coded, decoded,
LEN);
187 for (
size_t i = 0; i <
LEN; i++)
189 printf(
"%d ", pcm[i]);
195 for (
size_t i = 0; i <
LEN; i++)
197 printf(
"%u ", coded[i]);
203 for (
size_t i = 0; i <
LEN; i++)
205 printf(
"%d ", decoded[i]);
#define LEN
Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...
Definition: alaw.c:28
static void test(int16_t *pcm, uint8_t *coded, int16_t *decoded, size_t len)
Self-test implementations.
Definition: alaw.c:148
◆ 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
154 for (
size_t i = 0; i < len; i++)
156 assert(coded[i] == r_coded[i]);
160 decode(decoded, coded, len);
163 for (
size_t i = 0; i < len; i++)
165 assert(decoded[i] == r_decoded[i]);
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