📄 lame.c
字号:
gfc->samplerate_index = SmpFrqIndex((long)gfp->out_samplerate, &gfp->version); if( gfc->samplerate_index < 0) { display_bitrates(stderr); return -1; } if (gfp->free_format) { gfc->bitrate_index=0; }else{ if( (gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version,gfp->out_samplerate)) < 0) { display_bitrates(stderr); return -1; } } /* choose a min/max bitrate for VBR */ if (gfp->VBR!=vbr_off) { /* if the user didn't specify VBR_max_bitrate: */ if (0==gfp->VBR_max_bitrate_kbps) { gfc->VBR_max_bitrate=14; /* default: allow 320kbs */ }else{ if( (gfc->VBR_max_bitrate = BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version,gfp->out_samplerate)) < 0) { display_bitrates(stderr); return -1; } } if (0==gfp->VBR_min_bitrate_kbps) { gfc->VBR_min_bitrate=1; /* 32 kbps */ }else{ if( (gfc->VBR_min_bitrate = BitrateIndex(gfp->VBR_min_bitrate_kbps, gfp->version,gfp->out_samplerate)) < 0) { display_bitrates(stderr); return -1; } } gfp->VBR_mean_bitrate_kbps = Min(bitrate_table[gfp->version][gfc->VBR_max_bitrate]*.95,gfp->VBR_mean_bitrate_kbps); gfp->VBR_mean_bitrate_kbps = Max(bitrate_table[gfp->version][gfc->VBR_min_bitrate]*.95,gfp->VBR_mean_bitrate_kbps); /* Note: ABR mode should normally be used without a -V n setting, * (or with the default value of 4) * but the code below allows us to test how adjusting the maskings * effects CBR encodings. Lowering the maskings will make LAME * work harder to get over=0 and may give better noise shaping? */ if (gfp->VBR == vbr_abr) { static const FLOAT8 dbQ[10]={-5.0,-3.75,-2.5,-1.25,0,0.4,0.8,1.2,1.6,2.0}; FLOAT8 masking_lower_db; assert( gfp->VBR_q <= 9 ); assert( gfp->VBR_q >= 0 ); masking_lower_db = dbQ[gfp->VBR_q]; gfc->masking_lower = pow(10.0,masking_lower_db/10); gfc->ATH_lower = (4-gfp->VBR_q)*4.0; } /* - lower masking depending on Quality setting * - quality control together with adjusted ATH MDCT scaling * on lower quality setting allocate more noise from * ATH masking, and on higher quality setting allocate * less noise from ATH masking. * - experiments show that going more than 2dB over GPSYCHO's * limits ends up in very annoying artefacts */ if (gfp->VBR == vbr_rh) { static const FLOAT8 dbQ[10]={-5.0,-3.75,-2.5,-1.25,0,0.4,0.8,1.2,1.6,2.0}; FLOAT8 masking_lower_db; assert( gfp->VBR_q <= 9 ); assert( gfp->VBR_q >= 0 ); masking_lower_db = dbQ[gfp->VBR_q]; gfc->masking_lower = pow(10.0,masking_lower_db/10); gfc->ATH_lower = (4-gfp->VBR_q)*4.0; } } /* VBR needs at least the output of GPSYCHO, * so we have to garantee that by setting a minimum * quality level, actually level 7 does it. * the -v and -V x settings switch the quality to level 2 * you would have to add a -f or -q 5 to reduce the quality * down to level 7 or 5 */ if (gfp->VBR!=vbr_off) gfp->quality=Min(gfp->quality,7); /* dont allow forced mid/side stereo for mono output */ if (gfp->mode == MPG_MD_MONO) gfp->force_ms=0; /* Do not write VBR tag if VBR flag is not specified */ if (gfp->VBR==vbr_off) gfp->bWriteVbrTag=0; if (gfp->ogg) gfp->bWriteVbrTag=0; if (gfp->gtkflag) gfp->bWriteVbrTag=0; /* some file options not allowed if output is: not specified or stdout */ if (gfp->outPath!=NULL && gfp->outPath[0]=='-' ) { gfp->bWriteVbrTag=0; /* turn off VBR tag */ } if (gfp->outPath==NULL || gfp->outPath[0]=='-' ) { gfp->id3tag_used=0; /* turn of id3 tagging */ } if (gfc->pinfo != NULL) { gfp->bWriteVbrTag=0; /* disable Xing VBR tag */ } init_bit_stream_w(gfc); /* set internal feature flags. USER should not access these since * some combinations will produce strange results */ /* no psymodel, no noise shaping */ if (gfp->quality==9) { gfc->filter_type=0; gfc->psymodel=0; gfc->quantization=0; gfc->noise_shaping=0; gfc->noise_shaping_stop=0; gfc->use_best_huffman=0; } if (gfp->quality==8) gfp->quality=7; /* use psymodel (for short block and m/s switching), but no noise shapping */ if (gfp->quality==7) { gfc->filter_type=0; gfc->psymodel=1; gfc->quantization=0; gfc->noise_shaping=0; gfc->noise_shaping_stop=0; gfc->use_best_huffman=0; } if (gfp->quality==6) gfp->quality=5; if (gfp->quality==5) { /* the default */ gfc->filter_type=0; gfc->psymodel=1; gfc->quantization=0; gfc->noise_shaping=1; gfc->noise_shaping_stop=0; gfc->use_best_huffman=0; } if (gfp->quality==4) gfp->quality=2; if (gfp->quality==3) { gfc->filter_type=0; gfc->psymodel=1; gfc->quantization=1; gfc->noise_shaping=1; gfc->noise_shaping_stop=0; gfc->use_best_huffman=1; } if (gfp->quality==2) { gfc->filter_type=0; gfc->psymodel=1; gfc->quantization=1; if (gfp->VBR==vbr_mt || gfp->VBR==vbr_rh) /* VBR modes currently have some problems which are aggravated by * scalefac_scale */ gfc->noise_shaping=1; else gfc->noise_shaping=2; gfc->noise_shaping_stop=0; gfc->use_best_huffman=1; } if (gfp->quality==1) { gfc->filter_type=0; gfc->psymodel=1; gfc->quantization=1; gfc->noise_shaping=2; gfc->noise_shaping_stop=0; gfc->use_best_huffman=1; } if (gfp->quality==0) { /* 0..1 quality */ gfc->filter_type=1; /* not yet coded */ gfc->psymodel=1; gfc->quantization=1; gfc->noise_shaping=3; /* not yet coded */ gfc->noise_shaping_stop=2; /* not yet coded */ gfc->use_best_huffman=2; /* not yet coded */ return -1; } for (i = 0; i < SBMAX_l + 1; i++) { gfc->scalefac_band.l[i] = sfBandIndex[gfc->samplerate_index + (gfp->version * 3) + 6*(gfp->out_samplerate<16000)].l[i]; } for (i = 0; i < SBMAX_s + 1; i++) { gfc->scalefac_band.s[i] = sfBandIndex[gfc->samplerate_index + (gfp->version * 3) + 6*(gfp->out_samplerate<16000)].s[i]; } /* determine the mean bitrate for main data */ gfc->sideinfo_len = 4; if ( gfp->version == 1 ) { /* MPEG 1 */ if ( gfc->stereo == 1 ) gfc->sideinfo_len += 17; else gfc->sideinfo_len += 32; } else { /* MPEG 2 */ if ( gfc->stereo == 1 ) gfc->sideinfo_len += 9; else gfc->sideinfo_len += 17; } if (gfp->error_protection) gfc->sideinfo_len += 2; if (gfp->bWriteVbrTag) { /* Write initial VBR Header to bitstream */ InitVbrTag(gfp); } if (gfp->brhist_disp) brhist_init(gfp,1,14);#ifdef HAVEVORBIS if (gfp->ogg) { lame_encode_ogg_init(gfp); gfc->filter_type=-1; /* vorbis claims not to need filters */ gfp->VBR=vbr_off; /* ignore lame's various VBR modes */ }#endif return 0;}/************************************************************************ * * print_config * * PURPOSE: Prints the encoding parameters used * ************************************************************************/void lame_print_config(lame_global_flags *gfp){ lame_internal_flags *gfc=gfp->internal_flags; static const char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" }; FLOAT out_samplerate=gfp->out_samplerate/1000.0; FLOAT in_samplerate = gfc->resample_ratio*out_samplerate; lame_print_version(stderr); if (gfp->num_channels==2 && gfc->stereo==1) { MSGF("Autoconverting from stereo to mono. Setting encoding to mono mode.\n"); } if (gfc->resample_ratio!=1) { MSGF("Resampling: input=%.1fkHz output=%.1fkHz\n", in_samplerate,out_samplerate); } if (gfc->filter_type==0) { if (gfc->highpass2>0.0) MSGF("Using polyphase highpass filter, transition band: %.0f Hz - %.0f Hz\n", gfc->highpass1*out_samplerate*500, gfc->highpass2*out_samplerate*500); if (gfc->lowpass1>0.0) MSGF("Using polyphase lowpass filter, transition band: %.0f Hz - %.0f Hz\n", gfc->lowpass1*out_samplerate*500, gfc->lowpass2*out_samplerate*500); } if (gfp->gtkflag) { MSGF("Analyzing %s \n",gfp->inPath); } else { MSGF("Encoding %s to %s\n", (strcmp(gfp->inPath, "-")? gfp->inPath : "stdin"), (strcmp(gfp->outPath, "-")? gfp->outPath : "stdout")); if (gfp->ogg) { MSGF("Encoding as %.1f kHz VBR Ogg Vorbis \n", gfp->out_samplerate/1000.0); }else if (gfp->VBR==vbr_mt || gfp->VBR==vbr_rh) MSGF("Encoding as %.1f kHz VBR(q=%i) %s MPEG%i LayerIII (%4.1fx estimated) qval=%i\n", gfp->out_samplerate/1000.0, gfp->VBR_q,mode_names[gfp->mode],2-gfp->version,gfp->compression_ratio,gfp->quality); else if (gfp->VBR==vbr_abr) MSGF("Encoding as %.1f kHz average %d kbps %s MPEG%i LayerIII (%4.1fx) qval=%i\n", gfp->out_samplerate/1000.0, gfp->VBR_mean_bitrate_kbps,mode_names[gfp->mode],2-gfp->version,gfp->compression_ratio,gfp->quality); else { MSGF("Encoding as %.1f kHz %d kbps %s MPEG%i LayerIII (%4.1fx) qval=%i\n", gfp->out_samplerate/1000.0,gfp->brate, mode_names[gfp->mode],2-gfp->version,gfp->compression_ratio,gfp->quality); } } if (gfp->free_format) { MSGF("Warning: many decoders cannot handle free format bitstreams\n"); if (gfp->brate>320) { MSGF("Warning: many decoders cannot handle free format bitrates > 320kbs\n"); } } fflush(stderr);}/************************************************************************** encodeframe() Layer 3** encode a single frame*************************************************************************lame_encode_frame() gr 0 gr 1inbuf: |--------------|---------------|-------------|MDCT output: |--------------|---------------|-------------|FFT's <---------1024----------> <---------1024--------> inbuf = buffer of PCM data size=MP3 framesize encoder acts on inbuf[ch][0], but output is delayed by MDCTDELAY so the MDCT coefficints are from inbuf[ch][-MDCTDELAY] psy-model FFT has a 1 granule day, so we feed it data for the next granule. FFT is centered over granule: 224+576+224 So FFT starts at: 576-224-MDCTDELAY MPEG2: FFT ends at: BLKSIZE+576-224-MDCTDELAY MPEG1: FFT ends at: BLKSIZE+2*576-224-MDCTDELAY (1904) FFT starts at 576-224-MDCTDELAY (304) = 576-FFTOFFSET*/int lame_encode_mp3_frame(lame_global_flags *gfp,short int inbuf_l[],short int inbuf_r[],char *mp3buf, int mp3buf_size){ FLOAT8 xr[2][2][576]; int l3_enc[2][2][576]; int mp3count; III_psy_ratio masking_ratio[2][2]; /*LR ratios */ III_psy_ratio masking_MS_ratio[2][2]; /*MS ratios */ III_psy_ratio (*masking)[2][2]; /*LR ratios and MS ratios*/ III_scalefac_t scalefac[2][2]; short int *inbuf[2]; lame_internal_flags *gfc=gfp->internal_flags; typedef FLOAT8 pedata[2][2]; pedata pe,pe_MS; pedata *pe_use; int ch,gr,mean_bits; int bitsPerFrame; int check_ms_stereo; FLOAT8 ms_ratio_next=0; FLOAT8 ms_ratio_prev=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -