res0.c 14.7 KB
Newer Older
Monty's avatar
 
Monty committed
1
2
/********************************************************************
 *                                                                  *
Monty's avatar
   
Monty committed
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
   
Monty committed
4
5
6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Monty's avatar
 
Monty committed
7
 *                                                                  *
Monty's avatar
   
Monty committed
8
9
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
 * by the XIPHOPHORUS Company http://www.xiph.org/                  *
Monty's avatar
   
Monty committed
10
 *                                                                  *
Monty's avatar
 
Monty committed
11
12
 ********************************************************************

Monty's avatar
   
Monty committed
13
 function: residue backend 0 and 1 implementation
Monty's avatar
   
Monty committed
14
 last mod: $Id: res0.c,v 1.28 2001/06/04 05:50:10 xiphmont Exp $
Monty's avatar
 
Monty committed
15
16
17

 ********************************************************************/

Monty's avatar
   
Monty committed
18
19
20
21
22
/* Slow, slow, slow, simpleminded and did I mention it was slow?  The
   encode/decode loops are coded for clarity and performance is not
   yet even a nagging little idea lurking in the shadows.  Oh and BTW,
   it's slow. */

Monty's avatar
 
Monty committed
23
24
#include <stdlib.h>
#include <string.h>
Monty's avatar
   
Monty committed
25
#include <math.h>
Monty's avatar
   
Monty committed
26
#include <stdio.h>
Monty's avatar
   
Monty committed
27
#include <ogg/ogg.h>
Monty's avatar
 
Monty committed
28
#include "vorbis/codec.h"
Monty's avatar
   
Monty committed
29
#include "codec_internal.h"
Monty's avatar
   
Monty committed
30
#include "registry.h"
Monty's avatar
   
Monty committed
31
#include "codebook.h"
Monty's avatar
   
Monty committed
32
#include "misc.h"
Monty's avatar
   
Monty committed
33
#include "os.h"
Monty's avatar
 
Monty committed
34

Monty's avatar
   
Monty committed
35
36
typedef struct {
  vorbis_info_residue0 *info;
Monty's avatar
   
Monty committed
37
  int         map;
Monty's avatar
   
Monty committed
38
39
  
  int         parts;
Monty's avatar
   
Monty committed
40
41
  int         stages;
  codebook   *fullbooks;
Monty's avatar
   
Monty committed
42
43
44
45
46
  codebook   *phrasebook;
  codebook ***partbooks;

  int         partvals;
  int       **decodemap;
Monty's avatar
   
Monty committed
47
48
49
50

  long        phrase;
  long        bits[32];
  long        vals[32];
Monty's avatar
   
Monty committed
51
} vorbis_look_residue0;
Monty's avatar
 
Monty committed
52

Monty's avatar
   
Monty committed
53
54
55
56
57
58
59
vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
  memcpy(ret,info,sizeof(vorbis_info_residue0));
  return(ret);
}

Monty's avatar
   
Monty committed
60
void res0_free_info(vorbis_info_residue *i){
Monty's avatar
   
Monty committed
61
62
  if(i){
    memset(i,0,sizeof(vorbis_info_residue0));
Monty's avatar
   
Monty committed
63
    _ogg_free(i);
Monty's avatar
   
Monty committed
64
  }
Monty's avatar
 
Monty committed
65
66
}

Monty's avatar
   
Monty committed
67
void res0_free_look(vorbis_look_residue *i){
Monty's avatar
   
Monty committed
68
  int j;
Monty's avatar
   
Monty committed
69
  if(i){
Monty's avatar
   
Monty committed
70
    vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
Monty's avatar
   
Monty committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    long resbitsT=0;
    long resvalsT=0;

    for(j=0;j<look->parts;j++)resbitsT+=look->bits[j];
    for(j=0;j<look->parts;j++)resvalsT+=look->vals[j];
    fprintf(stderr,
	    "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
	    resvalsT,look->phrase,resbitsT);
    for(j=1;j<look->parts;j++)
      fprintf(stderr,"%ld(%ld):%ld  %g/sample\n",j,look->vals[j],look->bits[j],
	      (float)look->bits[j]/look->vals[j]);
    fprintf(stderr,"\n");
 

Monty's avatar
   
Monty committed
85
    for(j=0;j<look->parts;j++)
Monty's avatar
   
Monty committed
86
87
      if(look->partbooks[j])_ogg_free(look->partbooks[j]);
    _ogg_free(look->partbooks);
Monty's avatar
   
Monty committed
88
    for(j=0;j<look->partvals;j++)
Monty's avatar
   
Monty committed
89
90
      _ogg_free(look->decodemap[j]);
    _ogg_free(look->decodemap);
Monty's avatar
   
Monty committed
91
    memset(i,0,sizeof(vorbis_look_residue0));
Monty's avatar
   
Monty committed
92
    _ogg_free(i);
Monty's avatar
   
Monty committed
93
  }
Monty's avatar
   
Monty committed
94
95
}

Monty's avatar
   
Monty committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
static int ilog(unsigned int v){
  int ret=0;
  while(v){
    ret++;
    v>>=1;
  }
  return(ret);
}

static int icount(unsigned int v){
  int ret=0;
  while(v){
    ret+=v&1;
    v>>=1;
  }
  return(ret);
}


Monty's avatar
   
Monty committed
115
void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
Monty's avatar
   
Monty committed
116
117
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  int j,acc=0;
Monty's avatar
   
Monty committed
118
119
  oggpack_write(opb,info->begin,24);
  oggpack_write(opb,info->end,24);
Monty's avatar
   
Monty committed
120

Monty's avatar
   
Monty committed
121
  oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
Monty's avatar
   
Monty committed
122
					     code with a partitioned book */
Monty's avatar
   
Monty committed
123
124
  oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  oggpack_write(opb,info->groupbook,8);  /* group huffman book */
Monty's avatar
   
Monty committed
125
126
127
128

  /* secondstages is a bitmask; as encoding progresses pass by pass, a
     bitmask of one indicates this partition class has bits to write
     this pass */
Monty's avatar
   
Monty committed
129
  for(j=0;j<info->partitions;j++){
Monty's avatar
   
Monty committed
130
131
132
133
134
135
136
137
    if(ilog(info->secondstages[j])>3){
      /* yes, this is a minor hack due to not thinking ahead */
      oggpack_write(opb,info->secondstages[j],3); 
      oggpack_write(opb,1,1);
      oggpack_write(opb,info->secondstages[j]>>3,5); 
    }else
      oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
    acc+=icount(info->secondstages[j]);
Monty's avatar
   
Monty committed
138
139
  }
  for(j=0;j<acc;j++)
Monty's avatar
   
Monty committed
140
    oggpack_write(opb,info->booklist[j],8);
Monty's avatar
   
Monty committed
141

Monty's avatar
   
Monty committed
142
143
144
}

/* vorbis_info is for range checking */
Monty's avatar
   
Monty committed
145
vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
Monty's avatar
   
Monty committed
146
  int j,acc=0;
Monty's avatar
   
Monty committed
147
148
  vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
  codec_setup_info     *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
149

Monty's avatar
   
Monty committed
150
151
152
153
154
  info->begin=oggpack_read(opb,24);
  info->end=oggpack_read(opb,24);
  info->grouping=oggpack_read(opb,24)+1;
  info->partitions=oggpack_read(opb,6)+1;
  info->groupbook=oggpack_read(opb,8);
Monty's avatar
   
Monty committed
155

Monty's avatar
   
Monty committed
156
  for(j=0;j<info->partitions;j++){
Monty's avatar
   
Monty committed
157
158
159
160
    int cascade=info->secondstages[j]=oggpack_read(opb,3);
    if(oggpack_read(opb,1))
      cascade|=(oggpack_read(opb,5)<<3);
    acc+=icount(cascade);
Monty's avatar
   
Monty committed
161
  }
Monty's avatar
   
Monty committed
162
  for(j=0;j<acc;j++)
Monty's avatar
   
Monty committed
163
    info->booklist[j]=oggpack_read(opb,8);
Monty's avatar
   
Monty committed
164

Monty's avatar
   
Monty committed
165
  if(info->groupbook>=ci->books)goto errout;
Monty's avatar
   
Monty committed
166
  for(j=0;j<acc;j++)
Monty's avatar
   
Monty committed
167
    if(info->booklist[j]>=ci->books)goto errout;
Monty's avatar
   
Monty committed
168

Monty's avatar
   
Monty committed
169
  return(info);
Monty's avatar
   
Monty committed
170
 errout:
Monty's avatar
   
Monty committed
171
  res0_free_info(info);
Monty's avatar
   
Monty committed
172
  return(NULL);
Monty's avatar
 
Monty committed
173
174
}

Monty's avatar
   
Monty committed
175
vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
Monty's avatar
   
Monty committed
176
			  vorbis_info_residue *vr){
Monty's avatar
   
Monty committed
177
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
Monty's avatar
   
Monty committed
178
179
180
  vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
  backend_lookup_state *be=vd->backend_state;

Monty's avatar
   
Monty committed
181
182
  int j,k,acc=0;
  int dim;
Monty's avatar
   
Monty committed
183
  int maxstage=0;
Monty's avatar
   
Monty committed
184
  look->info=info;
Monty's avatar
   
Monty committed
185
  look->map=vm->mapping;
Monty's avatar
   
Monty committed
186
187

  look->parts=info->partitions;
Monty's avatar
   
Monty committed
188
  look->fullbooks=be->fullbooks;
Monty's avatar
   
Monty committed
189
  look->phrasebook=be->fullbooks+info->groupbook;
Monty's avatar
   
Monty committed
190
191
  dim=look->phrasebook->dim;

Monty's avatar
   
Monty committed
192
  look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
Monty's avatar
   
Monty committed
193
194

  for(j=0;j<look->parts;j++){
Monty's avatar
   
Monty committed
195
    int stages=ilog(info->secondstages[j]);
Monty's avatar
   
Monty committed
196
    if(stages){
Monty's avatar
   
Monty committed
197
198
199
200
201
      if(stages>maxstage)maxstage=stages;
      look->partbooks[j]=_ogg_calloc(stages,sizeof(codebook *));
      for(k=0;k<stages;k++)
	if(info->secondstages[j]&(1<<k))
	  look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
Monty's avatar
   
Monty committed
202
203
204
    }
  }

Monty's avatar
   
Monty committed
205
  look->partvals=rint(pow(look->parts,dim));
Monty's avatar
   
Monty committed
206
  look->stages=maxstage;
Monty's avatar
   
Monty committed
207
  look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
Monty's avatar
   
Monty committed
208
209
210
  for(j=0;j<look->partvals;j++){
    long val=j;
    long mult=look->partvals/look->parts;
Monty's avatar
   
Monty committed
211
    look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
Monty's avatar
   
Monty committed
212
213
214
215
216
217
    for(k=0;k<dim;k++){
      long deco=val/mult;
      val-=deco*mult;
      mult/=look->parts;
      look->decodemap[j][k]=deco;
    }
Monty's avatar
   
Monty committed
218
219
220
  }

  return(look);
Monty's avatar
 
Monty committed
221
}
Monty's avatar
   
Monty committed
222

Monty's avatar
   
Monty committed
223
224
225

/* does not guard against invalid settings; eg, a subn of 16 and a
   subgroup request of 32.  Max subn of 128 */
Monty's avatar
   
Monty committed
226
227
static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
				 int auxparts,int auxpartnum){
Monty's avatar
   
Monty committed
228
  vorbis_info_residue0 *info=look->info;
Monty's avatar
   
Monty committed
229
  int i,j=0;
Monty's avatar
   
Monty committed
230
  float max,localmax=0.f;
Monty's avatar
   
Monty committed
231
232
  float temp[128];
  float entropy[8];
Monty's avatar
   
Monty committed
233
234

  /* setup */
Monty's avatar
   
Monty committed
235
  for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
Monty's avatar
   
Monty committed
236
237

  /* handle case subgrp==1 outside */
Monty's avatar
   
Monty committed
238
  for(i=0;i<n;i++)
Monty's avatar
   
Monty committed
239
240
    if(temp[i]>localmax)localmax=temp[i];
  max=localmax;
Monty's avatar
   
Monty committed
241
242

  for(i=0;i<n;i++)temp[i]=rint(temp[i]);
Monty's avatar
   
Monty committed
243
  
Monty's avatar
   
Monty committed
244
245
246
  while(1){
    entropy[j]=localmax;
    n>>=1;
Monty's avatar
   
Monty committed
247
    if(!n)break;
Monty's avatar
   
Monty committed
248
249
250
251
252
    j++;

    for(i=0;i<n;i++){
      temp[i]+=temp[i+n];
    }
Monty's avatar
   
Monty committed
253
    localmax=0.f;
Monty's avatar
   
Monty committed
254
255
256
257
258
    for(i=0;i<n;i++)
      if(temp[i]>localmax)localmax=temp[i];
  }

  for(i=0;i<auxparts-1;i++)
Monty's avatar
   
Monty committed
259
260
    if(auxpartnum<info->blimit[i] &&
       entropy[info->subgrp[i]]<=info->entmax[i] &&
Monty's avatar
   
Monty committed
261
       max<=info->ampmax[i])
Monty's avatar
   
Monty committed
262
      break;
Monty's avatar
   
Monty committed
263

Monty's avatar
   
Monty committed
264
  return(i);
Monty's avatar
   
Monty committed
265
266
}

Monty's avatar
   
Monty committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
		     int auxparts,int auxpartnum){
  vorbis_info_residue0 *info=look->info;
  int i,j=0;
  float max,localmax=0.f;
  float temp[128];
  float entropy[8];

  /* setup */
  for(i=0;i<n;i++)temp[i]=fabs(vec[i]);

  /* handle case subgrp==1 outside */
  for(i=0;i<n;i++)
    if(temp[i]>localmax)localmax=temp[i];
  max=localmax;

  for(i=0;i<n;i++)temp[i]=rint(temp[i]);
  
  while(n){
    entropy[j]=localmax;
    n>>=1;
    j++;
    if(!n)break;
    for(i=0;i<n;i++){
      temp[i]=temp[i*2]+temp[i*2+1];
    }
    localmax=0.f;
    for(i=0;i<n;i++)
      if(temp[i]>localmax)localmax=temp[i];
  }

  for(i=0;i<auxparts-1;i++)
    if(auxpartnum<info->blimit[i] &&
       entropy[info->subgrp[i]]<=info->entmax[i] &&
       max<=info->ampmax[i])
      break;

  return(i);
}

static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
Monty's avatar
   
Monty committed
308
309
310
311
				   codebook *book,vorbis_look_residue0 *look){
  int i,bits=0;
  int dim=book->dim;
  int step=n/dim;
Monty's avatar
   
Monty committed
312
#ifdef TRAIN_RESENT      
Monty's avatar
   
Monty committed
313
314
315
316
  char buf[80];
  FILE *f;
  sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
  f=fopen(buf,"a");
Monty's avatar
   
Monty committed
317
#endif
Monty's avatar
   
Monty committed
318
319
320
321
322
323
324
325
326

  for(i=0;i<step;i++){
    int entry=vorbis_book_besterror(book,vec+i,step,0);

#ifdef TRAIN_RESENT      
    fprintf(f,"%d\n",entry);
#endif

    bits+=vorbis_book_encode(book,entry,opb);
Monty's avatar
   
Monty committed
327
  }
Monty's avatar
   
Monty committed
328
329
330
331

#ifdef TRAIN_RESENT      
  fclose(f);
#endif
Monty's avatar
   
Monty committed
332
333
334
  return(bits);
}
 
Monty's avatar
   
Monty committed
335
static int _encodepart(oggpack_buffer *opb,float *vec, int n,
Monty's avatar
   
Monty committed
336
337
338
339
		       codebook *book,vorbis_look_residue0 *look){
  int i,bits=0;
  int dim=book->dim;
  int step=n/dim;
Monty's avatar
   
Monty committed
340
#ifdef TRAIN_RESENT      
Monty's avatar
   
Monty committed
341
342
343
344
  char buf[80];
  FILE *f;
  sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
  f=fopen(buf,"a");
Monty's avatar
   
Monty committed
345
#endif
Monty's avatar
   
Monty committed
346

Monty's avatar
   
Monty committed
347
348
  for(i=0;i<step;i++){
    int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
Monty's avatar
   
Monty committed
349

Monty's avatar
   
Monty committed
350
351
352
353
354
#ifdef TRAIN_RESENT      
    fprintf(f,"%d\n",entry);
#endif

    bits+=vorbis_book_encode(book,entry,opb);
Monty's avatar
   
Monty committed
355
  }
Monty's avatar
   
Monty committed
356
357
358
359
360

#ifdef TRAIN_RESENT      
  fclose(f);
#endif
  return(bits);
Monty's avatar
   
Monty committed
361
362
363
364
365
366
}

static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
		      float **in,int ch,
		      int (*classify)(float *,int,vorbis_look_residue0 *,
				      int,int),
Monty's avatar
   
Monty committed
367
368
369
		      int (*encode)(oggpack_buffer *,float *,int,
				    codebook *,vorbis_look_residue0 *)){
  long i,j,k,l,s;
Monty's avatar
   
Monty committed
370
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
Monty's avatar
   
Monty committed
371
  vorbis_info_residue0 *info=look->info;
Monty's avatar
   
Monty committed
372
373
374
375
376
377
378
379
380
381

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
  int partitions_per_word=look->phrasebook->dim;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
Monty's avatar
   
Monty committed
382

Monty's avatar
   
Monty committed
383
384
  partvals=partwords*partitions_per_word;

Monty's avatar
   
Monty committed
385
  /* we find the patition type for each partition of each
Monty's avatar
   
Monty committed
386
387
     channel.  We'll go back and do the interleaved encoding in a
     bit.  For now, clarity */
Monty's avatar
   
Monty committed
388
 
Monty's avatar
   
Monty committed
389
390
391
392
393
  for(i=0;i<ch;i++){
    partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
    memset(partword[i],0,n/samples_per_partition*sizeof(long));
  }

Monty's avatar
   
Monty committed
394
  for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
Monty's avatar
   
Monty committed
395
    for(j=0;j<ch;j++)
Monty's avatar
   
Monty committed
396
397
      /* do the partition decision based on the 'entropy'
         int the block */
Monty's avatar
   
Monty committed
398
      partword[j][l]=
Monty's avatar
   
Monty committed
399
	classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
Monty's avatar
   
Monty committed
400
  
Monty's avatar
   
Monty committed
401
  }
Monty's avatar
   
Monty committed
402

Monty's avatar
   
Monty committed
403
404
  /* we code the partition words for each channel, then the residual
     words for a partition per channel until we've written all the
Monty's avatar
   
Monty committed
405
     residual words for that partition word.  Then write the next
Monty's avatar
   
Monty committed
406
     partition channel words... */
Monty's avatar
   
Monty committed
407
408
409
410
411
412
413
414
415
416
417
418

  for(s=0;s<look->stages;s++){
    for(i=info->begin,l=0;i<info->end;){

      /* first we encode a partition codeword for each channel */
      if(s==0){
	for(j=0;j<ch;j++){
	  long val=partword[j][l];
	  for(k=1;k<partitions_per_word;k++)
	    val= val*possible_partitions+partword[j][l+k];
	  look->phrase+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
	}
Monty's avatar
   
Monty committed
419
      }
Monty's avatar
   
Monty committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

      /* now we encode interleaved residual values for the partitions */
      for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition){
	
	for(j=0;j<ch;j++){
	  if(info->secondstages[partword[j][l]]&(1<<s)){
	    codebook *statebook=look->partbooks[partword[j][l]][s];
	    if(statebook){
	      int ret=encode(&vb->opb,in[j]+i,samples_per_partition,
			       statebook,look);
	      look->bits[partword[j][l]]+=ret;
	      if(s==0)look->vals[partword[j][l]]+=samples_per_partition;
	    }
	  }
	}
      }
    }
Monty's avatar
   
Monty committed
437
438
  }

Monty's avatar
   
Monty committed
439
  return(0);
Monty's avatar
   
Monty committed
440
}
Monty's avatar
   
Monty committed
441

Monty's avatar
   
Monty committed
442
/* a truncated packet here just means 'stop working'; it's not an error */
Monty's avatar
   
Monty committed
443
444
static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
		      float **in,int ch,
Monty's avatar
   
Monty committed
445
446
447
448
		      long (*decodepart)(codebook *, float *, 
					 oggpack_buffer *,int,int)){

  long i,j,k,l,s,transend=vb->pcmend/2;
Monty's avatar
   
Monty committed
449
450
451
452
453
454
455
456
457
458
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int partitions_per_word=look->phrasebook->dim;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
Monty's avatar
   
Monty committed
459
460
  int ***partword=alloca(ch*sizeof(int **));
  float **work=alloca(ch*sizeof(float *));
Monty's avatar
   
Monty committed
461
462
  partvals=partwords*partitions_per_word;

Monty's avatar
   
Monty committed
463
  /* make sure we're zeroed up to the start */
Monty's avatar
   
Monty committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  for(j=0;j<ch;j++){
    work[j]=_vorbis_block_alloc(vb,n*sizeof(float));
    partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
    memset(work[j],0,sizeof(float)*n);
  }

  for(s=0;s<look->stages;s++){
    for(i=info->begin,l=0;i<info->end;l++){

      if(s==0){
	/* fetch the partition word for each channel */
	for(j=0;j<ch;j++){
	  int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
	  if(temp==-1)goto eopbreak;
	  partword[j][l]=look->decodemap[temp];
	  if(partword[j][l]==NULL)goto errout;
	}
Monty's avatar
   
Monty committed
481
      }
Monty's avatar
   
Monty committed
482
483
484
485
486
487
488
489
490
491
492
493
494
      
      /* now we decode residual values for the partitions */
      for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
	for(j=0;j<ch;j++){
	  if(info->secondstages[partword[j][l][k]]&(1<<s)){
	    codebook *stagebook=look->partbooks[partword[j][l][k]][s];
	    if(stagebook){
	      if(decodepart(stagebook,work[j]+i,&vb->opb,
			    samples_per_partition,0)==-1)goto eopbreak;
	    }
	  }
	}
    } 
Monty's avatar
   
Monty committed
495
496
  }

Monty's avatar
   
Monty committed
497
 eopbreak:
Monty's avatar
   
Monty committed
498
499
500
501
502
503
  
  for(j=0;j<ch;j++){
    for(i=0;i<n;i++)
      in[j][i]*=work[j][i];
    for(;i<transend;i++)
      in[j][i]=0;
Monty's avatar
   
Monty committed
504
505
506
507
508
509
  }

  return(0);

 errout:
  for(j=0;j<ch;j++)
Monty's avatar
   
Monty committed
510
    memset(in[j],0,sizeof(float)*transend);
Monty's avatar
   
Monty committed
511
  return(0);
Monty's avatar
   
Monty committed
512
513
}

Monty's avatar
   
Monty committed
514
515
516
517
518
519
520
521
/* residue 0 and 1 are just slight variants of one another. 0 is
   interleaved, 1 is not */
int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
	    float **in,int ch){
  return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
}

int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
Monty's avatar
   
Monty committed
522
  return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs));
Monty's avatar
   
Monty committed
523
524
525
}

int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
Monty's avatar
   
Monty committed
526
		 float **in,int ch){
Monty's avatar
   
Monty committed
527
528
529
530
  return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
}

int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
Monty's avatar
   
Monty committed
531
  return(_01inverse(vb,vl,in,ch,vorbis_book_decodev));
Monty's avatar
   
Monty committed
532
533
}

Monty's avatar
   
Monty committed
534
vorbis_func_residue residue0_exportbundle={
Monty's avatar
   
Monty committed
535
536
537
  &res0_pack,
  &res0_unpack,
  &res0_look,
Monty's avatar
   
Monty committed
538
  &res0_copy_info,
Monty's avatar
   
Monty committed
539
540
541
542
  &res0_free_info,
  &res0_free_look,
  &res0_forward,
  &res0_inverse
Monty's avatar
   
Monty committed
543
};
Monty's avatar
   
Monty committed
544
545
546
547
548
549
550
551
552
553
554

vorbis_func_residue residue1_exportbundle={
  &res0_pack,
  &res0_unpack,
  &res0_look,
  &res0_copy_info,
  &res0_free_info,
  &res0_free_look,
  &res1_forward,
  &res1_inverse
};