Completely new transient analysis algorithm
[opus.git] / libcelt / rangedec.c
index 48cd3a4..35bc193 100644 (file)
@@ -1,5 +1,5 @@
-/* (C) 2001-2008 Timothy B. Terriberry
-   (C) 2008 Jean-Marc Valin */
+/* Copyright (c) 2001-2008 Timothy B. Terriberry
+   Copyright (c) 2008-2009 Xiph.Org Foundation */
 /*
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
@@ -141,9 +141,10 @@ void ec_dec_init(ec_dec *_this,ec_byte_buffer *_buf){
   _this->dif=_this->rng-(_this->rem>>EC_SYM_BITS-EC_CODE_EXTRA);
   /*Normalize the interval.*/
   ec_dec_normalize(_this);
-  /*_this->end_byte=ec_byte_look_at_end(_this->buf);*/
+  _this->end_byte=0; /* Required for platforms that have chars > 8 bits */
   _this->end_bits_left=0;
   _this->nb_end_bits=0;
+  _this->error=0;
 }
 
 
@@ -154,15 +155,14 @@ unsigned ec_decode(ec_dec *_this,unsigned _ft){
   return _ft-EC_MINI(s+1,_ft);
 }
 
-unsigned ec_decode_bin(ec_dec *_this,unsigned bits){
-#if 0
+unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
    unsigned s;
-   ec_uint32 ft;
-   ft = (ec_uint32)1<<bits;
-   _this->nrm=_this->rng>>bits;
+   _this->nrm=_this->rng>>_bits;
    s=(unsigned)((_this->dif-1)/_this->nrm);
-   return ft-EC_MINI(s+1,ft);
-#else
+   return (1<<_bits)-EC_MINI(s+1,1<<_bits);
+}
+
+unsigned ec_dec_bits(ec_dec *_this,unsigned bits){
   unsigned value=0;
   int count=0;
   _this->nb_end_bits += bits;
@@ -177,7 +177,6 @@ unsigned ec_decode_bin(ec_dec *_this,unsigned bits){
   value |= ((_this->end_byte>>(8-_this->end_bits_left))&((1<<bits)-1))<<count;
   _this->end_bits_left -= bits;
   return value;
-#endif
 }
 
 void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
@@ -188,10 +187,26 @@ void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
   ec_dec_normalize(_this);
 }
 
-long ec_dec_tell(ec_dec *_this,int _b){
+/*The probability of having a "one" is given in 1/65536.*/
+int ec_dec_bit_prob(ec_dec *_this,unsigned _prob){
+  ec_uint32 r;
+  ec_uint32 s;
+  ec_uint32 d;
+  int       val;
+  r=_this->rng;
+  d=_this->dif;
+  s=(r>>16)*_prob;
+  val=d<=s;
+  if(!val)_this->dif=d-s;
+  _this->rng=val?s:r-s;
+  ec_dec_normalize(_this);
+  return val;
+}
+
+ec_uint32 ec_dec_tell(ec_dec *_this,int _b){
   ec_uint32 r;
   int       l;
-  long      nbits;
+  ec_uint32      nbits;
   nbits=(ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS)*
    EC_SYM_BITS;
   /*To handle the non-integral number of bits still left in the decoder state,