Skip to content

Commit a1be950

Browse files
committed
Big rework to support SVT, libavif, kvazaar
1 parent b477314 commit a1be950

File tree

9 files changed

+524
-1
lines changed

9 files changed

+524
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ SVT-AV1/
5858
/SVT-AV1
5959
kvazaar/
6060
kvazaar
61-
x265_git
61+
x265_git
62+
SVT-AV1/
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license
4+
// that can be found in the COPYING file in the root of the source
5+
// tree. An additional intellectual property rights grant can be found
6+
// in the file PATENTS. All contributing project authors may
7+
// be found in the AUTHORS file in the root of the source tree.
8+
// -----------------------------------------------------------------------------
9+
//
10+
// Sharp RGB to YUV conversion.
11+
12+
#ifndef WEBP_SHARPYUV_SHARPYUV_H_
13+
#define WEBP_SHARPYUV_SHARPYUV_H_
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
#ifndef SHARPYUV_EXTERN
20+
#ifdef WEBP_EXTERN
21+
#define SHARPYUV_EXTERN WEBP_EXTERN
22+
#else
23+
// This explicitly marks library functions and allows for changing the
24+
// signature for e.g., Windows DLL builds.
25+
#if defined(_WIN32) && defined(WEBP_DLL)
26+
#define SHARPYUV_EXTERN __declspec(dllexport)
27+
#elif defined(__GNUC__) && __GNUC__ >= 4
28+
#define SHARPYUV_EXTERN extern __attribute__((visibility("default")))
29+
#else
30+
#define SHARPYUV_EXTERN extern
31+
#endif /* defined(_WIN32) && defined(WEBP_DLL) */
32+
#endif /* WEBP_EXTERN */
33+
#endif /* SHARPYUV_EXTERN */
34+
35+
#ifndef SHARPYUV_INLINE
36+
#ifdef WEBP_INLINE
37+
#define SHARPYUV_INLINE WEBP_INLINE
38+
#else
39+
#ifndef _MSC_VER
40+
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
41+
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
42+
#define SHARPYUV_INLINE inline
43+
#else
44+
#define SHARPYUV_INLINE
45+
#endif
46+
#else
47+
#define SHARPYUV_INLINE __forceinline
48+
#endif /* _MSC_VER */
49+
#endif /* WEBP_INLINE */
50+
#endif /* SHARPYUV_INLINE */
51+
52+
// SharpYUV API version following the convention from semver.org
53+
#define SHARPYUV_VERSION_MAJOR 0
54+
#define SHARPYUV_VERSION_MINOR 4
55+
#define SHARPYUV_VERSION_PATCH 0
56+
// Version as a uint32_t. The major number is the high 8 bits.
57+
// The minor number is the middle 8 bits. The patch number is the low 16 bits.
58+
#define SHARPYUV_MAKE_VERSION(MAJOR, MINOR, PATCH) \
59+
(((MAJOR) << 24) | ((MINOR) << 16) | (PATCH))
60+
#define SHARPYUV_VERSION \
61+
SHARPYUV_MAKE_VERSION(SHARPYUV_VERSION_MAJOR, SHARPYUV_VERSION_MINOR, \
62+
SHARPYUV_VERSION_PATCH)
63+
64+
// Returns the library's version number, packed in hexadecimal. See
65+
// SHARPYUV_VERSION.
66+
SHARPYUV_EXTERN int SharpYuvGetVersion(void);
67+
68+
// RGB to YUV conversion matrix, in 16 bit fixed point.
69+
// y = rgb_to_y[0] * r + rgb_to_y[1] * g + rgb_to_y[2] * b + rgb_to_y[3]
70+
// u = rgb_to_u[0] * r + rgb_to_u[1] * g + rgb_to_u[2] * b + rgb_to_u[3]
71+
// v = rgb_to_v[0] * r + rgb_to_v[1] * g + rgb_to_v[2] * b + rgb_to_v[3]
72+
// Then y, u and v values are divided by 1<<16 and rounded.
73+
typedef struct {
74+
int rgb_to_y[4];
75+
int rgb_to_u[4];
76+
int rgb_to_v[4];
77+
} SharpYuvConversionMatrix;
78+
79+
typedef struct SharpYuvOptions SharpYuvOptions;
80+
81+
// Enums for transfer functions, as defined in H.273,
82+
// https://www.itu.int/rec/T-REC-H.273-202107-I/en
83+
typedef enum SharpYuvTransferFunctionType {
84+
// 0 is reserved
85+
kSharpYuvTransferFunctionBt709 = 1,
86+
// 2 is unspecified
87+
// 3 is reserved
88+
kSharpYuvTransferFunctionBt470M = 4,
89+
kSharpYuvTransferFunctionBt470Bg = 5,
90+
kSharpYuvTransferFunctionBt601 = 6,
91+
kSharpYuvTransferFunctionSmpte240 = 7,
92+
kSharpYuvTransferFunctionLinear = 8,
93+
kSharpYuvTransferFunctionLog100 = 9,
94+
kSharpYuvTransferFunctionLog100_Sqrt10 = 10,
95+
kSharpYuvTransferFunctionIec61966 = 11,
96+
kSharpYuvTransferFunctionBt1361 = 12,
97+
kSharpYuvTransferFunctionSrgb = 13,
98+
kSharpYuvTransferFunctionBt2020_10Bit = 14,
99+
kSharpYuvTransferFunctionBt2020_12Bit = 15,
100+
kSharpYuvTransferFunctionSmpte2084 = 16, // PQ
101+
kSharpYuvTransferFunctionSmpte428 = 17,
102+
kSharpYuvTransferFunctionHlg = 18,
103+
kSharpYuvTransferFunctionNum
104+
} SharpYuvTransferFunctionType;
105+
106+
// Converts RGB to YUV420 using a downsampling algorithm that minimizes
107+
// artefacts caused by chroma subsampling.
108+
// This is slower than standard downsampling (averaging of 4 UV values).
109+
// Assumes that the image will be upsampled using a bilinear filter. If nearest
110+
// neighbor is used instead, the upsampled image might look worse than with
111+
// standard downsampling.
112+
// r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
113+
// to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
114+
// rgb_step: distance in bytes between two horizontally adjacent pixels on the
115+
// r, g and b channels. If rgb_bit_depth is > 8, it should be a
116+
// multiple of 2.
117+
// rgb_stride: distance in bytes between two vertically adjacent pixels on the
118+
// r, g, and b channels. If rgb_bit_depth is > 8, it should be a
119+
// multiple of 2.
120+
// rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.
121+
// Note: 16 bit input is truncated to 14 bits before conversion to yuv.
122+
// yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.
123+
// y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should
124+
// point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers
125+
// otherwise.
126+
// y_stride, u_stride, v_stride: distance in bytes between two vertically
127+
// adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they
128+
// should be multiples of 2.
129+
// width, height: width and height of the image in pixels
130+
// This function calls SharpYuvConvertWithOptions with a default transfer
131+
// function of kSharpYuvTransferFunctionSrgb.
132+
SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr,
133+
const void* b_ptr, int rgb_step,
134+
int rgb_stride, int rgb_bit_depth,
135+
void* y_ptr, int y_stride, void* u_ptr,
136+
int u_stride, void* v_ptr, int v_stride,
137+
int yuv_bit_depth, int width, int height,
138+
const SharpYuvConversionMatrix* yuv_matrix);
139+
140+
struct SharpYuvOptions {
141+
// This matrix cannot be NULL and can be initialized by
142+
// SharpYuvComputeConversionMatrix.
143+
const SharpYuvConversionMatrix* yuv_matrix;
144+
SharpYuvTransferFunctionType transfer_type;
145+
};
146+
147+
// Internal, version-checked, entry point
148+
SHARPYUV_EXTERN int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix*,
149+
SharpYuvOptions*, int);
150+
151+
// Should always be called, to initialize a fresh SharpYuvOptions
152+
// structure before modification. SharpYuvOptionsInit() must have succeeded
153+
// before using the 'options' object.
154+
static SHARPYUV_INLINE int SharpYuvOptionsInit(
155+
const SharpYuvConversionMatrix* yuv_matrix, SharpYuvOptions* options) {
156+
return SharpYuvOptionsInitInternal(yuv_matrix, options, SHARPYUV_VERSION);
157+
}
158+
159+
SHARPYUV_EXTERN int SharpYuvConvertWithOptions(
160+
const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step,
161+
int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, void* u_ptr,
162+
int u_stride, void* v_ptr, int v_stride, int yuv_bit_depth, int width,
163+
int height, const SharpYuvOptions* options);
164+
165+
// TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
166+
// support (it's rarely used in practice, especially for images).
167+
168+
#ifdef __cplusplus
169+
} // extern "C"
170+
#endif
171+
172+
#endif // WEBP_SHARPYUV_SHARPYUV_H_
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license
4+
// that can be found in the COPYING file in the root of the source
5+
// tree. An additional intellectual property rights grant can be found
6+
// in the file PATENTS. All contributing project authors may
7+
// be found in the AUTHORS file in the root of the source tree.
8+
// -----------------------------------------------------------------------------
9+
//
10+
// Colorspace utilities.
11+
12+
#ifndef WEBP_SHARPYUV_SHARPYUV_CSP_H_
13+
#define WEBP_SHARPYUV_SHARPYUV_CSP_H_
14+
15+
#include <sharpyuv/sharpyuv.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
// Range of YUV values.
22+
typedef enum {
23+
kSharpYuvRangeFull, // YUV values between [0;255] (for 8 bit)
24+
kSharpYuvRangeLimited // Y in [16;235], YUV in [16;240] (for 8 bit)
25+
} SharpYuvRange;
26+
27+
// Constants that define a YUV color space.
28+
typedef struct {
29+
// Kr and Kb are defined such that:
30+
// Y = Kr * r + Kg * g + Kb * b where Kg = 1 - Kr - Kb.
31+
float kr;
32+
float kb;
33+
int bit_depth; // 8, 10 or 12
34+
SharpYuvRange range;
35+
} SharpYuvColorSpace;
36+
37+
// Fills in 'matrix' for the given YUVColorSpace.
38+
SHARPYUV_EXTERN void SharpYuvComputeConversionMatrix(
39+
const SharpYuvColorSpace* yuv_color_space,
40+
SharpYuvConversionMatrix* matrix);
41+
42+
// Enums for precomputed conversion matrices.
43+
typedef enum {
44+
kSharpYuvMatrixWebp = 0,
45+
kSharpYuvMatrixRec601Limited,
46+
kSharpYuvMatrixRec601Full,
47+
kSharpYuvMatrixRec709Limited,
48+
kSharpYuvMatrixRec709Full,
49+
kSharpYuvMatrixNum
50+
} SharpYuvMatrixType;
51+
52+
// Returns a pointer to a matrix for one of the predefined colorspaces.
53+
SHARPYUV_EXTERN const SharpYuvConversionMatrix* SharpYuvGetConversionMatrix(
54+
SharpYuvMatrixType matrix_type);
55+
56+
#ifdef __cplusplus
57+
} // extern "C"
58+
#endif
59+
60+
#endif // WEBP_SHARPYUV_SHARPYUV_CSP_H_

avifpixart/Cargo.lock

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

avifpixart/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "avifweaver"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
num-traits = "0.2.19"
8+
pic-scale = { version = "0.2.11", default-features = false }
9+
10+
[target.'cfg(target_arch = "aarch64")'.dependencies]
11+
pic-scale = { version = "0.2.11", default-features = false }
12+
13+
[target.'cfg(target_arch = "x86")'.dependencies]
14+
pic-scale = { version = "0.2.11", default-features = false, features = ["disable_simd"] }
15+
16+
[target.'cfg(target_arch = "x86_64")'.dependencies]
17+
pic-scale = { version = "0.2.11", default-features = false, features = ["disable_simd"] }
18+
19+
[lib]
20+
crate-type = ["cdylib"]

avifpixart/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cbindgen --config cbindgen.toml --crate avifweaver --output include/avifweaver.h

avifpixart/cbindgen.toml

Whitespace-only changes.

avifpixart/include/avifweaver.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <cstdarg>
2+
#include <cstdint>
3+
#include <cstdlib>
4+
#include <ostream>
5+
#include <new>
6+
7+
extern "C" {
8+
9+
void weave_scale_u8(const uint8_t *src,
10+
uint32_t src_stride,
11+
uint32_t width,
12+
uint32_t height,
13+
const uint8_t *dst,
14+
uint32_t dst_stride,
15+
uint32_t new_width,
16+
uint32_t new_height,
17+
bool high_precision);
18+
19+
void weave_scale_u16(const uint16_t *src,
20+
uintptr_t src_stride,
21+
uint32_t width,
22+
uint32_t height,
23+
const uint16_t *dst,
24+
uint32_t new_width,
25+
uint32_t new_height,
26+
uintptr_t bit_depth,
27+
bool high_precision);
28+
29+
} // extern "C"

0 commit comments

Comments
 (0)