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 ********************************************************************/
18 #include "config-tremor.h"
22 #include "ivorbiscodec.h"
27 /* unpacks a codebook from the packet buffer into the codebook struct,
28 readies the codebook auxiliary structures for decode *************/
29 int vorbis_staticbook_unpack(oggpack_buffer
*opb
,static_codebook
*s
){
31 memset(s
,0,sizeof(*s
));
33 /* make sure alignment is correct */
34 if(oggpack_read(opb
,24)!=0x564342)goto _eofout
;
36 /* first the basic parameters */
37 s
->dim
=oggpack_read(opb
,16);
38 s
->entries
=oggpack_read(opb
,24);
39 if(s
->entries
==-1)goto _eofout
;
41 /* codeword ordering.... length ordered or unordered? */
42 switch((int)oggpack_read(opb
,1)){
45 s
->lengthlist
=(long *)_ogg_malloc(sizeof(*s
->lengthlist
)*s
->entries
);
47 /* allocated but unused entries? */
48 if(oggpack_read(opb
,1)){
49 /* yes, unused entries */
51 for(i
=0;i
<s
->entries
;i
++){
52 if(oggpack_read(opb
,1)){
53 long num
=oggpack_read(opb
,5);
54 if(num
==-1)goto _eofout
;
55 s
->lengthlist
[i
]=num
+1;
60 /* all entries used; no tagging */
61 for(i
=0;i
<s
->entries
;i
++){
62 long num
=oggpack_read(opb
,5);
63 if(num
==-1)goto _eofout
;
64 s
->lengthlist
[i
]=num
+1;
72 long length
=oggpack_read(opb
,5)+1;
73 s
->lengthlist
=(long *)_ogg_malloc(sizeof(*s
->lengthlist
)*s
->entries
);
75 for(i
=0;i
<s
->entries
;){
76 long num
=oggpack_read(opb
,_ilog(s
->entries
-i
));
77 if(num
==-1)goto _eofout
;
78 for(j
=0;j
<num
&& i
<s
->entries
;j
++,i
++)
79 s
->lengthlist
[i
]=length
;
89 /* Do we have a mapping to unpack? */
90 switch((s
->maptype
=oggpack_read(opb
,4))){
95 /* implicitly populated value mapping */
96 /* explicitly populated value mapping */
98 s
->q_min
=oggpack_read(opb
,32);
99 s
->q_delta
=oggpack_read(opb
,32);
100 s
->q_quant
=oggpack_read(opb
,4)+1;
101 s
->q_sequencep
=oggpack_read(opb
,1);
107 quantvals
=_book_maptype1_quantvals(s
);
110 quantvals
=s
->entries
*s
->dim
;
114 /* quantized values */
115 s
->quantlist
=(long *)_ogg_malloc(sizeof(*s
->quantlist
)*quantvals
);
116 for(i
=0;i
<quantvals
;i
++)
117 s
->quantlist
[i
]=oggpack_read(opb
,s
->q_quant
);
119 if(quantvals
&&s
->quantlist
[quantvals
-1]==-1)goto _eofout
;
131 vorbis_staticbook_clear(s
);
135 /* the 'eliminate the decode tree' optimization actually requires the
136 codewords to be MSb first, not LSb. This is an annoying inelegancy
137 (and one of the first places where carefully thought out design
138 turned out to be wrong; Vorbis II and future Ogg codecs should go
139 to an MSb bitpacker), but not actually the huge hit it appears to
140 be. The first-stage decode table catches most words so that
141 bitreverse is not in the main execution path. */
143 static inline ogg_uint32_t
bitreverse(register ogg_uint32_t x
){
144 x
= ((x
>>16)&0x0000ffff) | ((x
<<16)&0xffff0000);
145 x
= ((x
>> 8)&0x00ff00ff) | ((x
<< 8)&0xff00ff00);
146 x
= ((x
>> 4)&0x0f0f0f0f) | ((x
<< 4)&0xf0f0f0f0);
147 x
= ((x
>> 2)&0x33333333) | ((x
<< 2)&0xcccccccc);
148 return((x
>> 1)&0x55555555) | ((x
<< 1)&0xaaaaaaaa);
151 static inline long decode_packed_entry_number(codebook
*book
,
153 int read
=book
->dec_maxlength
;
155 long lok
= oggpack_look(b
,book
->dec_firsttablen
);
158 long entry
= book
->dec_firsttable
[lok
];
159 if(entry
&0x80000000UL
){
160 lo
=(entry
>>15)&0x7fff;
161 hi
=book
->used_entries
-(entry
&0x7fff);
163 oggpack_adv(b
, book
->dec_codelengths
[entry
-1]);
168 hi
=book
->used_entries
;
171 lok
= oggpack_look(b
, read
);
173 while(lok
<0 && read
>1)
174 lok
= oggpack_look(b
, --read
);
177 /* bisect search for the codeword in the ordered list */
179 ogg_uint32_t testword
=bitreverse((ogg_uint32_t
)lok
);
183 long test
=book
->codelist
[lo
+p
]>testword
;
188 if(book
->dec_codelengths
[lo
]<=read
){
189 oggpack_adv(b
, book
->dec_codelengths
[lo
]);
194 oggpack_adv(b
, read
);
198 /* Decode side is specced and easier, because we don't need to find
199 matches using different criteria; we simply read and map. There are
200 two things we need to do 'depending':
202 We may need to support interleave. We don't really, but it's
203 convenient to do it here rather than rebuild the vector later.
205 Cascades may be additive or multiplicitive; this is not inherent in
206 the codebook, but set in the code using the codebook. Like
207 interleaving, it's easiest to do it here.
208 addmul==0 -> declarative (set the value)
209 addmul==1 -> additive
210 addmul==2 -> multiplicitive */
212 /* returns the [original, not compacted] entry number or -1 on eof *********/
213 long vorbis_book_decode(codebook
*book
, oggpack_buffer
*b
) ICODE_ATTR
;
214 long vorbis_book_decode(codebook
*book
, oggpack_buffer
*b
){
215 long packed_entry
=decode_packed_entry_number(book
,b
);
217 return(book
->dec_index
[packed_entry
]);
219 /* if there's no dec_index, the codebook unpacking isn't collapsed */
220 return(packed_entry
);
223 /* returns 0 on OK or -1 on eof *************************************/
224 long vorbis_book_decodevs_add(codebook
*book
,ogg_int32_t
*a
,
225 oggpack_buffer
*b
,int n
,int point
){
226 int step
=n
/book
->dim
;
227 long *entry
= (long *)alloca(sizeof(*entry
)*step
);
228 ogg_int32_t
**t
= (ogg_int32_t
**)alloca(sizeof(*t
)*step
);
230 int shift
=point
-book
->binarypoint
;
233 for (i
= 0; i
< step
; i
++) {
234 entry
[i
]=decode_packed_entry_number(book
,b
);
235 if(entry
[i
]==-1)return(-1);
236 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
238 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
240 a
[o
+j
]+=t
[j
][i
]>>shift
;
242 for (i
= 0; i
< step
; i
++) {
243 entry
[i
]=decode_packed_entry_number(book
,b
);
244 if(entry
[i
]==-1)return(-1);
245 t
[i
] = book
->valuelist
+entry
[i
]*book
->dim
;
247 for(i
=0,o
=0;i
<book
->dim
;i
++,o
+=step
)
249 a
[o
+j
]+=t
[j
][i
]<<-shift
;
254 long vorbis_book_decodev_add(codebook
*book
,ogg_int32_t
*a
,
255 oggpack_buffer
*b
,int n
,int point
){
258 int shift
=point
-book
->binarypoint
;
262 entry
= decode_packed_entry_number(book
,b
);
263 if(entry
==-1)return(-1);
264 t
= book
->valuelist
+entry
*book
->dim
;
265 for (j
=0;j
<book
->dim
;)
266 a
[i
++]+=t
[j
++]>>shift
;
271 entry
= decode_packed_entry_number(book
,b
);
272 if(entry
==-1)return(-1);
273 t
= book
->valuelist
+entry
*book
->dim
;
274 for (j
=0;j
<book
->dim
;)
275 a
[i
++]+=t
[j
++]<<shift
;
281 long vorbis_book_decodev_set(codebook
*book
,ogg_int32_t
*a
,
282 oggpack_buffer
*b
,int n
,int point
){
285 int shift
=point
-book
->binarypoint
;
290 entry
= decode_packed_entry_number(book
,b
);
291 if(entry
==-1)return(-1);
292 t
= book
->valuelist
+entry
*book
->dim
;
293 for (j
=0;j
<book
->dim
;){
294 a
[i
++]=t
[j
++]>>shift
;
300 entry
= decode_packed_entry_number(book
,b
);
301 if(entry
==-1)return(-1);
302 t
= book
->valuelist
+entry
*book
->dim
;
303 for (j
=0;j
<book
->dim
;){
304 a
[i
++]=t
[j
++]<<shift
;
311 long vorbis_book_decodevv_add(codebook
*book
,ogg_int32_t
**a
,
313 oggpack_buffer
*b
,int n
,int point
){
316 int shift
=point
-book
->binarypoint
;
320 for(i
=offset
;i
<offset
+n
;){
321 entry
= decode_packed_entry_number(book
,b
);
322 if(entry
==-1)return(-1);
324 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
325 for (j
=0;j
<book
->dim
;j
++){
326 a
[chptr
++][i
]+=t
[j
]>>shift
;
336 for(i
=offset
;i
<offset
+n
;){
337 entry
= decode_packed_entry_number(book
,b
);
338 if(entry
==-1)return(-1);
340 const ogg_int32_t
*t
= book
->valuelist
+entry
*book
->dim
;
341 for (j
=0;j
<book
->dim
;j
++){
342 a
[chptr
++][i
]+=t
[j
]<<shift
;