vp9_frame_scale_ssse3.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2016 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 <tmmintrin.h> // SSSE3
  11. #include "./vp9_rtcd.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "./vpx_scale_rtcd.h"
  14. #include "vpx_scale/yv12config.h"
  15. extern void vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
  16. YV12_BUFFER_CONFIG *dst);
  17. static void downsample_2_to_1_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  18. uint8_t *dst, ptrdiff_t dst_stride, int w,
  19. int h) {
  20. const __m128i mask = _mm_set1_epi16(0x00FF);
  21. const int max_width = w & ~15;
  22. int y;
  23. for (y = 0; y < h; ++y) {
  24. int x;
  25. for (x = 0; x < max_width; x += 16) {
  26. const __m128i a = _mm_loadu_si128((const __m128i *)(src + x * 2 + 0));
  27. const __m128i b = _mm_loadu_si128((const __m128i *)(src + x * 2 + 16));
  28. const __m128i a_and = _mm_and_si128(a, mask);
  29. const __m128i b_and = _mm_and_si128(b, mask);
  30. const __m128i c = _mm_packus_epi16(a_and, b_and);
  31. _mm_storeu_si128((__m128i *)(dst + x), c);
  32. }
  33. for (; x < w; ++x) dst[x] = src[x * 2];
  34. src += src_stride * 2;
  35. dst += dst_stride;
  36. }
  37. }
  38. static INLINE __m128i filter(const __m128i *const a, const __m128i *const b,
  39. const __m128i *const c, const __m128i *const d,
  40. const __m128i *const e, const __m128i *const f,
  41. const __m128i *const g, const __m128i *const h) {
  42. const __m128i coeffs_ab =
  43. _mm_set_epi8(6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1);
  44. const __m128i coeffs_cd = _mm_set_epi8(78, -19, 78, -19, 78, -19, 78, -19, 78,
  45. -19, 78, -19, 78, -19, 78, -19);
  46. const __m128i const64_x16 = _mm_set1_epi16(64);
  47. const __m128i ab = _mm_unpacklo_epi8(*a, *b);
  48. const __m128i cd = _mm_unpacklo_epi8(*c, *d);
  49. const __m128i fe = _mm_unpacklo_epi8(*f, *e);
  50. const __m128i hg = _mm_unpacklo_epi8(*h, *g);
  51. const __m128i ab_terms = _mm_maddubs_epi16(ab, coeffs_ab);
  52. const __m128i cd_terms = _mm_maddubs_epi16(cd, coeffs_cd);
  53. const __m128i fe_terms = _mm_maddubs_epi16(fe, coeffs_cd);
  54. const __m128i hg_terms = _mm_maddubs_epi16(hg, coeffs_ab);
  55. // can not overflow
  56. const __m128i abcd_terms = _mm_add_epi16(ab_terms, cd_terms);
  57. // can not overflow
  58. const __m128i fehg_terms = _mm_add_epi16(fe_terms, hg_terms);
  59. // can overflow, use saturating add
  60. const __m128i terms = _mm_adds_epi16(abcd_terms, fehg_terms);
  61. const __m128i round = _mm_adds_epi16(terms, const64_x16);
  62. const __m128i shift = _mm_srai_epi16(round, 7);
  63. return _mm_packus_epi16(shift, shift);
  64. }
  65. static void eight_tap_row_ssse3(const uint8_t *src, uint8_t *dst, int w) {
  66. const int max_width = w & ~7;
  67. int x = 0;
  68. for (; x < max_width; x += 8) {
  69. const __m128i a = _mm_loadl_epi64((const __m128i *)(src + x + 0));
  70. const __m128i b = _mm_loadl_epi64((const __m128i *)(src + x + 1));
  71. const __m128i c = _mm_loadl_epi64((const __m128i *)(src + x + 2));
  72. const __m128i d = _mm_loadl_epi64((const __m128i *)(src + x + 3));
  73. const __m128i e = _mm_loadl_epi64((const __m128i *)(src + x + 4));
  74. const __m128i f = _mm_loadl_epi64((const __m128i *)(src + x + 5));
  75. const __m128i g = _mm_loadl_epi64((const __m128i *)(src + x + 6));
  76. const __m128i h = _mm_loadl_epi64((const __m128i *)(src + x + 7));
  77. const __m128i pack = filter(&a, &b, &c, &d, &e, &f, &g, &h);
  78. _mm_storel_epi64((__m128i *)(dst + x), pack);
  79. }
  80. }
  81. static void upsample_1_to_2_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  82. uint8_t *dst, ptrdiff_t dst_stride, int dst_w,
  83. int dst_h) {
  84. dst_w /= 2;
  85. dst_h /= 2;
  86. {
  87. DECLARE_ALIGNED(16, uint8_t, tmp[1920 * 8]);
  88. uint8_t *tmp0 = tmp + dst_w * 0;
  89. uint8_t *tmp1 = tmp + dst_w * 1;
  90. uint8_t *tmp2 = tmp + dst_w * 2;
  91. uint8_t *tmp3 = tmp + dst_w * 3;
  92. uint8_t *tmp4 = tmp + dst_w * 4;
  93. uint8_t *tmp5 = tmp + dst_w * 5;
  94. uint8_t *tmp6 = tmp + dst_w * 6;
  95. uint8_t *tmp7 = tmp + dst_w * 7;
  96. uint8_t *tmp8 = NULL;
  97. const int max_width = dst_w & ~7;
  98. int y;
  99. eight_tap_row_ssse3(src - src_stride * 3 - 3, tmp0, dst_w);
  100. eight_tap_row_ssse3(src - src_stride * 2 - 3, tmp1, dst_w);
  101. eight_tap_row_ssse3(src - src_stride * 1 - 3, tmp2, dst_w);
  102. eight_tap_row_ssse3(src + src_stride * 0 - 3, tmp3, dst_w);
  103. eight_tap_row_ssse3(src + src_stride * 1 - 3, tmp4, dst_w);
  104. eight_tap_row_ssse3(src + src_stride * 2 - 3, tmp5, dst_w);
  105. eight_tap_row_ssse3(src + src_stride * 3 - 3, tmp6, dst_w);
  106. for (y = 0; y < dst_h; y++) {
  107. int x;
  108. eight_tap_row_ssse3(src + src_stride * 4 - 3, tmp7, dst_w);
  109. for (x = 0; x < max_width; x += 8) {
  110. const __m128i A = _mm_loadl_epi64((const __m128i *)(src + x));
  111. const __m128i B = _mm_loadl_epi64((const __m128i *)(tmp3 + x));
  112. const __m128i AB = _mm_unpacklo_epi8(A, B);
  113. __m128i C, D, CD;
  114. _mm_storeu_si128((__m128i *)(dst + x * 2), AB);
  115. {
  116. const __m128i a =
  117. _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 3));
  118. const __m128i b =
  119. _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 2));
  120. const __m128i c =
  121. _mm_loadl_epi64((const __m128i *)(src + x - src_stride * 1));
  122. const __m128i d =
  123. _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 0));
  124. const __m128i e =
  125. _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 1));
  126. const __m128i f =
  127. _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 2));
  128. const __m128i g =
  129. _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 3));
  130. const __m128i h =
  131. _mm_loadl_epi64((const __m128i *)(src + x + src_stride * 4));
  132. C = filter(&a, &b, &c, &d, &e, &f, &g, &h);
  133. }
  134. {
  135. const __m128i a = _mm_loadl_epi64((const __m128i *)(tmp0 + x));
  136. const __m128i b = _mm_loadl_epi64((const __m128i *)(tmp1 + x));
  137. const __m128i c = _mm_loadl_epi64((const __m128i *)(tmp2 + x));
  138. const __m128i d = _mm_loadl_epi64((const __m128i *)(tmp3 + x));
  139. const __m128i e = _mm_loadl_epi64((const __m128i *)(tmp4 + x));
  140. const __m128i f = _mm_loadl_epi64((const __m128i *)(tmp5 + x));
  141. const __m128i g = _mm_loadl_epi64((const __m128i *)(tmp6 + x));
  142. const __m128i h = _mm_loadl_epi64((const __m128i *)(tmp7 + x));
  143. D = filter(&a, &b, &c, &d, &e, &f, &g, &h);
  144. }
  145. CD = _mm_unpacklo_epi8(C, D);
  146. _mm_storeu_si128((__m128i *)(dst + x * 2 + dst_stride), CD);
  147. }
  148. src += src_stride;
  149. dst += dst_stride * 2;
  150. tmp8 = tmp0;
  151. tmp0 = tmp1;
  152. tmp1 = tmp2;
  153. tmp2 = tmp3;
  154. tmp3 = tmp4;
  155. tmp4 = tmp5;
  156. tmp5 = tmp6;
  157. tmp6 = tmp7;
  158. tmp7 = tmp8;
  159. }
  160. }
  161. }
  162. void vp9_scale_and_extend_frame_ssse3(const YV12_BUFFER_CONFIG *src,
  163. YV12_BUFFER_CONFIG *dst) {
  164. const int src_w = src->y_crop_width;
  165. const int src_h = src->y_crop_height;
  166. const int dst_w = dst->y_crop_width;
  167. const int dst_h = dst->y_crop_height;
  168. const int dst_uv_w = dst_w / 2;
  169. const int dst_uv_h = dst_h / 2;
  170. if (dst_w * 2 == src_w && dst_h * 2 == src_h) {
  171. downsample_2_to_1_ssse3(src->y_buffer, src->y_stride, dst->y_buffer,
  172. dst->y_stride, dst_w, dst_h);
  173. downsample_2_to_1_ssse3(src->u_buffer, src->uv_stride, dst->u_buffer,
  174. dst->uv_stride, dst_uv_w, dst_uv_h);
  175. downsample_2_to_1_ssse3(src->v_buffer, src->uv_stride, dst->v_buffer,
  176. dst->uv_stride, dst_uv_w, dst_uv_h);
  177. vpx_extend_frame_borders(dst);
  178. } else if (dst_w == src_w * 2 && dst_h == src_h * 2) {
  179. // The upsample() supports widths up to 1920 * 2. If greater, fall back
  180. // to vp9_scale_and_extend_frame_c().
  181. if (dst_w / 2 <= 1920) {
  182. upsample_1_to_2_ssse3(src->y_buffer, src->y_stride, dst->y_buffer,
  183. dst->y_stride, dst_w, dst_h);
  184. upsample_1_to_2_ssse3(src->u_buffer, src->uv_stride, dst->u_buffer,
  185. dst->uv_stride, dst_uv_w, dst_uv_h);
  186. upsample_1_to_2_ssse3(src->v_buffer, src->uv_stride, dst->v_buffer,
  187. dst->uv_stride, dst_uv_w, dst_uv_h);
  188. vpx_extend_frame_borders(dst);
  189. } else {
  190. vp9_scale_and_extend_frame_c(src, dst);
  191. }
  192. } else {
  193. vp9_scale_and_extend_frame_c(src, dst);
  194. }
  195. }