Make FloatAutos::get_values() always use a PLAY_FORWARD direction.
[cinelerra_cv/pmdumuid.git] / toolame-02l / encode.c
blobfa9bd4a3bec705350748f19da37d5d27c8f55a9c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "common.h"
5 #include "encoder.h"
6 #include "bitstream.h"
7 #include "availbits.h"
8 #include "encode.h"
10 int vbrstats[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
12 /* This segment contains all the core routines of the encoder,
13 except for the psychoacoustic models.
15 The user can select either one of the two psychoacoustic
16 models. Model I is a simple tonal and noise masking threshold
17 generator, and Model II is a more sophisticated cochlear masking
18 threshold generator. Model I is recommended for lower complexity
19 applications whereas Model II gives better subjective quality at low
20 bit rates. */
22 /************************************************************************
23 * encode_info()
25 * PURPOSE: Puts the syncword and header information on the output
26 * bitstream.
28 ************************************************************************/
30 void encode_info (frame_info * frame, Bit_stream_struc * bs)
32 frame_header *header = frame->header;
34 putbits (bs, 0xfff, 12); /* syncword 12 bits */
35 put1bit (bs, header->version); /* ID 1 bit */
36 putbits (bs, 4 - header->lay, 2); /* layer 2 bits */
37 put1bit (bs, !header->error_protection); /* bit set => no err prot */
38 putbits (bs, header->bitrate_index, 4);
39 putbits (bs, header->sampling_frequency, 2);
40 put1bit (bs, header->padding);
41 put1bit (bs, header->extension); /* private_bit */
42 putbits (bs, header->mode, 2);
43 putbits (bs, header->mode_ext, 2);
44 put1bit (bs, header->copyright);
45 put1bit (bs, header->original);
46 putbits (bs, header->emphasis, 2);
49 /************************************************************************
51 * combine_LR (Layer II)
53 * PURPOSE:Combines left and right channels into a mono channel
55 * SEMANTICS: The average of left and right subband samples is put into
56 * #joint_sample#
59 ************************************************************************/
61 void combine_LR (double sb_sample[2][3][SCALE_BLOCK][SBLIMIT],
62 double joint_sample[3][SCALE_BLOCK][SBLIMIT], int sblimit)
63 { /* make a filtered mono for joint stereo */
64 int sb, smp, sufr;
66 for (sb = 0; sb < sblimit; ++sb)
67 for (smp = 0; smp < SCALE_BLOCK; ++smp)
68 for (sufr = 0; sufr < 3; ++sufr)
69 joint_sample[sufr][smp][sb] =
70 .5 * (sb_sample[0][sufr][smp][sb] + sb_sample[1][sufr][smp][sb]);
73 /************************************************************************
75 * scale_factor_calc (Layer II)
77 * PURPOSE:For each subband, calculate the scale factor for each set
78 * of the 12 subband samples
80 * SEMANTICS: Pick the scalefactor #multiple[]# just larger than the
81 * absolute value of the peak subband sample of 12 samples,
82 * and store the corresponding scalefactor index in #scalar#.
84 * Layer II has three sets of 12-subband samples for a given
85 * subband.
87 ************************************************************************/
89 #define PDS1
90 #ifdef PDS1
91 void scale_factor_calc (double sb_sample[][3][SCALE_BLOCK][SBLIMIT],
92 unsigned int scalar[][3][SBLIMIT], int nch,
93 int sblimit)
95 /* Optimized to use binary search instead of linear scan through the
96 scalefactor table; guarantees to find scalefactor in only 5
97 jumps/comparisons and not in {0 (lin. best) to 63 (lin. worst)}.
98 Scalefactors for subbands > sblimit are no longer computed.
99 Uses a single sblimit-loop.
100 Patrick De Smet Oct 1999.
102 int k, t;
103 /* Using '--' loops to avoid possible "cmp value + bne/beq" compiler */
104 /* inefficiencies. Below loops should compile to "bne/beq" only code */
105 for (k = nch; k--;)
106 for (t = 3; t--;) {
107 int i;
108 for (i = sblimit; i--;) {
109 int j;
110 unsigned int l;
111 register double temp;
112 unsigned int scale_fac;
113 /* Determination of max. over each set of 12 subband samples: */
114 /* PDS TODO: maybe this could/should ??!! be integrated into */
115 /* the subband filtering routines? */
116 register double cur_max = fabs (sb_sample[k][t][SCALE_BLOCK - 1][i]);
117 for (j = SCALE_BLOCK - 1; j--;) {
118 if ((temp = fabs (sb_sample[k][t][j][i])) > cur_max)
119 cur_max = temp;
121 /* PDS: binary search in the scalefactor table: */
122 /* This is the real speed up: */
123 for (l = 16, scale_fac = 32; l; l >>= 1) {
124 if (cur_max <= multiple[scale_fac])
125 scale_fac += l;
126 else
127 scale_fac -= l;
129 if (cur_max > multiple[scale_fac])
130 scale_fac--;
131 scalar[k][t][i] = scale_fac;
135 #else
136 void scale_factor_calc (sb_sample, scalar, nch, sblimit)
137 double sb_sample[][3][SCALE_BLOCK][SBLIMIT];
138 unsigned int scalar[][3][SBLIMIT];
139 int nch, sblimit;
141 int i, j, k, t;
142 double s[SBLIMIT];
144 for (k = 0; k < nch; k++)
145 for (t = 0; t < 3; t++) {
146 for (i = 0; i < sblimit; i++)
147 for (j = 1, s[i] = fabs (sb_sample[k][t][0][i]); j < SCALE_BLOCK; j++)
148 if (fabs (sb_sample[k][t][j][i]) > s[i])
149 s[i] = fabs (sb_sample[k][t][j][i]);
151 for (i = 0; i < sblimit; i++)
152 for (j = SCALE_RANGE - 2, scalar[k][t][i] = 0; j >= 0; j--) /* $A 6/16/92 */
153 if (s[i] <= multiple[j]) {
154 scalar[k][t][i] = j;
155 break;
157 for (i = sblimit; i < SBLIMIT; i++)
158 scalar[k][t][i] = SCALE_RANGE - 1;
162 #endif
163 /************************************************************************
165 * pick_scale (Layer II)
167 * PURPOSE:For each subband, puts the smallest scalefactor of the 3
168 * associated with a frame into #max_sc#. This is used
169 * used by Psychoacoustic Model I.
170 * (I would recommend changin max_sc to min_sc)
172 ************************************************************************/
174 void pick_scale (unsigned int scalar[2][3][SBLIMIT], frame_info * frame,
175 double max_sc[2][SBLIMIT])
177 int i, j, k, max;
178 int nch = frame->nch;
179 int sblimit = frame->sblimit;
181 for (k = 0; k < nch; k++)
182 for (i = 0; i < sblimit; max_sc[k][i] = multiple[max], i++)
183 for (j = 1, max = scalar[k][0][i]; j < 3; j++)
184 if (max > scalar[k][j][i])
185 max = scalar[k][j][i];
186 for (i = sblimit; i < SBLIMIT; i++)
187 max_sc[0][i] = max_sc[1][i] = 1E-20;
190 /************************************************************************
192 * transmission_pattern (Layer II only)
194 * PURPOSE:For a given subband, determines whether to send 1, 2, or
195 * all 3 of the scalefactors, and fills in the scalefactor
196 * select information accordingly
198 * SEMANTICS: The subbands and channels are classified based on how much
199 * the scalefactors changes over its three values (corresponding
200 * to the 3 sets of 12 samples per subband). The classification
201 * will send 1 or 2 scalefactors instead of three if the scalefactors
202 * do not change much. The scalefactor select information,
203 * #scfsi#, is filled in accordingly.
205 ************************************************************************/
207 void transmission_pattern (unsigned int scalar[2][3][SBLIMIT],
208 unsigned int scfsi[2][SBLIMIT],
209 frame_info * frame)
211 int nch = frame->nch;
212 int sblimit = frame->sblimit;
213 int dscf[2];
214 int class[2], i, j, k;
215 static int pattern[5][5] = { {0x123, 0x122, 0x122, 0x133, 0x123},
216 {0x113, 0x111, 0x111, 0x444, 0x113},
217 {0x111, 0x111, 0x111, 0x333, 0x113},
218 {0x222, 0x222, 0x222, 0x333, 0x123},
219 {0x123, 0x122, 0x122, 0x133, 0x123}
222 for (k = 0; k < nch; k++)
223 for (i = 0; i < sblimit; i++) {
224 dscf[0] = (scalar[k][0][i] - scalar[k][1][i]);
225 dscf[1] = (scalar[k][1][i] - scalar[k][2][i]);
226 for (j = 0; j < 2; j++) {
227 if (dscf[j] <= -3)
228 class[j] = 0;
229 else if (dscf[j] > -3 && dscf[j] < 0)
230 class[j] = 1;
231 else if (dscf[j] == 0)
232 class[j] = 2;
233 else if (dscf[j] > 0 && dscf[j] < 3)
234 class[j] = 3;
235 else
236 class[j] = 4;
238 switch (pattern[class[0]][class[1]]) {
239 case 0x123:
240 scfsi[k][i] = 0;
241 break;
242 case 0x122:
243 scfsi[k][i] = 3;
244 scalar[k][2][i] = scalar[k][1][i];
245 break;
246 case 0x133:
247 scfsi[k][i] = 3;
248 scalar[k][1][i] = scalar[k][2][i];
249 break;
250 case 0x113:
251 scfsi[k][i] = 1;
252 scalar[k][1][i] = scalar[k][0][i];
253 break;
254 case 0x111:
255 scfsi[k][i] = 2;
256 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
257 break;
258 case 0x222:
259 scfsi[k][i] = 2;
260 scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
261 break;
262 case 0x333:
263 scfsi[k][i] = 2;
264 scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
265 break;
266 case 0x444:
267 scfsi[k][i] = 2;
268 if (scalar[k][0][i] > scalar[k][2][i])
269 scalar[k][0][i] = scalar[k][2][i];
270 scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
275 /************************************************************************
277 * encode_scale (Layer II)
279 * PURPOSE:The encoded scalar factor information is arranged and
280 * queued into the output fifo to be transmitted.
282 * For Layer II, the three scale factors associated with
283 * a given subband and channel are transmitted in accordance
284 * with the scfsi, which is transmitted first.
286 ************************************************************************/
288 void
289 encode_scale (unsigned int bit_alloc[2][SBLIMIT],
290 unsigned int scfsi[2][SBLIMIT],
291 unsigned int scalar[2][3][SBLIMIT], frame_info * frame,
292 Bit_stream_struc * bs)
294 int nch = frame->nch;
295 int sblimit = frame->sblimit;
296 int i, j, k;
298 for (i = 0; i < sblimit; i++)
299 for (k = 0; k < nch; k++)
300 if (bit_alloc[k][i])
301 putbits (bs, scfsi[k][i], 2);
303 for (i = 0; i < sblimit; i++)
304 for (k = 0; k < nch; k++)
305 if (bit_alloc[k][i]) /* above jsbound, bit_alloc[0][i] == ba[1][i] */
306 switch (scfsi[k][i]) {
307 case 0:
308 for (j = 0; j < 3; j++)
309 putbits (bs, scalar[k][j][i], 6);
310 break;
311 case 1:
312 case 3:
313 putbits (bs, scalar[k][0][i], 6);
314 putbits (bs, scalar[k][2][i], 6);
315 break;
316 case 2:
317 putbits (bs, scalar[k][0][i], 6);
321 /*=======================================================================\
323 | The following routines are done after the masking threshold |
324 | has been calculated by the fft analysis routines in the Psychoacoustic |
325 | model. Using the MNR calculated, the actual number of bits allocated |
326 | to each subband is found iteratively. |
328 \=======================================================================*/
330 /************************************************************************
332 * bits_for_nonoise (Layer II)
334 * PURPOSE:Returns the number of bits required to produce a
335 * mask-to-noise ratio better or equal to the noise/no_noise threshold.
337 * SEMANTICS:
338 * bbal = # bits needed for encoding bit allocation
339 * bsel = # bits needed for encoding scalefactor select information
340 * banc = # bits needed for ancillary data (header info included)
342 * For each subband and channel, will add bits until one of the
343 * following occurs:
344 * - Hit maximum number of bits we can allocate for that subband
345 * - MNR is better than or equal to the minimum masking level
346 * (NOISY_MIN_MNR)
347 * Then the bits required for scalefactors, scfsi, bit allocation,
348 * and the subband samples are tallied (#req_bits#) and returned.
350 * (NOISY_MIN_MNR) is the smallest MNR a subband can have before it is
351 * counted as 'noisy' by the logic which chooses the number of JS
352 * subbands.
354 * Joint stereo is supported.
356 ************************************************************************/
358 static double snr[18] = { 0.00, 7.00, 11.00, 16.00, 20.84,
359 25.28, 31.59, 37.75, 43.84,
360 49.89, 55.93, 61.96, 67.98, 74.01,
361 80.03, 86.05, 92.01, 98.01
364 int bits_for_nonoise (double perm_smr[2][SBLIMIT],
365 unsigned int scfsi[2][SBLIMIT], frame_info * frame)
367 int sb, ch, ba;
368 int nch = frame->nch;
369 int sblimit = frame->sblimit;
370 int jsbound = frame->jsbound;
371 al_table *alloc = frame->alloc;
372 int req_bits = 0, bbal = 0, berr = 0, banc = 32;
373 int maxAlloc, sel_bits, sc_bits, smp_bits;
374 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
376 /* added 92-08-11 shn */
377 if (frame->header->error_protection)
378 berr = 16;
379 else
380 berr = 0;
382 for (sb = 0; sb < jsbound; ++sb)
383 bbal += nch * (*alloc)[sb][0].bits;
384 for (sb = jsbound; sb < sblimit; ++sb)
385 bbal += (*alloc)[sb][0].bits;
386 req_bits = banc + bbal + berr;
388 for (sb = 0; sb < sblimit; ++sb)
389 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ++ch) {
390 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
391 sel_bits = sc_bits = smp_bits = 0;
392 for (ba = 0; ba < maxAlloc - 1; ++ba)
393 if ((-perm_smr[ch][sb] +
394 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)]) >=
395 NOISY_MIN_MNR)
396 break; /* we found enough bits */
397 if (nch == 2 && sb >= jsbound) /* check other JS channel */
398 for (; ba < maxAlloc - 1; ++ba)
399 if ((-perm_smr[1 - ch][sb] +
400 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)]) >=
401 NOISY_MIN_MNR)
402 break;
403 if (ba > 0) {
404 smp_bits =
405 SCALE_BLOCK * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
406 /* scale factor bits required for subband */
407 sel_bits = 2;
408 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
409 if (nch == 2 && sb >= jsbound) {
410 /* each new js sb has L+R scfsis */
411 sel_bits += 2;
412 sc_bits += 6 * sfsPerScfsi[scfsi[1 - ch][sb]];
414 req_bits += smp_bits + sel_bits + sc_bits;
417 return req_bits;
420 int VBR_bits_for_nonoise (double perm_smr[2][SBLIMIT],
421 unsigned int scfsi[2][SBLIMIT],
422 frame_info * frame, int vbrlevel)
424 int sb, ch, ba;
425 int nch = frame->nch;
426 int sblimit = frame->sblimit;
427 int jsbound = frame->jsbound;
428 al_table *alloc = frame->alloc;
429 int req_bits = 0, bbal = 0, berr = 0, banc = 32;
430 int maxAlloc, sel_bits, sc_bits, smp_bits;
431 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
433 /* added 92-08-11 shn */
434 if (frame->header->error_protection)
435 berr = 16;
436 else
437 berr = 0;
439 for (sb = 0; sb < jsbound; ++sb)
440 bbal += nch * (*alloc)[sb][0].bits;
441 for (sb = jsbound; sb < sblimit; ++sb)
442 bbal += (*alloc)[sb][0].bits;
443 req_bits = banc + bbal + berr;
445 for (sb = 0; sb < sblimit; ++sb)
446 for (ch = 0; ch < ((sb < jsbound) ? nch : 1); ++ch) {
447 maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
448 sel_bits = sc_bits = smp_bits = 0;
449 for (ba = 0; ba < maxAlloc - 1; ++ba)
450 /* The change between this function and the normal one is that the MIN_MNR is increased by the vbrlevel */
451 if ((-perm_smr[ch][sb] +
452 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)]) >=
453 NOISY_MIN_MNR + vbrlevel)
454 break; /* we found enough bits */
455 if (nch == 2 && sb >= jsbound) /* check other JS channel */
456 for (; ba < maxAlloc - 1; ++ba)
457 if ((-perm_smr[1 - ch][sb] +
458 snr[(*alloc)[sb][ba].quant + ((ba > 0) ? 1 : 0)]) >=
459 NOISY_MIN_MNR + vbrlevel)
460 break;
461 if (ba > 0) {
462 smp_bits =
463 SCALE_BLOCK * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
464 /* scale factor bits required for subband */
465 sel_bits = 2;
466 sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
467 if (nch == 2 && sb >= jsbound) {
468 /* each new js sb has L+R scfsis */
469 sel_bits += 2;
470 sc_bits += 6 * sfsPerScfsi[scfsi[1 - ch][sb]];
472 req_bits += smp_bits + sel_bits + sc_bits;
475 return req_bits;
478 /************************************************************************
480 * main_bit_allocation (Layer II)
482 * PURPOSE:For joint stereo mode, determines which of the 4 joint
483 * stereo modes is needed. Then calls *_a_bit_allocation(), which
484 * allocates bits for each of the subbands until there are no more bits
485 * left, or the MNR is at the noise/no_noise threshold.
487 * SEMANTICS:
489 * For joint stereo mode, joint stereo is changed to stereo if
490 * there are enough bits to encode stereo at or better than the
491 * no-noise threshold (NOISY_MIN_MNR). Otherwise, the system
492 * iteratively allocates less bits by using joint stereo until one
493 * of the following occurs:
494 * - there are no more noisy subbands (MNR >= NOISY_MIN_MNR)
495 * - mode_ext has been reduced to 0, which means that all but the
496 * lowest 4 subbands have been converted from stereo to joint
497 * stereo, and no more subbands may be converted
499 * This function calls *_bits_for_nonoise() and *_a_bit_allocation().
501 ************************************************************************/
502 void main_bit_allocation (double perm_smr[2][SBLIMIT],
503 unsigned int scfsi[2][SBLIMIT],
504 unsigned int bit_alloc[2][SBLIMIT], int *adb,
505 frame_info * frame, options * glopts)
507 int noisy_sbs;
508 int mode, mode_ext, lay;
509 int rq_db; /* av_db = *adb; Not Used MFC Nov 99 */
511 /* these are the tables which specify the limits within which the VBR can vary
512 You can't vary outside these ranges, otherwise a new alloc table would have to
513 be loaded in the middle of encoding. This VBR hack is dodgy - the standard
514 says that LayerII decoders don't have to support a variable bitrate, but Layer3
515 decoders must do so. Hence, it is unlikely that a compliant layer2 decoder would be
516 written to dynmically change allocation tables. *BUT* a layer3 encoder might handle it
517 by default meaning we could switch tables mid-encode and enjoy a wider range of bitrates
518 for the VBR encoding.
519 None of this needs to be done for LSF, since there is only *one* possible alloc table in LSF
520 MFC Feb 2003 */
521 int vbrlimits[2][3][2] = {
522 /* MONO */
523 { /* 44 */ {6, 10},
524 /* 48 */ {3, 10},
525 /* 32 */ {6, 10}},
526 /* STEREO */
527 { /* 44 */ {10, 14},
528 /* 48 */ {7, 14},
529 /* 32 */ {10, 14}}
532 static int init = 0;
533 static int lower = 10, upper = 10;
534 static int bitrateindextobits[15] =
535 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
536 int guessindex = 0;
538 if (init == 0) {
539 int nch = 1;
540 int sfreq;
541 frame_header *header = frame->header;
542 init++;
543 if (header->version == 0) {
544 /* LSF: so can use any bitrate index from 1->15 */
545 lower = 1;
546 upper = 14;
547 } else {
548 if (frame->actual_mode == MPG_MD_MONO)
549 nch = 0;
550 sfreq = header->sampling_frequency;
551 lower = vbrlimits[nch][sfreq][0];
552 upper = vbrlimits[nch][sfreq][1];
554 if (glopts->verbosity > 2)
555 fprintf (stdout, "VBR bitrate index limits [%i -> %i]\n", lower, upper);
558 /* set up a conversion table for bitrateindex->bits for this version/sampl freq
559 This will be used to find the best bitrate to cope with the number of bits that
560 are needed (as determined by VBR_bits_for_nonoise) */
561 int brindex;
562 frame_header *header = frame->header;
563 for (brindex = lower; brindex <= upper; brindex++) {
564 bitrateindextobits[brindex] =
565 (int) (1152.0 / s_freq[header->version][header->sampling_frequency]) *
566 ((double) bitrate[header->version][brindex]);
572 if ((mode = frame->actual_mode) == MPG_MD_JOINT_STEREO) {
573 frame->header->mode = MPG_MD_STEREO;
574 frame->header->mode_ext = 0;
575 frame->jsbound = frame->sblimit;
576 if ((rq_db = bits_for_nonoise (perm_smr, scfsi, frame)) > *adb) {
577 frame->header->mode = MPG_MD_JOINT_STEREO;
578 mode_ext = 4; /* 3 is least severe reduction */
579 lay = frame->header->lay;
580 do {
581 --mode_ext;
582 frame->jsbound = js_bound (mode_ext);
583 rq_db = bits_for_nonoise (perm_smr, scfsi, frame);
585 while ((rq_db > *adb) && (mode_ext > 0));
586 frame->header->mode_ext = mode_ext;
587 } /* well we either eliminated noisy sbs or mode_ext == 0 */
590 /* decide on which bit allocation method to use */
591 if (glopts->vbr == FALSE) {
592 /* Just do the old bit allocation method */
593 noisy_sbs = a_bit_allocation (perm_smr, scfsi, bit_alloc, adb, frame);
594 } else {
595 /* do the VBR bit allocation method */
596 frame->header->bitrate_index = lower;
597 *adb = available_bits (frame->header, glopts);
599 int brindex;
600 int found = FALSE;
602 /* Work out how many bits are needed for there to be no noise (ie all MNR > 0.0 + VBRLEVEL) */
603 int req =
604 VBR_bits_for_nonoise (perm_smr, scfsi, frame, glopts->vbrlevel);
606 /* Look up this value in the bitrateindextobits table to find what bitrate we should use for
607 this frame */
608 for (brindex = lower; brindex <= upper; brindex++) {
609 if (bitrateindextobits[brindex] > req) {
610 /* this method always *overestimates* the bits that are needed
611 i.e. it will usually guess right but
612 when it's wrong it'll guess a higher bitrate than actually required.
613 e.g. on "messages from earth" track 6, the guess was
614 wrong on 75/36341 frames. each time it guessed higher.
615 MFC Feb 2003 */
616 guessindex = brindex;
617 found = TRUE;
618 break;
621 /* Just for sanity */
622 if (found == FALSE)
623 guessindex = upper;
626 frame->header->bitrate_index = guessindex;
627 *adb = available_bits (frame->header, glopts);
629 /* update the statistics */
630 vbrstats[frame->header->bitrate_index]++;
632 if (glopts->verbosity > 2) {
633 /* print out the VBR stats every 1000th frame */
634 static int count = 0;
635 int i;
636 if ((count++ % 1000) == 0) {
637 for (i = 1; i < 15; i++)
638 fprintf (stdout, "%4i ", vbrstats[i]);
639 fprintf (stdout, "\n");
642 /* Print out *every* frames bitrateindex, bits required, and bits available at this bitrate */
643 if (glopts->verbosity > 5)
644 fprintf (stdout,
645 "> bitrate index %2i has %i bits available to encode the %i bits\n",
646 frame->header->bitrate_index, *adb,
647 VBR_bits_for_nonoise (perm_smr, scfsi, frame,
648 glopts->vbrlevel));
652 noisy_sbs =
653 VBR_bit_allocation (perm_smr, scfsi, bit_alloc, adb, frame, glopts);
657 void VBR_maxmnr (double mnr[2][SBLIMIT], char used[2][SBLIMIT], int sblimit,
658 int nch, int *min_sb, int *min_ch, options * glopts)
660 int i, k;
661 double small;
663 small = 999999.0;
664 *min_sb = -1;
665 *min_ch = -1;
666 for (k = 0; k < nch; ++k)
667 for (i = 0; i < sblimit; i++)
668 if (used[k][i] != 2 && small > mnr[k][i]) {
669 small = mnr[k][i];
670 *min_sb = i;
671 *min_ch = k;
674 /********************
675 MFC Feb 2003
676 VBR_bit_allocation is different to the normal a_bit_allocation in that
677 it is known beforehand that there are definitely enough bits to do what we
678 have to - i.e. a bitrate was specificially chosen in main_bit_allocation so
679 that we have enough bits to encode what we have to.
680 This function should take that into account and just greedily assign
681 the bits, rather than fussing over the minimum MNR subband - we know
682 each subband gets its required bits, why quibble?
683 This function doesn't chew much CPU, so I haven't made any attempt
684 to do this yet.
685 *********************/
687 VBR_bit_allocation (double perm_smr[2][SBLIMIT],
688 unsigned int scfsi[2][SBLIMIT],
689 unsigned int bit_alloc[2][SBLIMIT], int *adb,
690 frame_info * frame, options * glopts)
692 int i, min_ch, min_sb, oth_ch, k, increment, scale, seli, ba;
693 int bspl, bscf, bsel, ad, bbal = 0;
694 double mnr[2][SBLIMIT];
695 char used[2][SBLIMIT];
696 int nch = frame->nch;
697 int sblimit = frame->sblimit;
698 int jsbound = frame->jsbound;
699 al_table *alloc = frame->alloc;
700 static char init = 0;
701 static int banc = 32, berr = 0;
702 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
704 if (!init) {
705 init = 1;
706 if (frame->header->error_protection)
707 berr = 16; /* added 92-08-11 shn */
710 for (i = 0; i < jsbound; ++i)
711 bbal += nch * (*alloc)[i][0].bits;
712 for (i = jsbound; i < sblimit; ++i)
713 bbal += (*alloc)[i][0].bits;
714 *adb -= bbal + berr + banc;
715 ad = *adb;
717 for (i = 0; i < sblimit; i++)
718 for (k = 0; k < nch; k++) {
719 mnr[k][i] = snr[0] - perm_smr[k][i];
720 bit_alloc[k][i] = 0;
721 used[k][i] = 0;
723 bspl = bscf = bsel = 0;
725 do {
726 /* locate the subband with minimum SMR */
727 VBR_maxmnr (mnr, used, sblimit, nch, &min_sb, &min_ch, glopts);
729 if (min_sb > -1) { /* there was something to find */
730 /* find increase in bit allocation in subband [min] */
731 increment =
732 SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].group *
733 (*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].bits);
734 if (used[min_ch][min_sb])
735 increment -=
736 SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group *
737 (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
739 /* scale factor bits required for subband [min] */
740 oth_ch = 1 - min_ch; /* above js bound, need both chans */
741 if (used[min_ch][min_sb])
742 scale = seli = 0;
743 else { /* this channel had no bits or scfs before */
744 seli = 2;
745 scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
746 if (nch == 2 && min_sb >= jsbound) {
747 /* each new js sb has L+R scfsis */
748 seli += 2;
749 scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
753 /* check to see enough bits were available for */
754 /* increasing resolution in the minimum band */
755 if (ad >= bspl + bscf + bsel + seli + scale + increment) {
756 ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
757 bspl += increment; /* bits for subband sample */
758 bscf += scale; /* bits for scale factor */
759 bsel += seli; /* bits for scfsi code */
760 used[min_ch][min_sb] = 1; /* subband has bits */
761 mnr[min_ch][min_sb] =
762 -perm_smr[min_ch][min_sb] + snr[(*alloc)[min_sb][ba].quant + 1];
763 /* Check if subband has been fully allocated max bits */
764 if (ba >= (1 << (*alloc)[min_sb][0].bits) - 1)
765 used[min_ch][min_sb] = 2; /* don't let this sb get any more bits */
766 } else
767 used[min_ch][min_sb] = 2; /* can't increase this alloc */
769 if (min_sb >= jsbound && nch == 2) {
770 /* above jsbound, alloc applies L+R */
771 ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
772 used[oth_ch][min_sb] = used[min_ch][min_sb];
773 mnr[oth_ch][min_sb] =
774 -perm_smr[oth_ch][min_sb] + snr[(*alloc)[min_sb][ba].quant + 1];
779 while (min_sb > -1); /* until could find no channel */
781 /* Calculate the number of bits left */
782 ad -= bspl + bscf + bsel;
783 *adb = ad;
784 for (k = 0; k < nch; k++)
785 for (i = sblimit; i < SBLIMIT; i++)
786 bit_alloc[k][i] = 0;
788 return 0;
791 /************************************************************************
793 * a_bit_allocation (Layer II)
795 * PURPOSE:Adds bits to the subbands with the lowest mask-to-noise
796 * ratios, until the maximum number of bits for the subband has
797 * been allocated.
799 * SEMANTICS:
800 * 1. Find the subband and channel with the smallest MNR (#min_sb#,
801 * and #min_ch#)
802 * 2. Calculate the increase in bits needed if we increase the bit
803 * allocation to the next higher level
804 * 3. If there are enough bits available for increasing the resolution
805 * in #min_sb#, #min_ch#, and the subband has not yet reached its
806 * maximum allocation, update the bit allocation, MNR, and bits
807 available accordingly
808 * 4. Repeat until there are no more bits left, or no more available
809 * subbands. (A subband is still available until the maximum
810 * number of bits for the subband has been allocated, or there
811 * aren't enough bits to go to the next higher resolution in the
812 subband.)
814 ************************************************************************/
816 void maxmnr (double mnr[2][SBLIMIT], char used[2][SBLIMIT], int sblimit,
817 int nch, int *min_sb, int *min_ch)
819 int i, k;
820 double small;
822 small = 999999.0;
823 *min_sb = -1;
824 *min_ch = -1;
825 for (k = 0; k < nch; ++k)
826 for (i = 0; i < sblimit; i++)
827 if (used[k][i] != 2 && small > mnr[k][i]) {
828 small = mnr[k][i];
829 *min_sb = i;
830 *min_ch = k;
834 int a_bit_allocation (double perm_smr[2][SBLIMIT],
835 unsigned int scfsi[2][SBLIMIT],
836 unsigned int bit_alloc[2][SBLIMIT], int *adb,
837 frame_info * frame)
839 int i, min_ch, min_sb, oth_ch, k, increment, scale, seli, ba;
840 int bspl, bscf, bsel, ad, bbal = 0;
841 double mnr[2][SBLIMIT];
842 char used[2][SBLIMIT];
843 int nch = frame->nch;
844 int sblimit = frame->sblimit;
845 int jsbound = frame->jsbound;
846 al_table *alloc = frame->alloc;
847 static char init = 0;
848 static int banc = 32, berr = 0;
849 static int sfsPerScfsi[] = { 3, 2, 1, 2 }; /* lookup # sfs per scfsi */
851 #define CHECKITERx
852 #ifdef CHECKITER
853 int count=0;
854 #endif
856 if (!init) {
857 init = 1;
858 if (frame->header->error_protection)
859 berr = 16; /* added 92-08-11 shn */
862 for (i = 0; i < jsbound; ++i)
863 bbal += nch * (*alloc)[i][0].bits;
864 for (i = jsbound; i < sblimit; ++i)
865 bbal += (*alloc)[i][0].bits;
866 *adb -= bbal + berr + banc;
867 ad = *adb;
869 for (i = 0; i < sblimit; i++)
870 for (k = 0; k < nch; k++) {
871 mnr[k][i] = snr[0] - perm_smr[k][i];
872 bit_alloc[k][i] = 0;
873 used[k][i] = 0;
875 bspl = bscf = bsel = 0;
877 do {
878 #ifdef CHECKITER
879 count++;
880 #endif
881 /* locate the subband with minimum SMR */
882 maxmnr (mnr, used, sblimit, nch, &min_sb, &min_ch);
884 if (min_sb > -1) { /* there was something to find */
885 /* find increase in bit allocation in subband [min] */
886 increment =
887 SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].group *
888 (*alloc)[min_sb][bit_alloc[min_ch][min_sb] + 1].bits);
889 if (used[min_ch][min_sb])
890 increment -=
891 SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group *
892 (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
894 /* scale factor bits required for subband [min] */
895 oth_ch = 1 - min_ch; /* above js bound, need both chans */
896 if (used[min_ch][min_sb])
897 scale = seli = 0;
898 else { /* this channel had no bits or scfs before */
899 seli = 2;
900 scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
901 if (nch == 2 && min_sb >= jsbound) {
902 /* each new js sb has L+R scfsis */
903 seli += 2;
904 scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
908 /* check to see enough bits were available for */
909 /* increasing resolution in the minimum band */
910 if (ad >= bspl + bscf + bsel + seli + scale + increment) {
911 ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
912 bspl += increment; /* bits for subband sample */
913 bscf += scale; /* bits for scale factor */
914 bsel += seli; /* bits for scfsi code */
915 used[min_ch][min_sb] = 1; /* subband has bits */
916 mnr[min_ch][min_sb] =
917 -perm_smr[min_ch][min_sb] + snr[(*alloc)[min_sb][ba].quant + 1];
918 /* Check if subband has been fully allocated max bits */
919 if (ba >= (1 << (*alloc)[min_sb][0].bits) - 1)
920 used[min_ch][min_sb] = 2; /* don't let this sb get any more bits */
921 } else
922 used[min_ch][min_sb] = 2; /* can't increase this alloc */
924 if (min_sb >= jsbound && nch == 2) {
925 /* above jsbound, alloc applies L+R */
926 ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
927 used[oth_ch][min_sb] = used[min_ch][min_sb];
928 mnr[oth_ch][min_sb] =
929 -perm_smr[oth_ch][min_sb] + snr[(*alloc)[min_sb][ba].quant + 1];
934 while (min_sb > -1); /* until could find no channel */
936 /* Calculate the number of bits left */
937 ad -= bspl + bscf + bsel;
938 *adb = ad;
939 for (k = 0; k < nch; k++)
940 for (i = sblimit; i < SBLIMIT; i++)
941 bit_alloc[k][i] = 0;
943 #ifdef USELESSCODE
944 /* this function is declared to return an INT, which is meant to be a count
945 of the subbands which are still noisy. But, the return value is ignored,
946 so why bother? Is the count of noisy_sbs useful as any sort of
947 quality measure? Leave this in, until I'm sure that noisy_sbs couldn't
948 be used for something
949 MFC Feb 2003 */
951 noisy_sbs = 0; /* calc worst noise in case */
952 for (k = 0; k < nch; ++k) {
953 for (i = 0; i < sblimit; i++) {
954 if (mnr[k][i] < NOISY_MIN_MNR)
955 ++noisy_sbs; /* noise is not masked */
958 return noisy_sbs;
959 #endif
960 #ifdef CHECKITER
961 fprintf(stdout,"a bit alloc %i\n", count);
962 #endif
963 return 0;
966 /************************************************************************
968 * subband_quantization (Layer II)
970 * PURPOSE:Quantizes subband samples to appropriate number of bits
972 * SEMANTICS: Subband samples are divided by their scalefactors, which
973 makes the quantization more efficient. The scaled samples are
974 * quantized by the function a*x+b, where a and b are functions of
975 * the number of quantization levels. The result is then truncated
976 * to the appropriate number of bits and the MSB is inverted.
978 * Note that for fractional 2's complement, inverting the MSB for a
979 negative number x is equivalent to adding 1 to it.
981 ************************************************************************/
982 #define PDS3
983 #ifdef PDS3
984 static double a[17] = {
985 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
986 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
987 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
988 0.999969482, 0.999984741
991 static double b[17] = {
992 -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
993 -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
994 -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
995 -0.000030518, -0.000015259
998 static unsigned int pds_quant_bits[17] = {
999 /* for a number of quantization steps; */
1000 /* 3, 5, 7, 9, 15,
1001 31, 63, 127, 255, 511,
1002 1023, 2047, 4095, 8191, 16383,
1003 32767, 65535
1005 /* below we need : */
1006 2, 4, 4, 8, 8,
1007 16, 32, 64, 128, 256,
1008 512, 1024, 2048, 4096, 8192,
1009 16384, 32768
1011 /* to retain succesfull quant */
1012 /* This is only a quick and dirty tric to speed up ISO code */
1013 /* In below quant routine : also rewrote loops to decrement */
1014 /* Added/changed by Patrick De Smet, Nov. 1999 */
1016 /* PDS TODO: maybe it is faster not to store pds_quant_bits */
1017 /* but rather store (char) n, and use (1L shift left n) ; */
1018 /* is a shift faster than loading unsigned int from array ? */
1020 void
1021 subband_quantization (unsigned int scalar[2][3][SBLIMIT],
1022 double sb_samples[2][3][SCALE_BLOCK][SBLIMIT],
1023 unsigned int j_scale[3][SBLIMIT],
1024 double j_samps[3][SCALE_BLOCK][SBLIMIT],
1025 unsigned int bit_alloc[2][SBLIMIT],
1026 unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
1027 frame_info * frame)
1029 int i, j, k, s, qnt, sig;
1030 int nch = frame->nch;
1031 int sblimit = frame->sblimit;
1032 int jsbound = frame->jsbound;
1033 double d;
1034 al_table *alloc = frame->alloc;
1036 for (s = 3; s--;)
1037 for (j = SCALE_BLOCK; j--;)
1038 for (i = sblimit; i--;)
1039 for (k = ((i < jsbound) ? nch : 1); k--;)
1040 if (bit_alloc[k][i]) {
1041 /* scale and quantize FLOATing point sample */
1042 if (nch == 2 && i >= jsbound) /* use j-stereo samples */
1043 d = j_samps[s][j][i] / multiple[j_scale[s][i]];
1044 else
1045 d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
1046 if (fabs(d) > 1.0)
1047 fprintf (stderr, "Not scaled properly %d %d %d %d\n", k, s, j,
1049 qnt = (*alloc)[i][bit_alloc[k][i]].quant;
1050 d = d * a[qnt] + b[qnt];
1051 /* extract MSB N-1 bits from the FLOATing point sample */
1052 if (d >= 0)
1053 sig = 1;
1054 else {
1055 sig = 0;
1056 d += 1.0;
1058 sbband[k][s][j][i] =
1059 (unsigned int) (d * (double) (pds_quant_bits[qnt]));
1060 /* tag the inverted sign bit to sbband at position N */
1061 /* The bit inversion is a must for grouping with 3,5,9 steps
1062 so it is done for all subbands */
1063 if (sig)
1064 sbband[k][s][j][i] |= (pds_quant_bits[qnt]);
1066 for (s = 3; s--;)
1067 for (j = sblimit; j < SBLIMIT; j++)
1068 for (i = SCALE_BLOCK; i--;)
1069 for (k = nch; k--;)
1070 sbband[k][s][i][j] = 0;
1072 #else
1074 static double a[17] = {
1075 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
1076 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
1077 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
1078 0.999969482, 0.999984741
1081 static double b[17] = {
1082 -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
1083 -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
1084 -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
1085 -0.000030518, -0.000015259
1088 void
1089 subband_quantization (unsigned int scalar[2][3][SBLIMIT],
1090 double sb_samples[2][3][SCALE_BLOCK][SBLIMIT],
1091 unsigned int j_scale[3][SBLIMIT],
1092 double j_samps[3][SCALE_BLOCK][SBLIMIT],
1093 unsigned int bit_alloc[2][SBLIMIT],
1094 unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
1095 frame_info * frame)
1097 int i, j, k, s, n, qnt, sig;
1098 int nch = frame->nch;
1099 int sblimit = frame->sblimit;
1100 int jsbound = frame->jsbound;
1101 unsigned int stps;
1102 double d;
1103 al_table *alloc = frame->alloc;
1105 for (s = 0; s < 3; s++)
1106 for (j = 0; j < SCALE_BLOCK; j++)
1107 for (i = 0; i < sblimit; i++)
1108 for (k = 0; k < ((i < jsbound) ? nch : 1); k++)
1109 if (bit_alloc[k][i]) {
1110 /* scale and quantize FLOATing point sample */
1111 if (nch == 2 && i >= jsbound) /* use j-stereo samples */
1112 d = j_samps[s][j][i] / multiple[j_scale[s][i]];
1113 else
1114 d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
1115 if (mod (d) > 1.0)
1116 fprintf (stderr, "Not scaled properly %d %d %d %d\n", k, s, j,
1118 qnt = (*alloc)[i][bit_alloc[k][i]].quant;
1119 d = d * a[qnt] + b[qnt];
1120 /* extract MSB N-1 bits from the FLOATing point sample */
1121 if (d >= 0)
1122 sig = 1;
1123 else {
1124 sig = 0;
1125 d += 1.0;
1127 n = 0;
1128 stps = (*alloc)[i][bit_alloc[k][i]].steps;
1129 while ((1L << n) < stps)
1130 n++;
1131 n--;
1132 sbband[k][s][j][i] = (unsigned int) (d * (double) (1L << n));
1133 /* tag the inverted sign bit to sbband at position N */
1134 /* The bit inversion is a must for grouping with 3,5,9 steps
1135 so it is done for all subbands */
1136 if (sig)
1137 sbband[k][s][j][i] |= 1 << n;
1140 for (k = 0; k < nch; k++)
1141 for (s = 0; s < 3; s++)
1142 for (i = 0; i < SCALE_BLOCK; i++)
1143 for (j = sblimit; j < SBLIMIT; j++)
1144 sbband[k][s][i][j] = 0;
1146 #endif
1148 /*************************************************************************
1149 * encode_bit_alloc (Layer II)
1151 * PURPOSE:Writes bit allocation information onto bitstream
1153 * Layer II uses 4,3,2, or 0 bits depending on the
1154 * quantization table used.
1156 ************************************************************************/
1158 void encode_bit_alloc (unsigned int bit_alloc[2][SBLIMIT],
1159 frame_info * frame, Bit_stream_struc * bs)
1161 int i, k;
1162 int nch = frame->nch;
1163 int sblimit = frame->sblimit;
1164 int jsbound = frame->jsbound;
1165 al_table *alloc = frame->alloc;
1167 for (i = 0; i < sblimit; i++)
1168 for (k = 0; k < ((i < jsbound) ? nch : 1); k++)
1169 putbits (bs, bit_alloc[k][i], (*alloc)[i][0].bits);
1172 /************************************************************************
1174 * sample_encoding (Layer II)
1176 * PURPOSE:Put one frame of subband samples on to the bitstream
1178 * SEMANTICS: The number of bits allocated per sample is read from
1179 * the bit allocation information #bit_alloc#. Layer 2
1180 * supports writing grouped samples for quantization steps
1181 * that are not a power of 2.
1183 ************************************************************************/
1185 void sample_encoding (unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
1186 unsigned int bit_alloc[2][SBLIMIT],
1187 frame_info * frame, Bit_stream_struc * bs)
1189 unsigned int temp;
1190 unsigned int i, j, k, s, x, y;
1191 int nch = frame->nch;
1192 int sblimit = frame->sblimit;
1193 int jsbound = frame->jsbound;
1194 al_table *alloc = frame->alloc;
1196 for (s = 0; s < 3; s++)
1197 for (j = 0; j < SCALE_BLOCK; j += 3)
1198 for (i = 0; i < sblimit; i++)
1199 for (k = 0; k < ((i < jsbound) ? nch : 1); k++)
1200 if (bit_alloc[k][i]) {
1201 if ((*alloc)[i][bit_alloc[k][i]].group == 3) {
1202 for (x = 0; x < 3; x++)
1203 putbits (bs, sbband[k][s][j + x][i],
1204 (*alloc)[i][bit_alloc[k][i]].bits);
1205 } else {
1206 y = (*alloc)[i][bit_alloc[k][i]].steps;
1207 temp =
1208 sbband[k][s][j][i] + sbband[k][s][j + 1][i] * y +
1209 sbband[k][s][j + 2][i] * y * y;
1210 putbits (bs, temp, (*alloc)[i][bit_alloc[k][i]].bits);
1215 /************************************************************************
1217 * encode_CRC
1219 ************************************************************************/
1221 void encode_CRC (unsigned int crc, Bit_stream_struc * bs)
1223 putbits (bs, crc, 16);