xref: /aosp_15_r20/external/mesa3d/src/amd/vpelib/src/core/inc/color.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* Copyright 2022 Advanced Micro Devices, Inc.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19  * OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Authors: AMD
22  *
23  */
24 
25 #pragma once
26 
27 #include "vpe_types.h"
28 #include "fixed31_32.h"
29 #include "hw_shared.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #define SDR_VIDEO_WHITE_POINT  100 // nits
36 #define SDR_WHITE_POINT        80  // nits
37 #define HDR_PEAK_WHITE         10000
38 #define CCCS_NORM              HDR_PEAK_WHITE/SDR_WHITE_POINT
39 #define STUDIO_RANGE_FOOT_ROOM_10_BIT  vpe_fixpt_from_fraction(64, 1023)
40 #define STUDIO_RANGE_SCALE_10_BIT      vpe_fixpt_from_fraction(940 - 64, 1023)
41 #define STUDIO_RANGE_FOOT_ROOM_8_BIT  vpe_fixpt_from_fraction(16, 255)
42 #define STUDIO_RANGE_SCALE_8_BIT      vpe_fixpt_from_fraction(235 - 16, 255)
43 
44 struct vpe_priv;
45 struct stream_ctx;
46 
47 enum color_depth {
48     COLOR_DEPTH_UNDEFINED,
49     COLOR_DEPTH_666,
50     COLOR_DEPTH_888,
51     COLOR_DEPTH_101010,
52     COLOR_DEPTH_121212,
53     COLOR_DEPTH_141414,
54     COLOR_DEPTH_161616,
55     COLOR_DEPTH_999,
56     COLOR_DEPTH_111111,
57     COLOR_DEPTH_COUNT
58 };
59 
60  enum color_transfer_func {
61     TRANSFER_FUNC_UNKNOWN,
62     TRANSFER_FUNC_SRGB,
63     TRANSFER_FUNC_BT709,
64     TRANSFER_FUNC_BT1886,
65     TRANSFER_FUNC_PQ2084,
66     TRANSFER_FUNC_LINEAR,
67     TRANSFER_FUNC_NORMALIZED_PQ,
68     TRANSFER_FUNC_HLG
69 };
70 
71 enum dither_option {
72     DITHER_OPTION_DEFAULT,
73     DITHER_OPTION_DISABLE,
74     DITHER_OPTION_FM6,
75     DITHER_OPTION_FM8,
76     DITHER_OPTION_FM10,
77     DITHER_OPTION_SPATIAL6_FRAME_RANDOM,
78     DITHER_OPTION_SPATIAL8_FRAME_RANDOM,
79     DITHER_OPTION_SPATIAL10_FRAME_RANDOM,
80     DITHER_OPTION_SPATIAL6,
81     DITHER_OPTION_SPATIAL8,
82     DITHER_OPTION_SPATIAL10,
83     DITHER_OPTION_TRUN6,
84     DITHER_OPTION_TRUN8,
85     DITHER_OPTION_TRUN10,
86     DITHER_OPTION_TRUN10_SPATIAL8,
87     DITHER_OPTION_TRUN10_SPATIAL6,
88     DITHER_OPTION_TRUN10_FM8,
89     DITHER_OPTION_TRUN10_FM6,
90     DITHER_OPTION_TRUN10_SPATIAL8_FM6,
91     DITHER_OPTION_SPATIAL10_FM8,
92     DITHER_OPTION_SPATIAL10_FM6,
93     DITHER_OPTION_TRUN8_SPATIAL6,
94     DITHER_OPTION_TRUN8_FM6,
95     DITHER_OPTION_SPATIAL8_FM6,
96     DITHER_OPTION_MAX = DITHER_OPTION_SPATIAL8_FM6,
97     DITHER_OPTION_INVALID
98 };
99 
100 enum color_space {
101     COLOR_SPACE_UNKNOWN,
102     COLOR_SPACE_SRGB,
103     COLOR_SPACE_SRGB_LIMITED,
104     COLOR_SPACE_MSREF_SCRGB,
105     COLOR_SPACE_YCBCR601,
106     COLOR_SPACE_RGB601,
107     COLOR_SPACE_RGB601_LIMITED,
108     COLOR_SPACE_YCBCR709,
109     COLOR_SPACE_YCBCR_JFIF,
110     COLOR_SPACE_RGB_JFIF,
111     COLOR_SPACE_YCBCR601_LIMITED,
112     COLOR_SPACE_YCBCR709_LIMITED,
113     COLOR_SPACE_2020_RGB_FULLRANGE,
114     COLOR_SPACE_2020_RGB_LIMITEDRANGE,
115     COLOR_SPACE_2020_YCBCR,
116     COLOR_SPACE_2020_YCBCR_LIMITED,
117     COLOR_SPACE_MAX,
118 };
119 
120 enum transfer_func_type {
121     TF_TYPE_PREDEFINED,
122     TF_TYPE_DISTRIBUTED_POINTS,
123     TF_TYPE_BYPASS,
124     TF_TYPE_HWPWL
125 };
126 
127 enum cm_type {
128     CM_DEGAM,
129     CM_REGAM,
130 };
131 
132 enum {
133     TRANSFER_FUNC_POINTS = 1025
134 };
135 
136 typedef struct fixed31_32 white_point_gain;
137 
138 struct transfer_func_distributed_points {
139     struct fixed31_32 red[TRANSFER_FUNC_POINTS];
140     struct fixed31_32 green[TRANSFER_FUNC_POINTS];
141     struct fixed31_32 blue[TRANSFER_FUNC_POINTS];
142 
143     uint16_t end_exponent;
144     uint16_t x_point_at_y1_red;
145     uint16_t x_point_at_y1_green;
146     uint16_t x_point_at_y1_blue;
147 };
148 
149 struct transfer_func {
150     enum transfer_func_type  type;
151     enum color_transfer_func tf;
152     enum cm_type             cm_gamma_type;
153     struct fixed31_32        start_base; //Used to clamp curve start
154 
155     /* FP16 1.0 reference level in nits, default is 80 nits, only for PQ*/
156     uint32_t sdr_ref_white_level;
157     union {
158         struct pwl_params                       pwl;
159         struct transfer_func_distributed_points tf_pts;
160     };
161 };
162 
163 enum color_white_point_type {
164     color_white_point_type_unknown,
165     color_white_point_type_5000k_horizon,
166     color_white_point_type_6500k_noon,
167     color_white_point_type_7500k_north_sky,
168     color_white_point_type_9300k,
169     color_white_point_type_custom_coordinates
170 };
171 
172 struct color_space_coordinates {
173     unsigned int redX;
174     unsigned int redY;
175     unsigned int greenX;
176     unsigned int greenY;
177     unsigned int blueX;
178     unsigned int blueY;
179     unsigned int whiteX;
180     unsigned int whiteY;
181 };
182 
183 enum predefined_gamut_type {
184     gamut_type_bt709,
185     gamut_type_bt601,
186     gamut_type_adobe_rgb,
187     gamut_type_srgb,
188     gamut_type_bt2020,
189     gamut_type_dcip3,
190     gamut_type_unknown,
191 };
192 
193 enum predefined_white_point_type {
194     white_point_type_5000k_horizon,
195     white_point_type_6500k_noon,
196     white_point_type_7500k_north_sky,
197     white_point_type_9300k,
198     white_point_type_unknown,
199 };
200 
201 struct colorspace_transform {
202     struct fixed31_32 matrix[12];
203     bool              enable_remap;
204 };
205 
206 struct color_gamut_data {
207     enum color_space               color_space;
208     enum color_white_point_type    white_point;
209     struct color_space_coordinates gamut;
210 };
211 
212 union vpe_3dlut_state {
213     struct {
214         uint32_t initialized : 1; /*if 3dlut is went through color module for initialization */
215         uint32_t reserved    : 15;
216     } bits;
217     uint32_t raw;
218 };
219 
220 struct vpe_3dlut {
221     // struct kref refcount;
222     struct tetrahedral_params lut_3d;
223     struct fixed31_32         hdr_multiplier;
224     union vpe_3dlut_state     state;
225 };
226 
227 enum vpe_status vpe_color_update_color_space_and_tf(
228     struct vpe_priv *vpe_priv, const struct vpe_build_param *param);
229 
230 enum vpe_status vpe_color_update_movable_cm(
231     struct vpe_priv *vpe_priv, const struct vpe_build_param *param);
232 
233 void vpe_color_get_color_space_and_tf(
234     const struct vpe_color_space *vcs, enum color_space *cs, enum color_transfer_func *tf);
235 
236 bool vpe_use_csc_adjust(const struct vpe_color_adjust *adjustments);
237 
238 bool vpe_is_rgb_equal(const struct pwl_result_data *rgb, uint32_t num);
239 
240 bool vpe_is_HDR(enum color_transfer_func tf);
241 
242 void vpe_convert_full_range_color_enum(enum color_space *cs);
243 
244 enum vpe_status vpe_color_update_whitepoint(
245     const struct vpe_priv *vpe_priv, const struct vpe_build_param *param);
246 
247 enum vpe_status vpe_color_tm_update_hdr_mult(uint16_t shaper_in_exp_max, uint32_t peak_white,
248     struct fixed31_32 *hdr_multiplier, bool enable_3dlut);
249 
250 enum vpe_status vpe_color_update_shaper(
251     uint16_t shaper_in_exp_max, struct transfer_func *shaper_func, bool enable_3dlut);
252 
253 enum vpe_status vpe_color_build_tm_cs(const struct vpe_tonemap_params *tm_params,
254     struct vpe_surface_info surface_info, struct vpe_color_space *vcs);
255 
256 #ifdef __cplusplus
257 }
258 #endif
259