Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * 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.
10 : */
11 :
12 : #include <limits.h>
13 :
14 : #include "./av1_rtcd.h"
15 : #include "./aom_dsp_rtcd.h"
16 :
17 : #include "aom_dsp/aom_dsp_common.h"
18 : #include "aom_mem/aom_mem.h"
19 : #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 :
26 0 : static unsigned int do_16x16_motion_iteration(AV1_COMP *cpi, const MV *ref_mv,
27 : int mb_row, int mb_col) {
28 0 : MACROBLOCK *const x = &cpi->td.mb;
29 0 : MACROBLOCKD *const xd = &x->e_mbd;
30 0 : const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
31 0 : const aom_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
32 :
33 0 : const MvLimits tmp_mv_limits = x->mv_limits;
34 : MV ref_full;
35 : int cost_list[5];
36 :
37 : // Further step/diamond searches as necessary
38 0 : int step_param = mv_sf->reduce_first_step_size;
39 0 : step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
40 :
41 0 : av1_set_mv_search_range(&x->mv_limits, ref_mv);
42 :
43 0 : ref_full.col = ref_mv->col >> 3;
44 0 : ref_full.row = ref_mv->row >> 3;
45 :
46 : /*cpi->sf.search_method == HEX*/
47 0 : av1_hex_search(x, &ref_full, step_param, x->errorperbit, 0,
48 : cond_cost_list(cpi, cost_list), &v_fn_ptr, 0, ref_mv);
49 :
50 : // Try sub-pixel MC
51 : // if (bestsme > error_thresh && bestsme < INT_MAX)
52 : {
53 : int distortion;
54 : unsigned int sse;
55 0 : 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);
63 : }
64 :
65 : #if CONFIG_EXT_INTER
66 0 : if (has_second_ref(&xd->mi[0]->mbmi))
67 0 : xd->mi[0]->mbmi.mode = NEW_NEWMV;
68 : else
69 : #endif // CONFIG_EXT_INTER
70 0 : xd->mi[0]->mbmi.mode = NEWMV;
71 :
72 0 : xd->mi[0]->mbmi.mv[0] = x->best_mv;
73 : #if CONFIG_EXT_INTER
74 0 : xd->mi[0]->mbmi.ref_frame[1] = NONE_FRAME;
75 : #endif // CONFIG_EXT_INTER
76 :
77 0 : av1_build_inter_predictors_sby(&cpi->common, xd, mb_row, mb_col, NULL,
78 : BLOCK_16X16);
79 :
80 : /* restore UMV window */
81 0 : x->mv_limits = tmp_mv_limits;
82 :
83 0 : return aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
84 0 : xd->plane[0].dst.buf, xd->plane[0].dst.stride);
85 : }
86 :
87 0 : static int do_16x16_motion_search(AV1_COMP *cpi, const MV *ref_mv, int mb_row,
88 : int mb_col) {
89 0 : MACROBLOCK *const x = &cpi->td.mb;
90 0 : 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
96 0 : err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
97 0 : xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
98 0 : 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 0 : tmp_err = do_16x16_motion_iteration(cpi, ref_mv, mb_row, mb_col);
103 0 : if (tmp_err < err) {
104 0 : err = tmp_err;
105 0 : 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 0 : if (ref_mv->row != 0 || ref_mv->col != 0) {
111 0 : MV zero_ref_mv = { 0, 0 };
112 :
113 0 : tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, mb_row, mb_col);
114 0 : if (tmp_err < err) {
115 0 : err = tmp_err;
116 0 : best_mv = x->best_mv.as_mv;
117 : }
118 : }
119 :
120 0 : x->best_mv.as_mv = best_mv;
121 0 : return err;
122 : }
123 :
124 0 : static int do_16x16_zerozero_search(AV1_COMP *cpi, int_mv *dst_mv) {
125 0 : MACROBLOCK *const x = &cpi->td.mb;
126 0 : 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
131 0 : err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
132 0 : xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
133 :
134 0 : dst_mv->as_int = 0;
135 :
136 0 : return err;
137 : }
138 0 : static int find_best_16x16_intra(AV1_COMP *cpi, PREDICTION_MODE *pbest_mode) {
139 0 : MACROBLOCK *const x = &cpi->td.mb;
140 0 : MACROBLOCKD *const xd = &x->e_mbd;
141 0 : PREDICTION_MODE best_mode = -1, mode;
142 0 : 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 0 : for (mode = DC_PRED; mode <= TM_PRED; mode++) {
147 : unsigned int err;
148 :
149 0 : xd->mi[0]->mbmi.mode = mode;
150 0 : av1_predict_intra_block(xd, 16, 16, BLOCK_16X16, mode, x->plane[0].src.buf,
151 : x->plane[0].src.stride, xd->plane[0].dst.buf,
152 : xd->plane[0].dst.stride, 0, 0, 0);
153 0 : err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
154 0 : xd->plane[0].dst.buf, xd->plane[0].dst.stride);
155 :
156 : // find best
157 0 : if (err < best_err) {
158 0 : best_err = err;
159 0 : best_mode = mode;
160 : }
161 : }
162 :
163 0 : if (pbest_mode) *pbest_mode = best_mode;
164 :
165 0 : return best_err;
166 : }
167 :
168 0 : static void update_mbgraph_mb_stats(AV1_COMP *cpi, MBGRAPH_MB_STATS *stats,
169 : 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 0 : MACROBLOCK *const x = &cpi->td.mb;
175 0 : MACROBLOCKD *const xd = &x->e_mbd;
176 : int intra_error;
177 0 : AV1_COMMON *cm = &cpi->common;
178 :
179 : // FIXME in practice we're completely ignoring chroma here
180 0 : x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
181 0 : x->plane[0].src.stride = buf->y_stride;
182 :
183 0 : xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
184 0 : xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
185 :
186 : // do intra 16x16 prediction
187 0 : intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
188 0 : if (intra_error <= 0) intra_error = 1;
189 0 : stats->ref[INTRA_FRAME].err = intra_error;
190 :
191 : // Golden frame MV search, if it exists and is different than last frame
192 0 : if (golden_ref) {
193 : int g_motion_error;
194 0 : xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
195 0 : xd->plane[0].pre[0].stride = golden_ref->y_stride;
196 0 : g_motion_error =
197 : do_16x16_motion_search(cpi, prev_golden_ref_mv, mb_row, mb_col);
198 0 : stats->ref[GOLDEN_FRAME].m.mv = x->best_mv;
199 0 : stats->ref[GOLDEN_FRAME].err = g_motion_error;
200 : } else {
201 0 : stats->ref[GOLDEN_FRAME].err = INT_MAX;
202 0 : 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 0 : if (alt_ref) {
208 : int a_motion_error;
209 0 : xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
210 0 : xd->plane[0].pre[0].stride = alt_ref->y_stride;
211 0 : a_motion_error =
212 0 : do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
213 :
214 0 : stats->ref[ALTREF_FRAME].err = a_motion_error;
215 : } else {
216 0 : stats->ref[ALTREF_FRAME].err = INT_MAX;
217 0 : stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
218 : }
219 0 : }
220 :
221 0 : static void update_mbgraph_frame_stats(AV1_COMP *cpi,
222 : MBGRAPH_FRAME_STATS *stats,
223 : YV12_BUFFER_CONFIG *buf,
224 : YV12_BUFFER_CONFIG *golden_ref,
225 : YV12_BUFFER_CONFIG *alt_ref) {
226 0 : MACROBLOCK *const x = &cpi->td.mb;
227 0 : MACROBLOCKD *const xd = &x->e_mbd;
228 0 : AV1_COMMON *const cm = &cpi->common;
229 :
230 0 : int mb_col, mb_row, offset = 0;
231 0 : int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
232 0 : MV gld_top_mv = { 0, 0 };
233 : MODE_INFO mi_local;
234 :
235 0 : av1_zero(mi_local);
236 : // Set up limit values for motion vectors to prevent them extending outside
237 : // the UMV borders.
238 0 : x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
239 0 : x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
240 0 : xd->up_available = 0;
241 0 : xd->plane[0].dst.stride = buf->y_stride;
242 0 : xd->plane[0].pre[0].stride = buf->y_stride;
243 0 : xd->plane[1].dst.stride = buf->uv_stride;
244 0 : xd->mi[0] = &mi_local;
245 0 : mi_local.mbmi.sb_type = BLOCK_16X16;
246 0 : mi_local.mbmi.ref_frame[0] = LAST_FRAME;
247 0 : mi_local.mbmi.ref_frame[1] = NONE_FRAME;
248 :
249 0 : for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
250 0 : MV gld_left_mv = gld_top_mv;
251 0 : int mb_y_in_offset = mb_y_offset;
252 0 : int arf_y_in_offset = arf_y_offset;
253 0 : 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.
257 0 : x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
258 0 : x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
259 0 : xd->left_available = 0;
260 :
261 0 : for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
262 0 : MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
263 :
264 0 : 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 0 : gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
267 0 : if (mb_col == 0) {
268 0 : gld_top_mv = gld_left_mv;
269 : }
270 0 : xd->left_available = 1;
271 0 : mb_y_in_offset += 16;
272 0 : gld_y_in_offset += 16;
273 0 : arf_y_in_offset += 16;
274 0 : x->mv_limits.col_min -= 16;
275 0 : x->mv_limits.col_max -= 16;
276 : }
277 0 : xd->up_available = 1;
278 0 : mb_y_offset += buf->y_stride * 16;
279 0 : gld_y_offset += golden_ref->y_stride * 16;
280 0 : if (alt_ref) arf_y_offset += alt_ref->y_stride * 16;
281 0 : x->mv_limits.row_min -= 16;
282 0 : x->mv_limits.row_max -= 16;
283 0 : offset += cm->mb_cols;
284 : }
285 0 : }
286 :
287 : // void separate_arf_mbs_byzz
288 0 : static void separate_arf_mbs(AV1_COMP *cpi) {
289 0 : AV1_COMMON *const cm = &cpi->common;
290 : int mb_col, mb_row, offset, i;
291 : int mi_row, mi_col;
292 0 : int ncnt[4] = { 0 };
293 0 : int n_frames = cpi->mbgraph_n_frames;
294 :
295 : int *arf_not_zz;
296 :
297 0 : CHECK_MEM_ERROR(
298 : cm, arf_not_zz,
299 : aom_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
300 :
301 : // We are not interested in results beyond the alt ref itself.
302 0 : if (n_frames > cpi->rc.frames_till_gf_update_due)
303 0 : n_frames = cpi->rc.frames_till_gf_update_due;
304 :
305 : // defer cost to reference frames
306 0 : for (i = n_frames - 1; i >= 0; i--) {
307 0 : MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
308 :
309 0 : for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
310 0 : offset += cm->mb_cols, mb_row++) {
311 0 : for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
312 0 : MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
313 :
314 0 : int altref_err = mb_stats->ref[ALTREF_FRAME].err;
315 0 : int intra_err = mb_stats->ref[INTRA_FRAME].err;
316 0 : 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 0 : if (altref_err > 1000 || altref_err > intra_err ||
320 : altref_err > golden_err) {
321 0 : 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 0 : for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
330 0 : 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 0 : if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
334 0 : ncnt[0]++;
335 0 : cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
336 : } else {
337 0 : cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
338 0 : 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 0 : if (cm->MBs)
348 0 : 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 0 : cpi->static_mb_pct = 0;
354 :
355 0 : av1_enable_segmentation(&cm->seg);
356 : } else {
357 : cpi->static_mb_pct = 0;
358 : av1_disable_segmentation(&cm->seg);
359 : }
360 :
361 : // Free localy allocated storage
362 0 : aom_free(arf_not_zz);
363 0 : }
364 :
365 0 : void av1_update_mbgraph_stats(AV1_COMP *cpi) {
366 0 : AV1_COMMON *const cm = &cpi->common;
367 0 : int i, n_frames = av1_lookahead_depth(cpi->lookahead);
368 0 : YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
369 :
370 0 : 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 0 : if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
375 :
376 0 : if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
377 :
378 0 : cpi->mbgraph_n_frames = n_frames;
379 0 : for (i = 0; i < n_frames; i++) {
380 0 : MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
381 0 : memset(frame_stats->mb_stats, 0,
382 0 : 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 0 : for (i = 0; i < n_frames; i++) {
390 0 : MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
391 0 : struct lookahead_entry *q_cur = av1_lookahead_peek(cpi->lookahead, i);
392 :
393 0 : assert(q_cur != NULL);
394 :
395 0 : update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
396 : cpi->source);
397 : }
398 :
399 0 : aom_clear_system_state();
400 :
401 0 : separate_arf_mbs(cpi);
402 : }
|