Skip to content

Commit 1346d9c

Browse files
authored
avifenc: make alpha quality be the same as the color quality by default. (#2548)
If only the color quality is set, alpha quality is automatically set to the same value.
1 parent 842a2ed commit 1346d9c

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The changes are relative to the previous release, unless the baseline is specifi
4949
flags. `-q` or `--qcolor` and `--qalpha` should be used instead.
5050
* For dependencies, the deprecated way of setting AVIF_LOCAL_* to ON is
5151
removed. Dependency options can now only be set to OFF/LOCAL/SYSTEM.
52+
* Change the default quality for alpha to be the same as the quality for color.
5253

5354
## [1.1.1] - 2024-07-30
5455

apps/avifenc.c

+27-10
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ static avifBool avifImageSplitGrid(const avifImage * gridSplitImage, uint32_t gr
807807

808808
#define INVALID_QUALITY (-1)
809809
#define DEFAULT_QUALITY 60 // Maps to a quantizer (QP) of 25.
810-
#define DEFAULT_QUALITY_ALPHA AVIF_QUALITY_LOSSLESS
811810
#define DEFAULT_QUALITY_GAIN_MAP DEFAULT_QUALITY
812811
#define PROGRESSIVE_WORST_QUALITY 10 // Not doing auto automatic layered encoding below this quality
813812
#define PROGRESSIVE_START_QUALITY 2 // First layer use this quality
@@ -925,12 +924,6 @@ static avifBool avifEncodeRestOfImageSequence(avifEncoder * encoder,
925924
while ((nextFile = avifInputGetFile(input, imageIndex)) != NULL) {
926925
uint64_t nextDurationInTimescales = nextFile->duration ? nextFile->duration : settings->outputTiming.duration;
927926

928-
printf(" * Encoding frame %d [%" PRIu64 "/%" PRIu64 " ts]: %s\n",
929-
imageIndex,
930-
nextDurationInTimescales,
931-
settings->outputTiming.timescale,
932-
nextFile->filename);
933-
934927
if (nextImage) {
935928
avifImageDestroy(nextImage);
936929
}
@@ -969,6 +962,17 @@ static avifBool avifEncodeRestOfImageSequence(avifEncoder * encoder,
969962
if (!avifEncodeUpdateEncoderSettings(encoder, nextSettings)) {
970963
goto cleanup;
971964
}
965+
966+
printf(" * Encoding frame %d [%" PRIu64 "/%" PRIu64 " ts] color quality [%d (%s)], alpha quality [%d (%s)]: %s\n",
967+
imageIndex,
968+
nextDurationInTimescales,
969+
settings->outputTiming.timescale,
970+
encoder->quality,
971+
qualityString(encoder->quality),
972+
encoder->qualityAlpha,
973+
qualityString(encoder->qualityAlpha),
974+
nextFile->filename);
975+
972976
const avifResult nextImageResult = avifEncoderAddImage(encoder, nextImage, nextDurationInTimescales, AVIF_ADD_IMAGE_FLAG_NONE);
973977
if (nextImageResult != AVIF_RESULT_OK) {
974978
fprintf(stderr, "ERROR: Failed to encode image: %s\n", avifResultToString(nextImageResult));
@@ -1150,7 +1154,7 @@ static avifBool avifEncodeImagesFixedQuality(const avifSettings * settings,
11501154
}
11511155
#endif
11521156

1153-
printf("Encoding with codec '%s' speed [%s], color quality [%d (%s)], alpha quality [%d (%s)]%s, %s, %d worker thread(s), please wait...\n",
1157+
printf("Encoding with initial settings: codec '%s' speed [%s], color quality [%d (%s)], alpha quality [%d (%s)]%s, %s, %d worker thread(s), please wait...\n",
11541158
codecName ? codecName : "none",
11551159
speedStr,
11561160
encoder->quality,
@@ -1195,10 +1199,14 @@ static avifBool avifEncodeImagesFixedQuality(const avifSettings * settings,
11951199

11961200
uint64_t firstDurationInTimescales = firstFile->duration ? firstFile->duration : settings->outputTiming.duration;
11971201
if (input->useStdin || (settings->layers == 1 && input->filesCount > 1)) {
1198-
printf(" * Encoding frame %d [%" PRIu64 "/%" PRIu64 " ts]: %s\n",
1202+
printf(" * Encoding frame %d [%" PRIu64 "/%" PRIu64 " ts] color quality [%d (%s)], alpha quality [%d (%s)]: %s\n",
11991203
0,
12001204
firstDurationInTimescales,
12011205
settings->outputTiming.timescale,
1206+
encoder->quality,
1207+
qualityString(encoder->quality),
1208+
encoder->qualityAlpha,
1209+
qualityString(encoder->qualityAlpha),
12021210
firstFile->filename);
12031211
}
12041212
const avifResult addImageResult = avifEncoderAddImage(encoder, firstImage, firstDurationInTimescales, addImageFlags);
@@ -1898,6 +1906,15 @@ int main(int argc, char * argv[])
18981906
++argIndex;
18991907
}
19001908

1909+
// Alpha quality defaults to the same value as (color) quality until the first time it's explicitly set.
1910+
for (int i = 0; i < input.filesCount; ++i) {
1911+
avifInputFileSettings * fileSettings = &input.files[i].settings;
1912+
if (fileSettings->qualityAlpha.set) {
1913+
break;
1914+
}
1915+
fileSettings->qualityAlpha = fileSettings->quality;
1916+
}
1917+
19011918
if (settings.jobs == -1) {
19021919
settings.jobs = avifQueryCPUCount();
19031920
}
@@ -2146,7 +2163,7 @@ int main(int argc, char * argv[])
21462163
} else {
21472164
assert(!fileSettings->maxQuantizerAlpha.set);
21482165
if (!fileSettings->qualityAlpha.set) {
2149-
fileSettings->qualityAlpha = intSettingsEntryOf(DEFAULT_QUALITY_ALPHA);
2166+
fileSettings->qualityAlpha = fileSettings->quality;
21502167
}
21512168
fileSettings->minQuantizerAlpha = intSettingsEntryOf(AVIF_QUANTIZER_BEST_QUALITY);
21522169
fileSettings->maxQuantizerAlpha = intSettingsEntryOf(AVIF_QUANTIZER_WORST_QUALITY);

tests/test_cmd.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ pushd ${TMP_DIR}
130130
# The default quality is 60. The default alpha quality is 100 (lossless).
131131
"${AVIFENC}" -s 10 "${INPUT_Y4M}" "${ENCODED_FILE}" > "${OUT_MSG}"
132132
grep " color quality \[60 " "${OUT_MSG}"
133-
grep " alpha quality \[100 " "${OUT_MSG}"
133+
grep " alpha quality \[60 " "${OUT_MSG}"
134134
"${AVIFENC}" -s 10 -q 85 "${INPUT_Y4M}" "${ENCODED_FILE}" > "${OUT_MSG}"
135135
grep " color quality \[85 " "${OUT_MSG}"
136-
grep " alpha quality \[100 " "${OUT_MSG}"
136+
grep " alpha quality \[85 " "${OUT_MSG}"
137137
# The average of 15 and 25 is 20. Quantizer 20 maps to quality 68.
138138
"${AVIFENC}" -s 10 --min 15 --max 25 "${INPUT_Y4M}" "${ENCODED_FILE}" > "${OUT_MSG}"
139139
grep " color quality \[68 " "${OUT_MSG}"
140-
grep " alpha quality \[100 " "${OUT_MSG}"
140+
grep " alpha quality \[68 " "${OUT_MSG}"
141141
"${AVIFENC}" -s 10 -q 65 --min 15 --max 25 "${INPUT_Y4M}" "${ENCODED_FILE}" > "${OUT_MSG}"
142142
grep " color quality \[65 " "${OUT_MSG}"
143-
grep " alpha quality \[100 " "${OUT_MSG}"
143+
grep " alpha quality \[65 " "${OUT_MSG}"
144144
popd
145145

146146
exit 0

tests/test_cmd_animation.sh

+25-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ cleanup() {
5252
}
5353
trap cleanup EXIT
5454

55+
# Looks for a string (NOT a regex) in a string.
56+
# findstr needle haystack
57+
findstr() {
58+
if ! echo "${2}" | grep -F "${1}"; then
59+
echo "ERROR: String '${1}' not found"
60+
return 1
61+
fi
62+
}
63+
5564
pushd ${TMP_DIR}
5665
# Lossy test.
5766
"${AVIFENC}" -s 8 "${INPUT_Y4M_0}" "${INPUT_Y4M_1}" -o "${ENCODED_FILE}"
@@ -73,11 +82,26 @@ pushd ${TMP_DIR}
7382
"${AVIFENC}" -s 8 -q 60 "${INPUT_Y4M_0}" "${INPUT_Y4M_1}" -o "${ENCODED_FILE}"
7483
"${AVIFDEC}" "${ENCODED_FILE}" "${DECODED_FILE}"
7584
Q60_FILE_SIZE=$(wc -c < "${ENCODED_FILE}")
76-
"${AVIFENC}" -s 8 -q 60 "${INPUT_Y4M_0}" -q:u 100 "${INPUT_Y4M_1}" -o "${ENCODED_FILE}"
85+
out=$("${AVIFENC}" -s 8 -q 60 "${INPUT_Y4M_0}" -q:u 100 "${INPUT_Y4M_1}" -o "${ENCODED_FILE}")
86+
findstr "Encoding frame 0 [1/25 ts] color quality [60 (Medium)], alpha quality [60 (Medium)]" "${out}"
87+
findstr "Encoding frame 1 [1/25 ts] color quality [100 (Lossless)], alpha quality [100 (Lossless)]" "${out}"
7788
"${AVIFDEC}" "${ENCODED_FILE}" "${DECODED_FILE}"
7889
Q60_Q100_FILE_SIZE=$(wc -c < "${ENCODED_FILE}")
7990
[[ ${Q60_FILE_SIZE} -lt ${Q60_Q100_FILE_SIZE} ]] || exit 1
8091

92+
# Test updating of q and qalpha. Alpha quality defaults to the same as color quality
93+
# until the first time it's explicitly set.
94+
out=$("${AVIFENC}" -s 8 -q 70 "${INPUT_Y4M_0}" -q:u 20 "${INPUT_Y4M_1}" --qalpha:u 10 "${INPUT_Y4M_0}" -q:u 30 "${INPUT_Y4M_1}" -o "${ENCODED_FILE}")
95+
findstr "Encoding frame 0 [1/25 ts] color quality [70 (Medium)], alpha quality [70 (Medium)]" "${out}"
96+
findstr "Encoding frame 1 [1/25 ts] color quality [20 (Low)], alpha quality [20 (Low)]" "${out}"
97+
findstr "Encoding frame 2 [1/25 ts] color quality [20 (Low)], alpha quality [10 (Low)]" "${out}"
98+
findstr "Encoding frame 3 [1/25 ts] color quality [30 (Low)], alpha quality [10 (Low)]" "${out}"
99+
out=$("${AVIFENC}" -s 8 --qalpha 50 -q 70 "${INPUT_Y4M_0}" -q:u 20 "${INPUT_Y4M_1}" -o "${ENCODED_FILE}")
100+
findstr "Encoding frame 0 [1/25 ts] color quality [70 (Medium)], alpha quality [50 (Medium)]" "${out}"
101+
findstr "Encoding frame 1 [1/25 ts] color quality [20 (Low)], alpha quality [50 (Medium)]" "${out}"
102+
out=$("${AVIFENC}" -s 8 "${INPUT_Y4M_0}" "${INPUT_Y4M_1}" -o "${ENCODED_FILE}")
103+
findstr "Encoding frame 0 [1/25 ts] color quality [60 (Medium)], alpha quality [60 (Medium)]" "${out}"
104+
findstr "Encoding frame 1 [1/25 ts] color quality [60 (Medium)], alpha quality [60 (Medium)]" "${out}"
81105
popd
82106

83107
exit 0

tests/test_cmd_avm.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ pushd ${TMP_DIR}
8686
# The default quality is 60. The default alpha quality is 100 (lossless).
8787
"${AVIFENC}" -c avm -s 10 "${INPUT_PNG}" "${ENCODED_FILE}" > "${OUT_MSG}"
8888
grep " color quality \[60 " "${OUT_MSG}"
89-
grep " alpha quality \[100 " "${OUT_MSG}"
89+
grep " alpha quality \[60 " "${OUT_MSG}"
9090
"${AVIFENC}" -c avm -s 10 -q 85 "${INPUT_PNG}" "${ENCODED_FILE}" > "${OUT_MSG}"
9191
grep " color quality \[85 " "${OUT_MSG}"
92-
grep " alpha quality \[100 " "${OUT_MSG}"
92+
grep " alpha quality \[85 " "${OUT_MSG}"
9393
# The average of 15 and 25 is 20. Quantizer 20 maps to quality 68.
9494
"${AVIFENC}" -c avm -s 10 --min 15 --max 25 "${INPUT_PNG}" "${ENCODED_FILE}" > "${OUT_MSG}"
9595
grep " color quality \[68 " "${OUT_MSG}"
96-
grep " alpha quality \[100 " "${OUT_MSG}"
96+
grep " alpha quality \[68 " "${OUT_MSG}"
9797
"${AVIFENC}" -c avm -s 10 -q 65 --min 15 --max 25 "${INPUT_PNG}" "${ENCODED_FILE}" > "${OUT_MSG}"
9898
grep " color quality \[65 " "${OUT_MSG}"
99-
grep " alpha quality \[100 " "${OUT_MSG}"
99+
grep " alpha quality \[65 " "${OUT_MSG}"
100100
popd
101101

102102
exit 0

0 commit comments

Comments
 (0)