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