1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2009 Younes Manton. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "util/u_math.h" 29bf215546Sopenharmony_ci#include "util/u_debug.h" 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci#include "vl_csc.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci/* 34bf215546Sopenharmony_ci * Color space conversion formulas 35bf215546Sopenharmony_ci * 36bf215546Sopenharmony_ci * To convert YCbCr to RGB, 37bf215546Sopenharmony_ci * vec4 ycbcr, rgb 38bf215546Sopenharmony_ci * mat44 csc 39bf215546Sopenharmony_ci * rgb = csc * ycbcr 40bf215546Sopenharmony_ci * 41bf215546Sopenharmony_ci * To calculate the color space conversion matrix csc with ProcAmp adjustments, 42bf215546Sopenharmony_ci * mat44 csc, cstd, procamp, bias 43bf215546Sopenharmony_ci * csc = cstd * (procamp * bias) 44bf215546Sopenharmony_ci * 45bf215546Sopenharmony_ci * Where cstd is a matrix corresponding to one of the color standards (BT.601, BT.709, etc) 46bf215546Sopenharmony_ci * adjusted for the kind of YCbCr -> RGB mapping wanted (1:1, full), 47bf215546Sopenharmony_ci * bias is a matrix corresponding to the kind of YCbCr -> RGB mapping wanted (1:1, full) 48bf215546Sopenharmony_ci * 49bf215546Sopenharmony_ci * To calculate procamp, 50bf215546Sopenharmony_ci * mat44 procamp, hue, saturation, brightness, contrast 51bf215546Sopenharmony_ci * procamp = brightness * (saturation * (contrast * hue)) 52bf215546Sopenharmony_ci * Alternatively, 53bf215546Sopenharmony_ci * procamp = saturation * (brightness * (contrast * hue)) 54bf215546Sopenharmony_ci * 55bf215546Sopenharmony_ci * contrast 56bf215546Sopenharmony_ci * [ c, 0, 0, 0] 57bf215546Sopenharmony_ci * [ 0, c, 0, 0] 58bf215546Sopenharmony_ci * [ 0, 0, c, 0] 59bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 60bf215546Sopenharmony_ci * 61bf215546Sopenharmony_ci * brightness 62bf215546Sopenharmony_ci * [ 1, 0, 0, b/c] 63bf215546Sopenharmony_ci * [ 0, 1, 0, 0] 64bf215546Sopenharmony_ci * [ 0, 0, 1, 0] 65bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 66bf215546Sopenharmony_ci * 67bf215546Sopenharmony_ci * saturation 68bf215546Sopenharmony_ci * [ 1, 0, 0, 0] 69bf215546Sopenharmony_ci * [ 0, s, 0, 0] 70bf215546Sopenharmony_ci * [ 0, 0, s, 0] 71bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 72bf215546Sopenharmony_ci * 73bf215546Sopenharmony_ci * hue 74bf215546Sopenharmony_ci * [ 1, 0, 0, 0] 75bf215546Sopenharmony_ci * [ 0, cos(h), sin(h), 0] 76bf215546Sopenharmony_ci * [ 0, -sin(h), cos(h), 0] 77bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 78bf215546Sopenharmony_ci * 79bf215546Sopenharmony_ci * procamp 80bf215546Sopenharmony_ci * [ c, 0, 0, b] 81bf215546Sopenharmony_ci * [ 0, c*s*cos(h), c*s*sin(h), 0] 82bf215546Sopenharmony_ci * [ 0, -c*s*sin(h), c*s*cos(h), 0] 83bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 84bf215546Sopenharmony_ci * 85bf215546Sopenharmony_ci * bias 86bf215546Sopenharmony_ci * [ 1, 0, 0, ybias] 87bf215546Sopenharmony_ci * [ 0, 1, 0, cbbias] 88bf215546Sopenharmony_ci * [ 0, 0, 1, crbias] 89bf215546Sopenharmony_ci * [ 0, 0, 0, 1] 90bf215546Sopenharmony_ci * 91bf215546Sopenharmony_ci * csc 92bf215546Sopenharmony_ci * [ c*cstd[ 0], c*cstd[ 1]*s*cos(h) - c*cstd[ 2]*s*sin(h), c*cstd[ 2]*s*cos(h) + c*cstd[ 1]*s*sin(h), cstd[ 3] + cstd[ 0]*(b + c*ybias) + cstd[ 1]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 2]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 93bf215546Sopenharmony_ci * [ c*cstd[ 4], c*cstd[ 5]*s*cos(h) - c*cstd[ 6]*s*sin(h), c*cstd[ 6]*s*cos(h) + c*cstd[ 5]*s*sin(h), cstd[ 7] + cstd[ 4]*(b + c*ybias) + cstd[ 5]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 6]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 94bf215546Sopenharmony_ci * [ c*cstd[ 8], c*cstd[ 9]*s*cos(h) - c*cstd[10]*s*sin(h), c*cstd[10]*s*cos(h) + c*cstd[ 9]*s*sin(h), cstd[11] + cstd[ 8]*(b + c*ybias) + cstd[ 9]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[10]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 95bf215546Sopenharmony_ci * [ c*cstd[12], c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h), c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h), cstd[15] + cstd[12]*(b + c*ybias) + cstd[13]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[14]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 96bf215546Sopenharmony_ci */ 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ci/* 99bf215546Sopenharmony_ci * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where: 100bf215546Sopenharmony_ci * Y is in [16,235], Cb and Cr are in [16,240] 101bf215546Sopenharmony_ci * R, G, and B are in [16,235] 102bf215546Sopenharmony_ci */ 103bf215546Sopenharmony_cistatic const vl_csc_matrix bt_601 = 104bf215546Sopenharmony_ci{ 105bf215546Sopenharmony_ci { 1.0f, 0.0f, 1.371f, 0.0f, }, 106bf215546Sopenharmony_ci { 1.0f, -0.336f, -0.698f, 0.0f, }, 107bf215546Sopenharmony_ci { 1.0f, 1.732f, 0.0f, 0.0f, } 108bf215546Sopenharmony_ci}; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci/* 111bf215546Sopenharmony_ci * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where: 112bf215546Sopenharmony_ci * Y is in [16,235], Cb and Cr are in [16,240] 113bf215546Sopenharmony_ci * R, G, and B are in [16,235] 114bf215546Sopenharmony_ci */ 115bf215546Sopenharmony_cistatic const vl_csc_matrix bt_709 = 116bf215546Sopenharmony_ci{ 117bf215546Sopenharmony_ci { 1.0f, 0.0f, 1.540f, 0.0f, }, 118bf215546Sopenharmony_ci { 1.0f, -0.183f, -0.459f, 0.0f, }, 119bf215546Sopenharmony_ci { 1.0f, 1.816f, 0.0f, 0.0f, } 120bf215546Sopenharmony_ci}; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci/* 123bf215546Sopenharmony_ci * Converts SMPTE 240M YCbCr pixels to RGB pixels where: 124bf215546Sopenharmony_ci * Y is in [16,235], Cb and Cr are in [16,240] 125bf215546Sopenharmony_ci * R, G, and B are in [16,235] 126bf215546Sopenharmony_ci */ 127bf215546Sopenharmony_cistatic const vl_csc_matrix smpte240m = 128bf215546Sopenharmony_ci{ 129bf215546Sopenharmony_ci { 1.0f, 0.0f, 1.541f, 0.0f, }, 130bf215546Sopenharmony_ci { 1.0f, -0.221f, -0.466f, 0.0f, }, 131bf215546Sopenharmony_ci { 1.0f, 1.785f, 0.0f, 0.0f, } 132bf215546Sopenharmony_ci}; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_cistatic const vl_csc_matrix bt_709_rev = { 135bf215546Sopenharmony_ci { 0.183f, 0.614f, 0.062f, 0.0625f}, 136bf215546Sopenharmony_ci {-0.101f, -0.338f, 0.439f, 0.5f }, 137bf215546Sopenharmony_ci { 0.439f, -0.399f, -0.040f, 0.5f } 138bf215546Sopenharmony_ci}; 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_cistatic const vl_csc_matrix identity = 141bf215546Sopenharmony_ci{ 142bf215546Sopenharmony_ci { 1.0f, 0.0f, 0.0f, 0.0f, }, 143bf215546Sopenharmony_ci { 0.0f, 1.0f, 0.0f, 0.0f, }, 144bf215546Sopenharmony_ci { 0.0f, 0.0f, 1.0f, 0.0f, } 145bf215546Sopenharmony_ci}; 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ciconst struct vl_procamp vl_default_procamp = { 148bf215546Sopenharmony_ci 0.0f, /* brightness */ 149bf215546Sopenharmony_ci 1.0f, /* contrast */ 150bf215546Sopenharmony_ci 1.0f, /* saturation */ 151bf215546Sopenharmony_ci 0.0f /* hue */ 152bf215546Sopenharmony_ci}; 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_civoid vl_csc_get_matrix(enum VL_CSC_COLOR_STANDARD cs, 155bf215546Sopenharmony_ci struct vl_procamp *procamp, 156bf215546Sopenharmony_ci bool full_range, 157bf215546Sopenharmony_ci vl_csc_matrix *matrix) 158bf215546Sopenharmony_ci{ 159bf215546Sopenharmony_ci float cbbias = -128.0f/255.0f; 160bf215546Sopenharmony_ci float crbias = -128.0f/255.0f; 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci const struct vl_procamp *p = procamp ? procamp : &vl_default_procamp; 163bf215546Sopenharmony_ci float c = p->contrast; 164bf215546Sopenharmony_ci float s = p->saturation; 165bf215546Sopenharmony_ci float b = p->brightness; 166bf215546Sopenharmony_ci float h = p->hue; 167bf215546Sopenharmony_ci float x, y; 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci const vl_csc_matrix *cstd; 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci if (full_range) { 172bf215546Sopenharmony_ci c *= 1.164f; /* Adjust for the y range */ 173bf215546Sopenharmony_ci b *= 1.164f; /* Adjust for the y range */ 174bf215546Sopenharmony_ci b -= c * 16.0f / 255.0f; /* Adjust for the y bias */ 175bf215546Sopenharmony_ci } 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci /* Parameter substitutions */ 178bf215546Sopenharmony_ci x = c * s * cosf(h); 179bf215546Sopenharmony_ci y = c * s * sinf(h); 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci assert(matrix); 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ci switch (cs) { 184bf215546Sopenharmony_ci case VL_CSC_COLOR_STANDARD_BT_601: 185bf215546Sopenharmony_ci cstd = &bt_601; 186bf215546Sopenharmony_ci break; 187bf215546Sopenharmony_ci case VL_CSC_COLOR_STANDARD_BT_709: 188bf215546Sopenharmony_ci cstd = &bt_709; 189bf215546Sopenharmony_ci break; 190bf215546Sopenharmony_ci case VL_CSC_COLOR_STANDARD_SMPTE_240M: 191bf215546Sopenharmony_ci cstd = &smpte240m; 192bf215546Sopenharmony_ci break; 193bf215546Sopenharmony_ci case VL_CSC_COLOR_STANDARD_BT_709_REV: 194bf215546Sopenharmony_ci memcpy(matrix, bt_709_rev, sizeof(vl_csc_matrix)); 195bf215546Sopenharmony_ci return; 196bf215546Sopenharmony_ci case VL_CSC_COLOR_STANDARD_IDENTITY: 197bf215546Sopenharmony_ci default: 198bf215546Sopenharmony_ci assert(cs == VL_CSC_COLOR_STANDARD_IDENTITY); 199bf215546Sopenharmony_ci memcpy(matrix, identity, sizeof(vl_csc_matrix)); 200bf215546Sopenharmony_ci return; 201bf215546Sopenharmony_ci } 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci (*matrix)[0][0] = c * (*cstd)[0][0]; 204bf215546Sopenharmony_ci (*matrix)[0][1] = (*cstd)[0][1] * x - (*cstd)[0][2] * y; 205bf215546Sopenharmony_ci (*matrix)[0][2] = (*cstd)[0][2] * x + (*cstd)[0][1] * y; 206bf215546Sopenharmony_ci (*matrix)[0][3] = (*cstd)[0][3] + (*cstd)[0][0] * b + 207bf215546Sopenharmony_ci (*cstd)[0][1] * (x * cbbias + y * crbias) + 208bf215546Sopenharmony_ci (*cstd)[0][2] * (x * crbias - y * cbbias); 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci (*matrix)[1][0] = c * (*cstd)[1][0]; 211bf215546Sopenharmony_ci (*matrix)[1][1] = (*cstd)[1][1] * x - (*cstd)[1][2] * y; 212bf215546Sopenharmony_ci (*matrix)[1][2] = (*cstd)[1][2] * x + (*cstd)[1][1] * y; 213bf215546Sopenharmony_ci (*matrix)[1][3] = (*cstd)[1][3] + (*cstd)[1][0] * b + 214bf215546Sopenharmony_ci (*cstd)[1][1] * (x * cbbias + y * crbias) + 215bf215546Sopenharmony_ci (*cstd)[1][2] * (x * crbias - y * cbbias); 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci (*matrix)[2][0] = c * (*cstd)[2][0]; 218bf215546Sopenharmony_ci (*matrix)[2][1] = (*cstd)[2][1] * x - (*cstd)[2][2] * y; 219bf215546Sopenharmony_ci (*matrix)[2][2] = (*cstd)[2][2] * x + (*cstd)[2][1] * y; 220bf215546Sopenharmony_ci (*matrix)[2][3] = (*cstd)[2][3] + (*cstd)[2][0] * b + 221bf215546Sopenharmony_ci (*cstd)[2][1] * (x * cbbias + y * crbias) + 222bf215546Sopenharmony_ci (*cstd)[2][2] * (x * crbias - y * cbbias); 223bf215546Sopenharmony_ci} 224