sharpyuv_dsp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (pascal.massimino@gmail.com)
  13. #include "sharpyuv/sharpyuv_dsp.h"
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include "sharpyuv/sharpyuv_cpu.h"
  17. #include "src/webp/types.h"
  18. //-----------------------------------------------------------------------------
  19. #if !WEBP_NEON_OMIT_C_CODE
  20. static uint16_t clip(int v, int max) {
  21. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  22. }
  23. static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src,
  24. uint16_t* dst, int len, int bit_depth) {
  25. uint64_t diff = 0;
  26. int i;
  27. const int max_y = (1 << bit_depth) - 1;
  28. for (i = 0; i < len; ++i) {
  29. const int diff_y = ref[i] - src[i];
  30. const int new_y = (int)dst[i] + diff_y;
  31. dst[i] = clip(new_y, max_y);
  32. diff += (uint64_t)abs(diff_y);
  33. }
  34. return diff;
  35. }
  36. static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src,
  37. int16_t* dst, int len) {
  38. int i;
  39. for (i = 0; i < len; ++i) {
  40. const int diff_uv = ref[i] - src[i];
  41. dst[i] += diff_uv;
  42. }
  43. }
  44. static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
  45. const uint16_t* best_y, uint16_t* out,
  46. int bit_depth) {
  47. int i;
  48. const int max_y = (1 << bit_depth) - 1;
  49. for (i = 0; i < len; ++i, ++A, ++B) {
  50. const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
  51. const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
  52. out[2 * i + 0] = clip(best_y[2 * i + 0] + v0, max_y);
  53. out[2 * i + 1] = clip(best_y[2 * i + 1] + v1, max_y);
  54. }
  55. }
  56. #endif // !WEBP_NEON_OMIT_C_CODE
  57. //-----------------------------------------------------------------------------
  58. uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
  59. uint16_t* dst, int len, int bit_depth);
  60. void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
  61. int len);
  62. void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
  63. const uint16_t* best_y, uint16_t* out, int bit_depth);
  64. extern VP8CPUInfo SharpYuvGetCPUInfo;
  65. extern void InitSharpYuvSSE2(void);
  66. extern void InitSharpYuvNEON(void);
  67. void SharpYuvInitDsp(void) {
  68. #if !WEBP_NEON_OMIT_C_CODE
  69. SharpYuvUpdateY = SharpYuvUpdateY_C;
  70. SharpYuvUpdateRGB = SharpYuvUpdateRGB_C;
  71. SharpYuvFilterRow = SharpYuvFilterRow_C;
  72. #endif
  73. if (SharpYuvGetCPUInfo != NULL) {
  74. #if defined(WEBP_HAVE_SSE2)
  75. if (SharpYuvGetCPUInfo(kSSE2)) {
  76. InitSharpYuvSSE2();
  77. }
  78. #endif // WEBP_HAVE_SSE2
  79. }
  80. #if defined(WEBP_HAVE_NEON)
  81. if (WEBP_NEON_OMIT_C_CODE ||
  82. (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) {
  83. InitSharpYuvNEON();
  84. }
  85. #endif // WEBP_HAVE_NEON
  86. assert(SharpYuvUpdateY != NULL);
  87. assert(SharpYuvUpdateRGB != NULL);
  88. assert(SharpYuvFilterRow != NULL);
  89. }