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 shared codebook operations
16 ********************************************************************/
18 #include "config-tremor.h"
24 #include "ivorbiscodec.h"
27 /**** pack/unpack helpers ******************************************/
28 int _ilog(unsigned int v
){
37 /* 32 bit float (not IEEE; nonnormalized mantissa +
38 biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
39 Why not IEEE? It's just not that important here. */
43 #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */
45 static ogg_int32_t
_float32_unpack(long val
,int *point
){
46 long mant
=val
&0x1fffff;
47 int sign
=val
&0x80000000;
48 long exp
=(val
&0x7fe00000L
)>>VQ_FMAN
;
50 exp
-=(VQ_FMAN
-1)+VQ_FEXP_BIAS
;
53 while(!(mant
&0x40000000)){
68 /* given a list of word lengths, generate a list of codewords. Works
69 for length ordered or unordered, always assigns the lowest valued
70 codewords first. Extended to handle unused entries (length 0) */
71 static ogg_uint32_t
*_make_words(long *l
,long n
,long sparsecount
){
73 ogg_uint32_t marker
[33];
74 ogg_uint32_t
*r
=(ogg_uint32_t
*)ogg_tmpmalloc((sparsecount
?sparsecount
:n
)*sizeof(*r
));
75 memset(marker
,0,sizeof(marker
));
80 ogg_uint32_t entry
=marker
[length
];
82 /* when we claim a node for an entry, we also claim the nodes
83 below it (pruning off the imagined tree that may have dangled
84 from it) as well as blocking the use of any nodes directly
88 if(length
<32 && (entry
>>length
)){
89 /* error condition; the lengths must specify an overpopulated tree */
95 /* Look to see if the next shorter marker points to the node
96 above. if so, update it and repeat. */
98 for(j
=length
;j
>0;j
--){
101 /* have to jump branches */
105 marker
[j
]=marker
[j
-1]<<1;
106 break; /* invariant says next upper marker would already
107 have been moved if it was on the same path */
113 /* prune the tree; the implicit invariant says all the longer
114 markers were dangling from our just-taken node. Dangle them
115 from our *new* node. */
116 for(j
=length
+1;j
<33;j
++)
117 if((marker
[j
]>>1) == entry
){
119 marker
[j
]=marker
[j
-1]<<1;
123 if(sparsecount
==0)count
++;
126 /* bitreverse the words because our bitwise packer/unpacker is LSb
128 for(i
=0,count
=0;i
<n
;i
++){
132 temp
|=(r
[count
]>>j
)&1;
145 /* there might be a straightforward one-line way to do the below
146 that's portable and totally safe against roundoff, but I haven't
147 thought of it. Therefore, we opt on the side of caution */
148 long _book_maptype1_quantvals(const static_codebook
*b
){
149 /* get us a starting hint, we'll polish it below */
150 int bits
=_ilog(b
->entries
);
151 int vals
=b
->entries
>>((bits
-1)*(b
->dim
-1)/b
->dim
);
157 for(i
=0;i
<b
->dim
;i
++){
161 if(acc
<=b
->entries
&& acc1
>b
->entries
){
173 /* different than what _book_unquantize does for mainline:
174 we repack the book in a fixed point format that shares the same
175 binary point. Upon first use, we can shift point if needed */
177 /* we need to deal with two map types: in map type 1, the values are
178 generated algorithmically (each column of the vector counts through
179 the values in the quant vector). in map type 2, all the values came
180 in in an explicit list. Both value lists must be unpacked */
182 static ogg_int32_t
*_book_unquantize(const static_codebook
*b
,int n
,
183 int *sparsemap
,int *maxpoint
){
185 if(b
->maptype
==1 || b
->maptype
==2){
187 int minpoint
,delpoint
;
188 ogg_int32_t mindel
=_float32_unpack(b
->q_min
,&minpoint
);
189 ogg_int32_t delta
=_float32_unpack(b
->q_delta
,&delpoint
);
190 ogg_int32_t
*r
=(ogg_int32_t
*)_ogg_calloc(n
*b
->dim
,sizeof(*r
));
191 int *rp
=(int *)ogg_tmpcalloc(n
*b
->dim
,sizeof(*rp
));
193 memset(rp
, 0, n
*b
->dim
*sizeof(*rp
));
196 /* maptype 1 and 2 both use a quantized value vector, but
200 /* most of the time, entries%dimensions == 0, but we need to be
201 well defined. We define that the possible vales at each
202 scalar is values == entries/dim. If entries%dim != 0, we'll
203 have 'too few' values (values*dim<entries), which means that
204 we'll have 'left over' entries; left over entries use zeroed
205 values (and are wasted). So don't generate codebooks like
207 quantvals
=_book_maptype1_quantvals(b
);
208 for(j
=0;j
<b
->entries
;j
++){
209 if((sparsemap
&& b
->lengthlist
[j
]) || !sparsemap
){
213 for(k
=0;k
<b
->dim
;k
++){
214 int index
= (j
/indexdiv
)%quantvals
;
216 int val
=VFLOAT_MULTI(delta
,delpoint
,
217 abs(b
->quantlist
[index
]),&point
);
219 val
=VFLOAT_ADD(mindel
,minpoint
,val
,point
,&point
);
220 val
=VFLOAT_ADD(last
,lastpoint
,val
,point
,&point
);
228 r
[sparsemap
[count
]*b
->dim
+k
]=val
;
229 rp
[sparsemap
[count
]*b
->dim
+k
]=point
;
231 r
[count
*b
->dim
+k
]=val
;
232 rp
[count
*b
->dim
+k
]=point
;
234 if(*maxpoint
<point
)*maxpoint
=point
;
243 for(j
=0;j
<b
->entries
;j
++){
244 if((sparsemap
&& b
->lengthlist
[j
]) || !sparsemap
){
248 for(k
=0;k
<b
->dim
;k
++){
250 int val
=VFLOAT_MULTI(delta
,delpoint
,
251 abs(b
->quantlist
[j
*b
->dim
+k
]),&point
);
253 val
=VFLOAT_ADD(mindel
,minpoint
,val
,point
,&point
);
254 val
=VFLOAT_ADD(last
,lastpoint
,val
,point
,&point
);
262 r
[sparsemap
[count
]*b
->dim
+k
]=val
;
263 rp
[sparsemap
[count
]*b
->dim
+k
]=point
;
265 r
[count
*b
->dim
+k
]=val
;
266 rp
[count
*b
->dim
+k
]=point
;
268 if(*maxpoint
<point
)*maxpoint
=point
;
276 for(j
=0;j
<n
*b
->dim
;j
++)
278 r
[j
]>>=*maxpoint
-rp
[j
];
286 void vorbis_staticbook_clear(static_codebook
*b
){
287 if(b
->quantlist
)_ogg_free(b
->quantlist
);
288 if(b
->lengthlist
)_ogg_free(b
->lengthlist
);
289 memset(b
,0,sizeof(*b
));
293 void vorbis_staticbook_destroy(static_codebook
*b
){
294 vorbis_staticbook_clear(b
);
298 void vorbis_book_clear(codebook
*b
){
299 /* static book is not cleared; we're likely called on the lookup and
300 the static codebook belongs to the info struct */
301 if(b
->valuelist
)_ogg_free(b
->valuelist
);
302 if(b
->codelist
)_ogg_free(b
->codelist
);
304 if(b
->dec_index
)_ogg_free(b
->dec_index
);
305 if(b
->dec_codelengths
)_ogg_free(b
->dec_codelengths
);
306 if(b
->dec_firsttable
)_ogg_free(b
->dec_firsttable
);
308 memset(b
,0,sizeof(*b
));
311 static ogg_uint32_t
bitreverse(ogg_uint32_t x
){
312 x
= ((x
>>16)&0x0000ffffUL
) | ((x
<<16)&0xffff0000UL
);
313 x
= ((x
>> 8)&0x00ff00ffUL
) | ((x
<< 8)&0xff00ff00UL
);
314 x
= ((x
>> 4)&0x0f0f0f0fUL
) | ((x
<< 4)&0xf0f0f0f0UL
);
315 x
= ((x
>> 2)&0x33333333UL
) | ((x
<< 2)&0xccccccccUL
);
316 return((x
>> 1)&0x55555555UL
) | ((x
<< 1)&0xaaaaaaaaUL
);
319 static int sort32a(const void *a
,const void *b
){
320 return (**(ogg_uint32_t
**)a
>**(ogg_uint32_t
**)b
)-
321 (**(ogg_uint32_t
**)a
<**(ogg_uint32_t
**)b
);
324 /* decode codebook arrangement is more heavily optimized than encode */
325 int vorbis_book_init_decode(codebook
*c
,const static_codebook
*s
){
328 long pos
= ogg_tmpmalloc_pos();
329 memset(c
,0,sizeof(*c
));
331 /* count actually used entries */
332 for(i
=0;i
<s
->entries
;i
++)
333 if(s
->lengthlist
[i
]>0)
336 c
->entries
=s
->entries
;
341 /* two different remappings go on here.
343 First, we collapse the likely sparse codebook down only to
344 actually represented values/words. This collapsing needs to be
345 indexed as map-valueless books are used to encode original entry
346 positions as integers.
348 Second, we reorder all vectors, including the entry index above,
349 by sorted bitreversed codeword to allow treeless decode. */
352 ogg_uint32_t
*codes
=_make_words(s
->lengthlist
,s
->entries
,c
->used_entries
);
353 ogg_uint32_t
**codep
=(ogg_uint32_t
**)ogg_tmpmalloc(sizeof(*codep
)*n
);
355 if(codes
==NULL
||codep
==NULL
)goto err_out
;
358 codes
[i
]=bitreverse(codes
[i
]);
362 qsort(codep
,n
,sizeof(*codep
),sort32a
);
364 sortindex
=(int *)ogg_tmpmalloc(n
*sizeof(*sortindex
));
365 c
->codelist
=(ogg_uint32_t
*)_ogg_malloc(n
*sizeof(*c
->codelist
));
366 /* the index is a reverse index */
368 int position
=codep
[i
]-codes
;
369 sortindex
[position
]=i
;
373 c
->codelist
[sortindex
[i
]]=codes
[i
];
374 /* _ogg_free(codes); */
378 c
->valuelist
=_book_unquantize(s
,n
,sortindex
,&c
->binarypoint
);
379 c
->dec_index
=(int *)_ogg_malloc(n
*sizeof(*c
->dec_index
));
381 for(n
=0,i
=0;i
<s
->entries
;i
++)
382 if(s
->lengthlist
[i
]>0)
383 c
->dec_index
[sortindex
[n
++]]=i
;
385 c
->dec_codelengths
=(char *)_ogg_malloc(n
*sizeof(*c
->dec_codelengths
));
386 for(n
=0,i
=0;i
<s
->entries
;i
++)
387 if(s
->lengthlist
[i
]>0)
388 c
->dec_codelengths
[sortindex
[n
++]]=s
->lengthlist
[i
];
390 c
->dec_firsttablen
=_ilog(c
->used_entries
)-4; /* this is magic */
391 if(c
->dec_firsttablen
<5)c
->dec_firsttablen
=5;
392 if(c
->dec_firsttablen
>8)c
->dec_firsttablen
=8;
394 tabn
=1<<c
->dec_firsttablen
;
395 c
->dec_firsttable
=(ogg_uint32_t
*)_ogg_calloc(tabn
,sizeof(*c
->dec_firsttable
));
399 if(c
->dec_maxlength
<c
->dec_codelengths
[i
])
400 c
->dec_maxlength
=c
->dec_codelengths
[i
];
401 if(c
->dec_codelengths
[i
]<=c
->dec_firsttablen
){
402 ogg_uint32_t orig
=bitreverse(c
->codelist
[i
]);
403 for(j
=0;j
<(1<<(c
->dec_firsttablen
-c
->dec_codelengths
[i
]));j
++)
404 c
->dec_firsttable
[orig
|(j
<<c
->dec_codelengths
[i
])]=i
+1;
408 /* now fill in 'unused' entries in the firsttable with hi/lo search
409 hints for the non-direct-hits */
411 ogg_uint32_t mask
=0xfffffffeUL
<<(31-c
->dec_firsttablen
);
415 ogg_uint32_t word
=i
<<(32-c
->dec_firsttablen
);
416 if(c
->dec_firsttable
[bitreverse(word
)]==0){
417 while((lo
+1)<n
&& c
->codelist
[lo
+1]<=word
)lo
++;
418 while( hi
<n
&& word
>=(c
->codelist
[hi
]&mask
))hi
++;
420 /* we only actually have 15 bits per hint to play with here.
421 In order to overflow gracefully (nothing breaks, efficiency
422 just drops), encode as the difference from the extremes. */
424 unsigned long loval
=lo
;
425 unsigned long hival
=n
-hi
;
427 if(loval
>0x7fff)loval
=0x7fff;
428 if(hival
>0x7fff)hival
=0x7fff;
429 c
->dec_firsttable
[bitreverse(word
)]=
430 0x80000000UL
| (loval
<<15) | hival
;
437 ogg_tmpmalloc_free(pos
);
440 ogg_tmpmalloc_free(pos
);
441 vorbis_book_clear(c
);