Debian Codegraph @

adpcm/encode.c

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