2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/encoder/vp9_rdopt.h"
15 #include "vp9/encoder/vp9_segmentation.h"
16 #include "vp9/encoder/vp9_mcomp.h"
17 #include "vp9/common/vp9_blockd.h"
18 #include "vp9/common/vp9_reconinter.h"
19 #include "vp9/common/vp9_reconintra.h"
20 #include "vp9/common/vp9_systemdependent.h"
24 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
29 MACROBLOCK *const x = &cpi->mb;
30 MACROBLOCKD *const xd = &x->e_mbd;
31 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
33 const int tmp_col_min = x->mv_col_min;
34 const int tmp_col_max = x->mv_col_max;
35 const int tmp_row_min = x->mv_row_min;
36 const int tmp_row_max = x->mv_row_max;
39 // Further step/diamond searches as necessary
40 int step_param = cpi->sf.reduce_first_step_size +
41 (cpi->oxcf.speed > 5 ? 1 : 0);
42 step_param = MIN(step_param, cpi->sf.max_step_search_steps - 2);
44 vp9_set_mv_search_range(x, ref_mv);
46 ref_full.col = ref_mv->col >> 3;
47 ref_full.row = ref_mv->row >> 3;
49 /*cpi->sf.search_method == HEX*/
50 vp9_hex_search(x, &ref_full, step_param, x->errorperbit, 0, &v_fn_ptr, 0,
54 // if (bestsme > error_thresh && bestsme < INT_MAX)
58 cpi->find_fractional_mv_step(
59 x, dst_mv, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
60 &v_fn_ptr, 0, cpi->sf.subpel_iters_per_step, NULL, NULL, &distortion,
64 xd->mi[0]->mbmi.mode = NEWMV;
65 xd->mi[0]->mbmi.mv[0].as_mv = *dst_mv;
67 vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
69 /* restore UMV window */
70 x->mv_col_min = tmp_col_min;
71 x->mv_col_max = tmp_col_max;
72 x->mv_row_min = tmp_row_min;
73 x->mv_row_max = tmp_row_max;
75 return vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
76 xd->plane[0].dst.buf, xd->plane[0].dst.stride,
80 static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
81 int_mv *dst_mv, int mb_row, int mb_col) {
82 MACROBLOCK *const x = &cpi->mb;
83 MACROBLOCKD *const xd = &x->e_mbd;
84 unsigned int err, tmp_err;
88 // FIXME should really use something like near/nearest MV and/or MV prediction
89 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
90 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
94 // Test last reference frame using the previous best mv as the
95 // starting point (best reference) for the search
96 tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
99 dst_mv->as_mv = tmp_mv;
102 // If the current best reference mv is not centered on 0,0 then do a 0,0
103 // based search as well.
104 if (ref_mv->row != 0 || ref_mv->col != 0) {
105 unsigned int tmp_err;
106 MV zero_ref_mv = {0, 0}, tmp_mv;
108 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
111 dst_mv->as_mv = tmp_mv;
119 static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
120 MACROBLOCK *const x = &cpi->mb;
121 MACROBLOCKD *const xd = &x->e_mbd;
125 // FIXME should really use something like near/nearest MV and/or MV prediction
126 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
127 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
134 static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
135 MACROBLOCK *const x = &cpi->mb;
136 MACROBLOCKD *const xd = &x->e_mbd;
137 PREDICTION_MODE best_mode = -1, mode;
138 unsigned int best_err = INT_MAX;
140 // calculate SATD for each intra prediction mode;
141 // we're intentionally not doing 4x4, we just want a rough estimate
142 for (mode = DC_PRED; mode <= TM_PRED; mode++) {
145 xd->mi[0]->mbmi.mode = mode;
146 vp9_predict_intra_block(xd, 0, 2, TX_16X16, mode,
147 x->plane[0].src.buf, x->plane[0].src.stride,
148 xd->plane[0].dst.buf, xd->plane[0].dst.stride,
150 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
151 xd->plane[0].dst.buf, xd->plane[0].dst.stride, best_err);
154 if (err < best_err) {
161 *pbest_mode = best_mode;
166 static void update_mbgraph_mb_stats
169 MBGRAPH_MB_STATS *stats,
170 YV12_BUFFER_CONFIG *buf,
172 YV12_BUFFER_CONFIG *golden_ref,
173 const MV *prev_golden_ref_mv,
174 YV12_BUFFER_CONFIG *alt_ref,
178 MACROBLOCK *const x = &cpi->mb;
179 MACROBLOCKD *const xd = &x->e_mbd;
181 VP9_COMMON *cm = &cpi->common;
183 // FIXME in practice we're completely ignoring chroma here
184 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
185 x->plane[0].src.stride = buf->y_stride;
187 xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
188 xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
190 // do intra 16x16 prediction
191 intra_error = find_best_16x16_intra(cpi,
192 &stats->ref[INTRA_FRAME].m.mode);
193 if (intra_error <= 0)
195 stats->ref[INTRA_FRAME].err = intra_error;
197 // Golden frame MV search, if it exists and is different than last frame
200 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
201 xd->plane[0].pre[0].stride = golden_ref->y_stride;
202 g_motion_error = do_16x16_motion_search(cpi,
204 &stats->ref[GOLDEN_FRAME].m.mv,
206 stats->ref[GOLDEN_FRAME].err = g_motion_error;
208 stats->ref[GOLDEN_FRAME].err = INT_MAX;
209 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
212 // Do an Alt-ref frame MV search, if it exists and is different than
213 // last/golden frame.
216 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
217 xd->plane[0].pre[0].stride = alt_ref->y_stride;
218 a_motion_error = do_16x16_zerozero_search(cpi,
219 &stats->ref[ALTREF_FRAME].m.mv);
221 stats->ref[ALTREF_FRAME].err = a_motion_error;
223 stats->ref[ALTREF_FRAME].err = INT_MAX;
224 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
228 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
229 MBGRAPH_FRAME_STATS *stats,
230 YV12_BUFFER_CONFIG *buf,
231 YV12_BUFFER_CONFIG *golden_ref,
232 YV12_BUFFER_CONFIG *alt_ref) {
233 MACROBLOCK *const x = &cpi->mb;
234 MACROBLOCKD *const xd = &x->e_mbd;
235 VP9_COMMON *const cm = &cpi->common;
237 int mb_col, mb_row, offset = 0;
238 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
239 MV arf_top_mv = {0, 0}, gld_top_mv = {0, 0};
240 MODE_INFO mi_local = { { 0 } };
242 // Set up limit values for motion vectors to prevent them extending outside
244 x->mv_row_min = -BORDER_MV_PIXELS_B16;
245 x->mv_row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
246 xd->up_available = 0;
247 xd->plane[0].dst.stride = buf->y_stride;
248 xd->plane[0].pre[0].stride = buf->y_stride;
249 xd->plane[1].dst.stride = buf->uv_stride;
250 xd->mi[0] = &mi_local;
251 mi_local.mbmi.sb_type = BLOCK_16X16;
252 mi_local.mbmi.ref_frame[0] = LAST_FRAME;
253 mi_local.mbmi.ref_frame[1] = NONE;
255 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
256 MV arf_left_mv = arf_top_mv, gld_left_mv = gld_top_mv;
257 int mb_y_in_offset = mb_y_offset;
258 int arf_y_in_offset = arf_y_offset;
259 int gld_y_in_offset = gld_y_offset;
261 // Set up limit values for motion vectors to prevent them extending outside
263 x->mv_col_min = -BORDER_MV_PIXELS_B16;
264 x->mv_col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
265 xd->left_available = 0;
267 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
268 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
270 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
271 golden_ref, &gld_left_mv, alt_ref,
273 arf_left_mv = mb_stats->ref[ALTREF_FRAME].m.mv.as_mv;
274 gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
276 arf_top_mv = arf_left_mv;
277 gld_top_mv = gld_left_mv;
279 xd->left_available = 1;
280 mb_y_in_offset += 16;
281 gld_y_in_offset += 16;
282 arf_y_in_offset += 16;
286 xd->up_available = 1;
287 mb_y_offset += buf->y_stride * 16;
288 gld_y_offset += golden_ref->y_stride * 16;
290 arf_y_offset += alt_ref->y_stride * 16;
293 offset += cm->mb_cols;
297 // void separate_arf_mbs_byzz
298 static void separate_arf_mbs(VP9_COMP *cpi) {
299 VP9_COMMON *const cm = &cpi->common;
300 int mb_col, mb_row, offset, i;
303 int n_frames = cpi->mbgraph_n_frames;
307 CHECK_MEM_ERROR(cm, arf_not_zz,
308 vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
311 // We are not interested in results beyond the alt ref itself.
312 if (n_frames > cpi->rc.frames_till_gf_update_due)
313 n_frames = cpi->rc.frames_till_gf_update_due;
315 // defer cost to reference frames
316 for (i = n_frames - 1; i >= 0; i--) {
317 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
319 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
320 offset += cm->mb_cols, mb_row++) {
321 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
322 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
324 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
325 int intra_err = mb_stats->ref[INTRA_FRAME ].err;
326 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
328 // Test for altref vs intra and gf and that its mv was 0,0.
329 if (altref_err > 1000 ||
330 altref_err > intra_err ||
331 altref_err > golden_err) {
332 arf_not_zz[offset + mb_col]++;
338 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
339 // of bound access in segmentation_map
340 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
341 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
342 // If any of the blocks in the sequence failed then the MB
344 if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
346 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
348 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
354 // Only bother with segmentation if over 10% of the MBs in static segment
355 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
357 // Note % of blocks that are marked as static
359 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
361 // This error case should not be reachable as this function should
362 // never be called with the common data structure uninitialized.
364 cpi->static_mb_pct = 0;
366 vp9_enable_segmentation(&cm->seg);
368 cpi->static_mb_pct = 0;
369 vp9_disable_segmentation(&cm->seg);
372 // Free localy allocated storage
373 vpx_free(arf_not_zz);
376 void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
377 VP9_COMMON *const cm = &cpi->common;
378 int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
379 YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
381 // we need to look ahead beyond where the ARF transitions into
382 // being a GF - so exit if we don't look ahead beyond that
383 if (n_frames <= cpi->rc.frames_till_gf_update_due)
386 if (n_frames > MAX_LAG_BUFFERS)
387 n_frames = MAX_LAG_BUFFERS;
389 cpi->mbgraph_n_frames = n_frames;
390 for (i = 0; i < n_frames; i++) {
391 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
392 vpx_memset(frame_stats->mb_stats, 0,
393 cm->mb_rows * cm->mb_cols *
394 sizeof(*cpi->mbgraph_stats[i].mb_stats));
397 // do motion search to find contribution of each reference to data
398 // later on in this GF group
399 // FIXME really, the GF/last MC search should be done forward, and
400 // the ARF MC search backwards, to get optimal results for MV caching
401 for (i = 0; i < n_frames; i++) {
402 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
403 struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
405 assert(q_cur != NULL);
407 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
408 golden_ref, cpi->Source);
411 vp9_clear_system_state();
413 separate_arf_mbs(cpi);