1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: residue backend 0 implementation
14 last mod: $Id: res0.c,v 1.25 2001/02/02 03:51:57 xiphmont Exp $
16 ********************************************************************/
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,
28 #include "vorbis/codec.h"
29 #include "codec_internal.h"
36 vorbis_info_residue0 *info;
41 codebook ***partbooks;
45 } vorbis_look_residue0;
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));
54 void res0_free_info(vorbis_info_residue *i){
56 memset(i,0,sizeof(vorbis_info_residue0));
61 void res0_free_look(vorbis_look_residue *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));
76 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
77 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
79 oggpack_write(opb,info->begin,24);
80 oggpack_write(opb,info->end,24);
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];
91 oggpack_write(opb,info->booklist[j],8);
95 /* vorbis_info is for range checking */
96 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
98 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
99 codec_setup_info *ci=vi->codec_setup;
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 */
114 info->booklist[j]=oggpack_read(opb,8);
115 if(info->booklist[j]==255)info->booklist[j]=-1;
118 if(info->groupbook>=ci->books)goto errout;
120 if(info->booklist[j]>=ci->books)goto errout;
124 res0_free_info(info);
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;
137 look->map=vm->mapping;
139 look->parts=info->partitions;
140 look->phrasebook=be->fullbooks+info->groupbook;
141 dim=look->phrasebook->dim;
143 look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
145 for(j=0;j<look->parts;j++){
146 int stages=info->secondstages[j];
148 look->partbooks[j]=_ogg_malloc(stages*sizeof(codebook *));
149 look->partbooks[j][0]=be->fullbooks+info->booklist[acc++];
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++){
157 long mult=look->partvals/look->parts;
158 look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
163 look->decodemap[j][k]=deco;
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 _testhack(float *vec,int n,vorbis_look_residue0 *look,
174 int auxparts,int auxpartnum){
175 vorbis_info_residue0 *info=look->info;
177 float max,localmax=0.f;
182 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
184 /* handle case subgrp==1 outside */
186 if(temp[i]>localmax)localmax=temp[i];
189 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
202 if(temp[i]>localmax)localmax=temp[i];
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])
214 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
215 int stages, codebook **books,int mode,int part){
218 int dim=books[j]->dim;
221 int entry=vorbis_book_besterror(books[j],vec+i,step,0);
226 sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
228 fprintf(f,"%d\n",entry);
232 bits+=vorbis_book_encode(books[j],entry,opb);
238 static int _decodepart(oggpack_buffer *opb,float *work,float *vec, int n,
239 int stages, codebook **books){
242 memset(work,0,sizeof(float)*n);
244 int dim=books[0]->dim;
246 if(s_vorbis_book_decodevs(books[0],work,opb,step,0)==-1)
256 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
259 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
260 vorbis_info_residue0 *info=look->info;
262 /* move all this setup out later */
263 int samples_per_partition=info->grouping;
264 int possible_partitions=info->partitions;
265 int partitions_per_word=look->phrasebook->dim;
266 int n=info->end-info->begin;
267 long phrasebits=0,resbitsT=0;
268 long *resbits=alloca(sizeof(long)*possible_partitions);
269 long *resvals=alloca(sizeof(long)*possible_partitions);
271 int partvals=n/samples_per_partition;
272 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
273 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
275 partvals=partwords*partitions_per_word;
277 /* we find the patition type for each partition of each
278 channel. We'll go back and do the interleaved encoding in a
279 bit. For now, clarity */
281 memset(resbits,0,sizeof(long)*possible_partitions);
282 memset(resvals,0,sizeof(long)*possible_partitions);
285 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
286 memset(partword[i],0,n/samples_per_partition*sizeof(long));
289 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
291 /* do the partition decision based on the number of 'bits'
292 needed to encode the block */
294 _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
297 /* we code the partition words for each channel, then the residual
298 words for a partition per channel until we've written all the
299 residual words for that partition word. Then write the next
300 partition channel words... */
302 for(i=info->begin,l=0;i<info->end;){
304 /* first we encode a partition codeword for each channel */
306 long val=partword[j][l];
307 for(k=1;k<partitions_per_word;k++)
308 val= val*possible_partitions+partword[j][l+k];
309 phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
311 /* now we encode interleaved residual values for the partitions */
312 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
314 /*resbits[partword[j][l]]+=*/
315 resbitsT+=_encodepart(&vb->opb,in[j]+i,samples_per_partition,
316 info->secondstages[partword[j][l]],
317 look->partbooks[partword[j][l]],look->map,partword[j][l]);
318 resvals[partword[j][l]]+=samples_per_partition;
323 for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
325 "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
326 ch*(info->end-info->begin),phrasebits,resbitsT);
327 for(i=0;i<possible_partitions;i++)
328 fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
329 fprintf(stderr,"\n");*/
334 /* a truncated packet here just means 'stop working'; it's not an error */
335 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
336 long i,j,k,l,transend=vb->pcmend/2;
337 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
338 vorbis_info_residue0 *info=look->info;
340 /* move all this setup out later */
341 int samples_per_partition=info->grouping;
342 int partitions_per_word=look->phrasebook->dim;
343 int n=info->end-info->begin;
345 int partvals=n/samples_per_partition;
346 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
347 int **partword=alloca(ch*sizeof(long *));
348 float *work=alloca(sizeof(float)*samples_per_partition);
349 partvals=partwords*partitions_per_word;
351 /* make sure we're zeroed up to the start */
353 memset(in[j],0,sizeof(float)*info->begin);
355 for(i=info->begin,l=0;i<info->end;){
356 /* fetch the partition word for each channel */
358 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
359 if(temp==-1)goto eopbreak;
360 partword[j]=look->decodemap[temp];
361 if(partword[j]==NULL)goto errout;
364 /* now we decode interleaved residual values for the partitions */
365 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
367 int part=partword[j][k];
368 if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
369 info->secondstages[part],
370 look->partbooks[part])==-1)goto eopbreak;
377 memset(in[j]+i,0,sizeof(float)*(transend-i));
384 memset(in[j],0,sizeof(float)*transend);
388 vorbis_func_residue residue0_exportbundle={