Merging the postbeta2 branch onto the mainline.
[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.18 2000/10/12 03:12:53 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 <ogg/ogg.h>
29 #include "vorbis/codec.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 res0_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 res0_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 res0_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 *res0_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   res0_free_info(info);
116   return(NULL);
117 }
118
119 vorbis_look_residue *res0_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(float *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   float max,localmax=0.;
168   float temp[128];
169   float 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,float *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,float *work,float *vec, int n,
231                        int stages, codebook **books){
232   int i;
233   
234   memset(work,0,sizeof(float)*n);
235   for(i=0;i<stages;i++){
236     int dim=books[i]->dim;
237     int step=n/dim;
238     if(s_vorbis_book_decodevs(books[i],work,opb,step,0)==-1)
239       return(-1);
240   }
241   
242   for(i=0;i<n;i++)
243     vec[i]*=work[i];
244   
245   return(0);
246 }
247
248 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
249             float **in,int ch){
250   long i,j,k,l;
251   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
252   vorbis_info_residue0 *info=look->info;
253
254   /* move all this setup out later */
255   int samples_per_partition=info->grouping;
256   int possible_partitions=info->partitions;
257   int partitions_per_word=look->phrasebook->dim;
258   int n=info->end-info->begin;
259   long phrasebits=0,resbitsT=0;
260   long *resbits=alloca(sizeof(long)*possible_partitions);
261   long *resvals=alloca(sizeof(long)*possible_partitions);
262
263   int partvals=n/samples_per_partition;
264   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
265   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
266
267   partvals=partwords*partitions_per_word;
268
269   /* we find the patition type for each partition of each
270      channel.  We'll go back and do the interleaved encoding in a
271      bit.  For now, clarity */
272   
273   memset(resbits,0,sizeof(long)*possible_partitions);
274   memset(resvals,0,sizeof(long)*possible_partitions);
275
276   for(i=0;i<ch;i++){
277     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
278     memset(partword[i],0,n/samples_per_partition*sizeof(long));
279   }
280
281   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
282     for(j=0;j<ch;j++)
283       /* do the partition decision based on the number of 'bits'
284          needed to encode the block */
285       partword[j][l]=
286         _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
287   
288   }
289   /* we code the partition words for each channel, then the residual
290      words for a partition per channel until we've written all the
291      residual words for that partition word.  Then write the next
292      partition channel words... */
293   
294   for(i=info->begin,l=0;i<info->end;){
295     /* first we encode a partition codeword for each channel */
296     for(j=0;j<ch;j++){
297       long val=partword[j][l];
298       for(k=1;k<partitions_per_word;k++)
299         val= val*possible_partitions+partword[j][l+k];
300       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
301     }
302     /* now we encode interleaved residual values for the partitions */
303     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
304       for(j=0;j<ch;j++){
305         resbits[partword[j][l]]+=
306           _encodepart(&vb->opb,in[j]+i,samples_per_partition,
307                       info->secondstages[partword[j][l]],
308                       look->partbooks[partword[j][l]],look->map,partword[j][l]);
309         resvals[partword[j][l]]+=samples_per_partition;
310       }
311       
312   }
313
314   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
315   /*fprintf(stderr,
316           "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
317           ch*(info->end-info->begin),phrasebits,resbitsT);
318   for(i=0;i<possible_partitions;i++)
319     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
320     fprintf(stderr,"\n");*/
321  
322   return(0);
323 }
324
325 /* a truncated packet here just means 'stop working'; it's not an error */
326 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
327   long i,j,k,l,transend=vb->pcmend/2;
328   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
329   vorbis_info_residue0 *info=look->info;
330
331   /* move all this setup out later */
332   int samples_per_partition=info->grouping;
333   int partitions_per_word=look->phrasebook->dim;
334   int n=info->end-info->begin;
335
336   int partvals=n/samples_per_partition;
337   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
338   int **partword=alloca(ch*sizeof(long *));
339   float *work=alloca(sizeof(float)*samples_per_partition);
340   partvals=partwords*partitions_per_word;
341
342   /* make sure we're zeroed up to the start */
343   for(j=0;j<ch;j++)
344     memset(in[j],0,sizeof(float)*info->begin);
345
346   for(i=info->begin,l=0;i<info->end;){
347     /* fetch the partition word for each channel */
348     for(j=0;j<ch;j++){
349       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
350       if(temp==-1)goto eopbreak;
351       partword[j]=look->decodemap[temp];
352       if(partword[j]==NULL)goto errout;
353     }
354     
355     /* now we decode interleaved residual values for the partitions */
356     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
357       for(j=0;j<ch;j++){
358         int part=partword[j][k];
359         if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
360                     info->secondstages[part],
361                        look->partbooks[part])==-1)goto eopbreak;
362       }
363   }
364
365  eopbreak:
366   if(i<transend){
367     for(j=0;j<ch;j++)
368       memset(in[j]+i,0,sizeof(float)*(transend-i));
369   }
370
371   return(0);
372
373  errout:
374   for(j=0;j<ch;j++)
375     memset(in[j],0,sizeof(float)*transend);
376   return(0);
377 }
378
379 vorbis_func_residue residue0_exportbundle={
380   &res0_pack,
381   &res0_unpack,
382   &res0_look,
383   &res0_free_info,
384   &res0_free_look,
385   &res0_forward,
386   &res0_inverse
387 };