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. //-----------------------------------------------------------------------------
  18. #if !WEBP_NEON_OMIT_C_CODE
  19. static uint16_t clip(int v, int max) {
  20. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  21. }
  22. static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src,
  23. uint16_t* dst, int len, int bit_depth) {
  24. uint64_t diff = 0;
  25. int i;
  26. const int max_y = (1 << bit_depth) - 1;
  27. for (i = 0; i < len; ++i) {
  28. const int diff_y = ref[i] - src[i];
  29. const int new_y = (int)dst[i] + diff_y;
  30. dst[i] = clip(new_y, max_y);
  31. diff += (uint64_t)abs(diff_y);
  32. }
  33. return diff;
  34. }
  35. static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src,
  36. int16_t* dst, int len) {
  37. int i;
  38. for (i = 0; i < len; ++i) {
  39. const int diff_uv = ref[i] - src[i];
  40. dst[i] += diff_uv;
  41. }
  42. }
  43. static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
  44. const uint16_t* best_y, uint16_t* out,
  45. int bit_depth) {
  46. int i;
  47. const int max_y = (1 << bit_depth) - 1;
  48. for (i = 0; i < len; ++i, ++A, ++B) {
  49. const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
  50. const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
  51. out[2 * i + 0] = clip(best_y[2 * i + 0] + v0, max_y);
  52. out[2 * i + 1] = clip(best_y[2 * i + 1] + v1, max_y);
  53. }
  54. }
  55. #endif // !WEBP_NEON_OMIT_C_CODE
  56. //-----------------------------------------------------------------------------
  57. uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
  58. uint16_t* dst, int len, int bit_depth);
  59. void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
  60. int len);
  61. void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
  62. const uint16_t* best_y, uint16_t* out,
  63. 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. }