sharpyuv.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Sharp RGB to YUV conversion.
  11. #ifndef WEBP_SHARPYUV_SHARPYUV_H_
  12. #define WEBP_SHARPYUV_SHARPYUV_H_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifndef SHARPYUV_EXTERN
  17. #ifdef WEBP_EXTERN
  18. #define SHARPYUV_EXTERN WEBP_EXTERN
  19. #else
  20. // This explicitly marks library functions and allows for changing the
  21. // signature for e.g., Windows DLL builds.
  22. #if defined(_WIN32) && defined(WEBP_DLL)
  23. #define SHARPYUV_EXTERN __declspec(dllexport)
  24. #elif defined(__GNUC__) && __GNUC__ >= 4
  25. #define SHARPYUV_EXTERN extern __attribute__((visibility("default")))
  26. #else
  27. #define SHARPYUV_EXTERN extern
  28. #endif /* defined(_WIN32) && defined(WEBP_DLL) */
  29. #endif /* WEBP_EXTERN */
  30. #endif /* SHARPYUV_EXTERN */
  31. #ifndef SHARPYUV_INLINE
  32. #ifdef WEBP_INLINE
  33. #define SHARPYUV_INLINE WEBP_INLINE
  34. #else
  35. #ifndef _MSC_VER
  36. #if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
  37. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
  38. #define SHARPYUV_INLINE inline
  39. #else
  40. #define SHARPYUV_INLINE
  41. #endif
  42. #else
  43. #define SHARPYUV_INLINE __forceinline
  44. #endif /* _MSC_VER */
  45. #endif /* WEBP_INLINE */
  46. #endif /* SHARPYUV_INLINE */
  47. // SharpYUV API version following the convention from semver.org
  48. #define SHARPYUV_VERSION_MAJOR 0
  49. #define SHARPYUV_VERSION_MINOR 4
  50. #define SHARPYUV_VERSION_PATCH 1
  51. // Version as a uint32_t. The major number is the high 8 bits.
  52. // The minor number is the middle 8 bits. The patch number is the low 16 bits.
  53. #define SHARPYUV_MAKE_VERSION(MAJOR, MINOR, PATCH) \
  54. (((MAJOR) << 24) | ((MINOR) << 16) | (PATCH))
  55. #define SHARPYUV_VERSION \
  56. SHARPYUV_MAKE_VERSION(SHARPYUV_VERSION_MAJOR, SHARPYUV_VERSION_MINOR, \
  57. SHARPYUV_VERSION_PATCH)
  58. // Returns the library's version number, packed in hexadecimal. See
  59. // SHARPYUV_VERSION.
  60. SHARPYUV_EXTERN int SharpYuvGetVersion(void);
  61. // RGB to YUV conversion matrix, in 16 bit fixed point.
  62. // y_ = rgb_to_y[0] * r + rgb_to_y[1] * g + rgb_to_y[2] * b + rgb_to_y[3]
  63. // u_ = rgb_to_u[0] * r + rgb_to_u[1] * g + rgb_to_u[2] * b + rgb_to_u[3]
  64. // v_ = rgb_to_v[0] * r + rgb_to_v[1] * g + rgb_to_v[2] * b + rgb_to_v[3]
  65. // Then the values are divided by 1<<16 and rounded.
  66. // y = (y_ + (1 << 15)) >> 16
  67. // u = (u_ + (1 << 15)) >> 16
  68. // v = (v_ + (1 << 15)) >> 16
  69. //
  70. // Typically, the offset values rgb_to_y[3], rgb_to_u[3] and rgb_to_v[3] depend
  71. // on the input's bit depth, e.g., rgb_to_u[3] = 1 << (rgb_bit_depth - 1 + 16).
  72. // See also sharpyuv_csp.h to get a predefined matrix or generate a matrix.
  73. typedef struct {
  74. int rgb_to_y[4];
  75. int rgb_to_u[4];
  76. int rgb_to_v[4];
  77. } SharpYuvConversionMatrix;
  78. typedef struct SharpYuvOptions SharpYuvOptions;
  79. // Enums for transfer functions, as defined in H.273,
  80. // https://www.itu.int/rec/T-REC-H.273-202107-I/en
  81. typedef enum SharpYuvTransferFunctionType {
  82. // 0 is reserved
  83. kSharpYuvTransferFunctionBt709 = 1,
  84. // 2 is unspecified
  85. // 3 is reserved
  86. kSharpYuvTransferFunctionBt470M = 4,
  87. kSharpYuvTransferFunctionBt470Bg = 5,
  88. kSharpYuvTransferFunctionBt601 = 6,
  89. kSharpYuvTransferFunctionSmpte240 = 7,
  90. kSharpYuvTransferFunctionLinear = 8,
  91. kSharpYuvTransferFunctionLog100 = 9,
  92. kSharpYuvTransferFunctionLog100_Sqrt10 = 10,
  93. kSharpYuvTransferFunctionIec61966 = 11,
  94. kSharpYuvTransferFunctionBt1361 = 12,
  95. kSharpYuvTransferFunctionSrgb = 13,
  96. kSharpYuvTransferFunctionBt2020_10Bit = 14,
  97. kSharpYuvTransferFunctionBt2020_12Bit = 15,
  98. kSharpYuvTransferFunctionSmpte2084 = 16, // PQ
  99. kSharpYuvTransferFunctionSmpte428 = 17,
  100. kSharpYuvTransferFunctionHlg = 18,
  101. kSharpYuvTransferFunctionNum
  102. } SharpYuvTransferFunctionType;
  103. // Converts RGB to YUV420 using a downsampling algorithm that minimizes
  104. // artefacts caused by chroma subsampling.
  105. // This is slower than standard downsampling (averaging of 4 UV values).
  106. // Assumes that the image will be upsampled using a bilinear filter. If nearest
  107. // neighbor is used instead, the upsampled image might look worse than with
  108. // standard downsampling.
  109. // r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
  110. // to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
  111. // rgb_step: distance in bytes between two horizontally adjacent pixels on the
  112. // r, g and b channels. If rgb_bit_depth is > 8, it should be a
  113. // multiple of 2.
  114. // rgb_stride: distance in bytes between two vertically adjacent pixels on the
  115. // r, g, and b channels. If rgb_bit_depth is > 8, it should be a
  116. // multiple of 2.
  117. // rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.
  118. // Note: 16 bit input is truncated to 14 bits before conversion to yuv.
  119. // yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.
  120. // y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should
  121. // point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers
  122. // otherwise.
  123. // y_stride, u_stride, v_stride: distance in bytes between two vertically
  124. // adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they
  125. // should be multiples of 2.
  126. // width, height: width and height of the image in pixels
  127. // yuv_matrix: RGB to YUV conversion matrix. The matrix values typically
  128. // depend on the input's rgb_bit_depth.
  129. // This function calls SharpYuvConvertWithOptions with a default transfer
  130. // function of kSharpYuvTransferFunctionSrgb.
  131. SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr,
  132. const void* b_ptr, int rgb_step,
  133. int rgb_stride, int rgb_bit_depth,
  134. void* y_ptr, int y_stride, void* u_ptr,
  135. int u_stride, void* v_ptr, int v_stride,
  136. int yuv_bit_depth, int width, int height,
  137. const SharpYuvConversionMatrix* yuv_matrix);
  138. struct SharpYuvOptions {
  139. // This matrix cannot be NULL and can be initialized by
  140. // SharpYuvComputeConversionMatrix.
  141. const SharpYuvConversionMatrix* yuv_matrix;
  142. SharpYuvTransferFunctionType transfer_type;
  143. };
  144. // Internal, version-checked, entry point
  145. SHARPYUV_EXTERN int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix*,
  146. SharpYuvOptions*, int);
  147. // Should always be called, to initialize a fresh SharpYuvOptions
  148. // structure before modification. SharpYuvOptionsInit() must have succeeded
  149. // before using the 'options' object.
  150. static SHARPYUV_INLINE int SharpYuvOptionsInit(
  151. const SharpYuvConversionMatrix* yuv_matrix, SharpYuvOptions* options) {
  152. return SharpYuvOptionsInitInternal(yuv_matrix, options, SHARPYUV_VERSION);
  153. }
  154. SHARPYUV_EXTERN int SharpYuvConvertWithOptions(
  155. const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step,
  156. int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, void* u_ptr,
  157. int u_stride, void* v_ptr, int v_stride, int yuv_bit_depth, int width,
  158. int height, const SharpYuvOptions* options);
  159. // TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
  160. // support (it's rarely used in practice, especially for images).
  161. #ifdef __cplusplus
  162. } // extern "C"
  163. #endif
  164. #endif // WEBP_SHARPYUV_SHARPYUV_H_