FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libtremor / res012.c
bloba42660a06571abc2325071d61161b3dd2b38128a
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
4 * *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
11 * *
12 ********************************************************************
14 function: residue backend 0, 1 and 2 implementation
16 ********************************************************************/
18 #include "config-tremor.h"
19 #include <string.h>
20 #include <math.h>
21 #include "ogg.h"
22 #include "ivorbiscodec.h"
23 #include "codec_internal.h"
24 #include "registry.h"
25 #include "codebook.h"
26 #include "misc.h"
27 #include "os.h"
29 typedef struct {
30 vorbis_info_residue0 *info;
31 int map;
33 int parts;
34 int stages;
35 codebook *fullbooks;
36 codebook *phrasebook;
37 codebook ***partbooks;
39 int partvals;
40 int **decodemap;
42 } vorbis_look_residue0;
44 static void res0_free_info(vorbis_info_residue *i){
45 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
46 if(info){
47 memset(info,0,sizeof(*info));
48 _ogg_free(info);
52 static void res0_free_look(vorbis_look_residue *i){
53 int j;
54 if(i){
56 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
58 for(j=0;j<look->parts;j++)
59 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
60 _ogg_free(look->partbooks);
61 for(j=0;j<look->partvals;j++)
62 _ogg_free(look->decodemap[j]);
63 _ogg_free(look->decodemap);
65 memset(look,0,sizeof(*look));
66 _ogg_free(look);
70 static int ilog(unsigned int v){
71 int ret=0;
72 while(v){
73 ret++;
74 v>>=1;
76 return(ret);
79 static int icount(unsigned int v){
80 int ret=0;
81 while(v){
82 ret+=v&1;
83 v>>=1;
85 return(ret);
88 /* vorbis_info is for range checking */
89 static vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
90 int j,acc=0;
91 vorbis_info_residue0 *info=(vorbis_info_residue0 *)_ogg_calloc(1,sizeof(*info));
92 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
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);
100 for(j=0;j<info->partitions;j++){
101 int cascade=oggpack_read(opb,3);
102 if(oggpack_read(opb,1))
103 cascade|=(oggpack_read(opb,5)<<3);
104 info->secondstages[j]=cascade;
106 acc+=icount(cascade);
108 for(j=0;j<acc;j++)
109 info->booklist[j]=oggpack_read(opb,8);
111 if(info->groupbook>=ci->books)goto errout;
112 for(j=0;j<acc;j++)
113 if(info->booklist[j]>=ci->books)goto errout;
115 return(info);
116 errout:
117 res0_free_info(info);
118 return(NULL);
121 static vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
122 vorbis_info_residue *vr){
123 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
124 vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look));
125 codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
127 int j,k,acc=0;
128 int dim;
129 int maxstage=0;
130 look->info=info;
131 look->map=vm->mapping;
133 look->parts=info->partitions;
134 look->fullbooks=ci->fullbooks;
135 look->phrasebook=ci->fullbooks+info->groupbook;
136 dim=look->phrasebook->dim;
138 look->partbooks=(codebook ***)_ogg_calloc(look->parts,sizeof(*look->partbooks));
140 for(j=0;j<look->parts;j++){
141 int stages=ilog(info->secondstages[j]);
142 if(stages){
143 if(stages>maxstage)maxstage=stages;
144 look->partbooks[j]=(codebook **)_ogg_calloc(stages,sizeof(*look->partbooks[j]));
145 for(k=0;k<stages;k++)
146 if(info->secondstages[j]&(1<<k)){
147 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
148 #ifdef TRAIN_RES
149 look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
150 sizeof(***look->training_data));
151 #endif
156 look->partvals=look->parts;
157 for(j=1;j<dim;j++)look->partvals*=look->parts;
158 look->stages=maxstage;
159 look->decodemap=(int **)_ogg_malloc(look->partvals*sizeof(*look->decodemap));
160 for(j=0;j<look->partvals;j++){
161 long val=j;
162 long mult=look->partvals/look->parts;
163 look->decodemap[j]=(int *)_ogg_malloc(dim*sizeof(*look->decodemap[j]));
164 for(k=0;k<dim;k++){
165 long deco=val/mult;
166 val-=deco*mult;
167 mult/=look->parts;
168 look->decodemap[j][k]=deco;
172 return(look);
175 /* a truncated packet here just means 'stop working'; it's not an error */
176 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
177 ogg_int32_t **in,int ch,
178 long (*decodepart)(codebook *, ogg_int32_t *,
179 oggpack_buffer *,int,int)){
181 long i,j,k,l,s;
182 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
183 vorbis_info_residue0 *info=look->info;
185 /* move all this setup out later */
186 int samples_per_partition=info->grouping;
187 int partitions_per_word=look->phrasebook->dim;
188 int max=vb->pcmend>>1;
189 int end=(info->end<max?info->end:max);
190 int n=end-info->begin;
192 if(n>0){
193 int partvals=n/samples_per_partition;
194 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
195 int ***partword=(int ***)alloca(ch*sizeof(*partword));
197 for(j=0;j<ch;j++)
198 partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
200 for(s=0;s<look->stages;s++){
202 /* each loop decodes on partition codeword containing
203 partitions_pre_word partitions */
204 for(i=0,l=0;i<partvals;l++){
205 if(s==0){
206 /* fetch the partition word for each channel */
207 for(j=0;j<ch;j++){
208 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
209 if(temp==-1)goto eopbreak;
210 partword[j][l]=look->decodemap[temp];
211 if(partword[j][l]==NULL)goto errout;
215 /* now we decode residual values for the partitions */
216 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
217 for(j=0;j<ch;j++){
218 long offset=info->begin+i*samples_per_partition;
219 if(info->secondstages[partword[j][l][k]]&(1<<s)){
220 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
221 if(stagebook){
222 if(decodepart(stagebook,in[j]+offset,&vb->opb,
223 samples_per_partition,-8)==-1)goto eopbreak;
230 errout:
231 eopbreak:
232 return(0);
235 static int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
236 ogg_int32_t **in,int *nonzero,int ch){
237 int i,used=0;
238 for(i=0;i<ch;i++)
239 if(nonzero[i])
240 in[used++]=in[i];
241 if(used)
242 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
243 else
244 return(0);
247 static int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
248 ogg_int32_t **in,int *nonzero,int ch){
249 int i,used=0;
250 for(i=0;i<ch;i++)
251 if(nonzero[i])
252 in[used++]=in[i];
253 if(used)
254 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
255 else
256 return(0);
259 /* duplicate code here as speed is somewhat more important */
260 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
261 ogg_int32_t **in,int *nonzero,int ch)
262 ICODE_ATTR_TREMOR_NOT_MDCT;
263 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
264 ogg_int32_t **in,int *nonzero,int ch){
265 long i,k,l,s;
266 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
267 vorbis_info_residue0 *info=look->info;
269 /* move all this setup out later */
270 int samples_per_partition=info->grouping;
271 int partitions_per_word=look->phrasebook->dim;
272 int max=(vb->pcmend*ch)>>1;
273 int end=(info->end<max?info->end:max);
274 int n=end-info->begin;
276 if(n>0){
278 int partvals=n/samples_per_partition;
279 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
280 int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword));
281 int beginoff=info->begin/ch;
283 for(i=0;i<ch;i++)if(nonzero[i])break;
284 if(i==ch)return(0); /* no nonzero vectors */
286 samples_per_partition/=ch;
288 for(s=0;s<look->stages;s++){
289 for(i=0,l=0;i<partvals;l++){
291 if(s==0){
292 /* fetch the partition word */
293 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
294 if(temp==-1)goto eopbreak;
295 partword[l]=look->decodemap[temp];
296 if(partword[l]==NULL)goto errout;
299 /* now we decode residual values for the partitions */
300 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
301 if(info->secondstages[partword[l][k]]&(1<<s)){
302 codebook *stagebook=look->partbooks[partword[l][k]][s];
303 if(stagebook){
304 if(vorbis_book_decodevv_add(stagebook,in,
305 i*samples_per_partition+beginoff,ch,
306 &vb->opb,
307 samples_per_partition,-8)==-1)
308 goto eopbreak;
314 errout:
315 eopbreak:
316 return(0);
320 const vorbis_func_residue residue0_exportbundle ICONST_ATTR ={
321 &res0_unpack,
322 &res0_look,
323 &res0_free_info,
324 &res0_free_look,
325 &res0_inverse
328 const vorbis_func_residue residue1_exportbundle ICONST_ATTR ={
329 &res0_unpack,
330 &res0_look,
331 &res0_free_info,
332 &res0_free_look,
333 &res1_inverse
336 const vorbis_func_residue residue2_exportbundle ICONST_ATTR ={
337 &res0_unpack,
338 &res0_look,
339 &res0_free_info,
340 &res0_free_look,
341 &res2_inverse