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 "config/av1_rtcd.h"
15 #include "config/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/common/blockd.h"
21 #include "av1/common/reconinter.h"
22 #include "av1/common/reconintra.h"
23 #include "av1/encoder/mcomp.h"
24 #include "av1/encoder/reconinter_enc.h"
25 #include "av1/encoder/segmentation.h"
26
do_16x16_motion_iteration(AV1_COMP * cpi,const MV * ref_mv,int mb_row,int mb_col)27 static unsigned int do_16x16_motion_iteration(AV1_COMP *cpi, const MV *ref_mv,
28 int mb_row, int mb_col) {
29 MACROBLOCK *const x = &cpi->td.mb;
30 MACROBLOCKD *const xd = &x->e_mbd;
31 const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
32 const aom_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
33
34 const MvLimits tmp_mv_limits = x->mv_limits;
35 MV ref_full;
36 int cost_list[5];
37
38 // Further step/diamond searches as necessary
39 int step_param = mv_sf->reduce_first_step_size;
40 step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
41
42 av1_set_mv_search_range(&x->mv_limits, ref_mv);
43
44 ref_full.col = ref_mv->col >> 3;
45 ref_full.row = ref_mv->row >> 3;
46
47 /*cpi->sf.search_method == HEX*/
48 av1_hex_search(x, &ref_full, step_param, x->errorperbit, 0,
49 cond_cost_list(cpi, cost_list), &v_fn_ptr, 0, ref_mv);
50
51 // Try sub-pixel MC
52 // if (bestsme > error_thresh && bestsme < INT_MAX)
53 if (cpi->common.cur_frame_force_integer_mv == 1) {
54 x->best_mv.as_mv.row *= 8;
55 x->best_mv.as_mv.col *= 8;
56 } else {
57 int distortion;
58 unsigned int sse;
59 cpi->find_fractional_mv_step(
60 x, &cpi->common, mb_row, mb_col, ref_mv,
61 cpi->common.allow_high_precision_mv, x->errorperbit, &v_fn_ptr, 0,
62 mv_sf->subpel_iters_per_step, cond_cost_list(cpi, cost_list), NULL,
63 NULL, &distortion, &sse, NULL, NULL, 0, 0, 0, 0, 0, 1);
64 }
65
66 if (has_second_ref(xd->mi[0]))
67 xd->mi[0]->mode = NEW_NEWMV;
68 else
69 xd->mi[0]->mode = NEWMV;
70
71 xd->mi[0]->mv[0] = x->best_mv;
72 xd->mi[0]->ref_frame[1] = NONE_FRAME;
73
74 av1_enc_build_inter_predictor(&cpi->common, xd, mb_row, mb_col, NULL,
75 BLOCK_16X16, AOM_PLANE_Y, AOM_PLANE_Y);
76
77 /* restore UMV window */
78 x->mv_limits = tmp_mv_limits;
79
80 return aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
81 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
82 }
83
do_16x16_motion_search(AV1_COMP * cpi,const MV * ref_mv,int mb_row,int mb_col)84 static int do_16x16_motion_search(AV1_COMP *cpi, const MV *ref_mv, int mb_row,
85 int mb_col) {
86 MACROBLOCK *const x = &cpi->td.mb;
87 MACROBLOCKD *const xd = &x->e_mbd;
88 unsigned int err, tmp_err;
89 MV best_mv;
90
91 // Try zero MV first
92 // FIXME should really use something like near/nearest MV and/or MV prediction
93 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
94 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
95 best_mv.col = best_mv.row = 0;
96
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, mb_row, mb_col);
100 if (tmp_err < err) {
101 err = tmp_err;
102 best_mv = x->best_mv.as_mv;
103 }
104
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 MV zero_ref_mv = kZeroMv;
109
110 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, mb_row, mb_col);
111 if (tmp_err < err) {
112 err = tmp_err;
113 best_mv = x->best_mv.as_mv;
114 }
115 }
116
117 x->best_mv.as_mv = best_mv;
118 return err;
119 }
120
do_16x16_zerozero_search(AV1_COMP * cpi,int_mv * dst_mv)121 static int do_16x16_zerozero_search(AV1_COMP *cpi, int_mv *dst_mv) {
122 MACROBLOCK *const x = &cpi->td.mb;
123 MACROBLOCKD *const xd = &x->e_mbd;
124 unsigned int err;
125
126 // Try zero MV first
127 // FIXME should really use something like near/nearest MV and/or MV prediction
128 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
129 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
130
131 dst_mv->as_int = 0;
132
133 return err;
134 }
find_best_16x16_intra(AV1_COMP * cpi,PREDICTION_MODE * pbest_mode)135 static int find_best_16x16_intra(AV1_COMP *cpi, PREDICTION_MODE *pbest_mode) {
136 const AV1_COMMON *cm = &cpi->common;
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;
141
142 // calculate SATD for each intra prediction mode;
143 // we're intentionally not doing 4x4, we just want a rough estimate
144 for (mode = INTRA_MODE_START; mode < INTRA_MODE_END; mode++) {
145 unsigned int err;
146
147 xd->mi[0]->mode = mode;
148 av1_predict_intra_block(cm, xd, 16, 16, TX_16X16, mode, 0, 0,
149 FILTER_INTRA_MODES, x->plane[0].src.buf,
150 x->plane[0].src.stride, xd->plane[0].dst.buf,
151 xd->plane[0].dst.stride, 0, 0, 0);
152 err = aom_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
153 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
154
155 // find best
156 if (err < best_err) {
157 best_err = err;
158 best_mode = mode;
159 }
160 }
161
162 if (pbest_mode) *pbest_mode = best_mode;
163
164 return best_err;
165 }
166
update_mbgraph_mb_stats(AV1_COMP * cpi,MBGRAPH_MB_STATS * stats,YV12_BUFFER_CONFIG * buf,int mb_y_offset,YV12_BUFFER_CONFIG * golden_ref,const MV * prev_golden_ref_mv,YV12_BUFFER_CONFIG * alt_ref,int mb_row,int mb_col)167 static void update_mbgraph_mb_stats(AV1_COMP *cpi, MBGRAPH_MB_STATS *stats,
168 YV12_BUFFER_CONFIG *buf, int mb_y_offset,
169 YV12_BUFFER_CONFIG *golden_ref,
170 const MV *prev_golden_ref_mv,
171 YV12_BUFFER_CONFIG *alt_ref, int mb_row,
172 int mb_col) {
173 MACROBLOCK *const x = &cpi->td.mb;
174 MACROBLOCKD *const xd = &x->e_mbd;
175 int intra_error;
176 AV1_COMMON *cm = &cpi->common;
177
178 // FIXME in practice we're completely ignoring chroma here
179 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
180 x->plane[0].src.stride = buf->y_stride;
181
182 xd->plane[0].dst.buf = cm->cur_frame->buf.y_buffer + mb_y_offset;
183 xd->plane[0].dst.stride = cm->cur_frame->buf.y_stride;
184
185 // do intra 16x16 prediction
186 intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
187 if (intra_error <= 0) intra_error = 1;
188 stats->ref[INTRA_FRAME].err = intra_error;
189
190 // Golden frame MV search, if it exists and is different than last frame
191 if (golden_ref) {
192 int g_motion_error;
193 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
194 xd->plane[0].pre[0].stride = golden_ref->y_stride;
195 g_motion_error =
196 do_16x16_motion_search(cpi, prev_golden_ref_mv, mb_row, mb_col);
197 stats->ref[GOLDEN_FRAME].m.mv = x->best_mv;
198 stats->ref[GOLDEN_FRAME].err = g_motion_error;
199 } else {
200 stats->ref[GOLDEN_FRAME].err = INT_MAX;
201 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
202 }
203
204 // Do an Alt-ref frame MV search, if it exists and is different than
205 // last/golden frame.
206 if (alt_ref) {
207 int a_motion_error;
208 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
209 xd->plane[0].pre[0].stride = alt_ref->y_stride;
210 a_motion_error =
211 do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
212
213 stats->ref[ALTREF_FRAME].err = a_motion_error;
214 } else {
215 stats->ref[ALTREF_FRAME].err = INT_MAX;
216 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
217 }
218 }
219
update_mbgraph_frame_stats(AV1_COMP * cpi,MBGRAPH_FRAME_STATS * stats,YV12_BUFFER_CONFIG * buf,YV12_BUFFER_CONFIG * golden_ref,YV12_BUFFER_CONFIG * alt_ref)220 static void update_mbgraph_frame_stats(AV1_COMP *cpi,
221 MBGRAPH_FRAME_STATS *stats,
222 YV12_BUFFER_CONFIG *buf,
223 YV12_BUFFER_CONFIG *golden_ref,
224 YV12_BUFFER_CONFIG *alt_ref) {
225 MACROBLOCK *const x = &cpi->td.mb;
226 MACROBLOCKD *const xd = &x->e_mbd;
227 AV1_COMMON *const cm = &cpi->common;
228
229 int mb_col, mb_row, offset = 0;
230 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
231 MV gld_top_mv = kZeroMv;
232 MB_MODE_INFO mi_local;
233
234 av1_zero(mi_local);
235 // Set up limit values for motion vectors to prevent them extending outside
236 // the UMV borders.
237 x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
238 x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
239 xd->up_available = 0;
240 xd->plane[0].dst.stride = buf->y_stride;
241 xd->plane[0].pre[0].stride = buf->y_stride;
242 xd->plane[1].dst.stride = buf->uv_stride;
243 xd->mi[0] = &mi_local;
244 mi_local.sb_type = BLOCK_16X16;
245 mi_local.ref_frame[0] = LAST_FRAME;
246 mi_local.ref_frame[1] = NONE_FRAME;
247
248 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
249 MV gld_left_mv = gld_top_mv;
250 int mb_y_in_offset = mb_y_offset;
251 int arf_y_in_offset = arf_y_offset;
252 int gld_y_in_offset = gld_y_offset;
253
254 // Set up limit values for motion vectors to prevent them extending outside
255 // the UMV borders.
256 x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
257 x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
258 xd->left_available = 0;
259
260 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
261 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
262
263 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
264 &gld_left_mv, alt_ref, mb_row, mb_col);
265 gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
266 if (mb_col == 0) {
267 gld_top_mv = gld_left_mv;
268 }
269 xd->left_available = 1;
270 mb_y_in_offset += 16;
271 gld_y_in_offset += 16;
272 arf_y_in_offset += 16;
273 x->mv_limits.col_min -= 16;
274 x->mv_limits.col_max -= 16;
275 }
276 xd->up_available = 1;
277 mb_y_offset += buf->y_stride * 16;
278 gld_y_offset += golden_ref->y_stride * 16;
279 if (alt_ref) arf_y_offset += alt_ref->y_stride * 16;
280 x->mv_limits.row_min -= 16;
281 x->mv_limits.row_max -= 16;
282 offset += cm->mb_cols;
283 }
284 }
285
286 // void separate_arf_mbs_byzz
separate_arf_mbs(AV1_COMP * cpi)287 static void separate_arf_mbs(AV1_COMP *cpi) {
288 AV1_COMMON *const cm = &cpi->common;
289 int mb_col, mb_row, offset, i;
290 int mi_row, mi_col;
291 int ncnt[4] = { 0 };
292 int n_frames = cpi->mbgraph_n_frames;
293
294 int *arf_not_zz;
295
296 CHECK_MEM_ERROR(
297 cm, arf_not_zz,
298 aom_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
299
300 // We are not interested in results beyond the alt ref itself.
301 if (n_frames > cpi->rc.frames_till_gf_update_due)
302 n_frames = cpi->rc.frames_till_gf_update_due;
303
304 // defer cost to reference frames
305 for (i = n_frames - 1; i >= 0; i--) {
306 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
307
308 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
309 offset += cm->mb_cols, mb_row++) {
310 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
311 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
312
313 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
314 int intra_err = mb_stats->ref[INTRA_FRAME].err;
315 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
316
317 // Test for altref vs intra and gf and that its mv was 0,0.
318 if (altref_err > 1000 || altref_err > intra_err ||
319 altref_err > golden_err) {
320 arf_not_zz[offset + mb_col]++;
321 }
322 }
323 }
324 }
325
326 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
327 // of bound access in segmentation_map
328 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
329 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
330 // If any of the blocks in the sequence failed then the MB
331 // goes in segment 0
332 if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
333 ncnt[0]++;
334 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
335 } else {
336 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
337 ncnt[1]++;
338 }
339 }
340 }
341
342 // Only bother with segmentation if over 10% of the MBs in static segment
343 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
344 if (1) {
345 // Note % of blocks that are marked as static
346 if (cm->MBs)
347 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
348
349 // This error case should not be reachable as this function should
350 // never be called with the common data structure uninitialized.
351 else
352 cpi->static_mb_pct = 0;
353
354 av1_enable_segmentation(&cm->seg);
355 } else {
356 cpi->static_mb_pct = 0;
357 av1_disable_segmentation(&cm->seg);
358 }
359
360 // Free localy allocated storage
361 aom_free(arf_not_zz);
362 }
363
av1_update_mbgraph_stats(AV1_COMP * cpi)364 void av1_update_mbgraph_stats(AV1_COMP *cpi) {
365 AV1_COMMON *const cm = &cpi->common;
366 int i, n_frames = av1_lookahead_depth(cpi->lookahead);
367 YV12_BUFFER_CONFIG *golden_ref = &get_ref_frame_buf(cm, GOLDEN_FRAME)->buf;
368
369 assert(golden_ref != NULL);
370
371 // we need to look ahead beyond where the ARF transitions into
372 // being a GF - so exit if we don't look ahead beyond that
373 if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
374
375 if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
376
377 cpi->mbgraph_n_frames = n_frames;
378 for (i = 0; i < n_frames; i++) {
379 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
380 memset(frame_stats->mb_stats, 0,
381 cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
382 }
383
384 // do motion search to find contribution of each reference to data
385 // later on in this GF group
386 // FIXME really, the GF/last MC search should be done forward, and
387 // the ARF MC search backwards, to get optimal results for MV caching
388 for (i = 0; i < n_frames; i++) {
389 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
390 struct lookahead_entry *q_cur = av1_lookahead_peek(cpi->lookahead, i);
391
392 assert(q_cur != NULL);
393
394 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
395 cpi->source);
396 }
397
398 aom_clear_system_state();
399
400 separate_arf_mbs(cpi);
401 }
402