1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: floor backend 0 implementation
15 last mod: $Id: floor0.c,v 1.11 2000/02/23 11:22:44 xiphmont Exp $
17 ********************************************************************/
22 #include "vorbis/codec.h"
27 #include "bookinternal.h"
37 vorbis_info_floor0 *vi;
41 static void free_info(vorbis_info_floor *i){
43 memset(i,0,sizeof(vorbis_info_floor0));
48 static void free_look(vorbis_look_floor *i){
49 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
51 lpc_clear(&look->lpclook);
52 memset(look,0,sizeof(vorbis_look_floor0));
57 static void pack (vorbis_info_floor *i,oggpack_buffer *opb){
58 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
60 _oggpack_write(opb,info->order,8);
61 _oggpack_write(opb,info->rate,16);
62 _oggpack_write(opb,info->barkmap,16);
63 _oggpack_write(opb,info->ampbits,6);
64 _oggpack_write(opb,info->ampdB,8);
65 _oggpack_write(opb,info->stages-1,4);
66 for(j=0;j<info->stages;j++)
67 _oggpack_write(opb,info->books[j],8);
70 static vorbis_info_floor *unpack (vorbis_info *vi,oggpack_buffer *opb){
72 vorbis_info_floor0 *info=malloc(sizeof(vorbis_info_floor0));
73 info->order=_oggpack_read(opb,8);
74 info->rate=_oggpack_read(opb,16);
75 info->barkmap=_oggpack_read(opb,16);
76 info->ampbits=_oggpack_read(opb,6);
77 info->ampdB=_oggpack_read(opb,8);
78 info->stages=_oggpack_read(opb,4)+1;
80 if(info->order<1)goto err_out;
81 if(info->rate<1)goto err_out;
82 if(info->barkmap<1)goto err_out;
83 if(info->stages<1)goto err_out;
85 for(j=0;j<info->stages;j++){
86 info->books[j]=_oggpack_read(opb,8);
87 if(info->books[j]<0 || info->books[j]>=vi->books)goto err_out;
95 static vorbis_look_floor *look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
96 vorbis_info_floor *i){
97 vorbis_info *vi=vd->vi;
98 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
99 vorbis_look_floor0 *look=malloc(sizeof(vorbis_look_floor0));
101 look->n=vi->blocksizes[mi->blockflag]/2;
103 lpc_init(&look->lpclook,look->n,info->barkmap,info->rate,look->m);
110 static int forward(vorbis_block *vb,vorbis_look_floor *i,
111 double *in,double *out){
113 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
114 vorbis_info_floor0 *info=look->vi;
118 /* use 'out' as temp storage */
119 /* Convert our floor to a set of lpc coefficients */
120 amp=sqrt(vorbis_curve_to_lpc(in,out,&look->lpclook));
122 /* amp is in the range 0. to 1. (well, more like .7). Log scale it */
125 (1<<ampbits)-1 == amp dB = 1. amp */
127 long ampscale=fromdB(info->ampdB);
128 long maxval=(1<<info->ampbits)-1;
130 long val=todB(amp*ampscale)/info->ampdB*maxval+1;
132 if(val<0)val=0; /* likely */
133 if(val>maxval)val=maxval; /* not bloody likely */
135 _oggpack_write(&vb->opb,val,info->ampbits);
137 amp=fromdB((val-.5)/maxval*info->ampdB)/ampscale;
143 double *work=alloca(sizeof(double)*look->m);
145 /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
146 vorbis_lpc_to_lsp(out,out,look->m);
147 memcpy(work,out,sizeof(double)*look->m);
154 sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
155 of=fopen(buffer,"a");
156 for(j=0;j<look->m;j++)
157 fprintf(of,"%g, ",out[j]);
163 /* code the spectral envelope, and keep track of the actual
164 quantized values; we don't want creeping error as each block is
165 nailed to the last quantized value of the previous block. */
167 /* first stage is a bit different because quantization error must be
169 for(stage=0;stage<info->stages;stage++){
170 codebook *b=vb->vd->fullbooks+info->books[stage];
175 for(k=0;k<b->dim;k++)out[j+k]-=last;
176 bits+=vorbis_book_encodev(b,out+j,&vb->opb);
177 for(k=0;k<b->dim;k++,j++){
184 memcpy(out,work,sizeof(double)*look->m);
186 bits+=vorbis_book_encodev(b,out+j,&vb->opb);
187 for(k=0;k<b->dim;k++,j++)work[j]-=out[j];
191 /* take the coefficients back to a spectral envelope curve */
192 vorbis_lsp_to_lpc(out,out,look->m);
193 vorbis_lpc_to_curve(out,out,amp,&look->lpclook);
194 fprintf(stderr,"Encoded %ld LSP coefficients in %ld bits\n",look->m,bits);
198 fprintf(stderr,"Encoded %ld LSP coefficients in %ld bits\n",look->m,bits);
200 memset(out,0,sizeof(double)*look->n);
204 static int inverse(vorbis_block *vb,vorbis_look_floor *i,double *out){
205 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
206 vorbis_info_floor0 *info=look->vi;
209 long ampraw=_oggpack_read(&vb->opb,info->ampbits);
211 long ampscale=fromdB(info->ampdB);
212 long maxval=(1<<info->ampbits)-1;
213 double amp=fromdB((ampraw-.5)/maxval*info->ampdB)/ampscale;
215 memset(out,0,sizeof(double)*look->m);
216 for(stage=0;stage<info->stages;stage++){
217 codebook *b=vb->vd->fullbooks+info->books[stage];
218 for(j=0;j<look->m;j+=b->dim)
219 vorbis_book_decodev(b,out+j,&vb->opb);
223 for(k=0;k<b->dim;k++,j++)out[j]+=last;
230 /* take the coefficients back to a spectral envelope curve */
231 vorbis_lsp_to_lpc(out,out,look->m);
232 vorbis_lpc_to_curve(out,out,amp,&look->lpclook);
235 memset(out,0,sizeof(double)*look->n);
240 vorbis_func_floor floor0_exportbundle={
241 &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse