Merge "Pass AV1_COMMON into av1_cost_coeffs" into nextgenv2
diff --git a/aom/aom_integer.h b/aom/aom_integer.h
index c70e696..2e8f23f 100644
--- a/aom/aom_integer.h
+++ b/aom/aom_integer.h
@@ -19,7 +19,7 @@
 #define AOM_FORCE_INLINE __forceinline
 #define AOM_INLINE __inline
 #else
-#define AOM_FORCE_INLINE __inline__ __attribute__(always_inline)
+#define AOM_FORCE_INLINE __inline__ __attribute__((always_inline))
 // TODO(jbb): Allow a way to force inline off for older compilers.
 #define AOM_INLINE inline
 #endif
diff --git a/aom_dsp/aom_dsp.mk b/aom_dsp/aom_dsp.mk
index 238853f..c74bfe3 100644
--- a/aom_dsp/aom_dsp.mk
+++ b/aom_dsp/aom_dsp.mk
@@ -388,5 +388,15 @@
 DSP_SRCS-yes += aom_dsp_rtcd_defs.pl
 
 DSP_SRCS-yes += aom_simd.c
+DSP_SRCS-yes += aom_simd.h
+DSP_SRCS-yes += aom_simd_inline.h
+DSP_SRCS-yes += simd/v64_intrinsics.h
+DSP_SRCS-yes += simd/v64_intrinsics_c.h
+DSP_SRCS-yes += simd/v128_intrinsics.h
+DSP_SRCS-yes += simd/v128_intrinsics_c.h
+DSP_SRCS-$(HAVE_SSE2) += simd/v64_intrinsics_x86.h
+DSP_SRCS-$(HAVE_SSE2) += simd/v128_intrinsics_x86.h
+DSP_SRCS-$(HAVE_NEON) += simd/v64_intrinsics_arm.h
+DSP_SRCS-$(HAVE_NEON) += simd/v128_intrinsics_arm.h
 
 $(eval $(call rtcd_h_template,aom_dsp_rtcd,aom_dsp/aom_dsp_rtcd_defs.pl))
diff --git a/aom_dsp/aom_dsp_rtcd_defs.pl b/aom_dsp/aom_dsp_rtcd_defs.pl
index 89f09fc..6d873bc 100644
--- a/aom_dsp/aom_dsp_rtcd_defs.pl
+++ b/aom_dsp/aom_dsp_rtcd_defs.pl
@@ -65,6 +65,36 @@
 add_proto qw/void aom_tm_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
 specialize qw/aom_tm_predictor_2x2/;
 
+add_proto qw/void aom_he_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_he_predictor_2x2/;
+
+add_proto qw/void aom_ve_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_ve_predictor_2x2/;
+
+add_proto qw/void aom_d207_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d207_predictor_2x2/;
+
+add_proto qw/void aom_d63_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d63_predictor_2x2/;
+
+add_proto qw/void aom_d63f_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d63f_predictor_2x2/;
+
+add_proto qw/void aom_d45_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d45_predictor_2x2/;
+
+add_proto qw/void aom_d45e_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d45e_predictor_2x2/;
+
+add_proto qw/void aom_d117_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d117_predictor_2x2/;
+
+add_proto qw/void aom_d135_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d135_predictor_2x2/;
+
+add_proto qw/void aom_d153_predictor_2x2/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
+specialize qw/aom_d153_predictor_2x2/;
+
 add_proto qw/void aom_d207_predictor_4x4/, "uint8_t *dst, ptrdiff_t y_stride, const uint8_t *above, const uint8_t *left";
 specialize qw/aom_d207_predictor_4x4 sse2/;
 
diff --git a/aom_dsp/aom_simd.h b/aom_dsp/aom_simd.h
index 7ffca4a..3879d95 100644
--- a/aom_dsp/aom_simd.h
+++ b/aom_dsp/aom_simd.h
@@ -9,20 +9,8 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */
 
-#ifndef _AOM_SIMD_H
-#define _AOM_SIMD_H
-
-#ifndef SIMD_INLINE
-#ifdef __GNUC__
-#define SIMD_INLINE static inline __attribute__((always_inline))
-#elif __STDC_VERSION__ >= 199901L
-#define SIMD_INLINE static inline
-#elif defined(_MSC_VER)
-#define SIMD_INLINE static __inline
-#else
-#define SIMD_INLINE static
-#endif
-#endif
+#ifndef AOM_DSP_AOM_AOM_SIMD_H_
+#define AOM_DSP_AOM_AOM_SIMD_H_
 
 #include <stdint.h>
 
@@ -31,6 +19,7 @@
 #endif
 
 #include "./aom_config.h"
+#include "./aom_simd_inline.h"
 
 #if HAVE_NEON
 #include "simd/v128_intrinsics_arm.h"
@@ -40,4 +29,4 @@
 #include "simd/v128_intrinsics.h"
 #endif
 
-#endif /* _AOM_SIMD_H */
+#endif  // AOM_DSP_AOM_AOM_SIMD_H_
diff --git a/aom_dsp/aom_simd_inline.h b/aom_dsp/aom_simd_inline.h
new file mode 100644
index 0000000..02a8b3a
--- /dev/null
+++ b/aom_dsp/aom_simd_inline.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#ifndef AOM_DSP_AOM_SIMD_INLINE_H_
+#define AOM_DSP_AOM_SIMD_INLINE_H_
+
+#include "aom/aom_integer.h"
+
+#ifndef SIMD_INLINE
+#define SIMD_INLINE static AOM_FORCE_INLINE
+#endif
+
+#endif  // AOM_DSP_AOM_SIMD_INLINE_H_
diff --git a/aom_dsp/daalaboolwriter.c b/aom_dsp/daalaboolwriter.c
index 15a3af7..0ba8f6a 100644
--- a/aom_dsp/daalaboolwriter.c
+++ b/aom_dsp/daalaboolwriter.c
@@ -28,4 +28,5 @@
      Must always be added, so that rawbits knows the exact length of the
       bitstream. */
   br->buffer[br->pos++] = 0;
+  od_ec_enc_clear(&br->ec);
 }
diff --git a/aom_dsp/intrapred.c b/aom_dsp/intrapred.c
index c3af1f4..b513dc5 100644
--- a/aom_dsp/intrapred.c
+++ b/aom_dsp/intrapred.c
@@ -321,6 +321,134 @@
   }
 }
 
+void aom_he_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                            const uint8_t *above, const uint8_t *left) {
+  const int H = above[-1];
+  const int I = left[0];
+  const int J = left[1];
+  const int K = left[2];
+
+  memset(dst + stride * 0, AVG3(H, I, J), 2);
+  memset(dst + stride * 1, AVG3(I, J, K), 2);
+}
+
+void aom_ve_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                            const uint8_t *above, const uint8_t *left) {
+  const int H = above[-1];
+  const int I = above[0];
+  const int J = above[1];
+  const int K = above[2];
+
+  dst[0] = AVG3(H, I, J);
+  dst[1] = AVG3(I, J, K);
+  memcpy(dst + stride * 1, dst, 2);
+}
+
+void aom_d207_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int I = left[0];
+  const int J = left[1];
+  const int K = left[2];
+  const int L = left[3];
+  (void)above;
+  DST(0, 0) = AVG2(I, J);
+  DST(0, 1) = AVG2(J, K);
+  DST(1, 0) = AVG3(I, J, K);
+  DST(1, 1) = AVG3(J, K, L);
+}
+
+void aom_d63_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                             const uint8_t *above, const uint8_t *left) {
+  const int A = above[0];
+  const int B = above[1];
+  const int C = above[2];
+  const int D = above[3];
+  (void)left;
+  DST(0, 0) = AVG2(A, B);
+  DST(1, 0) = AVG2(B, C);
+  DST(0, 1) = AVG3(A, B, C);
+  DST(1, 1) = AVG3(B, C, D);
+}
+
+void aom_d63f_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int A = above[0];
+  const int B = above[1];
+  const int C = above[2];
+  const int D = above[3];
+  (void)left;
+  DST(0, 0) = AVG2(A, B);
+  DST(1, 0) = AVG2(B, C);
+  DST(0, 1) = AVG3(A, B, C);
+  DST(1, 1) = AVG3(B, C, D);
+}
+
+void aom_d45_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                             const uint8_t *above, const uint8_t *left) {
+  const int A = above[0];
+  const int B = above[1];
+  const int C = above[2];
+  const int D = above[3];
+  const int E = above[4];
+  (void)stride;
+  (void)left;
+  DST(0, 0) = AVG3(A, B, C);
+  DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
+  DST(1, 1) = AVG3(C, D, E);
+}
+
+void aom_d45e_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int A = above[0];
+  const int B = above[1];
+  const int C = above[2];
+  const int D = above[3];
+  const int E = above[4];
+  (void)stride;
+  (void)left;
+  DST(0, 0) = AVG3(A, B, C);
+  DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
+  DST(1, 1) = AVG3(C, D, E);
+}
+
+void aom_d117_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int I = left[0];
+  const int X = above[-1];
+  const int A = above[0];
+  const int B = above[1];
+  DST(0, 0) = AVG2(X, A);
+  DST(1, 0) = AVG2(A, B);
+  DST(0, 1) = AVG3(I, X, A);
+  DST(1, 1) = AVG3(X, A, B);
+}
+
+void aom_d135_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int I = left[0];
+  const int J = left[1];
+  const int X = above[-1];
+  const int A = above[0];
+  const int B = above[1];
+  (void)stride;
+  DST(0, 1) = AVG3(X, I, J);
+  DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
+  DST(1, 0) = AVG3(B, A, X);
+}
+
+void aom_d153_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
+                              const uint8_t *above, const uint8_t *left) {
+  const int I = left[0];
+  const int J = left[1];
+  const int X = above[-1];
+  const int A = above[0];
+
+  DST(0, 0) = AVG2(I, X);
+  DST(0, 1) = AVG2(J, I);
+  DST(1, 0) = AVG3(I, X, A);
+  DST(1, 1) = AVG3(J, I, X);
+}
+
 void aom_he_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                             const uint8_t *above, const uint8_t *left) {
   const int H = above[-1];
diff --git a/aomenc.c b/aomenc.c
index 373b69d..8eb30ed 100644
--- a/aomenc.c
+++ b/aomenc.c
@@ -392,12 +392,15 @@
     ARG_DEF(NULL, "frame-parallel", 1,
             "Enable frame parallel decodability features "
             "(0: false (default), 1: true)");
+#if CONFIG_DELTA_Q
 static const arg_def_t aq_mode = ARG_DEF(
     NULL, "aq-mode", 1,
     "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
-#if CONFIG_DELTA_Q
     "3: cyclic refresh, 4: delta quant)");
 #else
+static const arg_def_t aq_mode = ARG_DEF(
+    NULL, "aq-mode", 1,
+    "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
     "3: cyclic refresh)");
 #endif
 static const arg_def_t frame_periodic_boost =
@@ -2068,8 +2071,9 @@
           print_time("ETA", estimated_time_left);
         }
 
-      } else
+      } else {
         frame_avail = 0;
+      }
 
       if (frames_in > global.skip_frames) {
 #if CONFIG_AOM_HIGHBITDEPTH
diff --git a/av1/av1_common.mk b/av1/av1_common.mk
index 29b71f3..38fb6fd 100644
--- a/av1/av1_common.mk
+++ b/av1/av1_common.mk
@@ -92,7 +92,7 @@
 AV1_COMMON_SRCS-yes += common/clpf_simd.h
 AV1_COMMON_SRCS-$(HAVE_SSE2) += common/clpf_sse2.c
 AV1_COMMON_SRCS-$(HAVE_SSSE3) += common/clpf_ssse3.c
-AV1_COMMON_SRCS-$(HAVE_SSE4_1) += common/clpf_sse4_1.c
+AV1_COMMON_SRCS-$(HAVE_SSE4_1) += common/clpf_sse4.c
 AV1_COMMON_SRCS-$(HAVE_NEON) += common/clpf_neon.c
 endif
 ifeq ($(CONFIG_DERING),yes)
@@ -139,9 +139,8 @@
 AV1_COMMON_SRCS-$(HAVE_NEON) += common/arm/neon/iht8x8_add_neon.c
 endif
 
-ifeq ($(CONFIG_EXT_INTRA),yes)
-AV1_COMMON_SRCS-yes += common/intra_filters.h
-AV1_COMMON_SRCS-$(HAVE_SSE4_1) += common/x86/reconintra_sse4.c
+ifeq ($(CONFIG_FILTER_INTRA),yes)
+AV1_COMMON_SRCS-$(HAVE_SSE4_1) += common/x86/filterintra_sse4.c
 endif
 
 AV1_COMMON_SRCS-$(HAVE_SSE2) += common/x86/av1_inv_txfm_sse2.c
diff --git a/av1/common/av1_rtcd_defs.pl b/av1/common/av1_rtcd_defs.pl
index 3e1b02f..53c9dc5 100644
--- a/av1/common/av1_rtcd_defs.pl
+++ b/av1/common/av1_rtcd_defs.pl
@@ -199,8 +199,8 @@
   specialize qw/quantize_32x32_fp_nuq/;
 }
 
-# EXT_INTRA predictor functions
-if (aom_config("CONFIG_EXT_INTRA") eq "yes") {
+# FILTER_INTRA predictor functions
+if (aom_config("CONFIG_FILTER_INTRA") eq "yes") {
   add_proto qw/void av1_dc_filter_predictor/, "uint8_t *dst, ptrdiff_t stride, int bs, const uint8_t *above, const uint8_t *left";
   specialize qw/av1_dc_filter_predictor sse4_1/;
   add_proto qw/void av1_v_filter_predictor/, "uint8_t *dst, ptrdiff_t stride, int bs, const uint8_t *above, const uint8_t *left";
diff --git a/av1/common/blockd.c b/av1/common/blockd.c
index e3c3ea5..8938bbf 100644
--- a/av1/common/blockd.c
+++ b/av1/common/blockd.c
@@ -150,8 +150,6 @@
   54,   49,    45,   40,   35,   31,   26,   22,   17,   13,   8,    4,
 };
 
-// Returns whether filter selection is needed for a given
-// intra prediction angle.
 int av1_is_intra_filter_switchable(int angle) {
   assert(angle > 0 && angle < 270);
   if (angle % 45 == 0) return 0;
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 3ff4943..6296faa 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -171,13 +171,13 @@
 } PALETTE_MODE_INFO;
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
 typedef struct {
   // 1: an ext intra mode is used; 0: otherwise.
-  uint8_t use_ext_intra_mode[PLANE_TYPES];
-  EXT_INTRA_MODE ext_intra_mode[PLANE_TYPES];
-} EXT_INTRA_MODE_INFO;
-#endif  // CONFIG_EXT_INTRA
+  uint8_t use_filter_intra_mode[PLANE_TYPES];
+  FILTER_INTRA_MODE filter_intra_mode[PLANE_TYPES];
+} FILTER_INTRA_MODE_INFO;
+#endif  // CONFIG_FILTER_INTRA
 
 // This structure now relates to 8x8 block regions.
 typedef struct {
@@ -214,8 +214,10 @@
   MV_REFERENCE_FRAME ref_frame[2];
   TX_TYPE tx_type;
 
+#if CONFIG_FILTER_INTRA
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_EXT_INTRA
-  EXT_INTRA_MODE_INFO ext_intra_mode_info;
   int8_t angle_delta[2];
   // To-Do (huisu): this may be replaced by interp_filter
   INTRA_FILTER intra_filter;
@@ -563,17 +565,7 @@
 #endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
 }
 
-#if CONFIG_EXT_INTRA
-#define ALLOW_FILTER_INTRA_MODES 1
-#define ANGLE_STEP 3
-#define MAX_ANGLE_DELTAS 3
-
-extern const int16_t dr_intra_derivative[90];
-
-static const uint8_t mode_to_angle_map[INTRA_MODES] = {
-  0, 90, 180, 45, 135, 111, 157, 203, 67, 0,
-};
-
+#if CONFIG_FILTER_INTRA
 static const TX_TYPE filter_intra_mode_to_tx_type_lookup[FILTER_INTRA_MODES] = {
   DCT_DCT,    // FILTER_DC
   ADST_DCT,   // FILTER_V
@@ -586,7 +578,18 @@
   ADST_DCT,   // FILTER_D63
   ADST_ADST,  // FILTER_TM
 };
+#endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_EXT_INTRA
+#define ANGLE_STEP 3
+#define MAX_ANGLE_DELTAS 3
+extern const int16_t dr_intra_derivative[90];
+static const uint8_t mode_to_angle_map[INTRA_MODES] = {
+  0, 90, 180, 45, 135, 111, 157, 203, 67, 0,
+};
+
+// Returns whether filter selection is needed for a given
+// intra prediction angle.
 int av1_is_intra_filter_switchable(int angle);
 #endif  // CONFIG_EXT_INTRA
 
@@ -618,15 +621,19 @@
   if (FIXED_TX_TYPE)
     return get_default_tx_type(plane_type, xd, block_idx, tx_size);
 
-#if CONFIG_EXT_INTRA
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
   if (!is_inter_block(mbmi)) {
-    const int use_ext_intra_mode_info =
-        mbmi->ext_intra_mode_info.use_ext_intra_mode[plane_type];
-    const EXT_INTRA_MODE ext_intra_mode =
-        mbmi->ext_intra_mode_info.ext_intra_mode[plane_type];
+#if CONFIG_FILTER_INTRA
+    const int use_filter_intra_mode_info =
+        mbmi->filter_intra_mode_info.use_filter_intra_mode[plane_type];
+    const FILTER_INTRA_MODE filter_intra_mode =
+        mbmi->filter_intra_mode_info.filter_intra_mode[plane_type];
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
     const PREDICTION_MODE mode = (plane_type == PLANE_TYPE_Y)
                                      ? get_y_mode(mi, block_idx)
                                      : mbmi->uv_mode;
+#endif  // CONFIG_EXT_INTRA
 
     if (xd->lossless[mbmi->segment_id] || tx_size >= TX_32X32) return DCT_DCT;
 
@@ -637,9 +644,11 @@
 #endif  // ALLOW_INTRA_EXT_TX
 #endif  // CONFIG_EXT_TX
 
-    if (use_ext_intra_mode_info)
-      return filter_intra_mode_to_tx_type_lookup[ext_intra_mode];
-
+#if CONFIG_FILTER_INTRA
+    if (use_filter_intra_mode_info)
+      return filter_intra_mode_to_tx_type_lookup[filter_intra_mode];
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
     if (mode == DC_PRED) {
       return DCT_DCT;
     } else if (mode == TM_PRED) {
@@ -658,8 +667,9 @@
       else
         return DCT_ADST;
     }
-  }
 #endif  // CONFIG_EXT_INTRA
+  }
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
 
 #if CONFIG_EXT_TX
 #if EXT_TX_SIZES == 4
diff --git a/av1/common/clpf_sse4_1.c b/av1/common/clpf_sse4.c
similarity index 100%
rename from av1/common/clpf_sse4_1.c
rename to av1/common/clpf_sse4.c
diff --git a/av1/common/common_data.h b/av1/common/common_data.h
index d7eb4b1..5b72b81 100644
--- a/av1/common/common_data.h
+++ b/av1/common/common_data.h
@@ -442,6 +442,17 @@
 #endif       // CONFIG_EXT_TX
 };
 
+static const int tx_size_1d[TX_SIZES] = { 4, 8, 16, 32 };
+
+static const int tx_size_2d[TX_SIZES] = { 16, 64, 256, 1024 };
+
+static const uint8_t tx_size_1d_log2[TX_SIZES] = { 2, 3, 4, 5 };
+
+static const int tx_size_1d_in_unit[TX_SIZES] = { 1, 2, 4, 8 };
+
+// TODO(jingning): Temporary table during the construction.
+static const int tx_size_1d_in_unit_log2[TX_SIZES] = { 0, 1, 2, 3 };
+
 static const BLOCK_SIZE txsize_to_bsize[TX_SIZES_ALL] = {
   BLOCK_4X4,    // TX_4X4
   BLOCK_8X8,    // TX_8X8
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 3f6f0da..20e8904 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -1313,14 +1313,16 @@
                                                   { 49, 25, 24 },
                                                   { 72, 38, 50 },
                                                 };
-static const aom_prob default_ext_intra_probs[2] = { 230, 230 };
-
 const aom_tree_index av1_intra_filter_tree[TREE_SIZE(INTRA_FILTERS)] = {
   -INTRA_FILTER_LINEAR,      2, -INTRA_FILTER_8TAP, 4, -INTRA_FILTER_8TAP_SHARP,
   -INTRA_FILTER_8TAP_SMOOTH,
 };
 #endif  // CONFIG_EXT_INTRA
 
+#if CONFIG_FILTER_INTRA
+static const aom_prob default_filter_intra_probs[2] = { 230, 230 };
+#endif  // CONFIG_FILTER_INTRA
+
 #if CONFIG_SUPERTX
 static const aom_prob default_supertx_prob[PARTITION_SUPERTX_CONTEXTS]
                                           [TX_SIZES] = {
@@ -1385,9 +1387,11 @@
   av1_copy(fc->seg.tree_probs, default_segment_tree_probs);
   av1_copy(fc->seg.pred_probs, default_segment_pred_probs);
 #if CONFIG_EXT_INTRA
-  av1_copy(fc->ext_intra_probs, default_ext_intra_probs);
   av1_copy(fc->intra_filter_probs, default_intra_filter_probs);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  av1_copy(fc->filter_intra_probs, default_filter_intra_probs);
+#endif  // CONFIG_FILTER_INTRA
   av1_copy(fc->inter_ext_tx_prob, default_inter_ext_tx_prob);
   av1_copy(fc->intra_ext_tx_prob, default_intra_ext_tx_prob);
 #if CONFIG_LOOP_RESTORATION
@@ -1657,22 +1661,23 @@
 #endif
   }
 #endif  // CONFIG_EXT_PARTITION_TYPES
-
 #if CONFIG_DELTA_Q
   for (i = 0; i < DELTA_Q_CONTEXTS; ++i)
     fc->delta_q_prob[i] =
         mode_mv_merge_probs(pre_fc->delta_q_prob[i], counts->delta_q[i]);
 #endif
 #if CONFIG_EXT_INTRA
-  for (i = 0; i < PLANE_TYPES; ++i) {
-    fc->ext_intra_probs[i] = av1_mode_mv_merge_probs(pre_fc->ext_intra_probs[i],
-                                                     counts->ext_intra[i]);
-  }
-
-  for (i = 0; i < INTRA_FILTERS + 1; ++i)
+  for (i = 0; i < INTRA_FILTERS + 1; ++i) {
     aom_tree_merge_probs(av1_intra_filter_tree, pre_fc->intra_filter_probs[i],
                          counts->intra_filter[i], fc->intra_filter_probs[i]);
+  }
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  for (i = 0; i < PLANE_TYPES; ++i) {
+    fc->filter_intra_probs[i] = av1_mode_mv_merge_probs(
+        pre_fc->filter_intra_probs[i], counts->filter_intra[i]);
+  }
+#endif  // CONFIG_FILTER_INTRA
 }
 
 static void set_default_lf_deltas(struct loopfilter *lf) {
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index c58fbb3..85c68e1 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -153,9 +153,11 @@
 #endif  // CONFIG_SUPERTX
   struct segmentation_probs seg;
 #if CONFIG_EXT_INTRA
-  aom_prob ext_intra_probs[PLANE_TYPES];
   aom_prob intra_filter_probs[INTRA_FILTERS + 1][INTRA_FILTERS - 1];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  aom_prob filter_intra_probs[PLANE_TYPES];
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_GLOBAL_MOTION
   aom_prob global_motion_types_prob[GLOBAL_MOTION_TYPES - 1];
 #endif  // CONFIG_GLOBAL_MOTION
@@ -265,9 +267,11 @@
 #endif  // CONFIG_SUPERTX
   struct seg_counts seg;
 #if CONFIG_EXT_INTRA
-  unsigned int ext_intra[PLANE_TYPES][2];
   unsigned int intra_filter[INTRA_FILTERS + 1][INTRA_FILTERS];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  unsigned int filter_intra[PLANE_TYPES][2];
+#endif  // CONFIG_FILTER_INTRA
 } FRAME_COUNTS;
 
 extern const aom_prob av1_kf_y_mode_prob[INTRA_MODES][INTRA_MODES]
diff --git a/av1/common/enums.h b/av1/common/enums.h
index ce6032b..b02c814 100644
--- a/av1/common/enums.h
+++ b/av1/common/enums.h
@@ -318,7 +318,7 @@
 
 #endif  // CONFIG_EXT_INTER
 
-#if CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
 typedef enum {
   FILTER_DC_PRED,
   FILTER_V_PRED,
@@ -330,10 +330,11 @@
   FILTER_D207_PRED,
   FILTER_D63_PRED,
   FILTER_TM_PRED,
-  EXT_INTRA_MODES,
-} EXT_INTRA_MODE;
+  FILTER_INTRA_MODES,
+} FILTER_INTRA_MODE;
+#endif  // CONFIG_FILTER_INTRA
 
-#define FILTER_INTRA_MODES (FILTER_TM_PRED + 1)
+#if CONFIG_EXT_INTRA
 #define DIRECTIONAL_MODES (INTRA_MODES - 2)
 #endif  // CONFIG_EXT_INTRA
 
diff --git a/av1/common/intra_filters.h b/av1/common/intra_filters.h
deleted file mode 100644
index 350f7ca..0000000
--- a/av1/common/intra_filters.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef AV1_COMMON_INTRA_FILTERS_H_
-#define AV1_COMMON_INTRA_FILTERS_H_
-
-#define FILTER_INTRA_PREC_BITS (10)
-
-static int filter_intra_taps_4[TX_SIZES][INTRA_MODES][4] = {
-  {
-      { 735, 881, -537, -54 },
-      { 1005, 519, -488, -11 },
-      { 383, 990, -343, -6 },
-      { 442, 805, -542, 319 },
-      { 658, 616, -133, -116 },
-      { 875, 442, -141, -151 },
-      { 386, 741, -23, -80 },
-      { 390, 1027, -446, 51 },
-      { 679, 606, -523, 262 },
-      { 903, 922, -778, -23 },
-  },
-  {
-      { 648, 803, -444, 16 },
-      { 972, 620, -576, 7 },
-      { 561, 967, -499, -5 },
-      { 585, 762, -468, 144 },
-      { 596, 619, -182, -9 },
-      { 895, 459, -176, -153 },
-      { 557, 722, -126, -129 },
-      { 601, 839, -523, 105 },
-      { 562, 709, -499, 251 },
-      { 803, 872, -695, 43 },
-  },
-  {
-      { 423, 728, -347, 111 },
-      { 963, 685, -665, 23 },
-      { 281, 1024, -480, 216 },
-      { 640, 596, -437, 78 },
-      { 429, 669, -259, 99 },
-      { 740, 646, -415, 23 },
-      { 568, 771, -346, 40 },
-      { 404, 833, -486, 209 },
-      { 398, 712, -423, 307 },
-      { 939, 935, -887, 17 },
-  },
-  {
-      { 477, 737, -393, 150 },
-      { 881, 630, -546, 67 },
-      { 506, 984, -443, -20 },
-      { 114, 459, -270, 528 },
-      { 433, 528, 14, 3 },
-      { 837, 470, -301, -30 },
-      { 181, 777, 89, -107 },
-      { -29, 716, -232, 259 },
-      { 589, 646, -495, 255 },
-      { 740, 884, -728, 77 },
-  },
-};
-
-#endif  // AV1_COMMON_INTRA_FILTERS_H_
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index bbcedc4..afc9da4 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -195,13 +195,6 @@
   int new_fb_idx;
 
   FRAME_TYPE last_frame_type; /* last frame's frame type for motion search.*/
-#if CONFIG_EXT_REFS
-  // frame type of the frame before last frame
-  FRAME_TYPE last2_frame_type;
-  // TODO(zoeliu): To check whether last3_frame_type is still needed.
-  // frame type of the frame two frames before last frame
-  FRAME_TYPE last3_frame_type;
-#endif  // CONFIG_EXT_REFS
   FRAME_TYPE frame_type;
 
   int show_frame;
diff --git a/av1/common/pred_common.c b/av1/common/pred_common.c
index 5d8cad0..35067f2 100644
--- a/av1/common/pred_common.c
+++ b/av1/common/pred_common.c
@@ -167,101 +167,60 @@
 }
 
 #if CONFIG_EXT_REFS
-
-#define CHECK_BWDREF_OR_ALTREF(ref_frame) \
-  (((ref_frame) == BWDREF_FRAME) || ((ref_frame) == ALTREF_FRAME))
-
-int av1_get_reference_mode_context(const AV1_COMMON *cm,
-                                   const MACROBLOCKD *xd) {
-  int ctx;
-  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
-  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
-  const int has_above = xd->up_available;
-  const int has_left = xd->left_available;
-
-  (void)cm;
-
-  // Note:
-  // The mode info data structure has a one element border above and to the
-  // left of the entries corresponding to real macroblocks.
-  // The prediction flags in these dummy entries are initialized to 0.
-  if (has_above && has_left) {  // both edges available
-    if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
-      // neither edge uses comp pred (0/1)
-      ctx = CHECK_BWDREF_OR_ALTREF(above_mbmi->ref_frame[0]) ^
-            CHECK_BWDREF_OR_ALTREF(left_mbmi->ref_frame[0]);
-    else if (!has_second_ref(above_mbmi))
-      // one of two edges uses comp pred (2/3)
-      ctx = 2 + (CHECK_BWDREF_OR_ALTREF(above_mbmi->ref_frame[0]) ||
-                 !is_inter_block(above_mbmi));
-    else if (!has_second_ref(left_mbmi))
-      // one of two edges uses comp pred (2/3)
-      ctx = 2 + (CHECK_BWDREF_OR_ALTREF(left_mbmi->ref_frame[0]) ||
-                 !is_inter_block(left_mbmi));
-    else  // both edges use comp pred (4)
-      ctx = 4;
-  } else if (has_above || has_left) {  // one edge available
-    const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
-
-    if (!has_second_ref(edge_mbmi))
-      // edge does not use comp pred (0/1)
-      ctx = CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]);
-    else
-      // edge uses comp pred (3)
-      ctx = 3;
-  } else {  // no edges available (1)
-    ctx = 1;
-  }
-  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
-  return ctx;
-}
-
-#else  // CONFIG_EXT_REFS
-
-int av1_get_reference_mode_context(const AV1_COMMON *cm,
-                                   const MACROBLOCKD *xd) {
-  int ctx;
-  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
-  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
-  const int has_above = xd->up_available;
-  const int has_left = xd->left_available;
-  // Note:
-  // The mode info data structure has a one element border above and to the
-  // left of the entries corresponding to real macroblocks.
-  // The prediction flags in these dummy entries are initialized to 0.
-  if (has_above && has_left) {  // both edges available
-    if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
-      // neither edge uses comp pred (0/1)
-      ctx = (above_mbmi->ref_frame[0] == cm->comp_fixed_ref) ^
-            (left_mbmi->ref_frame[0] == cm->comp_fixed_ref);
-    else if (!has_second_ref(above_mbmi))
-      // one of two edges uses comp pred (2/3)
-      ctx = 2 + (above_mbmi->ref_frame[0] == cm->comp_fixed_ref ||
-                 !is_inter_block(above_mbmi));
-    else if (!has_second_ref(left_mbmi))
-      // one of two edges uses comp pred (2/3)
-      ctx = 2 + (left_mbmi->ref_frame[0] == cm->comp_fixed_ref ||
-                 !is_inter_block(left_mbmi));
-    else  // both edges use comp pred (4)
-      ctx = 4;
-  } else if (has_above || has_left) {  // one edge available
-    const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
-
-    if (!has_second_ref(edge_mbmi))
-      // edge does not use comp pred (0/1)
-      ctx = edge_mbmi->ref_frame[0] == cm->comp_fixed_ref;
-    else
-      // edge uses comp pred (3)
-      ctx = 3;
-  } else {  // no edges available (1)
-    ctx = 1;
-  }
-  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
-  return ctx;
-}
-
+#define CHECK_BACKWARD_REFS(ref_frame) \
+  (((ref_frame) >= BWDREF_FRAME) && ((ref_frame) <= ALTREF_FRAME))
+#define IS_BACKWARD_REF_FRAME(ref_frame) CHECK_BACKWARD_REFS(ref_frame)
+#else
+#define IS_BACKWARD_REF_FRAME(ref_frame) ((ref_frame) == cm->comp_fixed_ref)
 #endif  // CONFIG_EXT_REFS
 
+int av1_get_reference_mode_context(const AV1_COMMON *cm,
+                                   const MACROBLOCKD *xd) {
+  int ctx;
+  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
+  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
+  const int has_above = xd->up_available;
+  const int has_left = xd->left_available;
+
+#if CONFIG_EXT_REFS
+  (void)cm;
+#endif  // CONFIG_EXT_REFS
+
+  // Note:
+  // The mode info data structure has a one element border above and to the
+  // left of the entries corresponding to real macroblocks.
+  // The prediction flags in these dummy entries are initialized to 0.
+  if (has_above && has_left) {  // both edges available
+    if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
+      // neither edge uses comp pred (0/1)
+      ctx = IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ^
+            IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]);
+    else if (!has_second_ref(above_mbmi))
+      // one of two edges uses comp pred (2/3)
+      ctx = 2 + (IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ||
+                 !is_inter_block(above_mbmi));
+    else if (!has_second_ref(left_mbmi))
+      // one of two edges uses comp pred (2/3)
+      ctx = 2 + (IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]) ||
+                 !is_inter_block(left_mbmi));
+    else  // both edges use comp pred (4)
+      ctx = 4;
+  } else if (has_above || has_left) {  // one edge available
+    const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
+
+    if (!has_second_ref(edge_mbmi))
+      // edge does not use comp pred (0/1)
+      ctx = IS_BACKWARD_REF_FRAME(edge_mbmi->ref_frame[0]);
+    else
+      // edge uses comp pred (3)
+      ctx = 3;
+  } else {  // no edges available (1)
+    ctx = 1;
+  }
+  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
+  return ctx;
+}
+
 #if CONFIG_EXT_REFS
 
 // TODO(zoeliu): Future work will be conducted to optimize the context design
@@ -323,8 +282,8 @@
       if (frfa == frfl && CHECK_GOLDEN_OR_LAST3(frfa)) {
         pred_context = 0;
       } else if (l_sg && a_sg) {  // single/single
-        if ((CHECK_BWDREF_OR_ALTREF(frfa) && CHECK_LAST_OR_LAST2(frfl)) ||
-            (CHECK_BWDREF_OR_ALTREF(frfl) && CHECK_LAST_OR_LAST2(frfa))) {
+        if ((CHECK_BACKWARD_REFS(frfa) && CHECK_LAST_OR_LAST2(frfl)) ||
+            (CHECK_BACKWARD_REFS(frfl) && CHECK_LAST_OR_LAST2(frfa))) {
           pred_context = 4;
         } else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl)) {
           pred_context = 1;
@@ -426,7 +385,7 @@
         else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl))
           pred_context = 2 + (frfa != frfl);
         else if (frfa == frfl ||
-                 (CHECK_BWDREF_OR_ALTREF(frfa) && CHECK_BWDREF_OR_ALTREF(frfl)))
+                 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
           pred_context = 3;
         else
           pred_context = 4;
@@ -527,7 +486,7 @@
         else if (CHECK_LAST_OR_LAST2(frfa) || CHECK_LAST_OR_LAST2(frfl))
           pred_context = 2 + (frfa != frfl);
         else if (frfa == frfl ||
-                 (CHECK_BWDREF_OR_ALTREF(frfa) && CHECK_BWDREF_OR_ALTREF(frfl)))
+                 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
           pred_context = 3;
         else
           pred_context = 4;
@@ -798,10 +757,10 @@
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
 
       if (!has_second_ref(edge_mbmi))
-        pred_context = 4 * (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]));
+        pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
       else
-        pred_context = 1 + (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]) ||
-                            !CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[1]));
+        pred_context = 1 + (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) ||
+                            !CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[1]));
     } else {  // inter/inter
       const int above_has_second = has_second_ref(above_mbmi);
       const int left_has_second = has_second_ref(left_mbmi);
@@ -812,24 +771,23 @@
       const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
 
       if (above_has_second && left_has_second) {
-        pred_context = 1 + (!CHECK_BWDREF_OR_ALTREF(above0) ||
-                            !CHECK_BWDREF_OR_ALTREF(above1) ||
-                            !CHECK_BWDREF_OR_ALTREF(left0) ||
-                            !CHECK_BWDREF_OR_ALTREF(left1));
+        pred_context =
+            1 + (!CHECK_BACKWARD_REFS(above0) || !CHECK_BACKWARD_REFS(above1) ||
+                 !CHECK_BACKWARD_REFS(left0) || !CHECK_BACKWARD_REFS(left1));
       } else if (above_has_second || left_has_second) {
         const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
         const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
 
-        if (!CHECK_BWDREF_OR_ALTREF(rfs))
-          pred_context = 3 + (!CHECK_BWDREF_OR_ALTREF(crf1) ||
-                              !CHECK_BWDREF_OR_ALTREF(crf2));
+        if (!CHECK_BACKWARD_REFS(rfs))
+          pred_context =
+              3 + (!CHECK_BACKWARD_REFS(crf1) || !CHECK_BACKWARD_REFS(crf2));
         else
           pred_context =
-              !CHECK_BWDREF_OR_ALTREF(crf1) || !CHECK_BWDREF_OR_ALTREF(crf2);
+              !CHECK_BACKWARD_REFS(crf1) || !CHECK_BACKWARD_REFS(crf2);
       } else {
-        pred_context = 2 * (!CHECK_BWDREF_OR_ALTREF(above0)) +
-                       2 * (!CHECK_BWDREF_OR_ALTREF(left0));
+        pred_context = 2 * (!CHECK_BACKWARD_REFS(above0)) +
+                       2 * (!CHECK_BACKWARD_REFS(left0));
       }
     }
   } else if (has_above || has_left) {  // one edge available
@@ -838,10 +796,10 @@
       pred_context = 2;
     } else {  // inter
       if (!has_second_ref(edge_mbmi))
-        pred_context = 4 * (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]));
+        pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
       else
-        pred_context = 1 + (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]) ||
-                            !CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[1]));
+        pred_context = 1 + (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) ||
+                            !CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[1]));
     }
   } else {  // no edges available
     pred_context = 2;
@@ -876,7 +834,7 @@
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
       if (!has_second_ref(edge_mbmi)) {
-        if (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]))
+        if (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME);
@@ -912,12 +870,12 @@
         else
           pred_context = 1 + 2 * (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
       } else {
-        if (!CHECK_BWDREF_OR_ALTREF(above0) && !CHECK_BWDREF_OR_ALTREF(left0)) {
+        if (!CHECK_BACKWARD_REFS(above0) && !CHECK_BACKWARD_REFS(left0)) {
           pred_context = 2 + (above0 == left0);
-        } else if (!CHECK_BWDREF_OR_ALTREF(above0) ||
-                   !CHECK_BWDREF_OR_ALTREF(left0)) {
+        } else if (!CHECK_BACKWARD_REFS(above0) ||
+                   !CHECK_BACKWARD_REFS(left0)) {
           const MV_REFERENCE_FRAME edge0 =
-              !CHECK_BWDREF_OR_ALTREF(above0) ? left0 : above0;
+              !CHECK_BACKWARD_REFS(above0) ? left0 : above0;
           pred_context = 4 * (edge0 == BWDREF_FRAME);
         } else {
           pred_context =
@@ -929,7 +887,7 @@
     const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
 
     if (!is_inter_block(edge_mbmi) ||
-        (!CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]) &&
+        (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
     else if (!has_second_ref(edge_mbmi))
@@ -970,7 +928,7 @@
     } else if (above_intra || left_intra) {  // intra/inter or inter/intra
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
       if (!has_second_ref(edge_mbmi)) {
-        if (CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]))
+        if (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
           pred_context = 3;
         else
           pred_context = 4 * CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
@@ -1009,12 +967,11 @@
           pred_context =
               1 + 2 * (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
       } else {
-        if (CHECK_BWDREF_OR_ALTREF(above0) && CHECK_BWDREF_OR_ALTREF(left0)) {
+        if (CHECK_BACKWARD_REFS(above0) && CHECK_BACKWARD_REFS(left0)) {
           pred_context = 2 + (above0 == left0);
-        } else if (CHECK_BWDREF_OR_ALTREF(above0) ||
-                   CHECK_BWDREF_OR_ALTREF(left0)) {
+        } else if (CHECK_BACKWARD_REFS(above0) || CHECK_BACKWARD_REFS(left0)) {
           const MV_REFERENCE_FRAME edge0 =
-              CHECK_BWDREF_OR_ALTREF(above0) ? left0 : above0;
+              CHECK_BACKWARD_REFS(above0) ? left0 : above0;
           pred_context = 4 * CHECK_LAST_OR_LAST2(edge0);
         } else {
           pred_context =
@@ -1026,7 +983,7 @@
     const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
 
     if (!is_inter_block(edge_mbmi) ||
-        (CHECK_BWDREF_OR_ALTREF(edge_mbmi->ref_frame[0]) &&
+        (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
          !has_second_ref(edge_mbmi)))
       pred_context = 2;
     else if (!has_second_ref(edge_mbmi))
diff --git a/av1/common/pred_common.h b/av1/common/pred_common.h
index 6b0a3d5..b3ef1c4 100644
--- a/av1/common/pred_common.h
+++ b/av1/common/pred_common.h
@@ -127,7 +127,6 @@
   const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd);
   return cm->fc->comp_bwdref_prob[pred_context][0];
 }
-
 #endif  // CONFIG_EXT_REFS
 
 int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd);
diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index 483b0b2..96ffb08 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -22,9 +22,6 @@
 #include "aom_mem/aom_mem.h"
 #include "aom_ports/mem.h"
 #include "aom_ports/aom_once.h"
-#if CONFIG_EXT_INTRA
-#include "av1/common/intra_filters.h"
-#endif
 #include "av1/common/reconintra.h"
 #include "av1/common/onyxc_int.h"
 
@@ -382,20 +379,6 @@
 }
 
 #if CONFIG_EXT_INTRA
-
-static const uint8_t ext_intra_extend_modes[FILTER_INTRA_MODES] = {
-  NEED_LEFT | NEED_ABOVE,  // FILTER_DC
-  NEED_LEFT | NEED_ABOVE,  // FILTER_V
-  NEED_LEFT | NEED_ABOVE,  // FILTER_H
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D45
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D135
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D117
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D153
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D207
-  NEED_LEFT | NEED_ABOVE,  // FILTER_D63
-  NEED_LEFT | NEED_ABOVE,  // FILTER_TM
-};
-
 static int intra_subpel_interp(int base, int shift, const uint8_t *ref,
                                int ref_start_idx, int ref_end_idx,
                                INTRA_FILTER filter_type) {
@@ -706,129 +689,6 @@
   }
 }
 
-static void filter_intra_predictors_4tap(uint8_t *dst, ptrdiff_t stride, int bs,
-                                         const uint8_t *above,
-                                         const uint8_t *left, int mode) {
-  int k, r, c;
-  int preds[33][65];
-  int mean, ipred;
-  const TX_SIZE tx_size =
-      (bs == 32) ? TX_32X32
-                 : ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
-  const int c0 = filter_intra_taps_4[tx_size][mode][0];
-  const int c1 = filter_intra_taps_4[tx_size][mode][1];
-  const int c2 = filter_intra_taps_4[tx_size][mode][2];
-  const int c3 = filter_intra_taps_4[tx_size][mode][3];
-
-  k = 0;
-  mean = 0;
-  while (k < bs) {
-    mean = mean + (int)left[k];
-    mean = mean + (int)above[k];
-    k++;
-  }
-  mean = (mean + bs) / (2 * bs);
-
-  for (r = 0; r < bs; ++r) preds[r + 1][0] = (int)left[r] - mean;
-
-  for (c = 0; c < 2 * bs + 1; ++c) preds[0][c] = (int)above[c - 1] - mean;
-
-  for (r = 1; r < bs + 1; ++r)
-    for (c = 1; c < 2 * bs + 1 - r; ++c) {
-      ipred = c0 * preds[r - 1][c] + c1 * preds[r][c - 1] +
-              c2 * preds[r - 1][c - 1] + c3 * preds[r - 1][c + 1];
-      preds[r][c] = ROUND_POWER_OF_TWO_SIGNED(ipred, FILTER_INTRA_PREC_BITS);
-    }
-
-  for (r = 0; r < bs; ++r) {
-    for (c = 0; c < bs; ++c) {
-      ipred = preds[r + 1][c + 1] + mean;
-      dst[c] = clip_pixel(ipred);
-    }
-    dst += stride;
-  }
-}
-
-void av1_dc_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                               const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, DC_PRED);
-}
-
-void av1_v_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                              const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, V_PRED);
-}
-
-void av1_h_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                              const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, H_PRED);
-}
-
-void av1_d45_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D45_PRED);
-}
-
-void av1_d135_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                 const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D135_PRED);
-}
-
-void av1_d117_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                 const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D117_PRED);
-}
-
-void av1_d153_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                 const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D153_PRED);
-}
-
-void av1_d207_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                 const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D207_PRED);
-}
-
-void av1_d63_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                                const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, D63_PRED);
-}
-
-void av1_tm_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
-                               const uint8_t *above, const uint8_t *left) {
-  filter_intra_predictors_4tap(dst, stride, bs, above, left, TM_PRED);
-}
-
-static void filter_intra_predictors(int mode, uint8_t *dst, ptrdiff_t stride,
-                                    int bs, const uint8_t *above,
-                                    const uint8_t *left) {
-  switch (mode) {
-    case DC_PRED: av1_dc_filter_predictor(dst, stride, bs, above, left); break;
-    case V_PRED: av1_v_filter_predictor(dst, stride, bs, above, left); break;
-    case H_PRED: av1_h_filter_predictor(dst, stride, bs, above, left); break;
-    case D45_PRED:
-      av1_d45_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case D135_PRED:
-      av1_d135_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case D117_PRED:
-      av1_d117_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case D153_PRED:
-      av1_d153_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case D207_PRED:
-      av1_d207_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case D63_PRED:
-      av1_d63_filter_predictor(dst, stride, bs, above, left);
-      break;
-    case TM_PRED: av1_tm_filter_predictor(dst, stride, bs, above, left); break;
-    default: assert(0);
-  }
-}
-
 #if CONFIG_AOM_HIGHBITDEPTH
 static int highbd_intra_subpel_interp(int base, int shift, const uint16_t *ref,
                                       int ref_start_idx, int ref_end_idx,
@@ -999,7 +859,184 @@
     highbd_h_predictor(dst, stride, bs, above, left, bd);
   }
 }
+#endif  // CONFIG_AOM_HIGHBITDEPTH
+#endif  // CONFIG_EXT_INTRA
 
+#if CONFIG_FILTER_INTRA
+int av1_filter_intra_taps_4[TX_SIZES][INTRA_MODES][4] = {
+  {
+      { 735, 881, -537, -54 },
+      { 1005, 519, -488, -11 },
+      { 383, 990, -343, -6 },
+      { 442, 805, -542, 319 },
+      { 658, 616, -133, -116 },
+      { 875, 442, -141, -151 },
+      { 386, 741, -23, -80 },
+      { 390, 1027, -446, 51 },
+      { 679, 606, -523, 262 },
+      { 903, 922, -778, -23 },
+  },
+  {
+      { 648, 803, -444, 16 },
+      { 972, 620, -576, 7 },
+      { 561, 967, -499, -5 },
+      { 585, 762, -468, 144 },
+      { 596, 619, -182, -9 },
+      { 895, 459, -176, -153 },
+      { 557, 722, -126, -129 },
+      { 601, 839, -523, 105 },
+      { 562, 709, -499, 251 },
+      { 803, 872, -695, 43 },
+  },
+  {
+      { 423, 728, -347, 111 },
+      { 963, 685, -665, 23 },
+      { 281, 1024, -480, 216 },
+      { 640, 596, -437, 78 },
+      { 429, 669, -259, 99 },
+      { 740, 646, -415, 23 },
+      { 568, 771, -346, 40 },
+      { 404, 833, -486, 209 },
+      { 398, 712, -423, 307 },
+      { 939, 935, -887, 17 },
+  },
+  {
+      { 477, 737, -393, 150 },
+      { 881, 630, -546, 67 },
+      { 506, 984, -443, -20 },
+      { 114, 459, -270, 528 },
+      { 433, 528, 14, 3 },
+      { 837, 470, -301, -30 },
+      { 181, 777, 89, -107 },
+      { -29, 716, -232, 259 },
+      { 589, 646, -495, 255 },
+      { 740, 884, -728, 77 },
+  },
+};
+
+static void filter_intra_predictors_4tap(uint8_t *dst, ptrdiff_t stride, int bs,
+                                         const uint8_t *above,
+                                         const uint8_t *left, int mode) {
+  int k, r, c;
+  int buffer[33][65];
+  int mean, ipred;
+  const TX_SIZE tx_size =
+      (bs == 32) ? TX_32X32
+                 : ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
+  const int c0 = av1_filter_intra_taps_4[tx_size][mode][0];
+  const int c1 = av1_filter_intra_taps_4[tx_size][mode][1];
+  const int c2 = av1_filter_intra_taps_4[tx_size][mode][2];
+  const int c3 = av1_filter_intra_taps_4[tx_size][mode][3];
+
+  k = 0;
+  mean = 0;
+  while (k < bs) {
+    mean = mean + (int)left[k];
+    mean = mean + (int)above[k];
+    k++;
+  }
+  mean = (mean + bs) / (2 * bs);
+
+  for (r = 0; r < bs; ++r) buffer[r + 1][0] = (int)left[r] - mean;
+
+  for (c = 0; c < 2 * bs + 1; ++c) buffer[0][c] = (int)above[c - 1] - mean;
+
+  for (r = 1; r < bs + 1; ++r)
+    for (c = 1; c < 2 * bs + 1 - r; ++c) {
+      ipred = c0 * buffer[r - 1][c] + c1 * buffer[r][c - 1] +
+              c2 * buffer[r - 1][c - 1] + c3 * buffer[r - 1][c + 1];
+      buffer[r][c] = ROUND_POWER_OF_TWO_SIGNED(ipred, FILTER_INTRA_PREC_BITS);
+    }
+
+  for (r = 0; r < bs; ++r) {
+    for (c = 0; c < bs; ++c) {
+      ipred = buffer[r + 1][c + 1] + mean;
+      dst[c] = clip_pixel(ipred);
+    }
+    dst += stride;
+  }
+}
+
+void av1_dc_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                               const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, DC_PRED);
+}
+
+void av1_v_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                              const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, V_PRED);
+}
+
+void av1_h_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                              const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, H_PRED);
+}
+
+void av1_d45_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D45_PRED);
+}
+
+void av1_d135_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                 const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D135_PRED);
+}
+
+void av1_d117_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                 const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D117_PRED);
+}
+
+void av1_d153_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                 const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D153_PRED);
+}
+
+void av1_d207_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                 const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D207_PRED);
+}
+
+void av1_d63_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                                const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, D63_PRED);
+}
+
+void av1_tm_filter_predictor_c(uint8_t *dst, ptrdiff_t stride, int bs,
+                               const uint8_t *above, const uint8_t *left) {
+  filter_intra_predictors_4tap(dst, stride, bs, above, left, TM_PRED);
+}
+
+static void filter_intra_predictors(int mode, uint8_t *dst, ptrdiff_t stride,
+                                    int bs, const uint8_t *above,
+                                    const uint8_t *left) {
+  switch (mode) {
+    case DC_PRED: av1_dc_filter_predictor(dst, stride, bs, above, left); break;
+    case V_PRED: av1_v_filter_predictor(dst, stride, bs, above, left); break;
+    case H_PRED: av1_h_filter_predictor(dst, stride, bs, above, left); break;
+    case D45_PRED:
+      av1_d45_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case D135_PRED:
+      av1_d135_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case D117_PRED:
+      av1_d117_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case D153_PRED:
+      av1_d153_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case D207_PRED:
+      av1_d207_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case D63_PRED:
+      av1_d63_filter_predictor(dst, stride, bs, above, left);
+      break;
+    case TM_PRED: av1_tm_filter_predictor(dst, stride, bs, above, left); break;
+    default: assert(0);
+  }
+}
+#if CONFIG_AOM_HIGHBITDEPTH
 static void highbd_filter_intra_predictors_4tap(uint16_t *dst, ptrdiff_t stride,
                                                 int bs, const uint16_t *above,
                                                 const uint16_t *left, int mode,
@@ -1010,10 +1047,10 @@
   const TX_SIZE tx_size =
       (bs == 32) ? TX_32X32
                  : ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
-  const int c0 = filter_intra_taps_4[tx_size][mode][0];
-  const int c1 = filter_intra_taps_4[tx_size][mode][1];
-  const int c2 = filter_intra_taps_4[tx_size][mode][2];
-  const int c3 = filter_intra_taps_4[tx_size][mode][3];
+  const int c0 = av1_filter_intra_taps_4[tx_size][mode][0];
+  const int c1 = av1_filter_intra_taps_4[tx_size][mode][1];
+  const int c2 = av1_filter_intra_taps_4[tx_size][mode][2];
+  const int c3 = av1_filter_intra_taps_4[tx_size][mode][3];
 
   k = 0;
   mean = 0;
@@ -1151,7 +1188,7 @@
   }
 }
 #endif  // CONFIG_AOM_HIGHBITDEPTH
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
 #if CONFIG_AOM_HIGHBITDEPTH
 static void build_intra_predictors_high(
@@ -1168,7 +1205,19 @@
   const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
   int need_left = extend_modes[mode] & NEED_LEFT;
   int need_above = extend_modes[mode] & NEED_ABOVE;
+  int need_above_left = extend_modes[mode] & NEED_ABOVELEFT;
   const uint16_t *above_ref = ref - ref_stride;
+#if CONFIG_EXT_INTRA
+  int p_angle = 0;
+  const int is_dr_mode = mode != DC_PRED && mode != TM_PRED &&
+                         xd->mi[0]->mbmi.sb_type >= BLOCK_8X8;
+#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  const FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
+      &xd->mi[0]->mbmi.filter_intra_mode_info;
+  const FILTER_INTRA_MODE filter_intra_mode =
+      filter_intra_mode_info->filter_intra_mode[plane != 0];
+#endif  // CONFIG_FILTER_INTRA
   int base = 128 << (xd->bd - 8);
 // 127 127 127 .. 127 127 127 127 127 127
 // 129  A   B  ..  Y   Z
@@ -1177,29 +1226,21 @@
 // 129  G   H  ..  S   T   T   T   T   T
 
 #if CONFIG_EXT_INTRA
-  const EXT_INTRA_MODE_INFO *ext_intra_mode_info =
-      &xd->mi[0]->mbmi.ext_intra_mode_info;
-  const EXT_INTRA_MODE ext_intra_mode =
-      ext_intra_mode_info->ext_intra_mode[plane != 0];
-  int p_angle = 0;
-
-  if (mode != DC_PRED && mode != TM_PRED &&
-      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
+  if (is_dr_mode) {
     p_angle = mode_to_angle_map[mode] +
               xd->mi[0]->mbmi.angle_delta[plane != 0] * ANGLE_STEP;
     if (p_angle <= 90)
-      need_above = 1, need_left = 0;
+      need_above = 1, need_left = 0, need_above_left = 1;
     else if (p_angle < 180)
-      need_above = 1, need_left = 1;
+      need_above = 1, need_left = 1, need_above_left = 1;
     else
-      need_above = 0, need_left = 1;
-  }
-
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
-    need_left = ext_intra_extend_modes[ext_intra_mode] & NEED_LEFT;
-    need_above = ext_intra_extend_modes[ext_intra_mode] & NEED_ABOVE;
+      need_above = 0, need_left = 1, need_above_left = 1;
   }
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
+    need_left = need_above = need_above_left = 1;
+#endif  // CONFIG_FILTER_INTRA
 
   (void)plane;
   assert(n_top_px >= 0);
@@ -1218,19 +1259,18 @@
 
   // NEED_LEFT
   if (need_left) {
-#if CONFIG_EXT_INTRA
-    int need_bottom;
-    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
+    int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
+#if CONFIG_FILTER_INTRA
+    if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
       need_bottom = 0;
-    } else if (mode != DC_PRED && mode != TM_PRED &&
-               xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
-      need_bottom = p_angle > 180;
-    } else {
-      need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
-    }
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
+    if (is_dr_mode) need_bottom = p_angle > 180;
+#endif  // CONFIG_EXT_INTRA
 #else
     const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
     i = 0;
     if (n_left_px > 0) {
       for (; i < n_left_px; i++) left_col[i] = ref[i * ref_stride - 1];
@@ -1248,19 +1288,18 @@
 
   // NEED_ABOVE
   if (need_above) {
-#if CONFIG_EXT_INTRA
-    int need_right;
-    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
+    int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
+#if CONFIG_FILTER_INTRA
+    if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
       need_right = 1;
-    } else if (mode != DC_PRED && mode != TM_PRED &&
-               xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
-      need_right = p_angle < 90;
-    } else {
-      need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
-    }
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
+    if (is_dr_mode) need_right = p_angle < 90;
+#endif  // CONFIG_EXT_INTRA
 #else
     const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
     if (n_top_px > 0) {
       memcpy(above_row, above_ref, n_top_px * sizeof(above_ref[0]));
       i = n_top_px;
@@ -1277,30 +1316,21 @@
     }
   }
 
-#if CONFIG_EXT_INTRA
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0] ||
-      (extend_modes[mode] & NEED_ABOVELEFT) ||
-      (mode != DC_PRED && mode != TM_PRED &&
-       xd->mi[0]->mbmi.sb_type >= BLOCK_8X8)) {
+  if (need_above_left) {
     above_row[-1] =
         n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : base + 1) : base - 1;
   }
-#else
-  if ((extend_modes[mode] & NEED_ABOVELEFT)) {
-    above_row[-1] =
-        n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : base + 1) : base - 1;
-  }
-#endif  // CONFIG_EXT_INTRA
 
-#if CONFIG_EXT_INTRA
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
-    highbd_filter_intra_predictors(ext_intra_mode, dst, dst_stride, bs,
+#if CONFIG_FILTER_INTRA
+  if (filter_intra_mode_info->use_filter_intra_mode[plane != 0]) {
+    highbd_filter_intra_predictors(filter_intra_mode, dst, dst_stride, bs,
                                    const_above_row, left_col, xd->bd);
     return;
   }
+#endif  // CONFIG_FILTER_INTRA
 
-  if (mode != DC_PRED && mode != TM_PRED &&
-      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
+#if CONFIG_EXT_INTRA
+  if (is_dr_mode) {
     INTRA_FILTER filter = INTRA_FILTER_LINEAR;
     if (plane == 0 && av1_is_intra_filter_switchable(p_angle))
       filter = xd->mi[0]->mbmi.intra_filter;
@@ -1336,37 +1366,42 @@
   const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
   int need_left = extend_modes[mode] & NEED_LEFT;
   int need_above = extend_modes[mode] & NEED_ABOVE;
+  int need_above_left = extend_modes[mode] & NEED_ABOVELEFT;
 #if CONFIG_EXT_INTRA
-  const EXT_INTRA_MODE_INFO *ext_intra_mode_info =
-      &xd->mi[0]->mbmi.ext_intra_mode_info;
-  const EXT_INTRA_MODE ext_intra_mode =
-      ext_intra_mode_info->ext_intra_mode[plane != 0];
   int p_angle = 0;
+  const int is_dr_mode = mode != DC_PRED && mode != TM_PRED &&
+                         xd->mi[0]->mbmi.sb_type >= BLOCK_8X8;
+#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  const FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
+      &xd->mi[0]->mbmi.filter_intra_mode_info;
+  const FILTER_INTRA_MODE filter_intra_mode =
+      filter_intra_mode_info->filter_intra_mode[plane != 0];
+#endif  // CONFIG_FILTER_INTRA
 
-  if (mode != DC_PRED && mode != TM_PRED &&
-      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
+// 127 127 127 .. 127 127 127 127 127 127
+// 129  A   B  ..  Y   Z
+// 129  C   D  ..  W   X
+// 129  E   F  ..  U   V
+// 129  G   H  ..  S   T   T   T   T   T
+// ..
+
+#if CONFIG_EXT_INTRA
+  if (is_dr_mode) {
     p_angle = mode_to_angle_map[mode] +
               xd->mi[0]->mbmi.angle_delta[plane != 0] * ANGLE_STEP;
     if (p_angle <= 90)
-      need_above = 1, need_left = 0;
+      need_above = 1, need_left = 0, need_above_left = 1;
     else if (p_angle < 180)
-      need_above = 1, need_left = 1;
+      need_above = 1, need_left = 1, need_above_left = 1;
     else
-      need_above = 0, need_left = 1;
-  }
-
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
-    need_left = ext_intra_extend_modes[ext_intra_mode] & NEED_LEFT;
-    need_above = ext_intra_extend_modes[ext_intra_mode] & NEED_ABOVE;
+      need_above = 0, need_left = 1, need_above_left = 1;
   }
 #endif  // CONFIG_EXT_INTRA
-
-  // 127 127 127 .. 127 127 127 127 127 127
-  // 129  A   B  ..  Y   Z
-  // 129  C   D  ..  W   X
-  // 129  E   F  ..  U   V
-  // 129  G   H  ..  S   T   T   T   T   T
-  // ..
+#if CONFIG_FILTER_INTRA
+  if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
+    need_left = need_above = need_above_left = 1;
+#endif  // CONFIG_FILTER_INTRA
 
   (void)xd;
   (void)plane;
@@ -1386,19 +1421,18 @@
 
   // NEED_LEFT
   if (need_left) {
-#if CONFIG_EXT_INTRA
-    int need_bottom;
-    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
+    int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
+#if CONFIG_FILTER_INTRA
+    if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
       need_bottom = 0;
-    } else if (mode != DC_PRED && mode != TM_PRED &&
-               xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
-      need_bottom = p_angle > 180;
-    } else {
-      need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
-    }
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
+    if (is_dr_mode) need_bottom = p_angle > 180;
+#endif  // CONFIG_EXT_INTRA
 #else
     const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
     i = 0;
     if (n_left_px > 0) {
       for (; i < n_left_px; i++) left_col[i] = ref[i * ref_stride - 1];
@@ -1416,19 +1450,18 @@
 
   // NEED_ABOVE
   if (need_above) {
-#if CONFIG_EXT_INTRA
-    int need_right;
-    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA
+    int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
+#if CONFIG_FILTER_INTRA
+    if (filter_intra_mode_info->use_filter_intra_mode[plane != 0])
       need_right = 1;
-    } else if (mode != DC_PRED && mode != TM_PRED &&
-               xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
-      need_right = p_angle < 90;
-    } else {
-      need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
-    }
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
+    if (is_dr_mode) need_right = p_angle < 90;
+#endif  // CONFIG_EXT_INTRA
 #else
     const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_EXT_INTRA || CONFIG_FITLER_INTRA
     if (n_top_px > 0) {
       memcpy(above_row, above_ref, n_top_px);
       i = n_top_px;
@@ -1444,26 +1477,18 @@
     }
   }
 
-#if CONFIG_EXT_INTRA
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0] ||
-      (extend_modes[mode] & NEED_ABOVELEFT) ||
-      (mode != DC_PRED && mode != TM_PRED &&
-       xd->mi[0]->mbmi.sb_type >= BLOCK_8X8)) {
+  if (need_above_left) {
     above_row[-1] = n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : 129) : 127;
   }
-#else
-  if ((extend_modes[mode] & NEED_ABOVELEFT)) {
-    above_row[-1] = n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : 129) : 127;
-  }
-#endif  // CONFIG_EXT_INTRA
 
-#if CONFIG_EXT_INTRA
-  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
-    filter_intra_predictors(ext_intra_mode, dst, dst_stride, bs,
+#if CONFIG_FILTER_INTRA
+  if (filter_intra_mode_info->use_filter_intra_mode[plane != 0]) {
+    filter_intra_predictors(filter_intra_mode, dst, dst_stride, bs,
                             const_above_row, left_col);
     return;
   }
-
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
   if (mode != DC_PRED && mode != TM_PRED &&
       xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
     INTRA_FILTER filter = INTRA_FILTER_LINEAR;
diff --git a/av1/common/reconintra.h b/av1/common/reconintra.h
index 5983a6c..7778874 100644
--- a/av1/common/reconintra.h
+++ b/av1/common/reconintra.h
@@ -25,9 +25,6 @@
                              TX_SIZE tx_size, PREDICTION_MODE mode,
                              const uint8_t *ref, int ref_stride, uint8_t *dst,
                              int dst_stride, int aoff, int loff, int plane);
-#if CONFIG_EXT_INTRA
-int av1_is_intra_filter_switchable(int angle);
-#endif  // CONFIG_EXT_INTRA
 
 #if CONFIG_EXT_INTER
 // Mapping of interintra to intra mode for use in the intra component
@@ -46,4 +43,9 @@
 }  // extern "C"
 #endif
 
+#if CONFIG_FILTER_INTRA
+#define FILTER_INTRA_PREC_BITS 10
+extern int av1_filter_intra_taps_4[TX_SIZES][INTRA_MODES][4];
+#endif  // CONFIG_FILTER_INTRA
+
 #endif  // AV1_COMMON_RECONINTRA_H_
diff --git a/av1/common/x86/reconintra_sse4.c b/av1/common/x86/filterintra_sse4.c
similarity index 96%
rename from av1/common/x86/reconintra_sse4.c
rename to av1/common/x86/filterintra_sse4.c
index ab1fa93..86b1c80 100644
--- a/av1/common/x86/reconintra_sse4.c
+++ b/av1/common/x86/filterintra_sse4.c
@@ -12,7 +12,7 @@
 #include "./av1_rtcd.h"
 #include "aom_ports/mem.h"
 #include "av1/common/enums.h"
-#include "av1/common/intra_filters.h"
+#include "av1/common/reconintra.h"
 
 static INLINE void AddPixelsSmall(const uint8_t *above, const uint8_t *left,
                                   __m128i *sum) {
@@ -153,25 +153,25 @@
       (bs == 32) ? TX_32X32
                  : ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
   // c0
-  params[0] = _mm_set_epi32(filter_intra_taps_4[tx_size][mode][0],
-                            filter_intra_taps_4[tx_size][mode][0],
-                            filter_intra_taps_4[tx_size][mode][0],
-                            filter_intra_taps_4[tx_size][mode][0]);
+  params[0] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][0],
+                            av1_filter_intra_taps_4[tx_size][mode][0],
+                            av1_filter_intra_taps_4[tx_size][mode][0],
+                            av1_filter_intra_taps_4[tx_size][mode][0]);
   // c1
-  params[1] = _mm_set_epi32(filter_intra_taps_4[tx_size][mode][1],
-                            filter_intra_taps_4[tx_size][mode][1],
-                            filter_intra_taps_4[tx_size][mode][1],
-                            filter_intra_taps_4[tx_size][mode][1]);
+  params[1] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][1],
+                            av1_filter_intra_taps_4[tx_size][mode][1],
+                            av1_filter_intra_taps_4[tx_size][mode][1],
+                            av1_filter_intra_taps_4[tx_size][mode][1]);
   // c2
-  params[2] = _mm_set_epi32(filter_intra_taps_4[tx_size][mode][2],
-                            filter_intra_taps_4[tx_size][mode][2],
-                            filter_intra_taps_4[tx_size][mode][2],
-                            filter_intra_taps_4[tx_size][mode][2]);
+  params[2] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][2],
+                            av1_filter_intra_taps_4[tx_size][mode][2],
+                            av1_filter_intra_taps_4[tx_size][mode][2],
+                            av1_filter_intra_taps_4[tx_size][mode][2]);
   // c3
-  params[3] = _mm_set_epi32(filter_intra_taps_4[tx_size][mode][3],
-                            filter_intra_taps_4[tx_size][mode][3],
-                            filter_intra_taps_4[tx_size][mode][3],
-                            filter_intra_taps_4[tx_size][mode][3]);
+  params[3] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][3],
+                            av1_filter_intra_taps_4[tx_size][mode][3],
+                            av1_filter_intra_taps_4[tx_size][mode][3],
+                            av1_filter_intra_taps_4[tx_size][mode][3]);
 }
 
 static const int maxBlkSize = 32;
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 0fbf659..7d69d78 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1441,8 +1441,8 @@
                                         ACCT_STR);
 #else
 #if CONFIG_DAALA_EC
-    p = (PARTITION_TYPE)aom_read_tree_cdf(r, cm->fc->partition_cdf[ctx],
-                                          PARTITION_TYPES, ACCT_STR);
+    p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
+                                        PARTITION_TYPES, ACCT_STR);
 #else
     p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
 #endif
@@ -3254,10 +3254,6 @@
   RefCntBuffer *const frame_bufs = pool->frame_bufs;
   int i, mask, ref_index = 0;
   size_t sz;
-#if CONFIG_EXT_REFS
-  cm->last3_frame_type = cm->last2_frame_type;
-  cm->last2_frame_type = cm->last_frame_type;
-#endif  // CONFIG_EXT_REFS
   cm->last_frame_type = cm->frame_type;
   cm->last_intra_only = cm->intra_only;
 
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index ed2fc4c..4d181fd 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -27,7 +27,7 @@
 
 #define ACCT_STR __func__
 
-#if CONFIG_EXT_INTRA || CONFIG_PALETTE
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 static INLINE int read_uniform(aom_reader *r, int n) {
   int l = get_unsigned_bits(n);
   int m = (1 << l) - n;
@@ -40,7 +40,7 @@
   else
     return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
 }
-#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 
 static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
   return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p, ACCT_STR);
@@ -545,46 +545,51 @@
 }
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-static void read_ext_intra_mode_info(AV1_COMMON *const cm,
-                                     MACROBLOCKD *const xd, aom_reader *r) {
+#if CONFIG_FILTER_INTRA
+static void read_filter_intra_mode_info(AV1_COMMON *const cm,
+                                        MACROBLOCKD *const xd, aom_reader *r) {
   MODE_INFO *const mi = xd->mi[0];
   MB_MODE_INFO *const mbmi = &mi->mbmi;
   FRAME_COUNTS *counts = xd->counts;
+  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
+      &mbmi->filter_intra_mode_info;
 
-#if !ALLOW_FILTER_INTRA_MODES
-  return;
-#endif
   if (mbmi->mode == DC_PRED
 #if CONFIG_PALETTE
       && mbmi->palette_mode_info.palette_size[0] == 0
 #endif  // CONFIG_PALETTE
       ) {
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[0] =
-        aom_read(r, cm->fc->ext_intra_probs[0], ACCT_STR);
-    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
-      mbmi->ext_intra_mode_info.ext_intra_mode[0] =
+    filter_intra_mode_info->use_filter_intra_mode[0] =
+        aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
+    if (filter_intra_mode_info->use_filter_intra_mode[0]) {
+      filter_intra_mode_info->filter_intra_mode[0] =
           read_uniform(r, FILTER_INTRA_MODES);
     }
-    if (counts)
-      ++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
+    if (counts) {
+      ++counts->filter_intra[0]
+                            [filter_intra_mode_info->use_filter_intra_mode[0]];
+    }
   }
   if (mbmi->uv_mode == DC_PRED
 #if CONFIG_PALETTE
       && mbmi->palette_mode_info.palette_size[1] == 0
 #endif  // CONFIG_PALETTE
       ) {
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-        aom_read(r, cm->fc->ext_intra_probs[1], ACCT_STR);
-    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
-      mbmi->ext_intra_mode_info.ext_intra_mode[1] =
+    filter_intra_mode_info->use_filter_intra_mode[1] =
+        aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
+    if (filter_intra_mode_info->use_filter_intra_mode[1]) {
+      filter_intra_mode_info->filter_intra_mode[1] =
           read_uniform(r, FILTER_INTRA_MODES);
     }
-    if (counts)
-      ++counts->ext_intra[1][mbmi->ext_intra_mode_info.use_ext_intra_mode[1]];
+    if (counts) {
+      ++counts->filter_intra[1]
+                            [filter_intra_mode_info->use_filter_intra_mode[1]];
+    }
   }
 }
+#endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_EXT_INTRA
 static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
@@ -682,11 +687,11 @@
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     read_palette_mode_info(cm, xd, r);
 #endif  // CONFIG_PALETTE
-#if CONFIG_EXT_INTRA
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-  if (bsize >= BLOCK_8X8) read_ext_intra_mode_info(cm, xd, r);
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+  if (bsize >= BLOCK_8X8) read_filter_intra_mode_info(cm, xd, r);
+#endif  // CONFIG_FILTER_INTRA
 
   if (!FIXED_TX_TYPE) {
 #if CONFIG_EXT_TX
@@ -986,11 +991,11 @@
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     read_palette_mode_info(cm, xd, r);
 #endif  // CONFIG_PALETTE
-#if CONFIG_EXT_INTRA
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-  if (bsize >= BLOCK_8X8) read_ext_intra_mode_info(cm, xd, r);
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+  if (bsize >= BLOCK_8X8) read_filter_intra_mode_info(cm, xd, r);
+#endif  // CONFIG_FILTER_INTRA
 }
 
 static INLINE int is_mv_valid(const MV *mv) {
@@ -1604,12 +1609,14 @@
       mbmi->ref_frame[1] = INTRA_FRAME;
       mbmi->interintra_mode = interintra_mode;
 #if CONFIG_EXT_INTRA
-      mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-      mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
       mbmi->angle_delta[0] = 0;
       mbmi->angle_delta[1] = 0;
       mbmi->intra_filter = INTRA_FILTER_LINEAR;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+      mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+      mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
       if (is_interintra_wedge_used(bsize)) {
         mbmi->use_wedge_interintra =
             aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 6326001..73a0bf1 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -50,33 +50,17 @@
 #include "av1/encoder/subexp.h"
 #include "av1/encoder/tokenize.h"
 
-static const struct av1_token intra_mode_encodings[INTRA_MODES] = {
-  { 0, 1 },  { 6, 3 },   { 28, 5 },  { 30, 5 }, { 58, 6 },
-  { 59, 6 }, { 126, 7 }, { 127, 7 }, { 62, 6 }, { 2, 2 }
-};
-#if CONFIG_EXT_INTERP
-static const struct av1_token switchable_interp_encodings[SWITCHABLE_FILTERS] =
-    { { 0, 1 }, { 4, 3 }, { 6, 3 }, { 5, 3 }, { 7, 3 } };
-#else
-static const struct av1_token switchable_interp_encodings[SWITCHABLE_FILTERS] =
-    { { 0, 1 }, { 2, 2 }, { 3, 2 } };
-#endif  // CONFIG_EXT_INTERP
+static struct av1_token intra_mode_encodings[INTRA_MODES];
+static struct av1_token switchable_interp_encodings[SWITCHABLE_FILTERS];
 #if CONFIG_EXT_PARTITION_TYPES
 static const struct av1_token ext_partition_encodings[EXT_PARTITION_TYPES] = {
   { 0, 1 },  { 4, 3 },  { 12, 4 }, { 7, 3 },
   { 10, 4 }, { 11, 4 }, { 26, 5 }, { 27, 5 }
 };
 #endif
-static const struct av1_token partition_encodings[PARTITION_TYPES] = {
-  { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 }
-};
+static struct av1_token partition_encodings[PARTITION_TYPES];
 #if !CONFIG_REF_MV
-static const struct av1_token inter_mode_encodings[INTER_MODES] =
-#if CONFIG_EXT_INTER
-    { { 2, 2 }, { 6, 3 }, { 0, 1 }, { 14, 4 }, { 15, 4 } };
-#else
-    { { 2, 2 }, { 6, 3 }, { 0, 1 }, { 7, 3 } };
-#endif  // CONFIG_EXT_INTER
+static struct av1_token inter_mode_encodings[INTER_MODES];
 #endif
 #if CONFIG_EXT_INTER
 static const struct av1_token
@@ -118,14 +102,13 @@
         { 127, 7 } },  // 8 colors
     };
 #endif  // CONFIG_PALETTE
-
 static const struct av1_token tx_size_encodings[TX_SIZES - 1][TX_SIZES] = {
   { { 0, 1 }, { 1, 1 } },                      // Max tx_size is 8X8
   { { 0, 1 }, { 2, 2 }, { 3, 2 } },            // Max tx_size is 16X16
   { { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 } },  // Max tx_size is 32X32
 };
 
-#if CONFIG_EXT_INTRA || CONFIG_PALETTE
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 static INLINE void write_uniform(aom_writer *w, int n, int v) {
   int l = get_unsigned_bits(n);
   int m = (1 << l) - n;
@@ -137,7 +120,7 @@
     aom_write_literal(w, (v - m) & 1, 1);
   }
 }
-#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 
 #if CONFIG_EXT_TX
 static struct av1_token ext_tx_inter_encodings[EXT_TX_SETS_INTER][TX_TYPES];
@@ -173,6 +156,13 @@
 #else
   av1_tokens_from_tree(ext_tx_encodings, av1_ext_tx_tree);
 #endif  // CONFIG_EXT_TX
+  av1_tokens_from_tree(intra_mode_encodings, av1_intra_mode_tree);
+  av1_tokens_from_tree(switchable_interp_encodings, av1_switchable_interp_tree);
+  av1_tokens_from_tree(partition_encodings, av1_partition_tree);
+#if !CONFIG_REF_MV
+  av1_tokens_from_tree(inter_mode_encodings, av1_inter_mode_tree);
+#endif
+
 #if CONFIG_EXT_INTRA
   av1_tokens_from_tree(intra_filter_encodings, av1_intra_filter_tree);
 #endif  // CONFIG_EXT_INTRA
@@ -931,22 +921,20 @@
   }
 }
 
-#if CONFIG_EXT_INTRA
-static void write_ext_intra_mode_info(const AV1_COMMON *const cm,
-                                      const MB_MODE_INFO *const mbmi,
-                                      aom_writer *w) {
-#if !ALLOW_FILTER_INTRA_MODES
-  return;
-#endif
+#if CONFIG_FILTER_INTRA
+static void write_filter_intra_mode_info(const AV1_COMMON *const cm,
+                                         const MB_MODE_INFO *const mbmi,
+                                         aom_writer *w) {
   if (mbmi->mode == DC_PRED
 #if CONFIG_PALETTE
       && mbmi->palette_mode_info.palette_size[0] == 0
 #endif  // CONFIG_PALETTE
       ) {
-    aom_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[0],
-              cm->fc->ext_intra_probs[0]);
-    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
-      EXT_INTRA_MODE mode = mbmi->ext_intra_mode_info.ext_intra_mode[0];
+    aom_write(w, mbmi->filter_intra_mode_info.use_filter_intra_mode[0],
+              cm->fc->filter_intra_probs[0]);
+    if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) {
+      const FILTER_INTRA_MODE mode =
+          mbmi->filter_intra_mode_info.filter_intra_mode[0];
       write_uniform(w, FILTER_INTRA_MODES, mode);
     }
   }
@@ -956,15 +944,18 @@
       && mbmi->palette_mode_info.palette_size[1] == 0
 #endif  // CONFIG_PALETTE
       ) {
-    aom_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[1],
-              cm->fc->ext_intra_probs[1]);
-    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
-      EXT_INTRA_MODE mode = mbmi->ext_intra_mode_info.ext_intra_mode[1];
+    aom_write(w, mbmi->filter_intra_mode_info.use_filter_intra_mode[1],
+              cm->fc->filter_intra_probs[1]);
+    if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) {
+      const FILTER_INTRA_MODE mode =
+          mbmi->filter_intra_mode_info.filter_intra_mode[1];
       write_uniform(w, FILTER_INTRA_MODES, mode);
     }
   }
 }
+#endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_EXT_INTRA
 static void write_intra_angle_info(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                                    aom_writer *w) {
   const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
@@ -1230,9 +1221,9 @@
     if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
       write_palette_mode_info(cm, xd, mi, w);
 #endif  // CONFIG_PALETTE
-#if CONFIG_EXT_INTRA
-    if (bsize >= BLOCK_8X8) write_ext_intra_mode_info(cm, mbmi, w);
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+    if (bsize >= BLOCK_8X8) write_filter_intra_mode_info(cm, mbmi, w);
+#endif  // CONFIG_FILTER_INTRA
   } else {
     int16_t mode_ctx = mbmi_ext->mode_context[mbmi->ref_frame[0]];
     write_ref_frames(cm, xd, w);
@@ -1623,9 +1614,9 @@
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     write_palette_mode_info(cm, xd, mi, w);
 #endif  // CONFIG_PALETTE
-#if CONFIG_EXT_INTRA
-  if (bsize >= BLOCK_8X8) write_ext_intra_mode_info(cm, mbmi, w);
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  if (bsize >= BLOCK_8X8) write_filter_intra_mode_info(cm, mbmi, w);
+#endif  // CONFIG_FILTER_INTRA
 
   if (!FIXED_TX_TYPE) {
 #if CONFIG_EXT_TX
@@ -2477,8 +2468,6 @@
 #if CONFIG_ENTROPY
   AV1_COMMON *cm = &cpi->common;
   SUBFRAME_STATS *subframe_stats = &cpi->subframe_stats;
-  unsigned int eob_counts_copy[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS]
-                              [COEFF_CONTEXTS];
   int i;
   av1_coeff_probs_model dummy_frame_coef_probs[PLANE_TYPES];
 
@@ -2503,8 +2492,10 @@
 #if CONFIG_ENTROPY
       if (cm->do_subframe_update &&
           cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
+        unsigned int this_eob_counts_copy[PLANE_TYPES][REF_TYPES][COEF_BANDS]
+                                         [COEFF_CONTEXTS];
         av1_coeff_count coef_counts_copy[PLANE_TYPES];
-        av1_copy(eob_counts_copy, cpi->common.counts.eob_branch[tx_size]);
+        av1_copy(this_eob_counts_copy, cpi->common.counts.eob_branch[tx_size]);
         av1_copy(coef_counts_copy, cpi->td.rd_counts.coef_counts[tx_size]);
         build_tree_distribution(cpi, tx_size, frame_branch_ct,
                                 frame_coef_probs);
@@ -2516,7 +2507,7 @@
           build_tree_distribution(cpi, tx_size, cpi->branch_ct_buf[i][tx_size],
                                   dummy_frame_coef_probs);
         }
-        av1_copy(cpi->common.counts.eob_branch[tx_size], eob_counts_copy);
+        av1_copy(cpi->common.counts.eob_branch[tx_size], this_eob_counts_copy);
         av1_copy(cpi->td.rd_counts.coef_counts[tx_size], coef_counts_copy);
 
         update_coef_probs_subframe(w, cpi, tx_size, cpi->branch_ct_buf,
@@ -2544,6 +2535,8 @@
   av1_copy(subframe_stats->coef_probs_buf[0], cm->fc->coef_probs);
   if (cm->do_subframe_update &&
       cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
+    unsigned int eob_counts_copy[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS]
+                                [COEFF_CONTEXTS];
     av1_copy(eob_counts_copy, cm->counts.eob_branch);
     for (i = 1; i <= cpi->common.coef_probs_update_idx; ++i) {
       for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 66de9c6..04d5282 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -5108,21 +5108,31 @@
       sum_intra_stats(td->counts, mi, xd->above_mi, xd->left_mi,
                       frame_is_intra_only(cm));
 
-#if CONFIG_EXT_INTRA
+    // TODO(huisu): move this into sum_intra_stats().
     if (!dry_run && bsize >= BLOCK_8X8) {
       FRAME_COUNTS *counts = td->counts;
+      (void)counts;
+#if CONFIG_FILTER_INTRA
       if (mbmi->mode == DC_PRED
 #if CONFIG_PALETTE
           && mbmi->palette_mode_info.palette_size[0] == 0
 #endif  // CONFIG_PALETTE
-          )
-        ++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
+          ) {
+        const int use_filter_intra_mode =
+            mbmi->filter_intra_mode_info.use_filter_intra_mode[0];
+        ++counts->filter_intra[0][use_filter_intra_mode];
+      }
       if (mbmi->uv_mode == DC_PRED
 #if CONFIG_PALETTE
           && mbmi->palette_mode_info.palette_size[1] == 0
 #endif  // CONFIG_PALETTE
-          )
-        ++counts->ext_intra[1][mbmi->ext_intra_mode_info.use_ext_intra_mode[1]];
+          ) {
+        const int use_filter_intra_mode =
+            mbmi->filter_intra_mode_info.use_filter_intra_mode[1];
+        ++counts->filter_intra[1][use_filter_intra_mode];
+      }
+#endif  // CONFIG_FILTER_INTRA
+#if CONFIG_EXT_INTRA
       if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) {
         int p_angle;
         const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
@@ -5131,8 +5141,8 @@
         if (av1_is_intra_filter_switchable(p_angle))
           ++counts->intra_filter[intra_filter_ctx][mbmi->intra_filter];
       }
-    }
 #endif  // CONFIG_EXT_INTRA
+    }
 
 #if CONFIG_PALETTE
     if (bsize >= BLOCK_8X8 && !dry_run) {
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index c39b78a..c22c5a8 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -300,7 +300,7 @@
       cm->frame_context_idx = ARF_FRAME;
 #else
     if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
-#endif
+#endif  // CONFIG_EXT_REFS
     else if (cpi->rc.is_src_frame_alt_ref)
       cm->frame_context_idx = OVERLAY_FRAME;
     else if (cpi->refresh_golden_frame)
@@ -308,7 +308,7 @@
 #if CONFIG_EXT_REFS
     else if (cpi->refresh_bwd_ref_frame)
       cm->frame_context_idx = BRF_FRAME;
-#endif
+#endif  // CONFIG_EXT_REFS
     else
       cm->frame_context_idx = REGULAR_FRAME;
   }
@@ -4412,7 +4412,8 @@
     arf_sign_bias =
         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
   }
-#endif
+#endif  // CONFIG_EXT_REFS
+
   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
 #if CONFIG_EXT_REFS
   cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
@@ -4430,7 +4431,7 @@
   // Get which arf used as ALTREF_FRAME
   if (cpi->oxcf.pass == 2)
     arf_idx += cpi->twopass.gf_group.arf_ref_idx[cpi->twopass.gf_group.index];
-#endif
+#endif  // CONFIG_EXT_REFS
 
   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
     return mask;
@@ -4446,7 +4447,7 @@
   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
     for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
-#endif
+#endif  // CONFIG_EXT_REFS
 
   for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
     if ((ref_total[LAST_FRAME] &&
@@ -4597,14 +4598,12 @@
     // Update the frame type
     cm->last_frame_type = cm->frame_type;
 
-#if CONFIG_EXT_REFS
     // Since we allocate a spot for the OVERLAY frame in the gf group, we need
     // to do post-encoding update accordingly.
     if (cpi->rc.is_src_frame_alt_ref) {
       av1_set_target_rate(cpi);
       av1_rc_postencode_update(cpi, *size);
     }
-#endif
 
     cm->last_width = cm->width;
     cm->last_height = cm->height;
@@ -4773,10 +4772,6 @@
   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
 #endif  // !CONFIG_EXT_REFS
 
-#if CONFIG_EXT_REFS
-  cm->last3_frame_type = cm->last2_frame_type;
-  cm->last2_frame_type = cm->last_frame_type;
-#endif  // CONFIG_EXT_REFS
   cm->last_frame_type = cm->frame_type;
 
   av1_rc_postencode_update(cpi, *size);
@@ -5041,7 +5036,7 @@
     rc->is_src_frame_alt_ref =
 #if CONFIG_EXT_REFS
         (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
-#endif
+#endif  // CONFIG_EXT_REFS
         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
   } else {
     rc->is_src_frame_alt_ref =
@@ -5409,7 +5404,8 @@
       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
     }
   }
-#endif
+#endif  // CONFIG_EXT_REFS
+
   // Start with a 0 size frame.
   *size = 0;
 
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 003ecb3..1e10531 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -23,6 +23,7 @@
 #include "aom_scale/aom_scale.h"
 #include "aom_scale/yv12config.h"
 
+#include "aom_dsp/variance.h"
 #include "av1/common/entropymv.h"
 #include "av1/common/quant_common.h"
 #include "av1/common/reconinter.h"  // av1_setup_dst_planes()
@@ -37,7 +38,6 @@
 #include "av1/encoder/mcomp.h"
 #include "av1/encoder/quantize.h"
 #include "av1/encoder/rd.h"
-#include "aom_dsp/variance.h"
 
 #define OUTPUT_FPF 0
 #define ARF_STATS_OUTPUT 0
@@ -1580,10 +1580,6 @@
   double modified_err = 0.0;
   double err_fraction;
   int mid_boost_bits = 0;
-#if !CONFIG_EXT_REFS
-  int mid_frame_idx;
-  unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
-#endif
 #if CONFIG_EXT_REFS
   // The use of bi-predictive frames are only enabled when following 3
   // conditions are met:
@@ -1597,6 +1593,7 @@
           (rc->baseline_gf_interval - rc->source_alt_ref_pending);
   int bipred_group_end = 0;
   int bipred_frame_index = 0;
+
   int arf_pos[MAX_EXT_ARFS + 1];
   const unsigned char ext_arf_interval =
       (unsigned char)(rc->baseline_gf_interval / (cpi->num_extra_arfs + 1) - 1);
@@ -1605,17 +1602,20 @@
   int ext_arf_boost[MAX_EXT_ARFS];
   int is_sg_bipred_enabled = is_bipred_enabled;
   int accumulative_subgroup_interval = 0;
+#else
+  int mid_frame_idx;
+  unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
 #endif  // CONFIG_EXT_REFS
 
 #if CONFIG_EXT_REFS
   av1_zero_array(ext_arf_boost, MAX_EXT_ARFS);
-#endif
+#endif  // CONFIG_EXT_REFS
 
   key_frame = cpi->common.frame_type == KEY_FRAME;
 
 #if !CONFIG_EXT_REFS
   get_arf_buffer_indices(arf_buffer_indices);
-#endif
+#endif  // !CONFIG_EXT_REFS
 
   // For key frames the frame target rate is already set and it
   // is also the golden frame.
@@ -1635,7 +1635,7 @@
 #else
     gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
     gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
-#endif
+#endif  // CONFIG_EXT_REFS
     // Step over the golden frame / overlay frame
     if (EOF == input_stats(twopass, &frame_stats)) return;
   }
@@ -1667,16 +1667,15 @@
 #if CONFIG_EXT_REFS
     gf_group->arf_update_idx[frame_index] = 0;
     gf_group->arf_ref_idx[frame_index] = 0;
+
+    gf_group->bidir_pred_enabled[frame_index] = 0;
+    gf_group->brf_src_offset[frame_index] = 0;
+// NOTE: "bidir_pred_frame_index" stays unchanged for ARF_UPDATE frames.
 #else
     gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
     gf_group->arf_ref_idx[frame_index] =
         arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
                            rc->source_alt_ref_active];
-#endif  // CONFIG_EXT_REFS && CONFIG_EXT_ARFS
-#if CONFIG_EXT_REFS
-    gf_group->bidir_pred_enabled[frame_index] = 0;
-    gf_group->brf_src_offset[frame_index] = 0;
-// NOTE: "bidir_pred_frame_index" stays unchanged for ARF_UPDATE frames.
 #endif  // CONFIG_EXT_REFS
 
 #if CONFIG_EXT_REFS
@@ -1730,13 +1729,14 @@
 #if !CONFIG_EXT_REFS
   // Define middle frame
   mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
-#endif
+#endif  // !CONFIG_EXT_REFS
 
   // Allocate bits to the other frames in the group.
   for (i = 0; i < rc->baseline_gf_interval - rc->source_alt_ref_pending; ++i) {
 #if !CONFIG_EXT_REFS
     int arf_idx = 0;
-#endif
+#endif  // !CONFIG_EXT_REFS
+
     if (EOF == input_stats(twopass, &frame_stats)) break;
 
     modified_err = calculate_modified_err(cpi, twopass, oxcf, &frame_stats);
@@ -1753,8 +1753,9 @@
       target_frame_size -= (target_frame_size >> 4);
 #if !CONFIG_EXT_REFS
       if (frame_index <= mid_frame_idx) arf_idx = 1;
-#endif
+#endif  // !CONFIG_EXT_REFS
     }
+
 #if CONFIG_EXT_REFS
     gf_group->arf_update_idx[frame_index] = which_arf;
     gf_group->arf_ref_idx[frame_index] = which_arf;
@@ -1762,6 +1763,7 @@
     gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
     gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
 #endif  // CONFIG_EXT_REFS
+
     target_frame_size =
         clamp(target_frame_size, 0, AOMMIN(max_bits, (int)total_group_bits));
 
@@ -1773,6 +1775,7 @@
           is_bipred_enabled &&
           (subgroup_interval[which_arf] > rc->bipred_group_interval);
     }
+
     // NOTE: BIDIR_PRED is only enabled when the length of the bi-predictive
     //       frame group interval is strictly smaller than that of the GOLDEN
     //       FRAME group interval.
@@ -1841,6 +1844,7 @@
 #endif  // CONFIG_EXT_REFS
 
     ++frame_index;
+
 #if CONFIG_EXT_REFS
     // Check if we need to update the ARF
     if (cpi->num_extra_arfs && frame_index > arf_pos[which_arf]) {
@@ -1858,7 +1862,7 @@
         ++frame_index;
       }
     }
-#endif
+#endif  // CONFIG_EXT_REFS
   }
 
 // Note:
@@ -1871,7 +1875,8 @@
 #else
   gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
   gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
-#endif
+#endif  // CONFIG_EXT_REFS
+
   if (rc->source_alt_ref_pending) {
     gf_group->update_type[frame_index] = OVERLAY_UPDATE;
     gf_group->rf_level[frame_index] = INTER_NORMAL;
@@ -1887,8 +1892,7 @@
         gf_group->rf_level[arf_pos[i]] = INTER_LOW;
       }
     }
-#endif
-#if !CONFIG_EXT_REFS
+#else
     // Final setup for second arf and its overlay.
     if (cpi->multi_arf_enabled) {
       gf_group->bit_allocation[2] =
@@ -1896,11 +1900,12 @@
       gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
       gf_group->bit_allocation[mid_frame_idx] = 0;
     }
-#endif
+#endif  // CONFIG_EXT_REFS
   } else {
     gf_group->update_type[frame_index] = GF_UPDATE;
     gf_group->rf_level[frame_index] = GF_ARF_STD;
   }
+
 #if CONFIG_EXT_REFS
   gf_group->bidir_pred_enabled[frame_index] = 0;
   gf_group->brf_src_offset[frame_index] = 0;
@@ -1909,6 +1914,7 @@
   // Note whether multi-arf was enabled this group for next time.
   cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
 }
+
 // Analyse and define a gf/arf group.
 static void define_gf_group(AV1_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   AV1_COMMON *const cm = &cpi->common;
@@ -2116,8 +2122,8 @@
   cpi->num_extra_arfs = get_number_of_extra_arfs(rc->baseline_gf_interval,
                                                  rc->source_alt_ref_pending);
   // Currently at maximum two extra ARFs' are allowed
-  assert(cpi->num_extra_arfs <= 2);
-#endif
+  assert(cpi->num_extra_arfs <= MAX_EXT_ARFS);
+#endif  // CONFIG_EXT_REFS
 
   rc->frames_till_gf_update_due = rc->baseline_gf_interval;
 
diff --git a/av1/encoder/firstpass.h b/av1/encoder/firstpass.h
index 84f9791..db459cc 100644
--- a/av1/encoder/firstpass.h
+++ b/av1/encoder/firstpass.h
@@ -193,7 +193,7 @@
   else
     return 0;
 }
-#endif
+#endif  // CONFIG_EXT_REFS
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index aaca103..2141f30 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -193,7 +193,7 @@
   if (cpi->rc.is_src_frame_alt_ref) {
 #else
   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
-#endif
+#endif  // CONFIG_EXT_REFS
     // If there is an active ARF at this location use the minimum
     // bits on this frame even if it is a constructed arf.
     // The active maximum quantizer insures that an appropriate
@@ -1189,11 +1189,12 @@
 #else
   // Update the Golden frame usage counts.
   if (cpi->refresh_golden_frame) {
-#endif
+#endif  // CONFIG_EXT_REFS
+
 #if CONFIG_EXT_REFS
     // We will not use internal overlay frames to replace the golden frame
     if (!rc->is_src_frame_ext_arf)
-#endif
+#endif  // CONFIG_EXT_REFS
       // this frame refreshes means next frames don't unless specified by user
       rc->frames_since_golden = 0;
 
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index eb453e2..36164c8 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -417,7 +417,7 @@
   { { INTRA_FRAME, NONE } },
 };
 
-#if CONFIG_EXT_INTRA || CONFIG_PALETTE
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 static INLINE int write_uniform_cost(int n, int v) {
   int l = get_unsigned_bits(n), m = (1 << l) - n;
   if (l == 0) return 0;
@@ -426,7 +426,7 @@
   else
     return l * av1_cost_bit(128, 0);
 }
-#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
 
 // constants for prune 1 and prune 2 decision boundaries
 #define FAST_EXT_TX_CORR_MID 0.0
@@ -1764,9 +1764,9 @@
 #endif  // CONFIG_AOM_HIGHBITDEPTH
     colors = av1_count_colors(src, src_stride, rows, cols);
   palette_mode_info->palette_size[0] = 0;
-#if CONFIG_EXT_INTRA
-  mic->mbmi.ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+#endif  // CONFIG_FILTER_INTRA
 
   if (colors > 1 && colors <= 64) {
     int r, c, i, j, k;
@@ -1816,9 +1816,9 @@
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
     mbmi->mode = DC_PRED;
-#if CONFIG_EXT_INTRA
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+#endif  // CONFIG_FILTER_INTRA
 
     if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return 0;
 
@@ -2177,9 +2177,11 @@
   const int *bmode_costs = cpi->mbmode_cost[0];
 
 #if CONFIG_EXT_INTRA
-  mic->mbmi.ext_intra_mode_info.use_ext_intra_mode[0] = 0;
   mic->mbmi.intra_filter = INTRA_FILTER_LINEAR;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+#endif  // CONFIG_FILTER_INTRA
 
   // TODO(any): Add search of the tx_type to improve rd performance at the
   // expense of speed.
@@ -2251,26 +2253,26 @@
   return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion);
 }
 
-#if CONFIG_EXT_INTRA
-// Return 1 if an ext intra mode is selected; return 0 otherwise.
-static int rd_pick_ext_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
-                                 int *rate, int *rate_tokenonly,
-                                 int64_t *distortion, int *skippable,
-                                 BLOCK_SIZE bsize, int mode_cost,
-                                 int64_t *best_rd, uint16_t skip_mask) {
+#if CONFIG_FILTER_INTRA
+// Return 1 if an filter intra mode is selected; return 0 otherwise.
+static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
+                                    int *rate, int *rate_tokenonly,
+                                    int64_t *distortion, int *skippable,
+                                    BLOCK_SIZE bsize, int mode_cost,
+                                    int64_t *best_rd, uint16_t skip_mask) {
   MACROBLOCKD *const xd = &x->e_mbd;
   MODE_INFO *const mic = xd->mi[0];
   MB_MODE_INFO *mbmi = &mic->mbmi;
   int this_rate, this_rate_tokenonly, s;
-  int ext_intra_selected_flag = 0;
+  int filter_intra_selected_flag = 0;
   int64_t this_distortion, this_rd;
-  EXT_INTRA_MODE mode;
+  FILTER_INTRA_MODE mode;
   TX_SIZE best_tx_size = TX_4X4;
-  EXT_INTRA_MODE_INFO ext_intra_mode_info;
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
   TX_TYPE best_tx_type;
 
-  av1_zero(ext_intra_mode_info);
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 1;
+  av1_zero(filter_intra_mode_info);
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1;
   mbmi->mode = DC_PRED;
 #if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
@@ -2278,43 +2280,45 @@
 
   for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
     if (skip_mask & (1 << mode)) continue;
-    mbmi->ext_intra_mode_info.ext_intra_mode[0] = mode;
+    mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode;
     super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL,
                     bsize, *best_rd);
     if (this_rate_tokenonly == INT_MAX) continue;
 
     this_rate = this_rate_tokenonly +
-                av1_cost_bit(cpi->common.fc->ext_intra_probs[0], 1) +
+                av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 1) +
                 write_uniform_cost(FILTER_INTRA_MODES, mode) + mode_cost;
     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
 
     if (this_rd < *best_rd) {
       *best_rd = this_rd;
       best_tx_size = mic->mbmi.tx_size;
-      ext_intra_mode_info = mbmi->ext_intra_mode_info;
+      filter_intra_mode_info = mbmi->filter_intra_mode_info;
       best_tx_type = mic->mbmi.tx_type;
       *rate = this_rate;
       *rate_tokenonly = this_rate_tokenonly;
       *distortion = this_distortion;
       *skippable = s;
-      ext_intra_selected_flag = 1;
+      filter_intra_selected_flag = 1;
     }
   }
 
-  if (ext_intra_selected_flag) {
+  if (filter_intra_selected_flag) {
     mbmi->mode = DC_PRED;
     mbmi->tx_size = best_tx_size;
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[0] =
-        ext_intra_mode_info.use_ext_intra_mode[0];
-    mbmi->ext_intra_mode_info.ext_intra_mode[0] =
-        ext_intra_mode_info.ext_intra_mode[0];
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[0] =
+        filter_intra_mode_info.use_filter_intra_mode[0];
+    mbmi->filter_intra_mode_info.filter_intra_mode[0] =
+        filter_intra_mode_info.filter_intra_mode[0];
     mbmi->tx_type = best_tx_type;
     return 1;
   } else {
     return 0;
   }
 }
+#endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_EXT_INTRA
 static void pick_intra_angle_routine_sby(
     const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly,
     int64_t *distortion, int *skippable, int *best_angle_delta,
@@ -2624,15 +2628,17 @@
 #endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
-  EXT_INTRA_MODE_INFO ext_intra_mode_info;
   int is_directional_mode, rate_overhead, best_angle_delta = 0;
   INTRA_FILTER best_filter = INTRA_FILTER_LINEAR;
   uint8_t directional_mode_skip_mask[INTRA_MODES];
-  uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1;
   const int src_stride = x->plane[0].src.stride;
   const uint8_t *src = x->plane[0].src.buf;
-  int beat_best_rd = 0;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  int beat_best_rd = 0;
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
+  uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1;
+#endif  // CONFIG_FILTER_INTRA
   TX_TYPE best_tx_type = DCT_DCT;
   const int *bmode_costs;
 #if CONFIG_PALETTE
@@ -2653,8 +2659,6 @@
   bmode_costs = cpi->y_mode_costs[A][L];
 
 #if CONFIG_EXT_INTRA
-  ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-  mic->mbmi.ext_intra_mode_info.use_ext_intra_mode[0] = 0;
   mic->mbmi.angle_delta[0] = 0;
   memset(directional_mode_skip_mask, 0,
          sizeof(directional_mode_skip_mask[0]) * INTRA_MODES);
@@ -2666,6 +2670,10 @@
 #endif
     angle_estimation(src, src_stride, rows, cols, directional_mode_skip_mask);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+  mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_PALETTE
   palette_mode_info.palette_size[0] = 0;
   pmi->palette_size[0] = 0;
@@ -2729,9 +2737,11 @@
       this_rate += av1_cost_bit(
           av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
 #endif  // CONFIG_PALETTE
+#if CONFIG_FILTER_INTRA
+    if (mic->mbmi.mode == DC_PRED)
+      this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 0);
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_EXT_INTRA
-    if (mic->mbmi.mode == DC_PRED && ALLOW_FILTER_INTRA_MODES)
-      this_rate += av1_cost_bit(cpi->common.fc->ext_intra_probs[0], 0);
     if (is_directional_mode) {
       int p_angle;
       this_rate +=
@@ -2745,11 +2755,11 @@
     }
 #endif  // CONFIG_EXT_INTRA
     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
-#if CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
     if (best_rd == INT64_MAX || this_rd - best_rd < (best_rd >> 4)) {
       filter_intra_mode_skip_mask ^= (1 << mic->mbmi.mode);
     }
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
     if (this_rd < best_rd) {
       mode_selected = mic->mbmi.mode;
@@ -2758,8 +2768,10 @@
 #if CONFIG_EXT_INTRA
       best_angle_delta = mic->mbmi.angle_delta[0];
       best_filter = mic->mbmi.intra_filter;
-      beat_best_rd = 1;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+      beat_best_rd = 1;
+#endif  // CONFIG_FILTER_INTRA
       best_tx_type = mic->mbmi.tx_type;
       *rate = this_rate;
       *rate_tokenonly = this_rate_tokenonly;
@@ -2776,28 +2788,28 @@
                               &best_rd);
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-  if (ALLOW_FILTER_INTRA_MODES && beat_best_rd) {
-    if (rd_pick_ext_intra_sby(cpi, x, rate, rate_tokenonly, distortion,
-                              skippable, bsize, bmode_costs[DC_PRED], &best_rd,
-                              filter_intra_mode_skip_mask)) {
+#if CONFIG_FILTER_INTRA
+  if (beat_best_rd) {
+    if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion,
+                                 skippable, bsize, bmode_costs[DC_PRED],
+                                 &best_rd, filter_intra_mode_skip_mask)) {
       mode_selected = mic->mbmi.mode;
       best_tx = mic->mbmi.tx_size;
-      ext_intra_mode_info = mic->mbmi.ext_intra_mode_info;
+      filter_intra_mode_info = mic->mbmi.filter_intra_mode_info;
       best_tx_type = mic->mbmi.tx_type;
     }
   }
 
-  mic->mbmi.ext_intra_mode_info.use_ext_intra_mode[0] =
-      ext_intra_mode_info.use_ext_intra_mode[0];
-  if (ext_intra_mode_info.use_ext_intra_mode[0]) {
-    mic->mbmi.ext_intra_mode_info.ext_intra_mode[0] =
-        ext_intra_mode_info.ext_intra_mode[0];
+  mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] =
+      filter_intra_mode_info.use_filter_intra_mode[0];
+  if (filter_intra_mode_info.use_filter_intra_mode[0]) {
+    mic->mbmi.filter_intra_mode_info.filter_intra_mode[0] =
+        filter_intra_mode_info.filter_intra_mode[0];
 #if CONFIG_PALETTE
     palette_mode_info.palette_size[0] = 0;
 #endif  // CONFIG_PALETTE
   }
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
   mic->mbmi.mode = mode_selected;
   mic->mbmi.tx_size = best_tx;
@@ -3620,9 +3632,9 @@
 
   if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return;
 
-#if CONFIG_EXT_INTRA
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
 
 #if CONFIG_AOM_HIGHBITDEPTH
   if (cpi->common.use_highbitdepth) {
@@ -3671,9 +3683,9 @@
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
     mbmi->uv_mode = DC_PRED;
-#if CONFIG_EXT_INTRA
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
     for (r = 0; r < rows; ++r) {
       for (c = 0; c < cols; ++c) {
 #if CONFIG_AOM_HIGHBITDEPTH
@@ -3765,35 +3777,35 @@
 }
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-// Return 1 if an ext intra mode is selected; return 0 otherwise.
-static int rd_pick_ext_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
-                                  int *rate, int *rate_tokenonly,
-                                  int64_t *distortion, int *skippable,
-                                  BLOCK_SIZE bsize, int64_t *best_rd) {
+#if CONFIG_FILTER_INTRA
+// Return 1 if an filter intra mode is selected; return 0 otherwise.
+static int rd_pick_filter_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
+                                     int *rate, int *rate_tokenonly,
+                                     int64_t *distortion, int *skippable,
+                                     BLOCK_SIZE bsize, int64_t *best_rd) {
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
-  int ext_intra_selected_flag = 0;
+  int filter_intra_selected_flag = 0;
   int this_rate_tokenonly, this_rate, s;
   int64_t this_distortion, this_sse, this_rd;
-  EXT_INTRA_MODE mode;
-  EXT_INTRA_MODE_INFO ext_intra_mode_info;
+  FILTER_INTRA_MODE mode;
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
 
-  av1_zero(ext_intra_mode_info);
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 1;
+  av1_zero(filter_intra_mode_info);
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 1;
   mbmi->uv_mode = DC_PRED;
 #if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[1] = 0;
 #endif  // CONFIG_PALETTE
 
   for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
-    mbmi->ext_intra_mode_info.ext_intra_mode[1] = mode;
+    mbmi->filter_intra_mode_info.filter_intra_mode[1] = mode;
     if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s,
                           &this_sse, bsize, *best_rd))
       continue;
 
     this_rate = this_rate_tokenonly +
-                av1_cost_bit(cpi->common.fc->ext_intra_probs[1], 1) +
+                av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 1) +
                 cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode] +
                 write_uniform_cost(FILTER_INTRA_MODES, mode);
     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
@@ -3803,23 +3815,25 @@
       *rate_tokenonly = this_rate_tokenonly;
       *distortion = this_distortion;
       *skippable = s;
-      ext_intra_mode_info = mbmi->ext_intra_mode_info;
-      ext_intra_selected_flag = 1;
+      filter_intra_mode_info = mbmi->filter_intra_mode_info;
+      filter_intra_selected_flag = 1;
     }
   }
 
-  if (ext_intra_selected_flag) {
+  if (filter_intra_selected_flag) {
     mbmi->uv_mode = DC_PRED;
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-        ext_intra_mode_info.use_ext_intra_mode[1];
-    mbmi->ext_intra_mode_info.ext_intra_mode[1] =
-        ext_intra_mode_info.ext_intra_mode[1];
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
+        filter_intra_mode_info.use_filter_intra_mode[1];
+    mbmi->filter_intra_mode_info.filter_intra_mode[1] =
+        filter_intra_mode_info.filter_intra_mode[1];
     return 1;
   } else {
     return 0;
   }
 }
+#endif  // CONFIG_FILTER_INTRA
 
+#if CONFIG_EXT_INTRA
 static void pick_intra_angle_routine_sbuv(
     const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly,
     int64_t *distortion, int *skippable, int *best_angle_delta,
@@ -3937,11 +3951,13 @@
 #endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   int is_directional_mode, rate_overhead, best_angle_delta = 0;
-  EXT_INTRA_MODE_INFO ext_intra_mode_info;
-
-  ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
+
+  filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_PALETTE
   palette_mode_info.palette_size[1] = 0;
   pmi->palette_size[1] = 0;
@@ -3969,15 +3985,16 @@
     if (mbmi->sb_type >= BLOCK_8X8 && is_directional_mode)
       this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1,
                                       MAX_ANGLE_DELTAS + mbmi->angle_delta[1]);
-    if (mbmi->sb_type >= BLOCK_8X8 && mode == DC_PRED &&
-        ALLOW_FILTER_INTRA_MODES)
-      this_rate += av1_cost_bit(cpi->common.fc->ext_intra_probs[1], 0);
 #else
     if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s,
                           &this_sse, bsize, best_rd))
       continue;
     this_rate = this_rate_tokenonly + cpi->intra_uv_mode_cost[mbmi->mode][mode];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+    if (mbmi->sb_type >= BLOCK_8X8 && mode == DC_PRED)
+      this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 0);
+#endif  // CONFIG_FILTER_INTRA
 #if CONFIG_PALETTE
     if (cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 &&
         mode == DC_PRED)
@@ -4010,24 +4027,27 @@
   }
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-  if (mbmi->sb_type >= BLOCK_8X8 && ALLOW_FILTER_INTRA_MODES) {
-    if (rd_pick_ext_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion,
-                               skippable, bsize, &best_rd)) {
+#if CONFIG_FILTER_INTRA
+  if (mbmi->sb_type >= BLOCK_8X8) {
+    if (rd_pick_filter_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion,
+                                  skippable, bsize, &best_rd)) {
       mode_selected = mbmi->uv_mode;
-      ext_intra_mode_info = mbmi->ext_intra_mode_info;
+      filter_intra_mode_info = mbmi->filter_intra_mode_info;
     }
   }
 
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-      ext_intra_mode_info.use_ext_intra_mode[1];
-  if (ext_intra_mode_info.use_ext_intra_mode[1]) {
-    mbmi->ext_intra_mode_info.ext_intra_mode[1] =
-        ext_intra_mode_info.ext_intra_mode[1];
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
+      filter_intra_mode_info.use_filter_intra_mode[1];
+  if (filter_intra_mode_info.use_filter_intra_mode[1]) {
+    mbmi->filter_intra_mode_info.filter_intra_mode[1] =
+        filter_intra_mode_info.filter_intra_mode[1];
 #if CONFIG_PALETTE
     palette_mode_info.palette_size[1] = 0;
 #endif  // CONFIG_PALETTE
   }
+#endif  // CONFIG_FILTER_INTRA
+
+#if CONFIG_EXT_INTRA
   mbmi->angle_delta[1] = best_angle_delta;
 #endif  // CONFIG_EXT_INTRA
   mbmi->uv_mode = mode_selected;
@@ -7935,12 +7955,15 @@
 }
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-static void pick_ext_intra_interframe(
-    const AV1_COMP *const cpi, MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
+#if CONFIG_FILTER_INTRA
+static void pick_filter_intra_interframe(
+    const AV1_COMP *cpi, MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
     BLOCK_SIZE bsize, int *rate_uv_intra, int *rate_uv_tokenonly,
     int64_t *dist_uv, int *skip_uv, PREDICTION_MODE *mode_uv,
-    EXT_INTRA_MODE_INFO *ext_intra_mode_info_uv, int8_t *uv_angle_delta,
+    FILTER_INTRA_MODE_INFO *filter_intra_mode_info_uv,
+#if CONFIG_EXT_INTRA
+    int8_t *uv_angle_delta,
+#endif  // CONFIG_EXT_INTRA
 #if CONFIG_PALETTE
     PALETTE_MODE_INFO *pmi_uv, int palette_ctx,
 #endif  // CONFIG_PALETTE
@@ -7977,10 +8000,11 @@
   mbmi->uv_mode = DC_PRED;
   mbmi->ref_frame[0] = INTRA_FRAME;
   mbmi->ref_frame[1] = NONE;
-  if (!rd_pick_ext_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y,
-                             &skippable, bsize, intra_mode_cost[mbmi->mode],
-                             &this_rd, 0))
+  if (!rd_pick_filter_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y,
+                                &skippable, bsize, intra_mode_cost[mbmi->mode],
+                                &this_rd, 0)) {
     return;
+  }
   if (rate_y == INT_MAX) return;
 
   uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x]
@@ -7992,8 +8016,10 @@
 #if CONFIG_PALETTE
     if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi;
 #endif  // CONFIG_PALETTE
-    ext_intra_mode_info_uv[uv_tx] = mbmi->ext_intra_mode_info;
+    filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
+#if CONFIG_EXT_INTRA
     uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
+#endif  // CONFIG_EXT_INTRA
   }
 
   rate_uv = rate_uv_tokenonly[uv_tx];
@@ -8008,12 +8034,14 @@
            2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
   }
 #endif  // CONFIG_PALETTE
+#if CONFIG_EXT_INTRA
   mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-      ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1];
-  if (ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1]) {
-    mbmi->ext_intra_mode_info.ext_intra_mode[1] =
-        ext_intra_mode_info_uv[uv_tx].ext_intra_mode[1];
+#endif  // CONFIG_EXT_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
+      filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
+  if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
+    mbmi->filter_intra_mode_info.filter_intra_mode[1] =
+        filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
   }
 
   rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
@@ -8033,20 +8061,24 @@
                                [mbmi->tx_size];
   }
 
-  rate2 += av1_cost_bit(cm->fc->ext_intra_probs[0],
-                        mbmi->ext_intra_mode_info.use_ext_intra_mode[0]);
-  rate2 += write_uniform_cost(FILTER_INTRA_MODES,
-                              mbmi->ext_intra_mode_info.ext_intra_mode[0]);
+  rate2 += av1_cost_bit(cm->fc->filter_intra_probs[0],
+                        mbmi->filter_intra_mode_info.use_filter_intra_mode[0]);
+  rate2 += write_uniform_cost(
+      FILTER_INTRA_MODES, mbmi->filter_intra_mode_info.filter_intra_mode[0]);
+#if CONFIG_EXT_INTRA
   if (mbmi->uv_mode != DC_PRED && mbmi->uv_mode != TM_PRED) {
     rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1,
                                 MAX_ANGLE_DELTAS + mbmi->angle_delta[1]);
   }
+#endif  // CONFIG_EXT_INTRA
   if (mbmi->mode == DC_PRED) {
-    rate2 += av1_cost_bit(cpi->common.fc->ext_intra_probs[1],
-                          mbmi->ext_intra_mode_info.use_ext_intra_mode[1]);
-    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1])
-      rate2 += write_uniform_cost(FILTER_INTRA_MODES,
-                                  mbmi->ext_intra_mode_info.ext_intra_mode[1]);
+    rate2 +=
+        av1_cost_bit(cpi->common.fc->filter_intra_probs[1],
+                     mbmi->filter_intra_mode_info.use_filter_intra_mode[1]);
+    if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1])
+      rate2 +=
+          write_uniform_cost(FILTER_INTRA_MODES,
+                             mbmi->filter_intra_mode_info.filter_intra_mode[1]);
   }
   distortion2 = distortion_y + distortion_uv;
   av1_encode_intra_block_plane(x, bsize, 0, 0);
@@ -8103,7 +8135,7 @@
     *best_mode_skippable = skippable;
   }
 }
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
 #if CONFIG_MOTION_VAR
 static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x,
@@ -8184,12 +8216,15 @@
   PALETTE_MODE_INFO pmi_uv[TX_SIZES];
 #endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
-  EXT_INTRA_MODE_INFO ext_intra_mode_info_uv[TX_SIZES];
-  int8_t uv_angle_delta[TX_SIZES], dc_skipped = 1;
+  int8_t uv_angle_delta[TX_SIZES];
   int is_directional_mode, angle_stats_ready = 0;
   int rate_overhead, rate_dummy;
   uint8_t directional_mode_skip_mask[INTRA_MODES];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  int8_t dc_skipped = 1;
+  FILTER_INTRA_MODE_INFO filter_intra_mode_info_uv[TX_SIZES];
+#endif  // CONFIG_FILTER_INTRA
   const int intra_cost_penalty = av1_get_intra_cost_penalty(
       cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth);
   const int *const intra_mode_cost = cpi->mbmode_cost[size_group_lookup[bsize]];
@@ -8650,10 +8685,10 @@
     pmi->palette_size[0] = 0;
     pmi->palette_size[1] = 0;
 #endif  // CONFIG_PALETTE
-#if CONFIG_EXT_INTRA
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
         // Evaluate all sub-pel filters irrespective of whether we can use
         // them for this frame.
 #if CONFIG_DUAL_FILTER
@@ -8720,9 +8755,9 @@
 
       if (rate_y == INT_MAX) continue;
 
-#if CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
       if (mbmi->mode == DC_PRED) dc_skipped = 0;
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
       uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x]
                               [pd->subsampling_y];
@@ -8735,9 +8770,11 @@
 #endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
-        ext_intra_mode_info_uv[uv_tx] = mbmi->ext_intra_mode_info;
         uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+        filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
+#endif  // CONFIG_FILTER_INTRA
       }
 
       rate_uv = rate_uv_tokenonly[uv_tx];
@@ -8755,13 +8792,15 @@
 
 #if CONFIG_EXT_INTRA
       mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
-      mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-          ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1];
-      if (ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1]) {
-        mbmi->ext_intra_mode_info.ext_intra_mode[1] =
-            ext_intra_mode_info_uv[uv_tx].ext_intra_mode[1];
-      }
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+      mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
+          filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
+      if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
+        mbmi->filter_intra_mode_info.filter_intra_mode[1] =
+            filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
+      }
+#endif  // CONFIG_FILTER_INTRA
 
       rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
               cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
@@ -8790,29 +8829,32 @@
         if (av1_is_intra_filter_switchable(p_angle))
           rate2 += cpi->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter];
       }
-
-      if (mbmi->mode == DC_PRED && ALLOW_FILTER_INTRA_MODES) {
-        rate2 += av1_cost_bit(cm->fc->ext_intra_probs[0],
-                              mbmi->ext_intra_mode_info.use_ext_intra_mode[0]);
-        if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
-          rate2 += write_uniform_cost(
-              FILTER_INTRA_MODES, mbmi->ext_intra_mode_info.ext_intra_mode[0]);
-        }
-      }
-
       if (mbmi->uv_mode != DC_PRED && mbmi->uv_mode != TM_PRED) {
         rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1,
                                     MAX_ANGLE_DELTAS + mbmi->angle_delta[1]);
       }
-
-      if (ALLOW_FILTER_INTRA_MODES && mbmi->mode == DC_PRED) {
-        rate2 += av1_cost_bit(cpi->common.fc->ext_intra_probs[1],
-                              mbmi->ext_intra_mode_info.use_ext_intra_mode[1]);
-        if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1])
-          rate2 += write_uniform_cost(
-              FILTER_INTRA_MODES, mbmi->ext_intra_mode_info.ext_intra_mode[1]);
-      }
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+      if (mbmi->mode == DC_PRED) {
+        rate2 +=
+            av1_cost_bit(cm->fc->filter_intra_probs[0],
+                         mbmi->filter_intra_mode_info.use_filter_intra_mode[0]);
+        if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) {
+          rate2 += write_uniform_cost(
+              FILTER_INTRA_MODES,
+              mbmi->filter_intra_mode_info.filter_intra_mode[0]);
+        }
+      }
+      if (mbmi->uv_mode == DC_PRED) {
+        rate2 +=
+            av1_cost_bit(cpi->common.fc->filter_intra_probs[1],
+                         mbmi->filter_intra_mode_info.use_filter_intra_mode[1]);
+        if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1])
+          rate2 += write_uniform_cost(
+              FILTER_INTRA_MODES,
+              mbmi->filter_intra_mode_info.filter_intra_mode[1]);
+      }
+#endif  // CONFIG_FILTER_INTRA
       if (this_mode != DC_PRED && this_mode != TM_PRED)
         rate2 += intra_cost_penalty;
       distortion2 = distortion_y + distortion_uv;
@@ -8840,15 +8882,17 @@
       if (second_ref_frame == INTRA_FRAME) {
         if (best_single_inter_ref != ref_frame) continue;
         mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode];
+// TODO(debargha|geza.lore):
+// Should we use ext_intra modes for interintra?
 #if CONFIG_EXT_INTRA
-        // TODO(debargha|geza.lore):
-        // Should we use ext_intra modes for interintra?
-        mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-        mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
         mbmi->angle_delta[0] = 0;
         mbmi->angle_delta[1] = 0;
         mbmi->intra_filter = INTRA_FILTER_LINEAR;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+        mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+        mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
       }
 #endif  // CONFIG_EXT_INTER
 #if CONFIG_REF_MV
@@ -9379,9 +9423,11 @@
                            &skip_uvs[uv_tx], &mode_uv[uv_tx]);
       pmi_uv[uv_tx] = *pmi;
 #if CONFIG_EXT_INTRA
-      ext_intra_mode_info_uv[uv_tx] = mbmi->ext_intra_mode_info;
       uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+      filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
+#endif  // CONFIG_FILTER_INTRA
     }
     mbmi->uv_mode = mode_uv[uv_tx];
     pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
@@ -9391,15 +9437,17 @@
              2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
 #if CONFIG_EXT_INTRA
     mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
-    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
-        ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1];
-    if (ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1]) {
-      mbmi->ext_intra_mode_info.ext_intra_mode[1] =
-          ext_intra_mode_info_uv[uv_tx].ext_intra_mode[1];
-    }
 #endif  // CONFIG_EXT_INTRA
-    skippable = skippable && skip_uvs[uv_tx];
-    distortion2 = distortion_y + dist_uvs[uv_tx];
+#if CONFIG_FILTER_INTRA
+    mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
+        filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
+    if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
+      mbmi->filter_intra_mode_info.filter_intra_mode[1] =
+          filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
+    }
+#endif  // CONFIG_FILTER_INTRA
+    skippable = skippable && skip_uv[uv_tx];
+    distortion2 = distortion_y + dist_uv[uv_tx];
     rate2 = rate_y + rate_overhead + rate_uv_intra[uv_tx];
     rate2 += ref_costs_single[INTRA_FRAME];
 
@@ -9434,18 +9482,21 @@
 PALETTE_EXIT:
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-  // TODO(huisu): ext-intra is turned off in lossless mode for now to
+#if CONFIG_FILTER_INTRA
+  // TODO(huisu): filter-intra is turned off in lossless mode for now to
   // avoid a unit test failure
-  if (!xd->lossless[mbmi->segment_id] && ALLOW_FILTER_INTRA_MODES &&
+  if (!xd->lossless[mbmi->segment_id] &&
 #if CONFIG_PALETTE
       mbmi->palette_mode_info.palette_size[0] == 0 &&
 #endif  // CONFIG_PALETTE
       !dc_skipped && best_mode_index >= 0 &&
       best_intra_rd < (best_rd + (best_rd >> 3))) {
-    pick_ext_intra_interframe(
+    pick_filter_intra_interframe(
         cpi, x, ctx, bsize, rate_uv_intra, rate_uv_tokenonly, dist_uvs,
-        skip_uvs, mode_uv, ext_intra_mode_info_uv, uv_angle_delta,
+        skip_uvs, mode_uv, filter_intra_mode_info_uv,
+#if CONFIG_EXT_INTRA
+        uv_angle_delta,
+#endif  // CONFIG_EXT_INTRA
 #if CONFIG_PALETTE
         pmi_uv, palette_ctx,
 #endif  // CONFIG_PALETTE
@@ -9456,7 +9507,7 @@
 #endif  // CONFIG_SUPERTX
         best_pred_rd, &best_mbmode, rd_cost);
   }
-#endif  // CONFIG_EXT_INTRA
+#endif  // CONFIG_FILTER_INTRA
 
   // The inter modes' rate costs are not calculated precisely in some cases.
   // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and
@@ -9728,10 +9779,10 @@
   mbmi->palette_mode_info.palette_size[1] = 0;
 #endif  // CONFIG_PALETTE
 
-#if CONFIG_EXT_INTRA
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
   mbmi->mode = ZEROMV;
   mbmi->motion_mode = SIMPLE_TRANSLATION;
   mbmi->uv_mode = DC_PRED;
@@ -9899,10 +9950,10 @@
 #endif  // CONFIG_SUPERTX
   av1_zero(best_mbmode);
 
-#if CONFIG_EXT_INTRA
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
-  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
-#endif  // CONFIG_EXT_INTRA
+#if CONFIG_FILTER_INTRA
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
+  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
+#endif  // CONFIG_FILTER_INTRA
   mbmi->motion_mode = SIMPLE_TRANSLATION;
 #if CONFIG_EXT_INTER
   mbmi->use_wedge_interinter = 0;
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 190dcc1..d32ff7d 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -134,6 +134,11 @@
   const int boosted = frame_is_boosted(cpi);
 
   if (speed >= 1) {
+    sf->tx_type_search.fast_intra_tx_type_search = 1;
+    sf->tx_type_search.fast_inter_tx_type_search = 1;
+  }
+
+  if (speed >= 2) {
     if ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
         av1_internal_image_edge(cpi)) {
       sf->use_square_partition_only = !frame_is_boosted(cpi);
@@ -160,8 +165,6 @@
     sf->tx_size_search_breakout = 1;
     sf->partition_search_breakout_rate_thr = 80;
     sf->tx_type_search.prune_mode = PRUNE_ONE;
-    sf->tx_type_search.fast_intra_tx_type_search = 1;
-    sf->tx_type_search.fast_inter_tx_type_search = 1;
     // Use transform domain distortion.
     // Note var-tx expt always uses pixel domain distortion.
     sf->use_transform_domain_distortion = 1;
@@ -171,7 +174,7 @@
 #endif  // CONFIG_EXT_INTER
   }
 
-  if (speed >= 2) {
+  if (speed >= 3) {
     sf->tx_size_search_method =
         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
     sf->mode_search_skip_flags =
@@ -190,7 +193,7 @@
 #endif
   }
 
-  if (speed >= 3) {
+  if (speed >= 4) {
     sf->use_square_partition_only = !frame_is_intra_only(cm);
     sf->tx_size_search_method =
         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
@@ -208,7 +211,7 @@
     sf->adaptive_interp_filter_search = 1;
   }
 
-  if (speed >= 4) {
+  if (speed >= 5) {
     sf->use_square_partition_only = 1;
     sf->tx_size_search_method = USE_LARGESTALL;
     sf->mv.search_method = BIGDIA;
@@ -223,7 +226,7 @@
     sf->partition_search_breakout_rate_thr = 300;
   }
 
-  if (speed >= 5) {
+  if (speed >= 6) {
     int i;
     sf->optimize_coefficients = 0;
     sf->mv.search_method = HEX;
diff --git a/test/avg_test.cc b/test/avg_test.cc
index cf6f89c..b7a707d 100644
--- a/test/avg_test.cc
+++ b/test/avg_test.cc
@@ -54,14 +54,14 @@
   }
 
   // Sum Pixels
-  unsigned int ReferenceAverage8x8(const uint8_t *source, int pitch) {
+  static unsigned int ReferenceAverage8x8(const uint8_t *source, int pitch) {
     unsigned int average = 0;
     for (int h = 0; h < 8; ++h)
       for (int w = 0; w < 8; ++w) average += source[h * pitch + w];
     return ((average + 32) >> 6);
   }
 
-  unsigned int ReferenceAverage4x4(const uint8_t *source, int pitch) {
+  static unsigned int ReferenceAverage4x4(const uint8_t *source, int pitch) {
     unsigned int average = 0;
     for (int h = 0; h < 4; ++h)
       for (int w = 0; w < 4; ++w) average += source[h * pitch + w];
@@ -97,11 +97,12 @@
 
  protected:
   void CheckAverages() {
+    const int block_size = GET_PARAM(3);
     unsigned int expected = 0;
-    if (GET_PARAM(3) == 8) {
+    if (block_size == 8) {
       expected =
           ReferenceAverage8x8(source_data_ + GET_PARAM(2), source_stride_);
-    } else if (GET_PARAM(3) == 4) {
+    } else if (block_size == 4) {
       expected =
           ReferenceAverage4x4(source_data_ + GET_PARAM(2), source_stride_);
     }
diff --git a/test/boolcoder_test.cc b/test/boolcoder_test.cc
index c0e6d48..e2ebdda 100644
--- a/test/boolcoder_test.cc
+++ b/test/boolcoder_test.cc
@@ -135,7 +135,7 @@
     }
     const uint32_t expected = (uint32_t)(-kSymbols * log2(probability));
     // Last tell should be close to the expected value.
-    GTEST_ASSERT_LE(last_tell - expected, 20) << " last_tell: " << last_tell;
+    GTEST_ASSERT_LE(last_tell, expected + 20) << " last_tell: " << last_tell;
     // The average frac_diff error should be pretty small.
     GTEST_ASSERT_LE(frac_diff_total / kSymbols, FRAC_DIFF_TOTAL_ERROR)
         << " frac_diff_total: " << frac_diff_total;
diff --git a/test/reconintra_predictors_test.cc b/test/filterintra_predictors_test.cc
similarity index 92%
rename from test/reconintra_predictors_test.cc
rename to test/filterintra_predictors_test.cc
index a86a6a5..66605f6 100644
--- a/test/reconintra_predictors_test.cc
+++ b/test/filterintra_predictors_test.cc
@@ -57,9 +57,10 @@
 const int MaxTestNum = 100;
 #endif
 
-class AV1IntraPredOptimzTest : public ::testing::TestWithParam<PredParams> {
+class AV1FilterIntraPredOptimzTest
+    : public ::testing::TestWithParam<PredParams> {
  public:
-  virtual ~AV1IntraPredOptimzTest() {}
+  virtual ~AV1FilterIntraPredOptimzTest() {}
   virtual void SetUp() {
     PredFuncMode funcMode = GET_PARAM(0);
     predFuncRef_ = std::tr1::get<0>(funcMode);
@@ -149,10 +150,10 @@
 };
 
 #if CONFIG_AOM_HIGHBITDEPTH
-class AV1HbdIntraPredOptimzTest
+class AV1HbdFilterIntraPredOptimzTest
     : public ::testing::TestWithParam<HbdPredParams> {
  public:
-  virtual ~AV1HbdIntraPredOptimzTest() {}
+  virtual ~AV1HbdFilterIntraPredOptimzTest() {}
   virtual void SetUp() {
     HbdPredFuncMode funcMode = GET_PARAM(0);
     predFuncRef_ = std::tr1::get<0>(funcMode);
@@ -245,21 +246,21 @@
 };
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
-TEST_P(AV1IntraPredOptimzTest, BitExactCheck) { RunTest(); }
+TEST_P(AV1FilterIntraPredOptimzTest, BitExactCheck) { RunTest(); }
 
 #if PREDICTORS_SPEED_TEST
-TEST_P(AV1IntraPredOptimzTest, SpeedCheckC) { RunSpeedTestC(); }
+TEST_P(AV1FilterIntraPredOptimzTest, SpeedCheckC) { RunSpeedTestC(); }
 
-TEST_P(AV1IntraPredOptimzTest, SpeedCheckSSE) { RunSpeedTestSSE(); }
+TEST_P(AV1FilterIntraPredOptimzTest, SpeedCheckSSE) { RunSpeedTestSSE(); }
 #endif
 
 #if CONFIG_AOM_HIGHBITDEPTH
-TEST_P(AV1HbdIntraPredOptimzTest, BitExactCheck) { RunTest(); }
+TEST_P(AV1HbdFilterIntraPredOptimzTest, BitExactCheck) { RunTest(); }
 
 #if PREDICTORS_SPEED_TEST
-TEST_P(AV1HbdIntraPredOptimzTest, SpeedCheckC) { RunSpeedTestC(); }
+TEST_P(AV1HbdFilterIntraPredOptimzTest, SpeedCheckC) { RunSpeedTestC(); }
 
-TEST_P(AV1HbdIntraPredOptimzTest, SpeedCheckSSE) { RunSpeedTestSSE(); }
+TEST_P(AV1HbdFilterIntraPredOptimzTest, SpeedCheckSSE) { RunSpeedTestSSE(); }
 #endif  // PREDICTORS_SPEED_TEST
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
@@ -289,7 +290,7 @@
 const int kBlkSize[] = { 4, 8, 16, 32 };
 
 INSTANTIATE_TEST_CASE_P(
-    SSE4_1, AV1IntraPredOptimzTest,
+    SSE4_1, AV1FilterIntraPredOptimzTest,
     ::testing::Combine(::testing::ValuesIn(kPredFuncMdArray),
                        ::testing::ValuesIn(kBlkSize)));
 
@@ -320,7 +321,7 @@
 const int kBd[] = { 10, 12 };
 
 INSTANTIATE_TEST_CASE_P(
-    SSE4_1, AV1HbdIntraPredOptimzTest,
+    SSE4_1, AV1HbdFilterIntraPredOptimzTest,
     ::testing::Combine(::testing::ValuesIn(kHbdPredFuncMdArray),
                        ::testing::ValuesIn(kBlkSize),
                        ::testing::ValuesIn(kBd)));
diff --git a/test/test.mk b/test/test.mk
index 1146428..61ea1d0 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -162,8 +162,8 @@
 LIBAOM_TEST_SRCS-$(CONFIG_AV1_ENCODER) += av1_wedge_utils_test.cc
 endif
 
-ifeq ($(CONFIG_EXT_INTRA),yes)
-LIBAOM_TEST_SRCS-$(HAVE_SSE4_1) += reconintra_predictors_test.cc
+ifeq ($(CONFIG_FILTER_INTRA),yes)
+LIBAOM_TEST_SRCS-$(HAVE_SSE4_1) += filterintra_predictors_test.cc
 endif
 
 ifeq ($(CONFIG_MOTION_VAR),yes)