]> granicus.if.org Git - libvpx/commitdiff
Add a new token stuffing function vp8_stuff_mb().
authorRonald S. Bultje <rbultje@google.com>
Mon, 15 Oct 2012 17:52:13 +0000 (10:52 -0700)
committerRonald S. Bultje <rbultje@google.com>
Mon, 15 Oct 2012 17:56:39 +0000 (10:56 -0700)
This way a caller doesn't need to implement the logic for which (and how
many) tokens to write out to stuff one macroblock worth of EOBs. Make
the actual function implementations static, since they are now only used
in tokenize.c; also do some minor stylistic changes so it follows the
style guide a little more closely; use PLANE_TYPE where appropriate,
remove old (stale) frame_type function arguments; hardcode plane type
where only a single one is possible (2nd order DC or U/V EOB stuffing);
support stuffing 8x8/4x4 transform EOBs with no 2nd order DC.

Change-Id: Ia448e251d19a4e3182eddeb9edd034bd7dc16fa3

vp8/encoder/encodeframe.c
vp8/encoder/onyx_int.h
vp8/encoder/tokenize.c

index 628a8df4dd54cb846f7adefb6402f00f2bd00989..ac22456d17dec75f885df0f8ed56b69a2e1ea682 100644 (file)
@@ -52,9 +52,6 @@ int enc_debug = 0;
 int mb_row_debug, mb_col_debug;
 #endif
 
-extern void vp8_stuff_mb_4x4(VP8_COMP *cpi, MACROBLOCKD *xd,
-                             TOKENEXTRA **t, int dry_run);
-
 extern void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex);
 extern void vp8_auto_select_speed(VP8_COMP *cpi);
 extern void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
@@ -81,8 +78,6 @@ void vp8cx_encode_intra_super_block(VP8_COMP *cpi,
                                     MACROBLOCK *x,
                                     TOKENEXTRA **t, int mb_col);
 static void adjust_act_zbin(VP8_COMP *cpi, MACROBLOCK *x);
-extern void vp8_stuff_mb_8x8(VP8_COMP *cpi,
-                             MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
 
 #ifdef MODE_STATS
 unsigned int inter_y_modes[MB_MODE_COUNT];
@@ -1843,7 +1838,7 @@ static void update_sb_skip_coeff_state(VP8_COMP *cpi,
     if (skip[n]) {
       x->e_mbd.above_context = &ta[n];
       x->e_mbd.left_context  = &tl[n];
-      vp8_stuff_mb_8x8(cpi, &x->e_mbd, tp, 0);
+      vp8_stuff_mb(cpi, &x->e_mbd, tp, 0);
     } else {
       if (n_tokens[n]) {
         memcpy(*tp, tokens[n], sizeof(*t[0]) * n_tokens[n]);
@@ -2150,7 +2145,7 @@ void vp8cx_encode_inter_macroblock (VP8_COMP *cpi, MACROBLOCK *x,
         cpi->skip_true_count[mb_skip_context]++;
       vp8_fix_contexts(xd);
     } else {
-      vp8_stuff_mb_4x4(cpi, xd, t, !output_enabled);
+      vp8_stuff_mb(cpi, xd, t, !output_enabled);
       mbmi->mb_skip_coeff = 0;
       if (output_enabled)
         cpi->skip_false_count[mb_skip_context]++;
@@ -2342,7 +2337,7 @@ void vp8cx_encode_inter_superblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t,
         cpi->skip_true_count[mb_skip_context]++;
         vp8_fix_contexts(xd);
       } else {
-        vp8_stuff_mb_4x4(cpi, xd, t, 0);
+        vp8_stuff_mb(cpi, xd, t, 0);
         xd->mode_info_context->mbmi.mb_skip_coeff = 0;
         cpi->skip_false_count[mb_skip_context]++;
       }
index 696da14ed8933457673fff5a02bba10d19b1835e..1e7494039547ca9389c470ee28f8e54cc24c6cce 100644 (file)
@@ -805,6 +805,7 @@ void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x);
 int rd_cost_intra_mb(MACROBLOCKD *x);
 
 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCKD *, TOKENEXTRA **, int dry_run);
+void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
 
 void vp8_set_speed_features(VP8_COMP *cpi);
 
index 3a516963cebbc6859b2d4dd9fef12e6200d4345c..c72c1e7e7865427b232d743b0482203b4300ddd3 100644 (file)
@@ -60,14 +60,7 @@ extern unsigned int hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16][COEF_BANDS]
 #endif
 #endif  /* ENTROPY_STATS */
 
-void vp8_stuff_mb_4x4(VP8_COMP *cpi,
-                      MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
-void vp8_stuff_mb_8x8(VP8_COMP *cpi,
-                      MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
-void vp8_stuff_mb_8x8_4x4uv(VP8_COMP *cpi,
-                            MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
-void vp8_stuff_mb_16x16(VP8_COMP *cpi, MACROBLOCKD *xd,
-                        TOKENEXTRA **t, int dry_run);
+void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
 void vp8_fix_contexts(MACROBLOCKD *xd);
 
 static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
@@ -718,16 +711,7 @@ void vp8_tokenize_mb(VP8_COMP *cpi,
     if (!dry_run)
       cpi->skip_true_count[mb_skip_context] += skip_inc;
     if (!cpi->common.mb_no_coeff_skip) {
-      if (tx_size == TX_16X16)
-        vp8_stuff_mb_16x16(cpi, xd, t, dry_run);
-      else
-      if (tx_size == TX_8X8) {
-        if (xd->mode_info_context->mbmi.mode == I8X8_PRED)
-          vp8_stuff_mb_8x8_4x4uv(cpi, xd, t, dry_run);
-        else
-          vp8_stuff_mb_8x8(cpi, xd, t, dry_run);
-      } else
-        vp8_stuff_mb_4x4(cpi, xd, t, dry_run);
+      vp8_stuff_mb(cpi, xd, t, dry_run);
     } else {
       vp8_fix_contexts(xd);
     }
@@ -1054,73 +1038,60 @@ void print_context_counters() {
 }
 #endif
 
-
 void vp8_tokenize_initialize() {
   fill_value_tokens();
 }
 
-
-static __inline void stuff2nd_order_b_8x8(
-  MACROBLOCKD *xd,
-  const BLOCKD *const b,
-  TOKENEXTRA **tp,
-  const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
-  const FRAME_TYPE frametype,
-  ENTROPY_CONTEXT *a,
-  ENTROPY_CONTEXT *l,
-  VP8_COMP *cpi,
-  int dry_run) {
+static __inline void stuff2nd_order_b_8x8(MACROBLOCKD *xd,
+                                          const BLOCKD *const b,
+                                          TOKENEXTRA **tp,
+                                          ENTROPY_CONTEXT *a,
+                                          ENTROPY_CONTEXT *l,
+                                          VP8_COMP *cpi,
+                                          int dry_run) {
   int pt; /* near block/prev token context index */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
-  (void) frametype;
-  (void) type;
   (void) b;
 
   t->Token = DCT_EOB_TOKEN;
-  t->context_tree = cpi->common.fc.coef_probs_8x8 [1] [0] [pt];
+  t->context_tree = cpi->common.fc.coef_probs_8x8[PLANE_TYPE_Y2][0][pt];
   // t->section = 11;
   t->skip_eob_node = 0;
   ++t;
 
   *tp = t;
   if (!dry_run)
-    ++cpi->coef_counts_8x8       [1] [0] [pt] [DCT_EOB_TOKEN];
+    ++cpi->coef_counts_8x8[PLANE_TYPE_Y2][0][pt][DCT_EOB_TOKEN];
   pt = 0;
   *a = *l = pt;
-
 }
 
-static __inline void stuff1st_order_b_8x8
-(
-  MACROBLOCKD *xd,
-  const BLOCKD *const b,
-  TOKENEXTRA **tp,
-  const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
-  const FRAME_TYPE frametype,
-  ENTROPY_CONTEXT *a,
-  ENTROPY_CONTEXT *l,
-  VP8_COMP *cpi,
-  int dry_run) {
+static __inline void stuff1st_order_b_8x8(MACROBLOCKD *xd,
+                                          const BLOCKD *const b,
+                                          TOKENEXTRA **tp,
+                                          PLANE_TYPE type,
+                                          ENTROPY_CONTEXT *a,
+                                          ENTROPY_CONTEXT *l,
+                                          VP8_COMP *cpi,
+                                          int dry_run) {
   int pt; /* near block/prev token context index */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
 #if CONFIG_HYBRIDTRANSFORM8X8
   TX_TYPE tx_type = xd->mode_info_context->mbmi.mode == I8X8_PRED ?
       get_tx_type(xd, b) : DCT_DCT;
 #endif
-
+  const int band = vp8_coef_bands_8x8[(type == PLANE_TYPE_Y_NO_DC) ? 1 : 0];
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
-  (void) frametype;
-  (void) type;
   (void) b;
 
   t->Token = DCT_EOB_TOKEN;
 #if CONFIG_HYBRIDTRANSFORM8X8
   if (tx_type != DCT_DCT)
-    t->context_tree = cpi->common.fc.hybrid_coef_probs_8x8 [0] [1] [pt];
+    t->context_tree = cpi->common.fc.hybrid_coef_probs_8x8[type][band][pt];
   else
 #endif
-    t->context_tree = cpi->common.fc.coef_probs_8x8 [0] [1] [pt];
+    t->context_tree = cpi->common.fc.coef_probs_8x8[type][band][pt];
   // t->section = 8;
   t->skip_eob_node = 0;
   ++t;
@@ -1128,66 +1099,61 @@ static __inline void stuff1st_order_b_8x8
   if (!dry_run) {
 #if CONFIG_HYBRIDTRANSFORM8X8
     if (tx_type == DCT_DCT)
-      ++cpi->hybrid_coef_counts_8x8[0] [1] [pt] [DCT_EOB_TOKEN];
+      ++cpi->hybrid_coef_counts_8x8[type][band][pt][DCT_EOB_TOKEN];
     else
 #endif
-      ++cpi->coef_counts_8x8[0] [1] [pt] [DCT_EOB_TOKEN];
+      ++cpi->coef_counts_8x8[type][band][pt][DCT_EOB_TOKEN];
   }
   pt = 0; /* 0 <-> all coeff data is zero */
   *a = *l = pt;
-
-
 }
 
-static __inline
-void stuff1st_order_buv_8x8
-(
-  MACROBLOCKD *xd,
-  const BLOCKD *const b,
-  TOKENEXTRA **tp,
-  const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
-  const FRAME_TYPE frametype,
-  ENTROPY_CONTEXT *a,
-  ENTROPY_CONTEXT *l,
-  VP8_COMP *cpi,
-  int dry_run) {
+static __inline void stuff1st_order_buv_8x8(MACROBLOCKD *xd,
+                                            const BLOCKD *const b,
+                                            TOKENEXTRA **tp,
+                                            ENTROPY_CONTEXT *a,
+                                            ENTROPY_CONTEXT *l,
+                                            VP8_COMP *cpi,
+                                            int dry_run) {
   int pt; /* near block/prev token context index */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
-  (void) frametype;
-  (void) type;
   (void) b;
 
   t->Token = DCT_EOB_TOKEN;
-  t->context_tree = cpi->common.fc.coef_probs_8x8 [2] [0] [pt];
+  t->context_tree = cpi->common.fc.coef_probs_8x8[PLANE_TYPE_UV][0][pt];
   // t->section = 13;
   t->skip_eob_node = 0;
   ++t;
   *tp = t;
   if (!dry_run)
-    ++cpi->coef_counts_8x8[2] [0] [pt] [DCT_EOB_TOKEN];
+    ++cpi->coef_counts_8x8[PLANE_TYPE_UV][0][pt][DCT_EOB_TOKEN];
   pt = 0; /* 0 <-> all coeff data is zero */
   *a = *l = pt;
-
 }
 
-void vp8_stuff_mb_8x8(VP8_COMP *cpi,
-                      MACROBLOCKD *xd,
-                      TOKENEXTRA **t,
-                      int dry_run) {
+static void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *xd,
+                             TOKENEXTRA **t, int dry_run) {
   ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
   ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
-  int plane_type;
+  PLANE_TYPE plane_type;
   int b;
   TOKENEXTRA *t_backup = *t;
+  const int has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED
+                            && xd->mode_info_context->mbmi.mode != I8X8_PRED
+                            && xd->mode_info_context->mbmi.mode != SPLITMV);
 
-  stuff2nd_order_b_8x8(xd, xd->block + 24, t, 1, xd->frame_type,
-                       A + vp8_block2above_8x8[24],
-                       L + vp8_block2left_8x8[24], cpi, dry_run);
-  plane_type = 0;
+  if (has_y2_block) {
+    stuff2nd_order_b_8x8(xd, xd->block + 24, t,
+                         A + vp8_block2above_8x8[24],
+                         L + vp8_block2left_8x8[24], cpi, dry_run);
+    plane_type = PLANE_TYPE_Y_NO_DC;
+  } else {
+    plane_type = PLANE_TYPE_Y_WITH_DC;
+  }
 
   for (b = 0; b < 16; b += 4) {
-    stuff1st_order_b_8x8(xd, xd->block + b, t, plane_type, xd->frame_type,
+    stuff1st_order_b_8x8(xd, xd->block + b, t, plane_type,
                          A + vp8_block2above_8x8[b],
                          L + vp8_block2left_8x8[b],
                          cpi, dry_run);
@@ -1196,7 +1162,7 @@ void vp8_stuff_mb_8x8(VP8_COMP *cpi,
   }
 
   for (b = 16; b < 24; b += 4) {
-    stuff1st_order_buv_8x8(xd, xd->block + b, t, 2, xd->frame_type,
+    stuff1st_order_buv_8x8(xd, xd->block + b, t,
                            A + vp8_block2above[b],
                            L + vp8_block2left[b],
                            cpi, dry_run);
@@ -1207,62 +1173,60 @@ void vp8_stuff_mb_8x8(VP8_COMP *cpi,
     *t = t_backup;
 }
 
-static __inline
-void stuff1st_order_b_16x16(MACROBLOCKD *xd,
-                            const BLOCKD *const b,
-                            TOKENEXTRA **tp,
-                            const FRAME_TYPE frametype,
-                            ENTROPY_CONTEXT *a,
-                            ENTROPY_CONTEXT *l,
-                            VP8_COMP *cpi,
-                            int dry_run){
-    int pt; /* near block/prev token context index */
-    TOKENEXTRA *t = *tp;        /* store tokens starting here */
+static __inline void stuff1st_order_b_16x16(MACROBLOCKD *xd,
+                                            const BLOCKD *const b,
+                                            TOKENEXTRA **tp,
+                                            PLANE_TYPE type,
+                                            ENTROPY_CONTEXT *a,
+                                            ENTROPY_CONTEXT *l,
+                                            VP8_COMP *cpi,
+                                            int dry_run) {
+  int pt; /* near block/prev token context index */
+  TOKENEXTRA *t = *tp;        /* store tokens starting here */
 #if CONFIG_HYBRIDTRANSFORM16X16
-    TX_TYPE tx_type = get_tx_type(xd, b);
+  TX_TYPE tx_type = get_tx_type(xd, b);
 #endif
-    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
-    (void) frametype;
-    (void) b;
+  const int band = vp8_coef_bands_16x16[(type == PLANE_TYPE_Y_NO_DC) ? 1 : 0];
+  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
+  (void) b;
 
-    t->Token = DCT_EOB_TOKEN;
+  t->Token = DCT_EOB_TOKEN;
 #if CONFIG_HYBRIDTRANSFORM16X16
-    if (tx_type != DCT_DCT)
-      t->context_tree = cpi->common.fc.hybrid_coef_probs_16x16[3][1][pt];
-    else
+  if (tx_type != DCT_DCT)
+    t->context_tree = cpi->common.fc.hybrid_coef_probs_16x16[type][band][pt];
+  else
 #endif
-    t->context_tree = cpi->common.fc.coef_probs_16x16[3][1][pt];
-    t->skip_eob_node = 0;
-    ++t;
-    *tp = t;
-    if (!dry_run) {
+    t->context_tree = cpi->common.fc.coef_probs_16x16[type][band][pt];
+  t->skip_eob_node = 0;
+  ++t;
+  *tp = t;
+  if (!dry_run) {
 #if CONFIG_HYBRIDTRANSFORM16X16
-      if (tx_type != DCT_DCT)
-        ++cpi->hybrid_coef_counts_16x16[3][1][pt][DCT_EOB_TOKEN];
-      else
+    if (tx_type != DCT_DCT)
+      ++cpi->hybrid_coef_counts_16x16[type][band][pt][DCT_EOB_TOKEN];
+    else
 #endif
-      ++cpi->coef_counts_16x16[3][1][pt][DCT_EOB_TOKEN];
-    }
-    pt = 0; /* 0 <-> all coeff data is zero */
-    *a = *l = pt;
+      ++cpi->coef_counts_16x16[type][band][pt][DCT_EOB_TOKEN];
+  }
+  pt = 0; /* 0 <-> all coeff data is zero */
+  *a = *l = pt;
 }
 
-void vp8_stuff_mb_16x16(VP8_COMP *cpi,
-                        MACROBLOCKD *xd,
-                        TOKENEXTRA **t,
-                        int dry_run) {
+static void vp8_stuff_mb_16x16(VP8_COMP *cpi, MACROBLOCKD *xd,
+                               TOKENEXTRA **t, int dry_run) {
   ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)xd->above_context;
   ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)xd->left_context;
   int b, i;
   TOKENEXTRA *t_backup = *t;
 
-  stuff1st_order_b_16x16(xd, xd->block, t, xd->frame_type, A, L, cpi, dry_run);
+  stuff1st_order_b_16x16(xd, xd->block, t, PLANE_TYPE_Y_WITH_DC,
+                         A, L, cpi, dry_run);
   for (i = 1; i < 16; i++) {
     *(A + vp8_block2above[i]) = *(A);
     *(L +  vp8_block2left[i]) = *(L);
   }
   for (b = 16; b < 24; b += 4) {
-    stuff1st_order_buv_8x8(xd, xd->block + b, t, 2, xd->frame_type,
+    stuff1st_order_buv_8x8(xd, xd->block + b, t,
         A + vp8_block2above[b],
         L + vp8_block2left[b],
         cpi, dry_run);
@@ -1275,35 +1239,33 @@ void vp8_stuff_mb_16x16(VP8_COMP *cpi,
     *t = t_backup;
 }
 
-static __inline void stuff2nd_order_b_4x4
-(
-  MACROBLOCKD *xd,
-  const BLOCKD *const b,
-  TOKENEXTRA **tp,
-  ENTROPY_CONTEXT *a,
-  ENTROPY_CONTEXT *l,
-  VP8_COMP *cpi,
-  int dry_run) {
+static __inline void stuff2nd_order_b_4x4(MACROBLOCKD *xd,
+                                          const BLOCKD *const b,
+                                          TOKENEXTRA **tp,
+                                          ENTROPY_CONTEXT *a,
+                                          ENTROPY_CONTEXT *l,
+                                          VP8_COMP *cpi,
+                                          int dry_run) {
   int pt; /* near block/prev token context index */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
 
   t->Token = DCT_EOB_TOKEN;
-  t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
+  t->context_tree = cpi->common.fc.coef_probs[PLANE_TYPE_Y2][0][pt];
   t->skip_eob_node = 0;
   ++t;
   *tp = t;
   if (!dry_run)
-    ++cpi->coef_counts       [1] [0] [pt] [DCT_EOB_TOKEN];
+    ++cpi->coef_counts[PLANE_TYPE_Y2][0][pt] [DCT_EOB_TOKEN];
 
   pt = 0;
   *a = *l = pt;
-
 }
 
 static __inline void stuff1st_order_b_4x4(MACROBLOCKD *xd,
                                           const BLOCKD *const b,
                                           TOKENEXTRA **tp,
+                                          PLANE_TYPE type,
                                           ENTROPY_CONTEXT *a,
                                           ENTROPY_CONTEXT *l,
                                           VP8_COMP *cpi,
@@ -1313,69 +1275,76 @@ static __inline void stuff1st_order_b_4x4(MACROBLOCKD *xd,
 #if CONFIG_HYBRIDTRANSFORM
   TX_TYPE tx_type = get_tx_type(xd, b);
 #endif
+  const int band = vp8_coef_bands[(type == PLANE_TYPE_Y_NO_DC) ? 1 : 0];
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
 
   t->Token = DCT_EOB_TOKEN;
 #if CONFIG_HYBRIDTRANSFORM
   if (tx_type != DCT_DCT)
-    t->context_tree = cpi->common.fc.hybrid_coef_probs [0] [1] [pt];
+    t->context_tree = cpi->common.fc.hybrid_coef_probs[type][band][pt];
   else
 #endif
-    t->context_tree = cpi->common.fc.coef_probs [0] [1] [pt];
+    t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
   t->skip_eob_node = 0;
   ++t;
   *tp = t;
   if (!dry_run) {
 #if CONFIG_HYBRIDTRANSFORM
     if (tx_type != DCT_DCT)
-      ++cpi->hybrid_coef_counts[0] [1] [pt] [DCT_EOB_TOKEN];
+      ++cpi->hybrid_coef_counts[type][band][pt][DCT_EOB_TOKEN];
     else
 #endif
-      ++cpi->coef_counts[0] [1] [pt] [DCT_EOB_TOKEN];
+      ++cpi->coef_counts[type][band][pt][DCT_EOB_TOKEN];
   }
   pt = 0; /* 0 <-> all coeff data is zero */
   *a = *l = pt;
-
 }
-static __inline
-void stuff1st_order_buv_4x4
-(
-  MACROBLOCKD *xd,
-  const BLOCKD *const b,
-  TOKENEXTRA **tp,
-  ENTROPY_CONTEXT *a,
-  ENTROPY_CONTEXT *l,
-  VP8_COMP *cpi,
-  int dry_run) {
+
+static __inline void stuff1st_order_buv_4x4(MACROBLOCKD *xd,
+                                            const BLOCKD *const b,
+                                            TOKENEXTRA **tp,
+                                            ENTROPY_CONTEXT *a,
+                                            ENTROPY_CONTEXT *l,
+                                            VP8_COMP *cpi,
+                                            int dry_run) {
   int pt; /* near block/prev token context index */
   TOKENEXTRA *t = *tp;        /* store tokens starting here */
   VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
 
   t->Token = DCT_EOB_TOKEN;
-  t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
+  t->context_tree = cpi->common.fc.coef_probs[PLANE_TYPE_UV][0][pt];
   t->skip_eob_node = 0;
   ++t;
   *tp = t;
   if (!dry_run)
-    ++cpi->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
+    ++cpi->coef_counts[PLANE_TYPE_UV][0][pt][DCT_EOB_TOKEN];
   pt = 0; /* 0 <-> all coeff data is zero */
   *a = *l = pt;
 }
 
-void vp8_stuff_mb_4x4(VP8_COMP *cpi, MACROBLOCKD *xd,
-                      TOKENEXTRA **t, int dry_run) {
+static void vp8_stuff_mb_4x4(VP8_COMP *cpi, MACROBLOCKD *xd,
+                             TOKENEXTRA **t, int dry_run) {
   ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
   ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
   int b;
   TOKENEXTRA *t_backup = *t;
+  PLANE_TYPE plane_type;
+  const int has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED
+                            && xd->mode_info_context->mbmi.mode != I8X8_PRED
+                            && xd->mode_info_context->mbmi.mode != SPLITMV);
 
-  stuff2nd_order_b_4x4(xd, xd->block + 24, t,
-                       A + vp8_block2above[24],
-                       L + vp8_block2left[24],
-                       cpi, dry_run);
+  if (has_y2_block) {
+    stuff2nd_order_b_4x4(xd, xd->block + 24, t,
+                         A + vp8_block2above[24],
+                         L + vp8_block2left[24],
+                         cpi, dry_run);
+    plane_type = PLANE_TYPE_Y_NO_DC;
+  } else {
+    plane_type = PLANE_TYPE_Y_WITH_DC;
+  }
 
   for (b = 0; b < 16; b++)
-    stuff1st_order_b_4x4(xd, xd->block + b, t,
+    stuff1st_order_b_4x4(xd, xd->block + b, t, plane_type,
                          A + vp8_block2above[b],
                          L + vp8_block2left[b],
                          cpi, dry_run);
@@ -1390,23 +1359,15 @@ void vp8_stuff_mb_4x4(VP8_COMP *cpi, MACROBLOCKD *xd,
     *t = t_backup;
 }
 
-void vp8_stuff_mb_8x8_4x4uv(VP8_COMP *cpi,
-                            MACROBLOCKD *xd,
-                            TOKENEXTRA **t,
-                            int dry_run) {
+static void vp8_stuff_mb_8x8_4x4uv(VP8_COMP *cpi, MACROBLOCKD *xd,
+                                   TOKENEXTRA **t, int dry_run) {
   ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
   ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
-  int plane_type;
   int b;
   TOKENEXTRA *t_backup = *t;
 
-  stuff2nd_order_b_8x8(xd, xd->block + 24, t, 1, xd->frame_type,
-                       A + vp8_block2above_8x8[24],
-                       L + vp8_block2left_8x8[24], cpi, dry_run);
-  plane_type = 3;
-
   for (b = 0; b < 16; b += 4) {
-    stuff1st_order_b_8x8(xd, xd->block + b, t, plane_type, xd->frame_type,
+    stuff1st_order_b_8x8(xd, xd->block + b, t, PLANE_TYPE_Y_WITH_DC,
                          A + vp8_block2above_8x8[b],
                          L + vp8_block2left_8x8[b],
                          cpi, dry_run);
@@ -1424,6 +1385,22 @@ void vp8_stuff_mb_8x8_4x4uv(VP8_COMP *cpi,
     *t = t_backup;
 }
 
+void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run) {
+  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
+
+  if (tx_size == TX_16X16) {
+    vp8_stuff_mb_16x16(cpi, xd, t, dry_run);
+  } else if (tx_size == TX_8X8) {
+    if (xd->mode_info_context->mbmi.mode == I8X8_PRED) {
+      vp8_stuff_mb_8x8_4x4uv(cpi, xd, t, dry_run);
+    } else {
+      vp8_stuff_mb_8x8(cpi, xd, t, dry_run);
+    }
+  } else {
+    vp8_stuff_mb_4x4(cpi, xd, t, dry_run);
+  }
+}
+
 void vp8_fix_contexts(MACROBLOCKD *xd) {
   /* Clear entropy contexts for Y2 blocks */
   if ((xd->mode_info_context->mbmi.mode != B_PRED