shithub: libvpx

Download patch

ref: 0d284ffed10d8df86caff32bc56caefc45ed5c44
parent: afa57bfc971242ce2205f8bb86ccd43b545fe144
author: Paul Wilkins <paulwilkins@google.com>
date: Mon Feb 11 14:19:21 EST 2013

Abstract the selection of coefficient context.

This is an initial step to facilitate experimentation
with changes to the prior token context used to code
coefficients to take better account of the energy of
preceding tokens.

This patch merely abstracts the selection of context into
two functions and does not alter the output.

Change-Id: I117fff0b49c61da83aed641e36620442f86def86

--- a/vp9/common/vp9_entropy.c
+++ b/vp9/common/vp9_entropy.c
@@ -318,6 +318,29 @@
 
 #include "vp9/common/vp9_default_coef_probs.h"
 
+// This function updates and then returns n AC coefficient context
+// This is currently a placeholder function to allow experimentation
+// using various context models based on the energy earlier tokens
+// within the current block.
+//
+// For now it just returns the previously used context.
+int vp9_get_coef_context(int * recent_energy, int token) {
+  // int token_energy;
+  // int av_energy;
+
+  // Placeholder code for experiments with token energy
+  // as a coefficient context.
+  /*token_energy = ((token != DCT_EOB_TOKEN) ? token : 0);
+  if (token_energy) {
+    av_energy = (token_energy + *recent_energy + 1) >> 1;
+  } else {
+    av_energy = 0;
+  }
+  *recent_energy = token_energy;*/
+
+  return vp9_prev_token_class[token];
+};
+
 void vp9_default_coef_probs(VP9_COMMON *pc) {
   vpx_memcpy(pc->fc.coef_probs_4x4, default_coef_probs_4x4,
              sizeof(pc->fc.coef_probs_4x4));
--- a/vp9/common/vp9_entropy.h
+++ b/vp9/common/vp9_entropy.h
@@ -106,9 +106,6 @@
 #define SUBEXP_PARAM                4   /* Subexponential code parameter */
 #define MODULUS_PARAM               13  /* Modulus parameter */
 
-extern DECLARE_ALIGNED(16, const uint8_t,
-                       vp9_prev_token_class[MAX_ENTROPY_TOKENS]);
-
 struct VP9Common;
 void vp9_default_coef_probs(struct VP9Common *);
 extern DECLARE_ALIGNED(16, const int, vp9_default_zig_zag1d_4x4[16]);
@@ -129,4 +126,5 @@
   vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
 }
 
+extern int vp9_get_coef_context(int * recent_energy, int token);
 #endif  // VP9_COMMON_VP9_ENTROPY_H_
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -65,8 +65,8 @@
 
 #define INCREMENT_COUNT(token)               \
   do {                                       \
-    coef_counts[type][coef_bands[c]][pt][token]++; \
-    pt = vp9_prev_token_class[token];              \
+    coef_counts[type][coef_bands[c]][pt][token]++;            \
+    pt = vp9_get_coef_context(&recent_energy, token);         \
   } while (0)
 
 #define WRITE_COEF_CONTINUE(val, token)                       \
@@ -95,6 +95,7 @@
   const int lidx = vp9_block2left[txfm_size][block_idx];
   ENTROPY_CONTEXT above_ec = A0[aidx] != 0, left_ec = L0[lidx] != 0;
   FRAME_CONTEXT *const fc = &dx->common.fc;
+  int recent_energy = 0;
   int nodc = (type == PLANE_TYPE_Y_NO_DC);
   int pt, c = nodc;
   vp9_coeff_probs *coef_probs;
--- a/vp9/encoder/vp9_encodemb.c
+++ b/vp9/encoder/vp9_encodemb.c
@@ -361,6 +361,13 @@
   }\
 }
 
+// This function is a place holder for now but may ultimately need
+// to scan previous tokens to work out the correct context.
+static int trellis_get_coeff_context(int token) {
+  int recent_energy = 0;
+  return vp9_get_coef_context(&recent_energy, token);
+}
+
 static void optimize_b(MACROBLOCK *mb, int i, PLANE_TYPE type,
                        ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                        int tx_size) {
@@ -453,7 +460,7 @@
       /* Consider both possible successor states. */
       if (next < default_eob) {
         band = bands[i + 1];
-        pt = vp9_prev_token_class[t0];
+        pt = trellis_get_coeff_context(t0);
         rate0 +=
           mb->token_costs[tx_size][type][band][pt][tokens[next][0].token];
         rate1 +=
@@ -501,12 +508,12 @@
       if (next < default_eob) {
         band = bands[i + 1];
         if (t0 != DCT_EOB_TOKEN) {
-          pt = vp9_prev_token_class[t0];
+          pt = trellis_get_coeff_context(t0);
           rate0 += mb->token_costs[tx_size][type][band][pt][
               tokens[next][0].token];
         }
         if (t1 != DCT_EOB_TOKEN) {
-          pt = vp9_prev_token_class[t1];
+          pt = trellis_get_coeff_context(t1);
           rate1 += mb->token_costs[tx_size][type][band][pt][
               tokens[next][1].token];
         }
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -490,23 +490,25 @@
     seg_eob = 0;
 
   if (tx_type != DCT_DCT) {
+    int recent_energy = 0;
     for (; c < eob; c++) {
       int v = qcoeff_ptr[scan[c]];
       int t = vp9_dct_value_tokens_ptr[v].Token;
       cost += token_costs[band[c]][pt][t];
       cost += vp9_dct_value_cost_ptr[v];
-      pt = vp9_prev_token_class[t];
+      pt = vp9_get_coef_context(&recent_energy, t);
     }
     if (c < seg_eob)
       cost += mb->hybrid_token_costs[tx_size][type][band[c]]
           [pt][DCT_EOB_TOKEN];
   } else {
+    int recent_energy = 0;
     for (; c < eob; c++) {
       int v = qcoeff_ptr[scan[c]];
       int t = vp9_dct_value_tokens_ptr[v].Token;
       cost += token_costs[band[c]][pt][t];
       cost += vp9_dct_value_cost_ptr[v];
-      pt = vp9_prev_token_class[t];
+      pt = vp9_get_coef_context(&recent_energy, t);
     }
     if (c < seg_eob)
       cost += mb->token_costs[tx_size][type][band[c]]
@@ -670,7 +672,8 @@
   // TODO(jingning) is it possible to quickly determine whether to force
   //                trailing coefficients to be zero, instead of running trellis
   //                optimization in the rate-distortion optimization loop?
-  if (mb->optimize && mb->e_mbd.mode_info_context->mbmi.mode < I8X8_PRED)
+  if (mb->optimize &&
+      xd->mode_info_context->mbmi.mode < I8X8_PRED)
     vp9_optimize_mby_16x16(mb);
 
   d = vp9_mbblock_error(mb, 0);
--- a/vp9/encoder/vp9_tokenize.c
+++ b/vp9/encoder/vp9_tokenize.c
@@ -109,6 +109,7 @@
                        int dry_run) {
   int pt; /* near block/prev token context index */
   int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
+  int recent_energy = 0;
   const BLOCKD * const b = xd->block + ib;
   const int eob = b->eob;     /* one beyond last nonzero coeff */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
@@ -245,7 +246,8 @@
     if (!dry_run) {
       ++counts[type][band][pt][token];
     }
-    pt = vp9_prev_token_class[token];
+
+    pt = vp9_get_coef_context(&recent_energy, token);
     ++t;
   } while (c < eob && ++c < seg_eob);