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