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-2009 *
9 * by the Xiph.Org Foundation 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 const static_codebook *books[12][3];
40 int limit_type; /* 0 lowpass limited, 1 point stereo limited */
41 const vorbis_info_residue0 *res;
42 const static_codebook *book_aux;
43 const static_codebook *book_aux_managed;
44 const static_bookblock *books_base;
45 const static_bookblock *books_base_managed;
46 } vorbis_residue_template;
49 const vorbis_info_mapping0 *map;
50 const 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];
93 const double *rate_mapping;
94 const double *quality_mapping;
95 int coupling_restriction;
96 long samplerate_min_restriction;
97 long samplerate_max_restriction;
100 const int *blocksize_short;
101 const int *blocksize_long;
103 const att3 *psy_tone_masteratt;
104 const int *psy_tone_0dB;
105 const int *psy_tone_dBsuppress;
107 const vp_adjblock *psy_tone_adj_impulse;
108 const vp_adjblock *psy_tone_adj_long;
109 const vp_adjblock *psy_tone_adj_other;
111 const noiseguard *psy_noiseguards;
112 const noise3 *psy_noise_bias_impulse;
113 const noise3 *psy_noise_bias_padding;
114 const noise3 *psy_noise_bias_trans;
115 const noise3 *psy_noise_bias_long;
116 const int *psy_noise_dBsuppress;
118 const compandblock *psy_noise_compand;
119 const double *psy_noise_compand_short_mapping;
120 const double *psy_noise_compand_long_mapping;
122 const int *psy_noise_normal_start[2];
123 const int *psy_noise_normal_partition[2];
124 const double *psy_noise_normal_thresh;
126 const int *psy_ath_float;
127 const int *psy_ath_abs;
129 const double *psy_lowpass;
131 const vorbis_info_psy_global *global_params;
132 const double *global_mapping;
133 const adj_stereo *stereo_modes;
135 const static_codebook *const *const *const floor_books;
136 const vorbis_info_floor1 *floor_params;
137 const int floor_mappings;
138 const int **floor_mapping_list;
140 const vorbis_mapping_template *maps;
141 } ve_setup_data_template;
143 /* a few static coder conventions */
144 static const vorbis_info_mode _mode_template[2]={
149 static const 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 const ve_setup_data_template *const 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,int s,
200 const static_codebook *const *const *const books,
201 const 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));
211 int partitions=f->partitions;
214 for(i=0;i<partitions;i++)
215 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
216 for(i=0;i<=maxclass;i++){
217 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
218 f->class_book[i]+=ci->books;
219 for(k=0;k<(1<<f->class_subs[i]);k++){
220 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
221 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
225 for(i=0;i<=maxbook;i++)
226 ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i];
229 /* for now, we're only using floor 1 */
230 ci->floor_type[ci->floors]=1;
231 ci->floor_param[ci->floors]=f;
237 static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
238 const vorbis_info_psy_global *in,
242 codec_setup_info *ci=vi->codec_setup;
243 vorbis_info_psy_global *g=&ci->psy_g_param;
245 memcpy(g,in+(int)x[is],sizeof(*g));
247 ds=x[is]*(1.-ds)+x[is+1]*ds;
255 /* interpolate the trigger threshholds */
257 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
258 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
260 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
264 static void vorbis_encode_global_stereo(vorbis_info *vi,
265 const highlevel_encode_setup *const hi,
266 const adj_stereo *p){
267 float s=hi->stereo_point_setting;
270 codec_setup_info *ci=vi->codec_setup;
271 vorbis_info_psy_global *g=&ci->psy_g_param;
274 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
275 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
278 /* interpolate the kHz threshholds */
279 for(i=0;i<PACKETBLOBS;i++){
280 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
281 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
282 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
283 g->coupling_pkHz[i]=kHz;
285 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
286 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
287 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
291 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
292 for(i=0;i<PACKETBLOBS;i++){
293 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
294 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
295 g->coupling_pkHz[i]=kHz;
298 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
299 for(i=0;i<PACKETBLOBS;i++){
300 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
301 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
305 for(i=0;i<PACKETBLOBS;i++){
306 g->sliding_lowpass[0][i]=ci->blocksizes[0];
307 g->sliding_lowpass[1][i]=ci->blocksizes[1];
313 static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
315 const int *nn_partition,
316 const double *nn_thresh,
318 codec_setup_info *ci=vi->codec_setup;
319 vorbis_info_psy *p=ci->psy_param[block];
320 highlevel_encode_setup *hi=&ci->hi;
326 p=_ogg_calloc(1,sizeof(*p));
327 ci->psy_param[block]=p;
330 memcpy(p,&_psy_info_template,sizeof(*p));
331 p->blockflag=block>>1;
333 if(hi->noise_normalize_p){
334 p->normal_channel_p=1;
336 p->normal_start=nn_start[is];
337 p->normal_partition=nn_partition[is];
338 p->normal_thresh=nn_thresh[is];
344 static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
347 const vp_adjblock *in){
350 codec_setup_info *ci=vi->codec_setup;
351 vorbis_info_psy *p=ci->psy_param[block];
353 /* 0 and 2 are only used by bitmanagement, but there's no harm to always
354 filling the values in here */
355 p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
356 p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
357 p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
358 p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
359 p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
361 p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
363 for(i=0;i<P_BANDS;i++)
364 p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
369 static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
370 const compandblock *in,
374 codec_setup_info *ci=vi->codec_setup;
375 vorbis_info_psy *p=ci->psy_param[block];
377 ds=x[is]*(1.-ds)+x[is+1]*ds;
385 /* interpolate the compander settings */
386 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
387 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
391 static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
392 const int *suppress){
395 codec_setup_info *ci=vi->codec_setup;
396 vorbis_info_psy *p=ci->psy_param[block];
398 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
403 static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
406 const noiseguard *guard,
410 codec_setup_info *ci=vi->codec_setup;
411 vorbis_info_psy *p=ci->psy_param[block];
413 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
414 p->noisewindowlomin=guard[block].lo;
415 p->noisewindowhimin=guard[block].hi;
416 p->noisewindowfixed=guard[block].fixed;
418 for(j=0;j<P_NOISECURVES;j++)
419 for(i=0;i<P_BANDS;i++)
420 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
422 /* impulse blocks may take a user specified bias to boost the
423 nominal/high noise encoding depth */
424 for(j=0;j<P_NOISECURVES;j++){
425 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
426 for(i=0;i<P_BANDS;i++){
427 p->noiseoff[j][i]+=userbias;
428 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
435 static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
436 codec_setup_info *ci=vi->codec_setup;
437 vorbis_info_psy *p=ci->psy_param[block];
439 p->ath_adjatt=ci->hi.ath_floating_dB;
440 p->ath_maxatt=ci->hi.ath_absolute_dB;
445 static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){
447 for(i=0;i<ci->books;i++)
448 if(ci->book_param[i]==book)return(i);
453 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
454 const int *shortb,const int *longb){
456 codec_setup_info *ci=vi->codec_setup;
459 int blockshort=shortb[is];
460 int blocklong=longb[is];
461 ci->blocksizes[0]=blockshort;
462 ci->blocksizes[1]=blocklong;
466 static void vorbis_encode_residue_setup(vorbis_info *vi,
467 int number, int block,
468 const vorbis_residue_template *res){
470 codec_setup_info *ci=vi->codec_setup;
473 vorbis_info_residue0 *r=ci->residue_param[number]=
474 _ogg_malloc(sizeof(*r));
476 memcpy(r,res->res,sizeof(*r));
477 if(ci->residues<=number)ci->residues=number+1;
479 switch(ci->blocksizes[block]){
480 case 64:case 128:case 256:
487 ci->residue_type[number]=res->res_type;
489 /* to be adjusted by lowpass/pointlimit later */
490 n=r->end=ci->blocksizes[block]>>1;
492 n=r->end*=vi->channels;
494 /* fill in all the books */
499 for(i=0;i<r->partitions;i++)
501 if(res->books_base_managed->books[i][k])
502 r->secondstages[i]|=(1<<k);
504 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
505 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed;
507 for(i=0;i<r->partitions;i++){
509 if(res->books_base_managed->books[i][k]){
510 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
511 r->booklist[booklist++]=bookid;
512 ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k];
519 for(i=0;i<r->partitions;i++)
521 if(res->books_base->books[i][k])
522 r->secondstages[i]|=(1<<k);
524 r->groupbook=book_dup_or_new(ci,res->book_aux);
525 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux;
527 for(i=0;i<r->partitions;i++){
529 if(res->books_base->books[i][k]){
530 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
531 r->booklist[booklist++]=bookid;
532 ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k];
539 /* lowpass setup/pointlimit */
541 double freq=ci->hi.lowpass_kHz*1000.;
542 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
543 double nyq=vi->rate/2.;
544 long blocksize=ci->blocksizes[block]>>1;
546 /* lowpass needs to be set in the floor and the residue. */
547 if(freq>nyq)freq=nyq;
548 /* in the floor, the granularity can be very fine; it doesn't alter
549 the encoding structure, only the samples used to fit the floor
551 f->n=freq/nyq*blocksize;
553 /* this res may by limited by the maximum pointlimit of the mode,
554 not the lowpass. the floor is always lowpass limited. */
555 switch(res->limit_type){
556 case 1: /* point stereo 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;
563 case 2: /* LFE channel; lowpass at ~ 250Hz */
571 /* in the residue, we're constrained, physically, by partition
572 boundaries. We still lowpass 'wherever', but we have to round up
573 here to next boundary, or the vorbis spec will round it *down* to
574 previous boundary in encode/decode */
575 if(ci->residue_type[block]==2)
576 r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
579 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
581 if(r->end==0)r->end=r->grouping; /* LFE channel */
585 /* we assume two maps in this encoder */
586 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
587 const vorbis_mapping_template *maps){
589 codec_setup_info *ci=vi->codec_setup;
590 int i,j,is=s,modes=2;
591 const vorbis_info_mapping0 *map=maps[is].map;
592 const vorbis_info_mode *mode=_mode_template;
593 const vorbis_residue_template *res=maps[is].res;
595 if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
597 for(i=0;i<modes;i++){
599 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
600 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
602 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
603 if(i>=ci->modes)ci->modes=i+1;
606 memcpy(ci->map_param[i],map+i,sizeof(*map));
607 if(i>=ci->maps)ci->maps=i+1;
609 for(j=0;j<map[i].submaps;j++)
610 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
611 ,res+map[i].residuesubmap[j]);
615 static double setting_to_approx_bitrate(vorbis_info *vi){
616 codec_setup_info *ci=vi->codec_setup;
617 highlevel_encode_setup *hi=&ci->hi;
618 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
619 int is=hi->base_setting;
620 double ds=hi->base_setting-is;
622 const double *r=setup->rate_mapping;
627 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
630 static void get_setup_template(vorbis_info *vi,
632 double req,int q_or_bitrate){
634 codec_setup_info *ci=vi->codec_setup;
635 highlevel_encode_setup *hi=&ci->hi;
636 if(q_or_bitrate)req/=ch;
638 while(setup_list[i]){
639 if(setup_list[i]->coupling_restriction==-1 ||
640 setup_list[i]->coupling_restriction==ch){
641 if(srate>=setup_list[i]->samplerate_min_restriction &&
642 srate<=setup_list[i]->samplerate_max_restriction){
643 int mappings=setup_list[i]->mappings;
644 const double *map=(q_or_bitrate?
645 setup_list[i]->rate_mapping:
646 setup_list[i]->quality_mapping);
648 /* the template matches. Does the requested quality mode
649 fall within this template's modes? */
650 if(req<map[0]){++i;continue;}
651 if(req>map[setup_list[i]->mappings]){++i;continue;}
652 for(j=0;j<mappings;j++)
653 if(req>=map[j] && req<map[j+1])break;
654 /* an all-points match */
655 hi->setup=setup_list[i];
657 hi->base_setting=j-.001;
661 float del=(req-low)/(high-low);
662 hi->base_setting=j+del;
674 /* encoders will need to use vorbis_info_init beforehand and call
675 vorbis_info clear when all done */
677 /* two interfaces; this, more detailed one, and later a convenience
680 /* the final setup call */
681 int vorbis_encode_setup_init(vorbis_info *vi){
682 int i,i0=0,singleblock=0;
683 codec_setup_info *ci=vi->codec_setup;
684 ve_setup_data_template *setup=NULL;
685 highlevel_encode_setup *hi=&ci->hi;
687 if(ci==NULL)return(OV_EINVAL);
688 if(!hi->impulse_block_p)i0=1;
690 /* too low/high an ATH floater is nonsensical, but doesn't break anything */
691 if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
692 if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
694 /* again, bound this to avoid the app shooting itself int he foot
696 if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
697 if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
699 /* get the appropriate setup template; matches the fetch in previous
701 setup=(ve_setup_data_template *)hi->setup;
702 if(setup==NULL)return(OV_EINVAL);
705 /* choose block sizes from configured sizes as well as paying
706 attention to long_block_p and short_block_p. If the configured
707 short and long blocks are the same length, we set long_block_p
708 and unset short_block_p */
709 vorbis_encode_blocksize_setup(vi,hi->base_setting,
710 setup->blocksize_short,
711 setup->blocksize_long);
712 if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
714 /* floor setup; choose proper floor params. Allocated on the floor
715 stack in order; if we alloc only a single long floor, it's 0 */
716 for(i=0;i<setup->floor_mappings;i++)
717 vorbis_encode_floor_setup(vi,hi->base_setting,
720 setup->floor_mapping_list[i]);
722 /* setup of [mostly] short block detection and stereo*/
723 vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
724 setup->global_params,
725 setup->global_mapping);
726 vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
728 /* basic psych setup and noise normalization */
729 vorbis_encode_psyset_setup(vi,hi->base_setting,
730 setup->psy_noise_normal_start[0],
731 setup->psy_noise_normal_partition[0],
732 setup->psy_noise_normal_thresh,
734 vorbis_encode_psyset_setup(vi,hi->base_setting,
735 setup->psy_noise_normal_start[0],
736 setup->psy_noise_normal_partition[0],
737 setup->psy_noise_normal_thresh,
740 vorbis_encode_psyset_setup(vi,hi->base_setting,
741 setup->psy_noise_normal_start[1],
742 setup->psy_noise_normal_partition[1],
743 setup->psy_noise_normal_thresh,
745 vorbis_encode_psyset_setup(vi,hi->base_setting,
746 setup->psy_noise_normal_start[1],
747 setup->psy_noise_normal_partition[1],
748 setup->psy_noise_normal_thresh,
752 /* tone masking setup */
753 vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
754 setup->psy_tone_masteratt,
756 setup->psy_tone_adj_impulse);
757 vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
758 setup->psy_tone_masteratt,
760 setup->psy_tone_adj_other);
762 vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
763 setup->psy_tone_masteratt,
765 setup->psy_tone_adj_other);
766 vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
767 setup->psy_tone_masteratt,
769 setup->psy_tone_adj_long);
772 /* noise companding setup */
773 vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
774 setup->psy_noise_compand,
775 setup->psy_noise_compand_short_mapping);
776 vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
777 setup->psy_noise_compand,
778 setup->psy_noise_compand_short_mapping);
780 vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
781 setup->psy_noise_compand,
782 setup->psy_noise_compand_long_mapping);
783 vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
784 setup->psy_noise_compand,
785 setup->psy_noise_compand_long_mapping);
788 /* peak guarding setup */
789 vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
790 setup->psy_tone_dBsuppress);
791 vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
792 setup->psy_tone_dBsuppress);
794 vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
795 setup->psy_tone_dBsuppress);
796 vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
797 setup->psy_tone_dBsuppress);
800 /* noise bias setup */
801 vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
802 setup->psy_noise_dBsuppress,
803 setup->psy_noise_bias_impulse,
804 setup->psy_noiseguards,
805 (i0==0?hi->impulse_noisetune:0.));
806 vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
807 setup->psy_noise_dBsuppress,
808 setup->psy_noise_bias_padding,
809 setup->psy_noiseguards,0.);
811 vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
812 setup->psy_noise_dBsuppress,
813 setup->psy_noise_bias_trans,
814 setup->psy_noiseguards,0.);
815 vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
816 setup->psy_noise_dBsuppress,
817 setup->psy_noise_bias_long,
818 setup->psy_noiseguards,0.);
821 vorbis_encode_ath_setup(vi,0);
822 vorbis_encode_ath_setup(vi,1);
824 vorbis_encode_ath_setup(vi,2);
825 vorbis_encode_ath_setup(vi,3);
828 vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
830 /* set bitrate readonlies and management */
832 vi->bitrate_nominal=hi->bitrate_av;
834 vi->bitrate_nominal=setting_to_approx_bitrate(vi);
837 vi->bitrate_lower=hi->bitrate_min;
838 vi->bitrate_upper=hi->bitrate_max;
840 vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
842 vi->bitrate_window=0.;
845 ci->bi.avg_rate=hi->bitrate_av;
846 ci->bi.min_rate=hi->bitrate_min;
847 ci->bi.max_rate=hi->bitrate_max;
849 ci->bi.reservoir_bits=hi->bitrate_reservoir;
850 ci->bi.reservoir_bias=
851 hi->bitrate_reservoir_bias;
853 ci->bi.slew_damp=hi->bitrate_av_damp;
861 static int vorbis_encode_setup_setting(vorbis_info *vi,
865 codec_setup_info *ci=vi->codec_setup;
866 highlevel_encode_setup *hi=&ci->hi;
867 const ve_setup_data_template *setup=hi->setup;
870 ret=vorbis_encode_toplevel_setup(vi,channels,rate);
874 ds=hi->base_setting-is;
878 hi->impulse_block_p=1;
879 hi->noise_normalize_p=1;
881 hi->stereo_point_setting=hi->base_setting;
883 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
885 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
886 setup->psy_ath_float[is+1]*ds;
887 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
888 setup->psy_ath_abs[is+1]*ds;
890 hi->amplitude_track_dBpersec=-6.;
891 hi->trigger_setting=hi->base_setting;
894 hi->block[i].tone_mask_setting=hi->base_setting;
895 hi->block[i].tone_peaklimit_setting=hi->base_setting;
896 hi->block[i].noise_bias_setting=hi->base_setting;
897 hi->block[i].noise_compand_setting=hi->base_setting;
903 int vorbis_encode_setup_vbr(vorbis_info *vi,
907 codec_setup_info *ci=vi->codec_setup;
908 highlevel_encode_setup *hi=&ci->hi;
911 if(quality>=1.)quality=.9999;
913 get_setup_template(vi,channels,rate,quality,0);
914 if(!hi->setup)return OV_EIMPL;
916 return vorbis_encode_setup_setting(vi,channels,rate);
919 int vorbis_encode_init_vbr(vorbis_info *vi,
923 float base_quality /* 0. to 1. */
927 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
930 vorbis_info_clear(vi);
933 ret=vorbis_encode_setup_init(vi);
935 vorbis_info_clear(vi);
939 int vorbis_encode_setup_managed(vorbis_info *vi,
944 long nominal_bitrate,
947 codec_setup_info *ci=vi->codec_setup;
948 highlevel_encode_setup *hi=&ci->hi;
949 double tnominal=nominal_bitrate;
952 if(nominal_bitrate<=0.){
955 nominal_bitrate=(max_bitrate+min_bitrate)*.5;
957 nominal_bitrate=max_bitrate*.875;
960 nominal_bitrate=min_bitrate;
967 get_setup_template(vi,channels,rate,nominal_bitrate,1);
968 if(!hi->setup)return OV_EIMPL;
970 ret=vorbis_encode_setup_setting(vi,channels,rate);
972 vorbis_info_clear(vi);
976 /* initialize management with sane defaults */
978 hi->bitrate_min=min_bitrate;
979 hi->bitrate_max=max_bitrate;
980 hi->bitrate_av=tnominal;
981 hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
982 hi->bitrate_reservoir=nominal_bitrate*2;
983 hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */
989 int vorbis_encode_init(vorbis_info *vi,
994 long nominal_bitrate,
997 int ret=vorbis_encode_setup_managed(vi,channels,rate,
1002 vorbis_info_clear(vi);
1006 ret=vorbis_encode_setup_init(vi);
1008 vorbis_info_clear(vi);
1012 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
1014 codec_setup_info *ci=vi->codec_setup;
1015 highlevel_encode_setup *hi=&ci->hi;
1016 int setp=(number&0xf); /* a read request has a low nibble of 0 */
1018 if(setp && hi->set_in_stone)return(OV_EINVAL);
1022 /* now deprecated *****************/
1023 case OV_ECTL_RATEMANAGE_GET:
1026 struct ovectl_ratemanage_arg *ai=
1027 (struct ovectl_ratemanage_arg *)arg;
1029 ai->management_active=hi->managed;
1030 ai->bitrate_hard_window=ai->bitrate_av_window=
1031 (double)hi->bitrate_reservoir/vi->rate;
1032 ai->bitrate_av_window_center=1.;
1033 ai->bitrate_hard_min=hi->bitrate_min;
1034 ai->bitrate_hard_max=hi->bitrate_max;
1035 ai->bitrate_av_lo=hi->bitrate_av;
1036 ai->bitrate_av_hi=hi->bitrate_av;
1041 /* now deprecated *****************/
1042 case OV_ECTL_RATEMANAGE_SET:
1044 struct ovectl_ratemanage_arg *ai=
1045 (struct ovectl_ratemanage_arg *)arg;
1049 hi->managed=ai->management_active;
1050 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
1051 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
1056 /* now deprecated *****************/
1057 case OV_ECTL_RATEMANAGE_AVG:
1059 struct ovectl_ratemanage_arg *ai=
1060 (struct ovectl_ratemanage_arg *)arg;
1064 hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
1068 /* now deprecated *****************/
1069 case OV_ECTL_RATEMANAGE_HARD:
1071 struct ovectl_ratemanage_arg *ai=
1072 (struct ovectl_ratemanage_arg *)arg;
1077 hi->bitrate_min=ai->bitrate_hard_min;
1078 hi->bitrate_max=ai->bitrate_hard_max;
1079 hi->bitrate_reservoir=ai->bitrate_hard_window*
1080 (hi->bitrate_max+hi->bitrate_min)*.5;
1082 if(hi->bitrate_reservoir<128.)
1083 hi->bitrate_reservoir=128.;
1087 /* replacement ratemanage interface */
1088 case OV_ECTL_RATEMANAGE2_GET:
1090 struct ovectl_ratemanage2_arg *ai=
1091 (struct ovectl_ratemanage2_arg *)arg;
1092 if(ai==NULL)return OV_EINVAL;
1094 ai->management_active=hi->managed;
1095 ai->bitrate_limit_min_kbps=hi->bitrate_min/1000;
1096 ai->bitrate_limit_max_kbps=hi->bitrate_max/1000;
1097 ai->bitrate_average_kbps=hi->bitrate_av/1000;
1098 ai->bitrate_average_damping=hi->bitrate_av_damp;
1099 ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
1100 ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
1103 case OV_ECTL_RATEMANAGE2_SET:
1105 struct ovectl_ratemanage2_arg *ai=
1106 (struct ovectl_ratemanage2_arg *)arg;
1110 /* sanity check; only catch invariant violations */
1111 if(ai->bitrate_limit_min_kbps>0 &&
1112 ai->bitrate_average_kbps>0 &&
1113 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
1116 if(ai->bitrate_limit_max_kbps>0 &&
1117 ai->bitrate_average_kbps>0 &&
1118 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
1121 if(ai->bitrate_limit_min_kbps>0 &&
1122 ai->bitrate_limit_max_kbps>0 &&
1123 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
1126 if(ai->bitrate_average_damping <= 0.)
1129 if(ai->bitrate_limit_reservoir_bits < 0)
1132 if(ai->bitrate_limit_reservoir_bias < 0.)
1135 if(ai->bitrate_limit_reservoir_bias > 1.)
1138 hi->managed=ai->management_active;
1139 hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000;
1140 hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000;
1141 hi->bitrate_av=ai->bitrate_average_kbps * 1000;
1142 hi->bitrate_av_damp=ai->bitrate_average_damping;
1143 hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
1144 hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
1149 case OV_ECTL_LOWPASS_GET:
1151 double *farg=(double *)arg;
1152 *farg=hi->lowpass_kHz;
1155 case OV_ECTL_LOWPASS_SET:
1157 double *farg=(double *)arg;
1158 hi->lowpass_kHz=*farg;
1160 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
1161 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
1164 case OV_ECTL_IBLOCK_GET:
1166 double *farg=(double *)arg;
1167 *farg=hi->impulse_noisetune;
1170 case OV_ECTL_IBLOCK_SET:
1172 double *farg=(double *)arg;
1173 hi->impulse_noisetune=*farg;
1175 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
1176 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;