A fix to fix the last fix.
[platform/upstream/libvorbis.git] / lib / res0.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: residue backend 0 and 1 implementation
14  last mod: $Id: res0.c,v 1.30 2001/06/05 01:14:44 xiphmont Exp $
15
16  ********************************************************************/
17
18 /* Slow, slow, slow, simpleminded and did I mention it was slow?  The
19    encode/decode loops are coded for clarity and performance is not
20    yet even a nagging little idea lurking in the shadows.  Oh and BTW,
21    it's slow. */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <ogg/ogg.h>
28 #include "vorbis/codec.h"
29 #include "codec_internal.h"
30 #include "registry.h"
31 #include "codebook.h"
32 #include "misc.h"
33 #include "os.h"
34
35 typedef struct {
36   vorbis_info_residue0 *info;
37   int         map;
38   
39   int         parts;
40   int         stages;
41   codebook   *fullbooks;
42   codebook   *phrasebook;
43   codebook ***partbooks;
44
45   int         partvals;
46   int       **decodemap;
47
48 } vorbis_look_residue0;
49
50 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
51   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
52   vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
53   memcpy(ret,info,sizeof(vorbis_info_residue0));
54   return(ret);
55 }
56
57 void res0_free_info(vorbis_info_residue *i){
58   if(i){
59     memset(i,0,sizeof(vorbis_info_residue0));
60     _ogg_free(i);
61   }
62 }
63
64 void res0_free_look(vorbis_look_residue *i){
65   int j;
66   if(i){
67     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
68
69     for(j=0;j<look->parts;j++)
70       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
71     _ogg_free(look->partbooks);
72     for(j=0;j<look->partvals;j++)
73       _ogg_free(look->decodemap[j]);
74     _ogg_free(look->decodemap);
75     memset(i,0,sizeof(vorbis_look_residue0));
76     _ogg_free(i);
77   }
78 }
79
80 static int ilog(unsigned int v){
81   int ret=0;
82   while(v){
83     ret++;
84     v>>=1;
85   }
86   return(ret);
87 }
88
89 static int icount(unsigned int v){
90   int ret=0;
91   while(v){
92     ret+=v&1;
93     v>>=1;
94   }
95   return(ret);
96 }
97
98
99 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
100   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
101   int j,acc=0;
102   oggpack_write(opb,info->begin,24);
103   oggpack_write(opb,info->end,24);
104
105   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
106                                              code with a partitioned book */
107   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
108   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
109
110   /* secondstages is a bitmask; as encoding progresses pass by pass, a
111      bitmask of one indicates this partition class has bits to write
112      this pass */
113   for(j=0;j<info->partitions;j++){
114     if(ilog(info->secondstages[j])>3){
115       /* yes, this is a minor hack due to not thinking ahead */
116       oggpack_write(opb,info->secondstages[j],3); 
117       oggpack_write(opb,1,1);
118       oggpack_write(opb,info->secondstages[j]>>3,5); 
119     }else
120       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
121     acc+=icount(info->secondstages[j]);
122   }
123   for(j=0;j<acc;j++)
124     oggpack_write(opb,info->booklist[j],8);
125
126 }
127
128 /* vorbis_info is for range checking */
129 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
130   int j,acc=0;
131   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
132   codec_setup_info     *ci=vi->codec_setup;
133
134   info->begin=oggpack_read(opb,24);
135   info->end=oggpack_read(opb,24);
136   info->grouping=oggpack_read(opb,24)+1;
137   info->partitions=oggpack_read(opb,6)+1;
138   info->groupbook=oggpack_read(opb,8);
139
140   for(j=0;j<info->partitions;j++){
141     int cascade=oggpack_read(opb,3);
142     if(oggpack_read(opb,1))
143       cascade|=(oggpack_read(opb,5)<<3);
144     info->secondstages[j]=cascade;
145
146     acc+=icount(cascade);
147   }
148   for(j=0;j<acc;j++)
149     info->booklist[j]=oggpack_read(opb,8);
150
151   if(info->groupbook>=ci->books)goto errout;
152   for(j=0;j<acc;j++)
153     if(info->booklist[j]>=ci->books)goto errout;
154
155   return(info);
156  errout:
157   res0_free_info(info);
158   return(NULL);
159 }
160
161 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
162                           vorbis_info_residue *vr){
163   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
164   vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
165   backend_lookup_state *be=vd->backend_state;
166
167   int j,k,acc=0;
168   int dim;
169   int maxstage=0;
170   look->info=info;
171   look->map=vm->mapping;
172
173   look->parts=info->partitions;
174   look->fullbooks=be->fullbooks;
175   look->phrasebook=be->fullbooks+info->groupbook;
176   dim=look->phrasebook->dim;
177
178   look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
179
180   for(j=0;j<look->parts;j++){
181     int stages=ilog(info->secondstages[j]);
182     if(stages){
183       if(stages>maxstage)maxstage=stages;
184       look->partbooks[j]=_ogg_calloc(stages,sizeof(codebook *));
185       for(k=0;k<stages;k++)
186         if(info->secondstages[j]&(1<<k))
187           look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
188     }
189   }
190
191   look->partvals=rint(pow(look->parts,dim));
192   look->stages=maxstage;
193   look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
194   for(j=0;j<look->partvals;j++){
195     long val=j;
196     long mult=look->partvals/look->parts;
197     look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
198     for(k=0;k<dim;k++){
199       long deco=val/mult;
200       val-=deco*mult;
201       mult/=look->parts;
202       look->decodemap[j][k]=deco;
203     }
204   }
205
206   return(look);
207 }
208
209
210 /* does not guard against invalid settings; eg, a subn of 16 and a
211    subgroup request of 32.  Max subn of 128 */
212 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
213                                  int auxparts,int auxpartnum){
214   vorbis_info_residue0 *info=look->info;
215   int i,j=0;
216   float max,localmax=0.f;
217   float temp[128];
218   float entropy[8];
219
220   /* setup */
221   for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
222
223   /* handle case subgrp==1 outside */
224   for(i=0;i<n;i++)
225     if(temp[i]>localmax)localmax=temp[i];
226   max=localmax;
227
228   for(i=0;i<n;i++)temp[i]=rint(temp[i]);
229   
230   while(1){
231     entropy[j]=localmax;
232     n>>=1;
233     if(!n)break;
234     j++;
235
236     for(i=0;i<n;i++){
237       temp[i]+=temp[i+n];
238     }
239     localmax=0.f;
240     for(i=0;i<n;i++)
241       if(temp[i]>localmax)localmax=temp[i];
242   }
243
244   for(i=0;i<auxparts-1;i++)
245     if(auxpartnum<info->blimit[i] &&
246        entropy[info->subgrp[i]]<=info->entmax[i] &&
247        max<=info->ampmax[i])
248       break;
249
250   return(i);
251 }
252
253 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
254                      int auxparts,int auxpartnum){
255   vorbis_info_residue0 *info=look->info;
256   int i,j=0;
257   float max,localmax=0.f;
258   float temp[128];
259   float entropy[8];
260
261   /* setup */
262   for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
263
264   /* handle case subgrp==1 outside */
265   for(i=0;i<n;i++)
266     if(temp[i]>localmax)localmax=temp[i];
267   max=localmax;
268
269   for(i=0;i<n;i++)temp[i]=rint(temp[i]);
270   
271   while(n){
272     entropy[j]=localmax;
273     n>>=1;
274     j++;
275     if(!n)break;
276     for(i=0;i<n;i++){
277       temp[i]=temp[i*2]+temp[i*2+1];
278     }
279     localmax=0.f;
280     for(i=0;i<n;i++)
281       if(temp[i]>localmax)localmax=temp[i];
282   }
283
284   for(i=0;i<auxparts-1;i++)
285     if(auxpartnum<info->blimit[i] &&
286        entropy[info->subgrp[i]]<=info->entmax[i] &&
287        max<=info->ampmax[i])
288       break;
289
290   return(i);
291 }
292
293 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
294                                    codebook *book,vorbis_look_residue0 *look){
295   int i,bits=0;
296   int dim=book->dim;
297   int step=n/dim;
298 #ifdef TRAIN_RESENT      
299   char buf[80];
300   FILE *f;
301   sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
302   f=fopen(buf,"a");
303 #endif
304
305   for(i=0;i<step;i++){
306     int entry=vorbis_book_besterror(book,vec+i,step,0);
307
308 #ifdef TRAIN_RESENT      
309     fprintf(f,"%d\n",entry);
310 #endif
311
312     bits+=vorbis_book_encode(book,entry,opb);
313   }
314
315 #ifdef TRAIN_RESENT      
316   fclose(f);
317 #endif
318   return(bits);
319 }
320  
321 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
322                        codebook *book,vorbis_look_residue0 *look){
323   int i,bits=0;
324   int dim=book->dim;
325   int step=n/dim;
326 #ifdef TRAIN_RESENT      
327   char buf[80];
328   FILE *f;
329   sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
330   f=fopen(buf,"a");
331 #endif
332
333   for(i=0;i<step;i++){
334     int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
335
336 #ifdef TRAIN_RESENT      
337     fprintf(f,"%d\n",entry);
338 #endif
339
340     bits+=vorbis_book_encode(book,entry,opb);
341   }
342
343 #ifdef TRAIN_RESENT      
344   fclose(f);
345 #endif
346   return(bits);
347 }
348
349 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
350                       float **in,int ch,
351                       int (*classify)(float *,int,vorbis_look_residue0 *,
352                                       int,int),
353                       int (*encode)(oggpack_buffer *,float *,int,
354                                     codebook *,vorbis_look_residue0 *)){
355   long i,j,k,l,s;
356   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
357   vorbis_info_residue0 *info=look->info;
358
359   /* move all this setup out later */
360   int samples_per_partition=info->grouping;
361   int possible_partitions=info->partitions;
362   int partitions_per_word=look->phrasebook->dim;
363   int n=info->end-info->begin;
364
365   int partvals=n/samples_per_partition;
366   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
367   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
368
369   partvals=partwords*partitions_per_word;
370
371   /* we find the patition type for each partition of each
372      channel.  We'll go back and do the interleaved encoding in a
373      bit.  For now, clarity */
374  
375   for(i=0;i<ch;i++){
376     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
377     memset(partword[i],0,n/samples_per_partition*sizeof(long));
378   }
379
380   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
381     for(j=0;j<ch;j++)
382       /* do the partition decision based on the 'entropy'
383          int the block */
384       partword[j][l]=
385         classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
386   
387   }
388
389   /* we code the partition words for each channel, then the residual
390      words for a partition per channel until we've written all the
391      residual words for that partition word.  Then write the next
392      partition channel words... */
393
394   for(s=0;s<look->stages;s++){
395     for(i=info->begin,l=0;i<info->end;){
396       
397       /* first we encode a partition codeword for each channel */
398       if(s==0){
399         for(j=0;j<ch;j++){
400           long val=partword[j][l];
401           for(k=1;k<partitions_per_word;k++)
402             val= val*possible_partitions+partword[j][l+k];
403           vorbis_book_encode(look->phrasebook,val,&vb->opb);
404         }
405       }
406       
407       /* now we encode interleaved residual values for the partitions */
408       for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition){
409         
410         for(j=0;j<ch;j++){
411           if(info->secondstages[partword[j][l]]&(1<<s)){
412             codebook *statebook=look->partbooks[partword[j][l]][s];
413             if(statebook){
414               int ret=encode(&vb->opb,in[j]+i,samples_per_partition,
415                              statebook,look);
416             }
417           }
418         }
419       }
420     }
421   }
422   
423   return(0);
424 }
425
426 /* a truncated packet here just means 'stop working'; it's not an error */
427 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
428                       float **in,int ch,
429                       long (*decodepart)(codebook *, float *, 
430                                          oggpack_buffer *,int,int)){
431
432   long i,j,k,l,s,transend=vb->pcmend/2;
433   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
434   vorbis_info_residue0 *info=look->info;
435
436   /* move all this setup out later */
437   int samples_per_partition=info->grouping;
438   int partitions_per_word=look->phrasebook->dim;
439   int n=info->end-info->begin;
440
441   int partvals=n/samples_per_partition;
442   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
443   int ***partword=alloca(ch*sizeof(int **));
444   float **work=alloca(ch*sizeof(float *));
445   partvals=partwords*partitions_per_word;
446
447   /* make sure we're zeroed up to the start */
448   for(j=0;j<ch;j++){
449     work[j]=_vorbis_block_alloc(vb,n*sizeof(float));
450     partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
451     memset(work[j],0,sizeof(float)*n);
452   }
453
454   for(s=0;s<look->stages;s++){
455     for(i=info->begin,l=0;i<info->end;l++){
456
457       if(s==0){
458         /* fetch the partition word for each channel */
459         for(j=0;j<ch;j++){
460           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
461           if(temp==-1)goto eopbreak;
462           partword[j][l]=look->decodemap[temp];
463           if(partword[j][l]==NULL)goto errout;
464         }
465       }
466       
467       /* now we decode residual values for the partitions */
468       for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
469         for(j=0;j<ch;j++){
470           if(info->secondstages[partword[j][l][k]]&(1<<s)){
471             codebook *stagebook=look->partbooks[partword[j][l][k]][s];
472             if(stagebook){
473               if(decodepart(stagebook,work[j]+i,&vb->opb,
474                             samples_per_partition,0)==-1)goto eopbreak;
475             }
476           }
477         }
478     } 
479   }
480
481  eopbreak:
482   
483   for(j=0;j<ch;j++){
484     for(i=0;i<n;i++)
485       in[j][i]*=work[j][i];
486     for(;i<transend;i++)
487       in[j][i]=0;
488   }
489
490   return(0);
491
492  errout:
493   for(j=0;j<ch;j++)
494     memset(in[j],0,sizeof(float)*transend);
495   return(0);
496 }
497
498 /* residue 0 and 1 are just slight variants of one another. 0 is
499    interleaved, 1 is not */
500 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
501             float **in,int ch){
502   return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
503 }
504
505 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
506   return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs));
507 }
508
509 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
510                  float **in,int ch){
511   return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
512 }
513
514 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
515   return(_01inverse(vb,vl,in,ch,vorbis_book_decodev));
516 }
517
518 vorbis_func_residue residue0_exportbundle={
519   &res0_pack,
520   &res0_unpack,
521   &res0_look,
522   &res0_copy_info,
523   &res0_free_info,
524   &res0_free_look,
525   &res0_forward,
526   &res0_inverse
527 };
528
529 vorbis_func_residue residue1_exportbundle={
530   &res0_pack,
531   &res0_unpack,
532   &res0_look,
533   &res0_copy_info,
534   &res0_free_info,
535   &res0_free_look,
536   &res1_forward,
537   &res1_inverse
538 };