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: basic codebook pack/unpack/code/decode operations
16 ********************************************************************/
22 #include "ivorbiscodec.h"
26 /* unpacks a codebook from the packet buffer into the codebook struct,
27 readies the codebook auxiliary structures for decode *************/
28 int vorbis_staticbook_unpack(oggpack_buffer
*opb
,static_codebook
*s
){
30 memset(s
,0,sizeof(*s
));
32 /* make sure alignment is correct */
33 if(oggpack_read(opb
,24)!=0x564342)goto _eofout
;
35 /* first the basic parameters */
36 s
->dim
=oggpack_read(opb
,16);
37 s
->entries
=oggpack_read(opb
,24);
38 if(s
->entries
==-1)goto _eofout
;
40 /* codeword ordering.... length ordered or unordered? */
41 switch((int)oggpack_read(opb
,1)){
44 s
->lengthlist
=(long *)_ogg_malloc(sizeof(*s
->lengthlist
)*s
->entries
);
46 /* allocated but unused entries? */
47 if(oggpack_read(opb
,1)){
48 /* yes, unused entries */
50 for(i
=0;i
<s
->entries
;i
++){
51 if(oggpack_read(opb
,1)){
52 long num
=oggpack_read(opb
,5);
53 if(num
==-1)goto _eofout
;
54 s
->lengthlist
[i
]=num
+1;
59 /* all entries used; no tagging */
60 for(i
=0;i
<s
->entries
;i
++){
61 long num
=oggpack_read(opb
,5);
62 if(num
==-1)goto _eofout
;
63 s
->lengthlist
[i
]=num
+1;
71 long length
=oggpack_read(opb
,5)+1;
72 s
->lengthlist
=(long *)_ogg_malloc(sizeof(*s
->lengthlist
)*s
->entries
);
74 for(i
=0;i
<s
->entries
;){
75 long num
=oggpack_read(opb
,_ilog(s
->entries
-i
));
76 if(num
==-1)goto _eofout
;
77 for(j
=0;j
<num
&& i
<s
->entries
;j
++,i
++)
78 s
->lengthlist
[i
]=length
;
88 /* Do we have a mapping to unpack? */
89 switch((s
->maptype
=oggpack_read(opb
,4))){
94 /* implicitly populated value mapping */
95 /* explicitly populated value mapping */
97 s
->q_min
=oggpack_read(opb
,32);
98 s
->q_delta
=oggpack_read(opb
,32);
99 s
->q_quant
=oggpack_read(opb
,4)+1;
100 s
->q_sequencep
=oggpack_read(opb
,1);
106 quantvals
=_book_maptype1_quantvals(s
);
109 quantvals
=s
->entries
*s
->dim
;
113 /* quantized values */
114 s
->quantlist
=(long *)_ogg_malloc(sizeof(*s
->quantlist
)*quantvals
);
115 for(i
=0;i
<quantvals
;i
++)
116 s
->quantlist
[i
]=oggpack_read(opb
,s
->q_quant
);
118 if(quantvals
&&s
->quantlist
[quantvals
-1]==-1)goto _eofout
;
130 vorbis_staticbook_clear(s
);
134 /* the 'eliminate the decode tree' optimization actually requires the
135 codewords to be MSb first, not LSb. This is an annoying inelegancy
136 (and one of the first places where carefully thought out design
137 turned out to be wrong; Vorbis II and future Ogg codecs should go
138 to an MSb bitpacker), but not actually the huge hit it appears to
139 be. The first-stage decode table catches most words so that
140 bitreverse is not in the main execution path. */
142 static ogg_uint32_t
bitreverse(ogg_uint32_t x
){
143 x
= ((x
>>16)&0x0000ffff) | ((x
<<16)&0xffff0000);
144 x
= ((x
>> 8)&0x00ff00ff) | ((x
<< 8)&0xff00ff00);
145 x
= ((x
>> 4)&0x0f0f0f0f) | ((x
<< 4)&0xf0f0f0f0);
146 x
= ((x
>> 2)&0x33333333) | ((x
<< 2)&0xcccccccc);
147 return((x
>> 1)&0x55555555) | ((x
<< 1)&0xaaaaaaaa);
150 STIN
long decode_packed_entry_number(codebook
*book
,
152 int read
=book
->dec_maxlength
;
154 long lok
= oggpack_look(b
,book
->dec_firsttablen
);
157 long entry
= book
->dec_firsttable
[lok
];
158 if(entry
&0x80000000UL
){
159 lo
=(entry
>>15)&0x7fff;
160 hi
=book
->used_entries
-(entry
&0x7fff);
162 oggpack_adv(b
, book
->dec_codelengths
[entry
-1]);
167 hi
=book
->used_entries
;
170 lok
= oggpack_look(b
, read
);
172 while(lok
<0 && read
>1)
173 lok
= oggpack_look(b
, --read
);
176 oggpack_adv(b
,1); /* force eop */
180 /* bisect search for the codeword in the ordered list */
182 ogg_uint32_t testword
=bitreverse((ogg_uint32_t
)lok
);
186 long test
=book
->codelist
[lo
+p
]>testword
;
191 if(book
->dec_codelengths
[lo
]<=read
){
192 oggpack_adv(b
, book
->dec_codelengths
[lo
]);
197 oggpack_adv(b
, read
+1);
201 /* Decode side is specced and easier, because we don't need to find
202 matches using different criteria; we simply read and map. There are
203 two things we need to do 'depending':
205 We may need to support interleave. We don't really, but it's
206 convenient to do it here rather than rebuild the vector later.
208 Cascades may be additive or multiplicitive; this is not inherent in
209 the codebook, but set in the code using the codebook. Like
210 interleaving, it's easiest to do it here.
211 addmul==0 -> declarative (set the value)
212 addmul==1 -> additive
213 addmul==2 -> multiplicitive */
215 /* returns the [original, not compacted] entry number or -1 on eof *********/
216 long vorbis_book_decode(codebook
*book
, oggpack_buffer
*b
){
217 if(book
->used_entries
>0){
218 long packed_entry
=decode_packed_entry_number(book
,b
);
220 return(book
->dec_index
[packed_entry
]);
223 /* if there's no dec_index, the codebook unpacking isn't collapsed */
227 /* returns 0 on OK or -1 on eof *************************************/
228 long vorbis_book_decodevs_add(codebook
*book
,ogg_int32_t
*a
,
229 oggpack_buffer
*b
,int n
,int point
){
230 if(book
->used_entries
>0){
231 int step
=n
/book
->dim
;
232 long *entry
= (long *)alloca(sizeof(*entry
)*step
);
233 ogg_int32_t
**t
= (ogg_int32_t
**)alloca(sizeof(*t
)*step
);
235 int shift
=point
-book
->binarypoint
;
238 for (i
= 0; i
< step
; i
++) {
239 entry
[i
]=decode_packed_entry_number(book
,b
);
240 if(entry
[i
]==-1)return(-1);
241 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
243 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
245 a
[o
+j
]+=t
[j
][i
]>>shift
;
247 for (i
= 0; i
< step
; i
++) {
248 entry
[i
]=decode_packed_entry_number(book
,b
);
249 if(entry
[i
]==-1)return(-1);
250 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
252 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
254 a
[o
+j
]+=t
[j
][i
]<<-shift
;
260 long vorbis_book_decodev_add(codebook
*book
,ogg_int32_t
*a
,
261 oggpack_buffer
*b
,int n
,int point
){
262 if(book
->used_entries
>0){
265 int shift
=point
-book
->binarypoint
;
269 entry
= decode_packed_entry_number(book
,b
);
270 if(entry
==-1)return(-1);
271 t
= book
->valuelist
+entry
*book
->dim
;
272 for (j
=0;j
<book
->dim
;)
273 a
[i
++]+=t
[j
++]>>shift
;
277 entry
= decode_packed_entry_number(book
,b
);
278 if(entry
==-1)return(-1);
279 t
= book
->valuelist
+entry
*book
->dim
;
280 for (j
=0;j
<book
->dim
;)
281 a
[i
++]+=t
[j
++]<<-shift
;
288 long vorbis_book_decodev_set(codebook
*book
,ogg_int32_t
*a
,
289 oggpack_buffer
*b
,int n
,int point
){
290 if(book
->used_entries
>0){
293 int shift
=point
-book
->binarypoint
;
298 entry
= decode_packed_entry_number(book
,b
);
299 if(entry
==-1)return(-1);
300 t
= book
->valuelist
+entry
*book
->dim
;
301 for (j
=0;j
<book
->dim
;){
302 a
[i
++]=t
[j
++]>>shift
;
308 entry
= decode_packed_entry_number(book
,b
);
309 if(entry
==-1)return(-1);
310 t
= book
->valuelist
+entry
*book
->dim
;
311 for (j
=0;j
<book
->dim
;){
312 a
[i
++]=t
[j
++]<<-shift
;
320 for (j
=0;j
<book
->dim
;){
328 long vorbis_book_decodevv_add(codebook
*book
,ogg_int32_t
**a
,\
330 oggpack_buffer
*b
,int n
,int point
){
331 if(book
->used_entries
>0){
334 int shift
=point
-book
->binarypoint
;
338 for(i
=offset
;i
<offset
+n
;){
339 entry
= decode_packed_entry_number(book
,b
);
340 if(entry
==-1)return(-1);
342 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
343 for (j
=0;j
<book
->dim
;j
++){
344 a
[chptr
++][i
]+=t
[j
]>>shift
;
354 for(i
=offset
;i
<offset
+n
;){
355 entry
= decode_packed_entry_number(book
,b
);
356 if(entry
==-1)return(-1);
358 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
359 for (j
=0;j
<book
->dim
;j
++){
360 a
[chptr
++][i
]+=t
[j
]<<-shift
;