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