Fork VP9 and VP10 codebase
[platform/upstream/libvpx.git] / vp10 / common / vp9_reconinter.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 <assert.h>
12
13 #include "./vpx_scale_rtcd.h"
14 #include "./vpx_config.h"
15
16 #include "vpx/vpx_integer.h"
17
18 #include "vp10/common/vp9_blockd.h"
19 #include "vp10/common/vp9_reconinter.h"
20 #include "vp10/common/vp9_reconintra.h"
21
22 #if CONFIG_VP9_HIGHBITDEPTH
23 void vp10_highbd_build_inter_predictor(const uint8_t *src, int src_stride,
24                                       uint8_t *dst, int dst_stride,
25                                       const MV *src_mv,
26                                       const struct scale_factors *sf,
27                                       int w, int h, int ref,
28                                       const InterpKernel *kernel,
29                                       enum mv_precision precision,
30                                       int x, int y, int bd) {
31   const int is_q4 = precision == MV_PRECISION_Q4;
32   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
33                      is_q4 ? src_mv->col : src_mv->col * 2 };
34   MV32 mv = vp10_scale_mv(&mv_q4, x, y, sf);
35   const int subpel_x = mv.col & SUBPEL_MASK;
36   const int subpel_y = mv.row & SUBPEL_MASK;
37
38   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
39
40   high_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
41                        sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4, bd);
42 }
43 #endif  // CONFIG_VP9_HIGHBITDEPTH
44
45 void vp10_build_inter_predictor(const uint8_t *src, int src_stride,
46                                uint8_t *dst, int dst_stride,
47                                const MV *src_mv,
48                                const struct scale_factors *sf,
49                                int w, int h, int ref,
50                                const InterpKernel *kernel,
51                                enum mv_precision precision,
52                                int x, int y) {
53   const int is_q4 = precision == MV_PRECISION_Q4;
54   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
55                      is_q4 ? src_mv->col : src_mv->col * 2 };
56   MV32 mv = vp10_scale_mv(&mv_q4, x, y, sf);
57   const int subpel_x = mv.col & SUBPEL_MASK;
58   const int subpel_y = mv.row & SUBPEL_MASK;
59
60   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
61
62   inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
63                   sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
64 }
65
66 void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
67                                    int bw, int bh,
68                                    int x, int y, int w, int h,
69                                    int mi_x, int mi_y) {
70   struct macroblockd_plane *const pd = &xd->plane[plane];
71   const MODE_INFO *mi = xd->mi[0];
72   const int is_compound = has_second_ref(&mi->mbmi);
73   const InterpKernel *kernel = vp10_filter_kernels[mi->mbmi.interp_filter];
74   int ref;
75
76   for (ref = 0; ref < 1 + is_compound; ++ref) {
77     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
78     struct buf_2d *const pre_buf = &pd->pre[ref];
79     struct buf_2d *const dst_buf = &pd->dst;
80     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
81     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
82                ? average_split_mvs(pd, mi, ref, block)
83                : mi->mbmi.mv[ref].as_mv;
84
85     // TODO(jkoleszar): This clamping is done in the incorrect place for the
86     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
87     // MV. Note however that it performs the subsampling aware scaling so
88     // that the result is always q4.
89     // mv_precision precision is MV_PRECISION_Q4.
90     const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
91                                                pd->subsampling_x,
92                                                pd->subsampling_y);
93
94     uint8_t *pre;
95     MV32 scaled_mv;
96     int xs, ys, subpel_x, subpel_y;
97     const int is_scaled = vp10_is_scaled(sf);
98
99     if (is_scaled) {
100       pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
101       scaled_mv = vp10_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
102       xs = sf->x_step_q4;
103       ys = sf->y_step_q4;
104     } else {
105       pre = pre_buf->buf + (y * pre_buf->stride + x);
106       scaled_mv.row = mv_q4.row;
107       scaled_mv.col = mv_q4.col;
108       xs = ys = 16;
109     }
110     subpel_x = scaled_mv.col & SUBPEL_MASK;
111     subpel_y = scaled_mv.row & SUBPEL_MASK;
112     pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
113            + (scaled_mv.col >> SUBPEL_BITS);
114
115 #if CONFIG_VP9_HIGHBITDEPTH
116     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
117       high_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
118                            subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
119                            xd->bd);
120     } else {
121       inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
122                       subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
123     }
124 #else
125     inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
126                     subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
127 #endif  // CONFIG_VP9_HIGHBITDEPTH
128   }
129 }
130
131 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
132                                               int mi_row, int mi_col,
133                                               int plane_from, int plane_to) {
134   int plane;
135   const int mi_x = mi_col * MI_SIZE;
136   const int mi_y = mi_row * MI_SIZE;
137   for (plane = plane_from; plane <= plane_to; ++plane) {
138     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
139                                                         &xd->plane[plane]);
140     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
141     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
142     const int bw = 4 * num_4x4_w;
143     const int bh = 4 * num_4x4_h;
144
145     if (xd->mi[0]->mbmi.sb_type < BLOCK_8X8) {
146       int i = 0, x, y;
147       assert(bsize == BLOCK_8X8);
148       for (y = 0; y < num_4x4_h; ++y)
149         for (x = 0; x < num_4x4_w; ++x)
150            build_inter_predictors(xd, plane, i++, bw, bh,
151                                   4 * x, 4 * y, 4, 4, mi_x, mi_y);
152     } else {
153       build_inter_predictors(xd, plane, 0, bw, bh,
154                              0, 0, bw, bh, mi_x, mi_y);
155     }
156   }
157 }
158
159 void vp10_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
160                                     BLOCK_SIZE bsize) {
161   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
162 }
163
164 void vp10_build_inter_predictors_sbp(MACROBLOCKD *xd, int mi_row, int mi_col,
165                                     BLOCK_SIZE bsize, int plane) {
166   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, plane, plane);
167 }
168
169 void vp10_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
170                                      BLOCK_SIZE bsize) {
171   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
172                                     MAX_MB_PLANE - 1);
173 }
174
175 void vp10_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
176                                    BLOCK_SIZE bsize) {
177   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
178                                     MAX_MB_PLANE - 1);
179 }
180
181 void vp10_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
182                           const YV12_BUFFER_CONFIG *src,
183                           int mi_row, int mi_col) {
184   uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
185       src->v_buffer};
186   const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
187       src->uv_stride};
188   int i;
189
190   for (i = 0; i < MAX_MB_PLANE; ++i) {
191     struct macroblockd_plane *const pd = &planes[i];
192     setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
193                      pd->subsampling_x, pd->subsampling_y);
194   }
195 }
196
197 void vp10_setup_pre_planes(MACROBLOCKD *xd, int idx,
198                           const YV12_BUFFER_CONFIG *src,
199                           int mi_row, int mi_col,
200                           const struct scale_factors *sf) {
201   if (src != NULL) {
202     int i;
203     uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
204         src->v_buffer};
205     const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
206         src->uv_stride};
207     for (i = 0; i < MAX_MB_PLANE; ++i) {
208       struct macroblockd_plane *const pd = &xd->plane[i];
209       setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
210                        sf, pd->subsampling_x, pd->subsampling_y);
211     }
212   }
213 }