2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Library <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
30 #include "video-color.h"
32 #ifndef GST_DISABLE_GST_DEBUG
33 #define GST_CAT_DEFAULT ensure_debug_category()
34 static GstDebugCategory *
35 ensure_debug_category (void)
37 static gsize cat_gonce = 0;
39 if (g_once_init_enter (&cat_gonce)) {
42 cat_done = (gsize) _gst_debug_category_new ("video-color", 0,
43 "video-color object");
45 g_once_init_leave (&cat_gonce, cat_done);
48 return (GstDebugCategory *) cat_gonce;
51 #define ensure_debug_category() /* NOOP */
52 #endif /* GST_DISABLE_GST_DEBUG */
57 GstVideoColorimetry color;
60 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
61 { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
62 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
64 #define GST_VIDEO_COLORIMETRY_NONAME NULL
66 #define DEFAULT_YUV_SD 0
67 #define DEFAULT_YUV_HD 1
69 #define DEFAULT_YUV_UHD 4
70 #define DEFAULT_GRAY 5
71 #define DEFAULT_UNKNOWN 6
73 static const ColorimetryInfo colorimetry[] = {
74 MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, SMPTE170M),
75 MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
76 MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
77 MAKE_COLORIMETRY (SRGB, _0_255, RGB, SRGB, BT709),
78 MAKE_COLORIMETRY (BT2020, _16_235, BT2020, BT2020_12, BT2020),
79 MAKE_COLORIMETRY (NONAME, _0_255, BT601, UNKNOWN, UNKNOWN),
80 MAKE_COLORIMETRY (NONAME, _UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN),
83 static const ColorimetryInfo *
84 gst_video_get_colorimetry (const gchar * s)
88 for (i = 0; colorimetry[i].name; i++) {
89 if (g_str_equal (colorimetry[i].name, s))
90 return &colorimetry[i];
95 #define CI_IS_EQUAL(ci,i) (((ci)->range == (i)->range) && \
96 ((ci)->matrix == (i)->matrix) && \
97 ((ci)->transfer == (i)->transfer) && \
98 ((ci)->primaries == (i)->primaries))
100 #define IS_EQUAL(ci,i) CI_IS_EQUAL(&(ci)->color, (i))
102 #define IS_UNKNOWN(ci) (IS_EQUAL (&colorimetry[DEFAULT_UNKNOWN], ci))
105 * gst_video_colorimetry_from_string:
106 * @cinfo: a #GstVideoColorimetry
107 * @color: a colorimetry string
109 * Parse the colorimetry string and update @cinfo with the parsed
112 * Returns: %TRUE if @color points to valid colorimetry info.
115 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
118 const ColorimetryInfo *ci;
119 gboolean res = FALSE;
121 if ((ci = gst_video_get_colorimetry (color))) {
127 if (sscanf (color, "%d:%d:%d:%d", &r, &m, &t, &p) == 4) {
131 cinfo->primaries = p;
139 * gst_video_colorimetry_to_string:
140 * @cinfo: a #GstVideoColorimetry
142 * Make a string representation of @cinfo.
144 * Returns: a string representation of @cinfo.
147 gst_video_colorimetry_to_string (const GstVideoColorimetry * cinfo)
151 for (i = 0; colorimetry[i].name; i++) {
152 if (IS_EQUAL (&colorimetry[i], cinfo)) {
153 return g_strdup (colorimetry[i].name);
156 if (!IS_UNKNOWN (cinfo)) {
157 return g_strdup_printf ("%d:%d:%d:%d", cinfo->range, cinfo->matrix,
158 cinfo->transfer, cinfo->primaries);
164 * gst_video_colorimetry_matches:
165 * @cinfo: a #GstVideoInfo
166 * @color: a colorimetry string
168 * Check if the colorimetry information in @info matches that of the
171 * Returns: %TRUE if @color conveys the same colorimetry info as the color
172 * information in @info.
175 gst_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
178 const ColorimetryInfo *ci;
180 if ((ci = gst_video_get_colorimetry (color)))
181 return IS_EQUAL (ci, cinfo);
187 * gst_video_color_range_offsets:
188 * @range: a #GstVideoColorRange
189 * @info: a #GstVideoFormatInfo
190 * @offset: (out) (array fixed-size=4): output offsets
191 * @scale: (out) (array fixed-size=4): output scale
193 * Compute the offset and scale values for each component of @info. For each
194 * component, (c[i] - offset[i]) / scale[i] will scale the component c[i] to the
195 * range [0.0 .. 1.0].
197 * The reverse operation (c[i] * scale[i]) + offset[i] can be used to convert
198 * the component values in range [0.0 .. 1.0] back to their representation in
202 gst_video_color_range_offsets (GstVideoColorRange range,
203 const GstVideoFormatInfo * info, gint offset[GST_VIDEO_MAX_COMPONENTS],
204 gint scale[GST_VIDEO_MAX_COMPONENTS])
208 yuv = GST_VIDEO_FORMAT_INFO_IS_YUV (info);
212 case GST_VIDEO_COLOR_RANGE_0_255:
215 offset[1] = 1 << (info->depth[1] - 1);
216 offset[2] = 1 << (info->depth[2] - 1);
221 scale[0] = (1 << info->depth[0]) - 1;
222 scale[1] = (1 << info->depth[1]) - 1;
223 scale[2] = (1 << info->depth[2]) - 1;
225 case GST_VIDEO_COLOR_RANGE_16_235:
226 offset[0] = 1 << (info->depth[0] - 4);
227 scale[0] = 219 << (info->depth[0] - 8);
229 offset[1] = 1 << (info->depth[1] - 1);
230 offset[2] = 1 << (info->depth[2] - 1);
231 scale[1] = 224 << (info->depth[1] - 8);
232 scale[2] = 224 << (info->depth[2] - 8);
234 offset[1] = 1 << (info->depth[1] - 4);
235 offset[2] = 1 << (info->depth[2] - 4);
236 scale[1] = 219 << (info->depth[1] - 8);
237 scale[2] = 219 << (info->depth[2] - 8);
241 /* alpha channel is always full range */
243 scale[3] = (1 << info->depth[3]) - 1;
245 GST_DEBUG ("scale: %d %d %d %d", scale[0], scale[1], scale[2], scale[3]);
246 GST_DEBUG ("offset: %d %d %d %d", offset[0], offset[1], offset[2], offset[3]);
250 * gst_video_colorimetry_is_equal:
251 * @cinfo: a #GstVideoColorimetry
252 * @other: another #GstVideoColorimetry
254 * Compare the 2 colorimetry sets for equality
256 * Returns: %TRUE if @cinfo and @other are equal.
261 gst_video_colorimetry_is_equal (const GstVideoColorimetry * cinfo,
262 const GstVideoColorimetry * other)
264 g_return_val_if_fail (cinfo != NULL, FALSE);
265 g_return_val_if_fail (other != NULL, FALSE);
267 return CI_IS_EQUAL (cinfo, other);
270 #define WP_C 0.31006, 0.31616
271 #define WP_D65 0.31271, 0.32902
273 static const GstVideoColorPrimariesInfo color_primaries[] = {
274 {GST_VIDEO_COLOR_PRIMARIES_UNKNOWN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
275 {GST_VIDEO_COLOR_PRIMARIES_BT709, WP_D65, 0.64, 0.33, 0.30, 0.60, 0.15, 0.06},
276 {GST_VIDEO_COLOR_PRIMARIES_BT470M, WP_C, 0.67, 0.33, 0.21, 0.71, 0.14, 0.08},
277 {GST_VIDEO_COLOR_PRIMARIES_BT470BG, WP_D65, 0.64, 0.33, 0.29, 0.60, 0.15,
279 {GST_VIDEO_COLOR_PRIMARIES_SMPTE170M, WP_D65, 0.63, 0.34, 0.31, 0.595, 0.155,
281 {GST_VIDEO_COLOR_PRIMARIES_SMPTE240M, WP_D65, 0.63, 0.34, 0.31, 0.595, 0.155,
283 {GST_VIDEO_COLOR_PRIMARIES_FILM, WP_C, 0.681, 0.319, 0.243, 0.692, 0.145,
285 {GST_VIDEO_COLOR_PRIMARIES_BT2020, WP_D65, 0.708, 0.292, 0.170, 0.797, 0.131,
287 {GST_VIDEO_COLOR_PRIMARIES_ADOBERGB, WP_D65, 0.64, 0.33, 0.21, 0.71, 0.15,
292 * gst_video_color_primaries_get_info:
293 * @primaries: a #GstVideoColorPrimaries
295 * Get information about the chromaticity coordinates of @primaries.
297 * Returns: a #GstVideoColorPrimariesInfo for @primaries.
301 const GstVideoColorPrimariesInfo *
302 gst_video_color_primaries_get_info (GstVideoColorPrimaries primaries)
304 g_return_val_if_fail ((gint) primaries <
305 G_N_ELEMENTS (color_primaries), NULL);
307 return &color_primaries[primaries];
311 * gst_video_color_matrix_get_Kr_Kb:
312 * @matrix: a #GstVideoColorMatrix
313 * @Kr: (out): result red channel coefficient
314 * @Kb: (out): result blue channel coefficient
316 * Get the coefficients used to convert between Y'PbPr and R'G'B' using @matrix.
321 * 0.0 <= [Y',R',G',B'] <= 1.0)
322 * (-0.5 <= [Pb,Pr] <= 0.5)
325 * the general conversion is given by:
328 * Y' = Kr*R' + (1-Kr-Kb)*G' + Kb*B'
329 * Pb = (B'-Y')/(2*(1-Kb))
330 * Pr = (R'-Y')/(2*(1-Kr))
333 * and the other way around:
336 * R' = Y' + Cr*2*(1-Kr)
337 * G' = Y' - Cb*2*(1-Kb)*Kb/(1-Kr-Kb) - Cr*2*(1-Kr)*Kr/(1-Kr-Kb)
338 * B' = Y' + Cb*2*(1-Kb)
341 * Returns: TRUE if @matrix was a YUV color format and @Kr and @Kb contain valid
347 gst_video_color_matrix_get_Kr_Kb (GstVideoColorMatrix matrix, gdouble * Kr,
355 case GST_VIDEO_COLOR_MATRIX_RGB:
359 case GST_VIDEO_COLOR_MATRIX_FCC:
363 case GST_VIDEO_COLOR_MATRIX_BT709:
367 case GST_VIDEO_COLOR_MATRIX_BT601:
371 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
375 case GST_VIDEO_COLOR_MATRIX_BT2020:
380 GST_DEBUG ("matrix: %d, Kr %f, Kb %f", matrix, *Kr, *Kb);
386 * gst_video_color_transfer_encode:
387 * @func: a #GstVideoTransferFunction
390 * Convert @val to its gamma encoded value.
392 * For a linear value L in the range [0..1], conversion to the non-linear
393 * (gamma encoded) L' is in general performed with a power function like:
396 * L' = L ^ (1 / gamma)
399 * Depending on @func, different formulas might be applied. Some formulas
400 * encode a linear segment in the lower range.
402 * Returns: the gamme encoded value of @val
407 gst_video_color_transfer_encode (GstVideoTransferFunction func, gdouble val)
412 case GST_VIDEO_TRANSFER_UNKNOWN:
413 case GST_VIDEO_TRANSFER_GAMMA10:
417 case GST_VIDEO_TRANSFER_GAMMA18:
418 res = pow (val, 1.0 / 1.8);
420 case GST_VIDEO_TRANSFER_GAMMA20:
421 res = pow (val, 1.0 / 2.0);
423 case GST_VIDEO_TRANSFER_GAMMA22:
424 res = pow (val, 1.0 / 2.2);
426 case GST_VIDEO_TRANSFER_BT709:
430 res = 1.099 * pow (val, 0.45) - 0.099;
432 case GST_VIDEO_TRANSFER_SMPTE240M:
436 res = 1.1115 * pow (val, 0.45) - 0.1115;
438 case GST_VIDEO_TRANSFER_SRGB:
439 if (val <= 0.0031308)
442 res = 1.055 * pow (val, 1.0 / 2.4) - 0.055;
444 case GST_VIDEO_TRANSFER_GAMMA28:
445 res = pow (val, 1 / 2.8);
447 case GST_VIDEO_TRANSFER_LOG100:
451 res = 1.0 + log10 (val) / 2.0;
453 case GST_VIDEO_TRANSFER_LOG316:
454 if (val < 0.0031622777)
457 res = 1.0 + log10 (val) / 2.5;
459 case GST_VIDEO_TRANSFER_BT2020_12:
463 res = 1.0993 * pow (val, 0.45) - 0.0993;
465 case GST_VIDEO_TRANSFER_ADOBERGB:
466 res = pow (val, 1.0 / 2.19921875);
473 * gst_video_color_transfer_decode:
474 * @func: a #GstVideoTransferFunction
477 * Convert @val to its gamma decoded value. This is the inverse operation of
478 * @gst_video_color_transfer_encode().
480 * For a non-linear value L' in the range [0..1], conversion to the linear
481 * L is in general performed with a power function like:
487 * Depending on @func, different formulas might be applied. Some formulas
488 * encode a linear segment in the lower range.
490 * Returns: the gamme decoded value of @val
495 gst_video_color_transfer_decode (GstVideoTransferFunction func, gdouble val)
500 case GST_VIDEO_TRANSFER_UNKNOWN:
501 case GST_VIDEO_TRANSFER_GAMMA10:
505 case GST_VIDEO_TRANSFER_GAMMA18:
506 res = pow (val, 1.8);
508 case GST_VIDEO_TRANSFER_GAMMA20:
509 res = pow (val, 2.0);
511 case GST_VIDEO_TRANSFER_GAMMA22:
512 res = pow (val, 2.2);
514 case GST_VIDEO_TRANSFER_BT709:
518 res = pow ((val + 0.099) / 1.099, 1.0 / 0.45);
520 case GST_VIDEO_TRANSFER_SMPTE240M:
524 res = pow ((val + 0.1115) / 1.1115, 1.0 / 0.45);
526 case GST_VIDEO_TRANSFER_SRGB:
530 res = pow ((val + 0.055) / 1.055, 2.4);
532 case GST_VIDEO_TRANSFER_GAMMA28:
533 res = pow (val, 2.8);
535 case GST_VIDEO_TRANSFER_LOG100:
539 res = pow (10.0, 2.0 * (val - 1.0));
541 case GST_VIDEO_TRANSFER_LOG316:
545 res = pow (10.0, 2.5 * (val - 1.0));
547 case GST_VIDEO_TRANSFER_BT2020_12:
551 res = pow ((val + 0.0993) / 1.0993, 1.0 / 0.45);
553 case GST_VIDEO_TRANSFER_ADOBERGB:
554 res = pow (val, 2.19921875);