refactor(mms): Add an algorithm for compressing GIFs in one pass

This commit is contained in:
Inhishonor
2026-06-26 15:39:27 -07:00
parent b0f5107f1f
commit 96fcb8841a
4 changed files with 193 additions and 21 deletions
@@ -59,6 +59,7 @@ import dev.octoshrimpy.quik.receiver.MessageDeliveredReceiver
import dev.octoshrimpy.quik.receiver.MessageSentReceiver import dev.octoshrimpy.quik.receiver.MessageSentReceiver
import dev.octoshrimpy.quik.receiver.SendDelayedMessageReceiver import dev.octoshrimpy.quik.receiver.SendDelayedMessageReceiver
import dev.octoshrimpy.quik.receiver.SendDelayedMessageReceiver.Companion.MESSAGE_ID_EXTRA import dev.octoshrimpy.quik.receiver.SendDelayedMessageReceiver.Companion.MESSAGE_ID_EXTRA
import dev.octoshrimpy.quik.util.GifCompressor
import dev.octoshrimpy.quik.util.ImageUtils import dev.octoshrimpy.quik.util.ImageUtils
import dev.octoshrimpy.quik.util.PhoneNumberUtils import dev.octoshrimpy.quik.util.PhoneNumberUtils
import dev.octoshrimpy.quik.util.Preferences import dev.octoshrimpy.quik.util.Preferences
@@ -83,6 +84,7 @@ open class MessageRepositoryImpl @Inject constructor(
private val context: Context, private val context: Context,
private val messageIds: KeyManager, private val messageIds: KeyManager,
private val phoneNumberUtils: PhoneNumberUtils, private val phoneNumberUtils: PhoneNumberUtils,
private val gifCompressor: GifCompressor,
private val prefs: Preferences, private val prefs: Preferences,
private val syncRepository: SyncRepository, private val syncRepository: SyncRepository,
private val reactions: EmojiReactionRepository, private val reactions: EmojiReactionRepository,
@@ -614,29 +616,36 @@ open class MessageRepositoryImpl @Inject constructor(
var bestBytes: ByteArray? = null var bestBytes: ByteArray? = null
var attempt = 0 var attempt = 0
while (lo <= hi) { if (isGif) {
val midWidth = (lo + hi) / 2 bestBytes = gifCompressor.compressGif(
val midHeight = (midWidth / aspectRatio).toInt().coerceAtLeast(1) context,
attempt++ attachment,
val candidate = if (isGif) { maxBytes,
ImageUtils.getScaledGif(context, attachment.uri, midWidth, midHeight) origWidth,
} else { aspectRatio
ImageUtils.getScaledImage(context, attachment.uri, midWidth, midHeight)
}
Timber.d(
"Compression attempt $attempt: ${
candidate.size / 1024
}/${maxBytes / 1024}Kb ($origWidth*$origHeight -> $midWidth*$midHeight)"
) )
if (candidate.size <= maxBytes) {
bestBytes = candidate
lo = midWidth + 1
} else {
hi = midWidth - 1
}
// release the attachment hold on the image bytes so the GC can reclaim
attachment.releaseResourceBytes() attachment.releaseResourceBytes()
} else {
while (lo <= hi) {
val midWidth = (lo + hi) / 2
val midHeight = (midWidth / aspectRatio).toInt().coerceAtLeast(1)
attempt++
val candidate = ImageUtils.getScaledImage(context, attachment.uri, midWidth, midHeight)
Timber.d(
"Compression attempt $attempt: ${
candidate.size / 1024
}/${maxBytes / 1024}Kb ($origWidth*$origHeight -> $midWidth*$midHeight)"
)
if (candidate.size <= maxBytes) {
bestBytes = candidate
lo = midWidth + 1
} else {
hi = midWidth - 1
}
// release the attachment hold on the image bytes so the GC can reclaim
attachment.releaseResourceBytes()
}
} }
val result = bestBytes ?: run { val result = bestBytes ?: run {
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2025 QUIK SMS
*
* This file is part of QUIK SMS.
*
* QUIK SMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QUIK SMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QUIK SMS. If not, see <http://www.gnu.org/licenses/>.
*/
package dev.octoshrimpy.quik.util
import android.content.Context
import android.content.res.AssetFileDescriptor
import android.provider.OpenableColumns
import dev.octoshrimpy.quik.model.Attachment
import dev.octoshrimpy.quik.util.ImageUtils.getScaledGif
import timber.log.Timber
import kotlin.math.ln
import kotlin.math.max
import kotlin.math.pow
import kotlin.math.sqrt
class GifCompressor(
/**
* Dampens the amount the target compression amount will influence scale ratio.
* In order to reduce compression, lower this number, and to increase it, raise this number
*
* @property compressionParameter
*/
val compressionParameter: Double = 0.35,
/**
* Determines minimum file dimensions,
* adjust to avoid large GIFS compressed to unreadable amounts
*/
val minFileDimensions: Int = 20
) {
/**
* GIFs are very expensive to compress, so does a quick compression
* by estimating the scale down ratio using byte density, and overall size
* then compressing that way.
*
* Can be fine-tuned using [compressionParameter], and currently leans toward overcompression
*/
fun compressGif(
context: Context,
attachment: Attachment,
maxBytes: Int,
origWidth: Int,
aspectRatio: Float
): ByteArray {
// Determine file properties
val rawFileSize = ImageUtils.fetchFileSize(context, attachment).coerceAtLeast(1)
val origHeight = (origWidth / aspectRatio).toInt()
val totalPixelsPerFrame = origWidth * origHeight
val byteDensity = max(1.0, rawFileSize.toDouble() / totalPixelsPerFrame)
// Figure out generally how much we will have to compress,
// then dampen it and create a base scale
val targetCompressionRatio = rawFileSize.toDouble() / maxBytes.toDouble()
val targetCompressionDampened =
compressionParameter * (compressionParameter * 10) / targetCompressionRatio
val baseScale = 1.0 / (sqrt(targetCompressionRatio) + targetCompressionDampened)
// Calculate how much byte density deviates from expected values
val byteDensityScore = byteDensityScore(byteDensity, rawFileSize.toDouble())
// Determine how aggressive the compression will be, based mainly on byteDensityScore
val compressionScale = (targetCompressionRatio / (compressionParameter * 2))
val compressionAggressiveness =
targetCompressionRatio.pow(byteDensityScore) / (compressionScale)
// How much we will have to scale down the GIF, can't be over 1
val scaleRatio = baseScale.pow(compressionAggressiveness).coerceAtMost(1.0)
// Determine compressed dimensions from scale ratio
val midWidthGif = (origWidth * scaleRatio).toInt().coerceAtLeast(minFileDimensions)
val midHeightGif = (midWidthGif / aspectRatio).toInt().coerceAtLeast(minFileDimensions)
return getScaledGif(context, attachment.uri, midWidthGif, midHeightGif)
}
/**
* Determines how much byte density deviates from expected.
* If there is a lot of deviation, we can assume that the GIF is going to require more compression
*/
private fun byteDensityScore(byteDensity: Double, rawFileSize: Double): Double {
val d = ln(1.0 + byteDensity)
val r = ln(1.0 + rawFileSize)
val t = d / (d + r)
return 2.0 - 8.0 * t * (1.0 - t)
}
}
@@ -19,9 +19,12 @@
package dev.octoshrimpy.quik.util package dev.octoshrimpy.quik.util
import android.content.Context import android.content.Context
import android.content.res.AssetFileDescriptor
import android.net.Uri import android.net.Uri
import android.provider.OpenableColumns
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import dev.octoshrimpy.quik.model.Attachment
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
object ImageUtils { object ImageUtils {
@@ -75,4 +78,52 @@ object ImageUtils {
return result return result
} }
/**
* Get file size, by first using contentResolver.openAssetFileDescriptor
* then if it fails, by reading the file size directly from the size column in contentResolver,
* then if both of those fail, by defaulting to reading the input stream
*/
fun fetchFileSize(context: Context, attachment: Attachment): Long {
val resolver = context.contentResolver
val afdSize = resolver
.openAssetFileDescriptor(attachment.uri, "r")
?.use { it.length }
if (afdSize != null && afdSize != AssetFileDescriptor.UNKNOWN_LENGTH) {
return afdSize
}
val cursorSize = resolver.query(
attachment.uri,
arrayOf(OpenableColumns.SIZE),
null,
null,
null
)?.use { cursor ->
val index = cursor.getColumnIndex(OpenableColumns.SIZE)
if (index != -1 && cursor.moveToFirst() && !cursor.isNull(index)) {
cursor.getLong(index)
} else {
null
}
}
if (cursorSize != null && cursorSize > 0) {
return cursorSize
}
val inputStreamBytes = resolver.openInputStream(attachment.uri)?.use { inputStream ->
val buffer = ByteArray(8192)
var totalBytes = 0L
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
totalBytes += bytesRead
}
totalBytes
}
return requireNotNull(inputStreamBytes)
}
} }
@@ -91,6 +91,7 @@ import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
import dev.octoshrimpy.quik.repository.ScheduledMessageRepositoryImpl import dev.octoshrimpy.quik.repository.ScheduledMessageRepositoryImpl
import dev.octoshrimpy.quik.repository.SyncRepository import dev.octoshrimpy.quik.repository.SyncRepository
import dev.octoshrimpy.quik.repository.SyncRepositoryImpl import dev.octoshrimpy.quik.repository.SyncRepositoryImpl
import dev.octoshrimpy.quik.util.GifCompressor
import dev.octoshrimpy.quik.worker.InjectionWorkerFactory import dev.octoshrimpy.quik.worker.InjectionWorkerFactory
import javax.inject.Singleton import javax.inject.Singleton
@@ -126,6 +127,12 @@ class AppModule(private var application: Application) {
.build() .build()
} }
@Provides
@Singleton
fun provideGifCompressor(): GifCompressor {
return GifCompressor()
}
@Provides @Provides
fun provideViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory = factory fun provideViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory = factory