Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/libvpx/source/libvpx/vp9/encoder/vp9_pickmode.c

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

Powered by Google App Engine
This is Rietveld 408576698