123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- /********************************************************************
- * *
- * 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-2001 *
- * by the XIPHOPHORUS Company http://www.xiph.org/ *
- * *
- ********************************************************************
- function: simple programmatic interface for encoder mode setup
- last mod: $Id: vorbisenc.c,v 1.23.2.2 2001/12/18 23:49:17 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]=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]=calloc(1,sizeof(_mode_set_short));
- memcpy(ci->mode_param[0],&_mode_set_short,sizeof(_mode_set_short));
- ci->mode_param[1]=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]=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]=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=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=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];
- 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;
- int res_position;
- 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]=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);
-
- ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
- 0,_psy_noise_suppress,_psy_noisebias_impulse,
- _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);
- }
- /* 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;
- 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,1.);
-
- {
- codec_setup_info *ci=vi->codec_setup;
- highlevel_encode_setup *hi=&ci->hi;
- hi->stereo_couple_p=0;
- hi->residue_backfill_p=1;
- }
- 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_init(vorbis_info *vi,
- long channels,
- long rate,
- long max_bitrate,
- long nominal_bitrate,
- long min_bitrate){
- /* it's temporary while I do the merge; relax */
- if(rate>40000){
- if(nominal_bitrate>360000){
- return(vorbis_encode_init_vbr(vi,channels,rate, 1.));
- }else if(nominal_bitrate>270000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .9));
- }else if(nominal_bitrate>230000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .8));
- }else if(nominal_bitrate>200000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .7));
- }else if(nominal_bitrate>180000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .6));
- }else if(nominal_bitrate>140000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .5));
- }else if(nominal_bitrate>120000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .4));
- }else if(nominal_bitrate>100000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .3));
- }else if(nominal_bitrate>90000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .2));
- }else if(nominal_bitrate>75000){
- return(vorbis_encode_init_vbr(vi,channels,rate, .1));
- }else{
- return(vorbis_encode_init_vbr(vi,channels,rate, .0));
- }
- }
- return(OV_EIMPL);
- }
- int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
- return(OV_EIMPL);
- }
-
|