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