Skip to content

Commit d7efa62

Browse files
committed
Rollback x265, added additional parameters
1 parent ead58ac commit d7efa62

File tree

25 files changed

+156
-35
lines changed

25 files changed

+156
-35
lines changed

app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import androidx.lifecycle.lifecycleScope
3939
import com.radzivon.bartoshyk.avif.coder.AvifAnimatedDecoder
4040
import com.radzivon.bartoshyk.avif.coder.AvifChromaSubsampling
4141
import com.radzivon.bartoshyk.avif.coder.HeifCoder
42+
import com.radzivon.bartoshyk.avif.coder.HeifPreset
4243
import com.radzivon.bartoshyk.avif.coder.PreciseMode
4344
import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4445
import com.radzivon.bartoshyk.avif.coder.ScaleMode
@@ -158,9 +159,16 @@ class MainActivity : AppCompatActivity() {
158159

159160
Log.i("AVIFFFF", "Starts encoding")
160161

161-
val encode = coder.encodeAvif(bitmap = bitmap0, quality = 55, avifChromaSubsampling = AvifChromaSubsampling.YUV400)
162-
163-
Log.i("AVIFFFF", "Encoding time ${System.currentTimeMillis() - start}, encoded size ${encode.size}")
162+
val encode = coder.encodeHeic(
163+
bitmap = bitmap0,
164+
quality = 25,
165+
preciseMode = PreciseMode.LOSSY,
166+
preset = HeifPreset.VERYSLOW
167+
)
168+
Log.i(
169+
"AVIFFFF",
170+
"Encoding time ${System.currentTimeMillis() - start}, encoded size ${encode.size}"
171+
)
164172

165173
val bitmap = coder.decode(encode)
166174

avif-coder/src/main/cpp/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ add_library(coder SHARED JniEncoder.cpp JniException.cpp SizeScaler.cpp
2121
colorspace/ColorMatrix.cpp imagebits/ScanAlpha.cpp imagebits/Rgba16.cpp
2222
AvifDecoderController.cpp HeifImageDecoder.cpp JniAnimatedController.cpp)
2323

24-
add_library(libkvazaar SHARED IMPORTED)
2524
add_library(libheif SHARED IMPORTED)
2625
add_library(libyuv STATIC IMPORTED)
2726
add_library(libde265 SHARED IMPORTED)
2827
add_library(libdav1d SHARED IMPORTED)
2928
add_library(libsharpyuv STATIC IMPORTED)
3029
add_library(aom SHARED IMPORTED)
30+
add_library(x265 SHARED IMPORTED)
3131
add_library(avifweaver SHARED IMPORTED)
3232

33-
set_target_properties(libkvazaar PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libkvazaar.so)
33+
set_target_properties(x265 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libx265.so)
3434
set_target_properties(libheif PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libheif.so)
3535
set_target_properties(libyuv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libyuv.a)
3636
set_target_properties(libde265 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libde265.so)
@@ -83,5 +83,5 @@ add_subdirectory(avif)
8383

8484
target_link_libraries( # Specifies the target library.
8585
coder PRIVATE
86-
${log-lib} libheif cpufeatures libyuv -ljnigraphics libkvazaar aom avif_shared
87-
libde265 libdav1d libsharpyuv ${android-lib} avifweaver)
86+
${log-lib} libheif cpufeatures libyuv -ljnigraphics aom avif_shared
87+
libde265 libdav1d libsharpyuv ${android-lib} avifweaver x265)

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

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ enum AvifEncodingSurface {
7070
};
7171

7272
enum AvifChromaSubsampling {
73-
AVIF_CHROMA_AUTO, AVIF_CHROMA_YUV_420, AVIF_CHROMA_YUV_422, AVIF_CHROMA_YUV_444, AVIF_CHROMA_YUV_400
73+
AVIF_CHROMA_AUTO,
74+
AVIF_CHROMA_YUV_420,
75+
AVIF_CHROMA_YUV_422,
76+
AVIF_CHROMA_YUV_444,
77+
AVIF_CHROMA_YUV_400
7478
};
7579

7680
struct heif_error writeHeifData(struct heif_context *ctx,
@@ -95,7 +99,9 @@ jbyteArray encodeBitmapHevc(JNIEnv *env,
9599
jobject bitmap,
96100
const int quality,
97101
const int dataSpace,
98-
const bool loseless) {
102+
const bool loseless,
103+
std::string &x265Preset,
104+
const int crf) {
99105
std::shared_ptr<heif_context> ctx(heif_context_alloc(),
100106
[](heif_context *c) { heif_context_free(c); });
101107
if (!ctx) {
@@ -166,6 +172,23 @@ jbyteArray encodeBitmapHevc(JNIEnv *env,
166172

167173
AndroidBitmap_unlockPixels(env, bitmap);
168174

175+
result = heif_encoder_set_parameter(encoder.get(), "x265:preset", x265Preset.c_str());
176+
if (result.code != heif_error_Ok) {
177+
std::string choke(result.message);
178+
std::string str = "Can't create encoded image with exception: " + choke;
179+
throwException(env, str);
180+
return static_cast<jbyteArray>(nullptr);
181+
}
182+
183+
auto crfString = std::to_string(crf);
184+
result = heif_encoder_set_parameter(encoder.get(), "x265:crf", crfString.c_str());
185+
if (result.code != heif_error_Ok) {
186+
std::string choke(result.message);
187+
std::string str = "Can't create encoded image with exception: " + choke;
188+
throwException(env, str);
189+
return static_cast<jbyteArray>(nullptr);
190+
}
191+
169192
heif_image *imagePtr;
170193

171194
result = heif_image_create((int) info.width, (int) info.height,
@@ -524,7 +547,7 @@ jbyteArray encodeBitmapAvif(JNIEnv *env,
524547
uint8_t *uPlane = nullptr;
525548

526549
uint32_t vStride = 0;
527-
uint8_t * vPlane = nullptr;
550+
uint8_t *vPlane = nullptr;
528551

529552
if (pixelFormat != AVIF_PIXEL_FORMAT_YUV400) {
530553
uStride = image->yuvRowBytes[1];
@@ -701,16 +724,44 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_encodeAvifImpl(JNIEnv *env,
701724

702725
extern "C"
703726
JNIEXPORT jbyteArray JNICALL
704-
Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_encodeHeicImpl(JNIEnv *env, jobject thiz,
705-
jobject bitmap, jint quality,
706-
jint dataSpace, jint qualityMode) {
727+
Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_encodeHeicImpl(JNIEnv *env,
728+
jobject thiz,
729+
jobject bitmap,
730+
jint quality,
731+
jint dataSpace,
732+
jint qualityMode,
733+
jint x265Preset, jint crf) {
707734
try {
735+
std::string preset = "superfast";
736+
if (x265Preset == 0) {
737+
preset = "placebo";
738+
} else if (x265Preset == 1) {
739+
preset = "veryslow";
740+
} else if (x265Preset == 2) {
741+
preset = "slower";
742+
} else if (x265Preset == 3) {
743+
preset = "slow";
744+
} else if (x265Preset == 4) {
745+
preset = "medium";
746+
} else if (x265Preset == 5) {
747+
preset = "fast";
748+
} else if (x265Preset == 6) {
749+
preset = "faster";
750+
} else if (x265Preset == 7) {
751+
preset = "veryfast";
752+
} else if (x265Preset == 8) {
753+
preset = "superfast";
754+
} else if (x265Preset == 9) {
755+
preset = "ultrafast";
756+
}
757+
708758
return encodeBitmapHevc(env,
709759
thiz,
710760
bitmap,
711761
quality,
712762
dataSpace,
713-
qualityMode == 2);
763+
qualityMode == 2,
764+
preset, crf);
714765
} catch (std::bad_alloc &err) {
715766
std::string exception = "Not enough memory to encode this image";
716767
throwException(env, exception);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,26 @@ bool RescaleImage(aligned_uint8_vector &initialData,
195195

196196
imageWidth = heif_image_get_width(img.get(), heif_channel_interleaved);
197197
imageHeight = heif_image_get_height(img.get(), heif_channel_interleaved);
198-
initialData.resize(*stride * imageHeight);
198+
199+
uint32_t newStride = static_cast<int >(imageWidth) * 4
200+
* static_cast<int>(useFloats ? sizeof(uint16_t) : sizeof(uint8_t));
201+
202+
initialData.resize(newStride * imageHeight);
199203

200204
if (useFloats) {
201205
coder::CopyUnaligned(reinterpret_cast<const uint16_t *>(data), *stride,
202-
reinterpret_cast<uint16_t *>(initialData.data()), *stride,
206+
reinterpret_cast<uint16_t *>(initialData.data()), newStride,
203207
imageWidth * 4,
204208
imageHeight);
205209
} else {
206210
coder::CopyUnaligned(reinterpret_cast<const uint8_t *>(data), *stride,
207-
reinterpret_cast<uint8_t *>(initialData.data()), *stride,
211+
reinterpret_cast<uint8_t *>(initialData.data()), newStride,
208212
imageWidth * 4,
209213
imageHeight);
210214
}
211215

216+
*stride = static_cast<int>(newStride);
217+
212218
*imageWidthPtr = imageWidth;
213219
*imageHeightPtr = imageHeight;
214220
}
10.9 KB
Binary file not shown.
-448 KB
Binary file not shown.
1.86 MB
Binary file not shown.
7.45 KB
Binary file not shown.
-339 KB
Binary file not shown.
1.63 MB
Binary file not shown.

0 commit comments

Comments
 (0)