cost_sse2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // SSE2 version of cost functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include <emmintrin.h>
  16. #include "../enc/cost_enc.h"
  17. #include "../enc/vp8i_enc.h"
  18. #include "../utils/utils.h"
  19. //------------------------------------------------------------------------------
  20. static void SetResidualCoeffs_SSE2(const int16_t* const coeffs,
  21. VP8Residual* const res) {
  22. const __m128i c0 = _mm_loadu_si128((const __m128i*)(coeffs + 0));
  23. const __m128i c1 = _mm_loadu_si128((const __m128i*)(coeffs + 8));
  24. // Use SSE2 to compare 16 values with a single instruction.
  25. const __m128i zero = _mm_setzero_si128();
  26. const __m128i m0 = _mm_packs_epi16(c0, c1);
  27. const __m128i m1 = _mm_cmpeq_epi8(m0, zero);
  28. // Get the comparison results as a bitmask into 16bits. Negate the mask to get
  29. // the position of entries that are not equal to zero. We don't need to mask
  30. // out least significant bits according to res->first, since coeffs[0] is 0
  31. // if res->first > 0.
  32. const uint32_t mask = 0x0000ffffu ^ (uint32_t)_mm_movemask_epi8(m1);
  33. // The position of the most significant non-zero bit indicates the position of
  34. // the last non-zero value.
  35. assert(res->first == 0 || coeffs[0] == 0);
  36. res->last = mask ? BitsLog2Floor(mask) : -1;
  37. res->coeffs = coeffs;
  38. }
  39. static int GetResidualCost_SSE2(int ctx0, const VP8Residual* const res) {
  40. uint8_t levels[16], ctxs[16];
  41. uint16_t abs_levels[16];
  42. int n = res->first;
  43. // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1
  44. const int p0 = res->prob[n][ctx0][0];
  45. CostArrayPtr const costs = res->costs;
  46. const uint16_t* t = costs[n][ctx0];
  47. // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0
  48. // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll
  49. // be missing during the loop.
  50. int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0;
  51. if (res->last < 0) {
  52. return VP8BitCost(0, p0);
  53. }
  54. { // precompute clamped levels and contexts, packed to 8b.
  55. const __m128i zero = _mm_setzero_si128();
  56. const __m128i kCst2 = _mm_set1_epi8(2);
  57. const __m128i kCst67 = _mm_set1_epi8(MAX_VARIABLE_LEVEL);
  58. const __m128i c0 = _mm_loadu_si128((const __m128i*)&res->coeffs[0]);
  59. const __m128i c1 = _mm_loadu_si128((const __m128i*)&res->coeffs[8]);
  60. const __m128i D0 = _mm_sub_epi16(zero, c0);
  61. const __m128i D1 = _mm_sub_epi16(zero, c1);
  62. const __m128i E0 = _mm_max_epi16(c0, D0); // abs(v), 16b
  63. const __m128i E1 = _mm_max_epi16(c1, D1);
  64. const __m128i F = _mm_packs_epi16(E0, E1);
  65. const __m128i G = _mm_min_epu8(F, kCst2); // context = 0,1,2
  66. const __m128i H = _mm_min_epu8(F, kCst67); // clamp_level in [0..67]
  67. _mm_storeu_si128((__m128i*)&ctxs[0], G);
  68. _mm_storeu_si128((__m128i*)&levels[0], H);
  69. _mm_storeu_si128((__m128i*)&abs_levels[0], E0);
  70. _mm_storeu_si128((__m128i*)&abs_levels[8], E1);
  71. }
  72. for (; n < res->last; ++n) {
  73. const int ctx = ctxs[n];
  74. const int level = levels[n];
  75. const int flevel = abs_levels[n]; // full level
  76. cost += VP8LevelFixedCosts[flevel] + t[level]; // simplified VP8LevelCost()
  77. t = costs[n + 1][ctx];
  78. }
  79. // Last coefficient is always non-zero
  80. {
  81. const int level = levels[n];
  82. const int flevel = abs_levels[n];
  83. assert(flevel != 0);
  84. cost += VP8LevelFixedCosts[flevel] + t[level];
  85. if (n < 15) {
  86. const int b = VP8EncBands[n + 1];
  87. const int ctx = ctxs[n];
  88. const int last_p0 = res->prob[b][ctx][0];
  89. cost += VP8BitCost(0, last_p0);
  90. }
  91. }
  92. return cost;
  93. }
  94. //------------------------------------------------------------------------------
  95. // Entry point
  96. extern void VP8EncDspCostInitSSE2(void);
  97. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitSSE2(void) {
  98. VP8SetResidualCoeffs = SetResidualCoeffs_SSE2;
  99. VP8GetResidualCost = GetResidualCost_SSE2;
  100. }
  101. #else // !WEBP_USE_SSE2
  102. WEBP_DSP_INIT_STUB(VP8EncDspCostInitSSE2)
  103. #endif // WEBP_USE_SSE2