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
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 /* a few static coder conventions */
144 static vorbis_info_mode _mode_template[2]={
149 static vorbis_info_mapping0 _map_nominal[2]={
150 {1, {0,0}, {0}, {0}, 1,{0},{1}},
151 {1, {0,0}, {1}, {1}, 1,{0},{1}}
154 #include "modes/setup_44.h"
155 #include "modes/setup_44u.h"
156 #include "modes/setup_32.h"
157 #include "modes/setup_8.h"
158 #include "modes/setup_11.h"
159 #include "modes/setup_16.h"
160 #include "modes/setup_22.h"
161 #include "modes/setup_X.h"
163 static ve_setup_data_template *setup_list[]={
165 &ve_setup_44_uncoupled,
168 &ve_setup_32_uncoupled,
171 &ve_setup_22_uncoupled,
173 &ve_setup_16_uncoupled,
176 &ve_setup_11_uncoupled,
178 &ve_setup_8_uncoupled,
181 &ve_setup_X_uncoupled,
183 &ve_setup_XX_uncoupled,
187 static int vorbis_encode_toplevel_setup(vorbis_info *vi,int ch,long rate){
188 if(vi && vi->codec_setup){
199 static void vorbis_encode_floor_setup(vorbis_info *vi,double s,int block,
200 static_codebook ***books,
201 vorbis_info_floor1 *in,
204 vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
205 codec_setup_info *ci=vi->codec_setup;
207 memcpy(f,in+x[is],sizeof(*f));
208 /* fill in the lowpass field, even if it's temporary */
209 f->n=ci->blocksizes[block]>>1;
213 int partitions=f->partitions;
216 for(i=0;i<partitions;i++)
217 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
218 for(i=0;i<=maxclass;i++){
219 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
220 f->class_book[i]+=ci->books;
221 for(k=0;k<(1<<f->class_subs[i]);k++){
222 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
223 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
227 for(i=0;i<=maxbook;i++)
228 ci->book_param[ci->books++]=books[x[is]][i];
231 /* for now, we're only using floor 1 */
232 ci->floor_type[ci->floors]=1;
233 ci->floor_param[ci->floors]=f;
239 static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
240 vorbis_info_psy_global *in,
244 codec_setup_info *ci=vi->codec_setup;
245 vorbis_info_psy_global *g=&ci->psy_g_param;
247 memcpy(g,in+(int)x[is],sizeof(*g));
249 ds=x[is]*(1.-ds)+x[is+1]*ds;
257 /* interpolate the trigger threshholds */
259 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
260 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
262 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
266 static void vorbis_encode_global_stereo(vorbis_info *vi,
267 highlevel_encode_setup *hi,
269 float s=hi->stereo_point_setting;
272 codec_setup_info *ci=vi->codec_setup;
273 vorbis_info_psy_global *g=&ci->psy_g_param;
276 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
277 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
280 /* interpolate the kHz threshholds */
281 for(i=0;i<PACKETBLOBS;i++){
282 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
283 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
284 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
285 g->coupling_pkHz[i]=kHz;
287 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
288 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
289 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
293 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
294 for(i=0;i<PACKETBLOBS;i++){
295 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
296 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
297 g->coupling_pkHz[i]=kHz;
300 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
301 for(i=0;i<PACKETBLOBS;i++){
302 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
303 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
307 for(i=0;i<PACKETBLOBS;i++){
308 g->sliding_lowpass[0][i]=ci->blocksizes[0];
309 g->sliding_lowpass[1][i]=ci->blocksizes[1];
315 static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
320 codec_setup_info *ci=vi->codec_setup;
321 vorbis_info_psy *p=ci->psy_param[block];
322 highlevel_encode_setup *hi=&ci->hi;
328 p=_ogg_calloc(1,sizeof(*p));
329 ci->psy_param[block]=p;
332 memcpy(p,&_psy_info_template,sizeof(*p));
333 p->blockflag=block>>1;
335 if(hi->noise_normalize_p){
336 p->normal_channel_p=1;
338 p->normal_start=nn_start[is];
339 p->normal_partition=nn_partition[is];
340 p->normal_thresh=nn_thresh[is];
346 static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
352 codec_setup_info *ci=vi->codec_setup;
353 vorbis_info_psy *p=ci->psy_param[block];
355 /* 0 and 2 are only used by bitmanagement, but there's no harm to always
356 filling the values in here */
357 p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
358 p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
359 p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
360 p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
361 p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
363 p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
365 for(i=0;i<P_BANDS;i++)
366 p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
371 static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
372 compandblock *in, double *x){
375 codec_setup_info *ci=vi->codec_setup;
376 vorbis_info_psy *p=ci->psy_param[block];
378 ds=x[is]*(1.-ds)+x[is+1]*ds;
386 /* interpolate the compander settings */
387 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
388 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
392 static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
396 codec_setup_info *ci=vi->codec_setup;
397 vorbis_info_psy *p=ci->psy_param[block];
399 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
404 static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
411 codec_setup_info *ci=vi->codec_setup;
412 vorbis_info_psy *p=ci->psy_param[block];
414 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
415 p->noisewindowlomin=guard[block].lo;
416 p->noisewindowhimin=guard[block].hi;
417 p->noisewindowfixed=guard[block].fixed;
419 for(j=0;j<P_NOISECURVES;j++)
420 for(i=0;i<P_BANDS;i++)
421 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
423 /* impulse blocks may take a user specified bias to boost the
424 nominal/high noise encoding depth */
425 for(j=0;j<P_NOISECURVES;j++){
426 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
427 for(i=0;i<P_BANDS;i++){
428 p->noiseoff[j][i]+=userbias;
429 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
436 static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
437 codec_setup_info *ci=vi->codec_setup;
438 vorbis_info_psy *p=ci->psy_param[block];
440 p->ath_adjatt=ci->hi.ath_floating_dB;
441 p->ath_maxatt=ci->hi.ath_absolute_dB;
446 static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
448 for(i=0;i<ci->books;i++)
449 if(ci->book_param[i]==book)return(i);
454 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
455 int *shortb,int *longb){
457 codec_setup_info *ci=vi->codec_setup;
460 int blockshort=shortb[is];
461 int blocklong=longb[is];
462 ci->blocksizes[0]=blockshort;
463 ci->blocksizes[1]=blocklong;
467 static void vorbis_encode_residue_setup(vorbis_info *vi,
468 int number, int block,
469 vorbis_residue_template *res){
471 codec_setup_info *ci=vi->codec_setup;
474 vorbis_info_residue0 *r=ci->residue_param[number]=
475 _ogg_malloc(sizeof(*r));
477 memcpy(r,res->res,sizeof(*r));
478 if(ci->residues<=number)ci->residues=number+1;
480 switch(ci->blocksizes[block]){
481 case 64:case 128:case 256:
488 ci->residue_type[number]=res->res_type;
490 /* to be adjusted by lowpass/pointlimit later */
491 n=r->end=ci->blocksizes[block]>>1;
493 n=r->end*=vi->channels;
495 /* fill in all the books */
500 for(i=0;i<r->partitions;i++)
502 if(res->books_base_managed->books[i][k])
503 r->secondstages[i]|=(1<<k);
505 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
506 ci->book_param[r->groupbook]=res->book_aux_managed;
508 for(i=0;i<r->partitions;i++){
510 if(res->books_base_managed->books[i][k]){
511 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
512 r->booklist[booklist++]=bookid;
513 ci->book_param[bookid]=res->books_base_managed->books[i][k];
520 for(i=0;i<r->partitions;i++)
522 if(res->books_base->books[i][k])
523 r->secondstages[i]|=(1<<k);
525 r->groupbook=book_dup_or_new(ci,res->book_aux);
526 ci->book_param[r->groupbook]=res->book_aux;
528 for(i=0;i<r->partitions;i++){
530 if(res->books_base->books[i][k]){
531 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
532 r->booklist[booklist++]=bookid;
533 ci->book_param[bookid]=res->books_base->books[i][k];
540 /* lowpass setup/pointlimit */
542 double freq=ci->hi.lowpass_kHz*1000.;
543 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
544 double nyq=vi->rate/2.;
545 long blocksize=ci->blocksizes[block]>>1;
547 /* lowpass needs to be set in the floor and the residue. */
548 if(freq>nyq)freq=nyq;
549 /* in the floor, the granularity can be very fine; it doesn't alter
550 the encoding structure, only the samples used to fit the floor
552 f->n=freq/nyq*blocksize;
554 /* this res may by limited by the maximum pointlimit of the mode,
555 not the lowpass. the floor is always lowpass limited. */
558 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
560 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
561 if(freq>nyq)freq=nyq;
564 /* in the residue, we're constrained, physically, by partition
565 boundaries. We still lowpass 'wherever', but we have to round up
566 here to next boundary, or the vorbis spec will round it *down* to
567 previous boundary in encode/decode */
568 if(ci->residue_type[block]==2)
569 r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
572 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
577 /* we assume two maps in this encoder */
578 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
579 vorbis_mapping_template *maps){
581 codec_setup_info *ci=vi->codec_setup;
582 int i,j,is=s,modes=2;
583 vorbis_info_mapping0 *map=maps[is].map;
584 vorbis_info_mode *mode=_mode_template;
585 vorbis_residue_template *res=maps[is].res;
587 if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
589 for(i=0;i<modes;i++){
591 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
592 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
594 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
595 if(i>=ci->modes)ci->modes=i+1;
598 memcpy(ci->map_param[i],map+i,sizeof(*map));
599 if(i>=ci->maps)ci->maps=i+1;
601 for(j=0;j<map[i].submaps;j++)
602 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
603 ,res+map[i].residuesubmap[j]);
607 static double setting_to_approx_bitrate(vorbis_info *vi){
608 codec_setup_info *ci=vi->codec_setup;
609 highlevel_encode_setup *hi=&ci->hi;
610 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
611 int is=hi->base_setting;
612 double ds=hi->base_setting-is;
614 double *r=setup->rate_mapping;
619 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
622 static void get_setup_template(vorbis_info *vi,
624 double req,int q_or_bitrate){
626 codec_setup_info *ci=vi->codec_setup;
627 highlevel_encode_setup *hi=&ci->hi;
628 if(q_or_bitrate)req/=ch;
630 while(setup_list[i]){
631 if(setup_list[i]->coupling_restriction==-1 ||
632 setup_list[i]->coupling_restriction==ch){
633 if(srate>=setup_list[i]->samplerate_min_restriction &&
634 srate<=setup_list[i]->samplerate_max_restriction){
635 int mappings=setup_list[i]->mappings;
636 double *map=(q_or_bitrate?
637 setup_list[i]->rate_mapping:
638 setup_list[i]->quality_mapping);
640 /* the template matches. Does the requested quality mode
641 fall within this template's modes? */
642 if(req<map[0]){++i;continue;}
643 if(req>map[setup_list[i]->mappings]){++i;continue;}
644 for(j=0;j<mappings;j++)
645 if(req>=map[j] && req<map[j+1])break;
646 /* an all-points match */
647 hi->setup=setup_list[i];
649 hi->base_setting=j-.001;
653 float del=(req-low)/(high-low);
654 hi->base_setting=j+del;
666 /* encoders will need to use vorbis_info_init beforehand and call
667 vorbis_info clear when all done */
669 /* two interfaces; this, more detailed one, and later a convenience
672 /* the final setup call */
673 int vorbis_encode_setup_init(vorbis_info *vi){
674 int i0=0,singleblock=0;
675 codec_setup_info *ci=vi->codec_setup;
676 ve_setup_data_template *setup=NULL;
677 highlevel_encode_setup *hi=&ci->hi;
679 if(ci==NULL)return(OV_EINVAL);
680 if(!hi->impulse_block_p)i0=1;
682 /* too low/high an ATH floater is nonsensical, but doesn't break anything */
683 if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
684 if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
686 /* again, bound this to avoid the app shooting itself int he foot
688 if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
689 if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
691 /* get the appropriate setup template; matches the fetch in previous
693 setup=(ve_setup_data_template *)hi->setup;
694 if(setup==NULL)return(OV_EINVAL);
697 /* choose block sizes from configured sizes as well as paying
698 attention to long_block_p and short_block_p. If the configured
699 short and long blocks are the same length, we set long_block_p
700 and unset short_block_p */
701 vorbis_encode_blocksize_setup(vi,hi->base_setting,
702 setup->blocksize_short,
703 setup->blocksize_long);
704 if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
706 /* floor setup; choose proper floor params. Allocated on the floor
707 stack in order; if we alloc only long floor, it's 0 */
708 vorbis_encode_floor_setup(vi,hi->short_setting,0,
711 setup->floor_short_mapping);
713 vorbis_encode_floor_setup(vi,hi->long_setting,1,
716 setup->floor_long_mapping);
718 /* setup of [mostly] short block detection and stereo*/
719 vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
720 setup->global_params,
721 setup->global_mapping);
722 vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
724 /* basic psych setup and noise normalization */
725 vorbis_encode_psyset_setup(vi,hi->short_setting,
726 setup->psy_noise_normal_start[0],
727 setup->psy_noise_normal_partition[0],
728 setup->psy_noise_normal_thresh,
730 vorbis_encode_psyset_setup(vi,hi->short_setting,
731 setup->psy_noise_normal_start[0],
732 setup->psy_noise_normal_partition[0],
733 setup->psy_noise_normal_thresh,
736 vorbis_encode_psyset_setup(vi,hi->long_setting,
737 setup->psy_noise_normal_start[1],
738 setup->psy_noise_normal_partition[1],
739 setup->psy_noise_normal_thresh,
741 vorbis_encode_psyset_setup(vi,hi->long_setting,
742 setup->psy_noise_normal_start[1],
743 setup->psy_noise_normal_partition[1],
744 setup->psy_noise_normal_thresh,
748 /* tone masking setup */
749 vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
750 setup->psy_tone_masteratt,
752 setup->psy_tone_adj_impulse);
753 vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
754 setup->psy_tone_masteratt,
756 setup->psy_tone_adj_other);
758 vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
759 setup->psy_tone_masteratt,
761 setup->psy_tone_adj_other);
762 vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
763 setup->psy_tone_masteratt,
765 setup->psy_tone_adj_long);
768 /* noise companding setup */
769 vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
770 setup->psy_noise_compand,
771 setup->psy_noise_compand_short_mapping);
772 vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
773 setup->psy_noise_compand,
774 setup->psy_noise_compand_short_mapping);
776 vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
777 setup->psy_noise_compand,
778 setup->psy_noise_compand_long_mapping);
779 vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
780 setup->psy_noise_compand,
781 setup->psy_noise_compand_long_mapping);
784 /* peak guarding setup */
785 vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
786 setup->psy_tone_dBsuppress);
787 vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
788 setup->psy_tone_dBsuppress);
790 vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
791 setup->psy_tone_dBsuppress);
792 vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
793 setup->psy_tone_dBsuppress);
796 /* noise bias setup */
797 vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
798 setup->psy_noise_dBsuppress,
799 setup->psy_noise_bias_impulse,
800 setup->psy_noiseguards,
801 (i0==0?hi->impulse_noisetune:0.));
802 vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
803 setup->psy_noise_dBsuppress,
804 setup->psy_noise_bias_padding,
805 setup->psy_noiseguards,0.);
807 vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
808 setup->psy_noise_dBsuppress,
809 setup->psy_noise_bias_trans,
810 setup->psy_noiseguards,0.);
811 vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
812 setup->psy_noise_dBsuppress,
813 setup->psy_noise_bias_long,
814 setup->psy_noiseguards,0.);
817 vorbis_encode_ath_setup(vi,0);
818 vorbis_encode_ath_setup(vi,1);
820 vorbis_encode_ath_setup(vi,2);
821 vorbis_encode_ath_setup(vi,3);
824 vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
826 /* set bitrate readonlies and management */
828 vi->bitrate_nominal=hi->bitrate_av;
830 vi->bitrate_nominal=setting_to_approx_bitrate(vi);
833 vi->bitrate_lower=hi->bitrate_min;
834 vi->bitrate_upper=hi->bitrate_max;
836 vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
838 vi->bitrate_window=0.;
841 ci->bi.avg_rate=hi->bitrate_av;
842 ci->bi.min_rate=hi->bitrate_min;
843 ci->bi.max_rate=hi->bitrate_max;
845 ci->bi.reservoir_bits=hi->bitrate_reservoir;
846 ci->bi.reservoir_bias=
847 hi->bitrate_reservoir_bias;
849 ci->bi.slew_damp=hi->bitrate_av_damp;
857 static int vorbis_encode_setup_setting(vorbis_info *vi,
861 codec_setup_info *ci=vi->codec_setup;
862 highlevel_encode_setup *hi=&ci->hi;
863 ve_setup_data_template *setup=hi->setup;
866 ret=vorbis_encode_toplevel_setup(vi,channels,rate);
870 ds=hi->base_setting-is;
872 hi->short_setting=hi->base_setting;
873 hi->long_setting=hi->base_setting;
877 hi->impulse_block_p=1;
878 hi->noise_normalize_p=1;
880 hi->stereo_point_setting=hi->base_setting;
882 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
884 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
885 setup->psy_ath_float[is+1]*ds;
886 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
887 setup->psy_ath_abs[is+1]*ds;
889 hi->amplitude_track_dBpersec=-6.;
890 hi->trigger_setting=hi->base_setting;
893 hi->block[i].tone_mask_setting=hi->base_setting;
894 hi->block[i].tone_peaklimit_setting=hi->base_setting;
895 hi->block[i].noise_bias_setting=hi->base_setting;
896 hi->block[i].noise_compand_setting=hi->base_setting;
902 int vorbis_encode_setup_vbr(vorbis_info *vi,
906 codec_setup_info *ci=vi->codec_setup;
907 highlevel_encode_setup *hi=&ci->hi;
910 if(quality>=1.)quality=.9999;
912 get_setup_template(vi,channels,rate,quality,0);
913 if(!hi->setup)return OV_EIMPL;
915 return vorbis_encode_setup_setting(vi,channels,rate);
918 int vorbis_encode_init_vbr(vorbis_info *vi,
922 float base_quality /* 0. to 1. */
926 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
929 vorbis_info_clear(vi);
932 ret=vorbis_encode_setup_init(vi);
934 vorbis_info_clear(vi);
938 int vorbis_encode_setup_managed(vorbis_info *vi,
943 long nominal_bitrate,
946 codec_setup_info *ci=vi->codec_setup;
947 highlevel_encode_setup *hi=&ci->hi;
948 double tnominal=nominal_bitrate;
951 if(nominal_bitrate<=0.){
954 nominal_bitrate=(max_bitrate+min_bitrate)*.5;
956 nominal_bitrate=max_bitrate*.875;
959 nominal_bitrate=min_bitrate;
966 get_setup_template(vi,channels,rate,nominal_bitrate,1);
967 if(!hi->setup)return OV_EIMPL;
969 ret=vorbis_encode_setup_setting(vi,channels,rate);
971 vorbis_info_clear(vi);
975 /* initialize management with sane defaults */
977 hi->bitrate_min=min_bitrate;
978 hi->bitrate_max=max_bitrate;
979 hi->bitrate_av=tnominal;
980 hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
981 hi->bitrate_reservoir=nominal_bitrate*2;
982 hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */
988 int vorbis_encode_init(vorbis_info *vi,
993 long nominal_bitrate,
996 int ret=vorbis_encode_setup_managed(vi,channels,rate,
1001 vorbis_info_clear(vi);
1005 ret=vorbis_encode_setup_init(vi);
1007 vorbis_info_clear(vi);
1011 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
1013 codec_setup_info *ci=vi->codec_setup;
1014 highlevel_encode_setup *hi=&ci->hi;
1015 int setp=(number&0xf); /* a read request has a low nibble of 0 */
1017 if(setp && hi->set_in_stone)return(OV_EINVAL);
1021 /* now deprecated *****************/
1022 case OV_ECTL_RATEMANAGE_GET:
1025 struct ovectl_ratemanage_arg *ai=
1026 (struct ovectl_ratemanage_arg *)arg;
1028 ai->management_active=hi->managed;
1029 ai->bitrate_hard_window=ai->bitrate_av_window=
1030 (double)hi->bitrate_reservoir/vi->rate;
1031 ai->bitrate_av_window_center=1.;
1032 ai->bitrate_hard_min=hi->bitrate_min;
1033 ai->bitrate_hard_max=hi->bitrate_max;
1034 ai->bitrate_av_lo=hi->bitrate_av;
1035 ai->bitrate_av_hi=hi->bitrate_av;
1040 /* now deprecated *****************/
1041 case OV_ECTL_RATEMANAGE_SET:
1043 struct ovectl_ratemanage_arg *ai=
1044 (struct ovectl_ratemanage_arg *)arg;
1048 hi->managed=ai->management_active;
1049 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
1050 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
1055 /* now deprecated *****************/
1056 case OV_ECTL_RATEMANAGE_AVG:
1058 struct ovectl_ratemanage_arg *ai=
1059 (struct ovectl_ratemanage_arg *)arg;
1063 hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
1067 /* now deprecated *****************/
1068 case OV_ECTL_RATEMANAGE_HARD:
1070 struct ovectl_ratemanage_arg *ai=
1071 (struct ovectl_ratemanage_arg *)arg;
1076 hi->bitrate_min=ai->bitrate_hard_min;
1077 hi->bitrate_max=ai->bitrate_hard_max;
1078 hi->bitrate_reservoir=ai->bitrate_hard_window*
1079 (hi->bitrate_max+hi->bitrate_min)*.5;
1081 if(hi->bitrate_reservoir<128.)
1082 hi->bitrate_reservoir=128.;
1086 /* replacement ratemanage interface */
1087 case OV_ECTL_RATEMANAGE2_GET:
1089 struct ovectl_ratemanage2_arg *ai=
1090 (struct ovectl_ratemanage2_arg *)arg;
1091 if(ai==NULL)return OV_EINVAL;
1093 ai->management_active=hi->managed;
1094 ai->bitrate_limit_min_kbps=hi->bitrate_min;
1095 ai->bitrate_limit_max_kbps=hi->bitrate_max;
1096 ai->bitrate_average_kbps=hi->bitrate_av;
1097 ai->bitrate_average_damping=hi->bitrate_av_damp;
1098 ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
1099 ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
1102 case OV_ECTL_RATEMANAGE2_SET:
1104 struct ovectl_ratemanage2_arg *ai=
1105 (struct ovectl_ratemanage2_arg *)arg;
1109 /* sanity check; only catch invariant violations */
1110 if(ai->bitrate_limit_min_kbps>0 &&
1111 ai->bitrate_average_kbps>0 &&
1112 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
1115 if(ai->bitrate_limit_max_kbps>0 &&
1116 ai->bitrate_average_kbps>0 &&
1117 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
1120 if(ai->bitrate_limit_min_kbps>0 &&
1121 ai->bitrate_limit_max_kbps>0 &&
1122 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
1125 if(ai->bitrate_average_damping <= 0.)
1128 if(ai->bitrate_limit_reservoir_bits < 0)
1131 if(ai->bitrate_limit_reservoir_bias < 0.)
1134 if(ai->bitrate_limit_reservoir_bias > 1.)
1137 hi->managed=ai->management_active;
1138 hi->bitrate_min=ai->bitrate_limit_min_kbps;
1139 hi->bitrate_max=ai->bitrate_limit_max_kbps;
1140 hi->bitrate_av=ai->bitrate_average_kbps;
1141 hi->bitrate_av_damp=ai->bitrate_average_damping;
1142 hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
1143 hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
1148 case OV_ECTL_LOWPASS_GET:
1150 double *farg=(double *)arg;
1151 *farg=hi->lowpass_kHz;
1154 case OV_ECTL_LOWPASS_SET:
1156 double *farg=(double *)arg;
1157 hi->lowpass_kHz=*farg;
1159 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
1160 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
1163 case OV_ECTL_IBLOCK_GET:
1165 double *farg=(double *)arg;
1166 *farg=hi->impulse_noisetune;
1169 case OV_ECTL_IBLOCK_SET:
1171 double *farg=(double *)arg;
1172 hi->impulse_noisetune=*farg;
1174 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
1175 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;