cost_neon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 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. // ARM NEON version of cost functions
  11. #include "./dsp.h"
  12. #if defined(WEBP_USE_NEON)
  13. #include "./neon.h"
  14. #include "../enc/cost_enc.h"
  15. static const uint8_t position[16] = { 1, 2, 3, 4, 5, 6, 7, 8,
  16. 9, 10, 11, 12, 13, 14, 15, 16 };
  17. static void SetResidualCoeffs_NEON(const int16_t* const coeffs,
  18. VP8Residual* const res) {
  19. const int16x8_t minus_one = vdupq_n_s16(-1);
  20. const int16x8_t coeffs_0 = vld1q_s16(coeffs);
  21. const int16x8_t coeffs_1 = vld1q_s16(coeffs + 8);
  22. const uint16x8_t eob_0 = vtstq_s16(coeffs_0, minus_one);
  23. const uint16x8_t eob_1 = vtstq_s16(coeffs_1, minus_one);
  24. const uint8x16_t eob = vcombine_u8(vqmovn_u16(eob_0), vqmovn_u16(eob_1));
  25. const uint8x16_t masked = vandq_u8(eob, vld1q_u8(position));
  26. #ifdef __aarch64__
  27. res->last = vmaxvq_u8(masked) - 1;
  28. #else
  29. const uint8x8_t eob_8x8 = vmax_u8(vget_low_u8(masked), vget_high_u8(masked));
  30. const uint16x8_t eob_16x8 = vmovl_u8(eob_8x8);
  31. const uint16x4_t eob_16x4 =
  32. vmax_u16(vget_low_u16(eob_16x8), vget_high_u16(eob_16x8));
  33. const uint32x4_t eob_32x4 = vmovl_u16(eob_16x4);
  34. uint32x2_t eob_32x2 =
  35. vmax_u32(vget_low_u32(eob_32x4), vget_high_u32(eob_32x4));
  36. eob_32x2 = vpmax_u32(eob_32x2, eob_32x2);
  37. vst1_lane_s32(&res->last, vreinterpret_s32_u32(eob_32x2), 0);
  38. --res->last;
  39. #endif // __aarch64__
  40. res->coeffs = coeffs;
  41. }
  42. static int GetResidualCost_NEON(int ctx0, const VP8Residual* const res) {
  43. uint8_t levels[16], ctxs[16];
  44. uint16_t abs_levels[16];
  45. int n = res->first;
  46. // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1
  47. const int p0 = res->prob[n][ctx0][0];
  48. CostArrayPtr const costs = res->costs;
  49. const uint16_t* t = costs[n][ctx0];
  50. // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0
  51. // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll
  52. // be missing during the loop.
  53. int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0;
  54. if (res->last < 0) {
  55. return VP8BitCost(0, p0);
  56. }
  57. { // precompute clamped levels and contexts, packed to 8b.
  58. const uint8x16_t kCst2 = vdupq_n_u8(2);
  59. const uint8x16_t kCst67 = vdupq_n_u8(MAX_VARIABLE_LEVEL);
  60. const int16x8_t c0 = vld1q_s16(res->coeffs);
  61. const int16x8_t c1 = vld1q_s16(res->coeffs + 8);
  62. const uint16x8_t E0 = vreinterpretq_u16_s16(vabsq_s16(c0));
  63. const uint16x8_t E1 = vreinterpretq_u16_s16(vabsq_s16(c1));
  64. const uint8x16_t F = vcombine_u8(vqmovn_u16(E0), vqmovn_u16(E1));
  65. const uint8x16_t G = vminq_u8(F, kCst2); // context = 0,1,2
  66. const uint8x16_t H = vminq_u8(F, kCst67); // clamp_level in [0..67]
  67. vst1q_u8(ctxs, G);
  68. vst1q_u8(levels, H);
  69. vst1q_u16(abs_levels, E0);
  70. vst1q_u16(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 VP8EncDspCostInitNEON(void);
  97. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitNEON(void) {
  98. VP8SetResidualCoeffs = SetResidualCoeffs_NEON;
  99. VP8GetResidualCost = GetResidualCost_NEON;
  100. }
  101. #else // !WEBP_USE_NEON
  102. WEBP_DSP_INIT_STUB(VP8EncDspCostInitNEON)
  103. #endif // WEBP_USE_NEON