sharpyuv.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 0
  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 y, u and v values are divided by 1<<16 and rounded.
  66. typedef struct {
  67. int rgb_to_y[4];
  68. int rgb_to_u[4];
  69. int rgb_to_v[4];
  70. } SharpYuvConversionMatrix;
  71. typedef struct SharpYuvOptions SharpYuvOptions;
  72. // Enums for transfer functions, as defined in H.273,
  73. // https://www.itu.int/rec/T-REC-H.273-202107-I/en
  74. typedef enum SharpYuvTransferFunctionType {
  75. // 0 is reserved
  76. kSharpYuvTransferFunctionBt709 = 1,
  77. // 2 is unspecified
  78. // 3 is reserved
  79. kSharpYuvTransferFunctionBt470M = 4,
  80. kSharpYuvTransferFunctionBt470Bg = 5,
  81. kSharpYuvTransferFunctionBt601 = 6,
  82. kSharpYuvTransferFunctionSmpte240 = 7,
  83. kSharpYuvTransferFunctionLinear = 8,
  84. kSharpYuvTransferFunctionLog100 = 9,
  85. kSharpYuvTransferFunctionLog100_Sqrt10 = 10,
  86. kSharpYuvTransferFunctionIec61966 = 11,
  87. kSharpYuvTransferFunctionBt1361 = 12,
  88. kSharpYuvTransferFunctionSrgb = 13,
  89. kSharpYuvTransferFunctionBt2020_10Bit = 14,
  90. kSharpYuvTransferFunctionBt2020_12Bit = 15,
  91. kSharpYuvTransferFunctionSmpte2084 = 16, // PQ
  92. kSharpYuvTransferFunctionSmpte428 = 17,
  93. kSharpYuvTransferFunctionHlg = 18,
  94. kSharpYuvTransferFunctionNum
  95. } SharpYuvTransferFunctionType;
  96. // Converts RGB to YUV420 using a downsampling algorithm that minimizes
  97. // artefacts caused by chroma subsampling.
  98. // This is slower than standard downsampling (averaging of 4 UV values).
  99. // Assumes that the image will be upsampled using a bilinear filter. If nearest
  100. // neighbor is used instead, the upsampled image might look worse than with
  101. // standard downsampling.
  102. // r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
  103. // to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
  104. // rgb_step: distance in bytes between two horizontally adjacent pixels on the
  105. // r, g and b channels. If rgb_bit_depth is > 8, it should be a
  106. // multiple of 2.
  107. // rgb_stride: distance in bytes between two vertically adjacent pixels on the
  108. // r, g, and b channels. If rgb_bit_depth is > 8, it should be a
  109. // multiple of 2.
  110. // rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.
  111. // Note: 16 bit input is truncated to 14 bits before conversion to yuv.
  112. // yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.
  113. // y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should
  114. // point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers
  115. // otherwise.
  116. // y_stride, u_stride, v_stride: distance in bytes between two vertically
  117. // adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they
  118. // should be multiples of 2.
  119. // width, height: width and height of the image in pixels
  120. // This function calls SharpYuvConvertWithOptions with a default transfer
  121. // function of kSharpYuvTransferFunctionSrgb.
  122. SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr,
  123. const void* b_ptr, int rgb_step,
  124. int rgb_stride, int rgb_bit_depth,
  125. void* y_ptr, int y_stride, void* u_ptr,
  126. int u_stride, void* v_ptr, int v_stride,
  127. int yuv_bit_depth, int width, int height,
  128. const SharpYuvConversionMatrix* yuv_matrix);
  129. struct SharpYuvOptions {
  130. // This matrix cannot be NULL and can be initialized by
  131. // SharpYuvComputeConversionMatrix.
  132. const SharpYuvConversionMatrix* yuv_matrix;
  133. SharpYuvTransferFunctionType transfer_type;
  134. };
  135. // Internal, version-checked, entry point
  136. SHARPYUV_EXTERN int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix*,
  137. SharpYuvOptions*, int);
  138. // Should always be called, to initialize a fresh SharpYuvOptions
  139. // structure before modification. SharpYuvOptionsInit() must have succeeded
  140. // before using the 'options' object.
  141. static SHARPYUV_INLINE int SharpYuvOptionsInit(
  142. const SharpYuvConversionMatrix* yuv_matrix, SharpYuvOptions* options) {
  143. return SharpYuvOptionsInitInternal(yuv_matrix, options, SHARPYUV_VERSION);
  144. }
  145. SHARPYUV_EXTERN int SharpYuvConvertWithOptions(
  146. const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step,
  147. int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, void* u_ptr,
  148. int u_stride, void* v_ptr, int v_stride, int yuv_bit_depth, int width,
  149. int height, const SharpYuvOptions* options);
  150. // TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
  151. // support (it's rarely used in practice, especially for images).
  152. #ifdef __cplusplus
  153. } // extern "C"
  154. #endif
  155. #endif // WEBP_SHARPYUV_SHARPYUV_H_