Skip to content

Commit 7d10bb7

Browse files
authored
Merge pull request #21 from snowfluke/master
Add speed/effort parameter
2 parents 7089cdc + b0de5ea commit 7d10bb7

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

avif-coder/src/main/cpp/JniEncoder.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct heif_error writeHeifData(struct heif_context *ctx,
7979

8080
jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
8181
jobject bitmap, heif_compression_format heifCompressionFormat,
82-
const int quality, const int dataSpace, const AvifQualityMode qualityMode) {
82+
const int quality, const int speed, const int dataSpace, const AvifQualityMode qualityMode) {
8383
std::shared_ptr<heif_context> ctx(heif_context_alloc(),
8484
[](heif_context *c) { heif_context_free(c); });
8585
if (!ctx) {
@@ -114,6 +114,15 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
114114
throwException(env, str);
115115
return static_cast<jbyteArray>(nullptr);
116116
}
117+
if(speed > -1 && speed <= 10){
118+
result = heif_encoder_set_parameter_string(encoder.get(), "speed", speed)
119+
if (result.code != heif_error_Ok) {
120+
std::string choke(result.message);
121+
std::string str = "Can't set speed/effort: " + choke;
122+
throwException(env, str);
123+
return static_cast<jbyteArray>(nullptr);
124+
}
125+
}
117126
}
118127
} else if (qualityMode == AVIF_LOSELESS_MODE) {
119128
result = heif_encoder_set_lossless(encoder.get(), true);
@@ -331,14 +340,15 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
331340
extern "C"
332341
JNIEXPORT jbyteArray JNICALL
333342
Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_encodeAvifImpl(JNIEnv *env, jobject thiz,
334-
jobject bitmap, jint quality,
343+
jobject bitmap, jint quality, jint speed,
335344
jint dataSpace, jint qualityMode) {
336345
try {
337346
return encodeBitmap(env,
338347
thiz,
339348
bitmap,
340349
heif_compression_AV1,
341350
quality,
351+
speed,
342352
dataSpace,
343353
static_cast<AvifQualityMode>(qualityMode));
344354
} catch (std::bad_alloc &err) {
@@ -359,6 +369,7 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_encodeHeicImpl(JNIEnv *env, job
359369
bitmap,
360370
heif_compression_HEVC,
361371
quality,
372+
0,
362373
dataSpace,
363374
static_cast<AvifQualityMode>(qualityMode));
364375
} catch (std::bad_alloc &err) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023 Radzivon Bartoshyk
5+
* avif-coder [https://github.com/awxkee/avif-coder]
6+
*
7+
* Created by Radzivon Bartoshyk on 23/9/2023
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE.
26+
*
27+
*/
28+
29+
package com.radzivon.bartoshyk.avif.coder
30+
31+
/**
32+
* Enum representing speed values from 0 to 10 (slowest-fastest).
33+
* Default is 6.
34+
* https://github.com/AOMediaCodec/libavif/blob/main/doc/avifenc.1.md
35+
*/
36+
enum class AvifSpeed(internal val value: Int) {
37+
ZERO(0), // Speed 0
38+
ONE(1), // Speed 1
39+
TWO(2), // Speed 2
40+
THREE(3), // Speed 3
41+
FOUR(4), // Speed 4
42+
FIVE(5), // Speed 5
43+
SIX(6), // Speed 6
44+
SEVEN(7), // Speed 7
45+
EIGHT(8), // Speed 8
46+
NINE(9), // Speed 9
47+
TEN(10) // Speed 10
48+
49+
// Not using speed parameter, fallback to use x265 instead of aom
50+
USE_X265(-1)
51+
}

avif-coder/src/main/java/com/radzivon/bartoshyk/avif/coder/HeifCoder.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class HeifCoder(
116116
)
117117
}
118118

119-
fun encodeAvif(bitmap: Bitmap, quality: Int = 80, preciseMode: PreciseMode = PreciseMode.LOSSY): ByteArray {
119+
fun encodeAvif(bitmap: Bitmap, quality: Int = 80, speed: AvifSpeed = AvifSpeed.SIX, preciseMode: PreciseMode = PreciseMode.LOSSY): ByteArray {
120120
require(quality in 0..100) {
121121
throw IllegalStateException("Quality should be in 0..100 range")
122122
}
123123
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
124-
encodeAvifImpl(bitmap, quality, bitmap.colorSpace?.dataSpace ?: -1, preciseMode.value)
124+
encodeAvifImpl(bitmap, quality, speed.value, bitmap.colorSpace?.dataSpace ?: -1, preciseMode.value)
125125
} else {
126-
encodeAvifImpl(bitmap, quality, -1, preciseMode.value)
126+
encodeAvifImpl(bitmap, quality, speed.value, -1, preciseMode.value)
127127
}
128128
}
129129

@@ -163,8 +163,8 @@ class HeifCoder(
163163
toneMapper: Int,
164164
): Bitmap
165165

166-
private external fun encodeAvifImpl(bitmap: Bitmap, quality: Int, dataSpace: Int, qualityMode: Int): ByteArray
167-
private external fun encodeHeicImpl(bitmap: Bitmap, quality: Int, dataSpace: Int, qualityMode: Int): ByteArray
166+
private external fun encodeAvifImpl(bitmap: Bitmap, quality: Int, speed: Int, dataSpace: Int, qualityMode: Int): ByteArray
167+
private external fun encodeHeicImpl(bitmap: Bitmap, quality: Int, speed: Int, dataSpace: Int, qualityMode: Int): ByteArray
168168

169169
@SuppressLint("ObsoleteSdkInt")
170170
companion object {

0 commit comments

Comments
 (0)