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