r1053: Add Russian translation.
[cinelerra_cv.git] / toolame-02l / encode_new.c
blobc9c02948e49e4b278a0b623c39515cc91ded1ee0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "common.h"
5 #include "options.h"
6 #include "bitstream.h"
7 #include "availbits.h"
8 #include "encode_new.h"
10 #define NUMTABLES 5
11 int vbrstats_new[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
13 /* There are really only 9 distinct lines in the allocation tables
14 each member of this table is an index into */
15 /* step_index[linenumber][index] */
16 static int step_index[9][16] = {
17 /*0*/ {0, 1, 3, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17},
18 /*1*/ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,17},
19 /*2*/ {0, 1, 2, 3, 4, 5, 6,17, 0, 0, 0, 0, 0, 0, 0, 0},
20 /*3*/ {0, 1, 2, 17,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
21 /*4*/ {0, 1, 2, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16},
22 /*5*/ {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
23 /* From ISO13818 Table B.1 */
24 /*6*/ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15},
25 /*7*/ {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
26 /*8*/ {0, 1, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
29 static int nbal[9] = {4, 4, 3, 2, 4, 3, 4, 3, 2};
32 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 */
33 /* The number of steps allowed */
34 static int steps[18] = {0, 3, 5, 7, 9, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};
35 /* The power of 2 just under the steps value */
36 static int steps2n[18]={0, 2, 4, 4, 8, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
37 /* The bits per codeword from TableB.4 */
38 static int bits[18] = {0, 5, 7, 3, 10, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
39 /* Samples per codeword Table B.4 Page 53 */
40 //static int group[18] = {0, 3, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
41 static int group[18] = {0, 1, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
43 /* nbal */
45 /* The sblimits of the 5 allocation tables
46 4 tables for MPEG-1
47 1 table for MPEG-2 LSF */
48 static int table_sblimit[5] = {27, 30, 8, 12, 30};
50 /* Each table contains a list of allowable quantization steps.
51 There are only 9 distinct lists of steps.
52 This table gives the index of which of the 9 lists is being used
53 A "-1" entry means that it is above the sblimit for this table */
54 static int line[5][SBLIMIT] = {
55 /*00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 */
56 {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,-1,-1,-1,-1,-1},
57 {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,-1,-1},
58 {4, 4, 5, 5, 5, 5, 5, 5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
59 {4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
60 /* LSF Table */
61 {6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}
64 /* This is ISO11172 Table B.1 */
65 double scalefactor[64] = { /* Equation for nth element = 2 / (cuberoot(2) ^ n) */
66 2.00000000000000, 1.58740105196820, 1.25992104989487,
67 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
68 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
69 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
70 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
71 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
72 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
73 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
74 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
75 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
76 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
77 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
78 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
79 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
80 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
81 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
82 1E-20
85 /* ISO11172 Table C.5 Layer II Signal to Noise Raios
86 MFC FIX find a reference for these in terms of bits->SNR value
87 Index into table is the steps index
88 index steps SNR
89 0 0 0.00
90 1 3 7.00
91 2 5 11.00
92 3 7 16.00
93 4 9 20.84
94 etc
96 static double SNR[18] = { 0.00, 7.00, 11.00, 16.00, 20.84,
97 25.28, 31.59, 37.75, 43.84,
98 49.89, 55.93, 61.96, 67.98, 74.01,
99 80.03, 86.05, 92.01, 98.01
102 int tablenum=0;
104 int encode_init(frame_info *frame) {
105 int ws, bsp, br_per_ch, sfrq;
107 bsp = frame->header->bitrate_index;
108 br_per_ch = bitrate[frame->header->version][bsp] / frame->nch;
109 ws = frame->header->sampling_frequency;
110 sfrq = s_freq[frame->header->version][ws];
111 /* decision rules refer to per-channel bitrates (kbits/sec/chan) */
112 if (frame->header->version == MPEG_AUDIO_ID) { /* MPEG-1 */
113 if ((sfrq == 48 && br_per_ch >= 56)
114 || (br_per_ch >= 56 && br_per_ch <= 80))
115 tablenum = 0;
116 else if (sfrq != 48 && br_per_ch >= 96)
117 tablenum = 1;
118 else if (sfrq != 32 && br_per_ch <= 48)
119 tablenum = 2;
120 else
121 tablenum = 3;
122 } else { /* MPEG-2 LSF */
123 tablenum = 4;
125 fprintf(stdout,"encode_init: using tablenum %i with sblimit %i\n",tablenum, table_sblimit[tablenum]);
127 #define DUMPTABLESx
128 #ifdef DUMPTABLES
130 int tablenumber,j,sblimit, sb;
131 fprintf(stdout,"Tables B.21,b,c,d from ISO11172 and the LSF table from ISO13818\n");
132 for (tablenumber=0;tablenumber<NUMTABLES;tablenumber++) {
133 /* Print Table Header */
134 fprintf(stdout,"Tablenum %i\n",tablenumber);
135 fprintf(stdout,"sb nbal ");
136 for (j=0;j<16;j++)
137 fprintf(stdout,"%6i ",j);
138 fprintf(stdout,"\n");
139 fprintf(stdout,"-----------------------------------------------------------------------------------------------------------------------\n");
141 sblimit = table_sblimit[tablenumber];
142 for (sb=0;sb<SBLIMIT;sb++) {
143 int thisline = line[tablenumber][sb];
144 fprintf(stdout,"%2i %4i ",sb,nbal[thisline]);
145 if (nbal[thisline] != 0)
146 for (j=0; j<(1<<nbal[thisline]); j++)
147 fprintf(stdout,"%6i ", steps[ step_index[thisline][j] ]);
148 fprintf(stdout,"\n");
150 fprintf(stdout,"\n");
152 exit(0);
154 #endif
155 return (table_sblimit[tablenum]);
159 scale_factor_calc
160 pick_scale
161 if JOINTSTEREO
162 combine_LR
163 scale_factor_calc
164 use psy model to determine SMR
165 transmission pattern
166 main_bit_allocation
167 if (error protection)
168 calc CRC
169 encode_info
170 if (error_protection)
171 encode_CRC
172 encode_bit_alloc
173 encode_scale
174 subband_quantization
175 sample_encoding
179 void scalefactor_calc_new (double sb_sample[][3][SCALE_BLOCK][SBLIMIT],
180 unsigned int sf_index[][3][SBLIMIT], int nch,
181 int sblimit)
183 /* Optimized to use binary search instead of linear scan through the
184 scalefactor table; guarantees to find scalefactor in only 5
185 jumps/comparisons and not in {0 (lin. best) to 63 (lin. worst)}.
186 Scalefactors for subbands > sblimit are no longer computed.
187 Uses a single sblimit-loop.
188 Patrick De Smet Oct 1999. */
189 int ch, gr;
190 /* Using '--' loops to avoid possible "cmp value + bne/beq" compiler */
191 /* inefficiencies. Below loops should compile to "bne/beq" only code */
192 for (ch = nch; ch--;)
193 for (gr = 3; gr--;) {
194 int sb;
195 for (sb = sblimit; sb--;) {
196 int j;
197 unsigned int l;
198 register double temp;
199 unsigned int scale_fac;
200 /* Determination of max. over each set of 12 subband samples: */
201 /* PDS TODO: maybe this could/should ??!! be integrated into */
202 /* the subband filtering routines? */
203 register double cur_max = fabs (sb_sample[ch][gr][SCALE_BLOCK - 1][sb]);
204 for (j = SCALE_BLOCK - 1; j--;) {
205 if ((temp = fabs (sb_sample[ch][gr][j][sb])) > cur_max)
206 cur_max = temp;
208 /* PDS: binary search in the scalefactor table: */
209 /* This is the real speed up: */
210 for (l = 16, scale_fac = 32; l; l >>= 1) {
211 if (cur_max <= scalefactor[scale_fac])
212 scale_fac += l;
213 else
214 scale_fac -= l;
216 if (cur_max > scalefactor[scale_fac])
217 scale_fac--;
218 sf_index[ch][gr][sb] = scale_fac;
219 /* There is a direct way of working out the index, if the
220 maximum value is known but since
221 it involves a log it isn't really speedy.
222 Items in the scalefactor[] table are calculated by:
223 the n'th entry = 2 / (cuberoot(2) ^ n)
224 And so using a bit of maths you get:
225 index = (int)(log(2.0/cur_max) / LNCUBEROOTTWO);
226 fprintf(stdout,"cur_max %.14lf scalefactorindex %i multiple %.14lf\n",cur_max, scale_fac, scalefactor[scale_fac]);
231 INLINE double mod (double a)
233 return (a > 0) ? a : -a;
236 /* Combine L&R channels into a mono joint stereo channel */
237 void combine_LR_new (double sb_sample[2][3][SCALE_BLOCK][SBLIMIT],
238 double joint_sample[3][SCALE_BLOCK][SBLIMIT], int sblimit) {
239 int sb, sample, gr;
241 for (sb = 0; sb < sblimit; ++sb)
242 for (sample = 0; sample < SCALE_BLOCK; ++sample)
243 for (gr = 0; gr < 3; ++gr)
244 joint_sample[gr][sample][sb] =
245 .5 * (sb_sample[0][gr][sample][sb] + sb_sample[1][gr][sample][sb]);
248 /* PURPOSE:For each subband, puts the smallest scalefactor of the 3
249 associated with a frame into #max_sc#. This is used
250 used by Psychoacoustic Model I.
251 Someone in dist10 source code's history, somebody wrote the following:
252 "(I would recommend changin max_sc to min_sc)"
254 In psy model 1, the *maximum* out of the scale picked here and
255 the maximum SPL within each subband is selected. So I'd think that
256 a maximum here makes heaps of sense.
258 MFC FIX: Feb 2003 - is this only needed for psy model 1?
260 void find_sf_max (unsigned int sf_index[2][3][SBLIMIT], frame_info * frame,
261 double sf_max[2][SBLIMIT])
263 int sb, gr, ch;
264 int lowest_sf_index;
265 int nch = frame->nch;
266 int sblimit = frame->sblimit;
268 for (ch = 0; ch < nch; ch++)
269 for (sb = 0; sb < sblimit; sb++) {
270 for (gr = 1, lowest_sf_index = sf_index[ch][0][sb]; gr < 3; gr++)
271 if (lowest_sf_index > sf_index[ch][gr][sb])
272 lowest_sf_index = sf_index[ch][gr][sb];
273 sf_max[ch][sb] = multiple[lowest_sf_index];
275 for (sb = sblimit; sb < SBLIMIT; sb++)
276 sf_max[0][sb] = sf_max[1][sb] = 1E-20;
279 /* sf_transmission_pattern
280 PURPOSE:For a given subband, determines whether to send 1, 2, or
281 all 3 of the scalefactors, and fills in the scalefactor
282 select information accordingly
284 This is From ISO11172 Sect C.1.5.2.5 "coding of scalefactors"
286 Table C.4 "LayerII Scalefactors Transmission Pattern"
288 void sf_transmission_pattern (unsigned int sf_index[2][3][SBLIMIT],
289 unsigned int sf_selectinfo[2][SBLIMIT],
290 frame_info * frame)
292 int nch = frame->nch;
293 int sblimit = frame->sblimit;
294 int dscf[2];
295 int class[2], i, j, k;
296 static int pattern[5][5] = { {0x123, 0x122, 0x122, 0x133, 0x123},
297 {0x113, 0x111, 0x111, 0x444, 0x113},
298 {0x111, 0x111, 0x111, 0x333, 0x113},
299 {0x222, 0x222, 0x222, 0x333, 0x123},
300 {0x123, 0x122, 0x122, 0x133, 0x123}
303 for (k = 0; k < nch; k++)
304 for (i = 0; i < sblimit; i++) {
305 dscf[0] = (sf_index[k][0][i] - sf_index[k][1][i]);
306 dscf[1] = (sf_index[k][1][i] - sf_index[k][2][i]);
307 for (j = 0; j < 2; j++) {
308 if (dscf[j] <= -3)
309 class[j] = 0;
310 else if (dscf[j] > -3 && dscf[j] < 0)
311 class[j] = 1;
312 else if (dscf[j] == 0)
313 class[j] = 2;
314 else if (dscf[j] > 0 && dscf[j] < 3)
315 class[j] = 3;
316 else
317 class[j] = 4;
319 switch (pattern[class[0]][class[1]]) {
320 case 0x123:
321 sf_selectinfo[k][i] = 0;
322 break;
323 case 0x122:
324 sf_selectinfo[k][i] = 3;
325 sf_index[k][2][i] = sf_index[k][1][i];
326 break;
327 case 0x133:
328 sf_selectinfo[k][i] = 3;
329 sf_index[k][1][i] = sf_index[k][2][i];
330 break;
331 case 0x113:
332 sf_selectinfo[k][i] = 1;
333 sf_index[k][1][i] = sf_index[k][0][i];
334 break;
335 case 0x111:
336 sf_selectinfo[k][i] = 2;
337 sf_index[k][1][i] = sf_index[k][2][i] = sf_index[k][0][i];
338 break;
339 case 0x222:
340 sf_selectinfo[k][i] = 2;
341 sf_index[k][0][i] = sf_index[k][2][i] = sf_index[k][1][i];
342 break;
343 case 0x333:
344 sf_selectinfo[k][i] = 2;
345 sf_index[k][0][i] = sf_index[k][1][i] = sf_index[k][2][i];
346 break;
347 case 0x444:
348 sf_selectinfo[k][i] = 2;
349 if (sf_index[k][0][i] > sf_index[k][2][i])
350 sf_index[k][0][i] = sf_index[k][2][i];
351 sf_index[k][1][i] = sf_index[k][2][i] = sf_index[k][0][i];
356 void write_header (frame_info * frame, Bit_stream_struc * bs)
358 frame_header *header = frame->header;
360 putbits (bs, 0xfff, 12); /* syncword 12 bits */
361 put1bit (bs, header->version); /* ID 1 bit */
362 putbits (bs, 4 - header->lay, 2); /* layer 2 bits */
363 put1bit (bs, !header->error_protection); /* bit set => no err prot */
364 putbits (bs, header->bitrate_index, 4);
365 putbits (bs, header->sampling_frequency, 2);
366 put1bit (bs, header->padding);
367 put1bit (bs, header->extension); /* private_bit */
368 putbits (bs, header->mode, 2);
369 putbits (bs, header->mode_ext, 2);
370 put1bit (bs, header->copyright);
371 put1bit (bs, header->original);
372 putbits (bs, header->emphasis, 2);
375 /*************************************************************************
376 encode_bit_alloc (Layer II)
378 PURPOSE:Writes bit allocation information onto bitstream
380 4,3,2, or 0 bits depending on the quantization table used.
382 ************************************************************************/
383 void write_bit_alloc (unsigned int bit_alloc[2][SBLIMIT],
384 frame_info * frame, Bit_stream_struc * bs)
386 int sb, ch;
387 int nch = frame->nch;
388 int sblimit = frame->sblimit;
389 int jsbound = frame->jsbound;
391 for (sb = 0; sb < sblimit; sb++) {
392 if (sb < jsbound) {
393 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ch++)
394 putbits (bs, bit_alloc[ch][sb], nbal[ line[tablenum][sb] ]); // (*alloc)[sb][0].bits);
396 else
397 putbits (bs, bit_alloc[0][sb], nbal[ line[tablenum][sb] ]); //(*alloc)[sb][0].bits);
401 /************************************************************************
402 write_scalefactors
404 PURPOSE:The encoded scalar factor information is arranged and
405 queued into the output fifo to be transmitted.
407 The three scale factors associated with
408 a given subband and channel are transmitted in accordance
409 with the scfsi, which is transmitted first.
411 ************************************************************************/
413 void write_scalefactors (unsigned int bit_alloc[2][SBLIMIT],
414 unsigned int sf_selectinfo[2][SBLIMIT],
415 unsigned int sf_index[2][3][SBLIMIT], frame_info * frame,
416 Bit_stream_struc * bs)
418 int nch = frame->nch;
419 int sblimit = frame->sblimit;
420 int sb, gr, ch;
422 /* Write out the scalefactor selection information */
423 for (sb = 0; sb < sblimit; sb++)
424 for (ch = 0; ch < nch; ch++)
425 if (bit_alloc[ch][sb])
426 putbits (bs, sf_selectinfo[ch][sb], 2);
428 for (sb = 0; sb < sblimit; sb++)
429 for (ch = 0; ch < nch; ch++)
430 if (bit_alloc[ch][sb]) /* above jsbound, bit_alloc[0][i] == ba[1][i] */
431 switch (sf_selectinfo[ch][sb]) {
432 case 0:
433 for (gr = 0; gr < 3; gr++)
434 putbits (bs, sf_index[ch][gr][sb], 6);
435 break;
436 case 1:
437 case 3:
438 putbits (bs, sf_index[ch][0][sb], 6);
439 putbits (bs, sf_index[ch][2][sb], 6);
440 break;
441 case 2:
442 putbits (bs, sf_index[ch][0][sb], 6);
447 /* ISO11172 Table C.6 Layer II quantization co-efficients */
448 static double a[18] = {
450 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
451 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
452 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
453 0.999969482, 0.999984741
456 static double b[18] = {
458 -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
459 -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
460 -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
461 -0.000030518, -0.000015259
464 /************************************************************************
465 subband_quantization (Layer II)
467 PURPOSE:Quantizes subband samples to appropriate number of bits
469 SEMANTICS: Subband samples are divided by their scalefactors, which
470 makes the quantization more efficient. The scaled samples are
471 quantized by the function a*x+b, where a and b are functions of
472 the number of quantization levels. The result is then truncated
473 to the appropriate number of bits and the MSB is inverted.
475 Note that for fractional 2's complement, inverting the MSB for a
476 negative number x is equivalent to adding 1 to it.
478 ************************************************************************/
479 void
480 subband_quantization_new (unsigned int sf_index[2][3][SBLIMIT],
481 double sb_samples[2][3][SCALE_BLOCK][SBLIMIT],
482 unsigned int j_scale[3][SBLIMIT],
483 double j_samps[3][SCALE_BLOCK][SBLIMIT],
484 unsigned int bit_alloc[2][SBLIMIT],
485 unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
486 frame_info * frame)
488 int sb, j, ch, gr, qnt_coeff_index, sig;
489 int nch = frame->nch;
490 int sblimit = frame->sblimit;
491 int jsbound = frame->jsbound;
492 double d;
494 for (gr = 0; gr < 3; gr++)
495 for (j = 0; j < SCALE_BLOCK; j++)
496 for (sb = 0; sb < sblimit; sb++)
497 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ch++)
499 if (bit_alloc[ch][sb]) {
500 /* scale and quantize FLOATing point sample */
501 if (nch == 2 && sb >= jsbound) /* use j-stereo samples */
502 d = j_samps[gr][j][sb] / scalefactor[j_scale[gr][sb]];
503 else
504 d = sb_samples[ch][gr][j][sb] / scalefactor[sf_index[ch][gr][sb]];
506 /* Check that the wrong scale factor hasn't been chosen -
507 which would result in a scaled sample being > 1.0
508 This error shouldn't ever happen *unless* something went wrong in
509 scalefactor calc
511 if (mod (d) > 1.0)
512 fprintf (stderr, "Not scaled properly %d %d %d %d\n", ch, gr, j,
513 sb);
517 /* 'index' indicates which "step line" we are using */
518 int index = line[tablenum][sb];
520 /* Find the "step index" within that line */
521 qnt_coeff_index = step_index[index][bit_alloc[ch][sb]];
523 d = d * a[qnt_coeff_index] + b[qnt_coeff_index];
525 /* extract MSB N-1 bits from the FLOATing point sample */
526 if (d >= 0)
527 sig = 1;
528 else {
529 sig = 0;
530 d += 1.0;
533 sbband[ch][gr][j][sb] = (unsigned int) (d * (double)steps2n[qnt_coeff_index]);
534 /* tag the inverted sign bit to sbband at position N */
535 /* The bit inversion is a must for grouping with 3,5,9 steps
536 so it is done for all subbands */
537 if (sig)
538 sbband[ch][gr][j][sb] |= steps2n[qnt_coeff_index];
541 /* Set everything above the sblimit to 0 */
542 for (ch = 0; ch < nch; ch++)
543 for (gr = 0; gr < 3; gr++)
544 for (sb = 0; sb < SCALE_BLOCK; sb++)
545 for (j = sblimit; j < SBLIMIT; j++)
546 sbband[ch][gr][sb][j] = 0;
549 /************************************************************************
550 sample_encoding
552 PURPOSE:Put one frame of subband samples on to the bitstream
554 SEMANTICS: The number of bits allocated per sample is read from
555 the bit allocation information #bit_alloc#. Layer 2
556 supports writing grouped samples for quantization steps
557 that are not a power of 2.
559 ***********************************************************************/
560 void write_samples_new (unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
561 unsigned int bit_alloc[2][SBLIMIT],
562 frame_info * frame, Bit_stream_struc * bs)
564 unsigned int temp;
565 unsigned int sb, j, ch, gr, x, y;
566 int nch = frame->nch;
567 int sblimit = frame->sblimit;
568 int jsbound = frame->jsbound;
570 for (gr = 0; gr < 3; gr++)
571 for (j = 0; j < SCALE_BLOCK; j += 3)
572 for (sb = 0; sb < sblimit; sb++)
573 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ch++)
575 if (bit_alloc[ch][sb]) {
576 int thisline = line[tablenum][sb];
577 int thisstep_index = step_index[thisline][bit_alloc[ch][sb]];
578 /* Check how many samples per codeword */
579 if (group[thisstep_index] == 3) {
580 /* Going to send 1 sample per codeword -> 3 samples */
581 for (x = 0; x < 3; x++) {
582 putbits (bs, sbband[ch][gr][j + x][sb], bits[thisstep_index]);
584 } else {
585 /* ISO11172 Sec C.1.5.2.8
586 If steps=3, 5 or 9, then three consecutive samples are coded
587 as one codeword i.e. only one value (V) is transmitted for this
588 triplet. If the 3 subband samples are x,y,z then
589 V = (steps*steps)*z + steps*y +x
591 y = steps[thisstep_index];
592 temp =
593 sbband[ch][gr][j][sb] + sbband[ch][gr][j + 1][sb] * y +
594 sbband[ch][gr][j + 2][sb] * y * y;
595 putbits (bs, temp, bits[thisstep_index]);
601 //#include "bit_alloc_new.c"
602 /***************************************************************************************/
603 /* Bit Allocation Routines */
606 /************************************************************************
608 * bits_for_nonoise (Layer II)
610 * PURPOSE:Returns the number of bits required to produce a
611 * mask-to-noise ratio better or equal to the noise/no_noise threshold.
613 * SEMANTICS:
614 * bbal = # bits needed for encoding bit allocation
615 * bsel = # bits needed for encoding scalefactor select information
616 * banc = # bits needed for ancillary data (header info included)
618 * For each subband and channel, will add bits until one of the
619 * following occurs:
620 * - Hit maximum number of bits we can allocate for that subband
621 * - MNR is better than or equal to the minimum masking level
622 * (NOISY_MIN_MNR)
623 * Then the bits required for scalefactors, scfsi, bit allocation,
624 * and the subband samples are tallied (#req_bits#) and returned.
626 * (NOISY_MIN_MNR) is the smallest MNR a subband can have before it is
627 * counted as 'noisy' by the logic which chooses the number of JS
628 * subbands.
630 * Joint stereo is supported.
632 ************************************************************************/
634 int bits_for_nonoise_new (double SMR[2][SBLIMIT],
635 unsigned int scfsi[2][SBLIMIT], frame_info * frame, float min_mnr,
636 unsigned int bit_alloc[2][SBLIMIT])
638 int sb, ch, ba;
639 int nch = frame->nch;
640 int sblimit = frame->sblimit;
641 int jsbound = frame->jsbound;
642 int req_bits = 0, bbal = 0, berr = 0, banc = 32;
643 int maxAlloc, sel_bits, sc_bits, smp_bits;
644 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
646 /* MFC Feb 2003
647 This works out the basic number of bits just to get a valid (but empty)
648 frame.
649 This needs to be done for every frame, since a joint_stereo frame
650 will change the number of basic bits (depending on the sblimit in
651 the particular js mode that's been selected */
653 /* Make sure there's room for the error protection bits */
654 if (frame->header->error_protection)
655 berr = 16;
656 else
657 berr = 0;
659 /* Count the number of bits required to encode the quantization index for both
660 channels in each subband. If we're above the jsbound, then pretend we only
661 have one channel */
662 for (sb = 0; sb < jsbound; ++sb)
663 bbal += nch * nbal[ line[tablenum][sb] ]; //(*alloc)[sb][0].bits;
664 for (sb = jsbound; sb < sblimit; ++sb)
665 bbal += nbal[ line[tablenum][sb] ]; //(*alloc)[sb][0].bits;
666 req_bits = banc + bbal + berr;
668 for (sb = 0; sb < sblimit; ++sb)
669 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ++ch) {
670 int thisline = line[tablenum][sb];
672 /* How many possible steps are there to choose from ? */
673 maxAlloc = (1 << nbal[ line[tablenum][sb] ]) -1; //(*alloc)[sb][0].bits) - 1;
674 sel_bits = sc_bits = smp_bits = 0;
675 /* Keep choosing the next number of steps (and hence our SNR value)
676 until we have the required MNR value */
677 for (ba = 0; ba < maxAlloc - 1; ++ba) {
678 int thisstep_index = step_index[thisline][ba];
679 if ((SNR[thisstep_index] - SMR[ch][sb]) >= min_mnr)
680 break; /* we found enough bits */
682 if (nch == 2 && sb >= jsbound) /* check other JS channel */
683 for (; ba < maxAlloc - 1; ++ba) {
684 int thisstep_index = step_index[thisline][ba];
685 if ((SNR[thisstep_index] - SMR[1-ch][sb]) >= min_mnr)
686 break;
688 if (ba > 0) {
689 //smp_bits = SCALE_BLOCK * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
690 int thisstep_index = step_index[thisline][ba];
691 smp_bits = SCALE_BLOCK * group[thisstep_index] * bits[thisstep_index];
692 /* scale factor bits required for subband */
693 sel_bits = 2;
694 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
695 if (nch == 2 && sb >= jsbound) {
696 /* each new js sb has L+R scfsis */
697 sel_bits += 2;
698 sc_bits += 6 * sfsPerScfsi[scfsi[1 - ch][sb]];
700 req_bits += smp_bits + sel_bits + sc_bits;
702 bit_alloc[ch][sb] = ba;
704 return req_bits;
709 /************************************************************************
711 * main_bit_allocation (Layer II)
713 * PURPOSE:For joint stereo mode, determines which of the 4 joint
714 * stereo modes is needed. Then calls *_a_bit_allocation(), which
715 * allocates bits for each of the subbands until there are no more bits
716 * left, or the MNR is at the noise/no_noise threshold.
718 * SEMANTICS:
720 * For joint stereo mode, joint stereo is changed to stereo if
721 * there are enough bits to encode stereo at or better than the
722 * no-noise threshold (NOISY_MIN_MNR). Otherwise, the system
723 * iteratively allocates less bits by using joint stereo until one
724 * of the following occurs:
725 * - there are no more noisy subbands (MNR >= NOISY_MIN_MNR)
726 * - mode_ext has been reduced to 0, which means that all but the
727 * lowest 4 subbands have been converted from stereo to joint
728 * stereo, and no more subbands may be converted
730 * This function calls *_bits_for_nonoise() and *_a_bit_allocation().
732 ************************************************************************/
733 void main_bit_allocation_new (double SMR[2][SBLIMIT],
734 unsigned int scfsi[2][SBLIMIT],
735 unsigned int bit_alloc[2][SBLIMIT], int *adb,
736 frame_info * frame, options * glopts)
738 int noisy_sbs;
739 int mode, mode_ext, lay;
740 int rq_db; /* av_db = *adb; Not Used MFC Nov 99 */
742 /* these are the tables which specify the limits within which the VBR can vary
743 You can't vary outside these ranges, otherwise a new alloc table would have to
744 be loaded in the middle of encoding. This VBR hack is dodgy - the standard
745 says that LayerII decoders don't have to support a variable bitrate, but Layer3
746 decoders must do so. Hence, it is unlikely that a compliant layer2 decoder would be
747 written to dynmically change allocation tables. *BUT* a layer3 encoder might handle it
748 by default, meaning we could switch tables mid-encode and enjoy a wider range of bitrates
749 for the VBR encoding.
750 None of this needs to be done for LSF, since there is only *one* possible alloc table in LSF
751 MFC Feb 2003 */
752 int vbrlimits[2][3][2] = {
753 /* MONO */
754 { /* 44 */ {6, 10},
755 /* 48 */ {3, 10},
756 /* 32 */ {6, 10}},
757 /* STEREO */
758 { /* 44 */ {10, 14},
759 /* 48 */ {7, 14},
760 /* 32 */ {10, 14}}
763 static int init = 0;
764 static int lower = 10, upper = 10;
765 static int bitrateindextobits[15] =
766 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
767 int guessindex = 0;
769 if (init == 0) {
770 int nch = 2;
771 int sfreq;
772 frame_header *header = frame->header;
773 init++;
774 if (header->version == 0) {
775 /* LSF: so can use any bitrate index from 1->15 */
776 lower = 1;
777 upper = 14;
778 } else {
779 if (frame->actual_mode == MPG_MD_MONO)
780 nch = 1;
781 sfreq = header->sampling_frequency;
782 lower = vbrlimits[nch-1][sfreq][0];
783 upper = vbrlimits[nch-1][sfreq][1];
785 if (glopts->verbosity > 2)
786 fprintf (stdout, "VBR bitrate index limits [%i -> %i]\n", lower, upper);
789 /* set up a conversion table for bitrateindex->bits for this version/sampl freq
790 This will be used to find the best bitrate to cope with the number of bits that
791 are needed (as determined by VBR_bits_for_nonoise) */
792 int brindex;
793 frame_header *header = frame->header;
794 for (brindex = lower; brindex <= upper; brindex++) {
795 bitrateindextobits[brindex] =
796 (int) (1152.0 / s_freq[header->version][header->sampling_frequency]) *
797 ((double) bitrate[header->version][brindex]);
803 if ((mode = frame->actual_mode) == MPG_MD_JOINT_STEREO) {
804 frame->header->mode = MPG_MD_STEREO;
805 frame->header->mode_ext = 0;
806 frame->jsbound = frame->sblimit;
807 if ((rq_db = bits_for_nonoise_new (SMR, scfsi, frame, 0, bit_alloc)) > *adb) {
808 frame->header->mode = MPG_MD_JOINT_STEREO;
809 mode_ext = 4; /* 3 is least severe reduction */
810 lay = frame->header->lay;
811 do {
812 --mode_ext;
813 frame->jsbound = js_bound (mode_ext);
814 rq_db = bits_for_nonoise_new (SMR, scfsi, frame, 0, bit_alloc);
816 while ((rq_db > *adb) && (mode_ext > 0));
817 frame->header->mode_ext = mode_ext;
818 } /* well we either eliminated noisy sbs or mode_ext == 0 */
821 /* decide on which bit allocation method to use */
822 if (glopts->vbr == FALSE) {
823 /* Just do the old bit allocation method */
824 noisy_sbs = a_bit_allocation_new (SMR, scfsi, bit_alloc, adb, frame);
825 } else {
826 /* do the VBR bit allocation method */
827 frame->header->bitrate_index = lower;
828 *adb = available_bits (frame->header, glopts);
830 int brindex;
831 int found = FALSE;
833 /* Work out how many bits are needed for there to be no noise (ie all MNR > VBRLEVEL) */
834 int req =
835 bits_for_nonoise_new (SMR, scfsi, frame, glopts->vbrlevel, bit_alloc);
837 /* Look up this value in the bitrateindextobits table to find what bitrate we should use for
838 this frame */
839 for (brindex = lower; brindex <= upper; brindex++) {
840 if (bitrateindextobits[brindex] > req) {
841 /* this method always *overestimates* the bits that are needed
842 i.e. it will usually guess right but
843 when it's wrong it'll guess a higher bitrate than actually required.
844 e.g. on "messages from earth" track 6, the guess was
845 wrong on 75/36341 frames. each time it guessed higher.
846 MFC Feb 2003 */
847 guessindex = brindex;
848 found = TRUE;
849 break;
852 /* Just for sanity */
853 if (found == FALSE)
854 guessindex = upper;
857 frame->header->bitrate_index = guessindex;
858 *adb = available_bits (frame->header, glopts);
860 /* update the statistics */
861 vbrstats_new[frame->header->bitrate_index]++;
863 if (glopts->verbosity > 2) {
864 /* print out the VBR stats every 1000th frame */
865 static int count = 0;
866 int i;
867 if ((count++ % 1000) == 0) {
868 for (i = 1; i < 15; i++)
869 fprintf (stdout, "%4i ", vbrstats_new[i]);
870 fprintf (stdout, "\n");
873 /* Print out *every* frames bitrateindex, bits required, and bits available at this bitrate */
874 if (glopts->verbosity > 5)
875 fprintf (stdout,
876 "> bitrate index %2i has %i bits available to encode the %i bits\n",
877 frame->header->bitrate_index, *adb,
878 bits_for_nonoise_new (SMR, scfsi, frame,
879 glopts->vbrlevel, bit_alloc));
883 noisy_sbs =
884 VBR_bit_allocation_new (SMR, scfsi, bit_alloc, adb, frame, glopts);
888 void VBR_maxmnr_new (double mnr[2][SBLIMIT], char used[2][SBLIMIT], int sblimit,
889 int nch, int *min_sb, int *min_ch, options * glopts)
891 int sb, ch;
892 double small;
894 small = 999999.0;
895 *min_sb = -1;
896 *min_ch = -1;
898 #define NEWBITx
899 #ifdef NEWBIT
900 /* Keep going until all subbands have reached the MNR level that we specified */
901 for (ch=0;ch<nch;ch++)
902 for (sb=0;sb<sblimit;sb++)
903 if (mnr[ch][sb] < glopts->vbrlevel) {
904 *min_sb = sb;
905 *min_ch = ch;
906 //fprintf(stdout,".");
907 //fflush(stdout);
908 return;
910 #endif
912 /* Then start adding bits to whichever is the min MNR */
913 for (ch = 0; ch < nch; ++ch)
914 for (sb = 0; sb < sblimit; sb++)
915 if (used[ch][sb] != 2 && small > mnr[ch][sb]) {
916 small = mnr[ch][sb];
917 *min_sb = sb;
918 *min_ch = ch;
920 //fprintf(stdout,"Min sb: %i\n",*min_sb);
922 /********************
923 MFC Feb 2003
924 VBR_bit_allocation is different to the normal a_bit_allocation in that
925 it is known beforehand that there are definitely enough bits to do what we
926 have to - i.e. a bitrate was specificially chosen in main_bit_allocation so
927 that we have enough bits to encode what we have to.
928 This function should take that into account and just greedily assign
929 the bits, rather than fussing over the minimum MNR subband - we know
930 each subband gets its required bits, why quibble?
931 This function doesn't chew much CPU, so I haven't made any attempt
932 to do this yet.
933 *********************/
934 int VBR_bit_allocation_new (double SMR[2][SBLIMIT],
935 unsigned int scfsi[2][SBLIMIT],
936 unsigned int bit_alloc[2][SBLIMIT], int *adb,
937 frame_info * frame, options *glopts)
939 int sb, min_ch, min_sb, oth_ch, ch, increment, scale, seli, ba;
940 int bspl, bscf, bsel, ad, bbal = 0;
941 double mnr[2][SBLIMIT];
942 char used[2][SBLIMIT];
943 int nch = frame->nch;
944 int sblimit = frame->sblimit;
945 int jsbound = frame->jsbound;
946 //al_table *alloc = frame->alloc;
947 static char init = 0;
948 static int banc = 32, berr = 0;
949 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
951 int thisstep_index;
953 if (!init) {
954 init = 1;
955 if (frame->header->error_protection)
956 berr = 16; /* added 92-08-11 shn */
959 /* No need to worry about jsbound here as JS is disabled for VBR mode */
960 for (sb = 0; sb < sblimit; sb++)
961 bbal += nch * nbal[ line[tablenum][sb] ];
962 *adb -= bbal + berr + banc;
963 ad = *adb;
965 for (sb = 0; sb < sblimit; sb++)
966 for (ch = 0; ch < nch; ch++) {
967 mnr[ch][sb] = SNR[0] - SMR[ch][sb];
968 bit_alloc[ch][sb] = 0;
969 used[ch][sb] = 0;
971 bspl = bscf = bsel = 0;
973 do {
974 /* locate the subband with minimum SMR */
975 VBR_maxmnr_new (mnr, used, sblimit, nch, &min_sb, &min_ch, glopts);
977 if (min_sb > -1) { /* there was something to find */
978 int thisline = line[tablenum][min_sb]; {
979 /* find increase in bit allocation in subband [min] */
980 int nextstep_index = step_index[thisline][bit_alloc[min_ch][min_sb]+1];
981 increment = SCALE_BLOCK * group[nextstep_index] * bits[nextstep_index];
983 if (used[min_ch][min_sb]) {
984 /* If we've already increased the limit on this ch/sb, then
985 subtract the last thing that we added */
986 thisstep_index = step_index[thisline][bit_alloc[min_ch][min_sb]];
987 increment -= SCALE_BLOCK * group[thisstep_index] * bits[thisstep_index];
990 /* scale factor bits required for subband [min] */
991 oth_ch = 1 - min_ch; /* above js bound, need both chans */
992 if (used[min_ch][min_sb])
993 scale = seli = 0;
994 else { /* this channel had no bits or scfs before */
995 seli = 2;
996 scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
997 if (nch == 2 && min_sb >= jsbound) {
998 /* each new js sb has L+R scfsis */
999 seli += 2;
1000 scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
1004 /* check to see enough bits were available for */
1005 /* increasing resolution in the minimum band */
1006 if (ad >= bspl + bscf + bsel + seli + scale + increment) {
1007 /* Then there are enough bits to have another go at allocating */
1008 ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
1009 bspl += increment; /* bits for subband sample */
1010 bscf += scale; /* bits for scale factor */
1011 bsel += seli; /* bits for scfsi code */
1012 used[min_ch][min_sb] = 1; /* subband has bits */
1013 thisstep_index = step_index[thisline][ba];
1014 mnr[min_ch][min_sb] = SNR[thisstep_index] - SMR[min_ch][min_sb];
1015 /* Check if this min_sb subband has been fully allocated max bits */
1016 if (ba >= (1 << nbal[ line[tablenum][min_sb] ]) -1 ) //(*alloc)[min_sb][0].bits) - 1)
1017 used[min_ch][min_sb] = 2; /* don't let this sb get any more bits */
1018 } else
1019 used[min_ch][min_sb] = 2; /* can't increase this alloc */
1022 while (min_sb > -1); /* until could find no channel */
1024 /* Calculate the number of bits left */
1025 ad -= bspl + bscf + bsel;
1026 *adb = ad;
1027 for (ch = 0; ch < nch; ch++)
1028 for (sb = sblimit; sb < SBLIMIT; sb++)
1029 bit_alloc[ch][sb] = 0;
1031 return 0;
1036 /************************************************************************
1038 * a_bit_allocation (Layer II)
1040 * PURPOSE:Adds bits to the subbands with the lowest mask-to-noise
1041 * ratios, until the maximum number of bits for the subband has
1042 * been allocated.
1044 * SEMANTICS:
1045 * 1. Find the subband and channel with the smallest MNR (#min_sb#,
1046 * and #min_ch#)
1047 * 2. Calculate the increase in bits needed if we increase the bit
1048 * allocation to the next higher level
1049 * 3. If there are enough bits available for increasing the resolution
1050 * in #min_sb#, #min_ch#, and the subband has not yet reached its
1051 * maximum allocation, update the bit allocation, MNR, and bits
1052 available accordingly
1053 * 4. Repeat until there are no more bits left, or no more available
1054 * subbands. (A subband is still available until the maximum
1055 * number of bits for the subband has been allocated, or there
1056 * aren't enough bits to go to the next higher resolution in the
1057 subband.)
1059 ************************************************************************/
1061 void maxmnr_new (double mnr[2][SBLIMIT], char used[2][SBLIMIT], int sblimit,
1062 int nch, int *min_sb, int *min_ch)
1064 int sb, ch;
1065 double small;
1067 small = 999999.0;
1068 *min_sb = -1;
1069 *min_ch = -1;
1070 for (ch = 0; ch < nch; ++ch)
1071 for (sb = 0; sb < sblimit; sb++)
1072 if (used[ch][sb] != 2 && small > mnr[ch][sb]) {
1073 small = mnr[ch][sb];
1074 *min_sb = sb;
1075 *min_ch = ch;
1078 int a_bit_allocation_new (double SMR[2][SBLIMIT],
1079 unsigned int scfsi[2][SBLIMIT],
1080 unsigned int bit_alloc[2][SBLIMIT], int *adb,
1081 frame_info * frame)
1083 int sb, min_ch, min_sb, oth_ch, ch, increment, scale, seli, ba;
1084 int bspl, bscf, bsel, ad, bbal = 0;
1085 double mnr[2][SBLIMIT];
1086 char used[2][SBLIMIT];
1087 int nch = frame->nch;
1088 int sblimit = frame->sblimit;
1089 int jsbound = frame->jsbound;
1090 //al_table *alloc = frame->alloc;
1091 static char init = 0;
1092 static int banc = 32, berr = 0;
1093 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
1095 int thisstep_index;
1097 if (!init) {
1098 init = 1;
1099 if (frame->header->error_protection)
1100 berr = 16; /* added 92-08-11 shn */
1103 for (sb = 0; sb < jsbound; sb++)
1104 bbal += nch * nbal[ line[tablenum][sb] ]; //(*alloc)[sb][0].bits;
1105 for (sb = jsbound; sb < sblimit; sb++)
1106 bbal += nbal[ line[tablenum][sb] ]; //(*alloc)[sb][0].bits;
1107 *adb -= bbal + berr + banc;
1108 ad = *adb;
1110 for (sb = 0; sb < sblimit; sb++)
1111 for (ch = 0; ch < nch; ch++) {
1112 mnr[ch][sb] = SNR[0] - SMR[ch][sb];
1113 bit_alloc[ch][sb] = 0;
1114 used[ch][sb] = 0;
1116 bspl = bscf = bsel = 0;
1118 do {
1119 /* locate the subband with minimum SMR */
1120 maxmnr_new (mnr, used, sblimit, nch, &min_sb, &min_ch);
1122 if (min_sb > -1) { /* there was something to find */
1123 int thisline = line[tablenum][min_sb]; {
1124 /* find increase in bit allocation in subband [min] */
1125 int nextstep_index = step_index[thisline][bit_alloc[min_ch][min_sb]+1];
1126 increment = SCALE_BLOCK * group[nextstep_index] * bits[nextstep_index];
1128 if (used[min_ch][min_sb]) {
1129 /* If we've already increased the limit on this ch/sb, then
1130 subtract the last thing that we added */
1131 thisstep_index = step_index[thisline][bit_alloc[min_ch][min_sb]];
1132 increment -= SCALE_BLOCK * group[thisstep_index] * bits[thisstep_index];
1135 /* scale factor bits required for subband [min] */
1136 oth_ch = 1 - min_ch; /* above js bound, need both chans */
1137 if (used[min_ch][min_sb])
1138 scale = seli = 0;
1139 else { /* this channel had no bits or scfs before */
1140 seli = 2;
1141 scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
1142 if (nch == 2 && min_sb >= jsbound) {
1143 /* each new js sb has L+R scfsis */
1144 seli += 2;
1145 scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
1149 /* check to see enough bits were available for */
1150 /* increasing resolution in the minimum band */
1151 if (ad >= bspl + bscf + bsel + seli + scale + increment) {
1152 /* Then there are enough bits to have another go at allocating */
1153 ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
1154 bspl += increment; /* bits for subband sample */
1155 bscf += scale; /* bits for scale factor */
1156 bsel += seli; /* bits for scfsi code */
1157 used[min_ch][min_sb] = 1; /* subband has bits */
1158 thisstep_index = step_index[thisline][ba];
1159 mnr[min_ch][min_sb] = SNR[thisstep_index] - SMR[min_ch][min_sb];
1160 /* Check if this min_sb subband has been fully allocated max bits */
1161 if (ba >= (1 << nbal[ line[tablenum][min_sb] ]) -1 ) //(*alloc)[min_sb][0].bits) - 1)
1162 used[min_ch][min_sb] = 2; /* don't let this sb get any more bits */
1163 } else
1164 used[min_ch][min_sb] = 2; /* can't increase this alloc */
1166 if (min_sb >= jsbound && nch == 2) {
1167 /* above jsbound, alloc applies L+R */
1168 ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
1169 used[oth_ch][min_sb] = used[min_ch][min_sb];
1170 thisstep_index = step_index[thisline][ba];
1171 mnr[oth_ch][min_sb] = SNR[thisstep_index] - SMR[oth_ch][min_sb];
1172 //mnr[oth_ch][min_sb] = SNR[(*alloc)[min_sb][ba].quant + 1] - SMR[oth_ch][min_sb];
1177 while (min_sb > -1); /* until could find no channel */
1179 /* Calculate the number of bits left */
1180 ad -= bspl + bscf + bsel;
1181 *adb = ad;
1182 for (ch = 0; ch < nch; ch++)
1183 for (sb = sblimit; sb < SBLIMIT; sb++)
1184 bit_alloc[ch][sb] = 0;
1186 return 0;