Skip to content

Commit

Permalink
tools/builder: Add codec option
Browse files Browse the repository at this point in the history
cc #135
  • Loading branch information
zzag committed Jan 14, 2024
1 parent 249a487 commit e4328e6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lib/kdynamicwallpaperwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class KDynamicWallpaperWriterPrivate
QList<KDynamicWallpaperMetaData> metaData;
std::optional<int> speed;
std::optional<int> maxThreadCount;
avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
};

KDynamicWallpaperWriterPrivate::KDynamicWallpaperWriterPrivate()
Expand Down Expand Up @@ -91,6 +92,7 @@ bool KDynamicWallpaperWriterPrivate::flush(QIODevice *device)

const QByteArray xmp = serializeMetaData(metaData);
avifEncoder *encoder = avifEncoderCreate();
encoder->codecChoice = codecChoice;
encoder->speed = speed.value_or(AVIF_SPEED_DEFAULT);
encoder->maxThreads = maxThreadCount.value_or(QThread::idealThreadCount());
auto encoderCleanup = qScopeGuard([&encoder]() {
Expand Down Expand Up @@ -206,6 +208,38 @@ QList<KDynamicWallpaperWriter::ImageView> KDynamicWallpaperWriter::images() cons
return d->images;
}

/*!
* Sets the codec that will be used to encode the wallpaper images. Returns \c true if successful;
* otherwise returns \c false.
*/
bool KDynamicWallpaperWriter::setCodecName(const QString &codecName)
{
if (const avifCodecChoice choice = avifCodecChoiceFromName(codecName.toLatin1().constData()); choice != AVIF_CODEC_CHOICE_AUTO) {
if (avifCodecName(choice, AVIF_CODEC_FLAG_CAN_ENCODE)) {
d->codecChoice = choice;
return true;
} else {
d->wallpaperWriterError = KDynamicWallpaperWriter::EncoderError;
d->errorString = QStringLiteral("Codec cannot encode");
}
} else {
d->wallpaperWriterError = KDynamicWallpaperWriter::EncoderError;
d->errorString = QStringLiteral("Unrecognized codec: ") + codecName;
}
return false;
}

/*!
* Returns the codec that will be used to encode wallpaper images.
*/
QString KDynamicWallpaperWriter::codecName() const
{
if (d->codecChoice != AVIF_CODEC_CHOICE_AUTO) {
return QString::fromLatin1(avifCodecName(d->codecChoice, AVIF_CODEC_FLAG_CAN_ENCODE));
}
return QString();
}

/*!
* Sets the desired maximum number of threads that can be used during the encoding step.
* If not set, QThread::idealThreadCount() will be used.
Expand Down
3 changes: 3 additions & 0 deletions src/lib/kdynamicwallpaperwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class KDYNAMICWALLPAPER_EXPORT KDynamicWallpaperWriter
void setImages(const QList<ImageView> &views);
QList<ImageView> images() const;

bool setCodecName(const QString &codecName);
QString codecName() const;

bool flush(QIODevice *device);
bool flush(const QString &fileName);

Expand Down
12 changes: 12 additions & 0 deletions src/tools/builder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ int main(int argc, char **argv)
maxThreadsOption.setDescription(i18n("Maximum number of threads that can be used when encoding a wallpaper"));
maxThreadsOption.setValueName(QStringLiteral("max-threads"));

QCommandLineOption codecOption(QStringLiteral("codec"));
codecOption.setDescription(i18n("Codec to use (aom|rav1e|svt)"));
codecOption.setValueName(QStringLiteral("codec"));

QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument(QStringLiteral("json"), i18n("Manifest file to use"));
parser.addOption(outputOption);
parser.addOption(maxThreadsOption);
parser.addOption(speedOption);
parser.addOption(codecOption);
parser.process(app);

if (parser.positionalArguments().count() != 1)
Expand Down Expand Up @@ -75,6 +80,13 @@ int main(int argc, char **argv)
}
}

if (parser.isSet(codecOption)) {
if (!writer.setCodecName(parser.value(codecOption))) {
qWarning() << qPrintable(writer.errorString());
return -1;
}
}

QString targetFileName = parser.value(outputOption);
if (targetFileName.isEmpty())
targetFileName = QStringLiteral("wallpaper.avif");
Expand Down

0 comments on commit e4328e6

Please sign in to comment.