Merge "Fix high bit depth in vp10 codebase"
[platform/upstream/libvpx.git] / vp10 / 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 "./vp10_rtcd.h"
14 #include "./vpx_dsp_rtcd.h"
15
16 #include "vpx_mem/vpx_mem.h"
17 #include "vp10/encoder/vp9_segmentation.h"
18 #include "vp10/encoder/vp9_mcomp.h"
19 #include "vp10/common/vp9_blockd.h"
20 #include "vp10/common/vp9_reconinter.h"
21 #include "vp10/common/vp9_reconintra.h"
22 #include "vp10/common/vp9_systemdependent.h"
23
24
25 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
26                                               const MV *ref_mv,
27                                               MV *dst_mv,
28                                               int mb_row,
29                                               int mb_col) {
30   MACROBLOCK *const x = &cpi->td.mb;
31   MACROBLOCKD *const xd = &x->e_mbd;
32   const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
33   const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
34
35   const int tmp_col_min = x->mv_col_min;
36   const int tmp_col_max = x->mv_col_max;
37   const int tmp_row_min = x->mv_row_min;
38   const int tmp_row_max = x->mv_row_max;
39   MV ref_full;
40   int cost_list[5];
41
42   // Further step/diamond searches as necessary
43   int step_param = mv_sf->reduce_first_step_size;
44   step_param = MIN(step_param, MAX_MVSEARCH_STEPS - 2);
45
46   vp10_set_mv_search_range(x, ref_mv);
47
48   ref_full.col = ref_mv->col >> 3;
49   ref_full.row = ref_mv->row >> 3;
50
51   /*cpi->sf.search_method == HEX*/
52   vp10_hex_search(x, &ref_full, step_param, x->errorperbit, 0,
53                  cond_cost_list(cpi, cost_list),
54                  &v_fn_ptr, 0, ref_mv, dst_mv);
55
56   // Try sub-pixel MC
57   // if (bestsme > error_thresh && bestsme < INT_MAX)
58   {
59     int distortion;
60     unsigned int sse;
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_iters_per_step,
64         cond_cost_list(cpi, cost_list),
65         NULL, NULL,
66         &distortion, &sse, NULL, 0, 0);
67   }
68
69   xd->mi[0]->mbmi.mode = NEWMV;
70   xd->mi[0]->mbmi.mv[0].as_mv = *dst_mv;
71
72   vp10_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
73
74   /* restore UMV window */
75   x->mv_col_min = tmp_col_min;
76   x->mv_col_max = tmp_col_max;
77   x->mv_row_min = tmp_row_min;
78   x->mv_row_max = tmp_row_max;
79
80   return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
81                       xd->plane[0].dst.buf, xd->plane[0].dst.stride);
82 }
83
84 static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
85                                   int_mv *dst_mv, int mb_row, 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 tmp_mv;
90
91   // Try zero MV first
92   // FIXME should really use something like near/nearest MV and/or MV prediction
93   err = vpx_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   dst_mv->as_int = 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, &tmp_mv, mb_row, mb_col);
100   if (tmp_err < err) {
101     err = tmp_err;
102     dst_mv->as_mv = tmp_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     unsigned int tmp_err;
109     MV zero_ref_mv = {0, 0}, tmp_mv;
110
111     tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
112                                         mb_row, mb_col);
113     if (tmp_err < err) {
114       dst_mv->as_mv = tmp_mv;
115       err = tmp_err;
116     }
117   }
118
119   return err;
120 }
121
122 static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
123   MACROBLOCK *const x = &cpi->td.mb;
124   MACROBLOCKD *const xd = &x->e_mbd;
125   unsigned int err;
126
127   // Try zero MV first
128   // FIXME should really use something like near/nearest MV and/or MV prediction
129   err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
130                      xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
131
132   dst_mv->as_int = 0;
133
134   return err;
135 }
136 static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
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 = DC_PRED; mode <= TM_PRED; mode++) {
145     unsigned int err;
146
147     xd->mi[0]->mbmi.mode = mode;
148     vp10_predict_intra_block(xd, 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 = vpx_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)
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   const MV *prev_golden_ref_mv,
176   YV12_BUFFER_CONFIG *alt_ref,
177   int mb_row,
178   int mb_col
179 ) {
180   MACROBLOCK *const x = &cpi->td.mb;
181   MACROBLOCKD *const xd = &x->e_mbd;
182   int intra_error;
183   VP9_COMMON *cm = &cpi->common;
184
185   // FIXME in practice we're completely ignoring chroma here
186   x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
187   x->plane[0].src.stride = buf->y_stride;
188
189   xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
190   xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
191
192   // do intra 16x16 prediction
193   intra_error = find_best_16x16_intra(cpi,
194                                       &stats->ref[INTRA_FRAME].m.mode);
195   if (intra_error <= 0)
196     intra_error = 1;
197   stats->ref[INTRA_FRAME].err = intra_error;
198
199   // Golden frame MV search, if it exists and is different than last frame
200   if (golden_ref) {
201     int g_motion_error;
202     xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
203     xd->plane[0].pre[0].stride = golden_ref->y_stride;
204     g_motion_error = do_16x16_motion_search(cpi,
205                                             prev_golden_ref_mv,
206                                             &stats->ref[GOLDEN_FRAME].m.mv,
207                                             mb_row, mb_col);
208     stats->ref[GOLDEN_FRAME].err = g_motion_error;
209   } else {
210     stats->ref[GOLDEN_FRAME].err = INT_MAX;
211     stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
212   }
213
214   // Do an Alt-ref frame MV search, if it exists and is different than
215   // last/golden frame.
216   if (alt_ref) {
217     int a_motion_error;
218     xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
219     xd->plane[0].pre[0].stride = alt_ref->y_stride;
220     a_motion_error = do_16x16_zerozero_search(cpi,
221                                               &stats->ref[ALTREF_FRAME].m.mv);
222
223     stats->ref[ALTREF_FRAME].err = a_motion_error;
224   } else {
225     stats->ref[ALTREF_FRAME].err = INT_MAX;
226     stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
227   }
228 }
229
230 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
231                                        MBGRAPH_FRAME_STATS *stats,
232                                        YV12_BUFFER_CONFIG *buf,
233                                        YV12_BUFFER_CONFIG *golden_ref,
234                                        YV12_BUFFER_CONFIG *alt_ref) {
235   MACROBLOCK *const x = &cpi->td.mb;
236   MACROBLOCKD *const xd = &x->e_mbd;
237   VP9_COMMON *const cm = &cpi->common;
238
239   int mb_col, mb_row, offset = 0;
240   int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
241   MV gld_top_mv = {0, 0};
242   MODE_INFO mi_local;
243
244   vp10_zero(mi_local);
245   // Set up limit values for motion vectors to prevent them extending outside
246   // the UMV borders.
247   x->mv_row_min     = -BORDER_MV_PIXELS_B16;
248   x->mv_row_max     = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
249   xd->up_available  = 0;
250   xd->plane[0].dst.stride  = buf->y_stride;
251   xd->plane[0].pre[0].stride  = buf->y_stride;
252   xd->plane[1].dst.stride = buf->uv_stride;
253   xd->mi[0] = &mi_local;
254   mi_local.mbmi.sb_type = BLOCK_16X16;
255   mi_local.mbmi.ref_frame[0] = LAST_FRAME;
256   mi_local.mbmi.ref_frame[1] = NONE;
257
258   for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
259     MV gld_left_mv = gld_top_mv;
260     int mb_y_in_offset  = mb_y_offset;
261     int arf_y_in_offset = arf_y_offset;
262     int gld_y_in_offset = gld_y_offset;
263
264     // Set up limit values for motion vectors to prevent them extending outside
265     // the UMV borders.
266     x->mv_col_min      = -BORDER_MV_PIXELS_B16;
267     x->mv_col_max      = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
268     xd->left_available = 0;
269
270     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
271       MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
272
273       update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
274                               golden_ref, &gld_left_mv, alt_ref,
275                               mb_row, mb_col);
276       gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
277       if (mb_col == 0) {
278         gld_top_mv = gld_left_mv;
279       }
280       xd->left_available = 1;
281       mb_y_in_offset    += 16;
282       gld_y_in_offset   += 16;
283       arf_y_in_offset   += 16;
284       x->mv_col_min     -= 16;
285       x->mv_col_max     -= 16;
286     }
287     xd->up_available = 1;
288     mb_y_offset     += buf->y_stride * 16;
289     gld_y_offset    += golden_ref->y_stride * 16;
290     if (alt_ref)
291       arf_y_offset    += alt_ref->y_stride * 16;
292     x->mv_row_min   -= 16;
293     x->mv_row_max   -= 16;
294     offset          += cm->mb_cols;
295   }
296 }
297
298 // void separate_arf_mbs_byzz
299 static void separate_arf_mbs(VP9_COMP *cpi) {
300   VP9_COMMON *const cm = &cpi->common;
301   int mb_col, mb_row, offset, i;
302   int mi_row, mi_col;
303   int ncnt[4] = { 0 };
304   int n_frames = cpi->mbgraph_n_frames;
305
306   int *arf_not_zz;
307
308   CHECK_MEM_ERROR(cm, arf_not_zz,
309                   vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
310                              1));
311
312   // We are not interested in results beyond the alt ref itself.
313   if (n_frames > cpi->rc.frames_till_gf_update_due)
314     n_frames = cpi->rc.frames_till_gf_update_due;
315
316   // defer cost to reference frames
317   for (i = n_frames - 1; i >= 0; i--) {
318     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
319
320     for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
321          offset += cm->mb_cols, mb_row++) {
322       for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
323         MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
324
325         int altref_err = mb_stats->ref[ALTREF_FRAME].err;
326         int intra_err  = mb_stats->ref[INTRA_FRAME ].err;
327         int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
328
329         // Test for altref vs intra and gf and that its mv was 0,0.
330         if (altref_err > 1000 ||
331             altref_err > intra_err ||
332             altref_err > golden_err) {
333           arf_not_zz[offset + mb_col]++;
334         }
335       }
336     }
337   }
338
339   // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
340   // of bound access in segmentation_map
341   for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
342     for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
343       // If any of the blocks in the sequence failed then the MB
344       // goes in segment 0
345       if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
346         ncnt[0]++;
347         cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
348       } else {
349         cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
350         ncnt[1]++;
351       }
352     }
353   }
354
355   // Only bother with segmentation if over 10% of the MBs in static segment
356   // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
357   if (1) {
358     // Note % of blocks that are marked as static
359     if (cm->MBs)
360       cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
361
362     // This error case should not be reachable as this function should
363     // never be called with the common data structure uninitialized.
364     else
365       cpi->static_mb_pct = 0;
366
367     vp10_enable_segmentation(&cm->seg);
368   } else {
369     cpi->static_mb_pct = 0;
370     vp10_disable_segmentation(&cm->seg);
371   }
372
373   // Free localy allocated storage
374   vpx_free(arf_not_zz);
375 }
376
377 void vp10_update_mbgraph_stats(VP9_COMP *cpi) {
378   VP9_COMMON *const cm = &cpi->common;
379   int i, n_frames = vp10_lookahead_depth(cpi->lookahead);
380   YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
381
382   assert(golden_ref != NULL);
383
384   // we need to look ahead beyond where the ARF transitions into
385   // being a GF - so exit if we don't look ahead beyond that
386   if (n_frames <= cpi->rc.frames_till_gf_update_due)
387     return;
388
389   if (n_frames > MAX_LAG_BUFFERS)
390     n_frames = MAX_LAG_BUFFERS;
391
392   cpi->mbgraph_n_frames = n_frames;
393   for (i = 0; i < n_frames; i++) {
394     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
395     memset(frame_stats->mb_stats, 0,
396            cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
397   }
398
399   // do motion search to find contribution of each reference to data
400   // later on in this GF group
401   // FIXME really, the GF/last MC search should be done forward, and
402   // the ARF MC search backwards, to get optimal results for MV caching
403   for (i = 0; i < n_frames; i++) {
404     MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
405     struct lookahead_entry *q_cur = vp10_lookahead_peek(cpi->lookahead, i);
406
407     assert(q_cur != NULL);
408
409     update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
410                                golden_ref, cpi->Source);
411   }
412
413   vpx_clear_system_state();
414
415   separate_arf_mbs(cpi);
416 }