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