xref: /aosp_15_r20/external/libvpx/vp9/common/vp9_reconinter.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 "vp9/common/vp9_blockd.h"
17 #include "vp9/common/vp9_reconinter.h"
18 #include "vp9/common/vp9_reconintra.h"
19 
20 #include "vpx/vpx_integer.h"
21 #include "vpx_scale/yv12config.h"
22 
23 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_build_inter_predictor(const uint16_t * src,int src_stride,uint16_t * dst,int dst_stride,const MV * src_mv,const struct scale_factors * sf,int w,int h,int ref,const InterpKernel * kernel,enum mv_precision precision,int x,int y,int bd)24 void vp9_highbd_build_inter_predictor(
25     const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
26     const MV *src_mv, const struct scale_factors *sf, int w, int h, int ref,
27     const InterpKernel *kernel, enum mv_precision precision, int x, int y,
28     int bd) {
29   const int is_q4 = precision == MV_PRECISION_Q4;
30   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
31                      is_q4 ? src_mv->col : src_mv->col * 2 };
32   MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
33   const int subpel_x = mv.col & SUBPEL_MASK;
34   const int subpel_y = mv.row & SUBPEL_MASK;
35 
36   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
37 
38   highbd_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
39                          sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4,
40                          bd);
41 }
42 #endif  // CONFIG_VP9_HIGHBITDEPTH
43 
vp9_build_inter_predictor(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,const MV * src_mv,const struct scale_factors * sf,int w,int h,int ref,const InterpKernel * kernel,enum mv_precision precision,int x,int y)44 void vp9_build_inter_predictor(const uint8_t *src, int src_stride, uint8_t *dst,
45                                int dst_stride, const MV *src_mv,
46                                const struct scale_factors *sf, int w, int h,
47                                int ref, const InterpKernel *kernel,
48                                enum mv_precision precision, int x, int y) {
49   const int is_q4 = precision == MV_PRECISION_Q4;
50   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
51                      is_q4 ? src_mv->col : src_mv->col * 2 };
52   MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
53   const int subpel_x = mv.col & SUBPEL_MASK;
54   const int subpel_y = mv.row & SUBPEL_MASK;
55 
56   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
57 
58   inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y, sf, w,
59                   h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
60 }
61 
round_mv_comp_q4(int value)62 static INLINE int round_mv_comp_q4(int value) {
63   return (value < 0 ? value - 2 : value + 2) / 4;
64 }
65 
mi_mv_pred_q4(const MODE_INFO * mi,int idx)66 static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
67   MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
68                               mi->bmi[1].as_mv[idx].as_mv.row +
69                               mi->bmi[2].as_mv[idx].as_mv.row +
70                               mi->bmi[3].as_mv[idx].as_mv.row),
71              round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
72                               mi->bmi[1].as_mv[idx].as_mv.col +
73                               mi->bmi[2].as_mv[idx].as_mv.col +
74                               mi->bmi[3].as_mv[idx].as_mv.col) };
75   return res;
76 }
77 
round_mv_comp_q2(int value)78 static INLINE int round_mv_comp_q2(int value) {
79   return (value < 0 ? value - 1 : value + 1) / 2;
80 }
81 
mi_mv_pred_q2(const MODE_INFO * mi,int idx,int block0,int block1)82 static MV mi_mv_pred_q2(const MODE_INFO *mi, int idx, int block0, int block1) {
83   MV res = { round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
84                               mi->bmi[block1].as_mv[idx].as_mv.row),
85              round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
86                               mi->bmi[block1].as_mv[idx].as_mv.col) };
87   return res;
88 }
89 
90 // TODO(jkoleszar): yet another mv clamping function :-(
clamp_mv_to_umv_border_sb(const MACROBLOCKD * xd,const MV * src_mv,int bw,int bh,int ss_x,int ss_y)91 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv, int bw,
92                              int bh, int ss_x, int ss_y) {
93   // If the MV points so far into the UMV border that no visible pixels
94   // are used for reconstruction, the subpel part of the MV can be
95   // discarded and the MV limited to 16 pixels with equivalent results.
96   const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
97   const int spel_right = spel_left - SUBPEL_SHIFTS;
98   const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
99   const int spel_bottom = spel_top - SUBPEL_SHIFTS;
100   MV clamped_mv = { (short)(src_mv->row * (1 << (1 - ss_y))),
101                     (short)(src_mv->col * (1 << (1 - ss_x))) };
102   assert(ss_x <= 1);
103   assert(ss_y <= 1);
104 
105   clamp_mv(&clamped_mv, xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
106            xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
107            xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
108            xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
109 
110   return clamped_mv;
111 }
112 
average_split_mvs(const struct macroblockd_plane * pd,const MODE_INFO * mi,int ref,int block)113 MV average_split_mvs(const struct macroblockd_plane *pd, const MODE_INFO *mi,
114                      int ref, int block) {
115   const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
116   MV res = { 0, 0 };
117   switch (ss_idx) {
118     case 0: res = mi->bmi[block].as_mv[ref].as_mv; break;
119     case 1: res = mi_mv_pred_q2(mi, ref, block, block + 2); break;
120     case 2: res = mi_mv_pred_q2(mi, ref, block, block + 1); break;
121     case 3: res = mi_mv_pred_q4(mi, ref); break;
122     default: assert(ss_idx <= 3 && ss_idx >= 0);
123   }
124   return res;
125 }
126 
build_inter_predictors(MACROBLOCKD * xd,int plane,int block,int bw,int bh,int x,int y,int w,int h,int mi_x,int mi_y)127 static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
128                                    int bw, int bh, int x, int y, int w, int h,
129                                    int mi_x, int mi_y) {
130   struct macroblockd_plane *const pd = &xd->plane[plane];
131   const MODE_INFO *mi = xd->mi[0];
132   const int is_compound = has_second_ref(mi);
133   const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
134   int ref;
135 
136   for (ref = 0; ref < 1 + is_compound; ++ref) {
137     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
138     struct buf_2d *const pre_buf = &pd->pre[ref];
139     struct buf_2d *const dst_buf = &pd->dst;
140     uint8_t *const dst = dst_buf->buf + (int64_t)dst_buf->stride * y + x;
141     const MV mv = mi->sb_type < BLOCK_8X8
142                       ? average_split_mvs(pd, mi, ref, block)
143                       : mi->mv[ref].as_mv;
144 
145     // TODO(jkoleszar): This clamping is done in the incorrect place for the
146     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
147     // MV. Note however that it performs the subsampling aware scaling so
148     // that the result is always q4.
149     // mv_precision precision is MV_PRECISION_Q4.
150     const MV mv_q4 = clamp_mv_to_umv_border_sb(
151         xd, &mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
152 
153     uint8_t *pre;
154     MV32 scaled_mv;
155     int xs, ys, subpel_x, subpel_y;
156     const int is_scaled = vp9_is_scaled(sf);
157 
158     if (is_scaled) {
159       // Co-ordinate of containing block to pixel precision.
160       const int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
161       const int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
162       const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
163       uint8_t *buf_array[] = { ref_buf->y_buffer, ref_buf->u_buffer,
164                                ref_buf->v_buffer };
165       const int stride_array[] = { ref_buf->y_stride, ref_buf->uv_stride,
166                                    ref_buf->uv_stride };
167 #if 0  // CONFIG_BETTER_HW_COMPATIBILITY
168       assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
169              xd->mi[0]->sb_type != BLOCK_8X4);
170       assert(mv_q4.row == mv.row * (1 << (1 - pd->subsampling_y)) &&
171              mv_q4.col == mv.col * (1 << (1 - pd->subsampling_x)));
172 #endif
173       pre_buf->buf = buf_array[plane];
174       pre_buf->stride = stride_array[plane];
175 
176       pre_buf->buf +=
177           scaled_buffer_offset(x_start + x, y_start + y, pre_buf->stride, sf);
178       pre = pre_buf->buf;
179       scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
180       xs = sf->x_step_q4;
181       ys = sf->y_step_q4;
182     } else {
183       pre = pre_buf->buf + ((int64_t)y * pre_buf->stride + x);
184       scaled_mv.row = mv_q4.row;
185       scaled_mv.col = mv_q4.col;
186       xs = ys = 16;
187     }
188     subpel_x = scaled_mv.col & SUBPEL_MASK;
189     subpel_y = scaled_mv.row & SUBPEL_MASK;
190     pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride +
191            (scaled_mv.col >> SUBPEL_BITS);
192 
193 #if CONFIG_VP9_HIGHBITDEPTH
194     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
195       highbd_inter_predictor(CONVERT_TO_SHORTPTR(pre), pre_buf->stride,
196                              CONVERT_TO_SHORTPTR(dst), dst_buf->stride,
197                              subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
198                              xd->bd);
199     } else {
200       inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, subpel_x,
201                       subpel_y, sf, w, h, ref, kernel, xs, ys);
202     }
203 #else
204     inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, subpel_x,
205                     subpel_y, sf, w, h, ref, kernel, xs, ys);
206 #endif  // CONFIG_VP9_HIGHBITDEPTH
207   }
208 }
209 
build_inter_predictors_for_planes(MACROBLOCKD * xd,BLOCK_SIZE bsize,int mi_row,int mi_col,int plane_from,int plane_to)210 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
211                                               int mi_row, int mi_col,
212                                               int plane_from, int plane_to) {
213   int plane;
214   const int mi_x = mi_col * MI_SIZE;
215   const int mi_y = mi_row * MI_SIZE;
216   for (plane = plane_from; plane <= plane_to; ++plane) {
217     const BLOCK_SIZE plane_bsize =
218         get_plane_block_size(bsize, &xd->plane[plane]);
219     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
220     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
221     const int bw = 4 * num_4x4_w;
222     const int bh = 4 * num_4x4_h;
223 
224     if (xd->mi[0]->sb_type < BLOCK_8X8) {
225       int i = 0, x, y;
226       assert(bsize == BLOCK_8X8);
227       for (y = 0; y < num_4x4_h; ++y)
228         for (x = 0; x < num_4x4_w; ++x)
229           build_inter_predictors(xd, plane, i++, bw, bh, 4 * x, 4 * y, 4, 4,
230                                  mi_x, mi_y);
231     } else {
232       build_inter_predictors(xd, plane, 0, bw, bh, 0, 0, bw, bh, mi_x, mi_y);
233     }
234   }
235 }
236 
vp9_build_inter_predictors_sby(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)237 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
238                                     BLOCK_SIZE bsize) {
239   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
240 }
241 
vp9_build_inter_predictors_sbp(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize,int plane)242 void vp9_build_inter_predictors_sbp(MACROBLOCKD *xd, int mi_row, int mi_col,
243                                     BLOCK_SIZE bsize, int plane) {
244   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, plane, plane);
245 }
246 
vp9_build_inter_predictors_sbuv(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)247 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
248                                      BLOCK_SIZE bsize) {
249   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
250                                     MAX_MB_PLANE - 1);
251 }
252 
vp9_build_inter_predictors_sb(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)253 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
254                                    BLOCK_SIZE bsize) {
255   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
256                                     MAX_MB_PLANE - 1);
257 }
258 
vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col)259 void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
260                           const YV12_BUFFER_CONFIG *src, int mi_row,
261                           int mi_col) {
262   uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
263                                            src->v_buffer };
264   const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
265                                       src->uv_stride };
266   int i;
267 
268   for (i = 0; i < MAX_MB_PLANE; ++i) {
269     struct macroblockd_plane *const pd = &planes[i];
270     setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
271                      pd->subsampling_x, pd->subsampling_y);
272   }
273 }
274 
vp9_setup_pre_planes(MACROBLOCKD * xd,int idx,const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col,const struct scale_factors * sf)275 void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
276                           const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
277                           const struct scale_factors *sf) {
278   if (src != NULL) {
279     int i;
280     uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
281                                              src->v_buffer };
282     const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
283                                         src->uv_stride };
284     for (i = 0; i < MAX_MB_PLANE; ++i) {
285       struct macroblockd_plane *const pd = &xd->plane[i];
286       setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
287                        sf, pd->subsampling_x, pd->subsampling_y);
288     }
289   }
290 }
291