From patchwork Sat Apr 6 23:09:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Longerbeam X-Patchwork-Id: 10888277 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 90F531669 for ; Sat, 6 Apr 2019 23:09:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64D022875A for ; Sat, 6 Apr 2019 23:09:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5968028761; Sat, 6 Apr 2019 23:09:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D7C182875A for ; Sat, 6 Apr 2019 23:09:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8E4A68966B; Sat, 6 Apr 2019 23:09:22 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by gabe.freedesktop.org (Postfix) with ESMTPS id AE8F58966B for ; Sat, 6 Apr 2019 23:09:20 +0000 (UTC) Received: by mail-pl1-x644.google.com with SMTP id y6so5069294plt.1 for ; Sat, 06 Apr 2019 16:09:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4LWWo6vClK616q2qAdH10ez3h7JYhnb9DJf05zCL0Rc=; b=N5UFC+xU2ZK4NqSTDzMwQm7sF+rd+evhi/XPZeT5hyHd0hE0mrsIe/Lzrx0xyKQjHv zVsuR45md85sYIFJRRXlBDRSCQ5OC9HwJmxi8h3jR5ZMvn28igGmPXycIl/Y//k6zK7Y dH6Gpj2TCOeUegqdTMnlo51WIEjb3eTuRPTvtW+qalri4GpipB8rVXeV4TWg2LyuQ5Bf aAwQ3wnn4FNlcdmPPhZFoEXS1i3Z3midbua1AB45EU7yEumNOaoFzygG094976/7zR2T mPjHddxbPQY16WBq2sbeHDYRchU1GUJvO97IHLfAz7yUzGcgYwQ0MJfFDmpQGD8KuXX+ Mqlg== X-Gm-Message-State: APjAAAWc/SCUo+aE93egBk16BHNltuNuHhx+927V6Ic14kH3iJFwXr/s gWlZyM9Fbs5RIT3OhBSMLSg= X-Google-Smtp-Source: APXvYqyB2zkXHJyKOMcaXbH72PsVzQ752yWo+UhPbj8OCDS9YFc0h8937cv0rgq2n+jqie8Vt7IIsA== X-Received: by 2002:a17:902:7c94:: with SMTP id y20mr21626181pll.263.1554592158783; Sat, 06 Apr 2019 16:09:18 -0700 (PDT) Received: from mappy.sklembedded.com (c-24-4-228-186.hsd1.ca.comcast.net. [24.4.228.186]) by smtp.gmail.com with ESMTPSA id i31sm44550127pgi.36.2019.04.06.16.09.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Apr 2019 16:09:18 -0700 (PDT) From: Steve Longerbeam To: linux-media@vger.kernel.org Subject: [PATCH v7 4/5] gpu: ipu-v3: ipu-ic-csc: Add support for Rec.709 encoding Date: Sat, 6 Apr 2019 16:09:02 -0700 Message-Id: <20190406230903.16488-5-slongerbeam@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190406230903.16488-1-slongerbeam@gmail.com> References: <20190406230903.16488-1-slongerbeam@gmail.com> X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4LWWo6vClK616q2qAdH10ez3h7JYhnb9DJf05zCL0Rc=; b=u2CJHuP+mlcuAlWQcV+owWFbPFC6wVKcWwh80i3aZoFl6gw7mh5esYsKmDURBbknUz Sn41Ks+a/L9sB8YjmtAKfSAMLmUcQXxk2Tz8DKhwKfvp1VfVBhEGhx1E0NDtYeVVB4H3 7Yx5JnFro5faXv5HURzYnPsE2qdpAr5adYXFkQu0izPsARg5pDtEYAonqBbzxZSBTkUU kJAt3PZewG//fUrFwXweB8fimmt8QFh+qF/bm1LE2NkFJJmwoZDW+KA9D8G7vOeh+63F Kcbcsvu+3ntHxUSRF04o7HSte/O8ZjIdEuy0rVSnDXCwV/1vFu9J6p0r8CR4L/v+3EA5 bLmg== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: open list , "open list:DRM DRIVERS FOR FREESCALE IMX" , Steve Longerbeam MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add support for Rec.709 encoding and inverse encoding. Reported-by: Tim Harvey Signed-off-by: Steve Longerbeam --- Changes in v7: - moved CSC tables to new module ipu-ic-csc.c. - express negative coefficients as true signed int's, for better readability. Changes in v5: - moved API changes to a previous patch. - moved CSC coeff calc to new function calc_csc_coeffs(). Changes in v4: - fix compile error. Chnges in v3: - none. Changes in v2: - only return "Unsupported YCbCr encoding" error if inf != outf, since if inf == outf, the identity matrix can be used. Reported by Tim Harvey. --- drivers/gpu/ipu-v3/ipu-ic-csc.c | 139 ++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/ipu-v3/ipu-ic-csc.c b/drivers/gpu/ipu-v3/ipu-ic-csc.c index 8e9150b1d668..09e94aa12c40 100644 --- a/drivers/gpu/ipu-v3/ipu-ic-csc.c +++ b/drivers/gpu/ipu-v3/ipu-ic-csc.c @@ -230,14 +230,133 @@ static const struct ipu_ic_csc_params *yuv2rgb_601[] = { &yuvl2rgbl_601, }; +/* + * REC.709 encoding from RGB full range to YUV full range: + * + * Y = .2126 * R + .7152 * G + .0722 * B + * U = -.1146 * R - .3854 * G + .5000 * B + 128 + * V = .5000 * R - .4542 * G - .0458 * B + 128 + */ +static const struct ipu_ic_csc_params rgbf2yuvf_709 = { + .coeff = { + { 54, 183, 19 }, + { -29, -99, 128 }, + { 128, -116, -12 }, + }, + .offset = { 0, 512, 512 }, + .scale = 1, +}; + +/* Rec.709 RGB full-range to YUV limited-range */ +static const struct ipu_ic_csc_params rgbf2yuvl_709 = { + .coeff = { + { 47, 157, 16, }, + { -26, -87, 112, }, + { 112, -102, -10, }, + }, + .offset = { 64, 512, 512, }, + .scale = 1, + .sat = true, +}; + +/* Rec.709 RGB limited-range to YUV full-range */ +static const struct ipu_ic_csc_params rgbl2yuvf_709 = { + .coeff = { + { 63, 213, 22, }, + { -34, -115, 149, }, + { 149, -135, -14, }, + }, + .offset = { -75, 512, 512, }, + .scale = 1, +}; + +/* Rec.709 RGB limited-range to YUV limited-range */ +static const struct ipu_ic_csc_params rgbl2yuvl_709 = { + .coeff = { + { 54, 183, 18, }, + { -30, -101, 131, }, + { 131, -119, -12, }, + }, + .offset = { 0, 512, 512, }, + .scale = 1, + .sat = true, +}; + +/* + * Inverse REC.709 encoding from YUV full range to RGB full range: + * + * R = 1. * Y + 0 * (Cb - 128) + 1.5748 * (Cr - 128) + * G = 1. * Y - .1873 * (Cb - 128) - .4681 * (Cr - 128) + * B = 1. * Y + 1.8556 * (Cb - 128) + 0 * (Cr - 128) + * + * equivalently (factoring out the offsets): + * + * R = 1. * Y + 0 * Cb + 1.5748 * Cr - 201.574 + * G = 1. * Y - .1873 * Cb - .4681 * Cr + 83.891 + * B = 1. * Y + 1.8556 * Cb + 0 * Cr - 237.517 + */ +static const struct ipu_ic_csc_params yuvf2rgbf_709 = { + .coeff = { + { 128, 0, 202 }, + { 128, -24, -60 }, + { 128, 238, 0 }, + }, + .offset = { -403, 168, -475 }, + .scale = 2, +}; + +/* Rec.709 YUV full-range to RGB limited-range */ +static const struct ipu_ic_csc_params yuvf2rgbl_709 = { + .coeff = { + { 110, 0, 173, }, + { 110, -21, -51, }, + { 110, 204, 0, }, + }, + .offset = { -314, 176, -376, }, + .scale = 2, +}; + +/* Rec.709 YUV limited-range to RGB full-range */ +static const struct ipu_ic_csc_params yuvl2rgbf_709 = { + .coeff = { + { 75, 0, 115, }, + { 75, -14, -34, }, + { 75, 135, 0, }, + }, + .offset = { -248, 77, -289, }, + .scale = 3, +}; + +/* Rec.709 YUV limited-range to RGB limited-range */ +static const struct ipu_ic_csc_params yuvl2rgbl_709 = { + .coeff = { + { 128, 0, 197, }, + { 128, -23, -59, }, + { 128, 232, 0, }, + }, + .offset = { -394, 164, -464, }, + .scale = 2, +}; + +static const struct ipu_ic_csc_params *rgb2yuv_709[] = { + &rgbf2yuvf_709, + &rgbf2yuvl_709, + &rgbl2yuvf_709, + &rgbl2yuvl_709, +}; + +static const struct ipu_ic_csc_params *yuv2rgb_709[] = { + &yuvf2rgbf_709, + &yuvf2rgbl_709, + &yuvl2rgbf_709, + &yuvl2rgbl_709, +}; + static int calc_csc_coeffs(struct ipu_ic_csc *csc) { const struct ipu_ic_csc_params **params_tbl; int tbl_idx; - if (csc->out_cs.enc != V4L2_YCBCR_ENC_601) - return -ENOTSUPP; - tbl_idx = (QUANT_MAP(csc->in_cs.quant) << 1) | QUANT_MAP(csc->out_cs.quant); @@ -250,8 +369,18 @@ static int calc_csc_coeffs(struct ipu_ic_csc *csc) /* YUV <-> RGB encoding is required */ - params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ? - yuv2rgb_601 : rgb2yuv_601; + switch (csc->out_cs.enc) { + case V4L2_YCBCR_ENC_601: + params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ? + yuv2rgb_601 : rgb2yuv_601; + break; + case V4L2_YCBCR_ENC_709: + params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ? + yuv2rgb_709 : rgb2yuv_709; + break; + default: + return -ENOTSUPP; + } csc->params = *params_tbl[tbl_idx];