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 ********************************************************************
13
14 function: residue backend 0, 1 and 2 implementation
15
16 ********************************************************************/
17
18 #include <stdlib.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"
28 #include "block.h"
29
30 typedef struct {
31 vorbis_info_residue0 *info;
32 int map;
33
34 int parts;
35 int stages;
36 codebook *fullbooks;
37 codebook *phrasebook;
38 codebook ***partbooks;
39
40 int partvals;
41 int **decodemap;
42
43 } vorbis_look_residue0;
44
res0_free_info(vorbis_info_residue * i)45 void res0_free_info(vorbis_info_residue *i){
46 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
47 if(info){
48 memset(info,0,sizeof(*info));
49 free(info);
50 }
51 }
52
res0_free_look(vorbis_look_residue * i)53 void res0_free_look(vorbis_look_residue *i){
54 int j;
55 if(i){
56
57 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
58
59 for(j=0;j<look->parts;j++)
60 if(look->partbooks[j])
61 free(look->partbooks[j]);
62 free(look->partbooks);
63 for(j=0;j<look->partvals;j++)
64 free(look->decodemap[j]);
65 free(look->decodemap);
66
67 memset(look,0,sizeof(*look));
68 free(look);
69 }
70 }
71
ilog(unsigned int v)72 static int ilog(unsigned int v){
73 int ret=0;
74 while(v){
75 ret++;
76 v>>=1;
77 }
78 return(ret);
79 }
80
icount(unsigned int v)81 static int icount(unsigned int v){
82 int ret=0;
83 while(v){
84 ret+=v&1;
85 v>>=1;
86 }
87 return(ret);
88 }
89
90 /* vorbis_info is for range checking */
res0_unpack(vorbis_info * vi,oggpack_buffer * opb)91 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
92 int j,acc=0;
93 vorbis_info_residue0 *info=(vorbis_info_residue0 *)calloc(1,sizeof(*info));
94 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
95
96 info->begin=oggpack_read(opb,24);
97 info->end=oggpack_read(opb,24);
98 info->grouping=oggpack_read(opb,24)+1;
99 info->partitions=oggpack_read(opb,6)+1;
100 info->groupbook=oggpack_read(opb,8);
101
102 /* check for premature EOP */
103 if(info->groupbook<0)goto errout;
104
105 for(j=0;j<info->partitions;j++){
106 int cascade=oggpack_read(opb,3);
107 int cflag=oggpack_read(opb,1);
108 if(cflag<0) goto errout;
109 if(cflag){
110 int c=oggpack_read(opb,5);
111 if(c<0) goto errout;
112 cascade|=(c<<3);
113 }
114 info->secondstages[j]=cascade;
115
116 acc+=icount(cascade);
117 }
118 for(j=0;j<acc;j++){
119 int book=oggpack_read(opb,8);
120 if(book<0) goto errout;
121 info->booklist[j]=book;
122 }
123
124 if(info->groupbook>=ci->books)goto errout;
125 for(j=0;j<acc;j++){
126 if(info->booklist[j]>=ci->books)goto errout;
127 if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
128 }
129
130 /* verify the phrasebook is not specifying an impossible or
131 inconsistent partitioning scheme. */
132 /* modify the phrasebook ranging check from r16327; an early beta
133 encoder had a bug where it used an oversized phrasebook by
134 accident. These files should continue to be playable, but don't
135 allow an exploit */
136 {
137 int entries = ci->book_param[info->groupbook]->entries;
138 int dim = ci->book_param[info->groupbook]->dim;
139 int partvals = 1;
140 if (dim<1) goto errout;
141 while(dim>0){
142 partvals *= info->partitions;
143 if(partvals > entries) goto errout;
144 dim--;
145 }
146 info->partvals = partvals;
147 }
148
149 return(info);
150 errout:
151 res0_free_info(info);
152 return(NULL);
153 }
154
res0_look(vorbis_dsp_state * vd,vorbis_info_mode * vm,vorbis_info_residue * vr)155 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
156 vorbis_info_residue *vr){
157 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
158 vorbis_look_residue0 *look=(vorbis_look_residue0 *)calloc(1,sizeof(*look));
159 codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
160
161 int j,k,acc=0;
162 int dim;
163 int maxstage=0;
164 look->info=info;
165 look->map=vm->mapping;
166
167 look->parts=info->partitions;
168 look->fullbooks=ci->fullbooks;
169 look->phrasebook=ci->fullbooks+info->groupbook;
170 dim=look->phrasebook->dim;
171
172 look->partbooks=(codebook ***)calloc(look->parts,sizeof(*look->partbooks));
173
174 for(j=0;j<look->parts;j++){
175 int stages=ilog(info->secondstages[j]);
176 if(stages){
177 if(stages>maxstage)maxstage=stages;
178 look->partbooks[j]=(codebook **)calloc(stages,sizeof(*look->partbooks[j]));
179 for(k=0;k<stages;k++)
180 if(info->secondstages[j]&(1<<k)){
181 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
182 #ifdef TRAIN_RES
183 look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
184 sizeof(***look->training_data));
185 #endif
186 }
187 }
188 }
189
190 look->partvals=look->parts;
191 for(j=1;j<dim;j++)look->partvals*=look->parts;
192 look->stages=maxstage;
193 look->decodemap=(int **)malloc(look->partvals*sizeof(*look->decodemap));
194 for(j=0;j<look->partvals;j++){
195 long val=j;
196 long mult=look->partvals/look->parts;
197 look->decodemap[j]=(int *)malloc(dim*sizeof(*look->decodemap[j]));
198 for(k=0;k<dim;k++){
199 long deco=val/mult;
200 val-=deco*mult;
201 mult/=look->parts;
202 look->decodemap[j][k]=deco;
203 }
204 }
205
206 return(look);
207 }
208
209
210 /* a truncated packet here just means 'stop working'; it's not an error */
_01inverse(vorbis_block * vb,vorbis_look_residue * vl,int32_t ** in,int ch,long (* decodepart)(codebook *,int32_t *,oggpack_buffer *,int,int))211 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
212 int32_t **in,int ch,
213 long (*decodepart)(codebook *, int32_t *,
214 oggpack_buffer *,int,int)){
215
216 long i,j,k,l,s;
217 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
218 vorbis_info_residue0 *info=look->info;
219
220 /* move all this setup out later */
221 int samples_per_partition=info->grouping;
222 int partitions_per_word=look->phrasebook->dim;
223 int max=vb->pcmend>>1;
224 int end=(info->end<max?info->end:max);
225 int n=end-info->begin;
226
227 if(n>0){
228 int partvals=n/samples_per_partition;
229 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
230 int ***partword=(int ***)alloca(ch*sizeof(*partword));
231
232 for(j=0;j<ch;j++)
233 partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
234
235 for(s=0;s<look->stages;s++){
236
237 /* each loop decodes on partition codeword containing
238 partitions_pre_word partitions */
239 for(i=0,l=0;i<partvals;l++){
240 if(s==0){
241 /* fetch the partition word for each channel */
242 for(j=0;j<ch;j++){
243 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
244 if(temp==-1 || temp>=info->partvals)goto eopbreak;
245 partword[j][l]=look->decodemap[temp];
246 if(partword[j][l]==NULL)goto errout;
247 }
248 }
249
250 /* now we decode residual values for the partitions */
251 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
252 for(j=0;j<ch;j++){
253 long offset=info->begin+i*samples_per_partition;
254 if(info->secondstages[partword[j][l][k]]&(1<<s)){
255 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
256 if(stagebook){
257 if(decodepart(stagebook,in[j]+offset,&vb->opb,
258 samples_per_partition,-8)==-1)goto eopbreak;
259 }
260 }
261 }
262 }
263 }
264 }
265 errout:
266 eopbreak:
267 return(0);
268 }
269
res0_inverse(vorbis_block * vb,vorbis_look_residue * vl,int32_t ** in,int * nonzero,int ch)270 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
271 int32_t **in,int *nonzero,int ch){
272 int i,used=0;
273 for(i=0;i<ch;i++)
274 if(nonzero[i])
275 in[used++]=in[i];
276 if(used)
277 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
278 else
279 return(0);
280 }
281
res1_inverse(vorbis_block * vb,vorbis_look_residue * vl,int32_t ** in,int * nonzero,int ch)282 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
283 int32_t **in,int *nonzero,int ch){
284 int i,used=0;
285 for(i=0;i<ch;i++)
286 if(nonzero[i])
287 in[used++]=in[i];
288 if(used)
289 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
290 else
291 return(0);
292 }
293
294 /* duplicate code here as speed is somewhat more important */
res2_inverse(vorbis_block * vb,vorbis_look_residue * vl,int32_t ** in,int * nonzero,int ch)295 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
296 int32_t **in,int *nonzero,int ch){
297 long i,k,l,s;
298 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
299 vorbis_info_residue0 *info=look->info;
300
301 /* move all this setup out later */
302 int samples_per_partition=info->grouping;
303 int partitions_per_word=look->phrasebook->dim;
304 int max=(vb->pcmend*ch)>>1;
305 int end=(info->end<max?info->end:max);
306 int n=end-info->begin;
307
308 if(n>0){
309
310 int partvals=n/samples_per_partition;
311 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
312 int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword));
313 int beginoff=info->begin/ch;
314
315 for(i=0;i<ch;i++)if(nonzero[i])break;
316 if(i==ch)return(0); /* no nonzero vectors */
317
318 samples_per_partition/=ch;
319
320 for(s=0;s<look->stages;s++){
321 for(i=0,l=0;i<partvals;l++){
322
323 if(s==0){
324 /* fetch the partition word */
325 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
326 if(temp==-1 || temp>=info->partvals)goto eopbreak;
327 partword[l]=look->decodemap[temp];
328 if(partword[l]==NULL)goto errout;
329 }
330
331 /* now we decode residual values for the partitions */
332 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
333 if(info->secondstages[partword[l][k]]&(1<<s)){
334 codebook *stagebook=look->partbooks[partword[l][k]][s];
335
336 if(stagebook){
337 if(vorbis_book_decodevv_add(stagebook,in,
338 i*samples_per_partition+beginoff,ch,
339 &vb->opb,
340 samples_per_partition,-8)==-1)
341 goto eopbreak;
342 }
343 }
344 }
345 }
346 }
347 errout:
348 eopbreak:
349 return(0);
350 }
351
352
353 vorbis_func_residue residue0_exportbundle={
354 &res0_unpack,
355 &res0_look,
356 &res0_free_info,
357 &res0_free_look,
358 &res0_inverse
359 };
360
361 vorbis_func_residue residue1_exportbundle={
362 &res0_unpack,
363 &res0_look,
364 &res0_free_info,
365 &res0_free_look,
366 &res1_inverse
367 };
368
369 vorbis_func_residue residue2_exportbundle={
370 &res0_unpack,
371 &res0_look,
372 &res0_free_info,
373 &res0_free_look,
374 &res2_inverse
375 };
376