/src/vorbis/lib/sharedbook.c
Line | Count | Source |
1 | | /******************************************************************** |
2 | | * * |
3 | | * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * |
4 | | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
5 | | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
6 | | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
7 | | * * |
8 | | * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * |
9 | | * by the Xiph.Org Foundation https://xiph.org/ * |
10 | | * * |
11 | | ******************************************************************** |
12 | | |
13 | | function: basic shared codebook operations |
14 | | |
15 | | ********************************************************************/ |
16 | | |
17 | | #include <stdlib.h> |
18 | | #include <limits.h> |
19 | | #include <math.h> |
20 | | #include <string.h> |
21 | | #include <ogg/ogg.h> |
22 | | #include "os.h" |
23 | | #include "misc.h" |
24 | | #include "vorbis/codec.h" |
25 | | #include "codebook.h" |
26 | | #include "scales.h" |
27 | | |
28 | | /**** pack/unpack helpers ******************************************/ |
29 | | |
30 | 367k | int ov_ilog(ogg_uint32_t v){ |
31 | 367k | int ret; |
32 | 2.66M | for(ret=0;v;ret++)v>>=1; |
33 | 367k | return ret; |
34 | 367k | } |
35 | | |
36 | | /* 32 bit float (not IEEE; nonnormalized mantissa + |
37 | | biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm |
38 | | Why not IEEE? It's just not that important here. */ |
39 | | |
40 | | #define VQ_FEXP 10 |
41 | 3.43k | #define VQ_FMAN 21 |
42 | 1.71k | #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */ |
43 | | |
44 | | /* doesn't currently guard under/overflow */ |
45 | 0 | long _float32_pack(float val){ |
46 | 0 | int sign=0; |
47 | 0 | long exp; |
48 | 0 | long mant; |
49 | 0 | if(val<0){ |
50 | 0 | sign=0x80000000; |
51 | 0 | val= -val; |
52 | 0 | } |
53 | 0 | exp= floor(log(val)/log(2.f)+.001); /* +epsilon */ |
54 | 0 | mant=rint(ldexp(val,(VQ_FMAN-1)-exp)); |
55 | 0 | exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN; |
56 | |
|
57 | 0 | return(sign|exp|mant); |
58 | 0 | } |
59 | | |
60 | 1.71k | float _float32_unpack(long val){ |
61 | 1.71k | double mant=val&0x1fffff; |
62 | 1.71k | int sign=val&0x80000000; |
63 | 1.71k | long exp =(val&0x7fe00000L)>>VQ_FMAN; |
64 | 1.71k | if(sign)mant= -mant; |
65 | 1.71k | exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS; |
66 | | /* clamp excessive exponent values */ |
67 | 1.71k | if (exp>63){ |
68 | 37 | exp=63; |
69 | 37 | } |
70 | 1.71k | if (exp<-63){ |
71 | 1.36k | exp=-63; |
72 | 1.36k | } |
73 | 1.71k | return(ldexp(mant,exp)); |
74 | 1.71k | } |
75 | | |
76 | | /* given a list of word lengths, generate a list of codewords. Works |
77 | | for length ordered or unordered, always assigns the lowest valued |
78 | | codewords first. Extended to handle unused entries (length 0) */ |
79 | 0 | ogg_uint32_t *_make_words(char *l,long n,long sparsecount){ |
80 | 0 | long i,j,count=0; |
81 | 0 | ogg_uint32_t marker[33]; |
82 | 0 | ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); |
83 | 0 | memset(marker,0,sizeof(marker)); |
84 | |
|
85 | 0 | for(i=0;i<n;i++){ |
86 | 0 | long length=l[i]; |
87 | 0 | if(length>0){ |
88 | 0 | ogg_uint32_t entry=marker[length]; |
89 | | |
90 | | /* when we claim a node for an entry, we also claim the nodes |
91 | | below it (pruning off the imagined tree that may have dangled |
92 | | from it) as well as blocking the use of any nodes directly |
93 | | above for leaves */ |
94 | | |
95 | | /* update ourself */ |
96 | 0 | if(length<32 && (entry>>length)){ |
97 | | /* error condition; the lengths must specify an overpopulated tree */ |
98 | 0 | _ogg_free(r); |
99 | 0 | return(NULL); |
100 | 0 | } |
101 | 0 | r[count++]=entry; |
102 | | |
103 | | /* Look to see if the next shorter marker points to the node |
104 | | above. if so, update it and repeat. */ |
105 | 0 | { |
106 | 0 | for(j=length;j>0;j--){ |
107 | |
|
108 | 0 | if(marker[j]&1){ |
109 | | /* have to jump branches */ |
110 | 0 | if(j==1) |
111 | 0 | marker[1]++; |
112 | 0 | else |
113 | 0 | marker[j]=marker[j-1]<<1; |
114 | 0 | break; /* invariant says next upper marker would already |
115 | | have been moved if it was on the same path */ |
116 | 0 | } |
117 | 0 | marker[j]++; |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | /* prune the tree; the implicit invariant says all the longer |
122 | | markers were dangling from our just-taken node. Dangle them |
123 | | from our *new* node. */ |
124 | 0 | for(j=length+1;j<33;j++) |
125 | 0 | if((marker[j]>>1) == entry){ |
126 | 0 | entry=marker[j]; |
127 | 0 | marker[j]=marker[j-1]<<1; |
128 | 0 | }else |
129 | 0 | break; |
130 | 0 | }else |
131 | 0 | if(sparsecount==0)count++; |
132 | 0 | } |
133 | | |
134 | | /* any underpopulated tree must be rejected. */ |
135 | | /* Single-entry codebooks are a retconned extension to the spec. |
136 | | They have a single codeword '0' of length 1 that results in an |
137 | | underpopulated tree. Shield that case from the underformed tree check. */ |
138 | 0 | if(!(count==1 && marker[2]==2)){ |
139 | 0 | for(i=1;i<33;i++) |
140 | 0 | if(marker[i] & (0xffffffffUL>>(32-i))){ |
141 | 0 | _ogg_free(r); |
142 | 0 | return(NULL); |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* bitreverse the words because our bitwise packer/unpacker is LSb |
147 | | endian */ |
148 | 0 | for(i=0,count=0;i<n;i++){ |
149 | 0 | ogg_uint32_t temp=0; |
150 | 0 | for(j=0;j<l[i];j++){ |
151 | 0 | temp<<=1; |
152 | 0 | temp|=(r[count]>>j)&1; |
153 | 0 | } |
154 | |
|
155 | 0 | if(sparsecount){ |
156 | 0 | if(l[i]) |
157 | 0 | r[count++]=temp; |
158 | 0 | }else |
159 | 0 | r[count++]=temp; |
160 | 0 | } |
161 | |
|
162 | 0 | return(r); |
163 | 0 | } |
164 | | |
165 | | /* given a list of word lengths, generate a list of packed MSb codewords and |
166 | | entry numbers. This is like the above, except that the codewords are MSb |
167 | | first and aligned to be 32-bits in size, and the lower 24 bits include the |
168 | | entry number the codeword corresponds to. */ |
169 | 709 | static ogg_int64_t *dec_make_words(signed char *l,long n,long sparsecount){ |
170 | 709 | long i,j,count=0; |
171 | 709 | ogg_uint32_t marker[33]; |
172 | 709 | ogg_int64_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); |
173 | 709 | if(r==NULL)return(NULL); |
174 | 709 | memset(marker,0,sizeof(marker)); |
175 | | |
176 | 2.17k | for(i=0;i<n;i++){ |
177 | 1.47k | long length=l[i]; |
178 | 1.47k | if(length>0){ |
179 | 1.46k | ogg_uint32_t entry=marker[length]; |
180 | | |
181 | | /* when we claim a node for an entry, we also claim the nodes |
182 | | below it (pruning off the imagined tree that may have dangled |
183 | | from it) as well as blocking the use of any nodes directly |
184 | | above for leaves */ |
185 | | |
186 | | /* update ourself */ |
187 | 1.46k | if(length<32 && (entry>>length)){ |
188 | | /* error condition; the lengths must specify an overpopulated tree */ |
189 | 13 | _ogg_free(r); |
190 | 13 | return(NULL); |
191 | 13 | } |
192 | 1.45k | r[count++]=(ogg_int64_t)entry<<(32-l[i]+24)|i; |
193 | | |
194 | | /* Look to see if the next shorter marker points to the node |
195 | | above. if so, update it and repeat. */ |
196 | 1.45k | { |
197 | 3.41k | for(j=length;j>0;j--){ |
198 | | |
199 | 2.70k | if(marker[j]&1){ |
200 | | /* have to jump branches */ |
201 | 745 | if(j==1) |
202 | 18 | marker[1]++; |
203 | 727 | else |
204 | 727 | marker[j]=marker[j-1]<<1; |
205 | 745 | break; /* invariant says next upper marker would already |
206 | | have been moved if it was on the same path */ |
207 | 745 | } |
208 | 1.96k | marker[j]++; |
209 | 1.96k | } |
210 | 1.45k | } |
211 | | |
212 | | /* prune the tree; the implicit invariant says all the longer |
213 | | markers were dangling from our just-taken node. Dangle them |
214 | | from our *new* node. */ |
215 | 23.9k | for(j=length+1;j<33;j++) |
216 | 23.0k | if((marker[j]>>1) == entry){ |
217 | 22.5k | entry=marker[j]; |
218 | 22.5k | marker[j]=marker[j-1]<<1; |
219 | 22.5k | }else |
220 | 512 | break; |
221 | 1.45k | }else |
222 | 6 | if(sparsecount==0)count++; |
223 | 1.47k | } |
224 | | |
225 | | /* any underpopulated tree must be rejected. */ |
226 | | /* Single-entry codebooks are a retconned extension to the spec. |
227 | | They have a single codeword '0' of length 1 that results in an |
228 | | underpopulated tree. Shield that case from the underformed tree check. */ |
229 | 696 | if(!(count==1 && marker[2]==2)){ |
230 | 37 | for(i=1;i<33;i++) |
231 | 37 | if(marker[i] & (0xffffffffUL>>(32-i))){ |
232 | 25 | _ogg_free(r); |
233 | 25 | return(NULL); |
234 | 25 | } |
235 | 25 | } |
236 | | |
237 | 671 | return(r); |
238 | 696 | } |
239 | | |
240 | | /* there might be a straightforward one-line way to do the below |
241 | | that's portable and totally safe against roundoff, but I haven't |
242 | | thought of it. Therefore, we opt on the side of caution */ |
243 | 2.01k | long _book_maptype1_quantvals(long dim, long entries){ |
244 | 2.01k | long vals; |
245 | 2.01k | if(entries<1){ |
246 | 8 | return(0); |
247 | 8 | } |
248 | 2.00k | vals=floor(pow((float)entries,1.f/dim)); |
249 | | |
250 | | /* the above *should* be reliable, but we'll not assume that FP is |
251 | | ever reliable when bitstream sync is at stake; verify via integer |
252 | | means that vals really is the greatest value of dim for which |
253 | | vals^dim <= entries */ |
254 | | /* treat the above as an initial guess */ |
255 | 2.00k | if(vals<1){ |
256 | 0 | vals=1; |
257 | 0 | } |
258 | 2.00k | while(1){ |
259 | 2.00k | long acc=1; |
260 | 2.00k | long acc1=1; |
261 | 2.00k | int i; |
262 | 24.9k | for(i=0;i<dim;i++){ |
263 | 22.9k | if(entries/vals<acc)break; |
264 | 22.9k | acc*=vals; |
265 | 22.9k | if(LONG_MAX/(vals+1)<acc1)acc1=LONG_MAX; |
266 | 20.6k | else acc1*=vals+1; |
267 | 22.9k | } |
268 | 2.00k | if(i>=dim && acc<=entries && acc1>entries){ |
269 | 2.00k | return(vals); |
270 | 2.00k | }else{ |
271 | 0 | if(i<dim || acc>entries){ |
272 | 0 | vals--; |
273 | 0 | }else{ |
274 | 0 | vals++; |
275 | 0 | } |
276 | 0 | } |
277 | 2.00k | } |
278 | 2.00k | } |
279 | | |
280 | | /* unpack the quantized list of values for decode ***********/ |
281 | | /* we need to deal with two map types: in map type 1, the values are |
282 | | generated algorithmically (each column of the vector counts through |
283 | | the values in the quant vector). in map type 2, all the values came |
284 | | in in an explicit list. Both value lists must be unpacked */ |
285 | | void _book_unquantize(float *r,const dec_codebook *b,ogg_int32_t n, |
286 | 858 | ogg_int64_t *sparsemap){ |
287 | 858 | ogg_int32_t i,j,k; |
288 | 858 | if(b->maptype==1 || b->maptype==2){ |
289 | 858 | ogg_int32_t quantvals; |
290 | 858 | float mindel=_float32_unpack(b->q_min); |
291 | 858 | float delta=_float32_unpack(b->q_delta); |
292 | | |
293 | | /* maptype 1 and 2 both use a quantized value vector, but |
294 | | different sizes */ |
295 | 858 | switch(b->maptype){ |
296 | 852 | case 1: |
297 | | /* most of the time, entries%dimensions == 0, but we need to be |
298 | | well defined. We define that the possible vales at each |
299 | | scalar is values == entries/dim. If entries%dim != 0, we'll |
300 | | have 'too few' values (values*dim<entries), which means that |
301 | | we'll have 'left over' entries; left over entries use zeroed |
302 | | values (and are wasted). So don't generate codebooks like |
303 | | that */ |
304 | 852 | quantvals=(ogg_int32_t)_book_maptype1_quantvals(b->dim, b->entries); |
305 | 1.89k | for(i=0;i<n;i++){ |
306 | 1.03k | float last=0.f; |
307 | 1.03k | int indexdiv=1; |
308 | 1.03k | j=(long)(sparsemap?sparsemap[i]&0xffffff:i); |
309 | 12.7k | for(k=0;k<b->dim;k++){ |
310 | 11.7k | ogg_int32_t index=(j/indexdiv)%quantvals; |
311 | 11.7k | float val=b->quantlist[index]; |
312 | 11.7k | val=fabs(val)*delta+mindel+last; |
313 | 11.7k | if(b->q_sequencep)last=val; |
314 | 11.7k | r[i*b->dim+k]=val; |
315 | 11.7k | indexdiv*=quantvals; |
316 | 11.7k | } |
317 | 1.03k | } |
318 | 852 | break; |
319 | 6 | case 2: |
320 | 12 | for(i=0;i<n;i++){ |
321 | 6 | float last=0.f; |
322 | 6 | j=(ogg_int32_t)(sparsemap?sparsemap[i]&0xffffff:i); |
323 | 15 | for(k=0;k<b->dim;k++){ |
324 | 9 | float val=b->quantlist[j*b->dim+k]; |
325 | 9 | val=fabs(val)*delta+mindel+last; |
326 | 9 | if(b->q_sequencep)last=val; |
327 | 9 | r[i*b->dim+k]=val; |
328 | 9 | } |
329 | 6 | } |
330 | 6 | break; |
331 | 858 | } |
332 | | |
333 | 858 | } |
334 | 858 | } |
335 | | |
336 | 0 | void vorbis_staticbook_destroy(static_codebook *b){ |
337 | 0 | if(b->allocedp){ |
338 | 0 | if(b->quantlist)_ogg_free(b->quantlist); |
339 | 0 | if(b->lengthlist)_ogg_free(b->lengthlist); |
340 | 0 | memset(b,0,sizeof(*b)); |
341 | 0 | _ogg_free(b); |
342 | 0 | } /* otherwise, it is in static memory */ |
343 | 0 | } |
344 | | |
345 | 0 | void vorbis_book_clear(codebook *b){ |
346 | | /* static book is not cleared; we're likely called on the lookup and |
347 | | the static codebook belongs to the info struct */ |
348 | 0 | if(b->valuelist)_ogg_free(b->valuelist); |
349 | 0 | if(b->codelist)_ogg_free(b->codelist); |
350 | |
|
351 | 0 | memset(b,0,sizeof(*b)); |
352 | 0 | } |
353 | | |
354 | 3.84k | void vorbis_decbook_clear(dec_codebook *b){ |
355 | 3.84k | if(b->quantlist)_ogg_free(b->quantlist); |
356 | 3.84k | if(b->firsttable)_ogg_free(b->firsttable); |
357 | 3.84k | if(b->codelist)_ogg_free(b->codelist); |
358 | 3.84k | if(b->codelengths)_ogg_free(b->codelengths); |
359 | 3.84k | if(b->index)_ogg_free(b->index); |
360 | 3.84k | if(b->valuelist)_ogg_free(b->valuelist); |
361 | 3.84k | memset(b,0,sizeof(*b)); |
362 | 3.84k | } |
363 | | |
364 | 0 | int vorbis_book_init_encode(codebook *c,const static_codebook *s){ |
365 | |
|
366 | 0 | memset(c,0,sizeof(*c)); |
367 | 0 | c->c=s; |
368 | 0 | c->entries=s->entries; |
369 | 0 | c->used_entries=s->entries; |
370 | 0 | c->dim=s->dim; |
371 | 0 | c->codelist=_make_words(s->lengthlist,s->entries,0); |
372 | 0 | c->quantvals=_book_maptype1_quantvals(s->dim, s->entries); |
373 | 0 | c->minval=(int)rint(_float32_unpack(s->q_min)); |
374 | 0 | c->delta=(int)rint(_float32_unpack(s->q_delta)); |
375 | |
|
376 | 0 | return(0); |
377 | 0 | } |
378 | | |
379 | 372 | static ogg_uint32_t bitreverse(ogg_uint32_t x){ |
380 | 372 | x= ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL); |
381 | 372 | x= ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL); |
382 | 372 | x= ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL); |
383 | 372 | x= ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL); |
384 | 372 | return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL); |
385 | 372 | } |
386 | | |
387 | 0 | static int sort64a(const void *a,const void *b){ |
388 | 0 | return ( *(ogg_int64_t *)a>*(ogg_int64_t *)b)- |
389 | 0 | ( *(ogg_int64_t *)a<*(ogg_int64_t *)b); |
390 | 0 | } |
391 | | |
392 | | /* decode codebook arrangement is more heavily optimized than encode */ |
393 | 907 | int vorbis_book_init_decode(dec_codebook *c){ |
394 | 907 | ogg_int32_t i,j,n; |
395 | 907 | ogg_int64_t *codes=NULL; |
396 | | |
397 | | /* only initialize once */ |
398 | 907 | if(c->codelist)return(0); |
399 | | |
400 | 907 | if(c->codelengths){ |
401 | | /* unordered codebook */ |
402 | | /* count actually used entries */ |
403 | 709 | n=0; |
404 | 2.84k | for(i=0;i<c->entries;i++) |
405 | 2.13k | if(c->codelengths[i]>0) |
406 | 2.12k | n++; |
407 | | |
408 | 709 | if(n>0){ |
409 | 709 | signed char *codelengths; |
410 | | /* two different remappings go on here. |
411 | | |
412 | | First, we collapse the likely sparse codebook down only to |
413 | | actually represented values/words. This collapsing needs to be |
414 | | indexed as map-valueless books are used to encode original entry |
415 | | positions as integers. |
416 | | |
417 | | Second, we reorder all vectors, including the entry index above, |
418 | | by sorted bitreversed codeword to allow treeless decode. */ |
419 | | |
420 | | /* perform sort */ |
421 | 709 | codes=dec_make_words(c->codelengths,c->entries,n); |
422 | 709 | if(codes==NULL)goto err_out; |
423 | | |
424 | 671 | qsort(codes,n,sizeof(*codes),sort64a); |
425 | | |
426 | 671 | c->codelist=_ogg_malloc(n*sizeof(*c->codelist)); |
427 | 671 | if(c->codelist==NULL)goto err_out; |
428 | | |
429 | 1.34k | for(i=0;i<n;i++){ |
430 | 671 | c->codelist[i]=(ogg_uint32_t)(codes[i]>>24); |
431 | 671 | } |
432 | | |
433 | 671 | if(c->maptype==1 || c->maptype==2){ |
434 | 671 | c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist)); |
435 | 671 | if(c->valuelist==NULL)goto err_out; |
436 | 671 | _book_unquantize(c->valuelist,c,n,codes); |
437 | 671 | _ogg_free(c->quantlist); |
438 | 671 | c->quantlist=NULL; |
439 | 671 | } |
440 | 671 | c->index=_ogg_malloc(n*sizeof(*c->index)); |
441 | 671 | if(c->index==NULL)goto err_out; |
442 | 671 | codelengths=_ogg_malloc(n*sizeof(*c->codelengths)); |
443 | 671 | if(codelengths==NULL)goto err_out; |
444 | | |
445 | | /* save the index of used entries, compact and reorder the codelengths, |
446 | | and find min/max length */ |
447 | 671 | c->minlength=32; |
448 | 671 | c->maxlength=0; |
449 | 1.34k | for(i=0;i<n;i++){ |
450 | 671 | j=(ogg_int32_t)(codes[i]&0xffffff); |
451 | 671 | c->index[i]=j; |
452 | 671 | codelengths[i]=c->codelengths[j]; |
453 | 671 | if(codelengths[i]<c->minlength) |
454 | 671 | c->minlength=codelengths[i]; |
455 | 671 | if(codelengths[i]>c->maxlength) |
456 | 671 | c->maxlength=codelengths[i]; |
457 | 671 | } |
458 | 671 | _ogg_free(codes); |
459 | 671 | codes=NULL; |
460 | 671 | _ogg_free(c->codelengths); |
461 | 671 | c->codelengths=codelengths; |
462 | 671 | } |
463 | 709 | }else{ |
464 | | /* ordered codebook: |
465 | | we can avoid building an explicit list of codepoints, which can be a big |
466 | | advantage as we approach the limits of the spec with upwards of 16 |
467 | | million codebook entries. We follow the approach of Alistair Moffat and |
468 | | Andrew Turpin: On the Implementation of Minimum Redundancy Prefix Codes, |
469 | | IEEE Transactions on Communications, 45(10):1200--1207, October, 1997, |
470 | | except modified to have codewords in order of increasing length, instead |
471 | | of decreasing. */ |
472 | 198 | n=c->entries; |
473 | 198 | if(n>0){ |
474 | 198 | ogg_uint32_t prev_entry; |
475 | 198 | ogg_uint32_t code; |
476 | 198 | int nlengths; |
477 | 198 | int length; |
478 | 198 | int l; |
479 | 198 | nlengths=c->maxlength-c->minlength+1; |
480 | 198 | c->codelist=_ogg_malloc(nlengths*sizeof(*c->codelist)); |
481 | 198 | if(c->codelist==NULL)goto err_out; |
482 | | /* for this case, codelist[l] contains the last codeword of the l'th |
483 | | length, left-aligned in a 32-bit word, with all remaining bits filled |
484 | | with trailing 1's. This ensures that 32 bits taken from the stream |
485 | | will be <= codelist[l] if the codeword length is <= length l. */ |
486 | 198 | prev_entry=0; |
487 | 198 | code=0; |
488 | 198 | length=c->minlength; |
489 | 396 | for(l=0;l<nlengths;l++,length++){ |
490 | 198 | ogg_uint32_t nentries; |
491 | 198 | nentries=c->index[l]-prev_entry; |
492 | | /* guard against an over-populated tree. We do not check the last |
493 | | length, because it should exactly use the remaining space, which |
494 | | causes code to wrap to 0 (something this check prevents for any |
495 | | earlier length). We'll check it after the loop. */ |
496 | 198 | if(l+1<nlengths && nentries>((0xffffffffUL-code)>>(32-length))) |
497 | 0 | goto err_out; |
498 | 198 | code+=nentries<<(32-length); |
499 | | /* this requires that the first length have a non-zero number of |
500 | | entries, or c->codelist[0] will wrap to 0xffffffff, making the list |
501 | | non-monotonic. */ |
502 | 198 | c->codelist[l]=code-1; |
503 | 198 | prev_entry=c->index[l]; |
504 | 198 | } |
505 | | /* for a complete tree, the last value will have all bits set, which |
506 | | guarantees we will not walk past the end of the array during decode */ |
507 | 198 | if(c->codelist[nlengths-1]!=0xffffffffUL){ |
508 | | /* reject over/underpopulated trees, with the exception of a |
509 | | single-entry codebook. */ |
510 | 12 | if(n!=1 || c->maxlength!=1)goto err_out; |
511 | 12 | } |
512 | 187 | if(c->maptype==1 || c->maptype==2){ |
513 | 187 | c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist)); |
514 | 187 | if(c->valuelist==NULL)goto err_out; |
515 | 187 | _book_unquantize(c->valuelist,c,n,NULL); |
516 | 187 | _ogg_free(c->quantlist); |
517 | 187 | c->quantlist=NULL; |
518 | 187 | } |
519 | 187 | } |
520 | 198 | } |
521 | | /* build the fastpath lookup table */ |
522 | 858 | if(n>0){ |
523 | 858 | if(n==1 && c->maxlength==1){ |
524 | | /* special case the 'single entry codebook' with a single bit |
525 | | fastpath table (that always returns entry 0 )in order to use |
526 | | unmodified decode paths. */ |
527 | 672 | c->firsttablen=1; |
528 | 672 | c->firsttable=_ogg_calloc(2,sizeof(*c->firsttable)); |
529 | 672 | if(c->firsttable==NULL)goto err_out; |
530 | 672 | c->firsttable[0]=c->firsttable[1]=1; |
531 | | |
532 | 672 | }else{ |
533 | 186 | int used_bits; |
534 | 186 | int tabn; |
535 | 186 | used_bits=ov_ilog(n); |
536 | 186 | c->firsttablen=(signed char)(used_bits-4); /* this is magic */ |
537 | 186 | if(c->firsttablen<5)c->firsttablen=5; |
538 | | /* if the codewords all have similar lengths, we might wind up with a |
539 | | table smaller than the shorter codes: increase the size or we could |
540 | | take the slow path in a large fraction of cases */ |
541 | 186 | if(c->firsttablen<c->minlength+1) |
542 | 0 | c->firsttablen=c->minlength+1; |
543 | | /* also cap the table by the max length, or we are just wasting space */ |
544 | 186 | if(c->firsttablen>c->maxlength) |
545 | 186 | c->firsttablen=c->maxlength; |
546 | | /* if we have an ordered book with a very large minimum length, an 8-bit |
547 | | table would be mostly wasted. Instead, try to make one just large |
548 | | enough to get a good guess for the codeword length. */ |
549 | 186 | if(c->codelengths==NULL && c->minlength>8) |
550 | 0 | c->firsttablen=c->maxlength-c->minlength+1; |
551 | 186 | if(c->firsttablen>8)c->firsttablen=8; |
552 | | |
553 | 186 | tabn=1<<c->firsttablen; |
554 | 186 | c->firsttable=_ogg_calloc(tabn,sizeof(*c->firsttable)); |
555 | 186 | if(c->firsttable==NULL)goto err_out; |
556 | | |
557 | 186 | if(c->codelengths!=NULL){ |
558 | | /* unordered codebook: |
559 | | use the codewords for each entry to build the fastpath table */ |
560 | 0 | for(i=0;i<n;i++){ |
561 | 0 | if(c->codelengths[i]<=c->firsttablen){ |
562 | 0 | ogg_uint32_t orig=bitreverse(c->codelist[i]); |
563 | 0 | for(j=0;j<(1<<(c->firsttablen-c->codelengths[i]));j++) |
564 | | /* pack the length into the table, too, to avoid an extra lookup. |
565 | | This also guarantees the table value is non-zero, since the |
566 | | length of any used entry is positive. */ |
567 | 0 | c->firsttable[orig|(j<<c->codelengths[i])]= |
568 | 0 | i<<6|c->codelengths[i]; |
569 | 0 | } |
570 | 0 | } |
571 | | |
572 | | /* now fill in 'unused' entries in the firsttable with hi/lo search |
573 | | hints for the non-direct-hits */ |
574 | 0 | { |
575 | 0 | ogg_uint32_t mask=0xfffffffeUL<<(31-c->firsttablen); |
576 | 0 | long lo=0,hi=0; |
577 | 0 | int hint_shift; |
578 | |
|
579 | 0 | c->hi_max=n; |
580 | 0 | hint_shift=used_bits>15?used_bits-15:0; |
581 | 0 | c->hint_shift=(signed char)hint_shift; |
582 | 0 | for(i=0;i<tabn;i++){ |
583 | 0 | ogg_uint32_t word=((ogg_uint32_t)i<<(32-c->firsttablen)); |
584 | 0 | if(c->firsttable[bitreverse(word)]==0){ |
585 | 0 | while((lo+1)<n && c->codelist[lo+1]<=word)lo++; |
586 | 0 | while( hi<n && word>=(c->codelist[hi]&mask))hi++; |
587 | | |
588 | | /* we only actually have 15 bits per hint to play with here. |
589 | | In order to overflow gracefully (nothing breaks, efficiency |
590 | | just drops), shift the values down when the range gets too |
591 | | large. We encode the hi value as a difference from the max so |
592 | | truncation error moves it upwards without risk of stepping |
593 | | past the end of the arrays. */ |
594 | 0 | { |
595 | 0 | unsigned long loval=lo>>hint_shift; |
596 | 0 | unsigned long hival=(n-hi)>>hint_shift; |
597 | |
|
598 | 0 | if(loval>0x7fff)loval=0x7fff; |
599 | 0 | if(hival>0x7fff)hival=0x7fff; |
600 | 0 | c->firsttable[bitreverse(word)]= |
601 | 0 | 0x80000000UL | (loval<<15) | hival; |
602 | 0 | } |
603 | 0 | } |
604 | 0 | } |
605 | 0 | } |
606 | 186 | }else{ |
607 | 186 | ogg_uint32_t code; |
608 | 186 | int nlengths; |
609 | 186 | int length; |
610 | 186 | int l; |
611 | | /* ordered codebook: |
612 | | step through the code lengths to build the fastpath table */ |
613 | 186 | nlengths=c->maxlength-c->minlength+1; |
614 | 186 | c->hi_max=nlengths-1; |
615 | 186 | c->hint_shift=0; |
616 | 186 | length=c->minlength; |
617 | 186 | code=0; |
618 | 372 | for(i=l=0;length<=c->firsttablen;l++,length++){ |
619 | 558 | for(;i<c->index[l];i++,code++){ |
620 | 372 | ogg_uint32_t orig=bitreverse(code<<(32-length)); |
621 | 744 | for(j=0;j<(1<<(c->firsttablen-length));j++){ |
622 | | /* pack the length into the table to avoid an extra lookup */ |
623 | 372 | c->firsttable[(j<<length)|orig]=i<<6|length; |
624 | 372 | } |
625 | 372 | } |
626 | 186 | code<<=1; |
627 | 186 | } |
628 | | |
629 | | /* now fill in 'unused' entries in the firsttable with length search |
630 | | hints for the non-direct hits. In this case, instead of storing the |
631 | | hi/lo entry codewords in the table, we store the (index of) the |
632 | | shortest and longest lengths of any codeword that starts with that |
633 | | prefix. */ |
634 | 186 | if(l<nlengths){ |
635 | 0 | int lo=l; |
636 | 0 | do{ |
637 | 0 | ogg_uint32_t nleft; |
638 | 0 | ogg_uint32_t slot_count; |
639 | | /* how many more codes are left of this length? */ |
640 | 0 | nleft=(c->codelist[l]>>(32-length))-code+1; |
641 | 0 | slot_count=(ogg_uint32_t)1<<(length-c->firsttablen); |
642 | 0 | if(nleft>=slot_count){ |
643 | 0 | ogg_uint32_t word=code>>(length-c->firsttablen); |
644 | 0 | c->firsttable[bitreverse(word<<(32-c->firsttablen))]= |
645 | 0 | 0x80000000UL | ((ogg_uint32_t)lo<<15) | (nlengths-1-l); |
646 | 0 | code+=slot_count; |
647 | 0 | lo=l; |
648 | 0 | }else{ |
649 | | /* not enough: consider longer codes */ |
650 | 0 | l++; |
651 | 0 | length++; |
652 | 0 | code<<=1; |
653 | 0 | if(nleft==0)lo=l; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | while(l<nlengths); |
657 | 0 | } |
658 | 186 | } |
659 | 186 | } |
660 | 858 | } |
661 | | |
662 | 858 | return(0); |
663 | 49 | err_out: |
664 | 49 | if(codes)_ogg_free(codes); |
665 | | /* our caller will clean up the partially initialized book */ |
666 | 49 | return(-1); |
667 | 858 | } |
668 | | |
669 | 0 | long vorbis_book_codeword(codebook *book,int entry){ |
670 | 0 | if(book->c) /* only use with encode; decode optimizations are |
671 | | allowed to break this */ |
672 | 0 | return book->codelist[entry]; |
673 | 0 | return -1; |
674 | 0 | } |
675 | | |
676 | 0 | long vorbis_book_codelen(codebook *book,int entry){ |
677 | 0 | if(book->c) /* only use with encode; decode optimizations are |
678 | | allowed to break this */ |
679 | 0 | return book->c->lengthlist[entry]; |
680 | 0 | return -1; |
681 | 0 | } |
682 | | |
683 | | #ifdef _V_SELFTEST |
684 | | |
685 | | /* Unit tests of the dequantizer; this stuff will be OK |
686 | | cross-platform, I simply want to be sure that special mapping cases |
687 | | actually work properly; a bug could go unnoticed for a while */ |
688 | | |
689 | | #include <stdio.h> |
690 | | |
691 | | /* cases: |
692 | | |
693 | | no mapping |
694 | | full, explicit mapping |
695 | | algorithmic mapping |
696 | | |
697 | | nonsequential |
698 | | sequential |
699 | | */ |
700 | | |
701 | | static ogg_uint16_t full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1}; |
702 | | static ogg_uint16_t partial_quantlist1[]={0,7,2}; |
703 | | |
704 | | /* no mapping */ |
705 | | dec_codebook test1={ |
706 | | 4,0,0,0,16,0,0, |
707 | | 0,0,0,0,0, |
708 | | NULL, |
709 | | NULL,NULL,NULL,NULL,NULL |
710 | | }; |
711 | | static float *test1_result=NULL; |
712 | | |
713 | | /* linear, full mapping, nonsequential */ |
714 | | dec_codebook test2={ |
715 | | 4,0,0,0,3,0,0, |
716 | | 2,4,0,3761766400U,1611661312, |
717 | | full_quantlist1, |
718 | | NULL,NULL,NULL,NULL,NULL |
719 | | }; |
720 | | static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; |
721 | | |
722 | | /* linear, full mapping, sequential */ |
723 | | dec_codebook test3={ |
724 | | 4,0,0,0,3,0,0, |
725 | | 2,4,1,3761766400U,1611661312, |
726 | | full_quantlist1, |
727 | | NULL,NULL,NULL,NULL,NULL |
728 | | }; |
729 | | static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; |
730 | | |
731 | | /* linear, algorithmic mapping, nonsequential */ |
732 | | dec_codebook test4={ |
733 | | 3,0,0,0,27,0,0, |
734 | | 1,4,0,3761766400U,1611661312, |
735 | | partial_quantlist1, |
736 | | NULL,NULL,NULL,NULL,NULL |
737 | | }; |
738 | | static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3, |
739 | | -3, 4,-3, 4, 4,-3, -1, 4,-3, |
740 | | -3,-1,-3, 4,-1,-3, -1,-1,-3, |
741 | | -3,-3, 4, 4,-3, 4, -1,-3, 4, |
742 | | -3, 4, 4, 4, 4, 4, -1, 4, 4, |
743 | | -3,-1, 4, 4,-1, 4, -1,-1, 4, |
744 | | -3,-3,-1, 4,-3,-1, -1,-3,-1, |
745 | | -3, 4,-1, 4, 4,-1, -1, 4,-1, |
746 | | -3,-1,-1, 4,-1,-1, -1,-1,-1}; |
747 | | |
748 | | /* linear, algorithmic mapping, sequential */ |
749 | | dec_codebook test5={ |
750 | | 3,0,0,0,27,0,0, |
751 | | 1,4,1,3761766400U,1611661312, |
752 | | partial_quantlist1, |
753 | | NULL,NULL,NULL,NULL,NULL, |
754 | | }; |
755 | | static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7, |
756 | | -3, 1,-2, 4, 8, 5, -1, 3, 0, |
757 | | -3,-4,-7, 4, 3, 0, -1,-2,-5, |
758 | | -3,-6,-2, 4, 1, 5, -1,-4, 0, |
759 | | -3, 1, 5, 4, 8,12, -1, 3, 7, |
760 | | -3,-4, 0, 4, 3, 7, -1,-2, 2, |
761 | | -3,-6,-7, 4, 1, 0, -1,-4,-5, |
762 | | -3, 1, 0, 4, 8, 7, -1, 3, 2, |
763 | | -3,-4,-5, 4, 3, 2, -1,-2,-3}; |
764 | | |
765 | | void run_test(dec_codebook *b,float *comp){ |
766 | | float out[3*27]; |
767 | | int i; |
768 | | |
769 | | if(comp){ |
770 | | _book_unquantize(out,b,b->entries,NULL); |
771 | | for(i=0;i<b->entries*b->dim;i++) |
772 | | if(fabs(out[i]-comp[i])>.0001){ |
773 | | fprintf(stderr,"disagreement in unquantized and reference data:\n" |
774 | | "position %d, %g != %g\n",i,out[i],comp[i]); |
775 | | exit(1); |
776 | | } |
777 | | |
778 | | } |
779 | | } |
780 | | |
781 | | int main(){ |
782 | | /* run the nine dequant tests, and compare to the hand-rolled results */ |
783 | | fprintf(stderr,"Dequant test 1... "); |
784 | | run_test(&test1,test1_result); |
785 | | fprintf(stderr,"OK\nDequant test 2... "); |
786 | | run_test(&test2,test2_result); |
787 | | fprintf(stderr,"OK\nDequant test 3... "); |
788 | | run_test(&test3,test3_result); |
789 | | fprintf(stderr,"OK\nDequant test 4... "); |
790 | | run_test(&test4,test4_result); |
791 | | fprintf(stderr,"OK\nDequant test 5... "); |
792 | | run_test(&test5,test5_result); |
793 | | fprintf(stderr,"OK\n\n"); |
794 | | |
795 | | return(0); |
796 | | } |
797 | | |
798 | | #endif |