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 "./vp10_rtcd.h"
14 #include "./vpx_dsp_rtcd.h"
16 #include "vpx_mem/vpx_mem.h"
17 #include "vp10/encoder/vp9_segmentation.h"
18 #include "vp10/encoder/vp9_mcomp.h"
19 #include "vp10/common/vp9_blockd.h"
20 #include "vp10/common/vp9_reconinter.h"
21 #include "vp10/common/vp9_reconintra.h"
22 #include "vp10/common/vp9_systemdependent.h"
25 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
30 MACROBLOCK *const x = &cpi->td.mb;
31 MACROBLOCKD *const xd = &x->e_mbd;
32 const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
33 const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
35 const int tmp_col_min = x->mv_col_min;
36 const int tmp_col_max = x->mv_col_max;
37 const int tmp_row_min = x->mv_row_min;
38 const int tmp_row_max = x->mv_row_max;
42 // Further step/diamond searches as necessary
43 int step_param = mv_sf->reduce_first_step_size;
44 step_param = MIN(step_param, MAX_MVSEARCH_STEPS - 2);
46 vp10_set_mv_search_range(x, ref_mv);
48 ref_full.col = ref_mv->col >> 3;
49 ref_full.row = ref_mv->row >> 3;
51 /*cpi->sf.search_method == HEX*/
52 vp10_hex_search(x, &ref_full, step_param, x->errorperbit, 0,
53 cond_cost_list(cpi, cost_list),
54 &v_fn_ptr, 0, ref_mv, dst_mv);
57 // if (bestsme > error_thresh && bestsme < INT_MAX)
61 cpi->find_fractional_mv_step(
62 x, dst_mv, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
63 &v_fn_ptr, 0, mv_sf->subpel_iters_per_step,
64 cond_cost_list(cpi, cost_list),
66 &distortion, &sse, NULL, 0, 0);
69 xd->mi[0]->mbmi.mode = NEWMV;
70 xd->mi[0]->mbmi.mv[0].as_mv = *dst_mv;
72 vp10_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
74 /* restore UMV window */
75 x->mv_col_min = tmp_col_min;
76 x->mv_col_max = tmp_col_max;
77 x->mv_row_min = tmp_row_min;
78 x->mv_row_max = tmp_row_max;
80 return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
81 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
84 static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
85 int_mv *dst_mv, int mb_row, int mb_col) {
86 MACROBLOCK *const x = &cpi->td.mb;
87 MACROBLOCKD *const xd = &x->e_mbd;
88 unsigned int err, tmp_err;
92 // FIXME should really use something like near/nearest MV and/or MV prediction
93 err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
94 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
97 // Test last reference frame using the previous best mv as the
98 // starting point (best reference) for the search
99 tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
102 dst_mv->as_mv = tmp_mv;
105 // If the current best reference mv is not centered on 0,0 then do a 0,0
106 // based search as well.
107 if (ref_mv->row != 0 || ref_mv->col != 0) {
108 unsigned int tmp_err;
109 MV zero_ref_mv = {0, 0}, tmp_mv;
111 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
114 dst_mv->as_mv = tmp_mv;
122 static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
123 MACROBLOCK *const x = &cpi->td.mb;
124 MACROBLOCKD *const xd = &x->e_mbd;
128 // FIXME should really use something like near/nearest MV and/or MV prediction
129 err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
130 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
136 static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
137 MACROBLOCK *const x = &cpi->td.mb;
138 MACROBLOCKD *const xd = &x->e_mbd;
139 PREDICTION_MODE best_mode = -1, mode;
140 unsigned int best_err = INT_MAX;
142 // calculate SATD for each intra prediction mode;
143 // we're intentionally not doing 4x4, we just want a rough estimate
144 for (mode = DC_PRED; mode <= TM_PRED; mode++) {
147 xd->mi[0]->mbmi.mode = mode;
148 vp10_predict_intra_block(xd, 2, TX_16X16, mode,
149 x->plane[0].src.buf, x->plane[0].src.stride,
150 xd->plane[0].dst.buf, xd->plane[0].dst.stride,
152 err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
153 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
156 if (err < best_err) {
163 *pbest_mode = best_mode;
168 static void update_mbgraph_mb_stats
171 MBGRAPH_MB_STATS *stats,
172 YV12_BUFFER_CONFIG *buf,
174 YV12_BUFFER_CONFIG *golden_ref,
175 const MV *prev_golden_ref_mv,
176 YV12_BUFFER_CONFIG *alt_ref,
180 MACROBLOCK *const x = &cpi->td.mb;
181 MACROBLOCKD *const xd = &x->e_mbd;
183 VP9_COMMON *cm = &cpi->common;
185 // FIXME in practice we're completely ignoring chroma here
186 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
187 x->plane[0].src.stride = buf->y_stride;
189 xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
190 xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
192 // do intra 16x16 prediction
193 intra_error = find_best_16x16_intra(cpi,
194 &stats->ref[INTRA_FRAME].m.mode);
195 if (intra_error <= 0)
197 stats->ref[INTRA_FRAME].err = intra_error;
199 // Golden frame MV search, if it exists and is different than last frame
202 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
203 xd->plane[0].pre[0].stride = golden_ref->y_stride;
204 g_motion_error = do_16x16_motion_search(cpi,
206 &stats->ref[GOLDEN_FRAME].m.mv,
208 stats->ref[GOLDEN_FRAME].err = g_motion_error;
210 stats->ref[GOLDEN_FRAME].err = INT_MAX;
211 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
214 // Do an Alt-ref frame MV search, if it exists and is different than
215 // last/golden frame.
218 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
219 xd->plane[0].pre[0].stride = alt_ref->y_stride;
220 a_motion_error = do_16x16_zerozero_search(cpi,
221 &stats->ref[ALTREF_FRAME].m.mv);
223 stats->ref[ALTREF_FRAME].err = a_motion_error;
225 stats->ref[ALTREF_FRAME].err = INT_MAX;
226 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
230 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
231 MBGRAPH_FRAME_STATS *stats,
232 YV12_BUFFER_CONFIG *buf,
233 YV12_BUFFER_CONFIG *golden_ref,
234 YV12_BUFFER_CONFIG *alt_ref) {
235 MACROBLOCK *const x = &cpi->td.mb;
236 MACROBLOCKD *const xd = &x->e_mbd;
237 VP9_COMMON *const cm = &cpi->common;
239 int mb_col, mb_row, offset = 0;
240 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
241 MV gld_top_mv = {0, 0};
245 // Set up limit values for motion vectors to prevent them extending outside
247 x->mv_row_min = -BORDER_MV_PIXELS_B16;
248 x->mv_row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
249 xd->up_available = 0;
250 xd->plane[0].dst.stride = buf->y_stride;
251 xd->plane[0].pre[0].stride = buf->y_stride;
252 xd->plane[1].dst.stride = buf->uv_stride;
253 xd->mi[0] = &mi_local;
254 mi_local.mbmi.sb_type = BLOCK_16X16;
255 mi_local.mbmi.ref_frame[0] = LAST_FRAME;
256 mi_local.mbmi.ref_frame[1] = NONE;
258 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
259 MV gld_left_mv = gld_top_mv;
260 int mb_y_in_offset = mb_y_offset;
261 int arf_y_in_offset = arf_y_offset;
262 int gld_y_in_offset = gld_y_offset;
264 // Set up limit values for motion vectors to prevent them extending outside
266 x->mv_col_min = -BORDER_MV_PIXELS_B16;
267 x->mv_col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
268 xd->left_available = 0;
270 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
271 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
273 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
274 golden_ref, &gld_left_mv, alt_ref,
276 gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
278 gld_top_mv = gld_left_mv;
280 xd->left_available = 1;
281 mb_y_in_offset += 16;
282 gld_y_in_offset += 16;
283 arf_y_in_offset += 16;
287 xd->up_available = 1;
288 mb_y_offset += buf->y_stride * 16;
289 gld_y_offset += golden_ref->y_stride * 16;
291 arf_y_offset += alt_ref->y_stride * 16;
294 offset += cm->mb_cols;
298 // void separate_arf_mbs_byzz
299 static void separate_arf_mbs(VP9_COMP *cpi) {
300 VP9_COMMON *const cm = &cpi->common;
301 int mb_col, mb_row, offset, i;
304 int n_frames = cpi->mbgraph_n_frames;
308 CHECK_MEM_ERROR(cm, arf_not_zz,
309 vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
312 // We are not interested in results beyond the alt ref itself.
313 if (n_frames > cpi->rc.frames_till_gf_update_due)
314 n_frames = cpi->rc.frames_till_gf_update_due;
316 // defer cost to reference frames
317 for (i = n_frames - 1; i >= 0; i--) {
318 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
320 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
321 offset += cm->mb_cols, mb_row++) {
322 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
323 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
325 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
326 int intra_err = mb_stats->ref[INTRA_FRAME ].err;
327 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
329 // Test for altref vs intra and gf and that its mv was 0,0.
330 if (altref_err > 1000 ||
331 altref_err > intra_err ||
332 altref_err > golden_err) {
333 arf_not_zz[offset + mb_col]++;
339 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
340 // of bound access in segmentation_map
341 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
342 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
343 // If any of the blocks in the sequence failed then the MB
345 if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
347 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
349 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
355 // Only bother with segmentation if over 10% of the MBs in static segment
356 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
358 // Note % of blocks that are marked as static
360 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
362 // This error case should not be reachable as this function should
363 // never be called with the common data structure uninitialized.
365 cpi->static_mb_pct = 0;
367 vp10_enable_segmentation(&cm->seg);
369 cpi->static_mb_pct = 0;
370 vp10_disable_segmentation(&cm->seg);
373 // Free localy allocated storage
374 vpx_free(arf_not_zz);
377 void vp10_update_mbgraph_stats(VP9_COMP *cpi) {
378 VP9_COMMON *const cm = &cpi->common;
379 int i, n_frames = vp10_lookahead_depth(cpi->lookahead);
380 YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
382 assert(golden_ref != NULL);
384 // we need to look ahead beyond where the ARF transitions into
385 // being a GF - so exit if we don't look ahead beyond that
386 if (n_frames <= cpi->rc.frames_till_gf_update_due)
389 if (n_frames > MAX_LAG_BUFFERS)
390 n_frames = MAX_LAG_BUFFERS;
392 cpi->mbgraph_n_frames = n_frames;
393 for (i = 0; i < n_frames; i++) {
394 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
395 memset(frame_stats->mb_stats, 0,
396 cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
399 // do motion search to find contribution of each reference to data
400 // later on in this GF group
401 // FIXME really, the GF/last MC search should be done forward, and
402 // the ARF MC search backwards, to get optimal results for MV caching
403 for (i = 0; i < n_frames; i++) {
404 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
405 struct lookahead_entry *q_cur = vp10_lookahead_peek(cpi->lookahead, i);
407 assert(q_cur != NULL);
409 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
410 golden_ref, cpi->Source);
413 vpx_clear_system_state();
415 separate_arf_mbs(cpi);