Floor 1
[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.27 2001/05/27 06:44:01 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   codebook   *phrasebook;
41   codebook ***partbooks;
42
43   int         partvals;
44   int       **decodemap;
45 } vorbis_look_residue0;
46
47 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
48   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
49   vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
50   memcpy(ret,info,sizeof(vorbis_info_residue0));
51   return(ret);
52 }
53
54 void res0_free_info(vorbis_info_residue *i){
55   if(i){
56     memset(i,0,sizeof(vorbis_info_residue0));
57     _ogg_free(i);
58   }
59 }
60
61 void res0_free_look(vorbis_look_residue *i){
62   int j;
63   if(i){
64     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
65     for(j=0;j<look->parts;j++)
66       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
67     _ogg_free(look->partbooks);
68     for(j=0;j<look->partvals;j++)
69       _ogg_free(look->decodemap[j]);
70     _ogg_free(look->decodemap);
71     memset(i,0,sizeof(vorbis_look_residue0));
72     _ogg_free(i);
73   }
74 }
75
76 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
77   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
78   int j,acc=0;
79   oggpack_write(opb,info->begin,24);
80   oggpack_write(opb,info->end,24);
81
82   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
83                                              code with a partitioned book */
84   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
85   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
86   for(j=0;j<info->partitions;j++){
87     oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
88     acc+=info->secondstages[j];
89   }
90   for(j=0;j<acc;j++)
91     oggpack_write(opb,info->booklist[j],8);
92
93 }
94
95 /* vorbis_info is for range checking */
96 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
97   int j,acc=0;
98   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
99   codec_setup_info     *ci=vi->codec_setup;
100
101   info->begin=oggpack_read(opb,24);
102   info->end=oggpack_read(opb,24);
103   info->grouping=oggpack_read(opb,24)+1;
104   info->partitions=oggpack_read(opb,6)+1;
105   info->groupbook=oggpack_read(opb,8);
106   for(j=0;j<info->partitions;j++){
107     int cascade=info->secondstages[j]=oggpack_read(opb,4);
108     if(cascade>1)goto errout; /* temporary!  when cascading gets
109                                  reworked and actually used, we don't
110                                  want old code to DTWT */
111     acc+=cascade;
112   }
113   for(j=0;j<acc;j++){
114     info->booklist[j]=oggpack_read(opb,8);
115     if(info->booklist[j]==255)info->booklist[j]=-1;
116   }
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       look->partbooks[j][0]=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 _interleaved_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.f;
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     if(!n)break;
195     j++;
196
197     for(i=0;i<n;i++){
198       temp[i]+=temp[i+n];
199     }
200     localmax=0.f;
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 _testhack(float *vec,int n,vorbis_look_residue0 *look,
215                      int auxparts,int auxpartnum){
216   vorbis_info_residue0 *info=look->info;
217   int i,j=0;
218   float max,localmax=0.f;
219   float temp[128];
220   float entropy[8];
221
222   /* setup */
223   for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
224
225   /* handle case subgrp==1 outside */
226   for(i=0;i<n;i++)
227     if(temp[i]>localmax)localmax=temp[i];
228   max=localmax;
229
230   for(i=0;i<n;i++)temp[i]=rint(temp[i]);
231   
232   while(n){
233     entropy[j]=localmax;
234     n>>=1;
235     j++;
236     if(!n)break;
237     for(i=0;i<n;i++){
238       temp[i]=temp[i*2]+temp[i*2+1];
239     }
240     localmax=0.f;
241     for(i=0;i<n;i++)
242       if(temp[i]>localmax)localmax=temp[i];
243   }
244
245   for(i=0;i<auxparts-1;i++)
246     if(auxpartnum<info->blimit[i] &&
247        entropy[info->subgrp[i]]<=info->entmax[i] &&
248        max<=info->ampmax[i])
249       break;
250
251   return(i);
252 }
253
254 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
255                                    int stages, codebook **books,int mode,
256                                    int part){
257   int i,j=0,bits=0;
258   if(stages){
259     int dim=books[j]->dim;
260     int step=n/dim;
261     for(i=0;i<step;i++){
262       int entry=vorbis_book_besterror(books[j],vec+i,step,0);
263 #ifdef TRAIN_RESENT      
264       {
265         char buf[80];
266         FILE *f;
267         sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
268         f=fopen(buf,"a");
269         fprintf(f,"%d\n",entry);
270         fclose(f);
271       }
272 #endif
273       bits+=vorbis_book_encode(books[j],entry,opb);
274     }
275   }
276   return(bits);
277 }
278  
279 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
280                        int stages, codebook **books,int mode,int part){
281   int i,j=0,bits=0;
282   if(stages){
283     int dim=books[j]->dim;
284     int step=n/dim;
285     for(i=0;i<step;i++){
286       int entry=vorbis_book_besterror(books[j],vec+i*dim,1,0);
287 #ifdef TRAIN_RESENT      
288       {
289         char buf[80];
290         FILE *f;
291         sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
292         f=fopen(buf,"a");
293         fprintf(f,"%d\n",entry);
294         fclose(f);
295       }
296 #endif
297       bits+=vorbis_book_encode(books[j],entry,opb);
298     }
299   }
300   return(bits);
301 }
302
303 /* doesn;t yet deal with cascading */
304 static int _interleaved_decodepart(oggpack_buffer *opb,float *work,
305                                    float *vec, int n,
306                                    int stages, codebook **books){
307   int i;
308   
309   memset(work,0,sizeof(float)*n);
310   if(stages){
311     int step=n/books[0]->dim;
312     if(s_vorbis_book_decodevs(books[0],work,opb,step,0)==-1)
313       return(-1);
314   }
315   
316   for(i=0;i<n;i++)
317     vec[i]*=work[i];
318   
319   return(0);
320 }
321
322 static int _decodepart(oggpack_buffer *opb,float *work,
323                        float *vec, int n,
324                        int stages, codebook **books){
325   int i;
326   
327   memset(work,0,sizeof(float)*n);
328   if(stages){
329     if(s_vorbis_book_decodev(books[0],work,opb,n,0)==-1)
330       return(-1);
331   }
332   
333   for(i=0;i<n;i++)
334     vec[i]*=work[i];
335   
336   return(0);
337 }
338
339 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
340                       float **in,int ch,
341                       int (*classify)(float *,int,vorbis_look_residue0 *,
342                                       int,int),
343                       int (*encode)(oggpack_buffer *,float *,int,int,
344                                     codebook **,int,int)){
345   long i,j,k,l;
346   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
347   vorbis_info_residue0 *info=look->info;
348
349   /* move all this setup out later */
350   int samples_per_partition=info->grouping;
351   int possible_partitions=info->partitions;
352   int partitions_per_word=look->phrasebook->dim;
353   int n=info->end-info->begin;
354   long phrasebits=0,resbitsT=0;
355   long *resbits=alloca(sizeof(long)*possible_partitions);
356   long *resvals=alloca(sizeof(long)*possible_partitions);
357
358   int partvals=n/samples_per_partition;
359   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
360   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
361
362   partvals=partwords*partitions_per_word;
363
364   /* we find the patition type for each partition of each
365      channel.  We'll go back and do the interleaved encoding in a
366      bit.  For now, clarity */
367   
368   memset(resbits,0,sizeof(long)*possible_partitions);
369   memset(resvals,0,sizeof(long)*possible_partitions);
370
371   for(i=0;i<ch;i++){
372     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
373     memset(partword[i],0,n/samples_per_partition*sizeof(long));
374   }
375
376   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
377     for(j=0;j<ch;j++)
378       /* do the partition decision based on the 'entropy'
379          int the block */
380       partword[j][l]=
381         classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
382   
383   }
384
385   /* we code the partition words for each channel, then the residual
386      words for a partition per channel until we've written all the
387      residual words for that partition word.  Then write the next
388      partition channel words... */
389   
390   for(i=info->begin,l=0;i<info->end;){
391     
392     /* first we encode a partition codeword for each channel */
393     for(j=0;j<ch;j++){
394       long val=partword[j][l];
395       for(k=1;k<partitions_per_word;k++)
396         val= val*possible_partitions+partword[j][l+k];
397       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
398     }
399     /* now we encode interleaved residual values for the partitions */
400     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
401       for(j=0;j<ch;j++){
402         /*resbits[partword[j][l]]+=*/
403         resbitsT+=encode(&vb->opb,in[j]+i,samples_per_partition,
404                          info->secondstages[partword[j][l]],
405                          look->partbooks[partword[j][l]],look->map,partword[j][l]);
406         resvals[partword[j][l]]+=samples_per_partition;
407       }
408       
409   }
410
411   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
412   /*fprintf(stderr,
413     "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
414     ch*(info->end-info->begin),phrasebits,resbitsT);
415     for(i=0;i<possible_partitions;i++)
416     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
417     fprintf(stderr,"\n");*/
418  
419   return(0);
420 }
421
422 /* a truncated packet here just means 'stop working'; it's not an error */
423 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
424                       float **in,int ch,
425                       int (*decodepart)(oggpack_buffer *,float *,float *,
426                                         int,int,codebook **)){
427   long i,j,k,l,transend=vb->pcmend/2;
428   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
429   vorbis_info_residue0 *info=look->info;
430
431   /* move all this setup out later */
432   int samples_per_partition=info->grouping;
433   int partitions_per_word=look->phrasebook->dim;
434   int n=info->end-info->begin;
435
436   int partvals=n/samples_per_partition;
437   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
438   int **partword=alloca(ch*sizeof(long *));
439   float *work=alloca(sizeof(float)*samples_per_partition);
440   partvals=partwords*partitions_per_word;
441
442   /* make sure we're zeroed up to the start */
443   for(j=0;j<ch;j++)
444     memset(in[j],0,sizeof(float)*info->begin);
445
446   for(i=info->begin,l=0;i<info->end;){
447     /* fetch the partition word for each channel */
448     for(j=0;j<ch;j++){
449       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
450       if(temp==-1)goto eopbreak;
451       partword[j]=look->decodemap[temp];
452       if(partword[j]==NULL)goto errout;
453     }
454     
455     /* now we decode residual values for the partitions */
456     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
457       for(j=0;j<ch;j++){
458         int part=partword[j][k];
459         if(decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
460                       info->secondstages[part],
461                       look->partbooks[part])==-1)goto eopbreak;
462       }
463   }
464
465  eopbreak:
466   if(i<transend){
467     for(j=0;j<ch;j++)
468       memset(in[j]+i,0,sizeof(float)*(transend-i));
469   }
470
471   return(0);
472
473  errout:
474   for(j=0;j<ch;j++)
475     memset(in[j],0,sizeof(float)*transend);
476   return(0);
477 }
478
479 /* residue 0 and 1 are just slight variants of one another. 0 is
480    interleaved, 1 is not */
481 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
482             float **in,int ch){
483   return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
484 }
485
486 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
487     return(_01inverse(vb,vl,in,ch,_interleaved_decodepart));
488 }
489
490 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
491             float **in,int ch){
492   return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
493 }
494
495 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
496     return(_01inverse(vb,vl,in,ch,_decodepart));
497 }
498
499 vorbis_func_residue residue0_exportbundle={
500   &res0_pack,
501   &res0_unpack,
502   &res0_look,
503   &res0_copy_info,
504   &res0_free_info,
505   &res0_free_look,
506   &res0_forward,
507   &res0_inverse
508 };
509
510 vorbis_func_residue residue1_exportbundle={
511   &res0_pack,
512   &res0_unpack,
513   &res0_look,
514   &res0_copy_info,
515   &res0_free_info,
516   &res0_free_look,
517   &res1_forward,
518   &res1_inverse
519 };