proper truncated packet support
[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.15 2000/06/15 09:18:34 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
155 /* does not guard against invalid settings; eg, a subn of 16 and a
156    subgroup request of 32.  Max subn of 128 */
157 static int _testhack(double *vec,int n,vorbis_look_residue0 *look,
158                      int auxparts,int auxpartnum){
159   vorbis_info_residue0 *info=look->info;
160   int i,j=0;
161   double max,localmax=0.;
162   double temp[128];
163   double entropy[8];
164
165   /* setup */
166   for(i=0;i<n;i++)temp[i]=fabs(rint(vec[i]));
167
168   /* handle case subgrp==1 outside */
169   for(i=0;i<n;i++)
170     if(temp[i]>localmax)localmax=temp[i];
171   max=localmax;
172   
173   while(1){
174     entropy[j]=localmax;
175     n>>=1;
176     j++;
177
178     if(n<=0)break;
179     for(i=0;i<n;i++){
180       temp[i]+=temp[i+n];
181     }
182     localmax=0.;
183     for(i=0;i<n;i++)
184       if(temp[i]>localmax)localmax=temp[i];
185   }
186
187   for(i=0;i<auxparts-1;i++)
188     if(auxpartnum<info->blimit[i] &&
189        entropy[info->subgrp[i]]<=info->entmax[i] &&
190        max<=info->ampmax[i])
191       break;
192
193   return(i);
194 }
195
196 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
197                        int stages, codebook **books){
198   int i,j,bits=0;
199
200   for(j=0;j<stages;j++){
201     int dim=books[j]->dim;
202     int step=n/dim;
203     for(i=0;i<step;i++)
204       bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
205  
206   }
207
208   return(bits);
209 }
210
211 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
212                        int stages, codebook **books){
213   int i,j;
214   
215   memset(work,0,sizeof(double)*n);
216   for(j=0;j<stages;j++){
217     int dim=books[j]->dim;
218     int step=n/dim;
219     for(i=0;i<step;i++)
220       if(vorbis_book_decodevs(books[j],work+i,opb,step,0)==-1)
221         return(-1);
222   }
223   
224   for(i=0;i<n;i++)
225     vec[i]*=work[i];
226   
227   return(0);
228 }
229
230 int forward(vorbis_block *vb,vorbis_look_residue *vl,
231             double **in,int ch){
232   long i,j,k,l;
233   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
234   vorbis_info_residue0 *info=look->info;
235
236   /* move all this setup out later */
237   int samples_per_partition=info->grouping;
238   int possible_partitions=info->partitions;
239   int partitions_per_word=look->phrasebook->dim;
240   int n=info->end-info->begin;
241   long phrasebits=0,resbitsT=0;
242   long *resbits=alloca(sizeof(long)*possible_partitions);
243   long *resvals=alloca(sizeof(long)*possible_partitions);
244
245   int partvals=n/samples_per_partition;
246   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
247   long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
248
249   partvals=partwords*partitions_per_word;
250
251   /* we find the patition type for each partition of each
252      channel.  We'll go back and do the interleaved encoding in a
253      bit.  For now, clarity */
254   
255   memset(resbits,0,sizeof(long)*possible_partitions);
256   memset(resvals,0,sizeof(long)*possible_partitions);
257
258   for(i=0;i<ch;i++){
259     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
260     memset(partword[i],0,n/samples_per_partition*sizeof(long));
261   }
262
263   for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
264     for(j=0;j<ch;j++)
265       /* do the partition decision based on the number of 'bits'
266          needed to encode the block */
267       partword[j][l]=
268         _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
269   
270   }
271   /* we code the partition words for each channel, then the residual
272      words for a partition per channel until we've written all the
273      residual words for that partition word.  Then write the next
274      parition channel words... */
275   
276   for(i=info->begin,l=0;i<info->end;){
277     /* first we encode a partition codeword for each channel */
278     for(j=0;j<ch;j++){
279       long val=partword[j][l];
280       for(k=1;k<partitions_per_word;k++)
281         val= val*possible_partitions+partword[j][l+k];
282       phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
283     }
284     /* now we encode interleaved residual values for the partitions */
285     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
286       for(j=0;j<ch;j++){
287         resbits[partword[j][l]]+=
288           _encodepart(&vb->opb,in[j]+i,samples_per_partition,
289                       info->secondstages[partword[j][l]],
290                       look->partbooks[partword[j][l]]);
291         resvals[partword[j][l]]+=samples_per_partition;
292       }
293       
294   }
295
296   for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
297   /*fprintf(stderr,
298           "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
299           ch*(info->end-info->begin),phrasebits,resbitsT);
300   for(i=0;i<possible_partitions;i++)
301     fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
302     fprintf(stderr,"\n");*/
303  
304   return(0);
305 }
306
307 /* a truncated packet here just means 'stop working'; it's not an error */
308 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
309   long i,j,k,l,transend=vb->pcmend/2;
310   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
311   vorbis_info_residue0 *info=look->info;
312
313   /* move all this setup out later */
314   int samples_per_partition=info->grouping;
315   int partitions_per_word=look->phrasebook->dim;
316   int n=info->end-info->begin;
317
318   int partvals=n/samples_per_partition;
319   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
320   int **partword=alloca(ch*sizeof(long *));
321   double *work=alloca(sizeof(double)*samples_per_partition);
322   partvals=partwords*partitions_per_word;
323
324   /* make sure we're zeroed up to the start */
325   for(j=0;j<ch;j++)
326     memset(in[j],0,sizeof(double)*info->begin);
327
328   for(i=info->begin,l=0;i<info->end;){
329     /* fetch the partition word for each channel */
330     for(j=0;j<ch;j++){
331       int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
332       if(temp==-1)goto eopbreak;
333       partword[j]=look->decodemap[temp];
334       if(partword[j]==NULL)goto errout;
335     }
336     
337     /* now we decode interleaved residual values for the partitions */
338     for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
339       for(j=0;j<ch;j++){
340         int part=partword[j][k];
341         if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
342                     info->secondstages[part],
343                        look->partbooks[part])==-1)goto eopbreak;
344       }
345   }
346
347  eopbreak:
348   if(i<transend){
349     for(j=0;j<ch;j++)
350       memset(in[j]+i,0,sizeof(double)*(transend-i));
351   }
352
353   return(0);
354
355  errout:
356   for(j=0;j<ch;j++)
357     memset(in[j],0,sizeof(double)*transend);
358   return(0);
359 }
360
361 vorbis_func_residue residue0_exportbundle={
362   &pack,
363   &unpack,
364   &look,
365   &free_info,
366   &free_look,
367   &forward,
368   &inverse
369 };