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