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