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