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.18 2000/10/12 03:12:53 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,
29 #include "vorbis/codec.h"
31 #include "bookinternal.h"
32 #include "sharedbook.h"
37 vorbis_info_residue0 *info;
42 codebook ***partbooks;
46 } vorbis_look_residue0;
48 void res0_free_info(vorbis_info_residue *i){
50 memset(i,0,sizeof(vorbis_info_residue0));
55 void res0_free_look(vorbis_look_residue *i){
58 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
59 for(j=0;j<look->parts;j++)
60 if(look->partbooks[j])free(look->partbooks[j]);
61 free(look->partbooks);
62 for(j=0;j<look->partvals;j++)
63 free(look->decodemap[j]);
64 free(look->decodemap);
65 memset(i,0,sizeof(vorbis_look_residue0));
70 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
71 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
73 oggpack_write(opb,info->begin,24);
74 oggpack_write(opb,info->end,24);
76 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
77 code with a partitioned book */
78 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
79 oggpack_write(opb,info->groupbook,8); /* group huffman book */
80 for(j=0;j<info->partitions;j++){
81 oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
82 acc+=info->secondstages[j];
85 oggpack_write(opb,info->booklist[j],8);
89 /* vorbis_info is for range checking */
90 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
92 vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
94 info->begin=oggpack_read(opb,24);
95 info->end=oggpack_read(opb,24);
96 info->grouping=oggpack_read(opb,24)+1;
97 info->partitions=oggpack_read(opb,6)+1;
98 info->groupbook=oggpack_read(opb,8);
99 for(j=0;j<info->partitions;j++){
100 int cascade=info->secondstages[j]=oggpack_read(opb,4);
101 if(cascade>1)goto errout; /* temporary! when cascading gets
102 reworked and actually used, we don't
103 want old code to DTWT */
107 info->booklist[j]=oggpack_read(opb,8);
109 if(info->groupbook>=vi->books)goto errout;
111 if(info->booklist[j]>=vi->books)goto errout;
115 res0_free_info(info);
119 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
120 vorbis_info_residue *vr){
121 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
122 vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
126 look->map=vm->mapping;
128 look->parts=info->partitions;
129 look->phrasebook=vd->fullbooks+info->groupbook;
130 dim=look->phrasebook->dim;
132 look->partbooks=calloc(look->parts,sizeof(codebook **));
134 for(j=0;j<look->parts;j++){
135 int stages=info->secondstages[j];
137 look->partbooks[j]=malloc(stages*sizeof(codebook *));
138 for(k=0;k<stages;k++)
139 look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
143 look->partvals=rint(pow(look->parts,dim));
144 look->decodemap=malloc(look->partvals*sizeof(int *));
145 for(j=0;j<look->partvals;j++){
147 long mult=look->partvals/look->parts;
148 look->decodemap[j]=malloc(dim*sizeof(int));
153 look->decodemap[j][k]=deco;
161 /* does not guard against invalid settings; eg, a subn of 16 and a
162 subgroup request of 32. Max subn of 128 */
163 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
164 int auxparts,int auxpartnum){
165 vorbis_info_residue0 *info=look->info;
167 float max,localmax=0.;
172 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
174 /* handle case subgrp==1 outside */
176 if(temp[i]>localmax)localmax=temp[i];
179 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
192 if(temp[i]>localmax)localmax=temp[i];
195 for(i=0;i<auxparts-1;i++)
196 if(auxpartnum<info->blimit[i] &&
197 entropy[info->subgrp[i]]<=info->entmax[i] &&
198 max<=info->ampmax[i])
204 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
205 int stages, codebook **books,int mode,int part){
208 for(j=0;j<stages;j++){
209 int dim=books[j]->dim;
212 int entry=vorbis_book_besterror(books[j],vec+i,step,0);
217 sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
219 fprintf(f,"%d\n",entry);
223 bits+=vorbis_book_encode(books[j],entry,opb);
230 static int _decodepart(oggpack_buffer *opb,float *work,float *vec, int n,
231 int stages, codebook **books){
234 memset(work,0,sizeof(float)*n);
235 for(i=0;i<stages;i++){
236 int dim=books[i]->dim;
238 if(s_vorbis_book_decodevs(books[i],work,opb,step,0)==-1)
248 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
251 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
252 vorbis_info_residue0 *info=look->info;
254 /* move all this setup out later */
255 int samples_per_partition=info->grouping;
256 int possible_partitions=info->partitions;
257 int partitions_per_word=look->phrasebook->dim;
258 int n=info->end-info->begin;
259 long phrasebits=0,resbitsT=0;
260 long *resbits=alloca(sizeof(long)*possible_partitions);
261 long *resvals=alloca(sizeof(long)*possible_partitions);
263 int partvals=n/samples_per_partition;
264 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
265 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
267 partvals=partwords*partitions_per_word;
269 /* we find the patition type for each partition of each
270 channel. We'll go back and do the interleaved encoding in a
271 bit. For now, clarity */
273 memset(resbits,0,sizeof(long)*possible_partitions);
274 memset(resvals,0,sizeof(long)*possible_partitions);
277 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
278 memset(partword[i],0,n/samples_per_partition*sizeof(long));
281 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
283 /* do the partition decision based on the number of 'bits'
284 needed to encode the block */
286 _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
289 /* we code the partition words for each channel, then the residual
290 words for a partition per channel until we've written all the
291 residual words for that partition word. Then write the next
292 partition channel words... */
294 for(i=info->begin,l=0;i<info->end;){
295 /* first we encode a partition codeword for each channel */
297 long val=partword[j][l];
298 for(k=1;k<partitions_per_word;k++)
299 val= val*possible_partitions+partword[j][l+k];
300 phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
302 /* now we encode interleaved residual values for the partitions */
303 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
305 resbits[partword[j][l]]+=
306 _encodepart(&vb->opb,in[j]+i,samples_per_partition,
307 info->secondstages[partword[j][l]],
308 look->partbooks[partword[j][l]],look->map,partword[j][l]);
309 resvals[partword[j][l]]+=samples_per_partition;
314 for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
316 "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
317 ch*(info->end-info->begin),phrasebits,resbitsT);
318 for(i=0;i<possible_partitions;i++)
319 fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
320 fprintf(stderr,"\n");*/
325 /* a truncated packet here just means 'stop working'; it's not an error */
326 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
327 long i,j,k,l,transend=vb->pcmend/2;
328 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
329 vorbis_info_residue0 *info=look->info;
331 /* move all this setup out later */
332 int samples_per_partition=info->grouping;
333 int partitions_per_word=look->phrasebook->dim;
334 int n=info->end-info->begin;
336 int partvals=n/samples_per_partition;
337 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
338 int **partword=alloca(ch*sizeof(long *));
339 float *work=alloca(sizeof(float)*samples_per_partition);
340 partvals=partwords*partitions_per_word;
342 /* make sure we're zeroed up to the start */
344 memset(in[j],0,sizeof(float)*info->begin);
346 for(i=info->begin,l=0;i<info->end;){
347 /* fetch the partition word for each channel */
349 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
350 if(temp==-1)goto eopbreak;
351 partword[j]=look->decodemap[temp];
352 if(partword[j]==NULL)goto errout;
355 /* now we decode interleaved residual values for the partitions */
356 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
358 int part=partword[j][k];
359 if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
360 info->secondstages[part],
361 look->partbooks[part])==-1)goto eopbreak;
368 memset(in[j]+i,0,sizeof(float)*(transend-i));
375 memset(in[j],0,sizeof(float)*transend);
379 vorbis_func_residue residue0_exportbundle={