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.48 2003/12/30 11:02:22 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 /* 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_stereo_low,
166 &ve_setup_44_uncoupled,
167 &ve_setup_44_uncoupled_low,
170 &ve_setup_32_stereo_low,
171 &ve_setup_32_uncoupled,
172 &ve_setup_32_uncoupled_low,
175 &ve_setup_22_uncoupled,
177 &ve_setup_16_uncoupled,
180 &ve_setup_11_uncoupled,
182 &ve_setup_8_uncoupled,
185 &ve_setup_X_uncoupled,
186 &ve_setup_X_stereo_low,
187 &ve_setup_X_uncoupled_low,
189 &ve_setup_XX_uncoupled,
193 static int vorbis_encode_toplevel_setup(vorbis_info *vi,int ch,long rate){
194 if(vi && vi->codec_setup){
205 static void vorbis_encode_floor_setup(vorbis_info *vi,double s,int block,
206 static_codebook ***books,
207 vorbis_info_floor1 *in,
210 vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
211 codec_setup_info *ci=vi->codec_setup;
213 memcpy(f,in+x[is],sizeof(*f));
214 /* fill in the lowpass field, even if it's temporary */
215 f->n=ci->blocksizes[block]>>1;
219 int partitions=f->partitions;
222 for(i=0;i<partitions;i++)
223 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
224 for(i=0;i<=maxclass;i++){
225 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
226 f->class_book[i]+=ci->books;
227 for(k=0;k<(1<<f->class_subs[i]);k++){
228 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
229 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
233 for(i=0;i<=maxbook;i++)
234 ci->book_param[ci->books++]=books[x[is]][i];
237 /* for now, we're only using floor 1 */
238 ci->floor_type[ci->floors]=1;
239 ci->floor_param[ci->floors]=f;
245 static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
246 vorbis_info_psy_global *in,
250 codec_setup_info *ci=vi->codec_setup;
251 vorbis_info_psy_global *g=&ci->psy_g_param;
253 memcpy(g,in+(int)x[is],sizeof(*g));
255 ds=x[is]*(1.-ds)+x[is+1]*ds;
263 /* interpolate the trigger threshholds */
265 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
266 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
268 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
272 static void vorbis_encode_global_stereo(vorbis_info *vi,
273 highlevel_encode_setup *hi,
275 float s=hi->stereo_point_setting;
278 codec_setup_info *ci=vi->codec_setup;
279 vorbis_info_psy_global *g=&ci->psy_g_param;
282 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
283 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
286 /* interpolate the kHz threshholds */
287 for(i=0;i<PACKETBLOBS;i++){
288 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
289 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
290 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
291 g->coupling_pkHz[i]=kHz;
293 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
294 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
295 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
299 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
300 for(i=0;i<PACKETBLOBS;i++){
301 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
302 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
303 g->coupling_pkHz[i]=kHz;
306 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
307 for(i=0;i<PACKETBLOBS;i++){
308 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
309 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
313 for(i=0;i<PACKETBLOBS;i++){
314 g->sliding_lowpass[0][i]=ci->blocksizes[0];
315 g->sliding_lowpass[1][i]=ci->blocksizes[1];
321 static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
326 codec_setup_info *ci=vi->codec_setup;
327 vorbis_info_psy *p=ci->psy_param[block];
328 highlevel_encode_setup *hi=&ci->hi;
334 p=_ogg_calloc(1,sizeof(*p));
335 ci->psy_param[block]=p;
338 memcpy(p,&_psy_info_template,sizeof(*p));
339 p->blockflag=block>>1;
341 if(hi->noise_normalize_p){
342 p->normal_channel_p=1;
344 p->normal_start=nn_start[is];
345 p->normal_partition=nn_partition[is];
346 p->normal_thresh=nn_thresh[is];
352 static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
358 codec_setup_info *ci=vi->codec_setup;
359 vorbis_info_psy *p=ci->psy_param[block];
361 /* 0 and 2 are only used by bitmanagement, but there's no harm to always
362 filling the values in here */
363 p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
364 p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
365 p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
366 p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
367 p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
369 p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
371 for(i=0;i<P_BANDS;i++)
372 p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
377 static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
378 compandblock *in, double *x){
381 codec_setup_info *ci=vi->codec_setup;
382 vorbis_info_psy *p=ci->psy_param[block];
384 ds=x[is]*(1.-ds)+x[is+1]*ds;
392 /* interpolate the compander settings */
393 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
394 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
398 static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
402 codec_setup_info *ci=vi->codec_setup;
403 vorbis_info_psy *p=ci->psy_param[block];
405 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
410 static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
417 codec_setup_info *ci=vi->codec_setup;
418 vorbis_info_psy *p=ci->psy_param[block];
420 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
421 p->noisewindowlomin=guard[block].lo;
422 p->noisewindowhimin=guard[block].hi;
423 p->noisewindowfixed=guard[block].fixed;
425 for(j=0;j<P_NOISECURVES;j++)
426 for(i=0;i<P_BANDS;i++)
427 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
429 /* impulse blocks may take a user specified bias to boost the
430 nominal/high noise encoding depth */
431 for(j=0;j<P_NOISECURVES;j++){
432 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
433 for(i=0;i<P_BANDS;i++){
434 p->noiseoff[j][i]+=userbias;
435 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
442 static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
443 codec_setup_info *ci=vi->codec_setup;
444 vorbis_info_psy *p=ci->psy_param[block];
446 p->ath_adjatt=ci->hi.ath_floating_dB;
447 p->ath_maxatt=ci->hi.ath_absolute_dB;
452 static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
454 for(i=0;i<ci->books;i++)
455 if(ci->book_param[i]==book)return(i);
460 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
461 int *shortb,int *longb){
463 codec_setup_info *ci=vi->codec_setup;
466 int blockshort=shortb[is];
467 int blocklong=longb[is];
468 ci->blocksizes[0]=blockshort;
469 ci->blocksizes[1]=blocklong;
473 static void vorbis_encode_residue_setup(vorbis_info *vi,
474 int number, int block,
475 vorbis_residue_template *res){
477 codec_setup_info *ci=vi->codec_setup;
480 vorbis_info_residue0 *r=ci->residue_param[number]=
481 _ogg_malloc(sizeof(*r));
483 memcpy(r,res->res,sizeof(*r));
484 if(ci->residues<=number)ci->residues=number+1;
486 switch(ci->blocksizes[block]){
487 case 64:case 128:case 256:
494 ci->residue_type[number]=res->res_type;
496 /* to be adjusted by lowpass/pointlimit later */
497 n=r->end=ci->blocksizes[block]>>1;
499 n=r->end*=vi->channels;
501 /* fill in all the books */
506 for(i=0;i<r->partitions;i++)
508 if(res->books_base_managed->books[i][k])
509 r->secondstages[i]|=(1<<k);
511 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
512 ci->book_param[r->groupbook]=res->book_aux_managed;
514 for(i=0;i<r->partitions;i++){
516 if(res->books_base_managed->books[i][k]){
517 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
518 r->booklist[booklist++]=bookid;
519 ci->book_param[bookid]=res->books_base_managed->books[i][k];
526 for(i=0;i<r->partitions;i++)
528 if(res->books_base->books[i][k])
529 r->secondstages[i]|=(1<<k);
531 r->groupbook=book_dup_or_new(ci,res->book_aux);
532 ci->book_param[r->groupbook]=res->book_aux;
534 for(i=0;i<r->partitions;i++){
536 if(res->books_base->books[i][k]){
537 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
538 r->booklist[booklist++]=bookid;
539 ci->book_param[bookid]=res->books_base->books[i][k];
546 /* lowpass setup/pointlimit */
548 double freq=ci->hi.lowpass_kHz*1000.;
549 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
550 double nyq=vi->rate/2.;
551 long blocksize=ci->blocksizes[block]>>1;
553 /* lowpass needs to be set in the floor and the residue. */
554 if(freq>nyq)freq=nyq;
555 /* in the floor, the granularity can be very fine; it doesn't alter
556 the encoding structure, only the samples used to fit the floor
558 f->n=freq/nyq*blocksize;
560 /* this res may by limited by the maximum pointlimit of the mode,
561 not the lowpass. the floor is always lowpass limited. */
564 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
566 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
567 if(freq>nyq)freq=nyq;
570 /* in the residue, we're constrained, physically, by partition
571 boundaries. We still lowpass 'wherever', but we have to round up
572 here to next boundary, or the vorbis spec will round it *down* to
573 previous boundary in encode/decode */
574 if(ci->residue_type[block]==2)
575 r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
578 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
583 /* we assume two maps in this encoder */
584 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
585 vorbis_mapping_template *maps){
587 codec_setup_info *ci=vi->codec_setup;
588 int i,j,is=s,modes=2;
589 vorbis_info_mapping0 *map=maps[is].map;
590 vorbis_info_mode *mode=_mode_template;
591 vorbis_residue_template *res=maps[is].res;
593 if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
595 for(i=0;i<modes;i++){
597 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
598 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
600 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
601 if(i>=ci->modes)ci->modes=i+1;
604 memcpy(ci->map_param[i],map+i,sizeof(*map));
605 if(i>=ci->maps)ci->maps=i+1;
607 for(j=0;j<map[i].submaps;j++)
608 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
609 ,res+map[i].residuesubmap[j]);
613 static double setting_to_approx_bitrate(vorbis_info *vi){
614 codec_setup_info *ci=vi->codec_setup;
615 highlevel_encode_setup *hi=&ci->hi;
616 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
617 int is=hi->base_setting;
618 double ds=hi->base_setting-is;
620 double *r=setup->rate_mapping;
625 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
628 static void get_setup_template(vorbis_info *vi,
630 double req,int q_or_bitrate){
632 codec_setup_info *ci=vi->codec_setup;
633 highlevel_encode_setup *hi=&ci->hi;
634 if(q_or_bitrate)req/=ch;
636 while(setup_list[i]){
637 if(setup_list[i]->coupling_restriction==-1 ||
638 setup_list[i]->coupling_restriction==ch){
639 if(srate>=setup_list[i]->samplerate_min_restriction &&
640 srate<=setup_list[i]->samplerate_max_restriction){
641 int mappings=setup_list[i]->mappings;
642 double *map=(q_or_bitrate?
643 setup_list[i]->rate_mapping:
644 setup_list[i]->quality_mapping);
646 /* the template matches. Does the requested quality mode
647 fall within this template's modes? */
648 if(req<map[0]){++i;continue;}
649 if(req>map[setup_list[i]->mappings]){++i;continue;}
650 for(j=0;j<mappings;j++)
651 if(req>=map[j] && req<map[j+1])break;
652 /* an all-points match */
653 hi->setup=setup_list[i];
655 hi->base_setting=j-.001;
659 float del=(req-low)/(high-low);
660 hi->base_setting=j+del;
671 /* encoders will need to use vorbis_info_init beforehand and call
672 vorbis_info clear when all done */
674 /* two interfaces; this, more detailed one, and later a convenience
677 /* the final setup call */
678 int vorbis_encode_setup_init(vorbis_info *vi){
679 int i0=0,singleblock=0;
680 codec_setup_info *ci=vi->codec_setup;
681 ve_setup_data_template *setup=NULL;
682 highlevel_encode_setup *hi=&ci->hi;
684 if(ci==NULL)return(OV_EINVAL);
685 if(!hi->impulse_block_p)i0=1;
687 /* too low/high an ATH floater is nonsensical, but doesn't break anything */
688 if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
689 if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
691 /* again, bound this to avoid the app shooting itself int he foot
693 if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
694 if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
696 /* get the appropriate setup template; matches the fetch in previous
698 setup=(ve_setup_data_template *)hi->setup;
699 if(setup==NULL)return(OV_EINVAL);
702 /* choose block sizes from configured sizes as well as paying
703 attention to long_block_p and short_block_p. If the configured
704 short and long blocks are the same length, we set long_block_p
705 and unset short_block_p */
706 vorbis_encode_blocksize_setup(vi,hi->base_setting,
707 setup->blocksize_short,
708 setup->blocksize_long);
709 if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
711 /* floor setup; choose proper floor params. Allocated on the floor
712 stack in order; if we alloc only long floor, it's 0 */
713 vorbis_encode_floor_setup(vi,hi->short_setting,0,
716 setup->floor_short_mapping);
718 vorbis_encode_floor_setup(vi,hi->long_setting,1,
721 setup->floor_long_mapping);
723 /* setup of [mostly] short block detection and stereo*/
724 vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
725 setup->global_params,
726 setup->global_mapping);
727 vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
729 /* basic psych setup and noise normalization */
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,
735 vorbis_encode_psyset_setup(vi,hi->short_setting,
736 setup->psy_noise_normal_start[0],
737 setup->psy_noise_normal_partition[0],
738 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,
746 vorbis_encode_psyset_setup(vi,hi->long_setting,
747 setup->psy_noise_normal_start[1],
748 setup->psy_noise_normal_partition[1],
749 setup->psy_noise_normal_thresh,
753 /* tone masking setup */
754 vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
755 setup->psy_tone_masteratt,
757 setup->psy_tone_adj_impulse);
758 vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
759 setup->psy_tone_masteratt,
761 setup->psy_tone_adj_other);
763 vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
764 setup->psy_tone_masteratt,
766 setup->psy_tone_adj_other);
767 vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
768 setup->psy_tone_masteratt,
770 setup->psy_tone_adj_long);
773 /* noise companding setup */
774 vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
775 setup->psy_noise_compand,
776 setup->psy_noise_compand_short_mapping);
777 vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
778 setup->psy_noise_compand,
779 setup->psy_noise_compand_short_mapping);
781 vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
782 setup->psy_noise_compand,
783 setup->psy_noise_compand_long_mapping);
784 vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
785 setup->psy_noise_compand,
786 setup->psy_noise_compand_long_mapping);
789 /* peak guarding setup */
790 vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
791 setup->psy_tone_dBsuppress);
792 vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
793 setup->psy_tone_dBsuppress);
795 vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
796 setup->psy_tone_dBsuppress);
797 vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
798 setup->psy_tone_dBsuppress);
801 /* noise bias setup */
802 vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
803 setup->psy_noise_dBsuppress,
804 setup->psy_noise_bias_impulse,
805 setup->psy_noiseguards,
806 (i0==0?hi->impulse_noisetune:0.));
807 vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
808 setup->psy_noise_dBsuppress,
809 setup->psy_noise_bias_padding,
810 setup->psy_noiseguards,0.);
812 vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
813 setup->psy_noise_dBsuppress,
814 setup->psy_noise_bias_trans,
815 setup->psy_noiseguards,0.);
816 vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
817 setup->psy_noise_dBsuppress,
818 setup->psy_noise_bias_long,
819 setup->psy_noiseguards,0.);
822 vorbis_encode_ath_setup(vi,0);
823 vorbis_encode_ath_setup(vi,1);
825 vorbis_encode_ath_setup(vi,2);
826 vorbis_encode_ath_setup(vi,3);
829 vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
831 /* set bitrate readonlies and management */
832 vi->bitrate_nominal=setting_to_approx_bitrate(vi);
833 vi->bitrate_lower=hi->bitrate_min;
834 vi->bitrate_upper=hi->bitrate_max;
835 vi->bitrate_window=(double)hi->bitrate_reservoir/vi->rate;
838 ci->bi.avg_rate=hi->bitrate_av;
839 ci->bi.min_rate=hi->bitrate_min;
840 ci->bi.max_rate=hi->bitrate_max;
842 ci->bi.reservoir_bits=hi->bitrate_reservoir;
843 ci->bi.reservoir_bias=
844 hi->bitrate_reservoir_bias;
846 ci->bi.slew_damp=hi->bitrate_av_damp;
854 static int vorbis_encode_setup_setting(vorbis_info *vi,
858 codec_setup_info *ci=vi->codec_setup;
859 highlevel_encode_setup *hi=&ci->hi;
860 ve_setup_data_template *setup=hi->setup;
863 ret=vorbis_encode_toplevel_setup(vi,channels,rate);
867 ds=hi->base_setting-is;
869 hi->short_setting=hi->base_setting;
870 hi->long_setting=hi->base_setting;
874 hi->impulse_block_p=1;
875 hi->noise_normalize_p=1;
877 hi->stereo_point_setting=hi->base_setting;
879 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
881 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
882 setup->psy_ath_float[is+1]*ds;
883 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
884 setup->psy_ath_abs[is+1]*ds;
886 hi->amplitude_track_dBpersec=-6.;
887 hi->trigger_setting=hi->base_setting;
890 hi->block[i].tone_mask_setting=hi->base_setting;
891 hi->block[i].tone_peaklimit_setting=hi->base_setting;
892 hi->block[i].noise_bias_setting=hi->base_setting;
893 hi->block[i].noise_compand_setting=hi->base_setting;
899 int vorbis_encode_setup_vbr(vorbis_info *vi,
903 codec_setup_info *ci=vi->codec_setup;
904 highlevel_encode_setup *hi=&ci->hi;
907 if(quality>=1.)quality=.9999;
909 get_setup_template(vi,channels,rate,quality,0);
910 if(!hi->setup)return OV_EIMPL;
912 return vorbis_encode_setup_setting(vi,channels,rate);
915 int vorbis_encode_init_vbr(vorbis_info *vi,
919 float base_quality /* 0. to 1. */
923 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
926 vorbis_info_clear(vi);
929 ret=vorbis_encode_setup_init(vi);
931 vorbis_info_clear(vi);
935 int vorbis_encode_setup_managed(vorbis_info *vi,
940 long nominal_bitrate,
943 codec_setup_info *ci=vi->codec_setup;
944 highlevel_encode_setup *hi=&ci->hi;
945 double tnominal=nominal_bitrate;
948 if(nominal_bitrate<=0.){
950 nominal_bitrate=max_bitrate*.875;
953 nominal_bitrate=min_bitrate;
960 get_setup_template(vi,channels,rate,nominal_bitrate,1);
961 if(!hi->setup)return OV_EIMPL;
963 ret=vorbis_encode_setup_setting(vi,channels,rate);
965 vorbis_info_clear(vi);
969 /* initialize management with sane defaults */
971 hi->bitrate_min=min_bitrate;
972 hi->bitrate_max=max_bitrate;
973 hi->bitrate_av=tnominal;
974 hi->bitrate_av_damp=.75f; /* full range in no less than 3/4 second */
975 hi->bitrate_reservoir=tnominal;
976 hi->bitrate_reservoir_bias=.2; /* bias toward hoarding bits */
982 int vorbis_encode_init(vorbis_info *vi,
987 long nominal_bitrate,
990 int ret=vorbis_encode_setup_managed(vi,channels,rate,
995 vorbis_info_clear(vi);
999 ret=vorbis_encode_setup_init(vi);
1001 vorbis_info_clear(vi);
1005 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
1007 codec_setup_info *ci=vi->codec_setup;
1008 highlevel_encode_setup *hi=&ci->hi;
1009 int setp=(number&0xf); /* a read request has a low nibble of 0 */
1011 if(setp && hi->set_in_stone)return(OV_EINVAL);
1015 /* now deprecated *****************/
1016 case OV_ECTL_RATEMANAGE_GET:
1019 struct ovectl_ratemanage_arg *ai=
1020 (struct ovectl_ratemanage_arg *)arg;
1022 ai->management_active=hi->managed;
1023 ai->bitrate_hard_window=ai->bitrate_av_window=
1024 (double)hi->bitrate_reservoir/vi->rate;
1025 ai->bitrate_av_window_center=1.;
1026 ai->bitrate_hard_min=hi->bitrate_min;
1027 ai->bitrate_hard_max=hi->bitrate_max;
1028 ai->bitrate_av_lo=hi->bitrate_av;
1029 ai->bitrate_av_hi=hi->bitrate_av;
1034 /* now deprecated *****************/
1035 case OV_ECTL_RATEMANAGE_SET:
1037 struct ovectl_ratemanage_arg *ai=
1038 (struct ovectl_ratemanage_arg *)arg;
1042 hi->managed=ai->management_active;
1043 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
1044 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
1049 /* now deprecated *****************/
1050 case OV_ECTL_RATEMANAGE_AVG:
1052 struct ovectl_ratemanage_arg *ai=
1053 (struct ovectl_ratemanage_arg *)arg;
1057 hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
1061 /* now deprecated *****************/
1062 case OV_ECTL_RATEMANAGE_HARD:
1064 struct ovectl_ratemanage_arg *ai=
1065 (struct ovectl_ratemanage_arg *)arg;
1070 hi->bitrate_min=ai->bitrate_hard_min;
1071 hi->bitrate_max=ai->bitrate_hard_max;
1072 hi->bitrate_reservoir=ai->bitrate_hard_window*
1073 (hi->bitrate_max+hi->bitrate_min)*.5;
1075 if(hi->bitrate_reservoir<128.)
1076 hi->bitrate_reservoir=128.;
1080 /* replacement ratemanage interface */
1081 case OV_ECTL_RATEMANAGE2_GET:
1083 struct ovectl_ratemanage2_arg *ai=
1084 (struct ovectl_ratemanage2_arg *)arg;
1085 if(ai==NULL)return OV_EINVAL;
1087 ai->management_active=hi->managed;
1088 ai->bitrate_limit_min_kbps=hi->bitrate_min;
1089 ai->bitrate_limit_max_kbps=hi->bitrate_max;
1090 ai->bitrate_average_kbps=hi->bitrate_av;
1091 ai->bitrate_average_damping=hi->bitrate_av_damp;
1092 ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
1093 ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
1096 case OV_ECTL_RATEMANAGE2_SET:
1098 struct ovectl_ratemanage2_arg *ai=
1099 (struct ovectl_ratemanage2_arg *)arg;
1103 /* sanity check; only catch invariant violations */
1104 if(ai->bitrate_limit_min_kbps>0 &&
1105 ai->bitrate_average_kbps>0 &&
1106 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
1109 if(ai->bitrate_limit_max_kbps>0 &&
1110 ai->bitrate_average_kbps>0 &&
1111 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
1114 if(ai->bitrate_limit_min_kbps>0 &&
1115 ai->bitrate_limit_max_kbps>0 &&
1116 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
1119 if(ai->bitrate_average_damping <= 0.)
1122 if(ai->bitrate_limit_reservoir_bits < 0)
1125 if(ai->bitrate_limit_reservoir_bias < 0.)
1128 if(ai->bitrate_limit_reservoir_bias > 1.)
1131 hi->managed=ai->management_active;
1132 hi->bitrate_min=ai->bitrate_limit_min_kbps;
1133 hi->bitrate_max=ai->bitrate_limit_max_kbps;
1134 hi->bitrate_av=ai->bitrate_average_kbps;
1135 hi->bitrate_av_damp=ai->bitrate_average_damping;
1136 hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
1137 hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
1142 case OV_ECTL_LOWPASS_GET:
1144 double *farg=(double *)arg;
1145 *farg=hi->lowpass_kHz;
1148 case OV_ECTL_LOWPASS_SET:
1150 double *farg=(double *)arg;
1151 hi->lowpass_kHz=*farg;
1153 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
1154 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
1157 case OV_ECTL_IBLOCK_GET:
1159 double *farg=(double *)arg;
1160 *farg=hi->impulse_noisetune;
1163 case OV_ECTL_IBLOCK_SET:
1165 double *farg=(double *)arg;
1166 hi->impulse_noisetune=*farg;
1168 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
1169 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;