comparison intercom/g726/cvt_h_b.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
1 /*
2 ==============================================================================
3
4 CVT_H_B.C
5 ~~~~~~~~~
6
7 Program to convert ITU-T test sequences in hexa formats to
8 word oriented binary files. Each line is supposed to have 32 valid
9 samples (16 bit, or a word), or 64 hexa characters, either upper or
10 lower case. In the last line is expected one word (2 hexa chars)
11 in hexadecimal representation as the checksum of all the samples of
12 the file modulus 255 (sum mod 255).
13
14
15 USAGE:
16 ~~~~~~
17 $ cvt_h_b [-r] [-q] hex-in bin-out
18 where:
19 hex-in is the (input) hexadecimal file's name
20 bin-out is the (output) binary file's name
21
22 Options
23 ~~~~~~~
24 -q Don't print progress indicator on the screen.
25 -r is an optional parameter; if specified as r or R,
26 even-bit reversal of all samples is accomplished
27 before saving the file. Default is NOT invert.
28
29 Compile:
30 ~~~~~~~~
31 SunC: cc -o cvt_h_b cvt_h_b.c # or #
32 acc -o cvt_h_b cvt_h_b.c
33 GNUC: gcc -o cvt_h_b cvt_h_b.c
34 VMS: cc cvt_h_b.c
35 link cvt_h_b
36
37 History:
38 ~~~~~~~~
39 92.01.22 1.0 1st release by tdsimao@venus.cpqd.ansp.br
40 93.11.23 1.01 ported to SunOS (Unix)
41
42 =============================================================
43 COPYRIGHT NOTE: This source code, and all of its derivations,
44 is subject to the ITU-T General Public License". Please have
45 it read in the distribution disk, or in the ITU-T
46 Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
47 CODING STANDARDS".
48 =============================================================
49 ==============================================================================
50 */
51
52 /* OS definition */
53 #ifdef __MSDOS__ /* definition for automatic compil. under TurboC v2.0 */
54 # define MSDOS /* this is already defined for Microsoft C 5.1 */
55 #endif
56
57
58 /* includes in general */
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <math.h>
62
63
64 /* Specific includes */
65 #if defined(MSDOS) /* for MSDOS */
66 # include <fcntl.h>
67 # include <io.h>
68 # include <sys\stat.h>
69 #elif defined(VMS) /* for VMS */
70 # include <perror.h>
71 # include <file.h>
72 # include <stat.h>
73 #else /* for OTHER OS, in special UNIX */
74 # include <sys/stat.h>
75 #endif
76
77
78 /* Generic defines */
79 #define askc(m,v) {printf(m); scanf("%c%*c",&v);}
80 #define aski(m,v) {printf(m);scanf("%d",&v);}
81 #define asks(m,v) {printf(m);gets(v);}
82 #define KILL(str,code) {perror(str); exit(code);}
83 #define GET_PAR_I(a,s,p,msg,i) {if(a>p) i=atoi(s[p]); else aski(msg,i);}
84 #define GET_PAR_C(a,s,p,msg,c) {if(a>p) c=toupper(s[p][0]);else askc(msg,c);}
85 #define GET_PAR_S(a,s,p,msg,S) {if(a>p) strcpy(S,s[p]); else asks(msg,S);}
86
87 #if defined(VMS)
88 #define WB "wb","mrs=512","rfm=fix","ctx=stm"
89 #define RB "rb","mrs=512","rfm=fix","ctx=stm"
90 #elif defined(MSDOS)
91 #define WB "wb"
92 #define RB "rb"
93 #else
94 #define WB "w"
95 #define RB "r"
96 #endif
97
98 #define YES 1
99 #define NO 0
100
101 /*
102 * --------------------------------------------------------------------------
103 * ... Calculate check-sum of a array of shorts ...
104 * Simao 07.Mar.94
105 * --------------------------------------------------------------------------
106 */
107 short short_check_sum(s_data, n)
108 short *s_data;
109 long n;
110 {
111 float csum = 0;
112 long i;
113
114 /* Add samples */
115 while (n)
116 csum += s_data[--n];
117
118 /* return mod as a short */
119 csum = fmod(csum, 255.0);
120 return ((short) csum);
121 }
122
123
124 /*
125 * --------------------------------------------------------------------------
126 * ... Display usage of program ...
127 * Simao 10.Jan.93
128 * --------------------------------------------------------------------------
129 */
130 #define FP(x) printf(x)
131 void display_usage()
132 {
133 FP("Version 1.01 of 23/Nov/1993 \n");
134
135 FP(" CVT_H_B.C:\n");
136 FP(" Program to convert ITU-T test sequences in hexa formats to\n");
137 FP(" word oriented binary files. Each line is supposed to have 32 valid\n");
138 FP(" samples (16 bit, or a word), or 64 hexa characters, either upper or\n");
139 FP(" lower case. In the last line is expected one word (2 hexa chars)\n");
140 FP(" in hexadecimal representation as the checksum of all the samples of\n");
141 FP(" the file modulus 255 (sum mod 255).\n");
142 FP("\n");
143 FP(" Usage:\n");
144 FP(" $ cvt_h_b [-r] hex-in bin-out\n");
145 FP(" where:\n");
146 FP(" hex-in is the (input) hexadecimal file's name\n");
147 FP(" bin-out is the (output) binary file's name\n\n");
148
149 FP(" Options\n");
150 FP(" -q Don't print progress indicator on the screen\n");
151 FP(" -r is an optional parameter; if specified as r or R,\n");
152 FP(" even-bit reversal of all samples is accomplished \n");
153 FP(" before saving the file. Default is NOT invert.\n");
154 FP("\n");
155
156 /* Quit program */
157 exit(-128);
158 }
159
160 #undef FP
161 /* ....................... end of display_usage() ...........................*/
162
163
164 /*============================== */
165 main(argc, argv)
166 int argc;
167 char *argv[];
168 /*============================== */
169 {
170 char line[200], *lp;
171 char inpfil[50], outfil[50];
172 int inp, out, itmp, count = 0;
173 float check_sum = 0;
174 short scheck_sum = 0;
175 short chk;
176 short *bin, *tmp;
177 long leng, i, smpno = 0, lineno = 0;
178 int lastline_len = 0;
179
180 FILE *Fi, *Fo;
181 struct stat st;
182 int invert_even_bits = NO, quiet = NO;
183
184 static char funny[8] = { '\\', '-', '/', '|', '\\', '-', '/', '|' };
185
186
187 /* GETTING OPTIONS */
188
189 if (argc < 2)
190 display_usage();
191 else {
192 while (argc > 1 && argv[1][0] == '-')
193 if (strcmp(argv[1], "-r") == 0) {
194 /* Reverse even bits */
195 invert_even_bits = YES;
196
197 /* Move argv over the option to the next argument */
198 argv++;
199
200 /* Update argc */
201 argc--;
202 } else if (strcmp(argv[1], "-q") == 0) {
203 /* Don't print funny chars */
204 quiet = YES;
205
206 /* Move argv over the option to the next argument */
207 argv++;
208 argc--;
209 } else {
210 fprintf(stderr,
211 "ERROR! Invalid option \"%s\" in command line\n\n", argv[1]);
212 display_usage();
213 }
214 }
215
216
217
218 /* WELCOME! */
219
220 fprintf(stderr, "\n\t\tConversion from ITU-T HEX of test sequence");
221 fprintf(stderr, "\n\t\tfiles to binary files, 16 bits per sample\n");
222
223
224 /* GETTING PARAMETERS */
225
226 GET_PAR_S(argc, argv, 1, "_Input (ITU-T HEX) file: ....... ", inpfil);
227 GET_PAR_S(argc, argv, 2, "_Output (`short') file: ........ ", outfil);
228
229
230 /* OPEN FILES */
231
232 if ((Fi = fopen(inpfil, "r")) == NULL)
233 KILL(inpfil, 2);
234 if ((Fo = fopen(outfil, WB)) == NULL)
235 KILL(outfil, 3);
236 out = fileno(Fo);
237
238 /* FIND FILE SIZE AND ALLOC MEMORY FOR BINARY BUFFER */
239
240 stat(inpfil, &st);
241 if ((bin = (short *) malloc(st.st_size)) == (short *) NULL) {
242 fprintf(stderr, "\n Can't allocate memory for output buffer");
243 exit(4);
244 }
245 tmp = bin;
246
247
248 /* INFOS */
249
250 fprintf(stderr, "\n Converting %s to %s\r", inpfil, outfil);
251
252
253 /* CONVERSION PART */
254
255 /* FIND NUMBER OF LINES IN FILE */
256 while (fgets(line, (int) 200, Fi) != NULL)
257 lineno++;
258 rewind(Fi);
259 lastline_len = strlen(line);
260 if (line[lastline_len - 1] = '\n')
261 lastline_len--;
262
263 /* READ ALL CHARS FOR THE FILE */
264 while (fgets(line, (int) 200, Fi) != NULL) {
265 /* Print `working' flag */
266 if (!quiet)
267 fprintf(stderr, "%c\r", funny[count % 8]);
268
269 /* Update line counter */
270 count++;
271
272 /* Remove CR from line end */
273 leng = strlen(line);
274 if (line[leng - 1] == '\n')
275 line[--leng] = '\0';
276
277 /* Convert line */
278 switch (leng) {
279 case 64:
280 for (lp = line, i = 0; i < leng; i += 2, lp += 2) {
281 sscanf(lp, "%2hx", tmp);
282 tmp++;
283 smpno++;
284 }
285 break;
286
287 case 2:
288 /* Check whether the last line */
289 if (count != lineno) {
290 /* If not, it's a regular sample */
291 sscanf(lp, "%2hx", tmp);
292 tmp++;
293 smpno++;
294 } else {
295 /* value is check-sum */
296 sscanf(line, "%2hx", &chk);
297 }
298 break;
299
300 default: /* for old (red-book) test sequences: initialization */
301 for (i = 0; i < leng - 2; i += 2) {
302 sscanf(&line[i], "%2hx", tmp);
303 tmp++;
304 smpno++;
305 }
306
307 /* Check sum for old files ... */
308 sscanf(&line[i], "%2hx", &chk);
309 break;
310
311 }
312 }
313
314 /* CALCULATE CHECK-SUMS */
315 scheck_sum = short_check_sum(bin, smpno);
316 fprintf(stderr, "Check-sum is: %d (calculated) %d (read)\n",
317 scheck_sum, chk);
318 if (chk != scheck_sum) {
319 fprintf(stderr, "\n Checksum Failed! (%d != %d)\n", chk,
320 scheck_sum);
321 exit(5);
322 }
323
324
325 /* INVERT EVEN BITS, IF IT IS THE CASE */
326
327 if (invert_even_bits) {
328 fprintf(stderr, "\r Inverting even bits, as requested\t\t");
329 for (i = 0; i < (tmp - bin) / 2; i++)
330 bin[i] ^= 0x55;
331 }
332
333
334 /* SAVE TO FILE */
335
336 for (i = 0; i < smpno; i += 256) {
337 if (write(out, &bin[i], 512) != 512)
338 KILL(outfil, 6);
339 }
340
341
342 /* EXITING */
343
344 fprintf(stderr, "\rDone: %d samples converted!\n", smpno);
345 fclose(Fi);
346 fclose(Fo);
347 #ifndef VMS
348 exit(0);
349 #endif
350 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.