annotate src/libvorbis-1.3.3/lib/mapping0.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 98c1576536ae
children
rev   line source
cannam@86 1 /********************************************************************
cannam@86 2 * *
cannam@86 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
cannam@86 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
cannam@86 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
cannam@86 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
cannam@86 7 * *
cannam@86 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
cannam@86 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
cannam@86 10 * *
cannam@86 11 ********************************************************************
cannam@86 12
cannam@86 13 function: channel mapping 0 implementation
cannam@86 14 last mod: $Id: mapping0.c 17022 2010-03-25 03:45:42Z xiphmont $
cannam@86 15
cannam@86 16 ********************************************************************/
cannam@86 17
cannam@86 18 #include <stdlib.h>
cannam@86 19 #include <stdio.h>
cannam@86 20 #include <string.h>
cannam@86 21 #include <math.h>
cannam@86 22 #include <ogg/ogg.h>
cannam@86 23 #include "vorbis/codec.h"
cannam@86 24 #include "codec_internal.h"
cannam@86 25 #include "codebook.h"
cannam@86 26 #include "window.h"
cannam@86 27 #include "registry.h"
cannam@86 28 #include "psy.h"
cannam@86 29 #include "misc.h"
cannam@86 30
cannam@86 31 /* simplistic, wasteful way of doing this (unique lookup for each
cannam@86 32 mode/submapping); there should be a central repository for
cannam@86 33 identical lookups. That will require minor work, so I'm putting it
cannam@86 34 off as low priority.
cannam@86 35
cannam@86 36 Why a lookup for each backend in a given mode? Because the
cannam@86 37 blocksize is set by the mode, and low backend lookups may require
cannam@86 38 parameters from other areas of the mode/mapping */
cannam@86 39
cannam@86 40 static void mapping0_free_info(vorbis_info_mapping *i){
cannam@86 41 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
cannam@86 42 if(info){
cannam@86 43 memset(info,0,sizeof(*info));
cannam@86 44 _ogg_free(info);
cannam@86 45 }
cannam@86 46 }
cannam@86 47
cannam@86 48 static int ilog(unsigned int v){
cannam@86 49 int ret=0;
cannam@86 50 if(v)--v;
cannam@86 51 while(v){
cannam@86 52 ret++;
cannam@86 53 v>>=1;
cannam@86 54 }
cannam@86 55 return(ret);
cannam@86 56 }
cannam@86 57
cannam@86 58 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
cannam@86 59 oggpack_buffer *opb){
cannam@86 60 int i;
cannam@86 61 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
cannam@86 62
cannam@86 63 /* another 'we meant to do it this way' hack... up to beta 4, we
cannam@86 64 packed 4 binary zeros here to signify one submapping in use. We
cannam@86 65 now redefine that to mean four bitflags that indicate use of
cannam@86 66 deeper features; bit0:submappings, bit1:coupling,
cannam@86 67 bit2,3:reserved. This is backward compatable with all actual uses
cannam@86 68 of the beta code. */
cannam@86 69
cannam@86 70 if(info->submaps>1){
cannam@86 71 oggpack_write(opb,1,1);
cannam@86 72 oggpack_write(opb,info->submaps-1,4);
cannam@86 73 }else
cannam@86 74 oggpack_write(opb,0,1);
cannam@86 75
cannam@86 76 if(info->coupling_steps>0){
cannam@86 77 oggpack_write(opb,1,1);
cannam@86 78 oggpack_write(opb,info->coupling_steps-1,8);
cannam@86 79
cannam@86 80 for(i=0;i<info->coupling_steps;i++){
cannam@86 81 oggpack_write(opb,info->coupling_mag[i],ilog(vi->channels));
cannam@86 82 oggpack_write(opb,info->coupling_ang[i],ilog(vi->channels));
cannam@86 83 }
cannam@86 84 }else
cannam@86 85 oggpack_write(opb,0,1);
cannam@86 86
cannam@86 87 oggpack_write(opb,0,2); /* 2,3:reserved */
cannam@86 88
cannam@86 89 /* we don't write the channel submappings if we only have one... */
cannam@86 90 if(info->submaps>1){
cannam@86 91 for(i=0;i<vi->channels;i++)
cannam@86 92 oggpack_write(opb,info->chmuxlist[i],4);
cannam@86 93 }
cannam@86 94 for(i=0;i<info->submaps;i++){
cannam@86 95 oggpack_write(opb,0,8); /* time submap unused */
cannam@86 96 oggpack_write(opb,info->floorsubmap[i],8);
cannam@86 97 oggpack_write(opb,info->residuesubmap[i],8);
cannam@86 98 }
cannam@86 99 }
cannam@86 100
cannam@86 101 /* also responsible for range checking */
cannam@86 102 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
cannam@86 103 int i,b;
cannam@86 104 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
cannam@86 105 codec_setup_info *ci=vi->codec_setup;
cannam@86 106 memset(info,0,sizeof(*info));
cannam@86 107
cannam@86 108 b=oggpack_read(opb,1);
cannam@86 109 if(b<0)goto err_out;
cannam@86 110 if(b){
cannam@86 111 info->submaps=oggpack_read(opb,4)+1;
cannam@86 112 if(info->submaps<=0)goto err_out;
cannam@86 113 }else
cannam@86 114 info->submaps=1;
cannam@86 115
cannam@86 116 b=oggpack_read(opb,1);
cannam@86 117 if(b<0)goto err_out;
cannam@86 118 if(b){
cannam@86 119 info->coupling_steps=oggpack_read(opb,8)+1;
cannam@86 120 if(info->coupling_steps<=0)goto err_out;
cannam@86 121 for(i=0;i<info->coupling_steps;i++){
cannam@86 122 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
cannam@86 123 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
cannam@86 124
cannam@86 125 if(testM<0 ||
cannam@86 126 testA<0 ||
cannam@86 127 testM==testA ||
cannam@86 128 testM>=vi->channels ||
cannam@86 129 testA>=vi->channels) goto err_out;
cannam@86 130 }
cannam@86 131
cannam@86 132 }
cannam@86 133
cannam@86 134 if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */
cannam@86 135
cannam@86 136 if(info->submaps>1){
cannam@86 137 for(i=0;i<vi->channels;i++){
cannam@86 138 info->chmuxlist[i]=oggpack_read(opb,4);
cannam@86 139 if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out;
cannam@86 140 }
cannam@86 141 }
cannam@86 142 for(i=0;i<info->submaps;i++){
cannam@86 143 oggpack_read(opb,8); /* time submap unused */
cannam@86 144 info->floorsubmap[i]=oggpack_read(opb,8);
cannam@86 145 if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out;
cannam@86 146 info->residuesubmap[i]=oggpack_read(opb,8);
cannam@86 147 if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)goto err_out;
cannam@86 148 }
cannam@86 149
cannam@86 150 return info;
cannam@86 151
cannam@86 152 err_out:
cannam@86 153 mapping0_free_info(info);
cannam@86 154 return(NULL);
cannam@86 155 }
cannam@86 156
cannam@86 157 #include "os.h"
cannam@86 158 #include "lpc.h"
cannam@86 159 #include "lsp.h"
cannam@86 160 #include "envelope.h"
cannam@86 161 #include "mdct.h"
cannam@86 162 #include "psy.h"
cannam@86 163 #include "scales.h"
cannam@86 164
cannam@86 165 #if 0
cannam@86 166 static long seq=0;
cannam@86 167 static ogg_int64_t total=0;
cannam@86 168 static float FLOOR1_fromdB_LOOKUP[256]={
cannam@86 169 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
cannam@86 170 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
cannam@86 171 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
cannam@86 172 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
cannam@86 173 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
cannam@86 174 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
cannam@86 175 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
cannam@86 176 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
cannam@86 177 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
cannam@86 178 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
cannam@86 179 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
cannam@86 180 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
cannam@86 181 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
cannam@86 182 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
cannam@86 183 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
cannam@86 184 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
cannam@86 185 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
cannam@86 186 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
cannam@86 187 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
cannam@86 188 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
cannam@86 189 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
cannam@86 190 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
cannam@86 191 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
cannam@86 192 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
cannam@86 193 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
cannam@86 194 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
cannam@86 195 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
cannam@86 196 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
cannam@86 197 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
cannam@86 198 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
cannam@86 199 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
cannam@86 200 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
cannam@86 201 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
cannam@86 202 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
cannam@86 203 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
cannam@86 204 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
cannam@86 205 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
cannam@86 206 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
cannam@86 207 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
cannam@86 208 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
cannam@86 209 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
cannam@86 210 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
cannam@86 211 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
cannam@86 212 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
cannam@86 213 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
cannam@86 214 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
cannam@86 215 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
cannam@86 216 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
cannam@86 217 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
cannam@86 218 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
cannam@86 219 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
cannam@86 220 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
cannam@86 221 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
cannam@86 222 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
cannam@86 223 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
cannam@86 224 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
cannam@86 225 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
cannam@86 226 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
cannam@86 227 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
cannam@86 228 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
cannam@86 229 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
cannam@86 230 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
cannam@86 231 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
cannam@86 232 0.82788260F, 0.88168307F, 0.9389798F, 1.F,
cannam@86 233 };
cannam@86 234
cannam@86 235 #endif
cannam@86 236
cannam@86 237
cannam@86 238 static int mapping0_forward(vorbis_block *vb){
cannam@86 239 vorbis_dsp_state *vd=vb->vd;
cannam@86 240 vorbis_info *vi=vd->vi;
cannam@86 241 codec_setup_info *ci=vi->codec_setup;
cannam@86 242 private_state *b=vb->vd->backend_state;
cannam@86 243 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
cannam@86 244 int n=vb->pcmend;
cannam@86 245 int i,j,k;
cannam@86 246
cannam@86 247 int *nonzero = alloca(sizeof(*nonzero)*vi->channels);
cannam@86 248 float **gmdct = _vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct));
cannam@86 249 int **iwork = _vorbis_block_alloc(vb,vi->channels*sizeof(*iwork));
cannam@86 250 int ***floor_posts = _vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts));
cannam@86 251
cannam@86 252 float global_ampmax=vbi->ampmax;
cannam@86 253 float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
cannam@86 254 int blocktype=vbi->blocktype;
cannam@86 255
cannam@86 256 int modenumber=vb->W;
cannam@86 257 vorbis_info_mapping0 *info=ci->map_param[modenumber];
cannam@86 258 vorbis_look_psy *psy_look=b->psy+blocktype+(vb->W?2:0);
cannam@86 259
cannam@86 260 vb->mode=modenumber;
cannam@86 261
cannam@86 262 for(i=0;i<vi->channels;i++){
cannam@86 263 float scale=4.f/n;
cannam@86 264 float scale_dB;
cannam@86 265
cannam@86 266 float *pcm =vb->pcm[i];
cannam@86 267 float *logfft =pcm;
cannam@86 268
cannam@86 269 iwork[i]=_vorbis_block_alloc(vb,n/2*sizeof(**iwork));
cannam@86 270 gmdct[i]=_vorbis_block_alloc(vb,n/2*sizeof(**gmdct));
cannam@86 271
cannam@86 272 scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original
cannam@86 273 todB estimation used on IEEE 754
cannam@86 274 compliant machines had a bug that
cannam@86 275 returned dB values about a third
cannam@86 276 of a decibel too high. The bug
cannam@86 277 was harmless because tunings
cannam@86 278 implicitly took that into
cannam@86 279 account. However, fixing the bug
cannam@86 280 in the estimator requires
cannam@86 281 changing all the tunings as well.
cannam@86 282 For now, it's easier to sync
cannam@86 283 things back up here, and
cannam@86 284 recalibrate the tunings in the
cannam@86 285 next major model upgrade. */
cannam@86 286
cannam@86 287 #if 0
cannam@86 288 if(vi->channels==2){
cannam@86 289 if(i==0)
cannam@86 290 _analysis_output("pcmL",seq,pcm,n,0,0,total-n/2);
cannam@86 291 else
cannam@86 292 _analysis_output("pcmR",seq,pcm,n,0,0,total-n/2);
cannam@86 293 }else{
cannam@86 294 _analysis_output("pcm",seq,pcm,n,0,0,total-n/2);
cannam@86 295 }
cannam@86 296 #endif
cannam@86 297
cannam@86 298 /* window the PCM data */
cannam@86 299 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
cannam@86 300
cannam@86 301 #if 0
cannam@86 302 if(vi->channels==2){
cannam@86 303 if(i==0)
cannam@86 304 _analysis_output("windowedL",seq,pcm,n,0,0,total-n/2);
cannam@86 305 else
cannam@86 306 _analysis_output("windowedR",seq,pcm,n,0,0,total-n/2);
cannam@86 307 }else{
cannam@86 308 _analysis_output("windowed",seq,pcm,n,0,0,total-n/2);
cannam@86 309 }
cannam@86 310 #endif
cannam@86 311
cannam@86 312 /* transform the PCM data */
cannam@86 313 /* only MDCT right now.... */
cannam@86 314 mdct_forward(b->transform[vb->W][0],pcm,gmdct[i]);
cannam@86 315
cannam@86 316 /* FFT yields more accurate tonal estimation (not phase sensitive) */
cannam@86 317 drft_forward(&b->fft_look[vb->W],pcm);
cannam@86 318 logfft[0]=scale_dB+todB(pcm) + .345; /* + .345 is a hack; the
cannam@86 319 original todB estimation used on
cannam@86 320 IEEE 754 compliant machines had a
cannam@86 321 bug that returned dB values about
cannam@86 322 a third of a decibel too high.
cannam@86 323 The bug was harmless because
cannam@86 324 tunings implicitly took that into
cannam@86 325 account. However, fixing the bug
cannam@86 326 in the estimator requires
cannam@86 327 changing all the tunings as well.
cannam@86 328 For now, it's easier to sync
cannam@86 329 things back up here, and
cannam@86 330 recalibrate the tunings in the
cannam@86 331 next major model upgrade. */
cannam@86 332 local_ampmax[i]=logfft[0];
cannam@86 333 for(j=1;j<n-1;j+=2){
cannam@86 334 float temp=pcm[j]*pcm[j]+pcm[j+1]*pcm[j+1];
cannam@86 335 temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp) + .345; /* +
cannam@86 336 .345 is a hack; the original todB
cannam@86 337 estimation used on IEEE 754
cannam@86 338 compliant machines had a bug that
cannam@86 339 returned dB values about a third
cannam@86 340 of a decibel too high. The bug
cannam@86 341 was harmless because tunings
cannam@86 342 implicitly took that into
cannam@86 343 account. However, fixing the bug
cannam@86 344 in the estimator requires
cannam@86 345 changing all the tunings as well.
cannam@86 346 For now, it's easier to sync
cannam@86 347 things back up here, and
cannam@86 348 recalibrate the tunings in the
cannam@86 349 next major model upgrade. */
cannam@86 350 if(temp>local_ampmax[i])local_ampmax[i]=temp;
cannam@86 351 }
cannam@86 352
cannam@86 353 if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
cannam@86 354 if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
cannam@86 355
cannam@86 356 #if 0
cannam@86 357 if(vi->channels==2){
cannam@86 358 if(i==0){
cannam@86 359 _analysis_output("fftL",seq,logfft,n/2,1,0,0);
cannam@86 360 }else{
cannam@86 361 _analysis_output("fftR",seq,logfft,n/2,1,0,0);
cannam@86 362 }
cannam@86 363 }else{
cannam@86 364 _analysis_output("fft",seq,logfft,n/2,1,0,0);
cannam@86 365 }
cannam@86 366 #endif
cannam@86 367
cannam@86 368 }
cannam@86 369
cannam@86 370 {
cannam@86 371 float *noise = _vorbis_block_alloc(vb,n/2*sizeof(*noise));
cannam@86 372 float *tone = _vorbis_block_alloc(vb,n/2*sizeof(*tone));
cannam@86 373
cannam@86 374 for(i=0;i<vi->channels;i++){
cannam@86 375 /* the encoder setup assumes that all the modes used by any
cannam@86 376 specific bitrate tweaking use the same floor */
cannam@86 377
cannam@86 378 int submap=info->chmuxlist[i];
cannam@86 379
cannam@86 380 /* the following makes things clearer to *me* anyway */
cannam@86 381 float *mdct =gmdct[i];
cannam@86 382 float *logfft =vb->pcm[i];
cannam@86 383
cannam@86 384 float *logmdct =logfft+n/2;
cannam@86 385 float *logmask =logfft;
cannam@86 386
cannam@86 387 vb->mode=modenumber;
cannam@86 388
cannam@86 389 floor_posts[i]=_vorbis_block_alloc(vb,PACKETBLOBS*sizeof(**floor_posts));
cannam@86 390 memset(floor_posts[i],0,sizeof(**floor_posts)*PACKETBLOBS);
cannam@86 391
cannam@86 392 for(j=0;j<n/2;j++)
cannam@86 393 logmdct[j]=todB(mdct+j) + .345; /* + .345 is a hack; the original
cannam@86 394 todB estimation used on IEEE 754
cannam@86 395 compliant machines had a bug that
cannam@86 396 returned dB values about a third
cannam@86 397 of a decibel too high. The bug
cannam@86 398 was harmless because tunings
cannam@86 399 implicitly took that into
cannam@86 400 account. However, fixing the bug
cannam@86 401 in the estimator requires
cannam@86 402 changing all the tunings as well.
cannam@86 403 For now, it's easier to sync
cannam@86 404 things back up here, and
cannam@86 405 recalibrate the tunings in the
cannam@86 406 next major model upgrade. */
cannam@86 407
cannam@86 408 #if 0
cannam@86 409 if(vi->channels==2){
cannam@86 410 if(i==0)
cannam@86 411 _analysis_output("mdctL",seq,logmdct,n/2,1,0,0);
cannam@86 412 else
cannam@86 413 _analysis_output("mdctR",seq,logmdct,n/2,1,0,0);
cannam@86 414 }else{
cannam@86 415 _analysis_output("mdct",seq,logmdct,n/2,1,0,0);
cannam@86 416 }
cannam@86 417 #endif
cannam@86 418
cannam@86 419 /* first step; noise masking. Not only does 'noise masking'
cannam@86 420 give us curves from which we can decide how much resolution
cannam@86 421 to give noise parts of the spectrum, it also implicitly hands
cannam@86 422 us a tonality estimate (the larger the value in the
cannam@86 423 'noise_depth' vector, the more tonal that area is) */
cannam@86 424
cannam@86 425 _vp_noisemask(psy_look,
cannam@86 426 logmdct,
cannam@86 427 noise); /* noise does not have by-frequency offset
cannam@86 428 bias applied yet */
cannam@86 429 #if 0
cannam@86 430 if(vi->channels==2){
cannam@86 431 if(i==0)
cannam@86 432 _analysis_output("noiseL",seq,noise,n/2,1,0,0);
cannam@86 433 else
cannam@86 434 _analysis_output("noiseR",seq,noise,n/2,1,0,0);
cannam@86 435 }else{
cannam@86 436 _analysis_output("noise",seq,noise,n/2,1,0,0);
cannam@86 437 }
cannam@86 438 #endif
cannam@86 439
cannam@86 440 /* second step: 'all the other crap'; all the stuff that isn't
cannam@86 441 computed/fit for bitrate management goes in the second psy
cannam@86 442 vector. This includes tone masking, peak limiting and ATH */
cannam@86 443
cannam@86 444 _vp_tonemask(psy_look,
cannam@86 445 logfft,
cannam@86 446 tone,
cannam@86 447 global_ampmax,
cannam@86 448 local_ampmax[i]);
cannam@86 449
cannam@86 450 #if 0
cannam@86 451 if(vi->channels==2){
cannam@86 452 if(i==0)
cannam@86 453 _analysis_output("toneL",seq,tone,n/2,1,0,0);
cannam@86 454 else
cannam@86 455 _analysis_output("toneR",seq,tone,n/2,1,0,0);
cannam@86 456 }else{
cannam@86 457 _analysis_output("tone",seq,tone,n/2,1,0,0);
cannam@86 458 }
cannam@86 459 #endif
cannam@86 460
cannam@86 461 /* third step; we offset the noise vectors, overlay tone
cannam@86 462 masking. We then do a floor1-specific line fit. If we're
cannam@86 463 performing bitrate management, the line fit is performed
cannam@86 464 multiple times for up/down tweakage on demand. */
cannam@86 465
cannam@86 466 #if 0
cannam@86 467 {
cannam@86 468 float aotuv[psy_look->n];
cannam@86 469 #endif
cannam@86 470
cannam@86 471 _vp_offset_and_mix(psy_look,
cannam@86 472 noise,
cannam@86 473 tone,
cannam@86 474 1,
cannam@86 475 logmask,
cannam@86 476 mdct,
cannam@86 477 logmdct);
cannam@86 478
cannam@86 479 #if 0
cannam@86 480 if(vi->channels==2){
cannam@86 481 if(i==0)
cannam@86 482 _analysis_output("aotuvM1_L",seq,aotuv,psy_look->n,1,1,0);
cannam@86 483 else
cannam@86 484 _analysis_output("aotuvM1_R",seq,aotuv,psy_look->n,1,1,0);
cannam@86 485 }else{
cannam@86 486 _analysis_output("aotuvM1",seq,aotuv,psy_look->n,1,1,0);
cannam@86 487 }
cannam@86 488 }
cannam@86 489 #endif
cannam@86 490
cannam@86 491
cannam@86 492 #if 0
cannam@86 493 if(vi->channels==2){
cannam@86 494 if(i==0)
cannam@86 495 _analysis_output("mask1L",seq,logmask,n/2,1,0,0);
cannam@86 496 else
cannam@86 497 _analysis_output("mask1R",seq,logmask,n/2,1,0,0);
cannam@86 498 }else{
cannam@86 499 _analysis_output("mask1",seq,logmask,n/2,1,0,0);
cannam@86 500 }
cannam@86 501 #endif
cannam@86 502
cannam@86 503 /* this algorithm is hardwired to floor 1 for now; abort out if
cannam@86 504 we're *not* floor1. This won't happen unless someone has
cannam@86 505 broken the encode setup lib. Guard it anyway. */
cannam@86 506 if(ci->floor_type[info->floorsubmap[submap]]!=1)return(-1);
cannam@86 507
cannam@86 508 floor_posts[i][PACKETBLOBS/2]=
cannam@86 509 floor1_fit(vb,b->flr[info->floorsubmap[submap]],
cannam@86 510 logmdct,
cannam@86 511 logmask);
cannam@86 512
cannam@86 513 /* are we managing bitrate? If so, perform two more fits for
cannam@86 514 later rate tweaking (fits represent hi/lo) */
cannam@86 515 if(vorbis_bitrate_managed(vb) && floor_posts[i][PACKETBLOBS/2]){
cannam@86 516 /* higher rate by way of lower noise curve */
cannam@86 517
cannam@86 518 _vp_offset_and_mix(psy_look,
cannam@86 519 noise,
cannam@86 520 tone,
cannam@86 521 2,
cannam@86 522 logmask,
cannam@86 523 mdct,
cannam@86 524 logmdct);
cannam@86 525
cannam@86 526 #if 0
cannam@86 527 if(vi->channels==2){
cannam@86 528 if(i==0)
cannam@86 529 _analysis_output("mask2L",seq,logmask,n/2,1,0,0);
cannam@86 530 else
cannam@86 531 _analysis_output("mask2R",seq,logmask,n/2,1,0,0);
cannam@86 532 }else{
cannam@86 533 _analysis_output("mask2",seq,logmask,n/2,1,0,0);
cannam@86 534 }
cannam@86 535 #endif
cannam@86 536
cannam@86 537 floor_posts[i][PACKETBLOBS-1]=
cannam@86 538 floor1_fit(vb,b->flr[info->floorsubmap[submap]],
cannam@86 539 logmdct,
cannam@86 540 logmask);
cannam@86 541
cannam@86 542 /* lower rate by way of higher noise curve */
cannam@86 543 _vp_offset_and_mix(psy_look,
cannam@86 544 noise,
cannam@86 545 tone,
cannam@86 546 0,
cannam@86 547 logmask,
cannam@86 548 mdct,
cannam@86 549 logmdct);
cannam@86 550
cannam@86 551 #if 0
cannam@86 552 if(vi->channels==2){
cannam@86 553 if(i==0)
cannam@86 554 _analysis_output("mask0L",seq,logmask,n/2,1,0,0);
cannam@86 555 else
cannam@86 556 _analysis_output("mask0R",seq,logmask,n/2,1,0,0);
cannam@86 557 }else{
cannam@86 558 _analysis_output("mask0",seq,logmask,n/2,1,0,0);
cannam@86 559 }
cannam@86 560 #endif
cannam@86 561
cannam@86 562 floor_posts[i][0]=
cannam@86 563 floor1_fit(vb,b->flr[info->floorsubmap[submap]],
cannam@86 564 logmdct,
cannam@86 565 logmask);
cannam@86 566
cannam@86 567 /* we also interpolate a range of intermediate curves for
cannam@86 568 intermediate rates */
cannam@86 569 for(k=1;k<PACKETBLOBS/2;k++)
cannam@86 570 floor_posts[i][k]=
cannam@86 571 floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
cannam@86 572 floor_posts[i][0],
cannam@86 573 floor_posts[i][PACKETBLOBS/2],
cannam@86 574 k*65536/(PACKETBLOBS/2));
cannam@86 575 for(k=PACKETBLOBS/2+1;k<PACKETBLOBS-1;k++)
cannam@86 576 floor_posts[i][k]=
cannam@86 577 floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
cannam@86 578 floor_posts[i][PACKETBLOBS/2],
cannam@86 579 floor_posts[i][PACKETBLOBS-1],
cannam@86 580 (k-PACKETBLOBS/2)*65536/(PACKETBLOBS/2));
cannam@86 581 }
cannam@86 582 }
cannam@86 583 }
cannam@86 584 vbi->ampmax=global_ampmax;
cannam@86 585
cannam@86 586 /*
cannam@86 587 the next phases are performed once for vbr-only and PACKETBLOB
cannam@86 588 times for bitrate managed modes.
cannam@86 589
cannam@86 590 1) encode actual mode being used
cannam@86 591 2) encode the floor for each channel, compute coded mask curve/res
cannam@86 592 3) normalize and couple.
cannam@86 593 4) encode residue
cannam@86 594 5) save packet bytes to the packetblob vector
cannam@86 595
cannam@86 596 */
cannam@86 597
cannam@86 598 /* iterate over the many masking curve fits we've created */
cannam@86 599
cannam@86 600 {
cannam@86 601 int **couple_bundle=alloca(sizeof(*couple_bundle)*vi->channels);
cannam@86 602 int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
cannam@86 603
cannam@86 604 for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2);
cannam@86 605 k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2);
cannam@86 606 k++){
cannam@86 607 oggpack_buffer *opb=vbi->packetblob[k];
cannam@86 608
cannam@86 609 /* start out our new packet blob with packet type and mode */
cannam@86 610 /* Encode the packet type */
cannam@86 611 oggpack_write(opb,0,1);
cannam@86 612 /* Encode the modenumber */
cannam@86 613 /* Encode frame mode, pre,post windowsize, then dispatch */
cannam@86 614 oggpack_write(opb,modenumber,b->modebits);
cannam@86 615 if(vb->W){
cannam@86 616 oggpack_write(opb,vb->lW,1);
cannam@86 617 oggpack_write(opb,vb->nW,1);
cannam@86 618 }
cannam@86 619
cannam@86 620 /* encode floor, compute masking curve, sep out residue */
cannam@86 621 for(i=0;i<vi->channels;i++){
cannam@86 622 int submap=info->chmuxlist[i];
cannam@86 623 int *ilogmask=iwork[i];
cannam@86 624
cannam@86 625 nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]],
cannam@86 626 floor_posts[i][k],
cannam@86 627 ilogmask);
cannam@86 628 #if 0
cannam@86 629 {
cannam@86 630 char buf[80];
cannam@86 631 sprintf(buf,"maskI%c%d",i?'R':'L',k);
cannam@86 632 float work[n/2];
cannam@86 633 for(j=0;j<n/2;j++)
cannam@86 634 work[j]=FLOOR1_fromdB_LOOKUP[iwork[i][j]];
cannam@86 635 _analysis_output(buf,seq,work,n/2,1,1,0);
cannam@86 636 }
cannam@86 637 #endif
cannam@86 638 }
cannam@86 639
cannam@86 640 /* our iteration is now based on masking curve, not prequant and
cannam@86 641 coupling. Only one prequant/coupling step */
cannam@86 642
cannam@86 643 /* quantize/couple */
cannam@86 644 /* incomplete implementation that assumes the tree is all depth
cannam@86 645 one, or no tree at all */
cannam@86 646 _vp_couple_quantize_normalize(k,
cannam@86 647 &ci->psy_g_param,
cannam@86 648 psy_look,
cannam@86 649 info,
cannam@86 650 gmdct,
cannam@86 651 iwork,
cannam@86 652 nonzero,
cannam@86 653 ci->psy_g_param.sliding_lowpass[vb->W][k],
cannam@86 654 vi->channels);
cannam@86 655
cannam@86 656 #if 0
cannam@86 657 for(i=0;i<vi->channels;i++){
cannam@86 658 char buf[80];
cannam@86 659 sprintf(buf,"res%c%d",i?'R':'L',k);
cannam@86 660 float work[n/2];
cannam@86 661 for(j=0;j<n/2;j++)
cannam@86 662 work[j]=iwork[i][j];
cannam@86 663 _analysis_output(buf,seq,work,n/2,1,0,0);
cannam@86 664 }
cannam@86 665 #endif
cannam@86 666
cannam@86 667 /* classify and encode by submap */
cannam@86 668 for(i=0;i<info->submaps;i++){
cannam@86 669 int ch_in_bundle=0;
cannam@86 670 long **classifications;
cannam@86 671 int resnum=info->residuesubmap[i];
cannam@86 672
cannam@86 673 for(j=0;j<vi->channels;j++){
cannam@86 674 if(info->chmuxlist[j]==i){
cannam@86 675 zerobundle[ch_in_bundle]=0;
cannam@86 676 if(nonzero[j])zerobundle[ch_in_bundle]=1;
cannam@86 677 couple_bundle[ch_in_bundle++]=iwork[j];
cannam@86 678 }
cannam@86 679 }
cannam@86 680
cannam@86 681 classifications=_residue_P[ci->residue_type[resnum]]->
cannam@86 682 class(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle);
cannam@86 683
cannam@86 684 ch_in_bundle=0;
cannam@86 685 for(j=0;j<vi->channels;j++)
cannam@86 686 if(info->chmuxlist[j]==i)
cannam@86 687 couple_bundle[ch_in_bundle++]=iwork[j];
cannam@86 688
cannam@86 689 _residue_P[ci->residue_type[resnum]]->
cannam@86 690 forward(opb,vb,b->residue[resnum],
cannam@86 691 couple_bundle,zerobundle,ch_in_bundle,classifications,i);
cannam@86 692 }
cannam@86 693
cannam@86 694 /* ok, done encoding. Next protopacket. */
cannam@86 695 }
cannam@86 696
cannam@86 697 }
cannam@86 698
cannam@86 699 #if 0
cannam@86 700 seq++;
cannam@86 701 total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;
cannam@86 702 #endif
cannam@86 703 return(0);
cannam@86 704 }
cannam@86 705
cannam@86 706 static int mapping0_inverse(vorbis_block *vb,vorbis_info_mapping *l){
cannam@86 707 vorbis_dsp_state *vd=vb->vd;
cannam@86 708 vorbis_info *vi=vd->vi;
cannam@86 709 codec_setup_info *ci=vi->codec_setup;
cannam@86 710 private_state *b=vd->backend_state;
cannam@86 711 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)l;
cannam@86 712
cannam@86 713 int i,j;
cannam@86 714 long n=vb->pcmend=ci->blocksizes[vb->W];
cannam@86 715
cannam@86 716 float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
cannam@86 717 int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
cannam@86 718
cannam@86 719 int *nonzero =alloca(sizeof(*nonzero)*vi->channels);
cannam@86 720 void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
cannam@86 721
cannam@86 722 /* recover the spectral envelope; store it in the PCM vector for now */
cannam@86 723 for(i=0;i<vi->channels;i++){
cannam@86 724 int submap=info->chmuxlist[i];
cannam@86 725 floormemo[i]=_floor_P[ci->floor_type[info->floorsubmap[submap]]]->
cannam@86 726 inverse1(vb,b->flr[info->floorsubmap[submap]]);
cannam@86 727 if(floormemo[i])
cannam@86 728 nonzero[i]=1;
cannam@86 729 else
cannam@86 730 nonzero[i]=0;
cannam@86 731 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
cannam@86 732 }
cannam@86 733
cannam@86 734 /* channel coupling can 'dirty' the nonzero listing */
cannam@86 735 for(i=0;i<info->coupling_steps;i++){
cannam@86 736 if(nonzero[info->coupling_mag[i]] ||
cannam@86 737 nonzero[info->coupling_ang[i]]){
cannam@86 738 nonzero[info->coupling_mag[i]]=1;
cannam@86 739 nonzero[info->coupling_ang[i]]=1;
cannam@86 740 }
cannam@86 741 }
cannam@86 742
cannam@86 743 /* recover the residue into our working vectors */
cannam@86 744 for(i=0;i<info->submaps;i++){
cannam@86 745 int ch_in_bundle=0;
cannam@86 746 for(j=0;j<vi->channels;j++){
cannam@86 747 if(info->chmuxlist[j]==i){
cannam@86 748 if(nonzero[j])
cannam@86 749 zerobundle[ch_in_bundle]=1;
cannam@86 750 else
cannam@86 751 zerobundle[ch_in_bundle]=0;
cannam@86 752 pcmbundle[ch_in_bundle++]=vb->pcm[j];
cannam@86 753 }
cannam@86 754 }
cannam@86 755
cannam@86 756 _residue_P[ci->residue_type[info->residuesubmap[i]]]->
cannam@86 757 inverse(vb,b->residue[info->residuesubmap[i]],
cannam@86 758 pcmbundle,zerobundle,ch_in_bundle);
cannam@86 759 }
cannam@86 760
cannam@86 761 /* channel coupling */
cannam@86 762 for(i=info->coupling_steps-1;i>=0;i--){
cannam@86 763 float *pcmM=vb->pcm[info->coupling_mag[i]];
cannam@86 764 float *pcmA=vb->pcm[info->coupling_ang[i]];
cannam@86 765
cannam@86 766 for(j=0;j<n/2;j++){
cannam@86 767 float mag=pcmM[j];
cannam@86 768 float ang=pcmA[j];
cannam@86 769
cannam@86 770 if(mag>0)
cannam@86 771 if(ang>0){
cannam@86 772 pcmM[j]=mag;
cannam@86 773 pcmA[j]=mag-ang;
cannam@86 774 }else{
cannam@86 775 pcmA[j]=mag;
cannam@86 776 pcmM[j]=mag+ang;
cannam@86 777 }
cannam@86 778 else
cannam@86 779 if(ang>0){
cannam@86 780 pcmM[j]=mag;
cannam@86 781 pcmA[j]=mag+ang;
cannam@86 782 }else{
cannam@86 783 pcmA[j]=mag;
cannam@86 784 pcmM[j]=mag-ang;
cannam@86 785 }
cannam@86 786 }
cannam@86 787 }
cannam@86 788
cannam@86 789 /* compute and apply spectral envelope */
cannam@86 790 for(i=0;i<vi->channels;i++){
cannam@86 791 float *pcm=vb->pcm[i];
cannam@86 792 int submap=info->chmuxlist[i];
cannam@86 793 _floor_P[ci->floor_type[info->floorsubmap[submap]]]->
cannam@86 794 inverse2(vb,b->flr[info->floorsubmap[submap]],
cannam@86 795 floormemo[i],pcm);
cannam@86 796 }
cannam@86 797
cannam@86 798 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
cannam@86 799 /* only MDCT right now.... */
cannam@86 800 for(i=0;i<vi->channels;i++){
cannam@86 801 float *pcm=vb->pcm[i];
cannam@86 802 mdct_backward(b->transform[vb->W][0],pcm,pcm);
cannam@86 803 }
cannam@86 804
cannam@86 805 /* all done! */
cannam@86 806 return(0);
cannam@86 807 }
cannam@86 808
cannam@86 809 /* export hooks */
cannam@86 810 const vorbis_func_mapping mapping0_exportbundle={
cannam@86 811 &mapping0_pack,
cannam@86 812 &mapping0_unpack,
cannam@86 813 &mapping0_free_info,
cannam@86 814 &mapping0_forward,
cannam@86 815 &mapping0_inverse
cannam@86 816 };