Skip to content

Commit 33c0c3b

Browse files
Additional AVIF options (GoogleChromeLabs#987)
* Separate colour & alpha options for AVIF * Adding broken noise synthesis * Build files * Updating build * Changing speed default * Remove old comment * Better speed default
1 parent e900d33 commit 33c0c3b

13 files changed

Lines changed: 131 additions & 90 deletions

File tree

codecs/avif/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Tracking the version in google3/third_party/libavif
2-
CODEC_URL = https://github.com/AOMediaCodec/libavif/archive/f56df6d4c99928af484634af65cdbd62bac2dd6e.tar.gz
1+
# libavif and libaom versions are from
2+
# https://docs.google.com/document/d/1wEEA5rRU7wT54k41u3qyZIZHDCJArIMzLuzsrLAwaK8/edit
3+
CODEC_URL = https://github.com/AOMediaCodec/libavif/archive/d37ef74127986184500e571bf1f9793cc0bdef50.tar.gz
34
CODEC_PACKAGE = node_modules/libavif.tar.gz
45

5-
# Tracking the version in google3/third_party/libaom
6-
LIBAOM_URL = https://aomedia.googlesource.com/aom/+archive/4a8e276e3e0ef3c76083f3975d5caa85bc9593ce.tar.gz
6+
LIBAOM_URL = https://aomedia.googlesource.com/aom/+archive/0a5da45c7f942908974f5ab8e107c9fa82048ae7.tar.gz
77
LIBAOM_PACKAGE = node_modules/libaom.tar.gz
88

99
export CODEC_DIR = node_modules/libavif

codecs/avif/dec/avif_dec.wasm

-229 Bytes
Binary file not shown.

codecs/avif/dec/avif_node_dec.wasm

-229 Bytes
Binary file not shown.

codecs/avif/enc/avif_enc.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ struct AvifOptions {
1010
// 0 = lossless
1111
// 63 = worst quality
1212
int cqLevel;
13-
int minQuantizerAlpha;
13+
// As above, but -1 means 'use cqLevel'
14+
int cqAlphaLevel;
1415
// [0 - 6]
1516
// Creates 2^n tiles in that dimension
1617
int tileRowsLog2;
@@ -30,6 +31,8 @@ struct AvifOptions {
3031
int sharpness;
3132
// Target ssim rather than psnr
3233
bool targetSsim;
34+
// 0-50
35+
int denoiseLevel;
3336
};
3437

3538
thread_local const val Uint8Array = val::global("Uint8Array");
@@ -54,8 +57,9 @@ val encode(std::string buffer, int width, int height, AvifOptions options) {
5457
}
5558

5659
bool lossless = options.cqLevel == AVIF_QUANTIZER_LOSSLESS &&
57-
options.minQuantizerAlpha == AVIF_QUANTIZER_LOSSLESS &&
60+
options.cqAlphaLevel <= AVIF_QUANTIZER_LOSSLESS &&
5861
format == AVIF_PIXEL_FORMAT_YUV444;
62+
5963
avifImage* image = avifImageCreate(width, height, depth, format);
6064

6165
if (lossless) {
@@ -82,20 +86,28 @@ val encode(std::string buffer, int width, int height, AvifOptions options) {
8286
} else {
8387
encoder->minQuantizer = AVIF_QUANTIZER_BEST_QUALITY;
8488
encoder->maxQuantizer = AVIF_QUANTIZER_WORST_QUALITY;
85-
encoder->minQuantizerAlpha = options.minQuantizerAlpha;
89+
encoder->minQuantizerAlpha = AVIF_QUANTIZER_BEST_QUALITY;
8690
encoder->maxQuantizerAlpha = AVIF_QUANTIZER_WORST_QUALITY;
8791
avifEncoderSetCodecSpecificOption(encoder, "end-usage", "q");
8892
avifEncoderSetCodecSpecificOption(encoder, "cq-level", std::to_string(options.cqLevel).c_str());
8993
avifEncoderSetCodecSpecificOption(encoder, "sharpness",
9094
std::to_string(options.sharpness).c_str());
9195

96+
if (options.cqAlphaLevel != -1) {
97+
avifEncoderSetCodecSpecificOption(encoder, "alpha:cq-level",
98+
std::to_string(options.cqAlphaLevel).c_str());
99+
}
100+
92101
if (options.targetSsim) {
93102
avifEncoderSetCodecSpecificOption(encoder, "tune", "ssim");
94103
}
95104

96105
if (options.chromaDeltaQ) {
97106
avifEncoderSetCodecSpecificOption(encoder, "enable-chroma-deltaq", "1");
98107
}
108+
109+
avifEncoderSetCodecSpecificOption(encoder, "color:denoise-noise-level",
110+
std::to_string(options.denoiseLevel).c_str());
99111
}
100112

101113
encoder->maxThreads = emscripten_num_logical_cores();
@@ -118,13 +130,14 @@ val encode(std::string buffer, int width, int height, AvifOptions options) {
118130
EMSCRIPTEN_BINDINGS(my_module) {
119131
value_object<AvifOptions>("AvifOptions")
120132
.field("cqLevel", &AvifOptions::cqLevel)
121-
.field("minQuantizerAlpha", &AvifOptions::minQuantizerAlpha)
133+
.field("cqAlphaLevel", &AvifOptions::cqAlphaLevel)
122134
.field("tileRowsLog2", &AvifOptions::tileRowsLog2)
123135
.field("tileColsLog2", &AvifOptions::tileColsLog2)
124136
.field("speed", &AvifOptions::speed)
125137
.field("chromaDeltaQ", &AvifOptions::chromaDeltaQ)
126138
.field("sharpness", &AvifOptions::sharpness)
127139
.field("targetSsim", &AvifOptions::targetSsim)
140+
.field("denoiseLevel", &AvifOptions::denoiseLevel)
128141
.field("subsample", &AvifOptions::subsample);
129142

130143
function("encode", &encode);

codecs/avif/enc/avif_enc.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface EncodeOptions {
22
cqLevel: number;
3-
minQuantizerAlpha: number;
3+
denoiseLevel: number;
4+
cqAlphaLevel: number;
45
tileRowsLog2: number;
56
tileColsLog2: number;
67
speed: number;

codecs/avif/enc/avif_enc.js

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

codecs/avif/enc/avif_enc.wasm

6.65 KB
Binary file not shown.

codecs/avif/enc/avif_enc_mt.js

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

codecs/avif/enc/avif_enc_mt.wasm

6.35 KB
Binary file not shown.

codecs/avif/enc/avif_node_enc.js

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

0 commit comments

Comments
 (0)