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