1 #include "common.h"
2 #include "decoder.h"
3 #include "decode.h"
4 
decode_info(Bit_stream * bs,frame_params * fr_ps)5 void decode_info (Bit_stream * bs, frame_params * fr_ps)
6 {
7   layer *hdr = fr_ps->header;
8 
9   hdr->version = get1bit (bs);
10   hdr->lay = 4 - getbits (bs, 2);
11   hdr->error_protection = !get1bit (bs);	/* error protect. TRUE/FALSE */
12   hdr->bitrate_index = getbits (bs, 4);
13   hdr->sampling_frequency = getbits (bs, 2);
14   hdr->padding = get1bit (bs);
15   hdr->extension = get1bit (bs);
16   hdr->mode = getbits (bs, 2);
17   hdr->mode_ext = getbits (bs, 2);
18   hdr->copyright = get1bit (bs);
19   hdr->original = get1bit (bs);
20   hdr->emphasis = getbits (bs, 2);
21 }
22 
23 /*******************************************************************
24 *
25 * The bit allocation information is decoded.
26 *
27 ********************************************************************/
28 
II_decode_bitalloc(Bit_stream * bs,frame_params * fr_ps,unsigned int bit_alloc[7][SBLIMIT],int bits_log)29 void II_decode_bitalloc (Bit_stream * bs,
30 			 frame_params * fr_ps,
31 			 unsigned int bit_alloc[7][SBLIMIT], int bits_log)
32 {
33   int i, j;
34   int stereo = fr_ps->stereo;
35   int sblimit = fr_ps->sblimit;
36   int jsbound = fr_ps->jsbound;
37   al_table *alloc = fr_ps->alloc;
38   unsigned int actual_alloc[7][SBLIMIT];
39 
40   for (i = 0; i < SBLIMIT; i++)
41     for (j = 0; j < stereo; j++)
42       actual_alloc[j][i] = 0;
43 
44   for (i = 0; i < jsbound; i++)
45     for (j = 0; j < stereo; j++)
46       actual_alloc[j][i] = bit_alloc[j][i] =
47 	(char) getbits (bs, (*alloc)[i][0].bits);
48 
49   for (i = jsbound; i < sblimit; i++)	/* expand to 2 channels */
50     actual_alloc[0][i] = bit_alloc[0][i] = bit_alloc[1][i] =
51       (char) getbits (bs, (*alloc)[i][0].bits);
52 
53   for (i = sblimit; i < SBLIMIT; i++)
54     for (j = 0; j < stereo; j++)
55       bit_alloc[j][i] = 0;
56 
57   if (bits_log) {
58     for (j = 0; j < stereo; j++)
59       for (i = 0; i < SBLIMIT; i++) {
60 	int alloc_bits = 0, alloc_id = bit_alloc[j][i];
61 
62 	if (actual_alloc[j][i] > 0)
63 	  alloc_bits =
64 	    12 * (*alloc)[i][alloc_id].bits * (*alloc)[i][alloc_id].group;
65 
66 	printf ("alloc_bi[ %d ][ %2d ]= %3d\n", j, i, alloc_bits);
67       }
68   }
69 }
70 
71 
72 
73 
74 
75 
76 /*****************************************************************
77 *
78 * The following two functions implement the layer II
79 * format of scale factor extraction. Layer I involves reading
80 * 6 bit per subband as scale factor. Layer II requires reading
81 * first the scfsi which in turn indicate the number of scale factors
82 * transmitted.
83 *   Layer II : II_decode_scale
84 *
85 *************************** Layer II stuff ***************************/
86 
II_decode_scale(Bit_stream * bs,frame_params * fr_ps,unsigned int scfsi[7][SBLIMIT],unsigned int bit_alloc[7][SBLIMIT],unsigned int scale_index[7][3][SBLIMIT],int * l,int * m,int scfsi_log)87 void II_decode_scale (Bit_stream * bs,
88 		      frame_params * fr_ps,
89 		      unsigned int scfsi[7][SBLIMIT],
90 		      unsigned int bit_alloc[7][SBLIMIT],
91 		      unsigned int scale_index[7][3][SBLIMIT],
92 		      int *l, int *m, int scfsi_log)
93 {
94   layer *info = fr_ps->header;
95   int stereo = fr_ps->stereo;
96   int i, j;
97   int px, pci;
98   int sblimit = fr_ps->sblimit;
99 
100   if (*m == stereo)
101     sblimit = fr_ps->sblimit;
102   else
103     sblimit = fr_ps->sblimit_mc;
104 
105   for (i = 0; i < sblimit; i++) {
106     for (j = *l; j < *m; j++)	/* 2 bit scfsi */
107       if (bit_alloc[j][i])
108 	scfsi[j][i] = (char) getbits (bs, 2);
109       else
110 	scfsi[j][i] = 4;
111   }
112 
113   for (i = sblimit; i < SBLIMIT; i++)
114     for (j = *l; j < *m; j++)
115       scfsi[j][i] = 4;
116 
117   if (scfsi_log) {
118     for (j = *l; j < *m; j++)
119       for (i = 0; i < SBLIMIT; i++) {
120 	int scf_bits;
121 
122 	switch (scfsi[j][i]) {
123 	  /* all three scale factors transmitted */
124 	case 0:
125 	  scf_bits = 20;
126 	  break;
127 	  /* two scale factors transmitted */
128 	case 1:
129 	case 3:
130 	  scf_bits = 14;
131 	  break;
132 	  /* only one scale factor transmitted */
133 	case 2:
134 	  scf_bits = 8;
135 	  break;
136 	  /* no scale factors transmitted */
137 	default:
138 	  scf_bits = 0;
139 	}
140 	printf ("scf_bits[ %d ][ %2d ]= %3d\n", j, i, scf_bits);
141       }
142   }
143 
144   /* 3.6.94 R.S. read the prediction coefficients in the mc - part */
145   if (*m > stereo && *m < 7)
146     if (fr_ps->header->mc_prediction_on == 1)
147       for (i = 0; i < 8; i++)
148 	if (fr_ps->header->mc_prediction[i] == 1) {
149 	  for (px = 0;
150 	       px <
151 	       pred_coef_table[fr_ps->pred_mode][fr_ps->header->
152 						 dyn_cross_mode[i]]; px++)
153 	    if (fr_ps->header->mc_predsi[i][px] != 0) {
154 	      /* predictors are transfered */
155 	      fr_ps->header->mc_delay_comp[i][px] = getbits (bs, 3);
156 	      for (pci = 0; pci < fr_ps->header->mc_predsi[i][px]; pci++)
157 		fr_ps->header->mc_pred_coeff[i][px][pci] = getbits (bs, 8);
158 	    } else {
159 	      /* no prediction coef. */
160 	      fr_ps->header->mc_pred_coeff[i][px][0] = 127;	/* Index 127 -> 0.0 */
161 	      fr_ps->header->mc_delay_comp[i][px] = 0;
162 	    }
163 	}
164   /* 31/10/95 Ralf Schwalbe LFE */
165   if (*l == stereo)
166     if (info->lfe)
167       if (info->lfe_alloc)
168 	info->lfe_scf = getbits (bs, 6);
169 
170   for (i = 0; i < sblimit; i++)
171     for (j = *l; j < *m; j++)
172       if (bit_alloc[j][i])
173 	switch (scfsi[j][i]) {
174 	  /* all three scale factors transmitted */
175 	case 0:
176 	  scale_index[j][0][i] = getbits (bs, 6);
177 	  scale_index[j][1][i] = getbits (bs, 6);
178 	  scale_index[j][2][i] = getbits (bs, 6);
179 	  break;
180 	  /* scale factor 1 & 3 transmitted */
181 	case 1:
182 	  scale_index[j][0][i] = scale_index[j][1][i] = getbits (bs, 6);
183 	  scale_index[j][2][i] = getbits (bs, 6);
184 	  break;
185 	  /* scale factor 1 & 2 transmitted */
186 	case 3:
187 	  scale_index[j][0][i] = getbits (bs, 6);
188 	  scale_index[j][1][i] = scale_index[j][2][i] = getbits (bs, 6);
189 	  break;
190 	  /* only one scale factor transmitted */
191 	case 2:
192 	  scale_index[j][0][i] =
193 	    scale_index[j][1][i] = scale_index[j][2][i] = getbits (bs, 6);
194 	  break;
195 	default:
196 	  scale_index[j][0][i] =
197 	    scale_index[j][1][i] = scale_index[j][2][i] = SCALE_RANGE - 1;
198 	  break;
199       } else
200 	scale_index[j][0][i] = scale_index[j][1][i] = scale_index[j][2][i] =
201 	  SCALE_RANGE - 1;
202 
203   for (i = sblimit; i < SBLIMIT; i++)
204     for (j = *l; j < *m; j++)
205       scale_index[j][0][i] = scale_index[j][1][i] = scale_index[j][2][i] =
206 	SCALE_RANGE - 1;
207 }
208 
209 /**************************************************************
210 *
211 * The following two routines take care of reading the
212 * compressed sample from the bit stream for layer 2.
213 * For layer 2, if grouping is
214 * indicated for a particular subband, then the sample size has
215 * to be read from the bits_group and the merged samples has
216 * to be decompose into the three distinct samples. Otherwise,
217 * it is the same for as layer one.
218 *
219 **************************************************/
220 
221 
II_buffer_sample(Bit_stream * bs,frame_params * fr_ps,unsigned int sample[7][3][SBLIMIT],unsigned int bit_alloc[7][SBLIMIT])222 void II_buffer_sample (Bit_stream * bs,
223 		       frame_params * fr_ps,
224 		       unsigned int sample[7][3][SBLIMIT],
225 		       unsigned int bit_alloc[7][SBLIMIT])
226 {
227   int i, j, k, m;
228   int stereo = fr_ps->stereo;
229   int sblimit = fr_ps->sblimit;
230   int jsbound = fr_ps->jsbound;
231   al_table *alloc = fr_ps->alloc;
232 
233   for (i = 0; i < sblimit; i++)
234     for (j = 0; j < ((i < jsbound) ? stereo : 1); j++) {
235       if (bit_alloc[j][i]) {
236 	/* check for grouping in subband */
237 	if ((*alloc)[i][bit_alloc[j][i]].group == 3) {
238 	  for (m = 0; m < 3; m++) {
239 	    k = (*alloc)[i][bit_alloc[j][i]].bits;
240 	    sample[j][m][i] = (unsigned int) getbits (bs, k);
241 	  }
242 	} else {		/* bit_alloc = 3, 5, 9 */
243 	  unsigned int nlevels, c = 0;
244 
245 	  nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
246 	  k = (*alloc)[i][bit_alloc[j][i]].bits;
247 	  c = (unsigned int) getbits (bs, k);
248 
249 	  for (k = 0; k < 3; k++) {
250 	    sample[j][k][i] = c % nlevels;
251 	    c /= nlevels;
252 	  }
253 	}
254       } else {			/* for no sample transmitted */
255 	for (k = 0; k < 3; k++)
256 	  sample[j][k][i] = 0;
257       }
258       if (stereo == 2 && i >= jsbound)	/* joint stereo : copy L to R */
259 	for (k = 0; k < 3; k++)
260 	  sample[1][k][i] = sample[0][k][i];
261     }
262   for (i = sblimit; i < SBLIMIT; i++)
263     for (j = 0; j < stereo; j++)
264       for (k = 0; k < 3; k++)
265 	sample[j][k][i] = 0;
266 }
267 
268 
269 
270 /**************************************************************
271 *
272 *   Restore the compressed sample to a factional number.
273 *   first complement the MSB of the sample
274 *   for Layer II :
275 *   Use the formula s = s' * c + d
276 *
277 **************************************************************/
278 
279 double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
280   1.77777777777, 1.06666666666, 1.03225806452,
281   1.01587301587, 1.00787401575, 1.00392156863,
282   1.00195694716, 1.00097751711, 1.00048851979,
283   1.00024420024, 1.00012208522, 1.00006103888,
284   1.00003051851, 1.00001525902
285 };
286 
287 double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
288   0.125000000, 0.062500000, 0.031250000, 0.015625000,
289   0.007812500, 0.003906250, 0.001953125, 0.0009765625,
290   0.00048828125, 0.00024414063, 0.00012207031,
291   0.00006103516, 0.00003051758
292 };
II_dequantize_sample(unsigned int sample[7][3][SBLIMIT],unsigned int bit_alloc[7][SBLIMIT],double fraction[7][SBLIMIT][3][12],frame_params * fr_ps,int * z)293 void II_dequantize_sample (unsigned int sample[7][3][SBLIMIT],
294 			   unsigned int bit_alloc[7][SBLIMIT],
295 			   double fraction[7][SBLIMIT][3][12],
296 			   frame_params * fr_ps, int *z)
297 {
298   int i, j, k, x;
299   int stereo = fr_ps->stereo;
300   int sblimit = fr_ps->sblimit;
301   al_table *alloc = fr_ps->alloc;
302 
303   for (i = 0; i < sblimit; i++)
304     for (j = 0; j < 3; j++)
305       for (k = 0; k < stereo; k++)
306 	if (bit_alloc[k][i]) {
307 	  /* locate MSB in the sample */
308 	  x = 0;
309 	  while ((1L << x) < (*alloc)[i][bit_alloc[k][i]].steps)
310 	    x++;
311 
312 	  /* MSB inversion */
313 	  if (((sample[k][j][i] >> (x - 1)) & 1) == 1)
314 	    fraction[k][i][j][*z] = 0.0;
315 	  else
316 	    fraction[k][i][j][*z] = -1.0;
317 
318 	  /* Form a 2's complement sample */
319 	  fraction[k][i][j][*z] +=
320 	    (double) (sample[k][j][i] & ((1 << (x - 1)) - 1)) /
321 	    (double) (1L << (x - 1));
322 
323 	  /* Dequantize the sample */
324 	  fraction[k][i][j][*z] += d[(*alloc)[i][bit_alloc[k][i]].quant];
325 	  fraction[k][i][j][*z] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
326 	} else
327 	  fraction[k][i][j][*z] = 0.0;
328 
329   for (i = sblimit; i < SBLIMIT; i++)
330     for (j = 0; j < 3; j++)
331       for (k = 0; k < stereo; k++)
332 	fraction[k][i][j][*z] = 0.0;
333 }
334 
II_lfe_calc(frame_params * fr_ps)335 void II_lfe_calc (frame_params * fr_ps)
336 {
337   layer *info = fr_ps->header;
338   int x, i;
339   al_table *alloc = fr_ps->alloc_mc;
340 
341   for (i = 0; i < 12; i++) {
342     x = 0;
343     while ((1L << x) < (*alloc)[0][info->lfe_alloc].steps)
344       x++;
345     /* MSB inversion */
346     if (((info->lfe_spl[i] >> (x - 1)) & 1) == 1)
347       info->lfe_spl_fraction[i] = 0.0;
348     else
349       info->lfe_spl_fraction[i] = -1.0;
350 
351     /* Form a 2's complement sample */
352     info->lfe_spl_fraction[i] +=
353       (double) (info->lfe_spl[i] & ((1 << (x - 1)) - 1)) /
354       (double) (1L << (x - 1));
355 
356     /* Dequantize the sample */
357     info->lfe_spl_fraction[i] += d[(*alloc)[0][info->lfe_alloc].quant];
358     info->lfe_spl_fraction[i] *= c[(*alloc)[0][info->lfe_alloc].quant];
359 
360     /* Denormalize the sample */
361     info->lfe_spl_fraction[i] *= multiple[info->lfe_scf];
362   }
363 }
364 
365 /************************************************************
366 *
367 *   Restore the original value of the sample ie multiply
368 *    the fraction value by its scalefactor.
369 *
370 *************************************************************/
371 
II_denormalize_sample(double fraction[7][SBLIMIT][3][12],unsigned int scale_index[7][3][SBLIMIT],frame_params * fr_ps,int x,int * z)372 void II_denormalize_sample (double fraction[7][SBLIMIT][3][12],
373 			    unsigned int scale_index[7][3][SBLIMIT],
374 			    frame_params * fr_ps, int x, int *z)
375 {
376   int i, j;
377   int stereo = fr_ps->stereo;
378   int sblimit = fr_ps->sblimit;
379 
380   for (i = 0; i < sblimit; i++)
381     for (j = 0; j < stereo; j++) {
382       fraction[j][i][0][*z] *= multiple[scale_index[j][x][i]];
383       fraction[j][i][1][*z] *= multiple[scale_index[j][x][i]];
384       fraction[j][i][2][*z] *= multiple[scale_index[j][x][i]];
385     }
386 }
387 
388 
389 
390 
391 
392 
393 
394 
395