Conversion to the new generic 2D adaptation code.
authorJean-Marc Valin <jmvalin@jmvalin.ca>
Thu, 21 Mar 2013 18:52:15 +0000 (14:52 -0400)
committerJean-Marc Valin <jmvalin@jmvalin.ca>
Thu, 21 Mar 2013 18:52:15 +0000 (14:52 -0400)
src/encode.c
src/pvq_code.h
src/pvq_decoder.c
src/pvq_encoder.c
src/tests/test_coef_coder.c

index ea4ee3f..5895cc8 100644 (file)
@@ -615,7 +615,7 @@ int daala_encode_img_in(daala_enc_ctx *_enc,od_img *_img,int _duration){
                 ex_ym=(65536/2)*vk;
                 generic_encode(&_enc->ec,model_ym+pli,vk-pred[1],&ex_ym,0);
               }
-              pvq_encoder(&_enc->ec,pred+2,14,vk-abs(pred[1]),&pvq_adapt);
+              pvq_encoder(&_enc->ec,pred+2,14,vk-abs(pred[1]),&adapt);
 #else
               vk=quant_scalar(cblock+1,predt+1,pvq_scale,pred+1,15,11,
                &pvq_adapt);
@@ -628,15 +628,15 @@ int daala_encode_img_in(daala_enc_ctx *_enc,od_img *_img,int _duration){
               /*Treat first component (y[m]) like all others.*/
               pvq_encoder(&_enc->ec,pred+1,15,vk,&pvq_adapt);
 #endif
-              if(pvq_adapt.k>=0){
+              if(adapt.curr[OD_ADAPT_K_Q8]>=0){
                 nk++;
-                k_total+=pvq_adapt.k;
-                sum_ex_total_q8+=pvq_adapt.sum_ex_q8;
+                k_total+=adapt.curr[OD_ADAPT_K_Q8];
+                sum_ex_total_q8+=adapt.curr[OD_ADAPT_SUM_EX_Q8];
               }
-              if(pvq_adapt.count_q8>=0){
+              if(adapt.curr[OD_ADAPT_COUNT_Q8]>=0){
                 ncount++;
-                count_total_q8+=pvq_adapt.count_q8;
-                count_ex_total_q8+=pvq_adapt.count_ex_q8;
+                count_total_q8+=adapt.curr[OD_ADAPT_COUNT_Q8];
+                count_ex_total_q8+=adapt.curr[OD_ADAPT_COUNT_EX_Q8];
               }
 #if !defined(OD_DISABLE_PVQ_CODE1)
               if(pvq_adapt.pos>=0){
index d5bcf73..f82efbe 100644 (file)
@@ -27,6 +27,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
 
 #include "entenc.h"
 #include "entdec.h"
+#include "adapt.h"
 
 extern const ogg_uint16_t cdf_table[][16];
 extern const unsigned char decayE[];
@@ -98,12 +99,12 @@ void laplace_encode_special(od_ec_enc *enc,int pos,unsigned decay,int max);
 int laplace_decode_special(od_ec_dec *dec,unsigned decay,int max);
 
 void pvq_encoder(od_ec_enc *enc,const int *y,int N,int K,
- od_pvq_adapt_ctx *_adapt);
-void pvq_decoder(od_ec_dec *dec,int *y,int N,int K,od_pvq_adapt_ctx *_adapt);
+ od_adapt_ctx *_adapt);
+void pvq_decoder(od_ec_dec *dec,int *y,int N,int K,od_adapt_ctx *_adapt);
 
 void pvq_encode_delta(od_ec_enc *enc, const int *y,int N,int K,
- od_pvq_adapt_ctx *_adapt);
+ od_adapt_ctx *_adapt);
 void pvq_decode_delta(od_ec_dec *dec, int *y,int N,int K,
- od_pvq_adapt_ctx *_adapt);
+ od_adapt_ctx *_adapt);
 
 #endif
index 499997f..b2762b1 100644 (file)
@@ -196,7 +196,7 @@ static void pvq_decoder1(od_ec_dec *dec, int *y,int N,
  * @param [in]     K   sum of the absolute value of components of y
  * @param [in,out] _adapt Adaptation context.
  */
-void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_pvq_adapt_ctx *_adapt)
+void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_adapt_ctx *_adapt)
 {
   int i;
   int sumEx;
@@ -204,16 +204,16 @@ void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_pvq_adapt_ctx *_adapt)
   int expQ8;
   int mean_k_q8;
   int mean_sum_ex_q8;
-  _adapt->count_q8=-1;
-  _adapt->count_ex_q8=0;
+  _adapt->curr[OD_ADAPT_COUNT_Q8]=OD_ADAPT_NO_VALUE;
+  _adapt->curr[OD_ADAPT_COUNT_EX_Q8]=OD_ADAPT_NO_VALUE;
   if(K<=1)
   {
     pvq_decode_delta(dec, y, N, K, _adapt);
     return;
   }
   if(K==0){
-    _adapt->k=0;
-    _adapt->sum_ex_q8=0;
+    _adapt->curr[OD_ADAPT_K_Q8]=0;
+    _adapt->curr[OD_ADAPT_SUM_EX_Q8]=0;
 #if !defined(OD_DISABLE_PVQ_CODE1)
     _adapt->pos=-1;
 #endif
@@ -233,8 +233,8 @@ void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_pvq_adapt_ctx *_adapt)
 #endif
 
   /* Estimates the factor relating pulses_left and positions_left to E(|x|) */
-  mean_k_q8=_adapt->mean_k_q8;
-  mean_sum_ex_q8=_adapt->mean_sum_ex_q8;
+  mean_k_q8=_adapt->mean[OD_ADAPT_K_Q8];
+  mean_sum_ex_q8=_adapt->mean[OD_ADAPT_SUM_EX_Q8];
   if(mean_k_q8<1<<23)expQ8=256*mean_k_q8/(1+mean_sum_ex_q8);
   else expQ8=mean_k_q8/(1+(mean_sum_ex_q8>>8));
   for(i=0;i<N;i++){
@@ -267,8 +267,8 @@ void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_pvq_adapt_ctx *_adapt)
     Kn-=abs(x);
   }
   /* Adapting the estimates for expQ8 */
-  _adapt->k=K-Kn;
-  _adapt->sum_ex_q8=sumEx;
+  _adapt->curr[OD_ADAPT_K_Q8]=K-Kn;
+  _adapt->curr[OD_ADAPT_SUM_EX_Q8]=sumEx;
 #if !defined(OD_DISABLE_PVQ_CODE1)
   _adapt->pos=-1;
 #endif
@@ -277,13 +277,13 @@ void pvq_decoder(od_ec_dec *dec, int *y,int N,int K,od_pvq_adapt_ctx *_adapt)
 }
 
 void pvq_decode_delta(od_ec_dec *dec, int *y,int N,int _K,
- od_pvq_adapt_ctx *_adapt)
+ od_adapt_ctx *_adapt)
 {
   int i;
   int prev=0;
   int sumEx=0;
   int sumC=0;
-  int coef = 256*_adapt->mean_count_q8/(1+_adapt->mean_count_ex_q8);
+  int coef = 256*_adapt->mean[OD_ADAPT_COUNT_Q8]/(1+_adapt->mean[OD_ADAPT_COUNT_EX_Q8]);
   int pos=0;
   int K0;
   int sign=0;
@@ -322,12 +322,12 @@ void pvq_decode_delta(od_ec_dec *dec, int *y,int N,int _K,
 
   if (_K>0)
   {
-    _adapt->count_q8=256*sumC;
-    _adapt->count_ex_q8=sumEx;
+    _adapt->curr[OD_ADAPT_COUNT_Q8]=256*sumC;
+    _adapt->curr[OD_ADAPT_COUNT_EX_Q8]=sumEx;
   } else {
-    _adapt->count_q8=-1;
-    _adapt->count_ex_q8=0;
+    _adapt->curr[OD_ADAPT_COUNT_Q8]=-1;
+    _adapt->curr[OD_ADAPT_COUNT_EX_Q8]=0;
   }
-  _adapt->k=0;
-  _adapt->sum_ex_q8=0;
+  _adapt->curr[OD_ADAPT_K_Q8]=0;
+  _adapt->curr[OD_ADAPT_SUM_EX_Q8]=0;
 }
index 8b930bf..def9c96 100644 (file)
@@ -28,6 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
 #include "entenc.h"
 #include "entcode.h"
 #include "pvq_code.h"
+#include "adapt.h"
 
 /** Encodes the tail of a Laplace-distributed variable, i.e. it doesn't
  * do anything special for the zero case.
@@ -201,23 +202,23 @@ static void pvq_encoder1(od_ec_enc *enc, const int *y,int N,
  * @param [in,out] _adapt Adaptation context.
  */
 void pvq_encoder(od_ec_enc *enc, const int *y,int N,int K,
- od_pvq_adapt_ctx *_adapt){
+ od_adapt_ctx *_adapt){
   int i;
   int sumEx;
   int Kn;
   int expQ8;
   int mean_k_q8;
   int mean_sum_ex_q8;
-  _adapt->count_q8=-1;
-  _adapt->count_ex_q8=0;
+  _adapt->curr[OD_ADAPT_COUNT_Q8] = OD_ADAPT_NO_VALUE;
+  _adapt->curr[OD_ADAPT_COUNT_EX_Q8] = OD_ADAPT_NO_VALUE;
   if (K<=1)
   {
     pvq_encode_delta(enc, y, N, K, _adapt);
     return;
   }
   if(K==0){
-    _adapt->k=0;
-    _adapt->sum_ex_q8=0;
+    _adapt->curr[OD_ADAPT_K_Q8]=0;
+    _adapt->curr[OD_ADAPT_SUM_EX_Q8]=0;
 #if !defined(OD_DISABLE_PVQ_CODE1)
     _adapt->pos=-1;
 #endif
@@ -233,8 +234,8 @@ void pvq_encoder(od_ec_enc *enc, const int *y,int N,int K,
   }
 #endif
   /* Estimates the factor relating pulses_left and positions_left to E(|x|) */
-  mean_k_q8=_adapt->mean_k_q8;
-  mean_sum_ex_q8=_adapt->mean_sum_ex_q8;
+  mean_k_q8=_adapt->mean[OD_ADAPT_K_Q8];
+  mean_sum_ex_q8=_adapt->mean[OD_ADAPT_SUM_EX_Q8];
   if(mean_k_q8<1<<23)expQ8=256*mean_k_q8/(1+mean_sum_ex_q8);
   else expQ8=mean_k_q8/(1+(mean_sum_ex_q8>>8));
   for(i=0;i<N;i++){
@@ -264,8 +265,8 @@ void pvq_encoder(od_ec_enc *enc, const int *y,int N,int K,
     Kn-=x;
   }
   /* Adapting the estimates for expQ8 */
-  _adapt->k=K-Kn;
-  _adapt->sum_ex_q8=sumEx;
+  _adapt->curr[OD_ADAPT_K_Q8]=K-Kn;
+  _adapt->curr[OD_ADAPT_SUM_EX_Q8]=sumEx;
 #if !defined(OD_DISABLE_PVQ_CODE1)
   _adapt->pos=-1;
 #endif
@@ -408,7 +409,7 @@ void pvq_encoder3(od_ec_enc *enc, const int *_y,int N,int K,int *num, int *den,
 
 
 void pvq_encode_delta(od_ec_enc *enc, const int *y,int N,int _K,
- od_pvq_adapt_ctx *_adapt)
+ od_adapt_ctx *_adapt)
 {
   int i;
   int prev=0;
@@ -416,7 +417,7 @@ void pvq_encode_delta(od_ec_enc *enc, const int *y,int N,int _K,
   int sumC=0;
   int first = 1;
   int K=_K;
-  int coef = 256*_adapt->mean_count_q8/(1+_adapt->mean_count_ex_q8);
+  int coef = 256*_adapt->mean[OD_ADAPT_COUNT_Q8]/(1+_adapt->mean[OD_ADAPT_COUNT_EX_Q8]);
   coef=OD_MAXI(coef,1);
   for(i=0;i<N;i++){
     if(y[i]!=0){
@@ -454,12 +455,12 @@ void pvq_encode_delta(od_ec_enc *enc, const int *y,int N,int _K,
 
   if (_K>0)
   {
-    _adapt->count_q8=256*sumC;
-    _adapt->count_ex_q8=sumEx;
+    _adapt->curr[OD_ADAPT_COUNT_Q8]=256*sumC;
+    _adapt->curr[OD_ADAPT_COUNT_EX_Q8]=sumEx;
   } else {
-    _adapt->count_q8=-1;
-    _adapt->count_ex_q8=0;
+    _adapt->curr[OD_ADAPT_COUNT_Q8] = OD_ADAPT_NO_VALUE;
+    _adapt->curr[OD_ADAPT_COUNT_EX_Q8] = OD_ADAPT_NO_VALUE;
   }
-  _adapt->k=0;
-  _adapt->sum_ex_q8=0;
+  _adapt->curr[OD_ADAPT_K_Q8]=0;
+  _adapt->curr[OD_ADAPT_SUM_EX_Q8]=0;
 }
index 821d312..334eccc 100644 (file)
@@ -36,7 +36,8 @@
 #define MAXN 256
 
 int run_pvq(int *X,int len,int N){
-  od_pvq_adapt_ctx adapt;
+  od_pvq_adapt_ctx pvq_adapt;
+  od_adapt_ctx adapt;
   int i, j;
   od_ec_enc enc;
   od_ec_dec dec;
@@ -48,12 +49,12 @@ int run_pvq(int *X,int len,int N){
   int bits_used;
 
   Ki = malloc(sizeof(*Ki)*len);
-  adapt.mean_k_q8=163;
-  adapt.mean_sum_ex_q8=64;
-  adapt.mean_count_q8=100*4;
-  adapt.mean_count_ex_q8=256*4;
+  pvq_adapt.mean_k_q8=163;
+  pvq_adapt.mean_sum_ex_q8=64;
+  pvq_adapt.mean_count_q8=100*4;
+  pvq_adapt.mean_count_ex_q8=256*4;
 #if !defined(OD_DISABLE_PVQ_CODE1)
-  adapt.mean_pos_q4=30<<4;
+  pvq_adapt.mean_pos_q4=30<<4;
 #endif
   od_ec_enc_init(&enc, EC_BUF_SIZE);
   generic_model_init(&model);
@@ -63,22 +64,30 @@ int run_pvq(int *X,int len,int N){
       K += abs(X[i*N+j]);
     Ki[i] = K;
     generic_encode(&enc, &model, K, &EK, 4);
+    adapt.mean[OD_ADAPT_K_Q8] = pvq_adapt.mean_k_q8;
+    adapt.mean[OD_ADAPT_SUM_EX_Q8] = pvq_adapt.mean_sum_ex_q8;
+    adapt.mean[OD_ADAPT_COUNT_Q8] = pvq_adapt.mean_count_q8;
+    adapt.mean[OD_ADAPT_COUNT_EX_Q8] = pvq_adapt.mean_count_ex_q8;
     pvq_encoder(&enc,&X[i*N],N,K,&adapt);
+    pvq_adapt.k = adapt.curr[OD_ADAPT_K_Q8];
+    pvq_adapt.sum_ex_q8 = adapt.curr[OD_ADAPT_SUM_EX_Q8];
+    pvq_adapt.count_q8 = adapt.curr[OD_ADAPT_COUNT_Q8];
+    pvq_adapt.count_ex_q8 = adapt.curr[OD_ADAPT_COUNT_EX_Q8];
 #if !defined(OD_DISABLE_PVQ_CODE1)
-    if(adapt.pos>=0){
-      adapt.mean_pos_q4=OD_MAXI(adapt.mean_pos_q4+
-          ((adapt.pos<<4)-adapt.mean_pos_q4>>OD_POS_ADAPT_SPEED),N/8);
+    if(pvq_adapt.pos>=0){
+      pvq_adapt.mean_pos_q4=OD_MAXI(pvq_adapt.mean_pos_q4+
+          ((pvq_adapt.pos<<4)-pvq_adapt.mean_pos_q4>>OD_POS_ADAPT_SPEED),N/8);
     }
 #endif
-    if(adapt.k>=0){
-      adapt.mean_k_q8+=(adapt.k<<8)-adapt.mean_k_q8>>OD_K_ADAPT_SPEED;
-      adapt.mean_sum_ex_q8+=
-       adapt.sum_ex_q8-adapt.mean_sum_ex_q8>>OD_SUM_EX_ADAPT_SPEED;
+    if(pvq_adapt.k>=0){
+      pvq_adapt.mean_k_q8+=(pvq_adapt.k<<8)-pvq_adapt.mean_k_q8>>OD_K_ADAPT_SPEED;
+      pvq_adapt.mean_sum_ex_q8+=
+       pvq_adapt.sum_ex_q8-pvq_adapt.mean_sum_ex_q8>>OD_SUM_EX_ADAPT_SPEED;
     }
-    if(adapt.count_q8>=0){
-      adapt.mean_count_q8+=(adapt.count_q8<<8)-adapt.mean_count_q8>>OD_DELTA_ADAPT_SPEED;
-      adapt.mean_count_ex_q8+=
-       adapt.count_ex_q8-adapt.mean_count_ex_q8>>OD_DELTA_ADAPT_SPEED;
+    if(pvq_adapt.count_q8>=0){
+      pvq_adapt.mean_count_q8+=(pvq_adapt.count_q8<<8)-pvq_adapt.mean_count_q8>>OD_DELTA_ADAPT_SPEED;
+      pvq_adapt.mean_count_ex_q8+=
+       pvq_adapt.count_ex_q8-pvq_adapt.mean_count_ex_q8>>OD_DELTA_ADAPT_SPEED;
     }
     /*if (i==0)
     {
@@ -90,12 +99,12 @@ int run_pvq(int *X,int len,int N){
 
   bits_used = od_ec_enc_tell(&enc);
 
-  adapt.mean_k_q8=163;
-  adapt.mean_sum_ex_q8=64;
-  adapt.mean_count_q8=100*4;
-  adapt.mean_count_ex_q8=256*4;
+  pvq_adapt.mean_k_q8=163;
+  pvq_adapt.mean_sum_ex_q8=64;
+  pvq_adapt.mean_count_q8=100*4;
+  pvq_adapt.mean_count_ex_q8=256*4;
 #if !defined(OD_DISABLE_PVQ_CODE1)
-  adapt.mean_pos_q4=30<<4;
+  pvq_adapt.mean_pos_q4=30<<4;
 #endif
   od_ec_dec_init(&dec, buf, buf_sz);
   generic_model_init(&model);
@@ -109,22 +118,30 @@ int run_pvq(int *X,int len,int N){
     if (K!=Ki[i]){
       fprintf(stderr, "mismatch for K of vector %d (N=%d)\n", i, N);
     }
+    adapt.mean[OD_ADAPT_K_Q8] = pvq_adapt.mean_k_q8;
+    adapt.mean[OD_ADAPT_SUM_EX_Q8] = pvq_adapt.mean_sum_ex_q8;
+    adapt.mean[OD_ADAPT_COUNT_Q8] = pvq_adapt.mean_count_q8;
+    adapt.mean[OD_ADAPT_COUNT_EX_Q8] = pvq_adapt.mean_count_ex_q8;
     pvq_decoder(&dec, y, N, Ki[i], &adapt);
+    pvq_adapt.k = adapt.curr[OD_ADAPT_K_Q8];
+    pvq_adapt.sum_ex_q8 = adapt.curr[OD_ADAPT_SUM_EX_Q8];
+    pvq_adapt.count_q8 = adapt.curr[OD_ADAPT_COUNT_Q8];
+    pvq_adapt.count_ex_q8 = adapt.curr[OD_ADAPT_COUNT_EX_Q8];
 #if !defined(OD_DISABLE_PVQ_CODE1)
-    if(adapt.pos>=0){
-      adapt.mean_pos_q4=OD_MAXI(adapt.mean_pos_q4+
-          ((adapt.pos<<4)-adapt.mean_pos_q4>>OD_POS_ADAPT_SPEED),N/8);
+    if(pvq_adapt.pos>=0){
+      pvq_adapt.mean_pos_q4=OD_MAXI(pvq_adapt.mean_pos_q4+
+          ((pvq_adapt.pos<<4)-pvq_adapt.mean_pos_q4>>OD_POS_ADAPT_SPEED),N/8);
     }
 #endif
-    if(adapt.k>=0){
-      adapt.mean_k_q8+=(adapt.k<<8)-adapt.mean_k_q8>>OD_K_ADAPT_SPEED;
-      adapt.mean_sum_ex_q8+=
-       adapt.sum_ex_q8-adapt.mean_sum_ex_q8>>OD_SUM_EX_ADAPT_SPEED;
+    if(pvq_adapt.k>=0){
+      pvq_adapt.mean_k_q8+=(pvq_adapt.k<<8)-pvq_adapt.mean_k_q8>>OD_K_ADAPT_SPEED;
+      pvq_adapt.mean_sum_ex_q8+=
+       pvq_adapt.sum_ex_q8-pvq_adapt.mean_sum_ex_q8>>OD_SUM_EX_ADAPT_SPEED;
     }
-    if(adapt.count_q8>=0){
-      adapt.mean_count_q8+=(adapt.count_q8<<8)-adapt.mean_count_q8>>OD_DELTA_ADAPT_SPEED;
-      adapt.mean_count_ex_q8+=
-       adapt.count_ex_q8-adapt.mean_count_ex_q8>>OD_DELTA_ADAPT_SPEED;
+    if(pvq_adapt.count_q8>=0){
+      pvq_adapt.mean_count_q8+=(pvq_adapt.count_q8<<8)-pvq_adapt.mean_count_q8>>OD_DELTA_ADAPT_SPEED;
+      pvq_adapt.mean_count_ex_q8+=
+       pvq_adapt.count_ex_q8-pvq_adapt.mean_count_ex_q8>>OD_DELTA_ADAPT_SPEED;
     }
     for (j=0;j<N;j++){
       if(y[j]!=X[i*N+j]){
@@ -140,10 +157,10 @@ int run_pvq(int *X,int len,int N){
         fprintf(stderr,"\n");
 #if !defined(OD_DISABLE_PVQ_CODE1)
         fprintf(stderr, "K[%d]=%d, num=%d, den=%d, u=%d\n", i, Ki[i],
-         adapt.mean_k_q8, adapt.mean_sum_ex_q8, adapt.mean_pos_q4);
+         pvq_adapt.mean_k_q8, pvq_adapt.mean_sum_ex_q8, pvq_adapt.mean_pos_q4);
 #else
         fprintf(stderr, "K[%d]=%d, num=%d, den=%d\n", i, Ki[i],
-         adapt.mean_k_q8, adapt.mean_sum_ex_q8);
+         pvq_adapt.mean_k_q8, pvq_adapt.mean_sum_ex_q8);
 #endif
         abort();
       }