michael@0: /* michael@0: * Copyright (c) 2013 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include "./vp9_rtcd.h" michael@0: #include "vp9/common/vp9_filter.h" michael@0: #include "vp9/common/vp9_scale.h" michael@0: michael@0: static INLINE int scaled_x(int val, const struct scale_factors_common *sfc) { michael@0: return val * sfc->x_scale_fp >> REF_SCALE_SHIFT; michael@0: } michael@0: michael@0: static INLINE int scaled_y(int val, const struct scale_factors_common *sfc) { michael@0: return val * sfc->y_scale_fp >> REF_SCALE_SHIFT; michael@0: } michael@0: michael@0: static int unscaled_value(int val, const struct scale_factors_common *sfc) { michael@0: (void) sfc; michael@0: return val; michael@0: } michael@0: michael@0: static MV32 scaled_mv(const MV *mv, const struct scale_factors *scale) { michael@0: const MV32 res = { michael@0: scaled_y(mv->row, scale->sfc) + scale->y_offset_q4, michael@0: scaled_x(mv->col, scale->sfc) + scale->x_offset_q4 michael@0: }; michael@0: return res; michael@0: } michael@0: michael@0: static MV32 unscaled_mv(const MV *mv, const struct scale_factors *scale) { michael@0: const MV32 res = { michael@0: mv->row, michael@0: mv->col michael@0: }; michael@0: return res; michael@0: } michael@0: michael@0: static void set_offsets_with_scaling(struct scale_factors *scale, michael@0: int row, int col) { michael@0: scale->x_offset_q4 = scaled_x(col << SUBPEL_BITS, scale->sfc) & SUBPEL_MASK; michael@0: scale->y_offset_q4 = scaled_y(row << SUBPEL_BITS, scale->sfc) & SUBPEL_MASK; michael@0: } michael@0: michael@0: static void set_offsets_without_scaling(struct scale_factors *scale, michael@0: int row, int col) { michael@0: scale->x_offset_q4 = 0; michael@0: scale->y_offset_q4 = 0; michael@0: } michael@0: michael@0: static int get_fixed_point_scale_factor(int other_size, int this_size) { michael@0: // Calculate scaling factor once for each reference frame michael@0: // and use fixed point scaling factors in decoding and encoding routines. michael@0: // Hardware implementations can calculate scale factor in device driver michael@0: // and use multiplication and shifting on hardware instead of division. michael@0: return (other_size << REF_SCALE_SHIFT) / this_size; michael@0: } michael@0: michael@0: static int check_scale_factors(int other_w, int other_h, michael@0: int this_w, int this_h) { michael@0: return 2 * this_w >= other_w && michael@0: 2 * this_h >= other_h && michael@0: this_w <= 16 * other_w && michael@0: this_h <= 16 * other_h; michael@0: } michael@0: michael@0: void vp9_setup_scale_factors_for_frame(struct scale_factors *scale, michael@0: struct scale_factors_common *scale_comm, michael@0: int other_w, int other_h, michael@0: int this_w, int this_h) { michael@0: if (!check_scale_factors(other_w, other_h, this_w, this_h)) { michael@0: scale_comm->x_scale_fp = REF_INVALID_SCALE; michael@0: scale_comm->y_scale_fp = REF_INVALID_SCALE; michael@0: return; michael@0: } michael@0: michael@0: scale_comm->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w); michael@0: scale_comm->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h); michael@0: scale_comm->x_step_q4 = scaled_x(16, scale_comm); michael@0: scale_comm->y_step_q4 = scaled_y(16, scale_comm); michael@0: michael@0: if (vp9_is_scaled(scale_comm)) { michael@0: scale_comm->scale_value_x = scaled_x; michael@0: scale_comm->scale_value_y = scaled_y; michael@0: scale_comm->set_scaled_offsets = set_offsets_with_scaling; michael@0: scale_comm->scale_mv = scaled_mv; michael@0: } else { michael@0: scale_comm->scale_value_x = unscaled_value; michael@0: scale_comm->scale_value_y = unscaled_value; michael@0: scale_comm->set_scaled_offsets = set_offsets_without_scaling; michael@0: scale_comm->scale_mv = unscaled_mv; michael@0: } michael@0: michael@0: // TODO(agrange): Investigate the best choice of functions to use here michael@0: // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what michael@0: // to do at full-pel offsets. The current selection, where the filter is michael@0: // applied in one direction only, and not at all for 0,0, seems to give the michael@0: // best quality, but it may be worth trying an additional mode that does michael@0: // do the filtering on full-pel. michael@0: if (scale_comm->x_step_q4 == 16) { michael@0: if (scale_comm->y_step_q4 == 16) { michael@0: // No scaling in either direction. michael@0: scale_comm->predict[0][0][0] = vp9_convolve_copy; michael@0: scale_comm->predict[0][0][1] = vp9_convolve_avg; michael@0: scale_comm->predict[0][1][0] = vp9_convolve8_vert; michael@0: scale_comm->predict[0][1][1] = vp9_convolve8_avg_vert; michael@0: scale_comm->predict[1][0][0] = vp9_convolve8_horiz; michael@0: scale_comm->predict[1][0][1] = vp9_convolve8_avg_horiz; michael@0: } else { michael@0: // No scaling in x direction. Must always scale in the y direction. michael@0: scale_comm->predict[0][0][0] = vp9_convolve8_vert; michael@0: scale_comm->predict[0][0][1] = vp9_convolve8_avg_vert; michael@0: scale_comm->predict[0][1][0] = vp9_convolve8_vert; michael@0: scale_comm->predict[0][1][1] = vp9_convolve8_avg_vert; michael@0: scale_comm->predict[1][0][0] = vp9_convolve8; michael@0: scale_comm->predict[1][0][1] = vp9_convolve8_avg; michael@0: } michael@0: } else { michael@0: if (scale_comm->y_step_q4 == 16) { michael@0: // No scaling in the y direction. Must always scale in the x direction. michael@0: scale_comm->predict[0][0][0] = vp9_convolve8_horiz; michael@0: scale_comm->predict[0][0][1] = vp9_convolve8_avg_horiz; michael@0: scale_comm->predict[0][1][0] = vp9_convolve8; michael@0: scale_comm->predict[0][1][1] = vp9_convolve8_avg; michael@0: scale_comm->predict[1][0][0] = vp9_convolve8_horiz; michael@0: scale_comm->predict[1][0][1] = vp9_convolve8_avg_horiz; michael@0: } else { michael@0: // Must always scale in both directions. michael@0: scale_comm->predict[0][0][0] = vp9_convolve8; michael@0: scale_comm->predict[0][0][1] = vp9_convolve8_avg; michael@0: scale_comm->predict[0][1][0] = vp9_convolve8; michael@0: scale_comm->predict[0][1][1] = vp9_convolve8_avg; michael@0: scale_comm->predict[1][0][0] = vp9_convolve8; michael@0: scale_comm->predict[1][0][1] = vp9_convolve8_avg; michael@0: } michael@0: } michael@0: // 2D subpel motion always gets filtered in both directions michael@0: scale_comm->predict[1][1][0] = vp9_convolve8; michael@0: scale_comm->predict[1][1][1] = vp9_convolve8_avg; michael@0: michael@0: scale->sfc = scale_comm; michael@0: scale->x_offset_q4 = 0; // calculated per block michael@0: scale->y_offset_q4 = 0; // calculated per block michael@0: }