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"
30 vorbis_info_residue0
*info
;
37 codebook
***partbooks
;
42 } vorbis_look_residue0
;
44 void res0_free_info(vorbis_info_residue
*i
){
45 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)i
;
47 memset(info
,0,sizeof(*info
));
52 void res0_free_look(vorbis_look_residue
*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
));
70 static int ilog(unsigned int v
){
79 static int icount(unsigned int v
){
88 /* vorbis_info is for range checking */
89 vorbis_info_residue
*res0_unpack(vorbis_info
*vi
,oggpack_buffer
*opb
){
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
);
109 info
->booklist
[j
]=oggpack_read(opb
,8);
111 if(info
->groupbook
>=ci
->books
)goto errout
;
113 if(info
->booklist
[j
]>=ci
->books
)goto errout
;
117 res0_free_info(info
);
121 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
;
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
]);
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
++];
149 look
->training_data
[k
][j
]=calloc(look
->partbooks
[j
][k
]->entries
,
150 sizeof(***look
->training_data
));
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
++){
162 long mult
=look
->partvals
/look
->parts
;
163 look
->decodemap
[j
]=(int *)_ogg_malloc(dim
*sizeof(*look
->decodemap
[j
]));
168 look
->decodemap
[j
][k
]=deco
;
176 /* a truncated packet here just means 'stop working'; it's not an error */
177 static int _01inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
178 ogg_int32_t
**in
,int ch
,
179 long (*decodepart
)(codebook
*, ogg_int32_t
*,
180 oggpack_buffer
*,int,int)){
183 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
184 vorbis_info_residue0
*info
=look
->info
;
186 /* move all this setup out later */
187 int samples_per_partition
=info
->grouping
;
188 int partitions_per_word
=look
->phrasebook
->dim
;
189 int n
=info
->end
-info
->begin
;
191 int partvals
=n
/samples_per_partition
;
192 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
193 int ***partword
=(int ***)alloca(ch
*sizeof(*partword
));
196 partword
[j
]=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
[j
]));
198 for(s
=0;s
<look
->stages
;s
++){
200 /* each loop decodes on partition codeword containing
201 partitions_pre_word partitions */
202 for(i
=0,l
=0;i
<partvals
;l
++){
204 /* fetch the partition word for each channel */
206 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
207 if(temp
==-1)goto eopbreak
;
208 partword
[j
][l
]=look
->decodemap
[temp
];
209 if(partword
[j
][l
]==NULL
)goto errout
;
213 /* now we decode residual values for the partitions */
214 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
216 long offset
=info
->begin
+i
*samples_per_partition
;
217 if(info
->secondstages
[partword
[j
][l
][k
]]&(1<<s
)){
218 codebook
*stagebook
=look
->partbooks
[partword
[j
][l
][k
]][s
];
220 if(decodepart(stagebook
,in
[j
]+offset
,&vb
->opb
,
221 samples_per_partition
,-8)==-1)goto eopbreak
;
233 int res0_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
234 ogg_int32_t
**in
,int *nonzero
,int ch
){
240 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodevs_add
));
245 int res1_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
246 ogg_int32_t
**in
,int *nonzero
,int ch
){
252 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodev_add
));
257 /* duplicate code here as speed is somewhat more important */
258 int res2_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
259 ogg_int32_t
**in
,int *nonzero
,int ch
){
261 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
262 vorbis_info_residue0
*info
=look
->info
;
264 /* move all this setup out later */
265 int samples_per_partition
=info
->grouping
;
266 int partitions_per_word
=look
->phrasebook
->dim
;
267 int n
=info
->end
-info
->begin
;
269 int partvals
=n
/samples_per_partition
;
270 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
271 int **partword
=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
));
272 int beginoff
=info
->begin
/ch
;
274 for(i
=0;i
<ch
;i
++)if(nonzero
[i
])break;
275 if(i
==ch
)return(0); /* no nonzero vectors */
277 samples_per_partition
/=ch
;
279 for(s
=0;s
<look
->stages
;s
++){
280 for(i
=0,l
=0;i
<partvals
;l
++){
283 /* fetch the partition word */
284 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
285 if(temp
==-1)goto eopbreak
;
286 partword
[l
]=look
->decodemap
[temp
];
287 if(partword
[l
]==NULL
)goto errout
;
290 /* now we decode residual values for the partitions */
291 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
292 if(info
->secondstages
[partword
[l
][k
]]&(1<<s
)){
293 codebook
*stagebook
=look
->partbooks
[partword
[l
][k
]][s
];
296 if(vorbis_book_decodevv_add(stagebook
,in
,
297 i
*samples_per_partition
+beginoff
,ch
,
299 samples_per_partition
,-8)==-1)
312 vorbis_func_residue residue0_exportbundle
={
320 vorbis_func_residue residue1_exportbundle
={
328 vorbis_func_residue residue2_exportbundle
={