nuclear@1: /******************************************************************** nuclear@1: * * nuclear@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@1: * * nuclear@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: floor backend 0 implementation nuclear@1: last mod: $Id: floor0.c 18184 2012-02-03 20:55:12Z xiphmont $ nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include "vorbis/codec.h" nuclear@1: #include "codec_internal.h" nuclear@1: #include "registry.h" nuclear@1: #include "lpc.h" nuclear@1: #include "lsp.h" nuclear@1: #include "codebook.h" nuclear@1: #include "scales.h" nuclear@1: #include "misc.h" nuclear@1: #include "os.h" nuclear@1: nuclear@1: #include "misc.h" nuclear@1: #include nuclear@1: nuclear@1: typedef struct { nuclear@1: int ln; nuclear@1: int m; nuclear@1: int **linearmap; nuclear@1: int n[2]; nuclear@1: nuclear@1: vorbis_info_floor0 *vi; nuclear@1: nuclear@1: long bits; nuclear@1: long frames; nuclear@1: } vorbis_look_floor0; nuclear@1: nuclear@1: nuclear@1: /***********************************************/ nuclear@1: nuclear@1: static void floor0_free_info(vorbis_info_floor *i){ nuclear@1: vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; nuclear@1: if(info){ nuclear@1: memset(info,0,sizeof(*info)); nuclear@1: _ogg_free(info); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void floor0_free_look(vorbis_look_floor *i){ nuclear@1: vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; nuclear@1: if(look){ nuclear@1: nuclear@1: if(look->linearmap){ nuclear@1: nuclear@1: if(look->linearmap[0])_ogg_free(look->linearmap[0]); nuclear@1: if(look->linearmap[1])_ogg_free(look->linearmap[1]); nuclear@1: nuclear@1: _ogg_free(look->linearmap); nuclear@1: } nuclear@1: memset(look,0,sizeof(*look)); nuclear@1: _ogg_free(look); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int j; nuclear@1: nuclear@1: vorbis_info_floor0 *info=_ogg_malloc(sizeof(*info)); nuclear@1: info->order=oggpack_read(opb,8); nuclear@1: info->rate=oggpack_read(opb,16); nuclear@1: info->barkmap=oggpack_read(opb,16); nuclear@1: info->ampbits=oggpack_read(opb,6); nuclear@1: info->ampdB=oggpack_read(opb,8); nuclear@1: info->numbooks=oggpack_read(opb,4)+1; nuclear@1: nuclear@1: if(info->order<1)goto err_out; nuclear@1: if(info->rate<1)goto err_out; nuclear@1: if(info->barkmap<1)goto err_out; nuclear@1: if(info->numbooks<1)goto err_out; nuclear@1: nuclear@1: for(j=0;jnumbooks;j++){ nuclear@1: info->books[j]=oggpack_read(opb,8); nuclear@1: if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out; nuclear@1: if(ci->book_param[info->books[j]]->maptype==0)goto err_out; nuclear@1: if(ci->book_param[info->books[j]]->dim<1)goto err_out; nuclear@1: } nuclear@1: return(info); nuclear@1: nuclear@1: err_out: nuclear@1: floor0_free_info(info); nuclear@1: return(NULL); nuclear@1: } nuclear@1: nuclear@1: /* initialize Bark scale and normalization lookups. We could do this nuclear@1: with static tables, but Vorbis allows a number of possible nuclear@1: combinations, so it's best to do it computationally. nuclear@1: nuclear@1: The below is authoritative in terms of defining scale mapping. nuclear@1: Note that the scale depends on the sampling rate as well as the nuclear@1: linear block and mapping sizes */ nuclear@1: nuclear@1: static void floor0_map_lazy_init(vorbis_block *vb, nuclear@1: vorbis_info_floor *infoX, nuclear@1: vorbis_look_floor0 *look){ nuclear@1: if(!look->linearmap[vb->W]){ nuclear@1: vorbis_dsp_state *vd=vb->vd; nuclear@1: vorbis_info *vi=vd->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX; nuclear@1: int W=vb->W; nuclear@1: int n=ci->blocksizes[W]/2,j; nuclear@1: nuclear@1: /* we choose a scaling constant so that: nuclear@1: floor(bark(rate/2-1)*C)=mapped-1 nuclear@1: floor(bark(rate/2)*C)=mapped */ nuclear@1: float scale=look->ln/toBARK(info->rate/2.f); nuclear@1: nuclear@1: /* the mapping from a linear scale to a smaller bark scale is nuclear@1: straightforward. We do *not* make sure that the linear mapping nuclear@1: does not skip bark-scale bins; the decoder simply skips them and nuclear@1: the encoder may do what it wishes in filling them. They're nuclear@1: necessary in some mapping combinations to keep the scale spacing nuclear@1: accurate */ nuclear@1: look->linearmap[W]=_ogg_malloc((n+1)*sizeof(**look->linearmap)); nuclear@1: for(j=0;jrate/2.f)/n*j) nuclear@1: *scale); /* bark numbers represent band edges */ nuclear@1: if(val>=look->ln)val=look->ln-1; /* guard against the approximation */ nuclear@1: look->linearmap[W][j]=val; nuclear@1: } nuclear@1: look->linearmap[W][j]=-1; nuclear@1: look->n[W]=n; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd, nuclear@1: vorbis_info_floor *i){ nuclear@1: vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; nuclear@1: vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(*look)); nuclear@1: look->m=info->order; nuclear@1: look->ln=info->barkmap; nuclear@1: look->vi=info; nuclear@1: nuclear@1: look->linearmap=_ogg_calloc(2,sizeof(*look->linearmap)); nuclear@1: nuclear@1: return look; nuclear@1: } nuclear@1: nuclear@1: static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){ nuclear@1: vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; nuclear@1: vorbis_info_floor0 *info=look->vi; nuclear@1: int j,k; nuclear@1: nuclear@1: int ampraw=oggpack_read(&vb->opb,info->ampbits); nuclear@1: if(ampraw>0){ /* also handles the -1 out of data case */ nuclear@1: long maxval=(1<ampbits)-1; nuclear@1: float amp=(float)ampraw/maxval*info->ampdB; nuclear@1: int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks)); nuclear@1: nuclear@1: if(booknum!=-1 && booknumnumbooks){ /* be paranoid */ nuclear@1: codec_setup_info *ci=vb->vd->vi->codec_setup; nuclear@1: codebook *b=ci->fullbooks+info->books[booknum]; nuclear@1: float last=0.f; nuclear@1: nuclear@1: /* the additional b->dim is a guard against any possible stack nuclear@1: smash; b->dim is provably more than we can overflow the nuclear@1: vector */ nuclear@1: float *lsp=_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1)); nuclear@1: nuclear@1: if(vorbis_book_decodev_set(b,lsp,&vb->opb,look->m)==-1)goto eop; nuclear@1: for(j=0;jm;){ nuclear@1: for(k=0;jm && kdim;k++,j++)lsp[j]+=last; nuclear@1: last=lsp[j-1]; nuclear@1: } nuclear@1: nuclear@1: lsp[look->m]=amp; nuclear@1: return(lsp); nuclear@1: } nuclear@1: } nuclear@1: eop: nuclear@1: return(NULL); nuclear@1: } nuclear@1: nuclear@1: static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i, nuclear@1: void *memo,float *out){ nuclear@1: vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; nuclear@1: vorbis_info_floor0 *info=look->vi; nuclear@1: nuclear@1: floor0_map_lazy_init(vb,info,look); nuclear@1: nuclear@1: if(memo){ nuclear@1: float *lsp=(float *)memo; nuclear@1: float amp=lsp[look->m]; nuclear@1: nuclear@1: /* take the coefficients back to a spectral envelope curve */ nuclear@1: vorbis_lsp_to_curve(out, nuclear@1: look->linearmap[vb->W], nuclear@1: look->n[vb->W], nuclear@1: look->ln, nuclear@1: lsp,look->m,amp,(float)info->ampdB); nuclear@1: return(1); nuclear@1: } nuclear@1: memset(out,0,sizeof(*out)*look->n[vb->W]); nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* export hooks */ nuclear@1: const vorbis_func_floor floor0_exportbundle={ nuclear@1: NULL,&floor0_unpack,&floor0_look,&floor0_free_info, nuclear@1: &floor0_free_look,&floor0_inverse1,&floor0_inverse2 nuclear@1: };