Kill some debugging output
[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.29 2001/06/04 05:56:42 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     long resbitsT=0;
69     long resvalsT=0;
70
71     for(j=0;j<look->parts;j++)
72       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
73     _ogg_free(look->partbooks);
74     for(j=0;j<look->partvals;j++)
75       _ogg_free(look->decodemap[j]);
76     _ogg_free(look->decodemap);
77     memset(i,0,sizeof(vorbis_look_residue0));
78     _ogg_free(i);
79   }
80 }
81
82 static int ilog(unsigned int v){
83   int ret=0;
84   while(v){
85     ret++;
86     v>>=1;
87   }
88   return(ret);
89 }
90
91 static int icount(unsigned int v){
92   int ret=0;
93   while(v){
94     ret+=v&1;
95     v>>=1;
96   }
97   return(ret);
98 }
99
100
101 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
102   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
103   int j,acc=0;
104   oggpack_write(opb,info->begin,24);
105   oggpack_write(opb,info->end,24);
106
107   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
108                                              code with a partitioned book */
109   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
110   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
111
112   /* secondstages is a bitmask; as encoding progresses pass by pass, a
113      bitmask of one indicates this partition class has bits to write
114      this pass */
115   for(j=0;j<info->partitions;j++){
116     if(ilog(info->secondstages[j])>3){
117       /* yes, this is a minor hack due to not thinking ahead */
118       oggpack_write(opb,info->secondstages[j],3); 
119       oggpack_write(opb,1,1);
120       oggpack_write(opb,info->secondstages[j]>>3,5); 
121     }else
122       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
123     acc+=icount(info->secondstages[j]);
124   }
125   for(j=0;j<acc;j++)
126     oggpack_write(opb,info->booklist[j],8);
127
128 }
129
130 /* vorbis_info is for range checking */
131 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
132   int j,acc=0;
133   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
134   codec_setup_info     *ci=vi->codec_setup;
135
136   info->begin=oggpack_read(opb,24);
137   info->end=oggpack_read(opb,24);
138   info->grouping=oggpack_read(opb,24)+1;
139   info->partitions=oggpack_read(opb,6)+1;
140   info->groupbook=oggpack_read(opb,8);
141
142   for(j=0;j<info->partitions;j++){
143     int cascade=info->secondstages[j]=oggpack_read(opb,3);
144     if(oggpack_read(opb,1))
145       cascade|=(oggpack_read(opb,5)<<3);
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           look->phrase+=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               look->bits[partword[j][l]]+=ret;
417               if(s==0)look->vals[partword[j][l]]+=samples_per_partition;
418             }
419           }
420         }
421       }
422     }
423   }
424
425   return(0);
426 }
427
428 /* a truncated packet here just means 'stop working'; it's not an error */
429 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
430                       float **in,int ch,
431                       long (*decodepart)(codebook *, float *, 
432                                          oggpack_buffer *,int,int)){
433
434   long i,j,k,l,s,transend=vb->pcmend/2;
435   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
436   vorbis_info_residue0 *info=look->info;
437
438   /* move all this setup out later */
439   int samples_per_partition=info->grouping;
440   int partitions_per_word=look->phrasebook->dim;
441   int n=info->end-info->begin;
442
443   int partvals=n/samples_per_partition;
444   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
445   int ***partword=alloca(ch*sizeof(int **));
446   float **work=alloca(ch*sizeof(float *));
447   partvals=partwords*partitions_per_word;
448
449   /* make sure we're zeroed up to the start */
450   for(j=0;j<ch;j++){
451     work[j]=_vorbis_block_alloc(vb,n*sizeof(float));
452     partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
453     memset(work[j],0,sizeof(float)*n);
454   }
455
456   for(s=0;s<look->stages;s++){
457     for(i=info->begin,l=0;i<info->end;l++){
458
459       if(s==0){
460         /* fetch the partition word for each channel */
461         for(j=0;j<ch;j++){
462           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
463           if(temp==-1)goto eopbreak;
464           partword[j][l]=look->decodemap[temp];
465           if(partword[j][l]==NULL)goto errout;
466         }
467       }
468       
469       /* now we decode residual values for the partitions */
470       for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
471         for(j=0;j<ch;j++){
472           if(info->secondstages[partword[j][l][k]]&(1<<s)){
473             codebook *stagebook=look->partbooks[partword[j][l][k]][s];
474             if(stagebook){
475               if(decodepart(stagebook,work[j]+i,&vb->opb,
476                             samples_per_partition,0)==-1)goto eopbreak;
477             }
478           }
479         }
480     } 
481   }
482
483  eopbreak:
484   
485   for(j=0;j<ch;j++){
486     for(i=0;i<n;i++)
487       in[j][i]*=work[j][i];
488     for(;i<transend;i++)
489       in[j][i]=0;
490   }
491
492   return(0);
493
494  errout:
495   for(j=0;j<ch;j++)
496     memset(in[j],0,sizeof(float)*transend);
497   return(0);
498 }
499
500 /* residue 0 and 1 are just slight variants of one another. 0 is
501    interleaved, 1 is not */
502 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
503             float **in,int ch){
504   return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
505 }
506
507 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
508   return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs));
509 }
510
511 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
512                  float **in,int ch){
513   return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
514 }
515
516 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
517   return(_01inverse(vb,vl,in,ch,vorbis_book_decodev));
518 }
519
520 vorbis_func_residue residue0_exportbundle={
521   &res0_pack,
522   &res0_unpack,
523   &res0_look,
524   &res0_copy_info,
525   &res0_free_info,
526   &res0_free_look,
527   &res0_forward,
528   &res0_inverse
529 };
530
531 vorbis_func_residue residue1_exportbundle={
532   &res0_pack,
533   &res0_unpack,
534   &res0_look,
535   &res0_copy_info,
536   &res0_free_info,
537   &res0_free_look,
538   &res1_forward,
539   &res1_inverse
540 };