Path: blob/master/thirdparty/libwebp/sharpyuv/sharpyuv.h
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// Sharp RGB to YUV conversion.1011#ifndef WEBP_SHARPYUV_SHARPYUV_H_12#define WEBP_SHARPYUV_SHARPYUV_H_1314#ifdef __cplusplus15extern "C" {16#endif1718#ifndef SHARPYUV_EXTERN19#ifdef WEBP_EXTERN20#define SHARPYUV_EXTERN WEBP_EXTERN21#else22// This explicitly marks library functions and allows for changing the23// signature for e.g., Windows DLL builds.24#if defined(_WIN32) && defined(WEBP_DLL)25#define SHARPYUV_EXTERN __declspec(dllexport)26#elif defined(__GNUC__) && __GNUC__ >= 427#define SHARPYUV_EXTERN extern __attribute__((visibility("default")))28#else29#define SHARPYUV_EXTERN extern30#endif /* defined(_WIN32) && defined(WEBP_DLL) */31#endif /* WEBP_EXTERN */32#endif /* SHARPYUV_EXTERN */3334#ifndef SHARPYUV_INLINE35#ifdef WEBP_INLINE36#define SHARPYUV_INLINE WEBP_INLINE37#else38#ifndef _MSC_VER39#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \40(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)41#define SHARPYUV_INLINE inline42#else43#define SHARPYUV_INLINE44#endif45#else46#define SHARPYUV_INLINE __forceinline47#endif /* _MSC_VER */48#endif /* WEBP_INLINE */49#endif /* SHARPYUV_INLINE */5051// SharpYUV API version following the convention from semver.org52#define SHARPYUV_VERSION_MAJOR 053#define SHARPYUV_VERSION_MINOR 454#define SHARPYUV_VERSION_PATCH 155// Version as a uint32_t. The major number is the high 8 bits.56// The minor number is the middle 8 bits. The patch number is the low 16 bits.57#define SHARPYUV_MAKE_VERSION(MAJOR, MINOR, PATCH) \58(((MAJOR) << 24) | ((MINOR) << 16) | (PATCH))59#define SHARPYUV_VERSION \60SHARPYUV_MAKE_VERSION(SHARPYUV_VERSION_MAJOR, SHARPYUV_VERSION_MINOR, \61SHARPYUV_VERSION_PATCH)6263// Returns the library's version number, packed in hexadecimal. See64// SHARPYUV_VERSION.65SHARPYUV_EXTERN int SharpYuvGetVersion(void);6667// RGB to YUV conversion matrix, in 16 bit fixed point.68// y_ = rgb_to_y[0] * r + rgb_to_y[1] * g + rgb_to_y[2] * b + rgb_to_y[3]69// u_ = rgb_to_u[0] * r + rgb_to_u[1] * g + rgb_to_u[2] * b + rgb_to_u[3]70// v_ = rgb_to_v[0] * r + rgb_to_v[1] * g + rgb_to_v[2] * b + rgb_to_v[3]71// Then the values are divided by 1<<16 and rounded.72// y = (y_ + (1 << 15)) >> 1673// u = (u_ + (1 << 15)) >> 1674// v = (v_ + (1 << 15)) >> 1675//76// Typically, the offset values rgb_to_y[3], rgb_to_u[3] and rgb_to_v[3] depend77// on the input's bit depth, e.g., rgb_to_u[3] = 1 << (rgb_bit_depth - 1 + 16).78// See also sharpyuv_csp.h to get a predefined matrix or generate a matrix.79typedef struct {80int rgb_to_y[4];81int rgb_to_u[4];82int rgb_to_v[4];83} SharpYuvConversionMatrix;8485typedef struct SharpYuvOptions SharpYuvOptions;8687// Enums for transfer functions, as defined in H.273,88// https://www.itu.int/rec/T-REC-H.273-202107-I/en89typedef enum SharpYuvTransferFunctionType {90// 0 is reserved91kSharpYuvTransferFunctionBt709 = 1,92// 2 is unspecified93// 3 is reserved94kSharpYuvTransferFunctionBt470M = 4,95kSharpYuvTransferFunctionBt470Bg = 5,96kSharpYuvTransferFunctionBt601 = 6,97kSharpYuvTransferFunctionSmpte240 = 7,98kSharpYuvTransferFunctionLinear = 8,99kSharpYuvTransferFunctionLog100 = 9,100kSharpYuvTransferFunctionLog100_Sqrt10 = 10,101kSharpYuvTransferFunctionIec61966 = 11,102kSharpYuvTransferFunctionBt1361 = 12,103kSharpYuvTransferFunctionSrgb = 13,104kSharpYuvTransferFunctionBt2020_10Bit = 14,105kSharpYuvTransferFunctionBt2020_12Bit = 15,106kSharpYuvTransferFunctionSmpte2084 = 16, // PQ107kSharpYuvTransferFunctionSmpte428 = 17,108kSharpYuvTransferFunctionHlg = 18,109kSharpYuvTransferFunctionNum110} SharpYuvTransferFunctionType;111112// Converts RGB to YUV420 using a downsampling algorithm that minimizes113// artefacts caused by chroma subsampling.114// This is slower than standard downsampling (averaging of 4 UV values).115// Assumes that the image will be upsampled using a bilinear filter. If nearest116// neighbor is used instead, the upsampled image might look worse than with117// standard downsampling.118// r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point119// to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.120// rgb_step: distance in bytes between two horizontally adjacent pixels on the121// r, g and b channels. If rgb_bit_depth is > 8, it should be a122// multiple of 2.123// rgb_stride: distance in bytes between two vertically adjacent pixels on the124// r, g, and b channels. If rgb_bit_depth is > 8, it should be a125// multiple of 2.126// rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.127// Note: 16 bit input is truncated to 14 bits before conversion to yuv.128// yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.129// y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should130// point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers131// otherwise.132// y_stride, u_stride, v_stride: distance in bytes between two vertically133// adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they134// should be multiples of 2.135// width, height: width and height of the image in pixels136// yuv_matrix: RGB to YUV conversion matrix. The matrix values typically137// depend on the input's rgb_bit_depth.138// This function calls SharpYuvConvertWithOptions with a default transfer139// function of kSharpYuvTransferFunctionSrgb.140SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr,141const void* b_ptr, int rgb_step,142int rgb_stride, int rgb_bit_depth,143void* y_ptr, int y_stride, void* u_ptr,144int u_stride, void* v_ptr, int v_stride,145int yuv_bit_depth, int width, int height,146const SharpYuvConversionMatrix* yuv_matrix);147148struct SharpYuvOptions {149// This matrix cannot be NULL and can be initialized by150// SharpYuvComputeConversionMatrix.151const SharpYuvConversionMatrix* yuv_matrix;152SharpYuvTransferFunctionType transfer_type;153};154155// Internal, version-checked, entry point156SHARPYUV_EXTERN int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix*,157SharpYuvOptions*, int);158159// Should always be called, to initialize a fresh SharpYuvOptions160// structure before modification. SharpYuvOptionsInit() must have succeeded161// before using the 'options' object.162static SHARPYUV_INLINE int SharpYuvOptionsInit(163const SharpYuvConversionMatrix* yuv_matrix, SharpYuvOptions* options) {164return SharpYuvOptionsInitInternal(yuv_matrix, options, SHARPYUV_VERSION);165}166167SHARPYUV_EXTERN int SharpYuvConvertWithOptions(168const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step,169int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, void* u_ptr,170int u_stride, void* v_ptr, int v_stride, int yuv_bit_depth, int width,171int height, const SharpYuvOptions* options);172173// TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422174// support (it's rarely used in practice, especially for images).175176#ifdef __cplusplus177} // extern "C"178#endif179180#endif // WEBP_SHARPYUV_SHARPYUV_H_181182183