quantize_sse4.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2012 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 <smmintrin.h> /* SSE4.1 */
  11. #include "./vp8_rtcd.h"
  12. #include "vp8/common/entropy.h" /* vp8_default_inv_zig_zag */
  13. #include "vp8/encoder/block.h"
  14. #define SELECT_EOB(i, z, x, y, q) \
  15. do { \
  16. short boost = *zbin_boost_ptr; \
  17. /* Technically _mm_extract_epi16() returns an int: */ \
  18. /* https://bugs.llvm.org/show_bug.cgi?id=41657 */ \
  19. short x_z = (short)_mm_extract_epi16(x, z); \
  20. short y_z = (short)_mm_extract_epi16(y, z); \
  21. int cmp = (x_z < boost) | (y_z == 0); \
  22. zbin_boost_ptr++; \
  23. if (cmp) break; \
  24. q = _mm_insert_epi16(q, y_z, z); \
  25. eob = i; \
  26. zbin_boost_ptr = b->zrun_zbin_boost; \
  27. } while (0)
  28. void vp8_regular_quantize_b_sse4_1(BLOCK *b, BLOCKD *d) {
  29. char eob = 0;
  30. short *zbin_boost_ptr = b->zrun_zbin_boost;
  31. __m128i x0, x1, y0, y1, x_minus_zbin0, x_minus_zbin1, dqcoeff0, dqcoeff1;
  32. __m128i quant_shift0 = _mm_load_si128((__m128i *)(b->quant_shift));
  33. __m128i quant_shift1 = _mm_load_si128((__m128i *)(b->quant_shift + 8));
  34. __m128i z0 = _mm_load_si128((__m128i *)(b->coeff));
  35. __m128i z1 = _mm_load_si128((__m128i *)(b->coeff + 8));
  36. __m128i zbin_extra = _mm_cvtsi32_si128(b->zbin_extra);
  37. __m128i zbin0 = _mm_load_si128((__m128i *)(b->zbin));
  38. __m128i zbin1 = _mm_load_si128((__m128i *)(b->zbin + 8));
  39. __m128i round0 = _mm_load_si128((__m128i *)(b->round));
  40. __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8));
  41. __m128i quant0 = _mm_load_si128((__m128i *)(b->quant));
  42. __m128i quant1 = _mm_load_si128((__m128i *)(b->quant + 8));
  43. __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant));
  44. __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8));
  45. __m128i qcoeff0 = _mm_setzero_si128();
  46. __m128i qcoeff1 = _mm_setzero_si128();
  47. /* Duplicate to all lanes. */
  48. zbin_extra = _mm_shufflelo_epi16(zbin_extra, 0);
  49. zbin_extra = _mm_unpacklo_epi16(zbin_extra, zbin_extra);
  50. /* x = abs(z) */
  51. x0 = _mm_abs_epi16(z0);
  52. x1 = _mm_abs_epi16(z1);
  53. /* zbin[] + zbin_extra */
  54. zbin0 = _mm_add_epi16(zbin0, zbin_extra);
  55. zbin1 = _mm_add_epi16(zbin1, zbin_extra);
  56. /* In C x is compared to zbin where zbin = zbin[] + boost + extra. Rebalance
  57. * the equation because boost is the only value which can change:
  58. * x - (zbin[] + extra) >= boost */
  59. x_minus_zbin0 = _mm_sub_epi16(x0, zbin0);
  60. x_minus_zbin1 = _mm_sub_epi16(x1, zbin1);
  61. /* All the remaining calculations are valid whether they are done now with
  62. * simd or later inside the loop one at a time. */
  63. x0 = _mm_add_epi16(x0, round0);
  64. x1 = _mm_add_epi16(x1, round1);
  65. y0 = _mm_mulhi_epi16(x0, quant0);
  66. y1 = _mm_mulhi_epi16(x1, quant1);
  67. y0 = _mm_add_epi16(y0, x0);
  68. y1 = _mm_add_epi16(y1, x1);
  69. /* Instead of shifting each value independently we convert the scaling
  70. * factor with 1 << (16 - shift) so we can use multiply/return high half. */
  71. y0 = _mm_mulhi_epi16(y0, quant_shift0);
  72. y1 = _mm_mulhi_epi16(y1, quant_shift1);
  73. /* Restore the sign. */
  74. y0 = _mm_sign_epi16(y0, z0);
  75. y1 = _mm_sign_epi16(y1, z1);
  76. /* The loop gets unrolled anyway. Avoid the vp8_default_zig_zag1d lookup. */
  77. SELECT_EOB(1, 0, x_minus_zbin0, y0, qcoeff0);
  78. SELECT_EOB(2, 1, x_minus_zbin0, y0, qcoeff0);
  79. SELECT_EOB(3, 4, x_minus_zbin0, y0, qcoeff0);
  80. SELECT_EOB(4, 0, x_minus_zbin1, y1, qcoeff1);
  81. SELECT_EOB(5, 5, x_minus_zbin0, y0, qcoeff0);
  82. SELECT_EOB(6, 2, x_minus_zbin0, y0, qcoeff0);
  83. SELECT_EOB(7, 3, x_minus_zbin0, y0, qcoeff0);
  84. SELECT_EOB(8, 6, x_minus_zbin0, y0, qcoeff0);
  85. SELECT_EOB(9, 1, x_minus_zbin1, y1, qcoeff1);
  86. SELECT_EOB(10, 4, x_minus_zbin1, y1, qcoeff1);
  87. SELECT_EOB(11, 5, x_minus_zbin1, y1, qcoeff1);
  88. SELECT_EOB(12, 2, x_minus_zbin1, y1, qcoeff1);
  89. SELECT_EOB(13, 7, x_minus_zbin0, y0, qcoeff0);
  90. SELECT_EOB(14, 3, x_minus_zbin1, y1, qcoeff1);
  91. SELECT_EOB(15, 6, x_minus_zbin1, y1, qcoeff1);
  92. SELECT_EOB(16, 7, x_minus_zbin1, y1, qcoeff1);
  93. _mm_store_si128((__m128i *)(d->qcoeff), qcoeff0);
  94. _mm_store_si128((__m128i *)(d->qcoeff + 8), qcoeff1);
  95. dqcoeff0 = _mm_mullo_epi16(qcoeff0, dequant0);
  96. dqcoeff1 = _mm_mullo_epi16(qcoeff1, dequant1);
  97. _mm_store_si128((__m128i *)(d->dqcoeff), dqcoeff0);
  98. _mm_store_si128((__m128i *)(d->dqcoeff + 8), dqcoeff1);
  99. *d->eob = eob;
  100. }