1 /*
2 * Copyright (c) 2014 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 <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15
16 #include "./vp9_rtcd.h"
17 #include "./vpx_dsp_rtcd.h"
18
19 #include "vpx_dsp/vpx_dsp_common.h"
20 #include "vpx_mem/vpx_mem.h"
21 #include "vpx_ports/mem.h"
22
23 #include "vp9/common/vp9_blockd.h"
24 #include "vp9/common/vp9_common.h"
25 #include "vp9/common/vp9_mvref_common.h"
26 #include "vp9/common/vp9_pred_common.h"
27 #include "vp9/common/vp9_reconinter.h"
28 #include "vp9/common/vp9_reconintra.h"
29 #include "vp9/common/vp9_scan.h"
30
31 #include "vp9/encoder/vp9_cost.h"
32 #include "vp9/encoder/vp9_encoder.h"
33 #include "vp9/encoder/vp9_pickmode.h"
34 #include "vp9/encoder/vp9_ratectrl.h"
35 #include "vp9/encoder/vp9_rd.h"
36
37 typedef struct {
38 uint8_t *data;
39 int stride;
40 int in_use;
41 } PRED_BUFFER;
42
mv_refs_rt(const VP9_COMMON * cm,const MACROBLOCK * x,const MACROBLOCKD * xd,const TileInfo * const tile,MODE_INFO * mi,MV_REFERENCE_FRAME ref_frame,int_mv * mv_ref_list,int mi_row,int mi_col)43 static int mv_refs_rt(const VP9_COMMON *cm, const MACROBLOCK *x,
44 const MACROBLOCKD *xd,
45 const TileInfo *const tile,
46 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
47 int_mv *mv_ref_list,
48 int mi_row, int mi_col) {
49 const int *ref_sign_bias = cm->ref_frame_sign_bias;
50 int i, refmv_count = 0;
51
52 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
53
54 int different_ref_found = 0;
55 int context_counter = 0;
56 int const_motion = 0;
57
58 // Blank the reference vector list
59 memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
60
61 // The nearest 2 blocks are treated differently
62 // if the size < 8x8 we get the mv from the bmi substructure,
63 // and we also need to keep a mode count.
64 for (i = 0; i < 2; ++i) {
65 const POSITION *const mv_ref = &mv_ref_search[i];
66 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
67 const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
68 xd->mi_stride];
69 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
70 // Keep counts for entropy encoding.
71 context_counter += mode_2_counter[candidate->mode];
72 different_ref_found = 1;
73
74 if (candidate->ref_frame[0] == ref_frame)
75 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
76 refmv_count, mv_ref_list, Done);
77 }
78 }
79
80 const_motion = 1;
81
82 // Check the rest of the neighbors in much the same way
83 // as before except we don't need to keep track of sub blocks or
84 // mode counts.
85 for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
86 const POSITION *const mv_ref = &mv_ref_search[i];
87 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
88 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
89 xd->mi_stride]->mbmi;
90 different_ref_found = 1;
91
92 if (candidate->ref_frame[0] == ref_frame)
93 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done);
94 }
95 }
96
97 // Since we couldn't find 2 mvs from the same reference frame
98 // go back through the neighbors and find motion vectors from
99 // different reference frames.
100 if (different_ref_found && !refmv_count) {
101 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
102 const POSITION *mv_ref = &mv_ref_search[i];
103 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
104 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
105 * xd->mi_stride]->mbmi;
106
107 // If the candidate is INTRA we don't want to consider its mv.
108 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
109 refmv_count, mv_ref_list, Done);
110 }
111 }
112 }
113
114 Done:
115
116 x->mbmi_ext->mode_context[ref_frame] = counter_to_context[context_counter];
117
118 // Clamp vectors
119 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
120 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
121
122 return const_motion;
123 }
124
combined_motion_search(VP9_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,int_mv * tmp_mv,int * rate_mv,int64_t best_rd_sofar)125 static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
126 BLOCK_SIZE bsize, int mi_row, int mi_col,
127 int_mv *tmp_mv, int *rate_mv,
128 int64_t best_rd_sofar) {
129 MACROBLOCKD *xd = &x->e_mbd;
130 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
131 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
132 const int step_param = cpi->sf.mv.fullpel_search_step_param;
133 const int sadpb = x->sadperbit16;
134 MV mvp_full;
135 const int ref = mbmi->ref_frame[0];
136 const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
137 int dis;
138 int rate_mode;
139 const int tmp_col_min = x->mv_col_min;
140 const int tmp_col_max = x->mv_col_max;
141 const int tmp_row_min = x->mv_row_min;
142 const int tmp_row_max = x->mv_row_max;
143 int rv = 0;
144 int cost_list[5];
145 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
146 ref);
147 if (scaled_ref_frame) {
148 int i;
149 // Swap out the reference frame for a version that's been scaled to
150 // match the resolution of the current frame, allowing the existing
151 // motion search code to be used without additional modifications.
152 for (i = 0; i < MAX_MB_PLANE; i++)
153 backup_yv12[i] = xd->plane[i].pre[0];
154 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
155 }
156 vp9_set_mv_search_range(x, &ref_mv);
157
158 assert(x->mv_best_ref_index[ref] <= 2);
159 if (x->mv_best_ref_index[ref] < 2)
160 mvp_full = x->mbmi_ext->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
161 else
162 mvp_full = x->pred_mv[ref];
163
164 mvp_full.col >>= 3;
165 mvp_full.row >>= 3;
166
167 vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
168 cond_cost_list(cpi, cost_list),
169 &ref_mv, &tmp_mv->as_mv, INT_MAX, 0);
170
171 x->mv_col_min = tmp_col_min;
172 x->mv_col_max = tmp_col_max;
173 x->mv_row_min = tmp_row_min;
174 x->mv_row_max = tmp_row_max;
175
176 // calculate the bit cost on motion vector
177 mvp_full.row = tmp_mv->as_mv.row * 8;
178 mvp_full.col = tmp_mv->as_mv.col * 8;
179
180 *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv,
181 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
182
183 rate_mode = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref]]
184 [INTER_OFFSET(NEWMV)];
185 rv = !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) >
186 best_rd_sofar);
187
188 if (rv) {
189 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
190 cpi->common.allow_high_precision_mv,
191 x->errorperbit,
192 &cpi->fn_ptr[bsize],
193 cpi->sf.mv.subpel_force_stop,
194 cpi->sf.mv.subpel_iters_per_step,
195 cond_cost_list(cpi, cost_list),
196 x->nmvjointcost, x->mvcost,
197 &dis, &x->pred_sse[ref], NULL, 0, 0);
198 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv,
199 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
200 }
201
202 if (scaled_ref_frame) {
203 int i;
204 for (i = 0; i < MAX_MB_PLANE; i++)
205 xd->plane[i].pre[0] = backup_yv12[i];
206 }
207 return rv;
208 }
209
block_variance(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h,unsigned int * sse,int * sum,int block_size,unsigned int * sse8x8,int * sum8x8,unsigned int * var8x8)210 static void block_variance(const uint8_t *src, int src_stride,
211 const uint8_t *ref, int ref_stride,
212 int w, int h, unsigned int *sse, int *sum,
213 int block_size, unsigned int *sse8x8,
214 int *sum8x8, unsigned int *var8x8) {
215 int i, j, k = 0;
216
217 *sse = 0;
218 *sum = 0;
219
220 for (i = 0; i < h; i += block_size) {
221 for (j = 0; j < w; j += block_size) {
222 vpx_get8x8var(src + src_stride * i + j, src_stride,
223 ref + ref_stride * i + j, ref_stride,
224 &sse8x8[k], &sum8x8[k]);
225 *sse += sse8x8[k];
226 *sum += sum8x8[k];
227 var8x8[k] = sse8x8[k] - (((unsigned int)sum8x8[k] * sum8x8[k]) >> 6);
228 k++;
229 }
230 }
231 }
232
calculate_variance(int bw,int bh,TX_SIZE tx_size,unsigned int * sse_i,int * sum_i,unsigned int * var_o,unsigned int * sse_o,int * sum_o)233 static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
234 unsigned int *sse_i, int *sum_i,
235 unsigned int *var_o, unsigned int *sse_o,
236 int *sum_o) {
237 const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
238 const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
239 const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
240 int i, j, k = 0;
241
242 for (i = 0; i < nh; i += 2) {
243 for (j = 0; j < nw; j += 2) {
244 sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
245 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
246 sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
247 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
248 var_o[k] = sse_o[k] - (((unsigned int)sum_o[k] * sum_o[k]) >>
249 (b_width_log2_lookup[unit_size] +
250 b_height_log2_lookup[unit_size] + 6));
251 k++;
252 }
253 }
254 }
255
model_rd_for_sb_y_large(VP9_COMP * cpi,BLOCK_SIZE bsize,MACROBLOCK * x,MACROBLOCKD * xd,int * out_rate_sum,int64_t * out_dist_sum,unsigned int * var_y,unsigned int * sse_y,int mi_row,int mi_col,int * early_term)256 static void model_rd_for_sb_y_large(VP9_COMP *cpi, BLOCK_SIZE bsize,
257 MACROBLOCK *x, MACROBLOCKD *xd,
258 int *out_rate_sum, int64_t *out_dist_sum,
259 unsigned int *var_y, unsigned int *sse_y,
260 int mi_row, int mi_col, int *early_term) {
261 // Note our transform coeffs are 8 times an orthogonal transform.
262 // Hence quantizer step is also 8 times. To get effective quantizer
263 // we need to divide by 8 before sending to modeling function.
264 unsigned int sse;
265 int rate;
266 int64_t dist;
267 struct macroblock_plane *const p = &x->plane[0];
268 struct macroblockd_plane *const pd = &xd->plane[0];
269 const uint32_t dc_quant = pd->dequant[0];
270 const uint32_t ac_quant = pd->dequant[1];
271 const int64_t dc_thr = dc_quant * dc_quant >> 6;
272 const int64_t ac_thr = ac_quant * ac_quant >> 6;
273 unsigned int var;
274 int sum;
275 int skip_dc = 0;
276
277 const int bw = b_width_log2_lookup[bsize];
278 const int bh = b_height_log2_lookup[bsize];
279 const int num8x8 = 1 << (bw + bh - 2);
280 unsigned int sse8x8[64] = {0};
281 int sum8x8[64] = {0};
282 unsigned int var8x8[64] = {0};
283 TX_SIZE tx_size;
284 int i, k;
285
286 // Calculate variance for whole partition, and also save 8x8 blocks' variance
287 // to be used in following transform skipping test.
288 block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
289 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
290 var = sse - (((int64_t)sum * sum) >> (bw + bh + 4));
291
292 *var_y = var;
293 *sse_y = sse;
294
295 if (cpi->common.tx_mode == TX_MODE_SELECT) {
296 if (sse > (var << 2))
297 tx_size = VPXMIN(max_txsize_lookup[bsize],
298 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
299 else
300 tx_size = TX_8X8;
301
302 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
303 cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id))
304 tx_size = TX_8X8;
305 else if (tx_size > TX_16X16)
306 tx_size = TX_16X16;
307 } else {
308 tx_size = VPXMIN(max_txsize_lookup[bsize],
309 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
310 }
311
312 assert(tx_size >= TX_8X8);
313 xd->mi[0]->mbmi.tx_size = tx_size;
314
315 // Evaluate if the partition block is a skippable block in Y plane.
316 {
317 unsigned int sse16x16[16] = {0};
318 int sum16x16[16] = {0};
319 unsigned int var16x16[16] = {0};
320 const int num16x16 = num8x8 >> 2;
321
322 unsigned int sse32x32[4] = {0};
323 int sum32x32[4] = {0};
324 unsigned int var32x32[4] = {0};
325 const int num32x32 = num8x8 >> 4;
326
327 int ac_test = 1;
328 int dc_test = 1;
329 const int num = (tx_size == TX_8X8) ? num8x8 :
330 ((tx_size == TX_16X16) ? num16x16 : num32x32);
331 const unsigned int *sse_tx = (tx_size == TX_8X8) ? sse8x8 :
332 ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
333 const unsigned int *var_tx = (tx_size == TX_8X8) ? var8x8 :
334 ((tx_size == TX_16X16) ? var16x16 : var32x32);
335
336 // Calculate variance if tx_size > TX_8X8
337 if (tx_size >= TX_16X16)
338 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
339 sum16x16);
340 if (tx_size == TX_32X32)
341 calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
342 sse32x32, sum32x32);
343
344 // Skipping test
345 x->skip_txfm[0] = SKIP_TXFM_NONE;
346 for (k = 0; k < num; k++)
347 // Check if all ac coefficients can be quantized to zero.
348 if (!(var_tx[k] < ac_thr || var == 0)) {
349 ac_test = 0;
350 break;
351 }
352
353 for (k = 0; k < num; k++)
354 // Check if dc coefficient can be quantized to zero.
355 if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
356 dc_test = 0;
357 break;
358 }
359
360 if (ac_test) {
361 x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
362
363 if (dc_test)
364 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
365 } else if (dc_test) {
366 skip_dc = 1;
367 }
368 }
369
370 if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
371 int skip_uv[2] = {0};
372 unsigned int var_uv[2];
373 unsigned int sse_uv[2];
374
375 *out_rate_sum = 0;
376 *out_dist_sum = sse << 4;
377
378 // Transform skipping test in UV planes.
379 for (i = 1; i <= 2; i++) {
380 struct macroblock_plane *const p = &x->plane[i];
381 struct macroblockd_plane *const pd = &xd->plane[i];
382 const TX_SIZE uv_tx_size = get_uv_tx_size(&xd->mi[0]->mbmi, pd);
383 const BLOCK_SIZE unit_size = txsize_to_bsize[uv_tx_size];
384 const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, pd);
385 const int uv_bw = b_width_log2_lookup[uv_bsize];
386 const int uv_bh = b_height_log2_lookup[uv_bsize];
387 const int sf = (uv_bw - b_width_log2_lookup[unit_size]) +
388 (uv_bh - b_height_log2_lookup[unit_size]);
389 const uint32_t uv_dc_thr = pd->dequant[0] * pd->dequant[0] >> (6 - sf);
390 const uint32_t uv_ac_thr = pd->dequant[1] * pd->dequant[1] >> (6 - sf);
391 int j = i - 1;
392
393 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, i);
394 var_uv[j] = cpi->fn_ptr[uv_bsize].vf(p->src.buf, p->src.stride,
395 pd->dst.buf, pd->dst.stride, &sse_uv[j]);
396
397 if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
398 (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
399 skip_uv[j] = 1;
400 else
401 break;
402 }
403
404 // If the transform in YUV planes are skippable, the mode search checks
405 // fewer inter modes and doesn't check intra modes.
406 if (skip_uv[0] & skip_uv[1]) {
407 *early_term = 1;
408 }
409
410 return;
411 }
412
413 if (!skip_dc) {
414 #if CONFIG_VP9_HIGHBITDEPTH
415 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
416 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
417 dc_quant >> (xd->bd - 5), &rate, &dist);
418 } else {
419 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
420 dc_quant >> 3, &rate, &dist);
421 }
422 #else
423 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
424 dc_quant >> 3, &rate, &dist);
425 #endif // CONFIG_VP9_HIGHBITDEPTH
426 }
427
428 if (!skip_dc) {
429 *out_rate_sum = rate >> 1;
430 *out_dist_sum = dist << 3;
431 } else {
432 *out_rate_sum = 0;
433 *out_dist_sum = (sse - var) << 4;
434 }
435
436 #if CONFIG_VP9_HIGHBITDEPTH
437 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
438 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
439 ac_quant >> (xd->bd - 5), &rate, &dist);
440 } else {
441 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
442 ac_quant >> 3, &rate, &dist);
443 }
444 #else
445 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
446 ac_quant >> 3, &rate, &dist);
447 #endif // CONFIG_VP9_HIGHBITDEPTH
448
449 *out_rate_sum += rate;
450 *out_dist_sum += dist << 4;
451 }
452
model_rd_for_sb_y(VP9_COMP * cpi,BLOCK_SIZE bsize,MACROBLOCK * x,MACROBLOCKD * xd,int * out_rate_sum,int64_t * out_dist_sum,unsigned int * var_y,unsigned int * sse_y)453 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
454 MACROBLOCK *x, MACROBLOCKD *xd,
455 int *out_rate_sum, int64_t *out_dist_sum,
456 unsigned int *var_y, unsigned int *sse_y) {
457 // Note our transform coeffs are 8 times an orthogonal transform.
458 // Hence quantizer step is also 8 times. To get effective quantizer
459 // we need to divide by 8 before sending to modeling function.
460 unsigned int sse;
461 int rate;
462 int64_t dist;
463 struct macroblock_plane *const p = &x->plane[0];
464 struct macroblockd_plane *const pd = &xd->plane[0];
465 const int64_t dc_thr = p->quant_thred[0] >> 6;
466 const int64_t ac_thr = p->quant_thred[1] >> 6;
467 const uint32_t dc_quant = pd->dequant[0];
468 const uint32_t ac_quant = pd->dequant[1];
469 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
470 pd->dst.buf, pd->dst.stride, &sse);
471 int skip_dc = 0;
472
473 *var_y = var;
474 *sse_y = sse;
475
476 if (cpi->common.tx_mode == TX_MODE_SELECT) {
477 if (sse > (var << 2))
478 xd->mi[0]->mbmi.tx_size =
479 VPXMIN(max_txsize_lookup[bsize],
480 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
481 else
482 xd->mi[0]->mbmi.tx_size = TX_8X8;
483
484 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
485 cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id))
486 xd->mi[0]->mbmi.tx_size = TX_8X8;
487 else if (xd->mi[0]->mbmi.tx_size > TX_16X16)
488 xd->mi[0]->mbmi.tx_size = TX_16X16;
489 } else {
490 xd->mi[0]->mbmi.tx_size =
491 VPXMIN(max_txsize_lookup[bsize],
492 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
493 }
494
495 // Evaluate if the partition block is a skippable block in Y plane.
496 {
497 const BLOCK_SIZE unit_size =
498 txsize_to_bsize[xd->mi[0]->mbmi.tx_size];
499 const unsigned int num_blk_log2 =
500 (b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
501 (b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]);
502 const unsigned int sse_tx = sse >> num_blk_log2;
503 const unsigned int var_tx = var >> num_blk_log2;
504
505 x->skip_txfm[0] = SKIP_TXFM_NONE;
506 // Check if all ac coefficients can be quantized to zero.
507 if (var_tx < ac_thr || var == 0) {
508 x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
509 // Check if dc coefficient can be quantized to zero.
510 if (sse_tx - var_tx < dc_thr || sse == var)
511 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
512 } else {
513 if (sse_tx - var_tx < dc_thr || sse == var)
514 skip_dc = 1;
515 }
516 }
517
518 if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
519 *out_rate_sum = 0;
520 *out_dist_sum = sse << 4;
521 return;
522 }
523
524 if (!skip_dc) {
525 #if CONFIG_VP9_HIGHBITDEPTH
526 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
527 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
528 dc_quant >> (xd->bd - 5), &rate, &dist);
529 } else {
530 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
531 dc_quant >> 3, &rate, &dist);
532 }
533 #else
534 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
535 dc_quant >> 3, &rate, &dist);
536 #endif // CONFIG_VP9_HIGHBITDEPTH
537 }
538
539 if (!skip_dc) {
540 *out_rate_sum = rate >> 1;
541 *out_dist_sum = dist << 3;
542 } else {
543 *out_rate_sum = 0;
544 *out_dist_sum = (sse - var) << 4;
545 }
546
547 #if CONFIG_VP9_HIGHBITDEPTH
548 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
549 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
550 ac_quant >> (xd->bd - 5), &rate, &dist);
551 } else {
552 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
553 ac_quant >> 3, &rate, &dist);
554 }
555 #else
556 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
557 ac_quant >> 3, &rate, &dist);
558 #endif // CONFIG_VP9_HIGHBITDEPTH
559
560 *out_rate_sum += rate;
561 *out_dist_sum += dist << 4;
562 }
563
564 #if CONFIG_VP9_HIGHBITDEPTH
block_yrd(VP9_COMP * cpi,MACROBLOCK * x,int * rate,int64_t * dist,int * skippable,int64_t * sse,int plane,BLOCK_SIZE bsize,TX_SIZE tx_size)565 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
566 int *skippable, int64_t *sse, int plane,
567 BLOCK_SIZE bsize, TX_SIZE tx_size) {
568 MACROBLOCKD *xd = &x->e_mbd;
569 unsigned int var_y, sse_y;
570 (void)plane;
571 (void)tx_size;
572 model_rd_for_sb_y(cpi, bsize, x, xd, rate, dist, &var_y, &sse_y);
573 *sse = INT_MAX;
574 *skippable = 0;
575 return;
576 }
577 #else
block_yrd(VP9_COMP * cpi,MACROBLOCK * x,int * rate,int64_t * dist,int * skippable,int64_t * sse,int plane,BLOCK_SIZE bsize,TX_SIZE tx_size)578 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
579 int *skippable, int64_t *sse, int plane,
580 BLOCK_SIZE bsize, TX_SIZE tx_size) {
581 MACROBLOCKD *xd = &x->e_mbd;
582 const struct macroblockd_plane *pd = &xd->plane[plane];
583 const struct macroblock_plane *const p = &x->plane[plane];
584 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
585 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
586 const int step = 1 << (tx_size << 1);
587 const int block_step = (1 << tx_size);
588 int block = 0, r, c;
589 int shift = tx_size == TX_32X32 ? 0 : 2;
590 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 :
591 xd->mb_to_right_edge >> (5 + pd->subsampling_x));
592 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 :
593 xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
594 int eob_cost = 0;
595
596 (void)cpi;
597 vp9_subtract_plane(x, bsize, plane);
598 *skippable = 1;
599 // Keep track of the row and column of the blocks we use so that we know
600 // if we are in the unrestricted motion border.
601 for (r = 0; r < max_blocks_high; r += block_step) {
602 for (c = 0; c < num_4x4_w; c += block_step) {
603 if (c < max_blocks_wide) {
604 const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
605 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
606 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
607 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
608 uint16_t *const eob = &p->eobs[block];
609 const int diff_stride = 4 * num_4x4_blocks_wide_lookup[bsize];
610 const int16_t *src_diff;
611 src_diff = &p->src_diff[(r * diff_stride + c) << 2];
612
613 switch (tx_size) {
614 case TX_32X32:
615 vpx_fdct32x32_rd(src_diff, coeff, diff_stride);
616 vp9_quantize_fp_32x32(coeff, 1024, x->skip_block, p->zbin,
617 p->round_fp, p->quant_fp, p->quant_shift,
618 qcoeff, dqcoeff, pd->dequant, eob,
619 scan_order->scan, scan_order->iscan);
620 break;
621 case TX_16X16:
622 vp9_hadamard_16x16(src_diff, diff_stride, (int16_t *)coeff);
623 vp9_quantize_fp(coeff, 256, x->skip_block, p->zbin, p->round_fp,
624 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
625 pd->dequant, eob,
626 scan_order->scan, scan_order->iscan);
627 break;
628 case TX_8X8:
629 vp9_hadamard_8x8(src_diff, diff_stride, (int16_t *)coeff);
630 vp9_quantize_fp(coeff, 64, x->skip_block, p->zbin, p->round_fp,
631 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
632 pd->dequant, eob,
633 scan_order->scan, scan_order->iscan);
634 break;
635 case TX_4X4:
636 x->fwd_txm4x4(src_diff, coeff, diff_stride);
637 vp9_quantize_fp(coeff, 16, x->skip_block, p->zbin, p->round_fp,
638 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
639 pd->dequant, eob,
640 scan_order->scan, scan_order->iscan);
641 break;
642 default:
643 assert(0);
644 break;
645 }
646 *skippable &= (*eob == 0);
647 eob_cost += 1;
648 }
649 block += step;
650 }
651 }
652
653 if (*skippable && *sse < INT64_MAX) {
654 *rate = 0;
655 *dist = (*sse << 6) >> shift;
656 *sse = *dist;
657 return;
658 }
659
660 block = 0;
661 *rate = 0;
662 *dist = 0;
663 if (*sse < INT64_MAX)
664 *sse = (*sse << 6) >> shift;
665 for (r = 0; r < max_blocks_high; r += block_step) {
666 for (c = 0; c < num_4x4_w; c += block_step) {
667 if (c < max_blocks_wide) {
668 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
669 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
670 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
671 uint16_t *const eob = &p->eobs[block];
672
673 if (*eob == 1)
674 *rate += (int)abs(qcoeff[0]);
675 else if (*eob > 1)
676 *rate += (int)vp9_satd((const int16_t *)qcoeff, step << 4);
677
678 *dist += vp9_block_error_fp(coeff, dqcoeff, step << 4) >> shift;
679 }
680 block += step;
681 }
682 }
683
684 if (*skippable == 0) {
685 *rate <<= 10;
686 *rate += (eob_cost << 8);
687 }
688 }
689 #endif
690
model_rd_for_sb_uv(VP9_COMP * cpi,BLOCK_SIZE plane_bsize,MACROBLOCK * x,MACROBLOCKD * xd,int * out_rate_sum,int64_t * out_dist_sum,unsigned int * var_y,unsigned int * sse_y,int start_plane,int stop_plane)691 static void model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE plane_bsize,
692 MACROBLOCK *x, MACROBLOCKD *xd,
693 int *out_rate_sum, int64_t *out_dist_sum,
694 unsigned int *var_y, unsigned int *sse_y,
695 int start_plane, int stop_plane) {
696 // Note our transform coeffs are 8 times an orthogonal transform.
697 // Hence quantizer step is also 8 times. To get effective quantizer
698 // we need to divide by 8 before sending to modeling function.
699 unsigned int sse;
700 int rate;
701 int64_t dist;
702 int i;
703
704 *out_rate_sum = 0;
705 *out_dist_sum = 0;
706
707 for (i = start_plane; i <= stop_plane; ++i) {
708 struct macroblock_plane *const p = &x->plane[i];
709 struct macroblockd_plane *const pd = &xd->plane[i];
710 const uint32_t dc_quant = pd->dequant[0];
711 const uint32_t ac_quant = pd->dequant[1];
712 const BLOCK_SIZE bs = plane_bsize;
713 unsigned int var;
714
715 if (!x->color_sensitivity[i - 1])
716 continue;
717
718 var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride,
719 pd->dst.buf, pd->dst.stride, &sse);
720 *var_y += var;
721 *sse_y += sse;
722
723 #if CONFIG_VP9_HIGHBITDEPTH
724 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
725 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
726 dc_quant >> (xd->bd - 5), &rate, &dist);
727 } else {
728 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
729 dc_quant >> 3, &rate, &dist);
730 }
731 #else
732 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
733 dc_quant >> 3, &rate, &dist);
734 #endif // CONFIG_VP9_HIGHBITDEPTH
735
736 *out_rate_sum += rate >> 1;
737 *out_dist_sum += dist << 3;
738
739 #if CONFIG_VP9_HIGHBITDEPTH
740 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
741 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
742 ac_quant >> (xd->bd - 5), &rate, &dist);
743 } else {
744 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
745 ac_quant >> 3, &rate, &dist);
746 }
747 #else
748 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
749 ac_quant >> 3, &rate, &dist);
750 #endif // CONFIG_VP9_HIGHBITDEPTH
751
752 *out_rate_sum += rate;
753 *out_dist_sum += dist << 4;
754 }
755 }
756
get_pred_buffer(PRED_BUFFER * p,int len)757 static int get_pred_buffer(PRED_BUFFER *p, int len) {
758 int i;
759
760 for (i = 0; i < len; i++) {
761 if (!p[i].in_use) {
762 p[i].in_use = 1;
763 return i;
764 }
765 }
766 return -1;
767 }
768
free_pred_buffer(PRED_BUFFER * p)769 static void free_pred_buffer(PRED_BUFFER *p) {
770 if (p != NULL)
771 p->in_use = 0;
772 }
773
encode_breakout_test(VP9_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,MV_REFERENCE_FRAME ref_frame,PREDICTION_MODE this_mode,unsigned int var_y,unsigned int sse_y,struct buf_2d yv12_mb[][MAX_MB_PLANE],int * rate,int64_t * dist)774 static void encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
775 BLOCK_SIZE bsize, int mi_row, int mi_col,
776 MV_REFERENCE_FRAME ref_frame,
777 PREDICTION_MODE this_mode,
778 unsigned int var_y, unsigned int sse_y,
779 struct buf_2d yv12_mb[][MAX_MB_PLANE],
780 int *rate, int64_t *dist) {
781 MACROBLOCKD *xd = &x->e_mbd;
782
783 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
784 unsigned int var = var_y, sse = sse_y;
785 // Skipping threshold for ac.
786 unsigned int thresh_ac;
787 // Skipping threshold for dc.
788 unsigned int thresh_dc;
789 if (x->encode_breakout > 0) {
790 // Set a maximum for threshold to avoid big PSNR loss in low bit rate
791 // case. Use extreme low threshold for static frames to limit
792 // skipping.
793 const unsigned int max_thresh = 36000;
794 // The encode_breakout input
795 const unsigned int min_thresh =
796 VPXMIN(((unsigned int)x->encode_breakout << 4), max_thresh);
797 #if CONFIG_VP9_HIGHBITDEPTH
798 const int shift = (xd->bd << 1) - 16;
799 #endif
800
801 // Calculate threshold according to dequant value.
802 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) >> 3;
803 #if CONFIG_VP9_HIGHBITDEPTH
804 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
805 thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
806 }
807 #endif // CONFIG_VP9_HIGHBITDEPTH
808 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
809
810 // Adjust ac threshold according to partition size.
811 thresh_ac >>=
812 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
813
814 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
815 #if CONFIG_VP9_HIGHBITDEPTH
816 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
817 thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
818 }
819 #endif // CONFIG_VP9_HIGHBITDEPTH
820 } else {
821 thresh_ac = 0;
822 thresh_dc = 0;
823 }
824
825 // Y skipping condition checking for ac and dc.
826 if (var <= thresh_ac && (sse - var) <= thresh_dc) {
827 unsigned int sse_u, sse_v;
828 unsigned int var_u, var_v;
829
830 // Skip UV prediction unless breakout is zero (lossless) to save
831 // computation with low impact on the result
832 if (x->encode_breakout == 0) {
833 xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
834 xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
835 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
836 }
837
838 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
839 x->plane[1].src.stride,
840 xd->plane[1].dst.buf,
841 xd->plane[1].dst.stride, &sse_u);
842
843 // U skipping condition checking
844 if (((var_u << 2) <= thresh_ac) && (sse_u - var_u <= thresh_dc)) {
845 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
846 x->plane[2].src.stride,
847 xd->plane[2].dst.buf,
848 xd->plane[2].dst.stride, &sse_v);
849
850 // V skipping condition checking
851 if (((var_v << 2) <= thresh_ac) && (sse_v - var_v <= thresh_dc)) {
852 x->skip = 1;
853
854 // The cost of skip bit needs to be added.
855 *rate = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
856 [INTER_OFFSET(this_mode)];
857
858 // More on this part of rate
859 // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
860
861 // Scaling factor for SSE from spatial domain to frequency
862 // domain is 16. Adjust distortion accordingly.
863 // TODO(yunqingwang): In this function, only y-plane dist is
864 // calculated.
865 *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
866
867 // *disable_skip = 1;
868 }
869 }
870 }
871 }
872
873 struct estimate_block_intra_args {
874 VP9_COMP *cpi;
875 MACROBLOCK *x;
876 PREDICTION_MODE mode;
877 int rate;
878 int64_t dist;
879 };
880
estimate_block_intra(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)881 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
882 TX_SIZE tx_size, void *arg) {
883 struct estimate_block_intra_args* const args = arg;
884 VP9_COMP *const cpi = args->cpi;
885 MACROBLOCK *const x = args->x;
886 MACROBLOCKD *const xd = &x->e_mbd;
887 struct macroblock_plane *const p = &x->plane[0];
888 struct macroblockd_plane *const pd = &xd->plane[0];
889 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
890 uint8_t *const src_buf_base = p->src.buf;
891 uint8_t *const dst_buf_base = pd->dst.buf;
892 const int src_stride = p->src.stride;
893 const int dst_stride = pd->dst.stride;
894 int i, j;
895 int rate;
896 int64_t dist;
897
898 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
899
900 p->src.buf = &src_buf_base[4 * (j * src_stride + i)];
901 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)];
902 // Use source buffer as an approximation for the fully reconstructed buffer.
903 vp9_predict_intra_block(xd, b_width_log2_lookup[plane_bsize],
904 tx_size, args->mode,
905 x->skip_encode ? p->src.buf : pd->dst.buf,
906 x->skip_encode ? src_stride : dst_stride,
907 pd->dst.buf, dst_stride,
908 i, j, plane);
909
910 if (plane == 0) {
911 int64_t this_sse = INT64_MAX;
912 int is_skippable;
913 // TODO(jingning): This needs further refactoring.
914 block_yrd(cpi, x, &rate, &dist, &is_skippable, &this_sse, 0,
915 bsize_tx, VPXMIN(tx_size, TX_16X16));
916 x->skip_txfm[0] = is_skippable;
917 // TODO(jingning): Skip is signalled per prediciton block not per tx block.
918 rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), is_skippable);
919 } else {
920 unsigned int var, sse;
921 model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &rate, &dist, &var, &sse,
922 plane, plane);
923 }
924
925 p->src.buf = src_buf_base;
926 pd->dst.buf = dst_buf_base;
927 args->rate += rate;
928 args->dist += dist;
929 }
930
931 static const THR_MODES mode_idx[MAX_REF_FRAMES - 1][4] = {
932 {THR_DC, THR_V_PRED, THR_H_PRED, THR_TM},
933 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV},
934 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG},
935 };
936
937 static const PREDICTION_MODE intra_mode_list[] = {
938 DC_PRED, V_PRED, H_PRED, TM_PRED
939 };
940
mode_offset(const PREDICTION_MODE mode)941 static int mode_offset(const PREDICTION_MODE mode) {
942 if (mode >= NEARESTMV) {
943 return INTER_OFFSET(mode);
944 } else {
945 switch (mode) {
946 case DC_PRED:
947 return 0;
948 case V_PRED:
949 return 1;
950 case H_PRED:
951 return 2;
952 case TM_PRED:
953 return 3;
954 default:
955 return -1;
956 }
957 }
958 }
959
update_thresh_freq_fact(VP9_COMP * cpi,TileDataEnc * tile_data,BLOCK_SIZE bsize,MV_REFERENCE_FRAME ref_frame,THR_MODES best_mode_idx,PREDICTION_MODE mode)960 static INLINE void update_thresh_freq_fact(VP9_COMP *cpi,
961 TileDataEnc *tile_data,
962 BLOCK_SIZE bsize,
963 MV_REFERENCE_FRAME ref_frame,
964 THR_MODES best_mode_idx,
965 PREDICTION_MODE mode) {
966 THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
967 int *freq_fact = &tile_data->thresh_freq_fact[bsize][thr_mode_idx];
968 if (thr_mode_idx == best_mode_idx)
969 *freq_fact -= (*freq_fact >> 4);
970 else
971 *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
972 cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
973 }
974
vp9_pick_intra_mode(VP9_COMP * cpi,MACROBLOCK * x,RD_COST * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)975 void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
976 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
977 MACROBLOCKD *const xd = &x->e_mbd;
978 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
979 RD_COST this_rdc, best_rdc;
980 PREDICTION_MODE this_mode;
981 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
982 const TX_SIZE intra_tx_size =
983 VPXMIN(max_txsize_lookup[bsize],
984 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
985 MODE_INFO *const mic = xd->mi[0];
986 int *bmode_costs;
987 const MODE_INFO *above_mi = xd->mi[-xd->mi_stride];
988 const MODE_INFO *left_mi = xd->left_available ? xd->mi[-1] : NULL;
989 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
990 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
991 bmode_costs = cpi->y_mode_costs[A][L];
992
993 (void) ctx;
994 vp9_rd_cost_reset(&best_rdc);
995 vp9_rd_cost_reset(&this_rdc);
996
997 mbmi->ref_frame[0] = INTRA_FRAME;
998 mbmi->mv[0].as_int = INVALID_MV;
999 mbmi->uv_mode = DC_PRED;
1000 memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
1001
1002 // Change the limit of this loop to add other intra prediction
1003 // mode tests.
1004 for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
1005 args.mode = this_mode;
1006 args.rate = 0;
1007 args.dist = 0;
1008 mbmi->tx_size = intra_tx_size;
1009 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
1010 estimate_block_intra, &args);
1011 this_rdc.rate = args.rate;
1012 this_rdc.dist = args.dist;
1013 this_rdc.rate += bmode_costs[this_mode];
1014 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1015 this_rdc.rate, this_rdc.dist);
1016
1017 if (this_rdc.rdcost < best_rdc.rdcost) {
1018 best_rdc = this_rdc;
1019 mbmi->mode = this_mode;
1020 }
1021 }
1022
1023 *rd_cost = best_rdc;
1024 }
1025
init_ref_frame_cost(VP9_COMMON * const cm,MACROBLOCKD * const xd,int ref_frame_cost[MAX_REF_FRAMES])1026 static void init_ref_frame_cost(VP9_COMMON *const cm,
1027 MACROBLOCKD *const xd,
1028 int ref_frame_cost[MAX_REF_FRAMES]) {
1029 vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
1030 vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
1031 vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
1032
1033 ref_frame_cost[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
1034 ref_frame_cost[LAST_FRAME] = ref_frame_cost[GOLDEN_FRAME] =
1035 ref_frame_cost[ALTREF_FRAME] = vp9_cost_bit(intra_inter_p, 1);
1036
1037 ref_frame_cost[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
1038 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1039 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1040 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
1041 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
1042 }
1043
1044 typedef struct {
1045 MV_REFERENCE_FRAME ref_frame;
1046 PREDICTION_MODE pred_mode;
1047 } REF_MODE;
1048
1049 #define RT_INTER_MODES 8
1050 static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
1051 {LAST_FRAME, ZEROMV},
1052 {LAST_FRAME, NEARESTMV},
1053 {GOLDEN_FRAME, ZEROMV},
1054 {LAST_FRAME, NEARMV},
1055 {LAST_FRAME, NEWMV},
1056 {GOLDEN_FRAME, NEARESTMV},
1057 {GOLDEN_FRAME, NEARMV},
1058 {GOLDEN_FRAME, NEWMV}
1059 };
1060 static const REF_MODE ref_mode_set_svc[RT_INTER_MODES] = {
1061 {LAST_FRAME, ZEROMV},
1062 {GOLDEN_FRAME, ZEROMV},
1063 {LAST_FRAME, NEARESTMV},
1064 {LAST_FRAME, NEARMV},
1065 {GOLDEN_FRAME, NEARESTMV},
1066 {GOLDEN_FRAME, NEARMV},
1067 {LAST_FRAME, NEWMV},
1068 {GOLDEN_FRAME, NEWMV}
1069 };
1070
1071 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
1072 // this needs various further optimizations. to be continued..
vp9_pick_inter_mode(VP9_COMP * cpi,MACROBLOCK * x,TileDataEnc * tile_data,int mi_row,int mi_col,RD_COST * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)1073 void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
1074 TileDataEnc *tile_data,
1075 int mi_row, int mi_col, RD_COST *rd_cost,
1076 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1077 VP9_COMMON *const cm = &cpi->common;
1078 SPEED_FEATURES *const sf = &cpi->sf;
1079 TileInfo *const tile_info = &tile_data->tile_info;
1080 MACROBLOCKD *const xd = &x->e_mbd;
1081 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1082 struct macroblockd_plane *const pd = &xd->plane[0];
1083 PREDICTION_MODE best_mode = ZEROMV;
1084 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
1085 MV_REFERENCE_FRAME usable_ref_frame;
1086 TX_SIZE best_tx_size = TX_SIZES;
1087 INTERP_FILTER best_pred_filter = EIGHTTAP;
1088 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
1089 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
1090 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1091 VP9_ALT_FLAG };
1092 RD_COST this_rdc, best_rdc;
1093 uint8_t skip_txfm = SKIP_TXFM_NONE, best_mode_skip_txfm = SKIP_TXFM_NONE;
1094 // var_y and sse_y are saved to be used in skipping checking
1095 unsigned int var_y = UINT_MAX;
1096 unsigned int sse_y = UINT_MAX;
1097 // Reduce the intra cost penalty for small blocks (<=16x16).
1098 const int reduction_fac = (bsize <= BLOCK_16X16) ?
1099 ((bsize <= BLOCK_8X8) ? 4 : 2) : 0;
1100 const int intra_cost_penalty = vp9_get_intra_cost_penalty(
1101 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth) >> reduction_fac;
1102 const int64_t inter_mode_thresh = RDCOST(x->rdmult, x->rddiv,
1103 intra_cost_penalty, 0);
1104 const int *const rd_threshes = cpi->rd.threshes[mbmi->segment_id][bsize];
1105 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
1106 INTERP_FILTER filter_ref;
1107 const int bsl = mi_width_log2_lookup[bsize];
1108 const int pred_filter_search = cm->interp_filter == SWITCHABLE ?
1109 (((mi_row + mi_col) >> bsl) +
1110 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
1111 int const_motion[MAX_REF_FRAMES] = { 0 };
1112 const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
1113 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
1114 // For speed 6, the result of interp filter is reused later in actual encoding
1115 // process.
1116 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
1117 PRED_BUFFER tmp[4];
1118 DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 64 * 64]);
1119 #if CONFIG_VP9_HIGHBITDEPTH
1120 DECLARE_ALIGNED(16, uint16_t, pred_buf_16[3 * 64 * 64]);
1121 #endif
1122 struct buf_2d orig_dst = pd->dst;
1123 PRED_BUFFER *best_pred = NULL;
1124 PRED_BUFFER *this_mode_pred = NULL;
1125 const int pixels_in_block = bh * bw;
1126 int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
1127 int ref_frame_skip_mask = 0;
1128 int idx;
1129 int best_pred_sad = INT_MAX;
1130 int best_early_term = 0;
1131 int ref_frame_cost[MAX_REF_FRAMES];
1132
1133 init_ref_frame_cost(cm, xd, ref_frame_cost);
1134
1135 if (reuse_inter_pred) {
1136 int i;
1137 for (i = 0; i < 3; i++) {
1138 #if CONFIG_VP9_HIGHBITDEPTH
1139 if (cm->use_highbitdepth)
1140 tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
1141 else
1142 tmp[i].data = &pred_buf[pixels_in_block * i];
1143 #else
1144 tmp[i].data = &pred_buf[pixels_in_block * i];
1145 #endif // CONFIG_VP9_HIGHBITDEPTH
1146 tmp[i].stride = bw;
1147 tmp[i].in_use = 0;
1148 }
1149 tmp[3].data = pd->dst.buf;
1150 tmp[3].stride = pd->dst.stride;
1151 tmp[3].in_use = 0;
1152 }
1153
1154 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1155 x->skip = 0;
1156
1157 if (xd->up_available)
1158 filter_ref = xd->mi[-xd->mi_stride]->mbmi.interp_filter;
1159 else if (xd->left_available)
1160 filter_ref = xd->mi[-1]->mbmi.interp_filter;
1161 else
1162 filter_ref = cm->interp_filter;
1163
1164 // initialize mode decisions
1165 vp9_rd_cost_reset(&best_rdc);
1166 vp9_rd_cost_reset(rd_cost);
1167 mbmi->sb_type = bsize;
1168 mbmi->ref_frame[0] = NONE;
1169 mbmi->ref_frame[1] = NONE;
1170 mbmi->tx_size = VPXMIN(max_txsize_lookup[bsize],
1171 tx_mode_to_biggest_tx_size[cm->tx_mode]);
1172
1173 #if CONFIG_VP9_TEMPORAL_DENOISING
1174 vp9_denoiser_reset_frame_stats(ctx);
1175 #endif
1176
1177 if (cpi->rc.frames_since_golden == 0 && !cpi->use_svc) {
1178 usable_ref_frame = LAST_FRAME;
1179 } else {
1180 usable_ref_frame = GOLDEN_FRAME;
1181 }
1182 for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
1183 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1184
1185 x->pred_mv_sad[ref_frame] = INT_MAX;
1186 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
1187 frame_mv[ZEROMV][ref_frame].as_int = 0;
1188
1189 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
1190 int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
1191 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
1192
1193 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
1194 sf, sf);
1195
1196 if (cm->use_prev_frame_mvs)
1197 vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame,
1198 candidates, mi_row, mi_col, NULL, NULL,
1199 x->mbmi_ext->mode_context);
1200 else
1201 const_motion[ref_frame] = mv_refs_rt(cm, x, xd, tile_info,
1202 xd->mi[0],
1203 ref_frame, candidates,
1204 mi_row, mi_col);
1205
1206 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1207 &frame_mv[NEARESTMV][ref_frame],
1208 &frame_mv[NEARMV][ref_frame]);
1209
1210 if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8)
1211 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
1212 ref_frame, bsize);
1213 } else {
1214 ref_frame_skip_mask |= (1 << ref_frame);
1215 }
1216 }
1217
1218 for (idx = 0; idx < RT_INTER_MODES; ++idx) {
1219 int rate_mv = 0;
1220 int mode_rd_thresh;
1221 int mode_index;
1222 int i;
1223 int64_t this_sse;
1224 int is_skippable;
1225 int this_early_term = 0;
1226 PREDICTION_MODE this_mode = ref_mode_set[idx].pred_mode;
1227 if (cpi->use_svc)
1228 this_mode = ref_mode_set_svc[idx].pred_mode;
1229
1230 if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode)))
1231 continue;
1232
1233 ref_frame = ref_mode_set[idx].ref_frame;
1234 if (cpi->use_svc)
1235 ref_frame = ref_mode_set_svc[idx].ref_frame;
1236 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
1237 continue;
1238 if (const_motion[ref_frame] && this_mode == NEARMV)
1239 continue;
1240
1241 if (!(this_mode == ZEROMV && ref_frame == LAST_FRAME)) {
1242 i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
1243 if ((cpi->ref_frame_flags & flag_list[i]) && sf->reference_masking)
1244 if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
1245 ref_frame_skip_mask |= (1 << ref_frame);
1246 }
1247 if (ref_frame_skip_mask & (1 << ref_frame))
1248 continue;
1249
1250 // Select prediction reference frames.
1251 for (i = 0; i < MAX_MB_PLANE; i++)
1252 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
1253
1254 mbmi->ref_frame[0] = ref_frame;
1255 set_ref_ptrs(cm, xd, ref_frame, NONE);
1256
1257 mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
1258 mode_rd_thresh = best_mode_skip_txfm ?
1259 rd_threshes[mode_index] << 1 : rd_threshes[mode_index];
1260 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
1261 rd_thresh_freq_fact[mode_index]))
1262 continue;
1263
1264 if (this_mode == NEWMV) {
1265 if (ref_frame > LAST_FRAME && !cpi->use_svc) {
1266 int tmp_sad;
1267 int dis, cost_list[5];
1268
1269 if (bsize < BLOCK_16X16)
1270 continue;
1271
1272 tmp_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
1273
1274 if (tmp_sad > x->pred_mv_sad[LAST_FRAME])
1275 continue;
1276 if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad)
1277 continue;
1278
1279 frame_mv[NEWMV][ref_frame].as_int = mbmi->mv[0].as_int;
1280 rate_mv = vp9_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv,
1281 &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1282 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
1283 frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
1284 frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
1285
1286 cpi->find_fractional_mv_step(x, &frame_mv[NEWMV][ref_frame].as_mv,
1287 &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1288 cpi->common.allow_high_precision_mv,
1289 x->errorperbit,
1290 &cpi->fn_ptr[bsize],
1291 cpi->sf.mv.subpel_force_stop,
1292 cpi->sf.mv.subpel_iters_per_step,
1293 cond_cost_list(cpi, cost_list),
1294 x->nmvjointcost, x->mvcost, &dis,
1295 &x->pred_sse[ref_frame], NULL, 0, 0);
1296 } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1297 &frame_mv[NEWMV][ref_frame], &rate_mv, best_rdc.rdcost)) {
1298 continue;
1299 }
1300 }
1301
1302 if (this_mode == NEWMV && ref_frame == LAST_FRAME &&
1303 frame_mv[NEWMV][LAST_FRAME].as_int != INVALID_MV) {
1304 const int pre_stride = xd->plane[0].pre[0].stride;
1305 const uint8_t * const pre_buf = xd->plane[0].pre[0].buf +
1306 (frame_mv[NEWMV][LAST_FRAME].as_mv.row >> 3) * pre_stride +
1307 (frame_mv[NEWMV][LAST_FRAME].as_mv.col >> 3);
1308 best_pred_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
1309 x->plane[0].src.stride,
1310 pre_buf, pre_stride);
1311 x->pred_mv_sad[LAST_FRAME] = best_pred_sad;
1312 }
1313
1314 if (cpi->use_svc) {
1315 if (this_mode == NEWMV && ref_frame == GOLDEN_FRAME &&
1316 frame_mv[NEWMV][GOLDEN_FRAME].as_int != INVALID_MV) {
1317 const int pre_stride = xd->plane[0].pre[0].stride;
1318 const uint8_t * const pre_buf = xd->plane[0].pre[0].buf +
1319 (frame_mv[NEWMV][GOLDEN_FRAME].as_mv.row >> 3) * pre_stride +
1320 (frame_mv[NEWMV][GOLDEN_FRAME].as_mv.col >> 3);
1321 best_pred_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
1322 x->plane[0].src.stride,
1323 pre_buf, pre_stride);
1324 x->pred_mv_sad[GOLDEN_FRAME] = best_pred_sad;
1325 }
1326 }
1327
1328
1329 if (this_mode != NEARESTMV &&
1330 frame_mv[this_mode][ref_frame].as_int ==
1331 frame_mv[NEARESTMV][ref_frame].as_int)
1332 continue;
1333
1334 mbmi->mode = this_mode;
1335 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
1336
1337 // Search for the best prediction filter type, when the resulting
1338 // motion vector is at sub-pixel accuracy level for luma component, i.e.,
1339 // the last three bits are all zeros.
1340 if (reuse_inter_pred) {
1341 if (!this_mode_pred) {
1342 this_mode_pred = &tmp[3];
1343 } else {
1344 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1345 pd->dst.buf = this_mode_pred->data;
1346 pd->dst.stride = bw;
1347 }
1348 }
1349
1350 if ((this_mode == NEWMV || filter_ref == SWITCHABLE) && pred_filter_search
1351 && (ref_frame == LAST_FRAME ||
1352 (ref_frame == GOLDEN_FRAME && cpi->use_svc))
1353 && (((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07) != 0)) {
1354 int pf_rate[3];
1355 int64_t pf_dist[3];
1356 unsigned int pf_var[3];
1357 unsigned int pf_sse[3];
1358 TX_SIZE pf_tx_size[3];
1359 int64_t best_cost = INT64_MAX;
1360 INTERP_FILTER best_filter = SWITCHABLE, filter;
1361 PRED_BUFFER *current_pred = this_mode_pred;
1362
1363 for (filter = EIGHTTAP; filter <= EIGHTTAP_SMOOTH; ++filter) {
1364 int64_t cost;
1365 mbmi->interp_filter = filter;
1366 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1367 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter], &pf_dist[filter],
1368 &pf_var[filter], &pf_sse[filter]);
1369 pf_rate[filter] += vp9_get_switchable_rate(cpi, xd);
1370 cost = RDCOST(x->rdmult, x->rddiv, pf_rate[filter], pf_dist[filter]);
1371 pf_tx_size[filter] = mbmi->tx_size;
1372 if (cost < best_cost) {
1373 best_filter = filter;
1374 best_cost = cost;
1375 skip_txfm = x->skip_txfm[0];
1376
1377 if (reuse_inter_pred) {
1378 if (this_mode_pred != current_pred) {
1379 free_pred_buffer(this_mode_pred);
1380 this_mode_pred = current_pred;
1381 }
1382
1383 if (filter < EIGHTTAP_SHARP) {
1384 current_pred = &tmp[get_pred_buffer(tmp, 3)];
1385 pd->dst.buf = current_pred->data;
1386 pd->dst.stride = bw;
1387 }
1388 }
1389 }
1390 }
1391
1392 if (reuse_inter_pred && this_mode_pred != current_pred)
1393 free_pred_buffer(current_pred);
1394
1395 mbmi->interp_filter = best_filter;
1396 mbmi->tx_size = pf_tx_size[best_filter];
1397 this_rdc.rate = pf_rate[best_filter];
1398 this_rdc.dist = pf_dist[best_filter];
1399 var_y = pf_var[best_filter];
1400 sse_y = pf_sse[best_filter];
1401 x->skip_txfm[0] = skip_txfm;
1402 if (reuse_inter_pred) {
1403 pd->dst.buf = this_mode_pred->data;
1404 pd->dst.stride = this_mode_pred->stride;
1405 }
1406 } else {
1407 mbmi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP : filter_ref;
1408 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1409
1410 // For large partition blocks, extra testing is done.
1411 if (bsize > BLOCK_32X32 &&
1412 !cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id) &&
1413 cm->base_qindex) {
1414 model_rd_for_sb_y_large(cpi, bsize, x, xd, &this_rdc.rate,
1415 &this_rdc.dist, &var_y, &sse_y, mi_row, mi_col,
1416 &this_early_term);
1417 } else {
1418 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1419 &var_y, &sse_y);
1420 }
1421 }
1422
1423 if (!this_early_term) {
1424 this_sse = (int64_t)sse_y;
1425 block_yrd(cpi, x, &this_rdc.rate, &this_rdc.dist, &is_skippable,
1426 &this_sse, 0, bsize, VPXMIN(mbmi->tx_size, TX_16X16));
1427 x->skip_txfm[0] = is_skippable;
1428 if (is_skippable) {
1429 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1430 } else {
1431 if (RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist) <
1432 RDCOST(x->rdmult, x->rddiv, 0, this_sse)) {
1433 this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
1434 } else {
1435 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1436 this_rdc.dist = this_sse;
1437 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
1438 }
1439 }
1440
1441 if (cm->interp_filter == SWITCHABLE) {
1442 if ((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07)
1443 this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
1444 }
1445 } else {
1446 this_rdc.rate += cm->interp_filter == SWITCHABLE ?
1447 vp9_get_switchable_rate(cpi, xd) : 0;
1448 this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1449 }
1450
1451 if (x->color_sensitivity[0] || x->color_sensitivity[1]) {
1452 int uv_rate = 0;
1453 int64_t uv_dist = 0;
1454 const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, &xd->plane[1]);
1455 if (x->color_sensitivity[0])
1456 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 1);
1457 if (x->color_sensitivity[1])
1458 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 2);
1459 model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &uv_rate, &uv_dist,
1460 &var_y, &sse_y, 1, 2);
1461 this_rdc.rate += uv_rate;
1462 this_rdc.dist += uv_dist;
1463 }
1464
1465 this_rdc.rate += rate_mv;
1466 this_rdc.rate +=
1467 cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]][INTER_OFFSET(
1468 this_mode)];
1469 this_rdc.rate += ref_frame_cost[ref_frame];
1470 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
1471
1472 // Skipping checking: test to see if this block can be reconstructed by
1473 // prediction only.
1474 if (cpi->allow_encode_breakout) {
1475 encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
1476 var_y, sse_y, yv12_mb, &this_rdc.rate,
1477 &this_rdc.dist);
1478 if (x->skip) {
1479 this_rdc.rate += rate_mv;
1480 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate,
1481 this_rdc.dist);
1482 }
1483 }
1484
1485 #if CONFIG_VP9_TEMPORAL_DENOISING
1486 if (cpi->oxcf.noise_sensitivity > 0)
1487 vp9_denoiser_update_frame_stats(mbmi, sse_y, this_mode, ctx);
1488 #else
1489 (void)ctx;
1490 #endif
1491
1492 if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
1493 best_rdc = this_rdc;
1494 best_mode = this_mode;
1495 best_pred_filter = mbmi->interp_filter;
1496 best_tx_size = mbmi->tx_size;
1497 best_ref_frame = ref_frame;
1498 best_mode_skip_txfm = x->skip_txfm[0];
1499 best_early_term = this_early_term;
1500
1501 if (reuse_inter_pred) {
1502 free_pred_buffer(best_pred);
1503 best_pred = this_mode_pred;
1504 }
1505 } else {
1506 if (reuse_inter_pred)
1507 free_pred_buffer(this_mode_pred);
1508 }
1509
1510 if (x->skip)
1511 break;
1512
1513 // If early termination flag is 1 and at least 2 modes are checked,
1514 // the mode search is terminated.
1515 if (best_early_term && idx > 0) {
1516 x->skip = 1;
1517 break;
1518 }
1519 }
1520
1521 mbmi->mode = best_mode;
1522 mbmi->interp_filter = best_pred_filter;
1523 mbmi->tx_size = best_tx_size;
1524 mbmi->ref_frame[0] = best_ref_frame;
1525 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
1526 xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
1527 x->skip_txfm[0] = best_mode_skip_txfm;
1528
1529 // Perform intra prediction search, if the best SAD is above a certain
1530 // threshold.
1531 if (best_rdc.rdcost == INT64_MAX ||
1532 (!x->skip && best_rdc.rdcost > inter_mode_thresh &&
1533 bsize <= cpi->sf.max_intra_bsize)) {
1534 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
1535 int i;
1536 TX_SIZE best_intra_tx_size = TX_SIZES;
1537 TX_SIZE intra_tx_size =
1538 VPXMIN(max_txsize_lookup[bsize],
1539 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
1540 if (cpi->oxcf.content != VP9E_CONTENT_SCREEN && intra_tx_size > TX_16X16)
1541 intra_tx_size = TX_16X16;
1542
1543 if (reuse_inter_pred && best_pred != NULL) {
1544 if (best_pred->data == orig_dst.buf) {
1545 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1546 #if CONFIG_VP9_HIGHBITDEPTH
1547 if (cm->use_highbitdepth)
1548 vpx_highbd_convolve_copy(best_pred->data, best_pred->stride,
1549 this_mode_pred->data, this_mode_pred->stride,
1550 NULL, 0, NULL, 0, bw, bh, xd->bd);
1551 else
1552 vpx_convolve_copy(best_pred->data, best_pred->stride,
1553 this_mode_pred->data, this_mode_pred->stride,
1554 NULL, 0, NULL, 0, bw, bh);
1555 #else
1556 vpx_convolve_copy(best_pred->data, best_pred->stride,
1557 this_mode_pred->data, this_mode_pred->stride,
1558 NULL, 0, NULL, 0, bw, bh);
1559 #endif // CONFIG_VP9_HIGHBITDEPTH
1560 best_pred = this_mode_pred;
1561 }
1562 }
1563 pd->dst = orig_dst;
1564
1565 for (i = 0; i < 4; ++i) {
1566 const PREDICTION_MODE this_mode = intra_mode_list[i];
1567 THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
1568 int mode_rd_thresh = rd_threshes[mode_index];
1569
1570 if (!((1 << this_mode) & cpi->sf.intra_y_mode_bsize_mask[bsize]))
1571 continue;
1572
1573 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
1574 rd_thresh_freq_fact[mode_index]))
1575 continue;
1576
1577 mbmi->mode = this_mode;
1578 mbmi->ref_frame[0] = INTRA_FRAME;
1579 args.mode = this_mode;
1580 args.rate = 0;
1581 args.dist = 0;
1582 mbmi->tx_size = intra_tx_size;
1583 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
1584 estimate_block_intra, &args);
1585 // Inter and intra RD will mismatch in scale for non-screen content.
1586 if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
1587 if (x->color_sensitivity[0])
1588 vp9_foreach_transformed_block_in_plane(xd, bsize, 1,
1589 estimate_block_intra, &args);
1590 if (x->color_sensitivity[1])
1591 vp9_foreach_transformed_block_in_plane(xd, bsize, 2,
1592 estimate_block_intra, &args);
1593 }
1594 this_rdc.rate = args.rate;
1595 this_rdc.dist = args.dist;
1596 this_rdc.rate += cpi->mbmode_cost[this_mode];
1597 this_rdc.rate += ref_frame_cost[INTRA_FRAME];
1598 this_rdc.rate += intra_cost_penalty;
1599 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1600 this_rdc.rate, this_rdc.dist);
1601
1602 if (this_rdc.rdcost < best_rdc.rdcost) {
1603 best_rdc = this_rdc;
1604 best_mode = this_mode;
1605 best_intra_tx_size = mbmi->tx_size;
1606 best_ref_frame = INTRA_FRAME;
1607 mbmi->uv_mode = this_mode;
1608 mbmi->mv[0].as_int = INVALID_MV;
1609 best_mode_skip_txfm = x->skip_txfm[0];
1610 }
1611 }
1612
1613 // Reset mb_mode_info to the best inter mode.
1614 if (best_ref_frame != INTRA_FRAME) {
1615 mbmi->tx_size = best_tx_size;
1616 } else {
1617 mbmi->tx_size = best_intra_tx_size;
1618 }
1619 }
1620
1621 pd->dst = orig_dst;
1622 mbmi->mode = best_mode;
1623 mbmi->ref_frame[0] = best_ref_frame;
1624 x->skip_txfm[0] = best_mode_skip_txfm;
1625
1626 if (reuse_inter_pred && best_pred != NULL) {
1627 if (best_pred->data != orig_dst.buf && is_inter_mode(mbmi->mode)) {
1628 #if CONFIG_VP9_HIGHBITDEPTH
1629 if (cm->use_highbitdepth)
1630 vpx_highbd_convolve_copy(best_pred->data, best_pred->stride,
1631 pd->dst.buf, pd->dst.stride, NULL, 0,
1632 NULL, 0, bw, bh, xd->bd);
1633 else
1634 vpx_convolve_copy(best_pred->data, best_pred->stride,
1635 pd->dst.buf, pd->dst.stride, NULL, 0,
1636 NULL, 0, bw, bh);
1637 #else
1638 vpx_convolve_copy(best_pred->data, best_pred->stride,
1639 pd->dst.buf, pd->dst.stride, NULL, 0,
1640 NULL, 0, bw, bh);
1641 #endif // CONFIG_VP9_HIGHBITDEPTH
1642 }
1643 }
1644
1645 if (cpi->sf.adaptive_rd_thresh) {
1646 THR_MODES best_mode_idx = mode_idx[best_ref_frame][mode_offset(mbmi->mode)];
1647
1648 if (best_ref_frame == INTRA_FRAME) {
1649 // Only consider the modes that are included in the intra_mode_list.
1650 int intra_modes = sizeof(intra_mode_list)/sizeof(PREDICTION_MODE);
1651 int i;
1652
1653 // TODO(yunqingwang): Check intra mode mask and only update freq_fact
1654 // for those valid modes.
1655 for (i = 0; i < intra_modes; i++) {
1656 update_thresh_freq_fact(cpi, tile_data, bsize, INTRA_FRAME,
1657 best_mode_idx, intra_mode_list[i]);
1658 }
1659 } else {
1660 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1661 PREDICTION_MODE this_mode;
1662 if (best_ref_frame != ref_frame) continue;
1663 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1664 update_thresh_freq_fact(cpi, tile_data, bsize, ref_frame,
1665 best_mode_idx, this_mode);
1666 }
1667 }
1668 }
1669 }
1670
1671 *rd_cost = best_rdc;
1672 }
1673
vp9_pick_inter_mode_sub8x8(VP9_COMP * cpi,MACROBLOCK * x,int mi_row,int mi_col,RD_COST * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)1674 void vp9_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x,
1675 int mi_row, int mi_col, RD_COST *rd_cost,
1676 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1677 VP9_COMMON *const cm = &cpi->common;
1678 SPEED_FEATURES *const sf = &cpi->sf;
1679 MACROBLOCKD *const xd = &x->e_mbd;
1680 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1681 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1682 const struct segmentation *const seg = &cm->seg;
1683 MV_REFERENCE_FRAME ref_frame, second_ref_frame = NONE;
1684 MV_REFERENCE_FRAME best_ref_frame = NONE;
1685 unsigned char segment_id = mbmi->segment_id;
1686 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
1687 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1688 VP9_ALT_FLAG };
1689 int64_t best_rd = INT64_MAX;
1690 b_mode_info bsi[MAX_REF_FRAMES][4];
1691 int ref_frame_skip_mask = 0;
1692 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1693 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1694 int idx, idy;
1695
1696 x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1697 ctx->pred_pixel_ready = 0;
1698
1699 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1700 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1701 int_mv dummy_mv[2];
1702 x->pred_mv_sad[ref_frame] = INT_MAX;
1703
1704 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
1705 int_mv *const candidates = mbmi_ext->ref_mvs[ref_frame];
1706 const struct scale_factors *const sf =
1707 &cm->frame_refs[ref_frame - 1].sf;
1708 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
1709 sf, sf);
1710 vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame,
1711 candidates, mi_row, mi_col, NULL, NULL,
1712 mbmi_ext->mode_context);
1713
1714 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1715 &dummy_mv[0], &dummy_mv[1]);
1716 } else {
1717 ref_frame_skip_mask |= (1 << ref_frame);
1718 }
1719 }
1720
1721 mbmi->sb_type = bsize;
1722 mbmi->tx_size = TX_4X4;
1723 mbmi->uv_mode = DC_PRED;
1724 mbmi->ref_frame[0] = LAST_FRAME;
1725 mbmi->ref_frame[1] = NONE;
1726 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
1727 : cm->interp_filter;
1728
1729 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1730 int64_t this_rd = 0;
1731 int plane;
1732
1733 if (ref_frame_skip_mask & (1 << ref_frame))
1734 continue;
1735
1736 // TODO(jingning, agrange): Scaling reference frame not supported for
1737 // sub8x8 blocks. Is this supported now?
1738 if (ref_frame > INTRA_FRAME &&
1739 vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
1740 continue;
1741
1742 // If the segment reference frame feature is enabled....
1743 // then do nothing if the current ref frame is not allowed..
1744 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
1745 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
1746 continue;
1747
1748 mbmi->ref_frame[0] = ref_frame;
1749 x->skip = 0;
1750 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
1751
1752 // Select prediction reference frames.
1753 for (plane = 0; plane < MAX_MB_PLANE; plane++)
1754 xd->plane[plane].pre[0] = yv12_mb[ref_frame][plane];
1755
1756 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1757 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1758 int_mv b_mv[MB_MODE_COUNT];
1759 int64_t b_best_rd = INT64_MAX;
1760 const int i = idy * 2 + idx;
1761 PREDICTION_MODE this_mode;
1762 RD_COST this_rdc;
1763 unsigned int var_y, sse_y;
1764
1765 struct macroblock_plane *p = &x->plane[0];
1766 struct macroblockd_plane *pd = &xd->plane[0];
1767
1768 const struct buf_2d orig_src = p->src;
1769 const struct buf_2d orig_dst = pd->dst;
1770 struct buf_2d orig_pre[2];
1771 memcpy(orig_pre, xd->plane[0].pre, sizeof(orig_pre));
1772
1773 // set buffer pointers for sub8x8 motion search.
1774 p->src.buf =
1775 &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1776 pd->dst.buf =
1777 &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
1778 pd->pre[0].buf =
1779 &pd->pre[0].buf[vp9_raster_block_offset(BLOCK_8X8,
1780 i, pd->pre[0].stride)];
1781
1782 b_mv[ZEROMV].as_int = 0;
1783 b_mv[NEWMV].as_int = INVALID_MV;
1784 vp9_append_sub8x8_mvs_for_idx(cm, xd, i, 0, mi_row, mi_col,
1785 &b_mv[NEARESTMV],
1786 &b_mv[NEARMV],
1787 mbmi_ext->mode_context);
1788
1789 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1790 int b_rate = 0;
1791 xd->mi[0]->bmi[i].as_mv[0].as_int = b_mv[this_mode].as_int;
1792
1793 if (this_mode == NEWMV) {
1794 const int step_param = cpi->sf.mv.fullpel_search_step_param;
1795 MV mvp_full;
1796 MV tmp_mv;
1797 int cost_list[5];
1798 const int tmp_col_min = x->mv_col_min;
1799 const int tmp_col_max = x->mv_col_max;
1800 const int tmp_row_min = x->mv_row_min;
1801 const int tmp_row_max = x->mv_row_max;
1802 int dummy_dist;
1803
1804 if (i == 0) {
1805 mvp_full.row = b_mv[NEARESTMV].as_mv.row >> 3;
1806 mvp_full.col = b_mv[NEARESTMV].as_mv.col >> 3;
1807 } else {
1808 mvp_full.row = xd->mi[0]->bmi[0].as_mv[0].as_mv.row >> 3;
1809 mvp_full.col = xd->mi[0]->bmi[0].as_mv[0].as_mv.col >> 3;
1810 }
1811
1812 vp9_set_mv_search_range(x, &mbmi_ext->ref_mvs[0]->as_mv);
1813
1814 vp9_full_pixel_search(
1815 cpi, x, bsize, &mvp_full, step_param, x->sadperbit4,
1816 cond_cost_list(cpi, cost_list),
1817 &mbmi_ext->ref_mvs[ref_frame][0].as_mv, &tmp_mv,
1818 INT_MAX, 0);
1819
1820 x->mv_col_min = tmp_col_min;
1821 x->mv_col_max = tmp_col_max;
1822 x->mv_row_min = tmp_row_min;
1823 x->mv_row_max = tmp_row_max;
1824
1825 // calculate the bit cost on motion vector
1826 mvp_full.row = tmp_mv.row * 8;
1827 mvp_full.col = tmp_mv.col * 8;
1828
1829 b_rate += vp9_mv_bit_cost(&mvp_full,
1830 &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1831 x->nmvjointcost, x->mvcost,
1832 MV_COST_WEIGHT);
1833
1834 b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
1835 [INTER_OFFSET(NEWMV)];
1836 if (RDCOST(x->rdmult, x->rddiv, b_rate, 0) > b_best_rd)
1837 continue;
1838
1839 cpi->find_fractional_mv_step(x, &tmp_mv,
1840 &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1841 cpi->common.allow_high_precision_mv,
1842 x->errorperbit,
1843 &cpi->fn_ptr[bsize],
1844 cpi->sf.mv.subpel_force_stop,
1845 cpi->sf.mv.subpel_iters_per_step,
1846 cond_cost_list(cpi, cost_list),
1847 x->nmvjointcost, x->mvcost,
1848 &dummy_dist,
1849 &x->pred_sse[ref_frame], NULL, 0, 0);
1850
1851 xd->mi[0]->bmi[i].as_mv[0].as_mv = tmp_mv;
1852 } else {
1853 b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
1854 [INTER_OFFSET(this_mode)];
1855 }
1856
1857 #if CONFIG_VP9_HIGHBITDEPTH
1858 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1859 vp9_highbd_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1860 pd->dst.buf, pd->dst.stride,
1861 &xd->mi[0]->bmi[i].as_mv[0].as_mv,
1862 &xd->block_refs[0]->sf,
1863 4 * num_4x4_blocks_wide,
1864 4 * num_4x4_blocks_high, 0,
1865 vp9_filter_kernels[mbmi->interp_filter],
1866 MV_PRECISION_Q3,
1867 mi_col * MI_SIZE + 4 * (i & 0x01),
1868 mi_row * MI_SIZE + 4 * (i >> 1), xd->bd);
1869 } else {
1870 #endif
1871 vp9_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1872 pd->dst.buf, pd->dst.stride,
1873 &xd->mi[0]->bmi[i].as_mv[0].as_mv,
1874 &xd->block_refs[0]->sf,
1875 4 * num_4x4_blocks_wide,
1876 4 * num_4x4_blocks_high, 0,
1877 vp9_filter_kernels[mbmi->interp_filter],
1878 MV_PRECISION_Q3,
1879 mi_col * MI_SIZE + 4 * (i & 0x01),
1880 mi_row * MI_SIZE + 4 * (i >> 1));
1881
1882 #if CONFIG_VP9_HIGHBITDEPTH
1883 }
1884 #endif
1885
1886 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1887 &var_y, &sse_y);
1888
1889 this_rdc.rate += b_rate;
1890 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1891 this_rdc.rate, this_rdc.dist);
1892 if (this_rdc.rdcost < b_best_rd) {
1893 b_best_rd = this_rdc.rdcost;
1894 bsi[ref_frame][i].as_mode = this_mode;
1895 bsi[ref_frame][i].as_mv[0].as_mv = xd->mi[0]->bmi[i].as_mv[0].as_mv;
1896 }
1897 } // mode search
1898
1899 // restore source and prediction buffer pointers.
1900 p->src = orig_src;
1901 pd->pre[0] = orig_pre[0];
1902 pd->dst = orig_dst;
1903 this_rd += b_best_rd;
1904
1905 xd->mi[0]->bmi[i] = bsi[ref_frame][i];
1906 if (num_4x4_blocks_wide > 1)
1907 xd->mi[0]->bmi[i + 1] = xd->mi[0]->bmi[i];
1908 if (num_4x4_blocks_high > 1)
1909 xd->mi[0]->bmi[i + 2] = xd->mi[0]->bmi[i];
1910 }
1911 } // loop through sub8x8 blocks
1912
1913 if (this_rd < best_rd) {
1914 best_rd = this_rd;
1915 best_ref_frame = ref_frame;
1916 }
1917 } // reference frames
1918
1919 mbmi->tx_size = TX_4X4;
1920 mbmi->ref_frame[0] = best_ref_frame;
1921 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1922 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1923 const int block = idy * 2 + idx;
1924 xd->mi[0]->bmi[block] = bsi[best_ref_frame][block];
1925 if (num_4x4_blocks_wide > 1)
1926 xd->mi[0]->bmi[block + 1] = bsi[best_ref_frame][block];
1927 if (num_4x4_blocks_high > 1)
1928 xd->mi[0]->bmi[block + 2] = bsi[best_ref_frame][block];
1929 }
1930 }
1931 mbmi->mode = xd->mi[0]->bmi[3].as_mode;
1932 ctx->mic = *(xd->mi[0]);
1933 ctx->mbmi_ext = *x->mbmi_ext;
1934 ctx->skip_txfm[0] = SKIP_TXFM_NONE;
1935 ctx->skip = 0;
1936 // Dummy assignment for speed -5. No effect in speed -6.
1937 rd_cost->rdcost = best_rd;
1938 }
1939