Commit monty_branch_20000724 to the mainline in prep for beta 2
[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.17 2000/08/15 09:09:43 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   int         map;
39   
40   int         parts;
41   codebook   *phrasebook;
42   codebook ***partbooks;
43
44   int         partvals;
45   int       **decodemap;
46 } vorbis_look_residue0;
47
48 void free_info(vorbis_info_residue *i){
49   if(i){
50     memset(i,0,sizeof(vorbis_info_residue0));
51     free(i);
52   }
53 }
54
55 void free_look(vorbis_look_residue *i){
56   int j;
57   if(i){
58     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
59     for(j=0;j<look->parts;j++)
60       if(look->partbooks[j])free(look->partbooks[j]);
61     free(look->partbooks);
62     for(j=0;j<look->partvals;j++)
63       free(look->decodemap[j]);
64     free(look->decodemap);
65     memset(i,0,sizeof(vorbis_look_residue0));
66     free(i);
67   }
68 }
69
70 void pack(vorbis_info_residue *vr,oggpack_buffer *opb){
71   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
72   int j,acc=0;
73   _oggpack_write(opb,info->begin,24);
74   _oggpack_write(opb,info->end,24);
75
76   _oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
77                                              code with a partitioned book */
78   _oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
79   _oggpack_write(opb,info->groupbook,8);  /* group huffman book */
80   for(j=0;j<info->partitions;j++){
81     _oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
82     acc+=info->secondstages[j];
83   }
84   for(j=0;j<acc;j++)
85     _oggpack_write(opb,info->booklist[j],8);
86
87 }
88
89 /* vorbis_info is for range checking */
90 vorbis_info_residue *unpack(vorbis_info *vi,oggpack_buffer *opb){
91   int j,acc=0;
92   vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
93
94   info->begin=_oggpack_read(opb,24);
95   info->end=_oggpack_read(opb,24);
96   info->grouping=_oggpack_read(opb,24)+1;
97   info->partitions=_oggpack_read(opb,6)+1;
98   info->groupbook=_oggpack_read(opb,8);
99   for(j=0;j<info->partitions;j++){
100     int cascade=info->secondstages[j]=_oggpack_read(opb,4);
101     if(cascade>1)goto errout; /* temporary!  when cascading gets
102                                  reworked and actually used, we don't
103                                  want old code to DTWT */
104     acc+=cascade;
105   }
106   for(j=0;j<acc;j++)
107     info->booklist[j]=_oggpack_read(opb,8);
108
109   if(info->groupbook>=vi->books)goto errout;
110   for(j=0;j<acc;j++)
111     if(info->booklist[j]>=vi->books)goto errout;
112
113   return(info);
114  errout:
115   free_info(info);
116   return(NULL);
117 }
118
119 vorbis_look_residue *look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
120                           vorbis_info_residue *vr){
121   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
122   vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
123   int j,k,acc=0;
124   int dim;
125   look->info=info;
126   look->map=vm->mapping;
127
128   look->parts=info->partitions;
129   look->phrasebook=vd->fullbooks+info->groupbook;
130   dim=look->phrasebook->dim;
131
132   look->partbooks=calloc(look->parts,sizeof(codebook **));
133
134   for(j=0;j<look->parts;j++){
135     int stages=info->secondstages[j];
136     if(stages){
137       look->partbooks[j]=malloc(stages*sizeof(codebook *));
138       for(k=0;k<stages;k++)
139         look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
140     }
141   }
142
143   look->partvals=rint(pow(look->parts,dim));
144   look->decodemap=malloc(look->partvals*sizeof(int *));
145   for(j=0;j<look->partvals;j++){
146     long val=j;
147     long mult=look->partvals/look->parts;
148     look->decodemap[j]=malloc(dim*sizeof(int));
149     for(k=0;k<dim;k++){
150       long deco=val/mult;
151       val-=deco*mult;
152       mult/=look->parts;
153       look->decodemap[j][k]=deco;
154     }
155   }
156
157   return(look);
158 }
159
160
161 /* does not guard against invalid settings; eg, a subn of 16 and a
162    subgroup request of 32.  Max subn of 128 */
163 static int _testhack(double *vec,int n,vorbis_look_residue0 *look,
164                      int auxparts,int auxpartnum){
165   vorbis_info_residue0 *info=look->info;
166   int i,j=0;
167   double max,localmax=0.;
168   double temp[128];
169   double entropy[8];
170
171   /* setup */
172   for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
173
174   /* handle case subgrp==1 outside */
175   for(i=0;i<n;i++)
176     if(temp[i]>localmax)localmax=temp[i];
177   max=localmax;
178
179   for(i=0;i<n;i++)temp[i]=rint(temp[i]);
180   
181   while(1){
182     entropy[j]=localmax;
183     n>>=1;
184     j++;
185
186     if(n<=0)break;
187     for(i=0;i<n;i++){
188       temp[i]+=temp[i+n];
189     }
190     localmax=0.;
191     for(i=0;i<n;i++)
192       if(temp[i]>localmax)localmax=temp[i];
193   }
194
195   for(i=0;i<auxparts-1;i++)
196     if(auxpartnum<info->blimit[i] &&
197        entropy[info->subgrp[i]]<=info->entmax[i] &&
198        max<=info->ampmax[i])
199       break;
200
201   return(i);
202 }
203
204 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
205                        int stages, codebook **books,int mode,int part){
206   int i,j,bits=0;
207
208   for(j=0;j<stages;j++){
209     int dim=books[j]->dim;
210     int step=n/dim;
211     for(i=0;i<step;i++){
212       int entry=vorbis_book_besterror(books[j],vec+i,step,0);
213 #ifdef TRAIN_RESENT      
214       {
215         char buf[80];
216         FILE *f;
217         sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
218         f=fopen(buf,"a");
219         fprintf(f,"%d\n",entry);
220         fclose(f);
221       }
222 #endif
223       bits+=vorbis_book_encode(books[j],entry,opb);
224     }
225   }
226
227   return(bits);
228 }
229
230 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
231                        int stages, codebook **books){
232   int i,j;
233   
234   memset(work,0,sizeof(double)*n);
235   for(j=0;j<stages;j++){
236     int dim=books[j]->dim;
237     int step=n/dim;
238     for(i=0;i<step;i++)
239       if(vorbis_book_decodevs(books[j],work+i,opb,step,0)==-1)
240         return(-1);
241   }
242   
243   for(i=0;i<n;i++)
244     vec[i]*=work[i];
245   
246   return(0);
247 }
248
249 int forward(vorbis_block *vb,vorbis_look_residue *vl,
250             double **in,int ch){
251   long i,j,k,l;
252   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
253   vorbis_info_residue0 *info=look->info;
254
255   /* move all this setup out later */
256   int samples_per_partition=info->grouping;
257   int possible_partitions=info->partitions;
258   int partitions_per_word=look->phrasebook->dim;
259   int n=info->end-info->begin;
260   long phrasebits=0,resbitsT=0;
261   long *resbits=alloca(sizeof(long)*possible_partitions);
262   long *resvals=alloca(sizeof(long)*possible_partitions);
263
264   int partvals=n/samples_per_partition;
265   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
266   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
267
268   partvals=partwords*partitions_per_word;
269
270   /* we find the patition type for each partition of each
271      channel.  We'll go back and do the interleaved encoding in a
272      bit.  For now, clarity */
273   
274   memset(resbits,0,sizeof(long)*possible_partitions);
275   memset(resvals,0,sizeof(long)*possible_partitions);
276
277   for(i=0;i<ch;i++){
278     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
279     memset(partword[i],0,n/samples_per_partition*sizeof(long));
280   }
281
282   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
283     for(j=0;j<ch;j++)
284       /* do the partition decision based on the number of 'bits'
285          needed to encode the block */
286       partword[j][l]=
287         _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
288   
289   }
290   /* we code the partition words for each channel, then the residual
291      words for a partition per channel until we've written all the
292      residual words for that partition word.  Then write the next
293      parition channel words... */
294   
295   for(i=info->begin,l=0;i<info->end;){
296     /* first we encode a partition codeword for each channel */
297     for(j=0;j<ch;j++){
298       long val=partword[j][l];
299       for(k=1;k<partitions_per_word;k++)
300         val= val*possible_partitions+partword[j][l+k];
301       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
302     }
303     /* now we encode interleaved residual values for the partitions */
304     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
305       for(j=0;j<ch;j++){
306         resbits[partword[j][l]]+=
307           _encodepart(&vb->opb,in[j]+i,samples_per_partition,
308                       info->secondstages[partword[j][l]],
309                       look->partbooks[partword[j][l]],look->map,partword[j][l]);
310         resvals[partword[j][l]]+=samples_per_partition;
311       }
312       
313   }
314
315   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
316   /*fprintf(stderr,
317           "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
318           ch*(info->end-info->begin),phrasebits,resbitsT);
319   for(i=0;i<possible_partitions;i++)
320     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
321     fprintf(stderr,"\n");*/
322  
323   return(0);
324 }
325
326 /* a truncated packet here just means 'stop working'; it's not an error */
327 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
328   long i,j,k,l,transend=vb->pcmend/2;
329   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
330   vorbis_info_residue0 *info=look->info;
331
332   /* move all this setup out later */
333   int samples_per_partition=info->grouping;
334   int partitions_per_word=look->phrasebook->dim;
335   int n=info->end-info->begin;
336
337   int partvals=n/samples_per_partition;
338   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
339   int **partword=alloca(ch*sizeof(long *));
340   double *work=alloca(sizeof(double)*samples_per_partition);
341   partvals=partwords*partitions_per_word;
342
343   /* make sure we're zeroed up to the start */
344   for(j=0;j<ch;j++)
345     memset(in[j],0,sizeof(double)*info->begin);
346
347   for(i=info->begin,l=0;i<info->end;){
348     /* fetch the partition word for each channel */
349     for(j=0;j<ch;j++){
350       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
351       if(temp==-1)goto eopbreak;
352       partword[j]=look->decodemap[temp];
353       if(partword[j]==NULL)goto errout;
354     }
355     
356     /* now we decode interleaved residual values for the partitions */
357     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
358       for(j=0;j<ch;j++){
359         int part=partword[j][k];
360         if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
361                     info->secondstages[part],
362                        look->partbooks[part])==-1)goto eopbreak;
363       }
364   }
365
366  eopbreak:
367   if(i<transend){
368     for(j=0;j<ch;j++)
369       memset(in[j]+i,0,sizeof(double)*(transend-i));
370   }
371
372   return(0);
373
374  errout:
375   for(j=0;j<ch;j++)
376     memset(in[j],0,sizeof(double)*transend);
377   return(0);
378 }
379
380 vorbis_func_residue residue0_exportbundle={
381   &pack,
382   &unpack,
383   &look,
384   &free_info,
385   &free_look,
386   &forward,
387   &inverse
388 };