nuclear@1: /******************************************************************** nuclear@1: * * nuclear@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@1: * * nuclear@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: simple programmatic interface for encoder mode setup nuclear@1: last mod: $Id: vorbisenc.c 17028 2010-03-25 05:22:15Z xiphmont $ nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: nuclear@1: #include "vorbis/codec.h" nuclear@1: #include "vorbis/vorbisenc.h" nuclear@1: nuclear@1: #include "codec_internal.h" nuclear@1: nuclear@1: #include "os.h" nuclear@1: #include "misc.h" nuclear@1: nuclear@1: /* careful with this; it's using static array sizing to make managing nuclear@1: all the modes a little less annoying. If we use a residue backend nuclear@1: with > 12 partition types, or a different division of iteration, nuclear@1: this needs to be updated. */ nuclear@1: typedef struct { nuclear@1: const static_codebook *books[12][4]; nuclear@1: } static_bookblock; nuclear@1: nuclear@1: typedef struct { nuclear@1: int res_type; nuclear@1: int limit_type; /* 0 lowpass limited, 1 point stereo limited */ nuclear@1: int grouping; nuclear@1: const vorbis_info_residue0 *res; nuclear@1: const static_codebook *book_aux; nuclear@1: const static_codebook *book_aux_managed; nuclear@1: const static_bookblock *books_base; nuclear@1: const static_bookblock *books_base_managed; nuclear@1: } vorbis_residue_template; nuclear@1: nuclear@1: typedef struct { nuclear@1: const vorbis_info_mapping0 *map; nuclear@1: const vorbis_residue_template *res; nuclear@1: } vorbis_mapping_template; nuclear@1: nuclear@1: typedef struct vp_adjblock{ nuclear@1: int block[P_BANDS]; nuclear@1: } vp_adjblock; nuclear@1: nuclear@1: typedef struct { nuclear@1: int data[NOISE_COMPAND_LEVELS]; nuclear@1: } compandblock; nuclear@1: nuclear@1: /* high level configuration information for setting things up nuclear@1: step-by-step with the detailed vorbis_encode_ctl interface. nuclear@1: There's a fair amount of redundancy such that interactive setup nuclear@1: does not directly deal with any vorbis_info or codec_setup_info nuclear@1: initialization; it's all stored (until full init) in this highlevel nuclear@1: setup, then flushed out to the real codec setup structs later. */ nuclear@1: nuclear@1: typedef struct { nuclear@1: int att[P_NOISECURVES]; nuclear@1: float boost; nuclear@1: float decay; nuclear@1: } att3; nuclear@1: typedef struct { int data[P_NOISECURVES]; } adj3; nuclear@1: nuclear@1: typedef struct { nuclear@1: int pre[PACKETBLOBS]; nuclear@1: int post[PACKETBLOBS]; nuclear@1: float kHz[PACKETBLOBS]; nuclear@1: float lowpasskHz[PACKETBLOBS]; nuclear@1: } adj_stereo; nuclear@1: nuclear@1: typedef struct { nuclear@1: int lo; nuclear@1: int hi; nuclear@1: int fixed; nuclear@1: } noiseguard; nuclear@1: typedef struct { nuclear@1: int data[P_NOISECURVES][17]; nuclear@1: } noise3; nuclear@1: nuclear@1: typedef struct { nuclear@1: int mappings; nuclear@1: const double *rate_mapping; nuclear@1: const double *quality_mapping; nuclear@1: int coupling_restriction; nuclear@1: long samplerate_min_restriction; nuclear@1: long samplerate_max_restriction; nuclear@1: nuclear@1: nuclear@1: const int *blocksize_short; nuclear@1: const int *blocksize_long; nuclear@1: nuclear@1: const att3 *psy_tone_masteratt; nuclear@1: const int *psy_tone_0dB; nuclear@1: const int *psy_tone_dBsuppress; nuclear@1: nuclear@1: const vp_adjblock *psy_tone_adj_impulse; nuclear@1: const vp_adjblock *psy_tone_adj_long; nuclear@1: const vp_adjblock *psy_tone_adj_other; nuclear@1: nuclear@1: const noiseguard *psy_noiseguards; nuclear@1: const noise3 *psy_noise_bias_impulse; nuclear@1: const noise3 *psy_noise_bias_padding; nuclear@1: const noise3 *psy_noise_bias_trans; nuclear@1: const noise3 *psy_noise_bias_long; nuclear@1: const int *psy_noise_dBsuppress; nuclear@1: nuclear@1: const compandblock *psy_noise_compand; nuclear@1: const double *psy_noise_compand_short_mapping; nuclear@1: const double *psy_noise_compand_long_mapping; nuclear@1: nuclear@1: const int *psy_noise_normal_start[2]; nuclear@1: const int *psy_noise_normal_partition[2]; nuclear@1: const double *psy_noise_normal_thresh; nuclear@1: nuclear@1: const int *psy_ath_float; nuclear@1: const int *psy_ath_abs; nuclear@1: nuclear@1: const double *psy_lowpass; nuclear@1: nuclear@1: const vorbis_info_psy_global *global_params; nuclear@1: const double *global_mapping; nuclear@1: const adj_stereo *stereo_modes; nuclear@1: nuclear@1: const static_codebook *const *const *const floor_books; nuclear@1: const vorbis_info_floor1 *floor_params; nuclear@1: const int floor_mappings; nuclear@1: const int **floor_mapping_list; nuclear@1: nuclear@1: const vorbis_mapping_template *maps; nuclear@1: } ve_setup_data_template; nuclear@1: nuclear@1: /* a few static coder conventions */ nuclear@1: static const vorbis_info_mode _mode_template[2]={ nuclear@1: {0,0,0,0}, nuclear@1: {1,0,0,1} nuclear@1: }; nuclear@1: nuclear@1: static const vorbis_info_mapping0 _map_nominal[2]={ nuclear@1: {1, {0,0}, {0}, {0}, 1,{0},{1}}, nuclear@1: {1, {0,0}, {1}, {1}, 1,{0},{1}} nuclear@1: }; nuclear@1: nuclear@1: #include "modes/setup_44.h" nuclear@1: #include "modes/setup_44u.h" nuclear@1: #include "modes/setup_44p51.h" nuclear@1: #include "modes/setup_32.h" nuclear@1: #include "modes/setup_8.h" nuclear@1: #include "modes/setup_11.h" nuclear@1: #include "modes/setup_16.h" nuclear@1: #include "modes/setup_22.h" nuclear@1: #include "modes/setup_X.h" nuclear@1: nuclear@1: static const ve_setup_data_template *const setup_list[]={ nuclear@1: &ve_setup_44_stereo, nuclear@1: &ve_setup_44_51, nuclear@1: &ve_setup_44_uncoupled, nuclear@1: nuclear@1: &ve_setup_32_stereo, nuclear@1: &ve_setup_32_uncoupled, nuclear@1: nuclear@1: &ve_setup_22_stereo, nuclear@1: &ve_setup_22_uncoupled, nuclear@1: &ve_setup_16_stereo, nuclear@1: &ve_setup_16_uncoupled, nuclear@1: nuclear@1: &ve_setup_11_stereo, nuclear@1: &ve_setup_11_uncoupled, nuclear@1: &ve_setup_8_stereo, nuclear@1: &ve_setup_8_uncoupled, nuclear@1: nuclear@1: &ve_setup_X_stereo, nuclear@1: &ve_setup_X_uncoupled, nuclear@1: &ve_setup_XX_stereo, nuclear@1: &ve_setup_XX_uncoupled, nuclear@1: 0 nuclear@1: }; nuclear@1: nuclear@1: static void vorbis_encode_floor_setup(vorbis_info *vi,int s, nuclear@1: const static_codebook *const *const *const books, nuclear@1: const vorbis_info_floor1 *in, nuclear@1: const int *x){ nuclear@1: int i,k,is=s; nuclear@1: vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f)); nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: nuclear@1: memcpy(f,in+x[is],sizeof(*f)); nuclear@1: nuclear@1: /* books */ nuclear@1: { nuclear@1: int partitions=f->partitions; nuclear@1: int maxclass=-1; nuclear@1: int maxbook=-1; nuclear@1: for(i=0;ipartitionclass[i]>maxclass)maxclass=f->partitionclass[i]; nuclear@1: for(i=0;i<=maxclass;i++){ nuclear@1: if(f->class_book[i]>maxbook)maxbook=f->class_book[i]; nuclear@1: f->class_book[i]+=ci->books; nuclear@1: for(k=0;k<(1<class_subs[i]);k++){ nuclear@1: if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k]; nuclear@1: if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: for(i=0;i<=maxbook;i++) nuclear@1: ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i]; nuclear@1: } nuclear@1: nuclear@1: /* for now, we're only using floor 1 */ nuclear@1: ci->floor_type[ci->floors]=1; nuclear@1: ci->floor_param[ci->floors]=f; nuclear@1: ci->floors++; nuclear@1: nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s, nuclear@1: const vorbis_info_psy_global *in, nuclear@1: const double *x){ nuclear@1: int i,is=s; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy_global *g=&ci->psy_g_param; nuclear@1: nuclear@1: memcpy(g,in+(int)x[is],sizeof(*g)); nuclear@1: nuclear@1: ds=x[is]*(1.-ds)+x[is+1]*ds; nuclear@1: is=(int)ds; nuclear@1: ds-=is; nuclear@1: if(ds==0 && is>0){ nuclear@1: is--; nuclear@1: ds=1.; nuclear@1: } nuclear@1: nuclear@1: /* interpolate the trigger threshholds */ nuclear@1: for(i=0;i<4;i++){ nuclear@1: g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds; nuclear@1: g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds; nuclear@1: } nuclear@1: g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_global_stereo(vorbis_info *vi, nuclear@1: const highlevel_encode_setup *const hi, nuclear@1: const adj_stereo *p){ nuclear@1: float s=hi->stereo_point_setting; nuclear@1: int i,is=s; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy_global *g=&ci->psy_g_param; nuclear@1: nuclear@1: if(p){ nuclear@1: memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS); nuclear@1: memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS); nuclear@1: nuclear@1: if(hi->managed){ nuclear@1: /* interpolate the kHz threshholds */ nuclear@1: for(i=0;icoupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0]; nuclear@1: g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1]; nuclear@1: g->coupling_pkHz[i]=kHz; nuclear@1: nuclear@1: kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds; nuclear@1: g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0]; nuclear@1: g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1]; nuclear@1: nuclear@1: } nuclear@1: }else{ nuclear@1: float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds; nuclear@1: for(i=0;icoupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0]; nuclear@1: g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1]; nuclear@1: g->coupling_pkHz[i]=kHz; nuclear@1: } nuclear@1: nuclear@1: kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds; nuclear@1: for(i=0;isliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0]; nuclear@1: g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1]; nuclear@1: } nuclear@1: } nuclear@1: }else{ nuclear@1: for(i=0;isliding_lowpass[0][i]=ci->blocksizes[0]; nuclear@1: g->sliding_lowpass[1][i]=ci->blocksizes[1]; nuclear@1: } nuclear@1: } nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_psyset_setup(vorbis_info *vi,double s, nuclear@1: const int *nn_start, nuclear@1: const int *nn_partition, nuclear@1: const double *nn_thresh, nuclear@1: int block){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: int is=s; nuclear@1: nuclear@1: if(block>=ci->psys) nuclear@1: ci->psys=block+1; nuclear@1: if(!p){ nuclear@1: p=_ogg_calloc(1,sizeof(*p)); nuclear@1: ci->psy_param[block]=p; nuclear@1: } nuclear@1: nuclear@1: memcpy(p,&_psy_info_template,sizeof(*p)); nuclear@1: p->blockflag=block>>1; nuclear@1: nuclear@1: if(hi->noise_normalize_p){ nuclear@1: p->normal_p=1; nuclear@1: p->normal_start=nn_start[is]; nuclear@1: p->normal_partition=nn_partition[is]; nuclear@1: p->normal_thresh=nn_thresh[is]; nuclear@1: } nuclear@1: nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block, nuclear@1: const att3 *att, nuclear@1: const int *max, nuclear@1: const vp_adjblock *in){ nuclear@1: int i,is=s; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: nuclear@1: /* 0 and 2 are only used by bitmanagement, but there's no harm to always nuclear@1: filling the values in here */ nuclear@1: p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds; nuclear@1: p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds; nuclear@1: p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds; nuclear@1: p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds; nuclear@1: p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds; nuclear@1: nuclear@1: p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds; nuclear@1: nuclear@1: for(i=0;itoneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block, nuclear@1: const compandblock *in, nuclear@1: const double *x){ nuclear@1: int i,is=s; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: nuclear@1: ds=x[is]*(1.-ds)+x[is+1]*ds; nuclear@1: is=(int)ds; nuclear@1: ds-=is; nuclear@1: if(ds==0 && is>0){ nuclear@1: is--; nuclear@1: ds=1.; nuclear@1: } nuclear@1: nuclear@1: /* interpolate the compander settings */ nuclear@1: for(i=0;inoisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block, nuclear@1: const int *suppress){ nuclear@1: int is=s; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: nuclear@1: p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds; nuclear@1: nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block, nuclear@1: const int *suppress, nuclear@1: const noise3 *in, nuclear@1: const noiseguard *guard, nuclear@1: double userbias){ nuclear@1: int i,is=s,j; nuclear@1: double ds=s-is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: nuclear@1: p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds; nuclear@1: p->noisewindowlomin=guard[block].lo; nuclear@1: p->noisewindowhimin=guard[block].hi; nuclear@1: p->noisewindowfixed=guard[block].fixed; nuclear@1: nuclear@1: for(j=0;jnoiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds; nuclear@1: nuclear@1: /* impulse blocks may take a user specified bias to boost the nuclear@1: nominal/high noise encoding depth */ nuclear@1: for(j=0;jnoiseoff[j][0]+6; /* the lowest it can go */ nuclear@1: for(i=0;inoiseoff[j][i]+=userbias; nuclear@1: if(p->noiseoff[j][i]noiseoff[j][i]=min; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_ath_setup(vorbis_info *vi,int block){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy *p=ci->psy_param[block]; nuclear@1: nuclear@1: p->ath_adjatt=ci->hi.ath_floating_dB; nuclear@1: p->ath_maxatt=ci->hi.ath_absolute_dB; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){ nuclear@1: int i; nuclear@1: for(i=0;ibooks;i++) nuclear@1: if(ci->book_param[i]==book)return(i); nuclear@1: nuclear@1: return(ci->books++); nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s, nuclear@1: const int *shortb,const int *longb){ nuclear@1: nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int is=s; nuclear@1: nuclear@1: int blockshort=shortb[is]; nuclear@1: int blocklong=longb[is]; nuclear@1: ci->blocksizes[0]=blockshort; nuclear@1: ci->blocksizes[1]=blocklong; nuclear@1: nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_residue_setup(vorbis_info *vi, nuclear@1: int number, int block, nuclear@1: const vorbis_residue_template *res){ nuclear@1: nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int i; nuclear@1: nuclear@1: vorbis_info_residue0 *r=ci->residue_param[number]= nuclear@1: _ogg_malloc(sizeof(*r)); nuclear@1: nuclear@1: memcpy(r,res->res,sizeof(*r)); nuclear@1: if(ci->residues<=number)ci->residues=number+1; nuclear@1: nuclear@1: r->grouping=res->grouping; nuclear@1: ci->residue_type[number]=res->res_type; nuclear@1: nuclear@1: /* fill in all the books */ nuclear@1: { nuclear@1: int booklist=0,k; nuclear@1: nuclear@1: if(ci->hi.managed){ nuclear@1: for(i=0;ipartitions;i++) nuclear@1: for(k=0;k<4;k++) nuclear@1: if(res->books_base_managed->books[i][k]) nuclear@1: r->secondstages[i]|=(1<groupbook=book_dup_or_new(ci,res->book_aux_managed); nuclear@1: ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed; nuclear@1: nuclear@1: for(i=0;ipartitions;i++){ nuclear@1: for(k=0;k<4;k++){ nuclear@1: if(res->books_base_managed->books[i][k]){ nuclear@1: int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]); nuclear@1: r->booklist[booklist++]=bookid; nuclear@1: ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k]; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: }else{ nuclear@1: nuclear@1: for(i=0;ipartitions;i++) nuclear@1: for(k=0;k<4;k++) nuclear@1: if(res->books_base->books[i][k]) nuclear@1: r->secondstages[i]|=(1<groupbook=book_dup_or_new(ci,res->book_aux); nuclear@1: ci->book_param[r->groupbook]=(static_codebook *)res->book_aux; nuclear@1: nuclear@1: for(i=0;ipartitions;i++){ nuclear@1: for(k=0;k<4;k++){ nuclear@1: if(res->books_base->books[i][k]){ nuclear@1: int bookid=book_dup_or_new(ci,res->books_base->books[i][k]); nuclear@1: r->booklist[booklist++]=bookid; nuclear@1: ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k]; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* lowpass setup/pointlimit */ nuclear@1: { nuclear@1: double freq=ci->hi.lowpass_kHz*1000.; nuclear@1: vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */ nuclear@1: double nyq=vi->rate/2.; nuclear@1: long blocksize=ci->blocksizes[block]>>1; nuclear@1: nuclear@1: /* lowpass needs to be set in the floor and the residue. */ nuclear@1: if(freq>nyq)freq=nyq; nuclear@1: /* in the floor, the granularity can be very fine; it doesn't alter nuclear@1: the encoding structure, only the samples used to fit the floor nuclear@1: approximation */ nuclear@1: f->n=freq/nyq*blocksize; nuclear@1: nuclear@1: /* this res may by limited by the maximum pointlimit of the mode, nuclear@1: not the lowpass. the floor is always lowpass limited. */ nuclear@1: switch(res->limit_type){ nuclear@1: case 1: /* point stereo limited */ nuclear@1: if(ci->hi.managed) nuclear@1: freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.; nuclear@1: else nuclear@1: freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.; nuclear@1: if(freq>nyq)freq=nyq; nuclear@1: break; nuclear@1: case 2: /* LFE channel; lowpass at ~ 250Hz */ nuclear@1: freq=250; nuclear@1: break; nuclear@1: default: nuclear@1: /* already set */ nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: /* in the residue, we're constrained, physically, by partition nuclear@1: boundaries. We still lowpass 'wherever', but we have to round up nuclear@1: here to next boundary, or the vorbis spec will round it *down* to nuclear@1: previous boundary in encode/decode */ nuclear@1: if(ci->residue_type[number]==2){ nuclear@1: /* residue 2 bundles together multiple channels; used by stereo nuclear@1: and surround. Count the channels in use */ nuclear@1: /* Multiple maps/submaps can point to the same residue. In the case nuclear@1: of residue 2, they all better have the same number of nuclear@1: channels/samples. */ nuclear@1: int j,k,ch=0; nuclear@1: for(i=0;imaps&&ch==0;i++){ nuclear@1: vorbis_info_mapping0 *mi=(vorbis_info_mapping0 *)ci->map_param[i]; nuclear@1: for(j=0;jsubmaps && ch==0;j++) nuclear@1: if(mi->residuesubmap[j]==number) /* we found a submap referencing theis residue backend */ nuclear@1: for(k=0;kchannels;k++) nuclear@1: if(mi->chmuxlist[k]==j) /* this channel belongs to the submap */ nuclear@1: ch++; nuclear@1: } nuclear@1: nuclear@1: r->end=(int)((freq/nyq*blocksize*ch)/r->grouping+.9)* /* round up only if we're well past */ nuclear@1: r->grouping; nuclear@1: /* the blocksize and grouping may disagree at the end */ nuclear@1: if(r->end>blocksize*ch)r->end=blocksize*ch/r->grouping*r->grouping; nuclear@1: nuclear@1: }else{ nuclear@1: nuclear@1: r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */ nuclear@1: r->grouping; nuclear@1: /* the blocksize and grouping may disagree at the end */ nuclear@1: if(r->end>blocksize)r->end=blocksize/r->grouping*r->grouping; nuclear@1: nuclear@1: } nuclear@1: nuclear@1: if(r->end==0)r->end=r->grouping; /* LFE channel */ nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* we assume two maps in this encoder */ nuclear@1: static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s, nuclear@1: const vorbis_mapping_template *maps){ nuclear@1: nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int i,j,is=s,modes=2; nuclear@1: const vorbis_info_mapping0 *map=maps[is].map; nuclear@1: const vorbis_info_mode *mode=_mode_template; nuclear@1: const vorbis_residue_template *res=maps[is].res; nuclear@1: nuclear@1: if(ci->blocksizes[0]==ci->blocksizes[1])modes=1; nuclear@1: nuclear@1: for(i=0;imap_param[i]=_ogg_calloc(1,sizeof(*map)); nuclear@1: ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode)); nuclear@1: nuclear@1: memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template)); nuclear@1: if(i>=ci->modes)ci->modes=i+1; nuclear@1: nuclear@1: ci->map_type[i]=0; nuclear@1: memcpy(ci->map_param[i],map+i,sizeof(*map)); nuclear@1: if(i>=ci->maps)ci->maps=i+1; nuclear@1: nuclear@1: for(j=0;jcodec_setup; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup; nuclear@1: int is=hi->base_setting; nuclear@1: double ds=hi->base_setting-is; nuclear@1: int ch=vi->channels; nuclear@1: const double *r=setup->rate_mapping; nuclear@1: nuclear@1: if(r==NULL) nuclear@1: return(-1); nuclear@1: nuclear@1: return((r[is]*(1.-ds)+r[is+1]*ds)*ch); nuclear@1: } nuclear@1: nuclear@1: static const void *get_setup_template(long ch,long srate, nuclear@1: double req,int q_or_bitrate, nuclear@1: double *base_setting){ nuclear@1: int i=0,j; nuclear@1: if(q_or_bitrate)req/=ch; nuclear@1: nuclear@1: while(setup_list[i]){ nuclear@1: if(setup_list[i]->coupling_restriction==-1 || nuclear@1: setup_list[i]->coupling_restriction==ch){ nuclear@1: if(srate>=setup_list[i]->samplerate_min_restriction && nuclear@1: srate<=setup_list[i]->samplerate_max_restriction){ nuclear@1: int mappings=setup_list[i]->mappings; nuclear@1: const double *map=(q_or_bitrate? nuclear@1: setup_list[i]->rate_mapping: nuclear@1: setup_list[i]->quality_mapping); nuclear@1: nuclear@1: /* the template matches. Does the requested quality mode nuclear@1: fall within this template's modes? */ nuclear@1: if(reqmap[setup_list[i]->mappings]){++i;continue;} nuclear@1: for(j=0;j=map[j] && reqcodec_setup; nuclear@1: ve_setup_data_template *setup=NULL; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: nuclear@1: if(ci==NULL)return(OV_EINVAL); nuclear@1: if(!hi->impulse_block_p)i0=1; nuclear@1: nuclear@1: /* too low/high an ATH floater is nonsensical, but doesn't break anything */ nuclear@1: if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80; nuclear@1: if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200; nuclear@1: nuclear@1: /* again, bound this to avoid the app shooting itself int he foot nuclear@1: too badly */ nuclear@1: if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.; nuclear@1: if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.; nuclear@1: nuclear@1: /* get the appropriate setup template; matches the fetch in previous nuclear@1: stages */ nuclear@1: setup=(ve_setup_data_template *)hi->setup; nuclear@1: if(setup==NULL)return(OV_EINVAL); nuclear@1: nuclear@1: hi->set_in_stone=1; nuclear@1: /* choose block sizes from configured sizes as well as paying nuclear@1: attention to long_block_p and short_block_p. If the configured nuclear@1: short and long blocks are the same length, we set long_block_p nuclear@1: and unset short_block_p */ nuclear@1: vorbis_encode_blocksize_setup(vi,hi->base_setting, nuclear@1: setup->blocksize_short, nuclear@1: setup->blocksize_long); nuclear@1: if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1; nuclear@1: nuclear@1: /* floor setup; choose proper floor params. Allocated on the floor nuclear@1: stack in order; if we alloc only a single long floor, it's 0 */ nuclear@1: for(i=0;ifloor_mappings;i++) nuclear@1: vorbis_encode_floor_setup(vi,hi->base_setting, nuclear@1: setup->floor_books, nuclear@1: setup->floor_params, nuclear@1: setup->floor_mapping_list[i]); nuclear@1: nuclear@1: /* setup of [mostly] short block detection and stereo*/ nuclear@1: vorbis_encode_global_psych_setup(vi,hi->trigger_setting, nuclear@1: setup->global_params, nuclear@1: setup->global_mapping); nuclear@1: vorbis_encode_global_stereo(vi,hi,setup->stereo_modes); nuclear@1: nuclear@1: /* basic psych setup and noise normalization */ nuclear@1: vorbis_encode_psyset_setup(vi,hi->base_setting, nuclear@1: setup->psy_noise_normal_start[0], nuclear@1: setup->psy_noise_normal_partition[0], nuclear@1: setup->psy_noise_normal_thresh, nuclear@1: 0); nuclear@1: vorbis_encode_psyset_setup(vi,hi->base_setting, nuclear@1: setup->psy_noise_normal_start[0], nuclear@1: setup->psy_noise_normal_partition[0], nuclear@1: setup->psy_noise_normal_thresh, nuclear@1: 1); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_psyset_setup(vi,hi->base_setting, nuclear@1: setup->psy_noise_normal_start[1], nuclear@1: setup->psy_noise_normal_partition[1], nuclear@1: setup->psy_noise_normal_thresh, nuclear@1: 2); nuclear@1: vorbis_encode_psyset_setup(vi,hi->base_setting, nuclear@1: setup->psy_noise_normal_start[1], nuclear@1: setup->psy_noise_normal_partition[1], nuclear@1: setup->psy_noise_normal_thresh, nuclear@1: 3); nuclear@1: } nuclear@1: nuclear@1: /* tone masking setup */ nuclear@1: vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0, nuclear@1: setup->psy_tone_masteratt, nuclear@1: setup->psy_tone_0dB, nuclear@1: setup->psy_tone_adj_impulse); nuclear@1: vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1, nuclear@1: setup->psy_tone_masteratt, nuclear@1: setup->psy_tone_0dB, nuclear@1: setup->psy_tone_adj_other); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2, nuclear@1: setup->psy_tone_masteratt, nuclear@1: setup->psy_tone_0dB, nuclear@1: setup->psy_tone_adj_other); nuclear@1: vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3, nuclear@1: setup->psy_tone_masteratt, nuclear@1: setup->psy_tone_0dB, nuclear@1: setup->psy_tone_adj_long); nuclear@1: } nuclear@1: nuclear@1: /* noise companding setup */ nuclear@1: vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0, nuclear@1: setup->psy_noise_compand, nuclear@1: setup->psy_noise_compand_short_mapping); nuclear@1: vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1, nuclear@1: setup->psy_noise_compand, nuclear@1: setup->psy_noise_compand_short_mapping); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2, nuclear@1: setup->psy_noise_compand, nuclear@1: setup->psy_noise_compand_long_mapping); nuclear@1: vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3, nuclear@1: setup->psy_noise_compand, nuclear@1: setup->psy_noise_compand_long_mapping); nuclear@1: } nuclear@1: nuclear@1: /* peak guarding setup */ nuclear@1: vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0, nuclear@1: setup->psy_tone_dBsuppress); nuclear@1: vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1, nuclear@1: setup->psy_tone_dBsuppress); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2, nuclear@1: setup->psy_tone_dBsuppress); nuclear@1: vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3, nuclear@1: setup->psy_tone_dBsuppress); nuclear@1: } nuclear@1: nuclear@1: /* noise bias setup */ nuclear@1: vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0, nuclear@1: setup->psy_noise_dBsuppress, nuclear@1: setup->psy_noise_bias_impulse, nuclear@1: setup->psy_noiseguards, nuclear@1: (i0==0?hi->impulse_noisetune:0.)); nuclear@1: vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1, nuclear@1: setup->psy_noise_dBsuppress, nuclear@1: setup->psy_noise_bias_padding, nuclear@1: setup->psy_noiseguards,0.); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2, nuclear@1: setup->psy_noise_dBsuppress, nuclear@1: setup->psy_noise_bias_trans, nuclear@1: setup->psy_noiseguards,0.); nuclear@1: vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3, nuclear@1: setup->psy_noise_dBsuppress, nuclear@1: setup->psy_noise_bias_long, nuclear@1: setup->psy_noiseguards,0.); nuclear@1: } nuclear@1: nuclear@1: vorbis_encode_ath_setup(vi,0); nuclear@1: vorbis_encode_ath_setup(vi,1); nuclear@1: if(!singleblock){ nuclear@1: vorbis_encode_ath_setup(vi,2); nuclear@1: vorbis_encode_ath_setup(vi,3); nuclear@1: } nuclear@1: nuclear@1: vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps); nuclear@1: nuclear@1: /* set bitrate readonlies and management */ nuclear@1: if(hi->bitrate_av>0) nuclear@1: vi->bitrate_nominal=hi->bitrate_av; nuclear@1: else{ nuclear@1: vi->bitrate_nominal=setting_to_approx_bitrate(vi); nuclear@1: } nuclear@1: nuclear@1: vi->bitrate_lower=hi->bitrate_min; nuclear@1: vi->bitrate_upper=hi->bitrate_max; nuclear@1: if(hi->bitrate_av) nuclear@1: vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av; nuclear@1: else nuclear@1: vi->bitrate_window=0.; nuclear@1: nuclear@1: if(hi->managed){ nuclear@1: ci->bi.avg_rate=hi->bitrate_av; nuclear@1: ci->bi.min_rate=hi->bitrate_min; nuclear@1: ci->bi.max_rate=hi->bitrate_max; nuclear@1: nuclear@1: ci->bi.reservoir_bits=hi->bitrate_reservoir; nuclear@1: ci->bi.reservoir_bias= nuclear@1: hi->bitrate_reservoir_bias; nuclear@1: nuclear@1: ci->bi.slew_damp=hi->bitrate_av_damp; nuclear@1: nuclear@1: } nuclear@1: nuclear@1: return(0); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: static void vorbis_encode_setup_setting(vorbis_info *vi, nuclear@1: long channels, nuclear@1: long rate){ nuclear@1: int i,is; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: const ve_setup_data_template *setup=hi->setup; nuclear@1: double ds; nuclear@1: nuclear@1: vi->version=0; nuclear@1: vi->channels=channels; nuclear@1: vi->rate=rate; nuclear@1: nuclear@1: hi->impulse_block_p=1; nuclear@1: hi->noise_normalize_p=1; nuclear@1: nuclear@1: is=hi->base_setting; nuclear@1: ds=hi->base_setting-is; nuclear@1: nuclear@1: hi->stereo_point_setting=hi->base_setting; nuclear@1: nuclear@1: if(!hi->lowpass_altered) nuclear@1: hi->lowpass_kHz= nuclear@1: setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds; nuclear@1: nuclear@1: hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+ nuclear@1: setup->psy_ath_float[is+1]*ds; nuclear@1: hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+ nuclear@1: setup->psy_ath_abs[is+1]*ds; nuclear@1: nuclear@1: hi->amplitude_track_dBpersec=-6.; nuclear@1: hi->trigger_setting=hi->base_setting; nuclear@1: nuclear@1: for(i=0;i<4;i++){ nuclear@1: hi->block[i].tone_mask_setting=hi->base_setting; nuclear@1: hi->block[i].tone_peaklimit_setting=hi->base_setting; nuclear@1: hi->block[i].noise_bias_setting=hi->base_setting; nuclear@1: hi->block[i].noise_compand_setting=hi->base_setting; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: int vorbis_encode_setup_vbr(vorbis_info *vi, nuclear@1: long channels, nuclear@1: long rate, nuclear@1: float quality){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: nuclear@1: quality+=.0000001; nuclear@1: if(quality>=1.)quality=.9999; nuclear@1: nuclear@1: hi->req=quality; nuclear@1: hi->setup=get_setup_template(channels,rate,quality,0,&hi->base_setting); nuclear@1: if(!hi->setup)return OV_EIMPL; nuclear@1: nuclear@1: vorbis_encode_setup_setting(vi,channels,rate); nuclear@1: hi->managed=0; nuclear@1: hi->coupling_p=1; nuclear@1: nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: int vorbis_encode_init_vbr(vorbis_info *vi, nuclear@1: long channels, nuclear@1: long rate, nuclear@1: nuclear@1: float base_quality /* 0. to 1. */ nuclear@1: ){ nuclear@1: int ret=0; nuclear@1: nuclear@1: ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality); nuclear@1: nuclear@1: if(ret){ nuclear@1: vorbis_info_clear(vi); nuclear@1: return ret; nuclear@1: } nuclear@1: ret=vorbis_encode_setup_init(vi); nuclear@1: if(ret) nuclear@1: vorbis_info_clear(vi); nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: int vorbis_encode_setup_managed(vorbis_info *vi, nuclear@1: long channels, nuclear@1: long rate, nuclear@1: nuclear@1: long max_bitrate, nuclear@1: long nominal_bitrate, nuclear@1: long min_bitrate){ nuclear@1: nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: double tnominal=nominal_bitrate; nuclear@1: nuclear@1: if(nominal_bitrate<=0.){ nuclear@1: if(max_bitrate>0.){ nuclear@1: if(min_bitrate>0.) nuclear@1: nominal_bitrate=(max_bitrate+min_bitrate)*.5; nuclear@1: else nuclear@1: nominal_bitrate=max_bitrate*.875; nuclear@1: }else{ nuclear@1: if(min_bitrate>0.){ nuclear@1: nominal_bitrate=min_bitrate; nuclear@1: }else{ nuclear@1: return(OV_EINVAL); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: hi->req=nominal_bitrate; nuclear@1: hi->setup=get_setup_template(channels,rate,nominal_bitrate,1,&hi->base_setting); nuclear@1: if(!hi->setup)return OV_EIMPL; nuclear@1: nuclear@1: vorbis_encode_setup_setting(vi,channels,rate); nuclear@1: nuclear@1: /* initialize management with sane defaults */ nuclear@1: hi->coupling_p=1; nuclear@1: hi->managed=1; nuclear@1: hi->bitrate_min=min_bitrate; nuclear@1: hi->bitrate_max=max_bitrate; nuclear@1: hi->bitrate_av=tnominal; nuclear@1: hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */ nuclear@1: hi->bitrate_reservoir=nominal_bitrate*2; nuclear@1: hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */ nuclear@1: nuclear@1: return(0); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: int vorbis_encode_init(vorbis_info *vi, nuclear@1: long channels, nuclear@1: long rate, nuclear@1: nuclear@1: long max_bitrate, nuclear@1: long nominal_bitrate, nuclear@1: long min_bitrate){ nuclear@1: nuclear@1: int ret=vorbis_encode_setup_managed(vi,channels,rate, nuclear@1: max_bitrate, nuclear@1: nominal_bitrate, nuclear@1: min_bitrate); nuclear@1: if(ret){ nuclear@1: vorbis_info_clear(vi); nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: ret=vorbis_encode_setup_init(vi); nuclear@1: if(ret) nuclear@1: vorbis_info_clear(vi); nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){ nuclear@1: if(vi){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: highlevel_encode_setup *hi=&ci->hi; nuclear@1: int setp=(number&0xf); /* a read request has a low nibble of 0 */ nuclear@1: nuclear@1: if(setp && hi->set_in_stone)return(OV_EINVAL); nuclear@1: nuclear@1: switch(number){ nuclear@1: nuclear@1: /* now deprecated *****************/ nuclear@1: case OV_ECTL_RATEMANAGE_GET: nuclear@1: { nuclear@1: nuclear@1: struct ovectl_ratemanage_arg *ai= nuclear@1: (struct ovectl_ratemanage_arg *)arg; nuclear@1: nuclear@1: ai->management_active=hi->managed; nuclear@1: ai->bitrate_hard_window=ai->bitrate_av_window= nuclear@1: (double)hi->bitrate_reservoir/vi->rate; nuclear@1: ai->bitrate_av_window_center=1.; nuclear@1: ai->bitrate_hard_min=hi->bitrate_min; nuclear@1: ai->bitrate_hard_max=hi->bitrate_max; nuclear@1: ai->bitrate_av_lo=hi->bitrate_av; nuclear@1: ai->bitrate_av_hi=hi->bitrate_av; nuclear@1: nuclear@1: } nuclear@1: return(0); nuclear@1: nuclear@1: /* now deprecated *****************/ nuclear@1: case OV_ECTL_RATEMANAGE_SET: nuclear@1: { nuclear@1: struct ovectl_ratemanage_arg *ai= nuclear@1: (struct ovectl_ratemanage_arg *)arg; nuclear@1: if(ai==NULL){ nuclear@1: hi->managed=0; nuclear@1: }else{ nuclear@1: hi->managed=ai->management_active; nuclear@1: vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg); nuclear@1: vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg); nuclear@1: } nuclear@1: } nuclear@1: return 0; nuclear@1: nuclear@1: /* now deprecated *****************/ nuclear@1: case OV_ECTL_RATEMANAGE_AVG: nuclear@1: { nuclear@1: struct ovectl_ratemanage_arg *ai= nuclear@1: (struct ovectl_ratemanage_arg *)arg; nuclear@1: if(ai==NULL){ nuclear@1: hi->bitrate_av=0; nuclear@1: }else{ nuclear@1: hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5; nuclear@1: } nuclear@1: } nuclear@1: return(0); nuclear@1: /* now deprecated *****************/ nuclear@1: case OV_ECTL_RATEMANAGE_HARD: nuclear@1: { nuclear@1: struct ovectl_ratemanage_arg *ai= nuclear@1: (struct ovectl_ratemanage_arg *)arg; nuclear@1: if(ai==NULL){ nuclear@1: hi->bitrate_min=0; nuclear@1: hi->bitrate_max=0; nuclear@1: }else{ nuclear@1: hi->bitrate_min=ai->bitrate_hard_min; nuclear@1: hi->bitrate_max=ai->bitrate_hard_max; nuclear@1: hi->bitrate_reservoir=ai->bitrate_hard_window* nuclear@1: (hi->bitrate_max+hi->bitrate_min)*.5; nuclear@1: } nuclear@1: if(hi->bitrate_reservoir<128.) nuclear@1: hi->bitrate_reservoir=128.; nuclear@1: } nuclear@1: return(0); nuclear@1: nuclear@1: /* replacement ratemanage interface */ nuclear@1: case OV_ECTL_RATEMANAGE2_GET: nuclear@1: { nuclear@1: struct ovectl_ratemanage2_arg *ai= nuclear@1: (struct ovectl_ratemanage2_arg *)arg; nuclear@1: if(ai==NULL)return OV_EINVAL; nuclear@1: nuclear@1: ai->management_active=hi->managed; nuclear@1: ai->bitrate_limit_min_kbps=hi->bitrate_min/1000; nuclear@1: ai->bitrate_limit_max_kbps=hi->bitrate_max/1000; nuclear@1: ai->bitrate_average_kbps=hi->bitrate_av/1000; nuclear@1: ai->bitrate_average_damping=hi->bitrate_av_damp; nuclear@1: ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir; nuclear@1: ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias; nuclear@1: } nuclear@1: return (0); nuclear@1: case OV_ECTL_RATEMANAGE2_SET: nuclear@1: { nuclear@1: struct ovectl_ratemanage2_arg *ai= nuclear@1: (struct ovectl_ratemanage2_arg *)arg; nuclear@1: if(ai==NULL){ nuclear@1: hi->managed=0; nuclear@1: }else{ nuclear@1: /* sanity check; only catch invariant violations */ nuclear@1: if(ai->bitrate_limit_min_kbps>0 && nuclear@1: ai->bitrate_average_kbps>0 && nuclear@1: ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_limit_max_kbps>0 && nuclear@1: ai->bitrate_average_kbps>0 && nuclear@1: ai->bitrate_limit_max_kbpsbitrate_average_kbps) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_limit_min_kbps>0 && nuclear@1: ai->bitrate_limit_max_kbps>0 && nuclear@1: ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_average_damping <= 0.) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_limit_reservoir_bits < 0) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_limit_reservoir_bias < 0.) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: if(ai->bitrate_limit_reservoir_bias > 1.) nuclear@1: return OV_EINVAL; nuclear@1: nuclear@1: hi->managed=ai->management_active; nuclear@1: hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000; nuclear@1: hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000; nuclear@1: hi->bitrate_av=ai->bitrate_average_kbps * 1000; nuclear@1: hi->bitrate_av_damp=ai->bitrate_average_damping; nuclear@1: hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits; nuclear@1: hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias; nuclear@1: } nuclear@1: } nuclear@1: return 0; nuclear@1: nuclear@1: case OV_ECTL_LOWPASS_GET: nuclear@1: { nuclear@1: double *farg=(double *)arg; nuclear@1: *farg=hi->lowpass_kHz; nuclear@1: } nuclear@1: return(0); nuclear@1: case OV_ECTL_LOWPASS_SET: nuclear@1: { nuclear@1: double *farg=(double *)arg; nuclear@1: hi->lowpass_kHz=*farg; nuclear@1: nuclear@1: if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.; nuclear@1: if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.; nuclear@1: hi->lowpass_altered=1; nuclear@1: } nuclear@1: return(0); nuclear@1: case OV_ECTL_IBLOCK_GET: nuclear@1: { nuclear@1: double *farg=(double *)arg; nuclear@1: *farg=hi->impulse_noisetune; nuclear@1: } nuclear@1: return(0); nuclear@1: case OV_ECTL_IBLOCK_SET: nuclear@1: { nuclear@1: double *farg=(double *)arg; nuclear@1: hi->impulse_noisetune=*farg; nuclear@1: nuclear@1: if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.; nuclear@1: if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.; nuclear@1: } nuclear@1: return(0); nuclear@1: case OV_ECTL_COUPLING_GET: nuclear@1: { nuclear@1: int *iarg=(int *)arg; nuclear@1: *iarg=hi->coupling_p; nuclear@1: } nuclear@1: return(0); nuclear@1: case OV_ECTL_COUPLING_SET: nuclear@1: { nuclear@1: const void *new_template; nuclear@1: double new_base=0.; nuclear@1: int *iarg=(int *)arg; nuclear@1: hi->coupling_p=((*iarg)!=0); nuclear@1: nuclear@1: /* Fetching a new template can alter the base_setting, which nuclear@1: many other parameters are based on. Right now, the only nuclear@1: parameter drawn from the base_setting that can be altered nuclear@1: by an encctl is the lowpass, so that is explictly flagged nuclear@1: to not be overwritten when we fetch a new template and nuclear@1: recompute the dependant settings */ nuclear@1: new_template = get_setup_template(hi->coupling_p?vi->channels:-1, nuclear@1: vi->rate, nuclear@1: hi->req, nuclear@1: hi->managed, nuclear@1: &new_base); nuclear@1: if(!hi->setup)return OV_EIMPL; nuclear@1: hi->setup=new_template; nuclear@1: hi->base_setting=new_base; nuclear@1: vorbis_encode_setup_setting(vi,vi->channels,vi->rate); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: return(OV_EIMPL); nuclear@1: } nuclear@1: return(OV_EINVAL); nuclear@1: }