vp9_scale.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = { scaled_y(mv->row, sf) + y_off_q4,
  35. scaled_x(mv->col, sf) + x_off_q4 };
  36. return res;
  37. }
  38. #if CONFIG_VP9_HIGHBITDEPTH
  39. void vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
  40. int other_h, int this_w, int this_h,
  41. int use_highbd) {
  42. #else
  43. void vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
  44. int other_h, int this_w, int this_h) {
  45. #endif
  46. if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
  47. sf->x_scale_fp = REF_INVALID_SCALE;
  48. sf->y_scale_fp = REF_INVALID_SCALE;
  49. return;
  50. }
  51. sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
  52. sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
  53. sf->x_step_q4 = scaled_x(16, sf);
  54. sf->y_step_q4 = scaled_y(16, sf);
  55. if (vp9_is_scaled(sf)) {
  56. sf->scale_value_x = scaled_x;
  57. sf->scale_value_y = scaled_y;
  58. } else {
  59. sf->scale_value_x = unscaled_value;
  60. sf->scale_value_y = unscaled_value;
  61. }
  62. // TODO(agrange): Investigate the best choice of functions to use here
  63. // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
  64. // to do at full-pel offsets. The current selection, where the filter is
  65. // applied in one direction only, and not at all for 0,0, seems to give the
  66. // best quality, but it may be worth trying an additional mode that does
  67. // do the filtering on full-pel.
  68. if (sf->x_step_q4 == 16) {
  69. if (sf->y_step_q4 == 16) {
  70. // No scaling in either direction.
  71. sf->predict[0][0][0] = vpx_convolve_copy;
  72. sf->predict[0][0][1] = vpx_convolve_avg;
  73. sf->predict[0][1][0] = vpx_convolve8_vert;
  74. sf->predict[0][1][1] = vpx_convolve8_avg_vert;
  75. sf->predict[1][0][0] = vpx_convolve8_horiz;
  76. sf->predict[1][0][1] = vpx_convolve8_avg_horiz;
  77. } else {
  78. // No scaling in x direction. Must always scale in the y direction.
  79. sf->predict[0][0][0] = vpx_scaled_vert;
  80. sf->predict[0][0][1] = vpx_scaled_avg_vert;
  81. sf->predict[0][1][0] = vpx_scaled_vert;
  82. sf->predict[0][1][1] = vpx_scaled_avg_vert;
  83. sf->predict[1][0][0] = vpx_scaled_2d;
  84. sf->predict[1][0][1] = vpx_scaled_avg_2d;
  85. }
  86. } else {
  87. if (sf->y_step_q4 == 16) {
  88. // No scaling in the y direction. Must always scale in the x direction.
  89. sf->predict[0][0][0] = vpx_scaled_horiz;
  90. sf->predict[0][0][1] = vpx_scaled_avg_horiz;
  91. sf->predict[0][1][0] = vpx_scaled_2d;
  92. sf->predict[0][1][1] = vpx_scaled_avg_2d;
  93. sf->predict[1][0][0] = vpx_scaled_horiz;
  94. sf->predict[1][0][1] = vpx_scaled_avg_horiz;
  95. } else {
  96. // Must always scale in both directions.
  97. sf->predict[0][0][0] = vpx_scaled_2d;
  98. sf->predict[0][0][1] = vpx_scaled_avg_2d;
  99. sf->predict[0][1][0] = vpx_scaled_2d;
  100. sf->predict[0][1][1] = vpx_scaled_avg_2d;
  101. sf->predict[1][0][0] = vpx_scaled_2d;
  102. sf->predict[1][0][1] = vpx_scaled_avg_2d;
  103. }
  104. }
  105. // 2D subpel motion always gets filtered in both directions
  106. if ((sf->x_step_q4 != 16) || (sf->y_step_q4 != 16)) {
  107. sf->predict[1][1][0] = vpx_scaled_2d;
  108. sf->predict[1][1][1] = vpx_scaled_avg_2d;
  109. } else {
  110. sf->predict[1][1][0] = vpx_convolve8;
  111. sf->predict[1][1][1] = vpx_convolve8_avg;
  112. }
  113. #if CONFIG_VP9_HIGHBITDEPTH
  114. if (use_highbd) {
  115. if (sf->x_step_q4 == 16) {
  116. if (sf->y_step_q4 == 16) {
  117. // No scaling in either direction.
  118. sf->highbd_predict[0][0][0] = vpx_highbd_convolve_copy;
  119. sf->highbd_predict[0][0][1] = vpx_highbd_convolve_avg;
  120. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
  121. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
  122. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
  123. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
  124. } else {
  125. // No scaling in x direction. Must always scale in the y direction.
  126. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_vert;
  127. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_vert;
  128. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
  129. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
  130. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
  131. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
  132. }
  133. } else {
  134. if (sf->y_step_q4 == 16) {
  135. // No scaling in the y direction. Must always scale in the x direction.
  136. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_horiz;
  137. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_horiz;
  138. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
  139. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
  140. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
  141. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
  142. } else {
  143. // Must always scale in both directions.
  144. sf->highbd_predict[0][0][0] = vpx_highbd_convolve8;
  145. sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg;
  146. sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
  147. sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
  148. sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
  149. sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
  150. }
  151. }
  152. // 2D subpel motion always gets filtered in both directions.
  153. sf->highbd_predict[1][1][0] = vpx_highbd_convolve8;
  154. sf->highbd_predict[1][1][1] = vpx_highbd_convolve8_avg;
  155. }
  156. #endif
  157. }