lame.c

Go to the documentation of this file.
00001 /* -*- mode: C; mode: fold -*- */
00002 /*
00003  *      LAME MP3 encoding engine
00004  *
00005  *      Copyright (c) 1999-2000 Mark Taylor
00006  *      Copyright (c) 2000-2005 Takehiro Tominaga
00007  *      Copyright (c) 2000-2005 Robert Hegemann
00008  *      Copyright (c) 2000-2005 Gabriel Bouvigne
00009  *      Copyright (c) 2000-2004 Alexander Leidinger
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Library General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00024  * Boston, MA 02111-1307, USA.
00025  */
00026 
00027 /* $Id: lame.c,v 1.310 2007/07/24 17:46:10 bouvigne Exp $ */
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 /* woraround for SunOS 4.x, it has SEEK_* defined here */
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     /* compute info needed for polyphase filter (filter type==0, default) */
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             /* this band and above will be zeroed: */
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         /* compute the *actual* transition band implemented by
00103          * the polyphase filter */
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     /* make sure highpass filter is within 90% of what the effective
00114      * highpass frequency will be */
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             /* this band and below will be zereod */
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         /* compute the *actual* transition band implemented by
00136          * the polyphase filter */
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  *  Input:
00162  *      bitrate     total bitrate in kbps
00163  *
00164  *   Output:
00165  *      lowerlimit: best lowpass frequency limit for input filter in Hz
00166  *      upperlimit: best highpass frequency limit for input filter in Hz
00167  */
00168     int     index;
00169 
00170     typedef struct {
00171         int     bitrate;     /* only indicative value */
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  *  Now we try to choose a good high pass filtering frequency.
00203  *  This value is currently not used.
00204  *    For fu < 16 kHz:  sqrt(fu*fl) = 560 Hz
00205  *    For fu = 18 kHz:  no high pass filtering
00206  *  This gives:
00207  *
00208  *   2 kHz => 160 Hz
00209  *   3 kHz => 107 Hz
00210  *   4 kHz =>  80 Hz
00211  *   8 kHz =>  40 Hz
00212  *  16 kHz =>  20 Hz
00213  *  17 kHz =>  10 Hz
00214  *  18 kHz =>   0 Hz
00215  *
00216  *  These are ad hoc values and these can be optimized if a high pass is available.
00217  */
00218 /*    if (f_low <= 16000)
00219         f_high = 16000. * 20. / f_low;
00220     else if (f_low <= 18000)
00221         f_high = 180. - 0.01 * f_low;
00222     else
00223         f_high = 0.;*/
00224 
00225     /*
00226      *  When we sometimes have a good highpass filter, we can add the highpass
00227      *  frequency to the lowpass frequency
00228      */
00229 
00230     /*if (upperlimit != NULL)
00231      *upperlimit = f_high;*/
00232 }
00233 
00234 
00235 static int
00236 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
00237 {
00238 /*
00239  * Rules:
00240  *  - if possible, sfb21 should NOT be used
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         /*  choose a valid MPEG sample frequency above the input sample frequency
00286             to avoid SFB21/12 bitrate bloat
00287             rh 061115
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 /* set internal feature flags.  USER should not access these since
00323  * some combinations will produce strange results */
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:            /* no psymodel, no noise shaping */
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         /*lint --fallthrough */
00345     case 7:            /* use psymodel (for short block and m/s switching), but no noise shapping */
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; /* inner loop */
00425         gfc->full_outer_loop = 0;
00426         break;
00427 
00428     case 1:
00429         gfc->filter_type = 0; /* 1 not yet coded */
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; /* 1 not yet coded */
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; /*type 2 disabled because of it slowness,
00457                                       in favor of full outer loop search */
00458         gfc->full_outer_loop = 0; /* full outer loop search disabled because
00459                                      of audible distortions it may generate
00460                                      rh 060629 */
00461         break;
00462     }
00463 
00464 }
00465 
00466 
00467 
00468 
00469 
00470 
00471 
00472 /********************************************************************
00473  *   initialize internal params based on data in gf
00474  *   (globalflags struct filled in by calling program)
00475  *
00476  *  OUTLINE:
00477  *
00478  * We first have some complex code to determine bitrate,
00479  * output samplerate and mode.  It is complicated by the fact
00480  * that we allow the user to set some or all of these parameters,
00481  * and need to determine best possible values for the rest of them:
00482  *
00483  *  1. set some CPU related flags
00484  *  2. check if we are mono->mono, stereo->mono or stereo->stereo
00485  *  3.  compute bitrate and output samplerate:
00486  *          user may have set compression ratio
00487  *          user may have set a bitrate
00488  *          user may have set a output samplerate
00489  *  4. set some options which depend on output samplerate
00490  *  5. compute the actual compression ratio
00491  *  6. set mode based on compression ratio
00492  *
00493  *  The remaining code is much simpler - it just sets options
00494  *  based on the mode & compression ratio:
00495  *
00496  *   set allow_diff_short based on mode
00497  *   select lowpass filter based on compression ratio & mode
00498  *   set the bitrate index, and min/max bitrates for VBR modes
00499  *   disable VBR tag if it is not appropriate
00500  *   initialize the bitstream
00501  *   initialize scalefac_band data
00502  *   set sideinfo_len (based on channels, CRC, out_samplerate)
00503  *   write an id3v2 tag into the bitstream
00504  *   write VBR tag into the bitstream
00505  *   set mpeg1/2 flag
00506  *   estimate the number of frames (based on a lot of data)
00507  *
00508  *   now we set more flags:
00509  *   nspsytune:
00510  *      see code
00511  *   VBR modes
00512  *      see code
00513  *   CBR/ABR
00514  *      see code
00515  *
00516  *  Finally, we set the algorithm flags based on the gfp->quality value
00517  *  lame_init_qval(gfp);
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     /* report functions */
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;      /* maybe error codes should be enumerated in lame.h ?? */
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; /* don't allow forced mid/side stereo for mono output */
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; /* VBR can't be mixed with free format */
00591 
00592     if (gfp->VBR == vbr_off && gfp->brate == 0) {
00593         /* no bitrate or compression ratio specified, use 11.025 */
00594         if (gfp->compression_ratio == 0)
00595             gfp->compression_ratio = 11.025; /* rate to compress a CD down to exactly 128000 bps */
00596     }
00597 
00598     /* find bitrate if user specify a compression ratio */
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)); /* round up with a margin of 3% */
00603 
00604         /* choose a bitrate for the output samplerate which achieves
00605          * specified compression ratio
00606          */
00607         gfp->brate = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->compression_ratio);
00608 
00609         /* we need the version for the bitrate table look up */
00610         gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
00611 
00612         if (!gfp->free_format) /* for non Free Format find the nearest allowed bitrate */
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; /* at 160 kbps (MPEG-2/2.5)/ 320 kbps (MPEG-1) only Free format or CBR are possible, no VBR */
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     /* if a filter has not been enabled, see if we should add one: */
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     /* do not compute ReplayGain values and do not find the peak sample
00687        if we can't store them */
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(); /* initialize the decoder  */
00710 #endif
00711 
00712     gfc->mode_gr = gfp->out_samplerate <= 24000 ? 1 : 2; /* Number of granules per frame */
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      *  sample freq       bitrate     compression ratio
00720      *     [kHz]      [kbps/channel]   for 16 bit input
00721      *     44.1            56               12.6
00722      *     44.1            64               11.025
00723      *     44.1            80                8.82
00724      *     22.05           24               14.7
00725      *     22.05           32               11.025
00726      *     22.05           40                8.82
00727      *     16              16               16.0
00728      *     16              24               10.667
00729      *
00730      */
00731     /*
00732      *  For VBR, take a guess at the compression_ratio.
00733      *  For example:
00734      *
00735      *    VBR_q    compression     like
00736      *     -        4.4         320 kbps/44 kHz
00737      *   0...1      5.5         256 kbps/44 kHz
00738      *     2        7.3         192 kbps/44 kHz
00739      *     4        8.8         160 kbps/44 kHz
00740      *     6       11           128 kbps/44 kHz
00741      *     9       14.7          96 kbps
00742      *
00743      *  for lower bitrates, downsample with --resample
00744      */
00745 
00746     switch (gfp->VBR) {
00747     case vbr_mt:
00748     case vbr_rh:
00749     case vbr_mtrh:
00750         {
00751             /*numbers are a bit strange, but they determine the lowpass value */
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     /* mode = -1 (not set by user) or
00767      * mode = MONO (because of only 1 input channel).
00768      * If mode has not been set, then select J-STEREO
00769      */
00770     if (gfp->mode == NOT_SET) {
00771         gfp->mode = JOINT_STEREO;
00772     }
00773 
00774 
00775     /* apply user driven high pass filter */
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            /* 0% above on default */
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     /* apply user driven low pass filter */
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) /* has to be >= 0 */
00794                 gfc->lowpass1 = 0;
00795         }
00796         else {          /* 0% below on default */
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     /* compute info needed for polyphase filter (filter type==0, default) */
00808   /**********************************************************************/
00809     lame_init_params_ppflt(gfp);
00810 
00811 
00812   /*******************************************************/
00813     /* compute info needed for FIR filter (filter_type==1) */
00814   /*******************************************************/
00815     /* not yet coded */
00816 
00817 
00818 
00819   /*******************************************************
00820    * samplerate and bitrate index
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     /* for CBR, we will write an "info" tag. */
00845     /*    if ((gfp->VBR == vbr_off))  */
00846     /*  gfp->bWriteVbrTag = 0; */
00847 
00848     if (gfp->analysis)
00849         gfp->bWriteVbrTag = 0;
00850 
00851     /* some file options not allowed if output is: not specified or stdout */
00852     if (gfc->pinfo != NULL)
00853         gfp->bWriteVbrTag = 0; /* disable Xing VBR tag */
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     /* determine the mean bitrate for main data */
00879     if (gfp->version == 1) /* MPEG 1 */
00880         gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 17 : 4 + 32;
00881     else                /* MPEG 2 */
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     /*if (gfp->exp_nspsytune & 1) */  {
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         /*lint --fallthrough */
00909     case vbr_mtrh:{
00910             if ( gfp->useTemporal < 0 ) {
00911                 gfp->useTemporal = 0;   /* off by default for this VBR mode */
00912             }
00913 
00914             (void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
00915             /*  The newer VBR code supports only a limited
00916                 subset of quality levels:
00917                 9-6=6 are the same, uses ISO quantization
00918                   5=5               uses x^3/4 quantization
00919                 4-0=0 are the same  5 plus best huffman divide code
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             /*  tonality
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             /*  sfb21 extra only with MPEG-1 at higher sampling rates
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             /*  sfb21 extra only with MPEG-1 at higher sampling rates
00974              */
00975             if (gfp->experimentalY)
00976                 gfc->sfb21_extra = 0;
00977             else
00978                 gfc->sfb21_extra = (gfp->out_samplerate > 44000);
00979 
00980             /*  VBR needs at least the output of GPSYCHO,
00981              *  so we have to garantee that by setting a minimum
00982              *  quality level, actually level 6 does it.
00983              *  down to level 6
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:           /* cbr/abr */  {
00997             vbr_mode vbrmode;
00998 
00999             /*  no sfb21 extra with CBR code
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             /* second, set parameters depending on bitrate */
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     /*initialize default values common for all modes */
01028 
01029 
01030     if (lame_get_VBR(gfp) != vbr_off) { /* choose a min/max bitrate for VBR */
01031         /* if the user didn't specify VBR_max_bitrate: */
01032         gfc->VBR_min_bitrate = 1; /* default: allow   8 kbps (MPEG-2) or  32 kbps (MPEG-1) */
01033         gfc->VBR_max_bitrate = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
01034         if (gfp->out_samplerate < 16000)
01035             gfc->VBR_max_bitrate = 8; /* default: allow 64 kbps (MPEG-2.5) */
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     /*  just another daily changing developer switch  */
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     /* initialize internal qval settings */
01068     lame_init_qval(gfp);
01069 
01070 
01071     /*  automatic ATH adjustment on
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     /* initialize internal adaptive ATH settings  -jd */
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     /*Note Jan/2003: Many hardware decoders cannot handle short blocks in regular
01090        stereo mode unless they are coupled (same type in both channels)
01091        it is a rare event (1 frame per min. or so) that LAME would use
01092        uncoupled short blocks, so lets turn them off until we decide
01093        how to handle this.  No other encoders allow uncoupled short blocks,
01094        even though it is in the standard.  */
01095     /* rh 20040217: coupling makes no sense for mono and dual-mono streams
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     /* select psychoacoustic model */
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; /* on by default */
01144 
01145 
01146 
01147     /* padding method as described in
01148      * "MPEG-Layer3 / Bitstream Syntax and Decoding"
01149      * by Martin Sieler, Ralph Sperschneider
01150      *
01151      * note: there is no padding for the very first frame
01152      *
01153      * Robert Hegemann 2000-06-22
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         /* nothing to change */
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  *  print_config
01184  *
01185  *  Prints some selected information about the coding parameters via
01186  *  the macro command MSGF(), which is currently mapped to lame_errorf
01187  *  (reports via a error function?), which is a printf-like function
01188  *  for <stderr>.
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 /* mono */ ) {
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     /*  compiler/processor optimizations, operational, etc.
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     /*  everything controlling the stream format
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     /*  everything controlling psychoacoustic settings, like ATH, etc.
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     /*  that's all ?
01471      */
01472     MSGF(gfc, "\n");
01473     return;
01474 }
01475 
01476 
01477 
01478 /* routine to feed exactly one frame (gfp->framesize) worth of data to the
01479 encoding engine.  All buffering, resampling, etc, handled by calling
01480 program.
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  * THE MAIN LAME ENCODING INTERFACE
01525  * mt 3/00
01526  *
01527  * input pcm data, output (maybe) mp3 frames.
01528  * This routine handles all buffering, resampling and filtering for you.
01529  * The required mp3buffer_size can be computed from num_samples,
01530  * samplerate and encoding rate, but here is a worst case estimate:
01531  *
01532  * mp3buffer_size in bytes = 1.25*num_samples + 7200
01533  *
01534  * return code = number of bytes output in mp3buffer.  can be 0
01535  *
01536  * NOTE: this routine uses LAME's internal PCM data representation,
01537  * 'sample_t'.  It should not be used by any application.
01538  * applications should use lame_encode_buffer(),
01539  *                         lame_encode_buffer_float()
01540  *                         lame_encode_buffer_int()
01541  * etc... depending on what type of data they are working with.
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     /* copy out any tags that may have been written into bitstream */
01562     mp3out = copy_buffer(gfc, mp3buf, mp3buf_size, 0);
01563     if (mp3out < 0)
01564         return mp3out;  /* not enough buffer space */
01565     mp3buf += mp3out;
01566     mp3size += mp3out;
01567 
01568 
01569     in_buffer[0] = buffer_l;
01570     in_buffer[1] = buffer_r;
01571 
01572 
01573     /* Apply user defined re-scaling */
01574 
01575     /* user selected scaling of the samples */
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     /* user selected scaling of the channel 0 (left) samples */
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     /* user selected scaling of the channel 1 (right) samples */
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     /* Downsample to Mono if 2 channels in and 1 channel out */
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     /* some sanity checks */
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; /* amount needed for FFT */
01616     /*mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); */
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;    /* number of input samples processed with fill_buffer */
01626         int     n_out = 0;   /* number of samples output with fill_buffer */
01627         /* n_in <> n_out if we are resampling */
01628 
01629         /* copy in new samples into mfbuf, with resampling */
01630         fill_buffer(gfp, mfbuf, in_buffer, nsamples, &n_in, &n_out);
01631 
01632         /* compute ReplayGain of resampled input if requested */
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         /* update in_buffer counters */
01642         nsamples -= n_in;
01643         in_buffer[0] += n_in;
01644         if (gfc->channels_out == 2)
01645             in_buffer[1] += n_in;
01646 
01647         /* update mfbuf[] counters */
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             /* encode the frame.  */
01655             /* mp3buf              = pointer to current location in buffer */
01656             /* mp3buf_size         = size of original mp3 output buffer */
01657             /*                     = 0 if we should not worry about the */
01658             /*                       buffer size because calling program is  */
01659             /*                       to lazy to compute it */
01660             /* mp3size             = size of data written to buffer so far */
01661             /* mp3buf_size-mp3size = amount of space avalable  */
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             /* shift out old samples */
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     /* make a copy of input buffer, changing type to sample_t */
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     /* make a copy of input buffer, changing type to sample_t */
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     /* make a copy of input buffer, changing type to sample_t */
01782     for (i = 0; i < nsamples; i++) {
01783         /* internal code expects +/- 32768.0 */
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     /* make a copy of input buffer, changing type to sample_t */
01820     for (i = 0; i < nsamples; i++) {
01821         /* internal code expects +/- 32768.0 */
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     /* make a copy of input buffer, changing type to sample_t */
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  Flush mp3 buffer, pad with ancillary data so last frame is complete.
01917  Reset reservoir size to 0
01918  but keep all PCM samples and MDCT data in memory
01919  This option is used to break a large file into several mp3 files
01920  that when concatenated together will decode with no gaps
01921  Because we set the reservoir=0, they will also decode seperately
01922  with no errors.
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 /* called by lame_init_params.  You can also call this after flush_nogap
01934    if you want to write new id3v2 and Xing VBR tags into the bitstream */
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     /* initialize histogram data optionally used by frontend */
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     /* Write initial VBR Header to bitstream and init VBR data */
01950     if (gfp->bWriteVbrTag)
01951         (void) InitVbrTag(gfp);
01952 
01953 
01954     return 0;
01955 }
01956 
01957 
01958 /*****************************************************************/
01959 /* flush internal PCM sample buffers, then mp3 buffers           */
01960 /* then write id3 v1 tags into bitstream.                        */
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     /* we always add POSTDELAY=288 padding to make sure granule with real
01971      * data can be complety decoded (because of 50% overlap with next granule */
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         /* if user specifed buffer size = 0, dont check size */
01983         if (mp3buffer_size == 0)
01984             mp3buffer_size_remaining = 0;
01985 
01986         /* send in a frame of 0 padding until all internal sample buffers
01987          * are flushed
01988          */
01989         imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], gfp->framesize,
01990                                   mp3buffer, mp3buffer_size_remaining);
01991 
01992         /* don't count the above padding: */
01993         gfc->mf_samples_to_encode -= gfp->framesize;
01994         if (gfc->mf_samples_to_encode < 0) {
01995             /* we added extra padding to the end */
01996             end_padding += -gfc->mf_samples_to_encode;
01997         }
01998 
01999 
02000         if (imp3 < 0) {
02001             /* some type of fatal error */
02002             return imp3;
02003         }
02004         mp3buffer += imp3;
02005         mp3count += imp3;
02006     }
02007 
02008     mp3buffer_size_remaining = mp3buffer_size - mp3count;
02009     /* if user specifed buffer size = 0, dont check size */
02010     if (mp3buffer_size == 0)
02011         mp3buffer_size_remaining = 0;
02012 
02013     /* mp3 related stuff.  bit buffer might still contain some mp3 data */
02014     flush_bitstream(gfp);
02015     imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 1);
02016     if (imp3 < 0) {
02017         /* some type of fatal error */
02018         return imp3;
02019     }
02020     mp3buffer += imp3;
02021     mp3count += imp3;
02022     mp3buffer_size_remaining = mp3buffer_size - mp3count;
02023     /* if user specifed buffer size = 0, dont check size */
02024     if (mp3buffer_size == 0)
02025         mp3buffer_size_remaining = 0;
02026 
02027     /* write a id3 tag to the bitstream */
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  *      lame_close ()
02042  *
02043  *  frees internal buffers
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     /* this routien will free all malloc'd data in gfc, and then free gfc: */
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 /* flush internal mp3 buffers, and free internal buffers         */
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 /* write VBR Xing header, and ID3 version 1 tag, if asked for    */
02086 /*****************************************************************/
02087 void
02088 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
02089 {
02090     if (gfp->bWriteVbrTag) {
02091         /* Write Xing header again */
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                 /* OK */
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 /* initialize mp3 encoder */
02141 int
02142 lame_init_old(lame_global_flags * gfp)
02143 {
02144     lame_internal_flags *gfc;
02145 
02146     disable_FPE();      /* disable floating point exceptions */
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     /* Global flags.  set defaults here for non-zero values */
02154     /* see lame.h for description */
02155     /* set integer values to -1 to mean that LAME will compute the
02156      * best value, UNLESS the calling program as set it
02157      * (and the value is no longer -1)
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; /* not  0 ????? */
02185     gfc->VBR_max_bitrate = 13; /* not 14 ????? */
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;  /* default = -1 = set in lame_init_params */
02206     gfp->athaa_loudapprox = -1; /* 1 = flat loudness approx. (total energy) */
02207     /* 2 = equal loudness curve */
02208     gfp->athaa_sensitivity = 0.0; /* no offset */
02209     gfp->useTemporal = -1;
02210     gfp->interChRatio = -1;
02211 
02212     /* The reason for
02213      *       int mf_samples_to_encode = ENCDELAY + POSTDELAY;
02214      * ENCDELAY = internal encoder delay.  And then we have to add POSTDELAY=288
02215      * because of the 50% MDCT overlap.  A 576 MDCT granule decodes to
02216      * 1152 samples.  To synthesize the 576 samples centered under this granule
02217      * we need the previous granule for the first 288 samples (no problem), and
02218      * the next granule for the next 288 samples (not possible if this is last
02219      * granule).  So we need to pad with 288 samples to make sure we can
02220      * encode the 576 samples we are interested in.
02221      */
02222     gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
02223     gfp->encoder_padding = 0;
02224     gfc->mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
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  *  some simple statistics
02252  *
02253  *  Robert Hegemann 2000-10-11
02254  *
02255  ***********************************************************************/
02256 
02257 /*  histogram of used bitrate indexes:
02258  *  One has to weight them to calculate the average bitrate in kbps
02259  *
02260  *  bitrate indices:
02261  *  there are 14 possible bitrate indices, 0 has the special meaning
02262  *  "free format" which is not possible to mix with VBR and 15 is forbidden
02263  *  anyway.
02264  *
02265  *  stereo modes:
02266  *  0: LR   number of left-right encoded frames
02267  *  1: LR-I number of left-right and intensity encoded frames
02268  *  2: MS   number of mid-side encoded frames
02269  *  3: MS-I number of mid-side and intensity encoded frames
02270  *
02271  *  4: number of encoded frames
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 /* end of lame.c */

Generated on Sun Dec 2 11:34:19 2007 for LAME by  doxygen 1.5.2