* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
- * by the XIPHOPHORUS Company http://www.xiph.org/ *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: simple programmatic interface for encoder mode setup
- last mod: $Id: vorbisenc.c,v 1.29 2001/12/21 08:44:07 xiphmont Exp $
+ last mod: $Id$
********************************************************************/
#include <stdlib.h>
#include <string.h>
#include <math.h>
-#include <stdarg.h>
#include "vorbis/codec.h"
#include "vorbis/vorbisenc.h"
#include "codec_internal.h"
-#include "registry-api.h"
#include "os.h"
#include "misc.h"
/* careful with this; it's using static array sizing to make managing
all the modes a little less annoying. If we use a residue backend
- with > 10 partition types, or a different division of iteration,
+ with > 12 partition types, or a different division of iteration,
this needs to be updated. */
typedef struct {
- vorbis_info_residue0 *res[2];
- static_codebook *book_aux[2];
- static_codebook *books_base[5][10][3];
- static_codebook *books_stereo_backfill[5][10];
- static_codebook *books_residue_backfill[5][10][2];
+ const static_codebook *books[12][4];
+} static_bookblock;
+
+typedef struct {
+ int res_type;
+ int limit_type; /* 0 lowpass limited, 1 point stereo limited */
+ int grouping;
+ const vorbis_info_residue0 *res;
+ const static_codebook *book_aux;
+ const static_codebook *book_aux_managed;
+ const static_bookblock *books_base;
+ const static_bookblock *books_base_managed;
} vorbis_residue_template;
-static double stereo_threshholds[]={0.0, 2.5, 4.5, 8.5, 16.5};
+typedef struct {
+ const vorbis_info_mapping0 *map;
+ const vorbis_residue_template *res;
+} vorbis_mapping_template;
typedef struct vp_adjblock{
- int block[P_BANDS][P_LEVELS];
+ int block[P_BANDS];
} vp_adjblock;
-#include "modes/residue_44.h"
-#include "modes/psych_44.h"
-#include "modes/floor_44.h"
+typedef struct {
+ int data[NOISE_COMPAND_LEVELS];
+} compandblock;
-/* a few static coder conventions */
-static vorbis_info_time0 _time_dummy={0};
-static vorbis_info_mode _mode_set_short={0,0,0,0};
-static vorbis_info_mode _mode_set_long={1,0,0,1};
-
-/* mapping conventions:
- only one submap (this would change for efficient 5.1 support for example)*/
-/* Four psychoacoustic profiles are used, one for each blocktype */
-static vorbis_info_mapping0 _mapping_set_short={
- 1, {0,0}, {0}, {0}, {0}, {0,1}, 0,{0},{0}};
-static vorbis_info_mapping0 _mapping_set_long={
- 1, {0,0}, {0}, {1}, {1}, {2,3}, 0,{0},{0}};
-
-static int vorbis_encode_toplevel_setup(vorbis_info *vi,int small,int large,int ch,long rate){
- if(vi && vi->codec_setup){
- codec_setup_info *ci=vi->codec_setup;
+/* high level configuration information for setting things up
+ step-by-step with the detailed vorbis_encode_ctl interface.
+ There's a fair amount of redundancy such that interactive setup
+ does not directly deal with any vorbis_info or codec_setup_info
+ initialization; it's all stored (until full init) in this highlevel
+ setup, then flushed out to the real codec setup structs later. */
- vi->version=0;
- vi->channels=ch;
- vi->rate=rate;
-
- ci->blocksizes[0]=small;
- ci->blocksizes[1]=large;
-
- /* time mapping hooks are unused in vorbis I */
- ci->times=1;
- ci->time_type[0]=0;
- ci->time_param[0]=calloc(1,sizeof(_time_dummy));
- memcpy(ci->time_param[0],&_time_dummy,sizeof(_time_dummy));
-
- /* by convention, two modes: one for short, one for long blocks.
- short block mode uses mapping sero, long block uses mapping 1 */
- ci->modes=2;
- ci->mode_param[0]=calloc(1,sizeof(_mode_set_short));
- memcpy(ci->mode_param[0],&_mode_set_short,sizeof(_mode_set_short));
- ci->mode_param[1]=calloc(1,sizeof(_mode_set_long));
- memcpy(ci->mode_param[1],&_mode_set_long,sizeof(_mode_set_long));
-
- /* by convention two mappings, both mapping type zero (polyphonic
- PCM), first for short, second for long blocks */
- ci->maps=2;
- ci->map_type[0]=0;
- ci->map_param[0]=calloc(1,sizeof(_mapping_set_short));
- memcpy(ci->map_param[0],&_mapping_set_short,sizeof(_mapping_set_short));
- ci->map_type[1]=0;
- ci->map_param[1]=calloc(1,sizeof(_mapping_set_long));
- memcpy(ci->map_param[1],&_mapping_set_long,sizeof(_mapping_set_long));
-
- return(0);
- }
- return(OV_EINVAL);
-}
+typedef struct {
+ int att[P_NOISECURVES];
+ float boost;
+ float decay;
+} att3;
+typedef struct { int data[P_NOISECURVES]; } adj3;
-static int vorbis_encode_floor_setup(vorbis_info *vi,double q,int block,
- static_codebook ***books,
- vorbis_info_floor1 *in,
- ...){
- int x[11],i,k,iq=rint(q*10);
- vorbis_info_floor1 *f=calloc(1,sizeof(*f));
- codec_setup_info *ci=vi->codec_setup;
- va_list ap;
+typedef struct {
+ int pre[PACKETBLOBS];
+ int post[PACKETBLOBS];
+ float kHz[PACKETBLOBS];
+ float lowpasskHz[PACKETBLOBS];
+} adj_stereo;
+
+typedef struct {
+ int lo;
+ int hi;
+ int fixed;
+} noiseguard;
+typedef struct {
+ int data[P_NOISECURVES][17];
+} noise3;
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,int);
- va_end(ap);
+typedef struct {
+ int mappings;
+ const double *rate_mapping;
+ const double *quality_mapping;
+ int coupling_restriction;
+ long samplerate_min_restriction;
+ long samplerate_max_restriction;
+
+
+ const int *blocksize_short;
+ const int *blocksize_long;
+
+ const att3 *psy_tone_masteratt;
+ const int *psy_tone_0dB;
+ const int *psy_tone_dBsuppress;
+
+ const vp_adjblock *psy_tone_adj_impulse;
+ const vp_adjblock *psy_tone_adj_long;
+ const vp_adjblock *psy_tone_adj_other;
+
+ const noiseguard *psy_noiseguards;
+ const noise3 *psy_noise_bias_impulse;
+ const noise3 *psy_noise_bias_padding;
+ const noise3 *psy_noise_bias_trans;
+ const noise3 *psy_noise_bias_long;
+ const int *psy_noise_dBsuppress;
+
+ const compandblock *psy_noise_compand;
+ const double *psy_noise_compand_short_mapping;
+ const double *psy_noise_compand_long_mapping;
+
+ const int *psy_noise_normal_start[2];
+ const int *psy_noise_normal_partition[2];
+ const double *psy_noise_normal_thresh;
- memcpy(f,in+x[iq],sizeof(*f));
- /* fill in the lowpass field, even if it's temporary */
- f->n=ci->blocksizes[block]>>1;
+ const int *psy_ath_float;
+ const int *psy_ath_abs;
+
+ const double *psy_lowpass;
+
+ const vorbis_info_psy_global *global_params;
+ const double *global_mapping;
+ const adj_stereo *stereo_modes;
+
+ const static_codebook *const *const *const floor_books;
+ const vorbis_info_floor1 *floor_params;
+ const int floor_mappings;
+ const int **floor_mapping_list;
+
+ const vorbis_mapping_template *maps;
+} ve_setup_data_template;
+
+/* a few static coder conventions */
+static const vorbis_info_mode _mode_template[2]={
+ {0,0,0,0},
+ {1,0,0,1}
+};
+
+static const vorbis_info_mapping0 _map_nominal[2]={
+ {1, {0,0}, {0}, {0}, 1,{0},{1}},
+ {1, {0,0}, {1}, {1}, 1,{0},{1}}
+};
+
+#include "modes/setup_44.h"
+#include "modes/setup_44u.h"
+#include "modes/setup_44p51.h"
+#include "modes/setup_32.h"
+#include "modes/setup_8.h"
+#include "modes/setup_11.h"
+#include "modes/setup_16.h"
+#include "modes/setup_22.h"
+#include "modes/setup_X.h"
+
+static const ve_setup_data_template *const setup_list[]={
+ &ve_setup_44_stereo,
+ &ve_setup_44_51,
+ &ve_setup_44_uncoupled,
+
+ &ve_setup_32_stereo,
+ &ve_setup_32_uncoupled,
+
+ &ve_setup_22_stereo,
+ &ve_setup_22_uncoupled,
+ &ve_setup_16_stereo,
+ &ve_setup_16_uncoupled,
+
+ &ve_setup_11_stereo,
+ &ve_setup_11_uncoupled,
+ &ve_setup_8_stereo,
+ &ve_setup_8_uncoupled,
+
+ &ve_setup_X_stereo,
+ &ve_setup_X_uncoupled,
+ &ve_setup_XX_stereo,
+ &ve_setup_XX_uncoupled,
+ 0
+};
+
+static void vorbis_encode_floor_setup(vorbis_info *vi,int s,
+ const static_codebook *const *const *const books,
+ const vorbis_info_floor1 *in,
+ const int *x){
+ int i,k,is=s;
+ vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
+ codec_setup_info *ci=vi->codec_setup;
+
+ memcpy(f,in+x[is],sizeof(*f));
/* books */
{
if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
f->class_book[i]+=ci->books;
for(k=0;k<(1<<f->class_subs[i]);k++){
- if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
- if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
+ if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
+ if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
}
}
for(i=0;i<=maxbook;i++)
- ci->book_param[ci->books++]=books[x[iq]][i];
+ ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i];
}
/* for now, we're only using floor 1 */
ci->floor_param[ci->floors]=f;
ci->floors++;
- return(0);
+ return;
}
-static int vorbis_encode_global_psych_setup(vorbis_info *vi,double q,
- vorbis_info_psy_global *in, ...){
- int i,iq=q*10;
- double x[11],dq;
+static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
+ const vorbis_info_psy_global *in,
+ const double *x){
+ int i,is=s;
+ double ds=s-is;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy_global *g=&ci->psy_g_param;
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
-
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- memcpy(g,in+(int)x[iq],sizeof(*g));
+ memcpy(g,in+(int)x[is],sizeof(*g));
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
+ ds=x[is]*(1.-ds)+x[is+1]*ds;
+ is=(int)ds;
+ ds-=is;
+ if(ds==0 && is>0){
+ is--;
+ ds=1.;
}
/* interpolate the trigger threshholds */
for(i=0;i<4;i++){
- g->preecho_thresh[i]=in[iq].preecho_thresh[i]*(1.-dq)+in[iq+1].preecho_thresh[i]*dq;
- g->postecho_thresh[i]=in[iq].postecho_thresh[i]*(1.-dq)+in[iq+1].postecho_thresh[i]*dq;
+ g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
+ g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
}
g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
- return(0);
+ return;
}
-static int vorbis_encode_psyset_setup(vorbis_info *vi,int block){
+static void vorbis_encode_global_stereo(vorbis_info *vi,
+ const highlevel_encode_setup *const hi,
+ const adj_stereo *p){
+ float s=hi->stereo_point_setting;
+ int i,is=s;
+ double ds=s-is;
+ codec_setup_info *ci=vi->codec_setup;
+ vorbis_info_psy_global *g=&ci->psy_g_param;
+
+ if(p){
+ memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
+ memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
+
+ if(hi->managed){
+ /* interpolate the kHz threshholds */
+ for(i=0;i<PACKETBLOBS;i++){
+ float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
+ g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
+ g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
+ g->coupling_pkHz[i]=kHz;
+
+ kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
+ g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
+ g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
+
+ }
+ }else{
+ float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
+ for(i=0;i<PACKETBLOBS;i++){
+ g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
+ g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
+ g->coupling_pkHz[i]=kHz;
+ }
+
+ kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
+ for(i=0;i<PACKETBLOBS;i++){
+ g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
+ g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
+ }
+ }
+ }else{
+ for(i=0;i<PACKETBLOBS;i++){
+ g->sliding_lowpass[0][i]=ci->blocksizes[0];
+ g->sliding_lowpass[1][i]=ci->blocksizes[1];
+ }
+ }
+ return;
+}
+
+static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
+ const int *nn_start,
+ const int *nn_partition,
+ const double *nn_thresh,
+ int block){
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
+ highlevel_encode_setup *hi=&ci->hi;
+ int is=s;
if(block>=ci->psys)
ci->psys=block+1;
if(!p){
- p=calloc(1,sizeof(*p));
+ p=_ogg_calloc(1,sizeof(*p));
ci->psy_param[block]=p;
}
memcpy(p,&_psy_info_template,sizeof(*p));
+ p->blockflag=block>>1;
- return 0;
+ if(hi->noise_normalize_p){
+ p->normal_p=1;
+ p->normal_start=nn_start[is];
+ p->normal_partition=nn_partition[is];
+ p->normal_thresh=nn_thresh[is];
+ }
+
+ return;
}
-static int vorbis_encode_tonemask_setup(vorbis_info *vi,double q,int block,
- double *att,
- double *max,
- int *peaklimit_bands,
- vp_adjblock *in){
- int i,j,iq;
- double dq;
+static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
+ const att3 *att,
+ const int *max,
+ const vp_adjblock *in){
+ int i,is=s;
+ double ds=s-is;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
- iq=q*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
+ /* 0 and 2 are only used by bitmanagement, but there's no harm to always
+ filling the values in here */
+ p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
+ p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
+ p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
+ p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
+ p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
- p->tone_masteratt=att[iq]*(1.-dq)+att[iq+1]*dq;
- p->max_curve_dB=max[iq];
- p->curvelimitp=peaklimit_bands[iq];
+ p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
- iq=q*5.;
- if(iq==5){
- iq=5;
- dq=1.;
- }else{
- dq=q*5.-iq;
- }
-
for(i=0;i<P_BANDS;i++)
- for(j=0;j<P_LEVELS;j++)
- p->toneatt.block[i][j]=(j<4?4:j)*-10.+
- in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
- return(0);
+ p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
+ return;
}
-static int vorbis_encode_compand_setup(vorbis_info *vi,double q,int block,
- float in[][NOISE_COMPAND_LEVELS], ...){
- int i,iq=q*10;
- double x[11],dq;
+static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
+ const compandblock *in,
+ const double *x){
+ int i,is=s;
+ double ds=s-is;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
-
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
+ ds=x[is]*(1.-ds)+x[is+1]*ds;
+ is=(int)ds;
+ ds-=is;
+ if(ds==0 && is>0){
+ is--;
+ ds=1.;
}
/* interpolate the compander settings */
for(i=0;i<NOISE_COMPAND_LEVELS;i++)
- p->noisecompand[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
+ p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
+ return;
}
-static int vorbis_encode_peak_setup(vorbis_info *vi,double q,int block,
- double *guard,
- double *suppress,
- vp_adjblock *in){
- int i,j,iq;
- double dq;
+static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
+ const int *suppress){
+ int is=s;
+ double ds=s-is;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
- iq=q*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
-
- p->peakattp=1;
- p->tone_guard=guard[iq]*(1.-dq)+guard[iq+1]*dq;
- p->tone_abs_limit=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
+ p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
- iq=q*5.;
- if(iq==5){
- iq=5;
- dq=1.;
- }else{
- dq=q*5.-iq;
- }
-
- for(i=0;i<P_BANDS;i++)
- for(j=0;j<P_LEVELS;j++)
- p->peakatt.block[i][j]=(j<4?4:j)*-10.+
- in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
- return(0);
+ return;
}
-static int vorbis_encode_noisebias_setup(vorbis_info *vi,double q,int block,
- double *suppress,
- int in[][17],int guard[33]){
- int i,iq=q*10;
- double dq;
+static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
+ const int *suppress,
+ const noise3 *in,
+ const noiseguard *guard,
+ double userbias){
+ int i,is=s,j;
+ double ds=s-is;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
+ p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
+ p->noisewindowlomin=guard[block].lo;
+ p->noisewindowhimin=guard[block].hi;
+ p->noisewindowfixed=guard[block].fixed;
+
+ for(j=0;j<P_NOISECURVES;j++)
+ for(i=0;i<P_BANDS;i++)
+ p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
+
+ /* impulse blocks may take a user specified bias to boost the
+ nominal/high noise encoding depth */
+ for(j=0;j<P_NOISECURVES;j++){
+ float min=p->noiseoff[j][0]+6; /* the lowest it can go */
+ for(i=0;i<P_BANDS;i++){
+ p->noiseoff[j][i]+=userbias;
+ if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
+ }
}
- p->noisemaxsupp=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
- p->noisewindowlomin=guard[iq*3];
- p->noisewindowhimin=guard[iq*3+1];
- p->noisewindowfixed=guard[iq*3+2];
-
- for(i=0;i<P_BANDS;i++)
- p->noiseoff[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
+ return;
}
-static int vorbis_encode_ath_setup(vorbis_info *vi,double q,int block,
- float in[][27], ...){
- int i,iq=q*10;
- double x[11],dq;
+static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
codec_setup_info *ci=vi->codec_setup;
vorbis_info_psy *p=ci->psy_param[block];
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
p->ath_adjatt=ci->hi.ath_floating_dB;
p->ath_maxatt=ci->hi.ath_absolute_dB;
-
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
-
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
- }
-
- for(i=0;i<27;i++)
- p->ath[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
+ return;
}
-static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
+static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){
int i;
for(i=0;i<ci->books;i++)
if(ci->book_param[i]==book)return(i);
-
+
return(ci->books++);
}
-static int vorbis_encode_residue_setup(vorbis_info *vi,double q,int block,
- int coupled_p,
- int stereo_backfill_p,
- int residue_backfill_p,
- vorbis_residue_template *in,
- int point_dB,
- double point_kHz){
-
- int i,iq=q*10;
- int n,k;
- int partition_position=0;
- int res_position=0;
- int iterations=1;
- int amplitude_select=0;
+static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
+ const int *shortb,const int *longb){
codec_setup_info *ci=vi->codec_setup;
- vorbis_info_residue0 *r;
- vorbis_info_psy *psy=ci->psy_param[block*2];
-
- /* may be re-called due to ctl */
- if(ci->residue_param[block])
- /* free preexisting instance */
- residue_free_info(ci->residue_param[block],ci->residue_type[block]);
-
- r=ci->residue_param[block]=malloc(sizeof(*r));
- memcpy(r,in[iq].res[block],sizeof(*r));
- if(ci->residues<=block)ci->residues=block+1;
-
- if(block){
- r->grouping=32;
- }else{
- r->grouping=16;
- }
+ int is=s;
- /* for uncoupled, we use type 1, else type 2 */
- if(coupled_p){
- ci->residue_type[block]=2;
- }else{
- ci->residue_type[block]=1;
- }
+ int blockshort=shortb[is];
+ int blocklong=longb[is];
+ ci->blocksizes[0]=blockshort;
+ ci->blocksizes[1]=blocklong;
- switch(ci->residue_type[block]){
- case 1:
- n=r->end=ci->blocksizes[block?1:0]>>1; /* to be adjusted by lowpass later */
- partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
- res_position=partition_position*r->grouping;
- break;
- case 2:
- n=r->end=(ci->blocksizes[block?1:0]>>1)*vi->channels; /* to be adjusted by lowpass later */
- partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
- res_position=partition_position*r->grouping/vi->channels;
- break;
- }
+}
- for(i=0;i<r->partitions;i++)
- if(r->blimit[i]<0)r->blimit[i]=partition_position;
-
- for(i=0;i<r->partitions;i++)
- for(k=0;k<3;k++)
- if(in[iq].books_base[point_dB][i][k])
- r->secondstages[i]|=(1<<k);
-
- ci->passlimit[0]=3;
-
- if(coupled_p){
- vorbis_info_mapping0 *map=ci->map_param[block];
-
- map->coupling_steps=1;
- map->coupling_mag[0]=0;
- map->coupling_ang[0]=1;
-
- psy->couple_pass[0].granulem=1.;
- psy->couple_pass[0].igranulem=1.;
-
- psy->couple_pass[0].couple_pass[0].limit=res_position;
- psy->couple_pass[0].couple_pass[0].outofphase_redundant_flip_p=1;
- psy->couple_pass[0].couple_pass[0].outofphase_requant_limit=9e10;
- psy->couple_pass[0].couple_pass[0].amppost_point=0;
- psy->couple_pass[0].couple_pass[1].limit=9999;
- psy->couple_pass[0].couple_pass[1].outofphase_redundant_flip_p=1;
- psy->couple_pass[0].couple_pass[1].outofphase_requant_limit=9e10;
- psy->couple_pass[0].couple_pass[1].amppost_point=
- stereo_threshholds[point_dB];
- amplitude_select=point_dB;
-
- if(stereo_backfill_p && amplitude_select){
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[1].couple_pass[1].amppost_point=stereo_threshholds[amplitude_select-1];
- ci->passlimit[1]=4;
- for(i=0;i<r->partitions;i++)
- if(in[iq].books_stereo_backfill[amplitude_select][i])
- r->secondstages[i]|=8;
- amplitude_select=amplitude_select-1;
- iterations++;
- }
-
- if(residue_backfill_p){
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[iterations].granulem=.333333333;
- psy->couple_pass[iterations].igranulem=3.;
- psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=1.;
- psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=1.;
+static void vorbis_encode_residue_setup(vorbis_info *vi,
+ int number, int block,
+ const vorbis_residue_template *res){
+
+ codec_setup_info *ci=vi->codec_setup;
+ int i;
+
+ vorbis_info_residue0 *r=ci->residue_param[number]=
+ _ogg_malloc(sizeof(*r));
+
+ memcpy(r,res->res,sizeof(*r));
+ if(ci->residues<=number)ci->residues=number+1;
+
+ r->grouping=res->grouping;
+ ci->residue_type[number]=res->res_type;
+
+ /* fill in all the books */
+ {
+ int booklist=0,k;
+
+ if(ci->hi.managed){
for(i=0;i<r->partitions;i++)
- if(in[iq].books_residue_backfill[amplitude_select][i][0])
- r->secondstages[i]|=(1<<(iterations+2));
- ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
- iterations++;
-
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[iterations].granulem=.1111111111;
- psy->couple_pass[iterations].igranulem=9.;
- psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=.3;
- psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=.3;
+ for(k=0;k<4;k++)
+ if(res->books_base_managed->books[i][k])
+ r->secondstages[i]|=(1<<k);
+
+ r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
+ ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed;
+
+ for(i=0;i<r->partitions;i++){
+ for(k=0;k<4;k++){
+ if(res->books_base_managed->books[i][k]){
+ int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
+ r->booklist[booklist++]=bookid;
+ ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k];
+ }
+ }
+ }
+
+ }else{
+
for(i=0;i<r->partitions;i++)
- if(in[iq].books_residue_backfill[amplitude_select][i][1])
- r->secondstages[i]|=(1<<(iterations+2));
- ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
- iterations++;
- }
- ci->coupling_passes=iterations;
+ for(k=0;k<4;k++)
+ if(res->books_base->books[i][k])
+ r->secondstages[i]|=(1<<k);
- }else{
+ r->groupbook=book_dup_or_new(ci,res->book_aux);
+ ci->book_param[r->groupbook]=(static_codebook *)res->book_aux;
- if(residue_backfill_p){
for(i=0;i<r->partitions;i++){
- if(in[iq].books_residue_backfill[0][i][0])
- r->secondstages[i]|=8;
- if(in[iq].books_residue_backfill[0][i][1])
- r->secondstages[i]|=16;
+ for(k=0;k<4;k++){
+ if(res->books_base->books[i][k]){
+ int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
+ r->booklist[booklist++]=bookid;
+ ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k];
+ }
+ }
}
- ci->passlimit[1]=4;
- ci->passlimit[2]=5;
- ci->coupling_passes=3;
- }else
- ci->coupling_passes=1;
+ }
}
-
- memcpy(&ci->psy_param[block*2+1]->couple_pass,
- &ci->psy_param[block*2]->couple_pass,
- sizeof(psy->couple_pass));
-
- /* fill in all the books */
+
+ /* lowpass setup/pointlimit */
{
- int booklist=0,k;
- r->groupbook=ci->books;
- ci->book_param[ci->books++]=in[iq].book_aux[block];
- for(i=0;i<r->partitions;i++){
- for(k=0;k<3;k++){
- if(in[iq].books_base[point_dB][i][k]){
- int bookid=book_dup_or_new(ci,in[iq].books_base[point_dB][i][k]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_base[point_dB][i][k];
- }
- }
- if(coupled_p && stereo_backfill_p && point_dB &&
- in[iq].books_stereo_backfill[point_dB][i]){
- int bookid=book_dup_or_new(ci,in[iq].books_stereo_backfill[point_dB][i]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_stereo_backfill[point_dB][i];
- }
- if(residue_backfill_p){
- for(k=0;k<2;k++){
- if(in[iq].books_residue_backfill[amplitude_select][i][k]){
- int bookid=book_dup_or_new(ci,in[iq].books_residue_backfill[amplitude_select][i][k]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_residue_backfill[amplitude_select][i][k];
- }
- }
+ double freq=ci->hi.lowpass_kHz*1000.;
+ vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
+ double nyq=vi->rate/2.;
+ long blocksize=ci->blocksizes[block]>>1;
+
+ /* lowpass needs to be set in the floor and the residue. */
+ if(freq>nyq)freq=nyq;
+ /* in the floor, the granularity can be very fine; it doesn't alter
+ the encoding structure, only the samples used to fit the floor
+ approximation */
+ f->n=freq/nyq*blocksize;
+
+ /* this res may by limited by the maximum pointlimit of the mode,
+ not the lowpass. the floor is always lowpass limited. */
+ switch(res->limit_type){
+ case 1: /* point stereo limited */
+ if(ci->hi.managed)
+ freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
+ else
+ freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
+ if(freq>nyq)freq=nyq;
+ break;
+ case 2: /* LFE channel; lowpass at ~ 250Hz */
+ freq=250;
+ break;
+ default:
+ /* already set */
+ break;
+ }
+
+ /* in the residue, we're constrained, physically, by partition
+ boundaries. We still lowpass 'wherever', but we have to round up
+ here to next boundary, or the vorbis spec will round it *down* to
+ previous boundary in encode/decode */
+ if(ci->residue_type[number]==2){
+ /* residue 2 bundles together multiple channels; used by stereo
+ and surround. Count the channels in use */
+ /* Multiple maps/submaps can point to the same residue. In the case
+ of residue 2, they all better have the same number of
+ channels/samples. */
+ int j,k,ch=0;
+ for(i=0;i<ci->maps&&ch==0;i++){
+ vorbis_info_mapping0 *mi=(vorbis_info_mapping0 *)ci->map_param[i];
+ for(j=0;j<mi->submaps && ch==0;j++)
+ if(mi->residuesubmap[j]==number) /* we found a submap referencing theis residue backend */
+ for(k=0;k<vi->channels;k++)
+ if(mi->chmuxlist[k]==j) /* this channel belongs to the submap */
+ ch++;
}
+
+ r->end=(int)((freq/nyq*blocksize*ch)/r->grouping+.9)* /* round up only if we're well past */
+ r->grouping;
+ /* the blocksize and grouping may disagree at the end */
+ if(r->end>blocksize*ch)r->end=blocksize*ch/r->grouping*r->grouping;
+
+ }else{
+
+ r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
+ r->grouping;
+ /* the blocksize and grouping may disagree at the end */
+ if(r->end>blocksize)r->end=blocksize/r->grouping*r->grouping;
+
}
+
+ if(r->end==0)r->end=r->grouping; /* LFE channel */
+
}
+}
- return(0);
-}
+/* we assume two maps in this encoder */
+static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
+ const vorbis_mapping_template *maps){
-static int vorbis_encode_lowpass_setup(vorbis_info *vi,double q,int block){
- int iq=q*10;
- double dq;
- double freq;
codec_setup_info *ci=vi->codec_setup;
- vorbis_info_floor1 *f=ci->floor_param[block];
- vorbis_info_residue0 *r=ci->residue_param[block];
- int blocksize=ci->blocksizes[block]>>1;
- double nyq=vi->rate/2.;
-
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
+ int i,j,is=s,modes=2;
+ const vorbis_info_mapping0 *map=maps[is].map;
+ const vorbis_info_mode *mode=_mode_template;
+ const vorbis_residue_template *res=maps[is].res;
+
+ if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
+
+ for(i=0;i<modes;i++){
+
+ ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
+ ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
+
+ memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
+ if(i>=ci->modes)ci->modes=i+1;
+
+ ci->map_type[i]=0;
+ memcpy(ci->map_param[i],map+i,sizeof(*map));
+ if(i>=ci->maps)ci->maps=i+1;
+
+ for(j=0;j<map[i].submaps;j++)
+ vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
+ ,res+map[i].residuesubmap[j]);
}
-
- freq=ci->hi.lowpass_kHz[block]*1000.;
- if(freq>vi->rate/2)freq=vi->rate/2;
- /* lowpass needs to be set in the floor and the residue. */
-
- /* in the floor, the granularity can be very fine; it doesn't alter
- the encoding structure, only the samples used to fit the floor
- approximation */
- f->n=freq/nyq*blocksize;
-
- /* in the residue, we're constrained, physically, by partition
- boundaries. We still lowpass 'wherever', but we have to round up
- here to next boundary, or the vorbis spec will round it *down* to
- previous boundary in encode/decode */
- if(ci->residue_type[block]==2)
- r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
- r->grouping;
- else
- r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
- r->grouping;
- return(0);
+}
+
+static double setting_to_approx_bitrate(vorbis_info *vi){
+ codec_setup_info *ci=vi->codec_setup;
+ highlevel_encode_setup *hi=&ci->hi;
+ ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
+ int is=hi->base_setting;
+ double ds=hi->base_setting-is;
+ int ch=vi->channels;
+ const double *r=setup->rate_mapping;
+
+ if(r==NULL)
+ return(-1);
+
+ return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
+}
+
+static const void *get_setup_template(long ch,long srate,
+ double req,int q_or_bitrate,
+ double *base_setting){
+ int i=0,j;
+ if(q_or_bitrate)req/=ch;
+
+ while(setup_list[i]){
+ if(setup_list[i]->coupling_restriction==-1 ||
+ setup_list[i]->coupling_restriction==ch){
+ if(srate>=setup_list[i]->samplerate_min_restriction &&
+ srate<=setup_list[i]->samplerate_max_restriction){
+ int mappings=setup_list[i]->mappings;
+ const double *map=(q_or_bitrate?
+ setup_list[i]->rate_mapping:
+ setup_list[i]->quality_mapping);
+
+ /* the template matches. Does the requested quality mode
+ fall within this template's modes? */
+ if(req<map[0]){++i;continue;}
+ if(req>map[setup_list[i]->mappings]){++i;continue;}
+ for(j=0;j<mappings;j++)
+ if(req>=map[j] && req<map[j+1])break;
+ /* an all-points match */
+ if(j==mappings)
+ *base_setting=j-.001;
+ else{
+ float low=map[j];
+ float high=map[j+1];
+ float del=(req-low)/(high-low);
+ *base_setting=j+del;
+ }
+
+ return(setup_list[i]);
+ }
+ }
+ i++;
+ }
+
+ return NULL;
}
/* encoders will need to use vorbis_info_init beforehand and call
/* the final setup call */
int vorbis_encode_setup_init(vorbis_info *vi){
- int ret=0;
- /*long rate=vi->rate;*/
- long channels=vi->channels;
+ int i,i0=0,singleblock=0;
codec_setup_info *ci=vi->codec_setup;
+ ve_setup_data_template *setup=NULL;
highlevel_encode_setup *hi=&ci->hi;
- ret|=vorbis_encode_floor_setup(vi,hi->base_quality_short,0,
- _floor_44_128_books,_floor_44_128,
- 0,1,1,2,2,2,2,2,2,2,2);
- ret|=vorbis_encode_floor_setup(vi,hi->base_quality_long,1,
- _floor_44_1024_books,_floor_44_1024,
- 0,0,0,0,0,0,0,0,0,0,0);
-
- ret|=vorbis_encode_global_psych_setup(vi,hi->trigger_quality,_psy_global_44,
- 0., 1., 1.5, 2., 2., 2., 2., 2., 2., 2., 2.);
-
- ret|=vorbis_encode_psyset_setup(vi,0);
- ret|=vorbis_encode_psyset_setup(vi,1);
- ret|=vorbis_encode_psyset_setup(vi,2);
- ret|=vorbis_encode_psyset_setup(vi,3);
-
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[0].tone_mask_quality,0,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[1].tone_mask_quality,1,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[2].tone_mask_quality,2,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[3].tone_mask_quality,3,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_longblock);
-
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[0].noise_compand_quality,
- 0,_psy_compand_44_short,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[1].noise_compand_quality,
- 1,_psy_compand_44_short,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[2].noise_compand_quality,
- 2,_psy_compand_44,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[3].noise_compand_quality,
- 3,_psy_compand_44,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[0].tone_peaklimit_quality,
- 0,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[1].tone_peaklimit_quality,
- 1,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[2].tone_peaklimit_quality,
- 2,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[3].tone_peaklimit_quality,
- 3,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
-
- if(hi->impulse_block_p){
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
- 0,_psy_noise_suppress,_psy_noisebias_impulse,
- _psy_noiseguards_short);
- }else{
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
- 0,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_short);
+ if(ci==NULL)return(OV_EINVAL);
+ if(!hi->impulse_block_p)i0=1;
+
+ /* too low/high an ATH floater is nonsensical, but doesn't break anything */
+ if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
+ if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
+
+ /* again, bound this to avoid the app shooting itself int he foot
+ too badly */
+ if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
+ if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
+
+ /* get the appropriate setup template; matches the fetch in previous
+ stages */
+ setup=(ve_setup_data_template *)hi->setup;
+ if(setup==NULL)return(OV_EINVAL);
+
+ hi->set_in_stone=1;
+ /* choose block sizes from configured sizes as well as paying
+ attention to long_block_p and short_block_p. If the configured
+ short and long blocks are the same length, we set long_block_p
+ and unset short_block_p */
+ vorbis_encode_blocksize_setup(vi,hi->base_setting,
+ setup->blocksize_short,
+ setup->blocksize_long);
+ if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
+
+ /* floor setup; choose proper floor params. Allocated on the floor
+ stack in order; if we alloc only a single long floor, it's 0 */
+ for(i=0;i<setup->floor_mappings;i++)
+ vorbis_encode_floor_setup(vi,hi->base_setting,
+ setup->floor_books,
+ setup->floor_params,
+ setup->floor_mapping_list[i]);
+
+ /* setup of [mostly] short block detection and stereo*/
+ vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
+ setup->global_params,
+ setup->global_mapping);
+ vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
+
+ /* basic psych setup and noise normalization */
+ vorbis_encode_psyset_setup(vi,hi->base_setting,
+ setup->psy_noise_normal_start[0],
+ setup->psy_noise_normal_partition[0],
+ setup->psy_noise_normal_thresh,
+ 0);
+ vorbis_encode_psyset_setup(vi,hi->base_setting,
+ setup->psy_noise_normal_start[0],
+ setup->psy_noise_normal_partition[0],
+ setup->psy_noise_normal_thresh,
+ 1);
+ if(!singleblock){
+ vorbis_encode_psyset_setup(vi,hi->base_setting,
+ setup->psy_noise_normal_start[1],
+ setup->psy_noise_normal_partition[1],
+ setup->psy_noise_normal_thresh,
+ 2);
+ vorbis_encode_psyset_setup(vi,hi->base_setting,
+ setup->psy_noise_normal_start[1],
+ setup->psy_noise_normal_partition[1],
+ setup->psy_noise_normal_thresh,
+ 3);
}
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[1].noise_bias_quality,
- 1,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_short);
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[2].noise_bias_quality,
- 2,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_long);
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[3].noise_bias_quality,
- 3,_psy_noise_suppress,_psy_noisebias_long,
- _psy_noiseguards_long);
-
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[0].ath_quality,0,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[1].ath_quality,1,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[2].ath_quality,2,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[3].ath_quality,3,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
-
- if(ret){
- vorbis_info_clear(vi);
- return ret;
+ /* tone masking setup */
+ vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
+ setup->psy_tone_masteratt,
+ setup->psy_tone_0dB,
+ setup->psy_tone_adj_impulse);
+ vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
+ setup->psy_tone_masteratt,
+ setup->psy_tone_0dB,
+ setup->psy_tone_adj_other);
+ if(!singleblock){
+ vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
+ setup->psy_tone_masteratt,
+ setup->psy_tone_0dB,
+ setup->psy_tone_adj_other);
+ vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
+ setup->psy_tone_masteratt,
+ setup->psy_tone_0dB,
+ setup->psy_tone_adj_long);
}
- if(channels==2 && hi->stereo_couple_p){
- /* setup specific to stereo coupling */
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
- 1, /* coupled */
- hi->stereo_backfill_p,
- hi->residue_backfill_p,
- _residue_template_44_stereo,
- hi->stereo_point_dB,
- hi->stereo_point_kHz[0]);
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
- 1, /* coupled */
- hi->stereo_backfill_p,
- hi->residue_backfill_p,
- _residue_template_44_stereo,
- hi->stereo_point_dB,
- hi->stereo_point_kHz[1]);
-
- }else{
- /* setup specific to non-stereo (mono or uncoupled polyphonic)
- coupling */
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
- 0, /* uncoupled */
- 0,
- hi->residue_backfill_p,
- _residue_template_44_uncoupled,
- 0,
- hi->stereo_point_kHz[0]); /* just
- used as an encoding partitioning
- point */
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
- 0, /* uncoupled */
- 0,
- hi->residue_backfill_p,
- _residue_template_44_uncoupled,
- 0,
- hi->stereo_point_kHz[1]); /* just
- used as an encoding partitioning
- point */
+ /* noise companding setup */
+ vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
+ setup->psy_noise_compand,
+ setup->psy_noise_compand_short_mapping);
+ vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
+ setup->psy_noise_compand,
+ setup->psy_noise_compand_short_mapping);
+ if(!singleblock){
+ vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
+ setup->psy_noise_compand,
+ setup->psy_noise_compand_long_mapping);
+ vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
+ setup->psy_noise_compand,
+ setup->psy_noise_compand_long_mapping);
}
- ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[0],0);
- ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[1],1);
-
- if(ret)
- vorbis_info_clear(vi);
- return(ret);
-}
+ /* peak guarding setup */
+ vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
+ setup->psy_tone_dBsuppress);
+ vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
+ setup->psy_tone_dBsuppress);
+ if(!singleblock){
+ vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
+ setup->psy_tone_dBsuppress);
+ vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
+ setup->psy_tone_dBsuppress);
+ }
-/* this is only tuned for 44.1kHz right now. S'ok, for other rates it
- just doesn't guess */
-static double ratepch_un44[11]=
- {40000.,50000.,60000.,70000.,75000.,85000.,105000.,
- 115000.,135000.,160000.,250000.};
-static double ratepch_st44[11]=
- {32000.,40000.,48000.,56000.,64000.,
- 80000.,96000.,112000.,128000.,160000.,250000.};
-
-static double vbr_to_approx_bitrate(int ch,int coupled,
- double q,long srate){
- int iq=q*10.;
- double dq;
- double *r=NULL;
-
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
+ /* noise bias setup */
+ vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
+ setup->psy_noise_dBsuppress,
+ setup->psy_noise_bias_impulse,
+ setup->psy_noiseguards,
+ (i0==0?hi->impulse_noisetune:0.));
+ vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
+ setup->psy_noise_dBsuppress,
+ setup->psy_noise_bias_padding,
+ setup->psy_noiseguards,0.);
+ if(!singleblock){
+ vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
+ setup->psy_noise_dBsuppress,
+ setup->psy_noise_bias_trans,
+ setup->psy_noiseguards,0.);
+ vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
+ setup->psy_noise_dBsuppress,
+ setup->psy_noise_bias_long,
+ setup->psy_noiseguards,0.);
}
- if(srate>42000 && srate<46000){
- if(coupled)
- r=ratepch_st44;
- else
- r=ratepch_un44;
+ vorbis_encode_ath_setup(vi,0);
+ vorbis_encode_ath_setup(vi,1);
+ if(!singleblock){
+ vorbis_encode_ath_setup(vi,2);
+ vorbis_encode_ath_setup(vi,3);
}
-
- if(r==NULL)
- return(-1);
-
- return((r[iq]*(1.-dq)+r[iq+1]*dq)*ch);
-}
-static double approx_bitrate_to_vbr(int ch,int coupled,
- double bitrate,long srate){
- double *r=NULL,del;
- int i;
+ vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
- if(srate>42000 && srate<46000){
- if(coupled)
- r=ratepch_st44;
- else
- r=ratepch_un44;
+ /* set bitrate readonlies and management */
+ if(hi->bitrate_av>0)
+ vi->bitrate_nominal=hi->bitrate_av;
+ else{
+ vi->bitrate_nominal=setting_to_approx_bitrate(vi);
}
-
- if(r==NULL)
- return(-1.);
- bitrate/=ch;
+ vi->bitrate_lower=hi->bitrate_min;
+ vi->bitrate_upper=hi->bitrate_max;
+ if(hi->bitrate_av)
+ vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
+ else
+ vi->bitrate_window=0.;
+
+ if(hi->managed){
+ ci->bi.avg_rate=hi->bitrate_av;
+ ci->bi.min_rate=hi->bitrate_min;
+ ci->bi.max_rate=hi->bitrate_max;
+
+ ci->bi.reservoir_bits=hi->bitrate_reservoir;
+ ci->bi.reservoir_bias=
+ hi->bitrate_reservoir_bias;
+
+ ci->bi.slew_damp=hi->bitrate_av_damp;
+
+ }
- if(bitrate<=r[0])return(0.);
- for(i=0;i<10;i++)
- if(r[i]<bitrate && r[i+1]>=bitrate)break;
- if(i==10)return(10.);
+ return(0);
- del=(bitrate-r[i])/(r[i+1]-r[i]);
-
- return((i+del)*.1);
}
-/* only populates the high-level settings so that we can tweak with ctl before final setup */
-int vorbis_encode_setup_vbr(vorbis_info *vi,
- long channels,
- long rate,
-
- float base_quality){
- int ret=0,i,iq;
- double dq;
+static void vorbis_encode_setup_setting(vorbis_info *vi,
+ long channels,
+ long rate){
+ int i,is;
codec_setup_info *ci=vi->codec_setup;
highlevel_encode_setup *hi=&ci->hi;
-
- base_quality+=.0001;
- if(base_quality<0.)base_quality=0.;
- if(base_quality>.999)base_quality=.999;
-
- iq=base_quality*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=base_quality*10.-iq;
- }
+ const ve_setup_data_template *setup=hi->setup;
+ double ds;
+
+ vi->version=0;
+ vi->channels=channels;
+ vi->rate=rate;
+
+ hi->impulse_block_p=1;
+ hi->noise_normalize_p=1;
- ret|=vorbis_encode_toplevel_setup(vi,256,2048,channels,rate);
- hi->base_quality=base_quality;
- hi->base_quality_short=base_quality;
- hi->base_quality_long=base_quality;
- hi->trigger_quality=base_quality;
+ is=hi->base_setting;
+ ds=hi->base_setting-is;
+
+ hi->stereo_point_setting=hi->base_setting;
+
+ if(!hi->lowpass_altered)
+ hi->lowpass_kHz=
+ setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
+
+ hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
+ setup->psy_ath_float[is+1]*ds;
+ hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
+ setup->psy_ath_abs[is+1]*ds;
+
+ hi->amplitude_track_dBpersec=-6.;
+ hi->trigger_setting=hi->base_setting;
for(i=0;i<4;i++){
- hi->blocktype[i].tone_mask_quality=base_quality;
- hi->blocktype[i].tone_peaklimit_quality=base_quality;
- hi->blocktype[i].noise_bias_quality=base_quality;
- hi->blocktype[i].noise_compand_quality=base_quality;
- hi->blocktype[i].ath_quality=base_quality;
+ hi->block[i].tone_mask_setting=hi->base_setting;
+ hi->block[i].tone_peaklimit_setting=hi->base_setting;
+ hi->block[i].noise_bias_setting=hi->base_setting;
+ hi->block[i].noise_compand_setting=hi->base_setting;
}
+}
- hi->short_block_p=1;
- hi->long_block_p=1;
- hi->impulse_block_p=1;
- hi->amplitude_track_dBpersec=-6.;
+int vorbis_encode_setup_vbr(vorbis_info *vi,
+ long channels,
+ long rate,
+ float quality){
+ codec_setup_info *ci;
+ highlevel_encode_setup *hi;
+ if(rate<=0) return OV_EINVAL;
- hi->stereo_couple_p=1; /* only relevant if a two channel input */
- hi->stereo_backfill_p=0;
- hi->residue_backfill_p=0;
-
- /* set the ATH floaters */
- hi->ath_floating_dB=_psy_ath_floater[iq]*(1.-dq)+_psy_ath_floater[iq+1]*dq;
- hi->ath_absolute_dB=_psy_ath_abs[iq]*(1.-dq)+_psy_ath_abs[iq+1]*dq;
-
- /* set stereo dB and Hz */
- hi->stereo_point_dB=_psy_stereo_point_dB_44[iq];
- hi->stereo_point_kHz[0]=_psy_stereo_point_kHz_44[0][iq]*(1.-dq)+
- _psy_stereo_point_kHz_44[0][iq+1]*dq;
- hi->stereo_point_kHz[1]=_psy_stereo_point_kHz_44[1][iq]*(1.-dq)+
- _psy_stereo_point_kHz_44[1][iq+1]*dq;
-
- /* set lowpass */
- hi->lowpass_kHz[0]=
- hi->lowpass_kHz[1]=
- _psy_lowpass_44[iq]*(1.-dq)+_psy_lowpass_44[iq+1]*dq;
-
- /* set bitrate approximation */
- vi->bitrate_nominal=vbr_to_approx_bitrate(vi->channels,hi->stereo_couple_p,
- base_quality,vi->rate);
- vi->bitrate_lower=-1;
- vi->bitrate_upper=-1;
- vi->bitrate_window=-1;
+ ci=vi->codec_setup;
+ hi=&ci->hi;
- return(ret);
+ quality+=.0000001;
+ if(quality>=1.)quality=.9999;
+
+ hi->req=quality;
+ hi->setup=get_setup_template(channels,rate,quality,0,&hi->base_setting);
+ if(!hi->setup)return OV_EIMPL;
+
+ vorbis_encode_setup_setting(vi,channels,rate);
+ hi->managed=0;
+ hi->coupling_p=1;
+
+ return 0;
}
int vorbis_encode_init_vbr(vorbis_info *vi,
- long channels,
- long rate,
-
- float base_quality /* 0. to 1. */
- ){
+ long channels,
+ long rate,
+
+ float base_quality /* 0. to 1. */
+ ){
int ret=0;
ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
-
+
if(ret){
vorbis_info_clear(vi);
- return ret;
+ return ret;
}
ret=vorbis_encode_setup_init(vi);
if(ret)
}
int vorbis_encode_setup_managed(vorbis_info *vi,
- long channels,
- long rate,
-
- long max_bitrate,
- long nominal_bitrate,
- long min_bitrate){
-
- double approx_vbr=approx_bitrate_to_vbr(channels,(channels==2),
- nominal_bitrate,rate);
- int ret=0;
- if(approx_vbr<0)return(OV_EIMPL);
+ long channels,
+ long rate,
+
+ long max_bitrate,
+ long nominal_bitrate,
+ long min_bitrate){
+
+ codec_setup_info *ci;
+ highlevel_encode_setup *hi;
+ double tnominal;
+ if(rate<=0) return OV_EINVAL;
+
+ ci=vi->codec_setup;
+ hi=&ci->hi;
+ tnominal=nominal_bitrate;
if(nominal_bitrate<=0.){
if(max_bitrate>0.){
- nominal_bitrate=max_bitrate*.875;
+ if(min_bitrate>0.)
+ nominal_bitrate=(max_bitrate+min_bitrate)*.5;
+ else
+ nominal_bitrate=max_bitrate*.875;
}else{
if(min_bitrate>0.){
- nominal_bitrate=min_bitrate;
+ nominal_bitrate=min_bitrate;
}else{
- return(OV_EINVAL);
+ return(OV_EINVAL);
}
}
}
- ret=vorbis_encode_setup_vbr(vi,channels,rate,approx_vbr);
- if(ret){
- vorbis_info_clear(vi);
- return ret;
- }
+ hi->req=nominal_bitrate;
+ hi->setup=get_setup_template(channels,rate,nominal_bitrate,1,&hi->base_setting);
+ if(!hi->setup)return OV_EIMPL;
- /* adjust to make management's life easier. Use the ctl() interface
- once it's implemented */
- {
- codec_setup_info *ci=vi->codec_setup;
- highlevel_encode_setup *hi=&ci->hi;
+ vorbis_encode_setup_setting(vi,channels,rate);
- /* backfills */
- hi->stereo_backfill_p=1;
- hi->residue_backfill_p=1;
-
- /* no impulse blocks */
- hi->impulse_block_p=0;
- /* de-rate stereo */
- if(hi->stereo_point_dB && hi->stereo_couple_p && channels==2){
- hi->stereo_point_dB++;
- if(hi->stereo_point_dB>3)hi->stereo_point_dB=3;
- }
- /* slug the vbr noise setting*/
- hi->blocktype[0].noise_bias_quality-=.1;
- if(hi->blocktype[0].noise_bias_quality<0.)
- hi->blocktype[0].noise_bias_quality=0.;
- hi->blocktype[1].noise_bias_quality-=.1;
- if(hi->blocktype[1].noise_bias_quality<0.)
- hi->blocktype[1].noise_bias_quality=0.;
- hi->blocktype[2].noise_bias_quality-=.05;
- if(hi->blocktype[2].noise_bias_quality<0.)
- hi->blocktype[2].noise_bias_quality=0.;
- hi->blocktype[3].noise_bias_quality-=.05;
- if(hi->blocktype[3].noise_bias_quality<0.)
- hi->blocktype[3].noise_bias_quality=0.;
-
- /* initialize management. Currently hardcoded for 44, but so is above. */
- memcpy(&ci->bi,&_bm_44_default,sizeof(ci->bi));
- ci->bi.queue_hardmin=min_bitrate;
- ci->bi.queue_hardmax=max_bitrate;
-
- ci->bi.queue_avgmin=nominal_bitrate;
- ci->bi.queue_avgmax=nominal_bitrate;
-
- /* adjust management */
- if(max_bitrate<=0. && min_bitrate<=0.){
- /* just an average tracker; no reason for the window to be as small as 2s. */
- ci->bi.queue_avg_time=4.;
- }
- ci->bi.avgfloat_noise_maxval=_bm_max_noise_offset[(int)approx_vbr];
- /*if(max_bitrate>0.){
- ci->bi.avgfloat_minimum=0.;
- }*/
+ /* initialize management with sane defaults */
+ hi->coupling_p=1;
+ hi->managed=1;
+ hi->bitrate_min=min_bitrate;
+ hi->bitrate_max=max_bitrate;
+ hi->bitrate_av=tnominal;
+ hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
+ hi->bitrate_reservoir=nominal_bitrate*2;
+ hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */
+
+ return(0);
- }
- return(ret);
}
int vorbis_encode_init(vorbis_info *vi,
- long channels,
- long rate,
+ long channels,
+ long rate,
- long max_bitrate,
- long nominal_bitrate,
- long min_bitrate){
+ long max_bitrate,
+ long nominal_bitrate,
+ long min_bitrate){
int ret=vorbis_encode_setup_managed(vi,channels,rate,
- max_bitrate,
- nominal_bitrate,
- min_bitrate);
+ max_bitrate,
+ nominal_bitrate,
+ min_bitrate);
if(ret){
vorbis_info_clear(vi);
return(ret);
}
int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
- return(OV_EIMPL);
-}
-
+ if(vi){
+ codec_setup_info *ci=vi->codec_setup;
+ highlevel_encode_setup *hi=&ci->hi;
+ int setp=(number&0xf); /* a read request has a low nibble of 0 */
+ if(setp && hi->set_in_stone)return(OV_EINVAL);
+ switch(number){
+ /* now deprecated *****************/
+ case OV_ECTL_RATEMANAGE_GET:
+ {
+ struct ovectl_ratemanage_arg *ai=
+ (struct ovectl_ratemanage_arg *)arg;
+ ai->management_active=hi->managed;
+ ai->bitrate_hard_window=ai->bitrate_av_window=
+ (double)hi->bitrate_reservoir/vi->rate;
+ ai->bitrate_av_window_center=1.;
+ ai->bitrate_hard_min=hi->bitrate_min;
+ ai->bitrate_hard_max=hi->bitrate_max;
+ ai->bitrate_av_lo=hi->bitrate_av;
+ ai->bitrate_av_hi=hi->bitrate_av;
+ }
+ return(0);
+
+ /* now deprecated *****************/
+ case OV_ECTL_RATEMANAGE_SET:
+ {
+ struct ovectl_ratemanage_arg *ai=
+ (struct ovectl_ratemanage_arg *)arg;
+ if(ai==NULL){
+ hi->managed=0;
+ }else{
+ hi->managed=ai->management_active;
+ vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
+ vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
+ }
+ }
+ return 0;
+
+ /* now deprecated *****************/
+ case OV_ECTL_RATEMANAGE_AVG:
+ {
+ struct ovectl_ratemanage_arg *ai=
+ (struct ovectl_ratemanage_arg *)arg;
+ if(ai==NULL){
+ hi->bitrate_av=0;
+ }else{
+ hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
+ }
+ }
+ return(0);
+ /* now deprecated *****************/
+ case OV_ECTL_RATEMANAGE_HARD:
+ {
+ struct ovectl_ratemanage_arg *ai=
+ (struct ovectl_ratemanage_arg *)arg;
+ if(ai==NULL){
+ hi->bitrate_min=0;
+ hi->bitrate_max=0;
+ }else{
+ hi->bitrate_min=ai->bitrate_hard_min;
+ hi->bitrate_max=ai->bitrate_hard_max;
+ hi->bitrate_reservoir=ai->bitrate_hard_window*
+ (hi->bitrate_max+hi->bitrate_min)*.5;
+ }
+ if(hi->bitrate_reservoir<128.)
+ hi->bitrate_reservoir=128.;
+ }
+ return(0);
+
+ /* replacement ratemanage interface */
+ case OV_ECTL_RATEMANAGE2_GET:
+ {
+ struct ovectl_ratemanage2_arg *ai=
+ (struct ovectl_ratemanage2_arg *)arg;
+ if(ai==NULL)return OV_EINVAL;
+
+ ai->management_active=hi->managed;
+ ai->bitrate_limit_min_kbps=hi->bitrate_min/1000;
+ ai->bitrate_limit_max_kbps=hi->bitrate_max/1000;
+ ai->bitrate_average_kbps=hi->bitrate_av/1000;
+ ai->bitrate_average_damping=hi->bitrate_av_damp;
+ ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
+ ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
+ }
+ return (0);
+ case OV_ECTL_RATEMANAGE2_SET:
+ {
+ struct ovectl_ratemanage2_arg *ai=
+ (struct ovectl_ratemanage2_arg *)arg;
+ if(ai==NULL){
+ hi->managed=0;
+ }else{
+ /* sanity check; only catch invariant violations */
+ if(ai->bitrate_limit_min_kbps>0 &&
+ ai->bitrate_average_kbps>0 &&
+ ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
+ return OV_EINVAL;
+
+ if(ai->bitrate_limit_max_kbps>0 &&
+ ai->bitrate_average_kbps>0 &&
+ ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
+ return OV_EINVAL;
+
+ if(ai->bitrate_limit_min_kbps>0 &&
+ ai->bitrate_limit_max_kbps>0 &&
+ ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
+ return OV_EINVAL;
+
+ if(ai->bitrate_average_damping <= 0.)
+ return OV_EINVAL;
+
+ if(ai->bitrate_limit_reservoir_bits < 0)
+ return OV_EINVAL;
+
+ if(ai->bitrate_limit_reservoir_bias < 0.)
+ return OV_EINVAL;
+
+ if(ai->bitrate_limit_reservoir_bias > 1.)
+ return OV_EINVAL;
+
+ hi->managed=ai->management_active;
+ hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000;
+ hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000;
+ hi->bitrate_av=ai->bitrate_average_kbps * 1000;
+ hi->bitrate_av_damp=ai->bitrate_average_damping;
+ hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
+ hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
+ }
+ }
+ return 0;
+ case OV_ECTL_LOWPASS_GET:
+ {
+ double *farg=(double *)arg;
+ *farg=hi->lowpass_kHz;
+ }
+ return(0);
+ case OV_ECTL_LOWPASS_SET:
+ {
+ double *farg=(double *)arg;
+ hi->lowpass_kHz=*farg;
+
+ if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
+ if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
+ hi->lowpass_altered=1;
+ }
+ return(0);
+ case OV_ECTL_IBLOCK_GET:
+ {
+ double *farg=(double *)arg;
+ *farg=hi->impulse_noisetune;
+ }
+ return(0);
+ case OV_ECTL_IBLOCK_SET:
+ {
+ double *farg=(double *)arg;
+ hi->impulse_noisetune=*farg;
+
+ if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
+ if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;
+ }
+ return(0);
+ case OV_ECTL_COUPLING_GET:
+ {
+ int *iarg=(int *)arg;
+ *iarg=hi->coupling_p;
+ }
+ return(0);
+ case OV_ECTL_COUPLING_SET:
+ {
+ const void *new_template;
+ double new_base=0.;
+ int *iarg=(int *)arg;
+ hi->coupling_p=((*iarg)!=0);
+
+ /* Fetching a new template can alter the base_setting, which
+ many other parameters are based on. Right now, the only
+ parameter drawn from the base_setting that can be altered
+ by an encctl is the lowpass, so that is explictly flagged
+ to not be overwritten when we fetch a new template and
+ recompute the dependant settings */
+ new_template = get_setup_template(hi->coupling_p?vi->channels:-1,
+ vi->rate,
+ hi->req,
+ hi->managed,
+ &new_base);
+ if(!hi->setup)return OV_EIMPL;
+ hi->setup=new_template;
+ hi->base_setting=new_base;
+ vorbis_encode_setup_setting(vi,vi->channels,vi->rate);
+ }
+ return(0);
+ }
+ return(OV_EIMPL);
+ }
+ return(OV_EINVAL);
+}