Debian Codegraph @

adpcm/decode.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * decode.c
3 *
4 * CCITT ADPCM decoder
5 *
6 * Usage : decode [-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 * Unpack input codes and pass them back as bytes.
15 * Returns 1 if there is residual input, returns -1 if eof, else returns 0.
16 */
17 int
18 unpack_input(code, bits)
19 unsigned char *code;
20 int bits;
21 {
22 static unsigned int in_buffer = 0;
23 static int in_bits = 0;
24 unsigned char in_byte;
25
26 if (in_bits < bits) {
27 if (fread(&in_byte, sizeof (char), 1, stdin) != 1) {
28 *code = 0;
29 return (-1);
30 }
31 in_buffer |= (in_byte << in_bits);
32 in_bits += 8;
33 }
34 *code = in_buffer & ((1 << bits) - 1);
35 in_buffer >>= bits;
36 in_bits -= bits;
37 return (in_bits > 0);
38 }
39
40
41 int main(argc, argv)
42 int argc;
43 char **argv;
44 {
45 short sample;
46 unsigned char code;
47 int n;
48 struct g72x_state state;
49 int out_coding;
50 int out_size;
51 int (*dec_routine)();
52 int dec_bits;
53
54 g72x_init_state(&state);
55 out_coding = AUDIO_ENCODING_ULAW;
56 out_size = sizeof (char);
57 dec_routine = g721_decoder;
58 dec_bits = 4;
59
60 /* Process encoding argument, if any */
61 while ((argc > 1) && (argv[1][0] == '-')) {
62 switch (argv[1][1]) {
63 case '3':
64 dec_routine = g723_24_decoder;
65 dec_bits = 3;
66 break;
67 case '4':
68 dec_routine = g721_decoder;
69 dec_bits = 4;
70 break;
71 case '5':
72 dec_routine = g723_40_decoder;
73 dec_bits = 5;
74 break;
75 case 'u':
76 out_coding = AUDIO_ENCODING_ULAW;
77 out_size = sizeof (char);
78 break;
79 case 'a':
80 out_coding = AUDIO_ENCODING_ALAW;
81 out_size = sizeof (char);
82 break;
83 case 'l':
84 out_coding = AUDIO_ENCODING_LINEAR;
85 out_size = sizeof (short);
86 break;
87 default:
88 fprintf(stderr, "CCITT ADPCM Decoder -- usage:\n");
89 fprintf(stderr, "\tdecode [-3|4|5] [-a|u|l] < infile > outfile\n");
90 fprintf(stderr, "where:\n");
91 fprintf(stderr, "\t-3\tProcess G.723 24kbps (3-bit) input data\n");
92 fprintf(stderr, "\t-4\tProcess G.721 32kbps (4-bit) input data [default]\n");
93 fprintf(stderr, "\t-5\tProcess G.723 40kbps (5-bit) input data\n");
94 fprintf(stderr, "\t-a\tGenerate 8-bit A-law data\n");
95 fprintf(stderr, "\t-u\tGenerate 8-bit u-law data [default]\n");
96 fprintf(stderr, "\t-l\tGenerate 16-bit linear PCM data\n");
97 exit(1);
98 }
99 argc--;
100 argv++;
101 }
102
103 /* Read and unpack input codes and process them */
104 while (unpack_input(&code, dec_bits) >= 0) {
105 sample = (*dec_routine)(code, out_coding, &state);
106 if (out_size == 2) {
107 fwrite(&sample, out_size, 1, stdout);
108 } else {
109 code = (unsigned char)sample;
110 fwrite(&code, out_size, 1, stdout);
111 }
112 }
113 fclose(stdout);
114 }