Path: blob/master/thirdparty/libwebp/sharpyuv/sharpyuv_csp.c
9898 views
// Copyright 2022 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Colorspace utilities.1011#include "sharpyuv/sharpyuv_csp.h"1213#include <assert.h>14#include <math.h>15#include <stddef.h>1617static int ToFixed16(float f) { return (int)floor(f * (1 << 16) + 0.5f); }1819void SharpYuvComputeConversionMatrix(const SharpYuvColorSpace* yuv_color_space,20SharpYuvConversionMatrix* matrix) {21const float kr = yuv_color_space->kr;22const float kb = yuv_color_space->kb;23const float kg = 1.0f - kr - kb;24const float cb = 0.5f / (1.0f - kb);25const float cr = 0.5f / (1.0f - kr);2627const int shift = yuv_color_space->bit_depth - 8;2829const float denom = (float)((1 << yuv_color_space->bit_depth) - 1);30float scale_y = 1.0f;31float add_y = 0.0f;32float scale_u = cb;33float scale_v = cr;34float add_uv = (float)(128 << shift);35assert(yuv_color_space->bit_depth >= 8);3637if (yuv_color_space->range == kSharpYuvRangeLimited) {38scale_y *= (219 << shift) / denom;39scale_u *= (224 << shift) / denom;40scale_v *= (224 << shift) / denom;41add_y = (float)(16 << shift);42}4344matrix->rgb_to_y[0] = ToFixed16(kr * scale_y);45matrix->rgb_to_y[1] = ToFixed16(kg * scale_y);46matrix->rgb_to_y[2] = ToFixed16(kb * scale_y);47matrix->rgb_to_y[3] = ToFixed16(add_y);4849matrix->rgb_to_u[0] = ToFixed16(-kr * scale_u);50matrix->rgb_to_u[1] = ToFixed16(-kg * scale_u);51matrix->rgb_to_u[2] = ToFixed16((1 - kb) * scale_u);52matrix->rgb_to_u[3] = ToFixed16(add_uv);5354matrix->rgb_to_v[0] = ToFixed16((1 - kr) * scale_v);55matrix->rgb_to_v[1] = ToFixed16(-kg * scale_v);56matrix->rgb_to_v[2] = ToFixed16(-kb * scale_v);57matrix->rgb_to_v[3] = ToFixed16(add_uv);58}5960// Matrices are in YUV_FIX fixed point precision.61// WebP's matrix, similar but not identical to kRec601LimitedMatrix62// Derived using the following formulas:63// Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 1664// U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 12865// V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 12866static const SharpYuvConversionMatrix kWebpMatrix = {67{16839, 33059, 6420, 16 << 16},68{-9719, -19081, 28800, 128 << 16},69{28800, -24116, -4684, 128 << 16},70};71// Kr=0.2990f Kb=0.1140f bit_depth=8 range=kSharpYuvRangeLimited72static const SharpYuvConversionMatrix kRec601LimitedMatrix = {73{16829, 33039, 6416, 16 << 16},74{-9714, -19071, 28784, 128 << 16},75{28784, -24103, -4681, 128 << 16},76};77// Kr=0.2990f Kb=0.1140f bit_depth=8 range=kSharpYuvRangeFull78static const SharpYuvConversionMatrix kRec601FullMatrix = {79{19595, 38470, 7471, 0},80{-11058, -21710, 32768, 128 << 16},81{32768, -27439, -5329, 128 << 16},82};83// Kr=0.2126f Kb=0.0722f bit_depth=8 range=kSharpYuvRangeLimited84static const SharpYuvConversionMatrix kRec709LimitedMatrix = {85{11966, 40254, 4064, 16 << 16},86{-6596, -22189, 28784, 128 << 16},87{28784, -26145, -2639, 128 << 16},88};89// Kr=0.2126f Kb=0.0722f bit_depth=8 range=kSharpYuvRangeFull90static const SharpYuvConversionMatrix kRec709FullMatrix = {91{13933, 46871, 4732, 0},92{-7509, -25259, 32768, 128 << 16},93{32768, -29763, -3005, 128 << 16},94};9596const SharpYuvConversionMatrix* SharpYuvGetConversionMatrix(97SharpYuvMatrixType matrix_type) {98switch (matrix_type) {99case kSharpYuvMatrixWebp:100return &kWebpMatrix;101case kSharpYuvMatrixRec601Limited:102return &kRec601LimitedMatrix;103case kSharpYuvMatrixRec601Full:104return &kRec601FullMatrix;105case kSharpYuvMatrixRec709Limited:106return &kRec709LimitedMatrix;107case kSharpYuvMatrixRec709Full:108return &kRec709FullMatrix;109case kSharpYuvMatrixNum:110return NULL;111}112return NULL;113}114115116