Commit minor speed patch (sliding window in vorbis_blockin)
[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 SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis 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.23 2000/12/21 21:04:41 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 "codec_internal.h"
31 #include "registry.h"
32 #include "codebook.h"
33 #include "misc.h"
34 #include "os.h"
35 #include "bitbuffer.h"
36
37 typedef struct {
38   vorbis_info_residue0 *info;
39   int         map;
40   
41   int         parts;
42   codebook   *phrasebook;
43   codebook ***partbooks;
44
45   int         partvals;
46   int       **decodemap;
47 } vorbis_look_residue0;
48
49 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
50   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
51   vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
52   memcpy(ret,info,sizeof(vorbis_info_residue0));
53   return(ret);
54 }
55
56 void res0_free_info(vorbis_info_residue *i){
57   if(i){
58     memset(i,0,sizeof(vorbis_info_residue0));
59     _ogg_free(i);
60   }
61 }
62
63 void res0_free_look(vorbis_look_residue *i){
64   int j;
65   if(i){
66     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
67     for(j=0;j<look->parts;j++)
68       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
69     _ogg_free(look->partbooks);
70     for(j=0;j<look->partvals;j++)
71       _ogg_free(look->decodemap[j]);
72     _ogg_free(look->decodemap);
73     memset(i,0,sizeof(vorbis_look_residue0));
74     _ogg_free(i);
75   }
76 }
77
78 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
79   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
80   int j,acc=0;
81   oggpack_write(opb,info->begin,24);
82   oggpack_write(opb,info->end,24);
83
84   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
85                                              code with a partitioned book */
86   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
87   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
88   for(j=0;j<info->partitions;j++){
89     oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
90     acc+=info->secondstages[j];
91   }
92   for(j=0;j<acc;j++)
93     oggpack_write(opb,info->booklist[j],8);
94
95 }
96
97 /* vorbis_info is for range checking */
98 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
99   int j,acc=0;
100   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
101   codec_setup_info     *ci=vi->codec_setup;
102
103   info->begin=oggpack_read(opb,24);
104   info->end=oggpack_read(opb,24);
105   info->grouping=oggpack_read(opb,24)+1;
106   info->partitions=oggpack_read(opb,6)+1;
107   info->groupbook=oggpack_read(opb,8);
108   for(j=0;j<info->partitions;j++){
109     int cascade=info->secondstages[j]=oggpack_read(opb,4);
110     if(cascade>1)goto errout; /* temporary!  when cascading gets
111                                  reworked and actually used, we don't
112                                  want old code to DTWT */
113     acc+=cascade;
114   }
115   for(j=0;j<acc;j++)
116     info->booklist[j]=oggpack_read(opb,8);
117
118   if(info->groupbook>=ci->books)goto errout;
119   for(j=0;j<acc;j++)
120     if(info->booklist[j]>=ci->books)goto errout;
121
122   return(info);
123  errout:
124   res0_free_info(info);
125   return(NULL);
126 }
127
128 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
129                           vorbis_info_residue *vr){
130   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
131   vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
132   backend_lookup_state *be=vd->backend_state;
133
134   int j,k,acc=0;
135   int dim;
136   look->info=info;
137   look->map=vm->mapping;
138
139   look->parts=info->partitions;
140   look->phrasebook=be->fullbooks+info->groupbook;
141   dim=look->phrasebook->dim;
142
143   look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
144
145   for(j=0;j<look->parts;j++){
146     int stages=info->secondstages[j];
147     if(stages){
148       look->partbooks[j]=_ogg_malloc(stages*sizeof(codebook *));
149       for(k=0;k<stages;k++)
150         look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
151     }
152   }
153
154   look->partvals=rint(pow(look->parts,dim));
155   look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
156   for(j=0;j<look->partvals;j++){
157     long val=j;
158     long mult=look->partvals/look->parts;
159     look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
160     for(k=0;k<dim;k++){
161       long deco=val/mult;
162       val-=deco*mult;
163       mult/=look->parts;
164       look->decodemap[j][k]=deco;
165     }
166   }
167
168   return(look);
169 }
170
171
172 /* does not guard against invalid settings; eg, a subn of 16 and a
173    subgroup request of 32.  Max subn of 128 */
174 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
175                      int auxparts,int auxpartnum){
176   vorbis_info_residue0 *info=look->info;
177   int i,j=0;
178   float max,localmax=0.f;
179   float temp[128];
180   float entropy[8];
181
182   /* setup */
183   for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
184
185   /* handle case subgrp==1 outside */
186   for(i=0;i<n;i++)
187     if(temp[i]>localmax)localmax=temp[i];
188   max=localmax;
189
190   for(i=0;i<n;i++)temp[i]=rint(temp[i]);
191   
192   while(1){
193     entropy[j]=localmax;
194     n>>=1;
195     j++;
196
197     if(n<=0)break;
198     for(i=0;i<n;i++){
199       temp[i]+=temp[i+n];
200     }
201     localmax=0.f;
202     for(i=0;i<n;i++)
203       if(temp[i]>localmax)localmax=temp[i];
204   }
205
206   for(i=0;i<auxparts-1;i++)
207     if(auxpartnum<info->blimit[i] &&
208        entropy[info->subgrp[i]]<=info->entmax[i] &&
209        max<=info->ampmax[i])
210       break;
211
212   return(i);
213 }
214
215 static int _encodepart(vorbis_bitbuffer *vbb,float *vec, int n,
216                        int stages, codebook **books,int mode,int part){
217   int i,j=0,bits=0;
218   if(stages){
219     int dim=books[j]->dim;
220     int step=n/dim;
221     for(i=0;i<step;i++){
222       int entry=vorbis_book_besterror(books[j],vec+i,step,0);
223 #ifdef TRAIN_RESENT      
224       {
225         char buf[80];
226         FILE *f;
227         sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
228         f=fopen(buf,"a");
229         fprintf(f,"%d\n",entry);
230         fclose(f);
231       }
232 #endif
233       bits+=vorbis_book_bufencode(books[j],entry,vbb);
234     }
235   }
236   return(bits);
237 }
238
239 static int _decodepart(oggpack_buffer *opb,float *work,float *vec, int n,
240                        int stages, codebook **books){
241   int i;
242   
243   memset(work,0,sizeof(float)*n);
244   for(i=0;i<stages;i++){
245     int dim=books[i]->dim;
246     int step=n/dim;
247     if(s_vorbis_book_decodevs(books[i],work,opb,step,0)==-1)
248       return(-1);
249   }
250   
251   for(i=0;i<n;i++)
252     vec[i]*=work[i];
253   
254   return(0);
255 }
256
257 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
258             float **in,int ch,vorbis_bitbuffer *vbb){
259   long i,j,k,l;
260   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
261   vorbis_info_residue0 *info=look->info;
262
263   /* move all this setup out later */
264   int samples_per_partition=info->grouping;
265   int possible_partitions=info->partitions;
266   int partitions_per_word=look->phrasebook->dim;
267   int n=info->end-info->begin;
268   long phrasebits=0,resbitsT=0;
269   long *resbits=alloca(sizeof(long)*possible_partitions);
270   long *resvals=alloca(sizeof(long)*possible_partitions);
271
272   int partvals=n/samples_per_partition;
273   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
274   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
275
276   partvals=partwords*partitions_per_word;
277
278   /* we find the patition type for each partition of each
279      channel.  We'll go back and do the interleaved encoding in a
280      bit.  For now, clarity */
281   
282   memset(resbits,0,sizeof(long)*possible_partitions);
283   memset(resvals,0,sizeof(long)*possible_partitions);
284
285   for(i=0;i<ch;i++){
286     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
287     memset(partword[i],0,n/samples_per_partition*sizeof(long));
288   }
289
290   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
291     for(j=0;j<ch;j++)
292       /* do the partition decision based on the number of 'bits'
293          needed to encode the block */
294       partword[j][l]=
295         _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
296   
297   }
298   /* we code the partition words for each channel, then the residual
299      words for a partition per channel until we've written all the
300      residual words for that partition word.  Then write the next
301      partition channel words... */
302   
303   for(i=info->begin,l=0;i<info->end;){
304     
305     /* first we encode a partition codeword for each channel */
306     for(j=0;j<ch;j++){
307       long val=partword[j][l];
308       for(k=1;k<partitions_per_word;k++)
309         val= val*possible_partitions+partword[j][l+k];
310       phrasebits+=vorbis_book_bufencode(look->phrasebook,val,vbb);
311     }
312     /* now we encode interleaved residual values for the partitions */
313     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
314       for(j=0;j<ch;j++){
315         /*resbits[partword[j][l]]+=*/
316         resbitsT+=_encodepart(vbb,in[j]+i,samples_per_partition,
317                               info->secondstages[partword[j][l]],
318                               look->partbooks[partword[j][l]],look->map,partword[j][l]);
319         resvals[partword[j][l]]+=samples_per_partition;
320       }
321       
322   }
323
324   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
325   /*fprintf(stderr,
326     "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
327     ch*(info->end-info->begin),phrasebits,resbitsT);
328     for(i=0;i<possible_partitions;i++)
329     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
330     fprintf(stderr,"\n");*/
331  
332   return(0);
333 }
334
335 /* a truncated packet here just means 'stop working'; it's not an error */
336 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
337   long i,j,k,l,transend=vb->pcmend/2;
338   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
339   vorbis_info_residue0 *info=look->info;
340
341   /* move all this setup out later */
342   int samples_per_partition=info->grouping;
343   int partitions_per_word=look->phrasebook->dim;
344   int n=info->end-info->begin;
345
346   int partvals=n/samples_per_partition;
347   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
348   int **partword=alloca(ch*sizeof(long *));
349   float *work=alloca(sizeof(float)*samples_per_partition);
350   partvals=partwords*partitions_per_word;
351
352   /* make sure we're zeroed up to the start */
353   for(j=0;j<ch;j++)
354     memset(in[j],0,sizeof(float)*info->begin);
355
356   for(i=info->begin,l=0;i<info->end;){
357     /* fetch the partition word for each channel */
358     for(j=0;j<ch;j++){
359       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
360       if(temp==-1)goto eopbreak;
361       partword[j]=look->decodemap[temp];
362       if(partword[j]==NULL)goto errout;
363     }
364     
365     /* now we decode interleaved residual values for the partitions */
366     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
367       for(j=0;j<ch;j++){
368         int part=partword[j][k];
369         if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
370                     info->secondstages[part],
371                        look->partbooks[part])==-1)goto eopbreak;
372       }
373   }
374
375  eopbreak:
376   if(i<transend){
377     for(j=0;j<ch;j++)
378       memset(in[j]+i,0,sizeof(float)*(transend-i));
379   }
380
381   return(0);
382
383  errout:
384   for(j=0;j<ch;j++)
385     memset(in[j],0,sizeof(float)*transend);
386   return(0);
387 }
388
389 vorbis_func_residue residue0_exportbundle={
390   &res0_pack,
391   &res0_unpack,
392   &res0_look,
393   &res0_copy_info,
394   &res0_free_info,
395   &res0_free_look,
396   &res0_forward,
397   &res0_inverse
398 };