Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / 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 "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
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
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
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
100 static INLINE int round_mv_comp_q4(int value) {
101   return (value < 0 ? value - 2 : value + 2) / 4;
102 }
103
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
116 // TODO(jkoleszar): yet another mv clamping function :-(
117 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
118                              int bw, int bh, int ss_x, int ss_y) {
119   // If the MV points so far into the UMV border that no visible pixels
120   // are used for reconstruction, the subpel part of the MV can be
121   // discarded and the MV limited to 16 pixels with equivalent results.
122   const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
123   const int spel_right = spel_left - SUBPEL_SHIFTS;
124   const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
125   const int spel_bottom = spel_top - SUBPEL_SHIFTS;
126   MV clamped_mv = {
127     src_mv->row * (1 << (1 - ss_y)),
128     src_mv->col * (1 << (1 - ss_x))
129   };
130   assert(ss_x <= 1);
131   assert(ss_y <= 1);
132
133   clamp_mv(&clamped_mv,
134            xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
135            xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
136            xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
137            xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
138
139   return clamped_mv;
140 }
141
142 // TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could
143 // calculate the subsampled BLOCK_SIZE, but that type isn't defined for
144 // sizes smaller than 16x16 yet.
145 static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
146                                    int bw, int bh,
147                                    int x, int y, int w, int h,
148                                    int mi_x, int mi_y) {
149   struct macroblockd_plane *const pd = &xd->plane[plane];
150   const MODE_INFO *mi = xd->mi_8x8[0];
151   const int is_compound = has_second_ref(&mi->mbmi);
152   int ref;
153
154   for (ref = 0; ref < 1 + is_compound; ++ref) {
155     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
156     struct buf_2d *const pre_buf = &pd->pre[ref];
157     struct buf_2d *const dst_buf = &pd->dst;
158     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
159
160     // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the
161     // same MV (the average of the 4 luma MVs) but we could do something
162     // smarter for non-4:2:0. Just punt for now, pending the changes to get
163     // rid of SPLITMV mode entirely.
164     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
165                ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv
166                              : mi_mv_pred_q4(mi, ref))
167                : mi->mbmi.mv[ref].as_mv;
168
169     // TODO(jkoleszar): This clamping is done in the incorrect place for the
170     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
171     // MV. Note however that it performs the subsampling aware scaling so
172     // that the result is always q4.
173     // mv_precision precision is MV_PRECISION_Q4.
174     const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
175                                                pd->subsampling_x,
176                                                pd->subsampling_y);
177
178     uint8_t *pre;
179     MV32 scaled_mv;
180     int xs, ys, subpel_x, subpel_y;
181
182     if (vp9_is_scaled(sf)) {
183       pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
184       scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
185       xs = sf->x_step_q4;
186       ys = sf->y_step_q4;
187     } else {
188       pre = pre_buf->buf + (y * pre_buf->stride + x);
189       scaled_mv.row = mv_q4.row;
190       scaled_mv.col = mv_q4.col;
191       xs = ys = 16;
192     }
193     subpel_x = scaled_mv.col & SUBPEL_MASK;
194     subpel_y = scaled_mv.row & SUBPEL_MASK;
195     pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
196            + (scaled_mv.col >> SUBPEL_BITS);
197
198     inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
199                     subpel_x, subpel_y, sf, w, h, ref, xd->interp_kernel,
200                     xs, ys);
201   }
202 }
203
204 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
205                                               int mi_row, int mi_col,
206                                               int plane_from, int plane_to) {
207   int plane;
208   const int mi_x = mi_col * MI_SIZE;
209   const int mi_y = mi_row * MI_SIZE;
210   for (plane = plane_from; plane <= plane_to; ++plane) {
211     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
212                                                         &xd->plane[plane]);
213     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
214     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
215     const int bw = 4 * num_4x4_w;
216     const int bh = 4 * num_4x4_h;
217
218     if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) {
219       int i = 0, x, y;
220       assert(bsize == BLOCK_8X8);
221       for (y = 0; y < num_4x4_h; ++y)
222         for (x = 0; x < num_4x4_w; ++x)
223            build_inter_predictors(xd, plane, i++, bw, bh,
224                                   4 * x, 4 * y, 4, 4, mi_x, mi_y);
225     } else {
226       build_inter_predictors(xd, plane, 0, bw, bh,
227                              0, 0, bw, bh, mi_x, mi_y);
228     }
229   }
230 }
231
232 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
233                                     BLOCK_SIZE bsize) {
234   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
235 }
236 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
237                                      BLOCK_SIZE bsize) {
238   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
239                                     MAX_MB_PLANE - 1);
240 }
241 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
242                                    BLOCK_SIZE bsize) {
243   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
244                                     MAX_MB_PLANE - 1);
245 }
246
247 // TODO(jingning): This function serves as a placeholder for decoder prediction
248 // using on demand border extension. It should be moved to /decoder/ directory.
249 static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
250                                        int bw, int bh,
251                                        int x, int y, int w, int h,
252                                        int mi_x, int mi_y) {
253   struct macroblockd_plane *const pd = &xd->plane[plane];
254   const MODE_INFO *mi = xd->mi_8x8[0];
255   const int is_compound = has_second_ref(&mi->mbmi);
256   int ref;
257
258   for (ref = 0; ref < 1 + is_compound; ++ref) {
259     const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
260     struct buf_2d *const pre_buf = &pd->pre[ref];
261     struct buf_2d *const dst_buf = &pd->dst;
262     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
263
264     // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the
265     // same MV (the average of the 4 luma MVs) but we could do something
266     // smarter for non-4:2:0. Just punt for now, pending the changes to get
267     // rid of SPLITMV mode entirely.
268     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
269                ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv
270                              : mi_mv_pred_q4(mi, ref))
271                : mi->mbmi.mv[ref].as_mv;
272     MV32 scaled_mv;
273     int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
274         subpel_x, subpel_y;
275     uint8_t *ref_frame, *buf_ptr;
276     const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
277     const MV mv_q4 = {
278       mv.row * (1 << (1 - pd->subsampling_y)),
279       mv.col * (1 << (1 - pd->subsampling_x))
280     };
281
282     // Get reference frame pointer, width and height.
283     if (plane == 0) {
284       frame_width = ref_buf->y_crop_width;
285       frame_height = ref_buf->y_crop_height;
286       ref_frame = ref_buf->y_buffer;
287     } else {
288       frame_width = ref_buf->uv_crop_width;
289       frame_height = ref_buf->uv_crop_height;
290       ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
291     }
292
293     // Get block position in current frame.
294     x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
295     y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
296
297     // Precision of x0_16 and y0_16 is 1/16th pixel.
298     x0_16 = x0 << SUBPEL_BITS;
299     y0_16 = y0 << SUBPEL_BITS;
300
301     if (vp9_is_scaled(sf)) {
302       scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
303       xs = sf->x_step_q4;
304       ys = sf->y_step_q4;
305       // Map the top left corner of the block into the reference frame.
306       x0 = sf->scale_value_x(x0, sf);
307       y0 = sf->scale_value_y(y0, sf);
308       x0_16 = sf->scale_value_x(x0_16, sf);
309       y0_16 = sf->scale_value_y(y0_16, sf);
310     } else {
311       scaled_mv.row = mv_q4.row;
312       scaled_mv.col = mv_q4.col;
313       xs = ys = 16;
314     }
315     subpel_x = scaled_mv.col & SUBPEL_MASK;
316     subpel_y = scaled_mv.row & SUBPEL_MASK;
317
318     // Calculate the top left corner of the best matching block in the reference frame.
319     x0 += scaled_mv.col >> SUBPEL_BITS;
320     y0 += scaled_mv.row >> SUBPEL_BITS;
321     x0_16 += scaled_mv.col;
322     y0_16 += scaled_mv.row;
323
324     // Get reference block pointer.
325     buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
326     buf_stride = pre_buf->stride;
327
328     // Do border extension if there is motion or the
329     // width/height is not a multiple of 8 pixels.
330     if (scaled_mv.col || scaled_mv.row ||
331         (frame_width & 0x7) || (frame_height & 0x7)) {
332       // Get reference block bottom right coordinate.
333       int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
334       int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
335       int x_pad = 0, y_pad = 0;
336
337       if (subpel_x || (sf->x_step_q4 & SUBPEL_MASK)) {
338         x0 -= VP9_INTERP_EXTEND - 1;
339         x1 += VP9_INTERP_EXTEND;
340         x_pad = 1;
341       }
342
343       if (subpel_y || (sf->y_step_q4 & SUBPEL_MASK)) {
344         y0 -= VP9_INTERP_EXTEND - 1;
345         y1 += VP9_INTERP_EXTEND;
346         y_pad = 1;
347       }
348
349       // Skip border extension if block is inside the frame.
350       if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width ||
351           y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
352         uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
353         // Extend the border.
354         build_mc_border(buf_ptr1, pre_buf->stride, xd->mc_buf, x1 - x0,
355                         x0, y0, x1 - x0, y1 - y0, frame_width, frame_height);
356         buf_stride = x1 - x0;
357         buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
358       }
359     }
360
361     inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
362                     subpel_y, sf, w, h, ref, xd->interp_kernel, xs, ys);
363   }
364 }
365
366 void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
367                                        BLOCK_SIZE bsize) {
368   int plane;
369   const int mi_x = mi_col * MI_SIZE;
370   const int mi_y = mi_row * MI_SIZE;
371   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
372     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
373                                                         &xd->plane[plane]);
374     const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
375     const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
376     const int bw = 4 * num_4x4_w;
377     const int bh = 4 * num_4x4_h;
378
379     if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) {
380       int i = 0, x, y;
381       assert(bsize == BLOCK_8X8);
382       for (y = 0; y < num_4x4_h; ++y)
383         for (x = 0; x < num_4x4_w; ++x)
384           dec_build_inter_predictors(xd, plane, i++, bw, bh,
385                                      4 * x, 4 * y, 4, 4, mi_x, mi_y);
386     } else {
387       dec_build_inter_predictors(xd, plane, 0, bw, bh,
388                                  0, 0, bw, bh, mi_x, mi_y);
389     }
390   }
391 }