diff --git a/gradle.properties b/gradle.properties index 6ff982a..3bfaa8b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.9 loader_version=0.15.11 # Mod Properties -mod_version=0.17.2 +mod_version=0.17.5 maven_group=com.moulberry.flashback archives_base_name=flashback diff --git a/src/main/java/com/moulberry/flashback/exporting/AsyncFFmpegVideoWriter.java b/src/main/java/com/moulberry/flashback/exporting/AsyncFFmpegVideoWriter.java index 51ab9aa..9a99a8e 100644 --- a/src/main/java/com/moulberry/flashback/exporting/AsyncFFmpegVideoWriter.java +++ b/src/main/java/com/moulberry/flashback/exporting/AsyncFFmpegVideoWriter.java @@ -216,6 +216,10 @@ public class AsyncFFmpegVideoWriter implements AutoCloseable, VideoWriter { PointerPointer tmp_picture_ptr = new PointerPointer<>(tmp_picture); PointerPointer picture_ptr = new PointerPointer<>(picture); + Flashback.LOGGER.info("Rescaling to pixel format: {}", dstPixelFormat); + + boolean useItu709Colorspace = PixelFormatHelper.isYuvFormat(dstPixelFormat); + Thread scaleThread = new Thread(() -> { SwsContext img_convert_ctx = null; @@ -234,20 +238,17 @@ public class AsyncFFmpegVideoWriter implements AutoCloseable, VideoWriter { } img_convert_ctx = swscale.sws_getCachedContext(img_convert_ctx, src.width, src.height, src.pixelFormat, - dstWidth, dstHeight, dstPixelFormat, swscale.SWS_BILINEAR, + dstWidth, dstHeight, dstPixelFormat, swscale.SWS_LANCZOS | swscale.SWS_ACCURATE_RND | swscale.SWS_FULL_CHR_H_INT, null, null, (DoublePointer) null); if (img_convert_ctx == null) { throw new RuntimeException("sws_getCachedContext() error: Cannot initialize the conversion context."); } - int dstRange = 1; // Full - if (dstPixelFormat == AV_PIX_FMT_YUV420P) { // todo: check more values - dstRange = 0; // Partial + if (useItu709Colorspace) { + IntPointer coefficients = swscale.sws_getCoefficients(swscale.SWS_CS_ITU709); + swscale.sws_setColorspaceDetails(img_convert_ctx, coefficients, 1, coefficients, 0, 0, 1 << 16, 1 << 16); } - IntPointer coefficients = swscale.sws_getCoefficients(swscale.SWS_CS_ITU709); - swscale.sws_setColorspaceDetails(img_convert_ctx, coefficients, 1, coefficients, dstRange, 0, 1 << 16, 1 << 16); - BytePointer data = new BytePointer() {{ this.address = src.pointer; this.position = 0; diff --git a/src/main/java/com/moulberry/flashback/exporting/PixelFormatHelper.java b/src/main/java/com/moulberry/flashback/exporting/PixelFormatHelper.java index 788be6a..bca534a 100644 --- a/src/main/java/com/moulberry/flashback/exporting/PixelFormatHelper.java +++ b/src/main/java/com/moulberry/flashback/exporting/PixelFormatHelper.java @@ -7,6 +7,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.ints.IntSet; import org.bytedeco.ffmpeg.avcodec.AVCodec; import org.bytedeco.ffmpeg.global.avcodec; import org.bytedeco.ffmpeg.global.avutil; @@ -122,4 +123,14 @@ public class PixelFormatHelper { return "UNKNOWN(" + pixelFormat + ")"; } + public static boolean isYuvFormat(int pixelFormat) { + try (var descriptor = avutil.av_pix_fmt_desc_get(pixelFormat)) { + if (descriptor == null || descriptor.isNull()) { + throw new RuntimeException(); + } + + return (descriptor.flags() & avutil.AV_PIX_FMT_FLAG_RGB) == 0 && descriptor.nb_components() >= 2; + } + } + }