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
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033 #include "lame.h"
00034 #include "machine.h"
00035 #include "encoder.h"
00036 #include "util.h"
00037 #include "lame_global_flags.h"
00038 #include "newmdct.h"
00039 #include "psymodel.h"
00040 #include "lame-analysis.h"
00041 #include "bitstream.h"
00042 #include "VbrTag.h"
00043 #include "quantize_pvt.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 static void
00056 adjust_ATH(lame_internal_flags const *const gfc)
00057 {
00058 FLOAT gr2_max, max_pow;
00059
00060 if (gfc->ATH->use_adjust == 0) {
00061 gfc->ATH->adjust = 1.0;
00062 return;
00063 }
00064
00065
00066
00067
00068 max_pow = gfc->loudness_sq[0][0];
00069 gr2_max = gfc->loudness_sq[1][0];
00070 if (gfc->channels_out == 2) {
00071 max_pow += gfc->loudness_sq[0][1];
00072 gr2_max += gfc->loudness_sq[1][1];
00073 }
00074 else {
00075 max_pow += max_pow;
00076 gr2_max += gr2_max;
00077 }
00078 if (gfc->mode_gr == 2) {
00079 max_pow = Max(max_pow, gr2_max);
00080 }
00081 max_pow *= 0.5;
00082
00083
00084
00085 max_pow *= gfc->ATH->aa_sensitivity_p;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 if (max_pow > 0.03125) {
00100 if (gfc->ATH->adjust >= 1.0) {
00101 gfc->ATH->adjust = 1.0;
00102 }
00103 else {
00104
00105
00106
00107 if (gfc->ATH->adjust < gfc->ATH->adjust_limit) {
00108 gfc->ATH->adjust = gfc->ATH->adjust_limit;
00109 }
00110 }
00111 gfc->ATH->adjust_limit = 1.0;
00112 }
00113 else {
00114
00115 FLOAT const adj_lim_new = 31.98 * max_pow + 0.000625;
00116 if (gfc->ATH->adjust >= adj_lim_new) {
00117 gfc->ATH->adjust *= adj_lim_new * 0.075 + 0.925;
00118 if (gfc->ATH->adjust < adj_lim_new) {
00119 gfc->ATH->adjust = adj_lim_new;
00120 }
00121 }
00122 else {
00123 if (gfc->ATH->adjust_limit >= adj_lim_new) {
00124 gfc->ATH->adjust = adj_lim_new;
00125 }
00126 else {
00127
00128 if (gfc->ATH->adjust < gfc->ATH->adjust_limit) {
00129 gfc->ATH->adjust = gfc->ATH->adjust_limit;
00130 }
00131 }
00132 }
00133 gfc->ATH->adjust_limit = adj_lim_new;
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 static void
00154 updateStats(lame_internal_flags * const gfc)
00155 {
00156 int gr, ch;
00157 assert(gfc->bitrate_index < 16u);
00158 assert(gfc->mode_ext < 4u);
00159
00160
00161 gfc->bitrate_stereoMode_Hist[gfc->bitrate_index][4]++;
00162 gfc->bitrate_stereoMode_Hist[15][4]++;
00163
00164
00165 if (gfc->channels_out == 2) {
00166 gfc->bitrate_stereoMode_Hist[gfc->bitrate_index][gfc->mode_ext]++;
00167 gfc->bitrate_stereoMode_Hist[15][gfc->mode_ext]++;
00168 }
00169 for (gr = 0; gr < gfc->mode_gr; ++gr) {
00170 for (ch = 0; ch < gfc->channels_out; ++ch) {
00171 int bt = gfc->l3_side.tt[gr][ch].block_type;
00172 if (gfc->l3_side.tt[gr][ch].mixed_block_flag)
00173 bt = 4;
00174 gfc->bitrate_blockType_Hist[gfc->bitrate_index][bt]++;
00175 gfc->bitrate_blockType_Hist[gfc->bitrate_index][5]++;
00176 gfc->bitrate_blockType_Hist[15][bt]++;
00177 gfc->bitrate_blockType_Hist[15][5]++;
00178 }
00179 }
00180 }
00181
00182
00183
00184
00185 static void
00186 lame_encode_frame_init(lame_global_flags const *const gfp, const sample_t * inbuf[2])
00187 {
00188 lame_internal_flags *const gfc = gfp->internal_flags;
00189
00190 int ch, gr;
00191
00192 if (gfc->lame_encode_frame_init == 0) {
00193
00194 int i, j;
00195 sample_t primebuff0[286 + 1152 + 576];
00196 sample_t primebuff1[286 + 1152 + 576];
00197 gfc->lame_encode_frame_init = 1;
00198 for (i = 0, j = 0; i < 286 + 576 * (1 + gfc->mode_gr); ++i) {
00199 if (i < 576 * gfc->mode_gr) {
00200 primebuff0[i] = 0;
00201 if (gfc->channels_out == 2)
00202 primebuff1[i] = 0;
00203 }
00204 else {
00205 primebuff0[i] = inbuf[0][j];
00206 if (gfc->channels_out == 2)
00207 primebuff1[i] = inbuf[1][j];
00208 ++j;
00209 }
00210 }
00211
00212 for (gr = 0; gr < gfc->mode_gr; gr++) {
00213 for (ch = 0; ch < gfc->channels_out; ch++) {
00214 gfc->l3_side.tt[gr][ch].block_type = SHORT_TYPE;
00215 }
00216 }
00217 mdct_sub48(gfc, primebuff0, primebuff1);
00218
00219
00220 #if 576 < FFTOFFSET
00221 # error FFTOFFSET greater than 576: FFT uses a negative offset
00222 #endif
00223
00224 assert(gfc->mf_size >= (BLKSIZE + gfp->framesize - FFTOFFSET));
00225
00226 assert(gfc->mf_size >= (512 + gfp->framesize - 32));
00227 }
00228
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 typedef FLOAT chgrdata[2][2];
00296
00297
00298 int
00299 lame_encode_mp3_frame(
00300 lame_global_flags * const gfp,
00301 sample_t const *inbuf_l,
00302 sample_t const *inbuf_r,
00303 unsigned char *mp3buf,
00304 int mp3buf_size)
00305 {
00306 int mp3count;
00307 III_psy_ratio masking_LR[2][2];
00308 III_psy_ratio masking_MS[2][2];
00309 III_psy_ratio(*masking)[2][2];
00310 const sample_t *inbuf[2];
00311 lame_internal_flags *const gfc = gfp->internal_flags;
00312
00313 FLOAT tot_ener[2][4];
00314 FLOAT ms_ener_ratio[2] = { .5, .5 };
00315 chgrdata pe = { {0., 0.}, {0., 0.} }, pe_MS = { {
00316 0., 0.}, {
00317 0., 0.}};
00318 chgrdata *pe_use;
00319
00320 int ch, gr;
00321
00322 FLOAT ms_ratio_next = 0.;
00323 FLOAT ms_ratio_prev = 0.;
00324
00325
00326 inbuf[0] = inbuf_l;
00327 inbuf[1] = inbuf_r;
00328
00329 if (gfc->lame_encode_frame_init == 0) {
00330
00331 lame_encode_frame_init(gfp, inbuf);
00332
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 gfc->padding = FALSE;
00346 if ((gfc->slot_lag -= gfc->frac_SpF) < 0) {
00347 gfc->slot_lag += gfp->out_samplerate;
00348 gfc->padding = TRUE;
00349 }
00350
00351
00352
00353
00354
00355
00356
00357 if (gfc->psymodel) {
00358
00359
00360
00361
00362 int ret;
00363 const sample_t *bufp[2];
00364 int blocktype[2];
00365
00366 ms_ratio_prev = gfc->ms_ratio[gfc->mode_gr - 1];
00367 for (gr = 0; gr < gfc->mode_gr; gr++) {
00368
00369 for (ch = 0; ch < gfc->channels_out; ch++)
00370 bufp[ch] = &inbuf[ch][576 + gr * 576 - FFTOFFSET];
00371
00372 if (gfp->psymodel == PSY_NSPSYTUNE) {
00373 ret = L3psycho_anal_ns(gfp, bufp, gr,
00374 masking_LR, masking_MS,
00375 pe[gr], pe_MS[gr], tot_ener[gr], blocktype);
00376 }
00377 else {
00378 ret = L3psycho_anal(gfp, bufp, gr,
00379 &gfc->ms_ratio[gr], &ms_ratio_next,
00380 masking_LR, masking_MS,
00381 pe[gr], pe_MS[gr], tot_ener[gr], blocktype);
00382 }
00383 if (ret != 0)
00384 return -4;
00385
00386 if (gfp->mode == JOINT_STEREO) {
00387 ms_ener_ratio[gr] = tot_ener[gr][2] + tot_ener[gr][3];
00388 if (ms_ener_ratio[gr] > 0)
00389 ms_ener_ratio[gr] = tot_ener[gr][3] / ms_ener_ratio[gr];
00390 }
00391
00392
00393 for (ch = 0; ch < gfc->channels_out; ch++) {
00394 gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
00395 cod_info->block_type = blocktype[ch];
00396 cod_info->mixed_block_flag = 0;
00397 }
00398 }
00399 }
00400 else {
00401
00402 memset((char *) masking_LR, 0, sizeof(masking_LR));
00403 memset((char *) masking_MS, 0, sizeof(masking_MS));
00404 for (gr = 0; gr < gfc->mode_gr; gr++)
00405 for (ch = 0; ch < gfc->channels_out; ch++) {
00406 gfc->l3_side.tt[gr][ch].block_type = NORM_TYPE;
00407 gfc->l3_side.tt[gr][ch].mixed_block_flag = 0;
00408 pe_MS[gr][ch] = pe[gr][ch] = 700;
00409 }
00410 }
00411
00412
00413
00414
00415 adjust_ATH(gfc);
00416
00417
00418
00419
00420
00421
00422
00423 mdct_sub48(gfc, inbuf[0], inbuf[1]);
00424
00425
00426
00427
00428
00429
00430
00431 gfc->mode_ext = MPG_MD_LR_LR;
00432
00433 if (gfp->force_ms) {
00434 gfc->mode_ext = MPG_MD_MS_LR;
00435 }
00436 else if (gfp->mode == JOINT_STEREO) {
00437 int check_ms_stereo = 1;
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 if (gfp->psymodel == PSY_GPSYCHO) {
00450 FLOAT ms_ratio_ave1;
00451 FLOAT ms_ratio_ave2;
00452 FLOAT const threshold1 = 0.35;
00453 FLOAT const threshold2 = 0.45;
00454
00455
00456 if (gfc->mode_gr == 1) {
00457
00458 ms_ratio_ave1 = 0.33 * (gfc->ms_ratio[0] + ms_ratio_prev + ms_ratio_next);
00459 ms_ratio_ave2 = gfc->ms_ratio[0];
00460 }
00461 else {
00462 ms_ratio_ave1 =
00463 0.25 * (gfc->ms_ratio[0] + gfc->ms_ratio[1] + ms_ratio_prev + ms_ratio_next);
00464 ms_ratio_ave2 = 0.50 * (gfc->ms_ratio[0] + gfc->ms_ratio[1]);
00465 }
00466
00467
00468 if (ms_ratio_ave1 >= threshold1 || ms_ratio_ave2 >= threshold2)
00469 check_ms_stereo = 0;
00470 }
00471
00472 if (check_ms_stereo) {
00473 FLOAT sum_pe_MS = 0;
00474 FLOAT sum_pe_LR = 0;
00475 for (gr = 0; gr < gfc->mode_gr; gr++) {
00476 for (ch = 0; ch < gfc->channels_out; ch++) {
00477 sum_pe_MS += pe_MS[gr][ch];
00478 sum_pe_LR += pe[gr][ch];
00479 }
00480 }
00481
00482
00483 if (((gfp->psymodel == PSY_GPSYCHO) && sum_pe_MS <= 1.07 * sum_pe_LR) ||
00484 ((gfp->psymodel == PSY_NSPSYTUNE) && sum_pe_MS <= 1.00 * sum_pe_LR)) {
00485
00486 gr_info const *const gi0 = &gfc->l3_side.tt[0][0];
00487 gr_info const *const gi1 = &gfc->l3_side.tt[gfc->mode_gr - 1][0];
00488
00489 if (gi0[0].block_type == gi0[1].block_type &&
00490 gi1[0].block_type == gi1[1].block_type) {
00491
00492 gfc->mode_ext = MPG_MD_MS_LR;
00493 }
00494 }
00495 }
00496 }
00497
00498
00499 if (gfc->mode_ext == MPG_MD_MS_LR) {
00500 masking = &masking_MS;
00501 pe_use = &pe_MS;
00502 }
00503 else {
00504 masking = &masking_LR;
00505 pe_use = &pe;
00506 }
00507
00508
00509
00510 if (gfp->analysis && gfc->pinfo != NULL) {
00511 for (gr = 0; gr < gfc->mode_gr; gr++) {
00512 for (ch = 0; ch < gfc->channels_out; ch++) {
00513 gfc->pinfo->ms_ratio[gr] = gfc->ms_ratio[gr];
00514 gfc->pinfo->ms_ener_ratio[gr] = ms_ener_ratio[gr];
00515 gfc->pinfo->blocktype[gr][ch] = gfc->l3_side.tt[gr][ch].block_type;
00516 gfc->pinfo->pe[gr][ch] = (*pe_use)[gr][ch];
00517 memcpy(gfc->pinfo->xr[gr][ch], &gfc->l3_side.tt[gr][ch].xr, sizeof(FLOAT) * 576);
00518
00519
00520 if (gfc->mode_ext == MPG_MD_MS_LR) {
00521 gfc->pinfo->ers[gr][ch] = gfc->pinfo->ers[gr][ch + 2];
00522 memcpy(gfc->pinfo->energy[gr][ch], gfc->pinfo->energy[gr][ch + 2],
00523 sizeof(gfc->pinfo->energy[gr][ch]));
00524 }
00525 }
00526 }
00527 }
00528
00529
00530
00531
00532
00533
00534 if (gfp->psymodel == PSY_NSPSYTUNE) {
00535 if (gfp->VBR == vbr_off || gfp->VBR == vbr_abr) {
00536 static FLOAT const fircoef[9] = {
00537 -0.0207887 * 5, -0.0378413 * 5, -0.0432472 * 5, -0.031183 * 5,
00538 7.79609e-18 * 5, 0.0467745 * 5, 0.10091 * 5, 0.151365 * 5,
00539 0.187098 * 5
00540 };
00541
00542 int i;
00543 FLOAT f;
00544
00545 for (i = 0; i < 18; i++)
00546 gfc->nsPsy.pefirbuf[i] = gfc->nsPsy.pefirbuf[i + 1];
00547
00548 f = 0.0;
00549 for (gr = 0; gr < gfc->mode_gr; gr++)
00550 for (ch = 0; ch < gfc->channels_out; ch++)
00551 f += (*pe_use)[gr][ch];
00552 gfc->nsPsy.pefirbuf[18] = f;
00553
00554 f = gfc->nsPsy.pefirbuf[9];
00555 for (i = 0; i < 9; i++)
00556 f += (gfc->nsPsy.pefirbuf[i] + gfc->nsPsy.pefirbuf[18 - i]) * fircoef[i];
00557
00558 f = (670 * 5 * gfc->mode_gr * gfc->channels_out) / f;
00559 for (gr = 0; gr < gfc->mode_gr; gr++) {
00560 for (ch = 0; ch < gfc->channels_out; ch++) {
00561 (*pe_use)[gr][ch] *= f;
00562 }
00563 }
00564 }
00565 }
00566 gfc->iteration_loop(gfp, *pe_use, ms_ener_ratio, *masking);
00567
00568
00569
00570
00571
00572
00573
00574
00575 (void) format_bitstream(gfp);
00576
00577
00578 mp3count = copy_buffer(gfc, mp3buf, mp3buf_size, 1);
00579
00580
00581 if (gfp->bWriteVbrTag)
00582 AddVbrFrame(gfp);
00583
00584
00585 if (gfp->analysis && gfc->pinfo != NULL) {
00586 for (ch = 0; ch < gfc->channels_out; ch++) {
00587 int j;
00588 for (j = 0; j < FFTOFFSET; j++)
00589 gfc->pinfo->pcmdata[ch][j] = gfc->pinfo->pcmdata[ch][j + gfp->framesize];
00590 for (j = FFTOFFSET; j < 1600; j++) {
00591 gfc->pinfo->pcmdata[ch][j] = inbuf[ch][j - FFTOFFSET];
00592 }
00593 }
00594 set_frame_pinfo(gfp, *masking);
00595 }
00596
00597 updateStats(gfc);
00598
00599 return mp3count;
00600 }