• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include <limits.h>
12 
13 #include "./vp9_rtcd.h"
14 #include "./vpx_dsp_rtcd.h"
15 
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/system_state.h"
19 #include "vp9/encoder/vp9_segmentation.h"
20 #include "vp9/encoder/vp9_mcomp.h"
21 #include "vp9/common/vp9_blockd.h"
22 #include "vp9/common/vp9_reconinter.h"
23 #include "vp9/common/vp9_reconintra.h"
24 
do_16x16_motion_iteration(VP9_COMP * cpi,const MV * ref_mv,MV * dst_mv,int mb_row,int mb_col)25 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi, const MV *ref_mv,
26                                               MV *dst_mv, int mb_row,
27                                               int mb_col) {
28   MACROBLOCK *const x = &cpi->td.mb;
29   MACROBLOCKD *const xd = &x->e_mbd;
30   MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
31   const SEARCH_METHODS old_search_method = mv_sf->search_method;
32   const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
33   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   int step_param = mv_sf->reduce_first_step_size;
39   step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
40 
41   vp9_set_mv_search_range(&x->mv_limits, ref_mv);
42 
43   ref_full.col = ref_mv->col >> 3;
44   ref_full.row = ref_mv->row >> 3;
45 
46   mv_sf->search_method = HEX;
47   vp9_full_pixel_search(cpi, x, BLOCK_16X16, &ref_full, step_param,
48                         cpi->sf.mv.search_method, x->errorperbit,
49                         cond_cost_list(cpi, cost_list), ref_mv, dst_mv, 0, 0);
50   mv_sf->search_method = old_search_method;
51 
52   /* restore UMV window */
53   x->mv_limits = tmp_mv_limits;
54 
55   // Try sub-pixel MC
56   // if (bestsme > error_thresh && bestsme < INT_MAX)
57   {
58     uint32_t distortion;
59     uint32_t sse;
60     // TODO(yunqing): may use higher tap interp filter than 2 taps if needed.
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_search_level,
64         cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0,
65         0, USE_2_TAPS);
66   }
67 
68   xd->mi[0]->mode = NEWMV;
69   xd->mi[0]->mv[0].as_mv = *dst_mv;
70 
71   vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
72 
73   return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
74                       xd->plane[0].dst.buf, xd->plane[0].dst.stride);
75 }
76 
do_16x16_motion_search(VP9_COMP * cpi,const MV * ref_mv,int_mv * dst_mv,int mb_row,int mb_col)77 static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
78                                   int_mv *dst_mv, int mb_row, int mb_col) {
79   MACROBLOCK *const x = &cpi->td.mb;
80   MACROBLOCKD *const xd = &x->e_mbd;
81   unsigned int err, tmp_err;
82   MV tmp_mv;
83 
84   // Try zero MV first
85   // FIXME should really use something like near/nearest MV and/or MV prediction
86   err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
87                      xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
88   dst_mv->as_int = 0;
89 
90   // Test last reference frame using the previous best mv as the
91   // starting point (best reference) for the search
92   tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
93   if (tmp_err < err) {
94     err = tmp_err;
95     dst_mv->as_mv = tmp_mv;
96   }
97 
98   // If the current best reference mv is not centered on 0,0 then do a 0,0
99   // based search as well.
100   if (ref_mv->row != 0 || ref_mv->col != 0) {
101     unsigned int tmp_err;
102     MV zero_ref_mv = { 0, 0 }, tmp_mv;
103 
104     tmp_err =
105         do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv, mb_row, mb_col);
106     if (tmp_err < err) {
107       dst_mv->as_mv = tmp_mv;
108       err = tmp_err;
109     }
110   }
111 
112   return err;
113 }
114 
do_16x16_zerozero_search(VP9_COMP * cpi,int_mv * dst_mv)115 static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
116   MACROBLOCK *const x = &cpi->td.mb;
117   MACROBLOCKD *const xd = &x->e_mbd;
118   unsigned int err;
119 
120   // Try zero MV first
121   // FIXME should really use something like near/nearest MV and/or MV prediction
122   err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
123                      xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
124 
125   dst_mv->as_int = 0;
126 
127   return err;
128 }
find_best_16x16_intra(VP9_COMP * cpi,PREDICTION_MODE * pbest_mode)129 static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
130   MACROBLOCK *const x = &cpi->td.mb;
131   MACROBLOCKD *const xd = &x->e_mbd;
132   PREDICTION_MODE best_mode = -1, mode;
133   unsigned int best_err = INT_MAX;
134 
135   // calculate SATD for each intra prediction mode;
136   // we're intentionally not doing 4x4, we just want a rough estimate
137   for (mode = DC_PRED; mode <= TM_PRED; mode++) {
138     unsigned int err;
139 
140     xd->mi[0]->mode = mode;
141     vp9_predict_intra_block(xd, 2, TX_16X16, mode, x->plane[0].src.buf,
142                             x->plane[0].src.stride, xd->plane[0].dst.buf,
143                             xd->plane[0].dst.stride, 0, 0, 0);
144     err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
145                        xd->plane[0].dst.buf, xd->plane[0].dst.stride);
146 
147     // find best
148     if (err < best_err) {
149       best_err = err;
150       best_mode = mode;
151     }
152   }
153 
154   if (pbest_mode) *pbest_mode = best_mode;
155 
156   return best_err;
157 }
158 
update_mbgraph_mb_stats(VP9_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)159 static void update_mbgraph_mb_stats(VP9_COMP *cpi, MBGRAPH_MB_STATS *stats,
160                                     YV12_BUFFER_CONFIG *buf, int mb_y_offset,
161                                     YV12_BUFFER_CONFIG *golden_ref,
162                                     const MV *prev_golden_ref_mv,
163                                     YV12_BUFFER_CONFIG *alt_ref, int mb_row,
164                                     int mb_col) {
165   MACROBLOCK *const x = &cpi->td.mb;
166   MACROBLOCKD *const xd = &x->e_mbd;
167   int intra_error;
168   VP9_COMMON *cm = &cpi->common;
169 
170   // FIXME in practice we're completely ignoring chroma here
171   x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
172   x->plane[0].src.stride = buf->y_stride;
173 
174   xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
175   xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
176 
177   // do intra 16x16 prediction
178   intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
179   if (intra_error <= 0) intra_error = 1;
180   stats->ref[INTRA_FRAME].err = intra_error;
181 
182   // Golden frame MV search, if it exists and is different than last frame
183   if (golden_ref) {
184     int g_motion_error;
185     xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
186     xd->plane[0].pre[0].stride = golden_ref->y_stride;
187     g_motion_error =
188         do_16x16_motion_search(cpi, prev_golden_ref_mv,
189                                &stats->ref[GOLDEN_FRAME].m.mv, mb_row, mb_col);
190     stats->ref[GOLDEN_FRAME].err = g_motion_error;
191   } else {
192     stats->ref[GOLDEN_FRAME].err = INT_MAX;
193     stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
194   }
195 
196   // Do an Alt-ref frame MV search, if it exists and is different than
197   // last/golden frame.
198   if (alt_ref) {
199     int a_motion_error;
200     xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
201     xd->plane[0].pre[0].stride = alt_ref->y_stride;
202     a_motion_error =
203         do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
204 
205     stats->ref[ALTREF_FRAME].err = a_motion_error;
206   } else {
207     stats->ref[ALTREF_FRAME].err = INT_MAX;
208     stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
209   }
210 }
211 
update_mbgraph_frame_stats(VP9_COMP * cpi,MBGRAPH_FRAME_STATS * stats,YV12_BUFFER_CONFIG * buf,YV12_BUFFER_CONFIG * golden_ref,YV12_BUFFER_CONFIG * alt_ref)212 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
213                                        MBGRAPH_FRAME_STATS *stats,
214                                        YV12_BUFFER_CONFIG *buf,
215                                        YV12_BUFFER_CONFIG *golden_ref,
216                                        YV12_BUFFER_CONFIG *alt_ref) {
217   MACROBLOCK *const x = &cpi->td.mb;
218   MACROBLOCKD *const xd = &x->e_mbd;
219   VP9_COMMON *const cm = &cpi->common;
220 
221   int mb_col, mb_row, offset = 0;
222   int mb_y_offset = 0;
223   MV gld_top_mv = { 0, 0 };
224   MODE_INFO mi_local;
225   MODE_INFO mi_above, mi_left;
226 
227   vp9_zero(mi_local);
228   // Set up limit values for motion vectors to prevent them extending outside
229   // the UMV borders.
230   x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
231   x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
232   // Signal to vp9_predict_intra_block() that above is not available
233   xd->above_mi = NULL;
234 
235   xd->plane[0].dst.stride = buf->y_stride;
236   xd->plane[0].pre[0].stride = buf->y_stride;
237   xd->plane[1].dst.stride = buf->uv_stride;
238   xd->mi[0] = &mi_local;
239   mi_local.sb_type = BLOCK_16X16;
240   mi_local.ref_frame[0] = LAST_FRAME;
241   mi_local.ref_frame[1] = NONE;
242 
243   for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
244     MV gld_left_mv = gld_top_mv;
245     int mb_y_in_offset = mb_y_offset;
246 
247     // Set up limit values for motion vectors to prevent them extending outside
248     // the UMV borders.
249     x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
250     x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
251     // Signal to vp9_predict_intra_block() that left is not available
252     xd->left_mi = NULL;
253 
254     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
255       MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
256 
257       update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
258                               &gld_left_mv, alt_ref, mb_row, mb_col);
259       gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
260       if (mb_col == 0) {
261         gld_top_mv = gld_left_mv;
262       }
263       // Signal to vp9_predict_intra_block() that left is available
264       xd->left_mi = &mi_left;
265 
266       mb_y_in_offset += 16;
267       x->mv_limits.col_min -= 16;
268       x->mv_limits.col_max -= 16;
269     }
270 
271     // Signal to vp9_predict_intra_block() that above is available
272     xd->above_mi = &mi_above;
273 
274     mb_y_offset += buf->y_stride * 16;
275     x->mv_limits.row_min -= 16;
276     x->mv_limits.row_max -= 16;
277     offset += cm->mb_cols;
278   }
279 }
280 
281 // void separate_arf_mbs_byzz
separate_arf_mbs(VP9_COMP * cpi)282 static void separate_arf_mbs(VP9_COMP *cpi) {
283   VP9_COMMON *const cm = &cpi->common;
284   int mb_col, mb_row, offset, i;
285   int mi_row, mi_col;
286   int ncnt[4] = { 0 };
287   int n_frames = cpi->mbgraph_n_frames;
288 
289   int *arf_not_zz;
290 
291   CHECK_MEM_ERROR(
292       cm, arf_not_zz,
293       vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
294 
295   // We are not interested in results beyond the alt ref itself.
296   if (n_frames > cpi->rc.frames_till_gf_update_due)
297     n_frames = cpi->rc.frames_till_gf_update_due;
298 
299   // defer cost to reference frames
300   for (i = n_frames - 1; i >= 0; i--) {
301     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
302 
303     for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
304          offset += cm->mb_cols, mb_row++) {
305       for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
306         MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
307 
308         int altref_err = mb_stats->ref[ALTREF_FRAME].err;
309         int intra_err = mb_stats->ref[INTRA_FRAME].err;
310         int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
311 
312         // Test for altref vs intra and gf and that its mv was 0,0.
313         if (altref_err > 1000 || altref_err > intra_err ||
314             altref_err > golden_err) {
315           arf_not_zz[offset + mb_col]++;
316         }
317       }
318     }
319   }
320 
321   // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
322   // of bound access in segmentation_map
323   for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
324     for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
325       // If any of the blocks in the sequence failed then the MB
326       // goes in segment 0
327       if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
328         ncnt[0]++;
329         cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
330       } else {
331         cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
332         ncnt[1]++;
333       }
334     }
335   }
336 
337   // Only bother with segmentation if over 10% of the MBs in static segment
338   // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
339   if (1) {
340     // Note % of blocks that are marked as static
341     if (cm->MBs)
342       cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
343 
344     // This error case should not be reachable as this function should
345     // never be called with the common data structure uninitialized.
346     else
347       cpi->static_mb_pct = 0;
348 
349     vp9_enable_segmentation(&cm->seg);
350   } else {
351     cpi->static_mb_pct = 0;
352     vp9_disable_segmentation(&cm->seg);
353   }
354 
355   // Free localy allocated storage
356   vpx_free(arf_not_zz);
357 }
358 
vp9_update_mbgraph_stats(VP9_COMP * cpi)359 void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
360   VP9_COMMON *const cm = &cpi->common;
361   int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
362   YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
363 
364   assert(golden_ref != NULL);
365 
366   // we need to look ahead beyond where the ARF transitions into
367   // being a GF - so exit if we don't look ahead beyond that
368   if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
369 
370   if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
371 
372   cpi->mbgraph_n_frames = n_frames;
373   for (i = 0; i < n_frames; i++) {
374     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
375     memset(frame_stats->mb_stats, 0,
376            cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
377   }
378 
379   // do motion search to find contribution of each reference to data
380   // later on in this GF group
381   // FIXME really, the GF/last MC search should be done forward, and
382   // the ARF MC search backwards, to get optimal results for MV caching
383   for (i = 0; i < n_frames; i++) {
384     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
385     struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
386 
387     assert(q_cur != NULL);
388 
389     update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
390                                cpi->Source);
391   }
392 
393   vpx_clear_system_state();
394 
395   separate_arf_mbs(cpi);
396 }
397