shithub: libvpx

Download patch

ref: e788146247d47e0e24ad92259f6f016ffcf7b55a
parent: 41e3a9ef14ce3e1da623e1e7e255e9376af3b7bd
author: Ronald S. Bultje <rbultje@google.com>
date: Mon Oct 15 06:52:13 EDT 2012

Add a new token stuffing function vp8_stuff_mb().

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

--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -52,9 +52,6 @@
 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 @@
                                     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 @@
     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 @@
         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 @@
         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]++;
       }
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -805,6 +805,7 @@
 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);
 
--- a/vp8/encoder/tokenize.c
+++ b/vp8/encoder/tokenize.c
@@ -60,14 +60,7 @@
 #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 @@
     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,31 +1038,24 @@
 }
 #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;
@@ -1085,23 +1062,19 @@
 
   *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
@@ -1108,19 +1081,17 @@
   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 @@
   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 @@
   }
 
   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 @@
     *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[type][band][pt];
+  else
+#endif
+    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)
-      t->context_tree = cpi->common.fc.hybrid_coef_probs_16x16[3][1][pt];
+      ++cpi->hybrid_coef_counts_16x16[type][band][pt][DCT_EOB_TOKEN];
     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) {
-#if CONFIG_HYBRIDTRANSFORM16X16
-      if (tx_type != DCT_DCT)
-        ++cpi->hybrid_coef_counts_16x16[3][1][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 @@
     *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,15 +1275,16 @@
 #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;
@@ -1328,54 +1291,60 @@
   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 @@
     *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);
@@ -1422,6 +1383,22 @@
 
   if (dry_run)
     *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) {
--