cost_mips32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_MIPS32)
  13. #include "../enc/cost_enc.h"
  14. static int GetResidualCost_MIPS32(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. "sll %[temp0], %[n], 1 \n\t"
  40. "blez %[temp1], 2f \n\t"
  41. " addu %[res_coeffs], %[res_coeffs], %[temp0] \n\t"
  42. "1: \n\t"
  43. "lh %[v_reg], 0(%[res_coeffs]) \n\t"
  44. "addiu %[n], %[n], 1 \n\t"
  45. "negu %[temp0], %[v_reg] \n\t"
  46. "slti %[temp1], %[v_reg], 0 \n\t"
  47. "movn %[v_reg], %[temp0], %[temp1] \n\t"
  48. "sltiu %[temp0], %[v_reg], 2 \n\t"
  49. "move %[ctx_reg], %[v_reg] \n\t"
  50. "movz %[ctx_reg], %[const_2], %[temp0] \n\t"
  51. "sll %[temp1], %[v_reg], 1 \n\t"
  52. "addu %[temp1], %[temp1], %[VP8LevelFixedCosts] \n\t"
  53. "lhu %[temp1], 0(%[temp1]) \n\t"
  54. "slt %[temp0], %[v_reg], %[const_max_level] \n\t"
  55. "movz %[v_reg], %[const_max_level], %[temp0] \n\t"
  56. "addu %[cost], %[cost], %[temp1] \n\t"
  57. "sll %[v_reg], %[v_reg], 1 \n\t"
  58. "sll %[ctx_reg], %[ctx_reg], 2 \n\t"
  59. "addu %[v_reg], %[v_reg], %[t] \n\t"
  60. "lhu %[temp0], 0(%[v_reg]) \n\t"
  61. "addu %[p_costs], %[p_costs], %[inc_p_costs] \n\t"
  62. "addu %[t], %[p_costs], %[ctx_reg] \n\t"
  63. "addu %[cost], %[cost], %[temp0] \n\t"
  64. "addiu %[res_coeffs], %[res_coeffs], 2 \n\t"
  65. "bne %[n], %[res_last], 1b \n\t"
  66. " lw %[t], 0(%[t]) \n\t"
  67. "2: \n\t"
  68. ".set pop \n\t"
  69. : [cost]"+&r"(cost), [t]"+&r"(t), [n]"+&r"(n), [v_reg]"=&r"(v_reg),
  70. [ctx_reg]"=&r"(ctx_reg), [p_costs]"+&r"(p_costs), [temp0]"=&r"(temp0),
  71. [temp1]"=&r"(temp1), [res_coeffs]"+&r"(res_coeffs)
  72. : [const_2]"r"(const_2), [const_max_level]"r"(const_max_level),
  73. [VP8LevelFixedCosts]"r"(VP8LevelFixedCosts), [res_last]"r"(res_last),
  74. [inc_p_costs]"r"(inc_p_costs)
  75. : "memory"
  76. );
  77. // Last coefficient is always non-zero
  78. {
  79. const int v = abs(res->coeffs[n]);
  80. assert(v != 0);
  81. cost += VP8LevelCost(t, v);
  82. if (n < 15) {
  83. const int b = VP8EncBands[n + 1];
  84. const int ctx = (v == 1) ? 1 : 2;
  85. const int last_p0 = res->prob[b][ctx][0];
  86. cost += VP8BitCost(0, last_p0);
  87. }
  88. }
  89. return cost;
  90. }
  91. static void SetResidualCoeffs_MIPS32(const int16_t* const coeffs,
  92. VP8Residual* const res) {
  93. const int16_t* p_coeffs = (int16_t*)coeffs;
  94. int temp0, temp1, temp2, n, n1;
  95. assert(res->first == 0 || coeffs[0] == 0);
  96. __asm__ volatile (
  97. ".set push \n\t"
  98. ".set noreorder \n\t"
  99. "addiu %[p_coeffs], %[p_coeffs], 28 \n\t"
  100. "li %[n], 15 \n\t"
  101. "li %[temp2], -1 \n\t"
  102. "0: \n\t"
  103. "ulw %[temp0], 0(%[p_coeffs]) \n\t"
  104. "beqz %[temp0], 1f \n\t"
  105. #if defined(WORDS_BIGENDIAN)
  106. " sll %[temp1], %[temp0], 16 \n\t"
  107. #else
  108. " srl %[temp1], %[temp0], 16 \n\t"
  109. #endif
  110. "addiu %[n1], %[n], -1 \n\t"
  111. "movz %[temp0], %[n1], %[temp1] \n\t"
  112. "movn %[temp0], %[n], %[temp1] \n\t"
  113. "j 2f \n\t"
  114. " addiu %[temp2], %[temp0], 0 \n\t"
  115. "1: \n\t"
  116. "addiu %[n], %[n], -2 \n\t"
  117. "bgtz %[n], 0b \n\t"
  118. " addiu %[p_coeffs], %[p_coeffs], -4 \n\t"
  119. "2: \n\t"
  120. ".set pop \n\t"
  121. : [p_coeffs]"+&r"(p_coeffs), [temp0]"=&r"(temp0),
  122. [temp1]"=&r"(temp1), [temp2]"=&r"(temp2),
  123. [n]"=&r"(n), [n1]"=&r"(n1)
  124. :
  125. : "memory"
  126. );
  127. res->last = temp2;
  128. res->coeffs = coeffs;
  129. }
  130. //------------------------------------------------------------------------------
  131. // Entry point
  132. extern void VP8EncDspCostInitMIPS32(void);
  133. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitMIPS32(void) {
  134. VP8GetResidualCost = GetResidualCost_MIPS32;
  135. VP8SetResidualCoeffs = SetResidualCoeffs_MIPS32;
  136. }
  137. #else // !WEBP_USE_MIPS32
  138. WEBP_DSP_INIT_STUB(VP8EncDspCostInitMIPS32)
  139. #endif // WEBP_USE_MIPS32