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 static inline 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 /* bisect search for the codeword in the ordered list */
178 ogg_uint32_t testword
=bitreverse((ogg_uint32_t
)lok
);
182 long test
=book
->codelist
[lo
+p
]>testword
;
187 if(book
->dec_codelengths
[lo
]<=read
){
188 oggpack_adv(b
, book
->dec_codelengths
[lo
]);
193 oggpack_adv(b
, read
);
197 /* Decode side is specced and easier, because we don't need to find
198 matches using different criteria; we simply read and map. There are
199 two things we need to do 'depending':
201 We may need to support interleave. We don't really, but it's
202 convenient to do it here rather than rebuild the vector later.
204 Cascades may be additive or multiplicitive; this is not inherent in
205 the codebook, but set in the code using the codebook. Like
206 interleaving, it's easiest to do it here.
207 addmul==0 -> declarative (set the value)
208 addmul==1 -> additive
209 addmul==2 -> multiplicitive */
211 /* returns the [original, not compacted] entry number or -1 on eof *********/
212 long vorbis_book_decode(codebook
*book
, oggpack_buffer
*b
){
213 long packed_entry
=decode_packed_entry_number(book
,b
);
215 return(book
->dec_index
[packed_entry
]);
217 /* if there's no dec_index, the codebook unpacking isn't collapsed */
218 return(packed_entry
);
221 /* returns 0 on OK or -1 on eof *************************************/
222 long vorbis_book_decodevs_add(codebook
*book
,ogg_int32_t
*a
,
223 oggpack_buffer
*b
,int n
,int point
){
224 int step
=n
/book
->dim
;
225 long *entry
= (long *)alloca(sizeof(*entry
)*step
);
226 ogg_int32_t
**t
= (ogg_int32_t
**)alloca(sizeof(*t
)*step
);
228 int shift
=point
-book
->binarypoint
;
231 for (i
= 0; i
< step
; i
++) {
232 entry
[i
]=decode_packed_entry_number(book
,b
);
233 if(entry
[i
]==-1)return(-1);
234 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
236 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
238 a
[o
+j
]+=t
[j
][i
]>>shift
;
240 for (i
= 0; i
< step
; i
++) {
241 entry
[i
]=decode_packed_entry_number(book
,b
);
242 if(entry
[i
]==-1)return(-1);
243 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
245 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
247 a
[o
+j
]+=t
[j
][i
]<<-shift
;
252 long vorbis_book_decodev_add(codebook
*book
,ogg_int32_t
*a
,
253 oggpack_buffer
*b
,int n
,int point
){
256 int shift
=point
-book
->binarypoint
;
260 entry
= decode_packed_entry_number(book
,b
);
261 if(entry
==-1)return(-1);
262 t
= book
->valuelist
+entry
*book
->dim
;
263 for (j
=0;j
<book
->dim
;)
264 a
[i
++]+=t
[j
++]>>shift
;
268 entry
= decode_packed_entry_number(book
,b
);
269 if(entry
==-1)return(-1);
270 t
= book
->valuelist
+entry
*book
->dim
;
271 for (j
=0;j
<book
->dim
;)
272 a
[i
++]+=t
[j
++]<<-shift
;
278 long vorbis_book_decodev_set(codebook
*book
,ogg_int32_t
*a
,
279 oggpack_buffer
*b
,int n
,int point
){
282 int shift
=point
-book
->binarypoint
;
287 entry
= decode_packed_entry_number(book
,b
);
288 if(entry
==-1)return(-1);
289 t
= book
->valuelist
+entry
*book
->dim
;
290 for (j
=0;j
<book
->dim
;){
291 a
[i
++]=t
[j
++]>>shift
;
297 entry
= decode_packed_entry_number(book
,b
);
298 if(entry
==-1)return(-1);
299 t
= book
->valuelist
+entry
*book
->dim
;
300 for (j
=0;j
<book
->dim
;){
301 a
[i
++]=t
[j
++]<<-shift
;
308 long vorbis_book_decodevv_add(codebook
*book
,ogg_int32_t
**a
,\
310 oggpack_buffer
*b
,int n
,int point
){
313 int shift
=point
-book
->binarypoint
;
317 for(i
=offset
;i
<offset
+n
;){
318 entry
= decode_packed_entry_number(book
,b
);
319 if(entry
==-1)return(-1);
321 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
322 for (j
=0;j
<book
->dim
;j
++){
323 a
[chptr
++][i
]+=t
[j
]>>shift
;
333 for(i
=offset
;i
<offset
+n
;){
334 entry
= decode_packed_entry_number(book
,b
);
335 if(entry
==-1)return(-1);
337 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
338 for (j
=0;j
<book
->dim
;j
++){
339 a
[chptr
++][i
]+=t
[j
]<<-shift
;