API change to stabilize psychoacoustic configuration for 1.0 beta. It
[platform/upstream/libvorbis.git] / lib / res0.c
1 /********************************************************************
2  *                                                                  *
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.                            *
7  *                                                                  *
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/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: residue backend 0 implementation
15  last mod: $Id: res0.c,v 1.14 2000/06/14 08:19:22 xiphmont Exp $
16
17  ********************************************************************/
18
19 /* Slow, slow, slow, simpleminded and did I mention it was slow?  The
20    encode/decode loops are coded for clarity and performance is not
21    yet even a nagging little idea lurking in the shadows.  Oh and BTW,
22    it's slow. */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include "vorbis/codec.h"
29 #include "bitwise.h"
30 #include "registry.h"
31 #include "bookinternal.h"
32 #include "sharedbook.h"
33 #include "misc.h"
34 #include "os.h"
35
36 typedef struct {
37   vorbis_info_residue0 *info;
38   
39   int         parts;
40   codebook   *phrasebook;
41   codebook ***partbooks;
42
43   int         partvals;
44   int       **decodemap;
45 } vorbis_look_residue0;
46
47 void free_info(vorbis_info_residue *i){
48   if(i){
49     memset(i,0,sizeof(vorbis_info_residue0));
50     free(i);
51   }
52 }
53
54 void free_look(vorbis_look_residue *i){
55   int j;
56   if(i){
57     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
58     for(j=0;j<look->parts;j++)
59       if(look->partbooks[j])free(look->partbooks[j]);
60     free(look->partbooks);
61     for(j=0;j<look->partvals;j++)
62       free(look->decodemap[j]);
63     free(look->decodemap);
64     memset(i,0,sizeof(vorbis_look_residue0));
65     free(i);
66   }
67 }
68
69 void pack(vorbis_info_residue *vr,oggpack_buffer *opb){
70   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
71   int j,acc=0;
72   _oggpack_write(opb,info->begin,24);
73   _oggpack_write(opb,info->end,24);
74
75   _oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
76                                              code with a partitioned book */
77   _oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
78   _oggpack_write(opb,info->groupbook,8);  /* group huffman book */
79   for(j=0;j<info->partitions;j++){
80     _oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
81     acc+=info->secondstages[j];
82   }
83   for(j=0;j<acc;j++)
84     _oggpack_write(opb,info->booklist[j],8);
85
86 }
87
88 /* vorbis_info is for range checking */
89 vorbis_info_residue *unpack(vorbis_info *vi,oggpack_buffer *opb){
90   int j,acc=0;
91   vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
92
93   info->begin=_oggpack_read(opb,24);
94   info->end=_oggpack_read(opb,24);
95   info->grouping=_oggpack_read(opb,24)+1;
96   info->partitions=_oggpack_read(opb,6)+1;
97   info->groupbook=_oggpack_read(opb,8);
98   for(j=0;j<info->partitions;j++){
99     acc+=info->secondstages[j]=_oggpack_read(opb,4);
100   }
101   for(j=0;j<acc;j++)
102     info->booklist[j]=_oggpack_read(opb,8);
103
104   if(info->groupbook>=vi->books)goto errout;
105   for(j=0;j<acc;j++)
106     if(info->booklist[j]>=vi->books)goto errout;
107
108   return(info);
109  errout:
110   free_info(info);
111   return(NULL);
112 }
113
114 vorbis_look_residue *look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
115                           vorbis_info_residue *vr){
116   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
117   vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
118   int j,k,acc=0;
119   int dim;
120   look->info=info;
121
122   look->parts=info->partitions;
123   look->phrasebook=vd->fullbooks+info->groupbook;
124   dim=look->phrasebook->dim;
125
126   look->partbooks=calloc(look->parts,sizeof(codebook **));
127
128   for(j=0;j<look->parts;j++){
129     int stages=info->secondstages[j];
130     if(stages){
131       look->partbooks[j]=malloc(stages*sizeof(codebook *));
132       for(k=0;k<stages;k++)
133         look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
134     }
135   }
136
137   look->partvals=rint(pow(look->parts,dim));
138   look->decodemap=malloc(look->partvals*sizeof(int *));
139   for(j=0;j<look->partvals;j++){
140     long val=j;
141     long mult=look->partvals/look->parts;
142     look->decodemap[j]=malloc(dim*sizeof(int));
143     for(k=0;k<dim;k++){
144       long deco=val/mult;
145       val-=deco*mult;
146       mult/=look->parts;
147       look->decodemap[j][k]=deco;
148     }
149   }
150
151   return(look);
152 }
153
154
155 /* does not guard against invalid settings; eg, a subn of 16 and a
156    subgroup request of 32.  Max subn of 128 */
157 static int _testhack(double *vec,int n,vorbis_look_residue0 *look,
158                      int auxparts,int auxpartnum){
159   vorbis_info_residue0 *info=look->info;
160   int i,j=0;
161   double max,localmax=0.;
162   double temp[128];
163   double entropy[8];
164
165   /* setup */
166   for(i=0;i<n;i++)temp[i]=fabs(rint(vec[i]));
167
168   /* handle case subgrp==1 outside */
169   for(i=0;i<n;i++)
170     if(temp[i]>localmax)localmax=temp[i];
171   max=localmax;
172   
173   while(1){
174     entropy[j]=localmax;
175     n>>=1;
176     j++;
177
178     if(n<=0)break;
179     for(i=0;i<n;i++){
180       temp[i]+=temp[i+n];
181     }
182     localmax=0.;
183     for(i=0;i<n;i++)
184       if(temp[i]>localmax)localmax=temp[i];
185   }
186
187   for(i=0;i<auxparts-1;i++)
188     if(auxpartnum<info->blimit[i] &&
189        entropy[info->subgrp[i]]<=info->entmax[i] &&
190        max<=info->ampmax[i])
191       break;
192
193   return(i);
194 }
195
196 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
197                        int stages, codebook **books){
198   int i,j,bits=0;
199
200   for(j=0;j<stages;j++){
201     int dim=books[j]->dim;
202     int step=n/dim;
203     for(i=0;i<step;i++)
204       bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
205  
206   }
207
208   return(bits);
209 }
210
211 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
212                        int stages, codebook **books){
213   int i,j;
214   
215   memset(work,0,sizeof(double)*n);
216   for(j=0;j<stages;j++){
217     int dim=books[j]->dim;
218     int step=n/dim;
219     for(i=0;i<step;i++)
220       vorbis_book_decodevs(books[j],work+i,opb,step,0);
221   }
222   
223   for(i=0;i<n;i++)
224     vec[i]*=work[i];
225   
226   return(0);
227 }
228
229 int forward(vorbis_block *vb,vorbis_look_residue *vl,
230             double **in,int ch){
231   long i,j,k,l;
232   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
233   vorbis_info_residue0 *info=look->info;
234
235   /* move all this setup out later */
236   int samples_per_partition=info->grouping;
237   int possible_partitions=info->partitions;
238   int partitions_per_word=look->phrasebook->dim;
239   int n=info->end-info->begin;
240   long phrasebits=0,resbitsT=0;
241   long *resbits=alloca(sizeof(long)*possible_partitions);
242   long *resvals=alloca(sizeof(long)*possible_partitions);
243
244   int partvals=n/samples_per_partition;
245   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
246   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
247
248   partvals=partwords*partitions_per_word;
249
250   /* we find the patition type for each partition of each
251      channel.  We'll go back and do the interleaved encoding in a
252      bit.  For now, clarity */
253   
254   memset(resbits,0,sizeof(long)*possible_partitions);
255   memset(resvals,0,sizeof(long)*possible_partitions);
256
257   for(i=0;i<ch;i++){
258     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
259     memset(partword[i],0,n/samples_per_partition*sizeof(long));
260   }
261
262   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
263     for(j=0;j<ch;j++)
264       /* do the partition decision based on the number of 'bits'
265          needed to encode the block */
266       partword[j][l]=
267         _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
268   
269   }
270   /* we code the partition words for each channel, then the residual
271      words for a partition per channel until we've written all the
272      residual words for that partition word.  Then write the next
273      parition channel words... */
274   
275   for(i=info->begin,l=0;i<info->end;){
276     /* first we encode a partition codeword for each channel */
277     for(j=0;j<ch;j++){
278       long val=partword[j][l];
279       for(k=1;k<partitions_per_word;k++)
280         val= val*possible_partitions+partword[j][l+k];
281       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
282     }
283     /* now we encode interleaved residual values for the partitions */
284     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
285       for(j=0;j<ch;j++){
286         resbits[partword[j][l]]+=
287           _encodepart(&vb->opb,in[j]+i,samples_per_partition,
288                       info->secondstages[partword[j][l]],
289                       look->partbooks[partword[j][l]]);
290         resvals[partword[j][l]]+=samples_per_partition;
291       }
292       
293   }
294
295   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
296   /*fprintf(stderr,
297           "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
298           ch*(info->end-info->begin),phrasebits,resbitsT);
299   for(i=0;i<possible_partitions;i++)
300     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
301     fprintf(stderr,"\n");*/
302  
303   return(0);
304 }
305
306 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
307   long i,j,k,l;
308   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
309   vorbis_info_residue0 *info=look->info;
310
311   /* move all this setup out later */
312   int samples_per_partition=info->grouping;
313   int partitions_per_word=look->phrasebook->dim;
314   int n=info->end-info->begin;
315
316   int partvals=n/samples_per_partition;
317   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
318   int **partword=alloca(ch*sizeof(long *));
319   double *work=alloca(sizeof(double)*samples_per_partition);
320   partvals=partwords*partitions_per_word;
321
322   for(i=info->begin,l=0;i<info->end;){
323     /* fetch the partition word for each channel */
324     for(j=0;j<ch;j++){
325       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
326       partword[j]=look->decodemap[temp];
327       if(partword[j]==NULL)exit(1);
328     }
329     
330     /* now we decode interleaved residual values for the partitions */
331     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
332       for(j=0;j<ch;j++){
333         int part=partword[j][k];
334         _decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
335                     info->secondstages[part],
336                     look->partbooks[part]);
337       }
338   }
339
340   return(0);
341 }
342
343 vorbis_func_residue residue0_exportbundle={
344   &pack,
345   &unpack,
346   &look,
347   &free_info,
348   &free_look,
349   &forward,
350   &inverse
351 };