blob: 3f5daebcc49aefa4fdf4a443fb2b0018c001ffd6 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13
Yaowu Xuf883b422016-08-30 14:01:10 -070014#include "./av1_rtcd.h"
15#include "./aom_dsp_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_dsp/aom_dsp_common.h"
18#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "aom_ports/system_state.h"
20#include "av1/encoder/segmentation.h"
21#include "av1/encoder/mcomp.h"
22#include "av1/common/blockd.h"
23#include "av1/common/reconinter.h"
24#include "av1/common/reconintra.h"
25
Yaowu Xuf883b422016-08-30 14:01:10 -070026static unsigned int do_16x16_motion_iteration(AV1_COMP *cpi, const MV *ref_mv,
Yaowu Xuc27fc142016-08-22 16:08:15 -070027 int mb_row, int mb_col) {
28 MACROBLOCK *const x = &cpi->td.mb;
29 MACROBLOCKD *const xd = &x->e_mbd;
30 const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
Yaowu Xuf883b422016-08-30 14:01:10 -070031 const aom_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
Yaowu Xuc27fc142016-08-22 16:08:15 -070032
Alex Converse0fa0f422017-04-24 12:51:14 -070033 const MvLimits tmp_mv_limits = x->mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -070034 MV ref_full;
35 int cost_list[5];
36
37 // Further step/diamond searches as necessary
38 int step_param = mv_sf->reduce_first_step_size;
Yaowu Xuf883b422016-08-30 14:01:10 -070039 step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070040
Alex Converse0fa0f422017-04-24 12:51:14 -070041 av1_set_mv_search_range(&x->mv_limits, ref_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -070042
43 ref_full.col = ref_mv->col >> 3;
44 ref_full.row = ref_mv->row >> 3;
45
46 /*cpi->sf.search_method == HEX*/
Yaowu Xuf883b422016-08-30 14:01:10 -070047 av1_hex_search(x, &ref_full, step_param, x->errorperbit, 0,
48 cond_cost_list(cpi, cost_list), &v_fn_ptr, 0, ref_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -070049
50 // Try sub-pixel MC
51 // if (bestsme > error_thresh && bestsme < INT_MAX)
52 {
53 int distortion;
54 unsigned int sse;
David Barkerc155e012017-05-11 13:54:54 +010055 cpi->find_fractional_mv_step(
56 x, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
57 &v_fn_ptr, 0, mv_sf->subpel_iters_per_step,
58 cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL,
59#if CONFIG_EXT_INTER
60 NULL, 0, 0,
61#endif
62 0, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070063 }
64
65#if CONFIG_EXT_INTER
66 if (has_second_ref(&xd->mi[0]->mbmi))
67 xd->mi[0]->mbmi.mode = NEW_NEWMV;
68 else
69#endif // CONFIG_EXT_INTER
70 xd->mi[0]->mbmi.mode = NEWMV;
71
72 xd->mi[0]->mbmi.mv[0] = x->best_mv;
73#if CONFIG_EXT_INTER
Emil Keyder01770b32017-01-20 18:03:11 -050074 xd->mi[0]->mbmi.ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -070075#endif // CONFIG_EXT_INTER
76
Jingning Hanc44009c2017-05-06 11:36:49 -070077 av1_build_inter_predictors_sby(&cpi->common, xd, mb_row, mb_col, NULL,
78 BLOCK_16X16);
Yaowu Xuc27fc142016-08-22 16:08:15 -070079
80 /* restore UMV window */
Alex Converse0fa0f422017-04-24 12:51:14 -070081 x->mv_limits = tmp_mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -070082
Yaowu Xuf883b422016-08-30 14:01:10 -070083 return aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -070084 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
85}
86
Yaowu Xuf883b422016-08-30 14:01:10 -070087static int do_16x16_motion_search(AV1_COMP *cpi, const MV *ref_mv, int mb_row,
Yaowu Xuc27fc142016-08-22 16:08:15 -070088 int mb_col) {
89 MACROBLOCK *const x = &cpi->td.mb;
90 MACROBLOCKD *const xd = &x->e_mbd;
91 unsigned int err, tmp_err;
92 MV best_mv;
93
94 // Try zero MV first
95 // FIXME should really use something like near/nearest MV and/or MV prediction
Yaowu Xuf883b422016-08-30 14:01:10 -070096 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -070097 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
98 best_mv.col = best_mv.row = 0;
99
100 // Test last reference frame using the previous best mv as the
101 // starting point (best reference) for the search
102 tmp_err = do_16x16_motion_iteration(cpi, ref_mv, mb_row, mb_col);
103 if (tmp_err < err) {
104 err = tmp_err;
105 best_mv = x->best_mv.as_mv;
106 }
107
108 // If the current best reference mv is not centered on 0,0 then do a 0,0
109 // based search as well.
110 if (ref_mv->row != 0 || ref_mv->col != 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111 MV zero_ref_mv = { 0, 0 };
112
113 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, mb_row, mb_col);
114 if (tmp_err < err) {
115 err = tmp_err;
116 best_mv = x->best_mv.as_mv;
117 }
118 }
119
120 x->best_mv.as_mv = best_mv;
121 return err;
122}
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static int do_16x16_zerozero_search(AV1_COMP *cpi, int_mv *dst_mv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 MACROBLOCK *const x = &cpi->td.mb;
126 MACROBLOCKD *const xd = &x->e_mbd;
127 unsigned int err;
128
129 // Try zero MV first
130 // FIXME should really use something like near/nearest MV and/or MV prediction
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
133
134 dst_mv->as_int = 0;
135
136 return err;
137}
Yaowu Xuf883b422016-08-30 14:01:10 -0700138static int find_best_16x16_intra(AV1_COMP *cpi, PREDICTION_MODE *pbest_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 MACROBLOCK *const x = &cpi->td.mb;
140 MACROBLOCKD *const xd = &x->e_mbd;
141 PREDICTION_MODE best_mode = -1, mode;
142 unsigned int best_err = INT_MAX;
143
144 // calculate SATD for each intra prediction mode;
145 // we're intentionally not doing 4x4, we just want a rough estimate
146 for (mode = DC_PRED; mode <= TM_PRED; mode++) {
147 unsigned int err;
148
149 xd->mi[0]->mbmi.mode = mode;
David Barker839467f2017-01-19 11:06:15 +0000150 av1_predict_intra_block(xd, 16, 16, BLOCK_16X16, mode, x->plane[0].src.buf,
Yaowu Xuf883b422016-08-30 14:01:10 -0700151 x->plane[0].src.stride, xd->plane[0].dst.buf,
152 xd->plane[0].dst.stride, 0, 0, 0);
153 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
155
156 // find best
157 if (err < best_err) {
158 best_err = err;
159 best_mode = mode;
160 }
161 }
162
163 if (pbest_mode) *pbest_mode = best_mode;
164
165 return best_err;
166}
167
Yaowu Xuf883b422016-08-30 14:01:10 -0700168static void update_mbgraph_mb_stats(AV1_COMP *cpi, MBGRAPH_MB_STATS *stats,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 YV12_BUFFER_CONFIG *buf, int mb_y_offset,
170 YV12_BUFFER_CONFIG *golden_ref,
171 const MV *prev_golden_ref_mv,
172 YV12_BUFFER_CONFIG *alt_ref, int mb_row,
173 int mb_col) {
174 MACROBLOCK *const x = &cpi->td.mb;
175 MACROBLOCKD *const xd = &x->e_mbd;
176 int intra_error;
Yaowu Xuf883b422016-08-30 14:01:10 -0700177 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178
179 // FIXME in practice we're completely ignoring chroma here
180 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
181 x->plane[0].src.stride = buf->y_stride;
182
183 xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
184 xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
185
186 // do intra 16x16 prediction
187 intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
188 if (intra_error <= 0) intra_error = 1;
189 stats->ref[INTRA_FRAME].err = intra_error;
190
191 // Golden frame MV search, if it exists and is different than last frame
192 if (golden_ref) {
193 int g_motion_error;
194 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
195 xd->plane[0].pre[0].stride = golden_ref->y_stride;
196 g_motion_error =
197 do_16x16_motion_search(cpi, prev_golden_ref_mv, mb_row, mb_col);
198 stats->ref[GOLDEN_FRAME].m.mv = x->best_mv;
199 stats->ref[GOLDEN_FRAME].err = g_motion_error;
200 } else {
201 stats->ref[GOLDEN_FRAME].err = INT_MAX;
202 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
203 }
204
205 // Do an Alt-ref frame MV search, if it exists and is different than
206 // last/golden frame.
207 if (alt_ref) {
208 int a_motion_error;
209 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
210 xd->plane[0].pre[0].stride = alt_ref->y_stride;
211 a_motion_error =
212 do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
213
214 stats->ref[ALTREF_FRAME].err = a_motion_error;
215 } else {
216 stats->ref[ALTREF_FRAME].err = INT_MAX;
217 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
218 }
219}
220
Yaowu Xuf883b422016-08-30 14:01:10 -0700221static void update_mbgraph_frame_stats(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 MBGRAPH_FRAME_STATS *stats,
223 YV12_BUFFER_CONFIG *buf,
224 YV12_BUFFER_CONFIG *golden_ref,
225 YV12_BUFFER_CONFIG *alt_ref) {
226 MACROBLOCK *const x = &cpi->td.mb;
227 MACROBLOCKD *const xd = &x->e_mbd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700228 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700229
230 int mb_col, mb_row, offset = 0;
231 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
232 MV gld_top_mv = { 0, 0 };
233 MODE_INFO mi_local;
234
Yaowu Xuf883b422016-08-30 14:01:10 -0700235 av1_zero(mi_local);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 // Set up limit values for motion vectors to prevent them extending outside
237 // the UMV borders.
Alex Converse0fa0f422017-04-24 12:51:14 -0700238 x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
239 x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 xd->up_available = 0;
241 xd->plane[0].dst.stride = buf->y_stride;
242 xd->plane[0].pre[0].stride = buf->y_stride;
243 xd->plane[1].dst.stride = buf->uv_stride;
244 xd->mi[0] = &mi_local;
245 mi_local.mbmi.sb_type = BLOCK_16X16;
246 mi_local.mbmi.ref_frame[0] = LAST_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -0500247 mi_local.mbmi.ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248
249 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
250 MV gld_left_mv = gld_top_mv;
251 int mb_y_in_offset = mb_y_offset;
252 int arf_y_in_offset = arf_y_offset;
253 int gld_y_in_offset = gld_y_offset;
254
255 // Set up limit values for motion vectors to prevent them extending outside
256 // the UMV borders.
Alex Converse0fa0f422017-04-24 12:51:14 -0700257 x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
258 x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 xd->left_available = 0;
260
261 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
262 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
263
264 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
265 &gld_left_mv, alt_ref, mb_row, mb_col);
266 gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
267 if (mb_col == 0) {
268 gld_top_mv = gld_left_mv;
269 }
270 xd->left_available = 1;
271 mb_y_in_offset += 16;
272 gld_y_in_offset += 16;
273 arf_y_in_offset += 16;
Alex Converse0fa0f422017-04-24 12:51:14 -0700274 x->mv_limits.col_min -= 16;
275 x->mv_limits.col_max -= 16;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 }
277 xd->up_available = 1;
278 mb_y_offset += buf->y_stride * 16;
279 gld_y_offset += golden_ref->y_stride * 16;
280 if (alt_ref) arf_y_offset += alt_ref->y_stride * 16;
Alex Converse0fa0f422017-04-24 12:51:14 -0700281 x->mv_limits.row_min -= 16;
282 x->mv_limits.row_max -= 16;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 offset += cm->mb_cols;
284 }
285}
286
287// void separate_arf_mbs_byzz
Yaowu Xuf883b422016-08-30 14:01:10 -0700288static void separate_arf_mbs(AV1_COMP *cpi) {
289 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 int mb_col, mb_row, offset, i;
291 int mi_row, mi_col;
292 int ncnt[4] = { 0 };
293 int n_frames = cpi->mbgraph_n_frames;
294
295 int *arf_not_zz;
296
297 CHECK_MEM_ERROR(
298 cm, arf_not_zz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700299 aom_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300
301 // We are not interested in results beyond the alt ref itself.
302 if (n_frames > cpi->rc.frames_till_gf_update_due)
303 n_frames = cpi->rc.frames_till_gf_update_due;
304
305 // defer cost to reference frames
306 for (i = n_frames - 1; i >= 0; i--) {
307 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
308
309 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
310 offset += cm->mb_cols, mb_row++) {
311 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
312 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
313
314 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
315 int intra_err = mb_stats->ref[INTRA_FRAME].err;
316 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
317
318 // Test for altref vs intra and gf and that its mv was 0,0.
319 if (altref_err > 1000 || altref_err > intra_err ||
320 altref_err > golden_err) {
321 arf_not_zz[offset + mb_col]++;
322 }
323 }
324 }
325 }
326
327 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
328 // of bound access in segmentation_map
329 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
330 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
331 // If any of the blocks in the sequence failed then the MB
332 // goes in segment 0
333 if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
334 ncnt[0]++;
335 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
336 } else {
337 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
338 ncnt[1]++;
339 }
340 }
341 }
342
343 // Only bother with segmentation if over 10% of the MBs in static segment
344 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
345 if (1) {
346 // Note % of blocks that are marked as static
347 if (cm->MBs)
348 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
349
350 // This error case should not be reachable as this function should
351 // never be called with the common data structure uninitialized.
352 else
353 cpi->static_mb_pct = 0;
354
Yaowu Xuf883b422016-08-30 14:01:10 -0700355 av1_enable_segmentation(&cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356 } else {
357 cpi->static_mb_pct = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700358 av1_disable_segmentation(&cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700359 }
360
361 // Free localy allocated storage
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 aom_free(arf_not_zz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700363}
364
Yaowu Xuf883b422016-08-30 14:01:10 -0700365void av1_update_mbgraph_stats(AV1_COMP *cpi) {
366 AV1_COMMON *const cm = &cpi->common;
367 int i, n_frames = av1_lookahead_depth(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700368 YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
369
370 assert(golden_ref != NULL);
371
372 // we need to look ahead beyond where the ARF transitions into
373 // being a GF - so exit if we don't look ahead beyond that
374 if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
375
376 if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
377
378 cpi->mbgraph_n_frames = n_frames;
379 for (i = 0; i < n_frames; i++) {
380 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
381 memset(frame_stats->mb_stats, 0,
382 cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
383 }
384
385 // do motion search to find contribution of each reference to data
386 // later on in this GF group
387 // FIXME really, the GF/last MC search should be done forward, and
388 // the ARF MC search backwards, to get optimal results for MV caching
389 for (i = 0; i < n_frames; i++) {
390 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700391 struct lookahead_entry *q_cur = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392
393 assert(q_cur != NULL);
394
395 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
Alex Conversef77fd0b2017-04-20 11:00:24 -0700396 cpi->source);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 }
398
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400
401 separate_arf_mbs(cpi);
402}