Skip to content

Commit

Permalink
Pass number of threads argument to external codecs as a hidden range …
Browse files Browse the repository at this point in the history
…parameter.
  • Loading branch information
enzo1982 committed Mar 22, 2024
1 parent 23bf68a commit cc5f68b
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 165 deletions.
20 changes: 14 additions & 6 deletions doc/dtds/component.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,41 @@

<!ELEMENT parameters (switch | selection | range)*>

<!ELEMENT switch (depends*)>
<!ELEMENT switch (requires*, depends*)>
<!ATTLIST switch name CDATA #REQUIRED
argument CDATA #REQUIRED
enabled (true|false) "false">
enabled (true|false) "false"
hidden (true|false) "false">

<!ELEMENT selection (depends*, option+)>
<!ELEMENT selection (requires*, depends*, option+)>
<!ATTLIST selection name CDATA #REQUIRED
argument CDATA #REQUIRED
enabled (true|false) "false"
default NMTOKEN #IMPLIED>
default NMTOKEN #IMPLIED
hidden (true|false) "false">

<!ELEMENT option (#PCDATA)>
<!ATTLIST option alias CDATA #IMPLIED>

<!ELEMENT range (depends*, min, max)>
<!ELEMENT range (requires*, depends*, min, max)>
<!ATTLIST range name CDATA #REQUIRED
argument CDATA #REQUIRED
enabled (true|false) "false"
default NMTOKEN #IMPLIED
step NMTOKEN #IMPLIED>
step NMTOKEN #IMPLIED
hidden (true|false) "false">

<!ELEMENT min (#PCDATA)>
<!ATTLIST min alias CDATA #IMPLIED>

<!ELEMENT max (#PCDATA)>
<!ATTLIST max alias CDATA #IMPLIED>

<!ELEMENT requires (#PCDATA)>
<!ATTLIST requires option CDATA #IMPLIED
args CDATA #IMPLIED
stream (stdout|stderr) "stdout">

<!ELEMENT depends (#PCDATA)>
<!ATTLIST depends setting CDATA #REQUIRED
state (enabled|disabled) "enabled"
Expand Down
21 changes: 17 additions & 4 deletions include/boca/application/componentspecs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* BoCA - BonkEnc Component Architecture
* Copyright (C) 2007-2022 Robert Kausch <[email protected]>
* Copyright (C) 2007-2024 Robert Kausch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
Expand All @@ -13,8 +13,7 @@
#ifndef H_BOCA_COMPONENTSPECS
#define H_BOCA_COMPONENTSPECS

#include <smooth.h>
#include "../core/definitions.h"
#include "../common/config.h"

using namespace smooth;
using namespace smooth::System;
Expand Down Expand Up @@ -74,6 +73,13 @@ namespace BoCA
NUM_PARAMETER_TYPES
};

struct ParameterRequirement
{
String option;
String arguments;
Bool useStderr;
};

struct ParameterDependency
{
String setting;
Expand Down Expand Up @@ -276,6 +282,12 @@ namespace BoCA

const Array<ParameterDependency> &GetDependencies() const;
Void AddDependency(const ParameterDependency &);

Bool GetHidden() const;
Void SetHidden(Bool nHidden);

const Array<ParameterRequirement> &GetRequirements() const;
Void AddRequirement(const ParameterRequirement &);
};

class BOCA_DLL_EXPORT ComponentSpecs
Expand All @@ -288,6 +300,7 @@ namespace BoCA
Bool ParseXMLSpec(const String &);

Bool ParseParameters(XML::Node *);
Bool ParseParameterRequirements(Parameter *, XML::Node *);
Bool ParseParameterDependencies(Parameter *, XML::Node *);
public:
String id;
Expand Down Expand Up @@ -333,7 +346,7 @@ namespace BoCA
Bool LoadFromDLL(const String &);
Bool LoadFromXML(const String &);

String GetExternalArgumentsString();
String GetExternalArgumentsString(const Config *);

const char *(*func_GetComponentSpecs)();

Expand Down
2 changes: 2 additions & 0 deletions include/boca/application/external/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace BoCA
class ExternalUtilities
{
public:
static Void CheckParameterRequirements(ComponentSpecs *);

static String GetMD5(const ComponentSpecs *, const String &);
};
};
Expand Down
107 changes: 93 additions & 14 deletions runtime/application/componentspecs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* BoCA - BonkEnc Component Architecture
* Copyright (C) 2007-2022 Robert Kausch <[email protected]>
* Copyright (C) 2007-2024 Robert Kausch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
Expand All @@ -14,6 +14,9 @@
#include <boca/common/config.h>

static Array< Array<String> > formatCompanions;

static Array< Bool > parameterHidden;
static Array< Array<BoCA::ParameterRequirement> > parameterRequirements;
static Array< Array<BoCA::ParameterDependency> > parameterDependencies;

const Array<String> &BoCA::AS::FileFormat::GetCompanionExtensions() const
Expand All @@ -32,6 +35,37 @@ Void BoCA::AS::FileFormat::AddCompanionExtension(const String &nExt)
formatCompanions.GetReference(index).Add(nExt);
}

Bool BoCA::AS::Parameter::GetHidden() const
{
Int index = Number(Int64(this)).ToHexString().ComputeCRC32();

return parameterHidden.Get(index);
}

Void BoCA::AS::Parameter::SetHidden(Bool nHidden)
{
Int index = Number(Int64(this)).ToHexString().ComputeCRC32();

if (!parameterHidden.Set(index, nHidden))
parameterHidden.Add(nHidden, index);
}

const Array<BoCA::ParameterRequirement> &BoCA::AS::Parameter::GetRequirements() const
{
Int index = Number(Int64(this)).ToHexString().ComputeCRC32();

return parameterRequirements.Get(index);
}

Void BoCA::AS::Parameter::AddRequirement(const BoCA::ParameterRequirement &nReq)
{
Int index = Number(Int64(this)).ToHexString().ComputeCRC32();

if (GetRequirements().Length() == 0) parameterRequirements.Add(Array<ParameterRequirement>(), index);

parameterRequirements.GetReference(index).Add(nReq);
}

const Array<BoCA::ParameterDependency> &BoCA::AS::Parameter::GetDependencies() const
{
Int index = Number(Int64(this)).ToHexString().ComputeCRC32();
Expand Down Expand Up @@ -296,33 +330,59 @@ Bool BoCA::AS::ComponentSpecs::LoadFromXML(const String &file)
return ParseXMLSpec(in.InputString(in.Size()).Trim());
}

String BoCA::AS::ComponentSpecs::GetExternalArgumentsString()
String BoCA::AS::ComponentSpecs::GetExternalArgumentsString(const Config *config)
{
Config *config = Config::Get();
/* Get number of threads to use.
*/
Bool enableParallel = config->GetIntValue("Resources", "EnableParallelConversions", True);
Bool enableSuperFast = config->GetIntValue("Resources", "EnableSuperFastMode", True);
Int numberOfThreads = enableParallel && enableSuperFast ? config->GetIntValue("Resources", "NumberOfConversionThreads", 0) : 1;

if (enableParallel && enableSuperFast && numberOfThreads <= 1) numberOfThreads = CPU().GetNumCores() + (CPU().GetNumLogicalCPUs() - CPU().GetNumCores()) / 2;

/* Build arguments string.
*/
String arguments;

foreach (Parameter *param, parameters)
{
String paramName = param->GetName();
Bool paramEnabled = param->GetEnabled();
String paramDefault = param->GetDefault().Replace("%THREADS", String::FromInt(numberOfThreads));

switch (param->GetType())
{
case PARAMETER_TYPE_SWITCH:
if (!config->GetIntValue(id, param->GetName(), param->GetEnabled())) continue;
if (!config->GetIntValue(id, paramName, paramEnabled)) continue;

arguments.Append(param->GetArgument()).Append(" ");

break;
case PARAMETER_TYPE_SELECTION:
if (!config->GetIntValue(id, String("Set ").Append(param->GetName()), param->GetEnabled())) continue;
if (!config->GetIntValue(id, String("Set ").Append(paramName), paramEnabled)) continue;

arguments.Append(param->GetArgument().Replace("%VALUE", config->GetStringValue(id, param->GetName(), param->GetDefault()))).Append(" ");
arguments.Append(param->GetArgument().Replace("%VALUE", config->GetStringValue(id, paramName, paramDefault))).Append(" ");

break;
case PARAMETER_TYPE_RANGE:
if (!config->GetIntValue(id, String("Set ").Append(param->GetName()), param->GetEnabled())) continue;
{
if (!config->GetIntValue(id, String("Set ").Append(paramName), paramEnabled)) continue;

Float value = config->GetIntValue(id, paramName, Math::Round(paramDefault.ToFloat() / param->GetStepSize())) * param->GetStepSize();

Float min = 0;
Float max = 0;

foreach(Option * option, param->GetOptions())
{
if (option->GetType() == OPTION_TYPE_MIN) min = option->GetValue().ToInt();
else if (option->GetType() == OPTION_TYPE_MAX) max = option->GetValue().ToInt();
}

arguments.Append(param->GetArgument().Replace("%VALUE", String::FromFloat(config->GetIntValue(id, param->GetName(), Math::Round(param->GetDefault().ToFloat() / param->GetStepSize())) * param->GetStepSize()))).Append(" ");
arguments.Append(param->GetArgument().Replace("%VALUE", String::FromFloat(Math::Max(min, Math::Min(value, max))))).Append(" ");

break;
}
default:
/* Unsupported parameter type.
*/
Expand Down Expand Up @@ -618,12 +678,11 @@ Bool BoCA::AS::ComponentSpecs::ParseParameters(XML::Node *root)
if (node->GetAttributeByName("name") != NIL) parameter->SetName(node->GetAttributeByName("name")->GetContent());
if (node->GetAttributeByName("argument") != NIL) parameter->SetArgument(node->GetAttributeByName("argument")->GetContent());
if (node->GetAttributeByName("enabled") != NIL) parameter->SetEnabled(node->GetAttributeByName("enabled")->GetContent() == "true" ? True : False);
if (node->GetAttributeByName("hidden") != NIL) parameter->SetHidden(node->GetAttributeByName("hidden")->GetContent() == "true" ? True : False);

if (node->GetName() == "switch")
{
parameter->SetType(PARAMETER_TYPE_SWITCH);

ParseParameterDependencies(parameter, node);
}
else if (node->GetName() == "selection")
{
Expand All @@ -649,8 +708,6 @@ Bool BoCA::AS::ComponentSpecs::ParseParameters(XML::Node *root)
parameter->AddOption(option);
}
}

ParseParameterDependencies(parameter, node);
}
else if (node->GetName() == "range")
{
Expand Down Expand Up @@ -680,16 +737,38 @@ Bool BoCA::AS::ComponentSpecs::ParseParameters(XML::Node *root)
parameter->AddOption(option);
}
}

ParseParameterDependencies(parameter, node);
}

ParseParameterRequirements(parameter, node);
ParseParameterDependencies(parameter, node);

parameters.Add(parameter);
}

return True;
}

Bool BoCA::AS::ComponentSpecs::ParseParameterRequirements(Parameter *parameter, XML::Node *node)
{
for (Int i = 0; i < node->GetNOfNodes(); i++)
{
XML::Node *node2 = node->GetNthNode(i);

if (node2->GetName() == "requires")
{
ParameterRequirement requirement;

requirement.option = node2->GetAttributeByName("option") != NIL ? node2->GetAttributeByName("option")->GetContent() : String();
requirement.arguments = node2->GetAttributeByName("arguments") != NIL ? node2->GetAttributeByName("arguments")->GetContent() : String();
requirement.useStderr = node2->GetAttributeByName("stream") != NIL ? (node2->GetAttributeByName("stream")->GetContent() == "stderr") : False;

if (requirement.option != NIL) parameter->AddRequirement(requirement);
}
}

return True;
}

Bool BoCA::AS::ComponentSpecs::ParseParameterDependencies(Parameter *parameter, XML::Node *node)
{
for (Int i = 0; i < node->GetNOfNodes(); i++)
Expand Down
Loading

0 comments on commit cc5f68b

Please sign in to comment.