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