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"
35 GstVideoColorimetry color;
38 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
39 { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
40 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
42 #define GST_VIDEO_COLORIMETRY_NONAME NULL
44 #define DEFAULT_YUV_SD 0
45 #define DEFAULT_YUV_HD 1
47 #define DEFAULT_GRAY 4
48 #define DEFAULT_UNKNOWN 5
50 static const ColorimetryInfo colorimetry[] = {
51 MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
52 MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
53 MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
54 MAKE_COLORIMETRY (SRGB, _0_255, RGB, SRGB, BT709),
55 MAKE_COLORIMETRY (NONAME, _0_255, BT601, UNKNOWN, UNKNOWN),
56 MAKE_COLORIMETRY (NONAME, _UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN),
59 static const ColorimetryInfo *
60 gst_video_get_colorimetry (const gchar * s)
64 for (i = 0; colorimetry[i].name; i++) {
65 if (g_str_equal (colorimetry[i].name, s))
66 return &colorimetry[i];
71 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
72 ((ci)->color.matrix == (i)->matrix) && \
73 ((ci)->color.transfer == (i)->transfer) && \
74 ((ci)->color.primaries == (i)->primaries))
76 #define IS_UNKNOWN(ci) (IS_EQUAL (&colorimetry[DEFAULT_UNKNOWN], ci))
79 * gst_video_colorimetry_from_string:
80 * @cinfo: a #GstVideoColorimetry
81 * @color: a colorimetry string
83 * Parse the colorimetry string and update @cinfo with the parsed
86 * Returns: #TRUE if @color points to valid colorimetry info.
89 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
92 const ColorimetryInfo *ci;
94 if ((ci = gst_video_get_colorimetry (color))) {
99 if (sscanf (color, "%d:%d:%d:%d", &r, &m, &t, &p) == 4) {
103 cinfo->primaries = p;
110 * gst_video_colorimetry_to_string:
111 * @cinfo: a #GstVideoColorimetry
113 * Make a string representation of @cinfo.
115 * Returns: a string representation of @cinfo.
118 gst_video_colorimetry_to_string (GstVideoColorimetry * cinfo)
122 for (i = 0; colorimetry[i].name; i++) {
123 if (IS_EQUAL (&colorimetry[i], cinfo)) {
124 return g_strdup (colorimetry[i].name);
127 if (!IS_UNKNOWN (cinfo)) {
128 return g_strdup_printf ("%d:%d:%d:%d", cinfo->range, cinfo->matrix,
129 cinfo->transfer, cinfo->primaries);
135 * gst_video_colorimetry_matches:
136 * @cinfo: a #GstVideoInfo
137 * @color: a colorimetry string
139 * Check if the colorimetry information in @info matches that of the
142 * Returns: #TRUE if @color conveys the same colorimetry info as the color
143 * information in @info.
146 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
148 const ColorimetryInfo *ci;
150 if ((ci = gst_video_get_colorimetry (color)))
151 return IS_EQUAL (ci, cinfo);
157 * gst_video_color_range_offsets:
158 * @range: a #GstVideoColorRange
159 * @info: a #GstVideoFormatInfo
160 * @offset: (out): output offsets
161 * @scale: (out): output scale
163 * Compute the offset and scale values for each component of @info. For each
164 * component, (c[i] - offset[i]) / scale[i] will scale the component c[i] to the
165 * range [0.0 .. 1.0].
167 * The reverse operation (c[i] * scale[i]) + offset[i] can be used to convert
168 * the component values in range [0.0 .. 1.0] back to their representation in
172 gst_video_color_range_offsets (GstVideoColorRange range,
173 const GstVideoFormatInfo * info, gint offset[GST_VIDEO_MAX_COMPONENTS],
174 gint scale[GST_VIDEO_MAX_COMPONENTS])
178 yuv = GST_VIDEO_FORMAT_INFO_IS_YUV (info);
182 case GST_VIDEO_COLOR_RANGE_0_255:
185 offset[1] = 1 << (info->depth[1] - 1);
186 offset[2] = 1 << (info->depth[2] - 1);
191 scale[0] = (1 << info->depth[0]) - 1;
192 scale[1] = (1 << info->depth[1]) - 1;
193 scale[2] = (1 << info->depth[2]) - 1;
195 case GST_VIDEO_COLOR_RANGE_16_235:
196 offset[0] = 1 << (info->depth[0] - 4);
197 scale[0] = 219 << (info->depth[0] - 8);
199 offset[1] = 1 << (info->depth[1] - 1);
200 offset[2] = 1 << (info->depth[2] - 1);
201 scale[1] = 224 << (info->depth[1] - 8);
202 scale[2] = 224 << (info->depth[2] - 8);
204 offset[1] = 1 << (info->depth[1] - 4);
205 offset[2] = 1 << (info->depth[2] - 4);
206 scale[1] = 219 << (info->depth[1] - 8);
207 scale[2] = 219 << (info->depth[2] - 8);
211 /* alpha channel is always full range */
213 scale[3] = (1 << info->depth[3]) - 1;
215 GST_DEBUG ("scale: %d %d %d %d", scale[0], scale[1], scale[2], scale[3]);
216 GST_DEBUG ("offset: %d %d %d %d", offset[0], offset[1], offset[2], offset[3]);
223 GstVideoColorPrimaries primaries;
230 #define WP_C 0.31006, 0.31616
231 #define WP_D65 0.31271, 0.32902
233 static const PrimariesInfo primaries[] = {
234 {GST_VIDEO_COLOR_PRIMARIES_UNKNOWN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
235 {GST_VIDEO_COLOR_PRIMARIES_BT709, WP_D65, 0.64, 0.33, 0.30, 0.60, 0.15, 0.06},
236 {GST_VIDEO_COLOR_PRIMARIES_BT470M, WP_C, 0.67, 0.33, 0.21, 0.71, 0.14, 0.08},
237 {GST_VIDEO_COLOR_PRIMARIES_BT470BG, WP_D65, 0.64, 0.33, 0.29, 0.60, 0.15,
239 {GST_VIDEO_COLOR_PRIMARIES_SMPTE170M, WP_D65, 0.63, 0.34, 0.31, 0.595, 0.155,
241 {GST_VIDEO_COLOR_PRIMARIES_SMPTE240M, WP_D65, 0.63, 0.34, 0.31, 0.595, 0.155,
243 {GST_VIDEO_COLOR_PRIMARIES_FILM, WP_C, 0.681, 0.319, 0.243, 0.692, 0.145,
249 * gst_video_color_matrix_get_Kr_Kb:
250 * @matrix: a #GstVideoColorMatrix
251 * @Kr: result red channel coefficient
252 * @Kb: result blue channel coefficient
254 * Get the coefficients used to convert between Y'PbPr and R'G'B' using @matrix.
259 * 0.0 <= [Y',R',G',B'] <= 1.0)
260 * (-0.5 <= [Pb,Pr] <= 0.5)
263 * the general conversion is given by:
266 * Y' = Kr*R' + (1-Kr-Kb)*G' + Kb*B'
267 * Pb = (B'-Y')/(2*(1-Kb))
268 * Pr = (R'-Y')/(2*(1-Kr))
271 * and the other way around:
274 * R' = Y' + Cr*2*(1-Kr)
275 * G' = Y' - Cb*2*(1-Kb)*Kb/(1-Kr-Kb) - Cr*2*(1-Kr)*Kr/(1-Kr-Kb)
276 * B' = Y' + Cb*2*(1-Kb)
279 * Returns: TRUE if @matrix was a YUV color format and @Kr and @Kb contain valid
285 gst_video_color_matrix_get_Kr_Kb (GstVideoColorMatrix matrix, gdouble * Kr,
293 case GST_VIDEO_COLOR_MATRIX_RGB:
297 case GST_VIDEO_COLOR_MATRIX_FCC:
301 case GST_VIDEO_COLOR_MATRIX_BT709:
305 case GST_VIDEO_COLOR_MATRIX_BT601:
309 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
314 GST_DEBUG ("matrix: %d, Kr %f, Kb %f", matrix, *Kr, *Kb);
320 * gst_video_color_transfer_encode:
321 * @func: a #GstVideoTransferFunction
324 * Convert @val to its gamma encoded value.
326 * For a linear value L in the range [0..1], conversion to the non-linear
327 * (gamma encoded) L' is in general performed with a power function like:
330 * L' = L ^ (1 / gamma)
333 * Depending on @func, different formulas might be applied. Some formulas
334 * encode a linear segment in the lower range.
336 * Returns: the gamme encoded value of @val
341 gst_video_color_transfer_encode (GstVideoTransferFunction func, gdouble val)
346 case GST_VIDEO_TRANSFER_UNKNOWN:
347 case GST_VIDEO_TRANSFER_GAMMA10:
351 case GST_VIDEO_TRANSFER_GAMMA18:
352 res = pow (val, 1.0 / 1.8);
354 case GST_VIDEO_TRANSFER_GAMMA20:
355 res = pow (val, 1.0 / 2.0);
357 case GST_VIDEO_TRANSFER_GAMMA22:
358 res = pow (val, 1.0 / 2.2);
360 case GST_VIDEO_TRANSFER_BT709:
364 res = 1.099 * pow (val, 0.45) - 0.099;
366 case GST_VIDEO_TRANSFER_SMPTE240M:
370 res = 1.1115 * pow (val, 0.45) - 0.1115;
372 case GST_VIDEO_TRANSFER_SRGB:
373 if (val <= 0.0031308)
376 res = 1.055 * pow (val, 1.0 / 2.4) - 0.055;
378 case GST_VIDEO_TRANSFER_GAMMA28:
379 res = pow (val, 1 / 2.8);
381 case GST_VIDEO_TRANSFER_LOG100:
385 res = 1.0 + log10 (val) / 2.0;
387 case GST_VIDEO_TRANSFER_LOG316:
388 if (val < 0.0031622777)
391 res = 1.0 + log10 (val) / 2.5;
398 * gst_video_color_transfer_decode:
399 * @func: a #GstVideoTransferFunction
402 * Convert @val to its gamma decoded value. This is the inverse operation of
403 * @gst_video_color_transfer_encode().
405 * For a non-linear value L' in the range [0..1], conversion to the linear
406 * L is in general performed with a power function like:
412 * Depending on @func, different formulas might be applied. Some formulas
413 * encode a linear segment in the lower range.
415 * Returns: the gamme decoded value of @val
420 gst_video_color_transfer_decode (GstVideoTransferFunction func, gdouble val)
425 case GST_VIDEO_TRANSFER_UNKNOWN:
426 case GST_VIDEO_TRANSFER_GAMMA10:
430 case GST_VIDEO_TRANSFER_GAMMA18:
431 res = pow (val, 1.8);
433 case GST_VIDEO_TRANSFER_GAMMA20:
434 res = pow (val, 2.0);
436 case GST_VIDEO_TRANSFER_GAMMA22:
437 res = pow (val, 2.2);
439 case GST_VIDEO_TRANSFER_BT709:
443 res = pow ((val + 0.099) / 1.099, 1.0 / 0.45);
445 case GST_VIDEO_TRANSFER_SMPTE240M:
449 res = pow ((val + 0.1115) / 1.1115, 1.0 / 0.45);
451 case GST_VIDEO_TRANSFER_SRGB:
455 res = pow ((val + 0.055) / 1.055, 2.4);
457 case GST_VIDEO_TRANSFER_GAMMA28:
458 res = pow (val, 2.8);
460 case GST_VIDEO_TRANSFER_LOG100:
464 res = pow (10.0, 2.0 * (val - 1.0));
466 case GST_VIDEO_TRANSFER_LOG316:
470 res = pow (10.0, 2.5 * (val - 1.0));