mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-25 13:18:22 +01:00
Add YUV420 riskiness metric.
Basic version for 8 bit only and C only (no simd). Hidden behind a compile flag because the precompiled table is fairly large (114kiB) and more than triples the size of the library. Change-Id: I165c78e863df6a17b32f578bdbffe3adda9ac1d0
This commit is contained in:
parent
89c5b91746
commit
d7a0506dcc
@ -42,6 +42,7 @@ sharpyuv_srcs := \
|
||||
sharpyuv/sharpyuv_dsp.c \
|
||||
sharpyuv/sharpyuv_gamma.c \
|
||||
sharpyuv/sharpyuv_neon.$(NEON) \
|
||||
sharpyuv/sharpyuv_risk_table.c \
|
||||
sharpyuv/sharpyuv_sse2.c \
|
||||
|
||||
dec_srcs := \
|
||||
|
@ -112,6 +112,7 @@ model {
|
||||
include "sharpyuv_dsp.c"
|
||||
include "sharpyuv_gamma.c"
|
||||
include "sharpyuv_neon.c"
|
||||
include "sharpyuv_risk_table.c"
|
||||
include "sharpyuv_sse2.c"
|
||||
srcDir "src/dec"
|
||||
include "alpha_dec.c"
|
||||
|
@ -133,6 +133,7 @@ SHARPYUV_OBJS = \
|
||||
sharpyuv/sharpyuv_dsp.o \
|
||||
sharpyuv/sharpyuv_gamma.o \
|
||||
sharpyuv/sharpyuv_neon.o \
|
||||
sharpyuv/sharpyuv_risk_table.o \
|
||||
sharpyuv/sharpyuv_sse2.o \
|
||||
|
||||
DEC_OBJS = \
|
||||
|
@ -30,6 +30,7 @@ libsharpyuv_la_SOURCES += sharpyuv_cpu.c sharpyuv_cpu.h
|
||||
libsharpyuv_la_SOURCES += sharpyuv_csp.c sharpyuv_csp.h
|
||||
libsharpyuv_la_SOURCES += sharpyuv_dsp.c sharpyuv_dsp.h
|
||||
libsharpyuv_la_SOURCES += sharpyuv_gamma.c sharpyuv_gamma.h
|
||||
libsharpyuv_la_SOURCES += sharpyuv_risk_table.c sharpyuv_risk_table.h
|
||||
libsharpyuv_la_SOURCES += sharpyuv.c sharpyuv.h
|
||||
|
||||
libsharpyuv_la_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "sharpyuv/sharpyuv_cpu.h"
|
||||
#include "sharpyuv/sharpyuv_dsp.h"
|
||||
#include "sharpyuv/sharpyuv_gamma.h"
|
||||
#include "sharpyuv/sharpyuv_risk_table.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@ -298,7 +299,7 @@ static void* SafeMalloc(uint64_t nmemb, size_t size) {
|
||||
return malloc((size_t)total_size);
|
||||
}
|
||||
|
||||
#define SAFE_ALLOC(W, H, T) ((T*)SafeMalloc((W) * (H), sizeof(T)))
|
||||
#define SAFE_ALLOC(W, H, T) ((T*)SafeMalloc((uint64_t)(W) * (H), sizeof(T)))
|
||||
|
||||
static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr,
|
||||
const uint8_t* b_ptr, int rgb_step, int rgb_stride,
|
||||
@ -434,7 +435,6 @@ static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr,
|
||||
free(tmp_buffer);
|
||||
return ok;
|
||||
}
|
||||
#undef SAFE_ALLOC
|
||||
|
||||
#if defined(WEBP_USE_THREAD) && !defined(_WIN32)
|
||||
#include <pthread.h> // NOLINT
|
||||
@ -571,3 +571,105 @@ int SharpYuvConvertWithOptions(const void* r_ptr, const void* g_ptr,
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// 420 risk metric
|
||||
|
||||
static int DoEstimateRisk(const uint8_t* r_ptr, const uint8_t* g_ptr,
|
||||
const uint8_t* b_ptr, int rgb_step, int rgb_stride,
|
||||
int rgb_bit_depth, int width, int height,
|
||||
const SharpYuvOptions* options,
|
||||
const uint8_t precomputed_scores_table[],
|
||||
int precomputed_scores_table_sampling,
|
||||
float* score_out) {
|
||||
const int sampling3 = precomputed_scores_table_sampling *
|
||||
precomputed_scores_table_sampling *
|
||||
precomputed_scores_table_sampling;
|
||||
const int kNoiseLevel = 4;
|
||||
double total_score = 0;
|
||||
double count = 0;
|
||||
// Rows of indices in
|
||||
uint16_t* row1 = SAFE_ALLOC(width, 1, uint16_t);
|
||||
uint16_t* row2 = SAFE_ALLOC(width, 1, uint16_t);
|
||||
uint16_t* tmp;
|
||||
int i, j;
|
||||
|
||||
if (row1 == NULL || row2 == NULL) {
|
||||
free(row1);
|
||||
free(row2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Convert the first row ahead.
|
||||
SharpYuvRowToYuvSharpnessIndex(r_ptr, g_ptr, b_ptr, rgb_step, rgb_bit_depth,
|
||||
width, row2, options->yuv_matrix,
|
||||
precomputed_scores_table_sampling);
|
||||
|
||||
for (j = 1; j < height; ++j) {
|
||||
r_ptr += rgb_stride;
|
||||
g_ptr += rgb_stride;
|
||||
b_ptr += rgb_stride;
|
||||
// Swap row 1 and row 2.
|
||||
tmp = row1;
|
||||
row1 = row2;
|
||||
row2 = tmp;
|
||||
// Convert the row below.
|
||||
SharpYuvRowToYuvSharpnessIndex(r_ptr, g_ptr, b_ptr, rgb_step, rgb_bit_depth,
|
||||
width, row2, options->yuv_matrix,
|
||||
precomputed_scores_table_sampling);
|
||||
for (i = 0; i < width - 1; ++i) {
|
||||
const int idx0 = row1[i + 0];
|
||||
const int idx1 = row1[i + 1];
|
||||
const int idx2 = row2[i + 0];
|
||||
const int score = precomputed_scores_table[idx0 + sampling3 * idx1] +
|
||||
precomputed_scores_table[idx0 + sampling3 * idx2] +
|
||||
precomputed_scores_table[idx1 + sampling3 * idx2];
|
||||
if (score > kNoiseLevel) {
|
||||
total_score += score;
|
||||
count += 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count > 0.) total_score /= count;
|
||||
|
||||
// If less than 1% of pixels were evaluated -> below noise level.
|
||||
if (100. * count / (width * height) < 1.) total_score = 0.;
|
||||
|
||||
// Rescale to [0:100]
|
||||
total_score = (total_score > 25.) ? 100. : total_score * 100. / 25.;
|
||||
|
||||
free(row1);
|
||||
free(row2);
|
||||
|
||||
*score_out = (float)total_score;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SharpYuvEstimate420Risk(const void* r_ptr, const void* g_ptr,
|
||||
const void* b_ptr, int rgb_step, int rgb_stride,
|
||||
int rgb_bit_depth, int width, int height,
|
||||
const SharpYuvOptions* options, float* score) {
|
||||
if (width < 1 || height < 1 || width == INT_MAX || height == INT_MAX ||
|
||||
r_ptr == NULL || g_ptr == NULL || b_ptr == NULL || options == NULL ||
|
||||
score == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (rgb_bit_depth != 8) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (width <= 4 || height <= 4) {
|
||||
*score = 0.0f; // too small, no real risk.
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The address of the function pointer is used to avoid a read race.
|
||||
SharpYuvInit((VP8CPUInfo)&SharpYuvGetCPUInfo);
|
||||
|
||||
return DoEstimateRisk(
|
||||
(const uint8_t*)r_ptr, (const uint8_t*)g_ptr, (const uint8_t*)b_ptr,
|
||||
rgb_step, rgb_stride, rgb_bit_depth, width, height, options,
|
||||
kSharpYuvPrecomputedRisk, kSharpYuvPrecomputedRiskYuvSampling, score);
|
||||
}
|
||||
|
||||
#undef SAFE_ALLOC
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -53,7 +53,7 @@ extern "C" {
|
||||
|
||||
// SharpYUV API version following the convention from semver.org
|
||||
#define SHARPYUV_VERSION_MAJOR 0
|
||||
#define SHARPYUV_VERSION_MINOR 4
|
||||
#define SHARPYUV_VERSION_MINOR 5
|
||||
#define SHARPYUV_VERSION_PATCH 0
|
||||
// Version as a uint32_t. The major number is the high 8 bits.
|
||||
// The minor number is the middle 8 bits. The patch number is the low 16 bits.
|
||||
@ -164,6 +164,36 @@ SHARPYUV_EXTERN int SharpYuvConvertWithOptions(
|
||||
int u_stride, void* v_ptr, int v_stride, int yuv_bit_depth, int width,
|
||||
int height, const SharpYuvOptions* options);
|
||||
|
||||
// Computes a score between 0 and 100 which represents the risk of having visual
|
||||
// quality loss from converting an RGB image to YUV420.
|
||||
// A low score, typically < 40, means there is a low risk of artifacts from
|
||||
// chroma subsampling and a simple averaging algorithm can be used instead of
|
||||
// the more expensive SharpYuvConvert function.
|
||||
// A medium score, typically >= 40 and < 70, means that simple chroma
|
||||
// subsampling will produce artifacts and it may be advisable to use the more
|
||||
// costly SharpYuvConvert for YUV420 conversion.
|
||||
// A high score, typically >= 70, means there is a very high risk of artifacts
|
||||
// from chroma subsampling even with SharpYuvConvert, and best results might be
|
||||
// achieved by using YUV444.
|
||||
// If not using SharpYuvConvert, a threshold of about 50 can be used to decide
|
||||
// between (simple averaging) 420 and 444.
|
||||
// r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
|
||||
// to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
|
||||
// rgb_step: distance in bytes between two horizontally adjacent pixels on the
|
||||
// r, g and b channels. If rgb_bit_depth is > 8, it should be a
|
||||
// multiple of 2.
|
||||
// rgb_stride: distance in bytes between two vertically adjacent pixels on the
|
||||
// r, g, and b channels. If rgb_bit_depth is > 8, it should be a
|
||||
// multiple of 2.
|
||||
// rgb_bit_depth: number of bits for each r/g/b value. Only a value of 8 is
|
||||
// currently supported.
|
||||
// width, height: width and height of the image in pixels
|
||||
// Returns 0 on failure.
|
||||
SHARPYUV_EXTERN int SharpYuvEstimate420Risk(
|
||||
const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step,
|
||||
int rgb_stride, int rgb_bit_depth, int width, int height,
|
||||
const SharpYuvOptions* options, float* score);
|
||||
|
||||
// TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
|
||||
// support (it's rarely used in practice, especially for images).
|
||||
|
||||
|
@ -16,7 +16,9 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sharpyuv/sharpyuv.h"
|
||||
#include "sharpyuv/sharpyuv_cpu.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
@ -62,6 +64,58 @@ static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
|
||||
}
|
||||
#endif // !WEBP_NEON_OMIT_C_CODE
|
||||
|
||||
#define YUV_FIX 16 // fixed-point precision for RGB->YUV
|
||||
static const int kYuvHalf = 1 << (YUV_FIX - 1);
|
||||
|
||||
// Maps a value in [0, (256 << YUV_FIX) - 1] to [0,
|
||||
// precomputed_scores_table_sampling - 1]. It is important that the extremal
|
||||
// values are preserved and 1:1 mapped:
|
||||
// ConvertValue(0) = 0
|
||||
// ConvertValue((256 << 16) - 1) = rgb_sampling_size - 1
|
||||
static int SharpYuvConvertValueToSampledIdx(int v, int rgb_sampling_size) {
|
||||
v = (v + kYuvHalf) >> YUV_FIX;
|
||||
v = (v < 0) ? 0 : (v > 255) ? 255 : v;
|
||||
return (v * (rgb_sampling_size - 1)) / 255;
|
||||
}
|
||||
|
||||
#undef YUV_FIX
|
||||
|
||||
static int SharpYuvConvertToYuvSharpnessIndex(
|
||||
int r, int g, int b, const SharpYuvConversionMatrix* matrix,
|
||||
int precomputed_scores_table_sampling) {
|
||||
const int y = SharpYuvConvertValueToSampledIdx(
|
||||
matrix->rgb_to_y[0] * r + matrix->rgb_to_y[1] * g +
|
||||
matrix->rgb_to_y[2] * b + matrix->rgb_to_y[3],
|
||||
precomputed_scores_table_sampling);
|
||||
const int u = SharpYuvConvertValueToSampledIdx(
|
||||
matrix->rgb_to_u[0] * r + matrix->rgb_to_u[1] * g +
|
||||
matrix->rgb_to_u[2] * b + matrix->rgb_to_u[3],
|
||||
precomputed_scores_table_sampling);
|
||||
const int v = SharpYuvConvertValueToSampledIdx(
|
||||
matrix->rgb_to_v[0] * r + matrix->rgb_to_v[1] * g +
|
||||
matrix->rgb_to_v[2] * b + matrix->rgb_to_v[3],
|
||||
precomputed_scores_table_sampling);
|
||||
return y + u * precomputed_scores_table_sampling +
|
||||
v * precomputed_scores_table_sampling *
|
||||
precomputed_scores_table_sampling;
|
||||
}
|
||||
|
||||
static void SharpYuvRowToYuvSharpnessIndex_C(
|
||||
const uint8_t* r_ptr, const uint8_t* g_ptr, const uint8_t* b_ptr,
|
||||
int rgb_step, int rgb_bit_depth, int width, uint16_t* dst,
|
||||
const SharpYuvConversionMatrix* matrix,
|
||||
int precomputed_scores_table_sampling) {
|
||||
int i;
|
||||
assert(rgb_bit_depth == 8);
|
||||
(void)rgb_bit_depth; // Unused for now.
|
||||
for (i = 0; i < width;
|
||||
++i, r_ptr += rgb_step, g_ptr += rgb_step, b_ptr += rgb_step) {
|
||||
dst[i] =
|
||||
SharpYuvConvertToYuvSharpnessIndex(r_ptr[0], g_ptr[0], b_ptr[0], matrix,
|
||||
precomputed_scores_table_sampling);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
|
||||
@ -69,8 +123,14 @@ uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
|
||||
void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
|
||||
int len);
|
||||
void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
|
||||
const uint16_t* best_y, uint16_t* out,
|
||||
int bit_depth);
|
||||
const uint16_t* best_y, uint16_t* out, int bit_depth);
|
||||
void (*SharpYuvRowToYuvSharpnessIndex)(const uint8_t* r_ptr,
|
||||
const uint8_t* g_ptr,
|
||||
const uint8_t* b_ptr, int rgb_step,
|
||||
int rgb_bit_depth, int width,
|
||||
uint16_t* dst,
|
||||
const SharpYuvConversionMatrix* matrix,
|
||||
int precomputed_scores_table_sampling);
|
||||
|
||||
extern VP8CPUInfo SharpYuvGetCPUInfo;
|
||||
extern void InitSharpYuvSSE2(void);
|
||||
@ -82,6 +142,8 @@ void SharpYuvInitDsp(void) {
|
||||
SharpYuvUpdateRGB = SharpYuvUpdateRGB_C;
|
||||
SharpYuvFilterRow = SharpYuvFilterRow_C;
|
||||
#endif
|
||||
// There is only a C version for now so always include it.
|
||||
SharpYuvRowToYuvSharpnessIndex = SharpYuvRowToYuvSharpnessIndex_C;
|
||||
|
||||
if (SharpYuvGetCPUInfo != NULL) {
|
||||
#if defined(WEBP_HAVE_SSE2)
|
||||
|
@ -12,6 +12,7 @@
|
||||
#ifndef WEBP_SHARPYUV_SHARPYUV_DSP_H_
|
||||
#define WEBP_SHARPYUV_SHARPYUV_DSP_H_
|
||||
|
||||
#include "sharpyuv/sharpyuv.h"
|
||||
#include "sharpyuv/sharpyuv_cpu.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
@ -23,6 +24,15 @@ extern void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
|
||||
const uint16_t* best_y, uint16_t* out,
|
||||
int bit_depth);
|
||||
|
||||
// For each pixel, computes the index to look up that color in a precomputed
|
||||
// risk score table where the YUV space is subsampled to a size of
|
||||
// precomputed_scores_table_sampling^3 (see sharpyuv_risk_table.h)
|
||||
extern void (*SharpYuvRowToYuvSharpnessIndex)(
|
||||
const uint8_t* r_ptr, const uint8_t* g_ptr, const uint8_t* b_ptr,
|
||||
int rgb_step, int rgb_bit_depth, int width, uint16_t* dst,
|
||||
const SharpYuvConversionMatrix* matrix,
|
||||
int precomputed_scores_table_sampling);
|
||||
|
||||
void SharpYuvInitDsp(void);
|
||||
|
||||
#endif // WEBP_SHARPYUV_SHARPYUV_DSP_H_
|
||||
|
6210
sharpyuv/sharpyuv_risk_table.c
Normal file
6210
sharpyuv/sharpyuv_risk_table.c
Normal file
File diff suppressed because it is too large
Load Diff
27
sharpyuv/sharpyuv_risk_table.h
Normal file
27
sharpyuv/sharpyuv_risk_table.h
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright 2023 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the COPYING file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Precomputed data for 420 risk estimation.
|
||||
|
||||
#ifndef WEBP_SHARPYUV_SHARPYUV_RISK_TABLE_H_
|
||||
#define WEBP_SHARPYUV_SHARPYUV_RISK_TABLE_H_
|
||||
|
||||
#include "src/webp/types.h"
|
||||
|
||||
extern const int kSharpYuvPrecomputedRiskYuvSampling;
|
||||
// Table of precomputed risk scores when chroma subsampling images with two
|
||||
// given colors.
|
||||
// Since precomputing values for all possible YUV colors would create a huge
|
||||
// table, the YUV space (i.e. [0, 255]^3) is reduced to
|
||||
// [0, kSharpYuvPrecomputedRiskYuvSampling-1]^3
|
||||
// where 255 maps to kSharpYuvPrecomputedRiskYuvSampling-1.
|
||||
// Table size: kSharpYuvPrecomputedRiskYuvSampling^6 bytes or 114 KiB
|
||||
extern const uint8_t kSharpYuvPrecomputedRisk[];
|
||||
|
||||
#endif // WEBP_SHARPYUV_SHARPYUV_RISK_TABLE_H_
|
Loading…
Reference in New Issue
Block a user