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.12 2000/03/10 13:21:18 xiphmont Exp $
17 ********************************************************************/
22 #include "vorbis/codec.h"
27 #include "bookinternal.h"
38 vorbis_info_floor0 *vi;
42 static void free_info(vorbis_info_floor *i){
44 memset(i,0,sizeof(vorbis_info_floor0));
49 static void free_look(vorbis_look_floor *i){
50 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
52 lpc_clear(&look->lpclook);
53 memset(look,0,sizeof(vorbis_look_floor0));
58 static void pack (vorbis_info_floor *i,oggpack_buffer *opb){
59 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
61 _oggpack_write(opb,info->order,8);
62 _oggpack_write(opb,info->rate,16);
63 _oggpack_write(opb,info->barkmap,16);
64 _oggpack_write(opb,info->ampbits,6);
65 _oggpack_write(opb,info->ampdB,8);
66 _oggpack_write(opb,info->stages-1,4);
67 for(j=0;j<info->stages;j++)
68 _oggpack_write(opb,info->books[j],8);
71 static vorbis_info_floor *unpack (vorbis_info *vi,oggpack_buffer *opb){
73 vorbis_info_floor0 *info=malloc(sizeof(vorbis_info_floor0));
74 info->order=_oggpack_read(opb,8);
75 info->rate=_oggpack_read(opb,16);
76 info->barkmap=_oggpack_read(opb,16);
77 info->ampbits=_oggpack_read(opb,6);
78 info->ampdB=_oggpack_read(opb,8);
79 info->stages=_oggpack_read(opb,4)+1;
81 if(info->order<1)goto err_out;
82 if(info->rate<1)goto err_out;
83 if(info->barkmap<1)goto err_out;
84 if(info->stages<1)goto err_out;
86 for(j=0;j<info->stages;j++){
87 info->books[j]=_oggpack_read(opb,8);
88 if(info->books[j]<0 || info->books[j]>=vi->books)goto err_out;
96 static vorbis_look_floor *look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
97 vorbis_info_floor *i){
98 vorbis_info *vi=vd->vi;
99 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
100 vorbis_look_floor0 *look=malloc(sizeof(vorbis_look_floor0));
102 look->n=vi->blocksizes[mi->blockflag]/2;
104 lpc_init(&look->lpclook,look->n,info->barkmap,info->rate,look->m);
111 static int forward(vorbis_block *vb,vorbis_look_floor *i,
112 double *in,double *out){
114 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
115 vorbis_info_floor0 *info=look->vi;
119 /* use 'out' as temp storage */
120 /* Convert our floor to a set of lpc coefficients */
121 amp=sqrt(vorbis_curve_to_lpc(in,out,&look->lpclook));
123 /* amp is in the range 0. to 1. (well, more like .7). Log scale it */
126 (1<<ampbits)-1 == amp dB = 1. amp */
128 long ampscale=fromdB(info->ampdB);
129 long maxval=(1<<info->ampbits)-1;
131 long val=todB(amp*ampscale)/info->ampdB*maxval+1;
133 if(val<0)val=0; /* likely */
134 if(val>maxval)val=maxval; /* not bloody likely */
136 _oggpack_write(&vb->opb,val,info->ampbits);
138 amp=fromdB((val-.5)/maxval*info->ampdB)/ampscale;
144 double *work=alloca(sizeof(double)*look->m);
146 /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
147 vorbis_lpc_to_lsp(out,out,look->m);
148 memcpy(work,out,sizeof(double)*look->m);
155 sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
156 of=fopen(buffer,"a");
157 for(j=0;j<look->m;j++)
158 fprintf(of,"%g, ",out[j]);
164 /* code the spectral envelope, and keep track of the actual
165 quantized values; we don't want creeping error as each block is
166 nailed to the last quantized value of the previous block. */
168 /* first stage is a bit different because quantization error must be
170 for(stage=0;stage<info->stages;stage++){
171 codebook *b=vb->vd->fullbooks+info->books[stage];
176 for(k=0;k<b->dim;k++)out[j+k]-=last;
177 bits+=vorbis_book_encodev(b,out+j,&vb->opb);
178 for(k=0;k<b->dim;k++,j++){
185 memcpy(out,work,sizeof(double)*look->m);
187 bits+=vorbis_book_encodev(b,out+j,&vb->opb);
188 for(k=0;k<b->dim;k++,j++)work[j]-=out[j];
192 /* take the coefficients back to a spectral envelope curve */
193 vorbis_lsp_to_lpc(out,out,look->m);
194 vorbis_lpc_to_curve(out,out,amp,&look->lpclook);
195 fprintf(stderr,"Encoded %ld LSP coefficients in %ld bits\n",look->m,bits);
199 fprintf(stderr,"Encoded %ld LSP coefficients in %ld bits\n",look->m,bits);
201 memset(out,0,sizeof(double)*look->n);
205 static int inverse(vorbis_block *vb,vorbis_look_floor *i,double *out){
206 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
207 vorbis_info_floor0 *info=look->vi;
210 long ampraw=_oggpack_read(&vb->opb,info->ampbits);
212 long ampscale=fromdB(info->ampdB);
213 long maxval=(1<<info->ampbits)-1;
214 double amp=fromdB((ampraw-.5)/maxval*info->ampdB)/ampscale;
216 memset(out,0,sizeof(double)*look->m);
217 for(stage=0;stage<info->stages;stage++){
218 codebook *b=vb->vd->fullbooks+info->books[stage];
219 for(j=0;j<look->m;j+=b->dim)
220 vorbis_book_decodev(b,out+j,&vb->opb);
224 for(k=0;k<b->dim;k++,j++)out[j]+=last;
231 /* take the coefficients back to a spectral envelope curve */
232 vorbis_lsp_to_lpc(out,out,look->m);
233 vorbis_lpc_to_curve(out,out,amp,&look->lpclook);
236 memset(out,0,sizeof(double)*look->n);
241 vorbis_func_floor floor0_exportbundle={
242 &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse