annotate src/libvorbis-1.3.3/lib/floor0.c @ 130:1c067f014d80

64-bit MSVC builds
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 15:59:23 +0100
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-2009 *
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: floor backend 0 implementation
cannam@86 14 last mod: $Id: floor0.c 18184 2012-02-03 20:55:12Z xiphmont $
cannam@86 15
cannam@86 16 ********************************************************************/
cannam@86 17
cannam@86 18 #include <stdlib.h>
cannam@86 19 #include <string.h>
cannam@86 20 #include <math.h>
cannam@86 21 #include <ogg/ogg.h>
cannam@86 22 #include "vorbis/codec.h"
cannam@86 23 #include "codec_internal.h"
cannam@86 24 #include "registry.h"
cannam@86 25 #include "lpc.h"
cannam@86 26 #include "lsp.h"
cannam@86 27 #include "codebook.h"
cannam@86 28 #include "scales.h"
cannam@86 29 #include "misc.h"
cannam@86 30 #include "os.h"
cannam@86 31
cannam@86 32 #include "misc.h"
cannam@86 33 #include <stdio.h>
cannam@86 34
cannam@86 35 typedef struct {
cannam@86 36 int ln;
cannam@86 37 int m;
cannam@86 38 int **linearmap;
cannam@86 39 int n[2];
cannam@86 40
cannam@86 41 vorbis_info_floor0 *vi;
cannam@86 42
cannam@86 43 long bits;
cannam@86 44 long frames;
cannam@86 45 } vorbis_look_floor0;
cannam@86 46
cannam@86 47
cannam@86 48 /***********************************************/
cannam@86 49
cannam@86 50 static void floor0_free_info(vorbis_info_floor *i){
cannam@86 51 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
cannam@86 52 if(info){
cannam@86 53 memset(info,0,sizeof(*info));
cannam@86 54 _ogg_free(info);
cannam@86 55 }
cannam@86 56 }
cannam@86 57
cannam@86 58 static void floor0_free_look(vorbis_look_floor *i){
cannam@86 59 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
cannam@86 60 if(look){
cannam@86 61
cannam@86 62 if(look->linearmap){
cannam@86 63
cannam@86 64 if(look->linearmap[0])_ogg_free(look->linearmap[0]);
cannam@86 65 if(look->linearmap[1])_ogg_free(look->linearmap[1]);
cannam@86 66
cannam@86 67 _ogg_free(look->linearmap);
cannam@86 68 }
cannam@86 69 memset(look,0,sizeof(*look));
cannam@86 70 _ogg_free(look);
cannam@86 71 }
cannam@86 72 }
cannam@86 73
cannam@86 74 static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
cannam@86 75 codec_setup_info *ci=vi->codec_setup;
cannam@86 76 int j;
cannam@86 77
cannam@86 78 vorbis_info_floor0 *info=_ogg_malloc(sizeof(*info));
cannam@86 79 info->order=oggpack_read(opb,8);
cannam@86 80 info->rate=oggpack_read(opb,16);
cannam@86 81 info->barkmap=oggpack_read(opb,16);
cannam@86 82 info->ampbits=oggpack_read(opb,6);
cannam@86 83 info->ampdB=oggpack_read(opb,8);
cannam@86 84 info->numbooks=oggpack_read(opb,4)+1;
cannam@86 85
cannam@86 86 if(info->order<1)goto err_out;
cannam@86 87 if(info->rate<1)goto err_out;
cannam@86 88 if(info->barkmap<1)goto err_out;
cannam@86 89 if(info->numbooks<1)goto err_out;
cannam@86 90
cannam@86 91 for(j=0;j<info->numbooks;j++){
cannam@86 92 info->books[j]=oggpack_read(opb,8);
cannam@86 93 if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
cannam@86 94 if(ci->book_param[info->books[j]]->maptype==0)goto err_out;
cannam@86 95 if(ci->book_param[info->books[j]]->dim<1)goto err_out;
cannam@86 96 }
cannam@86 97 return(info);
cannam@86 98
cannam@86 99 err_out:
cannam@86 100 floor0_free_info(info);
cannam@86 101 return(NULL);
cannam@86 102 }
cannam@86 103
cannam@86 104 /* initialize Bark scale and normalization lookups. We could do this
cannam@86 105 with static tables, but Vorbis allows a number of possible
cannam@86 106 combinations, so it's best to do it computationally.
cannam@86 107
cannam@86 108 The below is authoritative in terms of defining scale mapping.
cannam@86 109 Note that the scale depends on the sampling rate as well as the
cannam@86 110 linear block and mapping sizes */
cannam@86 111
cannam@86 112 static void floor0_map_lazy_init(vorbis_block *vb,
cannam@86 113 vorbis_info_floor *infoX,
cannam@86 114 vorbis_look_floor0 *look){
cannam@86 115 if(!look->linearmap[vb->W]){
cannam@86 116 vorbis_dsp_state *vd=vb->vd;
cannam@86 117 vorbis_info *vi=vd->vi;
cannam@86 118 codec_setup_info *ci=vi->codec_setup;
cannam@86 119 vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX;
cannam@86 120 int W=vb->W;
cannam@86 121 int n=ci->blocksizes[W]/2,j;
cannam@86 122
cannam@86 123 /* we choose a scaling constant so that:
cannam@86 124 floor(bark(rate/2-1)*C)=mapped-1
cannam@86 125 floor(bark(rate/2)*C)=mapped */
cannam@86 126 float scale=look->ln/toBARK(info->rate/2.f);
cannam@86 127
cannam@86 128 /* the mapping from a linear scale to a smaller bark scale is
cannam@86 129 straightforward. We do *not* make sure that the linear mapping
cannam@86 130 does not skip bark-scale bins; the decoder simply skips them and
cannam@86 131 the encoder may do what it wishes in filling them. They're
cannam@86 132 necessary in some mapping combinations to keep the scale spacing
cannam@86 133 accurate */
cannam@86 134 look->linearmap[W]=_ogg_malloc((n+1)*sizeof(**look->linearmap));
cannam@86 135 for(j=0;j<n;j++){
cannam@86 136 int val=floor( toBARK((info->rate/2.f)/n*j)
cannam@86 137 *scale); /* bark numbers represent band edges */
cannam@86 138 if(val>=look->ln)val=look->ln-1; /* guard against the approximation */
cannam@86 139 look->linearmap[W][j]=val;
cannam@86 140 }
cannam@86 141 look->linearmap[W][j]=-1;
cannam@86 142 look->n[W]=n;
cannam@86 143 }
cannam@86 144 }
cannam@86 145
cannam@86 146 static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd,
cannam@86 147 vorbis_info_floor *i){
cannam@86 148 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
cannam@86 149 vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(*look));
cannam@86 150 look->m=info->order;
cannam@86 151 look->ln=info->barkmap;
cannam@86 152 look->vi=info;
cannam@86 153
cannam@86 154 look->linearmap=_ogg_calloc(2,sizeof(*look->linearmap));
cannam@86 155
cannam@86 156 return look;
cannam@86 157 }
cannam@86 158
cannam@86 159 static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){
cannam@86 160 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
cannam@86 161 vorbis_info_floor0 *info=look->vi;
cannam@86 162 int j,k;
cannam@86 163
cannam@86 164 int ampraw=oggpack_read(&vb->opb,info->ampbits);
cannam@86 165 if(ampraw>0){ /* also handles the -1 out of data case */
cannam@86 166 long maxval=(1<<info->ampbits)-1;
cannam@86 167 float amp=(float)ampraw/maxval*info->ampdB;
cannam@86 168 int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks));
cannam@86 169
cannam@86 170 if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */
cannam@86 171 codec_setup_info *ci=vb->vd->vi->codec_setup;
cannam@86 172 codebook *b=ci->fullbooks+info->books[booknum];
cannam@86 173 float last=0.f;
cannam@86 174
cannam@86 175 /* the additional b->dim is a guard against any possible stack
cannam@86 176 smash; b->dim is provably more than we can overflow the
cannam@86 177 vector */
cannam@86 178 float *lsp=_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1));
cannam@86 179
cannam@86 180 if(vorbis_book_decodev_set(b,lsp,&vb->opb,look->m)==-1)goto eop;
cannam@86 181 for(j=0;j<look->m;){
cannam@86 182 for(k=0;j<look->m && k<b->dim;k++,j++)lsp[j]+=last;
cannam@86 183 last=lsp[j-1];
cannam@86 184 }
cannam@86 185
cannam@86 186 lsp[look->m]=amp;
cannam@86 187 return(lsp);
cannam@86 188 }
cannam@86 189 }
cannam@86 190 eop:
cannam@86 191 return(NULL);
cannam@86 192 }
cannam@86 193
cannam@86 194 static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i,
cannam@86 195 void *memo,float *out){
cannam@86 196 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
cannam@86 197 vorbis_info_floor0 *info=look->vi;
cannam@86 198
cannam@86 199 floor0_map_lazy_init(vb,info,look);
cannam@86 200
cannam@86 201 if(memo){
cannam@86 202 float *lsp=(float *)memo;
cannam@86 203 float amp=lsp[look->m];
cannam@86 204
cannam@86 205 /* take the coefficients back to a spectral envelope curve */
cannam@86 206 vorbis_lsp_to_curve(out,
cannam@86 207 look->linearmap[vb->W],
cannam@86 208 look->n[vb->W],
cannam@86 209 look->ln,
cannam@86 210 lsp,look->m,amp,(float)info->ampdB);
cannam@86 211 return(1);
cannam@86 212 }
cannam@86 213 memset(out,0,sizeof(*out)*look->n[vb->W]);
cannam@86 214 return(0);
cannam@86 215 }
cannam@86 216
cannam@86 217 /* export hooks */
cannam@86 218 const vorbis_func_floor floor0_exportbundle={
cannam@86 219 NULL,&floor0_unpack,&floor0_look,&floor0_free_info,
cannam@86 220 &floor0_free_look,&floor0_inverse1,&floor0_inverse2
cannam@86 221 };