quantize_sse2.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2015 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 <assert.h>
  11. #include <emmintrin.h>
  12. #include <xmmintrin.h>
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx/vpx_integer.h"
  15. #include "vpx_dsp/x86/bitdepth_conversion_sse2.h"
  16. #include "vpx_dsp/x86/quantize_sse2.h"
  17. void vpx_quantize_b_sse2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  18. int skip_block, const int16_t *zbin_ptr,
  19. const int16_t *round_ptr, const int16_t *quant_ptr,
  20. const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
  21. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  22. uint16_t *eob_ptr, const int16_t *scan,
  23. const int16_t *iscan) {
  24. const __m128i zero = _mm_setzero_si128();
  25. int index = 16;
  26. __m128i zbin, round, quant, dequant, shift;
  27. __m128i coeff0, coeff1, coeff0_sign, coeff1_sign;
  28. __m128i qcoeff0, qcoeff1;
  29. __m128i cmp_mask0, cmp_mask1;
  30. __m128i eob, eob0;
  31. (void)scan;
  32. (void)skip_block;
  33. assert(!skip_block);
  34. // Setup global values.
  35. load_b_values(zbin_ptr, &zbin, round_ptr, &round, quant_ptr, &quant,
  36. dequant_ptr, &dequant, quant_shift_ptr, &shift);
  37. // Do DC and first 15 AC.
  38. coeff0 = load_tran_low(coeff_ptr);
  39. coeff1 = load_tran_low(coeff_ptr + 8);
  40. // Poor man's abs().
  41. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  42. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  43. qcoeff0 = invert_sign_sse2(coeff0, coeff0_sign);
  44. qcoeff1 = invert_sign_sse2(coeff1, coeff1_sign);
  45. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  46. zbin = _mm_unpackhi_epi64(zbin, zbin); // Switch DC to AC
  47. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  48. calculate_qcoeff(&qcoeff0, round, quant, shift);
  49. round = _mm_unpackhi_epi64(round, round);
  50. quant = _mm_unpackhi_epi64(quant, quant);
  51. shift = _mm_unpackhi_epi64(shift, shift);
  52. calculate_qcoeff(&qcoeff1, round, quant, shift);
  53. // Reinsert signs
  54. qcoeff0 = invert_sign_sse2(qcoeff0, coeff0_sign);
  55. qcoeff1 = invert_sign_sse2(qcoeff1, coeff1_sign);
  56. // Mask out zbin threshold coeffs
  57. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  58. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  59. store_tran_low(qcoeff0, qcoeff_ptr);
  60. store_tran_low(qcoeff1, qcoeff_ptr + 8);
  61. calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr);
  62. dequant = _mm_unpackhi_epi64(dequant, dequant);
  63. calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + 8);
  64. eob = scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, 0, zero);
  65. // AC only loop.
  66. while (index < n_coeffs) {
  67. coeff0 = load_tran_low(coeff_ptr + index);
  68. coeff1 = load_tran_low(coeff_ptr + index + 8);
  69. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  70. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  71. qcoeff0 = invert_sign_sse2(coeff0, coeff0_sign);
  72. qcoeff1 = invert_sign_sse2(coeff1, coeff1_sign);
  73. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  74. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  75. calculate_qcoeff(&qcoeff0, round, quant, shift);
  76. calculate_qcoeff(&qcoeff1, round, quant, shift);
  77. qcoeff0 = invert_sign_sse2(qcoeff0, coeff0_sign);
  78. qcoeff1 = invert_sign_sse2(qcoeff1, coeff1_sign);
  79. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  80. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  81. store_tran_low(qcoeff0, qcoeff_ptr + index);
  82. store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
  83. calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr + index);
  84. calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + index + 8);
  85. eob0 = scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, index,
  86. zero);
  87. eob = _mm_max_epi16(eob, eob0);
  88. index += 16;
  89. }
  90. *eob_ptr = accumulate_eob(eob);
  91. }