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.13 2000/06/14 01:38:31 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 acc+=info->secondstages[j]=_oggpack_read(opb,4);
102 info->booklist[j]=_oggpack_read(opb,8);
104 if(info->groupbook>=vi->books)goto errout;
106 if(info->booklist[j]>=vi->books)goto errout;
114 vorbis_look_residue *look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
115 vorbis_info_residue *vr){
116 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
117 vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
122 look->parts=info->partitions;
123 look->phrasebook=vd->fullbooks+info->groupbook;
124 dim=look->phrasebook->dim;
126 look->partbooks=calloc(look->parts,sizeof(codebook **));
128 for(j=0;j<look->parts;j++){
129 int stages=info->secondstages[j];
131 look->partbooks[j]=malloc(stages*sizeof(codebook *));
132 for(k=0;k<stages;k++)
133 look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
137 look->partvals=rint(pow(look->parts,dim));
138 look->decodemap=malloc(look->partvals*sizeof(int *));
139 for(j=0;j<look->partvals;j++){
141 long mult=look->partvals/look->parts;
142 look->decodemap[j]=malloc(dim*sizeof(int));
147 look->decodemap[j][k]=deco;
155 /* does not guard against invalid settings; eg, a subn of 16 and a
156 subgroup request of 32. Max subn of 128 */
157 static int _testhack(double *vec,int n,vorbis_look_residue0 *look,int auxparts){
158 vorbis_info_residue0 *info=look->info;
160 double max,localmax=0.;
165 for(i=0;i<n;i++)temp[i]=fabs(rint(vec[i]));
167 /* handle case subgrp==1 outside */
169 if(temp[i]>localmax)localmax=temp[i];
183 if(temp[i]>localmax)localmax=temp[i];
186 for(i=0;i<auxparts-1;i++)
187 if(entropy[info->subgrp[i]]<=info->entmax[i] &&
188 max<=info->ampmax[i])
194 static int _encodepart(oggpack_buffer *opb,double *vec, int n,
195 int stages, codebook **books){
198 for(j=0;j<stages;j++){
199 int dim=books[j]->dim;
202 bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
209 static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
210 int stages, codebook **books){
213 memset(work,0,sizeof(double)*n);
214 for(j=0;j<stages;j++){
215 int dim=books[j]->dim;
218 vorbis_book_decodevs(books[j],work+i,opb,step,0);
227 int forward(vorbis_block *vb,vorbis_look_residue *vl,
230 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
231 vorbis_info_residue0 *info=look->info;
233 /* move all this setup out later */
234 int samples_per_partition=info->grouping;
235 int possible_partitions=info->partitions;
236 int partitions_per_word=look->phrasebook->dim;
237 int n=info->end-info->begin;
238 long phrasebits=0,resbitsT=0;
239 long *resbits=alloca(sizeof(long)*possible_partitions);
240 long *resvals=alloca(sizeof(long)*possible_partitions);
242 int partvals=n/samples_per_partition;
243 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
244 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
245 long auxperpart=(info->Bpoint==-1?look->parts:
246 (info->Cpoint==-1?look->parts/2:look->parts/3));
248 partvals=partwords*partitions_per_word;
250 /* we find the patition type for each partition of each
251 channel. We'll go back and do the interleaved encoding in a
252 bit. For now, clarity */
254 memset(resbits,0,sizeof(long)*possible_partitions);
255 memset(resvals,0,sizeof(long)*possible_partitions);
258 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
259 memset(partword[i],0,n/samples_per_partition*sizeof(long));
262 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
263 int offset=((info->Bpoint==-1 || l<info->Bpoint)?0:
264 ((info->Cpoint==-1 || l<info->Cpoint)?auxperpart:
268 /* do the partition decision based on the number of 'bits'
269 needed to encode the block */
271 _testhack(in[j]+i,samples_per_partition,look,auxperpart)+offset;
274 /* we code the partition words for each channel, then the residual
275 words for a partition per channel until we've written all the
276 residual words for that partition word. Then write the next
277 parition channel words... */
279 for(i=info->begin,l=0;i<info->end;){
280 /* first we encode a partition codeword for each channel */
282 long val=partword[j][l];
283 for(k=1;k<partitions_per_word;k++)
284 val= val*possible_partitions+partword[j][l+k];
285 phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
287 /* now we encode interleaved residual values for the partitions */
288 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
290 resbits[partword[j][l]]+=
291 _encodepart(&vb->opb,in[j]+i,samples_per_partition,
292 info->secondstages[partword[j][l]],
293 look->partbooks[partword[j][l]]);
294 resvals[partword[j][l]]+=samples_per_partition;
299 for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
301 "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
302 ch*(info->end-info->begin),phrasebits,resbitsT);
303 for(i=0;i<possible_partitions;i++)
304 fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
305 fprintf(stderr,"\n");*/
310 int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
312 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
313 vorbis_info_residue0 *info=look->info;
315 /* move all this setup out later */
316 int samples_per_partition=info->grouping;
317 int partitions_per_word=look->phrasebook->dim;
318 int n=info->end-info->begin;
320 int partvals=n/samples_per_partition;
321 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
322 int **partword=alloca(ch*sizeof(long *));
323 double *work=alloca(sizeof(double)*samples_per_partition);
324 partvals=partwords*partitions_per_word;
326 for(i=info->begin,l=0;i<info->end;){
327 /* fetch the partition word for each channel */
329 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
330 partword[j]=look->decodemap[temp];
331 if(partword[j]==NULL)exit(1);
334 /* now we decode interleaved residual values for the partitions */
335 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
337 int part=partword[j][k];
338 _decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
339 info->secondstages[part],
340 look->partbooks[part]);
347 vorbis_func_residue residue0_exportbundle={