12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- /********************************************************************
- * *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
- * *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
- * by the XIPHOPHORUS Company http://www.xiph.org/ *
- * *
- ********************************************************************
- function: simple programmatic interface for encoder mode setup
- last mod: $Id: vorbisenc.c,v 1.36 2002/01/22 08:06:07 xiphmont Exp $
- ********************************************************************/
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <stdarg.h>
- #include "vorbis/codec.h"
- #include "vorbis/vorbisenc.h"
- #include "codec_internal.h"
- #include "registry-api.h"
- #include "os.h"
- #include "misc.h"
- /* careful with this; it's using static array sizing to make managing
- all the modes a little less annoying. If we use a residue backend
- with > 10 partition types, or a different division of iteration,
- this needs to be updated. */
- typedef struct {
- vorbis_info_residue0 *res[2];
- static_codebook *book_aux[2];
- static_codebook *books_base[5][10][3];
- static_codebook *books_stereo_backfill[5][10];
- static_codebook *books_residue_backfill[5][10][2];
- } vorbis_residue_template;
- static double stereo_threshholds[]={0.0, 2.5, 4.5, 8.5, 16.5};
- typedef struct vp_adjblock{
- int block[P_BANDS][P_LEVELS];
- } vp_adjblock;
- #include "modes/residue_44.h"
- #include "modes/psych_44.h"
- #include "modes/floor_44.h"
- /* a few static coder conventions */
- static vorbis_info_time0 _time_dummy={0};
- static vorbis_info_mode _mode_set_short={0,0,0,0};
- static vorbis_info_mode _mode_set_long={1,0,0,1};
- /* mapping conventions:
- only one submap (this would change for efficient 5.1 support for example)*/
- /* Four psychoacoustic profiles are used, one for each blocktype */
- static vorbis_info_mapping0 _mapping_set_short={
- 1, {0,0}, {0}, {0}, {0}, {0,1}, 0,{0},{0}};
- static vorbis_info_mapping0 _mapping_set_long={
- 1, {0,0}, {0}, {1}, {1}, {2,3}, 0,{0},{0}};
- static int vorbis_encode_toplevel_setup(vorbis_info *vi,int small,int large,int ch,long rate){
- if(vi && vi->codec_setup){
- codec_setup_info *ci=vi->codec_setup;
- vi->version=0;
- vi->channels=ch;
- vi->rate=rate;
-
- ci->blocksizes[0]=small;
- ci->blocksizes[1]=large;
- /* time mapping hooks are unused in vorbis I */
- ci->times=1;
- ci->time_type[0]=0;
- ci->time_param[0]=_ogg_calloc(1,sizeof(_time_dummy));
- memcpy(ci->time_param[0],&_time_dummy,sizeof(_time_dummy));
- /* by convention, two modes: one for short, one for long blocks.
- short block mode uses mapping sero, long block uses mapping 1 */
- ci->modes=2;
- ci->mode_param[0]=_ogg_calloc(1,sizeof(_mode_set_short));
- memcpy(ci->mode_param[0],&_mode_set_short,sizeof(_mode_set_short));
- ci->mode_param[1]=_ogg_calloc(1,sizeof(_mode_set_long));
- memcpy(ci->mode_param[1],&_mode_set_long,sizeof(_mode_set_long));
- /* by convention two mappings, both mapping type zero (polyphonic
- PCM), first for short, second for long blocks */
- ci->maps=2;
- ci->map_type[0]=0;
- ci->map_param[0]=_ogg_calloc(1,sizeof(_mapping_set_short));
- memcpy(ci->map_param[0],&_mapping_set_short,sizeof(_mapping_set_short));
- ci->map_type[1]=0;
- ci->map_param[1]=_ogg_calloc(1,sizeof(_mapping_set_long));
- memcpy(ci->map_param[1],&_mapping_set_long,sizeof(_mapping_set_long));
- return(0);
- }
- return(OV_EINVAL);
- }
- static int vorbis_encode_floor_setup(vorbis_info *vi,double q,int block,
- static_codebook ***books,
- vorbis_info_floor1 *in,
- ...){
- int x[11],i,k,iq=rint(q*10);
- vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
- codec_setup_info *ci=vi->codec_setup;
- va_list ap;
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,int);
- va_end(ap);
- memcpy(f,in+x[iq],sizeof(*f));
- /* fill in the lowpass field, even if it's temporary */
- f->n=ci->blocksizes[block]>>1;
- /* books */
- {
- int partitions=f->partitions;
- int maxclass=-1;
- int maxbook=-1;
- for(i=0;i<partitions;i++)
- if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
- for(i=0;i<=maxclass;i++){
- if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
- f->class_book[i]+=ci->books;
- for(k=0;k<(1<<f->class_subs[i]);k++){
- if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
- if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
- }
- }
- for(i=0;i<=maxbook;i++)
- ci->book_param[ci->books++]=books[x[iq]][i];
- }
- /* for now, we're only using floor 1 */
- ci->floor_type[ci->floors]=1;
- ci->floor_param[ci->floors]=f;
- ci->floors++;
- return(0);
- }
- static int vorbis_encode_global_psych_setup(vorbis_info *vi,double q,
- vorbis_info_psy_global *in, ...){
- int i,iq=q*10;
- double x[11],dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy_global *g=&ci->psy_g_param;
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- memcpy(g,in+(int)x[iq],sizeof(*g));
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
- }
- /* interpolate the trigger threshholds */
- for(i=0;i<4;i++){
- g->preecho_thresh[i]=in[iq].preecho_thresh[i]*(1.-dq)+in[iq+1].preecho_thresh[i]*dq;
- g->postecho_thresh[i]=in[iq].postecho_thresh[i]*(1.-dq)+in[iq+1].postecho_thresh[i]*dq;
- }
- g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
- return(0);
- }
- static int vorbis_encode_psyset_setup(vorbis_info *vi,int block){
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- if(block>=ci->psys)
- ci->psys=block+1;
- if(!p){
- p=_ogg_calloc(1,sizeof(*p));
- ci->psy_param[block]=p;
- }
- memcpy(p,&_psy_info_template,sizeof(*p));
- return 0;
- }
- static int vorbis_encode_tonemask_setup(vorbis_info *vi,double q,int block,
- double *att,
- double *max,
- int *peaklimit_bands,
- vp_adjblock *in){
- int i,j,iq;
- double dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- iq=q*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- p->tone_masteratt=att[iq]*(1.-dq)+att[iq+1]*dq;
- p->max_curve_dB=max[iq]*(1.-dq)+max[iq+1]*dq;
- p->curvelimitp=peaklimit_bands[iq];
- iq=q*5.;
- if(iq==5){
- iq=5;
- dq=1.;
- }else{
- dq=q*5.-iq;
- }
-
- for(i=0;i<P_BANDS;i++)
- for(j=0;j<P_LEVELS;j++)
- p->toneatt.block[i][j]=(j<4?4:j)*-10.+
- in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
- return(0);
- }
- static int vorbis_encode_compand_setup(vorbis_info *vi,double q,int block,
- float in[][NOISE_COMPAND_LEVELS], ...){
- int i,iq=q*10;
- double x[11],dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
- }
- /* interpolate the compander settings */
- for(i=0;i<NOISE_COMPAND_LEVELS;i++)
- p->noisecompand[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
- }
- static int vorbis_encode_peak_setup(vorbis_info *vi,double q,int block,
- double *guard,
- double *suppress,
- vp_adjblock *in){
- int i,j,iq;
- double dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- iq=q*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- p->peakattp=1;
- p->tone_guard=guard[iq]*(1.-dq)+guard[iq+1]*dq;
- p->tone_abs_limit=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
- iq=q*5.;
- if(iq==5){
- iq=5;
- dq=1.;
- }else{
- dq=q*5.-iq;
- }
- for(i=0;i<P_BANDS;i++)
- for(j=0;j<P_LEVELS;j++)
- p->peakatt.block[i][j]=(j<4?4:j)*-10.+
- in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
- return(0);
- }
- static int vorbis_encode_noisebias_setup(vorbis_info *vi,double q,int block,
- double *suppress,
- int in[][17],int guard[33]){
- int i,iq=q*10;
- double dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- p->noisemaxsupp=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
- p->noisewindowlomin=guard[iq*3];
- p->noisewindowhimin=guard[iq*3+1];
- p->noisewindowfixed=guard[iq*3+2];
-
- for(i=0;i<P_BANDS;i++)
- p->noiseoff[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
- }
- static int vorbis_encode_ath_setup(vorbis_info *vi,double q,int block,
- float in[][27], ...){
- int i,iq=q*10;
- double x[11],dq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_psy *p=ci->psy_param[block];
- va_list ap;
-
- va_start(ap,in);
- for(i=0;i<11;i++)
- x[i]=va_arg(ap,double);
- va_end(ap);
- p->ath_adjatt=ci->hi.ath_floating_dB;
- p->ath_maxatt=ci->hi.ath_absolute_dB;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- dq=x[iq]*(1.-dq)+x[iq+1]*dq;
- iq=(int)dq;
- dq-=iq;
- if(dq==0 && iq>0){
- iq--;
- dq=1.;
- }
- for(i=0;i<27;i++)
- p->ath[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
- return(0);
- }
- static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
- int i;
- for(i=0;i<ci->books;i++)
- if(ci->book_param[i]==book)return(i);
-
- return(ci->books++);
- }
- static int vorbis_encode_residue_setup(vorbis_info *vi,double q,int block,
- int coupled_p,
- int stereo_backfill_p,
- int residue_backfill_p,
- vorbis_residue_template *in,
- int point_dB,
- double point_kHz){
- int i,iq=q*10;
- int n,k;
- int partition_position=0;
- int res_position=0;
- int iterations=1;
- int amplitude_select=0;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_residue0 *r;
- vorbis_info_psy *psy=ci->psy_param[block*2];
-
- /* may be re-called due to ctl */
- if(ci->residue_param[block])
- /* free preexisting instance */
- residue_free_info(ci->residue_param[block],ci->residue_type[block]);
- r=ci->residue_param[block]=_ogg_malloc(sizeof(*r));
- memcpy(r,in[iq].res[block],sizeof(*r));
- if(ci->residues<=block)ci->residues=block+1;
- if(block){
- r->grouping=32;
- }else{
- r->grouping=16;
- }
- /* for uncoupled, we use type 1, else type 2 */
- if(coupled_p){
- ci->residue_type[block]=2;
- }else{
- ci->residue_type[block]=1;
- }
- switch(ci->residue_type[block]){
- case 1:
- n=r->end=ci->blocksizes[block?1:0]>>1; /* to be adjusted by lowpass later */
- partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
- res_position=partition_position*r->grouping;
- break;
- case 2:
- n=r->end=(ci->blocksizes[block?1:0]>>1)*vi->channels; /* to be adjusted by lowpass later */
- partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
- res_position=partition_position*r->grouping/vi->channels;
- break;
- }
- for(i=0;i<r->partitions;i++)
- if(r->blimit[i]<0)r->blimit[i]=partition_position;
- for(i=0;i<r->partitions;i++)
- for(k=0;k<3;k++)
- if(in[iq].books_base[point_dB][i][k])
- r->secondstages[i]|=(1<<k);
-
- ci->passlimit[0]=3;
-
- if(coupled_p){
- vorbis_info_mapping0 *map=ci->map_param[block];
- map->coupling_steps=1;
- map->coupling_mag[0]=0;
- map->coupling_ang[0]=1;
- psy->couple_pass[0].granulem=1.;
- psy->couple_pass[0].igranulem=1.;
- psy->couple_pass[0].couple_pass[0].limit=res_position;
- psy->couple_pass[0].couple_pass[0].outofphase_redundant_flip_p=1;
- psy->couple_pass[0].couple_pass[0].outofphase_requant_limit=9e10;
- psy->couple_pass[0].couple_pass[0].amppost_point=0;
- psy->couple_pass[0].couple_pass[1].limit=9999;
- psy->couple_pass[0].couple_pass[1].outofphase_redundant_flip_p=1;
- psy->couple_pass[0].couple_pass[1].outofphase_requant_limit=9e10;
- psy->couple_pass[0].couple_pass[1].amppost_point=
- stereo_threshholds[point_dB];
- amplitude_select=point_dB;
- if(stereo_backfill_p && amplitude_select){
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[1].couple_pass[1].amppost_point=stereo_threshholds[amplitude_select-1];
- ci->passlimit[1]=4;
- for(i=0;i<r->partitions;i++)
- if(in[iq].books_stereo_backfill[amplitude_select][i])
- r->secondstages[i]|=8;
- amplitude_select=amplitude_select-1;
- iterations++;
- }
-
- if(residue_backfill_p){
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[iterations].granulem=.333333333;
- psy->couple_pass[iterations].igranulem=3.;
- psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=1.;
- psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=1.;
- for(i=0;i<r->partitions;i++)
- if(in[iq].books_residue_backfill[amplitude_select][i][0])
- r->secondstages[i]|=(1<<(iterations+2));
- ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
- iterations++;
-
- memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
- sizeof(*psy->couple_pass));
- psy->couple_pass[iterations].granulem=.1111111111;
- psy->couple_pass[iterations].igranulem=9.;
- psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=.3;
- psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=.3;
- for(i=0;i<r->partitions;i++)
- if(in[iq].books_residue_backfill[amplitude_select][i][1])
- r->secondstages[i]|=(1<<(iterations+2));
- ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
- iterations++;
- }
- ci->coupling_passes=iterations;
- }else{
- if(residue_backfill_p){
- for(i=0;i<r->partitions;i++){
- if(in[iq].books_residue_backfill[0][i][0])
- r->secondstages[i]|=8;
- if(in[iq].books_residue_backfill[0][i][1])
- r->secondstages[i]|=16;
- }
- ci->passlimit[1]=4;
- ci->passlimit[2]=5;
- ci->coupling_passes=3;
- }else
- ci->coupling_passes=1;
- }
-
- memcpy(&ci->psy_param[block*2+1]->couple_pass,
- &ci->psy_param[block*2]->couple_pass,
- sizeof(psy->couple_pass));
-
- /* fill in all the books */
- {
- int booklist=0,k;
- r->groupbook=ci->books;
- ci->book_param[ci->books++]=in[iq].book_aux[block];
- for(i=0;i<r->partitions;i++){
- for(k=0;k<3;k++){
- if(in[iq].books_base[point_dB][i][k]){
- int bookid=book_dup_or_new(ci,in[iq].books_base[point_dB][i][k]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_base[point_dB][i][k];
- }
- }
- if(coupled_p && stereo_backfill_p && point_dB &&
- in[iq].books_stereo_backfill[point_dB][i]){
- int bookid=book_dup_or_new(ci,in[iq].books_stereo_backfill[point_dB][i]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_stereo_backfill[point_dB][i];
- }
- if(residue_backfill_p){
- for(k=0;k<2;k++){
- if(in[iq].books_residue_backfill[amplitude_select][i][k]){
- int bookid=book_dup_or_new(ci,in[iq].books_residue_backfill[amplitude_select][i][k]);
- r->booklist[booklist++]=bookid;
- ci->book_param[bookid]=in[iq].books_residue_backfill[amplitude_select][i][k];
- }
- }
- }
- }
- }
- return(0);
- }
- static int vorbis_encode_lowpass_setup(vorbis_info *vi,double q,int block){
- int iq=q*10;
- double dq;
- double freq;
- codec_setup_info *ci=vi->codec_setup;
- vorbis_info_floor1 *f=ci->floor_param[block];
- vorbis_info_residue0 *r=ci->residue_param[block];
- int blocksize=ci->blocksizes[block]>>1;
- double nyq=vi->rate/2.;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
-
- freq=ci->hi.lowpass_kHz[block]*1000.;
- if(freq>vi->rate/2)freq=vi->rate/2;
- /* lowpass needs to be set in the floor and the residue. */
- /* in the floor, the granularity can be very fine; it doesn't alter
- the encoding structure, only the samples used to fit the floor
- approximation */
- f->n=freq/nyq*blocksize;
- /* in the residue, we're constrained, physically, by partition
- boundaries. We still lowpass 'wherever', but we have to round up
- here to next boundary, or the vorbis spec will round it *down* to
- previous boundary in encode/decode */
- if(ci->residue_type[block]==2)
- r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
- r->grouping;
- else
- r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
- r->grouping;
- return(0);
- }
- /* encoders will need to use vorbis_info_init beforehand and call
- vorbis_info clear when all done */
- /* two interfaces; this, more detailed one, and later a convenience
- layer on top */
- /* the final setup call */
- int vorbis_encode_setup_init(vorbis_info *vi){
- int ret=0;
- /*long rate=vi->rate;*/
- long channels=vi->channels;
- codec_setup_info *ci=vi->codec_setup;
- highlevel_encode_setup *hi=&ci->hi;
- ret|=vorbis_encode_floor_setup(vi,hi->base_quality_short,0,
- _floor_44_128_books,_floor_44_128,
- 0,1,1,2,2,2,2,2,2,2,2);
- ret|=vorbis_encode_floor_setup(vi,hi->base_quality_long,1,
- _floor_44_1024_books,_floor_44_1024,
- 0,0,0,0,0,0,0,0,0,0,0);
-
- ret|=vorbis_encode_global_psych_setup(vi,hi->trigger_quality,_psy_global_44,
- 0., 1., 1.5, 2., 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_psyset_setup(vi,0);
- ret|=vorbis_encode_psyset_setup(vi,1);
- ret|=vorbis_encode_psyset_setup(vi,2);
- ret|=vorbis_encode_psyset_setup(vi,3);
-
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[0].tone_mask_quality,0,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[1].tone_mask_quality,1,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[2].tone_mask_quality,2,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_otherblock);
- ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[3].tone_mask_quality,3,
- _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
- _vp_tonemask_adj_longblock);
-
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[0].noise_compand_quality,
- 0,_psy_compand_44_short,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[1].noise_compand_quality,
- 1,_psy_compand_44_short,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[2].noise_compand_quality,
- 2,_psy_compand_44,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_compand_setup(vi,hi->blocktype[3].noise_compand_quality,
- 3,_psy_compand_44,
- 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[0].tone_peaklimit_quality,
- 0,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[1].tone_peaklimit_quality,
- 1,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[2].tone_peaklimit_quality,
- 2,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- ret|=vorbis_encode_peak_setup(vi,hi->blocktype[3].tone_peaklimit_quality,
- 3,_psy_tone_masterguard,_psy_tone_suppress,
- _vp_peakguard);
- if(hi->impulse_block_p){
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
- 0,_psy_noise_suppress,_psy_noisebias_impulse,
- _psy_noiseguards_short);
- }else{
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
- 0,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_short);
- }
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[1].noise_bias_quality,
- 1,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_short);
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[2].noise_bias_quality,
- 2,_psy_noise_suppress,_psy_noisebias_other,
- _psy_noiseguards_long);
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[3].noise_bias_quality,
- 3,_psy_noise_suppress,_psy_noisebias_long,
- _psy_noiseguards_long);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[0].ath_quality,0,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[1].ath_quality,1,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[2].ath_quality,2,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- ret|=vorbis_encode_ath_setup(vi,hi->blocktype[3].ath_quality,3,ATH_Bark_dB,
- 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
- if(ret){
- vorbis_info_clear(vi);
- return ret;
- }
- if(channels==2 && hi->stereo_couple_p){
- /* setup specific to stereo coupling */
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
- 1, /* coupled */
- hi->stereo_backfill_p,
- hi->residue_backfill_p,
- _residue_template_44_stereo,
- hi->stereo_point_dB,
- hi->stereo_point_kHz[0]);
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
- 1, /* coupled */
- hi->stereo_backfill_p,
- hi->residue_backfill_p,
- _residue_template_44_stereo,
- hi->stereo_point_dB,
- hi->stereo_point_kHz[1]);
- }else{
- /* setup specific to non-stereo (mono or uncoupled polyphonic)
- coupling */
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
- 0, /* uncoupled */
- 0,
- hi->residue_backfill_p,
- _residue_template_44_uncoupled,
- 0,
- hi->stereo_point_kHz[0]); /* just
- used as an encoding partitioning
- point */
-
- ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
- 0, /* uncoupled */
- 0,
- hi->residue_backfill_p,
- _residue_template_44_uncoupled,
- 0,
- hi->stereo_point_kHz[1]); /* just
- used as an encoding partitioning
- point */
- }
- ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[0],0);
- ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[1],1);
-
- if(ret)
- vorbis_info_clear(vi);
- return(ret);
- }
- /* this is only tuned for 44.1kHz right now. S'ok, for other rates it
- just doesn't guess */
- static double ratepch_un44[11]=
- {40000.,50000.,60000.,70000.,75000.,85000.,105000.,
- 115000.,135000.,160000.,250000.};
- static double ratepch_st44[11]=
- {32000.,40000.,48000.,56000.,64000.,
- 80000.,96000.,112000.,128000.,160000.,250000.};
- static double vbr_to_approx_bitrate(int ch,int coupled,
- double q,long srate){
- int iq=q*10.;
- double dq;
- double *r=NULL;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=q*10.-iq;
- }
- if(srate>42000 && srate<46000){
- if(coupled)
- r=ratepch_st44;
- else
- r=ratepch_un44;
- }
-
- if(r==NULL)
- return(-1);
-
- return((r[iq]*(1.-dq)+r[iq+1]*dq)*ch);
- }
- static double approx_bitrate_to_vbr(int ch,int coupled,
- double bitrate,long srate){
- double *r=NULL,del;
- int i;
- if(srate>42000 && srate<46000){
- if(coupled)
- r=ratepch_st44;
- else
- r=ratepch_un44;
- }
-
- if(r==NULL)
- return(-1.);
- bitrate/=ch;
- if(bitrate<=r[0])return(0.);
- for(i=0;i<10;i++)
- if(r[i]<bitrate && r[i+1]>=bitrate)break;
- if(i==10)return(10.);
- del=(bitrate-r[i])/(r[i+1]-r[i]);
-
- return((i+del)*.1);
- }
- /* only populates the high-level settings so that we can tweak with ctl before final setup */
- int vorbis_encode_setup_vbr(vorbis_info *vi,
- long channels,
- long rate,
-
- float base_quality){
- int ret=0,i,iq;
- double dq;
- codec_setup_info *ci=vi->codec_setup;
- highlevel_encode_setup *hi=&ci->hi;
-
- base_quality+=.0001;
- if(base_quality<0.)base_quality=0.;
- if(base_quality>.999)base_quality=.999;
- iq=base_quality*10;
- if(iq==10){
- iq=9;
- dq=1.;
- }else{
- dq=base_quality*10.-iq;
- }
- ret|=vorbis_encode_toplevel_setup(vi,256,2048,channels,rate);
- hi->base_quality=base_quality;
- hi->base_quality_short=base_quality;
- hi->base_quality_long=base_quality;
- hi->trigger_quality=base_quality;
- for(i=0;i<4;i++){
- hi->blocktype[i].tone_mask_quality=base_quality;
- hi->blocktype[i].tone_peaklimit_quality=base_quality;
- hi->blocktype[i].noise_bias_quality=base_quality;
- hi->blocktype[i].noise_compand_quality=base_quality;
- hi->blocktype[i].ath_quality=base_quality;
- }
- hi->short_block_p=1;
- hi->long_block_p=1;
- hi->impulse_block_p=1;
- hi->amplitude_track_dBpersec=-6.;
- hi->stereo_couple_p=1; /* only relevant if a two channel input */
- hi->stereo_backfill_p=0;
- hi->residue_backfill_p=0;
- /* set the ATH floaters */
- hi->ath_floating_dB=_psy_ath_floater[iq]*(1.-dq)+_psy_ath_floater[iq+1]*dq;
- hi->ath_absolute_dB=_psy_ath_abs[iq]*(1.-dq)+_psy_ath_abs[iq+1]*dq;
- /* set stereo dB and Hz */
- hi->stereo_point_dB=_psy_stereo_point_dB_44[iq];
- hi->stereo_point_kHz[0]=_psy_stereo_point_kHz_44[0][iq]*(1.-dq)+
- _psy_stereo_point_kHz_44[0][iq+1]*dq;
- hi->stereo_point_kHz[1]=_psy_stereo_point_kHz_44[1][iq]*(1.-dq)+
- _psy_stereo_point_kHz_44[1][iq+1]*dq;
-
- /* set lowpass */
- hi->lowpass_kHz[0]=
- hi->lowpass_kHz[1]=
- _psy_lowpass_44[iq]*(1.-dq)+_psy_lowpass_44[iq+1]*dq;
- /* set bitrate approximation */
- vi->bitrate_nominal=vbr_to_approx_bitrate(vi->channels,hi->stereo_couple_p,
- base_quality,vi->rate);
- vi->bitrate_lower=-1;
- vi->bitrate_upper=-1;
- vi->bitrate_window=-1;
- return(ret);
- }
- int vorbis_encode_init_vbr(vorbis_info *vi,
- long channels,
- long rate,
-
- float base_quality /* 0. to 1. */
- ){
- int ret=0;
- ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
-
- if(ret){
- vorbis_info_clear(vi);
- return ret;
- }
- ret=vorbis_encode_setup_init(vi);
- if(ret)
- vorbis_info_clear(vi);
- return(ret);
- }
- int vorbis_encode_setup_managed(vorbis_info *vi,
- long channels,
- long rate,
-
- long max_bitrate,
- long nominal_bitrate,
- long min_bitrate){
- double tnominal=nominal_bitrate;
- double approx_vbr;
- int ret=0;
- if(nominal_bitrate<=0.){
- if(max_bitrate>0.){
- nominal_bitrate=max_bitrate*.875;
- }else{
- if(min_bitrate>0.){
- nominal_bitrate=min_bitrate;
- }else{
- return(OV_EINVAL);
- }
- }
- }
- approx_vbr=approx_bitrate_to_vbr(channels,(channels==2),
- (float)nominal_bitrate,rate);
- if(approx_vbr<0)return(OV_EIMPL);
- ret=vorbis_encode_setup_vbr(vi,channels,rate,approx_vbr);
- if(ret){
- vorbis_info_clear(vi);
- return ret;
- }
- /* adjust to make management's life easier. Use the ctl() interface
- once it's implemented */
- {
- codec_setup_info *ci=vi->codec_setup;
- highlevel_encode_setup *hi=&ci->hi;
- /* backfills */
- hi->stereo_backfill_p=1;
- hi->residue_backfill_p=1;
- /* no impulse blocks */
- hi->impulse_block_p=0;
- /* de-rate stereo */
- if(hi->stereo_point_dB && hi->stereo_couple_p && channels==2){
- hi->stereo_point_dB++;
- if(hi->stereo_point_dB>3)hi->stereo_point_dB=3;
- }
- /* slug the vbr noise setting*/
- hi->blocktype[0].noise_bias_quality-=.1;
- if(hi->blocktype[0].noise_bias_quality<0.)
- hi->blocktype[0].noise_bias_quality=0.;
- hi->blocktype[1].noise_bias_quality-=.1;
- if(hi->blocktype[1].noise_bias_quality<0.)
- hi->blocktype[1].noise_bias_quality=0.;
- hi->blocktype[2].noise_bias_quality-=.05;
- if(hi->blocktype[2].noise_bias_quality<0.)
- hi->blocktype[2].noise_bias_quality=0.;
- hi->blocktype[3].noise_bias_quality-=.05;
- if(hi->blocktype[3].noise_bias_quality<0.)
- hi->blocktype[3].noise_bias_quality=0.;
-
- /* initialize management. Currently hardcoded for 44, but so is above. */
- memcpy(&ci->bi,&_bm_44_default,sizeof(ci->bi));
- ci->bi.queue_hardmin=min_bitrate;
- ci->bi.queue_hardmax=max_bitrate;
-
- ci->bi.queue_avgmin=tnominal;
- ci->bi.queue_avgmax=tnominal;
- /* adjust management */
- ci->bi.avgfloat_noise_maxval=_bm_max_noise_offset[(int)approx_vbr];
- }
- return(ret);
- }
- int vorbis_encode_init(vorbis_info *vi,
- long channels,
- long rate,
- long max_bitrate,
- long nominal_bitrate,
- long min_bitrate){
- int ret=vorbis_encode_setup_managed(vi,channels,rate,
- max_bitrate,
- nominal_bitrate,
- min_bitrate);
- if(ret){
- vorbis_info_clear(vi);
- return(ret);
- }
- ret=vorbis_encode_setup_init(vi);
- if(ret)
- vorbis_info_clear(vi);
- return(ret);
- }
- int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
- return(OV_EIMPL);
- }
|