1 /***************************************************************************
2 **
3 **   ITU-T G.722.1 (2005-05) - Fixed point implementation for main body and Annex C
4 **   > Software Release 2.1 (2008-06)
5 **     (Simple repackaging; no change from 2005-05 Release 2.0 code)
6 **
7 **   � 2004 Polycom, Inc.
8 **
9 **   All rights reserved.
10 **
11 ***************************************************************************/
12 
13 /***************************************************************************
14   Filename:    decode.c
15 
16   Purpose:     Contains the main function for the G.722.1 Annex C decoder
17 
18   Design Notes:
19 
20   WMOPS:     7kHz |    24kbit    |     32kbit
21            -------|--------------|----------------
22              AVG  |     2.73     |     2.85
23            -------|--------------|----------------
24              MAX  |     2.79     |     2.88
25            -------|--------------|----------------
26 
27             14kHz |    24kbit    |     32kbit     |     48kbit
28            -------|--------------|----------------|----------------
29              AVG  |     5.28     |     5.53       |     5.85
30            -------|--------------|----------------|----------------
31              MAX  |     5.56     |     5.77       |     5.95
32            -------|--------------|----------------|----------------
33 
34 ***************************************************************************/
35 /***************************************************************************
36  Include files
37 ***************************************************************************/
38 #include "defs.h"
39 #include "count.h"
40 
41 /************************************************************************************
42  Local type declarations
43 *************************************************************************************/
44 /* This object is used to control the command line input */
45 typedef struct
46 {
47     Word16  syntax;
48     Word32  bit_rate;
49     Word16  bandwidth;
50     Word16  number_of_bits_per_frame;
51     Word16  number_of_regions;
52     Word16  frame_size;
53     FILE    *fpout;
54     FILE    *fp_bitstream;
55 } DECODER_CONTROL;
56 
57 /************************************************************************************
58  Constant definitions
59 *************************************************************************************/
60 #define MAX_SAMPLE_RATE     32000
61 #define MAX_FRAMESIZE   (MAX_SAMPLE_RATE/50)
62 #define MEASURE_WMOPS   1
63 #define WMOPS           1
64 
65 /***************************************************************************
66  Local function declarations
67 ***************************************************************************/
68 void parse_command_line(char *argv[],DECODER_CONTROL *control);
69 Word16 read_ITU_format(Word16 *, Word16 *, Word16, FILE *);
70 
71 
72 /************************************************************************************
73  Function:     G722.1 Annex C main decoder function
74 
75  Syntax:       decode <data format> <input file> <output file> <bit rate> <bandwidth>
76 
77                       <data format> - 0 for packed format/1 for ITU format
78                       <input file>  - encoded bitstream file
79                       <output file> - output audio file
80                       <bit rate>    - 24, 32, and 48 kbps
81                       <bandwidth>   - 7 or 14 kHz
82 
83  Description:  Main processing loop for the G.722.1 Annex C decoder
84 
85  Design Notes: 16kbit/sec is also supported for bandwidth of 7kHz
86 
87 ************************************************************************************/
88 
main(Word16 argc,char * argv[])89 main(Word16 argc,char *argv[])
90 {
91 
92     Word16 i;
93     Word16 words;
94     Word16 output[MAX_DCT_LENGTH];
95     Word16 number_of_16bit_words_per_frame;
96     Word16 out_words[MAX_BITS_PER_FRAME/16];
97     Word16 frame_error_flag = 0;
98     Word16 frame_cnt = 0;
99     Word16 decoder_mlt_coefs[MAX_DCT_LENGTH];
100     Word16 mag_shift;
101     Word16 old_mag_shift=0;
102     Word16 old_decoder_mlt_coefs[MAX_DCT_LENGTH];
103     Word16 old_samples[MAX_DCT_LENGTH>>1];
104     Bit_Obj bitobj;
105     Rand_Obj randobj;
106     DECODER_CONTROL control;
107 
108     if (argc < 6)
109     {
110         printf("Usage: decode  <0(packed)/1> <input-bitstream-file> <output-audio-file> <bit-rate> <bandwidth>\n\n");
111         printf("Valid Rates: 48kbps = 48000\n");
112         printf("             32kbps = 32000\n");
113         printf("             24kbps = 24000\n");
114         printf("\n");
115         printf("Bandwidth:    7kHz  =  7000\n");
116         printf("             14kHz  = 14000\n");
117         printf("\n");
118         exit(1);
119     }
120 
121     /* parse the command line input into the control structure */
122     parse_command_line(argv,&control);
123     number_of_16bit_words_per_frame = (Word16)((control.number_of_bits_per_frame)/16);
124 
125     /* initialize the coefs history */
126     for (i=0;i<control.frame_size;i++)
127         old_decoder_mlt_coefs[i] = 0;
128 
129     for (i = 0;i < (control.frame_size >> 1);i++)
130   	    old_samples[i] = 0;
131 
132     /* initialize the random number generator */
133     randobj.seed0 = 1;
134     randobj.seed1 = 1;
135     randobj.seed2 = 1;
136     randobj.seed3 = 1;
137 
138     /* Read first frame of samples from disk. */
139     if (control.syntax == 0)
140         words = (Word16 )fread(out_words, 2, number_of_16bit_words_per_frame, control.fp_bitstream);
141     else
142         words = read_ITU_format(out_words,
143                                 &frame_error_flag,
144     		                    number_of_16bit_words_per_frame,
145     		                    control.fp_bitstream);
146 
147 #if MEASURE_WMOPS
148     Init_WMOPS_counter ();
149 #endif
150 
151     while(words == number_of_16bit_words_per_frame)
152     {
153         frame_cnt++;
154 
155         /* reinit the current word to point to the start of the buffer */
156         bitobj.code_word_ptr = out_words;
157         bitobj.current_word =  *out_words;
158         bitobj.code_bit_count = 0;
159         bitobj.number_of_bits_left = control.number_of_bits_per_frame;
160 
161 #if MEASURE_WMOPS
162         Reset_WMOPS_counter ();
163 #endif
164         /* process the out_words into decoder_mlt_coefs */
165         decoder(&bitobj,
166                 &randobj,
167                 control.number_of_regions,
168                 decoder_mlt_coefs,
169                 &mag_shift,
170                 &old_mag_shift,
171                 old_decoder_mlt_coefs,
172                 frame_error_flag);
173 
174 
175 
176         /* convert the decoder_mlt_coefs to samples */
177         rmlt_coefs_to_samples(decoder_mlt_coefs, old_samples, output, control.frame_size, mag_shift);
178 
179         /* For ITU testing, off the 2 lsbs. */
180         for (i=0; i<control.frame_size; i++)
181             output[i] &= 0xfffc;
182 
183         /* Write frame of output samples */
184         fwrite(output, 2, control.frame_size, control.fpout);
185 
186         /* Read next frame of samples from disk. */
187         if (control.syntax == 0)
188 	        words = (Word16 )fread(out_words, 2, number_of_16bit_words_per_frame, control.fp_bitstream);
189         else
190 	        words = read_ITU_format(out_words,
191 			                        &frame_error_flag,
192 			                        number_of_16bit_words_per_frame,
193 			                        control.fp_bitstream);
194 
195     }
196 
197 #if MEASURE_WMOPS
198     WMOPS_output (0);
199 #endif
200 
201     fclose(control.fpout);
202     fclose(control.fp_bitstream);
203 
204     return 0;
205 }
206 /************************************************************************************
207  Procedure/Function:     Parse command line
208 
209  Syntax:        void parse_command_line(char *argv[],ENCODER_CONTROL *control)
210 
211                     char *argv[]              - contains the command line arguments
212                     DECODER_CONRTROL *control - contains the processed arguments
213 
214  Description:  parses the command line inputs
215 
216 ************************************************************************************/
217 
parse_command_line(char * argv[],DECODER_CONTROL * control)218 void parse_command_line(char *argv[],DECODER_CONTROL *control)
219 {
220     control->syntax = (Word16 )(atoi(*++argv));
221     if ((control->syntax != 0) && (control->syntax != 1))
222     {
223         printf("syntax must be 0 for packed or 1 for ITU format\n");
224         exit(1);
225     }
226 
227     if((control->fp_bitstream = fopen(*++argv,"rb")) == NULL)
228     {
229         printf("codec: error opening %s.\n",*argv);
230         exit(1);
231     }
232     if((control->fpout = fopen(*++argv,"wb")) == NULL)
233     {
234         printf("codec: error opening %s.\n",*argv);
235         exit(1);
236     }
237 
238     control->bit_rate = (Word32)(atoi(*++argv));
239     if ((control->bit_rate < 16000) || (control->bit_rate > 48000) ||
240         ((control->bit_rate/800)*800 != control->bit_rate))
241     {
242         printf("codec: Error. bit-rate must be multiple of 800 between 16000 and 48000\n");
243         exit(1);
244     }
245 
246     control->number_of_bits_per_frame = (Word16 )((control->bit_rate)/50);
247 
248     control->bandwidth = (Word16)(atoi(*++argv));
249     if (control->bandwidth == 7000)
250     {
251         control->number_of_regions = NUMBER_OF_REGIONS;
252         control->frame_size = MAX_FRAMESIZE >> 1;
253 
254         printf("\n*************** G.722.1 DECODER ***************\n");
255         printf("bandwidth = 7 khz\n");
256         printf("syntax = %d ",control->syntax);
257 
258         if (control->syntax == 0)
259             printf(" packed bitstream\n");
260         else if (control->syntax == 1)
261             printf(" ITU-format bitstream\n");
262 
263         printf("sample_rate = 16000    bit_rate = %d\n",control->bit_rate);
264         printf("framesize = %d samples\n",control->frame_size);
265         printf("number_of_regions = %d\n",control->number_of_regions);
266         printf("number_of_bits_per_frame = %d bits\n",control->number_of_bits_per_frame);
267         printf("\n");
268         printf("\n");
269     }
270     else if (control->bandwidth == 14000)
271     {
272         control->number_of_regions = MAX_NUMBER_OF_REGIONS;
273         control->frame_size = MAX_FRAMESIZE;
274 
275         printf("\n*************** G.722.1 EXTENSION DECODER ***************\n");
276         printf("bandwidth = 14 khz\n");
277         printf("syntax = %d ",control->syntax);
278 
279         if (control->syntax == 0)
280             printf(" packed bitstream\n");
281         else if (control->syntax == 1)
282             printf(" ITU-format bitstream\n");
283 
284         printf("sample_rate = 32000    bit_rate = %d\n",control->bit_rate);
285         printf("framesize = %d samples\n",control->frame_size);
286         printf("number_of_regions = %d\n",control->number_of_regions);
287         printf("number_of_bits_per_frame = %d bits\n",control->number_of_bits_per_frame);
288         printf("\n");
289         printf("\n");
290     }
291     else
292     {
293         printf("codec: Error. bandwidth must be 7000 or 14000\n");
294         exit(1);
295     }
296 
297 }
298 
299 /***************************************************************************
300  Procedure/Function:     Read ITU format function
301 
302  Syntax:
303 
304  Description:  reads file input in ITU format
305 
306  Design Notes:
307 
308 
309 ***************************************************************************/
310 
read_ITU_format(Word16 * out_words,Word16 * p_frame_error_flag,Word16 number_of_16bit_words_per_frame,FILE * fp_bitstream)311 Word16 read_ITU_format(Word16 *out_words,
312                        Word16 *p_frame_error_flag,
313                        Word16 number_of_16bit_words_per_frame,
314 		               FILE   *fp_bitstream)
315 {
316     Word16 i,j;
317     Word16 nsamp;
318     Word16 packed_word;
319     Word16 bit_count;
320     Word16 bit;
321     Word16 in_array[MAX_BITS_PER_FRAME+2];
322     Word16 one = 0x0081;
323     Word16 zero = 0x007f;
324     Word16 frame_start = 0x6b21;
325 
326     nsamp = (Word16 )fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame, fp_bitstream);
327 
328     j = 0;
329     bit = in_array[j++];
330     if (bit != frame_start)
331     {
332         *p_frame_error_flag = 1;
333     }
334     else
335     {
336         *p_frame_error_flag = 0;
337 
338         /* increment j to skip over the number of bits in frame */
339         j++;
340 
341         for (i=0; i<number_of_16bit_words_per_frame; i++)
342         {
343             packed_word = 0;
344             bit_count = 15;
345             while (bit_count >= 0)
346             {
347     	        bit = in_array[j++];
348     	        if (bit == zero)
349     	            bit = 0;
350     	        else if (bit == one)
351     	            bit = 1;
352     	        else
353                 {
354     	            *p_frame_error_flag = 1;
355                     /*	  printf("read_ITU_format: bit not zero or one: %4x\n",bit); */
356 	            }
357 	            packed_word <<= 1;
358 	            packed_word = (Word16 )(packed_word + bit);
359 	            bit_count--;
360             }
361             out_words[i] = packed_word;
362         }
363     }
364     return((Word16)((nsamp-1)/16));
365 }
366 
367