vrshoot

diff libs/vorbis/mapping0.c @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vorbis/mapping0.c	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,816 @@
     1.4 +/********************************************************************
     1.5 + *                                                                  *
     1.6 + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
     1.7 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     1.8 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     1.9 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    1.10 + *                                                                  *
    1.11 + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010             *
    1.12 + * by the Xiph.Org Foundation http://www.xiph.org/                  *
    1.13 + *                                                                  *
    1.14 + ********************************************************************
    1.15 +
    1.16 + function: channel mapping 0 implementation
    1.17 + last mod: $Id: mapping0.c 17022 2010-03-25 03:45:42Z xiphmont $
    1.18 +
    1.19 + ********************************************************************/
    1.20 +
    1.21 +#include <stdlib.h>
    1.22 +#include <stdio.h>
    1.23 +#include <string.h>
    1.24 +#include <math.h>
    1.25 +#include <ogg/ogg.h>
    1.26 +#include "vorbis/codec.h"
    1.27 +#include "codec_internal.h"
    1.28 +#include "codebook.h"
    1.29 +#include "window.h"
    1.30 +#include "registry.h"
    1.31 +#include "psy.h"
    1.32 +#include "misc.h"
    1.33 +
    1.34 +/* simplistic, wasteful way of doing this (unique lookup for each
    1.35 +   mode/submapping); there should be a central repository for
    1.36 +   identical lookups.  That will require minor work, so I'm putting it
    1.37 +   off as low priority.
    1.38 +
    1.39 +   Why a lookup for each backend in a given mode?  Because the
    1.40 +   blocksize is set by the mode, and low backend lookups may require
    1.41 +   parameters from other areas of the mode/mapping */
    1.42 +
    1.43 +static void mapping0_free_info(vorbis_info_mapping *i){
    1.44 +  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
    1.45 +  if(info){
    1.46 +    memset(info,0,sizeof(*info));
    1.47 +    _ogg_free(info);
    1.48 +  }
    1.49 +}
    1.50 +
    1.51 +static int ilog(unsigned int v){
    1.52 +  int ret=0;
    1.53 +  if(v)--v;
    1.54 +  while(v){
    1.55 +    ret++;
    1.56 +    v>>=1;
    1.57 +  }
    1.58 +  return(ret);
    1.59 +}
    1.60 +
    1.61 +static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
    1.62 +                          oggpack_buffer *opb){
    1.63 +  int i;
    1.64 +  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
    1.65 +
    1.66 +  /* another 'we meant to do it this way' hack...  up to beta 4, we
    1.67 +     packed 4 binary zeros here to signify one submapping in use.  We
    1.68 +     now redefine that to mean four bitflags that indicate use of
    1.69 +     deeper features; bit0:submappings, bit1:coupling,
    1.70 +     bit2,3:reserved. This is backward compatable with all actual uses
    1.71 +     of the beta code. */
    1.72 +
    1.73 +  if(info->submaps>1){
    1.74 +    oggpack_write(opb,1,1);
    1.75 +    oggpack_write(opb,info->submaps-1,4);
    1.76 +  }else
    1.77 +    oggpack_write(opb,0,1);
    1.78 +
    1.79 +  if(info->coupling_steps>0){
    1.80 +    oggpack_write(opb,1,1);
    1.81 +    oggpack_write(opb,info->coupling_steps-1,8);
    1.82 +
    1.83 +    for(i=0;i<info->coupling_steps;i++){
    1.84 +      oggpack_write(opb,info->coupling_mag[i],ilog(vi->channels));
    1.85 +      oggpack_write(opb,info->coupling_ang[i],ilog(vi->channels));
    1.86 +    }
    1.87 +  }else
    1.88 +    oggpack_write(opb,0,1);
    1.89 +
    1.90 +  oggpack_write(opb,0,2); /* 2,3:reserved */
    1.91 +
    1.92 +  /* we don't write the channel submappings if we only have one... */
    1.93 +  if(info->submaps>1){
    1.94 +    for(i=0;i<vi->channels;i++)
    1.95 +      oggpack_write(opb,info->chmuxlist[i],4);
    1.96 +  }
    1.97 +  for(i=0;i<info->submaps;i++){
    1.98 +    oggpack_write(opb,0,8); /* time submap unused */
    1.99 +    oggpack_write(opb,info->floorsubmap[i],8);
   1.100 +    oggpack_write(opb,info->residuesubmap[i],8);
   1.101 +  }
   1.102 +}
   1.103 +
   1.104 +/* also responsible for range checking */
   1.105 +static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
   1.106 +  int i,b;
   1.107 +  vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
   1.108 +  codec_setup_info     *ci=vi->codec_setup;
   1.109 +  memset(info,0,sizeof(*info));
   1.110 +
   1.111 +  b=oggpack_read(opb,1);
   1.112 +  if(b<0)goto err_out;
   1.113 +  if(b){
   1.114 +    info->submaps=oggpack_read(opb,4)+1;
   1.115 +    if(info->submaps<=0)goto err_out;
   1.116 +  }else
   1.117 +    info->submaps=1;
   1.118 +
   1.119 +  b=oggpack_read(opb,1);
   1.120 +  if(b<0)goto err_out;
   1.121 +  if(b){
   1.122 +    info->coupling_steps=oggpack_read(opb,8)+1;
   1.123 +    if(info->coupling_steps<=0)goto err_out;
   1.124 +    for(i=0;i<info->coupling_steps;i++){
   1.125 +      int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
   1.126 +      int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
   1.127 +
   1.128 +      if(testM<0 ||
   1.129 +         testA<0 ||
   1.130 +         testM==testA ||
   1.131 +         testM>=vi->channels ||
   1.132 +         testA>=vi->channels) goto err_out;
   1.133 +    }
   1.134 +
   1.135 +  }
   1.136 +
   1.137 +  if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */
   1.138 +
   1.139 +  if(info->submaps>1){
   1.140 +    for(i=0;i<vi->channels;i++){
   1.141 +      info->chmuxlist[i]=oggpack_read(opb,4);
   1.142 +      if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out;
   1.143 +    }
   1.144 +  }
   1.145 +  for(i=0;i<info->submaps;i++){
   1.146 +    oggpack_read(opb,8); /* time submap unused */
   1.147 +    info->floorsubmap[i]=oggpack_read(opb,8);
   1.148 +    if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out;
   1.149 +    info->residuesubmap[i]=oggpack_read(opb,8);
   1.150 +    if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)goto err_out;
   1.151 +  }
   1.152 +
   1.153 +  return info;
   1.154 +
   1.155 + err_out:
   1.156 +  mapping0_free_info(info);
   1.157 +  return(NULL);
   1.158 +}
   1.159 +
   1.160 +#include "os.h"
   1.161 +#include "lpc.h"
   1.162 +#include "lsp.h"
   1.163 +#include "envelope.h"
   1.164 +#include "mdct.h"
   1.165 +#include "psy.h"
   1.166 +#include "scales.h"
   1.167 +
   1.168 +#if 0
   1.169 +static long seq=0;
   1.170 +static ogg_int64_t total=0;
   1.171 +static float FLOOR1_fromdB_LOOKUP[256]={
   1.172 +  1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
   1.173 +  1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
   1.174 +  1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
   1.175 +  2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
   1.176 +  2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
   1.177 +  3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
   1.178 +  4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
   1.179 +  6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
   1.180 +  7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
   1.181 +  1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
   1.182 +  1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
   1.183 +  1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
   1.184 +  2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
   1.185 +  2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
   1.186 +  3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
   1.187 +  4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
   1.188 +  5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
   1.189 +  7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
   1.190 +  9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
   1.191 +  1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
   1.192 +  1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
   1.193 +  2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
   1.194 +  2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
   1.195 +  3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
   1.196 +  4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
   1.197 +  5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
   1.198 +  7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
   1.199 +  9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
   1.200 +  0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
   1.201 +  0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
   1.202 +  0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
   1.203 +  0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
   1.204 +  0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
   1.205 +  0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
   1.206 +  0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
   1.207 +  0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
   1.208 +  0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
   1.209 +  0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
   1.210 +  0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
   1.211 +  0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
   1.212 +  0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
   1.213 +  0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
   1.214 +  0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
   1.215 +  0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
   1.216 +  0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
   1.217 +  0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
   1.218 +  0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
   1.219 +  0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
   1.220 +  0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
   1.221 +  0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
   1.222 +  0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
   1.223 +  0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
   1.224 +  0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
   1.225 +  0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
   1.226 +  0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
   1.227 +  0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
   1.228 +  0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
   1.229 +  0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
   1.230 +  0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
   1.231 +  0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
   1.232 +  0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
   1.233 +  0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
   1.234 +  0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
   1.235 +  0.82788260F, 0.88168307F, 0.9389798F, 1.F,
   1.236 +};
   1.237 +
   1.238 +#endif
   1.239 +
   1.240 +
   1.241 +static int mapping0_forward(vorbis_block *vb){
   1.242 +  vorbis_dsp_state      *vd=vb->vd;
   1.243 +  vorbis_info           *vi=vd->vi;
   1.244 +  codec_setup_info      *ci=vi->codec_setup;
   1.245 +  private_state         *b=vb->vd->backend_state;
   1.246 +  vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
   1.247 +  int                    n=vb->pcmend;
   1.248 +  int i,j,k;
   1.249 +
   1.250 +  int    *nonzero    = alloca(sizeof(*nonzero)*vi->channels);
   1.251 +  float  **gmdct     = _vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct));
   1.252 +  int    **iwork      = _vorbis_block_alloc(vb,vi->channels*sizeof(*iwork));
   1.253 +  int ***floor_posts = _vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts));
   1.254 +
   1.255 +  float global_ampmax=vbi->ampmax;
   1.256 +  float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
   1.257 +  int blocktype=vbi->blocktype;
   1.258 +
   1.259 +  int modenumber=vb->W;
   1.260 +  vorbis_info_mapping0 *info=ci->map_param[modenumber];
   1.261 +  vorbis_look_psy *psy_look=b->psy+blocktype+(vb->W?2:0);
   1.262 +
   1.263 +  vb->mode=modenumber;
   1.264 +
   1.265 +  for(i=0;i<vi->channels;i++){
   1.266 +    float scale=4.f/n;
   1.267 +    float scale_dB;
   1.268 +
   1.269 +    float *pcm     =vb->pcm[i];
   1.270 +    float *logfft  =pcm;
   1.271 +
   1.272 +    iwork[i]=_vorbis_block_alloc(vb,n/2*sizeof(**iwork));
   1.273 +    gmdct[i]=_vorbis_block_alloc(vb,n/2*sizeof(**gmdct));
   1.274 +
   1.275 +    scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original
   1.276 +                                     todB estimation used on IEEE 754
   1.277 +                                     compliant machines had a bug that
   1.278 +                                     returned dB values about a third
   1.279 +                                     of a decibel too high.  The bug
   1.280 +                                     was harmless because tunings
   1.281 +                                     implicitly took that into
   1.282 +                                     account.  However, fixing the bug
   1.283 +                                     in the estimator requires
   1.284 +                                     changing all the tunings as well.
   1.285 +                                     For now, it's easier to sync
   1.286 +                                     things back up here, and
   1.287 +                                     recalibrate the tunings in the
   1.288 +                                     next major model upgrade. */
   1.289 +
   1.290 +#if 0
   1.291 +    if(vi->channels==2){
   1.292 +      if(i==0)
   1.293 +        _analysis_output("pcmL",seq,pcm,n,0,0,total-n/2);
   1.294 +      else
   1.295 +        _analysis_output("pcmR",seq,pcm,n,0,0,total-n/2);
   1.296 +    }else{
   1.297 +      _analysis_output("pcm",seq,pcm,n,0,0,total-n/2);
   1.298 +    }
   1.299 +#endif
   1.300 +
   1.301 +    /* window the PCM data */
   1.302 +    _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
   1.303 +
   1.304 +#if 0
   1.305 +    if(vi->channels==2){
   1.306 +      if(i==0)
   1.307 +        _analysis_output("windowedL",seq,pcm,n,0,0,total-n/2);
   1.308 +      else
   1.309 +        _analysis_output("windowedR",seq,pcm,n,0,0,total-n/2);
   1.310 +    }else{
   1.311 +      _analysis_output("windowed",seq,pcm,n,0,0,total-n/2);
   1.312 +    }
   1.313 +#endif
   1.314 +
   1.315 +    /* transform the PCM data */
   1.316 +    /* only MDCT right now.... */
   1.317 +    mdct_forward(b->transform[vb->W][0],pcm,gmdct[i]);
   1.318 +
   1.319 +    /* FFT yields more accurate tonal estimation (not phase sensitive) */
   1.320 +    drft_forward(&b->fft_look[vb->W],pcm);
   1.321 +    logfft[0]=scale_dB+todB(pcm)  + .345; /* + .345 is a hack; the
   1.322 +                                     original todB estimation used on
   1.323 +                                     IEEE 754 compliant machines had a
   1.324 +                                     bug that returned dB values about
   1.325 +                                     a third of a decibel too high.
   1.326 +                                     The bug was harmless because
   1.327 +                                     tunings implicitly took that into
   1.328 +                                     account.  However, fixing the bug
   1.329 +                                     in the estimator requires
   1.330 +                                     changing all the tunings as well.
   1.331 +                                     For now, it's easier to sync
   1.332 +                                     things back up here, and
   1.333 +                                     recalibrate the tunings in the
   1.334 +                                     next major model upgrade. */
   1.335 +    local_ampmax[i]=logfft[0];
   1.336 +    for(j=1;j<n-1;j+=2){
   1.337 +      float temp=pcm[j]*pcm[j]+pcm[j+1]*pcm[j+1];
   1.338 +      temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp)  + .345; /* +
   1.339 +                                     .345 is a hack; the original todB
   1.340 +                                     estimation used on IEEE 754
   1.341 +                                     compliant machines had a bug that
   1.342 +                                     returned dB values about a third
   1.343 +                                     of a decibel too high.  The bug
   1.344 +                                     was harmless because tunings
   1.345 +                                     implicitly took that into
   1.346 +                                     account.  However, fixing the bug
   1.347 +                                     in the estimator requires
   1.348 +                                     changing all the tunings as well.
   1.349 +                                     For now, it's easier to sync
   1.350 +                                     things back up here, and
   1.351 +                                     recalibrate the tunings in the
   1.352 +                                     next major model upgrade. */
   1.353 +      if(temp>local_ampmax[i])local_ampmax[i]=temp;
   1.354 +    }
   1.355 +
   1.356 +    if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
   1.357 +    if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
   1.358 +
   1.359 +#if 0
   1.360 +    if(vi->channels==2){
   1.361 +      if(i==0){
   1.362 +        _analysis_output("fftL",seq,logfft,n/2,1,0,0);
   1.363 +      }else{
   1.364 +        _analysis_output("fftR",seq,logfft,n/2,1,0,0);
   1.365 +      }
   1.366 +    }else{
   1.367 +      _analysis_output("fft",seq,logfft,n/2,1,0,0);
   1.368 +    }
   1.369 +#endif
   1.370 +
   1.371 +  }
   1.372 +
   1.373 +  {
   1.374 +    float   *noise        = _vorbis_block_alloc(vb,n/2*sizeof(*noise));
   1.375 +    float   *tone         = _vorbis_block_alloc(vb,n/2*sizeof(*tone));
   1.376 +
   1.377 +    for(i=0;i<vi->channels;i++){
   1.378 +      /* the encoder setup assumes that all the modes used by any
   1.379 +         specific bitrate tweaking use the same floor */
   1.380 +
   1.381 +      int submap=info->chmuxlist[i];
   1.382 +
   1.383 +      /* the following makes things clearer to *me* anyway */
   1.384 +      float *mdct    =gmdct[i];
   1.385 +      float *logfft  =vb->pcm[i];
   1.386 +
   1.387 +      float *logmdct =logfft+n/2;
   1.388 +      float *logmask =logfft;
   1.389 +
   1.390 +      vb->mode=modenumber;
   1.391 +
   1.392 +      floor_posts[i]=_vorbis_block_alloc(vb,PACKETBLOBS*sizeof(**floor_posts));
   1.393 +      memset(floor_posts[i],0,sizeof(**floor_posts)*PACKETBLOBS);
   1.394 +
   1.395 +      for(j=0;j<n/2;j++)
   1.396 +        logmdct[j]=todB(mdct+j)  + .345; /* + .345 is a hack; the original
   1.397 +                                     todB estimation used on IEEE 754
   1.398 +                                     compliant machines had a bug that
   1.399 +                                     returned dB values about a third
   1.400 +                                     of a decibel too high.  The bug
   1.401 +                                     was harmless because tunings
   1.402 +                                     implicitly took that into
   1.403 +                                     account.  However, fixing the bug
   1.404 +                                     in the estimator requires
   1.405 +                                     changing all the tunings as well.
   1.406 +                                     For now, it's easier to sync
   1.407 +                                     things back up here, and
   1.408 +                                     recalibrate the tunings in the
   1.409 +                                     next major model upgrade. */
   1.410 +
   1.411 +#if 0
   1.412 +      if(vi->channels==2){
   1.413 +        if(i==0)
   1.414 +          _analysis_output("mdctL",seq,logmdct,n/2,1,0,0);
   1.415 +        else
   1.416 +          _analysis_output("mdctR",seq,logmdct,n/2,1,0,0);
   1.417 +      }else{
   1.418 +        _analysis_output("mdct",seq,logmdct,n/2,1,0,0);
   1.419 +      }
   1.420 +#endif
   1.421 +
   1.422 +      /* first step; noise masking.  Not only does 'noise masking'
   1.423 +         give us curves from which we can decide how much resolution
   1.424 +         to give noise parts of the spectrum, it also implicitly hands
   1.425 +         us a tonality estimate (the larger the value in the
   1.426 +         'noise_depth' vector, the more tonal that area is) */
   1.427 +
   1.428 +      _vp_noisemask(psy_look,
   1.429 +                    logmdct,
   1.430 +                    noise); /* noise does not have by-frequency offset
   1.431 +                               bias applied yet */
   1.432 +#if 0
   1.433 +      if(vi->channels==2){
   1.434 +        if(i==0)
   1.435 +          _analysis_output("noiseL",seq,noise,n/2,1,0,0);
   1.436 +        else
   1.437 +          _analysis_output("noiseR",seq,noise,n/2,1,0,0);
   1.438 +      }else{
   1.439 +        _analysis_output("noise",seq,noise,n/2,1,0,0);
   1.440 +      }
   1.441 +#endif
   1.442 +
   1.443 +      /* second step: 'all the other crap'; all the stuff that isn't
   1.444 +         computed/fit for bitrate management goes in the second psy
   1.445 +         vector.  This includes tone masking, peak limiting and ATH */
   1.446 +
   1.447 +      _vp_tonemask(psy_look,
   1.448 +                   logfft,
   1.449 +                   tone,
   1.450 +                   global_ampmax,
   1.451 +                   local_ampmax[i]);
   1.452 +
   1.453 +#if 0
   1.454 +      if(vi->channels==2){
   1.455 +        if(i==0)
   1.456 +          _analysis_output("toneL",seq,tone,n/2,1,0,0);
   1.457 +        else
   1.458 +          _analysis_output("toneR",seq,tone,n/2,1,0,0);
   1.459 +      }else{
   1.460 +        _analysis_output("tone",seq,tone,n/2,1,0,0);
   1.461 +      }
   1.462 +#endif
   1.463 +
   1.464 +      /* third step; we offset the noise vectors, overlay tone
   1.465 +         masking.  We then do a floor1-specific line fit.  If we're
   1.466 +         performing bitrate management, the line fit is performed
   1.467 +         multiple times for up/down tweakage on demand. */
   1.468 +
   1.469 +#if 0
   1.470 +      {
   1.471 +      float aotuv[psy_look->n];
   1.472 +#endif
   1.473 +
   1.474 +        _vp_offset_and_mix(psy_look,
   1.475 +                           noise,
   1.476 +                           tone,
   1.477 +                           1,
   1.478 +                           logmask,
   1.479 +                           mdct,
   1.480 +                           logmdct);
   1.481 +
   1.482 +#if 0
   1.483 +        if(vi->channels==2){
   1.484 +          if(i==0)
   1.485 +            _analysis_output("aotuvM1_L",seq,aotuv,psy_look->n,1,1,0);
   1.486 +          else
   1.487 +            _analysis_output("aotuvM1_R",seq,aotuv,psy_look->n,1,1,0);
   1.488 +        }else{
   1.489 +          _analysis_output("aotuvM1",seq,aotuv,psy_look->n,1,1,0);
   1.490 +        }
   1.491 +      }
   1.492 +#endif
   1.493 +
   1.494 +
   1.495 +#if 0
   1.496 +      if(vi->channels==2){
   1.497 +        if(i==0)
   1.498 +          _analysis_output("mask1L",seq,logmask,n/2,1,0,0);
   1.499 +        else
   1.500 +          _analysis_output("mask1R",seq,logmask,n/2,1,0,0);
   1.501 +      }else{
   1.502 +        _analysis_output("mask1",seq,logmask,n/2,1,0,0);
   1.503 +      }
   1.504 +#endif
   1.505 +
   1.506 +      /* this algorithm is hardwired to floor 1 for now; abort out if
   1.507 +         we're *not* floor1.  This won't happen unless someone has
   1.508 +         broken the encode setup lib.  Guard it anyway. */
   1.509 +      if(ci->floor_type[info->floorsubmap[submap]]!=1)return(-1);
   1.510 +
   1.511 +      floor_posts[i][PACKETBLOBS/2]=
   1.512 +        floor1_fit(vb,b->flr[info->floorsubmap[submap]],
   1.513 +                   logmdct,
   1.514 +                   logmask);
   1.515 +
   1.516 +      /* are we managing bitrate?  If so, perform two more fits for
   1.517 +         later rate tweaking (fits represent hi/lo) */
   1.518 +      if(vorbis_bitrate_managed(vb) && floor_posts[i][PACKETBLOBS/2]){
   1.519 +        /* higher rate by way of lower noise curve */
   1.520 +
   1.521 +        _vp_offset_and_mix(psy_look,
   1.522 +                           noise,
   1.523 +                           tone,
   1.524 +                           2,
   1.525 +                           logmask,
   1.526 +                           mdct,
   1.527 +                           logmdct);
   1.528 +
   1.529 +#if 0
   1.530 +        if(vi->channels==2){
   1.531 +          if(i==0)
   1.532 +            _analysis_output("mask2L",seq,logmask,n/2,1,0,0);
   1.533 +          else
   1.534 +            _analysis_output("mask2R",seq,logmask,n/2,1,0,0);
   1.535 +        }else{
   1.536 +          _analysis_output("mask2",seq,logmask,n/2,1,0,0);
   1.537 +        }
   1.538 +#endif
   1.539 +
   1.540 +        floor_posts[i][PACKETBLOBS-1]=
   1.541 +          floor1_fit(vb,b->flr[info->floorsubmap[submap]],
   1.542 +                     logmdct,
   1.543 +                     logmask);
   1.544 +
   1.545 +        /* lower rate by way of higher noise curve */
   1.546 +        _vp_offset_and_mix(psy_look,
   1.547 +                           noise,
   1.548 +                           tone,
   1.549 +                           0,
   1.550 +                           logmask,
   1.551 +                           mdct,
   1.552 +                           logmdct);
   1.553 +
   1.554 +#if 0
   1.555 +        if(vi->channels==2){
   1.556 +          if(i==0)
   1.557 +            _analysis_output("mask0L",seq,logmask,n/2,1,0,0);
   1.558 +          else
   1.559 +            _analysis_output("mask0R",seq,logmask,n/2,1,0,0);
   1.560 +        }else{
   1.561 +          _analysis_output("mask0",seq,logmask,n/2,1,0,0);
   1.562 +        }
   1.563 +#endif
   1.564 +
   1.565 +        floor_posts[i][0]=
   1.566 +          floor1_fit(vb,b->flr[info->floorsubmap[submap]],
   1.567 +                     logmdct,
   1.568 +                     logmask);
   1.569 +
   1.570 +        /* we also interpolate a range of intermediate curves for
   1.571 +           intermediate rates */
   1.572 +        for(k=1;k<PACKETBLOBS/2;k++)
   1.573 +          floor_posts[i][k]=
   1.574 +            floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
   1.575 +                                   floor_posts[i][0],
   1.576 +                                   floor_posts[i][PACKETBLOBS/2],
   1.577 +                                   k*65536/(PACKETBLOBS/2));
   1.578 +        for(k=PACKETBLOBS/2+1;k<PACKETBLOBS-1;k++)
   1.579 +          floor_posts[i][k]=
   1.580 +            floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
   1.581 +                                   floor_posts[i][PACKETBLOBS/2],
   1.582 +                                   floor_posts[i][PACKETBLOBS-1],
   1.583 +                                   (k-PACKETBLOBS/2)*65536/(PACKETBLOBS/2));
   1.584 +      }
   1.585 +    }
   1.586 +  }
   1.587 +  vbi->ampmax=global_ampmax;
   1.588 +
   1.589 +  /*
   1.590 +    the next phases are performed once for vbr-only and PACKETBLOB
   1.591 +    times for bitrate managed modes.
   1.592 +
   1.593 +    1) encode actual mode being used
   1.594 +    2) encode the floor for each channel, compute coded mask curve/res
   1.595 +    3) normalize and couple.
   1.596 +    4) encode residue
   1.597 +    5) save packet bytes to the packetblob vector
   1.598 +
   1.599 +  */
   1.600 +
   1.601 +  /* iterate over the many masking curve fits we've created */
   1.602 +
   1.603 +  {
   1.604 +    int **couple_bundle=alloca(sizeof(*couple_bundle)*vi->channels);
   1.605 +    int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
   1.606 +
   1.607 +    for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2);
   1.608 +        k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2);
   1.609 +        k++){
   1.610 +      oggpack_buffer *opb=vbi->packetblob[k];
   1.611 +
   1.612 +      /* start out our new packet blob with packet type and mode */
   1.613 +      /* Encode the packet type */
   1.614 +      oggpack_write(opb,0,1);
   1.615 +      /* Encode the modenumber */
   1.616 +      /* Encode frame mode, pre,post windowsize, then dispatch */
   1.617 +      oggpack_write(opb,modenumber,b->modebits);
   1.618 +      if(vb->W){
   1.619 +        oggpack_write(opb,vb->lW,1);
   1.620 +        oggpack_write(opb,vb->nW,1);
   1.621 +      }
   1.622 +
   1.623 +      /* encode floor, compute masking curve, sep out residue */
   1.624 +      for(i=0;i<vi->channels;i++){
   1.625 +        int submap=info->chmuxlist[i];
   1.626 +        int *ilogmask=iwork[i];
   1.627 +
   1.628 +        nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]],
   1.629 +                                 floor_posts[i][k],
   1.630 +                                 ilogmask);
   1.631 +#if 0
   1.632 +        {
   1.633 +          char buf[80];
   1.634 +          sprintf(buf,"maskI%c%d",i?'R':'L',k);
   1.635 +          float work[n/2];
   1.636 +          for(j=0;j<n/2;j++)
   1.637 +            work[j]=FLOOR1_fromdB_LOOKUP[iwork[i][j]];
   1.638 +          _analysis_output(buf,seq,work,n/2,1,1,0);
   1.639 +        }
   1.640 +#endif
   1.641 +      }
   1.642 +
   1.643 +      /* our iteration is now based on masking curve, not prequant and
   1.644 +         coupling.  Only one prequant/coupling step */
   1.645 +
   1.646 +      /* quantize/couple */
   1.647 +      /* incomplete implementation that assumes the tree is all depth
   1.648 +         one, or no tree at all */
   1.649 +      _vp_couple_quantize_normalize(k,
   1.650 +                                    &ci->psy_g_param,
   1.651 +                                    psy_look,
   1.652 +                                    info,
   1.653 +                                    gmdct,
   1.654 +                                    iwork,
   1.655 +                                    nonzero,
   1.656 +                                    ci->psy_g_param.sliding_lowpass[vb->W][k],
   1.657 +                                    vi->channels);
   1.658 +
   1.659 +#if 0
   1.660 +      for(i=0;i<vi->channels;i++){
   1.661 +        char buf[80];
   1.662 +        sprintf(buf,"res%c%d",i?'R':'L',k);
   1.663 +        float work[n/2];
   1.664 +        for(j=0;j<n/2;j++)
   1.665 +          work[j]=iwork[i][j];
   1.666 +        _analysis_output(buf,seq,work,n/2,1,0,0);
   1.667 +      }
   1.668 +#endif
   1.669 +
   1.670 +      /* classify and encode by submap */
   1.671 +      for(i=0;i<info->submaps;i++){
   1.672 +        int ch_in_bundle=0;
   1.673 +        long **classifications;
   1.674 +        int resnum=info->residuesubmap[i];
   1.675 +
   1.676 +        for(j=0;j<vi->channels;j++){
   1.677 +          if(info->chmuxlist[j]==i){
   1.678 +            zerobundle[ch_in_bundle]=0;
   1.679 +            if(nonzero[j])zerobundle[ch_in_bundle]=1;
   1.680 +            couple_bundle[ch_in_bundle++]=iwork[j];
   1.681 +          }
   1.682 +        }
   1.683 +
   1.684 +        classifications=_residue_P[ci->residue_type[resnum]]->
   1.685 +          class(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle);
   1.686 +
   1.687 +        ch_in_bundle=0;
   1.688 +        for(j=0;j<vi->channels;j++)
   1.689 +          if(info->chmuxlist[j]==i)
   1.690 +            couple_bundle[ch_in_bundle++]=iwork[j];
   1.691 +
   1.692 +        _residue_P[ci->residue_type[resnum]]->
   1.693 +          forward(opb,vb,b->residue[resnum],
   1.694 +                  couple_bundle,zerobundle,ch_in_bundle,classifications,i);
   1.695 +      }
   1.696 +
   1.697 +      /* ok, done encoding.  Next protopacket. */
   1.698 +    }
   1.699 +
   1.700 +  }
   1.701 +
   1.702 +#if 0
   1.703 +  seq++;
   1.704 +  total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;
   1.705 +#endif
   1.706 +  return(0);
   1.707 +}
   1.708 +
   1.709 +static int mapping0_inverse(vorbis_block *vb,vorbis_info_mapping *l){
   1.710 +  vorbis_dsp_state     *vd=vb->vd;
   1.711 +  vorbis_info          *vi=vd->vi;
   1.712 +  codec_setup_info     *ci=vi->codec_setup;
   1.713 +  private_state        *b=vd->backend_state;
   1.714 +  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)l;
   1.715 +
   1.716 +  int                   i,j;
   1.717 +  long                  n=vb->pcmend=ci->blocksizes[vb->W];
   1.718 +
   1.719 +  float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
   1.720 +  int    *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
   1.721 +
   1.722 +  int   *nonzero  =alloca(sizeof(*nonzero)*vi->channels);
   1.723 +  void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
   1.724 +
   1.725 +  /* recover the spectral envelope; store it in the PCM vector for now */
   1.726 +  for(i=0;i<vi->channels;i++){
   1.727 +    int submap=info->chmuxlist[i];
   1.728 +    floormemo[i]=_floor_P[ci->floor_type[info->floorsubmap[submap]]]->
   1.729 +      inverse1(vb,b->flr[info->floorsubmap[submap]]);
   1.730 +    if(floormemo[i])
   1.731 +      nonzero[i]=1;
   1.732 +    else
   1.733 +      nonzero[i]=0;
   1.734 +    memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
   1.735 +  }
   1.736 +
   1.737 +  /* channel coupling can 'dirty' the nonzero listing */
   1.738 +  for(i=0;i<info->coupling_steps;i++){
   1.739 +    if(nonzero[info->coupling_mag[i]] ||
   1.740 +       nonzero[info->coupling_ang[i]]){
   1.741 +      nonzero[info->coupling_mag[i]]=1;
   1.742 +      nonzero[info->coupling_ang[i]]=1;
   1.743 +    }
   1.744 +  }
   1.745 +
   1.746 +  /* recover the residue into our working vectors */
   1.747 +  for(i=0;i<info->submaps;i++){
   1.748 +    int ch_in_bundle=0;
   1.749 +    for(j=0;j<vi->channels;j++){
   1.750 +      if(info->chmuxlist[j]==i){
   1.751 +        if(nonzero[j])
   1.752 +          zerobundle[ch_in_bundle]=1;
   1.753 +        else
   1.754 +          zerobundle[ch_in_bundle]=0;
   1.755 +        pcmbundle[ch_in_bundle++]=vb->pcm[j];
   1.756 +      }
   1.757 +    }
   1.758 +
   1.759 +    _residue_P[ci->residue_type[info->residuesubmap[i]]]->
   1.760 +      inverse(vb,b->residue[info->residuesubmap[i]],
   1.761 +              pcmbundle,zerobundle,ch_in_bundle);
   1.762 +  }
   1.763 +
   1.764 +  /* channel coupling */
   1.765 +  for(i=info->coupling_steps-1;i>=0;i--){
   1.766 +    float *pcmM=vb->pcm[info->coupling_mag[i]];
   1.767 +    float *pcmA=vb->pcm[info->coupling_ang[i]];
   1.768 +
   1.769 +    for(j=0;j<n/2;j++){
   1.770 +      float mag=pcmM[j];
   1.771 +      float ang=pcmA[j];
   1.772 +
   1.773 +      if(mag>0)
   1.774 +        if(ang>0){
   1.775 +          pcmM[j]=mag;
   1.776 +          pcmA[j]=mag-ang;
   1.777 +        }else{
   1.778 +          pcmA[j]=mag;
   1.779 +          pcmM[j]=mag+ang;
   1.780 +        }
   1.781 +      else
   1.782 +        if(ang>0){
   1.783 +          pcmM[j]=mag;
   1.784 +          pcmA[j]=mag+ang;
   1.785 +        }else{
   1.786 +          pcmA[j]=mag;
   1.787 +          pcmM[j]=mag-ang;
   1.788 +        }
   1.789 +    }
   1.790 +  }
   1.791 +
   1.792 +  /* compute and apply spectral envelope */
   1.793 +  for(i=0;i<vi->channels;i++){
   1.794 +    float *pcm=vb->pcm[i];
   1.795 +    int submap=info->chmuxlist[i];
   1.796 +    _floor_P[ci->floor_type[info->floorsubmap[submap]]]->
   1.797 +      inverse2(vb,b->flr[info->floorsubmap[submap]],
   1.798 +               floormemo[i],pcm);
   1.799 +  }
   1.800 +
   1.801 +  /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
   1.802 +  /* only MDCT right now.... */
   1.803 +  for(i=0;i<vi->channels;i++){
   1.804 +    float *pcm=vb->pcm[i];
   1.805 +    mdct_backward(b->transform[vb->W][0],pcm,pcm);
   1.806 +  }
   1.807 +
   1.808 +  /* all done! */
   1.809 +  return(0);
   1.810 +}
   1.811 +
   1.812 +/* export hooks */
   1.813 +const vorbis_func_mapping mapping0_exportbundle={
   1.814 +  &mapping0_pack,
   1.815 +  &mapping0_unpack,
   1.816 +  &mapping0_free_info,
   1.817 +  &mapping0_forward,
   1.818 +  &mapping0_inverse
   1.819 +};