27c580107e613bb4503ff16877e455e62f07fd8d
[platform/upstream/libvpx.git] / vp9 / encoder / vp9_rdopt.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <assert.h>
12 #include <math.h>
13
14 #include "./vp9_rtcd.h"
15 #include "./vpx_dsp_rtcd.h"
16
17 #include "vpx_dsp/vpx_dsp_common.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h"
20 #include "vpx_ports/system_state.h"
21
22 #include "vp9/common/vp9_common.h"
23 #include "vp9/common/vp9_entropy.h"
24 #include "vp9/common/vp9_entropymode.h"
25 #include "vp9/common/vp9_idct.h"
26 #include "vp9/common/vp9_mvref_common.h"
27 #include "vp9/common/vp9_pred_common.h"
28 #include "vp9/common/vp9_quant_common.h"
29 #include "vp9/common/vp9_reconinter.h"
30 #include "vp9/common/vp9_reconintra.h"
31 #include "vp9/common/vp9_scan.h"
32 #include "vp9/common/vp9_seg_common.h"
33
34 #include "vp9/encoder/vp9_cost.h"
35 #include "vp9/encoder/vp9_encodemb.h"
36 #include "vp9/encoder/vp9_encodemv.h"
37 #include "vp9/encoder/vp9_encoder.h"
38 #include "vp9/encoder/vp9_mcomp.h"
39 #include "vp9/encoder/vp9_quantize.h"
40 #include "vp9/encoder/vp9_ratectrl.h"
41 #include "vp9/encoder/vp9_rd.h"
42 #include "vp9/encoder/vp9_rdopt.h"
43 #include "vp9/encoder/vp9_aq_variance.h"
44
45 #define LAST_FRAME_MODE_MASK \
46   ((1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME))
47 #define GOLDEN_FRAME_MODE_MASK \
48   ((1 << LAST_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME))
49 #define ALT_REF_MODE_MASK \
50   ((1 << LAST_FRAME) | (1 << GOLDEN_FRAME) | (1 << INTRA_FRAME))
51
52 #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | 0x01)
53
54 #define MIN_EARLY_TERM_INDEX 3
55 #define NEW_MV_DISCOUNT_FACTOR 8
56
57 typedef struct {
58   PREDICTION_MODE mode;
59   MV_REFERENCE_FRAME ref_frame[2];
60 } MODE_DEFINITION;
61
62 typedef struct {
63   MV_REFERENCE_FRAME ref_frame[2];
64 } REF_DEFINITION;
65
66 struct rdcost_block_args {
67   const VP9_COMP *cpi;
68   MACROBLOCK *x;
69   ENTROPY_CONTEXT t_above[16];
70   ENTROPY_CONTEXT t_left[16];
71   int this_rate;
72   int64_t this_dist;
73   int64_t this_sse;
74   int64_t this_rd;
75   int64_t best_rd;
76   int exit_early;
77   int use_fast_coef_costing;
78   const scan_order *so;
79   uint8_t skippable;
80   struct buf_2d *this_recon;
81 };
82
83 #define LAST_NEW_MV_INDEX 6
84 static const MODE_DEFINITION vp9_mode_order[MAX_MODES] = {
85   { NEARESTMV, { LAST_FRAME, NONE } },
86   { NEARESTMV, { ALTREF_FRAME, NONE } },
87   { NEARESTMV, { GOLDEN_FRAME, NONE } },
88
89   { DC_PRED, { INTRA_FRAME, NONE } },
90
91   { NEWMV, { LAST_FRAME, NONE } },
92   { NEWMV, { ALTREF_FRAME, NONE } },
93   { NEWMV, { GOLDEN_FRAME, NONE } },
94
95   { NEARMV, { LAST_FRAME, NONE } },
96   { NEARMV, { ALTREF_FRAME, NONE } },
97   { NEARMV, { GOLDEN_FRAME, NONE } },
98
99   { ZEROMV, { LAST_FRAME, NONE } },
100   { ZEROMV, { GOLDEN_FRAME, NONE } },
101   { ZEROMV, { ALTREF_FRAME, NONE } },
102
103   { NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
104   { NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
105
106   { TM_PRED, { INTRA_FRAME, NONE } },
107
108   { NEARMV, { LAST_FRAME, ALTREF_FRAME } },
109   { NEWMV, { LAST_FRAME, ALTREF_FRAME } },
110   { NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
111   { NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
112
113   { ZEROMV, { LAST_FRAME, ALTREF_FRAME } },
114   { ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } },
115
116   { H_PRED, { INTRA_FRAME, NONE } },
117   { V_PRED, { INTRA_FRAME, NONE } },
118   { D135_PRED, { INTRA_FRAME, NONE } },
119   { D207_PRED, { INTRA_FRAME, NONE } },
120   { D153_PRED, { INTRA_FRAME, NONE } },
121   { D63_PRED, { INTRA_FRAME, NONE } },
122   { D117_PRED, { INTRA_FRAME, NONE } },
123   { D45_PRED, { INTRA_FRAME, NONE } },
124 };
125
126 static const REF_DEFINITION vp9_ref_order[MAX_REFS] = {
127   { { LAST_FRAME, NONE } },           { { GOLDEN_FRAME, NONE } },
128   { { ALTREF_FRAME, NONE } },         { { LAST_FRAME, ALTREF_FRAME } },
129   { { GOLDEN_FRAME, ALTREF_FRAME } }, { { INTRA_FRAME, NONE } },
130 };
131
132 static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, int m, int n,
133                            int min_plane, int max_plane) {
134   int i;
135
136   for (i = min_plane; i < max_plane; ++i) {
137     struct macroblock_plane *const p = &x->plane[i];
138     struct macroblockd_plane *const pd = &x->e_mbd.plane[i];
139
140     p->coeff = ctx->coeff_pbuf[i][m];
141     p->qcoeff = ctx->qcoeff_pbuf[i][m];
142     pd->dqcoeff = ctx->dqcoeff_pbuf[i][m];
143     p->eobs = ctx->eobs_pbuf[i][m];
144
145     ctx->coeff_pbuf[i][m] = ctx->coeff_pbuf[i][n];
146     ctx->qcoeff_pbuf[i][m] = ctx->qcoeff_pbuf[i][n];
147     ctx->dqcoeff_pbuf[i][m] = ctx->dqcoeff_pbuf[i][n];
148     ctx->eobs_pbuf[i][m] = ctx->eobs_pbuf[i][n];
149
150     ctx->coeff_pbuf[i][n] = p->coeff;
151     ctx->qcoeff_pbuf[i][n] = p->qcoeff;
152     ctx->dqcoeff_pbuf[i][n] = pd->dqcoeff;
153     ctx->eobs_pbuf[i][n] = p->eobs;
154   }
155 }
156
157 static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
158                             MACROBLOCKD *xd, int *out_rate_sum,
159                             int64_t *out_dist_sum, int *skip_txfm_sb,
160                             int64_t *skip_sse_sb) {
161   // Note our transform coeffs are 8 times an orthogonal transform.
162   // Hence quantizer step is also 8 times. To get effective quantizer
163   // we need to divide by 8 before sending to modeling function.
164   int i;
165   int64_t rate_sum = 0;
166   int64_t dist_sum = 0;
167   const int ref = xd->mi[0]->ref_frame[0];
168   unsigned int sse;
169   unsigned int var = 0;
170   int64_t total_sse = 0;
171   int skip_flag = 1;
172   const int shift = 6;
173   int64_t dist;
174   const int dequant_shift =
175 #if CONFIG_VP9_HIGHBITDEPTH
176       (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 :
177 #endif  // CONFIG_VP9_HIGHBITDEPTH
178                                                     3;
179   unsigned int qstep_vec[MAX_MB_PLANE];
180   unsigned int nlog2_vec[MAX_MB_PLANE];
181   unsigned int sum_sse_vec[MAX_MB_PLANE];
182   int any_zero_sum_sse = 0;
183
184   x->pred_sse[ref] = 0;
185
186   for (i = 0; i < MAX_MB_PLANE; ++i) {
187     struct macroblock_plane *const p = &x->plane[i];
188     struct macroblockd_plane *const pd = &xd->plane[i];
189     const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
190     const TX_SIZE max_tx_size = max_txsize_lookup[bs];
191     const BLOCK_SIZE unit_size = txsize_to_bsize[max_tx_size];
192     const int64_t dc_thr = p->quant_thred[0] >> shift;
193     const int64_t ac_thr = p->quant_thred[1] >> shift;
194     unsigned int sum_sse = 0;
195     // The low thresholds are used to measure if the prediction errors are
196     // low enough so that we can skip the mode search.
197     const int64_t low_dc_thr = VPXMIN(50, dc_thr >> 2);
198     const int64_t low_ac_thr = VPXMIN(80, ac_thr >> 2);
199     int bw = 1 << (b_width_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
200     int bh = 1 << (b_height_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
201     int idx, idy;
202     int lw = b_width_log2_lookup[unit_size] + 2;
203     int lh = b_height_log2_lookup[unit_size] + 2;
204
205     for (idy = 0; idy < bh; ++idy) {
206       for (idx = 0; idx < bw; ++idx) {
207         uint8_t *src = p->src.buf + (idy * p->src.stride << lh) + (idx << lw);
208         uint8_t *dst = pd->dst.buf + (idy * pd->dst.stride << lh) + (idx << lh);
209         int block_idx = (idy << 1) + idx;
210         int low_err_skip = 0;
211
212         var = cpi->fn_ptr[unit_size].vf(src, p->src.stride, dst, pd->dst.stride,
213                                         &sse);
214         x->bsse[(i << 2) + block_idx] = sse;
215         sum_sse += sse;
216
217         x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_NONE;
218         if (!x->select_tx_size) {
219           // Check if all ac coefficients can be quantized to zero.
220           if (var < ac_thr || var == 0) {
221             x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_AC_ONLY;
222
223             // Check if dc coefficient can be quantized to zero.
224             if (sse - var < dc_thr || sse == var) {
225               x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_AC_DC;
226
227               if (!sse || (var < low_ac_thr && sse - var < low_dc_thr))
228                 low_err_skip = 1;
229             }
230           }
231         }
232
233         if (skip_flag && !low_err_skip) skip_flag = 0;
234
235         if (i == 0) x->pred_sse[ref] += sse;
236       }
237     }
238
239     total_sse += sum_sse;
240     sum_sse_vec[i] = sum_sse;
241     any_zero_sum_sse = any_zero_sum_sse || (sum_sse == 0);
242     qstep_vec[i] = pd->dequant[1] >> dequant_shift;
243     nlog2_vec[i] = num_pels_log2_lookup[bs];
244   }
245
246   // Fast approximate the modelling function.
247   if (cpi->sf.simple_model_rd_from_var) {
248     for (i = 0; i < MAX_MB_PLANE; ++i) {
249       int64_t rate;
250       const int64_t square_error = sum_sse_vec[i];
251       int quantizer = qstep_vec[i];
252
253       if (quantizer < 120)
254         rate = (square_error * (280 - quantizer)) >> (16 - VP9_PROB_COST_SHIFT);
255       else
256         rate = 0;
257       dist = (square_error * quantizer) >> 8;
258       rate_sum += rate;
259       dist_sum += dist;
260     }
261   } else {
262     if (any_zero_sum_sse) {
263       for (i = 0; i < MAX_MB_PLANE; ++i) {
264         int rate;
265         vp9_model_rd_from_var_lapndz(sum_sse_vec[i], nlog2_vec[i], qstep_vec[i],
266                                      &rate, &dist);
267         rate_sum += rate;
268         dist_sum += dist;
269       }
270     } else {
271       vp9_model_rd_from_var_lapndz_vec(sum_sse_vec, nlog2_vec, qstep_vec,
272                                        &rate_sum, &dist_sum);
273     }
274   }
275
276   *skip_txfm_sb = skip_flag;
277   *skip_sse_sb = total_sse << VP9_DIST_SCALE_LOG2;
278   *out_rate_sum = (int)rate_sum;
279   *out_dist_sum = dist_sum << VP9_DIST_SCALE_LOG2;
280 }
281
282 #if CONFIG_VP9_HIGHBITDEPTH
283 int64_t vp9_highbd_block_error_c(const tran_low_t *coeff,
284                                  const tran_low_t *dqcoeff, intptr_t block_size,
285                                  int64_t *ssz, int bd) {
286   int i;
287   int64_t error = 0, sqcoeff = 0;
288   int shift = 2 * (bd - 8);
289   int rounding = shift > 0 ? 1 << (shift - 1) : 0;
290
291   for (i = 0; i < block_size; i++) {
292     const int64_t diff = coeff[i] - dqcoeff[i];
293     error += diff * diff;
294     sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i];
295   }
296   assert(error >= 0 && sqcoeff >= 0);
297   error = (error + rounding) >> shift;
298   sqcoeff = (sqcoeff + rounding) >> shift;
299
300   *ssz = sqcoeff;
301   return error;
302 }
303
304 static int64_t vp9_highbd_block_error_dispatch(const tran_low_t *coeff,
305                                                const tran_low_t *dqcoeff,
306                                                intptr_t block_size,
307                                                int64_t *ssz, int bd) {
308   if (bd == 8) {
309     return vp9_block_error(coeff, dqcoeff, block_size, ssz);
310   } else {
311     return vp9_highbd_block_error(coeff, dqcoeff, block_size, ssz, bd);
312   }
313 }
314 #endif  // CONFIG_VP9_HIGHBITDEPTH
315
316 int64_t vp9_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
317                           intptr_t block_size, int64_t *ssz) {
318   int i;
319   int64_t error = 0, sqcoeff = 0;
320
321   for (i = 0; i < block_size; i++) {
322     const int diff = coeff[i] - dqcoeff[i];
323     error += diff * diff;
324     sqcoeff += coeff[i] * coeff[i];
325   }
326
327   *ssz = sqcoeff;
328   return error;
329 }
330
331 int64_t vp9_block_error_fp_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
332                              int block_size) {
333   int i;
334   int64_t error = 0;
335
336   for (i = 0; i < block_size; i++) {
337     const int diff = coeff[i] - dqcoeff[i];
338     error += diff * diff;
339   }
340
341   return error;
342 }
343
344 /* The trailing '0' is a terminator which is used inside cost_coeffs() to
345  * decide whether to include cost of a trailing EOB node or not (i.e. we
346  * can skip this if the last coefficient in this transform block, e.g. the
347  * 16th coefficient in a 4x4 block or the 64th coefficient in a 8x8 block,
348  * were non-zero). */
349 static const int16_t band_counts[TX_SIZES][8] = {
350   { 1, 2, 3, 4, 3, 16 - 13, 0 },
351   { 1, 2, 3, 4, 11, 64 - 21, 0 },
352   { 1, 2, 3, 4, 11, 256 - 21, 0 },
353   { 1, 2, 3, 4, 11, 1024 - 21, 0 },
354 };
355 static int cost_coeffs(MACROBLOCK *x, int plane, int block, TX_SIZE tx_size,
356                        int pt, const int16_t *scan, const int16_t *nb,
357                        int use_fast_coef_costing) {
358   MACROBLOCKD *const xd = &x->e_mbd;
359   MODE_INFO *mi = xd->mi[0];
360   const struct macroblock_plane *p = &x->plane[plane];
361   const PLANE_TYPE type = get_plane_type(plane);
362   const int16_t *band_count = &band_counts[tx_size][1];
363   const int eob = p->eobs[block];
364   const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
365   unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
366       x->token_costs[tx_size][type][is_inter_block(mi)];
367   uint8_t token_cache[32 * 32];
368   int cost;
369 #if CONFIG_VP9_HIGHBITDEPTH
370   const uint16_t *cat6_high_cost = vp9_get_high_cost_table(xd->bd);
371 #else
372   const uint16_t *cat6_high_cost = vp9_get_high_cost_table(8);
373 #endif
374
375   // Check for consistency of tx_size with mode info
376   assert(type == PLANE_TYPE_Y
377              ? mi->tx_size == tx_size
378              : get_uv_tx_size(mi, &xd->plane[plane]) == tx_size);
379
380   if (eob == 0) {
381     // single eob token
382     cost = token_costs[0][0][pt][EOB_TOKEN];
383   } else {
384     if (use_fast_coef_costing) {
385       int band_left = *band_count++;
386       int c;
387
388       // dc token
389       int v = qcoeff[0];
390       int16_t prev_t;
391       cost = vp9_get_token_cost(v, &prev_t, cat6_high_cost);
392       cost += (*token_costs)[0][pt][prev_t];
393
394       token_cache[0] = vp9_pt_energy_class[prev_t];
395       ++token_costs;
396
397       // ac tokens
398       for (c = 1; c < eob; c++) {
399         const int rc = scan[c];
400         int16_t t;
401
402         v = qcoeff[rc];
403         cost += vp9_get_token_cost(v, &t, cat6_high_cost);
404         cost += (*token_costs)[!prev_t][!prev_t][t];
405         prev_t = t;
406         if (!--band_left) {
407           band_left = *band_count++;
408           ++token_costs;
409         }
410       }
411
412       // eob token
413       if (band_left) cost += (*token_costs)[0][!prev_t][EOB_TOKEN];
414
415     } else {  // !use_fast_coef_costing
416       int band_left = *band_count++;
417       int c;
418
419       // dc token
420       int v = qcoeff[0];
421       int16_t tok;
422       unsigned int(*tok_cost_ptr)[COEFF_CONTEXTS][ENTROPY_TOKENS];
423       cost = vp9_get_token_cost(v, &tok, cat6_high_cost);
424       cost += (*token_costs)[0][pt][tok];
425
426       token_cache[0] = vp9_pt_energy_class[tok];
427       ++token_costs;
428
429       tok_cost_ptr = &((*token_costs)[!tok]);
430
431       // ac tokens
432       for (c = 1; c < eob; c++) {
433         const int rc = scan[c];
434
435         v = qcoeff[rc];
436         cost += vp9_get_token_cost(v, &tok, cat6_high_cost);
437         pt = get_coef_context(nb, token_cache, c);
438         cost += (*tok_cost_ptr)[pt][tok];
439         token_cache[rc] = vp9_pt_energy_class[tok];
440         if (!--band_left) {
441           band_left = *band_count++;
442           ++token_costs;
443         }
444         tok_cost_ptr = &((*token_costs)[!tok]);
445       }
446
447       // eob token
448       if (band_left) {
449         pt = get_coef_context(nb, token_cache, c);
450         cost += (*token_costs)[0][pt][EOB_TOKEN];
451       }
452     }
453   }
454
455   return cost;
456 }
457
458 static INLINE int num_4x4_to_edge(int plane_4x4_dim, int mb_to_edge_dim,
459                                   int subsampling_dim, int blk_dim) {
460   return plane_4x4_dim + (mb_to_edge_dim >> (5 + subsampling_dim)) - blk_dim;
461 }
462
463 // Copy all visible 4x4s in the transform block.
464 static void copy_block_visible(const MACROBLOCKD *xd,
465                                const struct macroblockd_plane *const pd,
466                                const uint8_t *src, const int src_stride,
467                                uint8_t *dst, const int dst_stride, int blk_row,
468                                int blk_col, const BLOCK_SIZE plane_bsize,
469                                const BLOCK_SIZE tx_bsize) {
470   const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
471   const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
472   const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
473   const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
474   int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
475                                             pd->subsampling_x, blk_col);
476   int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
477                                              pd->subsampling_y, blk_row);
478   const int is_highbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
479   if (tx_bsize == BLOCK_4X4 ||
480       (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
481     const int w = tx_4x4_w << 2;
482     const int h = tx_4x4_h << 2;
483 #if CONFIG_VP9_HIGHBITDEPTH
484     if (is_highbd) {
485       vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src), src_stride,
486                                CONVERT_TO_SHORTPTR(dst), dst_stride, NULL, 0, 0,
487                                0, 0, w, h, xd->bd);
488     } else {
489 #endif
490       vpx_convolve_copy(src, src_stride, dst, dst_stride, NULL, 0, 0, 0, 0, w,
491                         h);
492 #if CONFIG_VP9_HIGHBITDEPTH
493     }
494 #endif
495   } else {
496     int r, c;
497     int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
498     int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
499     // if we are in the unrestricted motion border.
500     for (r = 0; r < max_r; ++r) {
501       // Skip visiting the sub blocks that are wholly within the UMV.
502       for (c = 0; c < max_c; ++c) {
503         const uint8_t *src_ptr = src + r * src_stride * 4 + c * 4;
504         uint8_t *dst_ptr = dst + r * dst_stride * 4 + c * 4;
505 #if CONFIG_VP9_HIGHBITDEPTH
506         if (is_highbd) {
507           vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
508                                    CONVERT_TO_SHORTPTR(dst_ptr), dst_stride,
509                                    NULL, 0, 0, 0, 0, 4, 4, xd->bd);
510         } else {
511 #endif
512           vpx_convolve_copy(src_ptr, src_stride, dst_ptr, dst_stride, NULL, 0,
513                             0, 0, 0, 4, 4);
514 #if CONFIG_VP9_HIGHBITDEPTH
515         }
516 #endif
517       }
518     }
519   }
520   (void)is_highbd;
521 }
522
523 // Compute the pixel domain sum square error on all visible 4x4s in the
524 // transform block.
525 static unsigned pixel_sse(const VP9_COMP *const cpi, const MACROBLOCKD *xd,
526                           const struct macroblockd_plane *const pd,
527                           const uint8_t *src, const int src_stride,
528                           const uint8_t *dst, const int dst_stride, int blk_row,
529                           int blk_col, const BLOCK_SIZE plane_bsize,
530                           const BLOCK_SIZE tx_bsize) {
531   unsigned int sse = 0;
532   const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
533   const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
534   const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
535   const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
536   int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
537                                             pd->subsampling_x, blk_col);
538   int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
539                                              pd->subsampling_y, blk_row);
540   if (tx_bsize == BLOCK_4X4 ||
541       (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
542     cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse);
543   } else {
544     const vpx_variance_fn_t vf_4x4 = cpi->fn_ptr[BLOCK_4X4].vf;
545     int r, c;
546     unsigned this_sse = 0;
547     int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
548     int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
549     sse = 0;
550     // if we are in the unrestricted motion border.
551     for (r = 0; r < max_r; ++r) {
552       // Skip visiting the sub blocks that are wholly within the UMV.
553       for (c = 0; c < max_c; ++c) {
554         vf_4x4(src + r * src_stride * 4 + c * 4, src_stride,
555                dst + r * dst_stride * 4 + c * 4, dst_stride, &this_sse);
556         sse += this_sse;
557       }
558     }
559   }
560   return sse;
561 }
562
563 // Compute the squares sum squares on all visible 4x4s in the transform block.
564 static int64_t sum_squares_visible(const MACROBLOCKD *xd,
565                                    const struct macroblockd_plane *const pd,
566                                    const int16_t *diff, const int diff_stride,
567                                    int blk_row, int blk_col,
568                                    const BLOCK_SIZE plane_bsize,
569                                    const BLOCK_SIZE tx_bsize) {
570   int64_t sse;
571   const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
572   const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
573   const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
574   const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
575   int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
576                                             pd->subsampling_x, blk_col);
577   int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
578                                              pd->subsampling_y, blk_row);
579   if (tx_bsize == BLOCK_4X4 ||
580       (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
581     assert(tx_4x4_w == tx_4x4_h);
582     sse = (int64_t)vpx_sum_squares_2d_i16(diff, diff_stride, tx_4x4_w << 2);
583   } else {
584     int r, c;
585     int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
586     int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
587     sse = 0;
588     // if we are in the unrestricted motion border.
589     for (r = 0; r < max_r; ++r) {
590       // Skip visiting the sub blocks that are wholly within the UMV.
591       for (c = 0; c < max_c; ++c) {
592         sse += (int64_t)vpx_sum_squares_2d_i16(
593             diff + r * diff_stride * 4 + c * 4, diff_stride, 4);
594       }
595     }
596   }
597   return sse;
598 }
599
600 static void dist_block(const VP9_COMP *cpi, MACROBLOCK *x, int plane,
601                        BLOCK_SIZE plane_bsize, int block, int blk_row,
602                        int blk_col, TX_SIZE tx_size, int64_t *out_dist,
603                        int64_t *out_sse, struct buf_2d *out_recon) {
604   MACROBLOCKD *const xd = &x->e_mbd;
605   const struct macroblock_plane *const p = &x->plane[plane];
606   const struct macroblockd_plane *const pd = &xd->plane[plane];
607   const int eob = p->eobs[block];
608
609   if (!out_recon && x->block_tx_domain && eob) {
610     const int ss_txfrm_size = tx_size << 1;
611     int64_t this_sse;
612     const int shift = tx_size == TX_32X32 ? 0 : 2;
613     const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
614     const tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
615 #if CONFIG_VP9_HIGHBITDEPTH
616     const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
617     *out_dist = vp9_highbd_block_error_dispatch(
618                     coeff, dqcoeff, 16 << ss_txfrm_size, &this_sse, bd) >>
619                 shift;
620 #else
621     *out_dist =
622         vp9_block_error(coeff, dqcoeff, 16 << ss_txfrm_size, &this_sse) >>
623         shift;
624 #endif  // CONFIG_VP9_HIGHBITDEPTH
625     *out_sse = this_sse >> shift;
626
627     if (x->skip_encode && !is_inter_block(xd->mi[0])) {
628       // TODO(jingning): tune the model to better capture the distortion.
629       const int64_t p =
630           (pd->dequant[1] * pd->dequant[1] * (1 << ss_txfrm_size)) >>
631 #if CONFIG_VP9_HIGHBITDEPTH
632           (shift + 2 + (bd - 8) * 2);
633 #else
634           (shift + 2);
635 #endif  // CONFIG_VP9_HIGHBITDEPTH
636       *out_dist += (p >> 4);
637       *out_sse += p;
638     }
639   } else {
640     const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
641     const int bs = 4 * num_4x4_blocks_wide_lookup[tx_bsize];
642     const int src_stride = p->src.stride;
643     const int dst_stride = pd->dst.stride;
644     const int src_idx = 4 * (blk_row * src_stride + blk_col);
645     const int dst_idx = 4 * (blk_row * dst_stride + blk_col);
646     const uint8_t *src = &p->src.buf[src_idx];
647     const uint8_t *dst = &pd->dst.buf[dst_idx];
648     uint8_t *out_recon_ptr = 0;
649
650     const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
651     unsigned int tmp;
652
653     tmp = pixel_sse(cpi, xd, pd, src, src_stride, dst, dst_stride, blk_row,
654                     blk_col, plane_bsize, tx_bsize);
655     *out_sse = (int64_t)tmp * 16;
656     if (out_recon) {
657       const int out_recon_idx = 4 * (blk_row * out_recon->stride + blk_col);
658       out_recon_ptr = &out_recon->buf[out_recon_idx];
659       copy_block_visible(xd, pd, dst, dst_stride, out_recon_ptr,
660                          out_recon->stride, blk_row, blk_col, plane_bsize,
661                          tx_bsize);
662     }
663
664     if (eob) {
665 #if CONFIG_VP9_HIGHBITDEPTH
666       DECLARE_ALIGNED(16, uint16_t, recon16[1024]);
667       uint8_t *recon = (uint8_t *)recon16;
668 #else
669       DECLARE_ALIGNED(16, uint8_t, recon[1024]);
670 #endif  // CONFIG_VP9_HIGHBITDEPTH
671
672 #if CONFIG_VP9_HIGHBITDEPTH
673       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
674         vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(dst), dst_stride, recon16,
675                                  32, NULL, 0, 0, 0, 0, bs, bs, xd->bd);
676         if (xd->lossless) {
677           vp9_highbd_iwht4x4_add(dqcoeff, recon16, 32, eob, xd->bd);
678         } else {
679           switch (tx_size) {
680             case TX_4X4:
681               vp9_highbd_idct4x4_add(dqcoeff, recon16, 32, eob, xd->bd);
682               break;
683             case TX_8X8:
684               vp9_highbd_idct8x8_add(dqcoeff, recon16, 32, eob, xd->bd);
685               break;
686             case TX_16X16:
687               vp9_highbd_idct16x16_add(dqcoeff, recon16, 32, eob, xd->bd);
688               break;
689             default:
690               assert(tx_size == TX_32X32);
691               vp9_highbd_idct32x32_add(dqcoeff, recon16, 32, eob, xd->bd);
692               break;
693           }
694         }
695         recon = CONVERT_TO_BYTEPTR(recon16);
696       } else {
697 #endif  // CONFIG_VP9_HIGHBITDEPTH
698         vpx_convolve_copy(dst, dst_stride, recon, 32, NULL, 0, 0, 0, 0, bs, bs);
699         switch (tx_size) {
700           case TX_32X32: vp9_idct32x32_add(dqcoeff, recon, 32, eob); break;
701           case TX_16X16: vp9_idct16x16_add(dqcoeff, recon, 32, eob); break;
702           case TX_8X8: vp9_idct8x8_add(dqcoeff, recon, 32, eob); break;
703           default:
704             assert(tx_size == TX_4X4);
705             // this is like vp9_short_idct4x4 but has a special case around
706             // eob<=1, which is significant (not just an optimization) for
707             // the lossless case.
708             x->inv_txfm_add(dqcoeff, recon, 32, eob);
709             break;
710         }
711 #if CONFIG_VP9_HIGHBITDEPTH
712       }
713 #endif  // CONFIG_VP9_HIGHBITDEPTH
714
715       tmp = pixel_sse(cpi, xd, pd, src, src_stride, recon, 32, blk_row, blk_col,
716                       plane_bsize, tx_bsize);
717       if (out_recon) {
718         copy_block_visible(xd, pd, recon, 32, out_recon_ptr, out_recon->stride,
719                            blk_row, blk_col, plane_bsize, tx_bsize);
720       }
721     }
722
723     *out_dist = (int64_t)tmp * 16;
724   }
725 }
726
727 static int rate_block(int plane, int block, TX_SIZE tx_size, int coeff_ctx,
728                       struct rdcost_block_args *args) {
729   return cost_coeffs(args->x, plane, block, tx_size, coeff_ctx, args->so->scan,
730                      args->so->neighbors, args->use_fast_coef_costing);
731 }
732
733 static void block_rd_txfm(int plane, int block, int blk_row, int blk_col,
734                           BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
735   struct rdcost_block_args *args = arg;
736   MACROBLOCK *const x = args->x;
737   MACROBLOCKD *const xd = &x->e_mbd;
738   MODE_INFO *const mi = xd->mi[0];
739   int64_t rd1, rd2, rd;
740   int rate;
741   int64_t dist;
742   int64_t sse;
743   const int coeff_ctx =
744       combine_entropy_contexts(args->t_left[blk_row], args->t_above[blk_col]);
745   struct buf_2d *recon = args->this_recon;
746   const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
747   const struct macroblockd_plane *const pd = &xd->plane[plane];
748   const int dst_stride = pd->dst.stride;
749   const uint8_t *dst = &pd->dst.buf[4 * (blk_row * dst_stride + blk_col)];
750
751   if (args->exit_early) return;
752
753   if (!is_inter_block(mi)) {
754     struct encode_b_args intra_arg = { x, x->block_qcoeff_opt, args->t_above,
755                                        args->t_left, &mi->skip };
756     vp9_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
757                            &intra_arg);
758     if (recon) {
759       uint8_t *rec_ptr = &recon->buf[4 * (blk_row * recon->stride + blk_col)];
760       copy_block_visible(xd, pd, dst, dst_stride, rec_ptr, recon->stride,
761                          blk_row, blk_col, plane_bsize, tx_bsize);
762     }
763     if (x->block_tx_domain) {
764       dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
765                  tx_size, &dist, &sse, /*recon =*/0);
766     } else {
767       const struct macroblock_plane *const p = &x->plane[plane];
768       const int src_stride = p->src.stride;
769       const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
770       const uint8_t *src = &p->src.buf[4 * (blk_row * src_stride + blk_col)];
771       const int16_t *diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
772       unsigned int tmp;
773       sse = sum_squares_visible(xd, pd, diff, diff_stride, blk_row, blk_col,
774                                 plane_bsize, tx_bsize);
775 #if CONFIG_VP9_HIGHBITDEPTH
776       if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && (xd->bd > 8))
777         sse = ROUND64_POWER_OF_TWO(sse, (xd->bd - 8) * 2);
778 #endif  // CONFIG_VP9_HIGHBITDEPTH
779       sse = sse * 16;
780       tmp = pixel_sse(args->cpi, xd, pd, src, src_stride, dst, dst_stride,
781                       blk_row, blk_col, plane_bsize, tx_bsize);
782       dist = (int64_t)tmp * 16;
783     }
784   } else {
785     int skip_txfm_flag = SKIP_TXFM_NONE;
786     if (max_txsize_lookup[plane_bsize] == tx_size)
787       skip_txfm_flag = x->skip_txfm[(plane << 2) + (block >> (tx_size << 1))];
788
789     if (skip_txfm_flag == SKIP_TXFM_NONE ||
790         (recon && skip_txfm_flag == SKIP_TXFM_AC_ONLY)) {
791       // full forward transform and quantization
792       vp9_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, tx_size);
793       if (x->block_qcoeff_opt)
794         vp9_optimize_b(x, plane, block, tx_size, coeff_ctx);
795       dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
796                  tx_size, &dist, &sse, recon);
797     } else if (skip_txfm_flag == SKIP_TXFM_AC_ONLY) {
798       // compute DC coefficient
799       tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block);
800       tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block);
801       vp9_xform_quant_dc(x, plane, block, blk_row, blk_col, plane_bsize,
802                          tx_size);
803       sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
804       dist = sse;
805       if (x->plane[plane].eobs[block]) {
806         const int64_t orig_sse = (int64_t)coeff[0] * coeff[0];
807         const int64_t resd_sse = coeff[0] - dqcoeff[0];
808         int64_t dc_correct = orig_sse - resd_sse * resd_sse;
809 #if CONFIG_VP9_HIGHBITDEPTH
810         dc_correct >>= ((xd->bd - 8) * 2);
811 #endif
812         if (tx_size != TX_32X32) dc_correct >>= 2;
813
814         dist = VPXMAX(0, sse - dc_correct);
815       }
816     } else {
817       // SKIP_TXFM_AC_DC
818       // skip forward transform. Because this is handled here, the quantization
819       // does not need to do it.
820       x->plane[plane].eobs[block] = 0;
821       sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
822       dist = sse;
823       if (recon) {
824         uint8_t *rec_ptr = &recon->buf[4 * (blk_row * recon->stride + blk_col)];
825         copy_block_visible(xd, pd, dst, dst_stride, rec_ptr, recon->stride,
826                            blk_row, blk_col, plane_bsize, tx_bsize);
827       }
828     }
829   }
830
831   rd = RDCOST(x->rdmult, x->rddiv, 0, dist);
832   if (args->this_rd + rd > args->best_rd) {
833     args->exit_early = 1;
834     return;
835   }
836
837   rate = rate_block(plane, block, tx_size, coeff_ctx, args);
838   args->t_above[blk_col] = (x->plane[plane].eobs[block] > 0) ? 1 : 0;
839   args->t_left[blk_row] = (x->plane[plane].eobs[block] > 0) ? 1 : 0;
840   rd1 = RDCOST(x->rdmult, x->rddiv, rate, dist);
841   rd2 = RDCOST(x->rdmult, x->rddiv, 0, sse);
842
843   // TODO(jingning): temporarily enabled only for luma component
844   rd = VPXMIN(rd1, rd2);
845   if (plane == 0) {
846     x->zcoeff_blk[tx_size][block] =
847         !x->plane[plane].eobs[block] ||
848         (x->sharpness == 0 && rd1 > rd2 && !xd->lossless);
849     x->sum_y_eobs[tx_size] += x->plane[plane].eobs[block];
850   }
851
852   args->this_rate += rate;
853   args->this_dist += dist;
854   args->this_sse += sse;
855   args->this_rd += rd;
856
857   if (args->this_rd > args->best_rd) {
858     args->exit_early = 1;
859     return;
860   }
861
862   args->skippable &= !x->plane[plane].eobs[block];
863 }
864
865 static void txfm_rd_in_plane(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
866                              int64_t *distortion, int *skippable, int64_t *sse,
867                              int64_t ref_best_rd, int plane, BLOCK_SIZE bsize,
868                              TX_SIZE tx_size, int use_fast_coef_costing,
869                              struct buf_2d *recon) {
870   MACROBLOCKD *const xd = &x->e_mbd;
871   const struct macroblockd_plane *const pd = &xd->plane[plane];
872   struct rdcost_block_args args;
873   vp9_zero(args);
874   args.cpi = cpi;
875   args.x = x;
876   args.best_rd = ref_best_rd;
877   args.use_fast_coef_costing = use_fast_coef_costing;
878   args.skippable = 1;
879   args.this_recon = recon;
880
881   if (plane == 0) xd->mi[0]->tx_size = tx_size;
882
883   vp9_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left);
884
885   args.so = get_scan(xd, tx_size, get_plane_type(plane), 0);
886
887   vp9_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm,
888                                          &args);
889   if (args.exit_early) {
890     *rate = INT_MAX;
891     *distortion = INT64_MAX;
892     *sse = INT64_MAX;
893     *skippable = 0;
894   } else {
895     *distortion = args.this_dist;
896     *rate = args.this_rate;
897     *sse = args.this_sse;
898     *skippable = args.skippable;
899   }
900 }
901
902 static void choose_largest_tx_size(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
903                                    int64_t *distortion, int *skip, int64_t *sse,
904                                    int64_t ref_best_rd, BLOCK_SIZE bs,
905                                    struct buf_2d *recon) {
906   const TX_SIZE max_tx_size = max_txsize_lookup[bs];
907   VP9_COMMON *const cm = &cpi->common;
908   const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
909   MACROBLOCKD *const xd = &x->e_mbd;
910   MODE_INFO *const mi = xd->mi[0];
911
912   mi->tx_size = VPXMIN(max_tx_size, largest_tx_size);
913
914   txfm_rd_in_plane(cpi, x, rate, distortion, skip, sse, ref_best_rd, 0, bs,
915                    mi->tx_size, cpi->sf.use_fast_coef_costing, recon);
916 }
917
918 static void choose_tx_size_from_rd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
919                                    int64_t *distortion, int *skip,
920                                    int64_t *psse, int64_t ref_best_rd,
921                                    BLOCK_SIZE bs, struct buf_2d *recon) {
922   const TX_SIZE max_tx_size = max_txsize_lookup[bs];
923   VP9_COMMON *const cm = &cpi->common;
924   MACROBLOCKD *const xd = &x->e_mbd;
925   MODE_INFO *const mi = xd->mi[0];
926   vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
927   int r[TX_SIZES][2], s[TX_SIZES];
928   int64_t d[TX_SIZES], sse[TX_SIZES];
929   int64_t rd[TX_SIZES][2] = { { INT64_MAX, INT64_MAX },
930                               { INT64_MAX, INT64_MAX },
931                               { INT64_MAX, INT64_MAX },
932                               { INT64_MAX, INT64_MAX } };
933   int n;
934   int s0, s1;
935   int64_t best_rd = ref_best_rd;
936   TX_SIZE best_tx = max_tx_size;
937   int start_tx, end_tx;
938   const int tx_size_ctx = get_tx_size_context(xd);
939 #if CONFIG_VP9_HIGHBITDEPTH
940   DECLARE_ALIGNED(16, uint16_t, recon_buf16[TX_SIZES][64 * 64]);
941   uint8_t *recon_buf[TX_SIZES];
942   for (n = 0; n < TX_SIZES; ++n) {
943     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
944       recon_buf[n] = CONVERT_TO_BYTEPTR(recon_buf16[n]);
945     } else {
946       recon_buf[n] = (uint8_t *)recon_buf16[n];
947     }
948   }
949 #else
950   DECLARE_ALIGNED(16, uint8_t, recon_buf[TX_SIZES][64 * 64]);
951 #endif  // CONFIG_VP9_HIGHBITDEPTH
952
953   assert(skip_prob > 0);
954   s0 = vp9_cost_bit(skip_prob, 0);
955   s1 = vp9_cost_bit(skip_prob, 1);
956
957   if (cm->tx_mode == TX_MODE_SELECT) {
958     start_tx = max_tx_size;
959     end_tx = VPXMAX(start_tx - cpi->sf.tx_size_search_depth, 0);
960     if (bs > BLOCK_32X32) end_tx = VPXMIN(end_tx + 1, start_tx);
961   } else {
962     TX_SIZE chosen_tx_size =
963         VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[cm->tx_mode]);
964     start_tx = chosen_tx_size;
965     end_tx = chosen_tx_size;
966   }
967
968   for (n = start_tx; n >= end_tx; n--) {
969     const int r_tx_size = cpi->tx_size_cost[max_tx_size - 1][tx_size_ctx][n];
970     if (recon) {
971       struct buf_2d this_recon;
972       this_recon.buf = recon_buf[n];
973       this_recon.stride = recon->stride;
974       txfm_rd_in_plane(cpi, x, &r[n][0], &d[n], &s[n], &sse[n], best_rd, 0, bs,
975                        n, cpi->sf.use_fast_coef_costing, &this_recon);
976     } else {
977       txfm_rd_in_plane(cpi, x, &r[n][0], &d[n], &s[n], &sse[n], best_rd, 0, bs,
978                        n, cpi->sf.use_fast_coef_costing, 0);
979     }
980     r[n][1] = r[n][0];
981     if (r[n][0] < INT_MAX) {
982       r[n][1] += r_tx_size;
983     }
984     if (d[n] == INT64_MAX || r[n][0] == INT_MAX) {
985       rd[n][0] = rd[n][1] = INT64_MAX;
986     } else if (s[n]) {
987       if (is_inter_block(mi)) {
988         rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]);
989         r[n][1] -= r_tx_size;
990       } else {
991         rd[n][0] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]);
992         rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1 + r_tx_size, sse[n]);
993       }
994     } else {
995       rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]);
996       rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]);
997     }
998
999     if (is_inter_block(mi) && !xd->lossless && !s[n] && sse[n] != INT64_MAX) {
1000       rd[n][0] = VPXMIN(rd[n][0], RDCOST(x->rdmult, x->rddiv, s1, sse[n]));
1001       rd[n][1] = VPXMIN(rd[n][1], RDCOST(x->rdmult, x->rddiv, s1, sse[n]));
1002     }
1003
1004     // Early termination in transform size search.
1005     if (cpi->sf.tx_size_search_breakout &&
1006         (rd[n][1] == INT64_MAX ||
1007          (n < (int)max_tx_size && rd[n][1] > rd[n + 1][1]) || s[n] == 1))
1008       break;
1009
1010     if (rd[n][1] < best_rd) {
1011       best_tx = n;
1012       best_rd = rd[n][1];
1013     }
1014   }
1015   mi->tx_size = best_tx;
1016
1017   *distortion = d[mi->tx_size];
1018   *rate = r[mi->tx_size][cm->tx_mode == TX_MODE_SELECT];
1019   *skip = s[mi->tx_size];
1020   *psse = sse[mi->tx_size];
1021   if (recon) {
1022 #if CONFIG_VP9_HIGHBITDEPTH
1023     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1024       memcpy(CONVERT_TO_SHORTPTR(recon->buf),
1025              CONVERT_TO_SHORTPTR(recon_buf[mi->tx_size]),
1026              64 * 64 * sizeof(uint16_t));
1027     } else {
1028 #endif
1029       memcpy(recon->buf, recon_buf[mi->tx_size], 64 * 64);
1030 #if CONFIG_VP9_HIGHBITDEPTH
1031     }
1032 #endif
1033   }
1034 }
1035
1036 static void super_block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
1037                             int64_t *distortion, int *skip, int64_t *psse,
1038                             BLOCK_SIZE bs, int64_t ref_best_rd,
1039                             struct buf_2d *recon) {
1040   MACROBLOCKD *xd = &x->e_mbd;
1041   int64_t sse;
1042   int64_t *ret_sse = psse ? psse : &sse;
1043
1044   assert(bs == xd->mi[0]->sb_type);
1045
1046   if (cpi->sf.tx_size_search_method == USE_LARGESTALL || xd->lossless) {
1047     choose_largest_tx_size(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd,
1048                            bs, recon);
1049   } else {
1050     choose_tx_size_from_rd(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd,
1051                            bs, recon);
1052   }
1053 }
1054
1055 static int conditional_skipintra(PREDICTION_MODE mode,
1056                                  PREDICTION_MODE best_intra_mode) {
1057   if (mode == D117_PRED && best_intra_mode != V_PRED &&
1058       best_intra_mode != D135_PRED)
1059     return 1;
1060   if (mode == D63_PRED && best_intra_mode != V_PRED &&
1061       best_intra_mode != D45_PRED)
1062     return 1;
1063   if (mode == D207_PRED && best_intra_mode != H_PRED &&
1064       best_intra_mode != D45_PRED)
1065     return 1;
1066   if (mode == D153_PRED && best_intra_mode != H_PRED &&
1067       best_intra_mode != D135_PRED)
1068     return 1;
1069   return 0;
1070 }
1071
1072 static int64_t rd_pick_intra4x4block(VP9_COMP *cpi, MACROBLOCK *x, int row,
1073                                      int col, PREDICTION_MODE *best_mode,
1074                                      const int *bmode_costs, ENTROPY_CONTEXT *a,
1075                                      ENTROPY_CONTEXT *l, int *bestrate,
1076                                      int *bestratey, int64_t *bestdistortion,
1077                                      BLOCK_SIZE bsize, int64_t rd_thresh) {
1078   PREDICTION_MODE mode;
1079   MACROBLOCKD *const xd = &x->e_mbd;
1080   int64_t best_rd = rd_thresh;
1081   struct macroblock_plane *p = &x->plane[0];
1082   struct macroblockd_plane *pd = &xd->plane[0];
1083   const int src_stride = p->src.stride;
1084   const int dst_stride = pd->dst.stride;
1085   const uint8_t *src_init = &p->src.buf[row * 4 * src_stride + col * 4];
1086   uint8_t *dst_init = &pd->dst.buf[row * 4 * src_stride + col * 4];
1087   ENTROPY_CONTEXT ta[2], tempa[2];
1088   ENTROPY_CONTEXT tl[2], templ[2];
1089   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1090   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1091   int idx, idy;
1092   uint8_t best_dst[8 * 8];
1093 #if CONFIG_VP9_HIGHBITDEPTH
1094   uint16_t best_dst16[8 * 8];
1095 #endif
1096   memcpy(ta, a, num_4x4_blocks_wide * sizeof(a[0]));
1097   memcpy(tl, l, num_4x4_blocks_high * sizeof(l[0]));
1098
1099   xd->mi[0]->tx_size = TX_4X4;
1100
1101 #if CONFIG_VP9_HIGHBITDEPTH
1102   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1103     for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
1104       int64_t this_rd;
1105       int ratey = 0;
1106       int64_t distortion = 0;
1107       int rate = bmode_costs[mode];
1108
1109       if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue;
1110
1111       // Only do the oblique modes if the best so far is
1112       // one of the neighboring directional modes
1113       if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
1114         if (conditional_skipintra(mode, *best_mode)) continue;
1115       }
1116
1117       memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0]));
1118       memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0]));
1119
1120       for (idy = 0; idy < num_4x4_blocks_high; ++idy) {
1121         for (idx = 0; idx < num_4x4_blocks_wide; ++idx) {
1122           const int block = (row + idy) * 2 + (col + idx);
1123           const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
1124           uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
1125           uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
1126           int16_t *const src_diff =
1127               vp9_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff);
1128           tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block);
1129           xd->mi[0]->bmi[block].as_mode = mode;
1130           vp9_predict_intra_block(xd, 1, TX_4X4, mode,
1131                                   x->skip_encode ? src : dst,
1132                                   x->skip_encode ? src_stride : dst_stride, dst,
1133                                   dst_stride, col + idx, row + idy, 0);
1134           vpx_highbd_subtract_block(4, 4, src_diff, 8, src, src_stride, dst,
1135                                     dst_stride, xd->bd);
1136           if (xd->lossless) {
1137             const scan_order *so = &vp9_default_scan_orders[TX_4X4];
1138             const int coeff_ctx =
1139                 combine_entropy_contexts(tempa[idx], templ[idy]);
1140             vp9_highbd_fwht4x4(src_diff, coeff, 8);
1141             vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
1142             ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
1143                                  so->neighbors, cpi->sf.use_fast_coef_costing);
1144             tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0 ? 1 : 0);
1145             if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
1146               goto next_highbd;
1147             vp9_highbd_iwht4x4_add(BLOCK_OFFSET(pd->dqcoeff, block), dst16,
1148                                    dst_stride, p->eobs[block], xd->bd);
1149           } else {
1150             int64_t unused;
1151             const TX_TYPE tx_type = get_tx_type_4x4(PLANE_TYPE_Y, xd, block);
1152             const scan_order *so = &vp9_scan_orders[TX_4X4][tx_type];
1153             const int coeff_ctx =
1154                 combine_entropy_contexts(tempa[idx], templ[idy]);
1155             if (tx_type == DCT_DCT)
1156               vpx_highbd_fdct4x4(src_diff, coeff, 8);
1157             else
1158               vp9_highbd_fht4x4(src_diff, coeff, 8, tx_type);
1159             vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
1160             ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
1161                                  so->neighbors, cpi->sf.use_fast_coef_costing);
1162             distortion += vp9_highbd_block_error_dispatch(
1163                               coeff, BLOCK_OFFSET(pd->dqcoeff, block), 16,
1164                               &unused, xd->bd) >>
1165                           2;
1166             tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0 ? 1 : 0);
1167             if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
1168               goto next_highbd;
1169             vp9_highbd_iht4x4_add(tx_type, BLOCK_OFFSET(pd->dqcoeff, block),
1170                                   dst16, dst_stride, p->eobs[block], xd->bd);
1171           }
1172         }
1173       }
1174
1175       rate += ratey;
1176       this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1177
1178       if (this_rd < best_rd) {
1179         *bestrate = rate;
1180         *bestratey = ratey;
1181         *bestdistortion = distortion;
1182         best_rd = this_rd;
1183         *best_mode = mode;
1184         memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0]));
1185         memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0]));
1186         for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) {
1187           memcpy(best_dst16 + idy * 8,
1188                  CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
1189                  num_4x4_blocks_wide * 4 * sizeof(uint16_t));
1190         }
1191       }
1192     next_highbd : {}
1193     }
1194     if (best_rd >= rd_thresh || x->skip_encode) return best_rd;
1195
1196     for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) {
1197       memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
1198              best_dst16 + idy * 8, num_4x4_blocks_wide * 4 * sizeof(uint16_t));
1199     }
1200
1201     return best_rd;
1202   }
1203 #endif  // CONFIG_VP9_HIGHBITDEPTH
1204
1205   for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
1206     int64_t this_rd;
1207     int ratey = 0;
1208     int64_t distortion = 0;
1209     int rate = bmode_costs[mode];
1210
1211     if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue;
1212
1213     // Only do the oblique modes if the best so far is
1214     // one of the neighboring directional modes
1215     if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
1216       if (conditional_skipintra(mode, *best_mode)) continue;
1217     }
1218
1219     memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0]));
1220     memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0]));
1221
1222     for (idy = 0; idy < num_4x4_blocks_high; ++idy) {
1223       for (idx = 0; idx < num_4x4_blocks_wide; ++idx) {
1224         const int block = (row + idy) * 2 + (col + idx);
1225         const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
1226         uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
1227         int16_t *const src_diff =
1228             vp9_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff);
1229         tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block);
1230         xd->mi[0]->bmi[block].as_mode = mode;
1231         vp9_predict_intra_block(xd, 1, TX_4X4, mode, x->skip_encode ? src : dst,
1232                                 x->skip_encode ? src_stride : dst_stride, dst,
1233                                 dst_stride, col + idx, row + idy, 0);
1234         vpx_subtract_block(4, 4, src_diff, 8, src, src_stride, dst, dst_stride);
1235
1236         if (xd->lossless) {
1237           const scan_order *so = &vp9_default_scan_orders[TX_4X4];
1238           const int coeff_ctx =
1239               combine_entropy_contexts(tempa[idx], templ[idy]);
1240           vp9_fwht4x4(src_diff, coeff, 8);
1241           vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
1242           ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
1243                                so->neighbors, cpi->sf.use_fast_coef_costing);
1244           tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0) ? 1 : 0;
1245           if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
1246             goto next;
1247           vp9_iwht4x4_add(BLOCK_OFFSET(pd->dqcoeff, block), dst, dst_stride,
1248                           p->eobs[block]);
1249         } else {
1250           int64_t unused;
1251           const TX_TYPE tx_type = get_tx_type_4x4(PLANE_TYPE_Y, xd, block);
1252           const scan_order *so = &vp9_scan_orders[TX_4X4][tx_type];
1253           const int coeff_ctx =
1254               combine_entropy_contexts(tempa[idx], templ[idy]);
1255           vp9_fht4x4(src_diff, coeff, 8, tx_type);
1256           vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
1257           ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
1258                                so->neighbors, cpi->sf.use_fast_coef_costing);
1259           tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0) ? 1 : 0;
1260           distortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, block),
1261                                         16, &unused) >>
1262                         2;
1263           if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
1264             goto next;
1265           vp9_iht4x4_add(tx_type, BLOCK_OFFSET(pd->dqcoeff, block), dst,
1266                          dst_stride, p->eobs[block]);
1267         }
1268       }
1269     }
1270
1271     rate += ratey;
1272     this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1273
1274     if (this_rd < best_rd) {
1275       *bestrate = rate;
1276       *bestratey = ratey;
1277       *bestdistortion = distortion;
1278       best_rd = this_rd;
1279       *best_mode = mode;
1280       memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0]));
1281       memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0]));
1282       for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
1283         memcpy(best_dst + idy * 8, dst_init + idy * dst_stride,
1284                num_4x4_blocks_wide * 4);
1285     }
1286   next : {}
1287   }
1288
1289   if (best_rd >= rd_thresh || x->skip_encode) return best_rd;
1290
1291   for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
1292     memcpy(dst_init + idy * dst_stride, best_dst + idy * 8,
1293            num_4x4_blocks_wide * 4);
1294
1295   return best_rd;
1296 }
1297
1298 static int64_t rd_pick_intra_sub_8x8_y_mode(VP9_COMP *cpi, MACROBLOCK *mb,
1299                                             int *rate, int *rate_y,
1300                                             int64_t *distortion,
1301                                             int64_t best_rd) {
1302   int i, j;
1303   const MACROBLOCKD *const xd = &mb->e_mbd;
1304   MODE_INFO *const mic = xd->mi[0];
1305   const MODE_INFO *above_mi = xd->above_mi;
1306   const MODE_INFO *left_mi = xd->left_mi;
1307   const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
1308   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1309   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1310   int idx, idy;
1311   int cost = 0;
1312   int64_t total_distortion = 0;
1313   int tot_rate_y = 0;
1314   int64_t total_rd = 0;
1315   const int *bmode_costs = cpi->mbmode_cost;
1316
1317   // Pick modes for each sub-block (of size 4x4, 4x8, or 8x4) in an 8x8 block.
1318   for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1319     for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1320       PREDICTION_MODE best_mode = DC_PRED;
1321       int r = INT_MAX, ry = INT_MAX;
1322       int64_t d = INT64_MAX, this_rd = INT64_MAX;
1323       i = idy * 2 + idx;
1324       if (cpi->common.frame_type == KEY_FRAME) {
1325         const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, i);
1326         const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, i);
1327
1328         bmode_costs = cpi->y_mode_costs[A][L];
1329       }
1330
1331       this_rd = rd_pick_intra4x4block(
1332           cpi, mb, idy, idx, &best_mode, bmode_costs,
1333           xd->plane[0].above_context + idx, xd->plane[0].left_context + idy, &r,
1334           &ry, &d, bsize, best_rd - total_rd);
1335
1336       if (this_rd >= best_rd - total_rd) return INT64_MAX;
1337
1338       total_rd += this_rd;
1339       cost += r;
1340       total_distortion += d;
1341       tot_rate_y += ry;
1342
1343       mic->bmi[i].as_mode = best_mode;
1344       for (j = 1; j < num_4x4_blocks_high; ++j)
1345         mic->bmi[i + j * 2].as_mode = best_mode;
1346       for (j = 1; j < num_4x4_blocks_wide; ++j)
1347         mic->bmi[i + j].as_mode = best_mode;
1348
1349       if (total_rd >= best_rd) return INT64_MAX;
1350     }
1351   }
1352
1353   *rate = cost;
1354   *rate_y = tot_rate_y;
1355   *distortion = total_distortion;
1356   mic->mode = mic->bmi[3].as_mode;
1357
1358   return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion);
1359 }
1360
1361 // This function is used only for intra_only frames
1362 static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
1363                                       int *rate_tokenonly, int64_t *distortion,
1364                                       int *skippable, BLOCK_SIZE bsize,
1365                                       int64_t best_rd) {
1366   PREDICTION_MODE mode;
1367   PREDICTION_MODE mode_selected = DC_PRED;
1368   MACROBLOCKD *const xd = &x->e_mbd;
1369   MODE_INFO *const mic = xd->mi[0];
1370   int this_rate, this_rate_tokenonly, s;
1371   int64_t this_distortion, this_rd;
1372   TX_SIZE best_tx = TX_4X4;
1373   int *bmode_costs;
1374   const MODE_INFO *above_mi = xd->above_mi;
1375   const MODE_INFO *left_mi = xd->left_mi;
1376   const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
1377   const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
1378   bmode_costs = cpi->y_mode_costs[A][L];
1379
1380   memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
1381   /* Y Search for intra prediction mode */
1382   for (mode = DC_PRED; mode <= TM_PRED; mode++) {
1383     if (cpi->sf.use_nonrd_pick_mode) {
1384       // These speed features are turned on in hybrid non-RD and RD mode
1385       // for key frame coding in the context of real-time setting.
1386       if (conditional_skipintra(mode, mode_selected)) continue;
1387       if (*skippable) break;
1388     }
1389
1390     mic->mode = mode;
1391
1392     super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL,
1393                     bsize, best_rd, /*recon = */ 0);
1394
1395     if (this_rate_tokenonly == INT_MAX) continue;
1396
1397     this_rate = this_rate_tokenonly + bmode_costs[mode];
1398     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
1399
1400     if (this_rd < best_rd) {
1401       mode_selected = mode;
1402       best_rd = this_rd;
1403       best_tx = mic->tx_size;
1404       *rate = this_rate;
1405       *rate_tokenonly = this_rate_tokenonly;
1406       *distortion = this_distortion;
1407       *skippable = s;
1408     }
1409   }
1410
1411   mic->mode = mode_selected;
1412   mic->tx_size = best_tx;
1413
1414   return best_rd;
1415 }
1416
1417 // Return value 0: early termination triggered, no valid rd cost available;
1418 //              1: rd cost values are valid.
1419 static int super_block_uvrd(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
1420                             int64_t *distortion, int *skippable, int64_t *sse,
1421                             BLOCK_SIZE bsize, int64_t ref_best_rd) {
1422   MACROBLOCKD *const xd = &x->e_mbd;
1423   MODE_INFO *const mi = xd->mi[0];
1424   const TX_SIZE uv_tx_size = get_uv_tx_size(mi, &xd->plane[1]);
1425   int plane;
1426   int pnrate = 0, pnskip = 1;
1427   int64_t pndist = 0, pnsse = 0;
1428   int is_cost_valid = 1;
1429
1430   if (ref_best_rd < 0) is_cost_valid = 0;
1431
1432   if (is_inter_block(mi) && is_cost_valid) {
1433     int plane;
1434     for (plane = 1; plane < MAX_MB_PLANE; ++plane)
1435       vp9_subtract_plane(x, bsize, plane);
1436   }
1437
1438   *rate = 0;
1439   *distortion = 0;
1440   *sse = 0;
1441   *skippable = 1;
1442
1443   for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
1444     txfm_rd_in_plane(cpi, x, &pnrate, &pndist, &pnskip, &pnsse, ref_best_rd,
1445                      plane, bsize, uv_tx_size, cpi->sf.use_fast_coef_costing,
1446                      /*recon = */ 0);
1447     if (pnrate == INT_MAX) {
1448       is_cost_valid = 0;
1449       break;
1450     }
1451     *rate += pnrate;
1452     *distortion += pndist;
1453     *sse += pnsse;
1454     *skippable &= pnskip;
1455   }
1456
1457   if (!is_cost_valid) {
1458     // reset cost value
1459     *rate = INT_MAX;
1460     *distortion = INT64_MAX;
1461     *sse = INT64_MAX;
1462     *skippable = 0;
1463   }
1464
1465   return is_cost_valid;
1466 }
1467
1468 static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
1469                                        PICK_MODE_CONTEXT *ctx, int *rate,
1470                                        int *rate_tokenonly, int64_t *distortion,
1471                                        int *skippable, BLOCK_SIZE bsize,
1472                                        TX_SIZE max_tx_size) {
1473   MACROBLOCKD *xd = &x->e_mbd;
1474   PREDICTION_MODE mode;
1475   PREDICTION_MODE mode_selected = DC_PRED;
1476   int64_t best_rd = INT64_MAX, this_rd;
1477   int this_rate_tokenonly, this_rate, s;
1478   int64_t this_distortion, this_sse;
1479
1480   memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
1481   for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
1482     if (!(cpi->sf.intra_uv_mode_mask[max_tx_size] & (1 << mode))) continue;
1483 #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
1484     if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
1485         (xd->above_mi == NULL || xd->left_mi == NULL) && need_top_left[mode])
1486       continue;
1487 #endif  // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
1488
1489     xd->mi[0]->uv_mode = mode;
1490
1491     if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s,
1492                           &this_sse, bsize, best_rd))
1493       continue;
1494     this_rate =
1495         this_rate_tokenonly +
1496         cpi->intra_uv_mode_cost[cpi->common.frame_type][xd->mi[0]->mode][mode];
1497     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
1498
1499     if (this_rd < best_rd) {
1500       mode_selected = mode;
1501       best_rd = this_rd;
1502       *rate = this_rate;
1503       *rate_tokenonly = this_rate_tokenonly;
1504       *distortion = this_distortion;
1505       *skippable = s;
1506       if (!x->select_tx_size) swap_block_ptr(x, ctx, 2, 0, 1, MAX_MB_PLANE);
1507     }
1508   }
1509
1510   xd->mi[0]->uv_mode = mode_selected;
1511   return best_rd;
1512 }
1513
1514 static int64_t rd_sbuv_dcpred(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
1515                               int *rate_tokenonly, int64_t *distortion,
1516                               int *skippable, BLOCK_SIZE bsize) {
1517   const VP9_COMMON *cm = &cpi->common;
1518   int64_t unused;
1519
1520   x->e_mbd.mi[0]->uv_mode = DC_PRED;
1521   memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
1522   super_block_uvrd(cpi, x, rate_tokenonly, distortion, skippable, &unused,
1523                    bsize, INT64_MAX);
1524   *rate =
1525       *rate_tokenonly +
1526       cpi->intra_uv_mode_cost[cm->frame_type][x->e_mbd.mi[0]->mode][DC_PRED];
1527   return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
1528 }
1529
1530 static void choose_intra_uv_mode(VP9_COMP *cpi, MACROBLOCK *const x,
1531                                  PICK_MODE_CONTEXT *ctx, BLOCK_SIZE bsize,
1532                                  TX_SIZE max_tx_size, int *rate_uv,
1533                                  int *rate_uv_tokenonly, int64_t *dist_uv,
1534                                  int *skip_uv, PREDICTION_MODE *mode_uv) {
1535   // Use an estimated rd for uv_intra based on DC_PRED if the
1536   // appropriate speed flag is set.
1537   if (cpi->sf.use_uv_intra_rd_estimate) {
1538     rd_sbuv_dcpred(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv,
1539                    bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize);
1540     // Else do a proper rd search for each possible transform size that may
1541     // be considered in the main rd loop.
1542   } else {
1543     rd_pick_intra_sbuv_mode(cpi, x, ctx, rate_uv, rate_uv_tokenonly, dist_uv,
1544                             skip_uv, bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
1545                             max_tx_size);
1546   }
1547   *mode_uv = x->e_mbd.mi[0]->uv_mode;
1548 }
1549
1550 static int cost_mv_ref(const VP9_COMP *cpi, PREDICTION_MODE mode,
1551                        int mode_context) {
1552   assert(is_inter_mode(mode));
1553   return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)];
1554 }
1555
1556 static int set_and_cost_bmi_mvs(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
1557                                 int i, PREDICTION_MODE mode, int_mv this_mv[2],
1558                                 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
1559                                 int_mv seg_mvs[MAX_REF_FRAMES],
1560                                 int_mv *best_ref_mv[2], const int *mvjcost,
1561                                 int *mvcost[2]) {
1562   MODE_INFO *const mi = xd->mi[0];
1563   const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1564   int thismvcost = 0;
1565   int idx, idy;
1566   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mi->sb_type];
1567   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mi->sb_type];
1568   const int is_compound = has_second_ref(mi);
1569
1570   switch (mode) {
1571     case NEWMV:
1572       this_mv[0].as_int = seg_mvs[mi->ref_frame[0]].as_int;
1573       thismvcost += vp9_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv,
1574                                     mvjcost, mvcost, MV_COST_WEIGHT_SUB);
1575       if (is_compound) {
1576         this_mv[1].as_int = seg_mvs[mi->ref_frame[1]].as_int;
1577         thismvcost += vp9_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv,
1578                                       mvjcost, mvcost, MV_COST_WEIGHT_SUB);
1579       }
1580       break;
1581     case NEARMV:
1582     case NEARESTMV:
1583       this_mv[0].as_int = frame_mv[mode][mi->ref_frame[0]].as_int;
1584       if (is_compound)
1585         this_mv[1].as_int = frame_mv[mode][mi->ref_frame[1]].as_int;
1586       break;
1587     default:
1588       assert(mode == ZEROMV);
1589       this_mv[0].as_int = 0;
1590       if (is_compound) this_mv[1].as_int = 0;
1591       break;
1592   }
1593
1594   mi->bmi[i].as_mv[0].as_int = this_mv[0].as_int;
1595   if (is_compound) mi->bmi[i].as_mv[1].as_int = this_mv[1].as_int;
1596
1597   mi->bmi[i].as_mode = mode;
1598
1599   for (idy = 0; idy < num_4x4_blocks_high; ++idy)
1600     for (idx = 0; idx < num_4x4_blocks_wide; ++idx)
1601       memmove(&mi->bmi[i + idy * 2 + idx], &mi->bmi[i], sizeof(mi->bmi[i]));
1602
1603   return cost_mv_ref(cpi, mode, mbmi_ext->mode_context[mi->ref_frame[0]]) +
1604          thismvcost;
1605 }
1606
1607 static int64_t encode_inter_mb_segment(VP9_COMP *cpi, MACROBLOCK *x,
1608                                        int64_t best_yrd, int i, int *labelyrate,
1609                                        int64_t *distortion, int64_t *sse,
1610                                        ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl,
1611                                        int mi_row, int mi_col) {
1612   int k;
1613   MACROBLOCKD *xd = &x->e_mbd;
1614   struct macroblockd_plane *const pd = &xd->plane[0];
1615   struct macroblock_plane *const p = &x->plane[0];
1616   MODE_INFO *const mi = xd->mi[0];
1617   const BLOCK_SIZE plane_bsize = get_plane_block_size(mi->sb_type, pd);
1618   const int width = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
1619   const int height = 4 * num_4x4_blocks_high_lookup[plane_bsize];
1620   int idx, idy;
1621
1622   const uint8_t *const src =
1623       &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1624   uint8_t *const dst =
1625       &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
1626   int64_t thisdistortion = 0, thissse = 0;
1627   int thisrate = 0, ref;
1628   const scan_order *so = &vp9_default_scan_orders[TX_4X4];
1629   const int is_compound = has_second_ref(mi);
1630   const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
1631
1632   for (ref = 0; ref < 1 + is_compound; ++ref) {
1633     const int bw = b_width_log2_lookup[BLOCK_8X8];
1634     const int h = 4 * (i >> bw);
1635     const int w = 4 * (i & ((1 << bw) - 1));
1636     const struct scale_factors *sf = &xd->block_refs[ref]->sf;
1637     int y_stride = pd->pre[ref].stride;
1638     uint8_t *pre = pd->pre[ref].buf + (h * pd->pre[ref].stride + w);
1639
1640     if (vp9_is_scaled(sf)) {
1641       const int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
1642       const int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
1643
1644       y_stride = xd->block_refs[ref]->buf->y_stride;
1645       pre = xd->block_refs[ref]->buf->y_buffer;
1646       pre += scaled_buffer_offset(x_start + w, y_start + h, y_stride, sf);
1647     }
1648 #if CONFIG_VP9_HIGHBITDEPTH
1649     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1650       vp9_highbd_build_inter_predictor(
1651           CONVERT_TO_SHORTPTR(pre), y_stride, CONVERT_TO_SHORTPTR(dst),
1652           pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
1653           &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
1654           mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2),
1655           xd->bd);
1656     } else {
1657       vp9_build_inter_predictor(
1658           pre, y_stride, dst, pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
1659           &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
1660           mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2));
1661     }
1662 #else
1663     vp9_build_inter_predictor(
1664         pre, y_stride, dst, pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
1665         &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
1666         mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2));
1667 #endif  // CONFIG_VP9_HIGHBITDEPTH
1668   }
1669
1670 #if CONFIG_VP9_HIGHBITDEPTH
1671   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1672     vpx_highbd_subtract_block(
1673         height, width, vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
1674         8, src, p->src.stride, dst, pd->dst.stride, xd->bd);
1675   } else {
1676     vpx_subtract_block(height, width,
1677                        vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
1678                        8, src, p->src.stride, dst, pd->dst.stride);
1679   }
1680 #else
1681   vpx_subtract_block(height, width,
1682                      vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
1683                      8, src, p->src.stride, dst, pd->dst.stride);
1684 #endif  // CONFIG_VP9_HIGHBITDEPTH
1685
1686   k = i;
1687   for (idy = 0; idy < height / 4; ++idy) {
1688     for (idx = 0; idx < width / 4; ++idx) {
1689 #if CONFIG_VP9_HIGHBITDEPTH
1690       const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
1691 #endif
1692       int64_t ssz, rd, rd1, rd2;
1693       tran_low_t *coeff;
1694       int coeff_ctx;
1695       k += (idy * 2 + idx);
1696       coeff_ctx = combine_entropy_contexts(ta[k & 1], tl[k >> 1]);
1697       coeff = BLOCK_OFFSET(p->coeff, k);
1698       x->fwd_txfm4x4(vp9_raster_block_offset_int16(BLOCK_8X8, k, p->src_diff),
1699                      coeff, 8);
1700       vp9_regular_quantize_b_4x4(x, 0, k, so->scan, so->iscan);
1701 #if CONFIG_VP9_HIGHBITDEPTH
1702       thisdistortion += vp9_highbd_block_error_dispatch(
1703           coeff, BLOCK_OFFSET(pd->dqcoeff, k), 16, &ssz, bd);
1704 #else
1705       thisdistortion +=
1706           vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, k), 16, &ssz);
1707 #endif  // CONFIG_VP9_HIGHBITDEPTH
1708       thissse += ssz;
1709       thisrate += cost_coeffs(x, 0, k, TX_4X4, coeff_ctx, so->scan,
1710                               so->neighbors, cpi->sf.use_fast_coef_costing);
1711       ta[k & 1] = tl[k >> 1] = (x->plane[0].eobs[k] > 0) ? 1 : 0;
1712       rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion >> 2);
1713       rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse >> 2);
1714       rd = VPXMIN(rd1, rd2);
1715       if (rd >= best_yrd) return INT64_MAX;
1716     }
1717   }
1718
1719   *distortion = thisdistortion >> 2;
1720   *labelyrate = thisrate;
1721   *sse = thissse >> 2;
1722
1723   return RDCOST(x->rdmult, x->rddiv, *labelyrate, *distortion);
1724 }
1725
1726 typedef struct {
1727   int eobs;
1728   int brate;
1729   int byrate;
1730   int64_t bdist;
1731   int64_t bsse;
1732   int64_t brdcost;
1733   int_mv mvs[2];
1734   ENTROPY_CONTEXT ta[2];
1735   ENTROPY_CONTEXT tl[2];
1736 } SEG_RDSTAT;
1737
1738 typedef struct {
1739   int_mv *ref_mv[2];
1740   int_mv mvp;
1741
1742   int64_t segment_rd;
1743   int r;
1744   int64_t d;
1745   int64_t sse;
1746   int segment_yrate;
1747   PREDICTION_MODE modes[4];
1748   SEG_RDSTAT rdstat[4][INTER_MODES];
1749   int mvthresh;
1750 } BEST_SEG_INFO;
1751
1752 static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) {
1753   return (mv->row >> 3) < mv_limits->row_min ||
1754          (mv->row >> 3) > mv_limits->row_max ||
1755          (mv->col >> 3) < mv_limits->col_min ||
1756          (mv->col >> 3) > mv_limits->col_max;
1757 }
1758
1759 static INLINE void mi_buf_shift(MACROBLOCK *x, int i) {
1760   MODE_INFO *const mi = x->e_mbd.mi[0];
1761   struct macroblock_plane *const p = &x->plane[0];
1762   struct macroblockd_plane *const pd = &x->e_mbd.plane[0];
1763
1764   p->src.buf =
1765       &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1766   assert(((intptr_t)pd->pre[0].buf & 0x7) == 0);
1767   pd->pre[0].buf =
1768       &pd->pre[0].buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)];
1769   if (has_second_ref(mi))
1770     pd->pre[1].buf =
1771         &pd->pre[1]
1772              .buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[1].stride)];
1773 }
1774
1775 static INLINE void mi_buf_restore(MACROBLOCK *x, struct buf_2d orig_src,
1776                                   struct buf_2d orig_pre[2]) {
1777   MODE_INFO *mi = x->e_mbd.mi[0];
1778   x->plane[0].src = orig_src;
1779   x->e_mbd.plane[0].pre[0] = orig_pre[0];
1780   if (has_second_ref(mi)) x->e_mbd.plane[0].pre[1] = orig_pre[1];
1781 }
1782
1783 static INLINE int mv_has_subpel(const MV *mv) {
1784   return (mv->row & 0x0F) || (mv->col & 0x0F);
1785 }
1786
1787 // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion.
1788 // TODO(aconverse): Find out if this is still productive then clean up or remove
1789 static int check_best_zero_mv(const VP9_COMP *cpi,
1790                               const uint8_t mode_context[MAX_REF_FRAMES],
1791                               int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
1792                               int this_mode,
1793                               const MV_REFERENCE_FRAME ref_frames[2]) {
1794   if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) &&
1795       frame_mv[this_mode][ref_frames[0]].as_int == 0 &&
1796       (ref_frames[1] == NONE ||
1797        frame_mv[this_mode][ref_frames[1]].as_int == 0)) {
1798     int rfc = mode_context[ref_frames[0]];
1799     int c1 = cost_mv_ref(cpi, NEARMV, rfc);
1800     int c2 = cost_mv_ref(cpi, NEARESTMV, rfc);
1801     int c3 = cost_mv_ref(cpi, ZEROMV, rfc);
1802
1803     if (this_mode == NEARMV) {
1804       if (c1 > c3) return 0;
1805     } else if (this_mode == NEARESTMV) {
1806       if (c2 > c3) return 0;
1807     } else {
1808       assert(this_mode == ZEROMV);
1809       if (ref_frames[1] == NONE) {
1810         if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) ||
1811             (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0))
1812           return 0;
1813       } else {
1814         if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 &&
1815              frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) ||
1816             (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 &&
1817              frame_mv[NEARMV][ref_frames[1]].as_int == 0))
1818           return 0;
1819       }
1820     }
1821   }
1822   return 1;
1823 }
1824
1825 static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
1826                                 int_mv *frame_mv, int mi_row, int mi_col,
1827                                 int_mv single_newmv[MAX_REF_FRAMES],
1828                                 int *rate_mv) {
1829   const VP9_COMMON *const cm = &cpi->common;
1830   const int pw = 4 * num_4x4_blocks_wide_lookup[bsize];
1831   const int ph = 4 * num_4x4_blocks_high_lookup[bsize];
1832   MACROBLOCKD *xd = &x->e_mbd;
1833   MODE_INFO *mi = xd->mi[0];
1834   const int refs[2] = { mi->ref_frame[0],
1835                         mi->ref_frame[1] < 0 ? 0 : mi->ref_frame[1] };
1836   int_mv ref_mv[2];
1837   int ite, ref;
1838   const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
1839   struct scale_factors sf;
1840
1841   // Do joint motion search in compound mode to get more accurate mv.
1842   struct buf_2d backup_yv12[2][MAX_MB_PLANE];
1843   uint32_t last_besterr[2] = { UINT_MAX, UINT_MAX };
1844   const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
1845     vp9_get_scaled_ref_frame(cpi, mi->ref_frame[0]),
1846     vp9_get_scaled_ref_frame(cpi, mi->ref_frame[1])
1847   };
1848
1849 // Prediction buffer from second frame.
1850 #if CONFIG_VP9_HIGHBITDEPTH
1851   DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[64 * 64]);
1852   uint8_t *second_pred;
1853 #else
1854   DECLARE_ALIGNED(16, uint8_t, second_pred[64 * 64]);
1855 #endif  // CONFIG_VP9_HIGHBITDEPTH
1856
1857   for (ref = 0; ref < 2; ++ref) {
1858     ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0];
1859
1860     if (scaled_ref_frame[ref]) {
1861       int i;
1862       // Swap out the reference frame for a version that's been scaled to
1863       // match the resolution of the current frame, allowing the existing
1864       // motion search code to be used without additional modifications.
1865       for (i = 0; i < MAX_MB_PLANE; i++)
1866         backup_yv12[ref][i] = xd->plane[i].pre[ref];
1867       vp9_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
1868                            NULL);
1869     }
1870
1871     frame_mv[refs[ref]].as_int = single_newmv[refs[ref]].as_int;
1872   }
1873
1874 // Since we have scaled the reference frames to match the size of the current
1875 // frame we must use a unit scaling factor during mode selection.
1876 #if CONFIG_VP9_HIGHBITDEPTH
1877   vp9_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
1878                                     cm->height, cm->use_highbitdepth);
1879 #else
1880   vp9_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
1881                                     cm->height);
1882 #endif  // CONFIG_VP9_HIGHBITDEPTH
1883
1884   // Allow joint search multiple times iteratively for each reference frame
1885   // and break out of the search loop if it couldn't find a better mv.
1886   for (ite = 0; ite < 4; ite++) {
1887     struct buf_2d ref_yv12[2];
1888     uint32_t bestsme = UINT_MAX;
1889     int sadpb = x->sadperbit16;
1890     MV tmp_mv;
1891     int search_range = 3;
1892
1893     const MvLimits tmp_mv_limits = x->mv_limits;
1894     int id = ite % 2;  // Even iterations search in the first reference frame,
1895                        // odd iterations search in the second. The predictor
1896                        // found for the 'other' reference frame is factored in.
1897
1898     // Initialized here because of compiler problem in Visual Studio.
1899     ref_yv12[0] = xd->plane[0].pre[0];
1900     ref_yv12[1] = xd->plane[0].pre[1];
1901
1902 // Get the prediction block from the 'other' reference frame.
1903 #if CONFIG_VP9_HIGHBITDEPTH
1904     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1905       second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
1906       vp9_highbd_build_inter_predictor(
1907           CONVERT_TO_SHORTPTR(ref_yv12[!id].buf), ref_yv12[!id].stride,
1908           second_pred_alloc_16, pw, &frame_mv[refs[!id]].as_mv, &sf, pw, ph, 0,
1909           kernel, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd->bd);
1910     } else {
1911       second_pred = (uint8_t *)second_pred_alloc_16;
1912       vp9_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride,
1913                                 second_pred, pw, &frame_mv[refs[!id]].as_mv,
1914                                 &sf, pw, ph, 0, kernel, MV_PRECISION_Q3,
1915                                 mi_col * MI_SIZE, mi_row * MI_SIZE);
1916     }
1917 #else
1918     vp9_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride,
1919                               second_pred, pw, &frame_mv[refs[!id]].as_mv, &sf,
1920                               pw, ph, 0, kernel, MV_PRECISION_Q3,
1921                               mi_col * MI_SIZE, mi_row * MI_SIZE);
1922 #endif  // CONFIG_VP9_HIGHBITDEPTH
1923
1924     // Do compound motion search on the current reference frame.
1925     if (id) xd->plane[0].pre[0] = ref_yv12[id];
1926     vp9_set_mv_search_range(&x->mv_limits, &ref_mv[id].as_mv);
1927
1928     // Use the mv result from the single mode as mv predictor.
1929     tmp_mv = frame_mv[refs[id]].as_mv;
1930
1931     tmp_mv.col >>= 3;
1932     tmp_mv.row >>= 3;
1933
1934     // Small-range full-pixel motion search.
1935     bestsme = vp9_refining_search_8p_c(x, &tmp_mv, sadpb, search_range,
1936                                        &cpi->fn_ptr[bsize], &ref_mv[id].as_mv,
1937                                        second_pred);
1938     if (bestsme < UINT_MAX)
1939       bestsme = vp9_get_mvpred_av_var(x, &tmp_mv, &ref_mv[id].as_mv,
1940                                       second_pred, &cpi->fn_ptr[bsize], 1);
1941
1942     x->mv_limits = tmp_mv_limits;
1943
1944     if (bestsme < UINT_MAX) {
1945       uint32_t dis; /* TODO: use dis in distortion calculation later. */
1946       uint32_t sse;
1947       bestsme = cpi->find_fractional_mv_step(
1948           x, &tmp_mv, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv,
1949           x->errorperbit, &cpi->fn_ptr[bsize], 0,
1950           cpi->sf.mv.subpel_search_level, NULL, x->nmvjointcost, x->mvcost,
1951           &dis, &sse, second_pred, pw, ph, cpi->sf.use_accurate_subpel_search);
1952     }
1953
1954     // Restore the pointer to the first (possibly scaled) prediction buffer.
1955     if (id) xd->plane[0].pre[0] = ref_yv12[0];
1956
1957     if (bestsme < last_besterr[id]) {
1958       frame_mv[refs[id]].as_mv = tmp_mv;
1959       last_besterr[id] = bestsme;
1960     } else {
1961       break;
1962     }
1963   }
1964
1965   *rate_mv = 0;
1966
1967   for (ref = 0; ref < 2; ++ref) {
1968     if (scaled_ref_frame[ref]) {
1969       // Restore the prediction frame pointers to their unscaled versions.
1970       int i;
1971       for (i = 0; i < MAX_MB_PLANE; i++)
1972         xd->plane[i].pre[ref] = backup_yv12[ref][i];
1973     }
1974
1975     *rate_mv += vp9_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
1976                                 &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv,
1977                                 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
1978   }
1979 }
1980
1981 static int64_t rd_pick_best_sub8x8_mode(
1982     VP9_COMP *cpi, MACROBLOCK *x, int_mv *best_ref_mv,
1983     int_mv *second_best_ref_mv, int64_t best_rd, int *returntotrate,
1984     int *returnyrate, int64_t *returndistortion, int *skippable, int64_t *psse,
1985     int mvthresh, int_mv seg_mvs[4][MAX_REF_FRAMES], BEST_SEG_INFO *bsi_buf,
1986     int filter_idx, int mi_row, int mi_col) {
1987   int i;
1988   BEST_SEG_INFO *bsi = bsi_buf + filter_idx;
1989   MACROBLOCKD *xd = &x->e_mbd;
1990   MODE_INFO *mi = xd->mi[0];
1991   int mode_idx;
1992   int k, br = 0, idx, idy;
1993   int64_t bd = 0, block_sse = 0;
1994   PREDICTION_MODE this_mode;
1995   VP9_COMMON *cm = &cpi->common;
1996   struct macroblock_plane *const p = &x->plane[0];
1997   struct macroblockd_plane *const pd = &xd->plane[0];
1998   const int label_count = 4;
1999   int64_t this_segment_rd = 0;
2000   int label_mv_thresh;
2001   int segmentyrate = 0;
2002   const BLOCK_SIZE bsize = mi->sb_type;
2003   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2004   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2005   const int pw = num_4x4_blocks_wide << 2;
2006   const int ph = num_4x4_blocks_high << 2;
2007   ENTROPY_CONTEXT t_above[2], t_left[2];
2008   int subpelmv = 1, have_ref = 0;
2009   SPEED_FEATURES *const sf = &cpi->sf;
2010   const int has_second_rf = has_second_ref(mi);
2011   const int inter_mode_mask = sf->inter_mode_mask[bsize];
2012   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
2013
2014   vp9_zero(*bsi);
2015
2016   bsi->segment_rd = best_rd;
2017   bsi->ref_mv[0] = best_ref_mv;
2018   bsi->ref_mv[1] = second_best_ref_mv;
2019   bsi->mvp.as_int = best_ref_mv->as_int;
2020   bsi->mvthresh = mvthresh;
2021
2022   for (i = 0; i < 4; i++) bsi->modes[i] = ZEROMV;
2023
2024   memcpy(t_above, pd->above_context, sizeof(t_above));
2025   memcpy(t_left, pd->left_context, sizeof(t_left));
2026
2027   // 64 makes this threshold really big effectively
2028   // making it so that we very rarely check mvs on
2029   // segments.   setting this to 1 would make mv thresh
2030   // roughly equal to what it is for macroblocks
2031   label_mv_thresh = 1 * bsi->mvthresh / label_count;
2032
2033   // Segmentation method overheads
2034   for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
2035     for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
2036       // TODO(jingning,rbultje): rewrite the rate-distortion optimization
2037       // loop for 4x4/4x8/8x4 block coding. to be replaced with new rd loop
2038       int_mv mode_mv[MB_MODE_COUNT][2];
2039       int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
2040       PREDICTION_MODE mode_selected = ZEROMV;
2041       int64_t best_rd = INT64_MAX;
2042       const int i = idy * 2 + idx;
2043       int ref;
2044
2045       for (ref = 0; ref < 1 + has_second_rf; ++ref) {
2046         const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
2047         frame_mv[ZEROMV][frame].as_int = 0;
2048         vp9_append_sub8x8_mvs_for_idx(
2049             cm, xd, i, ref, mi_row, mi_col, &frame_mv[NEARESTMV][frame],
2050             &frame_mv[NEARMV][frame], mbmi_ext->mode_context);
2051       }
2052
2053       // search for the best motion vector on this segment
2054       for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2055         const struct buf_2d orig_src = x->plane[0].src;
2056         struct buf_2d orig_pre[2];
2057
2058         mode_idx = INTER_OFFSET(this_mode);
2059         bsi->rdstat[i][mode_idx].brdcost = INT64_MAX;
2060         if (!(inter_mode_mask & (1 << this_mode))) continue;
2061
2062         if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, frame_mv,
2063                                 this_mode, mi->ref_frame))
2064           continue;
2065
2066         memcpy(orig_pre, pd->pre, sizeof(orig_pre));
2067         memcpy(bsi->rdstat[i][mode_idx].ta, t_above,
2068                sizeof(bsi->rdstat[i][mode_idx].ta));
2069         memcpy(bsi->rdstat[i][mode_idx].tl, t_left,
2070                sizeof(bsi->rdstat[i][mode_idx].tl));
2071
2072         // motion search for newmv (single predictor case only)
2073         if (!has_second_rf && this_mode == NEWMV &&
2074             seg_mvs[i][mi->ref_frame[0]].as_int == INVALID_MV) {
2075           MV *const new_mv = &mode_mv[NEWMV][0].as_mv;
2076           int step_param = 0;
2077           uint32_t bestsme = UINT_MAX;
2078           int sadpb = x->sadperbit4;
2079           MV mvp_full;
2080           int max_mv;
2081           int cost_list[5];
2082           const MvLimits tmp_mv_limits = x->mv_limits;
2083
2084           /* Is the best so far sufficiently good that we cant justify doing
2085            * and new motion search. */
2086           if (best_rd < label_mv_thresh) break;
2087
2088           if (cpi->oxcf.mode != BEST) {
2089             // use previous block's result as next block's MV predictor.
2090             if (i > 0) {
2091               bsi->mvp.as_int = mi->bmi[i - 1].as_mv[0].as_int;
2092               if (i == 2) bsi->mvp.as_int = mi->bmi[i - 2].as_mv[0].as_int;
2093             }
2094           }
2095           if (i == 0)
2096             max_mv = x->max_mv_context[mi->ref_frame[0]];
2097           else
2098             max_mv =
2099                 VPXMAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3;
2100
2101           if (sf->mv.auto_mv_step_size && cm->show_frame) {
2102             // Take wtd average of the step_params based on the last frame's
2103             // max mv magnitude and the best ref mvs of the current block for
2104             // the given reference.
2105             step_param =
2106                 (vp9_init_search_range(max_mv) + cpi->mv_step_param) / 2;
2107           } else {
2108             step_param = cpi->mv_step_param;
2109           }
2110
2111           mvp_full.row = bsi->mvp.as_mv.row >> 3;
2112           mvp_full.col = bsi->mvp.as_mv.col >> 3;
2113
2114           if (sf->adaptive_motion_search) {
2115             if (x->pred_mv[mi->ref_frame[0]].row != INT16_MAX &&
2116                 x->pred_mv[mi->ref_frame[0]].col != INT16_MAX) {
2117               mvp_full.row = x->pred_mv[mi->ref_frame[0]].row >> 3;
2118               mvp_full.col = x->pred_mv[mi->ref_frame[0]].col >> 3;
2119             }
2120             step_param = VPXMAX(step_param, 8);
2121           }
2122
2123           // adjust src pointer for this block
2124           mi_buf_shift(x, i);
2125
2126           vp9_set_mv_search_range(&x->mv_limits, &bsi->ref_mv[0]->as_mv);
2127
2128           bestsme = vp9_full_pixel_search(
2129               cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method,
2130               sadpb,
2131               sf->mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL,
2132               &bsi->ref_mv[0]->as_mv, new_mv, INT_MAX, 1);
2133
2134           x->mv_limits = tmp_mv_limits;
2135
2136           if (bestsme < UINT_MAX) {
2137             uint32_t distortion;
2138             cpi->find_fractional_mv_step(
2139                 x, new_mv, &bsi->ref_mv[0]->as_mv, cm->allow_high_precision_mv,
2140                 x->errorperbit, &cpi->fn_ptr[bsize], sf->mv.subpel_force_stop,
2141                 sf->mv.subpel_search_level, cond_cost_list(cpi, cost_list),
2142                 x->nmvjointcost, x->mvcost, &distortion,
2143                 &x->pred_sse[mi->ref_frame[0]], NULL, pw, ph,
2144                 cpi->sf.use_accurate_subpel_search);
2145
2146             // save motion search result for use in compound prediction
2147             seg_mvs[i][mi->ref_frame[0]].as_mv = *new_mv;
2148           }
2149
2150           x->pred_mv[mi->ref_frame[0]] = *new_mv;
2151
2152           // restore src pointers
2153           mi_buf_restore(x, orig_src, orig_pre);
2154         }
2155
2156         if (has_second_rf) {
2157           if (seg_mvs[i][mi->ref_frame[1]].as_int == INVALID_MV ||
2158               seg_mvs[i][mi->ref_frame[0]].as_int == INVALID_MV)
2159             continue;
2160         }
2161
2162         if (has_second_rf && this_mode == NEWMV &&
2163             mi->interp_filter == EIGHTTAP) {
2164           // adjust src pointers
2165           mi_buf_shift(x, i);
2166           if (sf->comp_inter_joint_search_thresh <= bsize) {
2167             int rate_mv;
2168             joint_motion_search(cpi, x, bsize, frame_mv[this_mode], mi_row,
2169                                 mi_col, seg_mvs[i], &rate_mv);
2170             seg_mvs[i][mi->ref_frame[0]].as_int =
2171                 frame_mv[this_mode][mi->ref_frame[0]].as_int;
2172             seg_mvs[i][mi->ref_frame[1]].as_int =
2173                 frame_mv[this_mode][mi->ref_frame[1]].as_int;
2174           }
2175           // restore src pointers
2176           mi_buf_restore(x, orig_src, orig_pre);
2177         }
2178
2179         bsi->rdstat[i][mode_idx].brate = set_and_cost_bmi_mvs(
2180             cpi, x, xd, i, this_mode, mode_mv[this_mode], frame_mv, seg_mvs[i],
2181             bsi->ref_mv, x->nmvjointcost, x->mvcost);
2182
2183         for (ref = 0; ref < 1 + has_second_rf; ++ref) {
2184           bsi->rdstat[i][mode_idx].mvs[ref].as_int =
2185               mode_mv[this_mode][ref].as_int;
2186           if (num_4x4_blocks_wide > 1)
2187             bsi->rdstat[i + 1][mode_idx].mvs[ref].as_int =
2188                 mode_mv[this_mode][ref].as_int;
2189           if (num_4x4_blocks_high > 1)
2190             bsi->rdstat[i + 2][mode_idx].mvs[ref].as_int =
2191                 mode_mv[this_mode][ref].as_int;
2192         }
2193
2194         // Trap vectors that reach beyond the UMV borders
2195         if (mv_check_bounds(&x->mv_limits, &mode_mv[this_mode][0].as_mv) ||
2196             (has_second_rf &&
2197              mv_check_bounds(&x->mv_limits, &mode_mv[this_mode][1].as_mv)))
2198           continue;
2199
2200         if (filter_idx > 0) {
2201           BEST_SEG_INFO *ref_bsi = bsi_buf;
2202           subpelmv = 0;
2203           have_ref = 1;
2204
2205           for (ref = 0; ref < 1 + has_second_rf; ++ref) {
2206             subpelmv |= mv_has_subpel(&mode_mv[this_mode][ref].as_mv);
2207             have_ref &= mode_mv[this_mode][ref].as_int ==
2208                         ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
2209           }
2210
2211           if (filter_idx > 1 && !subpelmv && !have_ref) {
2212             ref_bsi = bsi_buf + 1;
2213             have_ref = 1;
2214             for (ref = 0; ref < 1 + has_second_rf; ++ref)
2215               have_ref &= mode_mv[this_mode][ref].as_int ==
2216                           ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
2217           }
2218
2219           if (!subpelmv && have_ref &&
2220               ref_bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
2221             memcpy(&bsi->rdstat[i][mode_idx], &ref_bsi->rdstat[i][mode_idx],
2222                    sizeof(SEG_RDSTAT));
2223             if (num_4x4_blocks_wide > 1)
2224               bsi->rdstat[i + 1][mode_idx].eobs =
2225                   ref_bsi->rdstat[i + 1][mode_idx].eobs;
2226             if (num_4x4_blocks_high > 1)
2227               bsi->rdstat[i + 2][mode_idx].eobs =
2228                   ref_bsi->rdstat[i + 2][mode_idx].eobs;
2229
2230             if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
2231               mode_selected = this_mode;
2232               best_rd = bsi->rdstat[i][mode_idx].brdcost;
2233             }
2234             continue;
2235           }
2236         }
2237
2238         bsi->rdstat[i][mode_idx].brdcost = encode_inter_mb_segment(
2239             cpi, x, bsi->segment_rd - this_segment_rd, i,
2240             &bsi->rdstat[i][mode_idx].byrate, &bsi->rdstat[i][mode_idx].bdist,
2241             &bsi->rdstat[i][mode_idx].bsse, bsi->rdstat[i][mode_idx].ta,
2242             bsi->rdstat[i][mode_idx].tl, mi_row, mi_col);
2243         if (bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
2244           bsi->rdstat[i][mode_idx].brdcost +=
2245               RDCOST(x->rdmult, x->rddiv, bsi->rdstat[i][mode_idx].brate, 0);
2246           bsi->rdstat[i][mode_idx].brate += bsi->rdstat[i][mode_idx].byrate;
2247           bsi->rdstat[i][mode_idx].eobs = p->eobs[i];
2248           if (num_4x4_blocks_wide > 1)
2249             bsi->rdstat[i + 1][mode_idx].eobs = p->eobs[i + 1];
2250           if (num_4x4_blocks_high > 1)
2251             bsi->rdstat[i + 2][mode_idx].eobs = p->eobs[i + 2];
2252         }
2253
2254         if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
2255           mode_selected = this_mode;
2256           best_rd = bsi->rdstat[i][mode_idx].brdcost;
2257         }
2258       } /*for each 4x4 mode*/
2259
2260       if (best_rd == INT64_MAX) {
2261         int iy, midx;
2262         for (iy = i + 1; iy < 4; ++iy)
2263           for (midx = 0; midx < INTER_MODES; ++midx)
2264             bsi->rdstat[iy][midx].brdcost = INT64_MAX;
2265         bsi->segment_rd = INT64_MAX;
2266         return INT64_MAX;
2267       }
2268
2269       mode_idx = INTER_OFFSET(mode_selected);
2270       memcpy(t_above, bsi->rdstat[i][mode_idx].ta, sizeof(t_above));
2271       memcpy(t_left, bsi->rdstat[i][mode_idx].tl, sizeof(t_left));
2272
2273       set_and_cost_bmi_mvs(cpi, x, xd, i, mode_selected, mode_mv[mode_selected],
2274                            frame_mv, seg_mvs[i], bsi->ref_mv, x->nmvjointcost,
2275                            x->mvcost);
2276
2277       br += bsi->rdstat[i][mode_idx].brate;
2278       bd += bsi->rdstat[i][mode_idx].bdist;
2279       block_sse += bsi->rdstat[i][mode_idx].bsse;
2280       segmentyrate += bsi->rdstat[i][mode_idx].byrate;
2281       this_segment_rd += bsi->rdstat[i][mode_idx].brdcost;
2282
2283       if (this_segment_rd > bsi->segment_rd) {
2284         int iy, midx;
2285         for (iy = i + 1; iy < 4; ++iy)
2286           for (midx = 0; midx < INTER_MODES; ++midx)
2287             bsi->rdstat[iy][midx].brdcost = INT64_MAX;
2288         bsi->segment_rd = INT64_MAX;
2289         return INT64_MAX;
2290       }
2291     }
2292   } /* for each label */
2293
2294   bsi->r = br;
2295   bsi->d = bd;
2296   bsi->segment_yrate = segmentyrate;
2297   bsi->segment_rd = this_segment_rd;
2298   bsi->sse = block_sse;
2299
2300   // update the coding decisions
2301   for (k = 0; k < 4; ++k) bsi->modes[k] = mi->bmi[k].as_mode;
2302
2303   if (bsi->segment_rd > best_rd) return INT64_MAX;
2304   /* set it to the best */
2305   for (i = 0; i < 4; i++) {
2306     mode_idx = INTER_OFFSET(bsi->modes[i]);
2307     mi->bmi[i].as_mv[0].as_int = bsi->rdstat[i][mode_idx].mvs[0].as_int;
2308     if (has_second_ref(mi))
2309       mi->bmi[i].as_mv[1].as_int = bsi->rdstat[i][mode_idx].mvs[1].as_int;
2310     x->plane[0].eobs[i] = bsi->rdstat[i][mode_idx].eobs;
2311     mi->bmi[i].as_mode = bsi->modes[i];
2312   }
2313
2314   /*
2315    * used to set mbmi->mv.as_int
2316    */
2317   *returntotrate = bsi->r;
2318   *returndistortion = bsi->d;
2319   *returnyrate = bsi->segment_yrate;
2320   *skippable = vp9_is_skippable_in_plane(x, BLOCK_8X8, 0);
2321   *psse = bsi->sse;
2322   mi->mode = bsi->modes[3];
2323
2324   return bsi->segment_rd;
2325 }
2326
2327 static void estimate_ref_frame_costs(const VP9_COMMON *cm,
2328                                      const MACROBLOCKD *xd, int segment_id,
2329                                      unsigned int *ref_costs_single,
2330                                      unsigned int *ref_costs_comp,
2331                                      vpx_prob *comp_mode_p) {
2332   int seg_ref_active =
2333       segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
2334   if (seg_ref_active) {
2335     memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single));
2336     memset(ref_costs_comp, 0, MAX_REF_FRAMES * sizeof(*ref_costs_comp));
2337     *comp_mode_p = 128;
2338   } else {
2339     vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
2340     vpx_prob comp_inter_p = 128;
2341
2342     if (cm->reference_mode == REFERENCE_MODE_SELECT) {
2343       comp_inter_p = vp9_get_reference_mode_prob(cm, xd);
2344       *comp_mode_p = comp_inter_p;
2345     } else {
2346       *comp_mode_p = 128;
2347     }
2348
2349     ref_costs_single[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
2350
2351     if (cm->reference_mode != COMPOUND_REFERENCE) {
2352       vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
2353       vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
2354       unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
2355
2356       if (cm->reference_mode == REFERENCE_MODE_SELECT)
2357         base_cost += vp9_cost_bit(comp_inter_p, 0);
2358
2359       ref_costs_single[LAST_FRAME] = ref_costs_single[GOLDEN_FRAME] =
2360           ref_costs_single[ALTREF_FRAME] = base_cost;
2361       ref_costs_single[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
2362       ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
2363       ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
2364       ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
2365       ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
2366     } else {
2367       ref_costs_single[LAST_FRAME] = 512;
2368       ref_costs_single[GOLDEN_FRAME] = 512;
2369       ref_costs_single[ALTREF_FRAME] = 512;
2370     }
2371     if (cm->reference_mode != SINGLE_REFERENCE) {
2372       vpx_prob ref_comp_p = vp9_get_pred_prob_comp_ref_p(cm, xd);
2373       unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
2374
2375       if (cm->reference_mode == REFERENCE_MODE_SELECT)
2376         base_cost += vp9_cost_bit(comp_inter_p, 1);
2377
2378       ref_costs_comp[LAST_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 0);
2379       ref_costs_comp[GOLDEN_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 1);
2380     } else {
2381       ref_costs_comp[LAST_FRAME] = 512;
2382       ref_costs_comp[GOLDEN_FRAME] = 512;
2383     }
2384   }
2385 }
2386
2387 static void store_coding_context(
2388     MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, int mode_index,
2389     int64_t comp_pred_diff[REFERENCE_MODES],
2390     int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS], int skippable) {
2391   MACROBLOCKD *const xd = &x->e_mbd;
2392
2393   // Take a snapshot of the coding context so it can be
2394   // restored if we decide to encode this way
2395   ctx->skip = x->skip;
2396   ctx->skippable = skippable;
2397   ctx->best_mode_index = mode_index;
2398   ctx->mic = *xd->mi[0];
2399   ctx->mbmi_ext = *x->mbmi_ext;
2400   ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE];
2401   ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE];
2402   ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT];
2403
2404   memcpy(ctx->best_filter_diff, best_filter_diff,
2405          sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS);
2406 }
2407
2408 static void setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x,
2409                                MV_REFERENCE_FRAME ref_frame,
2410                                BLOCK_SIZE block_size, int mi_row, int mi_col,
2411                                int_mv frame_nearest_mv[MAX_REF_FRAMES],
2412                                int_mv frame_near_mv[MAX_REF_FRAMES],
2413                                struct buf_2d yv12_mb[4][MAX_MB_PLANE]) {
2414   const VP9_COMMON *cm = &cpi->common;
2415   const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
2416   MACROBLOCKD *const xd = &x->e_mbd;
2417   MODE_INFO *const mi = xd->mi[0];
2418   int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
2419   const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
2420   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
2421
2422   assert(yv12 != NULL);
2423
2424   // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this
2425   // use the UV scaling factors.
2426   vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
2427
2428   // Gets an initial list of candidate vectors from neighbours and orders them
2429   vp9_find_mv_refs(cm, xd, mi, ref_frame, candidates, mi_row, mi_col,
2430                    mbmi_ext->mode_context);
2431
2432   // Candidate refinement carried out at encoder and decoder
2433   vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
2434                         &frame_nearest_mv[ref_frame],
2435                         &frame_near_mv[ref_frame]);
2436
2437   // Further refinement that is encode side only to test the top few candidates
2438   // in full and choose the best as the centre point for subsequent searches.
2439   // The current implementation doesn't support scaling.
2440   if (!vp9_is_scaled(sf) && block_size >= BLOCK_8X8)
2441     vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
2442                 block_size);
2443 }
2444
2445 #if CONFIG_NON_GREEDY_MV
2446 static int ref_frame_to_gf_rf_idx(int ref_frame) {
2447   if (ref_frame == GOLDEN_FRAME) {
2448     return 0;
2449   }
2450   if (ref_frame == LAST_FRAME) {
2451     return 1;
2452   }
2453   if (ref_frame == ALTREF_FRAME) {
2454     return 2;
2455   }
2456   assert(0);
2457   return -1;
2458 }
2459 #endif
2460
2461 static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
2462                                  int mi_row, int mi_col, int_mv *tmp_mv,
2463                                  int *rate_mv) {
2464   MACROBLOCKD *xd = &x->e_mbd;
2465   const VP9_COMMON *cm = &cpi->common;
2466   MODE_INFO *mi = xd->mi[0];
2467   struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } };
2468   int step_param;
2469   MV mvp_full;
2470   int ref = mi->ref_frame[0];
2471   MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
2472   const MvLimits tmp_mv_limits = x->mv_limits;
2473   int cost_list[5];
2474   const int best_predmv_idx = x->mv_best_ref_index[ref];
2475   const YV12_BUFFER_CONFIG *scaled_ref_frame =
2476       vp9_get_scaled_ref_frame(cpi, ref);
2477   const int pw = num_4x4_blocks_wide_lookup[bsize] << 2;
2478   const int ph = num_4x4_blocks_high_lookup[bsize] << 2;
2479   MV pred_mv[3];
2480
2481 #if CONFIG_NON_GREEDY_MV
2482   double mv_dist = 0;
2483   double mv_cost = 0;
2484   double lambda = (pw * ph) / 4;
2485   double bestsme;
2486   int_mv nb_full_mvs[NB_MVS_NUM];
2487   const int nb_full_mv_num = NB_MVS_NUM;
2488   int gf_group_idx = cpi->twopass.gf_group.index;
2489   int gf_rf_idx = ref_frame_to_gf_rf_idx(ref);
2490   BLOCK_SIZE square_bsize = get_square_block_size(bsize);
2491   vp9_prepare_nb_full_mvs(&cpi->tpl_stats[gf_group_idx], mi_row, mi_col,
2492                           gf_rf_idx, square_bsize, nb_full_mvs);
2493 #else   // CONFIG_NON_GREEDY_MV
2494   int bestsme = INT_MAX;
2495   int sadpb = x->sadperbit16;
2496 #endif  // CONFIG_NON_GREEDY_MV
2497
2498   pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv;
2499   pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv;
2500   pred_mv[2] = x->pred_mv[ref];
2501
2502   if (scaled_ref_frame) {
2503     int i;
2504     // Swap out the reference frame for a version that's been scaled to
2505     // match the resolution of the current frame, allowing the existing
2506     // motion search code to be used without additional modifications.
2507     for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
2508
2509     vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
2510   }
2511
2512   // Work out the size of the first step in the mv step search.
2513   // 0 here is maximum length first step. 1 is VPXMAX >> 1 etc.
2514   if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
2515     // Take wtd average of the step_params based on the last frame's
2516     // max mv magnitude and that based on the best ref mvs of the current
2517     // block for the given reference.
2518     step_param =
2519         (vp9_init_search_range(x->max_mv_context[ref]) + cpi->mv_step_param) /
2520         2;
2521   } else {
2522     step_param = cpi->mv_step_param;
2523   }
2524
2525   if (cpi->sf.adaptive_motion_search && bsize < BLOCK_64X64) {
2526     const int boffset =
2527         2 * (b_width_log2_lookup[BLOCK_64X64] -
2528              VPXMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize]));
2529     step_param = VPXMAX(step_param, boffset);
2530   }
2531
2532   if (cpi->sf.adaptive_motion_search) {
2533     int bwl = b_width_log2_lookup[bsize];
2534     int bhl = b_height_log2_lookup[bsize];
2535     int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4);
2536
2537     if (tlevel < 5) step_param += 2;
2538
2539     // prev_mv_sad is not setup for dynamically scaled frames.
2540     if (cpi->oxcf.resize_mode != RESIZE_DYNAMIC) {
2541       int i;
2542       for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) {
2543         if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
2544           x->pred_mv[ref].row = INT16_MAX;
2545           x->pred_mv[ref].col = INT16_MAX;
2546           tmp_mv->as_int = INVALID_MV;
2547
2548           if (scaled_ref_frame) {
2549             int i;
2550             for (i = 0; i < MAX_MB_PLANE; ++i)
2551               xd->plane[i].pre[0] = backup_yv12[i];
2552           }
2553           return;
2554         }
2555       }
2556     }
2557   }
2558
2559   // Note: MV limits are modified here. Always restore the original values
2560   // after full-pixel motion search.
2561   vp9_set_mv_search_range(&x->mv_limits, &ref_mv);
2562
2563   mvp_full = pred_mv[best_predmv_idx];
2564   mvp_full.col >>= 3;
2565   mvp_full.row >>= 3;
2566
2567 #if CONFIG_NON_GREEDY_MV
2568   bestsme = vp9_full_pixel_diamond_new(
2569       cpi, x, &mvp_full, step_param, lambda, 1, &cpi->fn_ptr[bsize],
2570       nb_full_mvs, nb_full_mv_num, &tmp_mv->as_mv, &mv_dist, &mv_cost);
2571 #else   // CONFIG_NON_GREEDY_MV
2572   bestsme = vp9_full_pixel_search(
2573       cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, sadpb,
2574       cond_cost_list(cpi, cost_list), &ref_mv, &tmp_mv->as_mv, INT_MAX, 1);
2575 #endif  // CONFIG_NON_GREEDY_MV
2576
2577   if (cpi->sf.enhanced_full_pixel_motion_search) {
2578     int i;
2579     for (i = 0; i < 3; ++i) {
2580 #if CONFIG_NON_GREEDY_MV
2581       double this_me;
2582 #else   // CONFIG_NON_GREEDY_MV
2583       int this_me;
2584 #endif  // CONFIG_NON_GREEDY_MV
2585       MV this_mv;
2586       int diff_row;
2587       int diff_col;
2588       int step;
2589
2590       if (pred_mv[i].row == INT16_MAX || pred_mv[i].col == INT16_MAX) continue;
2591       if (i == best_predmv_idx) continue;
2592
2593       diff_row = ((int)pred_mv[i].row -
2594                   pred_mv[i > 0 ? (i - 1) : best_predmv_idx].row) >>
2595                  3;
2596       diff_col = ((int)pred_mv[i].col -
2597                   pred_mv[i > 0 ? (i - 1) : best_predmv_idx].col) >>
2598                  3;
2599       if (diff_row == 0 && diff_col == 0) continue;
2600       if (diff_row < 0) diff_row = -diff_row;
2601       if (diff_col < 0) diff_col = -diff_col;
2602       step = get_msb((diff_row + diff_col + 1) >> 1);
2603       if (step <= 0) continue;
2604
2605       mvp_full = pred_mv[i];
2606       mvp_full.col >>= 3;
2607       mvp_full.row >>= 3;
2608 #if CONFIG_NON_GREEDY_MV
2609       this_me = vp9_full_pixel_diamond_new(
2610           cpi, x, &mvp_full, VPXMAX(step_param, MAX_MVSEARCH_STEPS - step),
2611           lambda, 1, &cpi->fn_ptr[bsize], nb_full_mvs, nb_full_mv_num, &this_mv,
2612           &mv_dist, &mv_cost);
2613 #else   // CONFIG_NON_GREEDY_MV
2614       this_me = vp9_full_pixel_search(
2615           cpi, x, bsize, &mvp_full,
2616           VPXMAX(step_param, MAX_MVSEARCH_STEPS - step),
2617           cpi->sf.mv.search_method, sadpb, cond_cost_list(cpi, cost_list),
2618           &ref_mv, &this_mv, INT_MAX, 1);
2619 #endif  // CONFIG_NON_GREEDY_MV
2620       if (this_me < bestsme) {
2621         tmp_mv->as_mv = this_mv;
2622         bestsme = this_me;
2623       }
2624     }
2625   }
2626
2627   x->mv_limits = tmp_mv_limits;
2628
2629   if (bestsme < INT_MAX) {
2630     uint32_t dis; /* TODO: use dis in distortion calculation later. */
2631     cpi->find_fractional_mv_step(
2632         x, &tmp_mv->as_mv, &ref_mv, cm->allow_high_precision_mv, x->errorperbit,
2633         &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
2634         cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
2635         x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, pw, ph,
2636         cpi->sf.use_accurate_subpel_search);
2637   }
2638   *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmvjointcost,
2639                              x->mvcost, MV_COST_WEIGHT);
2640
2641   x->pred_mv[ref] = tmp_mv->as_mv;
2642
2643   if (scaled_ref_frame) {
2644     int i;
2645     for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
2646   }
2647 }
2648
2649 static INLINE void restore_dst_buf(MACROBLOCKD *xd,
2650                                    uint8_t *orig_dst[MAX_MB_PLANE],
2651                                    int orig_dst_stride[MAX_MB_PLANE]) {
2652   int i;
2653   for (i = 0; i < MAX_MB_PLANE; i++) {
2654     xd->plane[i].dst.buf = orig_dst[i];
2655     xd->plane[i].dst.stride = orig_dst_stride[i];
2656   }
2657 }
2658
2659 // In some situations we want to discount tha pparent cost of a new motion
2660 // vector. Where there is a subtle motion field and especially where there is
2661 // low spatial complexity then it can be hard to cover the cost of a new motion
2662 // vector in a single block, even if that motion vector reduces distortion.
2663 // However, once established that vector may be usable through the nearest and
2664 // near mv modes to reduce distortion in subsequent blocks and also improve
2665 // visual quality.
2666 static int discount_newmv_test(const VP9_COMP *cpi, int this_mode,
2667                                int_mv this_mv,
2668                                int_mv (*mode_mv)[MAX_REF_FRAMES], int ref_frame,
2669                                int mi_row, int mi_col, BLOCK_SIZE bsize) {
2670 #if CONFIG_NON_GREEDY_MV
2671   (void)mode_mv;
2672   (void)this_mv;
2673   if (this_mode == NEWMV && bsize >= BLOCK_8X8 && cpi->tpl_ready) {
2674     const int gf_group_idx = cpi->twopass.gf_group.index;
2675     const int gf_rf_idx = ref_frame_to_gf_rf_idx(ref_frame);
2676     const TplDepFrame tpl_frame = cpi->tpl_stats[gf_group_idx];
2677     const int tpl_block_mi_h = num_8x8_blocks_high_lookup[cpi->tpl_bsize];
2678     const int tpl_block_mi_w = num_8x8_blocks_wide_lookup[cpi->tpl_bsize];
2679     const int tpl_mi_row = mi_row - (mi_row % tpl_block_mi_h);
2680     const int tpl_mi_col = mi_col - (mi_col % tpl_block_mi_w);
2681     const int mv_mode =
2682         tpl_frame
2683             .mv_mode_arr[gf_rf_idx][tpl_mi_row * tpl_frame.stride + tpl_mi_col];
2684     if (mv_mode == NEW_MV_MODE) {
2685       int_mv tpl_new_mv = *get_pyramid_mv(&tpl_frame, gf_rf_idx, cpi->tpl_bsize,
2686                                           tpl_mi_row, tpl_mi_col);
2687       int row_diff = abs(tpl_new_mv.as_mv.row - this_mv.as_mv.row);
2688       int col_diff = abs(tpl_new_mv.as_mv.col - this_mv.as_mv.col);
2689       if (VPXMAX(row_diff, col_diff) <= 8) {
2690         return 1;
2691       } else {
2692         return 0;
2693       }
2694     } else {
2695       return 0;
2696     }
2697   } else {
2698     return 0;
2699   }
2700 #else
2701   (void)mi_row;
2702   (void)mi_col;
2703   (void)bsize;
2704   return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) &&
2705           (this_mv.as_int != 0) &&
2706           ((mode_mv[NEARESTMV][ref_frame].as_int == 0) ||
2707            (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) &&
2708           ((mode_mv[NEARMV][ref_frame].as_int == 0) ||
2709            (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV)));
2710 #endif
2711 }
2712
2713 static int64_t handle_inter_mode(
2714     VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int *rate2,
2715     int64_t *distortion, int *skippable, int *rate_y, int *rate_uv,
2716     struct buf_2d *recon, int *disable_skip, int_mv (*mode_mv)[MAX_REF_FRAMES],
2717     int mi_row, int mi_col, int_mv single_newmv[MAX_REF_FRAMES],
2718     INTERP_FILTER (*single_filter)[MAX_REF_FRAMES],
2719     int (*single_skippable)[MAX_REF_FRAMES], int64_t *psse,
2720     const int64_t ref_best_rd, int64_t *mask_filter, int64_t filter_cache[]) {
2721   VP9_COMMON *cm = &cpi->common;
2722   MACROBLOCKD *xd = &x->e_mbd;
2723   MODE_INFO *mi = xd->mi[0];
2724   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
2725   const int is_comp_pred = has_second_ref(mi);
2726   const int this_mode = mi->mode;
2727   int_mv *frame_mv = mode_mv[this_mode];
2728   int i;
2729   int refs[2] = { mi->ref_frame[0],
2730                   (mi->ref_frame[1] < 0 ? 0 : mi->ref_frame[1]) };
2731   int_mv cur_mv[2];
2732 #if CONFIG_VP9_HIGHBITDEPTH
2733   DECLARE_ALIGNED(16, uint16_t, tmp_buf16[MAX_MB_PLANE * 64 * 64]);
2734   uint8_t *tmp_buf;
2735 #else
2736   DECLARE_ALIGNED(16, uint8_t, tmp_buf[MAX_MB_PLANE * 64 * 64]);
2737 #endif  // CONFIG_VP9_HIGHBITDEPTH
2738   int pred_exists = 0;
2739   int intpel_mv;
2740   int64_t rd, tmp_rd, best_rd = INT64_MAX;
2741   int best_needs_copy = 0;
2742   uint8_t *orig_dst[MAX_MB_PLANE];
2743   int orig_dst_stride[MAX_MB_PLANE];
2744   int rs = 0;
2745   INTERP_FILTER best_filter = SWITCHABLE;
2746   uint8_t skip_txfm[MAX_MB_PLANE << 2] = { 0 };
2747   int64_t bsse[MAX_MB_PLANE << 2] = { 0 };
2748
2749   int bsl = mi_width_log2_lookup[bsize];
2750   int pred_filter_search =
2751       cpi->sf.cb_pred_filter_search
2752           ? (((mi_row + mi_col) >> bsl) +
2753              get_chessboard_index(cm->current_video_frame)) &
2754                 0x1
2755           : 0;
2756
2757   int skip_txfm_sb = 0;
2758   int64_t skip_sse_sb = INT64_MAX;
2759   int64_t distortion_y = 0, distortion_uv = 0;
2760
2761 #if CONFIG_VP9_HIGHBITDEPTH
2762   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
2763     tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf16);
2764   } else {
2765     tmp_buf = (uint8_t *)tmp_buf16;
2766   }
2767 #endif  // CONFIG_VP9_HIGHBITDEPTH
2768
2769   if (pred_filter_search) {
2770     INTERP_FILTER af = SWITCHABLE, lf = SWITCHABLE;
2771     if (xd->above_mi && is_inter_block(xd->above_mi))
2772       af = xd->above_mi->interp_filter;
2773     if (xd->left_mi && is_inter_block(xd->left_mi))
2774       lf = xd->left_mi->interp_filter;
2775
2776     if ((this_mode != NEWMV) || (af == lf)) best_filter = af;
2777   }
2778
2779   if (is_comp_pred) {
2780     if (frame_mv[refs[0]].as_int == INVALID_MV ||
2781         frame_mv[refs[1]].as_int == INVALID_MV)
2782       return INT64_MAX;
2783
2784     if (cpi->sf.adaptive_mode_search) {
2785       if (single_filter[this_mode][refs[0]] ==
2786           single_filter[this_mode][refs[1]])
2787         best_filter = single_filter[this_mode][refs[0]];
2788     }
2789   }
2790
2791   if (this_mode == NEWMV) {
2792     int rate_mv;
2793     if (is_comp_pred) {
2794       // Initialize mv using single prediction mode result.
2795       frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
2796       frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
2797
2798       if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
2799         joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col,
2800                             single_newmv, &rate_mv);
2801       } else {
2802         rate_mv = vp9_mv_bit_cost(&frame_mv[refs[0]].as_mv,
2803                                   &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
2804                                   x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2805         rate_mv += vp9_mv_bit_cost(&frame_mv[refs[1]].as_mv,
2806                                    &x->mbmi_ext->ref_mvs[refs[1]][0].as_mv,
2807                                    x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2808       }
2809       *rate2 += rate_mv;
2810     } else {
2811       int_mv tmp_mv;
2812       single_motion_search(cpi, x, bsize, mi_row, mi_col, &tmp_mv, &rate_mv);
2813       if (tmp_mv.as_int == INVALID_MV) return INT64_MAX;
2814
2815       frame_mv[refs[0]].as_int = xd->mi[0]->bmi[0].as_mv[0].as_int =
2816           tmp_mv.as_int;
2817       single_newmv[refs[0]].as_int = tmp_mv.as_int;
2818
2819       // Estimate the rate implications of a new mv but discount this
2820       // under certain circumstances where we want to help initiate a weak
2821       // motion field, where the distortion gain for a single block may not
2822       // be enough to overcome the cost of a new mv.
2823       if (discount_newmv_test(cpi, this_mode, tmp_mv, mode_mv, refs[0], mi_row,
2824                               mi_col, bsize)) {
2825         *rate2 += VPXMAX((rate_mv / NEW_MV_DISCOUNT_FACTOR), 1);
2826       } else {
2827         *rate2 += rate_mv;
2828       }
2829     }
2830   }
2831
2832   for (i = 0; i < is_comp_pred + 1; ++i) {
2833     cur_mv[i] = frame_mv[refs[i]];
2834     // Clip "next_nearest" so that it does not extend to far out of image
2835     if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd);
2836
2837     if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX;
2838     mi->mv[i].as_int = cur_mv[i].as_int;
2839   }
2840
2841   // do first prediction into the destination buffer. Do the next
2842   // prediction into a temporary buffer. Then keep track of which one
2843   // of these currently holds the best predictor, and use the other
2844   // one for future predictions. In the end, copy from tmp_buf to
2845   // dst if necessary.
2846   for (i = 0; i < MAX_MB_PLANE; i++) {
2847     orig_dst[i] = xd->plane[i].dst.buf;
2848     orig_dst_stride[i] = xd->plane[i].dst.stride;
2849   }
2850
2851   // We don't include the cost of the second reference here, because there
2852   // are only two options: Last/ARF or Golden/ARF; The second one is always
2853   // known, which is ARF.
2854   //
2855   // Under some circumstances we discount the cost of new mv mode to encourage
2856   // initiation of a motion field.
2857   if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, refs[0],
2858                           mi_row, mi_col, bsize)) {
2859     *rate2 +=
2860         VPXMIN(cost_mv_ref(cpi, this_mode, mbmi_ext->mode_context[refs[0]]),
2861                cost_mv_ref(cpi, NEARESTMV, mbmi_ext->mode_context[refs[0]]));
2862   } else {
2863     *rate2 += cost_mv_ref(cpi, this_mode, mbmi_ext->mode_context[refs[0]]);
2864   }
2865
2866   if (RDCOST(x->rdmult, x->rddiv, *rate2, 0) > ref_best_rd &&
2867       mi->mode != NEARESTMV)
2868     return INT64_MAX;
2869
2870   pred_exists = 0;
2871   // Are all MVs integer pel for Y and UV
2872   intpel_mv = !mv_has_subpel(&mi->mv[0].as_mv);
2873   if (is_comp_pred) intpel_mv &= !mv_has_subpel(&mi->mv[1].as_mv);
2874
2875   // Search for best switchable filter by checking the variance of
2876   // pred error irrespective of whether the filter will be used
2877   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
2878
2879   if (cm->interp_filter != BILINEAR) {
2880     if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) {
2881       best_filter = EIGHTTAP;
2882     } else if (best_filter == SWITCHABLE) {
2883       int newbest;
2884       int tmp_rate_sum = 0;
2885       int64_t tmp_dist_sum = 0;
2886
2887       for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
2888         int j;
2889         int64_t rs_rd;
2890         int tmp_skip_sb = 0;
2891         int64_t tmp_skip_sse = INT64_MAX;
2892
2893         mi->interp_filter = i;
2894         rs = vp9_get_switchable_rate(cpi, xd);
2895         rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
2896
2897         if (i > 0 && intpel_mv) {
2898           rd = RDCOST(x->rdmult, x->rddiv, tmp_rate_sum, tmp_dist_sum);
2899           filter_cache[i] = rd;
2900           filter_cache[SWITCHABLE_FILTERS] =
2901               VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
2902           if (cm->interp_filter == SWITCHABLE) rd += rs_rd;
2903           *mask_filter = VPXMAX(*mask_filter, rd);
2904         } else {
2905           int rate_sum = 0;
2906           int64_t dist_sum = 0;
2907           if (i > 0 && cpi->sf.adaptive_interp_filter_search &&
2908               (cpi->sf.interp_filter_search_mask & (1 << i))) {
2909             rate_sum = INT_MAX;
2910             dist_sum = INT64_MAX;
2911             continue;
2912           }
2913
2914           if ((cm->interp_filter == SWITCHABLE && (!i || best_needs_copy)) ||
2915               (cm->interp_filter != SWITCHABLE &&
2916                (cm->interp_filter == mi->interp_filter ||
2917                 (i == 0 && intpel_mv)))) {
2918             restore_dst_buf(xd, orig_dst, orig_dst_stride);
2919           } else {
2920             for (j = 0; j < MAX_MB_PLANE; j++) {
2921               xd->plane[j].dst.buf = tmp_buf + j * 64 * 64;
2922               xd->plane[j].dst.stride = 64;
2923             }
2924           }
2925           vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
2926           model_rd_for_sb(cpi, bsize, x, xd, &rate_sum, &dist_sum, &tmp_skip_sb,
2927                           &tmp_skip_sse);
2928
2929           rd = RDCOST(x->rdmult, x->rddiv, rate_sum, dist_sum);
2930           filter_cache[i] = rd;
2931           filter_cache[SWITCHABLE_FILTERS] =
2932               VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
2933           if (cm->interp_filter == SWITCHABLE) rd += rs_rd;
2934           *mask_filter = VPXMAX(*mask_filter, rd);
2935
2936           if (i == 0 && intpel_mv) {
2937             tmp_rate_sum = rate_sum;
2938             tmp_dist_sum = dist_sum;
2939           }
2940         }
2941
2942         if (i == 0 && cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
2943           if (rd / 2 > ref_best_rd) {
2944             restore_dst_buf(xd, orig_dst, orig_dst_stride);
2945             return INT64_MAX;
2946           }
2947         }
2948         newbest = i == 0 || rd < best_rd;
2949
2950         if (newbest) {
2951           best_rd = rd;
2952           best_filter = mi->interp_filter;
2953           if (cm->interp_filter == SWITCHABLE && i && !intpel_mv)
2954             best_needs_copy = !best_needs_copy;
2955         }
2956
2957         if ((cm->interp_filter == SWITCHABLE && newbest) ||
2958             (cm->interp_filter != SWITCHABLE &&
2959              cm->interp_filter == mi->interp_filter)) {
2960           pred_exists = 1;
2961           tmp_rd = best_rd;
2962
2963           skip_txfm_sb = tmp_skip_sb;
2964           skip_sse_sb = tmp_skip_sse;
2965           memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
2966           memcpy(bsse, x->bsse, sizeof(bsse));
2967         }
2968       }
2969       restore_dst_buf(xd, orig_dst, orig_dst_stride);
2970     }
2971   }
2972   // Set the appropriate filter
2973   mi->interp_filter =
2974       cm->interp_filter != SWITCHABLE ? cm->interp_filter : best_filter;
2975   rs = cm->interp_filter == SWITCHABLE ? vp9_get_switchable_rate(cpi, xd) : 0;
2976
2977   if (pred_exists) {
2978     if (best_needs_copy) {
2979       // again temporarily set the buffers to local memory to prevent a memcpy
2980       for (i = 0; i < MAX_MB_PLANE; i++) {
2981         xd->plane[i].dst.buf = tmp_buf + i * 64 * 64;
2982         xd->plane[i].dst.stride = 64;
2983       }
2984     }
2985     rd = tmp_rd + RDCOST(x->rdmult, x->rddiv, rs, 0);
2986   } else {
2987     int tmp_rate;
2988     int64_t tmp_dist;
2989     // Handles the special case when a filter that is not in the
2990     // switchable list (ex. bilinear) is indicated at the frame level, or
2991     // skip condition holds.
2992     vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
2993     model_rd_for_sb(cpi, bsize, x, xd, &tmp_rate, &tmp_dist, &skip_txfm_sb,
2994                     &skip_sse_sb);
2995     rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist);
2996     memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
2997     memcpy(bsse, x->bsse, sizeof(bsse));
2998   }
2999
3000   if (!is_comp_pred) single_filter[this_mode][refs[0]] = mi->interp_filter;
3001
3002   if (cpi->sf.adaptive_mode_search)
3003     if (is_comp_pred)
3004       if (single_skippable[this_mode][refs[0]] &&
3005           single_skippable[this_mode][refs[1]])
3006         memset(skip_txfm, SKIP_TXFM_AC_DC, sizeof(skip_txfm));
3007
3008   if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
3009     // if current pred_error modeled rd is substantially more than the best
3010     // so far, do not bother doing full rd
3011     if (rd / 2 > ref_best_rd) {
3012       restore_dst_buf(xd, orig_dst, orig_dst_stride);
3013       return INT64_MAX;
3014     }
3015   }
3016
3017   if (cm->interp_filter == SWITCHABLE) *rate2 += rs;
3018
3019   memcpy(x->skip_txfm, skip_txfm, sizeof(skip_txfm));
3020   memcpy(x->bsse, bsse, sizeof(bsse));
3021
3022   if (!skip_txfm_sb || xd->lossless) {
3023     int skippable_y, skippable_uv;
3024     int64_t sseuv = INT64_MAX;
3025     int64_t rdcosty = INT64_MAX;
3026
3027     // Y cost and distortion
3028     vp9_subtract_plane(x, bsize, 0);
3029     super_block_yrd(cpi, x, rate_y, &distortion_y, &skippable_y, psse, bsize,
3030                     ref_best_rd, recon);
3031
3032     if (*rate_y == INT_MAX) {
3033       *rate2 = INT_MAX;
3034       *distortion = INT64_MAX;
3035       restore_dst_buf(xd, orig_dst, orig_dst_stride);
3036       return INT64_MAX;
3037     }
3038
3039     *rate2 += *rate_y;
3040     *distortion += distortion_y;
3041
3042     rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion);
3043     rdcosty = VPXMIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse));
3044
3045     if (!super_block_uvrd(cpi, x, rate_uv, &distortion_uv, &skippable_uv,
3046                           &sseuv, bsize, ref_best_rd - rdcosty)) {
3047       *rate2 = INT_MAX;
3048       *distortion = INT64_MAX;
3049       restore_dst_buf(xd, orig_dst, orig_dst_stride);
3050       return INT64_MAX;
3051     }
3052
3053     *psse += sseuv;
3054     *rate2 += *rate_uv;
3055     *distortion += distortion_uv;
3056     *skippable = skippable_y && skippable_uv;
3057   } else {
3058     x->skip = 1;
3059     *disable_skip = 1;
3060
3061     // The cost of skip bit needs to be added.
3062     *rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
3063
3064     *distortion = skip_sse_sb;
3065   }
3066
3067   if (!is_comp_pred) single_skippable[this_mode][refs[0]] = *skippable;
3068
3069   restore_dst_buf(xd, orig_dst, orig_dst_stride);
3070   return 0;  // The rate-distortion cost will be re-calculated by caller.
3071 }
3072
3073 void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
3074                                BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
3075                                int64_t best_rd) {
3076   VP9_COMMON *const cm = &cpi->common;
3077   MACROBLOCKD *const xd = &x->e_mbd;
3078   struct macroblockd_plane *const pd = xd->plane;
3079   int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0;
3080   int y_skip = 0, uv_skip = 0;
3081   int64_t dist_y = 0, dist_uv = 0;
3082   TX_SIZE max_uv_tx_size;
3083   x->skip_encode = 0;
3084   ctx->skip = 0;
3085   xd->mi[0]->ref_frame[0] = INTRA_FRAME;
3086   xd->mi[0]->ref_frame[1] = NONE;
3087   // Initialize interp_filter here so we do not have to check for inter block
3088   // modes in get_pred_context_switchable_interp()
3089   xd->mi[0]->interp_filter = SWITCHABLE_FILTERS;
3090
3091   if (bsize >= BLOCK_8X8) {
3092     if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y,
3093                                &y_skip, bsize, best_rd) >= best_rd) {
3094       rd_cost->rate = INT_MAX;
3095       return;
3096     }
3097   } else {
3098     y_skip = 0;
3099     if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly,
3100                                      &dist_y, best_rd) >= best_rd) {
3101       rd_cost->rate = INT_MAX;
3102       return;
3103     }
3104   }
3105   max_uv_tx_size = uv_txsize_lookup[bsize][xd->mi[0]->tx_size]
3106                                    [pd[1].subsampling_x][pd[1].subsampling_y];
3107   rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, &dist_uv,
3108                           &uv_skip, VPXMAX(BLOCK_8X8, bsize), max_uv_tx_size);
3109
3110   if (y_skip && uv_skip) {
3111     rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly +
3112                     vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
3113     rd_cost->dist = dist_y + dist_uv;
3114   } else {
3115     rd_cost->rate =
3116         rate_y + rate_uv + vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
3117     rd_cost->dist = dist_y + dist_uv;
3118   }
3119
3120   ctx->mic = *xd->mi[0];
3121   ctx->mbmi_ext = *x->mbmi_ext;
3122   rd_cost->rdcost = RDCOST(x->rdmult, x->rddiv, rd_cost->rate, rd_cost->dist);
3123 }
3124
3125 // This function is designed to apply a bias or adjustment to an rd value based
3126 // on the relative variance of the source and reconstruction.
3127 #define LOW_VAR_THRESH 250
3128 #define VAR_MULT 250
3129 static unsigned int max_var_adjust[VP9E_CONTENT_INVALID] = { 16, 16, 250 };
3130
3131 static void rd_variance_adjustment(VP9_COMP *cpi, MACROBLOCK *x,
3132                                    BLOCK_SIZE bsize, int64_t *this_rd,
3133                                    struct buf_2d *recon,
3134                                    MV_REFERENCE_FRAME ref_frame) {
3135   MACROBLOCKD *const xd = &x->e_mbd;
3136   unsigned int rec_variance;
3137   unsigned int src_variance;
3138   unsigned int src_rec_min;
3139   unsigned int absvar_diff = 0;
3140   unsigned int var_factor = 0;
3141   unsigned int adj_max;
3142   const int bw = num_8x8_blocks_wide_lookup[bsize];
3143   const int bh = num_8x8_blocks_high_lookup[bsize];
3144   vp9e_tune_content content_type = cpi->oxcf.content;
3145
3146   if (*this_rd == INT64_MAX) return;
3147
3148 #if CONFIG_VP9_HIGHBITDEPTH
3149   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
3150     rec_variance = vp9_high_get_sby_variance(cpi, recon, bsize, xd->bd);
3151     src_variance =
3152         vp9_high_get_sby_variance(cpi, &x->plane[0].src, bsize, xd->bd);
3153   } else {
3154     rec_variance = vp9_get_sby_variance(cpi, recon, bsize);
3155     src_variance = vp9_get_sby_variance(cpi, &x->plane[0].src, bsize);
3156   }
3157 #else
3158   rec_variance = vp9_get_sby_variance(cpi, recon, bsize);
3159   src_variance = vp9_get_sby_variance(cpi, &x->plane[0].src, bsize);
3160 #endif  // CONFIG_VP9_HIGHBITDEPTH
3161
3162   // Scale based on area in 8x8 blocks
3163   rec_variance /= (bw * bh);
3164   src_variance /= (bw * bh);
3165
3166   // Lower of source (raw per pixel value) and recon variance. Note that
3167   // if the source per pixel is 0 then the recon value here will not be per
3168   // pixel (see above) so will likely be much larger.
3169   src_rec_min = VPXMIN(src_variance, rec_variance);
3170
3171   if (src_rec_min > LOW_VAR_THRESH) return;
3172
3173   absvar_diff = (src_variance > rec_variance) ? (src_variance - rec_variance)
3174                                               : (rec_variance - src_variance);
3175
3176   adj_max = max_var_adjust[content_type];
3177
3178   var_factor =
3179       (unsigned int)((int64_t)VAR_MULT * absvar_diff) / VPXMAX(1, src_variance);
3180   var_factor = VPXMIN(adj_max, var_factor);
3181
3182   *this_rd += (*this_rd * var_factor) / 100;
3183
3184   if (content_type == VP9E_CONTENT_FILM) {
3185     if (src_rec_min <= LOW_VAR_THRESH / 2) {
3186       if (ref_frame == INTRA_FRAME) *this_rd *= 2;
3187       if (bsize > BLOCK_16X16) *this_rd *= 2;
3188     }
3189   }
3190
3191   (void)xd;
3192 }
3193
3194 // Do we have an internal image edge (e.g. formatting bars).
3195 int vp9_internal_image_edge(VP9_COMP *cpi) {
3196   return (cpi->oxcf.pass == 2) &&
3197          ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) ||
3198           (cpi->twopass.this_frame_stats.inactive_zone_cols > 0));
3199 }
3200
3201 // Checks to see if a super block is on a horizontal image edge.
3202 // In most cases this is the "real" edge unless there are formatting
3203 // bars embedded in the stream.
3204 int vp9_active_h_edge(VP9_COMP *cpi, int mi_row, int mi_step) {
3205   int top_edge = 0;
3206   int bottom_edge = cpi->common.mi_rows;
3207   int is_active_h_edge = 0;
3208
3209   // For two pass account for any formatting bars detected.
3210   if (cpi->oxcf.pass == 2) {
3211     TWO_PASS *twopass = &cpi->twopass;
3212
3213     // The inactive region is specified in MBs not mi units.
3214     // The image edge is in the following MB row.
3215     top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
3216
3217     bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
3218     bottom_edge = VPXMAX(top_edge, bottom_edge);
3219   }
3220
3221   if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
3222       ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
3223     is_active_h_edge = 1;
3224   }
3225   return is_active_h_edge;
3226 }
3227
3228 // Checks to see if a super block is on a vertical image edge.
3229 // In most cases this is the "real" edge unless there are formatting
3230 // bars embedded in the stream.
3231 int vp9_active_v_edge(VP9_COMP *cpi, int mi_col, int mi_step) {
3232   int left_edge = 0;
3233   int right_edge = cpi->common.mi_cols;
3234   int is_active_v_edge = 0;
3235
3236   // For two pass account for any formatting bars detected.
3237   if (cpi->oxcf.pass == 2) {
3238     TWO_PASS *twopass = &cpi->twopass;
3239
3240     // The inactive region is specified in MBs not mi units.
3241     // The image edge is in the following MB row.
3242     left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
3243
3244     right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
3245     right_edge = VPXMAX(left_edge, right_edge);
3246   }
3247
3248   if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
3249       ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
3250     is_active_v_edge = 1;
3251   }
3252   return is_active_v_edge;
3253 }
3254
3255 // Checks to see if a super block is at the edge of the active image.
3256 // In most cases this is the "real" edge unless there are formatting
3257 // bars embedded in the stream.
3258 int vp9_active_edge_sb(VP9_COMP *cpi, int mi_row, int mi_col) {
3259   return vp9_active_h_edge(cpi, mi_row, MI_BLOCK_SIZE) ||
3260          vp9_active_v_edge(cpi, mi_col, MI_BLOCK_SIZE);
3261 }
3262
3263 void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, TileDataEnc *tile_data,
3264                                MACROBLOCK *x, int mi_row, int mi_col,
3265                                RD_COST *rd_cost, BLOCK_SIZE bsize,
3266                                PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far) {
3267   VP9_COMMON *const cm = &cpi->common;
3268   TileInfo *const tile_info = &tile_data->tile_info;
3269   RD_OPT *const rd_opt = &cpi->rd;
3270   SPEED_FEATURES *const sf = &cpi->sf;
3271   MACROBLOCKD *const xd = &x->e_mbd;
3272   MODE_INFO *const mi = xd->mi[0];
3273   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
3274   const struct segmentation *const seg = &cm->seg;
3275   PREDICTION_MODE this_mode;
3276   MV_REFERENCE_FRAME ref_frame, second_ref_frame;
3277   unsigned char segment_id = mi->segment_id;
3278   int comp_pred, i, k;
3279   int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
3280   struct buf_2d yv12_mb[4][MAX_MB_PLANE];
3281   int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } };
3282   INTERP_FILTER single_inter_filter[MB_MODE_COUNT][MAX_REF_FRAMES];
3283   int single_skippable[MB_MODE_COUNT][MAX_REF_FRAMES];
3284   static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
3285                                     VP9_ALT_FLAG };
3286   int64_t best_rd = best_rd_so_far;
3287   int64_t best_pred_diff[REFERENCE_MODES];
3288   int64_t best_pred_rd[REFERENCE_MODES];
3289   int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
3290   int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
3291   MODE_INFO best_mbmode;
3292   int best_mode_skippable = 0;
3293   int midx, best_mode_index = -1;
3294   unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
3295   vpx_prob comp_mode_p;
3296   int64_t best_intra_rd = INT64_MAX;
3297   unsigned int best_pred_sse = UINT_MAX;
3298   PREDICTION_MODE best_intra_mode = DC_PRED;
3299   int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES];
3300   int64_t dist_uv[TX_SIZES];
3301   int skip_uv[TX_SIZES];
3302   PREDICTION_MODE mode_uv[TX_SIZES];
3303   const int intra_cost_penalty =
3304       vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
3305   int best_skip2 = 0;
3306   uint8_t ref_frame_skip_mask[2] = { 0, 1 };
3307   uint16_t mode_skip_mask[MAX_REF_FRAMES] = { 0 };
3308   int mode_skip_start = sf->mode_skip_start + 1;
3309   const int *const rd_threshes = rd_opt->threshes[segment_id][bsize];
3310   const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
3311   int64_t mode_threshold[MAX_MODES];
3312   int8_t *tile_mode_map = tile_data->mode_map[bsize];
3313   int8_t mode_map[MAX_MODES];  // Maintain mode_map information locally to avoid
3314                                // lock mechanism involved with reads from
3315                                // tile_mode_map
3316   const int mode_search_skip_flags = sf->mode_search_skip_flags;
3317   const int is_rect_partition =
3318       num_4x4_blocks_wide_lookup[bsize] != num_4x4_blocks_high_lookup[bsize];
3319   int64_t mask_filter = 0;
3320   int64_t filter_cache[SWITCHABLE_FILTER_CONTEXTS];
3321
3322   struct buf_2d *recon;
3323   struct buf_2d recon_buf;
3324 #if CONFIG_VP9_HIGHBITDEPTH
3325   DECLARE_ALIGNED(16, uint16_t, recon16[64 * 64]);
3326   recon_buf.buf = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH
3327                       ? CONVERT_TO_BYTEPTR(recon16)
3328                       : (uint8_t *)recon16;
3329 #else
3330   DECLARE_ALIGNED(16, uint8_t, recon8[64 * 64]);
3331   recon_buf.buf = recon8;
3332 #endif  // CONFIG_VP9_HIGHBITDEPTH
3333   recon_buf.stride = 64;
3334   recon = cpi->oxcf.content == VP9E_CONTENT_FILM ? &recon_buf : 0;
3335
3336   vp9_zero(best_mbmode);
3337
3338   x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
3339
3340   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
3341
3342   estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
3343                            &comp_mode_p);
3344
3345   for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX;
3346   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
3347     best_filter_rd[i] = INT64_MAX;
3348   for (i = 0; i < TX_SIZES; i++) rate_uv_intra[i] = INT_MAX;
3349   for (i = 0; i < MAX_REF_FRAMES; ++i) x->pred_sse[i] = INT_MAX;
3350   for (i = 0; i < MB_MODE_COUNT; ++i) {
3351     for (k = 0; k < MAX_REF_FRAMES; ++k) {
3352       single_inter_filter[i][k] = SWITCHABLE;
3353       single_skippable[i][k] = 0;
3354     }
3355   }
3356
3357   rd_cost->rate = INT_MAX;
3358
3359   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3360     x->pred_mv_sad[ref_frame] = INT_MAX;
3361     if ((cpi->ref_frame_flags & flag_list[ref_frame]) &&
3362         !(is_rect_partition && (ctx->skip_ref_frame_mask & (1 << ref_frame)))) {
3363       assert(get_ref_frame_buffer(cpi, ref_frame) != NULL);
3364       setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col,
3365                          frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
3366     }
3367     frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
3368     frame_mv[ZEROMV][ref_frame].as_int = 0;
3369   }
3370
3371   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3372     if (!(cpi->ref_frame_flags & flag_list[ref_frame])) {
3373       // Skip checking missing references in both single and compound reference
3374       // modes. Note that a mode will be skipped if both reference frames
3375       // are masked out.
3376       ref_frame_skip_mask[0] |= (1 << ref_frame);
3377       ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
3378     } else if (sf->reference_masking) {
3379       for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3380         // Skip fixed mv modes for poor references
3381         if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) {
3382           mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO;
3383           break;
3384         }
3385       }
3386     }
3387     // If the segment reference frame feature is enabled....
3388     // then do nothing if the current ref frame is not allowed..
3389     if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
3390         get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
3391       ref_frame_skip_mask[0] |= (1 << ref_frame);
3392       ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
3393     }
3394   }
3395
3396   // Disable this drop out case if the ref frame
3397   // segment level feature is enabled for this segment. This is to
3398   // prevent the possibility that we end up unable to pick any mode.
3399   if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
3400     // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
3401     // unless ARNR filtering is enabled in which case we want
3402     // an unfiltered alternative. We allow near/nearest as well
3403     // because they may result in zero-zero MVs but be cheaper.
3404     if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
3405       ref_frame_skip_mask[0] = (1 << LAST_FRAME) | (1 << GOLDEN_FRAME);
3406       ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
3407       mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO;
3408       if (frame_mv[NEARMV][ALTREF_FRAME].as_int != 0)
3409         mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV);
3410       if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != 0)
3411         mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV);
3412     }
3413   }
3414
3415   if (cpi->rc.is_src_frame_alt_ref) {
3416     if (sf->alt_ref_search_fp) {
3417       mode_skip_mask[ALTREF_FRAME] = 0;
3418       ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME);
3419       ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
3420     }
3421   }
3422
3423   if (sf->alt_ref_search_fp)
3424     if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX)
3425       if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1))
3426         mode_skip_mask[ALTREF_FRAME] |= INTER_ALL;
3427
3428   if (sf->adaptive_mode_search) {
3429     if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref &&
3430         cpi->rc.frames_since_golden >= 3)
3431       if (x->pred_mv_sad[GOLDEN_FRAME] > (x->pred_mv_sad[LAST_FRAME] << 1))
3432         mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL;
3433   }
3434
3435   if (bsize > sf->max_intra_bsize) {
3436     ref_frame_skip_mask[0] |= (1 << INTRA_FRAME);
3437     ref_frame_skip_mask[1] |= (1 << INTRA_FRAME);
3438   }
3439
3440   mode_skip_mask[INTRA_FRAME] |=
3441       ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]);
3442
3443   for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0;
3444
3445   for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i)
3446     mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5;
3447
3448   midx = sf->schedule_mode_search ? mode_skip_start : 0;
3449
3450   while (midx > 4) {
3451     uint8_t end_pos = 0;
3452     for (i = 5; i < midx; ++i) {
3453       if (mode_threshold[tile_mode_map[i - 1]] >
3454           mode_threshold[tile_mode_map[i]]) {
3455         uint8_t tmp = tile_mode_map[i];
3456         tile_mode_map[i] = tile_mode_map[i - 1];
3457         tile_mode_map[i - 1] = tmp;
3458         end_pos = i;
3459       }
3460     }
3461     midx = end_pos;
3462   }
3463
3464   memcpy(mode_map, tile_mode_map, sizeof(mode_map));
3465
3466   for (midx = 0; midx < MAX_MODES; ++midx) {
3467     int mode_index = mode_map[midx];
3468     int mode_excluded = 0;
3469     int64_t this_rd = INT64_MAX;
3470     int disable_skip = 0;
3471     int compmode_cost = 0;
3472     int rate2 = 0, rate_y = 0, rate_uv = 0;
3473     int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
3474     int skippable = 0;
3475     int this_skip2 = 0;
3476     int64_t total_sse = INT64_MAX;
3477     int early_term = 0;
3478
3479     this_mode = vp9_mode_order[mode_index].mode;
3480     ref_frame = vp9_mode_order[mode_index].ref_frame[0];
3481     second_ref_frame = vp9_mode_order[mode_index].ref_frame[1];
3482
3483     vp9_zero(x->sum_y_eobs);
3484
3485     if (is_rect_partition) {
3486       if (ctx->skip_ref_frame_mask & (1 << ref_frame)) continue;
3487       if (second_ref_frame > 0 &&
3488           (ctx->skip_ref_frame_mask & (1 << second_ref_frame)))
3489         continue;
3490     }
3491
3492     // Look at the reference frame of the best mode so far and set the
3493     // skip mask to look at a subset of the remaining modes.
3494     if (midx == mode_skip_start && best_mode_index >= 0) {
3495       switch (best_mbmode.ref_frame[0]) {
3496         case INTRA_FRAME: break;
3497         case LAST_FRAME: ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; break;
3498         case GOLDEN_FRAME:
3499           ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK;
3500           break;
3501         case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALT_REF_MODE_MASK; break;
3502         case NONE:
3503         case MAX_REF_FRAMES: assert(0 && "Invalid Reference frame"); break;
3504       }
3505     }
3506
3507     if ((ref_frame_skip_mask[0] & (1 << ref_frame)) &&
3508         (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame))))
3509       continue;
3510
3511     if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue;
3512
3513     // Test best rd so far against threshold for trying this mode.
3514     if (best_mode_skippable && sf->schedule_mode_search)
3515       mode_threshold[mode_index] <<= 1;
3516
3517     if (best_rd < mode_threshold[mode_index]) continue;
3518
3519     // This is only used in motion vector unit test.
3520     if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue;
3521
3522     if (sf->motion_field_mode_search) {
3523       const int mi_width = VPXMIN(num_8x8_blocks_wide_lookup[bsize],
3524                                   tile_info->mi_col_end - mi_col);
3525       const int mi_height = VPXMIN(num_8x8_blocks_high_lookup[bsize],
3526                                    tile_info->mi_row_end - mi_row);
3527       const int bsl = mi_width_log2_lookup[bsize];
3528       int cb_partition_search_ctrl =
3529           (((mi_row + mi_col) >> bsl) +
3530            get_chessboard_index(cm->current_video_frame)) &
3531           0x1;
3532       MODE_INFO *ref_mi;
3533       int const_motion = 1;
3534       int skip_ref_frame = !cb_partition_search_ctrl;
3535       MV_REFERENCE_FRAME rf = NONE;
3536       int_mv ref_mv;
3537       ref_mv.as_int = INVALID_MV;
3538
3539       if ((mi_row - 1) >= tile_info->mi_row_start) {
3540         ref_mv = xd->mi[-xd->mi_stride]->mv[0];
3541         rf = xd->mi[-xd->mi_stride]->ref_frame[0];
3542         for (i = 0; i < mi_width; ++i) {
3543           ref_mi = xd->mi[-xd->mi_stride + i];
3544           const_motion &= (ref_mv.as_int == ref_mi->mv[0].as_int) &&
3545                           (ref_frame == ref_mi->ref_frame[0]);
3546           skip_ref_frame &= (rf == ref_mi->ref_frame[0]);
3547         }
3548       }
3549
3550       if ((mi_col - 1) >= tile_info->mi_col_start) {
3551         if (ref_mv.as_int == INVALID_MV) ref_mv = xd->mi[-1]->mv[0];
3552         if (rf == NONE) rf = xd->mi[-1]->ref_frame[0];
3553         for (i = 0; i < mi_height; ++i) {
3554           ref_mi = xd->mi[i * xd->mi_stride - 1];
3555           const_motion &= (ref_mv.as_int == ref_mi->mv[0].as_int) &&
3556                           (ref_frame == ref_mi->ref_frame[0]);
3557           skip_ref_frame &= (rf == ref_mi->ref_frame[0]);
3558         }
3559       }
3560
3561       if (skip_ref_frame && this_mode != NEARESTMV && this_mode != NEWMV)
3562         if (rf > INTRA_FRAME)
3563           if (ref_frame != rf) continue;
3564
3565       if (const_motion)
3566         if (this_mode == NEARMV || this_mode == ZEROMV) continue;
3567     }
3568
3569     comp_pred = second_ref_frame > INTRA_FRAME;
3570     if (comp_pred) {
3571       if (!cpi->allow_comp_inter_inter) continue;
3572
3573       if (cm->ref_frame_sign_bias[ref_frame] ==
3574           cm->ref_frame_sign_bias[second_ref_frame])
3575         continue;
3576
3577       // Skip compound inter modes if ARF is not available.
3578       if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
3579
3580       // Do not allow compound prediction if the segment level reference frame
3581       // feature is in use as in this case there can only be one reference.
3582       if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue;
3583
3584       if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
3585           best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME)
3586         continue;
3587
3588       mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
3589     } else {
3590       if (ref_frame != INTRA_FRAME)
3591         mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
3592     }
3593
3594     if (ref_frame == INTRA_FRAME) {
3595       if (sf->adaptive_mode_search)
3596         if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse)
3597           continue;
3598
3599       if (this_mode != DC_PRED) {
3600         // Disable intra modes other than DC_PRED for blocks with low variance
3601         // Threshold for intra skipping based on source variance
3602         // TODO(debargha): Specialize the threshold for super block sizes
3603         const unsigned int skip_intra_var_thresh =
3604             (cpi->oxcf.content == VP9E_CONTENT_FILM) ? 0 : 64;
3605         if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) &&
3606             x->source_variance < skip_intra_var_thresh)
3607           continue;
3608         // Only search the oblique modes if the best so far is
3609         // one of the neighboring directional modes
3610         if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) &&
3611             (this_mode >= D45_PRED && this_mode <= TM_PRED)) {
3612           if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME)
3613             continue;
3614         }
3615         if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
3616           if (conditional_skipintra(this_mode, best_intra_mode)) continue;
3617         }
3618       }
3619     } else {
3620       const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame };
3621       if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, frame_mv, this_mode,
3622                               ref_frames))
3623         continue;
3624     }
3625
3626     mi->mode = this_mode;
3627     mi->uv_mode = DC_PRED;
3628     mi->ref_frame[0] = ref_frame;
3629     mi->ref_frame[1] = second_ref_frame;
3630     // Evaluate all sub-pel filters irrespective of whether we can use
3631     // them for this frame.
3632     mi->interp_filter =
3633         cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
3634     mi->mv[0].as_int = mi->mv[1].as_int = 0;
3635
3636     x->skip = 0;
3637     set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
3638
3639     // Select prediction reference frames.
3640     for (i = 0; i < MAX_MB_PLANE; i++) {
3641       xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
3642       if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
3643     }
3644
3645     if (ref_frame == INTRA_FRAME) {
3646       TX_SIZE uv_tx;
3647       struct macroblockd_plane *const pd = &xd->plane[1];
3648       memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
3649       super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, bsize,
3650                       best_rd, recon);
3651       if (rate_y == INT_MAX) continue;
3652
3653       uv_tx = uv_txsize_lookup[bsize][mi->tx_size][pd->subsampling_x]
3654                               [pd->subsampling_y];
3655       if (rate_uv_intra[uv_tx] == INT_MAX) {
3656         choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx],
3657                              &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx],
3658                              &skip_uv[uv_tx], &mode_uv[uv_tx]);
3659       }
3660
3661       rate_uv = rate_uv_tokenonly[uv_tx];
3662       distortion_uv = dist_uv[uv_tx];
3663       skippable = skippable && skip_uv[uv_tx];
3664       mi->uv_mode = mode_uv[uv_tx];
3665
3666       rate2 = rate_y + cpi->mbmode_cost[mi->mode] + rate_uv_intra[uv_tx];
3667       if (this_mode != DC_PRED && this_mode != TM_PRED)
3668         rate2 += intra_cost_penalty;
3669       distortion2 = distortion_y + distortion_uv;
3670     } else {
3671       this_rd = handle_inter_mode(
3672           cpi, x, bsize, &rate2, &distortion2, &skippable, &rate_y, &rate_uv,
3673           recon, &disable_skip, frame_mv, mi_row, mi_col, single_newmv,
3674           single_inter_filter, single_skippable, &total_sse, best_rd,
3675           &mask_filter, filter_cache);
3676       if (this_rd == INT64_MAX) continue;
3677
3678       compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
3679
3680       if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost;
3681     }
3682
3683     // Estimate the reference frame signaling cost and add it
3684     // to the rolling cost variable.
3685     if (comp_pred) {
3686       rate2 += ref_costs_comp[ref_frame];
3687     } else {
3688       rate2 += ref_costs_single[ref_frame];
3689     }
3690
3691     if (!disable_skip) {
3692       const vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
3693       const int skip_cost0 = vp9_cost_bit(skip_prob, 0);
3694       const int skip_cost1 = vp9_cost_bit(skip_prob, 1);
3695
3696       if (skippable) {
3697         // Back out the coefficient coding costs
3698         rate2 -= (rate_y + rate_uv);
3699
3700         // Cost the skip mb case
3701         rate2 += skip_cost1;
3702       } else if (ref_frame != INTRA_FRAME && !xd->lossless) {
3703         if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + skip_cost0,
3704                    distortion2) <
3705             RDCOST(x->rdmult, x->rddiv, skip_cost1, total_sse)) {
3706           // Add in the cost of the no skip flag.
3707           rate2 += skip_cost0;
3708         } else {
3709           // FIXME(rbultje) make this work for splitmv also
3710           assert(total_sse >= 0);
3711
3712           rate2 += skip_cost1;
3713           distortion2 = total_sse;
3714           rate2 -= (rate_y + rate_uv);
3715           this_skip2 = 1;
3716         }
3717       } else {
3718         // Add in the cost of the no skip flag.
3719         rate2 += skip_cost0;
3720       }
3721
3722       // Calculate the final RD estimate for this mode.
3723       this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
3724     }
3725
3726     // Apply an adjustment to the rd value based on the similarity of the
3727     // source variance and reconstructed variance.
3728     if (recon) {
3729       rd_variance_adjustment(cpi, x, bsize, &this_rd, recon, ref_frame);
3730     }
3731
3732     if (ref_frame == INTRA_FRAME) {
3733       // Keep record of best intra rd
3734       if (this_rd < best_intra_rd) {
3735         best_intra_rd = this_rd;
3736         best_intra_mode = mi->mode;
3737       }
3738     }
3739
3740     if (!disable_skip && ref_frame == INTRA_FRAME) {
3741       for (i = 0; i < REFERENCE_MODES; ++i)
3742         best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd);
3743       for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
3744         best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd);
3745     }
3746
3747     // Did this mode help.. i.e. is it the new best mode
3748     if (this_rd < best_rd || x->skip) {
3749       int max_plane = MAX_MB_PLANE;
3750       if (!mode_excluded) {
3751         // Note index of best mode so far
3752         best_mode_index = mode_index;
3753
3754         if (ref_frame == INTRA_FRAME) {
3755           /* required for left and above block mv */
3756           mi->mv[0].as_int = 0;
3757           max_plane = 1;
3758           // Initialize interp_filter here so we do not have to check for
3759           // inter block modes in get_pred_context_switchable_interp()
3760           mi->interp_filter = SWITCHABLE_FILTERS;
3761         } else {
3762           best_pred_sse = x->pred_sse[ref_frame];
3763         }
3764
3765         rd_cost->rate = rate2;
3766         rd_cost->dist = distortion2;
3767         rd_cost->rdcost = this_rd;
3768         best_rd = this_rd;
3769         best_mbmode = *mi;
3770         best_skip2 = this_skip2;
3771         best_mode_skippable = skippable;
3772
3773         if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
3774         memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mi->tx_size],
3775                sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
3776         ctx->sum_y_eobs = x->sum_y_eobs[mi->tx_size];
3777
3778         // TODO(debargha): enhance this test with a better distortion prediction
3779         // based on qp, activity mask and history
3780         if ((mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
3781             (mode_index > MIN_EARLY_TERM_INDEX)) {
3782           int qstep = xd->plane[0].dequant[1];
3783           // TODO(debargha): Enhance this by specializing for each mode_index
3784           int scale = 4;
3785 #if CONFIG_VP9_HIGHBITDEPTH
3786           if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
3787             qstep >>= (xd->bd - 8);
3788           }
3789 #endif  // CONFIG_VP9_HIGHBITDEPTH
3790           if (x->source_variance < UINT_MAX) {
3791             const int var_adjust = (x->source_variance < 16);
3792             scale -= var_adjust;
3793           }
3794           if (ref_frame > INTRA_FRAME && distortion2 * scale < qstep * qstep) {
3795             early_term = 1;
3796           }
3797         }
3798       }
3799     }
3800
3801     /* keep record of best compound/single-only prediction */
3802     if (!disable_skip && ref_frame != INTRA_FRAME) {
3803       int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
3804
3805       if (cm->reference_mode == REFERENCE_MODE_SELECT) {
3806         single_rate = rate2 - compmode_cost;
3807         hybrid_rate = rate2;
3808       } else {
3809         single_rate = rate2;
3810         hybrid_rate = rate2 + compmode_cost;
3811       }
3812
3813       single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
3814       hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
3815
3816       if (!comp_pred) {
3817         if (single_rd < best_pred_rd[SINGLE_REFERENCE])
3818           best_pred_rd[SINGLE_REFERENCE] = single_rd;
3819       } else {
3820         if (single_rd < best_pred_rd[COMPOUND_REFERENCE])
3821           best_pred_rd[COMPOUND_REFERENCE] = single_rd;
3822       }
3823       if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
3824         best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
3825
3826       /* keep record of best filter type */
3827       if (!mode_excluded && cm->interp_filter != BILINEAR) {
3828         int64_t ref =
3829             filter_cache[cm->interp_filter == SWITCHABLE ? SWITCHABLE_FILTERS
3830                                                          : cm->interp_filter];
3831
3832         for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3833           int64_t adj_rd;
3834           if (ref == INT64_MAX)
3835             adj_rd = 0;
3836           else if (filter_cache[i] == INT64_MAX)
3837             // when early termination is triggered, the encoder does not have
3838             // access to the rate-distortion cost. it only knows that the cost
3839             // should be above the maximum valid value. hence it takes the known
3840             // maximum plus an arbitrary constant as the rate-distortion cost.
3841             adj_rd = mask_filter - ref + 10;
3842           else
3843             adj_rd = filter_cache[i] - ref;
3844
3845           adj_rd += this_rd;
3846           best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd);
3847         }
3848       }
3849     }
3850
3851     if (early_term) break;
3852
3853     if (x->skip && !comp_pred) break;
3854   }
3855
3856   // The inter modes' rate costs are not calculated precisely in some cases.
3857   // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and
3858   // ZEROMV. Here, checks are added for those cases, and the mode decisions
3859   // are corrected.
3860   if (best_mbmode.mode == NEWMV) {
3861     const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
3862                                          best_mbmode.ref_frame[1] };
3863     int comp_pred_mode = refs[1] > INTRA_FRAME;
3864
3865     if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
3866         ((comp_pred_mode &&
3867           frame_mv[NEARESTMV][refs[1]].as_int == best_mbmode.mv[1].as_int) ||
3868          !comp_pred_mode))
3869       best_mbmode.mode = NEARESTMV;
3870     else if (frame_mv[NEARMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
3871              ((comp_pred_mode &&
3872                frame_mv[NEARMV][refs[1]].as_int == best_mbmode.mv[1].as_int) ||
3873               !comp_pred_mode))
3874       best_mbmode.mode = NEARMV;
3875     else if (best_mbmode.mv[0].as_int == 0 &&
3876              ((comp_pred_mode && best_mbmode.mv[1].as_int == 0) ||
3877               !comp_pred_mode))
3878       best_mbmode.mode = ZEROMV;
3879   }
3880
3881   if (best_mode_index < 0 || best_rd >= best_rd_so_far) {
3882 // If adaptive interp filter is enabled, then the current leaf node of 8x8
3883 // data is needed for sub8x8. Hence preserve the context.
3884 #if CONFIG_CONSISTENT_RECODE
3885     if (bsize == BLOCK_8X8) ctx->mic = *xd->mi[0];
3886 #else
3887     if (cpi->row_mt && bsize == BLOCK_8X8) ctx->mic = *xd->mi[0];
3888 #endif
3889     rd_cost->rate = INT_MAX;
3890     rd_cost->rdcost = INT64_MAX;
3891     return;
3892   }
3893
3894   // If we used an estimate for the uv intra rd in the loop above...
3895   if (sf->use_uv_intra_rd_estimate) {
3896     // Do Intra UV best rd mode selection if best mode choice above was intra.
3897     if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
3898       TX_SIZE uv_tx_size;
3899       *mi = best_mbmode;
3900       uv_tx_size = get_uv_tx_size(mi, &xd->plane[1]);
3901       rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size],
3902                               &rate_uv_tokenonly[uv_tx_size],
3903                               &dist_uv[uv_tx_size], &skip_uv[uv_tx_size],
3904                               bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
3905                               uv_tx_size);
3906     }
3907   }
3908
3909   assert((cm->interp_filter == SWITCHABLE) ||
3910          (cm->interp_filter == best_mbmode.interp_filter) ||
3911          !is_inter_block(&best_mbmode));
3912
3913   if (!cpi->rc.is_src_frame_alt_ref)
3914     vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
3915                               sf->adaptive_rd_thresh, bsize, best_mode_index);
3916
3917   // macroblock modes
3918   *mi = best_mbmode;
3919   x->skip |= best_skip2;
3920
3921   for (i = 0; i < REFERENCE_MODES; ++i) {
3922     if (best_pred_rd[i] == INT64_MAX)
3923       best_pred_diff[i] = INT_MIN;
3924     else
3925       best_pred_diff[i] = best_rd - best_pred_rd[i];
3926   }
3927
3928   if (!x->skip) {
3929     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3930       if (best_filter_rd[i] == INT64_MAX)
3931         best_filter_diff[i] = 0;
3932       else
3933         best_filter_diff[i] = best_rd - best_filter_rd[i];
3934     }
3935     if (cm->interp_filter == SWITCHABLE)
3936       assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
3937   } else {
3938     vp9_zero(best_filter_diff);
3939   }
3940
3941   // TODO(yunqingwang): Moving this line in front of the above best_filter_diff
3942   // updating code causes PSNR loss. Need to figure out the confliction.
3943   x->skip |= best_mode_skippable;
3944
3945   if (!x->skip && !x->select_tx_size) {
3946     int has_high_freq_coeff = 0;
3947     int plane;
3948     int max_plane = is_inter_block(xd->mi[0]) ? MAX_MB_PLANE : 1;
3949     for (plane = 0; plane < max_plane; ++plane) {
3950       x->plane[plane].eobs = ctx->eobs_pbuf[plane][1];
3951       has_high_freq_coeff |= vp9_has_high_freq_in_plane(x, bsize, plane);
3952     }
3953
3954     for (plane = max_plane; plane < MAX_MB_PLANE; ++plane) {
3955       x->plane[plane].eobs = ctx->eobs_pbuf[plane][2];
3956       has_high_freq_coeff |= vp9_has_high_freq_in_plane(x, bsize, plane);
3957     }
3958
3959     best_mode_skippable |= !has_high_freq_coeff;
3960   }
3961
3962   assert(best_mode_index >= 0);
3963
3964   store_coding_context(x, ctx, best_mode_index, best_pred_diff,
3965                        best_filter_diff, best_mode_skippable);
3966 }
3967
3968 void vp9_rd_pick_inter_mode_sb_seg_skip(VP9_COMP *cpi, TileDataEnc *tile_data,
3969                                         MACROBLOCK *x, RD_COST *rd_cost,
3970                                         BLOCK_SIZE bsize,
3971                                         PICK_MODE_CONTEXT *ctx,
3972                                         int64_t best_rd_so_far) {
3973   VP9_COMMON *const cm = &cpi->common;
3974   MACROBLOCKD *const xd = &x->e_mbd;
3975   MODE_INFO *const mi = xd->mi[0];
3976   unsigned char segment_id = mi->segment_id;
3977   const int comp_pred = 0;
3978   int i;
3979   int64_t best_pred_diff[REFERENCE_MODES];
3980   int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
3981   unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
3982   vpx_prob comp_mode_p;
3983   INTERP_FILTER best_filter = SWITCHABLE;
3984   int64_t this_rd = INT64_MAX;
3985   int rate2 = 0;
3986   const int64_t distortion2 = 0;
3987
3988   x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
3989
3990   estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
3991                            &comp_mode_p);
3992
3993   for (i = 0; i < MAX_REF_FRAMES; ++i) x->pred_sse[i] = INT_MAX;
3994   for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i) x->pred_mv_sad[i] = INT_MAX;
3995
3996   rd_cost->rate = INT_MAX;
3997
3998   assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP));
3999
4000   mi->mode = ZEROMV;
4001   mi->uv_mode = DC_PRED;
4002   mi->ref_frame[0] = LAST_FRAME;
4003   mi->ref_frame[1] = NONE;
4004   mi->mv[0].as_int = 0;
4005   x->skip = 1;
4006
4007   ctx->sum_y_eobs = 0;
4008
4009   if (cm->interp_filter != BILINEAR) {
4010     best_filter = EIGHTTAP;
4011     if (cm->interp_filter == SWITCHABLE &&
4012         x->source_variance >= cpi->sf.disable_filter_search_var_thresh) {
4013       int rs;
4014       int best_rs = INT_MAX;
4015       for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
4016         mi->interp_filter = i;
4017         rs = vp9_get_switchable_rate(cpi, xd);
4018         if (rs < best_rs) {
4019           best_rs = rs;
4020           best_filter = mi->interp_filter;
4021         }
4022       }
4023     }
4024   }
4025   // Set the appropriate filter
4026   if (cm->interp_filter == SWITCHABLE) {
4027     mi->interp_filter = best_filter;
4028     rate2 += vp9_get_switchable_rate(cpi, xd);
4029   } else {
4030     mi->interp_filter = cm->interp_filter;
4031   }
4032
4033   if (cm->reference_mode == REFERENCE_MODE_SELECT)
4034     rate2 += vp9_cost_bit(comp_mode_p, comp_pred);
4035
4036   // Estimate the reference frame signaling cost and add it
4037   // to the rolling cost variable.
4038   rate2 += ref_costs_single[LAST_FRAME];
4039   this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
4040
4041   rd_cost->rate = rate2;
4042   rd_cost->dist = distortion2;
4043   rd_cost->rdcost = this_rd;
4044
4045   if (this_rd >= best_rd_so_far) {
4046     rd_cost->rate = INT_MAX;
4047     rd_cost->rdcost = INT64_MAX;
4048     return;
4049   }
4050
4051   assert((cm->interp_filter == SWITCHABLE) ||
4052          (cm->interp_filter == mi->interp_filter));
4053
4054   vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
4055                             cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV);
4056
4057   vp9_zero(best_pred_diff);
4058   vp9_zero(best_filter_diff);
4059
4060   if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, MAX_MB_PLANE);
4061   store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, best_filter_diff, 0);
4062 }
4063
4064 void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, TileDataEnc *tile_data,
4065                                    MACROBLOCK *x, int mi_row, int mi_col,
4066                                    RD_COST *rd_cost, BLOCK_SIZE bsize,
4067                                    PICK_MODE_CONTEXT *ctx,
4068                                    int64_t best_rd_so_far) {
4069   VP9_COMMON *const cm = &cpi->common;
4070   RD_OPT *const rd_opt = &cpi->rd;
4071   SPEED_FEATURES *const sf = &cpi->sf;
4072   MACROBLOCKD *const xd = &x->e_mbd;
4073   MODE_INFO *const mi = xd->mi[0];
4074   const struct segmentation *const seg = &cm->seg;
4075   MV_REFERENCE_FRAME ref_frame, second_ref_frame;
4076   unsigned char segment_id = mi->segment_id;
4077   int comp_pred, i;
4078   int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
4079   struct buf_2d yv12_mb[4][MAX_MB_PLANE];
4080   static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
4081                                     VP9_ALT_FLAG };
4082   int64_t best_rd = best_rd_so_far;
4083   int64_t best_yrd = best_rd_so_far;  // FIXME(rbultje) more precise
4084   int64_t best_pred_diff[REFERENCE_MODES];
4085   int64_t best_pred_rd[REFERENCE_MODES];
4086   int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
4087   int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
4088   MODE_INFO best_mbmode;
4089   int ref_index, best_ref_index = 0;
4090   unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
4091   vpx_prob comp_mode_p;
4092   INTERP_FILTER tmp_best_filter = SWITCHABLE;
4093   int rate_uv_intra, rate_uv_tokenonly;
4094   int64_t dist_uv;
4095   int skip_uv;
4096   PREDICTION_MODE mode_uv = DC_PRED;
4097   const int intra_cost_penalty =
4098       vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
4099   int_mv seg_mvs[4][MAX_REF_FRAMES];
4100   b_mode_info best_bmodes[4];
4101   int best_skip2 = 0;
4102   int ref_frame_skip_mask[2] = { 0 };
4103   int64_t mask_filter = 0;
4104   int64_t filter_cache[SWITCHABLE_FILTER_CONTEXTS];
4105   int internal_active_edge =
4106       vp9_active_edge_sb(cpi, mi_row, mi_col) && vp9_internal_image_edge(cpi);
4107   const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
4108
4109   x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
4110   memset(x->zcoeff_blk[TX_4X4], 0, 4);
4111   vp9_zero(best_mbmode);
4112
4113   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
4114
4115   for (i = 0; i < 4; i++) {
4116     int j;
4117     for (j = 0; j < MAX_REF_FRAMES; j++) seg_mvs[i][j].as_int = INVALID_MV;
4118   }
4119
4120   estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
4121                            &comp_mode_p);
4122
4123   for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX;
4124   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
4125     best_filter_rd[i] = INT64_MAX;
4126   rate_uv_intra = INT_MAX;
4127
4128   rd_cost->rate = INT_MAX;
4129
4130   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
4131     if (cpi->ref_frame_flags & flag_list[ref_frame]) {
4132       setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col,
4133                          frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
4134     } else {
4135       ref_frame_skip_mask[0] |= (1 << ref_frame);
4136       ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
4137     }
4138     frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
4139     frame_mv[ZEROMV][ref_frame].as_int = 0;
4140   }
4141
4142   for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) {
4143     int mode_excluded = 0;
4144     int64_t this_rd = INT64_MAX;
4145     int disable_skip = 0;
4146     int compmode_cost = 0;
4147     int rate2 = 0, rate_y = 0, rate_uv = 0;
4148     int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
4149     int skippable = 0;
4150     int i;
4151     int this_skip2 = 0;
4152     int64_t total_sse = INT_MAX;
4153     int early_term = 0;
4154     struct buf_2d backup_yv12[2][MAX_MB_PLANE];
4155
4156     ref_frame = vp9_ref_order[ref_index].ref_frame[0];
4157     second_ref_frame = vp9_ref_order[ref_index].ref_frame[1];
4158
4159     vp9_zero(x->sum_y_eobs);
4160
4161 #if CONFIG_BETTER_HW_COMPATIBILITY
4162     // forbid 8X4 and 4X8 partitions if any reference frame is scaled.
4163     if (bsize == BLOCK_8X4 || bsize == BLOCK_4X8) {
4164       int ref_scaled = ref_frame > INTRA_FRAME &&
4165                        vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf);
4166       if (second_ref_frame > INTRA_FRAME)
4167         ref_scaled += vp9_is_scaled(&cm->frame_refs[second_ref_frame - 1].sf);
4168       if (ref_scaled) continue;
4169     }
4170 #endif
4171     // Look at the reference frame of the best mode so far and set the
4172     // skip mask to look at a subset of the remaining modes.
4173     if (ref_index > 2 && sf->mode_skip_start < MAX_MODES) {
4174       if (ref_index == 3) {
4175         switch (best_mbmode.ref_frame[0]) {
4176           case INTRA_FRAME: break;
4177           case LAST_FRAME:
4178             ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME);
4179             ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
4180             break;
4181           case GOLDEN_FRAME:
4182             ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << ALTREF_FRAME);
4183             ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
4184             break;
4185           case ALTREF_FRAME:
4186             ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << LAST_FRAME);
4187             break;
4188           case NONE:
4189           case MAX_REF_FRAMES: assert(0 && "Invalid Reference frame"); break;
4190         }
4191       }
4192     }
4193
4194     if ((ref_frame_skip_mask[0] & (1 << ref_frame)) &&
4195         (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame))))
4196       continue;
4197
4198     // Test best rd so far against threshold for trying this mode.
4199     if (!internal_active_edge &&
4200         rd_less_than_thresh(best_rd,
4201                             rd_opt->threshes[segment_id][bsize][ref_index],
4202                             &rd_thresh_freq_fact[ref_index]))
4203       continue;
4204
4205     // This is only used in motion vector unit test.
4206     if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue;
4207
4208     comp_pred = second_ref_frame > INTRA_FRAME;
4209     if (comp_pred) {
4210       if (!cpi->allow_comp_inter_inter) continue;
4211
4212       if (cm->ref_frame_sign_bias[ref_frame] ==
4213           cm->ref_frame_sign_bias[second_ref_frame])
4214         continue;
4215
4216       if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
4217       // Do not allow compound prediction if the segment level reference frame
4218       // feature is in use as in this case there can only be one reference.
4219       if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue;
4220
4221       if ((sf->mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
4222           best_mbmode.ref_frame[0] == INTRA_FRAME)
4223         continue;
4224     }
4225
4226     if (comp_pred)
4227       mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
4228     else if (ref_frame != INTRA_FRAME)
4229       mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
4230
4231     // If the segment reference frame feature is enabled....
4232     // then do nothing if the current ref frame is not allowed..
4233     if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
4234         get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
4235       continue;
4236       // Disable this drop out case if the ref frame
4237       // segment level feature is enabled for this segment. This is to
4238       // prevent the possibility that we end up unable to pick any mode.
4239     } else if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
4240       // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
4241       // unless ARNR filtering is enabled in which case we want
4242       // an unfiltered alternative. We allow near/nearest as well
4243       // because they may result in zero-zero MVs but be cheaper.
4244       if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
4245         continue;
4246     }
4247
4248     mi->tx_size = TX_4X4;
4249     mi->uv_mode = DC_PRED;
4250     mi->ref_frame[0] = ref_frame;
4251     mi->ref_frame[1] = second_ref_frame;
4252     // Evaluate all sub-pel filters irrespective of whether we can use
4253     // them for this frame.
4254     mi->interp_filter =
4255         cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
4256     x->skip = 0;
4257     set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
4258
4259     // Select prediction reference frames.
4260     for (i = 0; i < MAX_MB_PLANE; i++) {
4261       xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
4262       if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
4263     }
4264
4265     if (ref_frame == INTRA_FRAME) {
4266       int rate;
4267       if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate, &rate_y, &distortion_y,
4268                                        best_rd) >= best_rd)
4269         continue;
4270       rate2 += rate;
4271       rate2 += intra_cost_penalty;
4272       distortion2 += distortion_y;
4273
4274       if (rate_uv_intra == INT_MAX) {
4275         choose_intra_uv_mode(cpi, x, ctx, bsize, TX_4X4, &rate_uv_intra,
4276                              &rate_uv_tokenonly, &dist_uv, &skip_uv, &mode_uv);
4277       }
4278       rate2 += rate_uv_intra;
4279       rate_uv = rate_uv_tokenonly;
4280       distortion2 += dist_uv;
4281       distortion_uv = dist_uv;
4282       mi->uv_mode = mode_uv;
4283     } else {
4284       int rate;
4285       int64_t distortion;
4286       int64_t this_rd_thresh;
4287       int64_t tmp_rd, tmp_best_rd = INT64_MAX, tmp_best_rdu = INT64_MAX;
4288       int tmp_best_rate = INT_MAX, tmp_best_ratey = INT_MAX;
4289       int64_t tmp_best_distortion = INT_MAX, tmp_best_sse, uv_sse;
4290       int tmp_best_skippable = 0;
4291       int switchable_filter_index;
4292       int_mv *second_ref =
4293           comp_pred ? &x->mbmi_ext->ref_mvs[second_ref_frame][0] : NULL;
4294       b_mode_info tmp_best_bmodes[16];
4295       MODE_INFO tmp_best_mbmode;
4296       BEST_SEG_INFO bsi[SWITCHABLE_FILTERS];
4297       int pred_exists = 0;
4298       int uv_skippable;
4299
4300       YV12_BUFFER_CONFIG *scaled_ref_frame[2] = { NULL, NULL };
4301       int ref;
4302
4303       for (ref = 0; ref < 2; ++ref) {
4304         scaled_ref_frame[ref] =
4305             mi->ref_frame[ref] > INTRA_FRAME
4306                 ? vp9_get_scaled_ref_frame(cpi, mi->ref_frame[ref])
4307                 : NULL;
4308
4309         if (scaled_ref_frame[ref]) {
4310           int i;
4311           // Swap out the reference frame for a version that's been scaled to
4312           // match the resolution of the current frame, allowing the existing
4313           // motion search code to be used without additional modifications.
4314           for (i = 0; i < MAX_MB_PLANE; i++)
4315             backup_yv12[ref][i] = xd->plane[i].pre[ref];
4316           vp9_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
4317                                NULL);
4318         }
4319       }
4320
4321       this_rd_thresh = (ref_frame == LAST_FRAME)
4322                            ? rd_opt->threshes[segment_id][bsize][THR_LAST]
4323                            : rd_opt->threshes[segment_id][bsize][THR_ALTR];
4324       this_rd_thresh = (ref_frame == GOLDEN_FRAME)
4325                            ? rd_opt->threshes[segment_id][bsize][THR_GOLD]
4326                            : this_rd_thresh;
4327       for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
4328         filter_cache[i] = INT64_MAX;
4329
4330       if (cm->interp_filter != BILINEAR) {
4331         tmp_best_filter = EIGHTTAP;
4332         if (x->source_variance < sf->disable_filter_search_var_thresh) {
4333           tmp_best_filter = EIGHTTAP;
4334         } else if (sf->adaptive_pred_interp_filter == 1 &&
4335                    ctx->pred_interp_filter < SWITCHABLE) {
4336           tmp_best_filter = ctx->pred_interp_filter;
4337         } else if (sf->adaptive_pred_interp_filter == 2) {
4338           tmp_best_filter = ctx->pred_interp_filter < SWITCHABLE
4339                                 ? ctx->pred_interp_filter
4340                                 : 0;
4341         } else {
4342           for (switchable_filter_index = 0;
4343                switchable_filter_index < SWITCHABLE_FILTERS;
4344                ++switchable_filter_index) {
4345             int newbest, rs;
4346             int64_t rs_rd;
4347             MB_MODE_INFO_EXT *mbmi_ext = x->mbmi_ext;
4348             mi->interp_filter = switchable_filter_index;
4349             tmp_rd = rd_pick_best_sub8x8_mode(
4350                 cpi, x, &mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd,
4351                 &rate, &rate_y, &distortion, &skippable, &total_sse,
4352                 (int)this_rd_thresh, seg_mvs, bsi, switchable_filter_index,
4353                 mi_row, mi_col);
4354
4355             if (tmp_rd == INT64_MAX) continue;
4356             rs = vp9_get_switchable_rate(cpi, xd);
4357             rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
4358             filter_cache[switchable_filter_index] = tmp_rd;
4359             filter_cache[SWITCHABLE_FILTERS] =
4360                 VPXMIN(filter_cache[SWITCHABLE_FILTERS], tmp_rd + rs_rd);
4361             if (cm->interp_filter == SWITCHABLE) tmp_rd += rs_rd;
4362
4363             mask_filter = VPXMAX(mask_filter, tmp_rd);
4364
4365             newbest = (tmp_rd < tmp_best_rd);
4366             if (newbest) {
4367               tmp_best_filter = mi->interp_filter;
4368               tmp_best_rd = tmp_rd;
4369             }
4370             if ((newbest && cm->interp_filter == SWITCHABLE) ||
4371                 (mi->interp_filter == cm->interp_filter &&
4372                  cm->interp_filter != SWITCHABLE)) {
4373               tmp_best_rdu = tmp_rd;
4374               tmp_best_rate = rate;
4375               tmp_best_ratey = rate_y;
4376               tmp_best_distortion = distortion;
4377               tmp_best_sse = total_sse;
4378               tmp_best_skippable = skippable;
4379               tmp_best_mbmode = *mi;
4380               for (i = 0; i < 4; i++) {
4381                 tmp_best_bmodes[i] = xd->mi[0]->bmi[i];
4382                 x->zcoeff_blk[TX_4X4][i] = !x->plane[0].eobs[i];
4383                 x->sum_y_eobs[TX_4X4] += x->plane[0].eobs[i];
4384               }
4385               pred_exists = 1;
4386               if (switchable_filter_index == 0 && sf->use_rd_breakout &&
4387                   best_rd < INT64_MAX) {
4388                 if (tmp_best_rdu / 2 > best_rd) {
4389                   // skip searching the other filters if the first is
4390                   // already substantially larger than the best so far
4391                   tmp_best_filter = mi->interp_filter;
4392                   tmp_best_rdu = INT64_MAX;
4393                   break;
4394                 }
4395               }
4396             }
4397           }  // switchable_filter_index loop
4398         }
4399       }
4400
4401       if (tmp_best_rdu == INT64_MAX && pred_exists) continue;
4402
4403       mi->interp_filter = (cm->interp_filter == SWITCHABLE ? tmp_best_filter
4404                                                            : cm->interp_filter);
4405       if (!pred_exists) {
4406         // Handles the special case when a filter that is not in the
4407         // switchable list (bilinear, 6-tap) is indicated at the frame level
4408         tmp_rd = rd_pick_best_sub8x8_mode(
4409             cpi, x, &x->mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd,
4410             &rate, &rate_y, &distortion, &skippable, &total_sse,
4411             (int)this_rd_thresh, seg_mvs, bsi, 0, mi_row, mi_col);
4412         if (tmp_rd == INT64_MAX) continue;
4413       } else {
4414         total_sse = tmp_best_sse;
4415         rate = tmp_best_rate;
4416         rate_y = tmp_best_ratey;
4417         distortion = tmp_best_distortion;
4418         skippable = tmp_best_skippable;
4419         *mi = tmp_best_mbmode;
4420         for (i = 0; i < 4; i++) xd->mi[0]->bmi[i] = tmp_best_bmodes[i];
4421       }
4422
4423       rate2 += rate;
4424       distortion2 += distortion;
4425
4426       if (cm->interp_filter == SWITCHABLE)
4427         rate2 += vp9_get_switchable_rate(cpi, xd);
4428
4429       if (!mode_excluded)
4430         mode_excluded = comp_pred ? cm->reference_mode == SINGLE_REFERENCE
4431                                   : cm->reference_mode == COMPOUND_REFERENCE;
4432
4433       compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
4434
4435       tmp_best_rdu =
4436           best_rd - VPXMIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2),
4437                            RDCOST(x->rdmult, x->rddiv, 0, total_sse));
4438
4439       if (tmp_best_rdu > 0) {
4440         // If even the 'Y' rd value of split is higher than best so far
4441         // then dont bother looking at UV
4442         vp9_build_inter_predictors_sbuv(&x->e_mbd, mi_row, mi_col, BLOCK_8X8);
4443         memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
4444         if (!super_block_uvrd(cpi, x, &rate_uv, &distortion_uv, &uv_skippable,
4445                               &uv_sse, BLOCK_8X8, tmp_best_rdu)) {
4446           for (ref = 0; ref < 2; ++ref) {
4447             if (scaled_ref_frame[ref]) {
4448               int i;
4449               for (i = 0; i < MAX_MB_PLANE; ++i)
4450                 xd->plane[i].pre[ref] = backup_yv12[ref][i];
4451             }
4452           }
4453           continue;
4454         }
4455
4456         rate2 += rate_uv;
4457         distortion2 += distortion_uv;
4458         skippable = skippable && uv_skippable;
4459         total_sse += uv_sse;
4460       }
4461
4462       for (ref = 0; ref < 2; ++ref) {
4463         if (scaled_ref_frame[ref]) {
4464           // Restore the prediction frame pointers to their unscaled versions.
4465           int i;
4466           for (i = 0; i < MAX_MB_PLANE; ++i)
4467             xd->plane[i].pre[ref] = backup_yv12[ref][i];
4468         }
4469       }
4470     }
4471
4472     if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost;
4473
4474     // Estimate the reference frame signaling cost and add it
4475     // to the rolling cost variable.
4476     if (second_ref_frame > INTRA_FRAME) {
4477       rate2 += ref_costs_comp[ref_frame];
4478     } else {
4479       rate2 += ref_costs_single[ref_frame];
4480     }
4481
4482     if (!disable_skip) {
4483       const vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
4484       const int skip_cost0 = vp9_cost_bit(skip_prob, 0);
4485       const int skip_cost1 = vp9_cost_bit(skip_prob, 1);
4486
4487       // Skip is never coded at the segment level for sub8x8 blocks and instead
4488       // always coded in the bitstream at the mode info level.
4489       if (ref_frame != INTRA_FRAME && !xd->lossless) {
4490         if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + skip_cost0,
4491                    distortion2) <
4492             RDCOST(x->rdmult, x->rddiv, skip_cost1, total_sse)) {
4493           // Add in the cost of the no skip flag.
4494           rate2 += skip_cost0;
4495         } else {
4496           // FIXME(rbultje) make this work for splitmv also
4497           rate2 += skip_cost1;
4498           distortion2 = total_sse;
4499           assert(total_sse >= 0);
4500           rate2 -= (rate_y + rate_uv);
4501           rate_y = 0;
4502           rate_uv = 0;
4503           this_skip2 = 1;
4504         }
4505       } else {
4506         // Add in the cost of the no skip flag.
4507         rate2 += skip_cost0;
4508       }
4509
4510       // Calculate the final RD estimate for this mode.
4511       this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
4512     }
4513
4514     if (!disable_skip && ref_frame == INTRA_FRAME) {
4515       for (i = 0; i < REFERENCE_MODES; ++i)
4516         best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd);
4517       for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
4518         best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd);
4519     }
4520
4521     // Did this mode help.. i.e. is it the new best mode
4522     if (this_rd < best_rd || x->skip) {
4523       if (!mode_excluded) {
4524         int max_plane = MAX_MB_PLANE;
4525         // Note index of best mode so far
4526         best_ref_index = ref_index;
4527
4528         if (ref_frame == INTRA_FRAME) {
4529           /* required for left and above block mv */
4530           mi->mv[0].as_int = 0;
4531           max_plane = 1;
4532           // Initialize interp_filter here so we do not have to check for
4533           // inter block modes in get_pred_context_switchable_interp()
4534           mi->interp_filter = SWITCHABLE_FILTERS;
4535         }
4536
4537         rd_cost->rate = rate2;
4538         rd_cost->dist = distortion2;
4539         rd_cost->rdcost = this_rd;
4540         best_rd = this_rd;
4541         best_yrd =
4542             best_rd - RDCOST(x->rdmult, x->rddiv, rate_uv, distortion_uv);
4543         best_mbmode = *mi;
4544         best_skip2 = this_skip2;
4545         if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
4546         memcpy(ctx->zcoeff_blk, x->zcoeff_blk[TX_4X4],
4547                sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
4548         ctx->sum_y_eobs = x->sum_y_eobs[TX_4X4];
4549
4550         for (i = 0; i < 4; i++) best_bmodes[i] = xd->mi[0]->bmi[i];
4551
4552         // TODO(debargha): enhance this test with a better distortion prediction
4553         // based on qp, activity mask and history
4554         if ((sf->mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
4555             (ref_index > MIN_EARLY_TERM_INDEX)) {
4556           int qstep = xd->plane[0].dequant[1];
4557           // TODO(debargha): Enhance this by specializing for each mode_index
4558           int scale = 4;
4559 #if CONFIG_VP9_HIGHBITDEPTH
4560           if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
4561             qstep >>= (xd->bd - 8);
4562           }
4563 #endif  // CONFIG_VP9_HIGHBITDEPTH
4564           if (x->source_variance < UINT_MAX) {
4565             const int var_adjust = (x->source_variance < 16);
4566             scale -= var_adjust;
4567           }
4568           if (ref_frame > INTRA_FRAME && distortion2 * scale < qstep * qstep) {
4569             early_term = 1;
4570           }
4571         }
4572       }
4573     }
4574
4575     /* keep record of best compound/single-only prediction */
4576     if (!disable_skip && ref_frame != INTRA_FRAME) {
4577       int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
4578
4579       if (cm->reference_mode == REFERENCE_MODE_SELECT) {
4580         single_rate = rate2 - compmode_cost;
4581         hybrid_rate = rate2;
4582       } else {
4583         single_rate = rate2;
4584         hybrid_rate = rate2 + compmode_cost;
4585       }
4586
4587       single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
4588       hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
4589
4590       if (!comp_pred && single_rd < best_pred_rd[SINGLE_REFERENCE])
4591         best_pred_rd[SINGLE_REFERENCE] = single_rd;
4592       else if (comp_pred && single_rd < best_pred_rd[COMPOUND_REFERENCE])
4593         best_pred_rd[COMPOUND_REFERENCE] = single_rd;
4594
4595       if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
4596         best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
4597     }
4598
4599     /* keep record of best filter type */
4600     if (!mode_excluded && !disable_skip && ref_frame != INTRA_FRAME &&
4601         cm->interp_filter != BILINEAR) {
4602       int64_t ref =
4603           filter_cache[cm->interp_filter == SWITCHABLE ? SWITCHABLE_FILTERS
4604                                                        : cm->interp_filter];
4605       int64_t adj_rd;
4606       for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
4607         if (ref == INT64_MAX)
4608           adj_rd = 0;
4609         else if (filter_cache[i] == INT64_MAX)
4610           // when early termination is triggered, the encoder does not have
4611           // access to the rate-distortion cost. it only knows that the cost
4612           // should be above the maximum valid value. hence it takes the known
4613           // maximum plus an arbitrary constant as the rate-distortion cost.
4614           adj_rd = mask_filter - ref + 10;
4615         else
4616           adj_rd = filter_cache[i] - ref;
4617
4618         adj_rd += this_rd;
4619         best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd);
4620       }
4621     }
4622
4623     if (early_term) break;
4624
4625     if (x->skip && !comp_pred) break;
4626   }
4627
4628   if (best_rd >= best_rd_so_far) {
4629     rd_cost->rate = INT_MAX;
4630     rd_cost->rdcost = INT64_MAX;
4631     return;
4632   }
4633
4634   // If we used an estimate for the uv intra rd in the loop above...
4635   if (sf->use_uv_intra_rd_estimate) {
4636     // Do Intra UV best rd mode selection if best mode choice above was intra.
4637     if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
4638       *mi = best_mbmode;
4639       rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra, &rate_uv_tokenonly,
4640                               &dist_uv, &skip_uv, BLOCK_8X8, TX_4X4);
4641     }
4642   }
4643
4644   if (best_rd == INT64_MAX) {
4645     rd_cost->rate = INT_MAX;
4646     rd_cost->dist = INT64_MAX;
4647     rd_cost->rdcost = INT64_MAX;
4648     return;
4649   }
4650
4651   assert((cm->interp_filter == SWITCHABLE) ||
4652          (cm->interp_filter == best_mbmode.interp_filter) ||
4653          !is_inter_block(&best_mbmode));
4654
4655   vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact, sf->adaptive_rd_thresh,
4656                             bsize, best_ref_index);
4657
4658   // macroblock modes
4659   *mi = best_mbmode;
4660   x->skip |= best_skip2;
4661   if (!is_inter_block(&best_mbmode)) {
4662     for (i = 0; i < 4; i++) xd->mi[0]->bmi[i].as_mode = best_bmodes[i].as_mode;
4663   } else {
4664     for (i = 0; i < 4; ++i)
4665       memcpy(&xd->mi[0]->bmi[i], &best_bmodes[i], sizeof(b_mode_info));
4666
4667     mi->mv[0].as_int = xd->mi[0]->bmi[3].as_mv[0].as_int;
4668     mi->mv[1].as_int = xd->mi[0]->bmi[3].as_mv[1].as_int;
4669   }
4670
4671   for (i = 0; i < REFERENCE_MODES; ++i) {
4672     if (best_pred_rd[i] == INT64_MAX)
4673       best_pred_diff[i] = INT_MIN;
4674     else
4675       best_pred_diff[i] = best_rd - best_pred_rd[i];
4676   }
4677
4678   if (!x->skip) {
4679     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
4680       if (best_filter_rd[i] == INT64_MAX)
4681         best_filter_diff[i] = 0;
4682       else
4683         best_filter_diff[i] = best_rd - best_filter_rd[i];
4684     }
4685     if (cm->interp_filter == SWITCHABLE)
4686       assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
4687   } else {
4688     vp9_zero(best_filter_diff);
4689   }
4690
4691   store_coding_context(x, ctx, best_ref_index, best_pred_diff, best_filter_diff,
4692                        0);
4693 }