| 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 codebook pack/unpack/code/decode operations |
| 14 |
|
| 15 |
********************************************************************/ |
| 16 |
|
| 17 |
#include "ogg.h" |
| 18 |
#include "vorbis_codec.h" |
| 19 |
#include "vorbis_codebook.h" |
| 20 |
#include "vorbis_scales.h" |
| 21 |
#include "vorbis_misc.h" |
| 22 |
#include "vorbis_os.h" |
| 23 |
|
| 24 |
/* packs the given codebook into the bitstream **************************/ |
| 25 |
|
| 26 |
int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){ |
| 27 |
long i,j; |
| 28 |
int ordered=0; |
| 29 |
|
| 30 |
/* first the basic parameters */ |
| 31 |
oggpack_write(opb,0x564342,24); |
| 32 |
oggpack_write(opb,c->dim,16); |
| 33 |
oggpack_write(opb,c->entries,24); |
| 34 |
|
| 35 |
/* pack the codewords. There are two packings; length ordered and |
| 36 |
length random. Decide between the two now. */ |
| 37 |
|
| 38 |
for(i=1;i<c->entries;i++) |
| 39 |
if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break; |
| 40 |
if(i==c->entries)ordered=1; |
| 41 |
|
| 42 |
if(ordered){ |
| 43 |
/* length ordered. We only need to say how many codewords of |
| 44 |
each length. The actual codewords are generated |
| 45 |
deterministically */ |
| 46 |
|
| 47 |
long count=0; |
| 48 |
oggpack_write(opb,1,1); /* ordered */ |
| 49 |
oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */ |
| 50 |
|
| 51 |
for(i=1;i<c->entries;i++){ |
| 52 |
char thiss=c->lengthlist[i]; |
| 53 |
char last=c->lengthlist[i-1]; |
| 54 |
if(thiss>last){ |
| 55 |
for(j=last;j<thiss;j++){ |
| 56 |
oggpack_write(opb,i-count,ov_ilog(c->entries-count)); |
| 57 |
count=i; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
oggpack_write(opb,i-count,ov_ilog(c->entries-count)); |
| 62 |
|
| 63 |
}else{ |
| 64 |
/* length random. Again, we don't code the codeword itself, just |
| 65 |
the length. This time, though, we have to encode each length */ |
| 66 |
oggpack_write(opb,0,1); /* unordered */ |
| 67 |
|
| 68 |
/* algortihmic mapping has use for 'unused entries', which we tag |
| 69 |
here. The algorithmic mapping happens as usual, but the unused |
| 70 |
entry has no codeword. */ |
| 71 |
for(i=0;i<c->entries;i++) |
| 72 |
if(c->lengthlist[i]==0)break; |
| 73 |
|
| 74 |
if(i==c->entries){ |
| 75 |
oggpack_write(opb,0,1); /* no unused entries */ |
| 76 |
for(i=0;i<c->entries;i++) |
| 77 |
oggpack_write(opb,c->lengthlist[i]-1,5); |
| 78 |
}else{ |
| 79 |
oggpack_write(opb,1,1); /* we have unused entries; thus we tag */ |
| 80 |
for(i=0;i<c->entries;i++){ |
| 81 |
if(c->lengthlist[i]==0){ |
| 82 |
oggpack_write(opb,0,1); |
| 83 |
}else{ |
| 84 |
oggpack_write(opb,1,1); |
| 85 |
oggpack_write(opb,c->lengthlist[i]-1,5); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* is the entry number the desired return value, or do we have a |
| 92 |
mapping? If we have a mapping, what type? */ |
| 93 |
oggpack_write(opb,c->maptype,4); |
| 94 |
switch(c->maptype){ |
| 95 |
case 0: |
| 96 |
/* no mapping */ |
| 97 |
break; |
| 98 |
case 1:case 2: |
| 99 |
/* implicitly populated value mapping */ |
| 100 |
/* explicitly populated value mapping */ |
| 101 |
|
| 102 |
if(!c->quantlist){ |
| 103 |
/* no quantlist? error */ |
| 104 |
return(-1); |
| 105 |
} |
| 106 |
|
| 107 |
/* values that define the dequantization */ |
| 108 |
oggpack_write(opb,c->q_min,32); |
| 109 |
oggpack_write(opb,c->q_delta,32); |
| 110 |
oggpack_write(opb,c->q_quant-1,4); |
| 111 |
oggpack_write(opb,c->q_sequencep,1); |
| 112 |
|
| 113 |
{ |
| 114 |
int quantvals; |
| 115 |
switch(c->maptype){ |
| 116 |
case 1: |
| 117 |
/* a single column of (c->entries/c->dim) quantized values for |
| 118 |
building a full value list algorithmically (square lattice) */ |
| 119 |
quantvals=_book_maptype1_quantvals(c); |
| 120 |
break; |
| 121 |
case 2: |
| 122 |
/* every value (c->entries*c->dim total) specified explicitly */ |
| 123 |
quantvals=c->entries*c->dim; |
| 124 |
break; |
| 125 |
default: /* NOT_REACHABLE */ |
| 126 |
quantvals=-1; |
| 127 |
} |
| 128 |
|
| 129 |
/* quantized values */ |
| 130 |
for(i=0;i<quantvals;i++) |
| 131 |
oggpack_write(opb,labs(c->quantlist[i]),c->q_quant); |
| 132 |
|
| 133 |
} |
| 134 |
break; |
| 135 |
default: |
| 136 |
/* error case; we don't have any other map types now */ |
| 137 |
return(-1); |
| 138 |
} |
| 139 |
|
| 140 |
return(0); |
| 141 |
} |
| 142 |
|
| 143 |
/* unpacks a codebook from the packet buffer into the codebook struct, |
| 144 |
readies the codebook auxiliary structures for decode *************/ |
| 145 |
static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){ |
| 146 |
long i,j; |
| 147 |
static_codebook *s = (static_codebook *) _Melder_calloc(1,sizeof(*s)); |
| 148 |
s->allocedp=1; |
| 149 |
|
| 150 |
/* make sure alignment is correct */ |
| 151 |
if(oggpack_read(opb,24)!=0x564342)goto _eofout; |
| 152 |
|
| 153 |
/* first the basic parameters */ |
| 154 |
s->dim=oggpack_read(opb,16); |
| 155 |
s->entries=oggpack_read(opb,24); |
| 156 |
if(s->entries==-1)goto _eofout; |
| 157 |
|
| 158 |
if(ov_ilog(s->dim)+ov_ilog(s->entries)>24)goto _eofout; |
| 159 |
|
| 160 |
/* codeword ordering.... length ordered or unordered? */ |
| 161 |
switch((int)oggpack_read(opb,1)){ |
| 162 |
case 0:{ |
| 163 |
long unused; |
| 164 |
/* allocated but unused entries? */ |
| 165 |
unused=oggpack_read(opb,1); |
| 166 |
if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb)) |
| 167 |
goto _eofout; |
| 168 |
/* unordered */ |
| 169 |
s->lengthlist = (char *) _Melder_malloc(sizeof(*s->lengthlist)*s->entries); |
| 170 |
|
| 171 |
/* allocated but unused entries? */ |
| 172 |
if(unused){ |
| 173 |
/* yes, unused entries */ |
| 174 |
|
| 175 |
for(i=0;i<s->entries;i++){ |
| 176 |
if(oggpack_read(opb,1)){ |
| 177 |
long num=oggpack_read(opb,5); |
| 178 |
if(num==-1)goto _eofout; |
| 179 |
s->lengthlist[i]=num+1; |
| 180 |
}else |
| 181 |
s->lengthlist[i]=0; |
| 182 |
} |
| 183 |
}else{ |
| 184 |
/* all entries used; no tagging */ |
| 185 |
for(i=0;i<s->entries;i++){ |
| 186 |
long num=oggpack_read(opb,5); |
| 187 |
if(num==-1)goto _eofout; |
| 188 |
s->lengthlist[i]=num+1; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
break; |
| 193 |
} |
| 194 |
case 1: |
| 195 |
/* ordered */ |
| 196 |
{ |
| 197 |
long length=oggpack_read(opb,5)+1; |
| 198 |
if(length==0)goto _eofout; |
| 199 |
s->lengthlist = (char *) _Melder_malloc(sizeof(*s->lengthlist)*s->entries); |
| 200 |
|
| 201 |
for(i=0;i<s->entries;){ |
| 202 |
long num=oggpack_read(opb,ov_ilog(s->entries-i)); |
| 203 |
if(num==-1)goto _eofout; |
| 204 |
if(length>32 || num>s->entries-i || |
| 205 |
(num>0 && (num-1)>>(length-1)>1)){ |
| 206 |
goto _errout; |
| 207 |
} |
| 208 |
if(length>32)goto _errout; |
| 209 |
for(j=0;j<num;j++,i++) |
| 210 |
s->lengthlist[i]=length; |
| 211 |
length++; |
| 212 |
} |
| 213 |
} |
| 214 |
break; |
| 215 |
default: |
| 216 |
/* EOF */ |
| 217 |
goto _eofout; |
| 218 |
} |
| 219 |
|
| 220 |
/* Do we have a mapping to unpack? */ |
| 221 |
switch((s->maptype=oggpack_read(opb,4))){ |
| 222 |
case 0: |
| 223 |
/* no mapping */ |
| 224 |
break; |
| 225 |
case 1: case 2: |
| 226 |
/* implicitly populated value mapping */ |
| 227 |
/* explicitly populated value mapping */ |
| 228 |
|
| 229 |
s->q_min=oggpack_read(opb,32); |
| 230 |
s->q_delta=oggpack_read(opb,32); |
| 231 |
s->q_quant=oggpack_read(opb,4)+1; |
| 232 |
s->q_sequencep=oggpack_read(opb,1); |
| 233 |
if(s->q_sequencep==-1)goto _eofout; |
| 234 |
|
| 235 |
{ |
| 236 |
int quantvals=0; |
| 237 |
switch(s->maptype){ |
| 238 |
case 1: |
| 239 |
quantvals=(s->dim==0?0:_book_maptype1_quantvals(s)); |
| 240 |
break; |
| 241 |
case 2: |
| 242 |
quantvals=s->entries*s->dim; |
| 243 |
break; |
| 244 |
} |
| 245 |
|
| 246 |
/* quantized values */ |
| 247 |
if(((quantvals*s->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb)) |
| 248 |
goto _eofout; |
| 249 |
s->quantlist= (long *) _Melder_malloc(sizeof(*s->quantlist)*quantvals); |
| 250 |
for(i=0;i<quantvals;i++) |
| 251 |
s->quantlist[i]=oggpack_read(opb,s->q_quant); |
| 252 |
|
| 253 |
if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout; |
| 254 |
} |
| 255 |
break; |
| 256 |
default: |
| 257 |
goto _errout; |
| 258 |
} |
| 259 |
|
| 260 |
/* all set */ |
| 261 |
return(s); |
| 262 |
|
| 263 |
_errout: |
| 264 |
_eofout: |
| 265 |
vorbis_staticbook_destroy(s); |
| 266 |
return(nullptr); |
| 267 |
} |
| 268 |
|
| 269 |
/* returns the number of bits ************************************************/ |
| 270 |
int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){ |
| 271 |
if(a<0 || a>=book->c->entries)return(0); |
| 272 |
oggpack_write(b,book->codelist[a],book->c->lengthlist[a]); |
| 273 |
return(book->c->lengthlist[a]); |
| 274 |
} |
| 275 |
|
| 276 |
/* the 'eliminate the decode tree' optimization actually requires the |
| 277 |
codewords to be MSb first, not LSb. This is an annoying inelegancy |
| 278 |
(and one of the first places where carefully thought out design |
| 279 |
turned out to be wrong; Vorbis II and future Ogg codecs should go |
| 280 |
to an MSb bitpacker), but not actually the huge hit it appears to |
| 281 |
be. The first-stage decode table catches most words so that |
| 282 |
bitreverse is not in the main execution path. */ |
| 283 |
|
| 284 |
static ogg_uint32_t bitreverse(ogg_uint32_t x){ |
| 285 |
x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000); |
| 286 |
x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00); |
| 287 |
x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0); |
| 288 |
x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc); |
| 289 |
return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa); |
| 290 |
} |
| 291 |
|
| 292 |
inline long decode_packed_entry_number(codebook *book, oggpack_buffer *b){ |
| 293 |
int read=book->dec_maxlength; |
| 294 |
long lo,hi; |
| 295 |
long lok = oggpack_look(b,book->dec_firsttablen); |
| 296 |
|
| 297 |
if (lok >= 0) { |
| 298 |
long entry = book->dec_firsttable[lok]; |
| 299 |
if(entry&0x80000000UL){ |
| 300 |
lo=(entry>>15)&0x7fff; |
| 301 |
hi=book->used_entries-(entry&0x7fff); |
| 302 |
}else{ |
| 303 |
oggpack_adv(b, book->dec_codelengths[entry-1]); |
| 304 |
return(entry-1); |
| 305 |
} |
| 306 |
}else{ |
| 307 |
lo=0; |
| 308 |
hi=book->used_entries; |
| 309 |
} |
| 310 |
|
| 311 |
/* Single entry codebooks use a firsttablen of 1 and a |
| 312 |
dec_maxlength of 1. If a single-entry codebook gets here (due to |
| 313 |
failure to read one bit above), the next look attempt will also |
| 314 |
fail and we'll correctly kick out instead of trying to walk the |
| 315 |
underformed tree */ |
| 316 |
|
| 317 |
lok = oggpack_look(b, read); |
| 318 |
|
| 319 |
while(lok<0 && read>1) |
| 320 |
lok = oggpack_look(b, --read); |
| 321 |
if(lok<0)return -1; |
| 322 |
|
| 323 |
/* bisect search for the codeword in the ordered list */ |
| 324 |
{ |
| 325 |
ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok); |
| 326 |
|
| 327 |
while(hi-lo>1){ |
| 328 |
long p=(hi-lo)>>1; |
| 329 |
long test=book->codelist[lo+p]>testword; |
| 330 |
lo+=p&(test-1); |
| 331 |
hi-=p&(-test); |
| 332 |
} |
| 333 |
|
| 334 |
if(book->dec_codelengths[lo]<=read){ |
| 335 |
oggpack_adv(b, book->dec_codelengths[lo]); |
| 336 |
return(lo); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
oggpack_adv(b, read); |
| 341 |
|
| 342 |
return(-1); |
| 343 |
} |
| 344 |
|
| 345 |
/* Decode side is specced and easier, because we don't need to find |
| 346 |
matches using different criteria; we simply read and map. There are |
| 347 |
two things we need to do 'depending': |
| 348 |
|
| 349 |
We may need to support interleave. We don't really, but it's |
| 350 |
convenient to do it here rather than rebuild the vector later. |
| 351 |
|
| 352 |
Cascades may be additive or multiplicitive; this is not inherent in |
| 353 |
the codebook, but set in the code using the codebook. Like |
| 354 |
interleaving, it's easiest to do it here. |
| 355 |
addmul==0 -> declarative (set the value) |
| 356 |
addmul==1 -> additive |
| 357 |
addmul==2 -> multiplicitive */ |
| 358 |
|
| 359 |
/* returns the [original, not compacted] entry number or -1 on eof *********/ |
| 360 |
long vorbis_book_decode(codebook *book, oggpack_buffer *b){ |
| 361 |
if(book->used_entries>0){ |
| 362 |
long packed_entry=decode_packed_entry_number(book,b); |
| 363 |
if(packed_entry>=0) |
| 364 |
return(book->dec_index[packed_entry]); |
| 365 |
} |
| 366 |
|
| 367 |
/* if there's no dec_index, the codebook unpacking isn't collapsed */ |
| 368 |
return(-1); |
| 369 |
} |
| 370 |
|
| 371 |
/* returns 0 on OK or -1 on eof *************************************/ |
| 372 |
/* decode vector / dim granularity gaurding is done in the upper layer */ |
| 373 |
long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){ |
| 374 |
if(book->used_entries>0){ |
| 375 |
int step=n/book->dim; |
| 376 |
long *entry = (long *) alloca(sizeof(*entry)*step); |
| 377 |
float **t = (float **) alloca(sizeof(*t)*step); |
| 378 |
int i,j,o; |
| 379 |
|
| 380 |
for (i = 0; i < step; i++) { |
| 381 |
entry[i]=decode_packed_entry_number(book,b); |
| 382 |
if(entry[i]==-1)return(-1); |
| 383 |
t[i] = book->valuelist+entry[i]*book->dim; |
| 384 |
} |
| 385 |
for(i=0,o=0;i<book->dim;i++,o+=step) |
| 386 |
for (j=0;o+j<n && j<step;j++) |
| 387 |
a[o+j]+=t[j][i]; |
| 388 |
} |
| 389 |
return(0); |
| 390 |
} |
| 391 |
|
| 392 |
/* decode vector / dim granularity gaurding is done in the upper layer */ |
| 393 |
long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){ |
| 394 |
if(book->used_entries>0){ |
| 395 |
int i,j,entry; |
| 396 |
float *t; |
| 397 |
|
| 398 |
for(i=0;i<n;){ |
| 399 |
entry = decode_packed_entry_number(book,b); |
| 400 |
if(entry==-1)return(-1); |
| 401 |
t = book->valuelist+entry*book->dim; |
| 402 |
for(j=0;i<n && j<book->dim;) |
| 403 |
a[i++]+=t[j++]; |
| 404 |
} |
| 405 |
} |
| 406 |
return(0); |
| 407 |
} |
| 408 |
|
| 409 |
/* unlike the others, we guard against n not being an integer number |
| 410 |
of <dim> internally rather than in the upper layer (called only by |
| 411 |
floor0) */ |
| 412 |
long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){ |
| 413 |
if(book->used_entries>0){ |
| 414 |
int i,j,entry; |
| 415 |
float *t; |
| 416 |
|
| 417 |
for(i=0;i<n;){ |
| 418 |
entry = decode_packed_entry_number(book,b); |
| 419 |
if(entry==-1)return(-1); |
| 420 |
t = book->valuelist+entry*book->dim; |
| 421 |
for (j=0;i<n && j<book->dim;){ |
| 422 |
a[i++]=t[j++]; |
| 423 |
} |
| 424 |
} |
| 425 |
}else{ |
| 426 |
int i; |
| 427 |
|
| 428 |
for(i=0;i<n;){ |
| 429 |
a[i++]=0.f; |
| 430 |
} |
| 431 |
} |
| 432 |
return(0); |
| 433 |
} |
| 434 |
|
| 435 |
long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch, |
| 436 |
oggpack_buffer *b,int n){ |
| 437 |
|
| 438 |
long i,j,entry; |
| 439 |
int chptr=0; |
| 440 |
if(book->used_entries>0){ |
| 441 |
int m=(offset+n)/ch; |
| 442 |
for(i=offset/ch;i<m;){ |
| 443 |
entry = decode_packed_entry_number(book,b); |
| 444 |
if(entry==-1)return(-1); |
| 445 |
{ |
| 446 |
const float *t = book->valuelist+entry*book->dim; |
| 447 |
for (j=0;i<m && j<book->dim;j++){ |
| 448 |
a[chptr++][i]+=t[j]; |
| 449 |
if(chptr==ch){ |
| 450 |
chptr=0; |
| 451 |
i++; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
return(0); |
| 458 |
} |