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 "vp9/common/vp9_blockd.h"
19 #include "vp9/common/vp9_filter.h"
20 #include "vp9/common/vp9_reconinter.h"
21 #include "vp9/common/vp9_reconintra.h"
22 
build_mc_border(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int x,int y,int b_w,int b_h,int w,int h)23 static void build_mc_border(const uint8_t *src, int src_stride,
24                             uint8_t *dst, int dst_stride,
25                             int x, int y, int b_w, int b_h, int w, int h) {
26   // Get a pointer to the start of the real data for this row.
27   const uint8_t *ref_row = src - x - y * src_stride;
28 
29   if (y >= h)
30     ref_row += (h - 1) * src_stride;
31   else if (y > 0)
32     ref_row += y * src_stride;
33 
34   do {
35     int right = 0, copy;
36     int left = x < 0 ? -x : 0;
37 
38     if (left > b_w)
39       left = b_w;
40 
41     if (x + b_w > w)
42       right = x + b_w - w;
43 
44     if (right > b_w)
45       right = b_w;
46 
47     copy = b_w - left - right;
48 
49     if (left)
50       memset(dst, ref_row[0], left);
51 
52     if (copy)
53       memcpy(dst + left, ref_row + x + left, copy);
54 
55     if (right)
56       memset(dst + left + copy, ref_row[w - 1], right);
57 
58     dst += dst_stride;
59     ++y;
60 
61     if (y > 0 && y < h)
62       ref_row += src_stride;
63   } while (--b_h);
64 }
65 
inter_predictor(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,const int subpel_x,const int subpel_y,const struct scale_factors * sf,int w,int h,int ref,const InterpKernel * kernel,int xs,int ys)66 static void inter_predictor(const uint8_t *src, int src_stride,
67                             uint8_t *dst, int dst_stride,
68                             const int subpel_x,
69                             const int subpel_y,
70                             const struct scale_factors *sf,
71                             int w, int h, int ref,
72                             const InterpKernel *kernel,
73                             int xs, int ys) {
74   sf->predict[subpel_x != 0][subpel_y != 0][ref](
75       src, src_stride, dst, dst_stride,
76       kernel[subpel_x], xs, kernel[subpel_y], ys, w, h);
77 }
78 
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)79 void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
80                                uint8_t *dst, int dst_stride,
81                                const MV *src_mv,
82                                const struct scale_factors *sf,
83                                int w, int h, int ref,
84                                const InterpKernel *kernel,
85                                enum mv_precision precision,
86                                int x, int y) {
87   const int is_q4 = precision == MV_PRECISION_Q4;
88   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
89                      is_q4 ? src_mv->col : src_mv->col * 2 };
90   MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
91   const int subpel_x = mv.col & SUBPEL_MASK;
92   const int subpel_y = mv.row & SUBPEL_MASK;
93 
94   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
95 
96   inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
97                   sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
98 }
99 
round_mv_comp_q4(int value)100 static INLINE int round_mv_comp_q4(int value) {
101   return (value < 0 ? value - 2 : value + 2) / 4;
102 }
103 
mi_mv_pred_q4(const MODE_INFO * mi,int idx)104 static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
105   MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
106                               mi->bmi[1].as_mv[idx].as_mv.row +
107                               mi->bmi[2].as_mv[idx].as_mv.row +
108                               mi->bmi[3].as_mv[idx].as_mv.row),
109              round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
110                               mi->bmi[1].as_mv[idx].as_mv.col +
111                               mi->bmi[2].as_mv[idx].as_mv.col +
112                               mi->bmi[3].as_mv[idx].as_mv.col) };
113   return res;
114 }
115 
round_mv_comp_q2(int value)116 static INLINE int round_mv_comp_q2(int value) {
117   return (value < 0 ? value - 1 : value + 1) / 2;
118 }
119 
mi_mv_pred_q2(const MODE_INFO * mi,int idx,int block0,int block1)120 static MV mi_mv_pred_q2(const MODE_INFO *mi, int idx, int block0, int block1) {
121   MV res = { round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
122                               mi->bmi[block1].as_mv[idx].as_mv.row),
123              round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
124                               mi->bmi[block1].as_mv[idx].as_mv.col) };
125   return res;
126 }
127 
128 // 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)129 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
130                              int bw, int bh, int ss_x, int ss_y) {
131   // If the MV points so far into the UMV border that no visible pixels
132   // are used for reconstruction, the subpel part of the MV can be
133   // discarded and the MV limited to 16 pixels with equivalent results.
134   const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
135   const int spel_right = spel_left - SUBPEL_SHIFTS;
136   const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
137   const int spel_bottom = spel_top - SUBPEL_SHIFTS;
138   MV clamped_mv = {
139     src_mv->row * (1 << (1 - ss_y)),
140     src_mv->col * (1 << (1 - ss_x))
141   };
142   assert(ss_x <= 1);
143   assert(ss_y <= 1);
144 
145   clamp_mv(&clamped_mv,
146            xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
147            xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
148            xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
149            xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
150 
151   return clamped_mv;
152 }
153 
average_split_mvs(const struct macroblockd_plane * pd,const MODE_INFO * mi,int ref,int block)154 static MV average_split_mvs(const struct macroblockd_plane *pd,
155                             const MODE_INFO *mi, int ref, int block) {
156   const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
157   MV res = {0, 0};
158   switch (ss_idx) {
159     case 0:
160       res = mi->bmi[block].as_mv[ref].as_mv;
161       break;
162     case 1:
163       res = mi_mv_pred_q2(mi, ref, block, block + 2);
164       break;
165     case 2:
166       res = mi_mv_pred_q2(mi, ref, block, block + 1);
167       break;
168     case 3:
169       res = mi_mv_pred_q4(mi, ref);
170       break;
171     default:
172       assert(ss_idx <= 3 || ss_idx >= 0);
173   }
174   return res;
175 }
176 
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)177 static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
178                                    int bw, int bh,
179                                    int x, int y, int w, int h,
180                                    int mi_x, int mi_y) {
181   struct macroblockd_plane *const pd = &xd->plane[plane];
182   const MODE_INFO *mi = xd->mi[0];
183   const int is_compound = has_second_ref(&mi->mbmi);
184   const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
185   int ref;
186 
187   for (ref = 0; ref < 1 + is_compound; ++ref) {
188     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
189     struct buf_2d *const pre_buf = &pd->pre[ref];
190     struct buf_2d *const dst_buf = &pd->dst;
191     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
192     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
193                ? average_split_mvs(pd, mi, ref, block)
194                : mi->mbmi.mv[ref].as_mv;
195 
196     // TODO(jkoleszar): This clamping is done in the incorrect place for the
197     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
198     // MV. Note however that it performs the subsampling aware scaling so
199     // that the result is always q4.
200     // mv_precision precision is MV_PRECISION_Q4.
201     const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
202                                                pd->subsampling_x,
203                                                pd->subsampling_y);
204 
205     uint8_t *pre;
206     MV32 scaled_mv;
207     int xs, ys, subpel_x, subpel_y;
208 
209     if (vp9_is_scaled(sf)) {
210       pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
211       scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
212       xs = sf->x_step_q4;
213       ys = sf->y_step_q4;
214     } else {
215       pre = pre_buf->buf + (y * pre_buf->stride + x);
216       scaled_mv.row = mv_q4.row;
217       scaled_mv.col = mv_q4.col;
218       xs = ys = 16;
219     }
220     subpel_x = scaled_mv.col & SUBPEL_MASK;
221     subpel_y = scaled_mv.row & SUBPEL_MASK;
222     pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
223            + (scaled_mv.col >> SUBPEL_BITS);
224 
225     inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
226                     subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
227   }
228 }
229 
build_inter_predictors_for_planes(MACROBLOCKD * xd,BLOCK_SIZE bsize,int mi_row,int mi_col,int plane_from,int plane_to)230 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
231                                               int mi_row, int mi_col,
232                                               int plane_from, int plane_to) {
233   int plane;
234   const int mi_x = mi_col * MI_SIZE;
235   const int mi_y = mi_row * MI_SIZE;
236   for (plane = plane_from; plane <= plane_to; ++plane) {
237     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
238                                                         &xd->plane[plane]);
239     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
240     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
241     const int bw = 4 * num_4x4_w;
242     const int bh = 4 * num_4x4_h;
243 
244     if (xd->mi[0]->mbmi.sb_type < BLOCK_8X8) {
245       int i = 0, x, y;
246       assert(bsize == BLOCK_8X8);
247       for (y = 0; y < num_4x4_h; ++y)
248         for (x = 0; x < num_4x4_w; ++x)
249            build_inter_predictors(xd, plane, i++, bw, bh,
250                                   4 * x, 4 * y, 4, 4, mi_x, mi_y);
251     } else {
252       build_inter_predictors(xd, plane, 0, bw, bh,
253                              0, 0, bw, bh, mi_x, mi_y);
254     }
255   }
256 }
257 
vp9_build_inter_predictors_sby(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)258 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
259                                     BLOCK_SIZE bsize) {
260   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
261 }
vp9_build_inter_predictors_sbuv(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)262 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
263                                      BLOCK_SIZE bsize) {
264   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
265                                     MAX_MB_PLANE - 1);
266 }
vp9_build_inter_predictors_sb(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)267 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
268                                    BLOCK_SIZE bsize) {
269   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
270                                     MAX_MB_PLANE - 1);
271 }
272 
273 // TODO(jingning): This function serves as a placeholder for decoder prediction
274 // using on demand border extension. It should be moved to /decoder/ directory.
dec_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)275 static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
276                                        int bw, int bh,
277                                        int x, int y, int w, int h,
278                                        int mi_x, int mi_y) {
279   struct macroblockd_plane *const pd = &xd->plane[plane];
280   const MODE_INFO *mi = xd->mi[0];
281   const int is_compound = has_second_ref(&mi->mbmi);
282   const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
283   int ref;
284 
285   for (ref = 0; ref < 1 + is_compound; ++ref) {
286     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
287     struct buf_2d *const pre_buf = &pd->pre[ref];
288     struct buf_2d *const dst_buf = &pd->dst;
289     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
290     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
291                ? average_split_mvs(pd, mi, ref, block)
292                : mi->mbmi.mv[ref].as_mv;
293 
294 
295     // TODO(jkoleszar): This clamping is done in the incorrect place for the
296     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
297     // MV. Note however that it performs the subsampling aware scaling so
298     // that the result is always q4.
299     // mv_precision precision is MV_PRECISION_Q4.
300     const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
301                                                pd->subsampling_x,
302                                                pd->subsampling_y);
303 
304     MV32 scaled_mv;
305     int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
306         subpel_x, subpel_y;
307     uint8_t *ref_frame, *buf_ptr;
308     const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
309 
310     // Get reference frame pointer, width and height.
311     if (plane == 0) {
312       frame_width = ref_buf->y_crop_width;
313       frame_height = ref_buf->y_crop_height;
314       ref_frame = ref_buf->y_buffer;
315     } else {
316       frame_width = ref_buf->uv_crop_width;
317       frame_height = ref_buf->uv_crop_height;
318       ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
319     }
320 
321     if (vp9_is_scaled(sf)) {
322       // Co-ordinate of containing block to pixel precision.
323       int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
324       int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
325 
326       // Co-ordinate of the block to 1/16th pixel precision.
327       x0_16 = (x_start + x) << SUBPEL_BITS;
328       y0_16 = (y_start + y) << SUBPEL_BITS;
329 
330       // Co-ordinate of current block in reference frame
331       // to 1/16th pixel precision.
332       x0_16 = sf->scale_value_x(x0_16, sf);
333       y0_16 = sf->scale_value_y(y0_16, sf);
334 
335       // Map the top left corner of the block into the reference frame.
336       x0 = sf->scale_value_x(x_start + x, sf);
337       y0 = sf->scale_value_y(y_start + y, sf);
338 
339       // Scale the MV and incorporate the sub-pixel offset of the block
340       // in the reference frame.
341       scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
342       xs = sf->x_step_q4;
343       ys = sf->y_step_q4;
344     } else {
345       // Co-ordinate of containing block to pixel precision.
346       x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
347       y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
348 
349       // Co-ordinate of the block to 1/16th pixel precision.
350       x0_16 = x0 << SUBPEL_BITS;
351       y0_16 = y0 << SUBPEL_BITS;
352 
353       scaled_mv.row = mv_q4.row;
354       scaled_mv.col = mv_q4.col;
355       xs = ys = 16;
356     }
357     subpel_x = scaled_mv.col & SUBPEL_MASK;
358     subpel_y = scaled_mv.row & SUBPEL_MASK;
359 
360     // Calculate the top left corner of the best matching block in the reference frame.
361     x0 += scaled_mv.col >> SUBPEL_BITS;
362     y0 += scaled_mv.row >> SUBPEL_BITS;
363     x0_16 += scaled_mv.col;
364     y0_16 += scaled_mv.row;
365 
366     // Get reference block pointer.
367     buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
368     buf_stride = pre_buf->stride;
369 
370     // Do border extension if there is motion or the
371     // width/height is not a multiple of 8 pixels.
372     if (scaled_mv.col || scaled_mv.row ||
373         (frame_width & 0x7) || (frame_height & 0x7)) {
374       // Get reference block bottom right coordinate.
375       int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
376       int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
377       int x_pad = 0, y_pad = 0;
378 
379       if (subpel_x || (sf->x_step_q4 & SUBPEL_MASK)) {
380         x0 -= VP9_INTERP_EXTEND - 1;
381         x1 += VP9_INTERP_EXTEND;
382         x_pad = 1;
383       }
384 
385       if (subpel_y || (sf->y_step_q4 & SUBPEL_MASK)) {
386         y0 -= VP9_INTERP_EXTEND - 1;
387         y1 += VP9_INTERP_EXTEND;
388         y_pad = 1;
389       }
390 
391       // Skip border extension if block is inside the frame.
392       if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
393           y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
394         uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
395         // Extend the border.
396         build_mc_border(buf_ptr1, pre_buf->stride, xd->mc_buf, x1 - x0 + 1,
397                         x0, y0, x1 - x0 + 1, y1 - y0 + 1, frame_width,
398                         frame_height);
399         buf_stride = x1 - x0 + 1;
400         buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
401       }
402     }
403 
404     inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
405                     subpel_y, sf, w, h, ref, kernel, xs, ys);
406   }
407 }
408 
vp9_dec_build_inter_predictors_sb(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)409 void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
410                                        BLOCK_SIZE bsize) {
411   int plane;
412   const int mi_x = mi_col * MI_SIZE;
413   const int mi_y = mi_row * MI_SIZE;
414   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
415     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
416                                                         &xd->plane[plane]);
417     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
418     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
419     const int bw = 4 * num_4x4_w;
420     const int bh = 4 * num_4x4_h;
421 
422     if (xd->mi[0]->mbmi.sb_type < BLOCK_8X8) {
423       int i = 0, x, y;
424       assert(bsize == BLOCK_8X8);
425       for (y = 0; y < num_4x4_h; ++y)
426         for (x = 0; x < num_4x4_w; ++x)
427           dec_build_inter_predictors(xd, plane, i++, bw, bh,
428                                      4 * x, 4 * y, 4, 4, mi_x, mi_y);
429     } else {
430       dec_build_inter_predictors(xd, plane, 0, bw, bh,
431                                  0, 0, bw, bh, mi_x, mi_y);
432     }
433   }
434 }
435 
vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col)436 void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
437                           const YV12_BUFFER_CONFIG *src,
438                           int mi_row, int mi_col) {
439   uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
440                                src->alpha_buffer};
441   const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
442                           src->alpha_stride};
443   int i;
444 
445   for (i = 0; i < MAX_MB_PLANE; ++i) {
446     struct macroblockd_plane *const pd = &planes[i];
447     setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
448                      pd->subsampling_x, pd->subsampling_y);
449   }
450 }
451 
vp9_setup_pre_planes(MACROBLOCKD * xd,int idx,const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col,const struct scale_factors * sf)452 void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
453                           const YV12_BUFFER_CONFIG *src,
454                           int mi_row, int mi_col,
455                           const struct scale_factors *sf) {
456   if (src != NULL) {
457     int i;
458     uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
459                                  src->alpha_buffer};
460     const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
461                             src->alpha_stride};
462 
463     for (i = 0; i < MAX_MB_PLANE; ++i) {
464       struct macroblockd_plane *const pd = &xd->plane[i];
465       setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
466                        sf, pd->subsampling_x, pd->subsampling_y);
467     }
468   }
469 }
470