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-2015 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: simple programmatic interface for encoder mode setup
15 ********************************************************************/
21 #include "vorbis/codec.h"
22 #include "vorbis/vorbisenc.h"
24 #include "codec_internal.h"
29 /* careful with this; it's using static array sizing to make managing
30 all the modes a little less annoying. If we use a residue backend
31 with > 12 partition types, or a different division of iteration,
32 this needs to be updated. */
34 const static_codebook *books[12][4];
39 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_44p51.h"
157 #include "modes/setup_32.h"
158 #include "modes/setup_8.h"
159 #include "modes/setup_11.h"
160 #include "modes/setup_16.h"
161 #include "modes/setup_22.h"
162 #include "modes/setup_X.h"
164 static const ve_setup_data_template *const setup_list[]={
167 &ve_setup_44_uncoupled,
170 &ve_setup_32_uncoupled,
173 &ve_setup_22_uncoupled,
175 &ve_setup_16_uncoupled,
178 &ve_setup_11_uncoupled,
180 &ve_setup_8_uncoupled,
183 &ve_setup_X_uncoupled,
185 &ve_setup_XX_uncoupled,
189 static void vorbis_encode_floor_setup(vorbis_info *vi,int s,
190 const static_codebook *const *const *const books,
191 const vorbis_info_floor1 *in,
194 vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
195 codec_setup_info *ci=vi->codec_setup;
197 memcpy(f,in+x[is],sizeof(*f));
201 int partitions=f->partitions;
204 for(i=0;i<partitions;i++)
205 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
206 for(i=0;i<=maxclass;i++){
207 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
208 f->class_book[i]+=ci->books;
209 for(k=0;k<(1<<f->class_subs[i]);k++){
210 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
211 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
215 for(i=0;i<=maxbook;i++)
216 ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i];
219 /* for now, we're only using floor 1 */
220 ci->floor_type[ci->floors]=1;
221 ci->floor_param[ci->floors]=f;
227 static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
228 const vorbis_info_psy_global *in,
232 codec_setup_info *ci=vi->codec_setup;
233 vorbis_info_psy_global *g=&ci->psy_g_param;
235 memcpy(g,in+(int)x[is],sizeof(*g));
237 ds=x[is]*(1.-ds)+x[is+1]*ds;
245 /* interpolate the trigger threshholds */
247 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
248 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
250 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
254 static void vorbis_encode_global_stereo(vorbis_info *vi,
255 const highlevel_encode_setup *const hi,
256 const adj_stereo *p){
257 float s=hi->stereo_point_setting;
260 codec_setup_info *ci=vi->codec_setup;
261 vorbis_info_psy_global *g=&ci->psy_g_param;
264 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
265 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
268 /* interpolate the kHz threshholds */
269 for(i=0;i<PACKETBLOBS;i++){
270 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
271 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
272 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
273 g->coupling_pkHz[i]=kHz;
275 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
276 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
277 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
281 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
282 for(i=0;i<PACKETBLOBS;i++){
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;
288 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
289 for(i=0;i<PACKETBLOBS;i++){
290 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
291 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
295 for(i=0;i<PACKETBLOBS;i++){
296 g->sliding_lowpass[0][i]=ci->blocksizes[0];
297 g->sliding_lowpass[1][i]=ci->blocksizes[1];
303 static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
305 const int *nn_partition,
306 const double *nn_thresh,
308 codec_setup_info *ci=vi->codec_setup;
309 vorbis_info_psy *p=ci->psy_param[block];
310 highlevel_encode_setup *hi=&ci->hi;
316 p=_ogg_calloc(1,sizeof(*p));
317 ci->psy_param[block]=p;
320 memcpy(p,&_psy_info_template,sizeof(*p));
321 p->blockflag=block>>1;
323 if(hi->noise_normalize_p){
325 p->normal_start=nn_start[is];
326 p->normal_partition=nn_partition[is];
327 p->normal_thresh=nn_thresh[is];
333 static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
336 const vp_adjblock *in){
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 void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
359 const compandblock *in,
363 codec_setup_info *ci=vi->codec_setup;
364 vorbis_info_psy *p=ci->psy_param[block];
366 ds=x[is]*(1.-ds)+x[is+1]*ds;
374 /* interpolate the compander settings */
375 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
376 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
380 static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
381 const int *suppress){
384 codec_setup_info *ci=vi->codec_setup;
385 vorbis_info_psy *p=ci->psy_param[block];
387 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
392 static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
395 const noiseguard *guard,
399 codec_setup_info *ci=vi->codec_setup;
400 vorbis_info_psy *p=ci->psy_param[block];
402 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
403 p->noisewindowlomin=guard[block].lo;
404 p->noisewindowhimin=guard[block].hi;
405 p->noisewindowfixed=guard[block].fixed;
407 for(j=0;j<P_NOISECURVES;j++)
408 for(i=0;i<P_BANDS;i++)
409 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
411 /* impulse blocks may take a user specified bias to boost the
412 nominal/high noise encoding depth */
413 for(j=0;j<P_NOISECURVES;j++){
414 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
415 for(i=0;i<P_BANDS;i++){
416 p->noiseoff[j][i]+=userbias;
417 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
424 static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
425 codec_setup_info *ci=vi->codec_setup;
426 vorbis_info_psy *p=ci->psy_param[block];
428 p->ath_adjatt=ci->hi.ath_floating_dB;
429 p->ath_maxatt=ci->hi.ath_absolute_dB;
434 static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){
436 for(i=0;i<ci->books;i++)
437 if(ci->book_param[i]==book)return(i);
442 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
443 const int *shortb,const int *longb){
445 codec_setup_info *ci=vi->codec_setup;
448 int blockshort=shortb[is];
449 int blocklong=longb[is];
450 ci->blocksizes[0]=blockshort;
451 ci->blocksizes[1]=blocklong;
455 static void vorbis_encode_residue_setup(vorbis_info *vi,
456 int number, int block,
457 const vorbis_residue_template *res){
459 codec_setup_info *ci=vi->codec_setup;
462 vorbis_info_residue0 *r=ci->residue_param[number]=
463 _ogg_malloc(sizeof(*r));
465 memcpy(r,res->res,sizeof(*r));
466 if(ci->residues<=number)ci->residues=number+1;
468 r->grouping=res->grouping;
469 ci->residue_type[number]=res->res_type;
471 /* fill in all the books */
476 for(i=0;i<r->partitions;i++)
478 if(res->books_base_managed->books[i][k])
479 r->secondstages[i]|=(1<<k);
481 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
482 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed;
484 for(i=0;i<r->partitions;i++){
486 if(res->books_base_managed->books[i][k]){
487 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
488 r->booklist[booklist++]=bookid;
489 ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k];
496 for(i=0;i<r->partitions;i++)
498 if(res->books_base->books[i][k])
499 r->secondstages[i]|=(1<<k);
501 r->groupbook=book_dup_or_new(ci,res->book_aux);
502 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux;
504 for(i=0;i<r->partitions;i++){
506 if(res->books_base->books[i][k]){
507 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
508 r->booklist[booklist++]=bookid;
509 ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k];
516 /* lowpass setup/pointlimit */
518 double freq=ci->hi.lowpass_kHz*1000.;
519 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
520 double nyq=vi->rate/2.;
521 long blocksize=ci->blocksizes[block]>>1;
523 /* lowpass needs to be set in the floor and the residue. */
524 if(freq>nyq)freq=nyq;
525 /* in the floor, the granularity can be very fine; it doesn't alter
526 the encoding structure, only the samples used to fit the floor
528 f->n=freq/nyq*blocksize;
530 /* this res may by limited by the maximum pointlimit of the mode,
531 not the lowpass. the floor is always lowpass limited. */
532 switch(res->limit_type){
533 case 1: /* point stereo limited */
535 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
537 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
538 if(freq>nyq)freq=nyq;
540 case 2: /* LFE channel; lowpass at ~ 250Hz */
548 /* in the residue, we're constrained, physically, by partition
549 boundaries. We still lowpass 'wherever', but we have to round up
550 here to next boundary, or the vorbis spec will round it *down* to
551 previous boundary in encode/decode */
552 if(ci->residue_type[number]==2){
553 /* residue 2 bundles together multiple channels; used by stereo
554 and surround. Count the channels in use */
555 /* Multiple maps/submaps can point to the same residue. In the case
556 of residue 2, they all better have the same number of
559 for(i=0;i<ci->maps&&ch==0;i++){
560 vorbis_info_mapping0 *mi=(vorbis_info_mapping0 *)ci->map_param[i];
561 for(j=0;j<mi->submaps && ch==0;j++)
562 if(mi->residuesubmap[j]==number) /* we found a submap referencing theis residue backend */
563 for(k=0;k<vi->channels;k++)
564 if(mi->chmuxlist[k]==j) /* this channel belongs to the submap */
568 r->end=(int)((freq/nyq*blocksize*ch)/r->grouping+.9)* /* round up only if we're well past */
570 /* the blocksize and grouping may disagree at the end */
571 if(r->end>blocksize*ch)r->end=blocksize*ch/r->grouping*r->grouping;
575 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
577 /* the blocksize and grouping may disagree at the end */
578 if(r->end>blocksize)r->end=blocksize/r->grouping*r->grouping;
582 if(r->end==0)r->end=r->grouping; /* LFE channel */
587 /* we assume two maps in this encoder */
588 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
589 const vorbis_mapping_template *maps){
591 codec_setup_info *ci=vi->codec_setup;
592 int i,j,is=s,modes=2;
593 const vorbis_info_mapping0 *map=maps[is].map;
594 const vorbis_info_mode *mode=_mode_template;
595 const vorbis_residue_template *res=maps[is].res;
597 if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
599 for(i=0;i<modes;i++){
601 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
602 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
604 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
605 if(i>=ci->modes)ci->modes=i+1;
608 memcpy(ci->map_param[i],map+i,sizeof(*map));
609 if(i>=ci->maps)ci->maps=i+1;
611 for(j=0;j<map[i].submaps;j++)
612 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
613 ,res+map[i].residuesubmap[j]);
617 static double setting_to_approx_bitrate(vorbis_info *vi){
618 codec_setup_info *ci=vi->codec_setup;
619 highlevel_encode_setup *hi=&ci->hi;
620 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
621 int is=hi->base_setting;
622 double ds=hi->base_setting-is;
624 const double *r=setup->rate_mapping;
629 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
632 static const void *get_setup_template(long ch,long srate,
633 double req,int q_or_bitrate,
634 double *base_setting){
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 */
656 *base_setting=j-.001;
660 float del=(req-low)/(high-low);
664 return(setup_list[i]);
673 /* encoders will need to use vorbis_info_init beforehand and call
674 vorbis_info clear when all done */
676 /* two interfaces; this, more detailed one, and later a convenience
679 /* the final setup call */
680 int vorbis_encode_setup_init(vorbis_info *vi){
681 int i,i0=0,singleblock=0;
682 codec_setup_info *ci=vi->codec_setup;
683 ve_setup_data_template *setup=NULL;
684 highlevel_encode_setup *hi=&ci->hi;
686 if(ci==NULL)return(OV_EINVAL);
687 if(vi->channels<1||vi->channels>255)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 void 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;
871 vi->channels=channels;
874 hi->impulse_block_p=1;
875 hi->noise_normalize_p=1;
878 ds=hi->base_setting-is;
880 hi->stereo_point_setting=hi->base_setting;
882 if(!hi->lowpass_altered)
884 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
886 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
887 setup->psy_ath_float[is+1]*ds;
888 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
889 setup->psy_ath_abs[is+1]*ds;
891 hi->amplitude_track_dBpersec=-6.;
892 hi->trigger_setting=hi->base_setting;
895 hi->block[i].tone_mask_setting=hi->base_setting;
896 hi->block[i].tone_peaklimit_setting=hi->base_setting;
897 hi->block[i].noise_bias_setting=hi->base_setting;
898 hi->block[i].noise_compand_setting=hi->base_setting;
902 int vorbis_encode_setup_vbr(vorbis_info *vi,
906 codec_setup_info *ci;
907 highlevel_encode_setup *hi;
908 if(rate<=0) return OV_EINVAL;
914 if(quality>=1.)quality=.9999;
917 hi->setup=get_setup_template(channels,rate,quality,0,&hi->base_setting);
918 if(!hi->setup)return OV_EIMPL;
920 vorbis_encode_setup_setting(vi,channels,rate);
927 int vorbis_encode_init_vbr(vorbis_info *vi,
931 float base_quality /* 0. to 1. */
935 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
938 vorbis_info_clear(vi);
941 ret=vorbis_encode_setup_init(vi);
943 vorbis_info_clear(vi);
947 int vorbis_encode_setup_managed(vorbis_info *vi,
952 long nominal_bitrate,
955 codec_setup_info *ci;
956 highlevel_encode_setup *hi;
958 if(rate<=0) return OV_EINVAL;
962 tnominal=nominal_bitrate;
964 if(nominal_bitrate<=0.){
967 nominal_bitrate=(max_bitrate+min_bitrate)*.5;
969 nominal_bitrate=max_bitrate*.875;
972 nominal_bitrate=min_bitrate;
979 hi->req=nominal_bitrate;
980 hi->setup=get_setup_template(channels,rate,nominal_bitrate,1,&hi->base_setting);
981 if(!hi->setup)return OV_EIMPL;
983 vorbis_encode_setup_setting(vi,channels,rate);
985 /* initialize management with sane defaults */
988 hi->bitrate_min=min_bitrate;
989 hi->bitrate_max=max_bitrate;
990 hi->bitrate_av=tnominal;
991 hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
992 hi->bitrate_reservoir=nominal_bitrate*2;
993 hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */
999 int vorbis_encode_init(vorbis_info *vi,
1004 long nominal_bitrate,
1007 int ret=vorbis_encode_setup_managed(vi,channels,rate,
1012 vorbis_info_clear(vi);
1016 ret=vorbis_encode_setup_init(vi);
1018 vorbis_info_clear(vi);
1022 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
1024 codec_setup_info *ci=vi->codec_setup;
1025 highlevel_encode_setup *hi=&ci->hi;
1026 int setp=(number&0xf); /* a read request has a low nibble of 0 */
1028 if(setp && hi->set_in_stone)return(OV_EINVAL);
1032 /* now deprecated *****************/
1033 case OV_ECTL_RATEMANAGE_GET:
1036 struct ovectl_ratemanage_arg *ai=
1037 (struct ovectl_ratemanage_arg *)arg;
1039 ai->management_active=hi->managed;
1040 ai->bitrate_hard_window=ai->bitrate_av_window=
1041 (double)hi->bitrate_reservoir/vi->rate;
1042 ai->bitrate_av_window_center=1.;
1043 ai->bitrate_hard_min=hi->bitrate_min;
1044 ai->bitrate_hard_max=hi->bitrate_max;
1045 ai->bitrate_av_lo=hi->bitrate_av;
1046 ai->bitrate_av_hi=hi->bitrate_av;
1051 /* now deprecated *****************/
1052 case OV_ECTL_RATEMANAGE_SET:
1054 struct ovectl_ratemanage_arg *ai=
1055 (struct ovectl_ratemanage_arg *)arg;
1059 hi->managed=ai->management_active;
1060 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
1061 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
1066 /* now deprecated *****************/
1067 case OV_ECTL_RATEMANAGE_AVG:
1069 struct ovectl_ratemanage_arg *ai=
1070 (struct ovectl_ratemanage_arg *)arg;
1074 hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
1078 /* now deprecated *****************/
1079 case OV_ECTL_RATEMANAGE_HARD:
1081 struct ovectl_ratemanage_arg *ai=
1082 (struct ovectl_ratemanage_arg *)arg;
1087 hi->bitrate_min=ai->bitrate_hard_min;
1088 hi->bitrate_max=ai->bitrate_hard_max;
1089 hi->bitrate_reservoir=ai->bitrate_hard_window*
1090 (hi->bitrate_max+hi->bitrate_min)*.5;
1092 if(hi->bitrate_reservoir<128.)
1093 hi->bitrate_reservoir=128.;
1097 /* replacement ratemanage interface */
1098 case OV_ECTL_RATEMANAGE2_GET:
1100 struct ovectl_ratemanage2_arg *ai=
1101 (struct ovectl_ratemanage2_arg *)arg;
1102 if(ai==NULL)return OV_EINVAL;
1104 ai->management_active=hi->managed;
1105 ai->bitrate_limit_min_kbps=hi->bitrate_min/1000;
1106 ai->bitrate_limit_max_kbps=hi->bitrate_max/1000;
1107 ai->bitrate_average_kbps=hi->bitrate_av/1000;
1108 ai->bitrate_average_damping=hi->bitrate_av_damp;
1109 ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
1110 ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
1113 case OV_ECTL_RATEMANAGE2_SET:
1115 struct ovectl_ratemanage2_arg *ai=
1116 (struct ovectl_ratemanage2_arg *)arg;
1120 /* sanity check; only catch invariant violations */
1121 if(ai->bitrate_limit_min_kbps>0 &&
1122 ai->bitrate_average_kbps>0 &&
1123 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
1126 if(ai->bitrate_limit_max_kbps>0 &&
1127 ai->bitrate_average_kbps>0 &&
1128 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
1131 if(ai->bitrate_limit_min_kbps>0 &&
1132 ai->bitrate_limit_max_kbps>0 &&
1133 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
1136 if(ai->bitrate_average_damping <= 0.)
1139 if(ai->bitrate_limit_reservoir_bits < 0)
1142 if(ai->bitrate_limit_reservoir_bias < 0.)
1145 if(ai->bitrate_limit_reservoir_bias > 1.)
1148 hi->managed=ai->management_active;
1149 hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000;
1150 hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000;
1151 hi->bitrate_av=ai->bitrate_average_kbps * 1000;
1152 hi->bitrate_av_damp=ai->bitrate_average_damping;
1153 hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
1154 hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
1159 case OV_ECTL_LOWPASS_GET:
1161 double *farg=(double *)arg;
1162 *farg=hi->lowpass_kHz;
1165 case OV_ECTL_LOWPASS_SET:
1167 double *farg=(double *)arg;
1168 hi->lowpass_kHz=*farg;
1170 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
1171 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
1172 hi->lowpass_altered=1;
1175 case OV_ECTL_IBLOCK_GET:
1177 double *farg=(double *)arg;
1178 *farg=hi->impulse_noisetune;
1181 case OV_ECTL_IBLOCK_SET:
1183 double *farg=(double *)arg;
1184 hi->impulse_noisetune=*farg;
1186 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
1187 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;
1190 case OV_ECTL_COUPLING_GET:
1192 int *iarg=(int *)arg;
1193 *iarg=hi->coupling_p;
1196 case OV_ECTL_COUPLING_SET:
1198 const void *new_template;
1200 int *iarg=(int *)arg;
1201 hi->coupling_p=((*iarg)!=0);
1203 /* Fetching a new template can alter the base_setting, which
1204 many other parameters are based on. Right now, the only
1205 parameter drawn from the base_setting that can be altered
1206 by an encctl is the lowpass, so that is explictly flagged
1207 to not be overwritten when we fetch a new template and
1208 recompute the dependant settings */
1209 new_template = get_setup_template(hi->coupling_p?vi->channels:-1,
1214 if(!new_template)return OV_EIMPL;
1215 hi->setup=new_template;
1216 hi->base_setting=new_base;
1217 vorbis_encode_setup_setting(vi,vi->channels,vi->rate);