1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
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. *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
12 ********************************************************************
14 function: residue backend 0, 1 and 2 implementation
16 ********************************************************************/
22 #include "ivorbiscodec.h"
23 #include "codec_internal.h"
31 vorbis_info_residue0
*info
;
38 codebook
***partbooks
;
43 } vorbis_look_residue0
;
45 void res0_free_info(vorbis_info_residue
*i
){
46 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)i
;
48 memset(info
,0,sizeof(*info
));
53 void res0_free_look(vorbis_look_residue
*i
){
57 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)i
;
59 for(j
=0;j
<look
->parts
;j
++)
60 if(look
->partbooks
[j
])_ogg_free(look
->partbooks
[j
]);
61 _ogg_free(look
->partbooks
);
62 for(j
=0;j
<look
->partvals
;j
++)
63 _ogg_free(look
->decodemap
[j
]);
64 _ogg_free(look
->decodemap
);
66 memset(look
,0,sizeof(*look
));
71 static int ilog(unsigned int v
){
80 static int icount(unsigned int v
){
89 /* vorbis_info is for range checking */
90 vorbis_info_residue
*res0_unpack(vorbis_info
*vi
,oggpack_buffer
*opb
){
92 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)_ogg_calloc(1,sizeof(*info
));
93 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
95 info
->begin
=oggpack_read(opb
,24);
96 info
->end
=oggpack_read(opb
,24);
97 info
->grouping
=oggpack_read(opb
,24)+1;
98 info
->partitions
=oggpack_read(opb
,6)+1;
99 info
->groupbook
=oggpack_read(opb
,8);
101 /* check for premature EOP */
102 if(info
->groupbook
<0)goto errout
;
104 for(j
=0;j
<info
->partitions
;j
++){
105 int cascade
=oggpack_read(opb
,3);
106 int cflag
=oggpack_read(opb
,1);
107 if(cflag
<0) goto errout
;
109 int c
=oggpack_read(opb
,5);
113 info
->secondstages
[j
]=cascade
;
115 acc
+=icount(cascade
);
118 int book
=oggpack_read(opb
,8);
119 if(book
<0) goto errout
;
120 info
->booklist
[j
]=book
;
123 if(info
->groupbook
>=ci
->books
)goto errout
;
125 if(info
->booklist
[j
]>=ci
->books
)goto errout
;
126 if(ci
->book_param
[info
->booklist
[j
]]->maptype
==0)goto errout
;
129 /* verify the phrasebook is not specifying an impossible or
130 inconsistent partitioning scheme. */
131 /* modify the phrasebook ranging check from r16327; an early beta
132 encoder had a bug where it used an oversized phrasebook by
133 accident. These files should continue to be playable, but don't
136 int entries
= ci
->book_param
[info
->groupbook
]->entries
;
137 int dim
= ci
->book_param
[info
->groupbook
]->dim
;
139 if (dim
<1) goto errout
;
141 partvals
*= info
->partitions
;
142 if(partvals
> entries
) goto errout
;
145 info
->partvals
= partvals
;
150 res0_free_info(info
);
154 vorbis_look_residue
*res0_look(vorbis_dsp_state
*vd
,vorbis_info_mode
*vm
,
155 vorbis_info_residue
*vr
){
156 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)vr
;
157 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)_ogg_calloc(1,sizeof(*look
));
158 codec_setup_info
*ci
=(codec_setup_info
*)vd
->vi
->codec_setup
;
164 look
->map
=vm
->mapping
;
166 look
->parts
=info
->partitions
;
167 look
->fullbooks
=ci
->fullbooks
;
168 look
->phrasebook
=ci
->fullbooks
+info
->groupbook
;
169 dim
=look
->phrasebook
->dim
;
171 look
->partbooks
=(codebook
***)_ogg_calloc(look
->parts
,sizeof(*look
->partbooks
));
173 for(j
=0;j
<look
->parts
;j
++){
174 int stages
=ilog(info
->secondstages
[j
]);
176 if(stages
>maxstage
)maxstage
=stages
;
177 look
->partbooks
[j
]=(codebook
**)_ogg_calloc(stages
,sizeof(*look
->partbooks
[j
]));
178 for(k
=0;k
<stages
;k
++)
179 if(info
->secondstages
[j
]&(1<<k
)){
180 look
->partbooks
[j
][k
]=ci
->fullbooks
+info
->booklist
[acc
++];
182 look
->training_data
[k
][j
]=calloc(look
->partbooks
[j
][k
]->entries
,
183 sizeof(***look
->training_data
));
189 look
->partvals
=look
->parts
;
190 for(j
=1;j
<dim
;j
++)look
->partvals
*=look
->parts
;
191 look
->stages
=maxstage
;
192 look
->decodemap
=(int **)_ogg_malloc(look
->partvals
*sizeof(*look
->decodemap
));
193 for(j
=0;j
<look
->partvals
;j
++){
195 long mult
=look
->partvals
/look
->parts
;
196 look
->decodemap
[j
]=(int *)_ogg_malloc(dim
*sizeof(*look
->decodemap
[j
]));
201 look
->decodemap
[j
][k
]=deco
;
209 /* a truncated packet here just means 'stop working'; it's not an error */
210 static int _01inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
211 ogg_int32_t
**in
,int ch
,
212 long (*decodepart
)(codebook
*, ogg_int32_t
*,
213 oggpack_buffer
*,int,int)){
216 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
217 vorbis_info_residue0
*info
=look
->info
;
219 /* move all this setup out later */
220 int samples_per_partition
=info
->grouping
;
221 int partitions_per_word
=look
->phrasebook
->dim
;
222 int max
=vb
->pcmend
>>1;
223 int end
=(info
->end
<max
?info
->end
:max
);
224 int n
=end
-info
->begin
;
227 int partvals
=n
/samples_per_partition
;
228 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
229 int ***partword
=(int ***)alloca(ch
*sizeof(*partword
));
232 partword
[j
]=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
[j
]));
234 for(s
=0;s
<look
->stages
;s
++){
236 /* each loop decodes on partition codeword containing
237 partitions_pre_word partitions */
238 for(i
=0,l
=0;i
<partvals
;l
++){
240 /* fetch the partition word for each channel */
242 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
243 if(temp
==-1 || temp
>=info
->partvals
)goto eopbreak
;
244 partword
[j
][l
]=look
->decodemap
[temp
];
245 if(partword
[j
][l
]==NULL
)goto errout
;
249 /* now we decode residual values for the partitions */
250 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
252 long offset
=info
->begin
+i
*samples_per_partition
;
253 if(info
->secondstages
[partword
[j
][l
][k
]]&(1<<s
)){
254 codebook
*stagebook
=look
->partbooks
[partword
[j
][l
][k
]][s
];
256 if(decodepart(stagebook
,in
[j
]+offset
,&vb
->opb
,
257 samples_per_partition
,-8)==-1)goto eopbreak
;
269 int res0_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
270 ogg_int32_t
**in
,int *nonzero
,int ch
){
276 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodevs_add
));
281 int res1_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
282 ogg_int32_t
**in
,int *nonzero
,int ch
){
288 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodev_add
));
293 /* duplicate code here as speed is somewhat more important */
294 int res2_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
295 ogg_int32_t
**in
,int *nonzero
,int ch
){
297 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
298 vorbis_info_residue0
*info
=look
->info
;
300 /* move all this setup out later */
301 int samples_per_partition
=info
->grouping
;
302 int partitions_per_word
=look
->phrasebook
->dim
;
303 int max
=(vb
->pcmend
*ch
)>>1;
304 int end
=(info
->end
<max
?info
->end
:max
);
305 int n
=end
-info
->begin
;
309 int partvals
=n
/samples_per_partition
;
310 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
311 int **partword
=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
));
312 int beginoff
=info
->begin
/ch
;
314 for(i
=0;i
<ch
;i
++)if(nonzero
[i
])break;
315 if(i
==ch
)return(0); /* no nonzero vectors */
317 samples_per_partition
/=ch
;
319 for(s
=0;s
<look
->stages
;s
++){
320 for(i
=0,l
=0;i
<partvals
;l
++){
323 /* fetch the partition word */
324 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
325 if(temp
==-1 || temp
>=info
->partvals
)goto eopbreak
;
326 partword
[l
]=look
->decodemap
[temp
];
327 if(partword
[l
]==NULL
)goto errout
;
330 /* now we decode residual values for the partitions */
331 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
332 if(info
->secondstages
[partword
[l
][k
]]&(1<<s
)){
333 codebook
*stagebook
=look
->partbooks
[partword
[l
][k
]][s
];
336 if(vorbis_book_decodevv_add(stagebook
,in
,
337 i
*samples_per_partition
+beginoff
,ch
,
339 samples_per_partition
,-8)==-1)
352 vorbis_func_residue residue0_exportbundle
={
360 vorbis_func_residue residue1_exportbundle
={
368 vorbis_func_residue residue2_exportbundle
={