2021-09-07 18:03:13 +02:00
using Flowframes.Data ;
using Flowframes.IO ;
2021-08-23 16:50:18 +02:00
using Flowframes.MiscUtils ;
2024-11-21 01:22:31 +01:00
using Flowframes.Os ;
2022-07-24 22:30:30 +02:00
using Flowframes.Ui ;
2021-08-23 16:50:18 +02:00
using Microsoft.WindowsAPICodePack.Dialogs ;
using System ;
2024-11-21 01:22:31 +01:00
using System.Collections.Generic ;
2021-08-23 16:50:18 +02:00
using System.Drawing ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
#pragma warning disable IDE1006
namespace Flowframes.Forms
{
2024-11-26 11:45:21 +01:00
public partial class SettingsForm : CustomForm
2021-08-23 16:50:18 +02:00
{
2024-11-26 11:45:21 +01:00
private bool _initialized = false ;
private AiInfo _currentAi = null ;
2021-08-23 16:50:18 +02:00
2024-11-26 11:45:21 +01:00
public SettingsForm ( int tabIndex = 0 , AiInfo currentAi = null )
2021-08-23 16:50:18 +02:00
{
AutoScaleMode = AutoScaleMode . None ;
InitializeComponent ( ) ;
2024-11-26 11:45:21 +01:00
_currentAi = currentAi = = null ? Program . mainForm . GetAi ( ) : currentAi ;
settingsTabList . SelectedIndex = tabIndex ;
2021-08-23 16:50:18 +02:00
}
private void SettingsForm_Load ( object sender , EventArgs e )
{
MinimumSize = new Size ( Width , Height ) ;
MaximumSize = new Size ( Width , ( Height * 1.5f ) . RoundToInt ( ) ) ;
2024-11-25 18:51:43 +01:00
mpdecimateMode . FillFromEnum < Enums . Interpolation . MpDecimateSens > ( useKeyNames : true ) ;
2024-11-26 11:45:21 +01:00
onlyShowRelevantSettings . Click + = ( s , ea ) = > SetVisibility ( ) ;
2021-08-23 16:50:18 +02:00
2024-11-24 03:39:52 +01:00
InitGpus ( ) ;
2021-09-07 18:03:13 +02:00
InitServers ( ) ;
2021-08-23 16:50:18 +02:00
LoadSettings ( ) ;
2024-11-25 18:51:43 +01:00
AddTooltipClickFunction ( ) ;
2024-11-26 11:45:21 +01:00
_initialized = true ;
2021-08-23 16:50:18 +02:00
Task . Run ( ( ) = > CheckModelCacheSize ( ) ) ;
}
2024-11-25 18:51:43 +01:00
private void InitGpus ( )
2024-11-24 03:39:52 +01:00
{
string tooltipTorch = "" ;
string tooltipNcnn = "" ;
for ( int i = 0 ; i < NvApi . NvGpus . Count ; i + + )
{
torchGpus . Items . Add ( i ) ;
tooltipTorch + = $"{i} = {NvApi.NvGpus[i].FullName} ({NvApi.NvGpus[i].GetVramGb().ToString(" 0. ")} GB)\n" ;
}
2024-12-27 19:14:43 +01:00
ncnnGpus . Items . Add ( - 1 ) ;
tooltipNcnn + = $"-1 = CPU\n" ;
2024-11-25 18:51:43 +01:00
foreach ( var vkGpu in VulkanUtils . VkDevices )
2024-11-24 03:39:52 +01:00
{
ncnnGpus . Items . Add ( vkGpu . Id ) ;
2024-12-27 19:14:43 +01:00
tooltipNcnn + = $"{vkGpu.Id.ToString().PadLeft(2)} = {vkGpu.Name}\n" ;
2024-11-24 03:39:52 +01:00
}
toolTip1 . SetToolTip ( tooltipTorchGpu , tooltipTorch . Trim ( ) ) ;
toolTip1 . SetToolTip ( tooltipNcnnGpu , tooltipNcnn . Trim ( ) ) ;
}
private void InitServers ( )
2021-09-07 18:03:13 +02:00
{
serverCombox . Items . Clear ( ) ;
serverCombox . Items . Add ( $"Automatic (Closest)" ) ;
foreach ( Servers . Server srv in Servers . serverList )
serverCombox . Items . Add ( srv . name ) ;
serverCombox . SelectedIndex = 0 ;
}
2024-11-25 18:51:43 +01:00
public async Task CheckModelCacheSize ( )
2021-08-23 16:50:18 +02:00
{
await Task . Delay ( 200 ) ;
long modelFoldersBytes = 0 ;
foreach ( string modelFolder in ModelDownloader . GetAllModelFolders ( ) )
modelFoldersBytes + = IoUtils . GetDirSize ( modelFolder , true ) ;
if ( modelFoldersBytes > 1024 * 1024 )
{
clearModelCacheBtn . Enabled = true ;
clearModelCacheBtn . Text = $"Clear Model Cache ({FormatUtils.Bytes(modelFoldersBytes)})" ;
}
else
{
clearModelCacheBtn . Enabled = false ;
}
}
private void SettingsForm_FormClosing ( object sender , FormClosingEventArgs e )
{
SaveSettings ( ) ;
Program . mainForm . UpdateStepByStepControls ( ) ;
Program . mainForm . LoadQuickSettings ( ) ;
}
2024-11-25 18:51:43 +01:00
void SaveSettings ( )
2021-08-23 16:50:18 +02:00
{
// Remove spaces...
torchGpus . Text = torchGpus . Text . Replace ( " " , "" ) ;
ncnnGpus . Text = ncnnGpus . Text . Replace ( " " , "" ) ;
// General
2024-11-26 11:45:21 +01:00
ConfigParser . SaveGuiElement ( onlyShowRelevantSettings ) ;
2021-08-23 16:50:18 +02:00
ConfigParser . SaveComboxIndex ( processingMode ) ;
ConfigParser . SaveGuiElement ( maxVidHeight , ConfigParser . StringMode . Int ) ;
ConfigParser . SaveComboxIndex ( tempFolderLoc ) ;
ConfigParser . SaveComboxIndex ( outFolderLoc ) ;
ConfigParser . SaveGuiElement ( keepTempFolder ) ;
ConfigParser . SaveGuiElement ( exportNamePattern ) ;
ConfigParser . SaveGuiElement ( exportNamePatternLoop ) ;
// Interpolation
ConfigParser . SaveGuiElement ( keepAudio ) ;
ConfigParser . SaveGuiElement ( keepSubs ) ;
ConfigParser . SaveGuiElement ( keepMeta ) ;
ConfigParser . SaveGuiElement ( enableAlpha ) ;
ConfigParser . SaveGuiElement ( jpegFrames ) ;
ConfigParser . SaveComboxIndex ( dedupMode ) ;
ConfigParser . SaveComboxIndex ( mpdecimateMode ) ;
ConfigParser . SaveGuiElement ( dedupThresh ) ;
ConfigParser . SaveGuiElement ( enableLoop ) ;
ConfigParser . SaveGuiElement ( scnDetect ) ;
ConfigParser . SaveGuiElement ( scnDetectValue ) ;
2024-10-13 14:21:50 +02:00
// ConfigParser.SaveComboxIndex(sceneChangeFillMode);
2021-08-23 16:50:18 +02:00
ConfigParser . SaveComboxIndex ( autoEncMode ) ;
ConfigParser . SaveComboxIndex ( autoEncBackupMode ) ;
ConfigParser . SaveGuiElement ( sbsAllowAutoEnc ) ;
ConfigParser . SaveGuiElement ( alwaysWaitForAutoEnc ) ;
// AI
ConfigParser . SaveGuiElement ( torchGpus ) ;
ConfigParser . SaveGuiElement ( ncnnGpus ) ;
ConfigParser . SaveGuiElement ( ncnnThreads ) ;
ConfigParser . SaveGuiElement ( uhdThresh ) ;
ConfigParser . SaveGuiElement ( rifeCudaFp16 ) ;
ConfigParser . SaveGuiElement ( dainNcnnTilesize , ConfigParser . StringMode . Int ) ;
// Export
ConfigParser . SaveGuiElement ( minOutVidLength , ConfigParser . StringMode . Int ) ;
ConfigParser . SaveGuiElement ( maxFps ) ;
ConfigParser . SaveComboxIndex ( loopMode ) ;
ConfigParser . SaveGuiElement ( fixOutputDuration ) ;
// Debugging
ConfigParser . SaveComboxIndex ( cmdDebugMode ) ;
2021-09-07 18:03:13 +02:00
ConfigParser . SaveComboxIndex ( serverCombox ) ;
2021-08-23 16:50:18 +02:00
ConfigParser . SaveGuiElement ( ffEncPreset ) ;
ConfigParser . SaveGuiElement ( ffEncArgs ) ;
}
void LoadSettings ( )
{
2024-11-26 11:45:21 +01:00
ConfigParser . LoadGuiElement ( onlyShowRelevantSettings ) ;
2021-08-23 16:50:18 +02:00
// General
ConfigParser . LoadComboxIndex ( processingMode ) ;
ConfigParser . LoadGuiElement ( maxVidHeight ) ;
ConfigParser . LoadComboxIndex ( tempFolderLoc ) ; ConfigParser . LoadGuiElement ( tempDirCustom ) ;
ConfigParser . LoadComboxIndex ( outFolderLoc ) ; ConfigParser . LoadGuiElement ( custOutDir ) ;
ConfigParser . LoadGuiElement ( keepTempFolder ) ;
ConfigParser . LoadGuiElement ( exportNamePattern ) ;
ConfigParser . LoadGuiElement ( exportNamePatternLoop ) ;
// Interpolation
ConfigParser . LoadGuiElement ( keepAudio ) ;
ConfigParser . LoadGuiElement ( keepSubs ) ;
ConfigParser . LoadGuiElement ( keepMeta ) ;
ConfigParser . LoadGuiElement ( enableAlpha ) ;
ConfigParser . LoadGuiElement ( jpegFrames ) ;
ConfigParser . LoadComboxIndex ( dedupMode ) ;
ConfigParser . LoadComboxIndex ( mpdecimateMode ) ;
ConfigParser . LoadGuiElement ( dedupThresh ) ;
ConfigParser . LoadGuiElement ( enableLoop ) ;
ConfigParser . LoadGuiElement ( scnDetect ) ;
ConfigParser . LoadGuiElement ( scnDetectValue ) ;
2024-10-13 14:21:50 +02:00
// ConfigParser.LoadComboxIndex(sceneChangeFillMode);
2021-08-23 16:50:18 +02:00
ConfigParser . LoadComboxIndex ( autoEncMode ) ;
ConfigParser . LoadComboxIndex ( autoEncBackupMode ) ;
ConfigParser . LoadGuiElement ( sbsAllowAutoEnc ) ;
ConfigParser . LoadGuiElement ( alwaysWaitForAutoEnc ) ;
// AI
ConfigParser . LoadGuiElement ( torchGpus ) ;
ConfigParser . LoadGuiElement ( ncnnGpus ) ;
ConfigParser . LoadGuiElement ( ncnnThreads ) ;
ConfigParser . LoadGuiElement ( uhdThresh ) ;
ConfigParser . LoadGuiElement ( rifeCudaFp16 ) ;
ConfigParser . LoadGuiElement ( dainNcnnTilesize ) ;
// Export
ConfigParser . LoadGuiElement ( minOutVidLength ) ;
2024-11-25 18:51:43 +01:00
ConfigParser . LoadGuiElement ( maxFps ) ;
2021-08-23 16:50:18 +02:00
ConfigParser . LoadComboxIndex ( loopMode ) ;
ConfigParser . LoadGuiElement ( fixOutputDuration ) ;
// Debugging
ConfigParser . LoadComboxIndex ( cmdDebugMode ) ;
2021-09-07 18:03:13 +02:00
ConfigParser . LoadComboxIndex ( serverCombox ) ;
2021-08-23 16:50:18 +02:00
ConfigParser . LoadGuiElement ( ffEncPreset ) ;
ConfigParser . LoadGuiElement ( ffEncArgs ) ;
}
2024-11-25 18:51:43 +01:00
private void SetVisibility ( )
2024-11-21 01:22:31 +01:00
{
2024-11-26 11:45:21 +01:00
bool onlyRelevant = onlyShowRelevantSettings . Checked ;
2024-11-21 01:22:31 +01:00
// Dev options
List < Control > devOptions = new List < Control > { panKeepTempFolder , } ;
devOptions . ForEach ( c = > c . SetVisible ( Program . Debug ) ) ;
// Legacy/deprecated/untested options
List < Control > legacyUntestedOptions = new List < Control > { panProcessingStyle , panEnableAlpha , panHqJpegImport } ;
legacyUntestedOptions . ForEach ( c = > c . SetVisible ( Program . Debug ) ) ;
// AutoEnc options
2024-11-26 11:45:21 +01:00
bool autoEncPossible = ! _currentAi . Piped ;
autoEncMode . Visible = ! ( onlyRelevant & & ! autoEncPossible ) ;
bool autoEncEnabled = autoEncMode . Visible & & autoEncMode . SelectedIndex ! = 0 ;
2024-11-21 01:22:31 +01:00
List < Control > autoEncOptions = new List < Control > { panAutoEncBackups , panAutoEncLowSpaceMode } ;
autoEncOptions . ForEach ( c = > c . SetVisible ( autoEncEnabled ) ) ;
panAutoEncInSbsMode . SetVisible ( autoEncEnabled & & panProcessingStyle . Visible ) ;
var availAis = Implementations . NetworksAvailable ;
2024-11-26 11:45:21 +01:00
bool showTorchSettings = ! ( onlyRelevant & & _currentAi . Backend ! = AiInfo . AiBackend . Pytorch ) ;
panTorchGpus . SetVisible ( showTorchSettings & & NvApi . NvGpus . Count > 0 & & Python . IsPytorchReady ( ) ) ;
bool showNcnnSettings = ! ( onlyRelevant & & _currentAi . Backend ! = AiInfo . AiBackend . Ncnn ) ;
panNcnnGpus . SetVisible ( showNcnnSettings & & VulkanUtils . VkDevices . Count > 0 ) ;
bool showRifeCudaSettings = ! ( onlyRelevant & & _currentAi ! = Implementations . rifeCuda ) ;
panRifeCudaHalfPrec . SetVisible ( showRifeCudaSettings & & NvApi . NvGpus . Count > 0 & & availAis . Contains ( Implementations . rifeCuda ) ) ;
bool showDainNcnnSettings = ! ( onlyRelevant & & _currentAi ! = Implementations . dainNcnn ) ;
new List < Control > { panTitleDainNcnn , panDainNcnnTileSize } . ForEach ( c = > c . SetVisible ( showDainNcnnSettings & & availAis . Contains ( Implementations . dainNcnn ) ) ) ;
2024-11-25 18:51:43 +01:00
}
private void AddTooltipClickFunction ( )
{
2024-11-26 11:45:21 +01:00
foreach ( Control control in AllControls )
2024-11-25 18:51:43 +01:00
{
if ( ! ( control is PictureBox ) )
continue ;
string tooltipText = toolTip1 . GetToolTip ( control ) ;
if ( tooltipText . IsEmpty ( ) )
continue ;
control . Click + = ( sender , e ) = > { MessageBox . Show ( tooltipText , "Tooltip" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ; } ;
}
}
2021-08-23 16:50:18 +02:00
private void tempFolderLoc_SelectedIndexChanged ( object sender , EventArgs e )
{
tempDirBrowseBtn . Visible = tempFolderLoc . SelectedIndex = = 4 ;
tempDirCustom . Visible = tempFolderLoc . SelectedIndex = = 4 ;
}
private void outFolderLoc_SelectedIndexChanged ( object sender , EventArgs e )
{
custOutDirBrowseBtn . Visible = outFolderLoc . SelectedIndex = = 1 ;
custOutDir . Visible = outFolderLoc . SelectedIndex = = 1 ;
}
private void tempDirBrowseBtn_Click ( object sender , EventArgs e )
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog { InitialDirectory = tempDirCustom . Text . Trim ( ) , IsFolderPicker = true } ;
2024-11-25 18:51:43 +01:00
2021-08-23 16:50:18 +02:00
if ( dialog . ShowDialog ( ) = = CommonFileDialogResult . Ok )
tempDirCustom . Text = dialog . FileName ;
ConfigParser . SaveGuiElement ( tempDirCustom ) ;
}
private void custOutDirBrowseBtn_Click ( object sender , EventArgs e )
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog { InitialDirectory = custOutDir . Text . Trim ( ) , IsFolderPicker = true } ;
if ( dialog . ShowDialog ( ) = = CommonFileDialogResult . Ok )
custOutDir . Text = dialog . FileName ;
ConfigParser . SaveGuiElement ( custOutDir ) ;
}
private void cmdDebugMode_SelectedIndexChanged ( object sender , EventArgs e )
{
2024-11-26 11:45:21 +01:00
if ( _initialized & & cmdDebugMode . SelectedIndex = = 2 )
2022-07-24 22:30:30 +02:00
UiUtils . ShowMessageBox ( "If you enable this, you need to close the CMD window manually after the process has finished, otherwise processing will be paused!" , UiUtils . MessageType . Warning ) ;
2021-08-23 16:50:18 +02:00
}
private void dedupMode_SelectedIndexChanged ( object sender , EventArgs e )
{
dedupeSensLabel . Visible = dedupMode . SelectedIndex ! = 0 ;
2025-03-05 22:57:13 +01:00
mpDedupePanel . Visible = dedupMode . SelectedIndex = = 1 ;
2021-08-23 16:50:18 +02:00
}
private void clearModelCacheBtn_Click ( object sender , EventArgs e )
{
ModelDownloader . DeleteAllModels ( ) ;
clearModelCacheBtn . Text = "Clear Model Cache" ;
CheckModelCacheSize ( ) ;
}
private void modelDownloaderBtn_Click ( object sender , EventArgs e )
{
new ModelDownloadForm ( ) . ShowDialog ( ) ;
CheckModelCacheSize ( ) ;
}
private void autoEncMode_SelectedIndexChanged ( object sender , EventArgs e )
{
2024-11-21 01:22:31 +01:00
SetVisibility ( ) ;
2021-08-23 16:50:18 +02:00
}
private async void resetBtn_Click ( object sender , EventArgs e )
{
2022-07-24 22:30:30 +02:00
DialogResult dialog = UiUtils . ShowMessageBox ( $"Are you sure you want to reset the configuration?" , "Are you sure?" , MessageBoxButtons . YesNo ) ;
2021-08-23 16:50:18 +02:00
if ( dialog = = DialogResult . No )
return ;
await Config . Reset ( 3 , this ) ;
SettingsForm_Load ( null , null ) ;
}
2023-10-24 17:39:44 +02:00
private void btnResetHwEnc_Click ( object sender , EventArgs e )
{
Close ( ) ;
Program . mainForm . ResetOutputUi ( ) ;
}
2024-09-25 14:08:26 +02:00
private bool _sizeFixApplied = false ;
private void settingsTabList_SelectedIndexChanged ( object sender , EventArgs e )
{
2024-11-21 01:22:31 +01:00
SetVisibility ( ) ;
2024-09-25 14:08:26 +02:00
if ( ! _sizeFixApplied )
{
Size = new Size ( Width + 1 , Height + 1 ) ;
Size = new Size ( Width - 1 , Height - 1 ) ;
_sizeFixApplied = true ;
}
}
2021-08-23 16:50:18 +02:00
}
}