1
2 #define VERY_FAST_FILTER 1 /* JMZ 08/03/1995 FILTER */
3
4 #include "common.h"
5 #include "encoder.h"
6 #include "dyn_cross.h"
7 #include "encode.h"
8
9 /*=======================================================================\
10 | |
11 | This segment contains all the core routines of the encoder, |
12 | except for the psychoacoustic models. |
13 | |
14 | The user can select either one of the two psychoacoustic |
15 | models. Model I is a simple tonal and noise masking threshold |
16 | generator, and Model II is a more sophisticated cochlear masking |
17 | threshold generator. Model I is recommended for lower complexity |
18 | applications whereas Model II gives better subjective quality at low |
19 | bit rates. |
20 | |
21 | Layers I and II of mono, stereo, and joint stereo modes are supported.|
22 | Routines associated with a given layer are prefixed by "I_" for layer |
23 | 1 and "II_" for layer 2. |
24 \=======================================================================*/
25
26
27 /*************************************************************************
28 *
29 * matricing()
30 *
31 * The five-channel signal must be matricied to guarantee
32 * the compatibility to the stereo-decoder.
33 * There must be something of the surround information in the
34 * two front channels. In a five-channel decoder there will
35 * be a dematricing.
36 * There must be 7 channels for channel switching 8/10/93, SR
37 *
38 * Channel 5 and 6 are the not matriced signals L and R
39 *
40 * If phantom coding is used the high-frequency part of the
41 * center signal is matrixed to left and right.
42 * 27/07/95 WtK
43 *
44 ***************************************************************************/
45
normalizing(double (* sb_sample)[3][12][32],frame_params * fr_ps)46 void normalizing (double (*sb_sample)[3][12][32], frame_params * fr_ps)
47 {
48 double matrNorm = 1; /* factor for normalizing JMZ */
49 double matrC = 1; /* weighting factor for C, Phantom C */
50 double matrLsRs = 1; /* weighting factor for Ls, Rs */
51 int stereo = fr_ps->stereo;
52 int i, j, k, l;
53
54 layer *info = fr_ps->header;
55
56 switch (info->matrix) {
57 /* Changed the factors according to International Standard */
58
59 case 0:
60 case 2:
61 matrNorm = 1 / (1 + sqrt (2.0));
62 matrC = 1 / sqrt (2.0);
63 matrLsRs = 1 / sqrt (2.0);
64 break;
65 case 1:
66 matrNorm = 1 / (1.5 + 0.5 * sqrt (2.0));
67 matrC = 1 / sqrt (2.0);
68 matrLsRs = 0.5;
69 break;
70 case 3:
71 matrNorm = 1;
72 matrC = 1;
73 matrLsRs = 1;
74 break;
75 }
76
77
78 for (i = 0; i < stereo; i++)
79 for (j = 0; j < 3; j++)
80 for (l = 0; l < 12; l++)
81 for (k = 0; k < SBLIMIT; k++)
82 sb_sample[i][j][l][k] *= matrNorm;
83
84 for (j = 0; j < 3; ++j)
85 for (l = 0; l < 12; l++)
86 for (k = 0; k < SBLIMIT; k++)
87 if (fr_ps->config == 320) {
88 sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
89 sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
90 sb_sample[4][j][l][k] = sb_sample[4][j][l][k] * matrNorm * matrLsRs;
91 } else if (fr_ps->config == 310) {
92 sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
93 sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
94 } else if (fr_ps->config == 220) {
95 sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrLsRs;
96 sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
97 } else if (fr_ps->config == 300 || fr_ps->config == 302)
98 sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
99 else if (fr_ps->config == 210)
100 sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrLsRs;
101 }
102
matricing(double (* sb_sample)[3][12][32],frame_params * fr_ps)103 void matricing (double (*sb_sample)[3][12][32], frame_params * fr_ps)
104 {
105 double matrPC=1; /* weighting factor for Phantom C */
106 double mono_surround;
107 register double val;
108 int j, k, l;
109
110 layer *info = fr_ps->header;
111
112 switch (info->matrix) {
113 /* Changed the factors according to International Standard */
114 case 0:
115 case 1:
116 case 2:
117 matrPC = 1;
118 break;
119 case 3:
120 matrPC = 1 / sqrt (2.0);
121 break;
122 }
123
124
125 for (j = 0; j < 3; ++j) {
126 for (l = 0; l < 12; l++) {
127 for (k = 0; k < SBLIMIT; k++) {
128 sb_sample[5][j][l][k] = sb_sample[0][j][l][k];
129 sb_sample[6][j][l][k] = sb_sample[1][j][l][k];
130
131 if (fr_ps->config == 320) {
132 /* 960819 FdB changed matricing */
133 if (info->matrix == 0 || info->matrix == 1) {
134 sb_sample[0][j][l][k] =
135 sb_sample[5][j][l][k] + sb_sample[2][j][l][k] +
136 sb_sample[3][j][l][k];
137 sb_sample[1][j][l][k] =
138 sb_sample[6][j][l][k] + sb_sample[2][j][l][k] +
139 sb_sample[4][j][l][k];
140 } else if (info->matrix == 2) {
141 mono_surround =
142 (sb_sample[3][j][l][k] + sb_sample[4][j][l][k]) / 2.0;
143 sb_sample[0][j][l][k] =
144 sb_sample[5][j][l][k] + sb_sample[2][j][l][k] - mono_surround;
145 sb_sample[1][j][l][k] =
146 sb_sample[6][j][l][k] + sb_sample[2][j][l][k] + mono_surround;
147 } else {
148 sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
149 sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
150 }
151 } else if (fr_ps->config == 310) {
152 /* 960819 FdB changed matricing */
153 if (info->matrix == 0 || info->matrix == 1) {
154 sb_sample[0][j][l][k] =
155 sb_sample[5][j][l][k] + sb_sample[2][j][l][k] +
156 sb_sample[3][j][l][k];
157 sb_sample[1][j][l][k] =
158 sb_sample[6][j][l][k] + sb_sample[2][j][l][k] +
159 sb_sample[3][j][l][k];
160 } else if (info->matrix == 2) {
161 sb_sample[0][j][l][k] =
162 sb_sample[5][j][l][k] + sb_sample[2][j][l][k] -
163 sb_sample[3][j][l][k];
164 sb_sample[1][j][l][k] =
165 sb_sample[6][j][l][k] + sb_sample[2][j][l][k] +
166 sb_sample[3][j][l][k];
167 } else {
168 sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
169 sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
170 }
171 } else if (fr_ps->config == 220) {
172 /* 960819 FdB changed matricing */
173 if (info->matrix == 0 || info->matrix == 1) {
174 sb_sample[0][j][l][k] =
175 sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
176 sb_sample[1][j][l][k] =
177 sb_sample[6][j][l][k] + sb_sample[3][j][l][k];
178 } else if (info->matrix == 3) {
179 sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
180 sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
181 }
182 } else if (fr_ps->config == 300 || fr_ps->config == 302) {
183 /* 960819 FdB changed matricing */
184 if (info->matrix == 0 || info->matrix == 1) {
185 sb_sample[0][j][l][k] =
186 sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
187 sb_sample[1][j][l][k] =
188 sb_sample[6][j][l][k] + sb_sample[2][j][l][k];
189 } else if (info->matrix == 3) {
190 sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
191 sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
192 }
193 } else if (fr_ps->config == 210) {
194 /* 960819 FdB changed matricing */
195 if (info->matrix == 0 || info->matrix == 1) {
196 sb_sample[0][j][l][k] =
197 sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
198 sb_sample[1][j][l][k] =
199 sb_sample[6][j][l][k] + sb_sample[2][j][l][k];
200 } else if (info->matrix == 3) {
201 sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
202 sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
203 }
204 }
205 }
206
207 if (info->center == 3) { /* 27/07/95 WtK */
208 if (info->matrix == 3)
209 for (k = 12; k < SBLIMIT; k++) {
210 val = matrPC * sb_sample[2][j][l][k];
211 sb_sample[0][j][l][k] += val;
212 sb_sample[1][j][l][k] += val;
213 sb_sample[2][j][l][k] = 0;
214 } else
215 for (k = 12; k < SBLIMIT; k++) {
216 val = matrPC * sb_sample[2][j][l][k];
217 sb_sample[5][j][l][k] += val;
218 sb_sample[6][j][l][k] += val;
219 sb_sample[2][j][l][k] = 0;
220 }
221 }
222 }
223 }
224 }
225
226
227
228 /*************************************************************************
229 *
230 * matricing_fft()
231 *
232 * To get the best results in psychoacoustics there must be both,
233 * the matriced and the not matriced signal. This matricing
234 * may be in full bandwith.
235 *
236 * If LFE is not enabled, "buffer" contains:
237 * buffer[0] L
238 * buffer[1] R
239 * buffer[2] C
240 * buffer[3] Ls
241 * buffer[4] Rs
242 *
243 * If LFE is enabled, "buffer" contains:
244 * buffer[0] L
245 * buffer[1] R
246 * buffer[2] C
247 * buffer[3] LFE
248 * buffer[4] Ls
249 * buffer[5] Rs
250 *
251 * This function matrixes the original audio samples to pass to the
252 * psychoacoustic model. The model considers the matrixed and non-
253 * matrixed versions of the signal, so both are retained here.
254 *
255 * On exit, buffer_matr[0] to buffer_matr[6] contain the channels
256 * Lo, Ro, C, Ls, Rs, L, R, respectively.
257 *
258 **************************************************************************/
259
matricing_fft(double (* buffer)[1152],double (* buffer_matr)[1152],frame_params * fr_ps)260 void matricing_fft (double (*buffer)[1152],
261 double (*buffer_matr)[1152], frame_params * fr_ps)
262 {
263 double matrNorm=0.0; /* factor for normalizing JMZ */
264 double matrC=0.0; // 1/sqrt(2) /* weighting factor for C */
265 double matrLsRs=0.0; /* weighting factor for Ls, Rs */
266 double mono_surround;
267 int i;
268 int lfe, lfe_pos;
269
270 layer *info = fr_ps->header;
271 lfe = info->lfe;
272 lfe_pos = fr_ps->lfe_pos;
273
274 switch (info->matrix) {
275 case 0:
276 case 2:
277 matrNorm = 1 / (1 + sqrt (2.0));
278 matrC = 1 / sqrt (2.0);
279 matrLsRs = 1 / sqrt (2.0);
280 break;
281 case 1:
282 matrNorm = 1 / (1.5 + 0.5 * sqrt (2.0));
283 matrC = 1 / sqrt (2.0);
284 matrLsRs = 0.5;
285 break;
286 case 3:
287 matrNorm = 1;
288 matrC = 1;
289 matrLsRs = 1;
290 break;
291 default:
292 fprintf(stdout,"Panic: Shouldn't be here in matricing_fft\n");
293 exit(0);
294 break;
295 }
296
297 for (i = 0; i < 1152; i++) {
298 if (lfe && lfe_pos < 3)
299 buffer_matr[2][i] = buffer[3][i];
300 else
301 buffer_matr[2][i] = buffer[2][i];
302 buffer_matr[3][i] = buffer[3 + lfe][i];
303 buffer_matr[4][i] = buffer[4 + lfe][i];
304 buffer_matr[5][i] = buffer[0][i];
305 buffer_matr[6][i] = buffer[1][i];
306
307 switch (info->matrix) {
308 case 0:
309 case 1:
310 buffer_matr[0][i] =
311 matrNorm * (buffer[0][i] + matrC * buffer_matr[2][i] +
312 matrLsRs * buffer_matr[3][i]);
313 buffer_matr[1][i] =
314 matrNorm * (buffer[1][i] + matrC * buffer_matr[2][i] +
315 matrLsRs * buffer_matr[4][i]);
316 break;
317 case 2:
318 mono_surround = (buffer_matr[3][i] + buffer_matr[4][i]) / 2.0;
319 buffer_matr[0][i] =
320 matrNorm * (buffer[0][i] + matrC * buffer_matr[2][i] -
321 matrLsRs * mono_surround);
322 buffer_matr[1][i] =
323 matrNorm * (buffer[1][i] + matrC * buffer_matr[2][i] +
324 matrLsRs * mono_surround);
325 break;
326 case 3:
327 buffer_matr[0][i] = buffer[0][i];
328 buffer_matr[1][i] = buffer[1][i];
329 break;
330 }
331 }
332 }
333
334
335 #ifndef NEWSUBBAND
336 /************************************************************************
337 *
338 * read_ana_window()
339 *
340 * PURPOSE: Reads encoder window file "enwindow" into array #ana_win#
341 *
342 ************************************************************************/
343
read_ana_window(double * ana_win)344 void read_ana_window (double *ana_win)
345 /*far */
346 {
347 int i, j[4];
348 FILE *fp;
349 double f[4];
350 char t[150];
351
352 if (!(fp = OpenTableFile ("enwindow"))) {
353 printf ("Please check analysis window table 'enwindow'\n");
354 exit (1);
355 }
356 for (i = 0; i < 512; i += 4) {
357 fgets (t, 80, fp); /* changed from 150, 92-08-11 shn */
358 sscanf (t, "C[%d] = %lf C[%d] = %lf C[%d] = %lf C[%d] = %lf\n",
359 j, f, j + 1, f + 1, j + 2, f + 2, j + 3, f + 3);
360 if (i == j[0]) {
361 ana_win[i] = f[0];
362 ana_win[i + 1] = f[1];
363 ana_win[i + 2] = f[2];
364 ana_win[i + 3] = f[3];
365 } else {
366 printf ("Check index in analysis window table\n");
367 exit (1);
368 }
369 fgets (t, 80, fp); /* changed from 150, 92-08-11 shn */
370 }
371 fclose (fp);
372 }
373
374 /************************************************************************
375 *
376 * window_subband()
377 *
378 * PURPOSE: Overlapping window on PCM samples
379 *
380 * SEMANTICS:
381 * 32 16-bit pcm samples are scaled to fractional 2's complement and
382 * concatenated to the end of the window buffer #x#. The updated window
383 * buffer #x# is then windowed by the analysis window #c# to produce the
384 * windowed sample #z#
385 *
386 ************************************************************************/
387
window_subband(double ** buffer,double * z,int k)388 void window_subband (double **buffer, double *z, int k)
389 {
390 typedef double XX[14][HAN_SIZE]; /* 08/03/1995 JMZ Multilingual */
391 static XX *x;
392 int i, j;
393 static int off[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* 08/03/1995 JMZ Multilingual */
394 static char init = 0;
395 static double *c;
396
397 if (!init) {
398 c = (double *) mem_alloc (sizeof (double) * HAN_SIZE, "window");
399 read_ana_window (c);
400 x = (XX *) mem_alloc (sizeof (XX), "x");
401 for (i = 0; i < 14; i++)
402 for (j = 0; j < HAN_SIZE; j++)
403 (*x)[i][j] = 0;
404 init = 1;
405 }
406
407 for (i = 0; i < 32; i++)
408 (*x)[k][31 - i + off[k]] = (double) *(*buffer)++ / SCALE;
409 for (i = 0; i < HAN_SIZE; i++)
410 z[i] = (*x)[k][(i + off[k]) & (HAN_SIZE - 1)] * c[i]; //MFC
411 off[k] += 480; /*offset is modulo (HAN_SIZE-1) */
412 off[k] &= HAN_SIZE - 1;
413 }
414
415 /************************************************************************
416 *
417 * create_ana_filter()
418 *
419 * PURPOSE: Calculates the analysis filter bank coefficients
420 *
421 * SEMANTICS:
422 * Calculates the analysis filterbank coefficients and rounds to the
423 * 9th decimal place accuracy of the filterbank tables in the ISO
424 * document. The coefficients are stored in #filter#
425 *
426 ************************************************************************/
427
create_ana_filter(double (* filter)[64])428 void create_ana_filter (double (*filter)[64])
429 /*far */
430 {
431 register int i, k;
432
433 for (i = 0; i < 32; i++)
434 for (k = 0; k < 64; k++) {
435 if ((filter[i][k] =
436 1e9 * cos ((double) ((2 * i + 1) * (16 - k) * PI64))) >= 0)
437 modf (filter[i][k] + 0.5, &filter[i][k]);
438 else
439 modf (filter[i][k] - 0.5, &filter[i][k]);
440 filter[i][k] *= 1e-9;
441 }
442 }
443
444 /************************************************************************
445 *
446 * filter_subband()
447 *
448 * PURPOSE: Calculates the analysis filter bank coefficients
449 *
450 * SEMANTICS:
451 * The windowed samples #z# is filtered by the digital filter matrix #m#
452 * to produce the subband samples #s#. This done by first selectively
453 * picking out values from the windowed samples, and then multiplying
454 * them by the filter matrix, producing 32 subband samples.
455 *
456 ************************************************************************/
457
filter_subband_old(double * z,double * s)458 void filter_subband_old (double *z, double *s)
459 /*far */
460 {
461 double y[64];
462 int i, k;
463 static char init = 0;
464 typedef double MM[SBLIMIT][64];
465 static MM /*far */ * m;
466 double sum1;
467
468 #ifdef MS_DOS
469 long SIZE_OF_MM;
470 SIZE_OF_MM = SBLIMIT * 64;
471 SIZE_OF_MM *= 8;
472 if (!init) {
473 m = (MM /*far */ *) mem_alloc (SIZE_OF_MM, "filter");
474 create_ana_filter (*m);
475 init = 1;
476 }
477 #else
478 if (!init) {
479 m = (MM /*far */ *) mem_alloc (sizeof (MM), "filter");
480 create_ana_filter (*m);
481 init = 1;
482 }
483 #endif
484 /* Window */
485 for (i = 0; i < 64; i++) {
486 for (k = 0, sum1 = 0.0; k < 8; k++)
487 sum1 += z[i + 64 * k];
488 y[i] = sum1;
489 }
490
491 /* Filter */
492 for (i = 0; i < SBLIMIT; i++) {
493 for (k = 0, sum1 = 0.0; k < 64; k++)
494 sum1 += (*m)[i][k] * y[k];
495 s[i] = sum1;
496 }
497
498 /* for (i=0;i<64;i++) for (j=0, y[i] = 0;j<8;j++) y[i] += z[i+64*j];*/
499 /* for (i=0;i<SBLIMIT;i++)*/
500 /* for (j=0, s[i]= 0;j<64;j++) s[i] += (*m)[i][j] * y[j];*/
501
502 }
503
504 /************************************************************************/
505 /* JMZ 08/03/1995 FILTER */
506
filter_subband(double * z,double * s)507 void filter_subband (double *z, double *s)
508 /*far */
509 {
510 double y[64];
511 int i, k;
512 static char init = 0;
513 typedef double MM[SBLIMIT][64];
514 static MM /*far */ * m;
515 double sum1, sum2;
516
517 if (!init) {
518 m = (MM /*far */ *) mem_alloc (sizeof (MM), "filter");
519 create_ana_filter (*m);
520 init = 1;
521 }
522 /* Window */
523 for (i = 0; i < 64; i++) {
524 for (k = 0, sum1 = 0.0; k < 8; k++)
525 sum1 += z[i + 64 * k];
526 y[i] = sum1;
527 }
528
529 /* Filter */
530 #if VERY_FAST_FILTER
531 for (i = 0; i < SBLIMIT / 2; i++) {
532 for (k = 0, sum1 = 0.0, sum2 = 0.0; k < 16;) {
533 sum1 += (*m)[i][k] * (y[k] + y[32 - k]);
534 sum2 += (*m)[i][k + 1] * (y[k + 1] + y[31 - k]);
535 sum2 += (*m)[i][k + 33] * (y[k + 33] - y[63 - k]);
536 sum1 += (*m)[i][k + 34] * (y[k + 34] - y[62 - k]);
537 k += 2;
538 }
539 sum1 += (*m)[i][16] * y[16] - (*m)[i][48] * y[48];
540
541 s[i] = sum1 + sum2;
542 s[31 - i] = sum1 - sum2;
543 }
544 #else
545 for (i = 0; i < SBLIMIT; i++) {
546 for (k = 0, sum1 = 0.0; k < 64; k++)
547 sum1 += (*m)[i][k] * y[k];
548 s[i] = sum1;
549 }
550 #endif /*VERY_FAST_FILTER */
551 }
552 #endif //NEWSUBBAND
553
554
555 /* JMZ 08/03/1995 FILTER */
556 /************************************************************************/
557
558 /************************************************************************
559 *
560 * encode_info()
561 * encode_infomc1() SR
562 * encode_infomc2() SR
563 *
564 * PURPOSE: Puts the syncword and header information on the output
565 * bitstream.
566 *
567 ************************************************************************/
568
encode_info(frame_params * fr_ps,Bit_stream_struc * bs)569 void encode_info (frame_params * fr_ps, Bit_stream_struc * bs)
570 {
571 layer *info = fr_ps->header;
572
573 putbits (bs, 0xfff, 12); /* syncword 12 bits */
574 put1bit (bs, info->version); /* ID 1 bit */
575 putbits (bs, 4 - info->lay, 2); /* layer 2 bits */
576 put1bit (bs, !info->error_protection); /* bit set => no err prot */
577 putbits (bs, info->bitrate_index, 4);
578 putbits (bs, info->sampling_frequency, 2);
579 put1bit (bs, info->padding);
580 put1bit (bs, info->extension); /* private_bit */
581 putbits (bs, info->mode, 2);
582 putbits (bs, info->mode_ext, 2);
583 put1bit (bs, info->copyright);
584 put1bit (bs, info->original);
585 putbits (bs, info->emphasis, 2);
586 }
587
encode_info_mc1(frame_params * fr_ps,Bit_stream_struc * bs)588 void encode_info_mc1 (frame_params * fr_ps, Bit_stream_struc * bs)
589 {
590 layer *info = fr_ps->header;
591
592 put1bit (bs, info->ext_bit_stream_present);
593 if (info->ext_bit_stream_present == 1)
594 putbits (bs, info->n_ad_bytes, 8);
595 putbits (bs, info->center, 2);
596 putbits (bs, info->surround, 2);
597 put1bit (bs, info->lfe);
598 put1bit (bs, info->audio_mix);
599 putbits (bs, info->matrix, 2);
600 putbits (bs, info->multiling_ch, 3);
601 put1bit (bs, info->multiling_fs);
602 put1bit (bs, info->multiling_lay);
603 put1bit (bs, info->copy_ident_bit);
604 put1bit (bs, info->copy_ident_start);
605 }
606
encode_info_mc2(frame_params * fr_ps,Bit_stream_struc * bs)607 void encode_info_mc2 (frame_params * fr_ps, Bit_stream_struc * bs)
608 {
609 layer *info = fr_ps->header;
610 int i, j;
611
612 put1bit (bs, info->tc_sbgr_select);
613 put1bit (bs, info->dyn_cross_on);
614 put1bit (bs, info->mc_prediction_on);
615
616 /* 960627 FdB tca bits dependent on configuration */
617 if (fr_ps->config == 320 || fr_ps->config == 310) {
618 /* 3 bits for tca's */
619 if (info->tc_sbgr_select == 1)
620 putbits (bs, info->tc_allocation, 3);
621 else
622 for (i = 0; i < 12; i++)
623 putbits (bs, info->tc_alloc[i], 3);
624 } else if (fr_ps->config == 300 || fr_ps->config == 302 ||
625 fr_ps->config == 220 || fr_ps->config == 210) {
626 /* 2 bits for tca's */
627 if (info->tc_sbgr_select == 1)
628 putbits (bs, info->tc_allocation, 2);
629 else
630 for (i = 0; i < 12; i++)
631 putbits (bs, info->tc_alloc[i], 2);
632 }
633
634 if (info->dyn_cross_on == 1) {
635 put1bit (bs, info->dyn_cross_LR);
636 for (i = 0; i < 12; i++) {
637 /* 960627 FdB DynX bits dependent on configuration */
638 if (fr_ps->config == 320)
639 /* 3/2 */
640 putbits (bs, info->dyn_cross[i], 4);
641 else if (fr_ps->config == 310 || fr_ps->config == 220)
642 /* 3/1 and 2/2 */
643 putbits (bs, info->dyn_cross[i], 3);
644 else if (fr_ps->config == 300 || fr_ps->config == 302
645 || fr_ps->config == 210)
646 /* 3/0 (+2/0) and 2/1 */
647 putbits (bs, info->dyn_cross[i], 1);
648
649 if (info->surround == 3)
650 put1bit (bs, info->dyn_second_stereo[i]);
651 }
652 }
653
654 if (info->mc_prediction_on == 1) {
655 for (i = 0; i < 8; i++) {
656 put1bit (bs, info->mc_pred[i]);
657 if (info->mc_pred[i] == 1) {
658 for (j = 0; j < n_pred_coef[info->dyn_cross[i]]; j++)
659 putbits (bs, info->predsi[i][j], 2);
660 }
661 }
662 }
663 }
664
665
666
encode_info_ext1(frame_params * fr_ps,Bit_stream_struc * bs_ext)667 void encode_info_ext1 (frame_params * fr_ps, Bit_stream_struc * bs_ext)
668 {
669 layer *info = fr_ps->header;
670
671 info->ext_sync = 0x7ff;
672
673 putbits (bs_ext, info->ext_sync, 12);
674 }
675
encode_info_ext2(frame_params * fr_ps,Bit_stream_struc * bs_ext,unsigned int crc)676 void encode_info_ext2 (frame_params * fr_ps, Bit_stream_struc * bs_ext,
677 unsigned int crc)
678 {
679 layer *info = fr_ps->header;
680
681 putbits (bs_ext, crc, 16);
682 putbits (bs_ext, info->ext_length, 11);
683 put1bit (bs_ext, info->ext_bit);
684 }
685
686
687 /************************************************************************
688 *
689 * mod()
690 *
691 * PURPOSE: Returns the absolute value of its argument
692 *
693 ************************************************************************/
694 /* USE fabs
695 INLINE double mod (double a)
696 {
697 return (a > 0) ? a : -a;
698 }
699 */
700 /************************************************************************
701 *
702 * I_combine_LR (Layer I)
703 * II_combine_LR (Layer II)
704 *
705 * PURPOSE:Combines left and right channels into a mono channel
706 *
707 * SEMANTICS: The average of left and right subband samples is put into
708 * #joint_sample#
709 *
710 * Layer I and II differ in frame length and # subbands used
711 *
712 ************************************************************************/
713
714
II_combine_LR(double (* sb_sample)[3][12][32],double (* joint_sample)[3][12][32],int sblimit)715 void II_combine_LR (double (*sb_sample)[3][12][32],
716 double (*joint_sample)[3][12][32], int sblimit)
717 /*far */
718 /*far */
719 { /* make a filtered mono for joint stereo */
720 int sb, smp, sufr;
721
722 for (sb = 0; sb < sblimit; ++sb)
723 for (smp = 0; smp < SCALE_BLOCK; ++smp)
724 for (sufr = 0; sufr < 3; ++sufr)
725 joint_sample[0][sufr][smp][sb] = .5 * (sb_sample[0][sufr][smp][sb]
726 + sb_sample[1][sufr][smp][sb]);
727 }
728
729
730 /************************************************************************
731 *
732 * I_scale_factor_calc (Layer I)
733 * II_scale_factor_calc (Layer II)
734 *
735 * PURPOSE:For each subband, calculate the scale factor for each set
736 * of the 12 (6 in case of lsf ML) subband samples
737 *
738 * SEMANTICS: Pick the scalefactor #multiple[]# just larger than the
739 * absolute value of the peak subband sample of 12 samples,
740 * and store the corresponding scalefactor index in #scalar#.
741 *
742 * Layer II has three sets of 12 (6 in case of lsf ML) subband samples
743 * for a given subband.
744 *
745 ************************************************************************/
746
II_scale_factor_calc(frame_params * fr_ps,double (* sb_sample)[3][12][32],unsigned int (* scalar)[3][32],int sblimit,int l,int m)747 void II_scale_factor_calc (frame_params * fr_ps,
748 double (*sb_sample)[3][12][32],
749 unsigned int (*scalar)[3][32],
750 int sblimit, int l, int m)
751 /* sblimit has the value of sblimit_ml in case II_scale_factor_calc */
752 /* is called in a ML channel , 7/8/95 WtK */
753 {
754 int i, j, k, t;
755 double s[SBLIMIT];
756 int leng;
757
758 leng = SCALE_BLOCK; /* == 12 */
759 if (l >= 7 && fr_ps->header->multiling_fs == 1)
760 leng /= 2;
761
762 for (k = l; k < m; k++)
763 for (t = 0; t < 3; t++) {
764 for (i = 0; i < sblimit; i++)
765 for (j = 1, s[i] = fabs (sb_sample[k][t][0][i]); j < leng; j++)
766 if (fabs (sb_sample[k][t][j][i]) > s[i])
767 s[i] = fabs (sb_sample[k][t][j][i]);
768
769 for (i = 0; i < sblimit; i++)
770 for (j = SCALE_RANGE - 1, scalar[k][t][i] = 0; j >= 0; j--)
771 if (s[i] < multiple[j]) {
772 /* <= changed to <, 1992-11-06 shn */
773 scalar[k][t][i] = j;
774 break;
775 }
776 for (i = sblimit; i < SBLIMIT; i++)
777 scalar[k][t][i] = SCALE_RANGE - 1;
778 }
779 }
780
781 /***************************************************************************
782 * void II_scale_factor_calc1(sb_sample, scalar, stereo, sblimit)
783 *
784 * in case of any joint stereo the scalefactor must be computed
785 * a second time for the combind samples
786 *
787 ***************************************************************************/
788
II_scale_factor_calc1(double (* sb_sample)[3][12][32],unsigned int (* scalar)[3][32],int sblimit,int dim)789 void II_scale_factor_calc1 (double (*sb_sample)[3][12][32],
790 unsigned int (*scalar)[3][32], int sblimit, int dim)
791 {
792 int i, j, t;
793 double s[SBLIMIT];
794
795 for (t = 0; t < 3; t++) {
796 for (i = 0; i < sblimit; i++)
797 for (j = 1, s[i] = fabs (sb_sample[dim][t][0][i]); j < SCALE_BLOCK; j++)
798 if (fabs (sb_sample[dim][t][j][i]) > s[i])
799 s[i] = fabs (sb_sample[dim][t][j][i]);
800
801 for (i = 0; i < sblimit; i++)
802 for (j = SCALE_RANGE - 1, scalar[dim][t][i] = 0; j >= 0; j--)
803 if (s[i] < multiple[j]) { /* <= changed to <, 1992-11-06 shn */
804 scalar[dim][t][i] = j;
805 break;
806 }
807 for (i = sblimit; i < SBLIMIT; i++)
808 scalar[dim][t][i] = SCALE_RANGE - 1;
809 }
810 }
811
812
813
814 /************************************************************************
815 *
816 * pick_scale (Layer II)
817 *
818 * PURPOSE:For each subband, puts the smallest scalefactor of the 3
819 * associated with a frame into #max_sc#. This is used
820 * used by Psychoacoustic Model I.
821 * (I would recommend changin max_sc to min_sc)
822 *
823 ************************************************************************/
824
pick_scale(unsigned int (* scalar)[3][32],frame_params * fr_ps,double (* max_sc)[32],int cha_sw,int aug_cha_sw,int aiff)825 void pick_scale (unsigned int (*scalar)[3][32],
826 frame_params * fr_ps,
827 double (*max_sc)[32], int cha_sw, int aug_cha_sw, int aiff)
828 {
829 int i, j, k, l, m;
830 int max;
831 int stereo = fr_ps->stereo;
832 // int stereomc = fr_ps->stereomc;
833 int stereoaug = fr_ps->stereoaug;
834 int sblimit = fr_ps->sblimit;
835 int sblimit_mc = fr_ps->sblimit_mc;
836 int sblimit_ml = fr_ps->sblimit_ml;
837 int n_ml_ch = fr_ps->header->multiling_ch; /* 08/03/1995 JMZ Multilingual */
838
839
840 if (aiff != 1) {
841 l = 0;
842 m = stereo;
843 } else {
844 l = 0;
845 if (stereoaug == 2)
846 m = 12;
847 else
848 m = 7;
849 }
850
851 for (k = 0; k < stereo; k++) {
852 for (i = 0; i < sblimit; max_sc[k][i] = multiple[max], i++)
853 for (j = 1, max = scalar[k][0][i]; j < 3; j++)
854 if (max > scalar[k][j][i])
855 max = scalar[k][j][i];
856 for (i = sblimit; i < SBLIMIT; i++)
857 max_sc[k][i] = 1E-20;
858 }
859
860 for (k = stereo; k < m; k++) {
861 for (i = 0; i < sblimit_mc; max_sc[k][i] = multiple[max], i++)
862 for (j = 1, max = scalar[k][0][i]; j < 3; j++)
863 if (max > scalar[k][j][i])
864 max = scalar[k][j][i];
865 for (i = sblimit_mc; i < SBLIMIT; i++)
866 max_sc[k][i] = 1E-20;
867 }
868
869 if (aiff == 1) {
870 /* OLD 961114 FdB
871 if (fr_ps->header->matrix == 3 || cha_sw == 0)
872 {
873 fr_ps->header->tc_sbgr_select = 1;
874 for (i = 0; i < 12; i++)
875 fr_ps->header->tc_alloc[i] = 0;
876 }
877 else
878 tc_alloc (fr_ps, max_sc);
879 */
880
881 for (i = 0; i < 12; i++)
882 if (cha_sw == -1 && fr_ps->header->matrix != 3)
883 switch (fr_ps->config) {
884 case 320:
885 if (fr_ps->header->center == 3 && i >= 10) { /* tc_alloc = 0,3,4,5 */
886 fr_ps->header->tc_alloc[i] = rand () % 4;
887 if (fr_ps->header->tc_alloc[i] > 0)
888 fr_ps->header->tc_alloc[i] += 2;
889 } else
890 fr_ps->header->tc_alloc[i] = rand () % 8;
891 break;
892 case 310:
893 if (fr_ps->header->center == 3 && i >= 10) { /* tc_alloc = 0,3,4 */
894 fr_ps->header->tc_alloc[i] = rand () % 3;
895 if (fr_ps->header->tc_alloc[i] > 0)
896 fr_ps->header->tc_alloc[i] += 2;
897 } else if (fr_ps->header->matrix == 2)
898 fr_ps->header->tc_alloc[i] = rand () % 6;
899 else
900 fr_ps->header->tc_alloc[i] = rand () % 5;
901 break;
902 case 300:
903 case 302:
904 if (fr_ps->header->center == 3 && i >= 10) /* tc_alloc = 0 */
905 fr_ps->header->tc_alloc[i] = 0;
906 else
907 fr_ps->header->tc_alloc[i] = rand () % 3;
908 break;
909 case 220:
910 fr_ps->header->tc_alloc[i] = rand () % 4;
911 break;
912 case 210:
913 fr_ps->header->tc_alloc[i] = rand () % 3;
914 break;
915 default:
916 break;
917 } else if (cha_sw == -2 && fr_ps->header->matrix != 3)
918 tc_alloc (fr_ps, max_sc);
919 else if (fr_ps->header->matrix == 3)
920 fr_ps->header->tc_alloc[i] = 0;
921 else
922 fr_ps->header->tc_alloc[i] = cha_sw;
923
924 fr_ps->header->tc_sbgr_select = 1;
925 fr_ps->header->tc_allocation = fr_ps->header->tc_alloc[0];
926 for (i = 1; i < 12; i++)
927 if (fr_ps->header->tc_alloc[i] != fr_ps->header->tc_alloc[0])
928 fr_ps->header->tc_sbgr_select = 0;
929
930 }
931
932 /********************************************************/
933 /* JMZ 08/03/1995 Multilingual , WtK 07/08/95 */
934
935 if (n_ml_ch > 0) {
936 for (k = 7; k < 7 + n_ml_ch; k++) {
937 for (i = 0; i < sblimit_ml; max_sc[k][i] = multiple[max], i++)
938 for (j = 1, max = scalar[k][0][i]; j < 3; j++)
939 if (max > scalar[k][j][i])
940 max = scalar[k][j][i];
941 for (i = sblimit_ml; i < SBLIMIT; i++)
942 max_sc[k][i] = 1E-20;
943 }
944 }
945
946 /* JMZ 08/03/1995 Multilingual */
947 /********************************************************/
948
949 }
950
951 /***************************************************************************
952 *
953 * tc_alloc (Layer II, multichannel)
954 *
955 * PURPOSE: For each subbandgroup the three transmissionchannels are
956 * determined by taking the channel with the lowest level
957 * according to the tabel tc_allocation in the draft
958 * 8/10/93, SR
959 *
960 * changed to a certain limit of TC_ALLOC which must be stepped
961 * beyond, before there is channel-switching
962 * 9/20/93 SR
963
964 * JMZ 08/03/1995 Ajout pour traiter les differentes configurations
965 * envisagees dans la norme
966 **************************************************************************/
967
tc_alloc(frame_params * fr_ps,double (* max_sc)[32])968 void tc_alloc (frame_params * fr_ps, double (*max_sc)[32])
969 {
970 layer *info = fr_ps->header;
971 int center = info->center;
972 int surround = info->surround;
973 int matrix = info->matrix;
974 int i, l, k;
975 int min;
976 // double min1;
977 double min2[7][12];
978
979
980 /* 01/03/1995 JMZ Configuration 3/2 */
981 if (surround == 2 && center != 0) {
982 /* if (matrix == 3) ->tc_alloc = 0
983 else if (center == 3) ->tc_alloc = 0,3,4,5
984 else ->tc_alloc = 0,1,2,3,4,5,6,7 */
985
986 if (matrix == 3) {
987 for (i = 0; i < 12; i++)
988 fr_ps->header->tc_alloc[i] = 0;
989 } else if (center == 3) { /* && matrix != 3 */
990 /* 3/2 Phantom Center coding */
991 for (i = 0; i < 8; i++) {
992 if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[5][i]))) >
993 TC_ALLOC) {
994 if (max_sc[6][i] < max_sc[4][i])
995 fr_ps->header->tc_alloc[i] = 5;
996 else
997 fr_ps->header->tc_alloc[i] = 3;
998 } else if (((20 * log10 (max_sc[4][i])) - (20 * log10 (max_sc[6][i]))) >
999 TC_ALLOC)
1000 fr_ps->header->tc_alloc[i] = 4;
1001 else
1002 fr_ps->header->tc_alloc[i] = 0;
1003 }
1004
1005 for (i = 8; i < 12; i++) {
1006 for (k = 2; k < 7; k++) {
1007 min2[k][i] = 0.0;
1008 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1009 min2[k][i] += max_sc[k][l];
1010 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1011 }
1012
1013 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1014 if (min2[6][i] < min2[4][i])
1015 fr_ps->header->tc_alloc[i] = 5;
1016 else
1017 fr_ps->header->tc_alloc[i] = 3;
1018 } else if (((20 * log10 (min2[4][i])) - (20 * log10 (min2[6][i]))) >
1019 TC_ALLOC)
1020 fr_ps->header->tc_alloc[i] = 4;
1021 else
1022 fr_ps->header->tc_alloc[i] = 0;
1023 }
1024 } else {
1025 /* 3/2 no Phantom Center coding */
1026 for (i = 0; i < 8; i++) {
1027 if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[5][i]))) >
1028 TC_ALLOC) {
1029 if (max_sc[6][i] < max_sc[5][i])
1030 min = 6;
1031 else if (max_sc[6][i] == max_sc[5][i]) {
1032 if (max_sc[3][i] <= max_sc[5][i])
1033 min = 5;
1034 else
1035 min = 6;
1036 } else
1037 min = 5;
1038 } else if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[6][i]))) >
1039 TC_ALLOC) {
1040 min = 6;
1041 /* 01/03/1995 JMZ Simplification */
1042 } else {
1043 min = 2;
1044 }
1045
1046 switch (min) {
1047 case 5:
1048 if (max_sc[4][i] <= max_sc[6][i]) /* left front,Rs */
1049 fr_ps->header->tc_alloc[i] = 1;
1050 else
1051 fr_ps->header->tc_alloc[i] = 7;
1052 /*R*/ break;
1053 case 6:
1054 if (max_sc[3][i] <= max_sc[5][i]) /* right front,Ls */
1055 fr_ps->header->tc_alloc[i] = 2;
1056 else
1057 fr_ps->header->tc_alloc[i] = 6; /* L */
1058 break;
1059 case 2:
1060 if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[5][i]))) >
1061 TC_ALLOC) {
1062 if (max_sc[4][i] <= max_sc[6][i])
1063 fr_ps->header->tc_alloc[i] = 3;
1064 else
1065 fr_ps->header->tc_alloc[i] = 5;
1066 } else {
1067 if (((20 * log10 (max_sc[4][i])) - (20 * log10 (max_sc[6][i]))) >
1068 TC_ALLOC)
1069 fr_ps->header->tc_alloc[i] = 4;
1070 else
1071 fr_ps->header->tc_alloc[i] = 0;
1072 }
1073 break;
1074 }
1075 }
1076
1077 for (i = 8; i < 12; i++) { /*taking the average scalefactor of each sb-group */
1078 for (k = 2; k < 7; k++) {
1079 min2[k][i] = 0.0;
1080 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1081 min2[k][i] += max_sc[k][l];
1082 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1083 }
1084
1085 if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1086 if (min2[6][i] < min2[5][i])
1087 min = 6;
1088 else if (min2[6][i] == min2[5][i]) {
1089 if (min2[3][i] <= min2[5][i])
1090 min = 5;
1091 else
1092 min = 6;
1093 } else
1094 min = 5;
1095 } else if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[6][i]))) >
1096 TC_ALLOC) {
1097 min = 6;
1098 /* 01/03/1995 JMZ Simplification */
1099 } else {
1100 min = 2;
1101 }
1102
1103 switch (min) {
1104 case 5:
1105 if (min2[4][i] <= min2[6][i]) /* left front,Rs */
1106 fr_ps->header->tc_alloc[i] = 1;
1107 else
1108 fr_ps->header->tc_alloc[i] = 7;
1109 /*R*/ break;
1110 case 6:
1111 if (min2[3][i] <= min2[5][i]) /* right front,Ls */
1112 fr_ps->header->tc_alloc[i] = 2;
1113 else
1114 fr_ps->header->tc_alloc[i] = 6; /* L */
1115 break;
1116 case 2:
1117 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[5][i]))) >
1118 TC_ALLOC) {
1119 if (min2[4][i] <= min2[6][i])
1120 fr_ps->header->tc_alloc[i] = 3;
1121 else
1122 fr_ps->header->tc_alloc[i] = 5;
1123 } else {
1124 if (((20 * log10 (min2[4][i])) - (20 * log10 (min2[6][i]))) >
1125 TC_ALLOC)
1126 fr_ps->header->tc_alloc[i] = 4;
1127 else
1128 fr_ps->header->tc_alloc[i] = 0;
1129 }
1130 break;
1131 }
1132 }
1133 }
1134 }
1135
1136 /* 01/03/1995 JMZ Configuration 3/1 */
1137 if (surround == 1 && center != 0) {
1138 /* if (matrix == 3) ->tc_alloc = 0
1139 else if (center == 3) ->tc_alloc = 0,3,4
1140 else if (matrix == 2) ->tc_alloc = 0,1,2,3,4,5
1141 else ->tc_alloc = 0,1,2,3,4 */
1142
1143 if (matrix == 3) {
1144 for (i = 0; i < 12; i++)
1145 fr_ps->header->tc_alloc[i] = 0;
1146 } else if (center == 3) { /* && matrix != 3 */
1147 /* 3/1 Phantom Center coding */
1148 for (i = 0; i < 8; i++) {
1149 if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[5][i]))) >
1150 TC_ALLOC) {
1151 if (max_sc[6][i] < max_sc[5][i])
1152 fr_ps->header->tc_alloc[i] = 4;
1153 else
1154 fr_ps->header->tc_alloc[i] = 3;
1155 } else if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[6][i]))) >
1156 TC_ALLOC)
1157 fr_ps->header->tc_alloc[i] = 4;
1158 else
1159 fr_ps->header->tc_alloc[i] = 0;
1160 }
1161
1162 for (i = 8; i < 12; i++) {
1163 for (k = 2; k < 7; k++) {
1164 min2[k][i] = 0.0;
1165 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1166 min2[k][i] += max_sc[k][l];
1167 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1168 }
1169
1170 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1171 if (min2[6][i] < min2[5][i])
1172 fr_ps->header->tc_alloc[i] = 4;
1173 else
1174 fr_ps->header->tc_alloc[i] = 3;
1175 } else
1176 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[6][i]))) >
1177 TC_ALLOC)
1178 fr_ps->header->tc_alloc[i] = 4;
1179 else
1180 fr_ps->header->tc_alloc[i] = 0;
1181 }
1182 } else
1183 /*
1184 if(matrix==2)
1185 {
1186 }
1187 else
1188 */
1189 {
1190 /* 3/1 no Phantom Center coding */
1191 for (i = 0; i < 8; i++) {
1192 if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[5][i]))) >
1193 TC_ALLOC) {
1194 if (max_sc[6][i] < max_sc[5][i])
1195 min = 6;
1196 else
1197 min = 5;
1198 } else if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[6][i]))) >
1199 TC_ALLOC)
1200 min = 6;
1201 else
1202 min = 2;
1203
1204 switch (min) {
1205 case 5:
1206 fr_ps->header->tc_alloc[i] = 1;
1207 break;
1208
1209 case 6:
1210 fr_ps->header->tc_alloc[i] = 2;
1211 break;
1212
1213 case 2:
1214 if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[5][i]))) >
1215 TC_ALLOC) {
1216 if (max_sc[6][i] <= max_sc[5][i])
1217 fr_ps->header->tc_alloc[i] = 4;
1218 else
1219 fr_ps->header->tc_alloc[i] = 3;
1220 } else {
1221 if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[6][i]))) >
1222 TC_ALLOC)
1223 fr_ps->header->tc_alloc[i] = 4;
1224 else
1225 fr_ps->header->tc_alloc[i] = 0;
1226 }
1227 break;
1228 }
1229 }
1230
1231 for (i = 8; i < 12; i++) {
1232 for (k = 2; k < 7; k++) {
1233 min2[k][i] = 0.0;
1234 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1235 min2[k][i] += max_sc[k][l];
1236 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1237 }
1238
1239 if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1240 if (min2[6][i] < min2[5][i])
1241 min = 6;
1242 else
1243 min = 5;
1244 } else if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[6][i]))) >
1245 TC_ALLOC)
1246 min = 6;
1247 else
1248 min = 2;
1249
1250 switch (min) {
1251 case 5:
1252 fr_ps->header->tc_alloc[i] = 1;
1253 break;
1254
1255 case 6:
1256 fr_ps->header->tc_alloc[i] = 2;
1257 break;
1258
1259 case 2:
1260 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[5][i]))) >
1261 TC_ALLOC) {
1262 if (min2[6][i] <= min2[5][i])
1263 fr_ps->header->tc_alloc[i] = 4;
1264 else
1265 fr_ps->header->tc_alloc[i] = 3;
1266 } else {
1267 if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[6][i]))) >
1268 TC_ALLOC)
1269 fr_ps->header->tc_alloc[i] = 4;
1270 else
1271 fr_ps->header->tc_alloc[i] = 0;
1272 }
1273 break;
1274 }
1275 }
1276 }
1277 }
1278
1279 /* 01/03/1995 JMZ Configuration 3/0 (+2/0) */
1280 if (center != 0 && (surround == 3 || surround == 0)) {
1281 if (matrix == 3 || center == 3) {
1282 for (i = 0; i < 12; i++)
1283 fr_ps->header->tc_alloc[i] = 0;
1284 }
1285 /* 02/02/97 FdB no matrix == 2 allowed for 3/0
1286 else if (matrix == 2)
1287 {
1288 for(i = 0; i < 8; i++)
1289 {
1290 if(((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
1291 {
1292 if(max_sc[6][i] < max_sc[5][i])
1293 fr_ps->header->tc_alloc[i] = 2;
1294 else fr_ps->header->tc_alloc[i] = 1;
1295 }
1296 else if(((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
1297 fr_ps->header->tc_alloc[i] = 2;
1298 else fr_ps->header->tc_alloc[i] = 0;
1299 }
1300
1301 for(i = 8; i < 12; i++)
1302 for(k = 2; k < 7; k++)
1303 min2[k][i] = 0.0;
1304
1305 for(i = 8; i < 12; i++)
1306 {
1307 for(k = 2; k < 7; k++)
1308 {
1309 for(l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
1310 {
1311 min2[k][i] += max_sc[k][l];
1312 }
1313 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
1314 }
1315
1316 if(((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
1317 {
1318 if(min2[6][i] < min2[5][i])
1319 fr_ps->header->tc_alloc[i] = 2;
1320 else fr_ps->header->tc_alloc[i] = 1;
1321 }
1322 else if(((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
1323 fr_ps->header->tc_alloc[i] = 2;
1324 else fr_ps->header->tc_alloc[i] = 0;
1325 }
1326 }
1327 */
1328 else {
1329 for (i = 0; i < 8; i++) {
1330 if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[5][i]))) >
1331 TC_ALLOC) {
1332 if (max_sc[6][i] < max_sc[5][i])
1333 min = 6;
1334 else
1335 min = 5;
1336 } else if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[6][i]))) >
1337 TC_ALLOC)
1338 min = 6;
1339 else
1340 min = 2;
1341
1342 switch (min) {
1343 case 5:
1344 fr_ps->header->tc_alloc[i] = 1;
1345 break;
1346
1347 case 6:
1348 fr_ps->header->tc_alloc[i] = 2;
1349 break;
1350
1351 case 2:
1352 fr_ps->header->tc_alloc[i] = 0;
1353 break;
1354 }
1355 }
1356
1357 for (i = 8; i < 12; i++) {
1358 for (k = 2; k < 7; k++) {
1359 min2[k][i] = 0.0;
1360 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1361 min2[k][i] += max_sc[k][l];
1362 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1363 }
1364
1365 if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1366 if (min2[6][i] < min2[5][i])
1367 min = 6;
1368 else
1369 min = 5;
1370 } else if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[6][i]))) >
1371 TC_ALLOC)
1372 min = 6;
1373 else
1374 min = 2;
1375
1376 switch (min) {
1377 case 5:
1378 fr_ps->header->tc_alloc[i] = 1;
1379 break;
1380
1381 case 6:
1382 fr_ps->header->tc_alloc[i] = 2;
1383 break;
1384
1385 case 2:
1386 fr_ps->header->tc_alloc[i] = 0;
1387 break;
1388 }
1389 }
1390 }
1391 }
1392
1393 /* 01/03/1995 JMZ Configuration 2/2 */
1394 if (center == 0 && surround == 2) {
1395 if (matrix == 3) {
1396 for (i = 0; i < 12; i++)
1397 fr_ps->header->tc_alloc[i] = 0;
1398 } else {
1399 for (i = 0; i < 8; i++) {
1400 if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[5][i]))) >
1401 TC_ALLOC) {
1402 if (max_sc[6][i] < max_sc[3][i])
1403 fr_ps->header->tc_alloc[i] = 3;
1404 else
1405 fr_ps->header->tc_alloc[i] = 2;
1406 } else if (((20 * log10 (max_sc[3][i])) - (20 * log10 (max_sc[6][i]))) >
1407 TC_ALLOC)
1408 fr_ps->header->tc_alloc[i] = 1;
1409 else
1410 fr_ps->header->tc_alloc[i] = 0;
1411 }
1412
1413 for (i = 8; i < 12; i++) {
1414 for (k = 2; k < 7; k++) {
1415 min2[k][i] = 0.0;
1416 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1417 min2[k][i] += max_sc[k][l];
1418 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1419 }
1420
1421 if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1422 if (min2[6][i] < min2[3][i])
1423 fr_ps->header->tc_alloc[i] = 3;
1424 else
1425 fr_ps->header->tc_alloc[i] = 2;
1426 } else if (((20 * log10 (min2[3][i])) - (20 * log10 (min2[6][i]))) >
1427 TC_ALLOC)
1428 fr_ps->header->tc_alloc[i] = 1;
1429 else
1430 fr_ps->header->tc_alloc[i] = 0;
1431 }
1432 }
1433 }
1434
1435 /* 01/03/1995 JMZ et Configuration 2/1 */
1436 if (center == 0 && surround == 1) {
1437 if (matrix == 3) {
1438 for (i = 0; i < 12; i++)
1439 fr_ps->header->tc_alloc[i] = 0;
1440 } else {
1441 for (i = 0; i < 8; i++) {
1442 if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[5][i]))) >
1443 TC_ALLOC) {
1444 if (max_sc[6][i] < max_sc[5][i])
1445 min = 6;
1446 else
1447 min = 5;
1448 } else if (((20 * log10 (max_sc[2][i])) - (20 * log10 (max_sc[6][i]))) >
1449 TC_ALLOC)
1450 min = 6;
1451 else
1452 min = 2;
1453
1454 switch (min) {
1455 case 5:
1456 fr_ps->header->tc_alloc[i] = 1;
1457 break;
1458
1459 case 6:
1460 fr_ps->header->tc_alloc[i] = 2;
1461 break;
1462
1463 case 2:
1464 fr_ps->header->tc_alloc[i] = 0;
1465 break;
1466 }
1467 }
1468
1469 for (i = 8; i < 12; i++) {
1470 for (k = 2; k < 7; k++) {
1471 min2[k][i] = 0.0;
1472 for (l = (sb_groups[i - 1] + 1); l <= sb_groups[i]; l++)
1473 min2[k][i] += max_sc[k][l];
1474 min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i - 1]);
1475 }
1476
1477 if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[5][i]))) > TC_ALLOC) {
1478 if (min2[6][i] < min2[5][i])
1479 min = 6;
1480 else
1481 min = 5;
1482 } else if (((20 * log10 (min2[2][i])) - (20 * log10 (min2[6][i]))) >
1483 TC_ALLOC)
1484 min = 6;
1485 else
1486 min = 2;
1487
1488 switch (min) {
1489 case 5:
1490 fr_ps->header->tc_alloc[i] = 1;
1491 break;
1492
1493 case 6:
1494 fr_ps->header->tc_alloc[i] = 2;
1495 break;
1496
1497 case 2:
1498 fr_ps->header->tc_alloc[i] = 0;
1499 break;
1500 }
1501 }
1502 }
1503 }
1504
1505 if (fr_ps->header->tc_alloc[0] == fr_ps->header->tc_alloc[1] &&
1506 fr_ps->header->tc_alloc[1] == fr_ps->header->tc_alloc[2] &&
1507 fr_ps->header->tc_alloc[2] == fr_ps->header->tc_alloc[3] &&
1508 fr_ps->header->tc_alloc[3] == fr_ps->header->tc_alloc[4] &&
1509 fr_ps->header->tc_alloc[4] == fr_ps->header->tc_alloc[5] &&
1510 fr_ps->header->tc_alloc[5] == fr_ps->header->tc_alloc[6] &&
1511 fr_ps->header->tc_alloc[6] == fr_ps->header->tc_alloc[7] &&
1512 fr_ps->header->tc_alloc[7] == fr_ps->header->tc_alloc[8] &&
1513 fr_ps->header->tc_alloc[8] == fr_ps->header->tc_alloc[9] &&
1514 fr_ps->header->tc_alloc[9] == fr_ps->header->tc_alloc[10] &&
1515 fr_ps->header->tc_alloc[10] == fr_ps->header->tc_alloc[11]) {
1516 fr_ps->header->tc_sbgr_select = 1;
1517 fr_ps->header->tc_allocation = fr_ps->header->tc_alloc[0];
1518 } else
1519 fr_ps->header->tc_sbgr_select = 0; /* added 8/20/93,SR */
1520 }
1521
1522
1523 /************************************************************************
1524 *
1525 * put_scale (Layer I)
1526 *
1527 * PURPOSE:Sets #max_sc# to the scalefactor index in #scalar.
1528 * This is used by Psychoacoustic Model I
1529 *
1530 ************************************************************************/
1531
put_scale(unsigned int (* scalar)[3][32],frame_params * fr_ps,double (* max_sc)[32])1532 void put_scale (unsigned int (*scalar)[3][32], frame_params * fr_ps,
1533 double (*max_sc)[32])
1534
1535 {
1536 int i, k;
1537 // int stereo = fr_ps->stereo;
1538 // int stereomc = fr_ps->stereomc;
1539 // int sblimit = fr_ps->sblimit;
1540
1541 /* for (k = 0; k < stereo+stereomc+2; k++) 960814 FdB bug for some configurations */
1542 for (k = 0; k < 7; k++)
1543 for (i = 0; i < SBLIMIT; i++)
1544 max_sc[k][i] = multiple[scalar[k][0][i]];
1545 }
1546
1547 /************************************************************************
1548 *
1549 * II_transmission_pattern (Layer II only)
1550 *
1551 * PURPOSE:For a given subband, determines whether to send 1, 2, or
1552 * all 3 of the scalefactors, and fills in the scalefactor
1553 * select information accordingly
1554 *
1555 * SEMANTICS: The subbands and channels are classified based on how much
1556 * the scalefactors changes over its three values (corresponding
1557 * to the 3 sets of 12 samples per subband). The classification
1558 * will send 1 or 2 scalefactors instead of three if the scalefactors
1559 * do not change much. The scalefactor select information,
1560 * #scfsi#, is filled in accordingly.
1561 *
1562 ************************************************************************/
1563
II_transmission_pattern(unsigned int (* scalar)[3][32],unsigned int (* scfsi)[32],frame_params * fr_ps)1564 void II_transmission_pattern (unsigned int (*scalar)[3][32],
1565 unsigned int (*scfsi)[32], frame_params * fr_ps)
1566 {
1567 // int stereo = fr_ps->stereo;
1568 int stereomc = fr_ps->stereomc;
1569 int sblimit;
1570 //int sblimit_mc = fr_ps->sblimit_mc;
1571 int sblimit_ml = fr_ps->sblimit_ml;
1572 int n_ml_ch = fr_ps->header->multiling_ch;
1573
1574 int dscf[2];
1575 int class[2], i, j, k;
1576
1577 static int pattern[5][5] = {
1578 {0x123, 0x122, 0x122, 0x133, 0x123},
1579 {0x113, 0x111, 0x111, 0x444, 0x113},
1580 {0x111, 0x111, 0x111, 0x333, 0x113},
1581 {0x222, 0x222, 0x222, 0x333, 0x123},
1582 {0x123, 0x122, 0x122, 0x133, 0x123}
1583 };
1584
1585 if (stereomc > 0 && fr_ps->sblimit_mc > fr_ps->sblimit)
1586 sblimit = fr_ps->sblimit_mc;
1587 else
1588 sblimit = fr_ps->sblimit;
1589
1590 /* for (k = 0; k < stereo+stereomc+2; k++) 960814 FdB bug for some configurations */
1591 for (k = 0; k < 12; k++)
1592 for (i = 0; i < SBLIMIT; i++) {
1593 dscf[0] = (scalar[k][0][i] - scalar[k][1][i]);
1594 dscf[1] = (scalar[k][1][i] - scalar[k][2][i]);
1595 for (j = 0; j < 2; j++) {
1596 if (dscf[j] <= -3)
1597 class[j] = 0;
1598 else if (dscf[j] > -3 && dscf[j] < 0)
1599 class[j] = 1;
1600 else if (dscf[j] == 0)
1601 class[j] = 2;
1602 else if (dscf[j] > 0 && dscf[j] < 3)
1603 class[j] = 3;
1604 else
1605 class[j] = 4;
1606 }
1607 switch (pattern[class[0]][class[1]]) {
1608 case 0x123:
1609 scfsi[k][i] = 0;
1610 break;
1611 case 0x122:
1612 scfsi[k][i] = 3;
1613 scalar[k][2][i] = scalar[k][1][i];
1614 break;
1615 case 0x133:
1616 scfsi[k][i] = 3;
1617 scalar[k][1][i] = scalar[k][2][i];
1618 break;
1619 case 0x113:
1620 scfsi[k][i] = 1;
1621 scalar[k][1][i] = scalar[k][0][i];
1622 break;
1623 case 0x111:
1624 scfsi[k][i] = 2;
1625 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
1626 break;
1627 case 0x222:
1628 scfsi[k][i] = 2;
1629 scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
1630 break;
1631 case 0x333:
1632 scfsi[k][i] = 2;
1633 scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
1634 break;
1635 case 0x444:
1636 scfsi[k][i] = 2;
1637 if (scalar[k][0][i] > scalar[k][2][i])
1638 scalar[k][0][i] = scalar[k][2][i];
1639 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
1640
1641
1642 } /* switch */
1643 } /* subband */
1644
1645 if (n_ml_ch > 0) {
1646 for (k = 7; k < 7 + n_ml_ch; k++)
1647 for (i = 0; i < sblimit_ml; i++) {
1648 dscf[0] = (scalar[k][0][i] - scalar[k][1][i]);
1649 dscf[1] = (scalar[k][1][i] - scalar[k][2][i]);
1650 for (j = 0; j < 2; j++) {
1651 if (dscf[j] <= -3)
1652 class[j] = 0;
1653 else if (dscf[j] > -3 && dscf[j] < 0)
1654 class[j] = 1;
1655 else if (dscf[j] == 0)
1656 class[j] = 2;
1657 else if (dscf[j] > 0 && dscf[j] < 3)
1658 class[j] = 3;
1659 else
1660 class[j] = 4;
1661 }
1662 switch (pattern[class[0]][class[1]]) {
1663 case 0x123:
1664 scfsi[k][i] = 0;
1665 break;
1666 case 0x122:
1667 scfsi[k][i] = 3;
1668 scalar[k][2][i] = scalar[k][1][i];
1669 break;
1670 case 0x133:
1671 scfsi[k][i] = 3;
1672 scalar[k][1][i] = scalar[k][2][i];
1673 break;
1674 case 0x113:
1675 scfsi[k][i] = 1;
1676 scalar[k][1][i] = scalar[k][0][i];
1677 break;
1678 case 0x111:
1679 scfsi[k][i] = 2;
1680 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
1681 break;
1682 case 0x222:
1683 scfsi[k][i] = 2;
1684 scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
1685 break;
1686 case 0x333:
1687 scfsi[k][i] = 2;
1688 scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
1689 break;
1690 case 0x444:
1691 scfsi[k][i] = 2;
1692 if (scalar[k][0][i] > scalar[k][2][i])
1693 scalar[k][0][i] = scalar[k][2][i];
1694 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
1695 } /* switch */
1696 } /* subband */
1697 }
1698 }
1699
1700 /************************************************************************
1701 *
1702 * I_encode_scale (Layer I)
1703 * II_encode_scale (Layer II)
1704 *
1705 * PURPOSE:The encoded scalar factor information is arranged and
1706 * queued into the output fifo to be transmitted.
1707 *
1708 * For Layer II, the three scale factors associated with
1709 * a given subband and channel are transmitted in accordance
1710 * with the scfsi, which is transmitted first.
1711 *
1712 ************************************************************************/
1713
II_encode_scale(unsigned int (* bit_alloc)[32],unsigned int (* scfsi)[32],unsigned int (* scalar)[3][32],unsigned int lfe_alloc,unsigned int lfe_scalar,frame_params * fr_ps,Bit_stream_struc * bs,int * l,int * z)1714 void II_encode_scale (unsigned int (*bit_alloc)[32], unsigned int (*scfsi)[32], unsigned int (*scalar)[3][32], /* JMZ 08/03/1995 Multilingual */
1715 unsigned int lfe_alloc,
1716 unsigned int lfe_scalar,
1717 frame_params * fr_ps,
1718 Bit_stream_struc * bs, int *l, int *z)
1719 {
1720 layer *info = fr_ps->header;
1721 int center = info->center;
1722 // int surround = info->surround;
1723 int stereo = fr_ps->stereo;
1724 int sblimit;
1725 int lfe = fr_ps->header->lfe;
1726 int i, j, k, m, n, pci;
1727 int pred;
1728
1729 if (*l == 0)
1730 sblimit = fr_ps->sblimit;
1731 else
1732 sblimit = fr_ps->sblimit_mc;
1733
1734 for (i = 0; i < sblimit; i++) {
1735 n = sbgrp[i];
1736 for (m = *l; m < *z; m++) {
1737 k = transmission_channel (fr_ps, n, m);
1738
1739 if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
1740 putbits (bs, scfsi[k][i], 2);
1741 }
1742 }
1743
1744 pred = 0;
1745 if (*l == stereo) {
1746 if (fr_ps->header->mc_prediction_on == 1) {
1747 for (i = 0; i < 8; i++) {
1748 if (fr_ps->header->mc_pred[i] == 1) {
1749 for (m = 0; m < n_pred_coef[fr_ps->header->dyn_cross[i]]; m++) {
1750 if (fr_ps->header->predsi[i][m] != 0) {
1751 putbits (bs, fr_ps->header->delay_comp[i][m], 3);
1752 pred += 3;
1753 for (pci = 0; pci < fr_ps->header->predsi[i][m]; pci++) {
1754 putbits (bs, fr_ps->header->pred_coef[i][m][pci], 8);
1755 pred += 8;
1756 }
1757 }
1758 }
1759 }
1760 }
1761 }
1762 }
1763
1764 if (*l == stereo && lfe && lfe_alloc)
1765 putbits (bs, lfe_scalar, 6);
1766
1767 for (i = 0; i < sblimit; i++) {
1768 n = sbgrp[i];
1769 for (m = *l; m < *z; m++) {
1770 k = transmission_channel (fr_ps, n, m);
1771
1772 if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
1773 switch (scfsi[k][i]) {
1774 case 0:
1775 for (j = 0; j < 3; j++)
1776 putbits (bs, scalar[k][j][i], 6);
1777 break;
1778 case 1:
1779 case 3:
1780 putbits (bs, scalar[k][0][i], 6);
1781 putbits (bs, scalar[k][2][i], 6);
1782 break;
1783 case 2:
1784 putbits (bs, scalar[k][0][i], 6);
1785 }
1786 }
1787 }
1788 }
1789
1790 /*JMZ 24/2/95 Multilingual , WtK 7/8/95*/
II_encode_scale_ml(unsigned int (* bit_alloc)[32],unsigned int (* scfsi)[32],unsigned int (* scalar)[3][32],frame_params * fr_ps,Bit_stream_struc * bs,int * l,int * z)1791 void II_encode_scale_ml (unsigned int (*bit_alloc)[32],
1792 unsigned int (*scfsi)[32],
1793 unsigned int (*scalar)[3][32], frame_params * fr_ps,
1794 Bit_stream_struc * bs, int *l, int *z)
1795 {
1796 // int stereo = fr_ps->stereo;
1797 int sblimit_ml = fr_ps->sblimit_ml;
1798 int i, j, k, m;
1799
1800 for (i = 0; i < sblimit_ml; i++)
1801 for (m = *l; m < *z; m++) {
1802 k = m;
1803 if (bit_alloc[k][i])
1804 putbits (bs, scfsi[k][i], 2);
1805 }
1806 for (i = 0; i < sblimit_ml; i++)
1807 for (m = *l; m < *z; m++) {
1808 k = m;
1809 if (bit_alloc[k][i])
1810 switch (scfsi[k][i]) {
1811 case 0:
1812 for (j = 0; j < 3; j++)
1813 putbits (bs, scalar[k][j][i], 6);
1814 break;
1815 case 1:
1816 case 3:
1817 putbits (bs, scalar[k][0][i], 6);
1818 putbits (bs, scalar[k][2][i], 6);
1819 break;
1820 case 2:
1821 putbits (bs, scalar[k][0][i], 6);
1822 break;
1823 }
1824 }
1825 }
1826
1827 /*=======================================================================\
1828 | |
1829 | The following routines are done after the masking threshold |
1830 | has been calculated by the fft analysis routines in the Psychoacoustic |
1831 | model. Using the MNR calculated, the actual number of bits allocated |
1832 | to each subband is found iteratively. |
1833 | |
1834 \=======================================================================*/
1835
1836 /************************************************************************
1837 *
1838 * I_bits_for_nonoise (Layer I)
1839 * II_bits_for_nonoise (Layer II)
1840 *
1841 * PURPOSE:Returns the number of bits required to produce a
1842 * mask-to-noise ratio better or equal to the noise/no_noise threshold.
1843 *
1844 * SEMANTICS:
1845 * bbal = # bits needed for encoding bit allocation
1846 * bsel = # bits needed for encoding scalefactor select information
1847 * banc = # bits needed for ancillary data (header info included)
1848 *
1849 * For each subband and channel, will add bits until one of the
1850 * following occurs:
1851 * - Hit maximum number of bits we can allocate for that subband
1852 * - MNR is better than or equal to the minimum masking level
1853 * (NOISY_MIN_MNR)
1854 * Then the bits required for scalefactors, scfsi, bit allocation,
1855 * and the subband samples are tallied (#req_bits#) and returned.
1856 *
1857 * (NOISY_MIN_MNR) is the smallest MNR a subband can have before it is
1858 * counted as 'noisy' by the logic which chooses the number of JS
1859 * subbands.
1860 *
1861 * Joint stereo is supported.
1862 *
1863 ************************************************************************/
1864
1865 /*static double snr[18] = {0.00, 7.00, 11.00, 16.00, 20.84,
1866 25.28, 31.59, 37.75, 43.84,
1867 49.89, 55.93, 61.96, 67.98, 74.01,
1868 80.03, 86.05, 92.01, 98.01};*/
1869 static double snr[18] = { 0.00, 6.03, 11.80, 15.81, /* 0, 3, 5, 7 */
1870 19.03, 23.50, 29.82, 35.99, /* 9,15,31,63 */
1871 42.08, 48.13, 54.17, 60.20, /* 127, ... */
1872 66.22, 72.25, 78.27, 84.29, /* 2047, ... */
1873 90.31, 96.33
1874 }; /* 16383, ... */
1875
1876
II_bits_for_nonoise(double (* perm_smr)[32],unsigned int (* scfsi)[32],frame_params * fr_ps,int a,int b,int * aiff)1877 int II_bits_for_nonoise (double (*perm_smr)[32], unsigned int (*scfsi)[32],
1878 frame_params * fr_ps, int a, int b, int *aiff)
1879 {
1880 int sb, ch, ba, i;
1881 int stereo = fr_ps->stereo;
1882 int stereomc = fr_ps->stereomc;
1883 int stereoaug = fr_ps->stereoaug;
1884 int n_ml_ch = fr_ps->header->multiling_ch; /* 23/03/1995 JMZ Multilingual */
1885 int sblimit = fr_ps->sblimit;
1886 int sblimit_mc = fr_ps->sblimit_mc;
1887 int sblimit_ml = fr_ps->sblimit_ml;
1888 int jsbound = fr_ps->jsbound;
1889 al_table *alloc = fr_ps->alloc;
1890 al_table *alloc_mc = fr_ps->alloc_mc;
1891 al_table *alloc_ml = fr_ps->alloc_ml;
1892 int bbal = 0;
1893 int berr; /* before: =0 92-08-11 shn */
1894 int banc = 32; /* header ISO Layer II */
1895 int bancmc = 0; /* header multichannel = 93, 5.7.93,SR */
1896 int bancext = 0; /* header multichannel = 93, 5.7.93,SR */
1897 int maxAlloc, sel_bits, sc_bits, smp_bits;
1898 int req_bits = 0;
1899 int tca_bits, dynx_bits; //, aug_tca_bits;
1900 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
1901
1902 if (verbosity >= 3)
1903 printf ("bits_for_nonoise\n");
1904
1905 if (*aiff == 1) {
1906 if (fr_ps->header->ext_bit_stream_present == 0)
1907 bancmc += 35; /* mc_header + crc + tc_sbgr_select+ dyn_cross_on +
1908 mc_prediction_on 01/05/94, SR new! 05/04/94, SR */
1909 else {
1910 bancmc += 43;
1911 bancext = 40;
1912 }
1913
1914 /* 960627 FdB TCA bits dependent on configuration */
1915 if (fr_ps->config == 320)
1916 tca_bits = 3;
1917 else if (fr_ps->config == 310)
1918 tca_bits = 3;
1919 else if (fr_ps->config == 220)
1920 tca_bits = 2;
1921 else if (fr_ps->config == 300 || fr_ps->config == 302
1922 || fr_ps->config == 210)
1923 tca_bits = 2;
1924 else
1925 tca_bits = 0;
1926
1927 if (fr_ps->header->tc_sbgr_select == 0)
1928 bancmc += 12 * tca_bits;
1929 else
1930 bancmc += tca_bits;
1931
1932 if (fr_ps->header->dyn_cross_on == 1) {
1933 /* 960627 FdB DynX dependent on configuration */
1934 if (fr_ps->config == 320)
1935 dynx_bits = 4;
1936 else if (fr_ps->config == 310 || fr_ps->config == 220)
1937 dynx_bits = 3;
1938 else if (fr_ps->config == 300 || fr_ps->config == 302
1939 || fr_ps->config == 210)
1940 dynx_bits = 1;
1941 else
1942 dynx_bits = 0;
1943
1944 bancmc = 1 + 12 * dynx_bits;
1945 if (fr_ps->header->surround == 3)
1946 bancmc += 12; /* now with dyn_second_stereo, 17/02/95, SR */
1947 }
1948
1949 if (fr_ps->header->mc_prediction_on == 1) {
1950 /* mc_pred, predsi, delay_comp, pred_coef */
1951 /* bancmc += 8 * (3 + 6 * (3 + 3 * 8)); */
1952 for (i = 0; i < 8; i++) {
1953 bancmc += 3;
1954 bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 27;
1955 }
1956 }
1957 } else
1958 bancmc = 0;
1959
1960 if (fr_ps->header->error_protection)
1961 berr = 16;
1962 else
1963 berr = 0; /* added 92-08-11 shn */
1964
1965 for (sb = 0; sb < jsbound; sb++)
1966 bbal += (stereo) * (*alloc)[sb][0].bits;
1967 for (sb = jsbound; sb < sblimit; sb++)
1968 bbal += (stereo - 1) * (*alloc)[sb][0].bits;
1969 for (sb = 0; sb < sblimit_mc; sb++)
1970 bbal += (stereomc + stereoaug) * (*alloc_mc)[sb][0].bits;
1971 for (sb = 0; sb < sblimit_ml; sb++)
1972 bbal += (n_ml_ch) * (*alloc_ml)[sb][0].bits;
1973
1974 req_bits = banc + bancmc + bbal + berr;
1975
1976 for (sb = 0; sb < SBLIMIT; sb++)
1977 for (ch = 0; ch < ((sb < jsbound) ? stereo + stereomc + stereoaug : 1);
1978 ch++) {
1979 if (ch < stereo) {
1980 alloc = fr_ps->alloc;
1981 sblimit = fr_ps->sblimit;
1982 } else {
1983 alloc = fr_ps->alloc_mc;
1984 sblimit = fr_ps->sblimit_mc;
1985 }
1986 if (sb < sblimit) {
1987 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
1988 sel_bits = sc_bits = smp_bits = 0;
1989 for (ba = 0; ba < maxAlloc - 1; ba++)
1990 if ((-perm_smr[ch][sb] +
1991 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
1992 >= fr_ps->mnr_min)
1993 break;
1994 if (((b - a) >= 1) && (sb >= jsbound)) { /* check other JS channels */
1995 for (; ba < maxAlloc - 1; ba++)
1996 if ((-perm_smr[1 - ch][sb] +
1997 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
1998 >= fr_ps->mnr_min)
1999 break;
2000 }
2001
2002 if (ba > 0) {
2003 smp_bits = 12 * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
2004 /* scale factor bits required for subband */
2005 sel_bits = 2;
2006 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
2007
2008 if (stereo == 2 && sb >= jsbound) {
2009 /* each new js sb has L+R scfsis */
2010 sel_bits += 2;
2011 sc_bits += 6 * sfsPerScfsi[scfsi[1 - ch][sb]];
2012 }
2013
2014 req_bits += (smp_bits + sel_bits + sc_bits);
2015 }
2016 }
2017 }
2018
2019 if (n_ml_ch > 0) {
2020 /* remaining part from jsbound to sblimit_ml in II_bits_for_indi() , WtK */
2021 for (sb = 0; sb < (jsbound < sblimit_ml) ? jsbound : sblimit_ml; sb++) {
2022 for (ch = 7; ch < 7 + n_ml_ch; ch++) {
2023 maxAlloc = (1 << (*alloc_ml)[sb][0].bits) - 1;
2024 sel_bits = sc_bits = smp_bits = 0;
2025 for (ba = 0; ba < maxAlloc - 1; ba++)
2026 if ((-perm_smr[ch][sb] +
2027 snr[(*alloc_ml)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
2028 >= fr_ps->mnr_min)
2029 break;
2030 if (ba > 0) {
2031 if (fr_ps->header->multiling_fs == 1)
2032 smp_bits =
2033 6 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
2034 else
2035 smp_bits =
2036 12 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
2037
2038 /* scale factor bits required for subband */
2039 sel_bits = 2;
2040 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
2041
2042 req_bits += (smp_bits + sel_bits + sc_bits);
2043 }
2044 }
2045 }
2046 }
2047
2048 return req_bits;
2049 }
2050
2051
2052 /**********************************************************************/
2053 /*now for the independent channels, 8/6/93, SR */
2054 /**********************************************************************/
II_bits_for_indi(double (* perm_smr)[32],unsigned int (* scfsi)[32],frame_params * fr_ps,int * a,int * b,int * aiff)2055 int II_bits_for_indi (double (*perm_smr)[32], unsigned int (*scfsi)[32],
2056 frame_params * fr_ps, int *a, int *b, int *aiff)
2057 {
2058 int sb, ch, ba;
2059 int stereo = fr_ps->stereo;
2060 int stereomc = fr_ps->stereomc;
2061 int stereoaug = fr_ps->stereoaug;
2062 int n_ml_ch = fr_ps->header->multiling_ch; /* 23/03/1995 JMZ Multilingual */
2063 int sblimit = fr_ps->sblimit_mc;
2064 int sblimit_ml = fr_ps->sblimit_ml;
2065 int jsbound = fr_ps->jsbound;
2066 al_table *alloc = fr_ps->alloc_mc;
2067 al_table *alloc_ml = fr_ps->alloc_ml;
2068 int req_bits = 0;
2069 int maxAlloc, sel_bits, sc_bits, smp_bits;
2070 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
2071
2072 for (sb = jsbound; sb < sblimit; sb++) {
2073 for (ch = stereo; ch < stereo + stereomc + stereoaug; ch++) {
2074 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
2075 sel_bits = sc_bits = smp_bits = 0;
2076 for (ba = 0; ba < maxAlloc - 1; ba++)
2077 if ((-perm_smr[ch][sb] +
2078 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
2079 >= fr_ps->mnr_min)
2080 break; /* we found enough bits */
2081
2082 if (ba > 0) {
2083 smp_bits = 12 * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
2084 /* scale factor bits required for subband */
2085 sel_bits = 2;
2086 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
2087 req_bits += smp_bits + sel_bits + sc_bits;
2088 }
2089 }
2090 }
2091
2092 if (n_ml_ch > 0)
2093 for (sb = jsbound; sb < sblimit_ml; sb++) {
2094 for (ch = 7; ch < 7 + n_ml_ch; ch++) {
2095 maxAlloc = (1 << (*alloc_ml)[sb][0].bits) - 1;
2096 sel_bits = sc_bits = smp_bits = 0;
2097 for (ba = 0; ba < maxAlloc - 1; ba++)
2098 if ((-perm_smr[ch][sb] +
2099 snr[(*alloc_ml)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
2100 >= fr_ps->mnr_min)
2101 break; /* we found enough bits */
2102
2103 if (ba > 0) {
2104 if (fr_ps->header->multiling_fs == 1)
2105 smp_bits =
2106 6 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
2107 else
2108 smp_bits =
2109 12 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
2110
2111 /* scale factor bits required for subband */
2112 sel_bits = 2;
2113 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
2114
2115 req_bits += smp_bits + sel_bits + sc_bits;
2116 }
2117 }
2118 }
2119
2120 return req_bits;
2121 }
2122
2123
2124 /************************************************************************
2125 *
2126 * I_main_bit_allocation (Layer I)
2127 * II_main_bit_allocation (Layer II)
2128 *
2129 * PURPOSE:For joint stereo mode, determines which of the 4 joint
2130 * stereo modes is needed. Then calls *_a_bit_allocation(), which
2131 * allocates bits for each of the subbands until there are no more bits
2132 * left, or the MNR is at the noise/no_noise threshold.
2133 *
2134 * SEMANTICS:
2135 *
2136 * For joint stereo mode, joint stereo is changed to stereo if
2137 * there are enough bits to encode stereo at or better than the
2138 * no-noise threshold (fr_ps->mnr_min). Otherwise, the system
2139 * iteratively allocates less bits by using joint stereo until one
2140 * of the following occurs:
2141 * - there are no more noisy subbands (MNR >= fr_ps->mnr_min)
2142 * - mode_ext has been reduced to 0, which means that all but the
2143 * lowest 4 subbands have been converted from stereo to joint
2144 * stereo, and no more subbands may be converted
2145 *
2146 * This function calls *_bits_for_nonoise() and *_a_bit_allocation().
2147 *
2148 ************************************************************************/
2149
II_main_bit_allocation(double (* perm_smr)[32],double (* ltmin)[32],unsigned int (* scfsi)[32],unsigned int (* bit_alloc)[32],int * adb,frame_params * fr_ps,int * aiff,double (* sb_sample)[3][12][32],unsigned int (* scalar)[3][32],double (* max_sc)[32],double (* buffer)[1152],double (* spiki)[32],double (* joint_sample)[3][12][32],unsigned int (* j_scale)[3][32],int dyn_cr,int aug_dyn_cr,unsigned int (* scfsi_dyn)[SBLIMIT],unsigned int (* scalar_dyn)[3][SBLIMIT])2150 void II_main_bit_allocation (double (*perm_smr)[32], /* minimum masking level *//* JMZ 08/03/1995 Multilingual */
2151 double (*ltmin)[32], /* minimum masking level *//* JMZ 08/03/1995 Multilingual */
2152 unsigned int (*scfsi)[32], /* JMZ 08/03/1995 Multilingual */
2153 unsigned int (*bit_alloc)[32], /* JMZ 08/03/1995 Multilingual */
2154 int *adb, frame_params * fr_ps, int *aiff, double (*sb_sample)[3][12][32], /* JMZ 08/03/1995 Multilingual */
2155 unsigned int (*scalar)[3][32], /* JMZ 08/03/1995 Multilingual */
2156 double (*max_sc)[32], /* JMZ 08/03/1995 Multilingual */
2157 double (*buffer)[1152], /* JMZ 08/03/1995 Multilingual */
2158 double (*spiki)[32], /* JMZ 08/03/1995 Multilingual */
2159 double (*joint_sample)[3][12][32],
2160 unsigned int (*j_scale)[3][32],
2161 int dyn_cr,
2162 int aug_dyn_cr,
2163 unsigned int (*scfsi_dyn)[SBLIMIT],
2164 unsigned int (*scalar_dyn)[3][SBLIMIT]
2165 )
2166 {
2167 int noisy_sbs; //, nn;
2168 int mode, mode_ext, lay; //, modemc_hlp;
2169 int rq_db, bits_nonoise, bits_indi;
2170 int a, b, sb;
2171 // int i, l, m , k, av_db = *adb, bits1, bits2
2172 //int sbbound = -30;
2173 float adb_help;
2174 //double smr_pred[7][SBLIMIT];
2175 //float preco[12][4];
2176 //float prega[3][32];
2177 int ba, ba1, subgr;
2178 int maxAlloc;
2179 al_table *alloc = fr_ps->alloc;
2180 //int stereoaug = fr_ps->stereoaug;
2181 //static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
2182 double sb_sample_sum[5][3][12][32];
2183 int subfr, ch, sum_chs;
2184 unsigned int scalar_sum[5][3][SBLIMIT];
2185 unsigned int scfsi_sum[5][SBLIMIT];
2186
2187 /***************************layer II two channels *******************/
2188 if (*aiff == 0) {
2189 if ((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO) {
2190 a = 0;
2191 b = 1;
2192 fr_ps->header->mode = MPG_MD_STEREO;
2193 fr_ps->header->mode_ext = 0;
2194 fr_ps->jsbound = fr_ps->sblimit;
2195 if ((rq_db = II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff) > *adb)) {
2196 fr_ps->header->mode = MPG_MD_JOINT_STEREO;
2197 mode_ext = 4; /* 3 is least severe reduction */
2198 lay = fr_ps->header->lay;
2199 do {
2200 --mode_ext;
2201 fr_ps->jsbound = js_bound (lay, mode_ext);
2202 rq_db = II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff);
2203 } while ((rq_db > *adb) && (mode_ext > 0));
2204 fr_ps->header->mode_ext = mode_ext;
2205 } /* well we either eliminated noisy sbs or mode_ext == 0 */
2206 }
2207 }
2208 /***************** layer II five channels ****************************/
2209 else {
2210 trans_chan (fr_ps);
2211 if (verbosity >= 2)
2212 printf
2213 ("js actual_mode: %d mode: %d mode_ext: %d jsbound: %d dyn_cr: %2d aug_dyn_cr: %2d\n",
2214 fr_ps->actual_mode, fr_ps->header->mode, fr_ps->header->mode_ext,
2215 fr_ps->jsbound, dyn_cr, aug_dyn_cr);
2216
2217 for (sb = 0; sb < SBLIMIT; ++sb) {
2218 subgr = sbgrp[sb];
2219 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
2220 for (ba = 0; ba < maxAlloc - 1; ++ba)
2221 if ((-perm_smr[0][sb] +
2222 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
2223 >= fr_ps->mnr_min)
2224 break; /* we found enough bits */
2225 for (ba1 = 0; ba1 < maxAlloc - 1; ++ba1)
2226 if ((-perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][0]][sb] +
2227 snr[(*alloc)[sb][ba1].quant + ((ba1 > 0) ? 1 : 0)])
2228 >= fr_ps->mnr_min)
2229 break; /* we found enough bits */
2230 if (ba1 > ba) {
2231 perm_smr[0][sb] =
2232 perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][0]][sb];
2233 ba = ba1;
2234 }
2235
2236 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
2237 for (ba = 0; ba < maxAlloc - 1; ++ba)
2238 if ((-perm_smr[1][sb] +
2239 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)])
2240 >= fr_ps->mnr_min)
2241 break; /* we found enough bits */
2242 for (ba1 = 0; ba1 < maxAlloc - 1; ++ba1)
2243 if ((-perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][1]][sb] +
2244 snr[(*alloc)[sb][ba1].quant + ((ba1 > 0) ? 1 : 0)])
2245 >= fr_ps->mnr_min)
2246 break; /* we found enough bits */
2247 if (ba1 > ba) {
2248 perm_smr[1][sb] =
2249 perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][1]][sb];
2250 ba = ba1;
2251 }
2252 }
2253
2254 /************ 05/24/95, SR, dyn_cross talk **********************/
2255 if (dyn_cr != 0 || (fr_ps->header->dyn_cross_LR &&
2256 (fr_ps->config == 302 || fr_ps->config == 202
2257 || fr_ps->config == 102))) {
2258 combine (fr_ps, sb_sample, sb_sample_sum);
2259
2260 for (ch = 0; ch < CHANMAX2; ch++) {
2261 for (subfr = 0; subfr < 3; subfr++)
2262 for (sb = 0; sb < fr_ps->sblimit; sb++)
2263 scalar_dyn[ch][subfr][sb] = scalar[ch][subfr][sb];
2264
2265 scfsi_calc_dyn (scalar_dyn, ch, fr_ps->sblimit, scfsi_dyn);
2266 scfsi_calc (scalar, ch, fr_ps->sblimit, scfsi);
2267 }
2268
2269 if (fr_ps->config == 320)
2270 sum_chs = 4;
2271 else if (fr_ps->config == 302 || fr_ps->config == 310
2272 || fr_ps->config == 220 || fr_ps->config == 202
2273 || fr_ps->config == 102)
2274 sum_chs = 1;
2275 else
2276 sum_chs = 0;
2277
2278 for (ch = 0; ch < sum_chs; ch++) {
2279 scf_calc (sb_sample_sum, fr_ps->sblimit, ch, scalar_sum);
2280 scfsi_calc_dyn (scalar_sum, ch, fr_ps->sblimit, scfsi_sum);
2281 }
2282 take_dyn_cross (dyn_cr, fr_ps, sb_sample_sum, scalar_sum, scfsi_sum,
2283 scfsi_dyn, sb_sample, scalar, scfsi);
2284 }
2285
2286 /************ 05/24/95, SR, dyn_cross talk **********************/
2287 if ((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO) {
2288 a = 0;
2289 b = 1;
2290 fr_ps->header->mode = MPG_MD_STEREO;
2291 fr_ps->header->mode_ext = 0;
2292 fr_ps->jsbound = fr_ps->sblimit;
2293 adb_help = *adb;
2294 bits_nonoise = II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff);
2295 bits_indi = II_bits_for_indi (perm_smr, scfsi, fr_ps, &a, &b, aiff);
2296 rq_db = ((bits_nonoise / 10) % 2) * 10 * bits_nonoise + bits_indi;
2297 if (verbosity >= 2)
2298 printf ("rq_db: %5d ( %5d + %5d ) available: %5d\n",
2299 rq_db, bits_nonoise, bits_indi, *adb);
2300 if (rq_db > adb_help) {
2301 fr_ps->header->mode = MPG_MD_JOINT_STEREO;
2302 mode_ext = 4; /* 3 is least severe reduction */
2303 lay = fr_ps->header->lay;
2304 do {
2305 --mode_ext;
2306 fr_ps->jsbound = js_bound (lay, mode_ext);
2307 bits_nonoise =
2308 II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff);
2309 bits_indi = II_bits_for_indi (perm_smr, scfsi, fr_ps, &a, &b, aiff);
2310 rq_db = ((bits_nonoise / 10) % 2) * 10 * bits_nonoise + bits_indi;
2311 if (verbosity >= 2)
2312 printf ("rq_db: %5d ( %5d + %5d ) available: %5d\n",
2313 rq_db, bits_nonoise, bits_indi, *adb);
2314 } while ((rq_db > *adb) && (mode_ext > 0));
2315 fr_ps->header->mode_ext = mode_ext;
2316 } /* well we either eliminated noisy sbs or mode_ext == 0 */
2317 }
2318 } /* end of else (five-channel) */
2319
2320 noisy_sbs =
2321 II_a_bit_allocation (perm_smr, scfsi, bit_alloc, adb, fr_ps, aiff);
2322 }
2323
2324 /************************************************************************
2325 *
2326 * I_a_bit_allocation (Layer I)
2327 * II_a_bit_allocation (Layer II)
2328 *
2329 * PURPOSE:Adds bits to the subbands with the lowest mask-to-noise
2330 * ratios, until the maximum number of bits for the subband has
2331 * been allocated.
2332 *
2333 * SEMANTICS:
2334 * 1. Find the subband and channel with the smallest MNR (#min_sb#,
2335 * and #min_ch#)
2336 * 2. Calculate the increase in bits needed if we increase the bit
2337 * allocation to the next higher level
2338 * 3. If there are enough bits available for increasing the resolution
2339 * in #min_sb#, #min_ch#, and the subband has not yet reached its
2340 * maximum allocation, update the bit allocation, MNR, and bits
2341 * available accordingly
2342 * 4. Repeat until there are no more bits left, or no more available
2343 * subbands. (A subband is still available until the maximum
2344 * number of bits for the subband has been allocated, or there
2345 * aren't enough bits to go to the next higher resolution in the
2346 * subband.)
2347 *
2348 ************************************************************************/
2349
2350
II_a_bit_allocation(double (* perm_smr)[32],unsigned int (* scfsi)[32],unsigned int (* bit_alloc)[32],int * adb,frame_params * fr_ps,int * aiff)2351 int II_a_bit_allocation (double (*perm_smr)[32], unsigned int (*scfsi)[32],
2352 unsigned int (*bit_alloc)[32], int *adb,
2353 frame_params * fr_ps, int *aiff)
2354 {
2355 int n_ml_ch = fr_ps->header->multiling_ch; /*JMZ 08/03/95 Multilingual */
2356 int i, min_ch, min_sb, oth_ch=0, k, increment, scale, seli, ba, j, l;
2357 // int increment1, scale1, seli1;
2358 int adb_hlp; //, adb_hlp1, adb_hlp2;
2359 int bspl, bscf, bsel, ad, noisy_sbs;
2360 int bspl_mpg1, bscf_mpg1, bsel_mpg1;
2361 double mnr[14][SBLIMIT], small; /* JMZ 08/03/1995 Multilingual */
2362 char used[14][SBLIMIT]; /* JMZ 08/03/1995 Multilingual */
2363 //MFC layer *info = fr_ps->header;
2364 // int center = info->center;
2365 // int surround = info->surround;
2366 int stereo = fr_ps->stereo;
2367 int stereomc = fr_ps->stereomc;
2368 int stereoaug = fr_ps->stereoaug;
2369 int sblimit = fr_ps->sblimit;
2370 int sblimit_mc = fr_ps->sblimit_mc;
2371 int sblimit_ml = fr_ps->sblimit_ml;
2372 int jsbound = fr_ps->jsbound;
2373 al_table *alloc = fr_ps->alloc;
2374 al_table *alloc_mc = fr_ps->alloc_mc;
2375 al_table *alloc_ml = fr_ps->alloc_ml;
2376 // double dynsmr = 0.0; /* border of SMR for dynamic datarate */
2377 static char init = 0;
2378 static int banc, berr;
2379 int bbal, bancmc, bancext, bbal_mpg1;
2380 int ll=0, pred, adb_mpg1, sbgr, sb, tca_bits, dynx_bits; //MFC, aug_tca_bits;
2381 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
2382
2383 if (!init) {
2384 init = 1;
2385 banc = 32; /* banc: bits for header */ ;
2386 /* 960627 FdB reserve extra bits dependent wrt bugs */
2387 banc += 200;
2388
2389 if (fr_ps->header->error_protection)
2390 berr = 16;
2391 else
2392 berr = 0; /* added 92-08-11 shn */
2393 }
2394
2395 pred = 0;
2396 bancmc = 0;
2397 bbal = 0;
2398 bbal_mpg1 = 0;
2399 bancext = 0;
2400 adb_mpg1 = bitrate[fr_ps->header->lay - 1][fr_ps->header->bitrate_index] * 24;
2401 /* 960814 FdB next line added for debugging */
2402 /* adb_mpg1 -= banc; */
2403
2404 if (*aiff == 1) {
2405 if (fr_ps->actual_mode == MPG_MD_JOINT_STEREO) {
2406 for (i = 0; i < jsbound; ++i)
2407 bbal += (stereo) * (*alloc)[i][0].bits;
2408 for (i = jsbound; i < sblimit; ++i)
2409 bbal += (stereo - 1) * (*alloc)[i][0].bits;
2410 for (i = 0; i < sblimit_mc; ++i)
2411 bbal += (stereomc + stereoaug) * (*alloc_mc)[i][0].bits;
2412 for (i = 0; i < sblimit_ml; ++i)
2413 bbal += (n_ml_ch) * (*alloc_ml)[i][0].bits;
2414 } else {
2415 for (i = 0; i < sblimit; ++i)
2416 bbal += (stereo) * (*alloc)[i][0].bits;
2417 for (i = 0; i < sblimit_mc; ++i)
2418 bbal += (stereomc + stereoaug) * (*alloc_mc)[i][0].bits;
2419 for (i = 0; i < sblimit_ml; ++i)
2420 bbal += (n_ml_ch) * (*alloc_ml)[i][0].bits;
2421 }
2422
2423 if (fr_ps->header->center == 3)
2424 bbal -= 41;
2425 } else {
2426 for (i = 0; i < jsbound; ++i)
2427 bbal += stereo * (*alloc)[i][0].bits;
2428 for (i = jsbound; i < sblimit; ++i)
2429 bbal += (*alloc)[i][0].bits;
2430 }
2431
2432 if (fr_ps->header->dyn_cross_on == 1)
2433 for (i = 0; i < 12; i++) {
2434 bbal -=
2435 dyn_bbal (fr_ps->config, fr_ps->header->center,
2436 fr_ps->header->dyn_cross[i], i);
2437 if (fr_ps->header->surround == 3)
2438 bbal -= dyn_bbal_2ndst (fr_ps->header->dyn_second_stereo[i], i);
2439 }
2440
2441
2442 if (fr_ps->header->ext_bit_stream_present == 0)
2443 bancmc += 35; /* mc_header + crc + tc_sbgr_select+ dyn_cross_on +
2444 mc_prediction_on 01/05/94, SR new! 05/04/94, SR */
2445 else {
2446 bancmc += 43;
2447 bancext = 40;
2448 }
2449
2450 /* 960627 FdB TCA bits dependent on configuration */
2451 if (fr_ps->config == 320)
2452 tca_bits = 3;
2453 else if (fr_ps->config == 310)
2454 tca_bits = 3;
2455 else if (fr_ps->config == 220)
2456 tca_bits = 2;
2457 else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
2458 tca_bits = 2;
2459 else
2460 tca_bits = 0;
2461
2462 if (fr_ps->header->tc_sbgr_select == 0)
2463 bancmc += 12 * tca_bits;
2464 else
2465 bancmc += tca_bits;
2466
2467 if (fr_ps->header->dyn_cross_on == 1) {
2468 /* 960627 FdB DynX dependent on configuration */
2469 if (fr_ps->config == 320)
2470 dynx_bits = 4;
2471 else if (fr_ps->config == 310 || fr_ps->config == 220)
2472 dynx_bits = 3;
2473 else if (fr_ps->config == 300 || fr_ps->config == 302
2474 || fr_ps->config == 210)
2475 dynx_bits = 1;
2476 else
2477 dynx_bits = 0;
2478
2479 bancmc += 12 * dynx_bits + 1;
2480 if (fr_ps->header->surround == 3)
2481 bancmc += 12; /* now with dyn_second_stereo, 17/02/95, SR */
2482 }
2483
2484 if (fr_ps->header->mc_prediction_on == 1) {
2485 /* for (i = 0; i < 8; i++)
2486 {
2487 bancmc += 1;
2488 if (fr_ps->header->mc_pred[i] == 1)
2489 bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 2;
2490 }
2491 */
2492 /* mc_pred, predsi, delay_comp, pred_coef */
2493 /* bancmc += 8 * (3 + 6 * (3 + 3 * 8)); */
2494 for (i = 0; i < 8; i++) {
2495 bancmc += 3;
2496 bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 27;
2497 }
2498 }
2499
2500 for (i = 0; i < SBLIMIT; i++)
2501 /* for (k = 0; k < (stereo+stereomc+2); k++) 960814 FdB bug for some configurations */
2502 for (k = 0; k < 12; k++) {
2503 mnr[k][i] = snr[0] - perm_smr[k][i];
2504 /* mask-to-noise-level = signal-to-noise-level - minimum-masking- */
2505 /* threshold */
2506
2507 bit_alloc[k][i] = 0;
2508 used[k][i] = 0;
2509 }
2510
2511 for (i = 0; i < sblimit_ml; i++)
2512 for (k = 7; k < 7 + n_ml_ch; k++) {
2513 mnr[k][i] = snr[0] - perm_smr[k][i];
2514 /* mask-to-noise-level = signal-to-noise-level - minimum-masking- */
2515 /* threshold */
2516
2517 bit_alloc[k][i] = 0;
2518 used[k][i] = 0;
2519 }
2520
2521 /* dyamic crosstalk, lock sbgr which are use for dyncr. */
2522 if (fr_ps->header->dyn_cross_on == 1) {
2523 for (sbgr = 0; sbgr < SBGRS; sbgr++) {
2524 for (sb = ((sbgr == 0) ? 0 : sb_groups[sbgr - 1] + 1);
2525 sb <= sb_groups[sbgr]; sb++) {
2526 /* 960627 FdB DynX dependent on configuration */
2527 if (fr_ps->config == 320) {
2528 /* 3/2 */
2529 switch (fr_ps->header->dyn_cross[sbgr]) {
2530 case 0:
2531 break;
2532 case 1:
2533 case 8:
2534 case 10:
2535 used[T4[sbgr]][sb] = 2;
2536 break;
2537 case 2:
2538 case 9:
2539 used[T3[sbgr]][sb] = 2;
2540 break;
2541 case 3:
2542 used[T2[sbgr]][sb] = 2;
2543 break;
2544 case 4:
2545 case 11:
2546 case 12:
2547 case 14:
2548 used[T3[sbgr]][sb] = 2;
2549 used[T4[sbgr]][sb] = 2;
2550 break;
2551 case 5:
2552 case 13:
2553 used[T2[sbgr]][sb] = 2;
2554 used[T4[sbgr]][sb] = 2;
2555 break;
2556 case 6:
2557 used[T2[sbgr]][sb] = 2;
2558 used[T3[sbgr]][sb] = 2;
2559 break;
2560 case 7:
2561 used[T2[sbgr]][sb] = 2;
2562 used[T4[sbgr]][sb] = 2;
2563 used[T3[sbgr]][sb] = 2;
2564 break;
2565 }
2566 } else if (fr_ps->config == 310 || fr_ps->config == 220) {
2567 /* 3/1 and 2/2 */
2568 switch (fr_ps->header->dyn_cross[sbgr]) {
2569 case 0:
2570 break;
2571 case 1:
2572 case 4:
2573 used[T3[sbgr]][sb] = 2;
2574 break;
2575 case 2:
2576 used[T2[sbgr]][sb] = 2;
2577 break;
2578 case 3:
2579 used[T2[sbgr]][sb] = 2;
2580 used[T3[sbgr]][sb] = 2;
2581 break;
2582 }
2583 } else if (fr_ps->config == 300 || fr_ps->config == 302
2584 || fr_ps->config == 210) {
2585 /* 3/0 (+2/0) and 2/1 */
2586 switch (fr_ps->header->dyn_cross[sbgr]) {
2587 case 0:
2588 break;
2589 case 1:
2590 used[T2[sbgr]][sb] = 2;
2591 break;
2592 }
2593 if (fr_ps->header->dyn_second_stereo[sbgr])
2594 used[T4[sbgr]][sb] = 2;
2595 } else if (fr_ps->config == 202) {
2596 if (fr_ps->header->dyn_second_stereo[sbgr])
2597 used[T3[sbgr]][sb] = 2;
2598 } else if (fr_ps->config == 102) {
2599 if (fr_ps->header->dyn_second_stereo[sbgr])
2600 used[T2[sbgr]][sb] = 2;
2601 }
2602 } /* for(sb.. */
2603 } /* for(sbgr.. */
2604 } /* if(fr_ps.. */
2605
2606
2607 /*
2608 if (fr_ps->header->mc_prediction_on == 1)
2609 {
2610 for (i = 0; i < 8; i++)
2611 {
2612 if (fr_ps->header->mc_pred[i] == 1)
2613 {
2614 for (j = 0; j < n_pred_coef[fr_ps->header->dyn_cross[i]]; j++)
2615 {
2616 if (fr_ps->header->predsi[i][j] != 0)
2617 {
2618 pred += 3;
2619 for (pci = 0; pci < fr_ps->header->predsi[i][j]; pci++)
2620 pred += 8;
2621 }
2622 }
2623 }
2624 }
2625 }
2626 */
2627 adb_hlp = *adb;
2628
2629 if (*aiff != 1) {
2630 *adb -= bbal + berr + banc;
2631 } else {
2632 if (fr_ps->header->ext_bit_stream_present == 0)
2633 *adb -= bbal + berr + banc + bancmc + pred +
2634 (fr_ps->header->n_ad_bytes * 8);
2635 else {
2636 *adb -= bbal + berr + banc + bancmc + pred +
2637 bancext + (fr_ps->header->n_ad_bytes * 8);
2638
2639 for (i = 0; i < jsbound; ++i)
2640 bbal_mpg1 += stereo * (*alloc)[i][0].bits;
2641 for (i = jsbound; i < sblimit; ++i)
2642 bbal_mpg1 += (*alloc)[i][0].bits;
2643
2644 adb_mpg1 -= bbal_mpg1 + berr + banc + bancmc +
2645 (fr_ps->header->n_ad_bytes * 8);
2646 }
2647 }
2648 ad = *adb;
2649
2650 bspl = bscf = bsel = bspl_mpg1 = bscf_mpg1 = bsel_mpg1 = 0;
2651
2652 do {
2653 /* locate the subband with minimum SMR */
2654 small = 999999.0;
2655 min_sb = -1;
2656 min_ch = -1;
2657
2658 for (i = 0; i < SBLIMIT; i++) { /* searching for the sb min SMR */
2659 l = sbgrp[i];
2660 for (j = 0; j < (stereo + stereomc + stereoaug); ++j)
2661 if ((j < stereo && i < sblimit) || (j >= stereo && i < sblimit_mc)) {
2662 k = transmission_channel (fr_ps, l, j);
2663
2664 if ((i >= 12) && (fr_ps->header->center == 3) && (k == 2))
2665 used[k][i] = 2;
2666
2667 if ((used[k][i] != 2) && (small > mnr[k][i])) {
2668 small = mnr[k][i];
2669 min_sb = i;
2670 min_ch = k;
2671 ll = l; /*sb-group */
2672 }
2673 }
2674 }
2675
2676 /******************************************************************/
2677 /* Multilingual JMZ 08/03/1995 */
2678 if (n_ml_ch > 0) {
2679 for (i = 0; i < sblimit_ml; i++)
2680 for (j = 7; j < (n_ml_ch + 7); ++j) {
2681 k = j;
2682 if ((used[k][i] != 2) && (small > mnr[k][i])) {
2683 small = mnr[k][i];
2684 min_sb = i;
2685 min_ch = k;
2686 ll = l;
2687 }
2688 }
2689 }
2690 /* Multilingual JMZ 08/03/1995 */
2691 /******************************************************************/
2692
2693
2694 if (min_sb > -1) { /* there was something to find */
2695 /* find increase in bit allocation in subband [min] */
2696 if (min_ch < stereo) {
2697 increment =
2698 12 * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].group *
2699 (*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].bits);
2700 /* how many bits are needed */
2701 if (used[min_ch][min_sb])
2702 increment -= 12 * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group *
2703 (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
2704 } else if (min_ch < 7 || n_ml_ch == 0) { /* Multichannel */
2705 increment =
2706 12 * ((*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb] + 1].group *
2707 (*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb] + 1].bits);
2708 /* how many bits are needed */
2709 if (used[min_ch][min_sb])
2710 increment -=
2711 12 * ((*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]].group *
2712 (*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
2713 } else { /* MultiLingual 7/8/95 WtK */
2714
2715 increment = ((*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb] + 1].group *
2716 (*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb] + 1].bits);
2717 if (used[min_ch][min_sb])
2718 increment -= ((*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]].group *
2719 (*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]].bits);
2720 if (fr_ps->header->multiling_fs == 1)
2721 increment *= 6;
2722 else
2723 increment *= 12;
2724 }
2725
2726 /* scale factor bits required for subband [min] */
2727 /* above js bound, need both chans */
2728
2729 if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) &&
2730 ((min_ch == 0) || (min_ch == 1)))
2731 oth_ch = 1 - min_ch;
2732
2733 if (used[min_ch][min_sb])
2734 scale = seli = 0;
2735 else { /* this channel had no bits or scfs before */
2736 seli = 2;
2737 scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
2738
2739 if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound)
2740 && ((min_ch == 0) || (min_ch == 1))) {
2741 /* each new js sb has L+R scfsis */
2742 seli += 2;
2743 scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
2744 }
2745 if (fr_ps->header->dyn_cross_on == 1) {
2746 dyn_bal (scfsi, ll, fr_ps, min_ch, min_sb, &seli, &scale);
2747 /* 960819 FdB joint stereo in combination with DynX added */
2748 if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound)
2749 && (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
2750 dyn_bal (scfsi, ll, fr_ps, oth_ch, min_sb, &seli, &scale);
2751 }
2752 }
2753
2754 /* check to see enough bits were available for */
2755 /* increasing resolution in the minimum band */
2756
2757 if (fr_ps->header->ext_bit_stream_present == 1) {
2758 if ((min_ch == 0) || (min_ch == 1 && stereo == 2)) {
2759 if (adb_mpg1 >
2760 bspl_mpg1 + bscf_mpg1 + bsel_mpg1 + seli + scale + increment) {
2761 bspl_mpg1 += increment; /* bits for subband sample */
2762 bscf_mpg1 += scale; /* bits for scale factor */
2763 bsel_mpg1 += seli; /* bits for scfsi code */
2764 } else
2765 used[min_ch][min_sb] = 2; /* can't increase this alloc */
2766 }
2767 }
2768
2769 if ((ad > bspl + bscf + bsel + seli + scale + increment)
2770 && (used[min_ch][min_sb] != 2)) {
2771 ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
2772 bspl += increment; /* bits for subband sample */
2773 bscf += scale; /* bits for scale factor */
2774 bsel += seli; /* bits for scfsi code */
2775 used[min_ch][min_sb] = 1; /* subband has bits */
2776
2777 if (min_ch < stereo) {
2778 mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
2779 snr[(*alloc)[min_sb][ba].quant + 1];
2780 /* Check if subband has been fully allocated max bits */
2781 if (ba >= (1 << (*alloc)[min_sb][0].bits) - 1)
2782 used[min_ch][min_sb] = 2;
2783 } else if (min_ch < 7 || n_ml_ch == 0) { /* Multichannel */
2784 mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
2785 snr[(*alloc_mc)[min_sb][ba].quant + 1];
2786 /* Check if subband has been fully allocated max bits */
2787 if (ba >= (1 << (*alloc_mc)[min_sb][0].bits) - 1)
2788 used[min_ch][min_sb] = 2;
2789 } else { /* MultiLingual 7/8/95 WtK */
2790
2791 mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
2792 snr[(*alloc_ml)[min_sb][ba].quant + 1];
2793 if (ba >= (1 << (*alloc_ml)[min_sb][0].bits) - 1)
2794 used[min_ch][min_sb] = 2;
2795 }
2796 } else
2797 used[min_ch][min_sb] = 2; /* can't increase this alloc */
2798
2799 if (verbosity >= 3) {
2800 if (fr_ps->header->ext_bit_stream_present == 1)
2801 printf
2802 ("ch: %02d sb: %02d used: %d adb: %05d used: %05d ad: %05d used: %05d\n",
2803 min_ch, min_sb, used[min_ch][min_sb], adb_mpg1,
2804 bspl_mpg1 + bscf_mpg1 + bsel_mpg1, ad, bspl + bscf + bsel);
2805 else
2806 printf ("ch: %02d sb: %02d used: %d ad: %05d used: %05d\n",
2807 min_ch, min_sb, used[min_ch][min_sb], ad, bspl + bscf + bsel);
2808 }
2809
2810
2811 if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound)
2812 && (stereo == 2) && ((min_ch == 0) || (min_ch == 1))) {
2813 /* above jsbound, alloc applies L+R */
2814 ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
2815 used[oth_ch][min_sb] = used[min_ch][min_sb];
2816 mnr[oth_ch][min_sb] = -perm_smr[oth_ch][min_sb] +
2817 snr[(*alloc)[min_sb][ba].quant + 1];
2818 }
2819 if (fr_ps->header->dyn_cross_on == 1) {
2820 choose_dyn (fr_ps, min_ch, min_sb, ll, bit_alloc);
2821 /* 960819 FdB joint stereo in combination with DynX added */
2822 if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound)
2823 && (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
2824 choose_dyn (fr_ps, oth_ch, min_sb, ll, bit_alloc);
2825 }
2826 } /* end of if-loop if min_sb >-1 */
2827 } while (min_sb > -1); /* until could find no channel */
2828 /* Calculate the number of bits left */
2829
2830 ad -= bspl + bscf + bsel;
2831 *adb = ad;
2832 for (k = 0; k < stereo; k++)
2833 for (i = sblimit; i < SBLIMIT; i++)
2834 bit_alloc[k][i] = 0;
2835 if (n_ml_ch > 0) {
2836 for (k = stereo; k < 7; k++)
2837 for (i = sblimit_mc; i < SBLIMIT; i++)
2838 bit_alloc[k][i] = 0;
2839 for (i = sblimit_ml; i < SBLIMIT; i++)
2840 for (k = 7; k < 14; k++)
2841 bit_alloc[k][i] = 0;
2842 } else
2843 for (k = stereo; k < 12; k++)
2844 for (i = sblimit_mc; i < SBLIMIT; i++)
2845 bit_alloc[k][i] = 0;
2846
2847 /* not used !?! perhaps later!! 8/21/93, SR */
2848 noisy_sbs = 0;
2849 small = mnr[0][0]; /* calc worst noise in case */
2850 for (k = 0; k < (stereo + stereomc + stereoaug); k++) {
2851 for (i = 0; i < sblimit; i++) {
2852 if (small > mnr[k][i])
2853 small = mnr[k][i];
2854 if (mnr[k][i] < fr_ps->mnr_min)
2855 noisy_sbs++; /* noise is not masked */
2856 }
2857 }
2858
2859 if (n_ml_ch > 0) {
2860 for (k = 7; k < (7 + n_ml_ch); k++) {
2861 for (i = 0; i < sblimit_ml; i++) {
2862 if (small > mnr[k][i])
2863 small = mnr[k][i];
2864 if (mnr[k][i] < fr_ps->mnr_min)
2865 noisy_sbs++; /* noise is not masked */
2866 }
2867 }
2868 }
2869
2870 return noisy_sbs;
2871 }
2872
2873 /************************************************************************
2874 *
2875 * I_subband_quantization (Layer I)
2876 * II_subband_quantization (Layer II)
2877 * II_subband_quantisationmc (MPEG2) SR
2878 * PURPOSE:Quantizes subband samples to appropriate number of bits
2879 *
2880 * SEMANTICS: Subband samples are divided by their scalefactors, which
2881 * makes the quantization more efficient. The scaled samples are
2882 * quantized by the function a*x+b, where a and b are functions of
2883 * the number of quantization levels. The result is then truncated
2884 * to the appropriate number of bits and the MSB is inverted.
2885 *
2886 * Note that for fractional 2's complement, inverting the MSB for a
2887 * negative number x is equivalent to adding 1 to it.
2888 *
2889 ************************************************************************/
2890
2891 static double II_a[17] = {
2892 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
2893 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
2894 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
2895 0.999969482, 0.999984741
2896 };
2897
2898 static double II_b[17] = {
2899 -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
2900 -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
2901 -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
2902 -0.000030518, -0.000015259
2903 };
2904
II_subband_quantization(unsigned int (* scalar)[3][32],double (* sb_samples)[3][12][32],unsigned int (* j_scale)[3][32],double (* j_samps)[3][12][32],unsigned int (* bit_alloc)[32],unsigned int (* sbband)[3][12][32],frame_params * fr_ps)2905 void II_subband_quantization (unsigned int (*scalar)[3][32],
2906 double (*sb_samples)[3][12][32],
2907 unsigned int (*j_scale)[3][32],
2908 double (*j_samps)[3][12][32],
2909 unsigned int (*bit_alloc)[32],
2910 unsigned int (*sbband)[3][12][32],
2911 frame_params * fr_ps)
2912 {
2913 int i, j, k, s, n, qnt, sig, m;
2914 int stereo = fr_ps->stereo;
2915 //MFC int stereomc = fr_ps->stereomc;
2916 int sblimit = fr_ps->sblimit;
2917 int jsbound = fr_ps->jsbound;
2918 unsigned int stps;
2919 double d;
2920 al_table *alloc = fr_ps->alloc;
2921
2922 for (s = 0; s < 3; s++)
2923 for (j = 0; j < 12; j++)
2924 for (i = 0; i < sblimit; i++)
2925 for (k = 0; k < ((i < jsbound) ? stereo : 1); k++) {
2926
2927 if (bit_alloc[k][i]) {
2928 /* scale and quantize floating point sample */
2929 if (stereo == 2 && i >= jsbound) /* use j-stereo samples */
2930 d = j_samps[0][s][j][i] / multiple[j_scale[0][s][i]];
2931 else
2932 d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
2933
2934 if (fabs (d) >= 1.0) { /* > changed to >=, 1992-11-06 shn */
2935 printf ("In compatible part, not scaled properly, %d %d %d %d\n",
2936 k, s, j, i);
2937 printf ("Value %1.10f\n", sb_samples[k][s][j][i]);
2938 printf ("Channel %d\n", k);
2939 }
2940 qnt = (*alloc)[i][bit_alloc[k][i]].quant;
2941 d = d * II_a[qnt] + II_b[qnt];
2942 /* extract MSB N-1 bits from the floating point sample */
2943 if (d >= 0)
2944 sig = 1;
2945 else {
2946 sig = 0;
2947 d += 1.0;
2948 }
2949 n = 0;
2950 #ifndef MS_DOS
2951 stps = (*alloc)[i][bit_alloc[k][i]].steps;
2952 while ((1L << n) < stps)
2953 n++;
2954 #else
2955 while (((unsigned long) (1L << (long) n) <
2956 ((unsigned long) ((*alloc)[i][bit_alloc[k][i]].steps)
2957 & 0xffff)) && (n < 16))
2958 n++;
2959 #endif
2960 n--;
2961 sbband[k][s][j][i] = (unsigned int) (d * (double) (1L << n));
2962 /* tag the inverted sign bit to sbband at position N */
2963 /* The bit inversion is a must for grouping with 3,5,9 steps
2964 so it is done for all subbands */
2965 if (sig)
2966 sbband[k][s][j][i] |= 1 << n;
2967 }
2968 }
2969 for (s = 0; s < 3; s++)
2970 for (j = sblimit; j < SBLIMIT; j++)
2971 for (i = 0; i < 12; i++)
2972 for (m = 0; m < stereo; m++)
2973 sbband[m][s][i][j] = 0;
2974 }
2975
2976
II_subband_quantization_mc(unsigned int (* scalar)[3][32],double (* sb_samples)[3][12][32],unsigned int (* j_scale)[3][32],double (* j_samps)[3][12][32],unsigned int (* bit_alloc)[32],unsigned int (* sbband)[3][12][32],frame_params * fr_ps)2977 void II_subband_quantization_mc (unsigned int (*scalar)[3][32],
2978 double (*sb_samples)[3][12][32],
2979 unsigned int (*j_scale)[3][32],
2980 double (*j_samps)[3][12][32],
2981 unsigned int (*bit_alloc)[32],
2982 unsigned int (*sbband)[3][12][32],
2983 frame_params * fr_ps)
2984 {
2985 int i, j, k, s, n, qnt, sig, m, ll;
2986 //MFC layer *info = fr_ps->header;
2987 //MFC int center = info->center;
2988 //MFC int surround = info->surround;
2989 int stereo = fr_ps->stereo;
2990 int stereomc = fr_ps->stereomc;
2991 int sblimit = fr_ps->sblimit_mc;
2992 unsigned int stps;
2993 double d;
2994 al_table *alloc = fr_ps->alloc_mc;
2995
2996 for (s = 0; s < 3; s++)
2997 for (j = 0; j < 12; j++)
2998 for (i = 0; i < sblimit; i++) {
2999 ll = sbgrp[i];
3000 for (m = stereo; m < stereo + stereomc; ++m) {
3001 k = transmission_channel (fr_ps, ll, m);
3002
3003 if (fr_ps->header->dyn_cross_on == 1) {
3004 /* 960627 FdB DynX dependent on configuration */
3005 if (dyn_ch (fr_ps, ll, m) == 0)
3006 bit_alloc[k][i] = 0;
3007 }
3008
3009 if (bit_alloc[k][i]) {
3010 d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
3011 if (fabs (d) >= 1.0) { /* > changed to >=, 1992-11-06 shn */
3012 printf ("In MC, not scaled properly, %d %d %d %d\n", k, s, j, i);
3013 printf ("Value %1.10f\n", sb_samples[k][s][j][i]);
3014 }
3015 qnt = (*alloc)[i][bit_alloc[k][i]].quant;
3016 d = d * II_a[qnt] + II_b[qnt];
3017 /* extract MSB N-1 bits from the floating point sample */
3018 if (d >= 0)
3019 sig = 1;
3020 else {
3021 sig = 0;
3022 d += 1.0;
3023 }
3024 n = 0;
3025 #ifndef MS_DOS
3026 stps = (*alloc)[i][bit_alloc[k][i]].steps;
3027 while ((1L << n) < stps)
3028 n++;
3029 #else
3030 while (((unsigned long) (1L << (long) n) <
3031 ((unsigned long) ((*alloc)[i][bit_alloc[k][i]].steps)
3032 & 0xffff)
3033 ) && (n < 16)
3034 )
3035 n++;
3036 #endif
3037 n--;
3038 sbband[k][s][j][i] = (unsigned int) (d * (double) (1L << n));
3039 /* tag the inverted sign bit to sbband at position N */
3040 /* The bit inversion is a must for grouping with 3,5,9 steps
3041 so it is done for all subbands */
3042 if (sig)
3043 sbband[k][s][j][i] |= 1 << n;
3044 } else
3045 sbband[k][s][j][i] = 0;
3046 }
3047 }
3048
3049 for (s = 0; s < 3; s++)
3050 for (j = sblimit; j < SBLIMIT; j++)
3051 for (i = 0; i < 12; i++)
3052 for (k = stereo; k < 7; k++)
3053 sbband[k][s][i][j] = 0;
3054 }
3055
3056
3057
II_subband_quantization_ml(unsigned int (* scalar)[3][32],double (* sb_samples)[3][12][32],unsigned int (* j_scale)[3][32],double (* j_samps)[3][12][32],unsigned int (* bit_alloc)[32],unsigned int (* sbband)[3][12][32],frame_params * fr_ps)3058 void II_subband_quantization_ml (unsigned int (*scalar)[3][32],
3059 double (*sb_samples)[3][12][32],
3060 unsigned int (*j_scale)[3][32],
3061 double (*j_samps)[3][12][32],
3062 unsigned int (*bit_alloc)[32],
3063 unsigned int (*sbband)[3][12][32],
3064 frame_params * fr_ps)
3065 {
3066 int i, j, k, s, n, qnt, sig, m;
3067 int n_ml_ch = fr_ps->header->multiling_ch;
3068 int sblimit = fr_ps->sblimit_ml;
3069 int ml_lsf = fr_ps->header->multiling_fs;
3070 unsigned int stps;
3071 double d;
3072 al_table *alloc = fr_ps->alloc_ml;
3073
3074 for (s = 0; s < 3; s++)
3075 for (j = 0; j < ((ml_lsf) ? 6 : 12); j++) /* WtK half sample frequency 7/8/95 */
3076 for (i = 0; i < sblimit; i++) {
3077 for (m = 7; m < 7 + n_ml_ch; ++m) {
3078 k = m;
3079 if (bit_alloc[k][i]) {
3080 d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
3081 if (fabs (d) >= 1.0) { /* > changed to >=, 1992-11-06 shn */
3082 printf ("In ML, not scaled properly, %d %d %d %d\n", k, s, j, i);
3083 printf ("Value %1.10f\n", sb_samples[k][s][j][i]);
3084 }
3085 qnt = (*alloc)[i][bit_alloc[k][i]].quant;
3086 d = d * II_a[qnt] + II_b[qnt];
3087 /* extract MSB N-1 bits from the floating point sample */
3088 if (d >= 0)
3089 sig = 1;
3090 else {
3091 sig = 0;
3092 d += 1.0;
3093 }
3094 n = 0;
3095 stps = (*alloc)[i][bit_alloc[k][i]].steps;
3096 while ((1L << n) < stps)
3097 n++;
3098 n--;
3099 sbband[k][s][j][i] = (unsigned int) (d * (double) (1L << n));
3100 /* tag the inverted sign bit to sbband at position N */
3101 /* The bit inversion is a must for grouping with 3,5,9 steps
3102 so it is done for all subbands */
3103 if (sig)
3104 sbband[k][s][j][i] |= 1 << n;
3105 } else
3106 sbband[k][s][j][i] = 0;
3107 }
3108 }
3109
3110 for (s = 0; s < 3; s++)
3111 for (j = sblimit; j < SBLIMIT; j++)
3112 for (i = 0; i < ((ml_lsf) ? 6 : 12); i++)
3113 for (k = 7; k < 7 + n_ml_ch; k++)
3114 sbband[k][s][i][j] = 0;
3115 }
3116
3117
3118
3119
3120
3121
3122 /************************************************************************
3123 *
3124 * II_encode_bit_alloc (Layer II)
3125 * II_encode_bit_alloc_mc (Layer II multichannel)
3126 *
3127 * PURPOSE:Writes bit allocation information onto bitstream
3128 *
3129 *
3130 ************************************************************************/
3131
3132
II_encode_bit_alloc(unsigned int (* bit_alloc)[32],frame_params * fr_ps,Bit_stream_struc * bs)3133 void II_encode_bit_alloc (unsigned int (*bit_alloc)[32], frame_params * fr_ps,
3134 Bit_stream_struc * bs)
3135 {
3136 int i, k;
3137 int stereo = fr_ps->stereo;
3138 int sblimit = fr_ps->sblimit;
3139 int jsbound = fr_ps->jsbound;
3140 al_table *alloc = fr_ps->alloc;
3141
3142 for (i = 0; i < sblimit; i++)
3143 for (k = 0; k < ((i < jsbound) ? stereo : 1); k++)
3144 putbits (bs, bit_alloc[k][i], (*alloc)[i][0].bits);
3145 }
3146
3147
II_encode_bit_alloc_mc(unsigned int (* bit_alloc)[32],frame_params * fr_ps,Bit_stream_struc * bs)3148 void II_encode_bit_alloc_mc (unsigned int (*bit_alloc)[32],
3149 frame_params * fr_ps, Bit_stream_struc * bs)
3150 {
3151 int i, k, l, m;
3152 //MFC layer *info = fr_ps->header;
3153 //MFC int center = info->center;
3154 int stereo = fr_ps->stereo;
3155 int stereomc = fr_ps->stereomc;
3156 int sblimit = fr_ps->sblimit_mc;
3157 al_table *alloc = fr_ps->alloc_mc;
3158
3159
3160 for (i = 0; i < sblimit; i++) {
3161 l = sbgrp[i];
3162 for (m = stereo; m < stereo + stereomc; ++m) {
3163 k = transmission_channel (fr_ps, l, m);
3164
3165 if ((fr_ps->header->center != 3) || (i < 12) || (k != 2))
3166 /* 960627 FdB DynX dependent on configuration */
3167 if (dyn_ch (fr_ps, l, m) == 1)
3168 putbits (bs, bit_alloc[k][i], (*alloc)[i][0].bits);
3169 }
3170 }
3171 }
3172
3173
3174
3175
3176 /************************************************************************
3177 *
3178 * II_sample_encoding (Layer II)
3179 * II_sample_encoding_mc (Layer II) SR
3180 *
3181 * PURPOSE:Put one frame of subband samples on to the bitstream
3182 *
3183 * SEMANTICS: The number of bits allocated per sample is read from
3184 * the bit allocation information #bit_alloc#. Layer 2
3185 * supports writing grouped samples for quantization steps
3186 * that are not a power of 2.
3187 *
3188 ************************************************************************/
3189
II_sample_encoding(unsigned int (* sbband)[3][12][32],unsigned int (* bit_alloc)[32],frame_params * fr_ps,Bit_stream_struc * bs)3190 void II_sample_encoding (unsigned int (*sbband)[3][12][32],
3191 unsigned int (*bit_alloc)[32], frame_params * fr_ps,
3192 Bit_stream_struc * bs)
3193 {
3194 unsigned int temp;
3195 unsigned int i, j, k, s, x, y;
3196 int stereo = fr_ps->stereo;
3197 // MFC int stereomc = fr_ps->stereomc;
3198 int sblimit = fr_ps->sblimit;
3199 int jsbound = fr_ps->jsbound;
3200 al_table *alloc = fr_ps->alloc;
3201
3202 for (s = 0; s < 3; s++)
3203 for (j = 0; j < 12; j += 3)
3204 for (i = 0; i < sblimit; i++)
3205 for (k = 0; k < ((i < jsbound) ? stereo : 1); k++)
3206 if (bit_alloc[k][i]) {
3207 if ((*alloc)[i][bit_alloc[k][i]].group == 3) {
3208 for (x = 0; x < 3; x++)
3209 putbits (bs, sbband[k][s][j + x][i],
3210 (*alloc)[i][bit_alloc[k][i]].bits);
3211
3212 } else {
3213 y = (*alloc)[i][bit_alloc[k][i]].steps;
3214 temp = sbband[k][s][j][i] +
3215 sbband[k][s][j + 1][i] * y + sbband[k][s][j + 2][i] * y * y;
3216 putbits (bs, temp, (*alloc)[i][bit_alloc[k][i]].bits);
3217
3218 }
3219 }
3220 }
3221
3222
3223
3224
3225 /******************* Layer II five channels ******************************/
3226
3227
II_sample_encoding_mc(unsigned int (* sbband)[3][12][32],unsigned int lfe_sbband[12],unsigned int (* bit_alloc)[32],unsigned int lfe_alloc,frame_params * fr_ps,Bit_stream_struc * bs)3228 void II_sample_encoding_mc (unsigned int (*sbband)[3][12][32],
3229 unsigned int lfe_sbband[12],
3230 unsigned int (*bit_alloc)[32],
3231 unsigned int lfe_alloc, frame_params * fr_ps,
3232 Bit_stream_struc * bs)
3233 {
3234 unsigned int temp;
3235 unsigned int i, j, k, s, x, y, l, m;
3236 layer *info = fr_ps->header;
3237 int center = info->center;
3238 int stereo = fr_ps->stereo;
3239 int stereomc = fr_ps->stereomc;
3240 int sblimit = fr_ps->sblimit_mc;
3241 al_table *alloc = fr_ps->alloc_mc;
3242 int lfe = fr_ps->header->lfe;
3243
3244 for (s = 0; s < 3; s++)
3245 for (j = 0; j < 12; j += 3) {
3246 if (lfe)
3247 putbits (bs, lfe_sbband[s * 4 + j / 3], (*alloc)[0][lfe_alloc].bits);
3248
3249 for (i = 0; i < sblimit; i++) {
3250 l = sbgrp[i];
3251 for (m = stereo; m < stereo + stereomc; m++) {
3252 k = transmission_channel (fr_ps, l, m);
3253
3254 if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3)) {
3255 if ((*alloc)[i][bit_alloc[k][i]].group == 3)
3256 for (x = 0; x < 3; x++)
3257 putbits (bs, sbband[k][s][j + x][i],
3258 (*alloc)[i][bit_alloc[k][i]].bits);
3259 else {
3260 y = (*alloc)[i][bit_alloc[k][i]].steps;
3261 temp = sbband[k][s][j][i] +
3262 sbband[k][s][j + 1][i] * y + sbband[k][s][j + 2][i] * y * y;
3263 putbits (bs, temp, (*alloc)[i][bit_alloc[k][i]].bits);
3264
3265 }
3266 }
3267 }
3268 }
3269 }
3270 }
3271
3272
3273
3274 /************************************************************************
3275 *
3276 * encode_CRC
3277 *
3278 ************************************************************************/
3279
encode_CRC(unsigned int crc,Bit_stream_struc * bs)3280 void encode_CRC (unsigned int crc, Bit_stream_struc * bs)
3281 {
3282 putbits (bs, crc, 16);
3283 }
3284
3285 /***************************************************************************
3286 *
3287 * ancillary_encode
3288 *
3289 * PURPOSE: The use of ancillary part of the bitstream for information
3290 * storage.
3291 *
3292 *
3293 **************************************************************************/
3294
ancillary_encode(fr_ps,bs,adb)3295 void ancillary_encode (fr_ps, bs, adb)
3296 frame_params *fr_ps;
3297 Bit_stream_struc *bs;
3298 int adb;
3299 {
3300 int bitsPerSlot;
3301 int samplesPerFrame;
3302 int bit_rate;
3303 int avg_slots_per_frame;
3304 int whole_SpF;
3305 int usedAdb;
3306 int adbNumberStart;
3307 //MFC int l;
3308 char *mesg = "This bitstream use ancillary part.";
3309 extern int mesg_index;
3310
3311 register int i;
3312
3313
3314 //MFC double frac_SpF;
3315 //MFC double slot_lag;
3316 layer *info = fr_ps->header;
3317
3318
3319 if (info->lay == 1) {
3320 bitsPerSlot = 32;
3321 samplesPerFrame = 384;
3322 } else {
3323 bitsPerSlot = 8;
3324 samplesPerFrame = 1152;
3325 }
3326
3327 bit_rate = bitrate[info->lay - 1][info->bitrate_index];
3328
3329 avg_slots_per_frame = ((double) samplesPerFrame /
3330 s_freq[info->sampling_frequency]) *
3331 ((double) bit_rate / (double) bitsPerSlot);
3332 whole_SpF = (int) avg_slots_per_frame;
3333
3334 adbNumberStart = (whole_SpF + info->padding) * bitsPerSlot;
3335 usedAdb = adbNumberStart - adb;
3336
3337 if (usedAdb <= (adbNumberStart - 8)) {
3338 if (usedAdb % 8) {
3339 for (i = 0; i < (8 - (usedAdb % 8)); i++)
3340 put1bit (bs, 0);
3341 usedAdb += (8 - (usedAdb % 8));
3342 }
3343 while (usedAdb < adbNumberStart - 8) {
3344 for (i = 0; i < 8; i++)
3345 put1bit (bs, 0);
3346 usedAdb += 8;
3347 }
3348 if (mesg_index >= strlen (mesg)) {
3349 for (i = 0; i < 8; i++)
3350 put1bit (bs, 0);
3351 } else {
3352 putbits (bs, (int) (mesg[mesg_index++]), 8);
3353 }
3354 } else {
3355 for (i = 0; i < adb; i++)
3356 put1bit (bs, 0);
3357 }
3358
3359 }
3360