First merge of new psychoacoustics. Have some unused codebooks to
[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.12 2000/05/08 20:49: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     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 /* classify by max quantized amplitude only */
155 static int _testhack(double *vec,int n,vorbis_look_residue0 *look){
156   vorbis_info_residue0 *info=look->info;
157   double max=0.;
158   int i;
159   
160   for(i=0;i<n;i++)
161     if(fabs(vec[i])>max)max=fabs(vec[i]);
162   
163   for(i=0;i<look->parts-1;i++)
164     if(max>=info->ampmax[i])
165       break;
166   return(i);
167 }
168
169 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
170                        int stages, codebook **books){
171   int i,j,bits=0;
172
173   for(j=0;j<stages;j++){
174     int dim=books[j]->dim;
175     int step=n/dim;
176     for(i=0;i<step;i++)
177       bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
178  
179   }
180
181   return(bits);
182 }
183
184 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
185                        int stages, codebook **books){
186   int i,j;
187   
188   memset(work,0,sizeof(double)*n);
189   for(j=0;j<stages;j++){
190     int dim=books[j]->dim;
191     int step=n/dim;
192     for(i=0;i<step;i++)
193       vorbis_book_decodevs(books[j],work+i,opb,step,0);
194   }
195   
196   for(i=0;i<n;i++)
197     vec[i]*=work[i];
198   
199   return(0);
200 }
201
202 int forward(vorbis_block *vb,vorbis_look_residue *vl,
203             double **in,int ch){
204   long i,j,k,l;
205   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
206   vorbis_info_residue0 *info=look->info;
207
208   /* move all this setup out later */
209   int samples_per_partition=info->grouping;
210   int possible_partitions=info->partitions;
211   int partitions_per_word=look->phrasebook->dim;
212   int n=info->end-info->begin;
213   long phrasebits=0,resbitsT=0;
214   long *resbits=alloca(sizeof(long)*possible_partitions);
215   long *resvals=alloca(sizeof(long)*possible_partitions);
216
217   int partvals=n/samples_per_partition;
218   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
219   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
220   partvals=partwords*partitions_per_word;
221
222   /* we find the patition type for each partition of each
223      channel.  We'll go back and do the interleaved encoding in a
224      bit.  For now, clarity */
225   
226   memset(resbits,0,sizeof(long)*possible_partitions);
227   memset(resvals,0,sizeof(long)*possible_partitions);
228
229   for(i=0;i<ch;i++){
230     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
231     memset(partword[i],0,n/samples_per_partition*sizeof(long));
232   }
233
234   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++)
235     for(j=0;j<ch;j++)
236       /* do the partition decision based on the number of 'bits'
237          needed to encode the block */
238       partword[j][l]=_testhack(in[j]+i,samples_per_partition,look);
239   
240   /* we code the partition words for each channel, then the residual
241      words for a partition per channel until we've written all the
242      residual words for that partition word.  Then write the next
243      parition channel words... */
244   
245   for(i=info->begin,l=0;i<info->end;){
246     /* first we encode a partition codeword for each channel */
247     for(j=0;j<ch;j++){
248       long val=partword[j][l];
249       for(k=1;k<partitions_per_word;k++)
250         val= val*possible_partitions+partword[j][l+k];
251       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
252     }
253     /* now we encode interleaved residual values for the partitions */
254     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
255       for(j=0;j<ch;j++){
256         resbits[partword[j][l]]+=
257           _encodepart(&vb->opb,in[j]+i,samples_per_partition,
258                       info->secondstages[partword[j][l]],
259                       look->partbooks[partword[j][l]]);
260         resvals[partword[j][l]]+=samples_per_partition;
261       }
262       
263   }
264
265   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
266   fprintf(stderr,
267           "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
268           ch*(info->end-info->begin),phrasebits,resbitsT);
269   for(i=0;i<possible_partitions;i++)
270     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
271   fprintf(stderr,"\n");
272  
273   return(0);
274 }
275
276 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
277   long i,j,k,l;
278   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
279   vorbis_info_residue0 *info=look->info;
280
281   /* move all this setup out later */
282   int samples_per_partition=info->grouping;
283   int partitions_per_word=look->phrasebook->dim;
284   int n=info->end-info->begin;
285
286   int partvals=n/samples_per_partition;
287   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
288   int **partword=alloca(ch*sizeof(long *));
289   double *work=alloca(sizeof(double)*samples_per_partition);
290   partvals=partwords*partitions_per_word;
291
292   for(i=info->begin,l=0;i<info->end;){
293     /* fetch the partition word for each channel */
294     for(j=0;j<ch;j++){
295       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
296       partword[j]=look->decodemap[temp];
297       if(partword[j]==NULL)exit(1);
298     }
299     
300     /* now we decode interleaved residual values for the partitions */
301     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
302       for(j=0;j<ch;j++){
303         int part=partword[j][k];
304         _decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
305                     info->secondstages[part],
306                     look->partbooks[part]);
307       }
308   }
309
310   return(0);
311 }
312
313 vorbis_func_residue residue0_exportbundle={
314   &pack,
315   &unpack,
316   &look,
317   &free_info,
318   &free_look,
319   &forward,
320   &inverse
321 };