1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: residue backend 0 implementation
15 last mod: $Id: res0.c,v 1.16 2000/08/14 21:53:49 xiphmont Exp $
17 ********************************************************************/
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,
28 #include "vorbis/codec.h"
31 #include "bookinternal.h"
32 #include "sharedbook.h"
37 vorbis_info_residue0 *info;
41 codebook ***partbooks;
45 } vorbis_look_residue0;
47 void free_info(vorbis_info_residue *i){
49 memset(i,0,sizeof(vorbis_info_residue0));
54 void free_look(vorbis_look_residue *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));
69 void pack(vorbis_info_residue *vr,oggpack_buffer *opb){
70 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
72 _oggpack_write(opb,info->begin,24);
73 _oggpack_write(opb,info->end,24);
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];
84 _oggpack_write(opb,info->booklist[j],8);
88 /* vorbis_info is for range checking */
89 vorbis_info_residue *unpack(vorbis_info *vi,oggpack_buffer *opb){
91 vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
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 int cascade=info->secondstages[j]=_oggpack_read(opb,4);
100 if(cascade>1)goto errout; /* temporary! when cascading gets
101 reworked and actually used, we don't
102 want old code to DTWT */
106 info->booklist[j]=_oggpack_read(opb,8);
108 if(info->groupbook>=vi->books)goto errout;
110 if(info->booklist[j]>=vi->books)goto errout;
118 vorbis_look_residue *look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
119 vorbis_info_residue *vr){
120 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
121 vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
126 look->parts=info->partitions;
127 look->phrasebook=vd->fullbooks+info->groupbook;
128 dim=look->phrasebook->dim;
130 look->partbooks=calloc(look->parts,sizeof(codebook **));
132 for(j=0;j<look->parts;j++){
133 int stages=info->secondstages[j];
135 look->partbooks[j]=malloc(stages*sizeof(codebook *));
136 for(k=0;k<stages;k++)
137 look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
141 look->partvals=rint(pow(look->parts,dim));
142 look->decodemap=malloc(look->partvals*sizeof(int *));
143 for(j=0;j<look->partvals;j++){
145 long mult=look->partvals/look->parts;
146 look->decodemap[j]=malloc(dim*sizeof(int));
151 look->decodemap[j][k]=deco;
159 /* does not guard against invalid settings; eg, a subn of 16 and a
160 subgroup request of 32. Max subn of 128 */
161 static int _testhack(double *vec,int n,vorbis_look_residue0 *look,
162 int auxparts,int auxpartnum){
163 vorbis_info_residue0 *info=look->info;
165 double max,localmax=0.;
170 for(i=0;i<n;i++)temp[i]=fabs(rint(vec[i]));
172 /* handle case subgrp==1 outside */
174 if(temp[i]>localmax)localmax=temp[i];
188 if(temp[i]>localmax)localmax=temp[i];
191 for(i=0;i<auxparts-1;i++)
192 if(auxpartnum<info->blimit[i] &&
193 entropy[info->subgrp[i]]<=info->entmax[i] &&
194 max<=info->ampmax[i])
200 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
201 int stages, codebook **books){
204 for(j=0;j<stages;j++){
205 int dim=books[j]->dim;
208 bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
215 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
216 int stages, codebook **books){
219 memset(work,0,sizeof(double)*n);
220 for(j=0;j<stages;j++){
221 int dim=books[j]->dim;
224 if(vorbis_book_decodevs(books[j],work+i,opb,step,0)==-1)
234 int forward(vorbis_block *vb,vorbis_look_residue *vl,
237 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
238 vorbis_info_residue0 *info=look->info;
240 /* move all this setup out later */
241 int samples_per_partition=info->grouping;
242 int possible_partitions=info->partitions;
243 int partitions_per_word=look->phrasebook->dim;
244 int n=info->end-info->begin;
245 long phrasebits=0,resbitsT=0;
246 long *resbits=alloca(sizeof(long)*possible_partitions);
247 long *resvals=alloca(sizeof(long)*possible_partitions);
249 int partvals=n/samples_per_partition;
250 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
251 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
253 partvals=partwords*partitions_per_word;
255 /* we find the patition type for each partition of each
256 channel. We'll go back and do the interleaved encoding in a
257 bit. For now, clarity */
259 memset(resbits,0,sizeof(long)*possible_partitions);
260 memset(resvals,0,sizeof(long)*possible_partitions);
263 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
264 memset(partword[i],0,n/samples_per_partition*sizeof(long));
267 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
269 /* do the partition decision based on the number of 'bits'
270 needed to encode the block */
272 _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
275 /* we code the partition words for each channel, then the residual
276 words for a partition per channel until we've written all the
277 residual words for that partition word. Then write the next
278 parition channel words... */
280 for(i=info->begin,l=0;i<info->end;){
281 /* first we encode a partition codeword for each channel */
283 long val=partword[j][l];
284 for(k=1;k<partitions_per_word;k++)
285 val= val*possible_partitions+partword[j][l+k];
286 phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
288 /* now we encode interleaved residual values for the partitions */
289 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
291 resbits[partword[j][l]]+=
292 _encodepart(&vb->opb,in[j]+i,samples_per_partition,
293 info->secondstages[partword[j][l]],
294 look->partbooks[partword[j][l]]);
295 resvals[partword[j][l]]+=samples_per_partition;
300 for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
302 "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
303 ch*(info->end-info->begin),phrasebits,resbitsT);
304 for(i=0;i<possible_partitions;i++)
305 fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
306 fprintf(stderr,"\n");*/
311 /* a truncated packet here just means 'stop working'; it's not an error */
312 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
313 long i,j,k,l,transend=vb->pcmend/2;
314 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
315 vorbis_info_residue0 *info=look->info;
317 /* move all this setup out later */
318 int samples_per_partition=info->grouping;
319 int partitions_per_word=look->phrasebook->dim;
320 int n=info->end-info->begin;
322 int partvals=n/samples_per_partition;
323 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
324 int **partword=alloca(ch*sizeof(long *));
325 double *work=alloca(sizeof(double)*samples_per_partition);
326 partvals=partwords*partitions_per_word;
328 /* make sure we're zeroed up to the start */
330 memset(in[j],0,sizeof(double)*info->begin);
332 for(i=info->begin,l=0;i<info->end;){
333 /* fetch the partition word for each channel */
335 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
336 if(temp==-1)goto eopbreak;
337 partword[j]=look->decodemap[temp];
338 if(partword[j]==NULL)goto errout;
341 /* now we decode interleaved residual values for the partitions */
342 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
344 int part=partword[j][k];
345 if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
346 info->secondstages[part],
347 look->partbooks[part])==-1)goto eopbreak;
354 memset(in[j]+i,0,sizeof(double)*(transend-i));
361 memset(in[j],0,sizeof(double)*transend);
365 vorbis_func_residue residue0_exportbundle={