media/libtremor/lib/tremor_codebook.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libtremor/lib/tremor_codebook.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,391 @@
     1.4 +/********************************************************************
     1.5 + *                                                                  *
     1.6 + * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
     1.7 + *                                                                  *
     1.8 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     1.9 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
    1.10 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    1.11 + *                                                                  *
    1.12 + * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
    1.13 + * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
    1.14 + *                                                                  *
    1.15 + ********************************************************************
    1.16 +
    1.17 + function: basic codebook pack/unpack/code/decode operations
    1.18 +
    1.19 + ********************************************************************/
    1.20 +
    1.21 +#include <stdlib.h>
    1.22 +#include <string.h>
    1.23 +#include <math.h>
    1.24 +#include <ogg/ogg.h>
    1.25 +#include "ivorbiscodec.h"
    1.26 +#include "codebook.h"
    1.27 +#include "misc.h"
    1.28 +
    1.29 +/* unpacks a codebook from the packet buffer into the codebook struct,
    1.30 +   readies the codebook auxiliary structures for decode *************/
    1.31 +static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){
    1.32 +  long i,j;
    1.33 +  static_codebook *s=_ogg_calloc(1,sizeof(*s));
    1.34 +
    1.35 +  /* make sure alignment is correct */
    1.36 +  if(oggpack_read(opb,24)!=0x564342)goto _eofout;
    1.37 +
    1.38 +  /* first the basic parameters */
    1.39 +  s->dim=oggpack_read(opb,16);
    1.40 +  s->entries=oggpack_read(opb,24);
    1.41 +  if(s->entries==-1)goto _eofout;
    1.42 +
    1.43 +  if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
    1.44 +
    1.45 +  /* codeword ordering.... length ordered or unordered? */
    1.46 +  switch((int)oggpack_read(opb,1)){
    1.47 +  case 0:{
    1.48 +    long unused;
    1.49 +    /* allocated but unused entries? */
    1.50 +    unused=oggpack_read(opb,1);
    1.51 +    if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
    1.52 +      goto _eofout;
    1.53 +    /* unordered */
    1.54 +    s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
    1.55 +
    1.56 +    /* allocated but unused entries? */
    1.57 +    if(unused){
    1.58 +      /* yes, unused entries */
    1.59 +
    1.60 +      for(i=0;i<s->entries;i++){
    1.61 +	if(oggpack_read(opb,1)){
    1.62 +	  long num=oggpack_read(opb,5);
    1.63 +	  if(num==-1)goto _eofout;
    1.64 +	  s->lengthlist[i]=num+1;
    1.65 +	}else
    1.66 +	  s->lengthlist[i]=0;
    1.67 +      }
    1.68 +    }else{
    1.69 +      /* all entries used; no tagging */
    1.70 +      for(i=0;i<s->entries;i++){
    1.71 +	long num=oggpack_read(opb,5);
    1.72 +	if(num==-1)goto _eofout;
    1.73 +	s->lengthlist[i]=num+1;
    1.74 +      }
    1.75 +    }
    1.76 +    
    1.77 +    break;
    1.78 +  }
    1.79 +  case 1:
    1.80 +    /* ordered */
    1.81 +    {
    1.82 +      long length=oggpack_read(opb,5)+1;
    1.83 +      if(length==0)goto _eofout;
    1.84 +      s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
    1.85 +
    1.86 +      for(i=0;i<s->entries;){
    1.87 +	long num=oggpack_read(opb,_ilog(s->entries-i));
    1.88 +	if(num==-1)goto _eofout;
    1.89 +	if(length>32 || num>s->entries-i ||
    1.90 +	   (num>0 && (num-1)>>(length>>1)>>((length+1)>>1))>0){
    1.91 +	  goto _errout;
    1.92 +	}
    1.93 +	for(j=0;j<num;j++,i++)
    1.94 +	  s->lengthlist[i]=length;
    1.95 +	length++;
    1.96 +      }
    1.97 +    }
    1.98 +    break;
    1.99 +  default:
   1.100 +    /* EOF */
   1.101 +    goto _eofout;
   1.102 +  }
   1.103 +  
   1.104 +  /* Do we have a mapping to unpack? */
   1.105 +  switch((s->maptype=oggpack_read(opb,4))){
   1.106 +  case 0:
   1.107 +    /* no mapping */
   1.108 +    break;
   1.109 +  case 1: case 2:
   1.110 +    /* implicitly populated value mapping */
   1.111 +    /* explicitly populated value mapping */
   1.112 +
   1.113 +    s->q_min=oggpack_read(opb,32);
   1.114 +    s->q_delta=oggpack_read(opb,32);
   1.115 +    s->q_quant=oggpack_read(opb,4)+1;
   1.116 +    s->q_sequencep=oggpack_read(opb,1);
   1.117 +    if(s->q_sequencep==-1)goto _eofout;
   1.118 +
   1.119 +    {
   1.120 +      int quantvals=0;
   1.121 +      switch(s->maptype){
   1.122 +      case 1:
   1.123 +	quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
   1.124 +	break;
   1.125 +      case 2:
   1.126 +	quantvals=s->entries*s->dim;
   1.127 +	break;
   1.128 +      }
   1.129 +      
   1.130 +      /* quantized values */
   1.131 +      if((quantvals*s->q_quant+7)>>3>opb->storage-oggpack_bytes(opb))
   1.132 +        goto _eofout;
   1.133 +      s->quantlist=(long *)_ogg_malloc(sizeof(*s->quantlist)*quantvals);
   1.134 +      for(i=0;i<quantvals;i++)
   1.135 +	s->quantlist[i]=oggpack_read(opb,s->q_quant);
   1.136 +      
   1.137 +      if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
   1.138 +    }
   1.139 +    break;
   1.140 +  default:
   1.141 +    goto _errout;
   1.142 +  }
   1.143 +
   1.144 +  /* all set */
   1.145 +  return(s);
   1.146 +  
   1.147 + _errout:
   1.148 + _eofout:
   1.149 +  vorbis_staticbook_destroy(s);
   1.150 +  return(NULL); 
   1.151 +}
   1.152 +
   1.153 +/* the 'eliminate the decode tree' optimization actually requires the
   1.154 +   codewords to be MSb first, not LSb.  This is an annoying inelegancy
   1.155 +   (and one of the first places where carefully thought out design
   1.156 +   turned out to be wrong; Vorbis II and future Ogg codecs should go
   1.157 +   to an MSb bitpacker), but not actually the huge hit it appears to
   1.158 +   be.  The first-stage decode table catches most words so that
   1.159 +   bitreverse is not in the main execution path. */
   1.160 +
   1.161 +static ogg_uint32_t bitreverse(ogg_uint32_t x){
   1.162 +  x=    ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
   1.163 +  x=    ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
   1.164 +  x=    ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
   1.165 +  x=    ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
   1.166 +  return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
   1.167 +}
   1.168 +
   1.169 +STIN long decode_packed_entry_number(codebook *book, 
   1.170 +					      oggpack_buffer *b){
   1.171 +  int  read=book->dec_maxlength;
   1.172 +  long lo,hi;
   1.173 +  long lok = oggpack_look(b,book->dec_firsttablen);
   1.174 + 
   1.175 +  if (lok >= 0) {
   1.176 +    long entry = book->dec_firsttable[lok];
   1.177 +    if(entry&0x80000000UL){
   1.178 +      lo=(entry>>15)&0x7fff;
   1.179 +      hi=book->used_entries-(entry&0x7fff);
   1.180 +    }else{
   1.181 +      oggpack_adv(b, book->dec_codelengths[entry-1]);
   1.182 +      return(entry-1);
   1.183 +    }
   1.184 +  }else{
   1.185 +    lo=0;
   1.186 +    hi=book->used_entries;
   1.187 +  }
   1.188 +
   1.189 +  lok = oggpack_look(b, read);
   1.190 +
   1.191 +  while(lok<0 && read>1)
   1.192 +    lok = oggpack_look(b, --read);
   1.193 +
   1.194 +  if(lok<0){
   1.195 +    oggpack_adv(b,1); /* force eop */
   1.196 +    return -1;
   1.197 +  }
   1.198 +
   1.199 +  /* bisect search for the codeword in the ordered list */
   1.200 +  {
   1.201 +    ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);
   1.202 +
   1.203 +    while(hi-lo>1){
   1.204 +      long p=(hi-lo)>>1;
   1.205 +      long test=book->codelist[lo+p]>testword;    
   1.206 +      lo+=p&(test-1);
   1.207 +      hi-=p&(-test);
   1.208 +    }
   1.209 +
   1.210 +    if(book->dec_codelengths[lo]<=read){
   1.211 +      oggpack_adv(b, book->dec_codelengths[lo]);
   1.212 +      return(lo);
   1.213 +    }
   1.214 +  }
   1.215 +  
   1.216 +  oggpack_adv(b, read+1);
   1.217 +  return(-1);
   1.218 +}
   1.219 +
   1.220 +/* Decode side is specced and easier, because we don't need to find
   1.221 +   matches using different criteria; we simply read and map.  There are
   1.222 +   two things we need to do 'depending':
   1.223 +   
   1.224 +   We may need to support interleave.  We don't really, but it's
   1.225 +   convenient to do it here rather than rebuild the vector later.
   1.226 +
   1.227 +   Cascades may be additive or multiplicitive; this is not inherent in
   1.228 +   the codebook, but set in the code using the codebook.  Like
   1.229 +   interleaving, it's easiest to do it here.  
   1.230 +   addmul==0 -> declarative (set the value)
   1.231 +   addmul==1 -> additive
   1.232 +   addmul==2 -> multiplicitive */
   1.233 +
   1.234 +/* returns the [original, not compacted] entry number or -1 on eof *********/
   1.235 +long vorbis_book_decode(codebook *book, oggpack_buffer *b){
   1.236 +  if(book->used_entries>0){
   1.237 +    long packed_entry=decode_packed_entry_number(book,b);
   1.238 +    if(packed_entry>=0)
   1.239 +      return(book->dec_index[packed_entry]);
   1.240 +  }
   1.241 +
   1.242 +  /* if there's no dec_index, the codebook unpacking isn't collapsed */
   1.243 +  return(-1);
   1.244 +}
   1.245 +
   1.246 +/* returns 0 on OK or -1 on eof *************************************/
   1.247 +/* decode vector / dim granularity gaurding is done in the upper layer */
   1.248 +long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
   1.249 +			      oggpack_buffer *b,int n,int point){
   1.250 +  if(book->used_entries>0){  
   1.251 +    int step=n/book->dim;
   1.252 +    long *entry = (long *)alloca(sizeof(*entry)*step);
   1.253 +    ogg_int32_t **t = (ogg_int32_t **)alloca(sizeof(*t)*step);
   1.254 +    int i,j,o;
   1.255 +    int shift=point-book->binarypoint;
   1.256 +    
   1.257 +    if(shift>=0){
   1.258 +      for (i = 0; i < step; i++) {
   1.259 +	entry[i]=decode_packed_entry_number(book,b);
   1.260 +	if(entry[i]==-1)return(-1);
   1.261 +	t[i] = book->valuelist+entry[i]*book->dim;
   1.262 +      }
   1.263 +      for(i=0,o=0;i<book->dim;i++,o+=step)
   1.264 +	for (j=0;j<step;j++)
   1.265 +	  a[o+j]+=t[j][i]>>shift;
   1.266 +    }else{
   1.267 +      for (i = 0; i < step; i++) {
   1.268 +	entry[i]=decode_packed_entry_number(book,b);
   1.269 +	if(entry[i]==-1)return(-1);
   1.270 +	t[i] = book->valuelist+entry[i]*book->dim;
   1.271 +      }
   1.272 +      for(i=0,o=0;i<book->dim;i++,o+=step)
   1.273 +	for (j=0;j<step;j++)
   1.274 +	  a[o+j]+=t[j][i]<<-shift;
   1.275 +    }
   1.276 +  }
   1.277 +  return(0);
   1.278 +}
   1.279 +
   1.280 +/* decode vector / dim granularity gaurding is done in the upper layer */
   1.281 +long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
   1.282 +			     oggpack_buffer *b,int n,int point){
   1.283 +  if(book->used_entries>0){
   1.284 +    int i,j,entry;
   1.285 +    ogg_int32_t *t;
   1.286 +    int shift=point-book->binarypoint;
   1.287 +    
   1.288 +    if(shift>=0){
   1.289 +      for(i=0;i<n;){
   1.290 +	entry = decode_packed_entry_number(book,b);
   1.291 +	if(entry==-1)return(-1);
   1.292 +	t     = book->valuelist+entry*book->dim;
   1.293 +	for (j=0;j<book->dim;)
   1.294 +	  a[i++]+=t[j++]>>shift;
   1.295 +      }
   1.296 +    }else{
   1.297 +      for(i=0;i<n;){
   1.298 +	entry = decode_packed_entry_number(book,b);
   1.299 +	if(entry==-1)return(-1);
   1.300 +	t     = book->valuelist+entry*book->dim;
   1.301 +	for (j=0;j<book->dim;)
   1.302 +	  a[i++]+=t[j++]<<-shift;
   1.303 +      }
   1.304 +    }
   1.305 +  }
   1.306 +  return(0);
   1.307 +}
   1.308 +
   1.309 +/* unlike the others, we guard against n not being an integer number
   1.310 +   of <dim> internally rather than in the upper layer (called only by
   1.311 +   floor0) */
   1.312 +long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
   1.313 +			     oggpack_buffer *b,int n,int point){
   1.314 +  if(book->used_entries>0){
   1.315 +    int i,j,entry;
   1.316 +    ogg_int32_t *t;
   1.317 +    int shift=point-book->binarypoint;
   1.318 +    
   1.319 +    if(shift>=0){
   1.320 +      
   1.321 +      for(i=0;i<n;){
   1.322 +	entry = decode_packed_entry_number(book,b);
   1.323 +	if(entry==-1)return(-1);
   1.324 +	t     = book->valuelist+entry*book->dim;
   1.325 +	for (j=0;i<n && j<book->dim;){
   1.326 +	  a[i++]=t[j++]>>shift;
   1.327 +	}
   1.328 +      }
   1.329 +    }else{
   1.330 +      
   1.331 +      for(i=0;i<n;){
   1.332 +	entry = decode_packed_entry_number(book,b);
   1.333 +	if(entry==-1)return(-1);
   1.334 +	t     = book->valuelist+entry*book->dim;
   1.335 +	for (j=0;i<n && j<book->dim;){
   1.336 +	  a[i++]=t[j++]<<-shift;
   1.337 +	}
   1.338 +      }
   1.339 +    }
   1.340 +  }else{
   1.341 +
   1.342 +    int i,j;
   1.343 +    for(i=0;i<n;){
   1.344 +      a[i++]=0;
   1.345 +    }
   1.346 +  }
   1.347 +  return(0);
   1.348 +}
   1.349 +
   1.350 +/* decode vector / dim granularity gaurding is done in the upper layer */
   1.351 +long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,\
   1.352 +			      long offset,int ch,
   1.353 +			      oggpack_buffer *b,int n,int point){
   1.354 +  if(book->used_entries>0){
   1.355 +    long i,j,entry;
   1.356 +    int chptr=0;
   1.357 +    int shift=point-book->binarypoint;
   1.358 +    
   1.359 +    if(shift>=0){
   1.360 +      
   1.361 +      for(i=offset;i<offset+n;){
   1.362 +	entry = decode_packed_entry_number(book,b);
   1.363 +	if(entry==-1)return(-1);
   1.364 +	{
   1.365 +	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
   1.366 +	  for (j=0;j<book->dim;j++){
   1.367 +	    a[chptr++][i]+=t[j]>>shift;
   1.368 +	    if(chptr==ch){
   1.369 +	      chptr=0;
   1.370 +	      i++;
   1.371 +	    }
   1.372 +	  }
   1.373 +	}
   1.374 +      }
   1.375 +    }else{
   1.376 +      
   1.377 +      for(i=offset;i<offset+n;){
   1.378 +	entry = decode_packed_entry_number(book,b);
   1.379 +	if(entry==-1)return(-1);
   1.380 +	{
   1.381 +	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
   1.382 +	  for (j=0;j<book->dim;j++){
   1.383 +	    a[chptr++][i]+=t[j]<<-shift;
   1.384 +	    if(chptr==ch){
   1.385 +	      chptr=0;
   1.386 +	      i++;
   1.387 +	    }
   1.388 +	  }
   1.389 +	}
   1.390 +      }
   1.391 +    }
   1.392 +  }
   1.393 +  return(0);
   1.394 +}

mercurial