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->speed < 8 ? (cpi->speed > 5 ? 1 : 0) : 2);
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 vp9_set_mbmode_and_mvs(xd, NEWMV, dst_mv);
65 vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
67 /* restore UMV window */
68 x->mv_col_min = tmp_col_min;
69 x->mv_col_max = tmp_col_max;
70 x->mv_row_min = tmp_row_min;
71 x->mv_row_max = tmp_row_max;
73 return vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
74 xd->plane[0].dst.buf, xd->plane[0].dst.stride,
78 static int do_16x16_motion_search(VP9_COMP *cpi, const int_mv *ref_mv,
79 int_mv *dst_mv, int mb_row, int mb_col) {
80 MACROBLOCK *const x = &cpi->mb;
81 MACROBLOCKD *const xd = &x->e_mbd;
82 unsigned int err, tmp_err;
86 // FIXME should really use something like near/nearest MV and/or MV prediction
87 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
88 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
92 // Test last reference frame using the previous best mv as the
93 // starting point (best reference) for the search
94 tmp_err = do_16x16_motion_iteration(cpi, &ref_mv->as_mv, &tmp_mv.as_mv,
98 dst_mv->as_int = tmp_mv.as_int;
101 // If the current best reference mv is not centered on 0,0 then do a 0,0
102 // based search as well.
103 if (ref_mv->as_int) {
104 unsigned int tmp_err;
105 int_mv zero_ref_mv, tmp_mv;
107 zero_ref_mv.as_int = 0;
108 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv.as_mv, &tmp_mv.as_mv,
111 dst_mv->as_int = tmp_mv.as_int;
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,
136 MB_PREDICTION_MODE *pbest_mode) {
137 MACROBLOCK *const x = &cpi->mb;
138 MACROBLOCKD *const xd = &x->e_mbd;
139 MB_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_8x8[0]->mbmi.mode = mode;
148 vp9_predict_intra_block(xd, 0, 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 = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
153 xd->plane[0].dst.buf, xd->plane[0].dst.stride, best_err);
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 int_mv *prev_golden_ref_mv,
177 YV12_BUFFER_CONFIG *alt_ref,
178 int_mv *prev_alt_ref_mv,
183 MACROBLOCK *const x = &cpi->mb;
184 MACROBLOCKD *const xd = &x->e_mbd;
186 VP9_COMMON *cm = &cpi->common;
188 // FIXME in practice we're completely ignoring chroma here
189 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
190 x->plane[0].src.stride = buf->y_stride;
192 xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
193 xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
195 // do intra 16x16 prediction
196 intra_error = find_best_16x16_intra(cpi, mb_y_offset,
197 &stats->ref[INTRA_FRAME].m.mode);
198 if (intra_error <= 0)
200 stats->ref[INTRA_FRAME].err = intra_error;
202 // Golden frame MV search, if it exists and is different than last frame
205 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
206 xd->plane[0].pre[0].stride = golden_ref->y_stride;
207 g_motion_error = do_16x16_motion_search(cpi,
209 &stats->ref[GOLDEN_FRAME].m.mv,
211 stats->ref[GOLDEN_FRAME].err = g_motion_error;
213 stats->ref[GOLDEN_FRAME].err = INT_MAX;
214 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
217 // Do an Alt-ref frame MV search, if it exists and is different than
218 // last/golden frame.
221 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
222 xd->plane[0].pre[0].stride = alt_ref->y_stride;
223 a_motion_error = do_16x16_zerozero_search(cpi,
224 &stats->ref[ALTREF_FRAME].m.mv);
226 stats->ref[ALTREF_FRAME].err = a_motion_error;
228 stats->ref[ALTREF_FRAME].err = INT_MAX;
229 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
233 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
234 MBGRAPH_FRAME_STATS *stats,
235 YV12_BUFFER_CONFIG *buf,
236 YV12_BUFFER_CONFIG *golden_ref,
237 YV12_BUFFER_CONFIG *alt_ref) {
238 MACROBLOCK *const x = &cpi->mb;
239 MACROBLOCKD *const xd = &x->e_mbd;
240 VP9_COMMON *const cm = &cpi->common;
242 int mb_col, mb_row, offset = 0;
243 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
244 int_mv arf_top_mv, gld_top_mv;
245 MODE_INFO mi_local = { { 0 } };
247 // Set up limit values for motion vectors to prevent them extending outside
249 arf_top_mv.as_int = 0;
250 gld_top_mv.as_int = 0;
251 x->mv_row_min = -BORDER_MV_PIXELS_B16;
252 x->mv_row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
253 xd->up_available = 0;
254 xd->plane[0].dst.stride = buf->y_stride;
255 xd->plane[0].pre[0].stride = buf->y_stride;
256 xd->plane[1].dst.stride = buf->uv_stride;
257 xd->mi_8x8[0] = &mi_local;
258 mi_local.mbmi.sb_type = BLOCK_16X16;
259 mi_local.mbmi.ref_frame[0] = LAST_FRAME;
260 mi_local.mbmi.ref_frame[1] = NONE;
262 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
263 int_mv arf_left_mv, gld_left_mv;
264 int mb_y_in_offset = mb_y_offset;
265 int arf_y_in_offset = arf_y_offset;
266 int gld_y_in_offset = gld_y_offset;
268 // Set up limit values for motion vectors to prevent them extending outside
270 arf_left_mv.as_int = arf_top_mv.as_int;
271 gld_left_mv.as_int = gld_top_mv.as_int;
272 x->mv_col_min = -BORDER_MV_PIXELS_B16;
273 x->mv_col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
274 xd->left_available = 0;
276 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
277 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
279 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
280 golden_ref, &gld_left_mv, gld_y_in_offset,
281 alt_ref, &arf_left_mv, arf_y_in_offset,
283 arf_left_mv.as_int = mb_stats->ref[ALTREF_FRAME].m.mv.as_int;
284 gld_left_mv.as_int = mb_stats->ref[GOLDEN_FRAME].m.mv.as_int;
286 arf_top_mv.as_int = arf_left_mv.as_int;
287 gld_top_mv.as_int = gld_left_mv.as_int;
289 xd->left_available = 1;
290 mb_y_in_offset += 16;
291 gld_y_in_offset += 16;
292 arf_y_in_offset += 16;
296 xd->up_available = 1;
297 mb_y_offset += buf->y_stride * 16;
298 gld_y_offset += golden_ref->y_stride * 16;
300 arf_y_offset += alt_ref->y_stride * 16;
303 offset += cm->mb_cols;
307 // void separate_arf_mbs_byzz
308 static void separate_arf_mbs(VP9_COMP *cpi) {
309 VP9_COMMON *const cm = &cpi->common;
310 int mb_col, mb_row, offset, i;
313 int n_frames = cpi->mbgraph_n_frames;
317 CHECK_MEM_ERROR(cm, arf_not_zz,
318 vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
321 // We are not interested in results beyond the alt ref itself.
322 if (n_frames > cpi->rc.frames_till_gf_update_due)
323 n_frames = cpi->rc.frames_till_gf_update_due;
325 // defer cost to reference frames
326 for (i = n_frames - 1; i >= 0; i--) {
327 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
329 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
330 offset += cm->mb_cols, mb_row++) {
331 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
332 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
334 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
335 int intra_err = mb_stats->ref[INTRA_FRAME ].err;
336 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
338 // Test for altref vs intra and gf and that its mv was 0,0.
339 if (altref_err > 1000 ||
340 altref_err > intra_err ||
341 altref_err > golden_err) {
342 arf_not_zz[offset + mb_col]++;
348 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
349 // of bound access in segmentation_map
350 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
351 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
352 // If any of the blocks in the sequence failed then the MB
354 if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
356 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
358 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
364 // Only bother with segmentation if over 10% of the MBs in static segment
365 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
367 // Note % of blocks that are marked as static
369 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
371 // This error case should not be reachable as this function should
372 // never be called with the common data structure uninitialized.
374 cpi->static_mb_pct = 0;
376 cpi->seg0_cnt = ncnt[0];
377 vp9_enable_segmentation((VP9_PTR)cpi);
379 cpi->static_mb_pct = 0;
380 vp9_disable_segmentation((VP9_PTR)cpi);
383 // Free localy allocated storage
384 vpx_free(arf_not_zz);
387 void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
388 VP9_COMMON *const cm = &cpi->common;
389 int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
390 YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
392 // we need to look ahead beyond where the ARF transitions into
393 // being a GF - so exit if we don't look ahead beyond that
394 if (n_frames <= cpi->rc.frames_till_gf_update_due)
397 if (n_frames > MAX_LAG_BUFFERS)
398 n_frames = MAX_LAG_BUFFERS;
400 cpi->mbgraph_n_frames = n_frames;
401 for (i = 0; i < n_frames; i++) {
402 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
403 vpx_memset(frame_stats->mb_stats, 0,
404 cm->mb_rows * cm->mb_cols *
405 sizeof(*cpi->mbgraph_stats[i].mb_stats));
408 // do motion search to find contribution of each reference to data
409 // later on in this GF group
410 // FIXME really, the GF/last MC search should be done forward, and
411 // the ARF MC search backwards, to get optimal results for MV caching
412 for (i = 0; i < n_frames; i++) {
413 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
414 struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
416 assert(q_cur != NULL);
418 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
419 golden_ref, cpi->Source);
422 vp9_clear_system_state(); // __asm emms;
424 separate_arf_mbs(cpi);