Coverage Report

Created: 2023-09-25 06:23

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