1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. 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 and 1 implementation
14 last mod: $Id: res0.c,v 1.28 2001/06/04 05:50:10 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;
43 codebook ***partbooks;
51 } vorbis_look_residue0;
53 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
54 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
55 vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
56 memcpy(ret,info,sizeof(vorbis_info_residue0));
60 void res0_free_info(vorbis_info_residue *i){
62 memset(i,0,sizeof(vorbis_info_residue0));
67 void res0_free_look(vorbis_look_residue *i){
70 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
74 for(j=0;j<look->parts;j++)resbitsT+=look->bits[j];
75 for(j=0;j<look->parts;j++)resvalsT+=look->vals[j];
77 "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
78 resvalsT,look->phrase,resbitsT);
79 for(j=1;j<look->parts;j++)
80 fprintf(stderr,"%ld(%ld):%ld %g/sample\n",j,look->vals[j],look->bits[j],
81 (float)look->bits[j]/look->vals[j]);
85 for(j=0;j<look->parts;j++)
86 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
87 _ogg_free(look->partbooks);
88 for(j=0;j<look->partvals;j++)
89 _ogg_free(look->decodemap[j]);
90 _ogg_free(look->decodemap);
91 memset(i,0,sizeof(vorbis_look_residue0));
96 static int ilog(unsigned int v){
105 static int icount(unsigned int v){
115 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
116 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
118 oggpack_write(opb,info->begin,24);
119 oggpack_write(opb,info->end,24);
121 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
122 code with a partitioned book */
123 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
124 oggpack_write(opb,info->groupbook,8); /* group huffman book */
126 /* secondstages is a bitmask; as encoding progresses pass by pass, a
127 bitmask of one indicates this partition class has bits to write
129 for(j=0;j<info->partitions;j++){
130 if(ilog(info->secondstages[j])>3){
131 /* yes, this is a minor hack due to not thinking ahead */
132 oggpack_write(opb,info->secondstages[j],3);
133 oggpack_write(opb,1,1);
134 oggpack_write(opb,info->secondstages[j]>>3,5);
136 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
137 acc+=icount(info->secondstages[j]);
140 oggpack_write(opb,info->booklist[j],8);
144 /* vorbis_info is for range checking */
145 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
147 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
148 codec_setup_info *ci=vi->codec_setup;
150 info->begin=oggpack_read(opb,24);
151 info->end=oggpack_read(opb,24);
152 info->grouping=oggpack_read(opb,24)+1;
153 info->partitions=oggpack_read(opb,6)+1;
154 info->groupbook=oggpack_read(opb,8);
156 for(j=0;j<info->partitions;j++){
157 int cascade=info->secondstages[j]=oggpack_read(opb,3);
158 if(oggpack_read(opb,1))
159 cascade|=(oggpack_read(opb,5)<<3);
160 acc+=icount(cascade);
163 info->booklist[j]=oggpack_read(opb,8);
165 if(info->groupbook>=ci->books)goto errout;
167 if(info->booklist[j]>=ci->books)goto errout;
171 res0_free_info(info);
175 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
176 vorbis_info_residue *vr){
177 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
178 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
179 backend_lookup_state *be=vd->backend_state;
185 look->map=vm->mapping;
187 look->parts=info->partitions;
188 look->fullbooks=be->fullbooks;
189 look->phrasebook=be->fullbooks+info->groupbook;
190 dim=look->phrasebook->dim;
192 look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
194 for(j=0;j<look->parts;j++){
195 int stages=ilog(info->secondstages[j]);
197 if(stages>maxstage)maxstage=stages;
198 look->partbooks[j]=_ogg_calloc(stages,sizeof(codebook *));
199 for(k=0;k<stages;k++)
200 if(info->secondstages[j]&(1<<k))
201 look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
205 look->partvals=rint(pow(look->parts,dim));
206 look->stages=maxstage;
207 look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
208 for(j=0;j<look->partvals;j++){
210 long mult=look->partvals/look->parts;
211 look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
216 look->decodemap[j][k]=deco;
224 /* does not guard against invalid settings; eg, a subn of 16 and a
225 subgroup request of 32. Max subn of 128 */
226 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
227 int auxparts,int auxpartnum){
228 vorbis_info_residue0 *info=look->info;
230 float max,localmax=0.f;
235 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
237 /* handle case subgrp==1 outside */
239 if(temp[i]>localmax)localmax=temp[i];
242 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
255 if(temp[i]>localmax)localmax=temp[i];
258 for(i=0;i<auxparts-1;i++)
259 if(auxpartnum<info->blimit[i] &&
260 entropy[info->subgrp[i]]<=info->entmax[i] &&
261 max<=info->ampmax[i])
267 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
268 int auxparts,int auxpartnum){
269 vorbis_info_residue0 *info=look->info;
271 float max,localmax=0.f;
276 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
278 /* handle case subgrp==1 outside */
280 if(temp[i]>localmax)localmax=temp[i];
283 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
291 temp[i]=temp[i*2]+temp[i*2+1];
295 if(temp[i]>localmax)localmax=temp[i];
298 for(i=0;i<auxparts-1;i++)
299 if(auxpartnum<info->blimit[i] &&
300 entropy[info->subgrp[i]]<=info->entmax[i] &&
301 max<=info->ampmax[i])
307 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
308 codebook *book,vorbis_look_residue0 *look){
315 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
320 int entry=vorbis_book_besterror(book,vec+i,step,0);
323 fprintf(f,"%d\n",entry);
326 bits+=vorbis_book_encode(book,entry,opb);
335 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
336 codebook *book,vorbis_look_residue0 *look){
343 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
348 int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
351 fprintf(f,"%d\n",entry);
354 bits+=vorbis_book_encode(book,entry,opb);
363 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
365 int (*classify)(float *,int,vorbis_look_residue0 *,
367 int (*encode)(oggpack_buffer *,float *,int,
368 codebook *,vorbis_look_residue0 *)){
370 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
371 vorbis_info_residue0 *info=look->info;
373 /* move all this setup out later */
374 int samples_per_partition=info->grouping;
375 int possible_partitions=info->partitions;
376 int partitions_per_word=look->phrasebook->dim;
377 int n=info->end-info->begin;
379 int partvals=n/samples_per_partition;
380 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
381 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
383 partvals=partwords*partitions_per_word;
385 /* we find the patition type for each partition of each
386 channel. We'll go back and do the interleaved encoding in a
387 bit. For now, clarity */
390 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
391 memset(partword[i],0,n/samples_per_partition*sizeof(long));
394 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
396 /* do the partition decision based on the 'entropy'
399 classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
403 /* we code the partition words for each channel, then the residual
404 words for a partition per channel until we've written all the
405 residual words for that partition word. Then write the next
406 partition channel words... */
408 for(s=0;s<look->stages;s++){
409 for(i=info->begin,l=0;i<info->end;){
411 /* first we encode a partition codeword for each channel */
414 long val=partword[j][l];
415 for(k=1;k<partitions_per_word;k++)
416 val= val*possible_partitions+partword[j][l+k];
417 look->phrase+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
421 /* now we encode interleaved residual values for the partitions */
422 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition){
425 if(info->secondstages[partword[j][l]]&(1<<s)){
426 codebook *statebook=look->partbooks[partword[j][l]][s];
428 int ret=encode(&vb->opb,in[j]+i,samples_per_partition,
430 look->bits[partword[j][l]]+=ret;
431 if(s==0)look->vals[partword[j][l]]+=samples_per_partition;
442 /* a truncated packet here just means 'stop working'; it's not an error */
443 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
445 long (*decodepart)(codebook *, float *,
446 oggpack_buffer *,int,int)){
448 long i,j,k,l,s,transend=vb->pcmend/2;
449 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
450 vorbis_info_residue0 *info=look->info;
452 /* move all this setup out later */
453 int samples_per_partition=info->grouping;
454 int partitions_per_word=look->phrasebook->dim;
455 int n=info->end-info->begin;
457 int partvals=n/samples_per_partition;
458 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
459 int ***partword=alloca(ch*sizeof(int **));
460 float **work=alloca(ch*sizeof(float *));
461 partvals=partwords*partitions_per_word;
463 /* make sure we're zeroed up to the start */
465 work[j]=_vorbis_block_alloc(vb,n*sizeof(float));
466 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
467 memset(work[j],0,sizeof(float)*n);
470 for(s=0;s<look->stages;s++){
471 for(i=info->begin,l=0;i<info->end;l++){
474 /* fetch the partition word for each channel */
476 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
477 if(temp==-1)goto eopbreak;
478 partword[j][l]=look->decodemap[temp];
479 if(partword[j][l]==NULL)goto errout;
483 /* now we decode residual values for the partitions */
484 for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
486 if(info->secondstages[partword[j][l][k]]&(1<<s)){
487 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
489 if(decodepart(stagebook,work[j]+i,&vb->opb,
490 samples_per_partition,0)==-1)goto eopbreak;
501 in[j][i]*=work[j][i];
510 memset(in[j],0,sizeof(float)*transend);
514 /* residue 0 and 1 are just slight variants of one another. 0 is
515 interleaved, 1 is not */
516 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
518 return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
521 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
522 return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs));
525 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
527 return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
530 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
531 return(_01inverse(vb,vl,in,ch,vorbis_book_decodev));
534 vorbis_func_residue residue0_exportbundle={
545 vorbis_func_residue residue1_exportbundle={