sharpyuv_dsp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2022 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. // Speed-critical functions for Sharp YUV.
  11. //
  12. // Author: Skal ([email protected])
  13. #include "sharpyuv/sharpyuv_dsp.h"
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include "sharpyuv/sharpyuv.h"
  17. #include "sharpyuv/sharpyuv_cpu.h"
  18. #include "src/webp/types.h"
  19. //-----------------------------------------------------------------------------
  20. #if !WEBP_NEON_OMIT_C_CODE
  21. static uint16_t clip(int v, int max) {
  22. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  23. }
  24. static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src,
  25. uint16_t* dst, int len, int bit_depth) {
  26. uint64_t diff = 0;
  27. int i;
  28. const int max_y = (1 << bit_depth) - 1;
  29. for (i = 0; i < len; ++i) {
  30. const int diff_y = ref[i] - src[i];
  31. const int new_y = (int)dst[i] + diff_y;
  32. dst[i] = clip(new_y, max_y);
  33. diff += (uint64_t)abs(diff_y);
  34. }
  35. return diff;
  36. }
  37. static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src,
  38. int16_t* dst, int len) {
  39. int i;
  40. for (i = 0; i < len; ++i) {
  41. const int diff_uv = ref[i] - src[i];
  42. dst[i] += diff_uv;
  43. }
  44. }
  45. static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
  46. const uint16_t* best_y, uint16_t* out,
  47. int bit_depth) {
  48. int i;
  49. const int max_y = (1 << bit_depth) - 1;
  50. for (i = 0; i < len; ++i, ++A, ++B) {
  51. const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
  52. const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
  53. out[2 * i + 0] = clip(best_y[2 * i + 0] + v0, max_y);
  54. out[2 * i + 1] = clip(best_y[2 * i + 1] + v1, max_y);
  55. }
  56. }
  57. #endif // !WEBP_NEON_OMIT_C_CODE
  58. #define YUV_FIX 16 // fixed-point precision for RGB->YUV
  59. static const int kYuvHalf = 1 << (YUV_FIX - 1);
  60. // Maps a value in [0, (256 << YUV_FIX) - 1] to [0,
  61. // precomputed_scores_table_sampling - 1]. It is important that the extremal
  62. // values are preserved and 1:1 mapped:
  63. // ConvertValue(0) = 0
  64. // ConvertValue((256 << 16) - 1) = rgb_sampling_size - 1
  65. static int SharpYuvConvertValueToSampledIdx(int v, int rgb_sampling_size) {
  66. v = (v + kYuvHalf) >> YUV_FIX;
  67. v = (v < 0) ? 0 : (v > 255) ? 255 : v;
  68. return (v * (rgb_sampling_size - 1)) / 255;
  69. }
  70. #undef YUV_FIX
  71. static int SharpYuvConvertToYuvSharpnessIndex(
  72. int r, int g, int b, const SharpYuvConversionMatrix* matrix,
  73. int precomputed_scores_table_sampling) {
  74. const int y = SharpYuvConvertValueToSampledIdx(
  75. matrix->rgb_to_y[0] * r + matrix->rgb_to_y[1] * g +
  76. matrix->rgb_to_y[2] * b + matrix->rgb_to_y[3],
  77. precomputed_scores_table_sampling);
  78. const int u = SharpYuvConvertValueToSampledIdx(
  79. matrix->rgb_to_u[0] * r + matrix->rgb_to_u[1] * g +
  80. matrix->rgb_to_u[2] * b + matrix->rgb_to_u[3],
  81. precomputed_scores_table_sampling);
  82. const int v = SharpYuvConvertValueToSampledIdx(
  83. matrix->rgb_to_v[0] * r + matrix->rgb_to_v[1] * g +
  84. matrix->rgb_to_v[2] * b + matrix->rgb_to_v[3],
  85. precomputed_scores_table_sampling);
  86. return y + u * precomputed_scores_table_sampling +
  87. v * precomputed_scores_table_sampling *
  88. precomputed_scores_table_sampling;
  89. }
  90. static void SharpYuvRowToYuvSharpnessIndex_C(
  91. const uint8_t* r_ptr, const uint8_t* g_ptr, const uint8_t* b_ptr,
  92. int rgb_step, int rgb_bit_depth, int width, uint16_t* dst,
  93. const SharpYuvConversionMatrix* matrix,
  94. int precomputed_scores_table_sampling) {
  95. int i;
  96. assert(rgb_bit_depth == 8);
  97. (void)rgb_bit_depth; // Unused for now.
  98. for (i = 0; i < width;
  99. ++i, r_ptr += rgb_step, g_ptr += rgb_step, b_ptr += rgb_step) {
  100. dst[i] =
  101. SharpYuvConvertToYuvSharpnessIndex(r_ptr[0], g_ptr[0], b_ptr[0], matrix,
  102. precomputed_scores_table_sampling);
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
  107. uint16_t* dst, int len, int bit_depth);
  108. void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
  109. int len);
  110. void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
  111. const uint16_t* best_y, uint16_t* out, int bit_depth);
  112. void (*SharpYuvRowToYuvSharpnessIndex)(const uint8_t* r_ptr,
  113. const uint8_t* g_ptr,
  114. const uint8_t* b_ptr, int rgb_step,
  115. int rgb_bit_depth, int width,
  116. uint16_t* dst,
  117. const SharpYuvConversionMatrix* matrix,
  118. int precomputed_scores_table_sampling);
  119. extern VP8CPUInfo SharpYuvGetCPUInfo;
  120. extern void InitSharpYuvSSE2(void);
  121. extern void InitSharpYuvNEON(void);
  122. void SharpYuvInitDsp(void) {
  123. #if !WEBP_NEON_OMIT_C_CODE
  124. SharpYuvUpdateY = SharpYuvUpdateY_C;
  125. SharpYuvUpdateRGB = SharpYuvUpdateRGB_C;
  126. SharpYuvFilterRow = SharpYuvFilterRow_C;
  127. #endif
  128. // There is only a C version for now so always include it.
  129. SharpYuvRowToYuvSharpnessIndex = SharpYuvRowToYuvSharpnessIndex_C;
  130. if (SharpYuvGetCPUInfo != NULL) {
  131. #if defined(WEBP_HAVE_SSE2)
  132. if (SharpYuvGetCPUInfo(kSSE2)) {
  133. InitSharpYuvSSE2();
  134. }
  135. #endif // WEBP_HAVE_SSE2
  136. }
  137. #if defined(WEBP_HAVE_NEON)
  138. if (WEBP_NEON_OMIT_C_CODE ||
  139. (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) {
  140. InitSharpYuvNEON();
  141. }
  142. #endif // WEBP_HAVE_NEON
  143. assert(SharpYuvUpdateY != NULL);
  144. assert(SharpYuvUpdateRGB != NULL);
  145. assert(SharpYuvFilterRow != NULL);
  146. }