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