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 for(j
=0;j
<info
->partitions
;j
++){
102 int cascade
=oggpack_read(opb
,3);
103 if(oggpack_read(opb
,1))
104 cascade
|=(oggpack_read(opb
,5)<<3);
105 info
->secondstages
[j
]=cascade
;
107 acc
+=icount(cascade
);
110 info
->booklist
[j
]=oggpack_read(opb
,8);
112 if(info
->groupbook
>=ci
->books
)goto errout
;
114 if(info
->booklist
[j
]>=ci
->books
)goto errout
;
118 res0_free_info(info
);
122 vorbis_look_residue
*res0_look(vorbis_dsp_state
*vd
,vorbis_info_mode
*vm
,
123 vorbis_info_residue
*vr
){
124 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)vr
;
125 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)_ogg_calloc(1,sizeof(*look
));
126 codec_setup_info
*ci
=(codec_setup_info
*)vd
->vi
->codec_setup
;
132 look
->map
=vm
->mapping
;
134 look
->parts
=info
->partitions
;
135 look
->fullbooks
=ci
->fullbooks
;
136 look
->phrasebook
=ci
->fullbooks
+info
->groupbook
;
137 dim
=look
->phrasebook
->dim
;
139 look
->partbooks
=(codebook
***)_ogg_calloc(look
->parts
,sizeof(*look
->partbooks
));
141 for(j
=0;j
<look
->parts
;j
++){
142 int stages
=ilog(info
->secondstages
[j
]);
144 if(stages
>maxstage
)maxstage
=stages
;
145 look
->partbooks
[j
]=(codebook
**)_ogg_calloc(stages
,sizeof(*look
->partbooks
[j
]));
146 for(k
=0;k
<stages
;k
++)
147 if(info
->secondstages
[j
]&(1<<k
)){
148 look
->partbooks
[j
][k
]=ci
->fullbooks
+info
->booklist
[acc
++];
150 look
->training_data
[k
][j
]=calloc(look
->partbooks
[j
][k
]->entries
,
151 sizeof(***look
->training_data
));
157 look
->partvals
=look
->parts
;
158 for(j
=1;j
<dim
;j
++)look
->partvals
*=look
->parts
;
159 look
->stages
=maxstage
;
160 look
->decodemap
=(int **)_ogg_malloc(look
->partvals
*sizeof(*look
->decodemap
));
161 for(j
=0;j
<look
->partvals
;j
++){
163 long mult
=look
->partvals
/look
->parts
;
164 look
->decodemap
[j
]=(int *)_ogg_malloc(dim
*sizeof(*look
->decodemap
[j
]));
169 look
->decodemap
[j
][k
]=deco
;
177 /* a truncated packet here just means 'stop working'; it's not an error */
178 static int _01inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
179 ogg_int32_t
**in
,int ch
,
180 long (*decodepart
)(codebook
*, ogg_int32_t
*,
181 oggpack_buffer
*,int,int)){
184 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
185 vorbis_info_residue0
*info
=look
->info
;
187 /* move all this setup out later */
188 int samples_per_partition
=info
->grouping
;
189 int partitions_per_word
=look
->phrasebook
->dim
;
190 int max
=vb
->pcmend
>>1;
191 int end
=(info
->end
<max
?info
->end
:max
);
192 int n
=end
-info
->begin
;
195 int partvals
=n
/samples_per_partition
;
196 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
197 int ***partword
=(int ***)alloca(ch
*sizeof(*partword
));
200 partword
[j
]=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
[j
]));
202 for(s
=0;s
<look
->stages
;s
++){
204 /* each loop decodes on partition codeword containing
205 partitions_pre_word partitions */
206 for(i
=0,l
=0;i
<partvals
;l
++){
208 /* fetch the partition word for each channel */
210 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
211 if(temp
==-1)goto eopbreak
;
212 partword
[j
][l
]=look
->decodemap
[temp
];
213 if(partword
[j
][l
]==NULL
)goto errout
;
217 /* now we decode residual values for the partitions */
218 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
220 long offset
=info
->begin
+i
*samples_per_partition
;
221 if(info
->secondstages
[partword
[j
][l
][k
]]&(1<<s
)){
222 codebook
*stagebook
=look
->partbooks
[partword
[j
][l
][k
]][s
];
224 if(decodepart(stagebook
,in
[j
]+offset
,&vb
->opb
,
225 samples_per_partition
,-8)==-1)goto eopbreak
;
237 int res0_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
238 ogg_int32_t
**in
,int *nonzero
,int ch
){
244 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodevs_add
));
249 int res1_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
250 ogg_int32_t
**in
,int *nonzero
,int ch
){
256 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodev_add
));
261 /* duplicate code here as speed is somewhat more important */
262 int res2_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
263 ogg_int32_t
**in
,int *nonzero
,int ch
){
265 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
266 vorbis_info_residue0
*info
=look
->info
;
268 /* move all this setup out later */
269 int samples_per_partition
=info
->grouping
;
270 int partitions_per_word
=look
->phrasebook
->dim
;
271 int max
=(vb
->pcmend
*ch
)>>1;
272 int end
=(info
->end
<max
?info
->end
:max
);
273 int n
=end
-info
->begin
;
277 int partvals
=n
/samples_per_partition
;
278 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
279 int **partword
=(int **)_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
));
280 int beginoff
=info
->begin
/ch
;
282 for(i
=0;i
<ch
;i
++)if(nonzero
[i
])break;
283 if(i
==ch
)return(0); /* no nonzero vectors */
285 samples_per_partition
/=ch
;
287 for(s
=0;s
<look
->stages
;s
++){
288 for(i
=0,l
=0;i
<partvals
;l
++){
291 /* fetch the partition word */
292 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
293 if(temp
==-1)goto eopbreak
;
294 partword
[l
]=look
->decodemap
[temp
];
295 if(partword
[l
]==NULL
)goto errout
;
298 /* now we decode residual values for the partitions */
299 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
300 if(info
->secondstages
[partword
[l
][k
]]&(1<<s
)){
301 codebook
*stagebook
=look
->partbooks
[partword
[l
][k
]][s
];
304 if(vorbis_book_decodevv_add(stagebook
,in
,
305 i
*samples_per_partition
+beginoff
,ch
,
307 samples_per_partition
,-8)==-1)
320 vorbis_func_residue residue0_exportbundle
={
328 vorbis_func_residue residue1_exportbundle
={
336 vorbis_func_residue residue2_exportbundle
={