00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00030 #include "lame.h"
00031 #include "machine.h"
00032 #include "encoder.h"
00033 #include "util.h"
00034 #include "quantize_pvt.h"
00035 #include "tables.h"
00036
00037
00038 static const struct {
00039 const int region0_count;
00040 const int region1_count;
00041 } subdv_table[23] = {
00042 {
00043 0, 0},
00044 {
00045 0, 0},
00046 {
00047 0, 0},
00048 {
00049 0, 0},
00050 {
00051 0, 0},
00052 {
00053 0, 1},
00054 {
00055 1, 1},
00056 {
00057 1, 1},
00058 {
00059 1, 2},
00060 {
00061 2, 2},
00062 {
00063 2, 3},
00064 {
00065 2, 3},
00066 {
00067 3, 4},
00068 {
00069 3, 4},
00070 {
00071 3, 4},
00072 {
00073 4, 5},
00074 {
00075 4, 5},
00076 {
00077 4, 6},
00078 {
00079 5, 6},
00080 {
00081 5, 6},
00082 {
00083 5, 7},
00084 {
00085 6, 7},
00086 {
00087 6, 7},
00088 };
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 void
00114 quantize_lines_xrpow_01(int l, FLOAT istep, const FLOAT * xr, int *ix)
00115 {
00116 const FLOAT compareval0 = (1.0 - 0.4054) / istep;
00117
00118 assert(l > 0);
00119 l = l >> 1;
00120 while (l--) {
00121 *(ix++) = (compareval0 > *xr++) ? 0 : 1;
00122 *(ix++) = (compareval0 > *xr++) ? 0 : 1;
00123 }
00124 }
00125
00126
00127
00128 #ifdef TAKEHIRO_IEEE754_HACK
00129
00130 typedef union {
00131 float f;
00132 int i;
00133 } fi_union;
00134
00135 #define MAGIC_FLOAT (65536*(128))
00136 #define MAGIC_INT 0x4b000000
00137
00138
00139 void
00140 quantize_lines_xrpow(int l, FLOAT istep, const FLOAT * xp, int *pi)
00141 {
00142 fi_union *fi;
00143 int remaining;
00144
00145 assert(l > 0);
00146
00147 fi = (fi_union *) pi;
00148
00149 l = l >> 1;
00150 remaining = l % 2;
00151 l = l >> 1;
00152 while (l--) {
00153 double x0 = istep * xp[0];
00154 double x1 = istep * xp[1];
00155 double x2 = istep * xp[2];
00156 double x3 = istep * xp[3];
00157
00158 x0 += MAGIC_FLOAT;
00159 fi[0].f = x0;
00160 x1 += MAGIC_FLOAT;
00161 fi[1].f = x1;
00162 x2 += MAGIC_FLOAT;
00163 fi[2].f = x2;
00164 x3 += MAGIC_FLOAT;
00165 fi[3].f = x3;
00166
00167 fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
00168 fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
00169 fi[2].f = x2 + (adj43asm - MAGIC_INT)[fi[2].i];
00170 fi[3].f = x3 + (adj43asm - MAGIC_INT)[fi[3].i];
00171
00172 fi[0].i -= MAGIC_INT;
00173 fi[1].i -= MAGIC_INT;
00174 fi[2].i -= MAGIC_INT;
00175 fi[3].i -= MAGIC_INT;
00176 fi += 4;
00177 xp += 4;
00178 };
00179 if (remaining) {
00180 double x0 = istep * xp[0];
00181 double x1 = istep * xp[1];
00182
00183 x0 += MAGIC_FLOAT;
00184 fi[0].f = x0;
00185 x1 += MAGIC_FLOAT;
00186 fi[1].f = x1;
00187
00188 fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
00189 fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
00190
00191 fi[0].i -= MAGIC_INT;
00192 fi[1].i -= MAGIC_INT;
00193 }
00194
00195 }
00196
00197
00198 # define ROUNDFAC -0.0946
00199 void
00200 quantize_lines_xrpow_ISO(int l, FLOAT istep, const FLOAT * xp, int *pi)
00201 {
00202 fi_union *fi;
00203 int remaining;
00204
00205 assert(l > 0);
00206
00207 fi = (fi_union *) pi;
00208
00209 l = l >> 1;
00210 remaining = l % 2;
00211 l = l >> 1;
00212 while (l--) {
00213 fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
00214 fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
00215 fi[2].f = istep * xp[2] + (ROUNDFAC + MAGIC_FLOAT);
00216 fi[3].f = istep * xp[3] + (ROUNDFAC + MAGIC_FLOAT);
00217
00218 fi[0].i -= MAGIC_INT;
00219 fi[1].i -= MAGIC_INT;
00220 fi[2].i -= MAGIC_INT;
00221 fi[3].i -= MAGIC_INT;
00222 fi += 4;
00223 xp += 4;
00224 };
00225 if (remaining) {
00226 fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
00227 fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
00228
00229 fi[0].i -= MAGIC_INT;
00230 fi[1].i -= MAGIC_INT;
00231 }
00232
00233 }
00234
00235
00236
00237
00238
00239
00240 #else
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
00255 #define QUANTFAC(rx) adj43[rx]
00256 #define ROUNDFAC 0.4054
00257
00258
00259 void
00260 quantize_lines_xrpow(int l, FLOAT istep, const FLOAT * xr, int *ix)
00261 {
00262 int remaining;
00263
00264 assert(l > 0);
00265
00266 l = l >> 1;
00267 remaining = l % 2;
00268 l = l >> 1;
00269 while (l--) {
00270 FLOAT x0, x1, x2, x3;
00271 int rx0, rx1, rx2, rx3;
00272
00273 x0 = *xr++ * istep;
00274 x1 = *xr++ * istep;
00275 XRPOW_FTOI(x0, rx0);
00276 x2 = *xr++ * istep;
00277 XRPOW_FTOI(x1, rx1);
00278 x3 = *xr++ * istep;
00279 XRPOW_FTOI(x2, rx2);
00280 x0 += QUANTFAC(rx0);
00281 XRPOW_FTOI(x3, rx3);
00282 x1 += QUANTFAC(rx1);
00283 XRPOW_FTOI(x0, *ix++);
00284 x2 += QUANTFAC(rx2);
00285 XRPOW_FTOI(x1, *ix++);
00286 x3 += QUANTFAC(rx3);
00287 XRPOW_FTOI(x2, *ix++);
00288 XRPOW_FTOI(x3, *ix++);
00289 };
00290 if (remaining) {
00291 FLOAT x0, x1;
00292 int rx0, rx1;
00293
00294 x0 = *xr++ * istep;
00295 x1 = *xr++ * istep;
00296 XRPOW_FTOI(x0, rx0);
00297 XRPOW_FTOI(x1, rx1);
00298 x0 += QUANTFAC(rx0);
00299 x1 += QUANTFAC(rx1);
00300 XRPOW_FTOI(x0, *ix++);
00301 XRPOW_FTOI(x1, *ix++);
00302 }
00303
00304 }
00305
00306
00307
00308 void
00309 quantize_lines_xrpow_ISO(int l, FLOAT istep, const FLOAT * xr, int *ix)
00310 {
00311
00312 const FLOAT compareval0 = (1.0 - 0.4054) / istep;
00313 const FLOAT compareval1 = (2.0 - 0.4054) / istep;
00314
00315 assert(l > 0);
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 while (l--) {
00334 if (compareval0 > *xr) {
00335 *(ix++) = 0;
00336 xr++;
00337 }
00338 else if (compareval1 > *xr) {
00339 *(ix++) = 1;
00340 xr++;
00341 }
00342 else {
00343
00344 XRPOW_FTOI(istep * (*(xr++)) + ROUNDFAC, *(ix++));
00345 }
00346 }
00347
00348 }
00349
00350
00351
00352
00353 #endif
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 static void
00364 quantize_xrpow(const FLOAT * xp, int *pi, FLOAT istep, gr_info const *const cod_info,
00365 calc_noise_data const *prev_noise, lame_internal_flags const *const gfc)
00366 {
00367
00368 int sfb;
00369 int sfbmax;
00370 int j = 0;
00371 int prev_data_use;
00372 int *iData;
00373 int accumulate = 0;
00374 int accumulate01 = 0;
00375 int *acc_iData;
00376 const FLOAT *acc_xp;
00377
00378 iData = pi;
00379 acc_xp = xp;
00380 acc_iData = iData;
00381
00382
00383
00384
00385
00386
00387 prev_data_use = (prev_noise && (cod_info->global_gain == prev_noise->global_gain));
00388
00389 if (cod_info->block_type == SHORT_TYPE)
00390 sfbmax = 38;
00391 else
00392 sfbmax = 21;
00393
00394 for (sfb = 0; sfb <= sfbmax; sfb++) {
00395 int step = -1;
00396
00397 if (prev_data_use || cod_info->block_type == NORM_TYPE) {
00398 step =
00399 cod_info->global_gain
00400 - ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
00401 << (cod_info->scalefac_scale + 1))
00402 - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
00403 }
00404 assert(cod_info->width[sfb] >= 0);
00405 if (prev_data_use && (prev_noise->step[sfb] == step)) {
00406
00407
00408 if (accumulate) {
00409 gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
00410 accumulate = 0;
00411 }
00412 if (accumulate01) {
00413 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
00414 accumulate01 = 0;
00415 }
00416 }
00417 else {
00418 int l;
00419 l = cod_info->width[sfb];
00420
00421 if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
00422
00423 int usefullsize;
00424 usefullsize = cod_info->max_nonzero_coeff - j + 1;
00425 memset(&pi[cod_info->max_nonzero_coeff], 0,
00426 sizeof(int) * (576 - cod_info->max_nonzero_coeff));
00427 l = usefullsize;
00428
00429 if (l < 0) {
00430 l = 0;
00431 }
00432
00433
00434 sfb = sfbmax + 1;
00435 }
00436
00437
00438 if (!accumulate && !accumulate01) {
00439 acc_iData = iData;
00440 acc_xp = xp;
00441 }
00442 if (prev_noise &&
00443 prev_noise->sfb_count1 > 0 &&
00444 sfb >= prev_noise->sfb_count1 &&
00445 prev_noise->step[sfb] > 0 && step >= prev_noise->step[sfb]) {
00446
00447 if (accumulate) {
00448 gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
00449 accumulate = 0;
00450 acc_iData = iData;
00451 acc_xp = xp;
00452 }
00453 accumulate01 += l;
00454 }
00455 else {
00456 if (accumulate01) {
00457 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
00458 accumulate01 = 0;
00459 acc_iData = iData;
00460 acc_xp = xp;
00461 }
00462 accumulate += l;
00463 }
00464
00465 if (l <= 0) {
00466
00467
00468
00469 if (accumulate01) {
00470 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
00471 accumulate01 = 0;
00472 }
00473 if (accumulate) {
00474 gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
00475 accumulate = 0;
00476 }
00477
00478 break;
00479 }
00480 }
00481 if (sfb <= sfbmax) {
00482 iData += cod_info->width[sfb];
00483 xp += cod_info->width[sfb];
00484 j += cod_info->width[sfb];
00485 }
00486 }
00487 if (accumulate) {
00488 gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
00489 accumulate = 0;
00490 }
00491 if (accumulate01) {
00492 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
00493 accumulate01 = 0;
00494 }
00495
00496 }
00497
00498
00499
00500
00501 void
00502 quantize_init(lame_internal_flags * const gfc)
00503 {
00504 if (gfc->quantization)
00505 gfc->quantize_lines_xrpow = quantize_lines_xrpow;
00506 else
00507 gfc->quantize_lines_xrpow = quantize_lines_xrpow_ISO;
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 int
00519 ix_max(const int *ix, const int *end)
00520 {
00521 int max1 = 0, max2 = 0;
00522
00523 do {
00524 int const x1 = *ix++;
00525 int const x2 = *ix++;
00526 if (max1 < x1)
00527 max1 = x1;
00528
00529 if (max2 < x2)
00530 max2 = x2;
00531 } while (ix < end);
00532 if (max1 < max2)
00533 max1 = max2;
00534 return max1;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544 int
00545 count_bit_ESC(const int *ix, const int *const end, int t1, const int t2, int *const s)
00546 {
00547
00548 int const linbits = ht[t1].xlen * 65536 + ht[t2].xlen;
00549 int sum = 0, sum2;
00550
00551 do {
00552 int x = *ix++;
00553 int y = *ix++;
00554
00555 if (x != 0) {
00556 if (x > 14) {
00557 x = 15;
00558 sum += linbits;
00559 }
00560 x *= 16;
00561 }
00562
00563 if (y != 0) {
00564 if (y > 14) {
00565 y = 15;
00566 sum += linbits;
00567 }
00568 x += y;
00569 }
00570
00571 sum += largetbl[x];
00572 } while (ix < end);
00573
00574 sum2 = sum & 0xffff;
00575 sum >>= 16;
00576
00577 if (sum > sum2) {
00578 sum = sum2;
00579 t1 = t2;
00580 }
00581
00582 *s += sum;
00583 return t1;
00584 }
00585
00586
00587 inline static int
00588 count_bit_noESC(const int *ix, const int *const end, int *const s)
00589 {
00590
00591 int sum1 = 0;
00592 const char *const hlen1 = ht[1].hlen;
00593
00594 do {
00595 int const x = ix[0] * 2 + ix[1];
00596 ix += 2;
00597 sum1 += hlen1[x];
00598 } while (ix < end);
00599
00600 *s += sum1;
00601 return 1;
00602 }
00603
00604
00605
00606 inline static int
00607 count_bit_noESC_from2(const int *ix, const int *const end, int t1, int *const s)
00608 {
00609
00610 unsigned int sum = 0, sum2;
00611 const int xlen = ht[t1].xlen;
00612 const unsigned int *hlen;
00613 if (t1 == 2)
00614 hlen = table23;
00615 else
00616 hlen = table56;
00617
00618 do {
00619 int const x = ix[0] * xlen + ix[1];
00620 ix += 2;
00621 sum += hlen[x];
00622 } while (ix < end);
00623
00624 sum2 = sum & 0xffff;
00625 sum >>= 16;
00626
00627 if (sum > sum2) {
00628 sum = sum2;
00629 t1++;
00630 }
00631
00632 *s += sum;
00633 return t1;
00634 }
00635
00636
00637 inline static int
00638 count_bit_noESC_from3(const int *ix, const int *const end, int t1, int *const s)
00639 {
00640
00641 int sum1 = 0;
00642 int sum2 = 0;
00643 int sum3 = 0;
00644 const int xlen = ht[t1].xlen;
00645 const char *const hlen1 = ht[t1].hlen;
00646 const char *const hlen2 = ht[t1 + 1].hlen;
00647 const char *const hlen3 = ht[t1 + 2].hlen;
00648 int t;
00649
00650 do {
00651 int const x = ix[0] * xlen + ix[1];
00652 ix += 2;
00653 sum1 += hlen1[x];
00654 sum2 += hlen2[x];
00655 sum3 += hlen3[x];
00656 } while (ix < end);
00657
00658 t = t1;
00659 if (sum1 > sum2) {
00660 sum1 = sum2;
00661 t++;
00662 }
00663 if (sum1 > sum3) {
00664 sum1 = sum3;
00665 t = t1 + 2;
00666 }
00667 *s += sum1;
00668
00669 return t;
00670 }
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 static int
00687 choose_table_nonMMX(const int *ix, const int *const end, int *const s)
00688 {
00689 int max;
00690 int choice, choice2;
00691 static const int huf_tbl_noESC[] = {
00692 1, 2, 5, 7, 7, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13
00693 };
00694
00695 max = ix_max(ix, end);
00696
00697 switch (max) {
00698 case 0:
00699 return max;
00700
00701 case 1:
00702 return count_bit_noESC(ix, end, s);
00703
00704 case 2:
00705 case 3:
00706 return count_bit_noESC_from2(ix, end, huf_tbl_noESC[max - 1], s);
00707
00708 case 4:
00709 case 5:
00710 case 6:
00711 case 7:
00712 case 8:
00713 case 9:
00714 case 10:
00715 case 11:
00716 case 12:
00717 case 13:
00718 case 14:
00719 case 15:
00720 return count_bit_noESC_from3(ix, end, huf_tbl_noESC[max - 1], s);
00721
00722 default:
00723
00724 if (max > IXMAX_VAL) {
00725 *s = LARGE_BITS;
00726 return -1;
00727 }
00728 max -= 15;
00729 for (choice2 = 24; choice2 < 32; choice2++) {
00730 if (ht[choice2].linmax >= max) {
00731 break;
00732 }
00733 }
00734
00735 for (choice = choice2 - 8; choice < 24; choice++) {
00736 if (ht[choice].linmax >= max) {
00737 break;
00738 }
00739 }
00740 return count_bit_ESC(ix, end, choice, choice2, s);
00741 }
00742 }
00743
00744
00745
00746
00747
00748
00749 int
00750 noquant_count_bits(lame_internal_flags const *const gfc,
00751 gr_info * const gi, calc_noise_data * prev_noise)
00752 {
00753 int bits = 0;
00754 int i, a1, a2;
00755 int const *const ix = gi->l3_enc;
00756
00757 i = Min(576, ((gi->max_nonzero_coeff + 2) >> 1) << 1);
00758
00759 if (prev_noise)
00760 prev_noise->sfb_count1 = 0;
00761
00762
00763 for (; i > 1; i -= 2)
00764 if (ix[i - 1] | ix[i - 2])
00765 break;
00766 gi->count1 = i;
00767
00768
00769 a1 = a2 = 0;
00770 for (; i > 3; i -= 4) {
00771 int p;
00772
00773 if ((unsigned int) (ix[i - 1] | ix[i - 2] | ix[i - 3] | ix[i - 4]) > 1)
00774 break;
00775
00776 p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
00777 a1 += t32l[p];
00778 a2 += t33l[p];
00779 }
00780
00781 bits = a1;
00782 gi->count1table_select = 0;
00783 if (a1 > a2) {
00784 bits = a2;
00785 gi->count1table_select = 1;
00786 }
00787
00788 gi->count1bits = bits;
00789 gi->big_values = i;
00790 if (i == 0)
00791 return bits;
00792
00793 if (gi->block_type == SHORT_TYPE) {
00794 a1 = 3 * gfc->scalefac_band.s[3];
00795 if (a1 > gi->big_values)
00796 a1 = gi->big_values;
00797 a2 = gi->big_values;
00798
00799 }
00800 else if (gi->block_type == NORM_TYPE) {
00801 assert(i <= 576);
00802 a1 = gi->region0_count = gfc->bv_scf[i - 2];
00803 a2 = gi->region1_count = gfc->bv_scf[i - 1];
00804
00805 assert(a1 + a2 + 2 < SBPSY_l);
00806 a2 = gfc->scalefac_band.l[a1 + a2 + 2];
00807 a1 = gfc->scalefac_band.l[a1 + 1];
00808 if (a2 < i)
00809 gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
00810
00811 }
00812 else {
00813 gi->region0_count = 7;
00814
00815 gi->region1_count = SBMAX_l - 1 - 7 - 1;
00816 a1 = gfc->scalefac_band.l[7 + 1];
00817 a2 = i;
00818 if (a1 > a2) {
00819 a1 = a2;
00820 }
00821 }
00822
00823
00824
00825
00826 a1 = Min(a1, i);
00827 a2 = Min(a2, i);
00828
00829 assert(a1 >= 0);
00830 assert(a2 >= 0);
00831
00832
00833 if (0 < a1)
00834 gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
00835 if (a1 < a2)
00836 gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
00837 if (gfc->use_best_huffman == 2) {
00838 gi->part2_3_length = bits;
00839 best_huffman_divide(gfc, gi);
00840 bits = gi->part2_3_length;
00841 }
00842
00843
00844 if (prev_noise) {
00845 if (gi->block_type == NORM_TYPE) {
00846 int sfb = 0;
00847 while (gfc->scalefac_band.l[sfb] < gi->big_values) {
00848 sfb++;
00849 }
00850 prev_noise->sfb_count1 = sfb;
00851 }
00852 }
00853
00854 return bits;
00855 }
00856
00857 int
00858 count_bits(lame_internal_flags const *const gfc,
00859 const FLOAT * const xr, gr_info * const gi, calc_noise_data * prev_noise)
00860 {
00861 int *const ix = gi->l3_enc;
00862
00863
00864 FLOAT const w = (IXMAX_VAL) / IPOW20(gi->global_gain);
00865
00866 if (gi->xrpow_max > w)
00867 return LARGE_BITS;
00868
00869 quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise, gfc);
00870
00871 if (gfc->substep_shaping & 2) {
00872 int sfb, j = 0;
00873
00874 int const gain = gi->global_gain + gi->scalefac_scale;
00875 const FLOAT roundfac = 0.634521682242439 / IPOW20(gain);
00876 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
00877 int const width = gi->width[sfb];
00878 int l;
00879 assert(width >= 0);
00880 j += width;
00881 if (!gfc->pseudohalf[sfb])
00882 continue;
00883 for (l = -width; l < 0; l++)
00884 if (xr[j + l] < roundfac)
00885 ix[j + l] = 0;
00886 }
00887 }
00888 return noquant_count_bits(gfc, gi, prev_noise);
00889 }
00890
00891
00892
00893
00894
00895
00896
00897 inline static void
00898 recalc_divide_init(const lame_internal_flags * const gfc,
00899 gr_info const *cod_info,
00900 int const *const ix, int r01_bits[], int r01_div[], int r0_tbl[], int r1_tbl[])
00901 {
00902 int r0, r1, bigv, r0t, r1t, bits;
00903
00904 bigv = cod_info->big_values;
00905
00906 for (r0 = 0; r0 <= 7 + 15; r0++) {
00907 r01_bits[r0] = LARGE_BITS;
00908 }
00909
00910 for (r0 = 0; r0 < 16; r0++) {
00911 int const a1 = gfc->scalefac_band.l[r0 + 1];
00912 int r0bits;
00913 if (a1 >= bigv)
00914 break;
00915 r0bits = 0;
00916 r0t = gfc->choose_table(ix, ix + a1, &r0bits);
00917
00918 for (r1 = 0; r1 < 8; r1++) {
00919 int const a2 = gfc->scalefac_band.l[r0 + r1 + 2];
00920 if (a2 >= bigv)
00921 break;
00922
00923 bits = r0bits;
00924 r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
00925 if (r01_bits[r0 + r1] > bits) {
00926 r01_bits[r0 + r1] = bits;
00927 r01_div[r0 + r1] = r0;
00928 r0_tbl[r0 + r1] = r0t;
00929 r1_tbl[r0 + r1] = r1t;
00930 }
00931 }
00932 }
00933 }
00934
00935 inline static void
00936 recalc_divide_sub(const lame_internal_flags * const gfc,
00937 const gr_info * cod_info2,
00938 gr_info * const gi,
00939 const int *const ix,
00940 const int r01_bits[], const int r01_div[], const int r0_tbl[], const int r1_tbl[])
00941 {
00942 int bits, r2, a2, bigv, r2t;
00943
00944 bigv = cod_info2->big_values;
00945
00946 for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
00947 a2 = gfc->scalefac_band.l[r2];
00948 if (a2 >= bigv)
00949 break;
00950
00951 bits = r01_bits[r2 - 2] + cod_info2->count1bits;
00952 if (gi->part2_3_length <= bits)
00953 break;
00954
00955 r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
00956 if (gi->part2_3_length <= bits)
00957 continue;
00958
00959 memcpy(gi, cod_info2, sizeof(gr_info));
00960 gi->part2_3_length = bits;
00961 gi->region0_count = r01_div[r2 - 2];
00962 gi->region1_count = r2 - 2 - r01_div[r2 - 2];
00963 gi->table_select[0] = r0_tbl[r2 - 2];
00964 gi->table_select[1] = r1_tbl[r2 - 2];
00965 gi->table_select[2] = r2t;
00966 }
00967 }
00968
00969
00970
00971
00972 void
00973 best_huffman_divide(const lame_internal_flags * const gfc, gr_info * const gi)
00974 {
00975 int i, a1, a2;
00976 gr_info cod_info2;
00977 int const *const ix = gi->l3_enc;
00978
00979 int r01_bits[7 + 15 + 1];
00980 int r01_div[7 + 15 + 1];
00981 int r0_tbl[7 + 15 + 1];
00982 int r1_tbl[7 + 15 + 1];
00983
00984
00985
00986 if (gi->block_type == SHORT_TYPE && gfc->mode_gr == 1)
00987 return;
00988
00989
00990 memcpy(&cod_info2, gi, sizeof(gr_info));
00991 if (gi->block_type == NORM_TYPE) {
00992 recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
00993 recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
00994 }
00995
00996 i = cod_info2.big_values;
00997 if (i == 0 || (unsigned int) (ix[i - 2] | ix[i - 1]) > 1)
00998 return;
00999
01000 i = gi->count1 + 2;
01001 if (i > 576)
01002 return;
01003
01004
01005 memcpy(&cod_info2, gi, sizeof(gr_info));
01006 cod_info2.count1 = i;
01007 a1 = a2 = 0;
01008
01009 assert(i <= 576);
01010
01011 for (; i > cod_info2.big_values; i -= 4) {
01012 int const p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
01013 a1 += t32l[p];
01014 a2 += t33l[p];
01015 }
01016 cod_info2.big_values = i;
01017
01018 cod_info2.count1table_select = 0;
01019 if (a1 > a2) {
01020 a1 = a2;
01021 cod_info2.count1table_select = 1;
01022 }
01023
01024 cod_info2.count1bits = a1;
01025
01026 if (cod_info2.block_type == NORM_TYPE)
01027 recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
01028 else {
01029
01030 cod_info2.part2_3_length = a1;
01031 a1 = gfc->scalefac_band.l[7 + 1];
01032 if (a1 > i) {
01033 a1 = i;
01034 }
01035 if (a1 > 0)
01036 cod_info2.table_select[0] =
01037 gfc->choose_table(ix, ix + a1, (int *) &cod_info2.part2_3_length);
01038 if (i > a1)
01039 cod_info2.table_select[1] =
01040 gfc->choose_table(ix + a1, ix + i, (int *) &cod_info2.part2_3_length);
01041 if (gi->part2_3_length > cod_info2.part2_3_length)
01042 memcpy(gi, &cod_info2, sizeof(gr_info));
01043 }
01044 }
01045
01046 static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16 };
01047 static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
01048 const int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
01049 const int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
01050
01051 static void
01052 scfsi_calc(int ch, III_side_info_t * l3_side)
01053 {
01054 int i, s1, s2, c1, c2;
01055 int sfb;
01056 gr_info *const gi = &l3_side->tt[1][ch];
01057 gr_info const *const g0 = &l3_side->tt[0][ch];
01058
01059 for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
01060 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
01061 if (g0->scalefac[sfb] != gi->scalefac[sfb]
01062 && gi->scalefac[sfb] >= 0)
01063 break;
01064 }
01065 if (sfb == scfsi_band[i + 1]) {
01066 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
01067 gi->scalefac[sfb] = -1;
01068 }
01069 l3_side->scfsi[ch][i] = 1;
01070 }
01071 }
01072
01073 s1 = c1 = 0;
01074 for (sfb = 0; sfb < 11; sfb++) {
01075 if (gi->scalefac[sfb] == -1)
01076 continue;
01077 c1++;
01078 if (s1 < gi->scalefac[sfb])
01079 s1 = gi->scalefac[sfb];
01080 }
01081
01082 s2 = c2 = 0;
01083 for (; sfb < SBPSY_l; sfb++) {
01084 if (gi->scalefac[sfb] == -1)
01085 continue;
01086 c2++;
01087 if (s2 < gi->scalefac[sfb])
01088 s2 = gi->scalefac[sfb];
01089 }
01090
01091 for (i = 0; i < 16; i++) {
01092 if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
01093 int const c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
01094 if (gi->part2_length > c) {
01095 gi->part2_length = c;
01096 gi->scalefac_compress = i;
01097 }
01098 }
01099 }
01100 }
01101
01102
01103
01104
01105
01106
01107 void
01108 best_scalefac_store(const lame_internal_flags * gfc,
01109 const int gr, const int ch, III_side_info_t * const l3_side)
01110 {
01111
01112 gr_info *const gi = &l3_side->tt[gr][ch];
01113 int sfb, i, j, l;
01114 int recalc = 0;
01115
01116
01117
01118
01119 j = 0;
01120 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
01121 int const width = gi->width[sfb];
01122 assert(width >= 0);
01123 j += width;
01124 for (l = -width; l < 0; l++) {
01125 if (gi->l3_enc[l + j] != 0)
01126 break;
01127 }
01128 if (l == 0)
01129 gi->scalefac[sfb] = recalc = -2;
01130
01131
01132
01133 }
01134
01135 if (!gi->scalefac_scale && !gi->preflag) {
01136 int s = 0;
01137 for (sfb = 0; sfb < gi->sfbmax; sfb++)
01138 if (gi->scalefac[sfb] > 0)
01139 s |= gi->scalefac[sfb];
01140
01141 if (!(s & 1) && s != 0) {
01142 for (sfb = 0; sfb < gi->sfbmax; sfb++)
01143 if (gi->scalefac[sfb] > 0)
01144 gi->scalefac[sfb] >>= 1;
01145
01146 gi->scalefac_scale = recalc = 1;
01147 }
01148 }
01149
01150 if (!gi->preflag && gi->block_type != SHORT_TYPE && gfc->mode_gr == 2) {
01151 for (sfb = 11; sfb < SBPSY_l; sfb++)
01152 if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2)
01153 break;
01154 if (sfb == SBPSY_l) {
01155 for (sfb = 11; sfb < SBPSY_l; sfb++)
01156 if (gi->scalefac[sfb] > 0)
01157 gi->scalefac[sfb] -= pretab[sfb];
01158
01159 gi->preflag = recalc = 1;
01160 }
01161 }
01162
01163 for (i = 0; i < 4; i++)
01164 l3_side->scfsi[ch][i] = 0;
01165
01166 if (gfc->mode_gr == 2 && gr == 1
01167 && l3_side->tt[0][ch].block_type != SHORT_TYPE
01168 && l3_side->tt[1][ch].block_type != SHORT_TYPE) {
01169 scfsi_calc(ch, l3_side);
01170 recalc = 0;
01171 }
01172 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
01173 if (gi->scalefac[sfb] == -2) {
01174 gi->scalefac[sfb] = 0;
01175 }
01176 }
01177 if (recalc) {
01178 if (gfc->mode_gr == 2) {
01179 (void) scale_bitcount(gi);
01180 }
01181 else {
01182 (void) scale_bitcount_lsf(gfc, gi);
01183 }
01184 }
01185 }
01186
01187
01188 #ifndef NDEBUG
01189 static int
01190 all_scalefactors_not_negative(int const *scalefac, int n)
01191 {
01192 int i;
01193 for (i = 0; i < n; ++i) {
01194 if (scalefac[i] < 0)
01195 return 0;
01196 }
01197 return 1;
01198 }
01199 #endif
01200
01201
01202
01203
01204
01205 static const int scale_short[16] = {
01206 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126
01207 };
01208
01209
01210 static const int scale_mixed[16] = {
01211 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122
01212 };
01213
01214
01215 static const int scale_long[16] = {
01216 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74
01217 };
01218
01219
01220
01221
01222
01223
01224
01225
01226 int
01227 scale_bitcount(gr_info * const cod_info)
01228 {
01229 int k, sfb, max_slen1 = 0, max_slen2 = 0;
01230
01231
01232 const int *tab;
01233 int *const scalefac = cod_info->scalefac;
01234
01235 assert(all_scalefactors_not_negative(scalefac, cod_info->sfbmax));
01236
01237 if (cod_info->block_type == SHORT_TYPE) {
01238 tab = scale_short;
01239 if (cod_info->mixed_block_flag)
01240 tab = scale_mixed;
01241 }
01242 else {
01243 tab = scale_long;
01244 if (!cod_info->preflag) {
01245 for (sfb = 11; sfb < SBPSY_l; sfb++)
01246 if (scalefac[sfb] < pretab[sfb])
01247 break;
01248
01249 if (sfb == SBPSY_l) {
01250 cod_info->preflag = 1;
01251 for (sfb = 11; sfb < SBPSY_l; sfb++)
01252 scalefac[sfb] -= pretab[sfb];
01253 }
01254 }
01255 }
01256
01257 for (sfb = 0; sfb < cod_info->sfbdivide; sfb++)
01258 if (max_slen1 < scalefac[sfb])
01259 max_slen1 = scalefac[sfb];
01260
01261 for (; sfb < cod_info->sfbmax; sfb++)
01262 if (max_slen2 < scalefac[sfb])
01263 max_slen2 = scalefac[sfb];
01264
01265
01266
01267
01268
01269 cod_info->part2_length = LARGE_BITS;
01270 for (k = 0; k < 16; k++) {
01271 if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
01272 && cod_info->part2_length > tab[k]) {
01273 cod_info->part2_length = tab[k];
01274 cod_info->scalefac_compress = k;
01275 }
01276 }
01277 return cod_info->part2_length == LARGE_BITS;
01278 }
01279
01280
01281
01282
01283
01284
01285 static const int max_range_sfac_tab[6][4] = {
01286 {15, 15, 7, 7},
01287 {15, 15, 7, 0},
01288 {7, 3, 0, 0},
01289 {15, 31, 31, 0},
01290 {7, 7, 7, 0},
01291 {3, 3, 0, 0}
01292 };
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307 int
01308 scale_bitcount_lsf(const lame_internal_flags * gfc, gr_info * const cod_info)
01309 {
01310 int table_number, row_in_table, partition, nr_sfb, window, over;
01311 int i, sfb, max_sfac[4];
01312 const int *partition_table;
01313 int const *const scalefac = cod_info->scalefac;
01314
01315
01316
01317
01318
01319 if (cod_info->preflag)
01320 table_number = 2;
01321 else
01322 table_number = 0;
01323
01324 for (i = 0; i < 4; i++)
01325 max_sfac[i] = 0;
01326
01327 if (cod_info->block_type == SHORT_TYPE) {
01328 row_in_table = 1;
01329 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
01330 for (sfb = 0, partition = 0; partition < 4; partition++) {
01331 nr_sfb = partition_table[partition] / 3;
01332 for (i = 0; i < nr_sfb; i++, sfb++)
01333 for (window = 0; window < 3; window++)
01334 if (scalefac[sfb * 3 + window] > max_sfac[partition])
01335 max_sfac[partition] = scalefac[sfb * 3 + window];
01336 }
01337 }
01338 else {
01339 row_in_table = 0;
01340 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
01341 for (sfb = 0, partition = 0; partition < 4; partition++) {
01342 nr_sfb = partition_table[partition];
01343 for (i = 0; i < nr_sfb; i++, sfb++)
01344 if (scalefac[sfb] > max_sfac[partition])
01345 max_sfac[partition] = scalefac[sfb];
01346 }
01347 }
01348
01349 for (over = 0, partition = 0; partition < 4; partition++) {
01350 if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
01351 over++;
01352 }
01353 if (!over) {
01354
01355
01356
01357
01358 static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
01359
01360 int slen1, slen2, slen3, slen4;
01361
01362 cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
01363 for (partition = 0; partition < 4; partition++)
01364 cod_info->slen[partition] = log2tab[max_sfac[partition]];
01365
01366
01367 slen1 = cod_info->slen[0];
01368 slen2 = cod_info->slen[1];
01369 slen3 = cod_info->slen[2];
01370 slen4 = cod_info->slen[3];
01371
01372 switch (table_number) {
01373 case 0:
01374 cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
01375 + (slen3 << 2)
01376 + slen4;
01377 break;
01378
01379 case 1:
01380 cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
01381 + slen3;
01382 break;
01383
01384 case 2:
01385 cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
01386 break;
01387
01388 default:
01389 ERRORF(gfc, "intensity stereo not implemented yet\n");
01390 break;
01391 }
01392 }
01393 #ifdef DEBUG
01394 if (over)
01395 ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n");
01396 #endif
01397 if (!over) {
01398 assert(cod_info->sfb_partition_table);
01399 cod_info->part2_length = 0;
01400 for (partition = 0; partition < 4; partition++)
01401 cod_info->part2_length +=
01402 cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
01403 }
01404 return over;
01405 }
01406
01407
01408
01409 void
01410 huffman_init(lame_internal_flags * const gfc)
01411 {
01412 int i;
01413
01414 gfc->choose_table = choose_table_nonMMX;
01415
01416 #ifdef MMX_choose_table
01417 if (gfc->CPU_features.MMX) {
01418 extern int choose_table_MMX(const int *ix, const int *const end, int *const s);
01419 gfc->choose_table = choose_table_MMX;
01420 }
01421 #endif
01422
01423 for (i = 2; i <= 576; i += 2) {
01424 int scfb_anz = 0, index;
01425 while (gfc->scalefac_band.l[++scfb_anz] < i);
01426
01427 index = subdv_table[scfb_anz].region0_count;
01428 while (gfc->scalefac_band.l[index + 1] > i)
01429 index--;
01430
01431 if (index < 0) {
01432
01433
01434
01435
01436 index = subdv_table[scfb_anz].region0_count;
01437 }
01438
01439 gfc->bv_scf[i - 2] = index;
01440
01441 index = subdv_table[scfb_anz].region1_count;
01442 while (gfc->scalefac_band.l[index + gfc->bv_scf[i - 2] + 2] > i)
01443 index--;
01444
01445 if (index < 0) {
01446 index = subdv_table[scfb_anz].region1_count;
01447 }
01448
01449 gfc->bv_scf[i - 1] = index;
01450 }
01451 }