examples/sfexamples/oggvorbiscodec/src/libvorbis/lib/mapping0.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
00009  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013  function: channel mapping 0 implementation
00014  last mod: $Id: mapping0.c 7187 2004-07-20 07:24:27Z xiphmont $
00015 
00016  ********************************************************************/
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <math.h>
00022 #include "ogg/ogg.h"
00023 #include "vorbis/codec.h"
00024 #include "codec_internal.h"
00025 #include "codebook.h"
00026 #include "window.h"
00027 #include "registry.h"
00028 #include "psy.h"
00029 #include "misc.h"
00030 
00031 /* simplistic, wasteful way of doing this (unique lookup for each
00032    mode/submapping); there should be a central repository for
00033    identical lookups.  That will require minor work, so I'm putting it
00034    off as low priority.
00035 
00036    Why a lookup for each backend in a given mode?  Because the
00037    blocksize is set by the mode, and low backend lookups may require
00038    parameters from other areas of the mode/mapping */
00039 
00040 static void mapping0_free_info(vorbis_info_mapping *i){
00041   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
00042   if(info){
00043     memset(info,0,sizeof(*info));
00044     _ogg_free(info);
00045   }
00046 }
00047 
00048 static int ilog(unsigned int v){
00049   int ret=0;
00050   if(v)--v;
00051   while(v){
00052     ret++;
00053     v>>=1;
00054   }
00055   return(ret);
00056 }
00057 
00058 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
00059                           oggpack_buffer *opb){
00060   int i;
00061   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
00062 
00063   /* another 'we meant to do it this way' hack...  up to beta 4, we
00064      packed 4 binary zeros here to signify one submapping in use.  We
00065      now redefine that to mean four bitflags that indicate use of
00066      deeper features; bit0:submappings, bit1:coupling,
00067      bit2,3:reserved. This is backward compatable with all actual uses
00068      of the beta code. */
00069 
00070   if(info->submaps>1){
00071     oggpack_write(opb,1,1);
00072     oggpack_write(opb,info->submaps-1,4);
00073   }else
00074     oggpack_write(opb,0,1);
00075 
00076   if(info->coupling_steps>0){
00077     oggpack_write(opb,1,1);
00078     oggpack_write(opb,info->coupling_steps-1,8);
00079     
00080     for(i=0;i<info->coupling_steps;i++){
00081       oggpack_write(opb,info->coupling_mag[i],ilog(vi->channels));
00082       oggpack_write(opb,info->coupling_ang[i],ilog(vi->channels));
00083     }
00084   }else
00085     oggpack_write(opb,0,1);
00086   
00087   oggpack_write(opb,0,2); /* 2,3:reserved */
00088 
00089   /* we don't write the channel submappings if we only have one... */
00090   if(info->submaps>1){
00091     for(i=0;i<vi->channels;i++)
00092       oggpack_write(opb,info->chmuxlist[i],4);
00093   }
00094   for(i=0;i<info->submaps;i++){
00095     oggpack_write(opb,0,8); /* time submap unused */
00096     oggpack_write(opb,info->floorsubmap[i],8);
00097     oggpack_write(opb,info->residuesubmap[i],8);
00098   }
00099 }
00100 
00101 /* also responsible for range checking */
00102 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
00103   int i;
00104   vorbis_info_mapping0 *info=(vorbis_info_mapping0*)_ogg_calloc(1,sizeof(*info));
00105   codec_setup_info     *ci=(codec_setup_info*)vi->codec_setup;
00106   memset(info,0,sizeof(*info));
00107 
00108   if(oggpack_read(opb,1))
00109     info->submaps=oggpack_read(opb,4)+1;
00110   else
00111     info->submaps=1;
00112 
00113   if(oggpack_read(opb,1)){
00114     info->coupling_steps=oggpack_read(opb,8)+1;
00115 
00116     for(i=0;i<info->coupling_steps;i++){
00117       int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
00118       int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
00119 
00120       if(testM<0 || 
00121          testA<0 || 
00122          testM==testA || 
00123          testM>=vi->channels ||
00124          testA>=vi->channels) goto err_out;
00125     }
00126 
00127   }
00128 
00129   if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
00130     
00131   if(info->submaps>1){
00132     for(i=0;i<vi->channels;i++){
00133       info->chmuxlist[i]=oggpack_read(opb,4);
00134       if(info->chmuxlist[i]>=info->submaps)goto err_out;
00135     }
00136   }
00137   for(i=0;i<info->submaps;i++){
00138     oggpack_read(opb,8); /* time submap unused */
00139     info->floorsubmap[i]=oggpack_read(opb,8);
00140     if(info->floorsubmap[i]>=ci->floors)goto err_out;
00141     info->residuesubmap[i]=oggpack_read(opb,8);
00142     if(info->residuesubmap[i]>=ci->residues)goto err_out;
00143   }
00144 
00145   return info;
00146 
00147  err_out:
00148   mapping0_free_info(info);
00149   return(NULL);
00150 }
00151 
00152 #include "os.h"
00153 #include "lpc.h"
00154 #include "lsp.h"
00155 #include "envelope.h"
00156 #include "mdct.h"
00157 #include "psy.h"
00158 #include "scales.h"
00159 
00160 #if 0
00161 static long seq=0;
00162 static ogg_int64_t total=0;
00163 static float FLOOR1_fromdB_LOOKUP[256]={
00164   1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F, 
00165   1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F, 
00166   1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F, 
00167   2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F, 
00168   2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F, 
00169   3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F, 
00170   4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F, 
00171   6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F, 
00172   7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F, 
00173   1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F, 
00174   1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F, 
00175   1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F, 
00176   2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F, 
00177   2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F, 
00178   3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F, 
00179   4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F, 
00180   5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F, 
00181   7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F, 
00182   9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F, 
00183   1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F, 
00184   1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F, 
00185   2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F, 
00186   2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F, 
00187   3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F, 
00188   4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F, 
00189   5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F, 
00190   7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F, 
00191   9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F, 
00192   0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F, 
00193   0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F, 
00194   0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F, 
00195   0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F, 
00196   0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F, 
00197   0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F, 
00198   0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F, 
00199   0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F, 
00200   0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F, 
00201   0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F, 
00202   0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F, 
00203   0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F, 
00204   0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F, 
00205   0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F, 
00206   0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F, 
00207   0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F, 
00208   0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F, 
00209   0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F, 
00210   0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F, 
00211   0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F, 
00212   0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F, 
00213   0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F, 
00214   0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F, 
00215   0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F, 
00216   0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F, 
00217   0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F, 
00218   0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F, 
00219   0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F, 
00220   0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F, 
00221   0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F, 
00222   0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F, 
00223   0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F, 
00224   0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F, 
00225   0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F, 
00226   0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F, 
00227   0.82788260F, 0.88168307F, 0.9389798F, 1.F, 
00228 };
00229 
00230 #endif 
00231 
00232 extern int *floor1_fit(vorbis_block *vb,vorbis_look_floor *look,
00233                        const float *logmdct,   /* in */
00234                        const float *logmask);
00235 extern int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor *look,
00236                                    int *A,int *B,
00237                                    int del);
00238 extern int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
00239                          vorbis_look_floor *look,
00240                          int *post,int *ilogmask);
00241 
00242 
00243 static int mapping0_forward(vorbis_block *vb){
00244   vorbis_dsp_state      *vd=vb->vd;
00245   vorbis_info           *vi=vd->vi;
00246   codec_setup_info      *ci=(codec_setup_info*)vi->codec_setup;
00247   private_state         *b=(private_state*)vb->vd->backend_state;
00248   vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
00249   int                    n=vb->pcmend;
00250   int i,j,k;
00251 
00252   //int    *nonzero    = alloca(sizeof(*nonzero)*vi->channels);//patch
00253   int    *nonzero    = (int*)_ogg_malloc(sizeof(*nonzero)*vi->channels); //patch
00254   int *nonzero_cpy = nonzero; //patch
00255   
00256   float  **gmdct     = (float**)_vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct));
00257   int    **ilogmaskch= (int**)_vorbis_block_alloc(vb,vi->channels*sizeof(*ilogmaskch));
00258   int ***floor_posts = (int***)_vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts));
00259   
00260   float global_ampmax=vbi->ampmax;
00261   //float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);//patch
00262   float *local_ampmax=(float*)_ogg_malloc(sizeof(*local_ampmax)*vi->channels);//patch
00263   float *local_ampmax_cpy = local_ampmax; //patch
00264   
00265   int blocktype=vbi->blocktype;
00266 
00267   int modenumber=vb->W;
00268   vorbis_info_mapping0 *info=(vorbis_info_mapping0*)ci->map_param[modenumber];
00269   vorbis_look_psy *psy_look=
00270     b->psy+blocktype+(vb->W?2:0);
00271 
00272   vb->mode=modenumber;
00273 
00274   for(i=0;i<vi->channels;i++){
00275     float scale=4.f/n;
00276     float scale_dB;
00277 
00278     float *pcm     =vb->pcm[i]; 
00279     float *logfft  =pcm;
00280 
00281     gmdct[i]=(float*)_vorbis_block_alloc(vb,n/2*sizeof(**gmdct));
00282 
00283     scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original
00284                                      todB estimation used on IEEE 754
00285                                      compliant machines had a bug that
00286                                      returned dB values about a third
00287                                      of a decibel too high.  The bug
00288                                      was harmless because tunings
00289                                      implicitly took that into
00290                                      account.  However, fixing the bug
00291                                      in the estimator requires
00292                                      changing all the tunings as well.
00293                                      For now, it's easier to sync
00294                                      things back up here, and
00295                                      recalibrate the tunings in the
00296                                      next major model upgrade. */
00297 
00298 #if 0
00299     if(vi->channels==2)
00300       if(i==0)
00301         _analysis_output("pcmL",seq,pcm,n,0,0,total-n/2);
00302       else
00303         _analysis_output("pcmR",seq,pcm,n,0,0,total-n/2);
00304 #endif
00305   
00306     /* window the PCM data */
00307     _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
00308 
00309 #if 0
00310     if(vi->channels==2)
00311       if(i==0)
00312         _analysis_output("windowedL",seq,pcm,n,0,0,total-n/2);
00313       else
00314         _analysis_output("windowedR",seq,pcm,n,0,0,total-n/2);
00315 #endif
00316 
00317     /* transform the PCM data */
00318     /* only MDCT right now.... */
00319     mdct_forward((mdct_lookup*)b->transform[vb->W][0],pcm,gmdct[i]);
00320     
00321     /* FFT yields more accurate tonal estimation (not phase sensitive) */
00322     drft_forward(&b->fft_look[vb->W],pcm);
00323     logfft[0]=scale_dB+todB(pcm)  + .345; /* + .345 is a hack; the
00324                                      original todB estimation used on
00325                                      IEEE 754 compliant machines had a
00326                                      bug that returned dB values about
00327                                      a third of a decibel too high.
00328                                      The bug was harmless because
00329                                      tunings implicitly took that into
00330                                      account.  However, fixing the bug
00331                                      in the estimator requires
00332                                      changing all the tunings as well.
00333                                      For now, it's easier to sync
00334                                      things back up here, and
00335                                      recalibrate the tunings in the
00336                                      next major model upgrade. */
00337     local_ampmax[i]=logfft[0];
00338     for(j=1;j<n-1;j+=2){
00339       float temp=pcm[j]*pcm[j]+pcm[j+1]*pcm[j+1];
00340       temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp)  + .345; /* +
00341                                      .345 is a hack; the original todB
00342                                      estimation used on IEEE 754
00343                                      compliant machines had a bug that
00344                                      returned dB values about a third
00345                                      of a decibel too high.  The bug
00346                                      was harmless because tunings
00347                                      implicitly took that into
00348                                      account.  However, fixing the bug
00349                                      in the estimator requires
00350                                      changing all the tunings as well.
00351                                      For now, it's easier to sync
00352                                      things back up here, and
00353                                      recalibrate the tunings in the
00354                                      next major model upgrade. */
00355       if(temp>local_ampmax[i])local_ampmax[i]=temp;
00356     }
00357 
00358     if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
00359     if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
00360 
00361 #if 0
00362     if(vi->channels==2){
00363       if(i==0){
00364         _analysis_output("fftL",seq,logfft,n/2,1,0,0);
00365       }else{
00366         _analysis_output("fftR",seq,logfft,n/2,1,0,0);
00367       }
00368     }
00369 #endif
00370 
00371   }
00372   
00373   {
00374     float   *noise        = (float*)_vorbis_block_alloc(vb,n/2*sizeof(*noise));
00375     float   *tone         = (float*)_vorbis_block_alloc(vb,n/2*sizeof(*tone));
00376     
00377     for(i=0;i<vi->channels;i++){
00378       /* the encoder setup assumes that all the modes used by any
00379          specific bitrate tweaking use the same floor */
00380       
00381       int submap=info->chmuxlist[i];
00382       
00383       /* the following makes things clearer to *me* anyway */
00384       float *mdct    =gmdct[i];
00385       float *logfft  =vb->pcm[i];
00386       
00387       float *logmdct =logfft+n/2;
00388       float *logmask =logfft;
00389 
00390       vb->mode=modenumber;
00391 
00392       floor_posts[i]=(int**)_vorbis_block_alloc(vb,PACKETBLOBS*sizeof(**floor_posts));
00393       memset(floor_posts[i],0,sizeof(**floor_posts)*PACKETBLOBS);
00394       
00395       for(j=0;j<n/2;j++)
00396         logmdct[j]=todB(mdct+j)  + .345; /* + .345 is a hack; the original
00397                                      todB estimation used on IEEE 754
00398                                      compliant machines had a bug that
00399                                      returned dB values about a third
00400                                      of a decibel too high.  The bug
00401                                      was harmless because tunings
00402                                      implicitly took that into
00403                                      account.  However, fixing the bug
00404                                      in the estimator requires
00405                                      changing all the tunings as well.
00406                                      For now, it's easier to sync
00407                                      things back up here, and
00408                                      recalibrate the tunings in the
00409                                      next major model upgrade. */
00410 
00411 #if 0
00412       if(vi->channels==2){
00413         if(i==0)
00414           _analysis_output("mdctL",seq,logmdct,n/2,1,0,0);
00415         else
00416           _analysis_output("mdctR",seq,logmdct,n/2,1,0,0);
00417       }else{
00418         _analysis_output("mdct",seq,logmdct,n/2,1,0,0);
00419       }
00420 #endif 
00421       
00422       /* first step; noise masking.  Not only does 'noise masking'
00423          give us curves from which we can decide how much resolution
00424          to give noise parts of the spectrum, it also implicitly hands
00425          us a tonality estimate (the larger the value in the
00426          'noise_depth' vector, the more tonal that area is) */
00427 
00428       _vp_noisemask(psy_look,
00429                     logmdct,
00430                     noise); /* noise does not have by-frequency offset
00431                                bias applied yet */
00432 #if 0
00433       if(vi->channels==2){
00434         if(i==0)
00435           _analysis_output("noiseL",seq,noise,n/2,1,0,0);
00436         else
00437           _analysis_output("noiseR",seq,noise,n/2,1,0,0);
00438       }
00439 #endif
00440 
00441       /* second step: 'all the other crap'; all the stuff that isn't
00442          computed/fit for bitrate management goes in the second psy
00443          vector.  This includes tone masking, peak limiting and ATH */
00444 
00445       _vp_tonemask(psy_look,
00446                    logfft,
00447                    tone,
00448                    global_ampmax,
00449                    local_ampmax[i]);
00450 
00451 #if 0
00452       if(vi->channels==2){
00453         if(i==0)
00454           _analysis_output("toneL",seq,tone,n/2,1,0,0);
00455         else
00456           _analysis_output("toneR",seq,tone,n/2,1,0,0);
00457       }
00458 #endif
00459 
00460       /* third step; we offset the noise vectors, overlay tone
00461          masking.  We then do a floor1-specific line fit.  If we're
00462          performing bitrate management, the line fit is performed
00463          multiple times for up/down tweakage on demand. */
00464 
00465 #if 0
00466       {
00467       float aotuv[psy_look->n];
00468 #endif
00469 
00470         _vp_offset_and_mix(psy_look,
00471                            noise,
00472                            tone,
00473                            1,
00474                            logmask,
00475                            mdct,
00476                            logmdct);
00477         
00478 #if 0
00479         if(vi->channels==2){
00480           if(i==0)
00481             _analysis_output("aotuvM1_L",seq,aotuv,psy_look->n,1,1,0);
00482           else
00483             _analysis_output("aotuvM1_R",seq,aotuv,psy_look->n,1,1,0);
00484         }
00485       }
00486 #endif
00487 
00488 
00489 #if 0
00490       if(vi->channels==2){
00491         if(i==0)
00492           _analysis_output("mask1L",seq,logmask,n/2,1,0,0);
00493         else
00494           _analysis_output("mask1R",seq,logmask,n/2,1,0,0);
00495       }
00496 #endif
00497 
00498       /* this algorithm is hardwired to floor 1 for now; abort out if
00499          we're *not* floor1.  This won't happen unless someone has
00500          broken the encode setup lib.  Guard it anyway. */
00501       if(ci->floor_type[info->floorsubmap[submap]]!=1){
00502                 free(nonzero_cpy); //patch              
00503                 free(local_ampmax_cpy);//patch
00504         return(-1);     
00505       }
00506       
00507 
00508       floor_posts[i][PACKETBLOBS/2]=
00509         floor1_fit(vb,b->flr[info->floorsubmap[submap]],
00510                    logmdct,
00511                    logmask);
00512       
00513       /* are we managing bitrate?  If so, perform two more fits for
00514          later rate tweaking (fits represent hi/lo) */
00515       if(vorbis_bitrate_managed(vb) && floor_posts[i][PACKETBLOBS/2]){
00516         /* higher rate by way of lower noise curve */
00517 
00518         _vp_offset_and_mix(psy_look,
00519                            noise,
00520                            tone,
00521                            2,
00522                            logmask,
00523                            mdct,
00524                            logmdct);
00525 
00526 #if 0
00527         if(vi->channels==2){
00528           if(i==0)
00529             _analysis_output("mask2L",seq,logmask,n/2,1,0,0);
00530           else
00531             _analysis_output("mask2R",seq,logmask,n/2,1,0,0);
00532         }
00533 #endif
00534         
00535         floor_posts[i][PACKETBLOBS-1]=
00536           floor1_fit(vb,b->flr[info->floorsubmap[submap]],
00537                      logmdct,
00538                      logmask);
00539       
00540         /* lower rate by way of higher noise curve */
00541         _vp_offset_and_mix(psy_look,
00542                            noise,
00543                            tone,
00544                            0,
00545                            logmask,
00546                            mdct,
00547                            logmdct);
00548 
00549 #if 0
00550         if(vi->channels==2)
00551           if(i==0)
00552             _analysis_output("mask0L",seq,logmask,n/2,1,0,0);
00553           else
00554             _analysis_output("mask0R",seq,logmask,n/2,1,0,0);
00555 #endif
00556 
00557         floor_posts[i][0]=
00558           floor1_fit(vb,b->flr[info->floorsubmap[submap]],
00559                      logmdct,
00560                      logmask);
00561         
00562         /* we also interpolate a range of intermediate curves for
00563            intermediate rates */
00564         for(k=1;k<PACKETBLOBS/2;k++)
00565           floor_posts[i][k]=
00566             floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
00567                                    floor_posts[i][0],
00568                                    floor_posts[i][PACKETBLOBS/2],
00569                                    k*65536/(PACKETBLOBS/2));
00570         for(k=PACKETBLOBS/2+1;k<PACKETBLOBS-1;k++)
00571           floor_posts[i][k]=
00572             floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
00573                                    floor_posts[i][PACKETBLOBS/2],
00574                                    floor_posts[i][PACKETBLOBS-1],
00575                                    (k-PACKETBLOBS/2)*65536/(PACKETBLOBS/2));
00576       }
00577     }
00578   }
00579   vbi->ampmax=global_ampmax;
00580 
00581   /*
00582     the next phases are performed once for vbr-only and PACKETBLOB
00583     times for bitrate managed modes.
00584     
00585     1) encode actual mode being used
00586     2) encode the floor for each channel, compute coded mask curve/res
00587     3) normalize and couple.
00588     4) encode residue
00589     5) save packet bytes to the packetblob vector
00590     
00591   */
00592 
00593   /* iterate over the many masking curve fits we've created */
00594 
00595   {
00596     //-----patch
00597     //float **res_bundle=alloca(sizeof(*res_bundle)*vi->channels);
00598     //float **couple_bundle=alloca(sizeof(*couple_bundle)*vi->channels);
00599     //int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
00600     //int **sortindex=alloca(sizeof(*sortindex)*vi->channels);
00601     
00602     float **res_bundle=(float**)_ogg_malloc(sizeof(*res_bundle)*vi->channels);
00603     float **couple_bundle=(float**)_ogg_malloc(sizeof(*couple_bundle)*vi->channels);
00604     int *zerobundle=(int*)_ogg_malloc(sizeof(*zerobundle)*vi->channels);
00605     int **sortindex=(int**)_ogg_malloc(sizeof(*sortindex)*vi->channels);
00606     
00607     float **res_bundle_cpy = res_bundle;
00608     float **couple_bundle_cpy = couple_bundle;
00609     int *zerobundle_cpy = zerobundle;
00610     int **sortindex_cpy = sortindex;
00611     unsigned char sortindex_elements = 0;
00612     //-------------patch----
00613     
00614     
00615     float **mag_memo = NULL;
00616     int **mag_sort = NULL;
00617 
00618     if(info->coupling_steps){
00619       mag_memo=_vp_quantize_couple_memo(vb,
00620                                         &ci->psy_g_param,
00621                                         psy_look,
00622                                         info,
00623                                         gmdct);    
00624       
00625       mag_sort=_vp_quantize_couple_sort(vb,
00626                                         psy_look,
00627                                         info,
00628                                         mag_memo);    
00629 
00630       hf_reduction(&ci->psy_g_param,
00631                    psy_look,
00632                    info,
00633                    mag_memo);
00634     }
00635 
00636     memset(sortindex,0,sizeof(*sortindex)*vi->channels);
00637     if(psy_look->vi->normal_channel_p){
00638     sortindex_elements = vi->channels; //patch
00639       for(i=0;i<vi->channels;i++){
00640         float *mdct    =gmdct[i];
00641         sortindex[i]= (int*)_ogg_malloc(sizeof(**sortindex)*n/2);//alloca(sizeof(**sortindex)*n/2); //patch
00642         _vp_noise_normalize_sort(psy_look,mdct,sortindex[i]);
00643       }
00644     }
00645 
00646     for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2);
00647         k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2);
00648         k++){
00649       oggpack_buffer *opb=vbi->packetblob[k];
00650 
00651       /* start out our new packet blob with packet type and mode */
00652       /* Encode the packet type */
00653       oggpack_write(opb,0,1);
00654       /* Encode the modenumber */
00655       /* Encode frame mode, pre,post windowsize, then dispatch */
00656       oggpack_write(opb,modenumber,b->modebits);
00657       if(vb->W){
00658         oggpack_write(opb,vb->lW,1);
00659         oggpack_write(opb,vb->nW,1);
00660       }
00661 
00662       /* encode floor, compute masking curve, sep out residue */
00663       for(i=0;i<vi->channels;i++){
00664         int submap=info->chmuxlist[i];
00665         float *mdct    =gmdct[i];
00666         float *res     =vb->pcm[i];
00667         int   *ilogmask=ilogmaskch[i]=(int*)
00668           _vorbis_block_alloc(vb,n/2*sizeof(**gmdct));
00669       
00670         nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]],
00671                                  floor_posts[i][k],
00672                                  ilogmask);
00673 #if 0
00674         {
00675           char buf[80];
00676           sprintf(buf,"maskI%c%d",i?'R':'L',k);
00677           float work[n/2];
00678           for(j=0;j<n/2;j++)
00679             work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]];
00680           _analysis_output(buf,seq,work,n/2,1,1,0);
00681         }
00682 #endif
00683         _vp_remove_floor(psy_look,
00684                          mdct,
00685                          ilogmask,
00686                          res,
00687                          ci->psy_g_param.sliding_lowpass[vb->W][k]);
00688 
00689         _vp_noise_normalize(psy_look,res,res+n/2,sortindex[i]);
00690 
00691         
00692 #if 0
00693         {
00694           char buf[80];
00695           float work[n/2];
00696           for(j=0;j<n/2;j++)
00697             work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]]*(res+n/2)[j];
00698           sprintf(buf,"resI%c%d",i?'R':'L',k);
00699           _analysis_output(buf,seq,work,n/2,1,1,0);
00700 
00701         }
00702 #endif
00703       }
00704       
00705       /* our iteration is now based on masking curve, not prequant and
00706          coupling.  Only one prequant/coupling step */
00707       
00708       /* quantize/couple */
00709       /* incomplete implementation that assumes the tree is all depth
00710          one, or no tree at all */
00711       if(info->coupling_steps){
00712         _vp_couple(k,
00713                    &ci->psy_g_param,
00714                    psy_look,
00715                    info,
00716                    vb->pcm,
00717                    mag_memo,
00718                    mag_sort,
00719                    ilogmaskch,
00720                    nonzero,
00721                    ci->psy_g_param.sliding_lowpass[vb->W][k]);
00722       }
00723       
00724       /* classify and encode by submap */
00725       for(i=0;i<info->submaps;i++){
00726         int ch_in_bundle=0;
00727         long **classifications;
00728         int resnum=info->residuesubmap[i];
00729 
00730         for(j=0;j<vi->channels;j++){
00731           if(info->chmuxlist[j]==i){
00732             zerobundle[ch_in_bundle]=0;
00733             if(nonzero[j])zerobundle[ch_in_bundle]=1;
00734             res_bundle[ch_in_bundle]=vb->pcm[j];
00735             couple_bundle[ch_in_bundle++]=vb->pcm[j]+n/2;
00736           }
00737         }
00738         
00739         classifications=_residue_P[ci->residue_type[resnum]]->
00740           fpclass(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle);
00741         
00742         _residue_P[ci->residue_type[resnum]]->
00743           forward(opb,vb,b->residue[resnum],
00744                   couple_bundle,NULL,zerobundle,ch_in_bundle,classifications);
00745       }
00746       
00747       /* ok, done encoding.  Next protopacket. */
00748     }
00749         
00750         //-----patch
00751         free(res_bundle_cpy);
00752         free(couple_bundle_cpy);
00753         free(zerobundle_cpy); 
00754         
00755         for(i=0; i < sortindex_elements; i++){
00756                 free(sortindex[i]);             
00757         }
00758         free(sortindex_cpy);   
00759         //--------patch
00760   }
00761 
00762 #if 0
00763   seq++;
00764   total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;
00765 #endif
00766   free(nonzero_cpy); //patch
00767   free(local_ampmax_cpy); //patch
00768   return(0);
00769 }
00770 
00771 static int mapping0_inverse(vorbis_block *vb,vorbis_info_mapping *l){
00772   vorbis_dsp_state     *vd=vb->vd;
00773   vorbis_info          *vi=vd->vi;
00774   codec_setup_info     *ci=(codec_setup_info*)vi->codec_setup;
00775   private_state        *b=(private_state*)vd->backend_state;
00776   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)l;
00777   //int hs=ci->halfrate_flag; 
00778 
00779   int                   i,j;
00780   long                  n=vb->pcmend=ci->blocksizes[vb->W];
00781 
00782   //---------patch      
00783   //float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
00784   //int    *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
00785 
00786   //int   *nonzero  =alloca(sizeof(*nonzero)*vi->channels);
00787   //void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
00788   
00789   float **pcmbundle=(float**)_ogg_malloc(sizeof(*pcmbundle)*vi->channels);
00790   int    *zerobundle=(int*)_ogg_malloc(sizeof(*zerobundle)*vi->channels);
00791 
00792   int   *nonzero  =(int*)_ogg_malloc(sizeof(*nonzero)*vi->channels);
00793   void **floormemo=(void**)_ogg_malloc(sizeof(*floormemo)*vi->channels);
00794   
00795   float **pcmbundle_cpy =pcmbundle;
00796   int    *zerobundle_cpy = zerobundle;
00797   int   *nonzero_cpy = nonzero;
00798   void **floormemo_cpy = floormemo;
00799   //check for double pointer free //caution!
00800   //----------patch
00801   
00802   /* recover the spectral envelope; store it in the PCM vector for now */
00803   for(i=0;i<vi->channels;i++){
00804     int submap=info->chmuxlist[i];
00805     floormemo[i]=_floor_P[ci->floor_type[info->floorsubmap[submap]]]->
00806       inverse1(vb,b->flr[info->floorsubmap[submap]]);
00807     if(floormemo[i])
00808       nonzero[i]=1;
00809     else
00810       nonzero[i]=0;      
00811     memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
00812   }
00813 
00814   /* channel coupling can 'dirty' the nonzero listing */
00815   for(i=0;i<info->coupling_steps;i++){
00816     if(nonzero[info->coupling_mag[i]] ||
00817        nonzero[info->coupling_ang[i]]){
00818       nonzero[info->coupling_mag[i]]=1; 
00819       nonzero[info->coupling_ang[i]]=1; 
00820     }
00821   }
00822 
00823   /* recover the residue into our working vectors */
00824   for(i=0;i<info->submaps;i++){
00825     int ch_in_bundle=0;
00826     for(j=0;j<vi->channels;j++){
00827       if(info->chmuxlist[j]==i){
00828         if(nonzero[j])
00829           zerobundle[ch_in_bundle]=1;
00830         else
00831           zerobundle[ch_in_bundle]=0;
00832         pcmbundle[ch_in_bundle++]=vb->pcm[j];
00833       }
00834     }
00835 
00836     _residue_P[ci->residue_type[info->residuesubmap[i]]]->
00837       inverse(vb,b->residue[info->residuesubmap[i]],
00838               pcmbundle,zerobundle,ch_in_bundle);
00839   }
00840 
00841   /* channel coupling */
00842   for(i=info->coupling_steps-1;i>=0;i--){
00843     float *pcmM=vb->pcm[info->coupling_mag[i]];
00844     float *pcmA=vb->pcm[info->coupling_ang[i]];
00845 
00846     for(j=0;j<n/2;j++){
00847       float mag=pcmM[j];
00848       float ang=pcmA[j];
00849 
00850       if(mag>0)
00851         if(ang>0){
00852           pcmM[j]=mag;
00853           pcmA[j]=mag-ang;
00854         }else{
00855           pcmA[j]=mag;
00856           pcmM[j]=mag+ang;
00857         }
00858       else
00859         if(ang>0){
00860           pcmM[j]=mag;
00861           pcmA[j]=mag+ang;
00862         }else{
00863           pcmA[j]=mag;
00864           pcmM[j]=mag-ang;
00865         }
00866     }
00867   }
00868 
00869   /* compute and apply spectral envelope */
00870   for(i=0;i<vi->channels;i++){
00871     float *pcm=vb->pcm[i];
00872     int submap=info->chmuxlist[i];
00873     _floor_P[ci->floor_type[info->floorsubmap[submap]]]->
00874       inverse2(vb,b->flr[info->floorsubmap[submap]],
00875                floormemo[i],pcm);
00876   }
00877 
00878   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
00879   /* only MDCT right now.... */
00880   for(i=0;i<vi->channels;i++){
00881     float *pcm=vb->pcm[i];
00882     mdct_backward((mdct_lookup*)b->transform[vb->W][0],pcm,pcm);
00883   }
00884 
00885   //patch---------
00886   free(pcmbundle_cpy);
00887   free(zerobundle_cpy);
00888   free(nonzero_cpy);
00889   free(floormemo_cpy);
00890   //patch---------
00891   
00892   /* all done! */         
00893   return(0);
00894 }
00895  
00896 /* export hooks */
00897 vorbis_func_mapping mapping0_exportbundle={
00898   &mapping0_pack,
00899   &mapping0_unpack,
00900   &mapping0_free_info,
00901   &mapping0_forward,
00902   &mapping0_inverse
00903 };
00904 

Generated by  doxygen 1.6.2