| 1 |
/* |
| 2 |
* encode.c |
| 3 |
* |
| 4 |
* CCITT ADPCM encoder |
| 5 |
* |
| 6 |
* Usage : encode [-3|4|5] [-a|u|l] < infile > outfile |
| 7 |
*/ |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include "g72x.h" |
| 11 |
#include "avoid-implicit.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* Pack output codes into bytes and write them to stdout. |
| 15 |
* Returns 1 if there is residual output, else returns 0. |
| 16 |
*/ |
| 17 |
int |
| 18 |
pack_output(code, bits) |
| 19 |
unsigned code; |
| 20 |
int bits; |
| 21 |
{ |
| 22 |
static unsigned int out_buffer = 0; |
| 23 |
static int out_bits = 0; |
| 24 |
unsigned char out_byte; |
| 25 |
|
| 26 |
out_buffer |= (code << out_bits); |
| 27 |
out_bits += bits; |
| 28 |
if (out_bits >= 8) { |
| 29 |
out_byte = out_buffer & 0xff; |
| 30 |
out_bits -= 8; |
| 31 |
out_buffer >>= 8; |
| 32 |
fwrite(&out_byte, sizeof (char), 1, stdout); |
| 33 |
} |
| 34 |
return (out_bits > 0); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
int main(argc, argv) |
| 39 |
int argc; |
| 40 |
char **argv; |
| 41 |
{ |
| 42 |
struct g72x_state state; |
| 43 |
unsigned char sample_char; |
| 44 |
short sample_short; |
| 45 |
unsigned char code; |
| 46 |
int resid; |
| 47 |
int in_coding; |
| 48 |
int in_size; |
| 49 |
unsigned *in_buf; |
| 50 |
int (*enc_routine)(); |
| 51 |
int enc_bits; |
| 52 |
|
| 53 |
g72x_init_state(&state); |
| 54 |
|
| 55 |
/* Set defaults to u-law input, G.721 output */ |
| 56 |
in_coding = AUDIO_ENCODING_ULAW; |
| 57 |
in_size = sizeof (char); |
| 58 |
in_buf = (unsigned *)&sample_char; |
| 59 |
enc_routine = g721_encoder; |
| 60 |
enc_bits = 4; |
| 61 |
|
| 62 |
/* Process encoding argument, if any */ |
| 63 |
while ((argc > 1) && (argv[1][0] == '-')) { |
| 64 |
switch (argv[1][1]) { |
| 65 |
case '3': |
| 66 |
enc_routine = g723_24_encoder; |
| 67 |
enc_bits = 3; |
| 68 |
break; |
| 69 |
case '4': |
| 70 |
enc_routine = g721_encoder; |
| 71 |
enc_bits = 4; |
| 72 |
break; |
| 73 |
case '5': |
| 74 |
enc_routine = g723_40_encoder; |
| 75 |
enc_bits = 5; |
| 76 |
break; |
| 77 |
case 'u': |
| 78 |
in_coding = AUDIO_ENCODING_ULAW; |
| 79 |
in_size = sizeof (char); |
| 80 |
in_buf = (unsigned *)&sample_char; |
| 81 |
break; |
| 82 |
case 'a': |
| 83 |
in_coding = AUDIO_ENCODING_ALAW; |
| 84 |
in_size = sizeof (char); |
| 85 |
in_buf = (unsigned *)&sample_char; |
| 86 |
break; |
| 87 |
case 'l': |
| 88 |
in_coding = AUDIO_ENCODING_LINEAR; |
| 89 |
in_size = sizeof (short); |
| 90 |
in_buf = (unsigned *)&sample_short; |
| 91 |
break; |
| 92 |
default: |
| 93 |
fprintf(stderr, "CCITT ADPCM Encoder -- usage:\n"); |
| 94 |
fprintf(stderr, "\tencode [-3|4|5] [-a|u|l] < infile > outfile\n"); |
| 95 |
fprintf(stderr, "where:\n"); |
| 96 |
fprintf(stderr, "\t-3\tGenerate G.723 24kbps (3-bit) data\n"); |
| 97 |
fprintf(stderr, "\t-4\tGenerate G.721 32kbps (4-bit) data [default]\n"); |
| 98 |
fprintf(stderr, "\t-5\tGenerate G.723 40kbps (5-bit) data\n"); |
| 99 |
fprintf(stderr, "\t-a\tProcess 8-bit A-law input data\n"); |
| 100 |
fprintf(stderr, "\t-u\tProcess 8-bit u-law input data [default]\n"); |
| 101 |
fprintf(stderr, "\t-l\tProcess 16-bit linear PCM input data\n"); |
| 102 |
exit(1); |
| 103 |
} |
| 104 |
argc--; |
| 105 |
argv++; |
| 106 |
} |
| 107 |
|
| 108 |
/* Read input file and process */ |
| 109 |
while (fread(in_buf, in_size, 1, stdin) == 1) { |
| 110 |
code = (*enc_routine)(in_size == 2 ? sample_short : sample_char, |
| 111 |
in_coding, &state); |
| 112 |
resid = pack_output(code, enc_bits); |
| 113 |
} |
| 114 |
|
| 115 |
/* Write zero codes until all residual codes are written out */ |
| 116 |
while (resid) { |
| 117 |
resid = pack_output(0, enc_bits); |
| 118 |
} |
| 119 |
fclose(stdout); |
| 120 |
} |