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