From 8a8324dc24617a9f0313ede55501e8080ccce2a5 Mon Sep 17 00:00:00 2001 From: n00mkrad Date: Sun, 8 Jan 2023 14:17:11 +0100 Subject: [PATCH] Fix VS modulo padding --- Code/Data/InterpSettings.cs | 13 ++-------- Code/IO/IoUtils.cs | 2 +- Code/Main/InterpolateUtils.cs | 47 ++++++++++++++++------------------- Code/Media/FfmpegAlpha.cs | 1 + Code/Media/FfmpegCommands.cs | 7 +++--- Code/Media/FfmpegEncode.cs | 1 + Code/Media/FfmpegExtract.cs | 2 +- 7 files changed, 31 insertions(+), 42 deletions(-) diff --git a/Code/Data/InterpSettings.cs b/Code/Data/InterpSettings.cs index b2917cc..5f600cf 100644 --- a/Code/Data/InterpSettings.cs +++ b/Code/Data/InterpSettings.cs @@ -35,8 +35,8 @@ namespace Flowframes private Size _inputResolution; public Size InputResolution { get { RefreshInputRes(); return _inputResolution; } } - private Size _scaledResolution; - public Size ScaledResolution { get { RefreshOutputRes(); return _scaledResolution; } } + public Size ScaledResolution { get { return InterpolateUtils.GetOutputResolution(InputResolution, false); } } + public Size ScaledPaddedResolution { get { return InterpolateUtils.GetOutputResolution(InputResolution, true); } } public bool alpha; public bool stepByStep; @@ -82,7 +82,6 @@ namespace Flowframes } _inputResolution = new Size(0, 0); - _scaledResolution = new Size(0, 0); RefreshExtensions(); } @@ -101,7 +100,6 @@ namespace Flowframes alpha = false; stepByStep = false; _inputResolution = new Size(0, 0); - _scaledResolution = new Size(0, 0); framesExt = ""; interpExt = ""; @@ -128,7 +126,6 @@ namespace Flowframes case "OUTMODE": outMode = (Interpolate.OutMode)Enum.Parse(typeof(Interpolate.OutMode), entry.Value); break; case "MODEL": model = AiModels.GetModelByName(ai, entry.Value); break; case "INPUTRES": _inputResolution = FormatUtils.ParseSize(entry.Value); break; - case "OUTPUTRES": _scaledResolution = FormatUtils.ParseSize(entry.Value); break; case "ALPHA": alpha = bool.Parse(entry.Value); break; case "STEPBYSTEP": stepByStep = bool.Parse(entry.Value); break; case "FRAMESEXT": framesExt = entry.Value; break; @@ -171,12 +168,6 @@ namespace Flowframes _inputResolution = await GetMediaResolutionCached.GetSizeAsync(inPath); } - void RefreshOutputRes () - { - if (_scaledResolution.IsEmpty) - _scaledResolution = InterpolateUtils.GetOutputResolution(InputResolution, false, true); - } - public void RefreshAlpha () { try diff --git a/Code/IO/IoUtils.cs b/Code/IO/IoUtils.cs index dd515c1..c5873f8 100644 --- a/Code/IO/IoUtils.cs +++ b/Code/IO/IoUtils.cs @@ -568,7 +568,7 @@ namespace Flowframes.IO if (curr.outMode == Interpolate.OutMode.VidGif && fps > 50f) fps = 50f; - Size outRes = await InterpolateUtils.GetOutputResolution(curr.inPath, false, false); + Size outRes = await InterpolateUtils.GetOutputResolution(curr.inPath, true); string pattern = Config.Get(Config.Key.exportNamePattern); string inName = Interpolate.currentSettings.inputIsFrames ? Path.GetFileName(curr.inPath) : Path.GetFileNameWithoutExtension(curr.inPath); bool encodeBoth = Config.GetInt(Config.Key.maxFpsMode) == 0; diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 118570f..adad637 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -16,6 +16,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using I = Flowframes.Interpolate; using Padding = Flowframes.Data.Padding; +using static Flowframes.Magick.Dedupe; namespace Flowframes.Main { @@ -138,7 +139,7 @@ namespace Flowframes.Main return passes; } - catch(Exception e) + catch (Exception e) { Logger.Log($"Failed to run InputIsValid: {e.Message}\n{e.StackTrace}", true); return false; @@ -186,7 +187,7 @@ namespace Flowframes.Main return true; } - public static void ShowWarnings (float factor, AI ai) + public static void ShowWarnings(float factor, AI ai) { if (Config.GetInt(Config.Key.cmdDebugMode) > 0) Logger.Log($"Warning: The CMD window for interpolation is enabled. This will disable Auto-Encode and the progress bar!"); @@ -221,7 +222,7 @@ namespace Flowframes.Main return true; } - public static async Task CheckEncoderValid (float interpFps) + public static async Task CheckEncoderValid(float interpFps) { string enc = FfmpegUtils.GetEnc(FfmpegUtils.GetCodec(I.currentSettings.outMode)); @@ -253,33 +254,29 @@ namespace Flowframes.Main return true; } - public static async Task GetOutputResolution(string inputPath, bool print, bool returnZeroIfUnchanged = false) + public static async Task GetOutputResolution(string inputPath, bool pad, bool print = false) { Size resolution = await GetMediaResolutionCached.GetSizeAsync(inputPath); - return GetOutputResolution(resolution, print, returnZeroIfUnchanged); + return GetOutputResolution(resolution, pad, print); } - public static Size GetOutputResolution(Size inputRes, bool print = false, bool returnZeroIfUnchanged = false) + public static Size GetOutputResolution(Size inputRes, bool pad, bool print = false) { - int maxHeightValue = Config.GetInt(Config.Key.maxVidHeight); - int maxHeight = RoundDivisibleBy(maxHeightValue, FfmpegCommands.GetPadding()); + Size res = new Size(inputRes.Width, inputRes.Height); + int maxHeight = Config.GetInt(Config.Key.maxVidHeight); + int mod = pad ? FfmpegCommands.GetModulo() : 1; + float factor = res.Height > maxHeight ? (float)maxHeight / res.Height : 1f; // Calculate downscale factor if bigger than max, otherwise just use 1x + Logger.Log($"Un-rounded downscaled size: {(res.Width * factor).ToString("0.###")}x{(res.Height * factor).ToString("0.###")}", true); + int width = RoundDivisibleBy((res.Width * factor).RoundToInt(), mod); + int height = RoundDivisibleBy((res.Height * factor).RoundToInt(), mod); - if (inputRes.Height > maxHeight) - { - float factor = (float)maxHeight / inputRes.Height; - Logger.Log($"Un-rounded downscaled size: {(inputRes.Width * factor).ToString("0.00")}x{maxHeightValue}", true); - int width = RoundDivisibleBy((inputRes.Width * factor).RoundToInt(), FfmpegCommands.GetPadding()); - if (print) - Logger.Log($"Video is bigger than the maximum - Downscaling to {width}x{maxHeight}."); - return new Size(width, maxHeight); - } - else - { - if (returnZeroIfUnchanged) - return new Size(); - else - return inputRes; - } + if (print && factor < 1f) + Logger.Log($"Video is bigger than the maximum - Downscaling to {width}x{height}."); + + if (res != inputRes) + Logger.Log($"Scaled {inputRes.Width}x{inputRes.Height} to {res.Width}x{res.Height}", true); + + return res; } public static int RoundDivisibleBy(int number, int divisibleBy) // Round to a number that's divisible by 2 (for h264 etc) @@ -305,7 +302,7 @@ namespace Flowframes.Main return false; } - if(current.outMode == I.OutMode.VidGif) + if (current.outMode == I.OutMode.VidGif) { Logger.Log($"Not Using AutoEnc: Using GIF output", true); return false; diff --git a/Code/Media/FfmpegAlpha.cs b/Code/Media/FfmpegAlpha.cs index e8753c8..4076254 100644 --- a/Code/Media/FfmpegAlpha.cs +++ b/Code/Media/FfmpegAlpha.cs @@ -18,6 +18,7 @@ namespace Flowframes.Media public static async Task ExtractAlphaDir(string rgbDir, string alphaDir) { Directory.CreateDirectory(alphaDir); + foreach (FileInfo file in IoUtils.GetFileInfosSorted(rgbDir)) { string args = $"-i {file.FullName.Wrap()} -vf \"format=yuva444p16le,alphaextract,format=yuv420p,{GetPadFilter()}\" {Path.Combine(alphaDir, file.Name).Wrap()}"; diff --git a/Code/Media/FfmpegCommands.cs b/Code/Media/FfmpegCommands.cs index 7fa160f..354c5dc 100644 --- a/Code/Media/FfmpegCommands.cs +++ b/Code/Media/FfmpegCommands.cs @@ -11,7 +11,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using static Flowframes.AvProcess; -using Utils = Flowframes.Media.FfmpegUtils; namespace Flowframes { @@ -23,15 +22,15 @@ namespace Flowframes public static string mpDecDef = "\"mpdecimate\""; public static string mpDecAggr = "\"mpdecimate=hi=64*32:lo=64*32:frac=0.1\""; - public static int GetPadding () + public static int GetModulo () { return (Interpolate.currentSettings.ai.NameInternal == Implementations.flavrCuda.NameInternal) ? 8 : 2; // FLAVR input needs to be divisible by 8 } public static string GetPadFilter () { - int padPixels = GetPadding(); - return $"pad=width=ceil(iw/{padPixels})*{padPixels}:height=ceil(ih/{padPixels})*{padPixels}:color=black@0"; + int mod = GetModulo(); + return $"pad=width=ceil(iw/{mod})*{mod}:height=ceil(ih/{mod})*{mod}:color=black@0"; } public static async Task ConcatVideos(string concatFile, string outPath, int looptimes = -1, bool showLog = true) diff --git a/Code/Media/FfmpegEncode.cs b/Code/Media/FfmpegEncode.cs index 460135f..1c31dfc 100644 --- a/Code/Media/FfmpegEncode.cs +++ b/Code/Media/FfmpegEncode.cs @@ -71,6 +71,7 @@ namespace Flowframes.Media if(!isChunk && outMode == Interpolate.OutMode.VidMp4) extraArgs += $" -movflags +faststart"; + filters.Add(GetPadFilter()); return filters.Count > 0 ? $"-vf {string.Join(",", filters)}" : "" + $" {extraArgs}"; } diff --git a/Code/Media/FfmpegExtract.cs b/Code/Media/FfmpegExtract.cs index e5dd3ae..7861641 100644 --- a/Code/Media/FfmpegExtract.cs +++ b/Code/Media/FfmpegExtract.cs @@ -177,7 +177,7 @@ namespace Flowframes.Media return false; } - int div = GetPadding(); + int div = GetModulo(); bool allDivBy2 = randomSamples.All(i => (i.Width % div == 0) && (i.Height % div == 0)); if (!allDivBy2)