cost_mips_dsp_r2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 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. // Author: Djordje Pesut (djordje.pesut@imgtec.com)
  11. #include "./dsp.h"
  12. #if defined(WEBP_USE_MIPS_DSP_R2)
  13. #include "../enc/cost_enc.h"
  14. static int GetResidualCost_MIPSdspR2(int ctx0, const VP8Residual* const res) {
  15. int temp0, temp1;
  16. int v_reg, ctx_reg;
  17. int n = res->first;
  18. // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1
  19. int p0 = res->prob[n][ctx0][0];
  20. CostArrayPtr const costs = res->costs;
  21. const uint16_t* t = costs[n][ctx0];
  22. // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0
  23. // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll
  24. // be missing during the loop.
  25. int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0;
  26. const int16_t* res_coeffs = res->coeffs;
  27. const int res_last = res->last;
  28. const int const_max_level = MAX_VARIABLE_LEVEL;
  29. const int const_2 = 2;
  30. const uint16_t** p_costs = &costs[n][0];
  31. const size_t inc_p_costs = NUM_CTX * sizeof(*p_costs);
  32. if (res->last < 0) {
  33. return VP8BitCost(0, p0);
  34. }
  35. __asm__ volatile (
  36. ".set push \n\t"
  37. ".set noreorder \n\t"
  38. "subu %[temp1], %[res_last], %[n] \n\t"
  39. "blez %[temp1], 2f \n\t"
  40. " nop \n\t"
  41. "1: \n\t"
  42. "sll %[temp0], %[n], 1 \n\t"
  43. "lhx %[v_reg], %[temp0](%[res_coeffs]) \n\t"
  44. "addiu %[n], %[n], 1 \n\t"
  45. "absq_s.w %[v_reg], %[v_reg] \n\t"
  46. "sltiu %[temp0], %[v_reg], 2 \n\t"
  47. "move %[ctx_reg], %[v_reg] \n\t"
  48. "movz %[ctx_reg], %[const_2], %[temp0] \n\t"
  49. "sll %[temp1], %[v_reg], 1 \n\t"
  50. "lhx %[temp1], %[temp1](%[VP8LevelFixedCosts]) \n\t"
  51. "slt %[temp0], %[v_reg], %[const_max_level] \n\t"
  52. "movz %[v_reg], %[const_max_level], %[temp0] \n\t"
  53. "addu %[cost], %[cost], %[temp1] \n\t"
  54. "sll %[v_reg], %[v_reg], 1 \n\t"
  55. "sll %[ctx_reg], %[ctx_reg], 2 \n\t"
  56. "lhx %[temp0], %[v_reg](%[t]) \n\t"
  57. "addu %[p_costs], %[p_costs], %[inc_p_costs] \n\t"
  58. "addu %[t], %[p_costs], %[ctx_reg] \n\t"
  59. "addu %[cost], %[cost], %[temp0] \n\t"
  60. "bne %[n], %[res_last], 1b \n\t"
  61. " lw %[t], 0(%[t]) \n\t"
  62. "2: \n\t"
  63. ".set pop \n\t"
  64. : [cost]"+&r"(cost), [t]"+&r"(t), [n]"+&r"(n), [v_reg]"=&r"(v_reg),
  65. [ctx_reg]"=&r"(ctx_reg), [p_costs]"+&r"(p_costs), [temp0]"=&r"(temp0),
  66. [temp1]"=&r"(temp1)
  67. : [const_2]"r"(const_2), [const_max_level]"r"(const_max_level),
  68. [VP8LevelFixedCosts]"r"(VP8LevelFixedCosts), [res_last]"r"(res_last),
  69. [res_coeffs]"r"(res_coeffs), [inc_p_costs]"r"(inc_p_costs)
  70. : "memory"
  71. );
  72. // Last coefficient is always non-zero
  73. {
  74. const int v = abs(res->coeffs[n]);
  75. assert(v != 0);
  76. cost += VP8LevelCost(t, v);
  77. if (n < 15) {
  78. const int b = VP8EncBands[n + 1];
  79. const int ctx = (v == 1) ? 1 : 2;
  80. const int last_p0 = res->prob[b][ctx][0];
  81. cost += VP8BitCost(0, last_p0);
  82. }
  83. }
  84. return cost;
  85. }
  86. //------------------------------------------------------------------------------
  87. // Entry point
  88. extern void VP8EncDspCostInitMIPSdspR2(void);
  89. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitMIPSdspR2(void) {
  90. VP8GetResidualCost = GetResidualCost_MIPSdspR2;
  91. }
  92. #else // !WEBP_USE_MIPS_DSP_R2
  93. WEBP_DSP_INIT_STUB(VP8EncDspCostInitMIPSdspR2)
  94. #endif // WEBP_USE_MIPS_DSP_R2