forked from mirrors/gecko-dev
Backed out changeset 61356e1447e3 (bug 1823953) Backed out changeset 85785505b6d6 (bug 1823953) Backed out changeset 46a61cbfe8a8 (bug 1833654) Backed out changeset 83e3de80337b (bug 1833654) Backed out changeset 1a10c12874ac (bug 1840399) Backed out changeset 6b087145b67f (bug 1833654) Backed out changeset b9ac857ad43f (bug 1840399) Backed out changeset 4b841e8dd033 (bug 1823953) Backed out changeset 650e35803834 (bug 1823953) Backed out changeset c11b58ac0709 (bug 1823953) Backed out changeset c0249c90bc31 (bug 1823953) Backed out changeset 8929288d5aec (bug 1823953) Backed out changeset 828792b886bd (bug 1823953) Backed out changeset 873f1d4a8875 (bug 1840869) Backed out changeset a25abd05302c (bug 1823953) Backed out changeset d4b1eb442c36 (bug 1840399) Backed out changeset c25509d72a96 (bug 1840399) Backed out changeset 0f72a0626a28 (bug 1840402) Backed out changeset 82e7574364ce (bug 1840399) Backed out changeset 93073105f063 (bug 1840399) Backed out changeset 56ec8e3405e9 (bug 1840399) Backed out changeset ff15dad37ab8 (bug 1840399) Backed out changeset 0655ebd61eda (bug 1840399) Backed out changeset 7bca1ae06c7d (bug 1828912) Backed out changeset 8a5a849cfe5f (bug 1828912) Backed out changeset 3d8422a2038a (bug 1828912) Backed out changeset f08ee5de9370 (bug 1823953) Backed out changeset a4eb210620ff (bug 1823953) Backed out changeset aa8914cd55be (bug 1839391) Backed out changeset 3ea1f43e4024 (bug 1823953) Backed out changeset 3efe02ffa1c8 (bug 1826382) Backed out changeset 81c4553ec23d (bug 1839391) Backed out changeset 130894e4a781 (bug 1839391) Backed out changeset 9a0247b0fc85 (bug 1839391) Backed out changeset 11a923064382 (bug 1839391) Backed out changeset 98ffb66160c3 (bug 1837160) Backed out changeset a80dda9a220a (bug 1837160) Backed out changeset 251b4ef97a2b (bug 1837160) Backed out changeset 7372632eb32f (bug 1837160) Backed out changeset c5d54bc3ee26 (bug 1839389) Backed out changeset b232ec1bbc2d (bug 1833654) Backed out changeset fc7ba125c2fe (bug 1833654) Backed out changeset 8a47f6882e61 (bug 1823953) Backed out changeset e29810541b53 (bug 1828912) Backed out changeset bcf10730c8c9 (bug 1828912) Backed out changeset 8df8290b6c33 (bug 1826382) Backed out changeset 2811d12803cf (bug 1826382) Backed out changeset 3fc718561ec9 (bug 1826382) Backed out changeset 7827183776e1 (bug 1823953) Backed out changeset a3eb5f228d9a (bug 1826382) Backed out changeset 3113ad2e0987 (bug 1823953) Backed out changeset 4b1dc01525af (bug 1823953) Backed out changeset f7f4a7585ceb (bug 1823953) Backed out changeset 93042f1becec (bug 1823953) Backed out changeset b9ca30a0a066 (bug 1823953) Backed out changeset 1000c4a6a92a (bug 1823953) Backed out changeset 05dc13775fd6 (bug 1823953)
391 lines
10 KiB
C
391 lines
10 KiB
C
/********************************************************************
|
|
* *
|
|
* THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
|
|
* *
|
|
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
|
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
|
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
|
* *
|
|
* THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
|
* BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
|
|
* *
|
|
********************************************************************
|
|
|
|
function: basic codebook pack/unpack/code/decode operations
|
|
|
|
********************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <ogg/ogg.h>
|
|
#include "ivorbiscodec.h"
|
|
#include "codebook.h"
|
|
#include "misc.h"
|
|
|
|
/* unpacks a codebook from the packet buffer into the codebook struct,
|
|
readies the codebook auxiliary structures for decode *************/
|
|
static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){
|
|
long i,j;
|
|
static_codebook *s=_ogg_calloc(1,sizeof(*s));
|
|
|
|
/* make sure alignment is correct */
|
|
if(oggpack_read(opb,24)!=0x564342)goto _eofout;
|
|
|
|
/* first the basic parameters */
|
|
s->dim=oggpack_read(opb,16);
|
|
s->entries=oggpack_read(opb,24);
|
|
if(s->entries==-1)goto _eofout;
|
|
|
|
if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
|
|
|
|
/* codeword ordering.... length ordered or unordered? */
|
|
switch((int)oggpack_read(opb,1)){
|
|
case 0:{
|
|
long unused;
|
|
/* allocated but unused entries? */
|
|
unused=oggpack_read(opb,1);
|
|
if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
|
|
goto _eofout;
|
|
/* unordered */
|
|
s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
|
|
|
|
/* allocated but unused entries? */
|
|
if(unused){
|
|
/* yes, unused entries */
|
|
|
|
for(i=0;i<s->entries;i++){
|
|
if(oggpack_read(opb,1)){
|
|
long num=oggpack_read(opb,5);
|
|
if(num==-1)goto _eofout;
|
|
s->lengthlist[i]=num+1;
|
|
}else
|
|
s->lengthlist[i]=0;
|
|
}
|
|
}else{
|
|
/* all entries used; no tagging */
|
|
for(i=0;i<s->entries;i++){
|
|
long num=oggpack_read(opb,5);
|
|
if(num==-1)goto _eofout;
|
|
s->lengthlist[i]=num+1;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case 1:
|
|
/* ordered */
|
|
{
|
|
long length=oggpack_read(opb,5)+1;
|
|
if(length==0)goto _eofout;
|
|
s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
|
|
|
|
for(i=0;i<s->entries;){
|
|
long num=oggpack_read(opb,_ilog(s->entries-i));
|
|
if(num==-1)goto _eofout;
|
|
if(length>32 || num>s->entries-i ||
|
|
(num>0 && (num-1)>>(length>>1)>>((length+1)>>1))>0){
|
|
goto _errout;
|
|
}
|
|
for(j=0;j<num;j++,i++)
|
|
s->lengthlist[i]=length;
|
|
length++;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
/* EOF */
|
|
goto _eofout;
|
|
}
|
|
|
|
/* Do we have a mapping to unpack? */
|
|
switch((s->maptype=oggpack_read(opb,4))){
|
|
case 0:
|
|
/* no mapping */
|
|
break;
|
|
case 1: case 2:
|
|
/* implicitly populated value mapping */
|
|
/* explicitly populated value mapping */
|
|
|
|
s->q_min=oggpack_read(opb,32);
|
|
s->q_delta=oggpack_read(opb,32);
|
|
s->q_quant=oggpack_read(opb,4)+1;
|
|
s->q_sequencep=oggpack_read(opb,1);
|
|
if(s->q_sequencep==-1)goto _eofout;
|
|
|
|
{
|
|
int quantvals=0;
|
|
switch(s->maptype){
|
|
case 1:
|
|
quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
|
|
break;
|
|
case 2:
|
|
quantvals=s->entries*s->dim;
|
|
break;
|
|
}
|
|
|
|
/* quantized values */
|
|
if((quantvals*s->q_quant+7)>>3>opb->storage-oggpack_bytes(opb))
|
|
goto _eofout;
|
|
s->quantlist=(long *)_ogg_malloc(sizeof(*s->quantlist)*quantvals);
|
|
for(i=0;i<quantvals;i++)
|
|
s->quantlist[i]=oggpack_read(opb,s->q_quant);
|
|
|
|
if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
|
|
}
|
|
break;
|
|
default:
|
|
goto _errout;
|
|
}
|
|
|
|
/* all set */
|
|
return(s);
|
|
|
|
_errout:
|
|
_eofout:
|
|
vorbis_staticbook_destroy(s);
|
|
return(NULL);
|
|
}
|
|
|
|
/* the 'eliminate the decode tree' optimization actually requires the
|
|
codewords to be MSb first, not LSb. This is an annoying inelegancy
|
|
(and one of the first places where carefully thought out design
|
|
turned out to be wrong; Vorbis II and future Ogg codecs should go
|
|
to an MSb bitpacker), but not actually the huge hit it appears to
|
|
be. The first-stage decode table catches most words so that
|
|
bitreverse is not in the main execution path. */
|
|
|
|
static ogg_uint32_t bitreverse(ogg_uint32_t x){
|
|
x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
|
|
x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
|
|
x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
|
|
x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
|
|
return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
|
|
}
|
|
|
|
STIN long decode_packed_entry_number(codebook *book,
|
|
oggpack_buffer *b){
|
|
int read=book->dec_maxlength;
|
|
long lo,hi;
|
|
long lok = oggpack_look(b,book->dec_firsttablen);
|
|
|
|
if (lok >= 0) {
|
|
long entry = book->dec_firsttable[lok];
|
|
if(entry&0x80000000UL){
|
|
lo=(entry>>15)&0x7fff;
|
|
hi=book->used_entries-(entry&0x7fff);
|
|
}else{
|
|
oggpack_adv(b, book->dec_codelengths[entry-1]);
|
|
return(entry-1);
|
|
}
|
|
}else{
|
|
lo=0;
|
|
hi=book->used_entries;
|
|
}
|
|
|
|
lok = oggpack_look(b, read);
|
|
|
|
while(lok<0 && read>1)
|
|
lok = oggpack_look(b, --read);
|
|
|
|
if(lok<0){
|
|
oggpack_adv(b,1); /* force eop */
|
|
return -1;
|
|
}
|
|
|
|
/* bisect search for the codeword in the ordered list */
|
|
{
|
|
ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);
|
|
|
|
while(hi-lo>1){
|
|
long p=(hi-lo)>>1;
|
|
long test=book->codelist[lo+p]>testword;
|
|
lo+=p&(test-1);
|
|
hi-=p&(-test);
|
|
}
|
|
|
|
if(book->dec_codelengths[lo]<=read){
|
|
oggpack_adv(b, book->dec_codelengths[lo]);
|
|
return(lo);
|
|
}
|
|
}
|
|
|
|
oggpack_adv(b, read+1);
|
|
return(-1);
|
|
}
|
|
|
|
/* Decode side is specced and easier, because we don't need to find
|
|
matches using different criteria; we simply read and map. There are
|
|
two things we need to do 'depending':
|
|
|
|
We may need to support interleave. We don't really, but it's
|
|
convenient to do it here rather than rebuild the vector later.
|
|
|
|
Cascades may be additive or multiplicitive; this is not inherent in
|
|
the codebook, but set in the code using the codebook. Like
|
|
interleaving, it's easiest to do it here.
|
|
addmul==0 -> declarative (set the value)
|
|
addmul==1 -> additive
|
|
addmul==2 -> multiplicitive */
|
|
|
|
/* returns the [original, not compacted] entry number or -1 on eof *********/
|
|
long vorbis_book_decode(codebook *book, oggpack_buffer *b){
|
|
if(book->used_entries>0){
|
|
long packed_entry=decode_packed_entry_number(book,b);
|
|
if(packed_entry>=0)
|
|
return(book->dec_index[packed_entry]);
|
|
}
|
|
|
|
/* if there's no dec_index, the codebook unpacking isn't collapsed */
|
|
return(-1);
|
|
}
|
|
|
|
/* returns 0 on OK or -1 on eof *************************************/
|
|
/* decode vector / dim granularity gaurding is done in the upper layer */
|
|
long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
|
|
oggpack_buffer *b,int n,int point){
|
|
if(book->used_entries>0){
|
|
int step=n/book->dim;
|
|
long *entry = (long *)alloca(sizeof(*entry)*step);
|
|
ogg_int32_t **t = (ogg_int32_t **)alloca(sizeof(*t)*step);
|
|
int i,j,o;
|
|
int shift=point-book->binarypoint;
|
|
|
|
if(shift>=0){
|
|
for (i = 0; i < step; i++) {
|
|
entry[i]=decode_packed_entry_number(book,b);
|
|
if(entry[i]==-1)return(-1);
|
|
t[i] = book->valuelist+entry[i]*book->dim;
|
|
}
|
|
for(i=0,o=0;i<book->dim;i++,o+=step)
|
|
for (j=0;o+j<n && j<step;j++)
|
|
a[o+j]+=t[j][i]>>shift;
|
|
}else{
|
|
for (i = 0; i < step; i++) {
|
|
entry[i]=decode_packed_entry_number(book,b);
|
|
if(entry[i]==-1)return(-1);
|
|
t[i] = book->valuelist+entry[i]*book->dim;
|
|
}
|
|
for(i=0,o=0;i<book->dim;i++,o+=step)
|
|
for (j=0;o+j<n && j<step;j++)
|
|
a[o+j]+=t[j][i]<<-shift;
|
|
}
|
|
}
|
|
return(0);
|
|
}
|
|
|
|
/* decode vector / dim granularity gaurding is done in the upper layer */
|
|
long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
|
|
oggpack_buffer *b,int n,int point){
|
|
if(book->used_entries>0){
|
|
int i,j,entry;
|
|
ogg_int32_t *t;
|
|
int shift=point-book->binarypoint;
|
|
|
|
if(shift>=0){
|
|
for(i=0;i<n;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<n && j<book->dim;)
|
|
a[i++]+=t[j++]>>shift;
|
|
}
|
|
}else{
|
|
for(i=0;i<n;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<n && j<book->dim;)
|
|
a[i++]+=t[j++]<<-shift;
|
|
}
|
|
}
|
|
}
|
|
return(0);
|
|
}
|
|
|
|
/* unlike the others, we guard against n not being an integer number
|
|
of <dim> internally rather than in the upper layer (called only by
|
|
floor0) */
|
|
long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
|
|
oggpack_buffer *b,int n,int point){
|
|
if(book->used_entries>0){
|
|
int i,j,entry;
|
|
ogg_int32_t *t;
|
|
int shift=point-book->binarypoint;
|
|
|
|
if(shift>=0){
|
|
|
|
for(i=0;i<n;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<n && j<book->dim;){
|
|
a[i++]=t[j++]>>shift;
|
|
}
|
|
}
|
|
}else{
|
|
|
|
for(i=0;i<n;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<n && j<book->dim;){
|
|
a[i++]=t[j++]<<-shift;
|
|
}
|
|
}
|
|
}
|
|
}else{
|
|
|
|
int i,j;
|
|
for(i=0;i<n;){
|
|
a[i++]=0;
|
|
}
|
|
}
|
|
return(0);
|
|
}
|
|
|
|
/* decode vector / dim granularity gaurding is done in the upper layer */
|
|
long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,\
|
|
long offset,int ch,
|
|
oggpack_buffer *b,int n,int point){
|
|
if(book->used_entries>0){
|
|
long i,j,entry;
|
|
int chptr=0;
|
|
int shift=point-book->binarypoint;
|
|
int m=offset+n;
|
|
if(shift>=0){
|
|
|
|
for(i=offset;i<m;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
{
|
|
const ogg_int32_t *t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<m && j<book->dim;j++){
|
|
a[chptr++][i]+=t[j]>>shift;
|
|
if(chptr==ch){
|
|
chptr=0;
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}else{
|
|
|
|
for(i=offset;i<m;){
|
|
entry = decode_packed_entry_number(book,b);
|
|
if(entry==-1)return(-1);
|
|
{
|
|
const ogg_int32_t *t = book->valuelist+entry*book->dim;
|
|
for (j=0;i<m && j<book->dim;j++){
|
|
a[chptr++][i]+=t[j]<<-shift;
|
|
if(chptr==ch){
|
|
chptr=0;
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return(0);
|
|
}
|