Home | History | Annotate | Download | only in common
      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 
     24 void vp9_setup_interp_filters(MACROBLOCKD *xd,
     25                               INTERPOLATIONFILTERTYPE mcomp_filter_type,
     26                               VP9_COMMON *cm) {
     27   if (xd->mi_8x8 && xd->this_mi) {
     28     MB_MODE_INFO * mbmi = &xd->this_mi->mbmi;
     29 
     30     set_scale_factors(xd, mbmi->ref_frame[0] - 1, mbmi->ref_frame[1] - 1,
     31                       cm->active_ref_scale);
     32   } else {
     33     set_scale_factors(xd, -1, -1, cm->active_ref_scale);
     34   }
     35 
     36   switch (mcomp_filter_type) {
     37     case EIGHTTAP:
     38     case SWITCHABLE:
     39       xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8;
     40       break;
     41     case EIGHTTAP_SMOOTH:
     42       xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8lp;
     43       break;
     44     case EIGHTTAP_SHARP:
     45       xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8s;
     46       break;
     47     case BILINEAR:
     48       xd->subpix.filter_x = xd->subpix.filter_y = vp9_bilinear_filters;
     49       break;
     50   }
     51   assert(((intptr_t)xd->subpix.filter_x & 0xff) == 0);
     52 }
     53 
     54 void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
     55                                uint8_t *dst, int dst_stride,
     56                                const MV *src_mv,
     57                                const struct scale_factors *scale,
     58                                int w, int h, int ref,
     59                                const struct subpix_fn_table *subpix,
     60                                enum mv_precision precision) {
     61   const int is_q4 = precision == MV_PRECISION_Q4;
     62   const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row << 1,
     63                      is_q4 ? src_mv->col : src_mv->col << 1 };
     64   const MV32 mv = scale->scale_mv(&mv_q4, scale);
     65   const int subpel_x = mv.col & SUBPEL_MASK;
     66   const int subpel_y = mv.row & SUBPEL_MASK;
     67 
     68   src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
     69   scale->predict[subpel_x != 0][subpel_y != 0][ref](
     70       src, src_stride, dst, dst_stride,
     71       subpix->filter_x[subpel_x], scale->x_step_q4,
     72       subpix->filter_y[subpel_y], scale->y_step_q4,
     73       w, h);
     74 }
     75 
     76 static INLINE int round_mv_comp_q4(int value) {
     77   return (value < 0 ? value - 2 : value + 2) / 4;
     78 }
     79 
     80 static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
     81   MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
     82                               mi->bmi[1].as_mv[idx].as_mv.row +
     83                               mi->bmi[2].as_mv[idx].as_mv.row +
     84                               mi->bmi[3].as_mv[idx].as_mv.row),
     85              round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
     86                               mi->bmi[1].as_mv[idx].as_mv.col +
     87                               mi->bmi[2].as_mv[idx].as_mv.col +
     88                               mi->bmi[3].as_mv[idx].as_mv.col) };
     89   return res;
     90 }
     91 
     92 // TODO(jkoleszar): yet another mv clamping function :-(
     93 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
     94                              int bw, int bh, int ss_x, int ss_y) {
     95   // If the MV points so far into the UMV border that no visible pixels
     96   // are used for reconstruction, the subpel part of the MV can be
     97   // discarded and the MV limited to 16 pixels with equivalent results.
     98   const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
     99   const int spel_right = spel_left - SUBPEL_SHIFTS;
    100   const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
    101   const int spel_bottom = spel_top - SUBPEL_SHIFTS;
    102   MV clamped_mv = {
    103     src_mv->row << (1 - ss_y),
    104     src_mv->col << (1 - ss_x)
    105   };
    106   assert(ss_x <= 1);
    107   assert(ss_y <= 1);
    108 
    109   clamp_mv(&clamped_mv, (xd->mb_to_left_edge << (1 - ss_x)) - spel_left,
    110                         (xd->mb_to_right_edge << (1 - ss_x)) + spel_right,
    111                         (xd->mb_to_top_edge << (1 - ss_y)) - spel_top,
    112                         (xd->mb_to_bottom_edge << (1 - ss_y)) + spel_bottom);
    113 
    114   return clamped_mv;
    115 }
    116 
    117 struct build_inter_predictors_args {
    118   MACROBLOCKD *xd;
    119   int x, y;
    120 };
    121 
    122 static void build_inter_predictors(int plane, int block, BLOCK_SIZE bsize,
    123                                    int pred_w, int pred_h,
    124                                    void *argv) {
    125   const struct build_inter_predictors_args* const arg = argv;
    126   MACROBLOCKD *const xd = arg->xd;
    127   struct macroblockd_plane *const pd = &xd->plane[plane];
    128   const int bwl = b_width_log2(bsize) - pd->subsampling_x;
    129   const int bw = 4 << bwl;
    130   const int bh = plane_block_height(bsize, pd);
    131   const int x = 4 * (block & ((1 << bwl) - 1));
    132   const int y = 4 * (block >> bwl);
    133   const MODE_INFO *mi = xd->this_mi;
    134   const int use_second_ref = mi->mbmi.ref_frame[1] > 0;
    135   int ref;
    136 
    137   assert(x < bw);
    138   assert(y < bh);
    139   assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_w == bw);
    140   assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_h == bh);
    141 
    142   for (ref = 0; ref < 1 + use_second_ref; ++ref) {
    143     struct scale_factors *const scale = &xd->scale_factor[ref];
    144     struct buf_2d *const pre_buf = &pd->pre[ref];
    145     struct buf_2d *const dst_buf = &pd->dst;
    146 
    147     const uint8_t *const pre = pre_buf->buf + scaled_buffer_offset(x, y,
    148                                pre_buf->stride, scale);
    149 
    150     uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
    151 
    152     // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the
    153     // same MV (the average of the 4 luma MVs) but we could do something
    154     // smarter for non-4:2:0. Just punt for now, pending the changes to get
    155     // rid of SPLITMV mode entirely.
    156     const MV mv = mi->mbmi.sb_type < BLOCK_8X8
    157                ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv
    158                              : mi_mv_pred_q4(mi, ref))
    159                : mi->mbmi.mv[ref].as_mv;
    160 
    161     // TODO(jkoleszar): This clamping is done in the incorrect place for the
    162     // scaling case. It needs to be done on the scaled MV, not the pre-scaling
    163     // MV. Note however that it performs the subsampling aware scaling so
    164     // that the result is always q4.
    165     const MV res_mv = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
    166                                                 pd->subsampling_x,
    167                                                 pd->subsampling_y);
    168 
    169     scale->set_scaled_offsets(scale, arg->y + y, arg->x + x);
    170     vp9_build_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
    171                               &res_mv, scale,
    172                               4 << pred_w, 4 << pred_h, ref,
    173                               &xd->subpix, MV_PRECISION_Q4);
    174   }
    175 }
    176 
    177 // TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could
    178 // calculate the subsampled BLOCK_SIZE, but that type isn't defined for
    179 // sizes smaller than 16x16 yet.
    180 typedef void (*foreach_predicted_block_visitor)(int plane, int block,
    181                                                 BLOCK_SIZE bsize,
    182                                                 int pred_w, int pred_h,
    183                                                 void *arg);
    184 static INLINE void foreach_predicted_block_in_plane(
    185     const MACROBLOCKD* const xd, BLOCK_SIZE bsize, int plane,
    186     foreach_predicted_block_visitor visit, void *arg) {
    187   int i, x, y;
    188 
    189   // block sizes in number of 4x4 blocks log 2 ("*_b")
    190   // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
    191   // subsampled size of the block
    192   const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x;
    193   const int bhl = b_height_log2(bsize) - xd->plane[plane].subsampling_y;
    194 
    195   // size of the predictor to use.
    196   int pred_w, pred_h;
    197 
    198   if (xd->this_mi->mbmi.sb_type < BLOCK_8X8) {
    199     assert(bsize == BLOCK_8X8);
    200     pred_w = 0;
    201     pred_h = 0;
    202   } else {
    203     pred_w = bwl;
    204     pred_h = bhl;
    205   }
    206   assert(pred_w <= bwl);
    207   assert(pred_h <= bhl);
    208 
    209   // visit each subblock in raster order
    210   i = 0;
    211   for (y = 0; y < 1 << bhl; y += 1 << pred_h) {
    212     for (x = 0; x < 1 << bwl; x += 1 << pred_w) {
    213       visit(plane, i, bsize, pred_w, pred_h, arg);
    214       i += 1 << pred_w;
    215     }
    216     i += (1 << (bwl + pred_h)) - (1 << bwl);
    217   }
    218 }
    219 
    220 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
    221                                               int mi_row, int mi_col,
    222                                               int plane_from, int plane_to) {
    223   int plane;
    224   for (plane = plane_from; plane <= plane_to; ++plane) {
    225     struct build_inter_predictors_args args = {
    226       xd, mi_col * MI_SIZE, mi_row * MI_SIZE,
    227     };
    228     foreach_predicted_block_in_plane(xd, bsize, plane, build_inter_predictors,
    229                                      &args);
    230   }
    231 }
    232 
    233 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
    234                                     BLOCK_SIZE bsize) {
    235   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
    236 }
    237 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
    238                                      BLOCK_SIZE bsize) {
    239   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
    240                                     MAX_MB_PLANE - 1);
    241 }
    242 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
    243                                    BLOCK_SIZE bsize) {
    244   build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
    245                                     MAX_MB_PLANE - 1);
    246 }
    247 
    248 // TODO(dkovalev: find better place for this function)
    249 void vp9_setup_scale_factors(VP9_COMMON *cm, int i) {
    250   const int ref = cm->active_ref_idx[i];
    251   struct scale_factors *const sf = &cm->active_ref_scale[i];
    252   if (ref >= NUM_YV12_BUFFERS) {
    253     vp9_zero(*sf);
    254   } else {
    255     YV12_BUFFER_CONFIG *const fb = &cm->yv12_fb[ref];
    256     vp9_setup_scale_factors_for_frame(sf,
    257                                       fb->y_crop_width, fb->y_crop_height,
    258                                       cm->width, cm->height);
    259 
    260     if (vp9_is_scaled(sf))
    261       vp9_extend_frame_borders(fb, cm->subsampling_x, cm->subsampling_y);
    262   }
    263 }
    264 
    265