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
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032
00033
00034 #include "lame.h"
00035 #include "machine.h"
00036
00037 #include "encoder.h"
00038 #include "util.h"
00039 #include "lame_global_flags.h"
00040 #include "gain_analysis.h"
00041 #include "bitstream.h"
00042 #include "quantize_pvt.h"
00043 #include "set_get.h"
00044 #include "quantize.h"
00045 #include "psymodel.h"
00046 #include "version.h"
00047 #include "VbrTag.h"
00048
00049
00050 #if defined(__FreeBSD__) && !defined(__alpha__)
00051 #include <floatingpoint.h>
00052 #endif
00053 #ifdef __riscos__
00054 #include "asmstuff.h"
00055 #endif
00056
00057 #ifdef __sun__
00058
00059 #include <unistd.h>
00060 #endif
00061
00062
00063 #define LAME_DEFAULT_QUALITY 3
00064
00065 static FLOAT
00066 filter_coef(FLOAT x)
00067 {
00068 if (x > 1.0)
00069 return 0.0;
00070 if (x <= 0.0)
00071 return 1.0;
00072
00073 return cos(PI / 2 * x);
00074 }
00075
00076 static void
00077 lame_init_params_ppflt(lame_global_flags const *gfp)
00078 {
00079 lame_internal_flags *const gfc = gfp->internal_flags;
00080
00081
00082
00083
00084 int band, maxband, minband;
00085 FLOAT freq;
00086 int lowpass_band = 32;
00087 int highpass_band = -1;
00088
00089 if (gfc->lowpass1 > 0) {
00090 minband = 999;
00091 for (band = 0; band <= 31; band++) {
00092 freq = band / 31.0;
00093
00094 if (freq >= gfc->lowpass2) {
00095 lowpass_band = Min(lowpass_band, band);
00096 }
00097 if (gfc->lowpass1 < freq && freq < gfc->lowpass2) {
00098 minband = Min(minband, band);
00099 }
00100 }
00101
00102
00103
00104 if (minband == 999) {
00105 gfc->lowpass1 = (lowpass_band - .75) / 31.0;
00106 }
00107 else {
00108 gfc->lowpass1 = (minband - .75) / 31.0;
00109 }
00110 gfc->lowpass2 = lowpass_band / 31.0;
00111 }
00112
00113
00114
00115 if (gfc->highpass2 > 0) {
00116 if (gfc->highpass2 < .9 * (.75 / 31.0)) {
00117 gfc->highpass1 = 0;
00118 gfc->highpass2 = 0;
00119 MSGF(gfc, "Warning: highpass filter disabled. " "highpass frequency too small\n");
00120 }
00121 }
00122
00123 if (gfc->highpass2 > 0) {
00124 maxband = -1;
00125 for (band = 0; band <= 31; band++) {
00126 freq = band / 31.0;
00127
00128 if (freq <= gfc->highpass1) {
00129 highpass_band = Max(highpass_band, band);
00130 }
00131 if (gfc->highpass1 < freq && freq < gfc->highpass2) {
00132 maxband = Max(maxband, band);
00133 }
00134 }
00135
00136
00137 gfc->highpass1 = highpass_band / 31.0;
00138 if (maxband == -1) {
00139 gfc->highpass2 = (highpass_band + .75) / 31.0;
00140 }
00141 else {
00142 gfc->highpass2 = (maxband + .75) / 31.0;
00143 }
00144 }
00145
00146 for (band = 0; band < 32; band++) {
00147 freq = band / 31.0;
00148 gfc->amp_filter[band]
00149 = filter_coef((gfc->highpass2 - freq)
00150 / (gfc->highpass2 - gfc->highpass1 + 1e-37))
00151 * filter_coef((freq - gfc->lowpass1)
00152 / (gfc->lowpass2 - gfc->lowpass1 - 1e-37));
00153 }
00154 }
00155
00156
00157 static void
00158 optimum_bandwidth(double *const lowerlimit, double *const upperlimit, const unsigned bitrate)
00159 {
00160
00161
00162
00163
00164
00165
00166
00167
00168 int index;
00169
00170 typedef struct {
00171 int bitrate;
00172 int lowpass;
00173 } band_pass_t;
00174
00175 const band_pass_t freq_map[] = {
00176 {8, 2000},
00177 {16, 3700},
00178 {24, 3900},
00179 {32, 5500},
00180 {40, 7000},
00181 {48, 7500},
00182 {56, 10000},
00183 {64, 11000},
00184 {80, 13500},
00185 {96, 15100},
00186 {112, 15600},
00187 {128, 17000},
00188 {160, 17500},
00189 {192, 18600},
00190 {224, 19400},
00191 {256, 19700},
00192 {320, 20500}
00193 };
00194
00195
00196 index = nearestBitrateFullIndex(bitrate);
00197
00198 *lowerlimit = freq_map[index].lowpass;
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 }
00233
00234
00235 static int
00236 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
00237 {
00238
00239
00240
00241
00242
00243 int suggested_samplefreq = 44100;
00244
00245 if (input_samplefreq >= 48000)
00246 suggested_samplefreq = 48000;
00247 else if (input_samplefreq >= 44100)
00248 suggested_samplefreq = 44100;
00249 else if (input_samplefreq >= 32000)
00250 suggested_samplefreq = 32000;
00251 else if (input_samplefreq >= 24000)
00252 suggested_samplefreq = 24000;
00253 else if (input_samplefreq >= 22050)
00254 suggested_samplefreq = 22050;
00255 else if (input_samplefreq >= 16000)
00256 suggested_samplefreq = 16000;
00257 else if (input_samplefreq >= 12000)
00258 suggested_samplefreq = 12000;
00259 else if (input_samplefreq >= 11025)
00260 suggested_samplefreq = 11025;
00261 else if (input_samplefreq >= 8000)
00262 suggested_samplefreq = 8000;
00263
00264 if (lowpassfreq == -1)
00265 return suggested_samplefreq;
00266
00267 if (lowpassfreq <= 15960)
00268 suggested_samplefreq = 44100;
00269 if (lowpassfreq <= 15250)
00270 suggested_samplefreq = 32000;
00271 if (lowpassfreq <= 11220)
00272 suggested_samplefreq = 24000;
00273 if (lowpassfreq <= 9970)
00274 suggested_samplefreq = 22050;
00275 if (lowpassfreq <= 7230)
00276 suggested_samplefreq = 16000;
00277 if (lowpassfreq <= 5420)
00278 suggested_samplefreq = 12000;
00279 if (lowpassfreq <= 4510)
00280 suggested_samplefreq = 11025;
00281 if (lowpassfreq <= 3970)
00282 suggested_samplefreq = 8000;
00283
00284 if (input_samplefreq < suggested_samplefreq) {
00285
00286
00287
00288
00289 if (input_samplefreq > 44100) {
00290 return 48000;
00291 }
00292 if (input_samplefreq > 32000) {
00293 return 44100;
00294 }
00295 if (input_samplefreq > 24000) {
00296 return 32000;
00297 }
00298 if (input_samplefreq > 22050) {
00299 return 24000;
00300 }
00301 if (input_samplefreq > 16000) {
00302 return 22050;
00303 }
00304 if (input_samplefreq > 12000) {
00305 return 16000;
00306 }
00307 if (input_samplefreq > 11025) {
00308 return 12000;
00309 }
00310 if (input_samplefreq > 8000) {
00311 return 11025;
00312 }
00313 return 8000;
00314 }
00315 return suggested_samplefreq;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 void
00325 lame_init_qval(lame_global_flags * gfp)
00326 {
00327 lame_internal_flags *const gfc = gfp->internal_flags;
00328
00329 switch (gfp->quality) {
00330 default:
00331 case 9:
00332 gfc->filter_type = 0;
00333 gfc->psymodel = 0;
00334 gfc->quantization = 0;
00335 gfc->noise_shaping = 0;
00336 gfc->noise_shaping_amp = 0;
00337 gfc->noise_shaping_stop = 0;
00338 gfc->use_best_huffman = 0;
00339 gfc->full_outer_loop = 0;
00340 break;
00341
00342 case 8:
00343 gfp->quality = 7;
00344
00345 case 7:
00346 gfc->filter_type = 0;
00347 gfc->psymodel = 1;
00348 gfc->quantization = 0;
00349 gfc->noise_shaping = 0;
00350 gfc->noise_shaping_amp = 0;
00351 gfc->noise_shaping_stop = 0;
00352 gfc->use_best_huffman = 0;
00353 gfc->full_outer_loop = 0;
00354 break;
00355
00356 case 6:
00357 gfc->filter_type = 0;
00358 gfc->psymodel = 1;
00359 gfc->quantization = 0;
00360 if (gfc->noise_shaping == 0)
00361 gfc->noise_shaping = 1;
00362 gfc->noise_shaping_amp = 0;
00363 gfc->noise_shaping_stop = 0;
00364 if (gfc->subblock_gain == -1)
00365 gfc->subblock_gain = 1;
00366 gfc->use_best_huffman = 0;
00367 gfc->full_outer_loop = 0;
00368 break;
00369
00370 case 5:
00371 gfc->filter_type = 0;
00372 gfc->psymodel = 1;
00373 gfc->quantization = 1;
00374 if (gfc->noise_shaping == 0)
00375 gfc->noise_shaping = 1;
00376 gfc->noise_shaping_amp = 0;
00377 gfc->noise_shaping_stop = 0;
00378 if (gfc->subblock_gain == -1)
00379 gfc->subblock_gain = 1;
00380 gfc->use_best_huffman = 0;
00381 gfc->full_outer_loop = 0;
00382 break;
00383
00384 case 4:
00385 gfc->filter_type = 0;
00386 gfc->psymodel = 1;
00387 gfc->quantization = 1;
00388 if (gfc->noise_shaping == 0)
00389 gfc->noise_shaping = 1;
00390 gfc->noise_shaping_amp = 0;
00391 gfc->noise_shaping_stop = 0;
00392 if (gfc->subblock_gain == -1)
00393 gfc->subblock_gain = 1;
00394 gfc->use_best_huffman = 1;
00395 gfc->full_outer_loop = 0;
00396 break;
00397
00398 case 3:
00399 gfc->filter_type = 0;
00400 gfc->psymodel = 1;
00401 gfc->quantization = 1;
00402 if (gfc->noise_shaping == 0)
00403 gfc->noise_shaping = 1;
00404 gfc->noise_shaping_amp = 1;
00405 gfc->noise_shaping_stop = 1;
00406 if (gfc->subblock_gain == -1)
00407 gfc->subblock_gain = 1;
00408 gfc->use_best_huffman = 1;
00409 gfc->full_outer_loop = 0;
00410 break;
00411
00412 case 2:
00413 gfc->filter_type = 0;
00414 gfc->psymodel = 1;
00415 gfc->quantization = 1;
00416 if (gfc->noise_shaping == 0)
00417 gfc->noise_shaping = 1;
00418 if (gfc->substep_shaping == 0)
00419 gfc->substep_shaping = 2;
00420 gfc->noise_shaping_amp = 1;
00421 gfc->noise_shaping_stop = 1;
00422 if (gfc->subblock_gain == -1)
00423 gfc->subblock_gain = 1;
00424 gfc->use_best_huffman = 1;
00425 gfc->full_outer_loop = 0;
00426 break;
00427
00428 case 1:
00429 gfc->filter_type = 0;
00430 gfc->psymodel = 1;
00431 gfc->quantization = 1;
00432 if (gfc->noise_shaping == 0)
00433 gfc->noise_shaping = 1;
00434 if (gfc->substep_shaping == 0)
00435 gfc->substep_shaping = 2;
00436 gfc->noise_shaping_amp = 2;
00437 gfc->noise_shaping_stop = 1;
00438 if (gfc->subblock_gain == -1)
00439 gfc->subblock_gain = 1;
00440 gfc->use_best_huffman = 1;
00441 gfc->full_outer_loop = 0;
00442 break;
00443
00444 case 0:
00445 gfc->filter_type = 0;
00446 gfc->psymodel = 1;
00447 gfc->quantization = 1;
00448 if (gfc->noise_shaping == 0)
00449 gfc->noise_shaping = 1;
00450 if (gfc->substep_shaping == 0)
00451 gfc->substep_shaping = 2;
00452 gfc->noise_shaping_amp = 2;
00453 gfc->noise_shaping_stop = 1;
00454 if (gfc->subblock_gain == -1)
00455 gfc->subblock_gain = 1;
00456 gfc->use_best_huffman = 1;
00457
00458 gfc->full_outer_loop = 0;
00459
00460
00461 break;
00462 }
00463
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 int
00521 lame_init_params(lame_global_flags * const gfp)
00522 {
00523
00524 int i;
00525 int j;
00526 lame_internal_flags *const gfc = gfp->internal_flags;
00527
00528 gfc->Class_ID = 0;
00529
00530
00531 gfc->report.msgf = gfp->report.msgf;
00532 gfc->report.debugf = gfp->report.debugf;
00533 gfc->report.errorf = gfp->report.errorf;
00534
00535 if (gfp->asm_optimizations.amd3dnow)
00536 gfc->CPU_features.AMD_3DNow = has_3DNow();
00537 else
00538 gfc->CPU_features.AMD_3DNow = 0;
00539
00540 if (gfp->asm_optimizations.mmx)
00541 gfc->CPU_features.MMX = has_MMX();
00542 else
00543 gfc->CPU_features.MMX = 0;
00544
00545 if (gfp->asm_optimizations.sse) {
00546 gfc->CPU_features.SSE = has_SSE();
00547 gfc->CPU_features.SSE2 = has_SSE2();
00548 }
00549 else {
00550 gfc->CPU_features.SSE = 0;
00551 gfc->CPU_features.SSE2 = 0;
00552 }
00553
00554
00555 if (NULL == gfc->ATH)
00556 gfc->ATH = calloc(1, sizeof(ATH_t));
00557
00558 if (NULL == gfc->ATH)
00559 return -2;
00560
00561 if (NULL == gfc->PSY)
00562 gfc->PSY = calloc(1, sizeof(PSY_t));
00563 if (NULL == gfc->PSY) {
00564 freegfc(gfc);
00565 gfp->internal_flags = NULL;
00566 return -2;
00567 }
00568
00569 if (NULL == gfc->rgdata)
00570 gfc->rgdata = calloc(1, sizeof(replaygain_t));
00571 if (NULL == gfc->rgdata) {
00572 freegfc(gfc);
00573 gfp->internal_flags = NULL;
00574 return -2;
00575 }
00576
00577 gfc->channels_in = gfp->num_channels;
00578 if (gfc->channels_in == 1)
00579 gfp->mode = MONO;
00580 gfc->channels_out = (gfp->mode == MONO) ? 1 : 2;
00581 gfc->mode_ext = MPG_MD_MS_LR;
00582 if (gfp->mode == MONO)
00583 gfp->force_ms = 0;
00584
00585 if (gfp->VBR == vbr_off && gfp->VBR_mean_bitrate_kbps != 128 && gfp->brate == 0)
00586 gfp->brate = gfp->VBR_mean_bitrate_kbps;
00587
00588
00589 if (gfp->VBR != vbr_off)
00590 gfp->free_format = 0;
00591
00592 if (gfp->VBR == vbr_off && gfp->brate == 0) {
00593
00594 if (gfp->compression_ratio == 0)
00595 gfp->compression_ratio = 11.025;
00596 }
00597
00598
00599 if (gfp->VBR == vbr_off && gfp->compression_ratio > 0) {
00600
00601 if (gfp->out_samplerate == 0)
00602 gfp->out_samplerate = map2MP3Frequency((int) (0.97 * gfp->in_samplerate));
00603
00604
00605
00606
00607 gfp->brate = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->compression_ratio);
00608
00609
00610 gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
00611
00612 if (!gfp->free_format)
00613 gfp->brate = FindNearestBitrate(gfp->brate, gfp->version, gfp->out_samplerate);
00614 }
00615
00616 if (gfp->VBR != vbr_off && gfp->brate >= 320)
00617 gfp->VBR = vbr_off;
00618 if (gfp->out_samplerate) {
00619 if (gfp->out_samplerate < 16000) {
00620 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
00621 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 64);
00622 }
00623 else if (gfp->out_samplerate < 32000) {
00624 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
00625 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 160);
00626 }
00627 else {
00628 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 32);
00629 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 320);
00630 }
00631 }
00632
00633
00634
00635
00636 if (gfp->lowpassfreq == 0) {
00637 double lowpass = 16000;
00638 double highpass;
00639
00640 switch (gfp->VBR) {
00641 case vbr_off:{
00642 optimum_bandwidth(&lowpass, &highpass, gfp->brate);
00643 break;
00644 }
00645 case vbr_abr:{
00646 optimum_bandwidth(&lowpass, &highpass, gfp->VBR_mean_bitrate_kbps);
00647 break;
00648 }
00649 default:{
00650 unsigned int const x[10] = {
00651 19500, 19000, 18600, 18000, 17500, 16000, 15600, 14900, 12500, 10000
00652 };
00653 if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
00654 lowpass = x[gfp->VBR_q];
00655 }
00656 else {
00657 lowpass = 19500;
00658 }
00659 }
00660 }
00661
00662 if (gfp->mode == MONO && (gfp->VBR == vbr_off || gfp->VBR == vbr_abr))
00663 lowpass *= 1.5;
00664
00665 gfp->lowpassfreq = lowpass;
00666 }
00667
00668 if (gfp->out_samplerate == 0) {
00669 if (2*gfp->lowpassfreq > gfp->in_samplerate) {
00670 gfp->lowpassfreq = gfp->in_samplerate/2;
00671 }
00672 gfp->out_samplerate = optimum_samplefreq((int) gfp->lowpassfreq, gfp->in_samplerate);
00673 }
00674
00675 gfp->lowpassfreq = Min(20500, gfp->lowpassfreq);
00676 gfp->lowpassfreq = Min(gfp->out_samplerate / 2, gfp->lowpassfreq);
00677
00678 if (gfp->VBR == vbr_off) {
00679 gfp->compression_ratio = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
00680 }
00681 if (gfp->VBR == vbr_abr) {
00682 gfp->compression_ratio =
00683 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
00684 }
00685
00686
00687
00688 if (!gfp->bWriteVbrTag) {
00689 gfp->findReplayGain = 0;
00690 gfp->decode_on_the_fly = 0;
00691 gfc->findPeakSample = 0;
00692 }
00693 gfc->findReplayGain = gfp->findReplayGain;
00694 gfc->decode_on_the_fly = gfp->decode_on_the_fly;
00695
00696 if (gfc->decode_on_the_fly)
00697 gfc->findPeakSample = 1;
00698
00699 if (gfc->findReplayGain) {
00700 if (InitGainAnalysis(gfc->rgdata, gfp->out_samplerate) == INIT_GAIN_ANALYSIS_ERROR) {
00701 freegfc(gfc);
00702 gfp->internal_flags = NULL;
00703 return -6;
00704 }
00705 }
00706
00707 #ifdef DECODE_ON_THE_FLY
00708 if (gfc->decode_on_the_fly && !gfp->decode_only)
00709 (void) lame_decode_init();
00710 #endif
00711
00712 gfc->mode_gr = gfp->out_samplerate <= 24000 ? 1 : 2;
00713 gfp->framesize = 576 * gfc->mode_gr;
00714 gfp->encoder_delay = ENCDELAY;
00715
00716 gfc->resample_ratio = (double) gfp->in_samplerate / gfp->out_samplerate;
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 switch (gfp->VBR) {
00747 case vbr_mt:
00748 case vbr_rh:
00749 case vbr_mtrh:
00750 {
00751
00752 FLOAT const cmp[] = { 5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14, 15, 16.5 };
00753 gfp->compression_ratio = cmp[gfp->VBR_q];
00754 }
00755 break;
00756 case vbr_abr:
00757 gfp->compression_ratio =
00758 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
00759 break;
00760 default:
00761 gfp->compression_ratio = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
00762 break;
00763 }
00764
00765
00766
00767
00768
00769
00770 if (gfp->mode == NOT_SET) {
00771 gfp->mode = JOINT_STEREO;
00772 }
00773
00774
00775
00776 if (gfp->highpassfreq > 0) {
00777 gfc->highpass1 = 2. * gfp->highpassfreq;
00778
00779 if (gfp->highpasswidth >= 0)
00780 gfc->highpass2 = 2. * (gfp->highpassfreq + gfp->highpasswidth);
00781 else
00782 gfc->highpass2 = (1 + 0.00) * 2. * gfp->highpassfreq;
00783
00784 gfc->highpass1 /= gfp->out_samplerate;
00785 gfc->highpass2 /= gfp->out_samplerate;
00786 }
00787
00788
00789 if (gfp->lowpassfreq > 0) {
00790 gfc->lowpass2 = 2. * gfp->lowpassfreq;
00791 if (gfp->lowpasswidth >= 0) {
00792 gfc->lowpass1 = 2. * (gfp->lowpassfreq - gfp->lowpasswidth);
00793 if (gfc->lowpass1 < 0)
00794 gfc->lowpass1 = 0;
00795 }
00796 else {
00797 gfc->lowpass1 = (1 - 0.00) * 2. * gfp->lowpassfreq;
00798 }
00799 gfc->lowpass1 /= gfp->out_samplerate;
00800 gfc->lowpass2 /= gfp->out_samplerate;
00801 }
00802
00803
00804
00805
00806
00807
00808
00809 lame_init_params_ppflt(gfp);
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
00823 if (gfc->samplerate_index < 0) {
00824 freegfc(gfc);
00825 gfp->internal_flags = NULL;
00826 return -1;
00827 }
00828
00829 if (gfp->VBR == vbr_off) {
00830 if (gfp->free_format) {
00831 gfc->bitrate_index = 0;
00832 }
00833 else {
00834 gfp->brate = FindNearestBitrate(gfp->brate, gfp->version, gfp->out_samplerate);
00835 gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version, gfp->out_samplerate);
00836 if (gfc->bitrate_index <= 0) {
00837 freegfc(gfc);
00838 gfp->internal_flags = NULL;
00839 return -1;
00840 }
00841 }
00842 }
00843
00844
00845
00846
00847
00848 if (gfp->analysis)
00849 gfp->bWriteVbrTag = 0;
00850
00851
00852 if (gfc->pinfo != NULL)
00853 gfp->bWriteVbrTag = 0;
00854
00855 init_bit_stream_w(gfc);
00856
00857 j = gfc->samplerate_index + (3 * gfp->version) + 6 * (gfp->out_samplerate < 16000);
00858 for (i = 0; i < SBMAX_l + 1; i++)
00859 gfc->scalefac_band.l[i] = sfBandIndex[j].l[i];
00860
00861 for (i = 0; i < PSFB21 + 1; i++) {
00862 int const size = (gfc->scalefac_band.l[22] - gfc->scalefac_band.l[21]) / PSFB21;
00863 int const start = gfc->scalefac_band.l[21] + i * size;
00864 gfc->scalefac_band.psfb21[i] = start;
00865 }
00866 gfc->scalefac_band.psfb21[PSFB21] = 576;
00867
00868 for (i = 0; i < SBMAX_s + 1; i++)
00869 gfc->scalefac_band.s[i] = sfBandIndex[j].s[i];
00870
00871 for (i = 0; i < PSFB12 + 1; i++) {
00872 int const size = (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) / PSFB12;
00873 int const start = gfc->scalefac_band.s[12] + i * size;
00874 gfc->scalefac_band.psfb12[i] = start;
00875 }
00876 gfc->scalefac_band.psfb12[PSFB12] = 192;
00877
00878
00879 if (gfp->version == 1)
00880 gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 17 : 4 + 32;
00881 else
00882 gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 9 : 4 + 17;
00883
00884 if (gfp->error_protection)
00885 gfc->sideinfo_len += 2;
00886
00887 (void) lame_init_bitstream(gfp);
00888
00889 gfc->Class_ID = LAME_ID;
00890
00891 {
00892 int k;
00893
00894 for (k = 0; k < 19; k++)
00895 gfc->nsPsy.pefirbuf[k] = 700 * gfc->mode_gr * gfc->channels_out;
00896
00897 if (gfp->ATHtype == -1)
00898 gfp->ATHtype = 4;
00899 }
00900
00901 assert(gfp->VBR_q <= 9);
00902 assert(gfp->VBR_q >= 0);
00903
00904 switch (gfp->VBR) {
00905
00906 case vbr_mt:
00907 gfp->VBR = vbr_mtrh;
00908
00909 case vbr_mtrh:{
00910 if ( gfp->useTemporal < 0 ) {
00911 gfp->useTemporal = 0;
00912 }
00913
00914 (void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
00915
00916
00917
00918
00919
00920
00921 if (gfp->quality < 0)
00922 gfp->quality = LAME_DEFAULT_QUALITY;
00923 if (gfp->quality < 5)
00924 gfp->quality = 0;
00925 if (gfp->quality > 6)
00926 gfp->quality = 6;
00927
00928
00929
00930
00931 if (gfp->cwlimit <= 0)
00932 gfp->cwlimit = 0.42 * gfp->out_samplerate;
00933
00934 gfc->PSY->mask_adjust = gfp->maskingadjust;
00935 gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
00936
00937
00938
00939 if (gfp->experimentalY)
00940 gfc->sfb21_extra = 0;
00941 else
00942 gfc->sfb21_extra = (gfp->out_samplerate > 44000);
00943
00944 switch (gfp->VBR_q) {
00945 case 0:
00946 case 1:
00947 case 2:
00948 break;
00949 case 3:
00950 gfc->PSY->mask_adjust += 0.6;
00951 gfc->PSY->mask_adjust_short += 0.6;
00952 break;
00953 case 4:
00954 gfc->PSY->mask_adjust += 1.2;
00955 gfc->PSY->mask_adjust_short += 1.2;
00956 break;
00957 case 5:
00958 default:
00959 gfc->PSY->mask_adjust += 2;
00960 gfc->PSY->mask_adjust_short += 2;
00961 }
00962 gfc->iteration_loop = VBR_new_iteration_loop;
00963 break;
00964
00965 }
00966 case vbr_rh:{
00967
00968 (void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
00969
00970 gfc->PSY->mask_adjust = gfp->maskingadjust;
00971 gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
00972
00973
00974
00975 if (gfp->experimentalY)
00976 gfc->sfb21_extra = 0;
00977 else
00978 gfc->sfb21_extra = (gfp->out_samplerate > 44000);
00979
00980
00981
00982
00983
00984
00985 if (gfp->quality > 6)
00986 gfp->quality = 6;
00987
00988
00989 if (gfp->quality < 0)
00990 gfp->quality = LAME_DEFAULT_QUALITY;
00991
00992 gfc->iteration_loop = VBR_old_iteration_loop;
00993 break;
00994 }
00995
00996 default: {
00997 vbr_mode vbrmode;
00998
00999
01000
01001 gfc->sfb21_extra = 0;
01002
01003 if (gfp->quality < 0)
01004 gfp->quality = LAME_DEFAULT_QUALITY;
01005
01006
01007 vbrmode = lame_get_VBR(gfp);
01008 if (vbrmode == vbr_off)
01009 (void) lame_set_VBR_mean_bitrate_kbps(gfp, gfp->brate);
01010
01011 (void) apply_preset(gfp, gfp->VBR_mean_bitrate_kbps, 0);
01012 (void) lame_set_VBR(gfp, vbrmode);
01013
01014 gfc->PSY->mask_adjust = gfp->maskingadjust;
01015 gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
01016
01017 if (vbrmode == vbr_off) {
01018 gfc->iteration_loop = CBR_iteration_loop;
01019 }
01020 else {
01021 gfc->iteration_loop = ABR_iteration_loop;
01022 }
01023 break;
01024 }
01025 }
01026
01027
01028
01029
01030 if (lame_get_VBR(gfp) != vbr_off) {
01031
01032 gfc->VBR_min_bitrate = 1;
01033 gfc->VBR_max_bitrate = 14;
01034 if (gfp->out_samplerate < 16000)
01035 gfc->VBR_max_bitrate = 8;
01036 if (gfp->VBR_min_bitrate_kbps) {
01037 gfp->VBR_min_bitrate_kbps =
01038 FindNearestBitrate(gfp->VBR_min_bitrate_kbps, gfp->version, gfp->out_samplerate);
01039 gfc->VBR_min_bitrate =
01040 BitrateIndex(gfp->VBR_min_bitrate_kbps, gfp->version, gfp->out_samplerate);
01041 if (gfc->VBR_min_bitrate < 0)
01042 return -1;
01043 }
01044 if (gfp->VBR_max_bitrate_kbps) {
01045 gfp->VBR_max_bitrate_kbps =
01046 FindNearestBitrate(gfp->VBR_max_bitrate_kbps, gfp->version, gfp->out_samplerate);
01047 gfc->VBR_max_bitrate =
01048 BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version, gfp->out_samplerate);
01049 if (gfc->VBR_max_bitrate < 0)
01050 return -1;
01051 }
01052 gfp->VBR_min_bitrate_kbps = bitrate_table[gfp->version][gfc->VBR_min_bitrate];
01053 gfp->VBR_max_bitrate_kbps = bitrate_table[gfp->version][gfc->VBR_max_bitrate];
01054 gfp->VBR_mean_bitrate_kbps =
01055 Min(bitrate_table[gfp->version][gfc->VBR_max_bitrate], gfp->VBR_mean_bitrate_kbps);
01056 gfp->VBR_mean_bitrate_kbps =
01057 Max(bitrate_table[gfp->version][gfc->VBR_min_bitrate], gfp->VBR_mean_bitrate_kbps);
01058 }
01059
01060
01061
01062 if (gfp->tune) {
01063 gfc->PSY->mask_adjust += gfp->tune_value_a;
01064 gfc->PSY->mask_adjust_short += gfp->tune_value_a;
01065 }
01066
01067
01068 lame_init_qval(gfp);
01069
01070
01071
01072
01073 if (gfp->athaa_type < 0)
01074 gfc->ATH->use_adjust = 3;
01075 else
01076 gfc->ATH->use_adjust = gfp->athaa_type;
01077
01078
01079
01080 gfc->ATH->aa_sensitivity_p = pow(10.0, gfp->athaa_sensitivity / -10.0);
01081
01082
01083 gfc->PSY->cwlimit = gfp->cwlimit <= 0 ? 8871.7f : gfp->cwlimit;
01084
01085 if (gfp->short_blocks == short_block_not_set) {
01086 gfp->short_blocks = short_block_allowed;
01087 }
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097 if (gfp->short_blocks == short_block_allowed
01098 && (gfp->mode == JOINT_STEREO || gfp->mode == STEREO)) {
01099 gfp->short_blocks = short_block_coupled;
01100 }
01101
01102
01103 if (lame_get_quant_comp(gfp) < 0)
01104 (void) lame_set_quant_comp(gfp, 1);
01105 if (lame_get_quant_comp_short(gfp) < 0)
01106 (void) lame_set_quant_comp_short(gfp, 0);
01107
01108 if (lame_get_msfix(gfp) < 0)
01109 lame_set_msfix(gfp, 0);
01110
01111
01112 if ((lame_get_psy_model(gfp) < 0) || (lame_get_psy_model(gfp) == PSY_NSPSYTUNE)) {
01113 (void) lame_set_psy_model(gfp, PSY_NSPSYTUNE);
01114 (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
01115 }
01116 else {
01117 (void) lame_set_psy_model(gfp, PSY_GPSYCHO);
01118 (void) lame_set_exp_nspsytune(gfp, (lame_get_exp_nspsytune(gfp) >> 1) << 1);
01119 }
01120
01121
01122 if (lame_get_short_threshold_lrm(gfp) < 0)
01123 (void) lame_set_short_threshold_lrm(gfp, NSATTACKTHRE);
01124 if (lame_get_short_threshold_s(gfp) < 0)
01125 (void) lame_set_short_threshold_s(gfp, NSATTACKTHRE_S);
01126
01127 if (gfp->scale < 0)
01128 gfp->scale = 1;
01129
01130 if (gfp->ATHtype < 0)
01131 gfp->ATHtype = 4;
01132
01133 if (gfp->ATHcurve < 0)
01134 gfp->ATHcurve = 4;
01135
01136 if (gfp->athaa_loudapprox < 0)
01137 gfp->athaa_loudapprox = 2;
01138
01139 if (gfp->interChRatio < 0)
01140 gfp->interChRatio = 0;
01141
01142 if (gfp->useTemporal < 0)
01143 gfp->useTemporal = 1;
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155 gfc->slot_lag = gfc->frac_SpF = 0;
01156 if (gfp->VBR == vbr_off)
01157 gfc->slot_lag = gfc->frac_SpF
01158 = ((gfp->version + 1) * 72000L * gfp->brate) % gfp->out_samplerate;
01159
01160 switch (gfp->quantization_type) {
01161 default:
01162 case 0:
01163
01164 break;
01165 case 1:
01166 gfc->quantization = 0;
01167 gfc->PSY->mask_adjust += 0.68125;
01168 gfc->PSY->mask_adjust_short += 0.68125;
01169 break;
01170 case 2:
01171 gfc->quantization = 1;
01172 break;
01173 }
01174
01175
01176 iteration_init(gfp);
01177 (void) psymodel_init(gfp);
01178
01179 return 0;
01180 }
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191 void
01192 lame_print_config(const lame_global_flags * gfp)
01193 {
01194 lame_internal_flags const *const gfc = gfp->internal_flags;
01195 double const out_samplerate = gfp->out_samplerate;
01196 double const in_samplerate = gfp->out_samplerate * gfc->resample_ratio;
01197
01198 MSGF(gfc, "LAME %s %s (%s)\n", get_lame_version(), get_lame_os_bitness(), get_lame_url());
01199
01200 if (LAME_ALPHA_VERSION)
01201 MSGF(gfc, "warning: alpha versions should be used for testing only\n");
01202
01203 if (gfc->CPU_features.MMX
01204 || gfc->CPU_features.AMD_3DNow || gfc->CPU_features.SSE || gfc->CPU_features.SSE2) {
01205 int fft_asm_used = 0;
01206 #ifdef HAVE_NASM
01207 if (gfc->CPU_features.AMD_3DNow) {
01208 fft_asm_used = 1;
01209 }
01210 else if (gfc->CPU_features.SSE) {
01211 fft_asm_used = 2;
01212 }
01213 else
01214 #endif
01215 {
01216 fft_asm_used = 0;
01217 }
01218 MSGF(gfc, "CPU features: ");
01219
01220 if (gfc->CPU_features.MMX) {
01221 #ifdef MMX_choose_table
01222 MSGF(gfc, "MMX (ASM used)");
01223 #else
01224 MSGF(gfc, "MMX");
01225 #endif
01226 }
01227 if (gfc->CPU_features.AMD_3DNow) {
01228 if (fft_asm_used == 1) {
01229 MSGF(gfc, ", 3DNow! (ASM used)");
01230 }
01231 else {
01232 MSGF(gfc, ", 3DNow!");
01233 }
01234 }
01235 if (gfc->CPU_features.SSE) {
01236 #if defined(HAVE_XMMINTRIN_H)
01237 MSGF(gfc, ", SSE (ASM used)");
01238 #else
01239 if (fft_asm_used == 2) {
01240 MSGF(gfc, ", SSE (ASM used)");
01241 }
01242 else {
01243 MSGF(gfc, ", SSE");
01244 }
01245 #endif
01246 }
01247 if (gfc->CPU_features.SSE2) {
01248 MSGF(gfc, ", SSE2");
01249 }
01250 MSGF(gfc, "\n");
01251 }
01252
01253 if (gfp->num_channels == 2 && gfc->channels_out == 1 ) {
01254 MSGF(gfc, "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
01255 }
01256
01257 if (gfc->resample_ratio != 1.) {
01258 MSGF(gfc, "Resampling: input %g kHz output %g kHz\n",
01259 1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
01260 }
01261
01262 if (gfc->filter_type == 0) {
01263 if (gfc->highpass2 > 0.)
01264 MSGF(gfc,
01265 "Using polyphase highpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
01266 0.5 * gfc->highpass1 * out_samplerate, 0.5 * gfc->highpass2 * out_samplerate);
01267 if (0. < gfc->lowpass1 && gfc->lowpass1 < 1.) {
01268 MSGF(gfc,
01269 "Using polyphase lowpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
01270 0.5 * gfc->lowpass1 * out_samplerate, 0.5 * gfc->lowpass2 * out_samplerate);
01271 }
01272 else {
01273 MSGF(gfc, "polyphase lowpass filter disabled\n");
01274 }
01275 }
01276 else {
01277 MSGF(gfc, "polyphase filters disabled\n");
01278 }
01279
01280 if (gfp->free_format) {
01281 MSGF(gfc, "Warning: many decoders cannot handle free format bitstreams\n");
01282 if (gfp->brate > 320) {
01283 MSGF(gfc,
01284 "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
01285 }
01286 }
01287 }
01288
01289
01295 void
01296 lame_print_internals(const lame_global_flags * gfp)
01297 {
01298 lame_internal_flags const *const gfc = gfp->internal_flags;
01299 const char *pc = "";
01300
01301
01302
01303 MSGF(gfc, "\nmisc:\n\n");
01304
01305 MSGF(gfc, "\tscaling: %g\n", gfp->scale);
01306 MSGF(gfc, "\tch0 (left) scaling: %g\n", gfp->scale_left);
01307 MSGF(gfc, "\tch1 (right) scaling: %g\n", gfp->scale_right);
01308 MSGF(gfc, "\tfilter type: %d\n", gfc->filter_type);
01309 pc = gfc->quantization ? "xr^3/4" : "ISO";
01310 MSGF(gfc, "\tquantization: %s\n", pc);
01311 switch (gfc->use_best_huffman) {
01312 default:
01313 pc = "normal";
01314 break;
01315 case 1:
01316 pc = "best (outside loop)";
01317 break;
01318 case 2:
01319 pc = "best (inside loop, slow)";
01320 break;
01321 }
01322 MSGF(gfc, "\thuffman search: %s\n", pc);
01323 MSGF(gfc, "\texperimental Y=%d\n", gfp->experimentalY);
01324 MSGF(gfc, "\t...\n");
01325
01326
01327
01328 MSGF(gfc, "\nstream format:\n\n");
01329 switch (gfp->version) {
01330 case 0:
01331 pc = "2.5";
01332 break;
01333 case 1:
01334 pc = "1";
01335 break;
01336 case 2:
01337 pc = "2";
01338 break;
01339 default:
01340 pc = "?";
01341 break;
01342 }
01343 MSGF(gfc, "\tMPEG-%s Layer 3\n", pc);
01344 switch (gfp->mode) {
01345 case JOINT_STEREO:
01346 pc = "joint stereo";
01347 break;
01348 case STEREO:
01349 pc = "stereo";
01350 break;
01351 case DUAL_CHANNEL:
01352 pc = "dual channel";
01353 break;
01354 case MONO:
01355 pc = "mono";
01356 break;
01357 case NOT_SET:
01358 pc = "not set (error)";
01359 break;
01360 default:
01361 pc = "unknown (error)";
01362 break;
01363 }
01364 MSGF(gfc, "\t%d channel - %s\n", gfc->channels_out, pc);
01365
01366 switch (gfp->VBR) {
01367 case vbr_off:
01368 pc = "off";
01369 break;
01370 default:
01371 pc = "all";
01372 break;
01373 }
01374 MSGF(gfc, "\tpadding: %s\n", pc);
01375
01376 if (vbr_default == gfp->VBR)
01377 pc = "(default)";
01378 else if (gfp->free_format)
01379 pc = "(free format)";
01380 else
01381 pc = "";
01382 switch (gfp->VBR) {
01383 case vbr_off:
01384 MSGF(gfc, "\tconstant bitrate - CBR %s\n", pc);
01385 break;
01386 case vbr_abr:
01387 MSGF(gfc, "\tvariable bitrate - ABR %s\n", pc);
01388 break;
01389 case vbr_rh:
01390 MSGF(gfc, "\tvariable bitrate - VBR rh %s\n", pc);
01391 break;
01392 case vbr_mt:
01393 MSGF(gfc, "\tvariable bitrate - VBR mt %s\n", pc);
01394 break;
01395 case vbr_mtrh:
01396 MSGF(gfc, "\tvariable bitrate - VBR mtrh %s\n", pc);
01397 break;
01398 default:
01399 MSGF(gfc, "\t ?? oops, some new one ?? \n");
01400 break;
01401 }
01402 if (gfp->bWriteVbrTag)
01403 MSGF(gfc, "\tusing LAME Tag\n");
01404 MSGF(gfc, "\t...\n");
01405
01406
01407
01408 MSGF(gfc, "\npsychoacoustic:\n\n");
01409
01410 MSGF(gfc, "\tusing psychoacoustic model: %d\n", gfc->psymodel);
01411 MSGF(gfc, "\tpsychoacoustic model: %s\n",
01412 (gfp->psymodel == PSY_NSPSYTUNE) ? "NSPsytune" : "GPsycho");
01413 MSGF(gfc, "\ttonality estimation limit: %f Hz %s\n", gfc->PSY->cwlimit,
01414 (gfp->psymodel == PSY_NSPSYTUNE) ? "(not relevant)" : "");
01415 switch (gfp->short_blocks) {
01416 default:
01417 case short_block_not_set:
01418 pc = "?";
01419 break;
01420 case short_block_allowed:
01421 pc = "allowed";
01422 break;
01423 case short_block_coupled:
01424 pc = "channel coupled";
01425 break;
01426 case short_block_dispensed:
01427 pc = "dispensed";
01428 break;
01429 case short_block_forced:
01430 pc = "forced";
01431 break;
01432 }
01433 MSGF(gfc, "\tusing short blocks: %s\n", pc);
01434 MSGF(gfc, "\tsubblock gain: %d\n", gfc->subblock_gain);
01435 MSGF(gfc, "\tadjust masking: %g dB\n", gfc->PSY->mask_adjust);
01436 MSGF(gfc, "\tadjust masking short: %g dB\n", gfc->PSY->mask_adjust_short);
01437 MSGF(gfc, "\tquantization comparison: %d\n", gfp->quant_comp);
01438 MSGF(gfc, "\t ^ comparison short blocks: %d\n", gfp->quant_comp_short);
01439 MSGF(gfc, "\tnoise shaping: %d\n", gfc->noise_shaping);
01440 MSGF(gfc, "\t ^ amplification: %d\n", gfc->noise_shaping_amp);
01441 MSGF(gfc, "\t ^ stopping: %d\n", gfc->noise_shaping_stop);
01442
01443 pc = "using";
01444 if (gfp->ATHshort)
01445 pc = "the only masking for short blocks";
01446 if (gfp->ATHonly)
01447 pc = "the only masking";
01448 if (gfp->noATH)
01449 pc = "not used";
01450 MSGF(gfc, "\tATH: %s\n", pc);
01451 MSGF(gfc, "\t ^ type: %d\n", gfp->ATHtype);
01452 MSGF(gfc, "\t ^ shape: %g%s\n", gfp->ATHcurve, " (only for type 4)");
01453 MSGF(gfc, "\t ^ level adjustement: %g\n", gfp->ATHlower);
01454 MSGF(gfc, "\t ^ adjust type: %d\n", gfc->ATH->use_adjust);
01455 MSGF(gfc, "\t ^ adjust sensitivity power: %f\n", gfc->ATH->aa_sensitivity_p);
01456 MSGF(gfc, "\t ^ adapt threshold type: %d\n", gfp->athaa_loudapprox);
01457
01458 if (gfp->psymodel == PSY_NSPSYTUNE) {
01459 MSGF(gfc, "\texperimental psy tunings by Naoki Shibata\n");
01460 MSGF(gfc, "\t adjust masking bass=%g dB, alto=%g dB, treble=%g dB, sfb21=%g dB\n",
01461 10 * log10(gfc->nsPsy.longfact[0]),
01462 10 * log10(gfc->nsPsy.longfact[7]),
01463 10 * log10(gfc->nsPsy.longfact[14]), 10 * log10(gfc->nsPsy.longfact[21]));
01464 }
01465 pc = gfp->useTemporal ? "yes" : "no";
01466 MSGF(gfc, "\tusing temporal masking effect: %s\n", pc);
01467 MSGF(gfc, "\tinterchannel masking ratio: %g\n", gfp->interChRatio);
01468 MSGF(gfc, "\t...\n");
01469
01470
01471
01472 MSGF(gfc, "\n");
01473 return;
01474 }
01475
01476
01477
01478
01479
01480
01481
01482 int
01483 lame_encode_frame(lame_global_flags * gfp,
01484 sample_t inbuf_l[], sample_t inbuf_r[], unsigned char *mp3buf, int mp3buf_size)
01485 {
01486 int ret;
01487 ret = lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
01488 gfp->frameNum++;
01489 return ret;
01490 }
01491
01492 static int
01493 update_inbuffer_size(lame_internal_flags * gfc, const int nsamples)
01494 {
01495 if (gfc->in_buffer_0 == 0 || gfc->in_buffer_nsamples < nsamples) {
01496 if (gfc->in_buffer_0) {
01497 free(gfc->in_buffer_0);
01498 }
01499 if (gfc->in_buffer_1) {
01500 free(gfc->in_buffer_1);
01501 }
01502 gfc->in_buffer_0 = calloc(sizeof(sample_t), nsamples);
01503 gfc->in_buffer_1 = calloc(sizeof(sample_t), nsamples);
01504 gfc->in_buffer_nsamples = nsamples;
01505 }
01506 if (gfc->in_buffer_0 == NULL || gfc->in_buffer_1 == NULL) {
01507 if (gfc->in_buffer_0) {
01508 free(gfc->in_buffer_0);
01509 }
01510 if (gfc->in_buffer_1) {
01511 free(gfc->in_buffer_1);
01512 }
01513 gfc->in_buffer_0 = 0;
01514 gfc->in_buffer_1 = 0;
01515 gfc->in_buffer_nsamples = 0;
01516 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
01517 return -2;
01518 }
01519 return 0;
01520 }
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543 int
01544 lame_encode_buffer_sample_t(lame_global_flags * gfp,
01545 sample_t buffer_l[],
01546 sample_t buffer_r[],
01547 int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01548 {
01549 lame_internal_flags *const gfc = gfp->internal_flags;
01550 int mp3size = 0, ret, i, ch, mf_needed;
01551 int mp3out;
01552 sample_t *mfbuf[2];
01553 sample_t *in_buffer[2];
01554
01555 if (gfc->Class_ID != LAME_ID)
01556 return -3;
01557
01558 if (nsamples == 0)
01559 return 0;
01560
01561
01562 mp3out = copy_buffer(gfc, mp3buf, mp3buf_size, 0);
01563 if (mp3out < 0)
01564 return mp3out;
01565 mp3buf += mp3out;
01566 mp3size += mp3out;
01567
01568
01569 in_buffer[0] = buffer_l;
01570 in_buffer[1] = buffer_r;
01571
01572
01573
01574
01575
01576 if (gfp->scale != 0 && gfp->scale != 1.0) {
01577 for (i = 0; i < nsamples; ++i) {
01578 in_buffer[0][i] *= gfp->scale;
01579 if (gfc->channels_out == 2)
01580 in_buffer[1][i] *= gfp->scale;
01581 }
01582 }
01583
01584
01585 if (gfp->scale_left != 0 && gfp->scale_left != 1.0) {
01586 for (i = 0; i < nsamples; ++i) {
01587 in_buffer[0][i] *= gfp->scale_left;
01588 }
01589 }
01590
01591
01592 if (gfp->scale_right != 0 && gfp->scale_right != 1.0) {
01593 for (i = 0; i < nsamples; ++i) {
01594 in_buffer[1][i] *= gfp->scale_right;
01595 }
01596 }
01597
01598
01599 if (gfp->num_channels == 2 && gfc->channels_out == 1) {
01600 for (i = 0; i < nsamples; ++i) {
01601 in_buffer[0][i] = 0.5 * ((FLOAT) in_buffer[0][i] + in_buffer[1][i]);
01602 in_buffer[1][i] = 0.0;
01603 }
01604 }
01605
01606
01607
01608 #if ENCDELAY < MDCTDELAY
01609 # error ENCDELAY is less than MDCTDELAY, see encoder.h
01610 #endif
01611 #if FFTOFFSET > BLKSIZE
01612 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
01613 #endif
01614
01615 mf_needed = BLKSIZE + gfp->framesize - FFTOFFSET;
01616
01617 mf_needed = Max(mf_needed, 512 + gfp->framesize - 32);
01618
01619 assert(MFSIZE >= mf_needed);
01620
01621 mfbuf[0] = gfc->mfbuf[0];
01622 mfbuf[1] = gfc->mfbuf[1];
01623
01624 while (nsamples > 0) {
01625 int n_in = 0;
01626 int n_out = 0;
01627
01628
01629
01630 fill_buffer(gfp, mfbuf, in_buffer, nsamples, &n_in, &n_out);
01631
01632
01633 if (gfc->findReplayGain && !gfc->decode_on_the_fly)
01634 if (AnalyzeSamples
01635 (gfc->rgdata, &mfbuf[0][gfc->mf_size], &mfbuf[1][gfc->mf_size], n_out,
01636 gfc->channels_out) == GAIN_ANALYSIS_ERROR)
01637 return -6;
01638
01639
01640
01641
01642 nsamples -= n_in;
01643 in_buffer[0] += n_in;
01644 if (gfc->channels_out == 2)
01645 in_buffer[1] += n_in;
01646
01647
01648 gfc->mf_size += n_out;
01649 assert(gfc->mf_size <= MFSIZE);
01650 gfc->mf_samples_to_encode += n_out;
01651
01652
01653 if (gfc->mf_size >= mf_needed) {
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663 int buf_size = mp3buf_size - mp3size;
01664 if (mp3buf_size == 0)
01665 buf_size = 0;
01666
01667 ret = lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf, buf_size);
01668
01669 if (ret < 0)
01670 return ret;
01671 mp3buf += ret;
01672 mp3size += ret;
01673
01674
01675 gfc->mf_size -= gfp->framesize;
01676 gfc->mf_samples_to_encode -= gfp->framesize;
01677 for (ch = 0; ch < gfc->channels_out; ch++)
01678 for (i = 0; i < gfc->mf_size; i++)
01679 mfbuf[ch][i] = mfbuf[ch][i + gfp->framesize];
01680 }
01681 }
01682 assert(nsamples == 0);
01683
01684 return mp3size;
01685 }
01686
01687
01688 int
01689 lame_encode_buffer(lame_global_flags * gfp,
01690 const short int buffer_l[],
01691 const short int buffer_r[],
01692 const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01693 {
01694 lame_internal_flags *const gfc = gfp->internal_flags;
01695 int i;
01696 sample_t *in_buffer[2];
01697
01698 if (gfc->Class_ID != LAME_ID)
01699 return -3;
01700
01701 if (nsamples == 0)
01702 return 0;
01703
01704 if (update_inbuffer_size(gfc, nsamples) != 0) {
01705 return -2;
01706 }
01707
01708 in_buffer[0] = gfc->in_buffer_0;
01709 in_buffer[1] = gfc->in_buffer_1;
01710
01711
01712 for (i = 0; i < nsamples; i++) {
01713 in_buffer[0][i] = buffer_l[i];
01714 if (gfc->channels_in > 1)
01715 in_buffer[1][i] = buffer_r[i];
01716 }
01717
01718 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
01719 nsamples, mp3buf, mp3buf_size);
01720 }
01721
01722
01723 int
01724 lame_encode_buffer_float(lame_global_flags * gfp,
01725 const float buffer_l[],
01726 const float buffer_r[],
01727 const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01728 {
01729 lame_internal_flags *const gfc = gfp->internal_flags;
01730 int i;
01731 sample_t *in_buffer[2];
01732
01733 if (gfc->Class_ID != LAME_ID)
01734 return -3;
01735
01736 if (nsamples == 0)
01737 return 0;
01738
01739 if (update_inbuffer_size(gfc, nsamples) != 0) {
01740 return -2;
01741 }
01742
01743 in_buffer[0] = gfc->in_buffer_0;
01744 in_buffer[1] = gfc->in_buffer_1;
01745
01746
01747 for (i = 0; i < nsamples; i++) {
01748 in_buffer[0][i] = buffer_l[i];
01749 if (gfc->channels_in > 1)
01750 in_buffer[1][i] = buffer_r[i];
01751 }
01752
01753 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
01754 nsamples, mp3buf, mp3buf_size);
01755 }
01756
01757
01758 int
01759 lame_encode_buffer_int(lame_global_flags * gfp,
01760 const int buffer_l[],
01761 const int buffer_r[],
01762 const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01763 {
01764 lame_internal_flags *const gfc = gfp->internal_flags;
01765 int i;
01766 sample_t *in_buffer[2];
01767
01768 if (gfc->Class_ID != LAME_ID)
01769 return -3;
01770
01771 if (nsamples == 0)
01772 return 0;
01773
01774 if (update_inbuffer_size(gfc, nsamples) != 0) {
01775 return -2;
01776 }
01777
01778 in_buffer[0] = gfc->in_buffer_0;
01779 in_buffer[1] = gfc->in_buffer_1;
01780
01781
01782 for (i = 0; i < nsamples; i++) {
01783
01784 in_buffer[0][i] = buffer_l[i] * (1.0 / (1L << (8 * sizeof(int) - 16)));
01785 if (gfc->channels_in > 1)
01786 in_buffer[1][i] = buffer_r[i] * (1.0 / (1L << (8 * sizeof(int) - 16)));
01787 }
01788
01789 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
01790 nsamples, mp3buf, mp3buf_size);
01791 }
01792
01793
01794
01795
01796 int
01797 lame_encode_buffer_long2(lame_global_flags * gfp,
01798 const long buffer_l[],
01799 const long buffer_r[],
01800 const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01801 {
01802 lame_internal_flags *const gfc = gfp->internal_flags;
01803 int i;
01804 sample_t *in_buffer[2];
01805
01806 if (gfc->Class_ID != LAME_ID)
01807 return -3;
01808
01809 if (nsamples == 0)
01810 return 0;
01811
01812 if (update_inbuffer_size(gfc, nsamples) != 0) {
01813 return -2;
01814 }
01815
01816 in_buffer[0] = gfc->in_buffer_0;
01817 in_buffer[1] = gfc->in_buffer_1;
01818
01819
01820 for (i = 0; i < nsamples; i++) {
01821
01822 in_buffer[0][i] = buffer_l[i] * (1.0 / (1L << (8 * sizeof(long) - 16)));
01823 if (gfc->channels_in > 1)
01824 in_buffer[1][i] = buffer_r[i] * (1.0 / (1L << (8 * sizeof(long) - 16)));
01825 }
01826
01827 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
01828 nsamples, mp3buf, mp3buf_size);
01829
01830 }
01831
01832
01833
01834 int
01835 lame_encode_buffer_long(lame_global_flags * gfp,
01836 const long buffer_l[],
01837 const long buffer_r[],
01838 const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
01839 {
01840 lame_internal_flags *const gfc = gfp->internal_flags;
01841 int i;
01842 sample_t *in_buffer[2];
01843
01844 if (gfc->Class_ID != LAME_ID)
01845 return -3;
01846
01847 if (nsamples == 0)
01848 return 0;
01849
01850 if (update_inbuffer_size(gfc, nsamples) != 0) {
01851 return -2;
01852 }
01853
01854 in_buffer[0] = gfc->in_buffer_0;
01855 in_buffer[1] = gfc->in_buffer_1;
01856
01857
01858 for (i = 0; i < nsamples; i++) {
01859 in_buffer[0][i] = buffer_l[i];
01860 if (gfc->channels_in > 1)
01861 in_buffer[1][i] = buffer_r[i];
01862 }
01863
01864 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
01865 nsamples, mp3buf, mp3buf_size);
01866 }
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878 int
01879 lame_encode_buffer_interleaved(lame_global_flags * gfp,
01880 short int buffer[],
01881 int nsamples, unsigned char *mp3buf, int mp3buf_size)
01882 {
01883 lame_internal_flags *const gfc = gfp->internal_flags;
01884 int i;
01885 sample_t *in_buffer[2];
01886
01887 if (update_inbuffer_size(gfc, nsamples) != 0) {
01888 return -2;
01889 }
01890
01891 in_buffer[0] = gfc->in_buffer_0;
01892 in_buffer[1] = gfc->in_buffer_1;
01893
01894 for (i = 0; i < nsamples; i++) {
01895 in_buffer[0][i] = buffer[2 * i];
01896 in_buffer[1][i] = buffer[2 * i + 1];
01897 }
01898 return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1], nsamples, mp3buf,
01899 mp3buf_size);
01900 }
01901
01902
01903 int
01904 lame_encode(lame_global_flags * const gfp,
01905 const short int in_buffer[2][1152], unsigned char *const mp3buf, const int size)
01906 {
01907 lame_internal_flags const *const gfc = gfp->internal_flags;
01908
01909 if (gfc->Class_ID != LAME_ID)
01910 return -3;
01911
01912 return lame_encode_buffer(gfp, in_buffer[0], in_buffer[1], gfp->framesize, mp3buf, size);
01913 }
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924 int
01925 lame_encode_flush_nogap(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
01926 {
01927 lame_internal_flags *const gfc = gfp->internal_flags;
01928 flush_bitstream(gfp);
01929 return copy_buffer(gfc, mp3buffer, mp3buffer_size, 1);
01930 }
01931
01932
01933
01934
01935 int
01936 lame_init_bitstream(lame_global_flags * gfp)
01937 {
01938 lame_internal_flags *const gfc = gfp->internal_flags;
01939 gfp->frameNum = 0;
01940
01941 (void) id3tag_write_v2(gfp);
01942
01943
01944 memset(gfc->bitrate_stereoMode_Hist, 0, sizeof(gfc->bitrate_stereoMode_Hist));
01945 memset(gfc->bitrate_blockType_Hist, 0, sizeof(gfc->bitrate_blockType_Hist));
01946
01947 gfc->PeakSample = 0.0;
01948
01949
01950 if (gfp->bWriteVbrTag)
01951 (void) InitVbrTag(gfp);
01952
01953
01954 return 0;
01955 }
01956
01957
01958
01959
01960
01961
01962
01963 int
01964 lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
01965 {
01966 lame_internal_flags *const gfc = gfp->internal_flags;
01967 short int buffer[2][1152];
01968 int imp3 = 0, mp3count, mp3buffer_size_remaining;
01969
01970
01971
01972 int end_padding = POSTDELAY;
01973
01974 memset(buffer, 0, sizeof(buffer));
01975 mp3count = 0;
01976
01977
01978 while (gfc->mf_samples_to_encode > 0) {
01979
01980 mp3buffer_size_remaining = mp3buffer_size - mp3count;
01981
01982
01983 if (mp3buffer_size == 0)
01984 mp3buffer_size_remaining = 0;
01985
01986
01987
01988
01989 imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], gfp->framesize,
01990 mp3buffer, mp3buffer_size_remaining);
01991
01992
01993 gfc->mf_samples_to_encode -= gfp->framesize;
01994 if (gfc->mf_samples_to_encode < 0) {
01995
01996 end_padding += -gfc->mf_samples_to_encode;
01997 }
01998
01999
02000 if (imp3 < 0) {
02001
02002 return imp3;
02003 }
02004 mp3buffer += imp3;
02005 mp3count += imp3;
02006 }
02007
02008 mp3buffer_size_remaining = mp3buffer_size - mp3count;
02009
02010 if (mp3buffer_size == 0)
02011 mp3buffer_size_remaining = 0;
02012
02013
02014 flush_bitstream(gfp);
02015 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 1);
02016 if (imp3 < 0) {
02017
02018 return imp3;
02019 }
02020 mp3buffer += imp3;
02021 mp3count += imp3;
02022 mp3buffer_size_remaining = mp3buffer_size - mp3count;
02023
02024 if (mp3buffer_size == 0)
02025 mp3buffer_size_remaining = 0;
02026
02027
02028 (void) id3tag_write_v1(gfp);
02029 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 0);
02030
02031 if (imp3 < 0) {
02032 return imp3;
02033 }
02034 mp3count += imp3;
02035 gfp->encoder_padding = end_padding;
02036 return mp3count;
02037 }
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047 int
02048 lame_close(lame_global_flags * gfp)
02049 {
02050 lame_internal_flags *const gfc = gfp->internal_flags;
02051
02052 if (NULL == gfc || gfc->Class_ID != LAME_ID)
02053 return -3;
02054
02055 gfc->Class_ID = 0;
02056
02057
02058 freegfc(gfc);
02059
02060 gfp->internal_flags = NULL;
02061
02062 if (gfp->lame_allocated_gfp) {
02063 gfp->lame_allocated_gfp = 0;
02064 free(gfp);
02065 }
02066
02067 return 0;
02068 }
02069
02070
02071
02072
02073
02074 int
02075 lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
02076 {
02077 int const ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
02078
02079 (void) lame_close(gfp);
02080
02081 return ret;
02082 }
02083
02084
02085
02086
02087 void
02088 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
02089 {
02090 if (gfp->bWriteVbrTag) {
02091
02092 if (fpStream && !fseek(fpStream, 0, SEEK_SET)) {
02093 lame_internal_flags *gfc = gfp->internal_flags;
02094 int rc = PutVbrTag(gfp, fpStream);
02095 switch (rc) {
02096 default:
02097
02098 break;
02099
02100 case -1:
02101 ERRORF(gfc, "Error: could not update LAME tag.\n");
02102 break;
02103
02104 case -2:
02105 ERRORF(gfc, "Error: could not update LAME tag, file not seekable.\n");
02106 break;
02107
02108 case -3:
02109 ERRORF(gfc, "Error: could not update LAME tag, file not readable.\n");
02110 break;
02111 }
02112 }
02113 }
02114 }
02115
02116 int lame_init_old(lame_global_flags * gfp);
02117
02118 lame_global_flags *
02119 lame_init(void)
02120 {
02121 lame_global_flags *gfp;
02122 int ret;
02123
02124 init_log_table();
02125
02126 gfp = calloc(1, sizeof(lame_global_flags));
02127 if (gfp == NULL)
02128 return NULL;
02129
02130 ret = lame_init_old(gfp);
02131 if (ret != 0) {
02132 free(gfp);
02133 return NULL;
02134 }
02135
02136 gfp->lame_allocated_gfp = 1;
02137 return gfp;
02138 }
02139
02140
02141 int
02142 lame_init_old(lame_global_flags * gfp)
02143 {
02144 lame_internal_flags *gfc;
02145
02146 disable_FPE();
02147
02148 memset(gfp, 0, sizeof(lame_global_flags));
02149
02150 if (NULL == (gfc = gfp->internal_flags = calloc(1, sizeof(lame_internal_flags))))
02151 return -1;
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161 gfp->mode = NOT_SET;
02162 gfp->original = 1;
02163 gfp->in_samplerate = 44100;
02164 gfp->num_channels = 2;
02165 gfp->num_samples = MAX_U_32_NUM;
02166
02167 gfp->bWriteVbrTag = 1;
02168 gfp->quality = -1;
02169 gfp->short_blocks = short_block_not_set;
02170 gfc->subblock_gain = -1;
02171
02172 gfp->lowpassfreq = 0;
02173 gfp->highpassfreq = 0;
02174 gfp->lowpasswidth = -1;
02175 gfp->highpasswidth = -1;
02176
02177 gfp->VBR = vbr_off;
02178 gfp->VBR_q = 4;
02179 gfp->ATHcurve = -1;
02180 gfp->VBR_mean_bitrate_kbps = 128;
02181 gfp->VBR_min_bitrate_kbps = 0;
02182 gfp->VBR_max_bitrate_kbps = 0;
02183 gfp->VBR_hard_min = 0;
02184 gfc->VBR_min_bitrate = 1;
02185 gfc->VBR_max_bitrate = 13;
02186
02187 gfp->quant_comp = -1;
02188 gfp->quant_comp_short = -1;
02189
02190 gfp->msfix = -1;
02191
02192 gfc->resample_ratio = 1;
02193
02194 gfc->OldValue[0] = 180;
02195 gfc->OldValue[1] = 180;
02196 gfc->CurrentStep[0] = 4;
02197 gfc->CurrentStep[1] = 4;
02198 gfc->masking_lower = 1;
02199 gfc->nsPsy.attackthre = -1;
02200 gfc->nsPsy.attackthre_s = -1;
02201
02202 gfp->scale = -1;
02203
02204 gfp->athaa_type = -1;
02205 gfp->ATHtype = -1;
02206 gfp->athaa_loudapprox = -1;
02207
02208 gfp->athaa_sensitivity = 0.0;
02209 gfp->useTemporal = -1;
02210 gfp->interChRatio = -1;
02211
02212
02213
02214
02215
02216
02217
02218
02219
02220
02221
02222 gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
02223 gfp->encoder_padding = 0;
02224 gfc->mf_size = ENCDELAY - MDCTDELAY;
02225
02226 gfp->findReplayGain = 0;
02227 gfp->decode_on_the_fly = 0;
02228
02229 gfc->decode_on_the_fly = 0;
02230 gfc->findReplayGain = 0;
02231 gfc->findPeakSample = 0;
02232
02233 gfc->RadioGain = 0;
02234 gfc->AudiophileGain = 0;
02235 gfc->noclipGainChange = 0;
02236 gfc->noclipScale = -1.0;
02237
02238 gfp->asm_optimizations.mmx = 1;
02239 gfp->asm_optimizations.amd3dnow = 1;
02240 gfp->asm_optimizations.sse = 1;
02241
02242 gfp->preset = 0;
02243
02244 gfp->psymodel = -1;
02245
02246 return 0;
02247 }
02248
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271
02272
02273
02274
02275 void
02276 lame_bitrate_kbps(const lame_global_flags * const gfp, int bitrate_kbps[14])
02277 {
02278 const lame_internal_flags *gfc;
02279 int i;
02280
02281 if (NULL == bitrate_kbps)
02282 return;
02283 if (NULL == gfp)
02284 return;
02285 gfc = gfp->internal_flags;
02286 if (NULL == gfc)
02287 return;
02288
02289 for (i = 0; i < 14; i++)
02290 bitrate_kbps[i] = bitrate_table[gfp->version][i + 1];
02291 }
02292
02293
02294 void
02295 lame_bitrate_hist(const lame_global_flags * const gfp, int bitrate_count[14])
02296 {
02297 const lame_internal_flags *gfc;
02298 int i;
02299
02300 if (NULL == bitrate_count)
02301 return;
02302 if (NULL == gfp)
02303 return;
02304 gfc = gfp->internal_flags;
02305 if (NULL == gfc)
02306 return;
02307
02308 for (i = 0; i < 14; i++)
02309 bitrate_count[i] = gfc->bitrate_stereoMode_Hist[i + 1][4];
02310 }
02311
02312
02313 void
02314 lame_stereo_mode_hist(const lame_global_flags * const gfp, int stmode_count[4])
02315 {
02316 const lame_internal_flags *gfc;
02317 int i;
02318
02319 if (NULL == stmode_count)
02320 return;
02321 if (NULL == gfp)
02322 return;
02323 gfc = gfp->internal_flags;
02324 if (NULL == gfc)
02325 return;
02326
02327 for (i = 0; i < 4; i++) {
02328 stmode_count[i] = gfc->bitrate_stereoMode_Hist[15][i];
02329 }
02330 }
02331
02332
02333
02334 void
02335 lame_bitrate_stereo_mode_hist(const lame_global_flags * const gfp, int bitrate_stmode_count[14][4])
02336 {
02337 const lame_internal_flags *gfc;
02338 int i;
02339 int j;
02340
02341 if (NULL == bitrate_stmode_count)
02342 return;
02343 if (NULL == gfp)
02344 return;
02345 gfc = gfp->internal_flags;
02346 if (NULL == gfc)
02347 return;
02348
02349 for (j = 0; j < 14; j++)
02350 for (i = 0; i < 4; i++)
02351 bitrate_stmode_count[j][i] = gfc->bitrate_stereoMode_Hist[j + 1][i];
02352 }
02353
02354
02355 void
02356 lame_block_type_hist(const lame_global_flags * const gfp, int btype_count[6])
02357 {
02358 const lame_internal_flags *gfc;
02359 int i;
02360
02361 if (NULL == btype_count)
02362 return;
02363 if (NULL == gfp)
02364 return;
02365 gfc = gfp->internal_flags;
02366 if (NULL == gfc)
02367 return;
02368
02369 for (i = 0; i < 6; ++i) {
02370 btype_count[i] = gfc->bitrate_blockType_Hist[15][i];
02371 }
02372 }
02373
02374
02375
02376 void
02377 lame_bitrate_block_type_hist(const lame_global_flags * const gfp, int bitrate_btype_count[14][6])
02378 {
02379 const lame_internal_flags *gfc;
02380 int i, j;
02381
02382 if (NULL == bitrate_btype_count)
02383 return;
02384 if (NULL == gfp)
02385 return;
02386 gfc = gfp->internal_flags;
02387 if (NULL == gfc)
02388 return;
02389
02390 for (j = 0; j < 14; ++j)
02391 for (i = 0; i < 6; ++i)
02392 bitrate_btype_count[j][i] = gfc->bitrate_blockType_Hist[j + 1][i];
02393 }
02394
02395
02396