diff --git a/app/src/main/java/jp/juggler/subwaytooter/util/AttachmentRequest.kt b/app/src/main/java/jp/juggler/subwaytooter/util/AttachmentRequest.kt index f2471b95..7d45a80a 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/util/AttachmentRequest.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/util/AttachmentRequest.kt @@ -97,13 +97,11 @@ class AttachmentRequest( suspend fun createOpener(): InputStreamOpener { val mimeType = this.mimeType - // GIFはそのまま投げる if (mimeType == MIME_TYPE_GIF) { + // GIFはそのまま投げる return contentUriOpener(context.contentResolver, uri, mimeType, isImage = true) - } - - // 静止画 - if (mimeType.startsWith("image")) { + }else if (mimeType.startsWith("image")) { + // 静止画 return createResizedImageOpener() } diff --git a/base/src/main/java/jp/juggler/util/media/MovieUtils.kt b/base/src/main/java/jp/juggler/util/media/MovieUtils.kt index da92fb21..80c44cf3 100644 --- a/base/src/main/java/jp/juggler/util/media/MovieUtils.kt +++ b/base/src/main/java/jp/juggler/util/media/MovieUtils.kt @@ -68,6 +68,36 @@ data class MovieResizeConfig( } } +/** + * レシーバが奇数なら+1した値を返す + * + * `[OMX.qcom.video.encoder.avc] video encoder does not support odd resolution 1018x2263` + */ +private fun Int.fixOdd() = if (and(1) == 0) this else this + 1 + +/** + * 動画のピクセルサイズを制限に合わせてスケーリングする + */ +private fun createScaledSize(inSize: Size, limitSquarePixels: Int): Size { + + if (inSize.major <= 0 || inSize.minor <= 0) { + // 入力サイズの縦横が0以下の場合、アスペクト比を計算できないのでリサイズできない + log.w("createScaledSize: video size not valid. major=${inSize.major}, minor=${inSize.minor}") + return inSize + } + + val squarePixels = inSize.major * inSize.minor + if (squarePixels <= limitSquarePixels) { + return inSize + } + + val aspect = inSize.major.toFloat() / inSize.minor.toFloat() + return Size( + max(1f, sqrt(limitSquarePixels.toFloat() * aspect)).toInt().fixOdd(), + max(1f, sqrt(limitSquarePixels.toFloat() / aspect)).toInt().fixOdd(), + ) +} + @Suppress("BlockingMethodInNonBlockingContext") suspend fun transcodeVideo( info: VideoInfo, @@ -122,19 +152,10 @@ suspend fun transcodeVideo( .addDataSource(inStream.fd) .setVideoTrackStrategy(DefaultVideoStrategy.Builder() .addResizer { inSize -> - val squarePixels = inSize.major * inSize.minor - val limit = resizeConfig.limitSquarePixels - if (squarePixels <= limit || inSize.major <= 0 || inSize.minor <= 0) { - // 入力サイズが0以下の場合もアスペクト計算に支障がでるのでリサイズできない - inSize - } else { - // アスペクト比を維持しつつ平方ピクセルが指定に収まるようにする - val aspect = inSize.major.toFloat() / inSize.minor.toFloat() - Size( - max(1, (sqrt(limit.toFloat() * aspect) + 0.5f).toInt()), - max(1, (sqrt(limit.toFloat() / aspect) + 0.5f).toInt()), - ) - } + createScaledSize( + inSize = inSize, + limitSquarePixels = resizeConfig.limitSquarePixels + ) } .frameRate(resizeConfig.limitFrameRate) .keyFrameInterval(10f)