vp8_quantize_ssse3.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <tmmintrin.h> /* SSSE3 */
  11. #include "./vp8_rtcd.h"
  12. #include "vp8/encoder/block.h"
  13. /* bitscan reverse (bsr) */
  14. #if defined(_MSC_VER)
  15. #include <intrin.h>
  16. #pragma intrinsic(_BitScanReverse)
  17. static int bsr(int mask) {
  18. unsigned long eob;
  19. _BitScanReverse(&eob, mask);
  20. eob++;
  21. if (mask == 0) eob = 0;
  22. return eob;
  23. }
  24. #else
  25. static int bsr(int mask) {
  26. int eob;
  27. #if defined(__GNUC__) && __GNUC__
  28. __asm__ __volatile__("bsr %1, %0" : "=r"(eob) : "r"(mask) : "flags");
  29. #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  30. asm volatile("bsr %1, %0" : "=r"(eob) : "r"(mask) : "flags");
  31. #endif
  32. eob++;
  33. if (mask == 0) eob = 0;
  34. return eob;
  35. }
  36. #endif
  37. void vp8_fast_quantize_b_ssse3(BLOCK *b, BLOCKD *d) {
  38. int eob, mask;
  39. __m128i z0 = _mm_load_si128((__m128i *)(b->coeff));
  40. __m128i z1 = _mm_load_si128((__m128i *)(b->coeff + 8));
  41. __m128i round0 = _mm_load_si128((__m128i *)(b->round));
  42. __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8));
  43. __m128i quant_fast0 = _mm_load_si128((__m128i *)(b->quant_fast));
  44. __m128i quant_fast1 = _mm_load_si128((__m128i *)(b->quant_fast + 8));
  45. __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant));
  46. __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8));
  47. __m128i sz0, sz1, x, x0, x1, y0, y1, zeros, abs0, abs1;
  48. DECLARE_ALIGNED(16, const uint8_t,
  49. pshufb_zig_zag_mask[16]) = { 0, 1, 4, 8, 5, 2, 3, 6,
  50. 9, 12, 13, 10, 7, 11, 14, 15 };
  51. __m128i zig_zag = _mm_load_si128((const __m128i *)pshufb_zig_zag_mask);
  52. /* sign of z: z >> 15 */
  53. sz0 = _mm_srai_epi16(z0, 15);
  54. sz1 = _mm_srai_epi16(z1, 15);
  55. /* x = abs(z) */
  56. x0 = _mm_abs_epi16(z0);
  57. x1 = _mm_abs_epi16(z1);
  58. /* x += round */
  59. x0 = _mm_add_epi16(x0, round0);
  60. x1 = _mm_add_epi16(x1, round1);
  61. /* y = (x * quant) >> 16 */
  62. y0 = _mm_mulhi_epi16(x0, quant_fast0);
  63. y1 = _mm_mulhi_epi16(x1, quant_fast1);
  64. /* ASM saves Y for EOB */
  65. /* I think we can ignore that because adding the sign doesn't change anything
  66. * and multiplying 0 by dequant is OK as well */
  67. abs0 = y0;
  68. abs1 = y1;
  69. /* Restore the sign bit. */
  70. y0 = _mm_xor_si128(y0, sz0);
  71. y1 = _mm_xor_si128(y1, sz1);
  72. x0 = _mm_sub_epi16(y0, sz0);
  73. x1 = _mm_sub_epi16(y1, sz1);
  74. /* qcoeff = x */
  75. _mm_store_si128((__m128i *)(d->qcoeff), x0);
  76. _mm_store_si128((__m128i *)(d->qcoeff + 8), x1);
  77. /* x * dequant */
  78. x0 = _mm_mullo_epi16(x0, dequant0);
  79. x1 = _mm_mullo_epi16(x1, dequant1);
  80. /* dqcoeff = x * dequant */
  81. _mm_store_si128((__m128i *)(d->dqcoeff), x0);
  82. _mm_store_si128((__m128i *)(d->dqcoeff + 8), x1);
  83. zeros = _mm_setzero_si128();
  84. x0 = _mm_cmpgt_epi16(abs0, zeros);
  85. x1 = _mm_cmpgt_epi16(abs1, zeros);
  86. x = _mm_packs_epi16(x0, x1);
  87. x = _mm_shuffle_epi8(x, zig_zag);
  88. mask = _mm_movemask_epi8(x);
  89. eob = bsr(mask);
  90. *d->eob = 0xFF & eob;
  91. }