vp9_scale.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2013 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. #include "./vpx_dsp_rtcd.h"
  11. #include "vp9/common/vp9_filter.h"
  12. #include "vp9/common/vp9_scale.h"
  13. #include "vpx_dsp/vpx_filter.h"
  14. static INLINE int scaled_x(int val, const struct scale_factors *sf) {
  15. return (int)((int64_t)val * sf->x_scale_fp >> REF_SCALE_SHIFT);
  16. }
  17. static INLINE int scaled_y(int val, const struct scale_factors *sf) {
  18. return (int)((int64_t)val * sf->y_scale_fp >> REF_SCALE_SHIFT);
  19. }
  20. static int unscaled_value(int val, const struct scale_factors *sf) {
  21. (void) sf;
  22. return val;
  23. }
  24. static int get_fixed_point_scale_factor(int other_size, int this_size) {
  25. // Calculate scaling factor once for each reference frame
  26. // and use fixed point scaling factors in decoding and encoding routines.
  27. // Hardware implementations can calculate scale factor in device driver
  28. // and use multiplication and shifting on hardware instead of division.
  29. return (other_size << REF_SCALE_SHIFT) / this_size;
  30. }
  31. MV32 vp9_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf) {
  32. const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf) & SUBPEL_MASK;
  33. const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf) & SUBPEL_MASK;
  34. const MV32 res = {
  35. scaled_y(mv->row, sf) + y_off_q4,
  36. scaled_x(mv->col, sf) + x_off_q4
  37. };
  38. return res;
  39. }
  40. #if CONFIG_VP9_HIGHBITDEPTH
  41. void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
  42. int other_w, int other_h,
  43. int this_w, int this_h,
  44. int use_highbd) {
  45. #else
  46. void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
  47. int other_w, int other_h,
  48. int this_w, int this_h) {
  49. #endif
  50. if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
  51. sf->x_scale_fp = REF_INVALID_SCALE;
  52. sf->y_scale_fp = REF_INVALID_SCALE;
  53. return;
  54. }
  55. sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
  56. sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
  57. sf->x_step_q4 = scaled_x(16, sf);
  58. sf->y_step_q4 = scaled_y(16, sf);
  59. if (vp9_is_scaled(sf)) {
  60. sf->scale_value_x = scaled_x;
  61. sf->scale_value_y = scaled_y;
  62. } else {
  63. sf->scale_value_x = unscaled_value;
  64. sf->scale_value_y = unscaled_value;
  65. }
  66. // TODO(agrange): Investigate the best choice of functions to use here
  67. // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
  68. // to do at full-pel offsets. The current selection, where the filter is
  69. // applied in one direction only, and not at all for 0,0, seems to give the
  70. // best quality, but it may be worth trying an additional mode that does
  71. // do the filtering on full-pel.
  72. if (sf->x_step_q4 == 16) {
  73. if (sf->y_step_q4 == 16) {
  74. // No scaling in either direction.
  75. sf->predict[0][0][0] = vpx_convolve_copy;
  76. sf->predict[0][0][1] = vpx_convolve_avg;
  77. sf->predict[0][1][0] = vpx_convolve8_vert;
  78. sf->predict[0][1][1] = vpx_convolve8_avg_vert;
  79. sf->predict[1][0][0] = vpx_convolve8_horiz;
  80. sf->predict[1][0][1] = vpx_convolve8_avg_horiz;
  81. } else {
  82. // No scaling in x direction. Must always scale in the y direction.
  83. sf->predict[0][0][0] = vpx_scaled_vert;
  84. sf->predict[0][0][1] = vpx_scaled_avg_vert;
  85. sf->predict[0][1][0] = vpx_scaled_vert;
  86. sf->predict[0][1][1] = vpx_scaled_avg_vert;
  87. sf->predict[1][0][0] = vpx_scaled_2d;
  88. sf->predict[1][0][1] = vpx_scaled_avg_2d;
  89. }
  90. } else {
  91. if (sf->y_step_q4 == 16) {
  92. // No scaling in the y direction. Must always scale in the x direction.
  93. sf->predict[0][0][0] = vpx_scaled_horiz;
  94. sf->predict[0][0][1] = vpx_scaled_avg_horiz;
  95. sf->predict[0][1][0] = vpx_scaled_2d;
  96. sf->predict[0][1][1] = vpx_scaled_avg_2d;
  97. sf->predict[1][0][0] = vpx_scaled_horiz;
  98. sf->predict[1][0][1] = vpx_scaled_avg_horiz;
  99. } else {
  100. // Must always scale in both directions.
  101. sf->predict[0][0][0] = vpx_scaled_2d;
  102. sf->predict[0][0][1] = vpx_scaled_avg_2d;
  103. sf->predict[0][1][0] = vpx_scaled_2d;
  104. sf->predict[0][1][1] = vpx_scaled_avg_2d;
  105. sf->predict[1][0][0] = vpx_scaled_2d;
  106. sf->predict[1][0][1] = vpx_scaled_avg_2d;
  107. }
  108. }
  109. // 2D subpel motion always gets filtered in both directions
  110. if ((sf->x_step_q4 != 16) || (sf->y_step_q4 != 16)) {
  111. sf->predict[1][1][0] = vpx_scaled_2d;
  112. sf->predict[1][1][1] = vpx_scaled_avg_2d;
  113. } else {
  114. sf->predict[1][1][0] = vpx_convolve8;
  115. sf->predict[1][1][1] = vpx_convolve8_avg;
  116. }
  117. #if CONFIG_VP9_HIGHBITDEPTH
  118. if (use_highbd) {
  119. if (sf->x_step_q4 == 16) {
  120. if (sf->y_step_q4 == 16) {
  121. // No scaling in either direction.
  122. sf->highbd_predict[0][0][0] = vpx_highbd_convolve_copy;
  123. sf->highbd_predict[0][0][1] = vpx_highbd_convolve_avg;
  124. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
  125. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
  126. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
  127. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
  128. } else {
  129. // No scaling in x direction. Must always scale in the y direction.
  130. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_vert;
  131. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_vert;
  132. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
  133. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
  134. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
  135. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
  136. }
  137. } else {
  138. if (sf->y_step_q4 == 16) {
  139. // No scaling in the y direction. Must always scale in the x direction.
  140. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_horiz;
  141. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_horiz;
  142. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
  143. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
  144. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
  145. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
  146. } else {
  147. // Must always scale in both directions.
  148. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8;
  149. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg;
  150. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
  151. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
  152. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
  153. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
  154. }
  155. }
  156. // 2D subpel motion always gets filtered in both directions.
  157. sf->highbd_predict[1][1][0] = vpx_highbd_convolve8;
  158. sf->highbd_predict[1][1][1] = vpx_highbd_convolve8_avg;
  159. }
  160. #endif
  161. }