1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: simple programmatic interface for encoder mode setup
14 last mod: $Id: vorbisenc.c,v 1.46 2002/07/02 04:25:16 xiphmont Exp $
16 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "vorbis/vorbisenc.h"
25 #include "codec_internal.h"
30 /* careful with this; it's using static array sizing to make managing
31 all the modes a little less annoying. If we use a residue backend
32 with > 12 partition types, or a different division of iteration,
33 this needs to be updated. */
35 static_codebook *books[12][3];
40 int limit_type; /* 0 lowpass limited, 1 point stereo limited */
41 vorbis_info_residue0 *res;
42 static_codebook *book_aux;
43 static_codebook *book_aux_managed;
44 static_bookblock *books_base;
45 static_bookblock *books_base_managed;
46 } vorbis_residue_template;
49 vorbis_info_mapping0 *map;
50 vorbis_residue_template *res;
51 } vorbis_mapping_template;
53 typedef struct vp_adjblock{
58 int data[NOISE_COMPAND_LEVELS];
61 /* high level configuration information for setting things up
62 step-by-step with the detailed vorbis_encode_ctl interface.
63 There's a fair amount of redundancy such that interactive setup
64 does not directly deal with any vorbis_info or codec_setup_info
65 initialization; it's all stored (until full init) in this highlevel
66 setup, then flushed out to the real codec setup structs later. */
69 int att[P_NOISECURVES];
73 typedef struct { int data[P_NOISECURVES]; } adj3;
77 int post[PACKETBLOBS];
78 float kHz[PACKETBLOBS];
79 float lowpasskHz[PACKETBLOBS];
88 int data[P_NOISECURVES][17];
94 double *quality_mapping;
95 int coupling_restriction;
96 long samplerate_min_restriction;
97 long samplerate_max_restriction;
100 int *blocksize_short;
103 att3 *psy_tone_masteratt;
105 int *psy_tone_dBsuppress;
107 vp_adjblock *psy_tone_adj_impulse;
108 vp_adjblock *psy_tone_adj_long;
109 vp_adjblock *psy_tone_adj_other;
111 noiseguard *psy_noiseguards;
112 noise3 *psy_noise_bias_impulse;
113 noise3 *psy_noise_bias_padding;
114 noise3 *psy_noise_bias_trans;
115 noise3 *psy_noise_bias_long;
116 int *psy_noise_dBsuppress;
118 compandblock *psy_noise_compand;
119 double *psy_noise_compand_short_mapping;
120 double *psy_noise_compand_long_mapping;
122 int *psy_noise_normal_start[2];
123 int *psy_noise_normal_partition[2];
124 double *psy_noise_normal_thresh;
131 vorbis_info_psy_global *global_params;
132 double *global_mapping;
133 adj_stereo *stereo_modes;
135 static_codebook ***floor_books;
136 vorbis_info_floor1 *floor_params;
137 int *floor_short_mapping;
138 int *floor_long_mapping;
140 vorbis_mapping_template *maps;
141 } ve_setup_data_template;
143 #include "modes/setup_44.h"
144 #include "modes/setup_44u.h"
145 #include "modes/setup_32.h"
146 #include "modes/setup_X.h"
148 static ve_setup_data_template *setup_list[]={
150 &ve_setup_44_stereo_low,
151 &ve_setup_44_uncoupled,
152 &ve_setup_44_uncoupled_low,
155 &ve_setup_32_stereo_low,
156 &ve_setup_32_uncoupled,
157 &ve_setup_32_uncoupled_low,
160 &ve_setup_X_uncoupled,
161 &ve_setup_X_stereo_low,
162 &ve_setup_X_uncoupled_low,
167 /* a few static coder conventions */
168 static vorbis_info_mode _mode_template[2]={
174 static int vorbis_encode_toplevel_setup(vorbis_info *vi,int ch,long rate){
175 if(vi && vi->codec_setup){
186 static int vorbis_encode_floor_setup(vorbis_info *vi,double s,int block,
187 static_codebook ***books,
188 vorbis_info_floor1 *in,
191 vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
192 codec_setup_info *ci=vi->codec_setup;
194 memcpy(f,in+x[is],sizeof(*f));
195 /* fill in the lowpass field, even if it's temporary */
196 f->n=ci->blocksizes[block]>>1;
200 int partitions=f->partitions;
203 for(i=0;i<partitions;i++)
204 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
205 for(i=0;i<=maxclass;i++){
206 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
207 f->class_book[i]+=ci->books;
208 for(k=0;k<(1<<f->class_subs[i]);k++){
209 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
210 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
214 for(i=0;i<=maxbook;i++)
215 ci->book_param[ci->books++]=books[x[is]][i];
218 /* for now, we're only using floor 1 */
219 ci->floor_type[ci->floors]=1;
220 ci->floor_param[ci->floors]=f;
226 static int vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
227 vorbis_info_psy_global *in,
231 codec_setup_info *ci=vi->codec_setup;
232 vorbis_info_psy_global *g=&ci->psy_g_param;
234 memcpy(g,in+(int)x[is],sizeof(*g));
236 ds=x[is]*(1.-ds)+x[is+1]*ds;
244 /* interpolate the trigger threshholds */
246 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
247 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
249 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
253 static int vorbis_encode_global_stereo(vorbis_info *vi,
254 highlevel_encode_setup *hi,
256 float s=hi->stereo_point_setting;
259 codec_setup_info *ci=vi->codec_setup;
260 vorbis_info_psy_global *g=&ci->psy_g_param;
263 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
264 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
267 /* interpolate the kHz threshholds */
268 for(i=0;i<PACKETBLOBS;i++){
269 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
270 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
271 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
272 g->coupling_pkHz[i]=kHz;
274 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
275 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
276 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
280 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
281 for(i=0;i<PACKETBLOBS;i++){
282 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
283 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
284 g->coupling_pkHz[i]=kHz;
287 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
288 for(i=0;i<PACKETBLOBS;i++){
289 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
290 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
294 for(i=0;i<PACKETBLOBS;i++){
295 g->sliding_lowpass[0][i]=ci->blocksizes[0];
296 g->sliding_lowpass[1][i]=ci->blocksizes[1];
302 static int vorbis_encode_psyset_setup(vorbis_info *vi,double s,
307 codec_setup_info *ci=vi->codec_setup;
308 vorbis_info_psy *p=ci->psy_param[block];
309 highlevel_encode_setup *hi=&ci->hi;
315 p=_ogg_calloc(1,sizeof(*p));
316 ci->psy_param[block]=p;
319 memcpy(p,&_psy_info_template,sizeof(*p));
320 p->blockflag=block>>1;
322 if(hi->noise_normalize_p){
323 p->normal_channel_p=1;
325 p->normal_start=nn_start[is];
326 p->normal_partition=nn_partition[is];
327 p->normal_thresh=nn_thresh[is];
333 static int vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
339 codec_setup_info *ci=vi->codec_setup;
340 vorbis_info_psy *p=ci->psy_param[block];
342 /* 0 and 2 are only used by bitmanagement, but there's no harm to always
343 filling the values in here */
344 p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
345 p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
346 p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
347 p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
348 p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
350 p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
352 for(i=0;i<P_BANDS;i++)
353 p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
358 static int vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
359 compandblock *in, double *x){
362 codec_setup_info *ci=vi->codec_setup;
363 vorbis_info_psy *p=ci->psy_param[block];
365 ds=x[is]*(1.-ds)+x[is+1]*ds;
373 /* interpolate the compander settings */
374 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
375 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
379 static int vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
383 codec_setup_info *ci=vi->codec_setup;
384 vorbis_info_psy *p=ci->psy_param[block];
386 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
391 static int vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
398 codec_setup_info *ci=vi->codec_setup;
399 vorbis_info_psy *p=ci->psy_param[block];
401 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
402 p->noisewindowlomin=guard[block].lo;
403 p->noisewindowhimin=guard[block].hi;
404 p->noisewindowfixed=guard[block].fixed;
406 for(j=0;j<P_NOISECURVES;j++)
407 for(i=0;i<P_BANDS;i++)
408 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
410 /* impulse blocks may take a user specified bias to boost the
411 nominal/high noise encoding depth */
412 for(j=0;j<P_NOISECURVES;j++){
413 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
414 for(i=0;i<P_BANDS;i++){
415 p->noiseoff[j][i]+=userbias;
416 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
423 static int vorbis_encode_ath_setup(vorbis_info *vi,int block){
424 codec_setup_info *ci=vi->codec_setup;
425 vorbis_info_psy *p=ci->psy_param[block];
427 p->ath_adjatt=ci->hi.ath_floating_dB;
428 p->ath_maxatt=ci->hi.ath_absolute_dB;
433 static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
435 for(i=0;i<ci->books;i++)
436 if(ci->book_param[i]==book)return(i);
441 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
442 int *shortb,int *longb){
444 codec_setup_info *ci=vi->codec_setup;
447 int blockshort=shortb[is];
448 int blocklong=longb[is];
449 ci->blocksizes[0]=blockshort;
450 ci->blocksizes[1]=blocklong;
454 static void vorbis_encode_residue_setup(vorbis_info *vi,
455 int number, int block,
456 vorbis_residue_template *res){
458 codec_setup_info *ci=vi->codec_setup;
461 vorbis_info_residue0 *r=ci->residue_param[number]=
462 _ogg_malloc(sizeof(*r));
464 memcpy(r,res->res,sizeof(*r));
465 if(ci->residues<=number)ci->residues=number+1;
467 switch(ci->blocksizes[block]){
468 case 64:case 128:case 256:case 512:
475 ci->residue_type[number]=res->res_type;
477 /* to be adjusted by lowpass/pointlimit later */
478 n=r->end=ci->blocksizes[block]>>1;
480 n=r->end*=vi->channels;
482 /* fill in all the books */
487 for(i=0;i<r->partitions;i++)
489 if(res->books_base_managed->books[i][k])
490 r->secondstages[i]|=(1<<k);
492 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
493 ci->book_param[r->groupbook]=res->book_aux_managed;
495 for(i=0;i<r->partitions;i++){
497 if(res->books_base_managed->books[i][k]){
498 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
499 r->booklist[booklist++]=bookid;
500 ci->book_param[bookid]=res->books_base_managed->books[i][k];
507 for(i=0;i<r->partitions;i++)
509 if(res->books_base->books[i][k])
510 r->secondstages[i]|=(1<<k);
512 r->groupbook=book_dup_or_new(ci,res->book_aux);
513 ci->book_param[r->groupbook]=res->book_aux;
515 for(i=0;i<r->partitions;i++){
517 if(res->books_base->books[i][k]){
518 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
519 r->booklist[booklist++]=bookid;
520 ci->book_param[bookid]=res->books_base->books[i][k];
527 /* lowpass setup/pointlimit */
529 double freq=ci->hi.lowpass_kHz*1000.;
530 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
531 double nyq=vi->rate/2.;
532 long blocksize=ci->blocksizes[block]>>1;
534 /* lowpass needs to be set in the floor and the residue. */
535 if(freq>nyq)freq=nyq;
536 /* in the floor, the granularity can be very fine; it doesn't alter
537 the encoding structure, only the samples used to fit the floor
539 f->n=freq/nyq*blocksize;
541 /* this res may by limited by the maximum pointlimit of the mode,
542 not the lowpass. the floor is always lowpass limited. */
545 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
547 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
548 if(freq>nyq)freq=nyq;
551 /* in the residue, we're constrained, physically, by partition
552 boundaries. We still lowpass 'wherever', but we have to round up
553 here to next boundary, or the vorbis spec will round it *down* to
554 previous boundary in encode/decode */
555 if(ci->residue_type[block]==2)
556 r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
559 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
564 /* we assume two maps in this encoder */
565 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
566 vorbis_mapping_template *maps){
568 codec_setup_info *ci=vi->codec_setup;
570 vorbis_info_mapping0 *map=maps[is].map;
571 vorbis_info_mode *mode=_mode_template;
572 vorbis_residue_template *res=maps[is].res;
576 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
577 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
579 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
580 if(i>=ci->modes)ci->modes=i+1;
583 memcpy(ci->map_param[i],map+i,sizeof(*map));
584 if(i>=ci->maps)ci->maps=i+1;
586 for(j=0;j<map[i].submaps;j++)
587 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
588 ,res+map[i].residuesubmap[j]);
592 static double setting_to_approx_bitrate(vorbis_info *vi){
593 codec_setup_info *ci=vi->codec_setup;
594 highlevel_encode_setup *hi=&ci->hi;
595 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
596 int is=hi->base_setting;
597 double ds=hi->base_setting-is;
599 double *r=setup->rate_mapping;
604 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
607 static void get_setup_template(vorbis_info *vi,
609 double req,int q_or_bitrate){
611 codec_setup_info *ci=vi->codec_setup;
612 highlevel_encode_setup *hi=&ci->hi;
613 if(q_or_bitrate)req/=ch;
615 while(setup_list[i]){
616 if(setup_list[i]->coupling_restriction==-1 ||
617 setup_list[i]->coupling_restriction==ch){
618 if(srate>=setup_list[i]->samplerate_min_restriction &&
619 srate<=setup_list[i]->samplerate_max_restriction){
620 int mappings=setup_list[i]->mappings;
621 double *map=(q_or_bitrate?
622 setup_list[i]->rate_mapping:
623 setup_list[i]->quality_mapping);
625 /* the template matches. Does the requested quality mode
626 fall within this template's modes? */
627 if(req<map[0]){++i;continue;}
628 if(req>map[setup_list[i]->mappings]){++i;continue;}
629 for(j=0;j<mappings;j++)
630 if(req>=map[j] && req<map[j+1])break;
631 /* an all-points match */
632 hi->setup=setup_list[i];
634 hi->base_setting=j-.001;
638 float del=(req-low)/(high-low);
639 hi->base_setting=j+del;
650 /* encoders will need to use vorbis_info_init beforehand and call
651 vorbis_info clear when all done */
653 /* two interfaces; this, more detailed one, and later a convenience
656 /* the final setup call */
657 int vorbis_encode_setup_init(vorbis_info *vi){
659 codec_setup_info *ci=vi->codec_setup;
660 ve_setup_data_template *setup=NULL;
661 highlevel_encode_setup *hi=&ci->hi;
663 if(ci==NULL)return(OV_EINVAL);
664 if(!hi->impulse_block_p)i0=1;
666 /* too low/high an ATH floater is nonsensical, but doesn't break anything */
667 if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
668 if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
670 /* again, bound this to avoid the app shooting itself int he foot
672 if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
673 if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
675 /* get the appropriate setup template; matches the fetch in previous
677 setup=(ve_setup_data_template *)hi->setup;
678 if(setup==NULL)return(OV_EINVAL);
681 /* choose block sizes from configured sizes as well as paying
682 attention to long_block_p and short_block_p. If the configured
683 short and long blocks are the same length, we set long_block_p
684 and unset short_block_p */
685 vorbis_encode_blocksize_setup(vi,hi->base_setting,
686 setup->blocksize_short,
687 setup->blocksize_long);
689 /* floor setup; choose proper floor params. Allocated on the floor
690 stack in order; if we alloc only long floor, it's 0 */
691 ret|=vorbis_encode_floor_setup(vi,hi->short_setting,0,
694 setup->floor_short_mapping);
695 ret|=vorbis_encode_floor_setup(vi,hi->long_setting,1,
698 setup->floor_long_mapping);
700 /* setup of [mostly] short block detection and stereo*/
701 ret|=vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
702 setup->global_params,
703 setup->global_mapping);
704 ret|=vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
706 /* basic psych setup and noise normalization */
707 ret|=vorbis_encode_psyset_setup(vi,hi->short_setting,
708 setup->psy_noise_normal_start[0],
709 setup->psy_noise_normal_partition[0],
710 setup->psy_noise_normal_thresh,
712 ret|=vorbis_encode_psyset_setup(vi,hi->short_setting,
713 setup->psy_noise_normal_start[0],
714 setup->psy_noise_normal_partition[0],
715 setup->psy_noise_normal_thresh,
717 ret|=vorbis_encode_psyset_setup(vi,hi->long_setting,
718 setup->psy_noise_normal_start[1],
719 setup->psy_noise_normal_partition[1],
720 setup->psy_noise_normal_thresh,
722 ret|=vorbis_encode_psyset_setup(vi,hi->long_setting,
723 setup->psy_noise_normal_start[1],
724 setup->psy_noise_normal_partition[1],
725 setup->psy_noise_normal_thresh,
728 /* tone masking setup */
729 ret|=vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
730 setup->psy_tone_masteratt,
732 setup->psy_tone_adj_impulse);
733 ret|=vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
734 setup->psy_tone_masteratt,
736 setup->psy_tone_adj_other);
737 ret|=vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
738 setup->psy_tone_masteratt,
740 setup->psy_tone_adj_other);
741 ret|=vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
742 setup->psy_tone_masteratt,
744 setup->psy_tone_adj_long);
746 /* noise companding setup */
747 ret|=vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
748 setup->psy_noise_compand,
749 setup->psy_noise_compand_short_mapping);
750 ret|=vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
751 setup->psy_noise_compand,
752 setup->psy_noise_compand_short_mapping);
753 ret|=vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
754 setup->psy_noise_compand,
755 setup->psy_noise_compand_long_mapping);
756 ret|=vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
757 setup->psy_noise_compand,
758 setup->psy_noise_compand_long_mapping);
760 /* peak guarding setup */
761 ret|=vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
762 setup->psy_tone_dBsuppress);
763 ret|=vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
764 setup->psy_tone_dBsuppress);
765 ret|=vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
766 setup->psy_tone_dBsuppress);
767 ret|=vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
768 setup->psy_tone_dBsuppress);
770 /* noise bias setup */
771 ret|=vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
772 setup->psy_noise_dBsuppress,
773 setup->psy_noise_bias_impulse,
774 setup->psy_noiseguards,
775 (i0==0?hi->impulse_noisetune:0.));
776 ret|=vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
777 setup->psy_noise_dBsuppress,
778 setup->psy_noise_bias_padding,
779 setup->psy_noiseguards,0.);
780 ret|=vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
781 setup->psy_noise_dBsuppress,
782 setup->psy_noise_bias_trans,
783 setup->psy_noiseguards,0.);
784 ret|=vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
785 setup->psy_noise_dBsuppress,
786 setup->psy_noise_bias_long,
787 setup->psy_noiseguards,0.);
789 ret|=vorbis_encode_ath_setup(vi,0);
790 ret|=vorbis_encode_ath_setup(vi,1);
791 ret|=vorbis_encode_ath_setup(vi,2);
792 ret|=vorbis_encode_ath_setup(vi,3);
795 vorbis_info_clear(vi);
799 vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
801 /* set bitrate readonlies and management */
802 vi->bitrate_nominal=setting_to_approx_bitrate(vi);
803 vi->bitrate_lower=hi->bitrate_min;
804 vi->bitrate_upper=hi->bitrate_max;
805 vi->bitrate_window=hi->bitrate_limit_window;
808 ci->bi.queue_avg_time=hi->bitrate_av_window;
809 ci->bi.queue_avg_center=hi->bitrate_av_window_center;
810 ci->bi.queue_minmax_time=hi->bitrate_limit_window;
811 ci->bi.queue_hardmin=hi->bitrate_min;
812 ci->bi.queue_hardmax=hi->bitrate_max;
813 ci->bi.queue_avgmin=hi->bitrate_av_lo;
814 ci->bi.queue_avgmax=hi->bitrate_av_hi;
815 ci->bi.avgfloat_downslew_max=-999999.f;
816 ci->bi.avgfloat_upslew_max=999999.f;
823 static int vorbis_encode_setup_setting(vorbis_info *vi,
827 codec_setup_info *ci=vi->codec_setup;
828 highlevel_encode_setup *hi=&ci->hi;
829 ve_setup_data_template *setup=hi->setup;
832 ret=vorbis_encode_toplevel_setup(vi,channels,rate);
836 ds=hi->base_setting-is;
838 hi->short_setting=hi->base_setting;
839 hi->long_setting=hi->base_setting;
843 hi->impulse_block_p=1;
844 hi->noise_normalize_p=1;
846 hi->stereo_point_setting=hi->base_setting;
848 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
850 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
851 setup->psy_ath_float[is+1]*ds;
852 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
853 setup->psy_ath_abs[is+1]*ds;
855 hi->amplitude_track_dBpersec=-6.;
856 hi->trigger_setting=hi->base_setting;
859 hi->block[i].tone_mask_setting=hi->base_setting;
860 hi->block[i].tone_peaklimit_setting=hi->base_setting;
861 hi->block[i].noise_bias_setting=hi->base_setting;
862 hi->block[i].noise_compand_setting=hi->base_setting;
868 int vorbis_encode_setup_vbr(vorbis_info *vi,
872 codec_setup_info *ci=vi->codec_setup;
873 highlevel_encode_setup *hi=&ci->hi;
876 if(quality>=1.)quality=.9999;
878 get_setup_template(vi,channels,rate,quality,0);
879 if(!hi->setup)return OV_EIMPL;
881 return vorbis_encode_setup_setting(vi,channels,rate);
884 int vorbis_encode_init_vbr(vorbis_info *vi,
888 float base_quality /* 0. to 1. */
892 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
895 vorbis_info_clear(vi);
898 ret=vorbis_encode_setup_init(vi);
900 vorbis_info_clear(vi);
904 int vorbis_encode_setup_managed(vorbis_info *vi,
909 long nominal_bitrate,
912 codec_setup_info *ci=vi->codec_setup;
913 highlevel_encode_setup *hi=&ci->hi;
914 double tnominal=nominal_bitrate;
917 if(nominal_bitrate<=0.){
919 nominal_bitrate=max_bitrate*.875;
922 nominal_bitrate=min_bitrate;
929 get_setup_template(vi,channels,rate,nominal_bitrate,1);
930 if(!hi->setup)return OV_EIMPL;
932 ret=vorbis_encode_setup_setting(vi,channels,rate);
934 vorbis_info_clear(vi);
938 /* initialize management with sane defaults */
939 /* initialize management with sane defaults */
941 hi->bitrate_av_window=4.;
942 hi->bitrate_av_window_center=.5;
943 hi->bitrate_limit_window=2.;
944 hi->bitrate_min=min_bitrate;
945 hi->bitrate_max=max_bitrate;
946 hi->bitrate_av_lo=tnominal;
947 hi->bitrate_av_hi=tnominal;
953 int vorbis_encode_init(vorbis_info *vi,
958 long nominal_bitrate,
961 int ret=vorbis_encode_setup_managed(vi,channels,rate,
966 vorbis_info_clear(vi);
970 ret=vorbis_encode_setup_init(vi);
972 vorbis_info_clear(vi);
976 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
978 codec_setup_info *ci=vi->codec_setup;
979 highlevel_encode_setup *hi=&ci->hi;
980 int setp=(number&0xf); /* a read request has a low nibble of 0 */
982 if(setp && hi->set_in_stone)return(OV_EINVAL);
985 case OV_ECTL_RATEMANAGE_GET:
988 struct ovectl_ratemanage_arg *ai=
989 (struct ovectl_ratemanage_arg *)arg;
991 ai->management_active=hi->managed;
992 ai->bitrate_av_window=hi->bitrate_av_window;
993 ai->bitrate_av_window_center=hi->bitrate_av_window_center;
994 ai->bitrate_hard_window=hi->bitrate_limit_window;
995 ai->bitrate_hard_min=hi->bitrate_min;
996 ai->bitrate_hard_max=hi->bitrate_max;
997 ai->bitrate_av_lo=hi->bitrate_av_lo;
998 ai->bitrate_av_hi=hi->bitrate_av_hi;
1003 case OV_ECTL_RATEMANAGE_SET:
1005 struct ovectl_ratemanage_arg *ai=
1006 (struct ovectl_ratemanage_arg *)arg;
1010 hi->managed=ai->management_active;
1011 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
1012 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
1017 case OV_ECTL_RATEMANAGE_AVG:
1019 struct ovectl_ratemanage_arg *ai=
1020 (struct ovectl_ratemanage_arg *)arg;
1022 hi->bitrate_av_lo=0;
1023 hi->bitrate_av_hi=0;
1024 hi->bitrate_av_window=0;
1026 hi->bitrate_av_window=ai->bitrate_av_window;
1027 hi->bitrate_av_window_center=ai->bitrate_av_window_center;
1028 hi->bitrate_av_lo=ai->bitrate_av_lo;
1029 hi->bitrate_av_hi=ai->bitrate_av_hi;
1032 if(hi->bitrate_av_window<.25)hi->bitrate_av_window=.25;
1033 if(hi->bitrate_av_window>10.)hi->bitrate_av_window=10.;
1034 if(hi->bitrate_av_window_center<0.)hi->bitrate_av_window=0.;
1035 if(hi->bitrate_av_window_center>1.)hi->bitrate_av_window=1.;
1037 if( ( (hi->bitrate_av_lo<=0 && hi->bitrate_av_hi<=0)||
1038 (hi->bitrate_av_window<=0) ) &&
1039 ( (hi->bitrate_min<=0 && hi->bitrate_max<=0)||
1040 (hi->bitrate_limit_window<=0) ))
1044 case OV_ECTL_RATEMANAGE_HARD:
1046 struct ovectl_ratemanage_arg *ai=
1047 (struct ovectl_ratemanage_arg *)arg;
1051 hi->bitrate_limit_window=0;
1053 hi->bitrate_limit_window=ai->bitrate_hard_window;
1054 hi->bitrate_min=ai->bitrate_hard_min;
1055 hi->bitrate_max=ai->bitrate_hard_max;
1057 if(hi->bitrate_limit_window<0.)hi->bitrate_limit_window=0.;
1058 if(hi->bitrate_limit_window>10.)hi->bitrate_limit_window=10.;
1060 if( ( (hi->bitrate_av_lo<=0 && hi->bitrate_av_hi<=0)||
1061 (hi->bitrate_av_window<=0) ) &&
1062 ( (hi->bitrate_min<=0 && hi->bitrate_max<=0)||
1063 (hi->bitrate_limit_window<=0) ))
1068 case OV_ECTL_LOWPASS_GET:
1070 double *farg=(double *)arg;
1071 *farg=hi->lowpass_kHz;
1074 case OV_ECTL_LOWPASS_SET:
1076 double *farg=(double *)arg;
1077 hi->lowpass_kHz=*farg;
1079 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
1080 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
1083 case OV_ECTL_IBLOCK_GET:
1085 double *farg=(double *)arg;
1086 *farg=hi->impulse_noisetune;
1089 case OV_ECTL_IBLOCK_SET:
1091 double *farg=(double *)arg;
1092 hi->impulse_noisetune=*farg;
1094 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
1095 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;